Code

cleaning help and usage + license
[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 * License Information:
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 * $Id$
31
32 ******************************************************************************/
34 const char *progname = "check_ssh";
35 const char *revision = "$Revision$";
36 const char *copyright = "2000-2006";
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'},
102                 {"port", required_argument, 0, 'p'},
103                 {"use-ipv4", no_argument, 0, '4'},
104                 {"use-ipv6", no_argument, 0, '6'},
105                 {"timeout", required_argument, 0, 't'},
106                 {"verbose", no_argument, 0, 'v'},
107                 {"remote-version", required_argument, 0, 'r'},
108                 {0, 0, 0, 0}
109         };
111         if (argc < 2)
112                 return ERROR;
114         for (c = 1; c < argc; c++)
115                 if (strcmp ("-to", argv[c]) == 0)
116                         strcpy (argv[c], "-t");
118         while (1) {
119                 c = getopt_long (argc, argv, "+Vhv46t:r:H:p:", longopts, &option);
121                 if (c == -1 || c == EOF)
122                         break;
124                 switch (c) {
125                 case '?':                                                                       /* help */
126                         usage2 (_("Unknown argument"), optarg);
127                 case 'V':                                                                       /* version */
128                         print_revision (progname, revision);
129                         exit (STATE_OK);
130                 case 'h':                                                                       /* help */
131                         print_help ();
132                         exit (STATE_OK);
133                 case 'v':                                                                       /* verbose */
134                         verbose = TRUE;
135                         break;
136                 case 't':                                                                       /* timeout period */
137                         if (!is_integer (optarg))
138                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
139                         else
140                                 socket_timeout = atoi (optarg);
141                         break;
142                 case '4':
143                         address_family = AF_INET;
144                         break;
145                 case '6':
146 #ifdef USE_IPV6
147                         address_family = AF_INET6;
148 #else
149                         usage4 (_("IPv6 support not available"));
150 #endif
151                         break;
152                 case 'r':                                                                       /* remote version */
153                         remote_version = optarg;
154                         break;
155                 case 'H':                                                                       /* host */
156                         if (is_host (optarg) == FALSE)
157                                 usage2 (_("Invalid hostname/address"), optarg);
158                         server_name = optarg;
159                         break;
160                 case 'p':                                                                       /* port */
161                         if (is_intpos (optarg)) {
162                                 port = atoi (optarg);
163                         }
164                         else {
165                                 usage2 (_("Port number must be a positive integer"), optarg);
166                         }
167                 }
168         }
170         c = optind;
171         if (server_name == NULL && c < argc) {
172                 if (is_host (argv[c])) {
173                         server_name = argv[c++];
174                 }
175         }
177         if (port == -1 && c < argc) {
178                 if (is_intpos (argv[c])) {
179                         port = atoi (argv[c++]);
180                 }
181                 else {
182                         print_usage ();
183                         exit (STATE_UNKNOWN);
184                 }
185         }
187         return validate_arguments ();
190 int
191 validate_arguments (void)
193         if (server_name == NULL)
194                 return ERROR;
195         if (port == -1)                                                         /* funky, but allows -p to override stray integer in args */
196                 port = SSH_DFL_PORT;
197         return OK;
201 /************************************************************************
203 * Try to connect to SSH server at specified server and port
205 *-----------------------------------------------------------------------*/
208 int
209 ssh_connect (char *haddr, int hport, char *remote_version)
211         int sd;
212         int result;
213         char *output = NULL;
214         char *buffer = NULL;
215         char *ssh_proto = NULL;
216         char *ssh_server = NULL;
217         char rev_no[20];
219         sscanf ("$Revision$", "$Revision: %[0123456789.]", rev_no);
221         result = my_tcp_connect (haddr, hport, &sd);
223         if (result != STATE_OK)
224                 return result;
226         output = (char *) malloc (BUFF_SZ + 1);
227         memset (output, 0, BUFF_SZ + 1);
228         recv (sd, output, BUFF_SZ, 0);
229         if (strncmp (output, "SSH", 3)) {
230                 printf (_("Server answer: %s"), output);
231                 exit (STATE_CRITICAL);
232         }
233         else {
234                 strip (output);
235                 if (verbose)
236                         printf ("%s\n", output);
237                 ssh_proto = output + 4;
238                 ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
239                 ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
241                 asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
242                 send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
243                 if (verbose)
244                         printf ("%s\n", buffer);
246                 if (remote_version && strcmp(remote_version, ssh_server)) {
247                         printf
248                                 (_("SSH WARNING - %s (protocol %s) version mismatch, expected '%s'\n"),
249                                  ssh_server, ssh_proto, remote_version);
250                         exit (STATE_WARNING);
251                 }
252                 
253                 printf
254                         (_("SSH OK - %s (protocol %s)\n"),
255                          ssh_server, ssh_proto);
256                 close(sd);
257                 exit (STATE_OK);
258         }
263 void
264 print_help (void)
266         char *myport;
267         asprintf (&myport, "%d", SSH_DFL_PORT);
269         print_revision (progname, revision);
271         printf ("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n");
272         printf (COPYRIGHT, copyright, email);
274         printf ("%s\n", _("Try to connect to an SSH server at specified server and port"));
276   printf ("\n\n");
278         print_usage ();
280         printf (_(UT_HELP_VRSN));
282         printf (_(UT_HOST_PORT), 'p', myport);
284         printf (_(UT_IPv46));
286         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
288         printf (" %s\n" "-r, --remote-version=STRING");
289   printf ("    %s\n", _("Warn if string doesn't match expected server version (ex: OpenSSH_3.9p1)"));
290         
291         printf (_(UT_VERBOSE));
293         printf (_(UT_SUPPORT));
298 void
299 print_usage (void)
301   printf (_("Usage:"));
302         printf ("%s [-46] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n", progname);