1 /*****************************************************************************
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.
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.
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.
17 *****************************************************************************/
19 /* progname "check_tcp" changes depending on symlink called */
20 char *progname;
21 const char *revision = "$Revision$";
22 const char *copyright = "1999-2003";
23 const char *email = "nagiosplug-devel@lists.sourceforge.net";
25 #include "common.h"
26 #include "netutils.h"
27 #include "utils.h"
29 #ifdef HAVE_SSL_H
30 # include <rsa.h>
31 # include <crypto.h>
32 # include <x509.h>
33 # include <pem.h>
34 # include <ssl.h>
35 # include <err.h>
36 #else
37 # ifdef HAVE_OPENSSL_SSL_H
38 # include <openssl/rsa.h>
39 # include <openssl/crypto.h>
40 # include <openssl/x509.h>
41 # include <openssl/pem.h>
42 # include <openssl/ssl.h>
43 # include <openssl/err.h>
44 # endif
45 #endif
47 #ifdef HAVE_SSL
48 SSL_CTX *ctx;
49 SSL *ssl;
50 int connect_SSL (void);
51 #endif
53 enum {
54 TCP_PROTOCOL = 1,
55 UDP_PROTOCOL = 2,
56 MAXBUF = 1024
57 };
59 int process_arguments (int, char **);
60 int my_recv (void);
61 void print_help (void);
62 void print_usage (void);
64 char *SERVICE = NULL;
65 char *SEND = NULL;
66 char *EXPECT = NULL;
67 char *QUIT = NULL;
68 int PROTOCOL = 0;
69 int PORT = 0;
71 int server_port = 0;
72 char *server_address = NULL;
73 char *server_send = NULL;
74 char *server_quit = NULL;
75 char **server_expect = NULL;
76 size_t server_expect_count = 0;
77 int maxbytes = 0;
78 char **warn_codes = NULL;
79 size_t warn_codes_count = 0;
80 char **crit_codes = NULL;
81 size_t crit_codes_count = 0;
82 unsigned int delay = 0;
83 double warning_time = 0;
84 int check_warning_time = FALSE;
85 double critical_time = 0;
86 int check_critical_time = FALSE;
87 double elapsed_time = 0;
88 long microsec;
89 int verbose = FALSE;
90 int use_ssl = FALSE;
91 int sd = 0;
92 char *buffer;
98 \f
99 int
100 main (int argc, char **argv)
101 {
102 int result;
103 int i;
104 char *status;
105 struct timeval tv;
107 setlocale (LC_ALL, "");
108 bindtextdomain (PACKAGE, LOCALEDIR);
109 textdomain (PACKAGE);
111 if (strstr (argv[0], "check_udp")) {
112 progname = strdup ("check_udp");
113 SERVICE = strdup ("UDP");
114 SEND = NULL;
115 EXPECT = NULL;
116 QUIT = NULL;
117 PROTOCOL = UDP_PROTOCOL;
118 PORT = 0;
119 }
120 else if (strstr (argv[0], "check_tcp")) {
121 progname = strdup ("check_tcp");
122 SERVICE = strdup ("TCP");
123 SEND = NULL;
124 EXPECT = NULL;
125 QUIT = NULL;
126 PROTOCOL = TCP_PROTOCOL;
127 PORT = 0;
128 }
129 else if (strstr (argv[0], "check_ftp")) {
130 progname = strdup ("check_ftp");
131 SERVICE = strdup ("FTP");
132 SEND = NULL;
133 EXPECT = strdup ("220");
134 QUIT = strdup ("QUIT\r\n");
135 PROTOCOL = TCP_PROTOCOL;
136 PORT = 21;
137 }
138 else if (strstr (argv[0], "check_smtp")) {
139 progname = strdup ("check_smtp");
140 SERVICE = strdup ("SMTP");
141 SEND = NULL;
142 EXPECT = strdup ("220");
143 QUIT = strdup ("QUIT\r\n");
144 PROTOCOL = TCP_PROTOCOL;
145 PORT = 25;
146 }
147 else if (strstr (argv[0], "check_pop")) {
148 progname = strdup ("check_pop");
149 SERVICE = strdup ("POP");
150 SEND = NULL;
151 EXPECT = strdup ("+OK");
152 QUIT = strdup ("QUIT\r\n");
153 PROTOCOL = TCP_PROTOCOL;
154 PORT = 110;
155 }
156 else if (strstr (argv[0], "check_imap")) {
157 progname = strdup ("check_imap");
158 SERVICE = strdup ("IMAP");
159 SEND = NULL;
160 EXPECT = strdup ("* OK");
161 QUIT = strdup ("a1 LOGOUT\r\n");
162 PROTOCOL = TCP_PROTOCOL;
163 PORT = 143;
164 }
165 #ifdef HAVE_SSL
166 else if (strstr(argv[0],"check_simap")) {
167 progname = strdup ("check_simap");
168 SERVICE = strdup ("SIMAP");
169 SEND=NULL;
170 EXPECT = strdup ("* OK");
171 QUIT = strdup ("a1 LOGOUT\r\n");
172 PROTOCOL=TCP_PROTOCOL;
173 use_ssl=TRUE;
174 PORT=993;
175 }
176 else if (strstr(argv[0],"check_spop")) {
177 progname = strdup ("check_spop");
178 SERVICE = strdup ("SPOP");
179 SEND=NULL;
180 EXPECT = strdup ("+OK");
181 QUIT = strdup ("QUIT\r\n");
182 PROTOCOL=TCP_PROTOCOL;
183 use_ssl=TRUE;
184 PORT=995;
185 }
186 #endif
187 else if (strstr (argv[0], "check_nntp")) {
188 progname = strdup ("check_nntp");
189 SERVICE = strdup ("NNTP");
190 SEND = NULL;
191 EXPECT = NULL;
192 server_expect = realloc (server_expect, ++server_expect_count);
193 asprintf (&server_expect[server_expect_count - 1], "200");
194 server_expect = realloc (server_expect, ++server_expect_count);
195 asprintf (&server_expect[server_expect_count - 1], "201");
196 asprintf (&QUIT, "QUIT\r\n");
197 PROTOCOL = TCP_PROTOCOL;
198 PORT = 119;
199 }
200 else {
201 progname = strdup ("check_tcp");
202 usage (_("ERROR: Generic check_tcp called with unknown service\n"));
203 }
205 server_address = strdup ("127.0.0.1");
206 server_port = PORT;
207 server_send = SEND;
208 server_quit = QUIT;
210 if (process_arguments (argc, argv) == ERROR)
211 usage (_("Could not parse arguments\n"));
213 /* use default expect if none listed in process_arguments() */
214 if (EXPECT && server_expect_count == 0) {
215 server_expect = malloc (++server_expect_count);
216 server_expect[server_expect_count - 1] = EXPECT;
217 }
219 /* initialize alarm signal handling */
220 signal (SIGALRM, socket_timeout_alarm_handler);
222 /* set socket timeout */
223 alarm (socket_timeout);
225 /* try to connect to the host at the given port number */
226 gettimeofday (&tv, NULL);
227 #ifdef HAVE_SSL
228 if (use_ssl)
229 result = connect_SSL ();
230 else
231 #endif
232 {
233 if (PROTOCOL == UDP_PROTOCOL)
234 result = my_udp_connect (server_address, server_port, &sd);
235 else
236 /* default is TCP */
237 result = my_tcp_connect (server_address, server_port, &sd);
238 }
240 if (result == STATE_CRITICAL)
241 return STATE_CRITICAL;
243 if (server_send != NULL) { /* Something to send? */
244 asprintf (&server_send, "%s\r\n", server_send);
245 #ifdef HAVE_SSL
246 if (use_ssl)
247 SSL_write(ssl, server_send, (int)strlen(server_send));
248 else
249 #endif
250 send (sd, server_send, strlen(server_send), 0);
251 }
253 if (delay > 0) {
254 tv.tv_sec += delay;
255 sleep (delay);
256 }
258 if (server_send || server_expect_count > 0) {
260 buffer = malloc (MAXBUF);
261 memset (buffer, '\0', MAXBUF);
262 status = strdup ("");
263 /* watch for the expect string */
264 while ((i = my_recv ()) > 0) {
265 buffer[i] = '\0';
266 asprintf (&status, "%s%s", status, buffer);
267 if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
268 break;
269 if (maxbytes>0 && strlen(status) >= (unsigned)maxbytes)
270 break;
271 }
273 /* return a CRITICAL status if we couldn't read any data */
274 if (status == NULL)
275 die (STATE_CRITICAL, _("No data received from host\n"));
277 strip (status);
279 if (status && verbose)
280 printf ("%s\n", status);
282 if (server_expect_count > 0) {
283 for (i = 0;; i++) {
284 if (verbose)
285 printf ("%d %d\n", i, (int)server_expect_count);
286 if (i >= (int)server_expect_count)
287 die (STATE_WARNING, _("Invalid response from host\n"));
288 if (strstr (status, server_expect[i]))
289 break;
290 }
291 }
292 }
294 if (server_quit != NULL) {
295 #ifdef HAVE_SSL
296 if (use_ssl) {
297 SSL_write (ssl, server_quit, (int)strlen(server_quit));
298 SSL_shutdown (ssl);
299 SSL_free (ssl);
300 SSL_CTX_free (ctx);
301 }
302 else {
303 #endif
304 send (sd, server_quit, strlen (server_quit), 0);
305 #ifdef HAVE_SSL
306 }
307 #endif
308 }
310 /* close the connection */
311 if (sd)
312 close (sd);
314 microsec = deltime (tv);
315 elapsed_time = (double)microsec / 1.0e6;
317 if (check_critical_time == TRUE && elapsed_time > critical_time)
318 result = STATE_CRITICAL;
319 else if (check_warning_time == TRUE && elapsed_time > warning_time)
320 result = STATE_WARNING;
322 /* reset the alarm */
323 alarm (0);
325 printf
326 (_("%s %s%s - %.3f second response time on port %d"),
327 SERVICE,
328 state_text (result),
329 (was_refused) ? " (refused)" : "",
330 elapsed_time, server_port);
332 if (status && strlen(status) > 0)
333 printf (" [%s]", status);
335 printf ("|time=%ldus\n", microsec);
337 return result;
338 }
339 \f
343 /* process command-line arguments */
344 int
345 process_arguments (int argc, char **argv)
346 {
347 int c;
349 int option = 0;
350 static struct option longopts[] = {
351 {"hostname", required_argument, 0, 'H'},
352 {"critical-time", required_argument, 0, 'c'},
353 {"warning-time", required_argument, 0, 'w'},
354 {"critical-codes", required_argument, 0, 'C'},
355 {"warning-codes", required_argument, 0, 'W'},
356 {"timeout", required_argument, 0, 't'},
357 {"protocol", required_argument, 0, 'P'},
358 {"port", required_argument, 0, 'p'},
359 {"send", required_argument, 0, 's'},
360 {"expect", required_argument, 0, 'e'},
361 {"maxbytes", required_argument, 0, 'm'},
362 {"quit", required_argument, 0, 'q'},
363 {"delay", required_argument, 0, 'd'},
364 {"refuse", required_argument, 0, 'r'},
365 {"use-ipv4", no_argument, 0, '4'},
366 {"use-ipv6", no_argument, 0, '6'},
367 {"verbose", no_argument, 0, 'v'},
368 {"version", no_argument, 0, 'V'},
369 {"help", no_argument, 0, 'h'},
370 {0, 0, 0, 0}
371 };
373 if (argc < 2)
374 usage ("No arguments found\n");
376 /* backwards compatibility */
377 for (c = 1; c < argc; c++) {
378 if (strcmp ("-to", argv[c]) == 0)
379 strcpy (argv[c], "-t");
380 else if (strcmp ("-wt", argv[c]) == 0)
381 strcpy (argv[c], "-w");
382 else if (strcmp ("-ct", argv[c]) == 0)
383 strcpy (argv[c], "-c");
384 }
386 if (!is_option (argv[1])) {
387 server_address = argv[1];
388 argv[1] = argv[0];
389 argv = &argv[1];
390 argc--;
391 }
393 while (1) {
394 c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:",
395 longopts, &option);
397 if (c == -1 || c == EOF || c == 1)
398 break;
400 switch (c) {
401 case '?': /* print short usage statement if args not parsable */
402 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
403 print_usage ();
404 exit (STATE_UNKNOWN);
405 case 'h': /* help */
406 print_help ();
407 exit (STATE_OK);
408 case 'V': /* version */
409 print_revision (progname, "$Revision$");
410 exit (STATE_OK);
411 case 'v': /* verbose mode */
412 verbose = TRUE;
413 break;
414 case '4':
415 address_family = AF_INET;
416 break;
417 case '6':
418 #ifdef USE_IPV6
419 address_family = AF_INET6;
420 #else
421 usage (_("IPv6 support not available\n"));
422 #endif
423 break;
424 case 'H': /* hostname */
425 if (is_host (optarg) == FALSE)
426 usage2 (_("invalid host name or address"), optarg);
427 server_address = optarg;
428 break;
429 case 'c': /* critical */
430 if (!is_intnonneg (optarg))
431 usage (_("Critical threshold must be a nonnegative integer\n"));
432 else
433 critical_time = strtod (optarg, NULL);
434 check_critical_time = TRUE;
435 break;
436 case 'w': /* warning */
437 if (!is_intnonneg (optarg))
438 usage (_("Warning threshold must be a nonnegative integer\n"));
439 else
440 warning_time = strtod (optarg, NULL);
441 check_warning_time = TRUE;
442 break;
443 case 'C':
444 crit_codes = realloc (crit_codes, ++crit_codes_count);
445 crit_codes[crit_codes_count - 1] = optarg;
446 break;
447 case 'W':
448 warn_codes = realloc (warn_codes, ++warn_codes_count);
449 warn_codes[warn_codes_count - 1] = optarg;
450 break;
451 case 't': /* timeout */
452 if (!is_intpos (optarg))
453 usage (_("Timeout interval must be a positive integer\n"));
454 else
455 socket_timeout = atoi (optarg);
456 break;
457 case 'p': /* port */
458 if (!is_intpos (optarg))
459 usage (_("Server port must be a positive integer\n"));
460 else
461 server_port = atoi (optarg);
462 break;
463 case 's':
464 server_send = optarg;
465 break;
466 case 'e': /* expect string (may be repeated) */
467 EXPECT = NULL;
468 if (server_expect_count == 0)
469 server_expect = malloc (++server_expect_count);
470 else
471 server_expect = realloc (server_expect, ++server_expect_count);
472 server_expect[server_expect_count - 1] = optarg;
473 break;
474 case 'm':
475 if (!is_intpos (optarg))
476 usage (_("Maxbytes must be a positive integer\n"));
477 else
478 maxbytes = atoi (optarg);
479 case 'q':
480 server_quit = optarg;
481 break;
482 case 'r':
483 if (!strncmp(optarg,"ok",2))
484 econn_refuse_state = STATE_OK;
485 else if (!strncmp(optarg,"warn",4))
486 econn_refuse_state = STATE_WARNING;
487 else if (!strncmp(optarg,"crit",4))
488 econn_refuse_state = STATE_CRITICAL;
489 else
490 usage (_("Refuse mut be one of ok, warn, crit\n"));
491 break;
492 case 'd':
493 if (is_intpos (optarg))
494 delay = atoi (optarg);
495 else
496 usage (_("Delay must be a positive integer\n"));
497 break;
498 case 'S':
499 #ifndef HAVE_SSL
500 die (STATE_UNKNOWN,
501 _("SSL support not available. Install OpenSSL and recompile."));
502 #endif
503 use_ssl = TRUE;
504 break;
505 }
506 }
508 if (server_address == NULL)
509 usage (_("You must provide a server address\n"));
511 return OK;
512 }
513 \f
515 #ifdef HAVE_SSL
516 int
517 connect_SSL (void)
518 {
519 SSL_METHOD *meth;
521 /* Initialize SSL context */
522 SSLeay_add_ssl_algorithms ();
523 meth = SSLv2_client_method ();
524 SSL_load_error_strings ();
525 if ((ctx = SSL_CTX_new (meth)) == NULL)
526 {
527 printf (_("ERROR: Cannot create SSL context.\n"));
528 return STATE_CRITICAL;
529 }
531 /* Initialize alarm signal handling */
532 signal (SIGALRM, socket_timeout_alarm_handler);
534 /* Set socket timeout */
535 alarm (socket_timeout);
537 /* Save start time */
538 time (&start_time);
540 /* Make TCP connection */
541 if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
542 {
543 /* Do the SSL handshake */
544 if ((ssl = SSL_new (ctx)) != NULL)
545 {
546 SSL_set_fd (ssl, sd);
547 if (SSL_connect (ssl) != -1)
548 return OK;
549 ERR_print_errors_fp (stderr);
550 }
551 else
552 {
553 printf (_("ERROR: Cannot initiate SSL handshake.\n"));
554 }
555 SSL_free (ssl);
556 }
558 SSL_CTX_free (ctx);
559 close (sd);
561 return STATE_CRITICAL;
562 }
563 #endif
567 int
568 my_recv (void)
569 {
570 int i;
572 #ifdef HAVE_SSL
573 if (use_ssl) {
574 i = SSL_read (ssl, buffer, MAXBUF - 1);
575 }
576 else {
577 #endif
578 i = read (sd, buffer, MAXBUF - 1);
579 #ifdef HAVE_SSL
580 }
581 #endif
583 return i;
584 }
590 \f
591 void
592 print_help (void)
593 {
594 print_revision (progname, revision);
596 printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
597 printf (_(COPYRIGHT), copyright, email);
599 printf (_("This plugin tests %s connections with the specified host.\n\n"),
600 SERVICE);
602 print_usage ();
604 printf (_(UT_HELP_VRSN));
606 printf (_(UT_HOST_PORT), 'p', "none");
608 printf (_(UT_IPv46));
610 printf (_("\
611 -s, --send=STRING\n\
612 String to send to the server\n\
613 -e, --expect=STRING\n\
614 String to expect in server response\n\
615 -q, --quit=STRING\n\
616 String to send server to initiate a clean close of the connection\n"));
618 printf (_("\
619 -r, --refuse=ok|warn|crit\n\
620 Accept tcp refusals with states ok, warn, crit (default: crit)\n\
621 -m, --maxbytes=INTEGER\n\
622 Close connection once more than this number of bytes are received\n\
623 -d, --delay=INTEGER\n\
624 Seconds to wait between sending string and polling for response\n"));
626 printf (_(UT_WARN_CRIT));
628 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
630 printf (_(UT_VERBOSE));
632 printf (_(UT_SUPPORT));
633 }
638 void
639 print_usage (void)
640 {
641 printf (_("\
642 Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\
643 [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
644 [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
645 [-r <refuse state>] [-v] [-4|-6]\n"), progname);
646 printf (" %s (-h|--help)\n", progname);
647 printf (" %s (-V|--version)\n", progname);
648 }