Code

initial "experimental" support for gnutls. by default openssl is still
[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_GNUTLS_OPENSSL_H
32 #  include <gnutls/openssl.h>
33 #else
34 #  ifdef HAVE_SSL_H
35 #    include <rsa.h>
36 #    include <crypto.h>
37 #    include <x509.h>
38 #    include <pem.h>
39 #    include <ssl.h>
40 #    include <err.h>
41 #  else
42 #    ifdef HAVE_OPENSSL_SSL_H
43 #      include <openssl/rsa.h>
44 #      include <openssl/crypto.h>
45 #      include <openssl/x509.h>
46 #      include <openssl/pem.h>
47 #      include <openssl/ssl.h>
48 #      include <openssl/err.h>
49 #    endif
50 #  endif
51 #endif
53 #ifdef HAVE_SSL
54 static int check_cert = FALSE;
55 static int days_till_exp;
56 static char *randbuff = "";
57 static SSL_CTX *ctx;
58 static SSL *ssl;
59 static X509 *server_cert;
60 static int connect_SSL (void);
61 # ifdef USE_OPENSSL
62 static int check_certificate (X509 **);
63 # endif /* USE_OPENSSL */
64 # define my_recv(buf, len) ((flags & FLAG_SSL) ? SSL_read(ssl, buf, len) : read(sd, buf, len))
65 #else
66 # define my_recv(buf, len) read(sd, buf, len)
67 #endif
70 /* int my_recv(char *, size_t); */
71 static int process_arguments (int, char **);
72 void print_help (void);
73 void print_usage (void);
75 #define EXPECT server_expect[0]
76 static char *SERVICE = "TCP";
77 static char *SEND = NULL;
78 static char *QUIT = NULL;
79 static int PROTOCOL = IPPROTO_TCP; /* most common is default */
80 static int PORT = 0;
82 static char timestamp[17] = "";
83 static int server_port = 0;
84 static char *server_address = NULL;
85 static char *server_send = NULL;
86 static char *server_quit = NULL;
87 static char **server_expect;
88 static size_t server_expect_count = 0;
89 static size_t maxbytes = 0;
90 static char **warn_codes = NULL;
91 static size_t warn_codes_count = 0;
92 static char **crit_codes = NULL;
93 static size_t crit_codes_count = 0;
94 static unsigned int delay = 0;
95 static double warning_time = 0;
96 static double critical_time = 0;
97 static double elapsed_time = 0;
98 static long microsec;
99 static int sd = 0;
100 #define MAXBUF 1024
101 static char buffer[MAXBUF];
102 static int expect_mismatch_state = STATE_WARNING;
104 #define FLAG_SSL 0x01
105 #define FLAG_VERBOSE 0x02
106 #define FLAG_EXACT_MATCH 0x04
107 #define FLAG_TIME_WARN 0x08
108 #define FLAG_TIME_CRIT 0x10
109 #define FLAG_HIDE_OUTPUT 0x20
110 static size_t flags = FLAG_EXACT_MATCH;
112 int
113 main (int argc, char **argv)
115         int result = STATE_UNKNOWN;
116         int i;
117         char *status = NULL;
118         struct timeval tv;
119         size_t len, match = -1;
121         setlocale (LC_ALL, "");
122         bindtextdomain (PACKAGE, LOCALEDIR);
123         textdomain (PACKAGE);
125         /* determine program- and service-name quickly */
126         progname = strrchr(argv[0], '/');
127         if(progname != NULL) progname++;
128         else progname = argv[0];
130         len = strlen(progname);
131         if(len > 6 && !memcmp(progname, "check_", 6)) {
132                 SERVICE = progname + 6;
133                 for(i = 0; i < len - 6; i++)
134                         SERVICE[i] = toupper(SERVICE[i]);
135         }
137         /* set up a resonable buffer at first (will be realloc()'ed if
138          * user specifies other options) */
139         server_expect = calloc(sizeof(char *), 2);
141         /* determine defaults for this service's protocol */
142         if (!strncmp(SERVICE, "UDP", 3)) {
143                 PROTOCOL = IPPROTO_UDP;
144         }
145         else if (!strncmp(SERVICE, "FTP", 3)) {
146                 EXPECT = "220";
147                 QUIT = "QUIT\r\n";
148                 PORT = 21;
149         }
150         else if (!strncmp(SERVICE, "POP", 3) || !strncmp(SERVICE, "POP3", 4)) {
151                 EXPECT = "+OK";
152                 QUIT = "QUIT\r\n";
153                 PORT = 110;
154         }
155         else if (!strncmp(SERVICE, "SMTP", 4)) {
156                 EXPECT = "220";
157                 QUIT = "QUIT\r\n";
158                 PORT = 25;
159         }
160         else if (!strncmp(SERVICE, "IMAP", 4)) {
161                 EXPECT = "* OK";
162                 QUIT = "a1 LOGOUT\r\n";
163                 PORT = 143;
164         }
165 #ifdef HAVE_SSL
166         else if (!strncmp(SERVICE, "SIMAP", 5)) {
167                 EXPECT = "* OK";
168                 QUIT = "a1 LOGOUT\r\n";
169                 flags |= FLAG_SSL;
170                 PORT = 993;
171         }
172         else if (!strncmp(SERVICE, "SPOP", 4)) {
173                 EXPECT = "+OK";
174                 QUIT = "QUIT\r\n";
175                 flags |= FLAG_SSL;
176                 PORT = 995;
177         }
178         else if (!strncmp(SERVICE, "SSMTP", 5)) {
179                 EXPECT = "220";
180                 QUIT = "QUIT\r\n";
181                 flags |= FLAG_SSL;
182                 PORT = 465;
183         }
184         else if (!strncmp(SERVICE, "JABBER", 6)) {
185                 SEND = "<stream:stream to=\'host\' xmlns=\'jabber:client\' xmlns:stream=\'http://etherx.jabber.org/streams\'>\n";
186                 EXPECT = "<?xml version=\'1.0\'?><stream:stream xmlns:stream=\'http://etherx.jabber.org/streams\'";
187                 QUIT = "</stream:stream>\n";
188                 flags |= FLAG_SSL | FLAG_HIDE_OUTPUT;
189                 PORT = 5222;
190         }
191         else if (!strncmp (SERVICE, "NNTPS", 5)) {
192                 server_expect_count = 2;
193                 server_expect[0] = "200";
194                 server_expect[1] = "201";
195                 QUIT = "QUIT\r\n";
196                 flags |= FLAG_SSL;
197                 PORT = 563;
198         }
199 #endif
200         else if (!strncmp (SERVICE, "NNTP", 4)) {
201                 server_expect_count = 2;
202                 server_expect = malloc(sizeof(char *) * server_expect_count);
203                 server_expect[0] = strdup("200");
204                 server_expect[1] = strdup("201");
205                 QUIT = "QUIT\r\n";
206                 PORT = 119;
207         }
208         /* fallthrough check, so it's supposed to use reverse matching */
209         else if (strcmp (SERVICE, "TCP"))
210                 usage (_("CRITICAL - Generic check_tcp called with unknown service\n"));
212         server_address = "127.0.0.1";
213         server_port = PORT;
214         server_send = SEND;
215         server_quit = QUIT;
216         status = NULL;
218         if (process_arguments (argc, argv) == ERROR)
219                 usage4 (_("Could not parse arguments"));
221         if(flags & FLAG_VERBOSE) {
222                 printf("Using service %s\n", SERVICE);
223                 printf("Port: %d\n", PORT);
224                 printf("flags: 0x%x\n", flags);
225         }
227         if(EXPECT && !server_expect_count)
228                 server_expect_count++;
230         /* set up the timer */
231         signal (SIGALRM, socket_timeout_alarm_handler);
232         alarm (socket_timeout);
234         /* try to connect to the host at the given port number */
235         gettimeofday (&tv, NULL);
236 #ifdef HAVE_SSL
237         if (flags & FLAG_SSL && check_cert == TRUE) {
238                 if (connect_SSL () != OK)
239                         die (STATE_CRITICAL,_("CRITICAL - Could not make SSL connection\n"));
240 #  ifdef USE_OPENSSL /* XXX gnutls does cert checking differently */
241                 if ((server_cert = SSL_get_peer_certificate (ssl)) != NULL) {
242                         result = check_certificate (&server_cert);
243                         X509_free(server_cert);
244                 }
245                 else {
246                         printf(_("CRITICAL - Cannot retrieve server certificate.\n"));
247                         result = STATE_CRITICAL;
248                 }
249 #  endif /* USE_OPENSSL */
251                 SSL_shutdown (ssl);
252                 SSL_free (ssl);
253                 SSL_CTX_free (ctx);
254                 close (sd);
255                 return result;
256         }
257         else if (flags & FLAG_SSL)
258                 result = connect_SSL ();
259         else
260 #endif
261                 result = np_net_connect (server_address, server_port, &sd, PROTOCOL);
263         if (result == STATE_CRITICAL)
264                 return STATE_CRITICAL;
266         if (server_send != NULL) {              /* Something to send? */
267 #ifdef HAVE_SSL
268                 if (flags & FLAG_SSL)
269                         SSL_write(ssl, server_send, (int)strlen(server_send));
270                 else
271 #endif
272                         send (sd, server_send, strlen(server_send), 0);
273         }
275         if (delay > 0) {
276                 tv.tv_sec += delay;
277                 sleep (delay);
278         }
280         if(flags & FLAG_VERBOSE) {
281                 printf("server_expect_count: %d\n", server_expect_count);
282                 for(i = 0; i < server_expect_count; i++)
283                         printf("\t%d: %s\n", i, server_expect[i]);
284         }
286         /* if(len) later on, we know we have a non-NULL response */
287         len = 0;
288         if (server_expect_count) {
290                 /* watch for the expect string */
291                 while ((i = my_recv(buffer, sizeof(buffer))) > 0) {
292                         status = realloc(status, len + i + 1);
293                         memcpy(&status[len], buffer, i);
294                         len += i;
296                         /* stop reading if user-forced or data-starved */
297                         if(i < sizeof(buffer) || (maxbytes && len >= maxbytes))
298                                 break;
300                         if (maxbytes && len >= maxbytes)
301                                 break;
302                 }
304                 /* no data when expected, so return critical */
305                 if (len == 0)
306                         die (STATE_CRITICAL, _("No data received from host\n"));
308                 /* force null-termination and strip whitespace from end of output */
309                 status[len--] = '\0';
310                 /* print raw output if we're debugging */
311                 if(flags & FLAG_VERBOSE)
312                         printf("received %d bytes from host\n#-raw-recv-------#\n%s\n#-raw-recv-------#\n",
313                                len + 1, status);
314                 while(isspace(status[len])) status[len--] = '\0';
316                 for (i = 0; i < server_expect_count; i++) {
317                         match = -2;             /* tag it so we know if we tried and failed */
318                         if (flags & FLAG_VERBOSE)
319                                 printf ("looking for [%s] %s [%s]\n", server_expect[i],
320                                         (flags & FLAG_EXACT_MATCH) ? "in beginning of" : "anywhere in",
321                                         status);
323                         /* match it. math first in short-circuit */
324                         if ((flags & FLAG_EXACT_MATCH && !strncmp(status, server_expect[i], strlen(server_expect[i]))) ||
325                             (!(flags & FLAG_EXACT_MATCH) && strstr(status, server_expect[i])))
326                         {
327                                 if(flags & FLAG_VERBOSE) puts("found it");
328                                 match = i;
329                                 break;
330                         }
331                 }
332         }
334         if (server_quit != NULL) {
335 #ifdef HAVE_SSL
336                 if (flags & FLAG_SSL) {
337                         SSL_write (ssl, server_quit, (int)strlen(server_quit));
338                         SSL_shutdown (ssl);
339                         SSL_free (ssl);
340                         SSL_CTX_free (ctx);
341                 }
342                 else
343 #endif
344                         send (sd, server_quit, strlen (server_quit), 0);
345         }
347         /* close the connection */
348         if (sd)
349                 close (sd);
351         microsec = deltime (tv);
352         elapsed_time = (double)microsec / 1.0e6;
354         if (flags & FLAG_TIME_CRIT && elapsed_time > critical_time)
355                 result = STATE_CRITICAL;
356         else if (flags & FLAG_TIME_WARN && elapsed_time > warning_time)
357                 result = STATE_WARNING;
359         /* did we get the response we hoped? */
360         if(match == -2 && result != STATE_CRITICAL)
361                 result = STATE_WARNING;
363         /* reset the alarm */
364         alarm (0);
366         /* this is a bit stupid, because we don't want to print the
367          * response time (which can look ok to the user) if we didn't get
368          * the response we were looking for. if-else */
369         printf(_("%s %s - "), SERVICE, state_text(result));
371         if(match == -2 && len && !(flags & FLAG_HIDE_OUTPUT))
372                 printf("Unexpected response from host: %s", status);
373         else
374                 printf("%.3f second response time on port %d",
375                        elapsed_time, server_port);
377         if (match != -2 && !(flags & FLAG_HIDE_OUTPUT) && len)
378                 printf (" [%s]", status);
380         /* perf-data doesn't apply when server doesn't talk properly,
381          * so print all zeroes on warn and crit */
382         if(match == -2)
383                 printf ("|time=%fs;0.0;0.0;0.0;0.0", elapsed_time);
384         else
385                 printf("|%s",
386                                 fperfdata ("time", elapsed_time, "s",
387                                    TRUE, warning_time,
388                                    TRUE, critical_time,
389                                    TRUE, 0,
390                                    TRUE, socket_timeout)
391                       );
393         putchar('\n');
394         return result;
399 /* process command-line arguments */
400 static int
401 process_arguments (int argc, char **argv)
403         int c;
405         int option = 0;
406         static struct option longopts[] = {
407                 {"hostname", required_argument, 0, 'H'},
408                 {"critical-time", required_argument, 0, 'c'},
409                 {"warning-time", required_argument, 0, 'w'},
410                 {"critical-codes", required_argument, 0, 'C'},
411                 {"warning-codes", required_argument, 0, 'W'},
412                 {"timeout", required_argument, 0, 't'},
413                 {"protocol", required_argument, 0, 'P'},
414                 {"port", required_argument, 0, 'p'},
415                 {"send", required_argument, 0, 's'},
416                 {"expect", required_argument, 0, 'e'},
417                 {"maxbytes", required_argument, 0, 'm'},
418                 {"quit", required_argument, 0, 'q'},
419                 {"jail", required_argument, 0, 'j'},
420                 {"delay", required_argument, 0, 'd'},
421                 {"refuse", required_argument, 0, 'r'},
422                 {"mismatch", required_argument, 0, 'M'},
423                 {"use-ipv4", no_argument, 0, '4'},
424                 {"use-ipv6", no_argument, 0, '6'},
425                 {"verbose", no_argument, 0, 'v'},
426                 {"version", no_argument, 0, 'V'},
427                 {"help", no_argument, 0, 'h'},
428 #ifdef HAVE_SSL
429                 {"ssl", no_argument, 0, 'S'},
430                 {"certificate", required_argument, 0, 'D'},
431 #endif
432                 {0, 0, 0, 0}
433         };
435         if (argc < 2)
436                 usage4 (_("No arguments found"));
438         /* backwards compatibility */
439         for (c = 1; c < argc; c++) {
440                 if (strcmp ("-to", argv[c]) == 0)
441                         strcpy (argv[c], "-t");
442                 else if (strcmp ("-wt", argv[c]) == 0)
443                         strcpy (argv[c], "-w");
444                 else if (strcmp ("-ct", argv[c]) == 0)
445                         strcpy (argv[c], "-c");
446         }
448         if (!is_option (argv[1])) {
449                 server_address = argv[1];
450                 argv[1] = argv[0];
451                 argv = &argv[1];
452                 argc--;
453         }
455         while (1) {
456                 c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:",
457                                  longopts, &option);
459                 if (c == -1 || c == EOF || c == 1)
460                         break;
462                 switch (c) {
463                 case '?':                 /* print short usage statement if args not parsable */
464                         usage2 (_("Unknown argument"), optarg);
465                 case 'h':                 /* help */
466                         print_help ();
467                         exit (STATE_OK);
468                 case 'V':                 /* version */
469                         print_revision (progname, revision);
470                         exit (STATE_OK);
471                 case 'v':                 /* verbose mode */
472                         flags |= FLAG_VERBOSE;
473                         break;
474                 case '4':
475                         address_family = AF_INET;
476                         break;
477                 case '6':
478 #ifdef USE_IPV6
479                         address_family = AF_INET6;
480 #else
481                         usage4 (_("IPv6 support not available"));
482 #endif
483                         break;
484                 case 'H':                 /* hostname */
485                         if (is_host (optarg) == FALSE)
486                                 usage2 (_("Invalid hostname/address"), optarg);
487                         server_address = optarg;
488                         break;
489                 case 'c':                 /* critical */
490                         if (!is_intnonneg (optarg))
491                                 usage4 (_("Critical threshold must be a positive integer"));
492                         else
493                                 critical_time = strtod (optarg, NULL);
494                         flags |= FLAG_TIME_CRIT;
495                         break;
496                 case 'j':                 /* hide output */
497                         flags |= FLAG_HIDE_OUTPUT;
498                         break;
499                 case 'w':                 /* warning */
500                         if (!is_intnonneg (optarg))
501                                 usage4 (_("Warning threshold must be a positive integer"));
502                         else
503                                 warning_time = strtod (optarg, NULL);
504                         flags |= FLAG_TIME_WARN;
505                         break;
506                 case 'C':
507                         crit_codes = realloc (crit_codes, ++crit_codes_count);
508                         crit_codes[crit_codes_count - 1] = optarg;
509                         break;
510                 case 'W':
511                         warn_codes = realloc (warn_codes, ++warn_codes_count);
512                         warn_codes[warn_codes_count - 1] = optarg;
513                         break;
514                 case 't':                 /* timeout */
515                         if (!is_intpos (optarg))
516                                 usage4 (_("Timeout interval must be a positive integer"));
517                         else
518                                 socket_timeout = atoi (optarg);
519                         break;
520                 case 'p':                 /* port */
521                         if (!is_intpos (optarg))
522                                 usage4 (_("Port must be a positive integer"));
523                         else
524                                 server_port = atoi (optarg);
525                         break;
526                 case 's':
527                         server_send = optarg;
528                         break;
529                 case 'e': /* expect string (may be repeated) */
530                         EXPECT = NULL;
531                         flags &= ~FLAG_EXACT_MATCH;
532                         if (server_expect_count == 0)
533                                 server_expect = malloc (sizeof (char *) * (++server_expect_count));
534                         else
535                                 server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
536                         server_expect[server_expect_count - 1] = optarg;
537                         break;
538                 case 'm':
539                         if (!is_intpos (optarg))
540                                 usage4 (_("Maxbytes must be a positive integer"));
541                         else
542                                 maxbytes = strtol (optarg, NULL, 0);
543                 case 'q':
544                         asprintf(&server_quit, "%s\r\n", optarg);
545                         break;
546                 case 'r':
547                         if (!strncmp(optarg,"ok",2))
548                                 econn_refuse_state = STATE_OK;
549                         else if (!strncmp(optarg,"warn",4))
550                                 econn_refuse_state = STATE_WARNING;
551                         else if (!strncmp(optarg,"crit",4))
552                                 econn_refuse_state = STATE_CRITICAL;
553                         else
554                                 usage4 (_("Refuse must be one of ok, warn, crit"));
555                         break;
556                 case 'M':
557                         if (!strncmp(optarg,"ok",2))
558                                 expect_mismatch_state = STATE_OK;
559                         else if (!strncmp(optarg,"warn",4))
560                                 expect_mismatch_state = STATE_WARNING;
561                         else if (!strncmp(optarg,"crit",4))
562                                 expect_mismatch_state = STATE_CRITICAL;
563                         else
564                                 usage4 (_("Mismatch must be one of ok, warn, crit"));
565                         break;
566                 case 'd':
567                         if (is_intpos (optarg))
568                                 delay = atoi (optarg);
569                         else
570                                 usage4 (_("Delay must be a positive integer"));
571                         break;
572                 case 'D': /* Check SSL cert validity - days 'til certificate expiration */
573 #ifdef HAVE_SSL
574 #  ifdef USE_OPENSSL /* XXX */
575                         if (!is_intnonneg (optarg))
576                                 usage2 (_("Invalid certificate expiration period"), optarg);
577                         days_till_exp = atoi (optarg);
578                         check_cert = TRUE;
579                         flags |= FLAG_SSL;
580                         break;
581 #  endif /* USE_OPENSSL */
582 #endif
583                         /* fallthrough if we don't have ssl */
584                 case 'S':
585 #ifdef HAVE_SSL
586                         flags |= FLAG_SSL;
587 #else
588                         die (STATE_UNKNOWN, _("Invalid option - SSL is not available"));
589 #endif
590                         break;
591                 }
592         }
594         if (server_address == NULL)
595                 usage4 (_("You must provide a server address"));
597         return TRUE;
601 /* SSL-specific functions */
602 #ifdef HAVE_SSL
603 static int
604 connect_SSL (void)
606   SSL_METHOD *meth;
608   /* Initialize SSL context */
609   SSLeay_add_ssl_algorithms ();
610   meth = SSLv23_client_method ();
611   SSL_load_error_strings ();
612   OpenSSL_add_all_algorithms();
613   if ((ctx = SSL_CTX_new (meth)) == NULL)
614     {
615       printf (_("CRITICAL - Cannot create SSL context.\n"));
616       return STATE_CRITICAL;
617     }
619   /* Initialize alarm signal handling */
620   signal (SIGALRM, socket_timeout_alarm_handler);
622   /* Set socket timeout */
623   alarm (socket_timeout);
625   /* Save start time */
626   time (&start_time);
628   /* Make TCP connection */
629   if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
630     {
631     /* Do the SSL handshake */
632       if ((ssl = SSL_new (ctx)) != NULL)
633       {
634         SSL_set_fd (ssl, sd);
635         if (SSL_connect(ssl) == 1)
636           return OK;
637         /* ERR_print_errors_fp (stderr); */
638         printf (_("CRITICAL - Cannot make  SSL connection "));
639 #ifdef USE_OPENSSL /* XXX */
640         ERR_print_errors_fp (stdout);
641 #endif /* USE_OPENSSL */
642         /* printf("\n"); */
643       }
644       else
645       {
646         printf (_("CRITICAL - Cannot initiate SSL handshake.\n"));
647       }
648       SSL_free (ssl);
649     }
651   SSL_CTX_free (ctx);
652   close (sd);
654   return STATE_CRITICAL;
657 #ifdef USE_OPENSSL /* XXX */
658 static int
659 check_certificate (X509 ** certificate)
661   ASN1_STRING *tm;
662   int offset;
663   struct tm stamp;
664   int days_left;
667   /* Retrieve timestamp of certificate */
668   tm = X509_get_notAfter (*certificate);
670   /* Generate tm structure to process timestamp */
671   if (tm->type == V_ASN1_UTCTIME) {
672     if (tm->length < 10) {
673       printf (_("CRITICAL - Wrong time format in certificate.\n"));
674       return STATE_CRITICAL;
675     }
676     else {
677       stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
678       if (stamp.tm_year < 50)
679         stamp.tm_year += 100;
680       offset = 0;
681     }
682   }
683   else {
684     if (tm->length < 12) {
685       printf (_("CRITICAL - Wrong time format in certificate.\n"));
686       return STATE_CRITICAL;
687     }
688     else {
689                         stamp.tm_year =
690                           (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
691                           (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
692                         stamp.tm_year -= 1900;
693                         offset = 2;
694     }
695   }
696         stamp.tm_mon =
697           (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
698         stamp.tm_mday =
699           (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
700         stamp.tm_hour =
701           (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
702         stamp.tm_min =
703           (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
704         stamp.tm_sec = 0;
705         stamp.tm_isdst = -1;
707         days_left = (mktime (&stamp) - time (NULL)) / 86400;
708         snprintf
709           (timestamp, 16, "%02d/%02d/%04d %02d:%02d",
710            stamp.tm_mon + 1,
711            stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
713         if (days_left > 0 && days_left <= days_till_exp) {
714           printf (_("Certificate expires in %d day(s) (%s).\n"), days_left, timestamp);
715           return STATE_WARNING;
716         }
717         if (days_left < 0) {
718           printf (_("Certificate expired on %s.\n"), timestamp);
719           return STATE_CRITICAL;
720         }
722         if (days_left == 0) {
723           printf (_("Certificate expires today (%s).\n"), timestamp);
724           return STATE_WARNING;
725         }
727         printf (_("Certificate will expire on %s.\n"), timestamp);
729         return STATE_OK;
731 #  endif /* USE_OPENSSL */
732 #endif /* HAVE_SSL */
735 void
736 print_help (void)
738         print_revision (progname, revision);
740         printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
741         printf (COPYRIGHT, copyright, email);
743         printf (_("This plugin tests %s connections with the specified host.\n\n"),
744                 SERVICE);
746         print_usage ();
748         printf (_(UT_HELP_VRSN));
750         printf (_(UT_HOST_PORT), 'p', "none");
752         printf (_(UT_IPv46));
754         printf (_("\
755  -s, --send=STRING\n\
756     String to send to the server\n\
757  -e, --expect=STRING\n\
758     String to expect in server response\n\
759  -q, --quit=STRING\n\
760     String to send server to initiate a clean close of the connection\n"));
762         printf (_("\
763  -r, --refuse=ok|warn|crit\n\
764     Accept tcp refusals with states ok, warn, crit (default: crit)\n\
765  -M, --mismatch=ok|warn|crit\n\
766     Accept expected string mismatches with states ok, warn, crit (default: warn)\n\
767  -j, --jail\n\
768     Hide output from TCP socket\n\
769  -m, --maxbytes=INTEGER\n\
770     Close connection once more than this number of bytes are received\n\
771  -d, --delay=INTEGER\n\
772     Seconds to wait between sending string and polling for response\n"));
774 #ifdef HAVE_SSL
775         printf (_("\
776  -D, --certificate=INTEGER\n\
777     Minimum number of days a certificate has to be valid.\n\
778  -S, --ssl\n\
779     Use SSL for the connection.\n"));
780 #endif
782         printf (_(UT_WARN_CRIT));
784         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
786         printf (_(UT_VERBOSE));
788         printf (_(UT_SUPPORT));
792 void
793 print_usage (void)
795         printf ("\
796 Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\
797                   [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
798                   [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
799                   [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]\n\
800                   [-D <days to cert expiry>] [-S <use SSL>]\n", progname);