Code

optimizations and cleanup from andreas
[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 #define MAXBUF 1024
62 int process_arguments (int, char **);
63 int my_recv (void);
64 void print_help (void);
65 void print_usage (void);
67 char *SERVICE = NULL;
68 char *SEND = NULL;
69 char *EXPECT = NULL;
70 char *QUIT = NULL;
71 int PROTOCOL = 0;
72 int PORT = 0;
74 char timestamp[17] = "";
75 int server_port = 0;
76 char *server_address = NULL;
77 char *server_send = NULL;
78 char *server_quit = NULL;
79 char **server_expect = NULL;
80 size_t server_expect_count = 0;
81 int maxbytes = 0;
82 char **warn_codes = NULL;
83 size_t warn_codes_count = 0;
84 char **crit_codes = NULL;
85 size_t crit_codes_count = 0;
86 unsigned int delay = 0;
87 double warning_time = 0;
88 int check_warning_time = FALSE;
89 double critical_time = 0;
90 int check_critical_time = FALSE;
91 int hide_output = FALSE;
92 double elapsed_time = 0;
93 long microsec;
94 int verbose = FALSE;
95 int use_ssl = FALSE;
96 int sd = 0;
97 char *buffer;
98 int expect_mismatch_state = STATE_WARNING;
99 int exact_matching = TRUE;
101 int
102 main (int argc, char **argv)
104         int result = STATE_UNKNOWN;
105         int i;
106         char *status;
107         struct timeval tv;
109         setlocale (LC_ALL, "");
110         bindtextdomain (PACKAGE, LOCALEDIR);
111         textdomain (PACKAGE);
113         if (strstr (argv[0], "check_udp")) {
114                 progname = strdup ("check_udp");
115                 SERVICE = strdup ("UDP");
116                 SEND = NULL;
117                 EXPECT = NULL;
118                 QUIT = NULL;
119                 PROTOCOL = IPPROTO_UDP;
120                 PORT = 0;
121         }
122         else if (strstr (argv[0], "check_tcp")) {
123                 progname = strdup ("check_tcp");
124                 SERVICE = strdup ("TCP");
125                 SEND = NULL;
126                 EXPECT = NULL;
127                 QUIT = NULL;
128                 PROTOCOL = IPPROTO_TCP;
129                 PORT = 0;
130         }
131         else if (strstr (argv[0], "check_ftp")) {
132                 progname = strdup ("check_ftp");
133                 SERVICE = strdup ("FTP");
134                 SEND = NULL;
135                 EXPECT = strdup ("220");
136                 QUIT = strdup ("QUIT\r\n");
137                 PROTOCOL = IPPROTO_TCP;
138                 PORT = 21;
139         }
140         else if (strstr (argv[0], "check_smtp")) {
141                 progname = strdup ("check_smtp");
142                 SERVICE = strdup ("SMTP");
143                 SEND = NULL;
144                 EXPECT = strdup ("220");
145                 QUIT = strdup ("QUIT\r\n");
146                 PROTOCOL = IPPROTO_TCP;
147                 PORT = 25;
148         }
149         else if (strstr (argv[0], "check_pop")) {
150                 progname = strdup ("check_pop");
151                 SERVICE = strdup ("POP");
152                 SEND = NULL;
153                 EXPECT = strdup ("+OK");
154                 QUIT = strdup ("QUIT\r\n");
155                 PROTOCOL = IPPROTO_TCP;
156                 PORT = 110;
157         }
158         else if (strstr (argv[0], "check_imap")) {
159                 progname = strdup ("check_imap");
160                 SERVICE = strdup ("IMAP");
161                 SEND = NULL;
162                 EXPECT = strdup ("* OK");
163                 QUIT = strdup ("a1 LOGOUT\r\n");
164                 PROTOCOL = IPPROTO_TCP;
165                 PORT = 143;
166         }
167 #ifdef HAVE_SSL
168         else if (strstr(argv[0],"check_simap")) {
169                 progname = strdup ("check_simap");
170                 SERVICE = strdup ("SIMAP");
171                 SEND=NULL;
172                 EXPECT = strdup ("* OK");
173                 QUIT = strdup ("a1 LOGOUT\r\n");
174                 PROTOCOL=IPPROTO_TCP;
175                 use_ssl=TRUE;
176                 PORT=993;
177         }
178         else if (strstr(argv[0],"check_spop")) {
179                 progname = strdup ("check_spop");
180                 SERVICE = strdup ("SPOP");
181                 SEND=NULL;
182                 EXPECT = strdup ("+OK");
183                 QUIT = strdup ("QUIT\r\n");
184                 PROTOCOL=IPPROTO_TCP;
185                 use_ssl=TRUE;
186                 PORT=995;
187         }
188         else if (strstr(argv[0],"check_ssmtp")) {
189                 progname = strdup ("check_ssmtp");
190                 SERVICE = strdup ("SSMTP");
191                 SEND=NULL;
192                 EXPECT = strdup ("220");
193                 QUIT = strdup ("QUIT\r\n");
194                 PROTOCOL=IPPROTO_TCP;
195                 use_ssl=TRUE;
196                 PORT=465;
197         }
198         else if (strstr(argv[0],"check_jabber")) {
199                 progname = strdup("check_jabber");
200                 SERVICE = strdup("JABBER");
201                 SEND = strdup("<stream:stream to=\'host\' xmlns=\'jabber:client\' xmlns:stream=\'http://etherx.jabber.org/streams\'>\n");
202                 EXPECT = strdup("<?xml version=\'1.0\'?><stream:stream xmlns:stream=\'http://etherx.jabber.org/streams\'");
203                 QUIT = strdup("</stream:stream>\n");
204                 PROTOCOL=IPPROTO_TCP;
205                 use_ssl=TRUE;
206                 PORT = 5222;
207         }
208        else if (strstr (argv[0], "check_nntps")) {
209                 progname = strdup("check_nntps");
210                 SERVICE = strdup("NNTPS");
211                 SEND = NULL;
212                 EXPECT = NULL;
213                 server_expect = realloc (server_expect, ++server_expect_count);
214                 asprintf (&server_expect[server_expect_count - 1], "200");
215                 server_expect = realloc (server_expect, ++server_expect_count);
216                 asprintf (&server_expect[server_expect_count - 1], "201");
217                 QUIT = strdup("QUIT\r\n");
218                 PROTOCOL = IPPROTO_TCP;
219                 use_ssl=TRUE;
220                 PORT = 563;
223 #endif
224         else if (strstr (argv[0], "check_nntp")) {
225                 progname = strdup ("check_nntp");
226                 SERVICE = strdup ("NNTP");
227                 SEND = NULL;
228                 EXPECT = NULL;
229                 server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
230                 asprintf (&server_expect[server_expect_count - 1], "200");
231                 server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
232                 asprintf (&server_expect[server_expect_count - 1], "201");
233                 asprintf (&QUIT, "QUIT\r\n");
234                 PROTOCOL = IPPROTO_TCP;
235                 PORT = 119;
236         }
237         else {
238                 progname = strdup ("check_tcp");
239                 usage (_("CRITICAL - Generic check_tcp called with unknown service\n"));
240         }
242         server_address = strdup ("127.0.0.1");
243         server_port = PORT;
244         server_send = SEND;
245         server_quit = QUIT;
246         status = strdup ("");
248         if (process_arguments (argc, argv) == ERROR)
249                 usage4 (_("Could not parse arguments"));
251         /* use default expect if none listed in process_arguments() */
252         if (EXPECT && server_expect_count == 0) {
253                 server_expect = malloc (sizeof (char *) * (++server_expect_count));
254                 server_expect[server_expect_count - 1] = EXPECT;
255         }
257         /* initialize alarm signal handling */
258         signal (SIGALRM, socket_timeout_alarm_handler);
260         /* set socket timeout */
261         alarm (socket_timeout);
263         /* try to connect to the host at the given port number */
264         gettimeofday (&tv, NULL);
265 #ifdef HAVE_SSL
266         if (use_ssl && check_cert == TRUE) {
267           if (connect_SSL () != OK)
268             die (STATE_CRITICAL,_("CRITICAL - Could not make SSL connection\n"));
269           if ((server_cert = SSL_get_peer_certificate (ssl)) != NULL) {
270             result = check_certificate (&server_cert);
271             X509_free(server_cert);
272           }
273           else {
274             printf(_("CRITICAL - Cannot retrieve server certificate.\n"));
275             result = STATE_CRITICAL;
276           }
277           SSL_shutdown (ssl);
278           SSL_free (ssl);
279           SSL_CTX_free (ctx);
280           close (sd);
281           return result;
282         }
283         else if (use_ssl)
284                 result = connect_SSL ();
285         else
286 #endif
287                 result = my_connect (server_address, server_port, &sd, PROTOCOL);
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-1] == '\n') {
316                                 if (buffer[i-2] == '\r' || i < MAXBUF-1)
317                                         break;
318                         }
319                         if (maxbytes>0 && strlen(status) >= (unsigned)maxbytes)
320                                 break;
321                 }
323                 /* return a CRITICAL status if we couldn't read any data */
324                 if (strlen(status) == 0)
325                         die (STATE_CRITICAL, _("No data received from host\n"));
327                 strip (status);
329                 if (status && verbose)
330                         printf ("%s\n", status);
332                 if (server_expect_count > 0) {
333                         for (i = 0;; i++) {
334                                 if (verbose)
335                                         printf ("%d %d\n", i, (int)server_expect_count);
336                                 if (i >= (int)server_expect_count)
337                                         die (expect_mismatch_state, _("Unexpected response from host: %s\n"), status);
338                                 /* default expect gets exact matching */
339                                 if (exact_matching) {
340                                         if (strncmp(status, server_expect[i], strlen(server_expect[i])) == 0)
341                                                 break;
342                                 } else {
343                                         if (strstr (status, server_expect[i]))
344                                                 break;
345                                 }
346                         }
347                 }
348         }
350         if (server_quit != NULL) {
351 #ifdef HAVE_SSL
352                 if (use_ssl) {
353                         SSL_write (ssl, server_quit, (int)strlen(server_quit));
354                         SSL_shutdown (ssl);
355                         SSL_free (ssl);
356                         SSL_CTX_free (ctx);
357                 }
358                 else {
359 #endif
360                         send (sd, server_quit, strlen (server_quit), 0);
361 #ifdef HAVE_SSL
362                 }
363 #endif
364         }
366         /* close the connection */
367         if (sd)
368                 close (sd);
370         microsec = deltime (tv);
371         elapsed_time = (double)microsec / 1.0e6;
373         if (check_critical_time == TRUE && elapsed_time > critical_time)
374                 result = STATE_CRITICAL;
375         else if (check_warning_time == TRUE && elapsed_time > warning_time)
376                 result = STATE_WARNING;
378         /* reset the alarm */
379         alarm (0);
381         printf
382                 (_("%s %s%s - %.3f second response time on port %d"),
383                  SERVICE,
384                  state_text (result),
385                  (was_refused) ? " (refused)" : "",
386                  elapsed_time, server_port);
388         if (hide_output == FALSE && status && strlen(status) > 0)
389                 printf (" [%s]", status);
391         printf (" |%s\n", fperfdata ("time", elapsed_time, "s",
392                 TRUE, warning_time,
393                 TRUE, critical_time,
394                 TRUE, 0,
395                 TRUE, socket_timeout));
397         return result;
402 /* process command-line arguments */
403 int
404 process_arguments (int argc, char **argv)
406         int c;
408         int option = 0;
409         static struct option longopts[] = {
410                 {"hostname", required_argument, 0, 'H'},
411                 {"critical-time", required_argument, 0, 'c'},
412                 {"warning-time", required_argument, 0, 'w'},
413                 {"critical-codes", required_argument, 0, 'C'},
414                 {"warning-codes", required_argument, 0, 'W'},
415                 {"timeout", required_argument, 0, 't'},
416                 {"protocol", required_argument, 0, 'P'},
417                 {"port", required_argument, 0, 'p'},
418                 {"send", required_argument, 0, 's'},
419                 {"expect", required_argument, 0, 'e'},
420                 {"maxbytes", required_argument, 0, 'm'},
421                 {"quit", required_argument, 0, 'q'},
422                 {"jail", required_argument, 0, 'j'},
423                 {"delay", required_argument, 0, 'd'},
424                 {"refuse", required_argument, 0, 'r'},
425                 {"mismatch", required_argument, 0, 'M'},
426                 {"use-ipv4", no_argument, 0, '4'},
427                 {"use-ipv6", no_argument, 0, '6'},
428                 {"verbose", no_argument, 0, 'v'},
429                 {"version", no_argument, 0, 'V'},
430                 {"help", no_argument, 0, 'h'},
431 #ifdef HAVE_SSL
432                 {"ssl", no_argument, 0, 'S'},
433                 {"certificate", required_argument, 0, 'D'},
434 #endif
435                 {0, 0, 0, 0}
436         };
438         if (argc < 2)
439                 usage4 (_("No arguments found"));
441         /* backwards compatibility */
442         for (c = 1; c < argc; c++) {
443                 if (strcmp ("-to", argv[c]) == 0)
444                         strcpy (argv[c], "-t");
445                 else if (strcmp ("-wt", argv[c]) == 0)
446                         strcpy (argv[c], "-w");
447                 else if (strcmp ("-ct", argv[c]) == 0)
448                         strcpy (argv[c], "-c");
449         }
451         if (!is_option (argv[1])) {
452                 server_address = argv[1];
453                 argv[1] = argv[0];
454                 argv = &argv[1];
455                 argc--;
456         }
458         while (1) {
459                 c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:",
460                                  longopts, &option);
462                 if (c == -1 || c == EOF || c == 1)
463                         break;
465                 switch (c) {
466                 case '?':                 /* print short usage statement if args not parsable */
467                         usage2 (_("Unknown argument"), optarg);
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, _("Invalid option - SSL is not available"));
587 #endif
588                         break;
589                 }
590         }
592         if (server_address == NULL)
593                 usage4 (_("You must provide a server address"));
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);