Code

Some HTTP server applications/configurations cause trouble if the port is
[nagiosplug.git] / plugins / check_http.c
index 07e0079e64b6035fae062004ec9044d6881c1f66..69ed2fa91031f0bf89eccd15734661e69cba45da 100644 (file)
@@ -573,7 +573,25 @@ parse_time_string (const char *string)
   }
 }
 
+/* Checks if the server 'reply' is one of the expected 'statuscodes' */
+static int
+expected_statuscode (const char *reply, const char *statuscodes)
+{
+  char *expected, *code;
+  int result = 0;
+
+  if ((expected = strdup (statuscodes)) == NULL)
+    die (STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n"));
+
+  for (code = strtok (expected, ","); code != NULL; code = strtok (NULL, ","))
+    if (strstr (reply, code) != NULL) {
+      result = 1;
+      break;
+    }
 
+  free (expected);
+  return result;
+}
 
 static void
 check_document_dates (const char *headers)
@@ -714,6 +732,22 @@ get_content_length (const char *headers)
   return (content_length);
 }
 
+char *
+prepend_slash (char *path)
+{
+  char *newpath;
+
+  if (path[0] == '/')
+    return path;
+
+  if ((newpath = malloc (strlen(path) + 2)) == NULL)
+    die (STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n"));
+  newpath[0] = '/';
+  strcpy (newpath + 1, path);
+  free (path);
+  return newpath;
+}
+
 int
 check_http (void)
 {
@@ -743,7 +777,7 @@ check_http (void)
     if (check_cert == TRUE) {
       result = np_net_ssl_check_cert(days_till_exp);
       np_net_ssl_cleanup();
-      if(sd) close(sd);
+      if (sd) close(sd);
       return result;
     }
   }
@@ -755,8 +789,18 @@ check_http (void)
   asprintf (&buf, "%sConnection: close\r\n", buf);
 
   /* optionally send the host header info */
-  if (host_name)
-    asprintf (&buf, "%sHost: %s:%d\r\n", buf, host_name, server_port);
+  if (host_name) {
+    /*
+     * Specify the port only if we're using a non-default port (see RFC 2616,
+     * 14.23).  Some server applications/configurations cause trouble if the
+     * (default) port is explicitly specified in the "Host:" header line.
+     */
+    if ((use_ssl == FALSE && server_port == HTTP_PORT) ||
+        (use_ssl == TRUE && server_port == HTTPS_PORT))
+      asprintf (&buf, "%sHost: %s\r\n", buf, host_name);
+    else
+      asprintf (&buf, "%sHost: %s:%d\r\n", buf, host_name, server_port);
+  }
 
   /* optionally send any other header tag */
   if (http_opt_headers_count) {
@@ -835,7 +879,7 @@ check_http (void)
 #ifdef HAVE_SSL
   np_net_ssl_cleanup();
 #endif
-  if(sd) close(sd);
+  if (sd) close(sd);
 
   /* reset the alarm */
   alarm (0);
@@ -876,14 +920,15 @@ check_http (void)
                 (no_body ? "  [[ skipped ]]" : page));
 
   /* make sure the status line matches the response we are looking for */
-  if (!strstr (status_line, server_expect)) {
+  if (!expected_statuscode (status_line, server_expect)) {
     if (server_port == HTTP_PORT)
       asprintf (&msg,
-                _("Invalid HTTP response received from host\n"));
+                _("Invalid HTTP response received from host: %s\n"),
+                status_line);
     else
       asprintf (&msg,
-                _("Invalid HTTP response received from host on port %d\n"),
-                server_port);
+                _("Invalid HTTP response received from host on port %d: %s\n"),
+                server_port, status_line);
     die (STATE_CRITICAL, "HTTP CRITICAL - %s", msg);
   }
 
@@ -1091,23 +1136,26 @@ redir (char *pos, char *status_line)
       die (STATE_UNKNOWN, _("HTTP UNKNOWN - could not allocate url\n"));
 
     /* URI_HTTP, URI_HOST, URI_PORT, URI_PATH */
-    if (sscanf (pos, HD1, type, addr, &i, url) == 4)
+    if (sscanf (pos, HD1, type, addr, &i, url) == 4) {
+      url = prepend_slash (url);
       use_ssl = server_type_check (type);
+    }
 
     /* URI_HTTP URI_HOST URI_PATH */
     else if (sscanf (pos, HD2, type, addr, url) == 3 ) { 
+      url = prepend_slash (url);
       use_ssl = server_type_check (type);
       i = server_port_check (use_ssl);
     }
 
     /* URI_HTTP URI_HOST URI_PORT */
-    else if(sscanf (pos, HD3, type, addr, &i) == 3) {
+    else if (sscanf (pos, HD3, type, addr, &i) == 3) {
       strcpy (url, HTTP_URL);
       use_ssl = server_type_check (type);
     }
 
     /* URI_HTTP URI_HOST */
-    else if(sscanf (pos, HD4, type, addr) == 2) {
+    else if (sscanf (pos, HD4, type, addr) == 2) {
       strcpy (url, HTTP_URL);
       use_ssl = server_type_check (type);
       i = server_port_check (use_ssl);
@@ -1158,12 +1206,7 @@ redir (char *pos, char *status_line)
   server_address = strdup (addr);
 
   free (server_url);
-  if ((url[0] == '/'))
-    server_url = strdup (url);
-  else if (asprintf(&server_url, "/%s", url) == -1)
-    die (STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate server_url%s\n"),
-         display_html ? "</A>" : "");
-  free(url);
+  server_url = url;
 
   if ((server_port = i) > MAX_PORT)
     die (STATE_UNKNOWN,
@@ -1260,7 +1303,8 @@ print_help (void)
 #endif
 
   printf (" %s\n", "-e, --expect=STRING");
-  printf ("    %s\n", _("String to expect in first (status) line of server response (default: "));
+  printf ("    %s\n", _("Comma-delimited list of strings, at least one of them is expected in"));
+  printf ("    %s", _("the first (status) line of the server response (default: "));
   printf ("%s)\n", HTTP_EXPECT);
   printf ("    %s\n", _("If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)"));
   printf (" %s\n", "-s, --string=STRING");