Code

failed if header was more than 1023 bytes
[nagiosplug.git] / plugins / check_tcp.c
1 /******************************************************************************
2  *
3  * This file is part of the Nagios Plugins.
4  *
5  * Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>
6  *
7  * The Nagios Plugins are free software; you can redistribute them
8  * and/or modify them under the terms of the GNU General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * $Id$
22  *
23  *****************************************************************************/
25 #define REVISION "$Revision$"
26 #define DESCRIPTION "Check a TCP port"
27 #define AUTHOR "Ethan Galstad"
28 #define EMAIL "nagios@nagios.org"
29 #define COPYRIGHTDATE "2002"
31 #include "config.h"
32 #include "common.h"
33 #include "netutils.h"
34 #include "utils.h"
36 #ifdef HAVE_SSL_H
37 #include <rsa.h>
38 #include <crypto.h>
39 #include <x509.h>
40 #include <pem.h>
41 #include <ssl.h>
42 #include <err.h>
43 #endif
45 #ifdef HAVE_OPENSSL_SSL_H
46 #include <openssl/rsa.h>
47 #include <openssl/crypto.h>
48 #include <openssl/x509.h>
49 #include <openssl/pem.h>
50 #include <openssl/ssl.h>
51 #include <openssl/err.h>
52 #endif
54 #ifdef HAVE_SSL
55 SSL_CTX *ctx;
56 SSL *ssl;
57 int connect_SSL (void);
58 #endif
60 enum {
61         TCP_PROTOCOL = 1,
62         UDP_PROTOCOL = 2,
63         MAXBUF = 1024
64 };
66 int process_arguments (int, char **);
67 void print_usage (void);
68 void print_help (void);
69 int my_recv (void);
71 char *progname = "check_tcp";
72 char *SERVICE = NULL;
73 char *SEND = NULL;
74 char *EXPECT = NULL;
75 char *QUIT = NULL;
76 int PROTOCOL = 0;
77 int PORT = 0;
79 int server_port = 0;
80 char *server_address = NULL;
81 char *server_send = NULL;
82 char *server_quit = NULL;
83 char **server_expect = NULL;
84 int server_expect_count = 0;
85 char **warn_codes = NULL;
86 int warn_codes_count = 0;
87 char **crit_codes = NULL;
88 int crit_codes_count = 0;
89 int delay = 0;
90 double warning_time = 0;
91 int check_warning_time = FALSE;
92 double critical_time = 0;
93 int check_critical_time = FALSE;
94 double elapsed_time = 0;
95 int verbose = FALSE;
96 int use_ssl = FALSE;
97 int sd = 0;
98 char *buffer = "";
100 int
101 main (int argc, char **argv)
103         int result;
104         int i;
105         char *status = "";
106         struct timeval tv;
108         if (strstr (argv[0], "check_udp")) {
109                 asprintf (&progname, "check_udp");
110                 asprintf (&SERVICE, "UDP");
111                 SEND = NULL;
112                 EXPECT = NULL;
113                 QUIT = NULL;
114                 PROTOCOL = UDP_PROTOCOL;
115                 PORT = 0;
116         }
117         else if (strstr (argv[0], "check_tcp")) {
118                 asprintf (&progname, "check_tcp");
119                 asprintf (&SERVICE, "TCP");
120                 SEND = NULL;
121                 EXPECT = NULL;
122                 QUIT = NULL;
123                 PROTOCOL = TCP_PROTOCOL;
124                 PORT = 0;
125         }
126         else if (strstr (argv[0], "check_ftp")) {
127                 asprintf (&progname, "check_ftp");
128                 asprintf (&SERVICE, "FTP");
129                 SEND = NULL;
130                 asprintf (&EXPECT, "220");
131                 asprintf (&QUIT, "QUIT\r\n");
132                 PROTOCOL = TCP_PROTOCOL;
133                 PORT = 21;
134         }
135         else if (strstr (argv[0], "check_smtp")) {
136                 asprintf (&progname, "check_smtp");
137                 asprintf (&SERVICE, "SMTP");
138                 SEND = NULL;
139                 asprintf (&EXPECT, "220");
140                 asprintf (&QUIT, "QUIT\r\n");
141                 PROTOCOL = TCP_PROTOCOL;
142                 PORT = 25;
143         }
144         else if (strstr (argv[0], "check_pop")) {
145                 asprintf (&progname, "check_pop");
146                 asprintf (&SERVICE, "POP");
147                 SEND = NULL;
148                 asprintf (&EXPECT, "+OK");
149                 asprintf (&QUIT, "QUIT\r\n");
150                 PROTOCOL = TCP_PROTOCOL;
151                 PORT = 110;
152         }
153         else if (strstr (argv[0], "check_imap")) {
154                 asprintf (&progname, "check_imap");
155                 asprintf (&SERVICE, "IMAP");
156                 SEND = NULL;
157                 asprintf (&EXPECT, "* OK");
158                 asprintf (&QUIT, "a1 LOGOUT\r\n");
159                 PROTOCOL = TCP_PROTOCOL;
160                 PORT = 143;
161         }
162 #ifdef HAVE_SSL
163         else if (strstr(argv[0],"check_simap")) {
164                 asprintf (&progname, "check_simap");
165                 asprintf (&SERVICE, "SIMAP");
166                 SEND=NULL;
167                 asprintf (&EXPECT, "* OK");
168                 asprintf (&QUIT, "a1 LOGOUT\r\n");
169                 PROTOCOL=TCP_PROTOCOL;
170                 use_ssl=TRUE;
171                 PORT=993;
172         }
173         else if (strstr(argv[0],"check_spop")) {
174                 asprintf (&progname, "check_spop");
175                 asprintf (&SERVICE, "SPOP");
176                 SEND=NULL;
177                 asprintf (&EXPECT, "+OK");
178                 asprintf (&QUIT, "QUIT\r\n");
179                 PROTOCOL=TCP_PROTOCOL;
180                 use_ssl=TRUE;
181                 PORT=995;
182         }
183 #endif
184         else if (strstr (argv[0], "check_nntp")) {
185                 asprintf (&progname, "check_nntp");
186                 asprintf (&SERVICE, "NNTP");
187                 SEND = NULL;
188                 EXPECT = NULL;
189                 server_expect = realloc (server_expect, ++server_expect_count);
190                 asprintf (&server_expect[server_expect_count - 1], "200");
191                 server_expect = realloc (server_expect, ++server_expect_count);
192                 asprintf (&server_expect[server_expect_count - 1], "201");
193                 asprintf (&QUIT, "QUIT\r\n");
194                 PROTOCOL = TCP_PROTOCOL;
195                 PORT = 119;
196         }
197         else {
198                 usage ("ERROR: Generic check_tcp called with unknown service\n");
199         }
201         asprintf (&server_address, "127.0.0.1");
202         server_port = PORT;
203         server_send = SEND;
204         server_quit = QUIT;
206         if (process_arguments (argc, argv) == ERROR)
207                 usage ("Could not parse arguments\n");
209         /* use default expect if none listed in process_arguments() */
210         if (EXPECT && server_expect_count == 0) {
211                 server_expect = malloc (++server_expect_count);
212                 server_expect[server_expect_count - 1] = EXPECT;
213         }
215         /* initialize alarm signal handling */
216         signal (SIGALRM, socket_timeout_alarm_handler);
218         /* set socket timeout */
219         alarm (socket_timeout);
221         /* try to connect to the host at the given port number */
222         gettimeofday (&tv, NULL);
223 #ifdef HAVE_SSL
224         if (use_ssl)
225                 result = connect_SSL ();
226         else
227 #endif
228                 {
229                         if (PROTOCOL == UDP_PROTOCOL)
230                                 result = my_udp_connect (server_address, server_port, &sd);
231                         else                                                                                                    /* default is TCP */
232                                 result = my_tcp_connect (server_address, server_port, &sd);
233                 }
235         if (result == STATE_CRITICAL)
236                 return STATE_CRITICAL;
238         if (server_send != NULL) {              /* Something to send? */
239                 asprintf (&server_send, "%s\r\n", server_send);
240 #ifdef HAVE_SSL
241                 if (use_ssl)
242                         SSL_write(ssl, server_send, strlen (server_send));
243                 else
244 #endif
245                         send (sd, server_send, strlen (server_send), 0);
246         }
248         if (delay > 0) {
249                 tv.tv_sec += delay;
250                 sleep (delay);
251         }
253         if (server_send || server_expect_count > 0) {
255                 buffer = malloc (MAXBUF);
256                 memset (buffer, '\0', MAXBUF);
257                 /* watch for the expect string */
258                 while ((i = my_recv ()) > 0) {
259                         buffer[i] = '\0';
260                         asprintf (&status, "%s%s", status, buffer);
261                         if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
262                                 break;
263                 }
265                 /* return a CRITICAL status if we couldn't read any data */
266                 if (status == NULL)
267                         terminate (STATE_CRITICAL, "No data received from host\n");
269                 strip (status);
271                 if (status && verbose)
272                         printf ("%s\n", status);
274                 if (server_expect_count > 0) {
275                         for (i = 0;; i++) {
276                                 if (verbose)
277                                         printf ("%d %d\n", i, server_expect_count);
278                                 if (i >= server_expect_count)
279                                         terminate (STATE_WARNING, "Invalid response from host\n");
280                                 if (strstr (status, server_expect[i]))
281                                         break;
282                         }
283                 }
284         }
286         if (server_quit != NULL)
287 #ifdef HAVE_SSL
288                 if (use_ssl) {
289                         SSL_write (ssl, QUIT, strlen (QUIT));
290                         SSL_shutdown (ssl);
291                         SSL_free (ssl);
292                         SSL_CTX_free (ctx);
293                 }
294                 else
295 #endif
296                 send (sd, server_quit, strlen (server_quit), 0);
298         /* close the connection */
299         if (sd)
300                 close (sd);
302         elapsed_time = delta_time (tv);
304         if (check_critical_time == TRUE && elapsed_time > critical_time)
305                 result = STATE_CRITICAL;
306         else if (check_warning_time == TRUE && elapsed_time > warning_time)
307                 result = STATE_WARNING;
309         /* reset the alarm */
310         alarm (0);
312         printf
313                 ("%s %s - %7.3f second response time on port %d",
314                  SERVICE,
315                  state_text (result), elapsed_time, server_port);
317         if (status && strlen(status) > 0)
318                 printf (" [%s]", status);
320         printf ("|time=%7.3f\n", elapsed_time);
322         return result;
331 /* process command-line arguments */
332 int
333 process_arguments (int argc, char **argv)
335         int c;
337 #ifdef HAVE_GETOPT_H
338         int option_index = 0;
339         static struct option long_options[] = {
340                 {"hostname", required_argument, 0, 'H'},
341                 {"critical-time", required_argument, 0, 'c'},
342                 {"warning-time", required_argument, 0, 'w'},
343                 {"critical-codes", required_argument, 0, 'C'},
344                 {"warning-codes", required_argument, 0, 'W'},
345                 {"timeout", required_argument, 0, 't'},
346                 {"protocol", required_argument, 0, 'P'},
347                 {"port", required_argument, 0, 'p'},
348                 {"send", required_argument, 0, 's'},
349                 {"expect", required_argument, 0, 'e'},
350                 {"quit", required_argument, 0, 'q'},
351                 {"delay", required_argument, 0, 'd'},
352                 {"verbose", no_argument, 0, 'v'},
353                 {"version", no_argument, 0, 'V'},
354                 {"help", no_argument, 0, 'h'},
355                 {0, 0, 0, 0}
356         };
357 #endif
359         if (argc < 2)
360                 usage ("No arguments found\n");
362         /* backwards compatibility */
363         for (c = 1; c < argc; c++) {
364                 if (strcmp ("-to", argv[c]) == 0)
365                         strcpy (argv[c], "-t");
366                 else if (strcmp ("-wt", argv[c]) == 0)
367                         strcpy (argv[c], "-w");
368                 else if (strcmp ("-ct", argv[c]) == 0)
369                         strcpy (argv[c], "-c");
370         }
372         if (!is_option (argv[1])) {
373                 server_address = argv[1];
374                 argv[1] = argv[0];
375                 argv = &argv[1];
376                 argc--;
377         }
379         while (1) {
380 #ifdef HAVE_GETOPT_H
381                 c =
382                         getopt_long (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:S", long_options,
383                                                                          &option_index);
384 #else
385                 c = getopt (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:S");
386 #endif
388                 if (c == -1 || c == EOF || c == 1)
389                         break;
391                 switch (c) {
392                 case '?':                 /* print short usage statement if args not parsable */
393                         printf ("%s: Unknown argument: %s\n\n", progname, optarg);
394                         print_usage ();
395                         exit (STATE_UNKNOWN);
396                 case 'h':                 /* help */
397                         print_help ();
398                         exit (STATE_OK);
399                 case 'V':                 /* version */
400                         print_revision (progname, "$Revision$");
401                         exit (STATE_OK);
402                 case 'v':                 /* verbose mode */
403                         verbose = TRUE;
404                         break;
405                 case 'H':                 /* hostname */
406                         if (is_host (optarg) == FALSE)
407                                 usage2 ("invalid host name or address", optarg);
408                         server_address = optarg;
409                         break;
410                 case 'c':                 /* critical */
411                         if (!is_intnonneg (optarg))
412                                 usage ("Critical threshold must be a nonnegative integer\n");
413                         critical_time = strtod (optarg, NULL);
414                         check_critical_time = TRUE;
415                         break;
416                 case 'w':                 /* warning */
417                         if (!is_intnonneg (optarg))
418                                 usage ("Warning threshold must be a nonnegative integer\n");
419                         warning_time = strtod (optarg, NULL);
420                         check_warning_time = TRUE;
421                         break;
422                 case 'C':
423                         crit_codes = realloc (crit_codes, ++crit_codes_count);
424                         crit_codes[crit_codes_count - 1] = optarg;
425                         break;
426                 case 'W':
427                         warn_codes = realloc (warn_codes, ++warn_codes_count);
428                         warn_codes[warn_codes_count - 1] = optarg;
429                         break;
430                 case 't':                 /* timeout */
431                         if (!is_intpos (optarg))
432                                 usage ("Timeout interval must be a positive integer\n");
433                         socket_timeout = atoi (optarg);
434                         break;
435                 case 'p':                 /* port */
436                         if (!is_intpos (optarg))
437                                 usage ("Server port must be a positive integer\n");
438                         server_port = atoi (optarg);
439                         break;
440                 case 's':
441                         server_send = optarg;
442                         break;
443                 case 'e': /* expect string (may be repeated) */
444                         EXPECT = NULL;
445                         if (server_expect_count == 0)
446                                 server_expect = malloc (++server_expect_count);
447                         else
448                                 server_expect = realloc (server_expect, ++server_expect_count);
449                         server_expect[server_expect_count - 1] = optarg;
450                         break;
451                 case 'q':
452                         server_quit = optarg;
453                         break;
454                 case 'd':
455                         if (is_intpos (optarg))
456                                 delay = atoi (optarg);
457                         else
458                                 usage ("Delay must be a positive integer\n");
459                         break;
460                 case 'S':
461 #ifndef HAVE_SSL
462                         terminate (STATE_UNKNOWN,
463                                 "SSL support not available. Install OpenSSL and recompile.");
464 #endif
465                         use_ssl = TRUE;
466                         break;
467                 }
468         }
470         if (server_address == NULL)
471                 usage ("You must provide a server address\n");
473         return OK;
480 void
481 print_usage (void)
483         printf
484                 ("Usage: %s -H host -p port [-w warn_time] [-c crit_time] [-s send]\n"
485                  "         [-e expect] [-W wait] [-t to_sec] [-v]\n", progname);
492 void
493 print_help (void)
495         print_revision (progname, "$Revision$");
496         printf
497                 ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
498                  "This plugin tests %s connections with the specified host.\n\n",
499                  SERVICE);
500         print_usage ();
501         printf
502                 ("Options:\n"
503                  " -H, --hostname=ADDRESS\n"
504                  "    Host name argument for servers using host headers (use numeric\n"
505                  "    address if possible to bypass DNS lookup).\n"
506                  " -p, --port=INTEGER\n"
507                  "    Port number\n"
508                  " -s, --send=STRING\n"
509                  "    String to send to the server\n"
510                  " -e, --expect=STRING\n"
511                  "    String to expect in server response"
512                  " -W, --wait=INTEGER\n"
513                  "    Seconds to wait between sending string and polling for response\n"
514                  " -w, --warning=DOUBLE\n"
515                  "    Response time to result in warning status (seconds)\n"
516                  " -c, --critical=DOUBLE\n"
517                  "    Response time to result in critical status (seconds)\n"
518                  " -t, --timeout=INTEGER\n"
519                  "    Seconds before connection times out (default: %d)\n"
520                  " -v"
521                  "    Show details for command-line debugging (do not use with nagios server)\n"
522                  " -h, --help\n"
523                  "    Print detailed help screen\n"
524                  " -V, --version\n"
525                  "    Print version information\n", DEFAULT_SOCKET_TIMEOUT);
529 #ifdef HAVE_SSL
530 int
531 connect_SSL (void)
533   SSL_METHOD *meth;
535   /* Initialize SSL context */
536   SSLeay_add_ssl_algorithms ();
537   meth = SSLv2_client_method ();
538   SSL_load_error_strings ();
539   if ((ctx = SSL_CTX_new (meth)) == NULL)
540     {
541       printf ("ERROR: Cannot create SSL context.\n");
542       return STATE_CRITICAL;
543     }
545   /* Initialize alarm signal handling */
546   signal (SIGALRM, socket_timeout_alarm_handler);
548   /* Set socket timeout */
549   alarm (socket_timeout);
551   /* Save start time */
552   time (&start_time);
554   /* Make TCP connection */
555   if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK)
556     {
557     /* Do the SSL handshake */
558       if ((ssl = SSL_new (ctx)) != NULL)
559       {
560         SSL_set_fd (ssl, sd);
561         if (SSL_connect (ssl) != -1)
562           return OK;
563         ERR_print_errors_fp (stderr);
564       }
565       else
566       {
567         printf ("ERROR: Cannot initiate SSL handshake.\n");
568       }
569       SSL_free (ssl);
570     }
572   SSL_CTX_free (ctx);
573   close (sd);
575   return STATE_CRITICAL;
577 #endif
581 int
582 my_recv (void)
584         int i;
586 #ifdef HAVE_SSL
587         if (use_ssl) {
588                 i = SSL_read (ssl, buffer, MAXBUF - 1);
589         }
590         else {
591 #endif
592                 i = read (sd, buffer, MAXBUF - 1);
593 #ifdef HAVE_SSL
594         }
595 #endif
597         return i;