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 changes depending on symlink called */
20 char *progname = "check_tcp";
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 usage (_("ERROR: Generic check_tcp called with unknown service\n"));
202 }
204 server_address = strdup ("127.0.0.1");
205 server_port = PORT;
206 server_send = SEND;
207 server_quit = QUIT;
209 if (process_arguments (argc, argv) == ERROR)
210 usage (_("Could not parse arguments\n"));
212 /* use default expect if none listed in process_arguments() */
213 if (EXPECT && server_expect_count == 0) {
214 server_expect = malloc (++server_expect_count);
215 server_expect[server_expect_count - 1] = EXPECT;
216 }
218 /* initialize alarm signal handling */
219 signal (SIGALRM, socket_timeout_alarm_handler);
221 /* set socket timeout */
222 alarm (socket_timeout);
224 /* try to connect to the host at the given port number */
225 gettimeofday (&tv, NULL);
226 #ifdef HAVE_SSL
227 if (use_ssl)
228 result = connect_SSL ();
229 else
230 #endif
231 {
232 if (PROTOCOL == UDP_PROTOCOL)
233 result = my_udp_connect (server_address, server_port, &sd);
234 else
235 /* default is TCP */
236 result = my_tcp_connect (server_address, server_port, &sd);
237 }
239 if (result == STATE_CRITICAL)
240 return STATE_CRITICAL;
242 if (server_send != NULL) { /* Something to send? */
243 asprintf (&server_send, "%s\r\n", server_send);
244 #ifdef HAVE_SSL
245 if (use_ssl)
246 SSL_write(ssl, server_send, (int)strlen(server_send));
247 else
248 #endif
249 send (sd, server_send, strlen(server_send), 0);
250 }
252 if (delay > 0) {
253 tv.tv_sec += delay;
254 sleep (delay);
255 }
257 if (server_send || server_expect_count > 0) {
259 buffer = malloc (MAXBUF);
260 memset (buffer, '\0', MAXBUF);
261 status = strdup ("");
262 /* watch for the expect string */
263 while ((i = my_recv ()) > 0) {
264 buffer[i] = '\0';
265 asprintf (&status, "%s%s", status, buffer);
266 if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
267 break;
268 if (maxbytes>0 && strlen(status) >= (unsigned)maxbytes)
269 break;
270 }
272 /* return a CRITICAL status if we couldn't read any data */
273 if (status == NULL)
274 die (STATE_CRITICAL, _("No data received from host\n"));
276 strip (status);
278 if (status && verbose)
279 printf ("%s\n", status);
281 if (server_expect_count > 0) {
282 for (i = 0;; i++) {
283 if (verbose)
284 printf ("%d %d\n", i, (int)server_expect_count);
285 if (i >= (int)server_expect_count)
286 die (STATE_WARNING, _("Invalid response from host\n"));
287 if (strstr (status, server_expect[i]))
288 break;
289 }
290 }
291 }
293 if (server_quit != NULL) {
294 #ifdef HAVE_SSL
295 if (use_ssl) {
296 SSL_write (ssl, server_quit, (int)strlen(server_quit));
297 SSL_shutdown (ssl);
298 SSL_free (ssl);
299 SSL_CTX_free (ctx);
300 }
301 else {
302 #endif
303 send (sd, server_quit, strlen (server_quit), 0);
304 #ifdef HAVE_SSL
305 }
306 #endif
307 }
309 /* close the connection */
310 if (sd)
311 close (sd);
313 microsec = deltime (tv);
314 elapsed_time = (double)microsec / 1.0e6;
316 if (check_critical_time == TRUE && elapsed_time > critical_time)
317 result = STATE_CRITICAL;
318 else if (check_warning_time == TRUE && elapsed_time > warning_time)
319 result = STATE_WARNING;
321 /* reset the alarm */
322 alarm (0);
324 printf
325 (_("%s %s%s - %.3f second response time on port %d"),
326 SERVICE,
327 state_text (result),
328 (was_refused) ? " (refused)" : "",
329 elapsed_time, server_port);
331 if (status && strlen(status) > 0)
332 printf (" [%s]", status);
334 printf ("|time=%ldus\n", microsec);
336 return result;
337 }
338 \f
342 /* process command-line arguments */
343 int
344 process_arguments (int argc, char **argv)
345 {
346 int c;
348 int option = 0;
349 static struct option longopts[] = {
350 {"hostname", required_argument, 0, 'H'},
351 {"critical-time", required_argument, 0, 'c'},
352 {"warning-time", required_argument, 0, 'w'},
353 {"critical-codes", required_argument, 0, 'C'},
354 {"warning-codes", required_argument, 0, 'W'},
355 {"timeout", required_argument, 0, 't'},
356 {"protocol", required_argument, 0, 'P'},
357 {"port", required_argument, 0, 'p'},
358 {"send", required_argument, 0, 's'},
359 {"expect", required_argument, 0, 'e'},
360 {"maxbytes", required_argument, 0, 'm'},
361 {"quit", required_argument, 0, 'q'},
362 {"delay", required_argument, 0, 'd'},
363 {"refuse", required_argument, 0, 'r'},
364 {"use-ipv4", no_argument, 0, '4'},
365 {"use-ipv6", no_argument, 0, '6'},
366 {"verbose", no_argument, 0, 'v'},
367 {"version", no_argument, 0, 'V'},
368 {"help", no_argument, 0, 'h'},
369 {0, 0, 0, 0}
370 };
372 if (argc < 2)
373 usage ("No arguments found\n");
375 /* backwards compatibility */
376 for (c = 1; c < argc; c++) {
377 if (strcmp ("-to", argv[c]) == 0)
378 strcpy (argv[c], "-t");
379 else if (strcmp ("-wt", argv[c]) == 0)
380 strcpy (argv[c], "-w");
381 else if (strcmp ("-ct", argv[c]) == 0)
382 strcpy (argv[c], "-c");
383 }
385 if (!is_option (argv[1])) {
386 server_address = argv[1];
387 argv[1] = argv[0];
388 argv = &argv[1];
389 argc--;
390 }
392 while (1) {
393 c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:",
394 longopts, &option);
396 if (c == -1 || c == EOF || c == 1)
397 break;
399 switch (c) {
400 case '?': /* print short usage statement if args not parsable */
401 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
402 print_usage ();
403 exit (STATE_UNKNOWN);
404 case 'h': /* help */
405 print_help ();
406 exit (STATE_OK);
407 case 'V': /* version */
408 print_revision (progname, "$Revision$");
409 exit (STATE_OK);
410 case 'v': /* verbose mode */
411 verbose = TRUE;
412 break;
413 case '4':
414 address_family = AF_INET;
415 break;
416 case '6':
417 #ifdef USE_IPV6
418 address_family = AF_INET6;
419 #else
420 usage (_("IPv6 support not available\n"));
421 #endif
422 break;
423 case 'H': /* hostname */
424 if (is_host (optarg) == FALSE)
425 usage2 (_("invalid host name or address"), optarg);
426 server_address = optarg;
427 break;
428 case 'c': /* critical */
429 if (!is_intnonneg (optarg))
430 usage (_("Critical threshold must be a nonnegative integer\n"));
431 else
432 critical_time = strtod (optarg, NULL);
433 check_critical_time = TRUE;
434 break;
435 case 'w': /* warning */
436 if (!is_intnonneg (optarg))
437 usage (_("Warning threshold must be a nonnegative integer\n"));
438 else
439 warning_time = strtod (optarg, NULL);
440 check_warning_time = TRUE;
441 break;
442 case 'C':
443 crit_codes = realloc (crit_codes, ++crit_codes_count);
444 crit_codes[crit_codes_count - 1] = optarg;
445 break;
446 case 'W':
447 warn_codes = realloc (warn_codes, ++warn_codes_count);
448 warn_codes[warn_codes_count - 1] = optarg;
449 break;
450 case 't': /* timeout */
451 if (!is_intpos (optarg))
452 usage (_("Timeout interval must be a positive integer\n"));
453 else
454 socket_timeout = atoi (optarg);
455 break;
456 case 'p': /* port */
457 if (!is_intpos (optarg))
458 usage (_("Server port must be a positive integer\n"));
459 else
460 server_port = atoi (optarg);
461 break;
462 case 's':
463 server_send = optarg;
464 break;
465 case 'e': /* expect string (may be repeated) */
466 EXPECT = NULL;
467 if (server_expect_count == 0)
468 server_expect = malloc (++server_expect_count);
469 else
470 server_expect = realloc (server_expect, ++server_expect_count);
471 server_expect[server_expect_count - 1] = optarg;
472 break;
473 case 'm':
474 if (!is_intpos (optarg))
475 usage (_("Maxbytes must be a positive integer\n"));
476 else
477 maxbytes = atoi (optarg);
478 case 'q':
479 server_quit = optarg;
480 break;
481 case 'r':
482 if (!strncmp(optarg,"ok",2))
483 econn_refuse_state = STATE_OK;
484 else if (!strncmp(optarg,"warn",4))
485 econn_refuse_state = STATE_WARNING;
486 else if (!strncmp(optarg,"crit",4))
487 econn_refuse_state = STATE_CRITICAL;
488 else
489 usage (_("Refuse mut be one of ok, warn, crit\n"));
490 break;
491 case 'd':
492 if (is_intpos (optarg))
493 delay = atoi (optarg);
494 else
495 usage (_("Delay must be a positive integer\n"));
496 break;
497 case 'S':
498 #ifndef HAVE_SSL
499 die (STATE_UNKNOWN,
500 _("SSL support not available. Install OpenSSL and recompile."));
501 #endif
502 use_ssl = TRUE;
503 break;
504 }
505 }
507 if (server_address == NULL)
508 usage (_("You must provide a server address\n"));
510 return OK;
511 }
512 \f
514 #ifdef HAVE_SSL
515 int
516 connect_SSL (void)
517 {
518 SSL_METHOD *meth;
520 /* Initialize SSL context */
521 SSLeay_add_ssl_algorithms ();
522 meth = SSLv2_client_method ();
523 SSL_load_error_strings ();
524 if ((ctx = SSL_CTX_new (meth)) == NULL)
525 {
526 printf (_("ERROR: Cannot create SSL context.\n"));
527 return STATE_CRITICAL;
528 }
530 /* Initialize alarm signal handling */
531 signal (SIGALRM, socket_timeout_alarm_handler);
533 /* Set socket timeout */
534 alarm (socket_timeout);
536 /* Save start time */
537 time (&start_time);
539 /* Make TCP connection */
540 if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
541 {
542 /* Do the SSL handshake */
543 if ((ssl = SSL_new (ctx)) != NULL)
544 {
545 SSL_set_fd (ssl, sd);
546 if (SSL_connect (ssl) != -1)
547 return OK;
548 ERR_print_errors_fp (stderr);
549 }
550 else
551 {
552 printf (_("ERROR: Cannot initiate SSL handshake.\n"));
553 }
554 SSL_free (ssl);
555 }
557 SSL_CTX_free (ctx);
558 close (sd);
560 return STATE_CRITICAL;
561 }
562 #endif
566 int
567 my_recv (void)
568 {
569 int i;
571 #ifdef HAVE_SSL
572 if (use_ssl) {
573 i = SSL_read (ssl, buffer, MAXBUF - 1);
574 }
575 else {
576 #endif
577 i = read (sd, buffer, MAXBUF - 1);
578 #ifdef HAVE_SSL
579 }
580 #endif
582 return i;
583 }
589 \f
590 void
591 print_help (void)
592 {
593 print_revision (progname, revision);
595 printf (_(COPYRIGHT), copyright, email);
597 printf (_("This plugin tests %s connections with the specified host.\n\n"),
598 SERVICE);
600 print_usage ();
602 printf (_(UT_HELP_VRSN));
604 printf (_(UT_HOST_PORT), 'p', "none");
606 printf (_(UT_IPv46));
608 printf (_("\
609 -s, --send=STRING\n\
610 String to send to the server\n\
611 -e, --expect=STRING\n\
612 String to expect in server response\n\
613 -q, --quit=STRING\n\
614 String to send server to initiate a clean close of the connection\n"));
616 printf (_("\
617 -r, --refuse=ok|warn|crit\n\
618 Accept tcp refusals with states ok, warn, crit (default: crit)\n\
619 -m, --maxbytes=INTEGER\n\
620 Close connection once more than this number of bytes are received\n\
621 -d, --delay=INTEGER\n\
622 Seconds to wait between sending string and polling for response\n"));
624 printf (_(UT_WARN_CRIT));
626 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
628 printf (_(UT_VERBOSE));
630 printf (_(UT_SUPPORT));
631 }
636 void
637 print_usage (void)
638 {
639 printf (_("\
640 Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\
641 [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
642 [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
643 [-r <refuse state>] [-v] [-4|-6]\n"), progname);
644 printf (" %s (-h|--help)\n", progname);
645 printf (" %s (-V|--version)\n", progname);
646 }