Code

Add socket support to check_mysql*
[nagiosplug.git] / plugins / check_ssh.c
1 /*****************************************************************************
2
3 * Nagios check_ssh plugin
4
5 * License: GPL
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
7
8 * Last Modified: $Date$
9
10 * Description:
11
12 * This file contains the check_ssh plugin
13
14 * Try to connect to an SSH server at specified server and port
15
16
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 * GNU General Public License for more details.
26
27 * You should have received a copy of the GNU General Public License
28 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29
30 * $Id$
31
32 *****************************************************************************/
34 const char *progname = "check_ssh";
35 const char *revision = "$Revision$";
36 const char *copyright = "2000-2007";
37 const char *email = "nagiosplug-devel@lists.sourceforge.net";
39 #include "common.h"
40 #include "netutils.h"
41 #include "utils.h"
43 #ifndef MSG_DONTWAIT
44 #define MSG_DONTWAIT 0
45 #endif
47 #define SSH_DFL_PORT    22
48 #define BUFF_SZ         256
50 int port = -1;
51 char *server_name = NULL;
52 char *remote_version = NULL;
53 int verbose = FALSE;
55 int process_arguments (int, char **);
56 int validate_arguments (void);
57 void print_help (void);
58 void print_usage (void);
60 int ssh_connect (char *haddr, int hport, char *remote_version);
64 int
65 main (int argc, char **argv)
66 {
67         int result = STATE_UNKNOWN;
69         setlocale (LC_ALL, "");
70         bindtextdomain (PACKAGE, LOCALEDIR);
71         textdomain (PACKAGE);
73         if (process_arguments (argc, argv) == ERROR)
74                 usage4 (_("Could not parse arguments"));
76         /* initialize alarm signal handling */
77         signal (SIGALRM, socket_timeout_alarm_handler);
78         
79         alarm (socket_timeout);
81         /* ssh_connect exits if error is found */
82         result = ssh_connect (server_name, port, remote_version);
84         alarm (0);
86         return (result);
87 }
91 /* process command-line arguments */
92 int
93 process_arguments (int argc, char **argv)
94 {
95         int c;
97         int option = 0;
98         static struct option longopts[] = {
99                 {"help", no_argument, 0, 'h'},
100                 {"version", no_argument, 0, 'V'},
101                 {"host", required_argument, 0, 'H'},    /* backward compatibility */
102                 {"hostname", required_argument, 0, 'H'},
103                 {"port", required_argument, 0, 'p'},
104                 {"use-ipv4", no_argument, 0, '4'},
105                 {"use-ipv6", no_argument, 0, '6'},
106                 {"timeout", required_argument, 0, 't'},
107                 {"verbose", no_argument, 0, 'v'},
108                 {"remote-version", required_argument, 0, 'r'},
109                 {0, 0, 0, 0}
110         };
112         if (argc < 2)
113                 return ERROR;
115         for (c = 1; c < argc; c++)
116                 if (strcmp ("-to", argv[c]) == 0)
117                         strcpy (argv[c], "-t");
119         while (1) {
120                 c = getopt_long (argc, argv, "+Vhv46t:r:H:p:", longopts, &option);
122                 if (c == -1 || c == EOF)
123                         break;
125                 switch (c) {
126                 case '?':                                                                       /* help */
127                         usage5 ();
128                 case 'V':                                                                       /* version */
129                         print_revision (progname, revision);
130                         exit (STATE_OK);
131                 case 'h':                                                                       /* help */
132                         print_help ();
133                         exit (STATE_OK);
134                 case 'v':                                                                       /* verbose */
135                         verbose = TRUE;
136                         break;
137                 case 't':                                                                       /* timeout period */
138                         if (!is_integer (optarg))
139                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
140                         else
141                                 socket_timeout = atoi (optarg);
142                         break;
143                 case '4':
144                         address_family = AF_INET;
145                         break;
146                 case '6':
147 #ifdef USE_IPV6
148                         address_family = AF_INET6;
149 #else
150                         usage4 (_("IPv6 support not available"));
151 #endif
152                         break;
153                 case 'r':                                                                       /* remote version */
154                         remote_version = optarg;
155                         break;
156                 case 'H':                                                                       /* host */
157                         if (is_host (optarg) == FALSE)
158                                 usage2 (_("Invalid hostname/address"), optarg);
159                         server_name = optarg;
160                         break;
161                 case 'p':                                                                       /* port */
162                         if (is_intpos (optarg)) {
163                                 port = atoi (optarg);
164                         }
165                         else {
166                                 usage2 (_("Port number must be a positive integer"), optarg);
167                         }
168                 }
169         }
171         c = optind;
172         if (server_name == NULL && c < argc) {
173                 if (is_host (argv[c])) {
174                         server_name = argv[c++];
175                 }
176         }
178         if (port == -1 && c < argc) {
179                 if (is_intpos (argv[c])) {
180                         port = atoi (argv[c++]);
181                 }
182                 else {
183                         print_usage ();
184                         exit (STATE_UNKNOWN);
185                 }
186         }
188         return validate_arguments ();
191 int
192 validate_arguments (void)
194         if (server_name == NULL)
195                 return ERROR;
196         if (port == -1)                                                         /* funky, but allows -p to override stray integer in args */
197                 port = SSH_DFL_PORT;
198         return OK;
202 /************************************************************************
204 * Try to connect to SSH server at specified server and port
206 *-----------------------------------------------------------------------*/
209 int
210 ssh_connect (char *haddr, int hport, char *remote_version)
212         int sd;
213         int result;
214         char *output = NULL;
215         char *buffer = NULL;
216         char *ssh_proto = NULL;
217         char *ssh_server = NULL;
218         char rev_no[20];
220         sscanf ("$Revision$", "$Revision: %[0123456789.]", rev_no);
222         result = my_tcp_connect (haddr, hport, &sd);
224         if (result != STATE_OK)
225                 return result;
227         output = (char *) malloc (BUFF_SZ + 1);
228         memset (output, 0, BUFF_SZ + 1);
229         recv (sd, output, BUFF_SZ, 0);
230         if (strncmp (output, "SSH", 3)) {
231                 printf (_("Server answer: %s"), output);
232                 close(sd);
233                 exit (STATE_CRITICAL);
234         }
235         else {
236                 strip (output);
237                 if (verbose)
238                         printf ("%s\n", output);
239                 ssh_proto = output + 4;
240                 ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
241                 ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
243                 asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
244                 send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
245                 if (verbose)
246                         printf ("%s\n", buffer);
248                 if (remote_version && strcmp(remote_version, ssh_server)) {
249                         printf
250                                 (_("SSH WARNING - %s (protocol %s) version mismatch, expected '%s'\n"),
251                                  ssh_server, ssh_proto, remote_version);
252                         close(sd);
253                         exit (STATE_WARNING);
254                 }
255                 
256                 printf
257                         (_("SSH OK - %s (protocol %s)\n"),
258                          ssh_server, ssh_proto);
259                 close(sd);
260                 exit (STATE_OK);
261         }
266 void
267 print_help (void)
269         char *myport;
270         asprintf (&myport, "%d", SSH_DFL_PORT);
272         print_revision (progname, revision);
274         printf ("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n");
275         printf (COPYRIGHT, copyright, email);
277         printf ("%s\n", _("Try to connect to an SSH server at specified server and port"));
279   printf ("\n\n");
281         print_usage ();
283         printf (_(UT_HELP_VRSN));
285         printf (_(UT_HOST_PORT), 'p', myport);
287         printf (_(UT_IPv46));
289         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
291         printf (" %s\n", "-r, --remote-version=STRING");
292   printf ("    %s\n", _("Warn if string doesn't match expected server version (ex: OpenSSH_3.9p1)"));
293         
294         printf (_(UT_VERBOSE));
296         printf (_(UT_SUPPORT));
301 void
302 print_usage (void)
304   printf (_("Usage:"));
305         printf ("%s [-46] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n", progname);