Code

Patch from Ollie Cook to define return code when expected value not received (#1082275).
[nagiosplug.git] / plugins / check_tcp.c
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  $Id$
18  
19 *****************************************************************************/
21 /* progname "check_tcp" changes depending on symlink called */
22 char *progname;
23 const char *revision = "$Revision$";
24 const char *copyright = "1999-2004";
25 const char *email = "nagiosplug-devel@lists.sourceforge.net";
27 #include "common.h"
28 #include "netutils.h"
29 #include "utils.h"
31 #ifdef HAVE_SSL_H
32 #  include <rsa.h>
33 #  include <crypto.h>
34 #  include <x509.h>
35 #  include <pem.h>
36 #  include <ssl.h>
37 #  include <err.h>
38 #else
39 #  ifdef HAVE_OPENSSL_SSL_H
40 #    include <openssl/rsa.h>
41 #    include <openssl/crypto.h>
42 #    include <openssl/x509.h>
43 #    include <openssl/pem.h>
44 #    include <openssl/ssl.h>
45 #    include <openssl/err.h>
46 #  endif
47 #endif
49 #ifdef HAVE_SSL
50 int check_cert = FALSE;
51 int days_till_exp;
52 char *randbuff = "";
53 SSL_CTX *ctx;
54 SSL *ssl;
55 X509 *server_cert;
56 int connect_SSL (void);
57 int check_certificate (X509 **);
58 #endif
60 enum {
61         TCP_PROTOCOL = 1,
62         UDP_PROTOCOL = 2,
63         MAXBUF = 1024
64 };
66 int process_arguments (int, char **);
67 int my_recv (void);
68 void print_help (void);
69 void print_usage (void);
71 char *SERVICE = NULL;
72 char *SEND = NULL;
73 char *EXPECT = NULL;
74 char *QUIT = NULL;
75 int PROTOCOL = 0;
76 int PORT = 0;
78 char timestamp[17] = "";
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 size_t server_expect_count = 0;
85 int maxbytes = 0;
86 char **warn_codes = NULL;
87 size_t warn_codes_count = 0;
88 char **crit_codes = NULL;
89 size_t crit_codes_count = 0;
90 unsigned int delay = 0;
91 double warning_time = 0;
92 int check_warning_time = FALSE;
93 double critical_time = 0;
94 int check_critical_time = FALSE;
95 int hide_output = FALSE;
96 double elapsed_time = 0;
97 long microsec;
98 int verbose = FALSE;
99 int use_ssl = FALSE;
100 int sd = 0;
101 char *buffer;
102 int expect_mismatch_state = STATE_WARNING;
103 int exact_matching = TRUE;
105 int
106 main (int argc, char **argv)
108         int result = STATE_UNKNOWN;
109         int i;
110         char *status;
111         struct timeval tv;
113         setlocale (LC_ALL, "");
114         bindtextdomain (PACKAGE, LOCALEDIR);
115         textdomain (PACKAGE);
117         if (strstr (argv[0], "check_udp")) {
118                 progname = strdup ("check_udp");
119                 SERVICE = strdup ("UDP");
120                 SEND = NULL;
121                 EXPECT = NULL;
122                 QUIT = NULL;
123                 PROTOCOL = UDP_PROTOCOL;
124                 PORT = 0;
125         }
126         else if (strstr (argv[0], "check_tcp")) {
127                 progname = strdup ("check_tcp");
128                 SERVICE = strdup ("TCP");
129                 SEND = NULL;
130                 EXPECT = NULL;
131                 QUIT = NULL;
132                 PROTOCOL = TCP_PROTOCOL;
133                 PORT = 0;
134         }
135         else if (strstr (argv[0], "check_ftp")) {
136                 progname = strdup ("check_ftp");
137                 SERVICE = strdup ("FTP");
138                 SEND = NULL;
139                 EXPECT = strdup ("220");
140                 QUIT = strdup ("QUIT\r\n");
141                 PROTOCOL = TCP_PROTOCOL;
142                 PORT = 21;
143         }
144         else if (strstr (argv[0], "check_smtp")) {
145                 progname = strdup ("check_smtp");
146                 SERVICE = strdup ("SMTP");
147                 SEND = NULL;
148                 EXPECT = strdup ("220");
149                 QUIT = strdup ("QUIT\r\n");
150                 PROTOCOL = TCP_PROTOCOL;
151                 PORT = 25;
152         }
153         else if (strstr (argv[0], "check_pop")) {
154                 progname = strdup ("check_pop");
155                 SERVICE = strdup ("POP");
156                 SEND = NULL;
157                 EXPECT = strdup ("+OK");
158                 QUIT = strdup ("QUIT\r\n");
159                 PROTOCOL = TCP_PROTOCOL;
160                 PORT = 110;
161         }
162         else if (strstr (argv[0], "check_imap")) {
163                 progname = strdup ("check_imap");
164                 SERVICE = strdup ("IMAP");
165                 SEND = NULL;
166                 EXPECT = strdup ("* OK");
167                 QUIT = strdup ("a1 LOGOUT\r\n");
168                 PROTOCOL = TCP_PROTOCOL;
169                 PORT = 143;
170         }
171 #ifdef HAVE_SSL
172         else if (strstr(argv[0],"check_simap")) {
173                 progname = strdup ("check_simap");
174                 SERVICE = strdup ("SIMAP");
175                 SEND=NULL;
176                 EXPECT = strdup ("* OK");
177                 QUIT = strdup ("a1 LOGOUT\r\n");
178                 PROTOCOL=TCP_PROTOCOL;
179                 use_ssl=TRUE;
180                 PORT=993;
181         }
182         else if (strstr(argv[0],"check_spop")) {
183                 progname = strdup ("check_spop");
184                 SERVICE = strdup ("SPOP");
185                 SEND=NULL;
186                 EXPECT = strdup ("+OK");
187                 QUIT = strdup ("QUIT\r\n");
188                 PROTOCOL=TCP_PROTOCOL;
189                 use_ssl=TRUE;
190                 PORT=995;
191         }
192         else if (strstr(argv[0],"check_jabber")) {
193                 progname = strdup("check_jabber");
194                 SERVICE = strdup("JABBER");
195                 SEND = strdup("<stream:stream to=\'host\' xmlns=\'jabber:client\' xmlns:stream=\'http://etherx.jabber.org/streams\'>\n");
196                 EXPECT = strdup("<?xml version=\'1.0\'?><stream:stream xmlns:stream=\'http://etherx.jabber.org/streams\'");
197                 QUIT = strdup("</stream:stream>\n");
198                 PROTOCOL=TCP_PROTOCOL;
199                 use_ssl=TRUE;
200                 PORT = 5222;
201         }
202        else if (strstr (argv[0], "check_nntps")) {
203                 progname = strdup("check_nntps");
204                 SERVICE = strdup("NNTPS");
205                 SEND = NULL;
206                 EXPECT = NULL;
207                 server_expect = realloc (server_expect, ++server_expect_count);
208                 asprintf (&server_expect[server_expect_count - 1], "200");
209                 server_expect = realloc (server_expect, ++server_expect_count);
210                 asprintf (&server_expect[server_expect_count - 1], "201");
211                 QUIT = strdup("QUIT\r\n");
212                 PROTOCOL = TCP_PROTOCOL;
213                 use_ssl=TRUE;
214                 PORT = 563;
217 #endif
218         else if (strstr (argv[0], "check_nntp")) {
219                 progname = strdup ("check_nntp");
220                 SERVICE = strdup ("NNTP");
221                 SEND = NULL;
222                 EXPECT = NULL;
223                 server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
224                 asprintf (&server_expect[server_expect_count - 1], "200");
225                 server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
226                 asprintf (&server_expect[server_expect_count - 1], "201");
227                 asprintf (&QUIT, "QUIT\r\n");
228                 PROTOCOL = TCP_PROTOCOL;
229                 PORT = 119;
230         }
231         else {
232                 progname = strdup ("check_tcp");
233                 usage (_("CRITICAL - Generic check_tcp called with unknown service\n"));
234         }
236         server_address = strdup ("127.0.0.1");
237         server_port = PORT;
238         server_send = SEND;
239         server_quit = QUIT;
240         status = strdup ("");
242         if (process_arguments (argc, argv) == ERROR)
243                 usage4 (_("Could not parse arguments"));
245         /* use default expect if none listed in process_arguments() */
246         if (EXPECT && server_expect_count == 0) {
247                 server_expect = malloc (sizeof (char *) * (++server_expect_count));
248                 server_expect[server_expect_count - 1] = EXPECT;
249         }
251         /* initialize alarm signal handling */
252         signal (SIGALRM, socket_timeout_alarm_handler);
254         /* set socket timeout */
255         alarm (socket_timeout);
257         /* try to connect to the host at the given port number */
258         gettimeofday (&tv, NULL);
259 #ifdef HAVE_SSL
260         if (use_ssl && check_cert == TRUE) {
261           if (connect_SSL () != OK)
262             die (STATE_CRITICAL,"CRITICAL - Could not make SSL connection\n");
263           if ((server_cert = SSL_get_peer_certificate (ssl)) != NULL) {
264             result = check_certificate (&server_cert);
265             X509_free(server_cert);
266           }
267           else {
268             printf("CRITICAL - Cannot retrieve server certificate.\n");
269             result = STATE_CRITICAL;
270           }
271           SSL_shutdown (ssl);
272           SSL_free (ssl);
273           SSL_CTX_free (ctx);
274           close (sd);
275           return result;
276         }
277         else if (use_ssl)
278                 result = connect_SSL ();
279         else
280 #endif
281                 {
282                         if (PROTOCOL == UDP_PROTOCOL)
283                                 result = my_udp_connect (server_address, server_port, &sd);
284                         else
285                                 /* default is TCP */
286                                 result = my_tcp_connect (server_address, server_port, &sd);
287                 }
289         if (result == STATE_CRITICAL)
290                 return STATE_CRITICAL;
292         if (server_send != NULL) {              /* Something to send? */
293                 asprintf (&server_send, "%s\r\n", server_send);
294 #ifdef HAVE_SSL
295                 if (use_ssl)
296                         SSL_write(ssl, server_send, (int)strlen(server_send));
297                 else
298 #endif
299                         send (sd, server_send, strlen(server_send), 0);
300         }
302         if (delay > 0) {
303                 tv.tv_sec += delay;
304                 sleep (delay);
305         }
307         if (server_send || server_expect_count > 0) {
309                 buffer = malloc (MAXBUF);
310                 memset (buffer, '\0', MAXBUF);
311                 /* watch for the expect string */
312                 while ((i = my_recv ()) > 0) {
313                         buffer[i] = '\0';
314                         asprintf (&status, "%s%s", status, buffer);
315                         if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
316                                 break;
317                         if (maxbytes>0 && strlen(status) >= (unsigned)maxbytes)
318                                 break;
319                 }
321                 /* return a CRITICAL status if we couldn't read any data */
322                 if (strlen(status) == 0)
323                         die (STATE_CRITICAL, _("No data received from host\n"));
325                 strip (status);
327                 if (status && verbose)
328                         printf ("%s\n", status);
330                 if (server_expect_count > 0) {
331                         for (i = 0;; i++) {
332                                 if (verbose)
333                                         printf ("%d %d\n", i, (int)server_expect_count);
334                                 if (i >= (int)server_expect_count)
335                                         die (expect_mismatch_state, _("Unexpected response from host: %s\n"), status);
336                                 /* default expect gets exact matching */
337                                 if (exact_matching) {
338                                         if (strncmp(status, server_expect[i], strlen(server_expect[i])) == 0)
339                                                 break;
340                                 } else {
341                                         if (strstr (status, server_expect[i]))
342                                                 break;
343                                 }
344                         }
345                 }
346         }
348         if (server_quit != NULL) {
349 #ifdef HAVE_SSL
350                 if (use_ssl) {
351                         SSL_write (ssl, server_quit, (int)strlen(server_quit));
352                         SSL_shutdown (ssl);
353                         SSL_free (ssl);
354                         SSL_CTX_free (ctx);
355                 }
356                 else {
357 #endif
358                         send (sd, server_quit, strlen (server_quit), 0);
359 #ifdef HAVE_SSL
360                 }
361 #endif
362         }
364         /* close the connection */
365         if (sd)
366                 close (sd);
368         microsec = deltime (tv);
369         elapsed_time = (double)microsec / 1.0e6;
371         if (check_critical_time == TRUE && elapsed_time > critical_time)
372                 result = STATE_CRITICAL;
373         else if (check_warning_time == TRUE && elapsed_time > warning_time)
374                 result = STATE_WARNING;
376         /* reset the alarm */
377         alarm (0);
379         printf
380                 (_("%s %s%s - %.3f second response time on port %d"),
381                  SERVICE,
382                  state_text (result),
383                  (was_refused) ? " (refused)" : "",
384                  elapsed_time, server_port);
386         if (hide_output == FALSE && status && strlen(status) > 0)
387                 printf (" [%s]", status);
389         printf (" |%s\n", fperfdata ("time", elapsed_time, "s",
390                 TRUE, warning_time,
391                 TRUE, critical_time,
392                 TRUE, 0,
393                 TRUE, socket_timeout));
395         return result;
400 /* process command-line arguments */
401 int
402 process_arguments (int argc, char **argv)
404         int c;
406         int option = 0;
407         static struct option longopts[] = {
408                 {"hostname", required_argument, 0, 'H'},
409                 {"critical-time", required_argument, 0, 'c'},
410                 {"warning-time", required_argument, 0, 'w'},
411                 {"critical-codes", required_argument, 0, 'C'},
412                 {"warning-codes", required_argument, 0, 'W'},
413                 {"timeout", required_argument, 0, 't'},
414                 {"protocol", required_argument, 0, 'P'},
415                 {"port", required_argument, 0, 'p'},
416                 {"send", required_argument, 0, 's'},
417                 {"expect", required_argument, 0, 'e'},
418                 {"maxbytes", required_argument, 0, 'm'},
419                 {"quit", required_argument, 0, 'q'},
420                 {"jail", required_argument, 0, 'j'},
421                 {"delay", required_argument, 0, 'd'},
422                 {"refuse", required_argument, 0, 'r'},
423                 {"mismatch", required_argument, 0, 'M'},
424                 {"use-ipv4", no_argument, 0, '4'},
425                 {"use-ipv6", no_argument, 0, '6'},
426                 {"verbose", no_argument, 0, 'v'},
427                 {"version", no_argument, 0, 'V'},
428                 {"help", no_argument, 0, 'h'},
429 #ifdef HAVE_SSL
430                 {"ssl", no_argument, 0, 'S'},
431                 {"certificate", required_argument, 0, 'D'},
432 #endif
433                 {0, 0, 0, 0}
434         };
436         if (argc < 2)
437                 usage ("No arguments found\n");
439         /* backwards compatibility */
440         for (c = 1; c < argc; c++) {
441                 if (strcmp ("-to", argv[c]) == 0)
442                         strcpy (argv[c], "-t");
443                 else if (strcmp ("-wt", argv[c]) == 0)
444                         strcpy (argv[c], "-w");
445                 else if (strcmp ("-ct", argv[c]) == 0)
446                         strcpy (argv[c], "-c");
447         }
449         if (!is_option (argv[1])) {
450                 server_address = argv[1];
451                 argv[1] = argv[0];
452                 argv = &argv[1];
453                 argc--;
454         }
456         while (1) {
457                 c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:",
458                                  longopts, &option);
460                 if (c == -1 || c == EOF || c == 1)
461                         break;
463                 switch (c) {
464                 case '?':                 /* print short usage statement if args not parsable */
465                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
466                         print_usage ();
467                         exit (STATE_UNKNOWN);
468                 case 'h':                 /* help */
469                         print_help ();
470                         exit (STATE_OK);
471                 case 'V':                 /* version */
472                         print_revision (progname, "$Revision$");
473                         exit (STATE_OK);
474                 case 'v':                 /* verbose mode */
475                         verbose = TRUE;
476                         break;
477                 case '4':
478                         address_family = AF_INET;
479                         break;
480                 case '6':
481 #ifdef USE_IPV6
482                         address_family = AF_INET6;
483 #else
484                         usage4 (_("IPv6 support not available"));
485 #endif
486                         break;
487                 case 'H':                 /* hostname */
488                         if (is_host (optarg) == FALSE)
489                                 usage2 (_("Invalid hostname/address"), optarg);
490                         server_address = optarg;
491                         break;
492                 case 'c':                 /* critical */
493                         if (!is_intnonneg (optarg))
494                                 usage4 (_("Critical threshold must be a positive integer"));
495                         else
496                                 critical_time = strtod (optarg, NULL);
497                         check_critical_time = TRUE;
498                         break;
499                 case 'j':                 /* hide output */
500                         hide_output = TRUE;
501                         break;
502                 case 'w':                 /* warning */
503                         if (!is_intnonneg (optarg))
504                                 usage4 (_("Warning threshold must be a positive integer"));
505                         else
506                                 warning_time = strtod (optarg, NULL);
507                         check_warning_time = TRUE;
508                         break;
509                 case 'C':
510                         crit_codes = realloc (crit_codes, ++crit_codes_count);
511                         crit_codes[crit_codes_count - 1] = optarg;
512                         break;
513                 case 'W':
514                         warn_codes = realloc (warn_codes, ++warn_codes_count);
515                         warn_codes[warn_codes_count - 1] = optarg;
516                         break;
517                 case 't':                 /* timeout */
518                         if (!is_intpos (optarg))
519                                 usage4 (_("Timeout interval must be a positive integer"));
520                         else
521                                 socket_timeout = atoi (optarg);
522                         break;
523                 case 'p':                 /* port */
524                         if (!is_intpos (optarg))
525                                 usage4 (_("Port must be a positive integer"));
526                         else
527                                 server_port = atoi (optarg);
528                         break;
529                 case 's':
530                         server_send = optarg;
531                         break;
532                 case 'e': /* expect string (may be repeated) */
533                         EXPECT = NULL;
534                         exact_matching = FALSE;
535                         if (server_expect_count == 0)
536                                 server_expect = malloc (sizeof (char *) * (++server_expect_count));
537                         else
538                                 server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
539                         server_expect[server_expect_count - 1] = optarg;
540                         break;
541                 case 'm':
542                         if (!is_intpos (optarg))
543                                 usage4 (_("Maxbytes must be a positive integer"));
544                         else
545                                 maxbytes = atoi (optarg);
546                 case 'q':
547                         asprintf(&server_quit, "%s\r\n", optarg);
548                         break;
549                 case 'r':
550                         if (!strncmp(optarg,"ok",2))
551                                 econn_refuse_state = STATE_OK;
552                         else if (!strncmp(optarg,"warn",4))
553                                 econn_refuse_state = STATE_WARNING;
554                         else if (!strncmp(optarg,"crit",4))
555                                 econn_refuse_state = STATE_CRITICAL;
556                         else
557                                 usage4 (_("Refuse must be one of ok, warn, crit"));
558                         break;
559                 case 'M':
560                         if (!strncmp(optarg,"ok",2))
561                                 expect_mismatch_state = STATE_OK;
562                         else if (!strncmp(optarg,"warn",4))
563                                 expect_mismatch_state = STATE_WARNING;
564                         else if (!strncmp(optarg,"crit",4))
565                                 expect_mismatch_state = STATE_CRITICAL;
566                         else
567                                 usage4 (_("Mismatch must be one of ok, warn, crit"));
568                         break;
569                 case 'd':
570                         if (is_intpos (optarg))
571                                 delay = atoi (optarg);
572                         else
573                                 usage4 (_("Delay must be a positive integer"));
574                         break;
575                  case 'D': /* Check SSL cert validity - days 'til certificate expiration */
576 #ifdef HAVE_SSL
577                         if (!is_intnonneg (optarg))
578                                 usage2 ("invalid certificate expiration period", optarg);
579                         days_till_exp = atoi (optarg);
580                         check_cert = TRUE;
581                         use_ssl = TRUE;
582                         break;
583                 case 'S':
584                         use_ssl = TRUE;
585 #else
586                         die (STATE_UNKNOWN, "SSL support not available.  Install OpenSSL and recompile.");
587 #endif
588                         break;
589                 }
590         }
592         if (server_address == NULL)
593                 usage (_("You must provide a server address\n"));
595         return TRUE;
600 #ifdef HAVE_SSL
601 int
602 connect_SSL (void)
604   SSL_METHOD *meth;
606   /* Initialize SSL context */
607   SSLeay_add_ssl_algorithms ();
608   meth = SSLv23_client_method ();
609   SSL_load_error_strings ();
610   OpenSSL_add_all_algorithms();
611   if ((ctx = SSL_CTX_new (meth)) == NULL)
612     {
613       printf (_("CRITICAL - Cannot create SSL context.\n"));
614       return STATE_CRITICAL;
615     }
617   /* Initialize alarm signal handling */
618   signal (SIGALRM, socket_timeout_alarm_handler);
620   /* Set socket timeout */
621   alarm (socket_timeout);
623   /* Save start time */
624   time (&start_time);
626   /* Make TCP connection */
627   if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
628     {
629     /* Do the SSL handshake */
630       if ((ssl = SSL_new (ctx)) != NULL)
631       {
632         SSL_set_fd (ssl, sd);
633         if (SSL_connect(ssl) == 1)
634           return OK;
635         /* ERR_print_errors_fp (stderr); */
636         printf (_("CRITICAL - Cannot make  SSL connection "));
637         ERR_print_errors_fp (stdout);
638         /* printf("\n"); */
639       }
640       else
641       {
642         printf (_("CRITICAL - Cannot initiate SSL handshake.\n"));
643       }
644       SSL_free (ssl);
645     }
647   SSL_CTX_free (ctx);
648   close (sd);
650   return STATE_CRITICAL;
652 #endif
656 #ifdef HAVE_SSL
657 int
658 check_certificate (X509 ** certificate)
660   ASN1_STRING *tm;
661   int offset;
662   struct tm stamp;
663   int days_left;
666   /* Retrieve timestamp of certificate */
667   tm = X509_get_notAfter (*certificate);
669   /* Generate tm structure to process timestamp */
670   if (tm->type == V_ASN1_UTCTIME) {
671     if (tm->length < 10) {
672       printf ("CRITICAL - Wrong time format in certificate.\n");
673       return STATE_CRITICAL;
674     }
675     else {
676       stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
677       if (stamp.tm_year < 50)
678         stamp.tm_year += 100;
679       offset = 0;
680     }
681   }
682   else {
683     if (tm->length < 12) {
684       printf ("CRITICAL - Wrong time format in certificate.\n");
685       return STATE_CRITICAL;
686     }
687     else {
688                         stamp.tm_year =
689                           (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
690                           (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
691                         stamp.tm_year -= 1900;
692                         offset = 2;
693     }
694   }
695         stamp.tm_mon =
696           (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
697         stamp.tm_mday =
698           (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
699         stamp.tm_hour =
700           (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
701         stamp.tm_min =
702           (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
703         stamp.tm_sec = 0;
704         stamp.tm_isdst = -1;
706         days_left = (mktime (&stamp) - time (NULL)) / 86400;
707         snprintf
708           (timestamp, 16, "%02d/%02d/%04d %02d:%02d",
709            stamp.tm_mon + 1,
710            stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
712         if (days_left > 0 && days_left <= days_till_exp) {
713           printf ("Certificate expires in %d day(s) (%s).\n", days_left, timestamp);
714           return STATE_WARNING;
715         }
716         if (days_left < 0) {
717           printf ("Certificate expired on %s.\n", timestamp);
718           return STATE_CRITICAL;
719         }
721         if (days_left == 0) {
722           printf ("Certificate expires today (%s).\n", timestamp);
723           return STATE_WARNING;
724         }
726         printf ("Certificate will expire on %s.\n", timestamp);
728         return STATE_OK;
730 #endif
734 int
735 my_recv (void)
737         int i;
739 #ifdef HAVE_SSL
740         if (use_ssl) {
741                 i = SSL_read (ssl, buffer, MAXBUF - 1);
742         }
743         else {
744 #endif
745                 i = read (sd, buffer, MAXBUF - 1);
746 #ifdef HAVE_SSL
747         }
748 #endif
750         return i;
755 void
756 print_help (void)
758         print_revision (progname, revision);
760         printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
761         printf (COPYRIGHT, copyright, email);
763         printf (_("This plugin tests %s connections with the specified host.\n\n"),
764                 SERVICE);
766         print_usage ();
768         printf (_(UT_HELP_VRSN));
770         printf (_(UT_HOST_PORT), 'p', "none");
772         printf (_(UT_IPv46));
774         printf (_("\
775  -s, --send=STRING\n\
776     String to send to the server\n\
777  -e, --expect=STRING\n\
778     String to expect in server response\n\
779  -q, --quit=STRING\n\
780     String to send server to initiate a clean close of the connection\n"));
782         printf (_("\
783  -r, --refuse=ok|warn|crit\n\
784     Accept tcp refusals with states ok, warn, crit (default: crit)\n\
785  -M, --mismatch=ok|warn|crit\n\
786     Accept expected string mismatches with states ok, warn, crit (default: warn)\n\
787  -j, --jail\n\
788     Hide output from TCP socket\n\
789  -m, --maxbytes=INTEGER\n\
790     Close connection once more than this number of bytes are received\n\
791  -d, --delay=INTEGER\n\
792     Seconds to wait between sending string and polling for response\n"));
794 #ifdef HAVE_SSL
795         printf (_("\
796  -D, --certificate=INTEGER\n\
797     Minimum number of days a certificate has to be valid.\n\
798  -S, --ssl\n\
799     Use SSL for the connection.\n"));
800 #endif
802         printf (_(UT_WARN_CRIT));
804         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
806         printf (_(UT_VERBOSE));
808         printf (_(UT_SUPPORT));
813 void
814 print_usage (void)
816         printf ("\
817 Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\
818                   [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
819                   [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
820                   [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]\n\
821                   [-D <days to cert expiry>] [-S <use SSL>]\n", progname);