ce4c08482f1b47cab0452ce686a8e6f21e6a0353
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);
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'},
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 usage2 (_("Unknown argument"), optarg);
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 ();
192 }
194 int
195 validate_arguments (void)
196 {
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;
202 }
205 /************************************************************************
206 *
207 * Try to connect to SSH server at specified server and port
208 *
209 *-----------------------------------------------------------------------*/
212 int
213 ssh_connect (char *haddr, int hport, char *remote_version)
214 {
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 exit (STATE_CRITICAL);
236 }
237 else {
238 strip (output);
239 if (verbose)
240 printf ("%s\n", output);
241 ssh_proto = output + 4;
242 ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
243 ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
245 asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
246 send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
247 if (verbose)
248 printf ("%s\n", buffer);
250 if (remote_version && strcmp(remote_version, ssh_server)) {
251 printf
252 (_("SSH WARNING - %s (protocol %s) version mismatch, expected '%s'\n"),
253 ssh_server, ssh_proto, remote_version);
254 exit (STATE_WARNING);
255 }
257 printf
258 (_("SSH OK - %s (protocol %s)\n"),
259 ssh_server, ssh_proto);
260 close(sd);
261 exit (STATE_OK);
262 }
263 }
267 void
268 print_help (void)
269 {
270 char *myport;
271 asprintf (&myport, "%d", SSH_DFL_PORT);
273 print_revision (progname, revision);
275 printf ("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n");
276 printf (COPYRIGHT, copyright, email);
278 printf ("%s\n", _("Try to connect to an SSH server at specified server and port"));
280 printf ("\n\n");
282 print_usage ();
284 printf (_(UT_HELP_VRSN));
286 printf (_(UT_HOST_PORT), 'p', myport);
288 printf (_(UT_IPv46));
290 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
292 printf (" %s\n", "-r, --remote-version=STRING");
293 printf (" %s\n", _("Warn if string doesn't match expected server version (ex: OpenSSH_3.9p1)"));
295 printf (_(UT_VERBOSE));
297 printf (_(UT_SUPPORT));
298 }
302 void
303 print_usage (void)
304 {
305 printf (_("Usage:"));
306 printf ("%s [-46] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n", progname);
307 }