Code

if check_tcp was called with -e but not -s, it would hang in a call to my_recv. the
[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_ssmtp")) {
193                 progname = strdup ("check_ssmtp");
194                 SERVICE = strdup ("SSMTP");
195                 SEND=NULL;
196                 EXPECT = strdup ("220");
197                 QUIT = strdup ("QUIT\r\n");
198                 PROTOCOL=TCP_PROTOCOL;
199                 use_ssl=TRUE;
200                 PORT=465;
201         }
202         else if (strstr(argv[0],"check_jabber")) {
203                 progname = strdup("check_jabber");
204                 SERVICE = strdup("JABBER");
205                 SEND = strdup("<stream:stream to=\'host\' xmlns=\'jabber:client\' xmlns:stream=\'http://etherx.jabber.org/streams\'>\n");
206                 EXPECT = strdup("<?xml version=\'1.0\'?><stream:stream xmlns:stream=\'http://etherx.jabber.org/streams\'");
207                 QUIT = strdup("</stream:stream>\n");
208                 PROTOCOL=TCP_PROTOCOL;
209                 use_ssl=TRUE;
210                 PORT = 5222;
211         }
212        else if (strstr (argv[0], "check_nntps")) {
213                 progname = strdup("check_nntps");
214                 SERVICE = strdup("NNTPS");
215                 SEND = NULL;
216                 EXPECT = NULL;
217                 server_expect = realloc (server_expect, ++server_expect_count);
218                 asprintf (&server_expect[server_expect_count - 1], "200");
219                 server_expect = realloc (server_expect, ++server_expect_count);
220                 asprintf (&server_expect[server_expect_count - 1], "201");
221                 QUIT = strdup("QUIT\r\n");
222                 PROTOCOL = TCP_PROTOCOL;
223                 use_ssl=TRUE;
224                 PORT = 563;
227 #endif
228         else if (strstr (argv[0], "check_nntp")) {
229                 progname = strdup ("check_nntp");
230                 SERVICE = strdup ("NNTP");
231                 SEND = NULL;
232                 EXPECT = NULL;
233                 server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
234                 asprintf (&server_expect[server_expect_count - 1], "200");
235                 server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
236                 asprintf (&server_expect[server_expect_count - 1], "201");
237                 asprintf (&QUIT, "QUIT\r\n");
238                 PROTOCOL = TCP_PROTOCOL;
239                 PORT = 119;
240         }
241         else {
242                 progname = strdup ("check_tcp");
243                 usage (_("CRITICAL - Generic check_tcp called with unknown service\n"));
244         }
246         server_address = strdup ("127.0.0.1");
247         server_port = PORT;
248         server_send = SEND;
249         server_quit = QUIT;
250         status = strdup ("");
252         if (process_arguments (argc, argv) == ERROR)
253                 usage4 (_("Could not parse arguments"));
255         /* use default expect if none listed in process_arguments() */
256         if (EXPECT && server_expect_count == 0) {
257                 server_expect = malloc (sizeof (char *) * (++server_expect_count));
258                 server_expect[server_expect_count - 1] = EXPECT;
259         }
261         /* initialize alarm signal handling */
262         signal (SIGALRM, socket_timeout_alarm_handler);
264         /* set socket timeout */
265         alarm (socket_timeout);
267         /* try to connect to the host at the given port number */
268         gettimeofday (&tv, NULL);
269 #ifdef HAVE_SSL
270         if (use_ssl && check_cert == TRUE) {
271           if (connect_SSL () != OK)
272             die (STATE_CRITICAL,_("CRITICAL - Could not make SSL connection\n"));
273           if ((server_cert = SSL_get_peer_certificate (ssl)) != NULL) {
274             result = check_certificate (&server_cert);
275             X509_free(server_cert);
276           }
277           else {
278             printf(_("CRITICAL - Cannot retrieve server certificate.\n"));
279             result = STATE_CRITICAL;
280           }
281           SSL_shutdown (ssl);
282           SSL_free (ssl);
283           SSL_CTX_free (ctx);
284           close (sd);
285           return result;
286         }
287         else if (use_ssl)
288                 result = connect_SSL ();
289         else
290 #endif
291                 {
292                         if (PROTOCOL == UDP_PROTOCOL)
293                                 result = my_udp_connect (server_address, server_port, &sd);
294                         else
295                                 /* default is TCP */
296                                 result = my_tcp_connect (server_address, server_port, &sd);
297                 }
299         if (result == STATE_CRITICAL)
300                 return STATE_CRITICAL;
302         if (server_send != NULL) {              /* Something to send? */
303                 asprintf (&server_send, "%s\r\n", server_send);
304 #ifdef HAVE_SSL
305                 if (use_ssl)
306                         SSL_write(ssl, server_send, (int)strlen(server_send));
307                 else
308 #endif
309                         send (sd, server_send, strlen(server_send), 0);
310         }
312         if (delay > 0) {
313                 tv.tv_sec += delay;
314                 sleep (delay);
315         }
317         if (server_send || server_expect_count > 0) {
319                 buffer = malloc (MAXBUF);
320                 memset (buffer, '\0', MAXBUF);
321                 /* watch for the expect string */
322                 while ((i = my_recv ()) > 0) {
323                         buffer[i] = '\0';
324                         asprintf (&status, "%s%s", status, buffer);
325                         if (buffer[i-1] == '\n') {
326                                 if (buffer[i-2] == '\r' || i < MAXBUF-1)
327                                         break;
328                         }
329                         if (maxbytes>0 && strlen(status) >= (unsigned)maxbytes)
330                                 break;
331                 }
333                 /* return a CRITICAL status if we couldn't read any data */
334                 if (strlen(status) == 0)
335                         die (STATE_CRITICAL, _("No data received from host\n"));
337                 strip (status);
339                 if (status && verbose)
340                         printf ("%s\n", status);
342                 if (server_expect_count > 0) {
343                         for (i = 0;; i++) {
344                                 if (verbose)
345                                         printf ("%d %d\n", i, (int)server_expect_count);
346                                 if (i >= (int)server_expect_count)
347                                         die (expect_mismatch_state, _("Unexpected response from host: %s\n"), status);
348                                 /* default expect gets exact matching */
349                                 if (exact_matching) {
350                                         if (strncmp(status, server_expect[i], strlen(server_expect[i])) == 0)
351                                                 break;
352                                 } else {
353                                         if (strstr (status, server_expect[i]))
354                                                 break;
355                                 }
356                         }
357                 }
358         }
360         if (server_quit != NULL) {
361 #ifdef HAVE_SSL
362                 if (use_ssl) {
363                         SSL_write (ssl, server_quit, (int)strlen(server_quit));
364                         SSL_shutdown (ssl);
365                         SSL_free (ssl);
366                         SSL_CTX_free (ctx);
367                 }
368                 else {
369 #endif
370                         send (sd, server_quit, strlen (server_quit), 0);
371 #ifdef HAVE_SSL
372                 }
373 #endif
374         }
376         /* close the connection */
377         if (sd)
378                 close (sd);
380         microsec = deltime (tv);
381         elapsed_time = (double)microsec / 1.0e6;
383         if (check_critical_time == TRUE && elapsed_time > critical_time)
384                 result = STATE_CRITICAL;
385         else if (check_warning_time == TRUE && elapsed_time > warning_time)
386                 result = STATE_WARNING;
388         /* reset the alarm */
389         alarm (0);
391         printf
392                 (_("%s %s%s - %.3f second response time on port %d"),
393                  SERVICE,
394                  state_text (result),
395                  (was_refused) ? " (refused)" : "",
396                  elapsed_time, server_port);
398         if (hide_output == FALSE && status && strlen(status) > 0)
399                 printf (" [%s]", status);
401         printf (" |%s\n", fperfdata ("time", elapsed_time, "s",
402                 TRUE, warning_time,
403                 TRUE, critical_time,
404                 TRUE, 0,
405                 TRUE, socket_timeout));
407         return result;
412 /* process command-line arguments */
413 int
414 process_arguments (int argc, char **argv)
416         int c;
418         int option = 0;
419         static struct option longopts[] = {
420                 {"hostname", required_argument, 0, 'H'},
421                 {"critical-time", required_argument, 0, 'c'},
422                 {"warning-time", required_argument, 0, 'w'},
423                 {"critical-codes", required_argument, 0, 'C'},
424                 {"warning-codes", required_argument, 0, 'W'},
425                 {"timeout", required_argument, 0, 't'},
426                 {"protocol", required_argument, 0, 'P'},
427                 {"port", required_argument, 0, 'p'},
428                 {"send", required_argument, 0, 's'},
429                 {"expect", required_argument, 0, 'e'},
430                 {"maxbytes", required_argument, 0, 'm'},
431                 {"quit", required_argument, 0, 'q'},
432                 {"jail", required_argument, 0, 'j'},
433                 {"delay", required_argument, 0, 'd'},
434                 {"refuse", required_argument, 0, 'r'},
435                 {"mismatch", required_argument, 0, 'M'},
436                 {"use-ipv4", no_argument, 0, '4'},
437                 {"use-ipv6", no_argument, 0, '6'},
438                 {"verbose", no_argument, 0, 'v'},
439                 {"version", no_argument, 0, 'V'},
440                 {"help", no_argument, 0, 'h'},
441 #ifdef HAVE_SSL
442                 {"ssl", no_argument, 0, 'S'},
443                 {"certificate", required_argument, 0, 'D'},
444 #endif
445                 {0, 0, 0, 0}
446         };
448         if (argc < 2)
449                 usage4 (_("No arguments found"));
451         /* backwards compatibility */
452         for (c = 1; c < argc; c++) {
453                 if (strcmp ("-to", argv[c]) == 0)
454                         strcpy (argv[c], "-t");
455                 else if (strcmp ("-wt", argv[c]) == 0)
456                         strcpy (argv[c], "-w");
457                 else if (strcmp ("-ct", argv[c]) == 0)
458                         strcpy (argv[c], "-c");
459         }
461         if (!is_option (argv[1])) {
462                 server_address = argv[1];
463                 argv[1] = argv[0];
464                 argv = &argv[1];
465                 argc--;
466         }
468         while (1) {
469                 c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:",
470                                  longopts, &option);
472                 if (c == -1 || c == EOF || c == 1)
473                         break;
475                 switch (c) {
476                 case '?':                 /* print short usage statement if args not parsable */
477                         usage2 (_("Unknown argument"), optarg);
478                 case 'h':                 /* help */
479                         print_help ();
480                         exit (STATE_OK);
481                 case 'V':                 /* version */
482                         print_revision (progname, revision);
483                         exit (STATE_OK);
484                 case 'v':                 /* verbose mode */
485                         verbose = TRUE;
486                         break;
487                 case '4':
488                         address_family = AF_INET;
489                         break;
490                 case '6':
491 #ifdef USE_IPV6
492                         address_family = AF_INET6;
493 #else
494                         usage4 (_("IPv6 support not available"));
495 #endif
496                         break;
497                 case 'H':                 /* hostname */
498                         if (is_host (optarg) == FALSE)
499                                 usage2 (_("Invalid hostname/address"), optarg);
500                         server_address = optarg;
501                         break;
502                 case 'c':                 /* critical */
503                         if (!is_intnonneg (optarg))
504                                 usage4 (_("Critical threshold must be a positive integer"));
505                         else
506                                 critical_time = strtod (optarg, NULL);
507                         check_critical_time = TRUE;
508                         break;
509                 case 'j':                 /* hide output */
510                         hide_output = TRUE;
511                         break;
512                 case 'w':                 /* warning */
513                         if (!is_intnonneg (optarg))
514                                 usage4 (_("Warning threshold must be a positive integer"));
515                         else
516                                 warning_time = strtod (optarg, NULL);
517                         check_warning_time = TRUE;
518                         break;
519                 case 'C':
520                         crit_codes = realloc (crit_codes, ++crit_codes_count);
521                         crit_codes[crit_codes_count - 1] = optarg;
522                         break;
523                 case 'W':
524                         warn_codes = realloc (warn_codes, ++warn_codes_count);
525                         warn_codes[warn_codes_count - 1] = optarg;
526                         break;
527                 case 't':                 /* timeout */
528                         if (!is_intpos (optarg))
529                                 usage4 (_("Timeout interval must be a positive integer"));
530                         else
531                                 socket_timeout = atoi (optarg);
532                         break;
533                 case 'p':                 /* port */
534                         if (!is_intpos (optarg))
535                                 usage4 (_("Port must be a positive integer"));
536                         else
537                                 server_port = atoi (optarg);
538                         break;
539                 case 's':
540                         server_send = optarg;
541                         break;
542                 case 'e': /* expect string (may be repeated) */
543                         EXPECT = NULL;
544                         exact_matching = FALSE;
545                         if (server_expect_count == 0)
546                                 server_expect = malloc (sizeof (char *) * (++server_expect_count));
547                         else
548                                 server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
549                         server_expect[server_expect_count - 1] = optarg;
550                         break;
551                 case 'm':
552                         if (!is_intpos (optarg))
553                                 usage4 (_("Maxbytes must be a positive integer"));
554                         else
555                                 maxbytes = atoi (optarg);
556                 case 'q':
557                         asprintf(&server_quit, "%s\r\n", optarg);
558                         break;
559                 case 'r':
560                         if (!strncmp(optarg,"ok",2))
561                                 econn_refuse_state = STATE_OK;
562                         else if (!strncmp(optarg,"warn",4))
563                                 econn_refuse_state = STATE_WARNING;
564                         else if (!strncmp(optarg,"crit",4))
565                                 econn_refuse_state = STATE_CRITICAL;
566                         else
567                                 usage4 (_("Refuse must be one of ok, warn, crit"));
568                         break;
569                 case 'M':
570                         if (!strncmp(optarg,"ok",2))
571                                 expect_mismatch_state = STATE_OK;
572                         else if (!strncmp(optarg,"warn",4))
573                                 expect_mismatch_state = STATE_WARNING;
574                         else if (!strncmp(optarg,"crit",4))
575                                 expect_mismatch_state = STATE_CRITICAL;
576                         else
577                                 usage4 (_("Mismatch must be one of ok, warn, crit"));
578                         break;
579                 case 'd':
580                         if (is_intpos (optarg))
581                                 delay = atoi (optarg);
582                         else
583                                 usage4 (_("Delay must be a positive integer"));
584                         break;
585                  case 'D': /* Check SSL cert validity - days 'til certificate expiration */
586 #ifdef HAVE_SSL
587                         if (!is_intnonneg (optarg))
588                                 usage2 (_("Invalid certificate expiration period"), optarg);
589                         days_till_exp = atoi (optarg);
590                         check_cert = TRUE;
591                         use_ssl = TRUE;
592                         break;
593                 case 'S':
594                         use_ssl = TRUE;
595 #else
596                         die (STATE_UNKNOWN, _("Invalid option - SSL is not available"));
597 #endif
598                         break;
599                 }
600         }
602         if (server_address == NULL)
603                 usage4 (_("You must provide a server address"));
605         return TRUE;
610 #ifdef HAVE_SSL
611 int
612 connect_SSL (void)
614   SSL_METHOD *meth;
616   /* Initialize SSL context */
617   SSLeay_add_ssl_algorithms ();
618   meth = SSLv23_client_method ();
619   SSL_load_error_strings ();
620   OpenSSL_add_all_algorithms();
621   if ((ctx = SSL_CTX_new (meth)) == NULL)
622     {
623       printf (_("CRITICAL - Cannot create SSL context.\n"));
624       return STATE_CRITICAL;
625     }
627   /* Initialize alarm signal handling */
628   signal (SIGALRM, socket_timeout_alarm_handler);
630   /* Set socket timeout */
631   alarm (socket_timeout);
633   /* Save start time */
634   time (&start_time);
636   /* Make TCP connection */
637   if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
638     {
639     /* Do the SSL handshake */
640       if ((ssl = SSL_new (ctx)) != NULL)
641       {
642         SSL_set_fd (ssl, sd);
643         if (SSL_connect(ssl) == 1)
644           return OK;
645         /* ERR_print_errors_fp (stderr); */
646         printf (_("CRITICAL - Cannot make  SSL connection "));
647         ERR_print_errors_fp (stdout);
648         /* printf("\n"); */
649       }
650       else
651       {
652         printf (_("CRITICAL - Cannot initiate SSL handshake.\n"));
653       }
654       SSL_free (ssl);
655     }
657   SSL_CTX_free (ctx);
658   close (sd);
660   return STATE_CRITICAL;
662 #endif
666 #ifdef HAVE_SSL
667 int
668 check_certificate (X509 ** certificate)
670   ASN1_STRING *tm;
671   int offset;
672   struct tm stamp;
673   int days_left;
676   /* Retrieve timestamp of certificate */
677   tm = X509_get_notAfter (*certificate);
679   /* Generate tm structure to process timestamp */
680   if (tm->type == V_ASN1_UTCTIME) {
681     if (tm->length < 10) {
682       printf (_("CRITICAL - Wrong time format in certificate.\n"));
683       return STATE_CRITICAL;
684     }
685     else {
686       stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
687       if (stamp.tm_year < 50)
688         stamp.tm_year += 100;
689       offset = 0;
690     }
691   }
692   else {
693     if (tm->length < 12) {
694       printf (_("CRITICAL - Wrong time format in certificate.\n"));
695       return STATE_CRITICAL;
696     }
697     else {
698                         stamp.tm_year =
699                           (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
700                           (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
701                         stamp.tm_year -= 1900;
702                         offset = 2;
703     }
704   }
705         stamp.tm_mon =
706           (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
707         stamp.tm_mday =
708           (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
709         stamp.tm_hour =
710           (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
711         stamp.tm_min =
712           (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
713         stamp.tm_sec = 0;
714         stamp.tm_isdst = -1;
716         days_left = (mktime (&stamp) - time (NULL)) / 86400;
717         snprintf
718           (timestamp, 16, "%02d/%02d/%04d %02d:%02d",
719            stamp.tm_mon + 1,
720            stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
722         if (days_left > 0 && days_left <= days_till_exp) {
723           printf (_("Certificate expires in %d day(s) (%s).\n"), days_left, timestamp);
724           return STATE_WARNING;
725         }
726         if (days_left < 0) {
727           printf (_("Certificate expired on %s.\n"), timestamp);
728           return STATE_CRITICAL;
729         }
731         if (days_left == 0) {
732           printf (_("Certificate expires today (%s).\n"), timestamp);
733           return STATE_WARNING;
734         }
736         printf (_("Certificate will expire on %s.\n"), timestamp);
738         return STATE_OK;
740 #endif
744 int
745 my_recv (void)
747         int i;
749 #ifdef HAVE_SSL
750         if (use_ssl) {
751                 i = SSL_read (ssl, buffer, MAXBUF - 1);
752         }
753         else {
754 #endif
755                 i = read (sd, buffer, MAXBUF - 1);
756 #ifdef HAVE_SSL
757         }
758 #endif
760         return i;
765 void
766 print_help (void)
768         print_revision (progname, revision);
770         printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
771         printf (COPYRIGHT, copyright, email);
773         printf (_("This plugin tests %s connections with the specified host.\n\n"),
774                 SERVICE);
776         print_usage ();
778         printf (_(UT_HELP_VRSN));
780         printf (_(UT_HOST_PORT), 'p', "none");
782         printf (_(UT_IPv46));
784         printf (_("\
785  -s, --send=STRING\n\
786     String to send to the server\n\
787  -e, --expect=STRING\n\
788     String to expect in server response\n\
789  -q, --quit=STRING\n\
790     String to send server to initiate a clean close of the connection\n"));
792         printf (_("\
793  -r, --refuse=ok|warn|crit\n\
794     Accept tcp refusals with states ok, warn, crit (default: crit)\n\
795  -M, --mismatch=ok|warn|crit\n\
796     Accept expected string mismatches with states ok, warn, crit (default: warn)\n\
797  -j, --jail\n\
798     Hide output from TCP socket\n\
799  -m, --maxbytes=INTEGER\n\
800     Close connection once more than this number of bytes are received\n\
801  -d, --delay=INTEGER\n\
802     Seconds to wait between sending string and polling for response\n"));
804 #ifdef HAVE_SSL
805         printf (_("\
806  -D, --certificate=INTEGER\n\
807     Minimum number of days a certificate has to be valid.\n\
808  -S, --ssl\n\
809     Use SSL for the connection.\n"));
810 #endif
812         printf (_(UT_WARN_CRIT));
814         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
816         printf (_(UT_VERBOSE));
818         printf (_(UT_SUPPORT));
823 void
824 print_usage (void)
826         printf ("\
827 Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\
828                   [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
829                   [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
830                   [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]\n\
831                   [-D <days to cert expiry>] [-S <use SSL>]\n", progname);