Code

remove NULL string inits that can lead to segfaults
[nagiosplug.git] / plugins / check_http.c
index de7a2db7f7b70b70d3917874ddca0a2908df03fc..506a1ec720ff18b88baf7ddeb265a595e8e84f0c 100644 (file)
@@ -44,7 +44,7 @@ certificate expiration times.\n"
 \(-H <vhost> | -I <IP-address>) [-u <uri>] [-p <port>]\n\
             [-w <warn time>] [-c <critical time>] [-t <timeout>] [-L]\n\
             [-a auth] [-f <ok | warn | critcal | follow>] [-e <expect>]\n\
-            [-s string] [-r <regex> | -R <case-insensitive regex>]\n\
+            [-s string] [-l] [-r <regex> | -R <case-insensitive regex>]\n\
             [-P string]"
 
 #define LONGOPTIONS "\
@@ -74,7 +74,7 @@ certificate expiration times.\n"
  -L, --link=URL\n\
    Wrap output in HTML link (obsoleted by urlize)\n\
  -f, --onredirect=<ok|warning|critical|follow>\n\
-   How to handle redirected pages\n%s\
+   How to handle redirected pages\n%s%s\
  -v, --verbose\n\
     Show details for command-line debugging (do not use with nagios server)\n\
  -h, --help\n\
@@ -93,6 +93,18 @@ certificate expiration times.\n"
 #define SSLOPTIONS ""
 #endif
 
+#ifdef HAVE_REGEX_H
+#define REGOPTIONS "\
+ -l, --linespan\n\
+    Allow regex to span newlines (must precede -r or -R)\n\
+ -r, --regex, --ereg=STRING\n\
+    Search page for regex STRING\n\
+ -R, --eregi=STRING\n\
+    Search page for case-insensitive regex STRING\n"
+#else
+#define REGOPTIONS ""
+#endif
+
 #define DESCRIPTION "\
 This plugin will attempt to open an HTTP connection with the host. Successul\n\
 connects return STATE_OK, refusals and timeouts return STATE_CRITICAL, other\n\
@@ -177,16 +189,17 @@ struct timeval tv;
 #define HTTPS_PORT 443
 #define HTTP_EXPECT "HTTP/1."
 #define HTTP_URL "/"
+#define CRLF "\r\n"
 
 char timestamp[17] = "";
 int specify_port = FALSE;
 int server_port = HTTP_PORT;
 char server_port_text[6] = "";
 char server_type[6] = "http";
-/*@null@*/ char *server_address = NULL
-/*@null@*/ char *host_name = NULL;
-/*@null@*/ char *server_url = NULL;
-int server_url_length = 0;
+char *server_address = ""
+char *host_name = "";
+char *server_url = HTTP_URL;
+int server_url_length = 1;
 int server_expect_yn = 0;
 char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT;
 char string_expect[MAX_INPUT_BUFFER] = "";
@@ -200,8 +213,8 @@ int onredirect = STATE_OK;
 int use_ssl = FALSE;
 int verbose = FALSE;
 int sd;
-/*@null@*/ char *http_method = NULL;
-/*@null@*/ char *http_post_data = NULL;
+char *http_method = "GET";
+char *http_post_data = "";
 char buffer[MAX_INPUT_BUFFER];
 
 void print_usage (void);
@@ -286,6 +299,7 @@ process_arguments (int argc, char **argv)
                {"regex", required_argument, 0, 'r'},
                {"ereg", required_argument, 0, 'r'},
                {"eregi", required_argument, 0, 'R'},
+               {"linespan", no_argument, 0, 'l'},
                {"onredirect", required_argument, 0, 'f'},
                {"certificate", required_argument, 0, 'C'},
                {0, 0, 0, 0}
@@ -308,7 +322,7 @@ process_arguments (int argc, char **argv)
                        strcpy (argv[c], "-n");
        }
 
-#define OPTCHARS "Vvht:c:w:H:P:I:a:e:p:s:R:r:u:f:C:nLS"
+#define OPTCHARS "Vvht:c:w:H:P:I:a:e:p:s:R:r:u:f:C:nlLS"
 
        while (1) {
 #ifdef HAVE_GETOPT_H
@@ -388,14 +402,14 @@ process_arguments (int argc, char **argv)
                        break;
                /* Note: H, I, and u must be malloc'd or will fail on redirects */
                case 'H': /* Host Name (virtual host) */
-                       host_name = strscpy (host_name, optarg);
+                       asprintf (&host_name, "%s", optarg);
                        break;
                case 'I': /* Server IP-address */
-                       server_address = strscpy (server_address, optarg);
+                       asprintf (&server_address, "%s", optarg);
                        break;
                case 'u': /* Host or server */
-                       server_url = strscpy (server_url, optarg);
-                       server_url_length = strlen (optarg);
+                       asprintf (&server_url, "%s", optarg);
+                       server_url_length = strlen (server_url);
                        break;
                case 'p': /* Host or server */
                        if (!is_intnonneg (optarg))
@@ -408,8 +422,8 @@ process_arguments (int argc, char **argv)
                        user_auth[MAX_INPUT_BUFFER - 1] = 0;
                        break;
                case 'P': /* HTTP POST data in URL encoded format */
-                       http_method = strscpy (http_method, "POST");
-                       http_post_data = strscpy (http_post_data, optarg);
+                       asprintf (&http_method, "%s", "POST");
+                       asprintf (&http_post_data, "%s", optarg);
                        break;
                case 's': /* string or substring */
                        strncpy (string_expect, optarg, MAX_INPUT_BUFFER - 1);
@@ -420,15 +434,19 @@ process_arguments (int argc, char **argv)
                        server_expect[MAX_INPUT_BUFFER - 1] = 0;
                        server_expect_yn = 1;
                        break;
-               case 'R': /* regex */
-#ifdef HAVE_REGEX_H
-                       cflags = REG_ICASE;
-#else
+#ifndef HAVE_REGEX_H
+               case 'l': /* linespan */
+               case 'r': /* linespan */
+               case 'R': /* linespan */
                        usage ("check_http: call for regex which was not a compiled option\n");
-#endif
+                       break;
+#else
+               case 'l': /* linespan */
+                       cflags &= ~REG_NEWLINE;
+                       break;
+               case 'R': /* regex */
+                       cflags |= REG_ICASE;
                case 'r': /* regex */
-#ifdef HAVE_REGEX_H
-                       cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
                        strncpy (regexp, optarg, MAX_RE_SIZE - 1);
                        regexp[MAX_RE_SIZE - 1] = 0;
                        errcode = regcomp (&preg, regexp, cflags);
@@ -437,10 +455,8 @@ process_arguments (int argc, char **argv)
                                printf ("Could Not Compile Regular Expression: %s", errbuf);
                                return ERROR;
                        }
-#else
-                       usage ("check_http: call for regex which was not a compiled option\n");
-#endif
                        break;
+#endif
                case 'v': /* verbose */
                        verbose = TRUE;
                        break;
@@ -449,26 +465,17 @@ process_arguments (int argc, char **argv)
 
        c = optind;
 
-       if (server_address == NULL && host_name == NULL) {
-               server_address = strscpy (NULL, argv[c]);
-               host_name = strscpy (NULL, argv[c++]);
-       }
-
-       if (server_address == NULL && host_name == NULL)
-               usage ("check_http: you must specify a host name\n");
-
-       if (server_address == NULL)
-               server_address = strscpy (NULL, host_name);
+       if (strcmp (server_address, "") == 0 && c < argc)
+                       asprintf (&server_address, "%s", argv[c++]);
 
-       if (host_name == NULL)
-               host_name = strscpy (NULL, server_address);
+       if (strcmp (host_name, "") == 0 && c < argc)
+               asprintf (&host_name, "%s", argv[c++]);
 
-       if (http_method == NULL)
-               http_method = strscpy (http_method, "GET");
-
-       if (server_url == NULL) {
-               server_url = strscpy (NULL, "/");
-               server_url_length = strlen(HTTP_URL);
+       if (strcmp (server_address ,"") == 0) {
+               if (strcmp (host_name, "") == 0)
+                       usage ("check_http: you must specify a server address or host name\n");
+               else
+                       asprintf (&server_address, "%s", host_name);
        }
 
        return TRUE;
@@ -583,8 +590,8 @@ check_http (void)
                        }
                }
 
-               /* optionally send http POST data */
-               if (http_post_data) {
+               /* either send http POST data */
+               if (strlen (http_post_data)) {
                        asprintf (&buf, "Content-Type: application/x-www-form-urlencoded\r\n");
                        if (SSL_write (ssl, buf, strlen (buf)) == -1) {
                                ERR_print_errors_fp (stderr);
@@ -595,18 +602,23 @@ check_http (void)
                                ERR_print_errors_fp (stderr);
                                return STATE_CRITICAL;
                        }
-                       http_post_data = strscat (http_post_data, "\r\n");
                        if (SSL_write (ssl, http_post_data, strlen (http_post_data)) == -1) {
                                ERR_print_errors_fp (stderr);
                                return STATE_CRITICAL;
                        }
+                       asprintf (&buf, CRLF);
+                       if (SSL_write (ssl, buf, strlen (buf)) == -1) {
+                               ERR_print_errors_fp (stderr);
+                               return STATE_CRITICAL;
+                       }
                }
-
-               /* send a newline so the server knows we're done with the request */
-               asprintf (&buf, "\r\n\r\n");
-               if (SSL_write (ssl, buf, strlen (buf)) == -1) {
-                       ERR_print_errors_fp (stderr);
-                       return STATE_CRITICAL;
+               else {
+                       /* or just a newline so the server knows we're done with the request */
+                       asprintf (&buf, "\r\n");
+                       if (SSL_write (ssl, buf, strlen (buf)) == -1) {
+                               ERR_print_errors_fp (stderr);
+                               return STATE_CRITICAL;
+                       }
                }
 
        }
@@ -616,8 +628,6 @@ check_http (void)
                        terminate (STATE_CRITICAL, "Unable to open TCP socket");
                asprintf (&buf, "%s %s HTTP/1.0\r\n", http_method, server_url);
                send (sd, buf, strlen (buf), 0);
-               
-
 
                /* optionally send the host header info */
                if (strcmp (host_name, "")) {
@@ -638,20 +648,21 @@ check_http (void)
                        send (sd, buf, strlen (buf), 0);
                }
 
-               /* optionally send http POST data */
+               /* either send http POST data */
                /* written by Chris Henesy <lurker@shadowtech.org> */
-               if (http_post_data) {
+               if (strlen (http_post_data)) {
                        asprintf (&buf, "Content-Type: application/x-www-form-urlencoded\r\n");
                        send (sd, buf, strlen (buf), 0);
                        asprintf (&buf, "Content-Length: %i\r\n\r\n", strlen (http_post_data));
                        send (sd, buf, strlen (buf), 0);
-                       http_post_data = strscat (http_post_data, "\r\n");
                        send (sd, http_post_data, strlen (http_post_data), 0);
+                       send (sd, CRLF, strlen (CRLF), 0);
+               }
+               else {
+                       /* or just a newline so the server knows we're done with the request */
+                       asprintf (&buf, "\r\n");
+                       send (sd, buf, strlen (buf), 0);
                }
-
-               /* send a newline so the server knows we're done with the request */
-               asprintf (&buf, "\r\n");
-               send (sd, buf, strlen (buf), 0);
 #ifdef HAVE_SSL
        }
 #endif
@@ -757,7 +768,7 @@ check_http (void)
                    strstr (status_line, "304")) {
                        if (onredirect == STATE_DEPENDENT) {
 
-                               orig_url = strscpy(NULL, server_url);
+                               asprintf (&orig_url, "%s", server_url);
                                pos = header;
                                while (pos) {
                                        server_address = realloc (server_address, MAX_IPV4_HOSTLENGTH);
@@ -772,26 +783,26 @@ check_http (void)
                                                server_url_length = strcspn (pos, "\r\n");
                                        }
                                        if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PORT URI_PATH, server_type, server_address, server_port_text, server_url) == 4) {
-                                               host_name = strscpy (host_name, server_address);
+                                               asprintf (&host_name, "%s", server_address);
                                                use_ssl = server_type_check (server_type);
                                                server_port = atoi (server_port_text);
                                                check_http ();
                                        }
                                        else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PATH, server_type, server_address, server_url) == 3 ) { 
-                                               host_name = strscpy (host_name, server_address);
+                                               asprintf (&host_name, "%s", server_address);
                                                use_ssl = server_type_check (server_type);
                                                server_port = server_port_check (use_ssl);
                                                check_http ();
                                        }
                                        else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST URI_PORT, server_type, server_address, server_port_text) == 3) {
-                                               host_name = strscpy (host_name, server_address);
+                                               asprintf (&host_name, "%s", server_address);
                                                strcpy (server_url, "/");
                                                use_ssl = server_type_check (server_type);
                                                server_port = atoi (server_port_text);
                                                check_http ();
                                        }
                                        else if (sscanf (pos, HDR_LOCATION URI_HTTP URI_HOST, server_type, server_address) == 2) {
-                                               host_name = strscpy (host_name, server_address);
+                                               asprintf (&host_name, "%s", server_address);
                                                strcpy (server_url, "/");
                                                use_ssl = server_type_check (server_type);
                                                server_port = server_port_check (use_ssl);
@@ -896,7 +907,7 @@ int connect_SSL (void)
 {
        SSL_METHOD *meth;
 
-       randbuff = strscpy (NULL, "qwertyuiopasdfghjkl");
+       asprintf (randbuff, "%s", "qwertyuiopasdfghjkl");
        RAND_seed (randbuff, strlen (randbuff));
        /* Initialize SSL context */
        SSLeay_add_ssl_algorithms ();
@@ -1065,7 +1076,7 @@ print_help (void)
        print_usage ();
        printf ("NOTE: One or both of -H and -I must be specified\n");
        printf ("\nOptions:\n" LONGOPTIONS "\n", HTTP_EXPECT, HTTP_PORT,
-               DEFAULT_SOCKET_TIMEOUT, SSLOPTIONS);
+               DEFAULT_SOCKET_TIMEOUT, SSLOPTIONS, REGOPTIONS);
 #ifdef HAVE_SSL
        printf (SSLDESCRIPTION);
 #endif