eb31774d1fa4f01f54b9f6aec5c38014f5449db1
1 /*****************************************************************************
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 *****************************************************************************/
18 #include "config.h"
19 #include "common.h"
20 #include "netutils.h"
21 #include "utils.h"
23 #ifdef HAVE_SSL_H
24 # include <rsa.h>
25 # include <crypto.h>
26 # include <x509.h>
27 # include <pem.h>
28 # include <ssl.h>
29 # include <err.h>
30 #else
31 # ifdef HAVE_OPENSSL_SSL_H
32 # include <openssl/rsa.h>
33 # include <openssl/crypto.h>
34 # include <openssl/x509.h>
35 # include <openssl/pem.h>
36 # include <openssl/ssl.h>
37 # include <openssl/err.h>
38 # endif
39 #endif
41 #ifdef HAVE_SSL
42 SSL_CTX *ctx;
43 SSL *ssl;
44 int connect_SSL (void);
45 #endif
47 enum {
48 TCP_PROTOCOL = 1,
49 UDP_PROTOCOL = 2,
50 MAXBUF = 1024
51 };
53 int process_arguments (int, char **);
54 void print_usage (void);
55 void print_help (void);
56 int my_recv (void);
58 char *SERVICE = NULL;
59 char *SEND = NULL;
60 char *EXPECT = NULL;
61 char *QUIT = NULL;
62 int PROTOCOL = 0;
63 int PORT = 0;
65 int server_port = 0;
66 char *server_address = NULL;
67 char *server_send = NULL;
68 char *server_quit = NULL;
69 char **server_expect = NULL;
70 int server_expect_count = 0;
71 int maxbytes = 0;
72 char **warn_codes = NULL;
73 int warn_codes_count = 0;
74 char **crit_codes = NULL;
75 int crit_codes_count = 0;
76 int delay = 0;
77 double warning_time = 0;
78 int check_warning_time = FALSE;
79 double critical_time = 0;
80 int check_critical_time = FALSE;
81 double elapsed_time = 0;
82 int verbose = FALSE;
83 int use_ssl = FALSE;
84 int sd = 0;
85 char *buffer = "";
87 /* progname changes depending on symlink called */
88 char *progname = "check_tcp";
89 const char *revision = "$Revision$";
90 const char *copyright = "2002-2003";
91 const char *email = "nagiosplug-devel@lists.sourceforge.net";
92 \f
96 void
97 print_usage (void)
98 {
99 printf (_("\
100 Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\
101 [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
102 [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
103 [-r <refuse state>] [-v] [-4|-6]\n"), progname);
104 printf (" %s (-h|--help)\n", progname);
105 printf (" %s (-V|--version)\n", progname);
106 }
107 void
108 print_help (void)
109 {
110 print_revision (progname, revision);
112 printf (_(COPYRIGHT), copyright, email);
114 printf (_("This plugin tests %s connections with the specified host.\n\n"),
115 SERVICE);
117 print_usage ();
119 printf (_(HELP_VRSN));
121 printf (_(HOST_PORT_46), 'p', "none");
123 printf (_("\
124 -s, --send=STRING\n\
125 String to send to the server\n\
126 -e, --expect=STRING\n\
127 String to expect in server response\n\
128 -q, --quit=STRING\n\
129 String to send server to initiate a clean close of the connection\n"));
131 printf (_("\
132 -r, --refuse=ok|warn|crit\n\
133 Accept tcp refusals with states ok, warn, crit (default: crit)\n\
134 -m, --maxbytes=INTEGER\n\
135 Close connection once more than this number of bytes are received\n\
136 -d, --delay=INTEGER\n\
137 Seconds to wait between sending string and polling for response\n"));
139 printf (_(WARN_CRIT_TO), DEFAULT_SOCKET_TIMEOUT);
141 printf (_(VRBS));
143 support ();
144 }
145 \f
148 int
149 main (int argc, char **argv)
150 {
151 int result;
152 int i;
153 char *status = "";
154 struct timeval tv;
156 setlocale (LC_ALL, "");
157 bindtextdomain (PACKAGE, LOCALEDIR);
158 textdomain (PACKAGE);
160 if (strstr (argv[0], "check_udp")) {
161 progname = strdup ("check_udp");
162 SERVICE = strdup ("UDP");
163 SEND = NULL;
164 EXPECT = NULL;
165 QUIT = NULL;
166 PROTOCOL = UDP_PROTOCOL;
167 PORT = 0;
168 }
169 else if (strstr (argv[0], "check_tcp")) {
170 progname = strdup ("check_tcp");
171 SERVICE = strdup ("TCP");
172 SEND = NULL;
173 EXPECT = NULL;
174 QUIT = NULL;
175 PROTOCOL = TCP_PROTOCOL;
176 PORT = 0;
177 }
178 else if (strstr (argv[0], "check_ftp")) {
179 progname = strdup ("check_ftp");
180 SERVICE = strdup ("FTP");
181 SEND = NULL;
182 EXPECT = strdup ("220");
183 QUIT = strdup ("QUIT\r\n");
184 PROTOCOL = TCP_PROTOCOL;
185 PORT = 21;
186 }
187 else if (strstr (argv[0], "check_smtp")) {
188 progname = strdup ("check_smtp");
189 SERVICE = strdup ("SMTP");
190 SEND = NULL;
191 EXPECT = strdup ("220");
192 QUIT = strdup ("QUIT\r\n");
193 PROTOCOL = TCP_PROTOCOL;
194 PORT = 25;
195 }
196 else if (strstr (argv[0], "check_pop")) {
197 progname = strdup ("check_pop");
198 SERVICE = strdup ("POP");
199 SEND = NULL;
200 EXPECT = strdup ("+OK");
201 QUIT = strdup ("QUIT\r\n");
202 PROTOCOL = TCP_PROTOCOL;
203 PORT = 110;
204 }
205 else if (strstr (argv[0], "check_imap")) {
206 progname = strdup ("check_imap");
207 SERVICE = strdup ("IMAP");
208 SEND = NULL;
209 EXPECT = strdup ("* OK");
210 QUIT = strdup ("a1 LOGOUT\r\n");
211 PROTOCOL = TCP_PROTOCOL;
212 PORT = 143;
213 }
214 #ifdef HAVE_SSL
215 else if (strstr(argv[0],"check_simap")) {
216 progname = strdup ("check_simap");
217 SERVICE = strdup ("SIMAP");
218 SEND=NULL;
219 EXPECT = strdup ("* OK");
220 QUIT = strdup ("a1 LOGOUT\r\n");
221 PROTOCOL=TCP_PROTOCOL;
222 use_ssl=TRUE;
223 PORT=993;
224 }
225 else if (strstr(argv[0],"check_spop")) {
226 progname = strdup ("check_spop");
227 SERVICE = strdup ("SPOP");
228 SEND=NULL;
229 EXPECT = strdup ("+OK");
230 QUIT = strdup ("QUIT\r\n");
231 PROTOCOL=TCP_PROTOCOL;
232 use_ssl=TRUE;
233 PORT=995;
234 }
235 #endif
236 else if (strstr (argv[0], "check_nntp")) {
237 progname = strdup ("check_nntp");
238 SERVICE = strdup ("NNTP");
239 SEND = NULL;
240 EXPECT = NULL;
241 server_expect = realloc (server_expect, ++server_expect_count);
242 asprintf (&server_expect[server_expect_count - 1], "200");
243 server_expect = realloc (server_expect, ++server_expect_count);
244 asprintf (&server_expect[server_expect_count - 1], "201");
245 asprintf (&QUIT, "QUIT\r\n");
246 PROTOCOL = TCP_PROTOCOL;
247 PORT = 119;
248 }
249 else {
250 usage (_("ERROR: Generic check_tcp called with unknown service\n"));
251 }
253 server_address = strdup ("127.0.0.1");
254 server_port = PORT;
255 server_send = SEND;
256 server_quit = QUIT;
258 if (process_arguments (argc, argv) == ERROR)
259 usage (_("Could not parse arguments\n"));
261 /* use default expect if none listed in process_arguments() */
262 if (EXPECT && server_expect_count == 0) {
263 server_expect = malloc (++server_expect_count);
264 server_expect[server_expect_count - 1] = EXPECT;
265 }
267 /* initialize alarm signal handling */
268 signal (SIGALRM, socket_timeout_alarm_handler);
270 /* set socket timeout */
271 alarm (socket_timeout);
273 /* try to connect to the host at the given port number */
274 gettimeofday (&tv, NULL);
275 #ifdef HAVE_SSL
276 if (use_ssl)
277 result = connect_SSL ();
278 else
279 #endif
280 {
281 if (PROTOCOL == UDP_PROTOCOL)
282 result = my_udp_connect (server_address, server_port, &sd);
283 else
284 /* default is TCP */
285 result = my_tcp_connect (server_address, server_port, &sd);
286 }
288 if (result == STATE_CRITICAL)
289 return STATE_CRITICAL;
291 if (server_send != NULL) { /* Something to send? */
292 asprintf (&server_send, "%s\r\n", server_send);
293 #ifdef HAVE_SSL
294 if (use_ssl)
295 SSL_write(ssl, server_send, strlen (server_send));
296 else
297 #endif
298 send (sd, server_send, strlen (server_send), 0);
299 }
301 if (delay > 0) {
302 tv.tv_sec += delay;
303 sleep (delay);
304 }
306 if (server_send || server_expect_count > 0) {
308 buffer = malloc (MAXBUF);
309 memset (buffer, '\0', MAXBUF);
310 /* watch for the expect string */
311 while ((i = my_recv ()) > 0) {
312 buffer[i] = '\0';
313 asprintf (&status, "%s%s", status, buffer);
314 if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
315 break;
316 if (maxbytes>0 && strlen(status) >= (unsigned)maxbytes)
317 break;
318 }
320 /* return a CRITICAL status if we couldn't read any data */
321 if (status == NULL)
322 terminate (STATE_CRITICAL, _("No data received from host\n"));
324 strip (status);
326 if (status && verbose)
327 printf ("%s\n", status);
329 if (server_expect_count > 0) {
330 for (i = 0;; i++) {
331 if (verbose)
332 printf ("%d %d\n", i, server_expect_count);
333 if (i >= server_expect_count)
334 terminate (STATE_WARNING, _("Invalid response from host\n"));
335 if (strstr (status, server_expect[i]))
336 break;
337 }
338 }
339 }
341 if (server_quit != NULL) {
342 #ifdef HAVE_SSL
343 if (use_ssl) {
344 SSL_write (ssl, server_quit, strlen (server_quit));
345 SSL_shutdown (ssl);
346 SSL_free (ssl);
347 SSL_CTX_free (ctx);
348 }
349 else {
350 #endif
351 send (sd, server_quit, strlen (server_quit), 0);
352 #ifdef HAVE_SSL
353 }
354 #endif
355 }
357 /* close the connection */
358 if (sd)
359 close (sd);
361 elapsed_time = delta_time (tv);
363 if (check_critical_time == TRUE && elapsed_time > critical_time)
364 result = STATE_CRITICAL;
365 else if (check_warning_time == TRUE && elapsed_time > warning_time)
366 result = STATE_WARNING;
368 /* reset the alarm */
369 alarm (0);
371 printf
372 (_("%s %s%s - %.3f second response time on port %d"),
373 SERVICE,
374 state_text (result),
375 (was_refused) ? " (refused)" : "",
376 elapsed_time, server_port);
378 if (status && strlen(status) > 0)
379 printf (" [%s]", status);
381 printf ("|time=%.3f\n", elapsed_time);
383 return result;
384 }
385 \f
389 /* process command-line arguments */
390 int
391 process_arguments (int argc, char **argv)
392 {
393 int c;
395 int option_index = 0;
396 static struct option long_options[] = {
397 {"hostname", required_argument, 0, 'H'},
398 {"critical-time", required_argument, 0, 'c'},
399 {"warning-time", required_argument, 0, 'w'},
400 {"critical-codes", required_argument, 0, 'C'},
401 {"warning-codes", required_argument, 0, 'W'},
402 {"timeout", required_argument, 0, 't'},
403 {"protocol", required_argument, 0, 'P'},
404 {"port", required_argument, 0, 'p'},
405 {"send", required_argument, 0, 's'},
406 {"expect", required_argument, 0, 'e'},
407 {"maxbytes", required_argument, 0, 'm'},
408 {"quit", required_argument, 0, 'q'},
409 {"delay", required_argument, 0, 'd'},
410 {"refuse", required_argument, 0, 'r'},
411 {"use-ipv4", no_argument, 0, '4'},
412 {"use-ipv6", no_argument, 0, '6'},
413 {"verbose", no_argument, 0, 'v'},
414 {"version", no_argument, 0, 'V'},
415 {"help", no_argument, 0, 'h'},
416 {0, 0, 0, 0}
417 };
419 if (argc < 2)
420 usage ("No arguments found\n");
422 /* backwards compatibility */
423 for (c = 1; c < argc; c++) {
424 if (strcmp ("-to", argv[c]) == 0)
425 strcpy (argv[c], "-t");
426 else if (strcmp ("-wt", argv[c]) == 0)
427 strcpy (argv[c], "-w");
428 else if (strcmp ("-ct", argv[c]) == 0)
429 strcpy (argv[c], "-c");
430 }
432 if (!is_option (argv[1])) {
433 server_address = argv[1];
434 argv[1] = argv[0];
435 argv = &argv[1];
436 argc--;
437 }
439 while (1) {
440 c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:",
441 long_options, &option_index);
443 if (c == -1 || c == EOF || c == 1)
444 break;
446 switch (c) {
447 case '?': /* print short usage statement if args not parsable */
448 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
449 print_usage ();
450 exit (STATE_UNKNOWN);
451 case 'h': /* help */
452 print_help ();
453 exit (STATE_OK);
454 case 'V': /* version */
455 print_revision (progname, "$Revision$");
456 exit (STATE_OK);
457 case 'v': /* verbose mode */
458 verbose = TRUE;
459 break;
460 case '4':
461 address_family = AF_INET;
462 break;
463 case '6':
464 #ifdef USE_IPV6
465 address_family = AF_INET6;
466 #else
467 usage (_("IPv6 support not available\n"));
468 #endif
469 break;
470 case 'H': /* hostname */
471 if (is_host (optarg) == FALSE)
472 usage2 (_("invalid host name or address"), optarg);
473 server_address = optarg;
474 break;
475 case 'c': /* critical */
476 if (!is_intnonneg (optarg))
477 usage (_("Critical threshold must be a nonnegative integer\n"));
478 critical_time = strtod (optarg, NULL);
479 check_critical_time = TRUE;
480 break;
481 case 'w': /* warning */
482 if (!is_intnonneg (optarg))
483 usage (_("Warning threshold must be a nonnegative integer\n"));
484 warning_time = strtod (optarg, NULL);
485 check_warning_time = TRUE;
486 break;
487 case 'C':
488 crit_codes = realloc (crit_codes, ++crit_codes_count);
489 crit_codes[crit_codes_count - 1] = optarg;
490 break;
491 case 'W':
492 warn_codes = realloc (warn_codes, ++warn_codes_count);
493 warn_codes[warn_codes_count - 1] = optarg;
494 break;
495 case 't': /* timeout */
496 if (!is_intpos (optarg))
497 usage (_("Timeout interval must be a positive integer\n"));
498 socket_timeout = atoi (optarg);
499 break;
500 case 'p': /* port */
501 if (!is_intpos (optarg))
502 usage (_("Server port must be a positive integer\n"));
503 server_port = atoi (optarg);
504 break;
505 case 's':
506 server_send = optarg;
507 break;
508 case 'e': /* expect string (may be repeated) */
509 EXPECT = NULL;
510 if (server_expect_count == 0)
511 server_expect = malloc (++server_expect_count);
512 else
513 server_expect = realloc (server_expect, ++server_expect_count);
514 server_expect[server_expect_count - 1] = optarg;
515 break;
516 case 'm':
517 if (!is_intpos (optarg))
518 usage (_("Maxbytes must be a positive integer\n"));
519 maxbytes = atoi (optarg);
520 case 'q':
521 server_quit = optarg;
522 break;
523 case 'r':
524 if (!strncmp(optarg,"ok",2))
525 econn_refuse_state = STATE_OK;
526 else if (!strncmp(optarg,"warn",4))
527 econn_refuse_state = STATE_WARNING;
528 else if (!strncmp(optarg,"crit",4))
529 econn_refuse_state = STATE_CRITICAL;
530 else
531 usage (_("Refuse mut be one of ok, warn, crit\n"));
532 break;
533 case 'd':
534 if (is_intpos (optarg))
535 delay = atoi (optarg);
536 else
537 usage (_("Delay must be a positive integer\n"));
538 break;
539 case 'S':
540 #ifndef HAVE_SSL
541 terminate (STATE_UNKNOWN,
542 _("SSL support not available. Install OpenSSL and recompile."));
543 #endif
544 use_ssl = TRUE;
545 break;
546 }
547 }
549 if (server_address == NULL)
550 usage (_("You must provide a server address\n"));
552 return OK;
553 }
554 \f
556 #ifdef HAVE_SSL
557 int
558 connect_SSL (void)
559 {
560 SSL_METHOD *meth;
562 /* Initialize SSL context */
563 SSLeay_add_ssl_algorithms ();
564 meth = SSLv2_client_method ();
565 SSL_load_error_strings ();
566 if ((ctx = SSL_CTX_new (meth)) == NULL)
567 {
568 printf (_("ERROR: Cannot create SSL context.\n"));
569 return STATE_CRITICAL;
570 }
572 /* Initialize alarm signal handling */
573 signal (SIGALRM, socket_timeout_alarm_handler);
575 /* Set socket timeout */
576 alarm (socket_timeout);
578 /* Save start time */
579 time (&start_time);
581 /* Make TCP connection */
582 if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
583 {
584 /* Do the SSL handshake */
585 if ((ssl = SSL_new (ctx)) != NULL)
586 {
587 SSL_set_fd (ssl, sd);
588 if (SSL_connect (ssl) != -1)
589 return OK;
590 ERR_print_errors_fp (stderr);
591 }
592 else
593 {
594 printf (_("ERROR: Cannot initiate SSL handshake.\n"));
595 }
596 SSL_free (ssl);
597 }
599 SSL_CTX_free (ctx);
600 close (sd);
602 return STATE_CRITICAL;
603 }
604 #endif
608 int
609 my_recv (void)
610 {
611 int i;
613 #ifdef HAVE_SSL
614 if (use_ssl) {
615 i = SSL_read (ssl, buffer, MAXBUF - 1);
616 }
617 else {
618 #endif
619 i = read (sd, buffer, MAXBUF - 1);
620 #ifdef HAVE_SSL
621 }
622 #endif
624 return i;
625 }