Code

Added support for --extra-opts in all C plugins (disabled by default, see configure...
[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         /* Parse extra opts if any */
74         argv=np_extra_opts (&argc, argv, progname);
76         if (process_arguments (argc, argv) == ERROR)
77                 usage4 (_("Could not parse arguments"));
79         /* initialize alarm signal handling */
80         signal (SIGALRM, socket_timeout_alarm_handler);
81         
82         alarm (socket_timeout);
84         /* ssh_connect exits if error is found */
85         result = ssh_connect (server_name, port, remote_version);
87         alarm (0);
89         return (result);
90 }
94 /* process command-line arguments */
95 int
96 process_arguments (int argc, char **argv)
97 {
98         int c;
100         int option = 0;
101         static struct option longopts[] = {
102                 {"help", no_argument, 0, 'h'},
103                 {"version", no_argument, 0, 'V'},
104                 {"host", required_argument, 0, 'H'},    /* backward compatibility */
105                 {"hostname", required_argument, 0, 'H'},
106                 {"port", required_argument, 0, 'p'},
107                 {"use-ipv4", no_argument, 0, '4'},
108                 {"use-ipv6", no_argument, 0, '6'},
109                 {"timeout", required_argument, 0, 't'},
110                 {"verbose", no_argument, 0, 'v'},
111                 {"remote-version", required_argument, 0, 'r'},
112                 {0, 0, 0, 0}
113         };
115         if (argc < 2)
116                 return ERROR;
118         for (c = 1; c < argc; c++)
119                 if (strcmp ("-to", argv[c]) == 0)
120                         strcpy (argv[c], "-t");
122         while (1) {
123                 c = getopt_long (argc, argv, "+Vhv46t:r:H:p:", longopts, &option);
125                 if (c == -1 || c == EOF)
126                         break;
128                 switch (c) {
129                 case '?':                                                                       /* help */
130                         usage5 ();
131                 case 'V':                                                                       /* version */
132                         print_revision (progname, revision);
133                         exit (STATE_OK);
134                 case 'h':                                                                       /* help */
135                         print_help ();
136                         exit (STATE_OK);
137                 case 'v':                                                                       /* verbose */
138                         verbose = TRUE;
139                         break;
140                 case 't':                                                                       /* timeout period */
141                         if (!is_integer (optarg))
142                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
143                         else
144                                 socket_timeout = atoi (optarg);
145                         break;
146                 case '4':
147                         address_family = AF_INET;
148                         break;
149                 case '6':
150 #ifdef USE_IPV6
151                         address_family = AF_INET6;
152 #else
153                         usage4 (_("IPv6 support not available"));
154 #endif
155                         break;
156                 case 'r':                                                                       /* remote version */
157                         remote_version = optarg;
158                         break;
159                 case 'H':                                                                       /* host */
160                         if (is_host (optarg) == FALSE)
161                                 usage2 (_("Invalid hostname/address"), optarg);
162                         server_name = optarg;
163                         break;
164                 case 'p':                                                                       /* port */
165                         if (is_intpos (optarg)) {
166                                 port = atoi (optarg);
167                         }
168                         else {
169                                 usage2 (_("Port number must be a positive integer"), optarg);
170                         }
171                 }
172         }
174         c = optind;
175         if (server_name == NULL && c < argc) {
176                 if (is_host (argv[c])) {
177                         server_name = argv[c++];
178                 }
179         }
181         if (port == -1 && c < argc) {
182                 if (is_intpos (argv[c])) {
183                         port = atoi (argv[c++]);
184                 }
185                 else {
186                         print_usage ();
187                         exit (STATE_UNKNOWN);
188                 }
189         }
191         return validate_arguments ();
194 int
195 validate_arguments (void)
197         if (server_name == NULL)
198                 return ERROR;
199         if (port == -1)                                                         /* funky, but allows -p to override stray integer in args */
200                 port = SSH_DFL_PORT;
201         return OK;
205 /************************************************************************
207 * Try to connect to SSH server at specified server and port
209 *-----------------------------------------------------------------------*/
212 int
213 ssh_connect (char *haddr, int hport, char *remote_version)
215         int sd;
216         int result;
217         char *output = NULL;
218         char *buffer = NULL;
219         char *ssh_proto = NULL;
220         char *ssh_server = NULL;
221         char rev_no[20];
223         sscanf ("$Revision$", "$Revision: %[0123456789.]", rev_no);
225         result = my_tcp_connect (haddr, hport, &sd);
227         if (result != STATE_OK)
228                 return result;
230         output = (char *) malloc (BUFF_SZ + 1);
231         memset (output, 0, BUFF_SZ + 1);
232         recv (sd, output, BUFF_SZ, 0);
233         if (strncmp (output, "SSH", 3)) {
234                 printf (_("Server answer: %s"), output);
235                 close(sd);
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                         close(sd);
256                         exit (STATE_WARNING);
257                 }
258                 
259                 printf
260                         (_("SSH OK - %s (protocol %s)\n"),
261                          ssh_server, ssh_proto);
262                 close(sd);
263                 exit (STATE_OK);
264         }
269 void
270 print_help (void)
272         char *myport;
273         asprintf (&myport, "%d", SSH_DFL_PORT);
275         print_revision (progname, revision);
277         printf ("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n");
278         printf (COPYRIGHT, copyright, email);
280         printf ("%s\n", _("Try to connect to an SSH server at specified server and port"));
282   printf ("\n\n");
284         print_usage ();
286         printf (_(UT_HELP_VRSN));
287         printf (_(UT_EXTRA_OPTS));
289         printf (_(UT_HOST_PORT), 'p', myport);
291         printf (_(UT_IPv46));
293         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
295         printf (" %s\n", "-r, --remote-version=STRING");
296   printf ("    %s\n", _("Warn if string doesn't match expected server version (ex: OpenSSH_3.9p1)"));
297         
298         printf (_(UT_VERBOSE));
300 #ifdef NP_EXTRA_OPTS
301         printf ("\n");
302         printf ("%s\n", _("Notes:"));
303         printf (_(UT_EXTRA_OPTS_NOTES));
304 #endif
306         printf (_(UT_SUPPORT));
311 void
312 print_usage (void)
314   printf (_("Usage:"));
315         printf ("%s [-46] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n", progname);