summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/check_http.c61
1 files changed, 29 insertions, 32 deletions
diff --git a/plugins/check_http.c b/plugins/check_http.c
index 1b4bae34..306e4a2e 100644
--- a/plugins/check_http.c
+++ b/plugins/check_http.c
@@ -177,15 +177,14 @@ int errcode;
177#define HTTP_EXPECT "HTTP/1." 177#define HTTP_EXPECT "HTTP/1."
178#define HTTP_URL "/" 178#define HTTP_URL "/"
179 179
180time_t start_time, end_time;
181char timestamp[10] = ""; 180char timestamp[10] = "";
182int specify_port = FALSE; 181int specify_port = FALSE;
183int server_port = HTTP_PORT; 182int server_port = HTTP_PORT;
184char server_port_text[6] = ""; 183char server_port_text[6] = "";
185char server_type[6] = "http"; 184char server_type[6] = "http";
186char *server_address = NULL; 185/*@null@*/ char *server_address = NULL;
187char *host_name = NULL; 186/*@null@*/ char *host_name = NULL;
188char *server_url = NULL; 187/*@null@*/ char *server_url = NULL;
189int server_url_length = 0; 188int server_url_length = 0;
190int server_expect_yn = 0; 189int server_expect_yn = 0;
191char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT; 190char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT;
@@ -200,8 +199,8 @@ int onredirect = STATE_OK;
200int use_ssl = FALSE; 199int use_ssl = FALSE;
201int verbose = FALSE; 200int verbose = FALSE;
202int sd; 201int sd;
203char *http_method = NULL; 202/*@null@*/ char *http_method = NULL;
204char *http_post_data = NULL; 203/*@null@*/ char *http_post_data = NULL;
205char buffer[MAX_INPUT_BUFFER]; 204char buffer[MAX_INPUT_BUFFER];
206 205
207void print_usage (void); 206void print_usage (void);
@@ -223,9 +222,9 @@ main (int argc, char **argv)
223 222
224 if (strstr (timestamp, ":")) { 223 if (strstr (timestamp, ":")) {
225 if (strstr (server_url, "?")) 224 if (strstr (server_url, "?"))
226 sprintf (server_url, "%s&%s", server_url, timestamp); 225 server_url = ssprintf (server_url, "%s&%s", server_url, timestamp);
227 else 226 else
228 sprintf (server_url, "%s?%s", server_url, timestamp); 227 server_url = ssprintf (server_url, "%s?%s", server_url, timestamp);
229 } 228 }
230 229
231 if (display_html == TRUE) 230 if (display_html == TRUE)
@@ -233,9 +232,9 @@ main (int argc, char **argv)
233 host_name, server_port, server_url); 232 host_name, server_port, server_url);
234 233
235 /* initialize alarm signal handling, set socket timeout, start timer */ 234 /* initialize alarm signal handling, set socket timeout, start timer */
236 signal (SIGALRM, socket_timeout_alarm_handler); 235 (void) signal (SIGALRM, socket_timeout_alarm_handler);
237 alarm (socket_timeout); 236 (void) alarm (socket_timeout);
238 time (&start_time); 237 (void) time (&start_time);
239 238
240#ifdef HAVE_SSL 239#ifdef HAVE_SSL
241 if (use_ssl && check_cert == TRUE) { 240 if (use_ssl && check_cert == TRUE) {
@@ -431,8 +430,8 @@ process_arguments (int argc, char **argv)
431 case 'r': /* regex */ 430 case 'r': /* regex */
432#ifdef HAVE_REGEX_H 431#ifdef HAVE_REGEX_H
433 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE; 432 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
434 strncpy (regexp, optarg, MAX_INPUT_BUFFER - 1); 433 strncpy (regexp, optarg, MAX_RE_SIZE - 1);
435 regexp[MAX_INPUT_BUFFER - 1] = 0; 434 regexp[MAX_RE_SIZE - 1] = 0;
436 errcode = regcomp (&preg, regexp, cflags); 435 errcode = regcomp (&preg, regexp, cflags);
437 if (errcode != 0) { 436 if (errcode != 0) {
438 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); 437 regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
@@ -487,9 +486,7 @@ base64 (char *bin, int len)
487 int i = 0, j = 0; 486 int i = 0, j = 0;
488 487
489 char BASE64_END = '='; 488 char BASE64_END = '=';
490 char base64_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 489 char base64_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
491 "abcdefghijklmnopqrstuvwxyz"
492 "0123456789+/";
493 490
494 while (j < len - 2) { 491 while (j < len - 2) {
495 buf[i++] = base64_table[bin[j] >> 2]; 492 buf[i++] = base64_table[bin[j] >> 2];
@@ -552,7 +549,7 @@ check_http (void)
552 return STATE_CRITICAL; 549 return STATE_CRITICAL;
553 } 550 }
554 551
555 sprintf (buffer, "%s %s HTTP/1.0\r\n", http_method, server_url); 552 snprintf (buffer, MAX_INPUT_BUFFER - 1, "%s %s HTTP/1.0\r\n", http_method, server_url);
556 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { 553 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) {
557 ERR_print_errors_fp (stderr); 554 ERR_print_errors_fp (stderr);
558 return STATE_CRITICAL; 555 return STATE_CRITICAL;
@@ -560,7 +557,7 @@ check_http (void)
560 557
561 /* optionally send the host header info (not clear if it's usable) */ 558 /* optionally send the host header info (not clear if it's usable) */
562 if (strcmp (host_name, "")) { 559 if (strcmp (host_name, "")) {
563 sprintf (buffer, "Host: %s\r\n", host_name); 560 snprintf (buffer, MAX_INPUT_BUFFER - 1, "Host: %s\r\n", host_name);
564 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { 561 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) {
565 ERR_print_errors_fp (stderr); 562 ERR_print_errors_fp (stderr);
566 return STATE_CRITICAL; 563 return STATE_CRITICAL;
@@ -568,7 +565,7 @@ check_http (void)
568 } 565 }
569 566
570 /* send user agent */ 567 /* send user agent */
571 sprintf (buffer, "User-Agent: check_http/%s (nagios-plugins %s)\r\n", 568 snprintf (buffer, MAX_INPUT_BUFFER - 1, "User-Agent: check_http/%s (nagios-plugins %s)\r\n",
572 clean_revstring (REVISION), PACKAGE_VERSION); 569 clean_revstring (REVISION), PACKAGE_VERSION);
573 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { 570 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) {
574 ERR_print_errors_fp (stderr); 571 ERR_print_errors_fp (stderr);
@@ -578,7 +575,7 @@ check_http (void)
578 /* optionally send the authentication info */ 575 /* optionally send the authentication info */
579 if (strcmp (user_auth, "")) { 576 if (strcmp (user_auth, "")) {
580 auth = base64 (user_auth, strlen (user_auth)); 577 auth = base64 (user_auth, strlen (user_auth));
581 sprintf (buffer, "Authorization: Basic %s\r\n", auth); 578 snprintf (buffer, MAX_INPUT_BUFFER - 1, "Authorization: Basic %s\r\n", auth);
582 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { 579 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) {
583 ERR_print_errors_fp (stderr); 580 ERR_print_errors_fp (stderr);
584 return STATE_CRITICAL; 581 return STATE_CRITICAL;
@@ -587,12 +584,12 @@ check_http (void)
587 584
588 /* optionally send http POST data */ 585 /* optionally send http POST data */
589 if (http_post_data) { 586 if (http_post_data) {
590 sprintf (buffer, "Content-Type: application/x-www-form-urlencoded\r\n"); 587 snprintf (buffer, MAX_INPUT_BUFFER - 1, "Content-Type: application/x-www-form-urlencoded\r\n");
591 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { 588 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) {
592 ERR_print_errors_fp (stderr); 589 ERR_print_errors_fp (stderr);
593 return STATE_CRITICAL; 590 return STATE_CRITICAL;
594 } 591 }
595 sprintf (buffer, "Content-Length: %i\r\n\r\n", strlen (http_post_data)); 592 snprintf (buffer, MAX_INPUT_BUFFER - 1, "Content-Length: %i\r\n\r\n", strlen (http_post_data));
596 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { 593 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) {
597 ERR_print_errors_fp (stderr); 594 ERR_print_errors_fp (stderr);
598 return STATE_CRITICAL; 595 return STATE_CRITICAL;
@@ -605,7 +602,7 @@ check_http (void)
605 } 602 }
606 603
607 /* send a newline so the server knows we're done with the request */ 604 /* send a newline so the server knows we're done with the request */
608 sprintf (buffer, "\r\n\r\n"); 605 snprintf (buffer, MAX_INPUT_BUFFER - 1, "\r\n\r\n");
609 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) { 606 if (SSL_write (ssl, buffer, strlen (buffer)) == -1) {
610 ERR_print_errors_fp (stderr); 607 ERR_print_errors_fp (stderr);
611 return STATE_CRITICAL; 608 return STATE_CRITICAL;
@@ -618,19 +615,19 @@ check_http (void)
618 msg = ssprintf (msg, "Unable to open TCP socket"); 615 msg = ssprintf (msg, "Unable to open TCP socket");
619 terminate (STATE_CRITICAL, msg); 616 terminate (STATE_CRITICAL, msg);
620 } 617 }
621 sprintf (buffer, "%s %s HTTP/1.0\r\n", http_method, server_url); 618 snprintf (buffer, MAX_INPUT_BUFFER - 1, "%s %s HTTP/1.0\r\n", http_method, server_url);
622 send (sd, buffer, strlen (buffer), 0); 619 send (sd, buffer, strlen (buffer), 0);
623 620
624 621
625 622
626 /* optionally send the host header info */ 623 /* optionally send the host header info */
627 if (strcmp (host_name, "")) { 624 if (strcmp (host_name, "")) {
628 sprintf (buffer, "Host: %s\r\n", host_name); 625 snprintf (buffer, MAX_INPUT_BUFFER - 1, "Host: %s\r\n", host_name);
629 send (sd, buffer, strlen (buffer), 0); 626 send (sd, buffer, strlen (buffer), 0);
630 } 627 }
631 628
632 /* send user agent */ 629 /* send user agent */
633 sprintf (buffer, 630 snprintf (buffer, MAX_INPUT_BUFFER - 1,
634 "User-Agent: check_http/%s (nagios-plugins %s)\r\n", 631 "User-Agent: check_http/%s (nagios-plugins %s)\r\n",
635 clean_revstring (REVISION), PACKAGE_VERSION); 632 clean_revstring (REVISION), PACKAGE_VERSION);
636 send (sd, buffer, strlen (buffer), 0); 633 send (sd, buffer, strlen (buffer), 0);
@@ -638,23 +635,23 @@ check_http (void)
638 /* optionally send the authentication info */ 635 /* optionally send the authentication info */
639 if (strcmp (user_auth, "")) { 636 if (strcmp (user_auth, "")) {
640 auth = base64 (user_auth, strlen (user_auth)); 637 auth = base64 (user_auth, strlen (user_auth));
641 sprintf (buffer, "Authorization: Basic %s\r\n", auth); 638 snprintf (buffer, MAX_INPUT_BUFFER - 1, "Authorization: Basic %s\r\n", auth);
642 send (sd, buffer, strlen (buffer), 0); 639 send (sd, buffer, strlen (buffer), 0);
643 } 640 }
644 641
645 /* optionally send http POST data */ 642 /* optionally send http POST data */
646 /* written by Chris Henesy <lurker@shadowtech.org> */ 643 /* written by Chris Henesy <lurker@shadowtech.org> */
647 if (http_post_data) { 644 if (http_post_data) {
648 sprintf (buffer, "Content-Type: application/x-www-form-urlencoded\r\n"); 645 snprintf (buffer, MAX_INPUT_BUFFER - 1, "Content-Type: application/x-www-form-urlencoded\r\n");
649 send (sd, buffer, strlen (buffer), 0); 646 send (sd, buffer, strlen (buffer), 0);
650 sprintf (buffer, "Content-Length: %i\r\n\r\n", strlen (http_post_data)); 647 snprintf (buffer, MAX_INPUT_BUFFER - 1, "Content-Length: %i\r\n\r\n", strlen (http_post_data));
651 send (sd, buffer, strlen (buffer), 0); 648 send (sd, buffer, strlen (buffer), 0);
652 http_post_data = strscat (http_post_data, "\r\n"); 649 http_post_data = strscat (http_post_data, "\r\n");
653 send (sd, http_post_data, strlen (http_post_data), 0); 650 send (sd, http_post_data, strlen (http_post_data), 0);
654 } 651 }
655 652
656 /* send a newline so the server knows we're done with the request */ 653 /* send a newline so the server knows we're done with the request */
657 sprintf (buffer, "\r\n\r\n"); 654 snprintf (buffer, MAX_INPUT_BUFFER - 1, "\r\n\r\n");
658 send (sd, buffer, strlen (buffer), 0); 655 send (sd, buffer, strlen (buffer), 0);
659#ifdef HAVE_SSL 656#ifdef HAVE_SSL
660 } 657 }
@@ -993,8 +990,8 @@ check_certificate (X509 ** certificate)
993 stamp.tm_isdst = -1; 990 stamp.tm_isdst = -1;
994 991
995 days_left = (mktime (&stamp) - time (NULL)) / 86400; 992 days_left = (mktime (&stamp) - time (NULL)) / 86400;
996 sprintf 993 snprintf
997 (timestamp, "%02d/%02d/%04d %02d:%02d", 994 (timestamp, MAX_INPUT_BUFFER - 1, "%02d/%02d/%04d %02d:%02d",
998 stamp.tm_mon + 1, 995 stamp.tm_mon + 1,
999 stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min); 996 stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
1000 997