Code

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