Code

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