Code

mark for translation
[nagiosplug.git] / plugins / check_tcp.c
index cbe859568a7b051cda21cf963a4848b4630fca9a..eb31774d1fa4f01f54b9f6aec5c38014f5449db1 100644 (file)
@@ -1,55 +1,41 @@
-/******************************************************************************
- *
- * This file is part of the Nagios Plugins.
- *
- * Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>
- *
- * The Nagios Plugins are free software; you can redistribute them
- * and/or modify them under the terms of the GNU General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id$
- *
- *****************************************************************************/
-
-#define PROGRAM check_tcp
-#define REVISION "$Revision$"
-#define DESCRIPTION "Check a TCP port"
-#define AUTHOR "Ethan Galstad"
-#define EMAIL "nagios@nagios.org"
-#define COPYRIGHTDATE "2002"
-
+/*****************************************************************************
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation; either version 2 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software
+* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*
+*****************************************************************************/
 #include "config.h"
 #include "common.h"
 #include "netutils.h"
 #include "utils.h"
 
 #ifdef HAVE_SSL_H
-#include <rsa.h>
-#include <crypto.h>
-#include <x509.h>
-#include <pem.h>
-#include <ssl.h>
-#include <err.h>
-#endif
-
-#ifdef HAVE_OPENSSL_SSL_H
-#include <openssl/rsa.h>
-#include <openssl/crypto.h>
-#include <openssl/x509.h>
-#include <openssl/pem.h>
-#include <openssl/ssl.h>
-#include <openssl/err.h>
+#  include <rsa.h>
+#  include <crypto.h>
+#  include <x509.h>
+#  include <pem.h>
+#  include <ssl.h>
+#  include <err.h>
+#else
+#  ifdef HAVE_OPENSSL_SSL_H
+#    include <openssl/rsa.h>
+#    include <openssl/crypto.h>
+#    include <openssl/x509.h>
+#    include <openssl/pem.h>
+#    include <openssl/ssl.h>
+#    include <openssl/err.h>
+#  endif
 #endif
 
 #ifdef HAVE_SSL
@@ -58,14 +44,17 @@ SSL *ssl;
 int connect_SSL (void);
 #endif
 
-#define TCP_PROTOCOL 1
-#define UDP_PROTOCOL 2
+enum {
+       TCP_PROTOCOL = 1,
+       UDP_PROTOCOL = 2,
+       MAXBUF = 1024
+};
 
 int process_arguments (int, char **);
 void print_usage (void);
 void print_help (void);
+int my_recv (void);
 
-char *PROGNAME = NULL;
 char *SERVICE = NULL;
 char *SEND = NULL;
 char *EXPECT = NULL;
@@ -79,6 +68,7 @@ char *server_send = NULL;
 char *server_quit = NULL;
 char **server_expect = NULL;
 int server_expect_count = 0;
+int maxbytes = 0;
 char **warn_codes = NULL;
 int warn_codes_count = 0;
 char **crit_codes = NULL;
@@ -92,19 +82,84 @@ double elapsed_time = 0;
 int verbose = FALSE;
 int use_ssl = FALSE;
 int sd = 0;
+char *buffer = "";
+
+/* progname changes depending on symlink called */
+char *progname = "check_tcp";
+const char *revision = "$Revision$";
+const char *copyright = "2002-2003";
+const char *email = "nagiosplug-devel@lists.sourceforge.net";
+\f
+
+
+
+void
+print_usage (void)
+{
+       printf (_("\
+Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\
+       [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
+       [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
+       [-r <refuse state>] [-v] [-4|-6]\n"), progname);
+       printf ("       %s (-h|--help)\n", progname);
+       printf ("       %s (-V|--version)\n", progname);
+}
+void
+print_help (void)
+{
+       print_revision (progname, revision);
+
+       printf (_(COPYRIGHT), copyright, email);
+
+       printf (_("This plugin tests %s connections with the specified host.\n\n"),
+               SERVICE);
+
+       print_usage ();
+
+       printf (_(HELP_VRSN));
+
+       printf (_(HOST_PORT_46), 'p', "none");
+
+       printf (_("\
+ -s, --send=STRING\n\
+    String to send to the server\n\
+ -e, --expect=STRING\n\
+    String to expect in server response\n\
+ -q, --quit=STRING\n\
+    String to send server to initiate a clean close of the connection\n"));
+
+       printf (_("\
+ -r, --refuse=ok|warn|crit\n\
+    Accept tcp refusals with states ok, warn, crit (default: crit)\n\
+ -m, --maxbytes=INTEGER\n\
+    Close connection once more than this number of bytes are received\n\
+ -d, --delay=INTEGER\n\
+    Seconds to wait between sending string and polling for response\n"));
+
+       printf (_(WARN_CRIT_TO), DEFAULT_SOCKET_TIMEOUT);
+
+       printf (_(VRBS));
+
+       support ();
+}
+\f
+
 
 int
 main (int argc, char **argv)
 {
        int result;
        int i;
-       char *buffer = "";
        char *status = "";
        struct timeval tv;
 
+       setlocale (LC_ALL, "");
+       bindtextdomain (PACKAGE, LOCALEDIR);
+       textdomain (PACKAGE);
+
        if (strstr (argv[0], "check_udp")) {
-               asprintf (&PROGNAME, "check_udp");
-               asprintf (&SERVICE, "UDP");
+               progname = strdup ("check_udp");
+               SERVICE = strdup ("UDP");
                SEND = NULL;
                EXPECT = NULL;
                QUIT = NULL;
@@ -112,8 +167,8 @@ main (int argc, char **argv)
                PORT = 0;
        }
        else if (strstr (argv[0], "check_tcp")) {
-               asprintf (&PROGNAME, "check_tcp");
-               asprintf (&SERVICE, "TCP");
+               progname = strdup ("check_tcp");
+               SERVICE = strdup ("TCP");
                SEND = NULL;
                EXPECT = NULL;
                QUIT = NULL;
@@ -121,66 +176,66 @@ main (int argc, char **argv)
                PORT = 0;
        }
        else if (strstr (argv[0], "check_ftp")) {
-               asprintf (&PROGNAME, "check_ftp");
-               asprintf (&SERVICE, "FTP");
+               progname = strdup ("check_ftp");
+               SERVICE = strdup ("FTP");
                SEND = NULL;
-               asprintf (&EXPECT, "220");
-               asprintf (&QUIT, "QUIT\r\n");
+               EXPECT = strdup ("220");
+               QUIT = strdup ("QUIT\r\n");
                PROTOCOL = TCP_PROTOCOL;
                PORT = 21;
        }
        else if (strstr (argv[0], "check_smtp")) {
-               asprintf (&PROGNAME, "check_smtp");
-               asprintf (&SERVICE, "SMTP");
+               progname = strdup ("check_smtp");
+               SERVICE = strdup ("SMTP");
                SEND = NULL;
-               asprintf (&EXPECT, "220");
-               asprintf (&QUIT, "QUIT\r\n");
+               EXPECT = strdup ("220");
+               QUIT = strdup ("QUIT\r\n");
                PROTOCOL = TCP_PROTOCOL;
                PORT = 25;
        }
        else if (strstr (argv[0], "check_pop")) {
-               asprintf (&PROGNAME, "check_pop");
-               asprintf (&SERVICE, "POP");
+               progname = strdup ("check_pop");
+               SERVICE = strdup ("POP");
                SEND = NULL;
-               asprintf (&EXPECT, "+OK");
-               asprintf (&QUIT, "QUIT\r\n");
+               EXPECT = strdup ("+OK");
+               QUIT = strdup ("QUIT\r\n");
                PROTOCOL = TCP_PROTOCOL;
                PORT = 110;
        }
        else if (strstr (argv[0], "check_imap")) {
-               asprintf (&PROGNAME, "check_imap");
-               asprintf (&SERVICE, "IMAP");
+               progname = strdup ("check_imap");
+               SERVICE = strdup ("IMAP");
                SEND = NULL;
-               asprintf (&EXPECT, "* OK");
-               asprintf (&QUIT, "a1 LOGOUT\r\n");
+               EXPECT = strdup ("* OK");
+               QUIT = strdup ("a1 LOGOUT\r\n");
                PROTOCOL = TCP_PROTOCOL;
                PORT = 143;
        }
 #ifdef HAVE_SSL
        else if (strstr(argv[0],"check_simap")) {
-               asprintf (&PROGNAME, "check_simap");
-               asprintf (&SERVICE, "SIMAP");
+               progname = strdup ("check_simap");
+               SERVICE = strdup ("SIMAP");
                SEND=NULL;
-               asprintf (&EXPECT, "* OK");
-               asprintf (&QUIT, "a1 LOGOUT\r\n");
+               EXPECT = strdup ("* OK");
+               QUIT = strdup ("a1 LOGOUT\r\n");
                PROTOCOL=TCP_PROTOCOL;
                use_ssl=TRUE;
                PORT=993;
        }
        else if (strstr(argv[0],"check_spop")) {
-               asprintf (&PROGNAME, "check_spop");
-               asprintf (&SERVICE, "SPOP");
+               progname = strdup ("check_spop");
+               SERVICE = strdup ("SPOP");
                SEND=NULL;
-               asprintf (&EXPECT, "110");
-               asprintf (&QUIT, "QUIT\r\n");
+               EXPECT = strdup ("+OK");
+               QUIT = strdup ("QUIT\r\n");
                PROTOCOL=TCP_PROTOCOL;
                use_ssl=TRUE;
                PORT=995;
        }
 #endif
        else if (strstr (argv[0], "check_nntp")) {
-               asprintf (&PROGNAME, "check_nntp");
-               asprintf (&SERVICE, "NNTP");
+               progname = strdup ("check_nntp");
+               SERVICE = strdup ("NNTP");
                SEND = NULL;
                EXPECT = NULL;
                server_expect = realloc (server_expect, ++server_expect_count);
@@ -192,16 +247,16 @@ main (int argc, char **argv)
                PORT = 119;
        }
        else {
-               usage ("ERROR: Generic check_tcp called with unknown service\n");
+               usage (_("ERROR: Generic check_tcp called with unknown service\n"));
        }
 
-       asprintf (&server_address, "127.0.0.1");
+       server_address = strdup ("127.0.0.1");
        server_port = PORT;
        server_send = SEND;
        server_quit = QUIT;
 
        if (process_arguments (argc, argv) == ERROR)
-               usage ("Could not parse arguments\n");
+               usage (_("Could not parse arguments\n"));
 
        /* use default expect if none listed in process_arguments() */
        if (EXPECT && server_expect_count == 0) {
@@ -225,7 +280,8 @@ main (int argc, char **argv)
                {
                        if (PROTOCOL == UDP_PROTOCOL)
                                result = my_udp_connect (server_address, server_port, &sd);
-                       else                                                                                                    /* default is TCP */
+                       else
+                               /* default is TCP */
                                result = my_tcp_connect (server_address, server_port, &sd);
                }
 
@@ -249,19 +305,21 @@ main (int argc, char **argv)
 
        if (server_send || server_expect_count > 0) {
 
-               buffer = malloc (MAX_INPUT_BUFFER);
+               buffer = malloc (MAXBUF);
+               memset (buffer, '\0', MAXBUF);
                /* watch for the expect string */
-#ifdef HAVE_SSL
-               if (use_ssl && SSL_read (ssl, buffer, MAX_INPUT_BUFFER - 1) > 0)
+               while ((i = my_recv ()) > 0) {
+                       buffer[i] = '\0';
                        asprintf (&status, "%s%s", status, buffer);
-               else
-#endif
-                       if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) > 0)
-                               asprintf (&status, "%s%s", status, buffer);
+                       if (buffer[i-2] == '\r' && buffer[i-1] == '\n')
+                               break;
+                       if (maxbytes>0 && strlen(status) >= (unsigned)maxbytes)
+                               break;
+               }
 
                /* return a CRITICAL status if we couldn't read any data */
                if (status == NULL)
-                       terminate (STATE_CRITICAL, "No data received from host\n");
+                       terminate (STATE_CRITICAL, _("No data received from host\n"));
 
                strip (status);
 
@@ -273,24 +331,28 @@ main (int argc, char **argv)
                                if (verbose)
                                        printf ("%d %d\n", i, server_expect_count);
                                if (i >= server_expect_count)
-                                       terminate (STATE_WARNING, "Invalid response from host\n");
+                                       terminate (STATE_WARNING, _("Invalid response from host\n"));
                                if (strstr (status, server_expect[i]))
                                        break;
                        }
                }
        }
 
-       if (server_quit)
+       if (server_quit != NULL) {
 #ifdef HAVE_SSL
                if (use_ssl) {
-                       SSL_write (ssl, QUIT, strlen (QUIT));
+                       SSL_write (ssl, server_quit, strlen (server_quit));
                        SSL_shutdown (ssl);
                        SSL_free (ssl);
                        SSL_CTX_free (ctx);
                }
-               else
+               else {
+#endif
+                       send (sd, server_quit, strlen (server_quit), 0);
+#ifdef HAVE_SSL
+               }
 #endif
-               send (sd, server_quit, strlen (server_quit), 0);
+       }
 
        /* close the connection */
        if (sd)
@@ -307,21 +369,20 @@ main (int argc, char **argv)
        alarm (0);
 
        printf
-               ("%s %s - %7.3f second response time on port %d",
+               (_("%s %s%s - %.3f second response time on port %d"),
                 SERVICE,
-                state_text (result), elapsed_time, server_port);
+                state_text (result),
+                (was_refused) ? " (refused)" : "",
+                elapsed_time, server_port);
 
        if (status && strlen(status) > 0)
                printf (" [%s]", status);
 
-       printf ("|time=%7.3f\n", elapsed_time);
+       printf ("|time=%.3f\n", elapsed_time);
 
        return result;
 }
-
-
-
-
+\f
 
 
 
@@ -331,7 +392,6 @@ process_arguments (int argc, char **argv)
 {
        int c;
 
-#ifdef HAVE_GETOPT_H
        int option_index = 0;
        static struct option long_options[] = {
                {"hostname", required_argument, 0, 'H'},
@@ -344,14 +404,17 @@ process_arguments (int argc, char **argv)
                {"port", required_argument, 0, 'p'},
                {"send", required_argument, 0, 's'},
                {"expect", required_argument, 0, 'e'},
+               {"maxbytes", required_argument, 0, 'm'},
                {"quit", required_argument, 0, 'q'},
                {"delay", required_argument, 0, 'd'},
+               {"refuse", required_argument, 0, 'r'},
+               {"use-ipv4", no_argument, 0, '4'},
+               {"use-ipv6", no_argument, 0, '6'},
                {"verbose", no_argument, 0, 'v'},
                {"version", no_argument, 0, 'V'},
                {"help", no_argument, 0, 'h'},
                {0, 0, 0, 0}
        };
-#endif
 
        if (argc < 2)
                usage ("No arguments found\n");
@@ -374,45 +437,50 @@ process_arguments (int argc, char **argv)
        }
 
        while (1) {
-#ifdef HAVE_GETOPT_H
-               c =
-                       getopt_long (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:S", long_options,
-                                                                        &option_index);
-#else
-               c = getopt (argc, argv, "+hVvH:s:e:q:c:w:t:p:C:W:d:S");
-#endif
+               c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:",
+                                long_options, &option_index);
 
                if (c == -1 || c == EOF || c == 1)
                        break;
 
                switch (c) {
                case '?':                 /* print short usage statement if args not parsable */
-                       printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
+                       printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
                        print_usage ();
                        exit (STATE_UNKNOWN);
                case 'h':                 /* help */
                        print_help ();
                        exit (STATE_OK);
                case 'V':                 /* version */
-                       print_revision (PROGNAME, "$Revision$");
+                       print_revision (progname, "$Revision$");
                        exit (STATE_OK);
                case 'v':                 /* verbose mode */
                        verbose = TRUE;
                        break;
+               case '4':
+                       address_family = AF_INET;
+                       break;
+               case '6':
+#ifdef USE_IPV6
+                       address_family = AF_INET6;
+#else
+                       usage (_("IPv6 support not available\n"));
+#endif
+                       break;
                case 'H':                 /* hostname */
                        if (is_host (optarg) == FALSE)
-                               usage ("Invalid host name/address\n");
+                               usage2 (_("invalid host name or address"), optarg);
                        server_address = optarg;
                        break;
                case 'c':                 /* critical */
                        if (!is_intnonneg (optarg))
-                               usage ("Critical threshold must be a nonnegative integer\n");
+                               usage (_("Critical threshold must be a nonnegative integer\n"));
                        critical_time = strtod (optarg, NULL);
                        check_critical_time = TRUE;
                        break;
                case 'w':                 /* warning */
                        if (!is_intnonneg (optarg))
-                               usage ("Warning threshold must be a nonnegative integer\n");
+                               usage (_("Warning threshold must be a nonnegative integer\n"));
                        warning_time = strtod (optarg, NULL);
                        check_warning_time = TRUE;
                        break;
@@ -426,12 +494,12 @@ process_arguments (int argc, char **argv)
                        break;
                case 't':                 /* timeout */
                        if (!is_intpos (optarg))
-                               usage ("Timeout interval must be a positive integer\n");
+                               usage (_("Timeout interval must be a positive integer\n"));
                        socket_timeout = atoi (optarg);
                        break;
                case 'p':                 /* port */
                        if (!is_intpos (optarg))
-                               usage ("Server port must be a positive integer\n");
+                               usage (_("Server port must be a positive integer\n"));
                        server_port = atoi (optarg);
                        break;
                case 's':
@@ -445,19 +513,33 @@ process_arguments (int argc, char **argv)
                                server_expect = realloc (server_expect, ++server_expect_count);
                        server_expect[server_expect_count - 1] = optarg;
                        break;
+               case 'm':
+                       if (!is_intpos (optarg))
+                               usage (_("Maxbytes must be a positive integer\n"));
+                       maxbytes = atoi (optarg);
                case 'q':
                        server_quit = optarg;
                        break;
+               case 'r':
+                       if (!strncmp(optarg,"ok",2))
+                               econn_refuse_state = STATE_OK;
+                       else if (!strncmp(optarg,"warn",4))
+                               econn_refuse_state = STATE_WARNING;
+                       else if (!strncmp(optarg,"crit",4))
+                               econn_refuse_state = STATE_CRITICAL;
+                       else
+                               usage (_("Refuse mut be one of ok, warn, crit\n"));
+                       break;
                case 'd':
                        if (is_intpos (optarg))
                                delay = atoi (optarg);
                        else
-                               usage ("Delay must be a positive integer\n");
+                               usage (_("Delay must be a positive integer\n"));
                        break;
                case 'S':
 #ifndef HAVE_SSL
                        terminate (STATE_UNKNOWN,
-                               "SSL support not available. Install OpenSSL and recompile.");
+                               _("SSL support not available. Install OpenSSL and recompile."));
 #endif
                        use_ssl = TRUE;
                        break;
@@ -465,63 +547,11 @@ process_arguments (int argc, char **argv)
        }
 
        if (server_address == NULL)
-               usage ("You must provide a server address\n");
+               usage (_("You must provide a server address\n"));
 
        return OK;
 }
-
-
-
-
-
-void
-print_usage (void)
-{
-       printf
-               ("Usage: %s -H host -p port [-w warn_time] [-c crit_time] [-s send]\n"
-                "         [-e expect] [-W wait] [-t to_sec] [-v]\n", PROGNAME);
-}
-
-
-
-
-
-void
-print_help (void)
-{
-       print_revision (PROGNAME, "$Revision$");
-       printf
-               ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
-                "This plugin tests %s connections with the specified host.\n\n",
-                SERVICE);
-       print_usage ();
-       printf
-               ("Options:\n"
-                " -H, --hostname=ADDRESS\n"
-                "    Host name argument for servers using host headers (use numeric\n"
-                "    address if possible to bypass DNS lookup).\n"
-                " -p, --port=INTEGER\n"
-                "    Port number\n"
-                " -s, --send=STRING\n"
-                "    String to send to the server\n"
-                " -e, --expect=STRING\n"
-                "    String to expect in server response"
-                " -W, --wait=INTEGER\n"
-                "    Seconds to wait between sending string and polling for response\n"
-                " -w, --warning=DOUBLE\n"
-                "    Response time to result in warning status (seconds)\n"
-                " -c, --critical=DOUBLE\n"
-                "    Response time to result in critical status (seconds)\n"
-                " -t, --timeout=INTEGER\n"
-                "    Seconds before connection times out (default: %d)\n"
-                " -v"
-                "    Show details for command-line debugging (do not use with nagios server)\n"
-                " -h, --help\n"
-                "    Print detailed help screen\n"
-                " -V, --version\n"
-                "    Print version information\n", DEFAULT_SOCKET_TIMEOUT);
-}
-
+\f
 
 #ifdef HAVE_SSL
 int
@@ -535,7 +565,7 @@ connect_SSL (void)
   SSL_load_error_strings ();
   if ((ctx = SSL_CTX_new (meth)) == NULL)
     {
-      printf ("ERROR: Cannot create SSL context.\n");
+      printf (_("ERROR: Cannot create SSL context.\n"));
       return STATE_CRITICAL;
     }
 
@@ -549,7 +579,7 @@ connect_SSL (void)
   time (&start_time);
 
   /* Make TCP connection */
-  if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK)
+  if (my_tcp_connect (server_address, server_port, &sd) == STATE_OK && was_refused == FALSE)
     {
     /* Do the SSL handshake */
       if ((ssl = SSL_new (ctx)) != NULL)
@@ -561,7 +591,7 @@ connect_SSL (void)
       }
       else
       {
-        printf ("ERROR: Cannot initiate SSL handshake.\n");
+        printf (_("ERROR: Cannot initiate SSL handshake.\n"));
       }
       SSL_free (ssl);
     }
@@ -573,3 +603,23 @@ connect_SSL (void)
 }
 #endif
 
+
+
+int
+my_recv (void)
+{
+       int i;
+
+#ifdef HAVE_SSL
+       if (use_ssl) {
+               i = SSL_read (ssl, buffer, MAXBUF - 1);
+       }
+       else {
+#endif
+               i = read (sd, buffer, MAXBUF - 1);
+#ifdef HAVE_SSL
+       }
+#endif
+
+       return i;
+}