diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-10 22:56:34 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-10 22:56:34 +0100 |
commit | aa137f7d4adb8ebbbdd3a4c71cd28824d6fcfaca (patch) | |
tree | ec93c9339bc346505df6c056476661dfa4fec383 /plugins/check_curl.c | |
parent | 14671421030843ed15816162245a0ef348db3315 (diff) | |
download | monitoring-plugins-aa137f7d4adb8ebbbdd3a4c71cd28824d6fcfaca.tar.gz |
check_curl: clang-format
Diffstat (limited to 'plugins/check_curl.c')
-rw-r--r-- | plugins/check_curl.c | 429 |
1 files changed, 271 insertions, 158 deletions
diff --git a/plugins/check_curl.c b/plugins/check_curl.c index 748201e8..cf755316 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c | |||
@@ -32,8 +32,8 @@ | |||
32 | * | 32 | * |
33 | * | 33 | * |
34 | *****************************************************************************/ | 34 | *****************************************************************************/ |
35 | const char *progname = "check_curl"; | ||
36 | 35 | ||
36 | const char *progname = "check_curl"; | ||
37 | const char *copyright = "2006-2024"; | 37 | const char *copyright = "2006-2024"; |
38 | const char *email = "devel@monitoring-plugins.org"; | 38 | const char *email = "devel@monitoring-plugins.org"; |
39 | 39 | ||
@@ -63,7 +63,7 @@ const char *email = "devel@monitoring-plugins.org"; | |||
63 | 63 | ||
64 | #include <netdb.h> | 64 | #include <netdb.h> |
65 | 65 | ||
66 | #define MAKE_LIBCURL_VERSION(major, minor, patch) ((major)*0x10000 + (minor)*0x100 + (patch)) | 66 | #define MAKE_LIBCURL_VERSION(major, minor, patch) ((major) * 0x10000 + (minor) * 0x100 + (patch)) |
67 | 67 | ||
68 | #define DEFAULT_BUFFER_SIZE 2048 | 68 | #define DEFAULT_BUFFER_SIZE 2048 |
69 | #define DEFAULT_SERVER_URL "/" | 69 | #define DEFAULT_SERVER_URL "/" |
@@ -275,12 +275,14 @@ int main(int argc, char **argv) { | |||
275 | snprintf(user_agent, DEFAULT_BUFFER_SIZE, "%s/v%s (monitoring-plugins %s, %s)", progname, NP_VERSION, VERSION, curl_version()); | 275 | snprintf(user_agent, DEFAULT_BUFFER_SIZE, "%s/v%s (monitoring-plugins %s, %s)", progname, NP_VERSION, VERSION, curl_version()); |
276 | 276 | ||
277 | /* parse arguments */ | 277 | /* parse arguments */ |
278 | if (process_arguments(argc, argv) == false) | 278 | if (process_arguments(argc, argv) == false) { |
279 | usage4(_("Could not parse arguments")); | 279 | usage4(_("Could not parse arguments")); |
280 | } | ||
280 | 281 | ||
281 | if (display_html) | 282 | if (display_html) { |
282 | printf("<A HREF=\"%s://%s:%d%s\" target=\"_blank\">", use_ssl ? "https" : "http", host_name ? host_name : server_address, | 283 | printf("<A HREF=\"%s://%s:%d%s\" target=\"_blank\">", use_ssl ? "https" : "http", host_name ? host_name : server_address, |
283 | virtual_port ? virtual_port : server_port, server_url); | 284 | virtual_port ? virtual_port : server_port, server_url); |
285 | } | ||
284 | 286 | ||
285 | result = check_http(); | 287 | result = check_http(); |
286 | return result; | 288 | return result; |
@@ -361,14 +363,16 @@ static int expected_statuscode(const char *reply, const char *statuscodes) { | |||
361 | char *code; | 363 | char *code; |
362 | int result = 0; | 364 | int result = 0; |
363 | 365 | ||
364 | if ((expected = strdup(statuscodes)) == NULL) | 366 | if ((expected = strdup(statuscodes)) == NULL) { |
365 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n")); | 367 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n")); |
368 | } | ||
366 | 369 | ||
367 | for (code = strtok(expected, ","); code != NULL; code = strtok(NULL, ",")) | 370 | for (code = strtok(expected, ","); code != NULL; code = strtok(NULL, ",")) { |
368 | if (strstr(reply, code) != NULL) { | 371 | if (strstr(reply, code) != NULL) { |
369 | result = 1; | 372 | result = 1; |
370 | break; | 373 | break; |
371 | } | 374 | } |
375 | } | ||
372 | 376 | ||
373 | free(expected); | 377 | free(expected); |
374 | return result; | 378 | return result; |
@@ -396,8 +400,9 @@ int lookup_host(const char *host, char *buf, size_t buflen) { | |||
396 | hints.ai_flags |= AI_CANONNAME; | 400 | hints.ai_flags |= AI_CANONNAME; |
397 | 401 | ||
398 | errcode = getaddrinfo(host, NULL, &hints, &result); | 402 | errcode = getaddrinfo(host, NULL, &hints, &result); |
399 | if (errcode != 0) | 403 | if (errcode != 0) { |
400 | return errcode; | 404 | return errcode; |
405 | } | ||
401 | 406 | ||
402 | strcpy(buf, ""); | 407 | strcpy(buf, ""); |
403 | res = result; | 408 | res = result; |
@@ -437,23 +442,29 @@ int lookup_host(const char *host, char *buf, size_t buflen) { | |||
437 | } | 442 | } |
438 | 443 | ||
439 | static void cleanup(void) { | 444 | static void cleanup(void) { |
440 | if (status_line_initialized) | 445 | if (status_line_initialized) { |
441 | curlhelp_free_statusline(&status_line); | 446 | curlhelp_free_statusline(&status_line); |
447 | } | ||
442 | status_line_initialized = false; | 448 | status_line_initialized = false; |
443 | if (curl_easy_initialized) | 449 | if (curl_easy_initialized) { |
444 | curl_easy_cleanup(curl); | 450 | curl_easy_cleanup(curl); |
451 | } | ||
445 | curl_easy_initialized = false; | 452 | curl_easy_initialized = false; |
446 | if (curl_global_initialized) | 453 | if (curl_global_initialized) { |
447 | curl_global_cleanup(); | 454 | curl_global_cleanup(); |
455 | } | ||
448 | curl_global_initialized = false; | 456 | curl_global_initialized = false; |
449 | if (body_buf_initialized) | 457 | if (body_buf_initialized) { |
450 | curlhelp_freewritebuffer(&body_buf); | 458 | curlhelp_freewritebuffer(&body_buf); |
459 | } | ||
451 | body_buf_initialized = false; | 460 | body_buf_initialized = false; |
452 | if (header_buf_initialized) | 461 | if (header_buf_initialized) { |
453 | curlhelp_freewritebuffer(&header_buf); | 462 | curlhelp_freewritebuffer(&header_buf); |
463 | } | ||
454 | header_buf_initialized = false; | 464 | header_buf_initialized = false; |
455 | if (put_buf_initialized) | 465 | if (put_buf_initialized) { |
456 | curlhelp_freereadbuffer(&put_buf); | 466 | curlhelp_freereadbuffer(&put_buf); |
467 | } | ||
457 | put_buf_initialized = false; | 468 | put_buf_initialized = false; |
458 | } | 469 | } |
459 | 470 | ||
@@ -468,8 +479,9 @@ int check_http(void) { | |||
468 | char dnscache[DEFAULT_BUFFER_SIZE]; | 479 | char dnscache[DEFAULT_BUFFER_SIZE]; |
469 | 480 | ||
470 | /* initialize curl */ | 481 | /* initialize curl */ |
471 | if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) | 482 | if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) { |
472 | die(STATE_UNKNOWN, "HTTP UNKNOWN - curl_global_init failed\n"); | 483 | die(STATE_UNKNOWN, "HTTP UNKNOWN - curl_global_init failed\n"); |
484 | } | ||
473 | curl_global_initialized = true; | 485 | curl_global_initialized = true; |
474 | 486 | ||
475 | if ((curl = curl_easy_init()) == NULL) { | 487 | if ((curl = curl_easy_init()) == NULL) { |
@@ -480,8 +492,9 @@ int check_http(void) { | |||
480 | /* register cleanup function to shut down libcurl properly */ | 492 | /* register cleanup function to shut down libcurl properly */ |
481 | atexit(cleanup); | 493 | atexit(cleanup); |
482 | 494 | ||
483 | if (verbose >= 1) | 495 | if (verbose >= 1) { |
484 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_VERBOSE, 1), "CURLOPT_VERBOSE"); | 496 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_VERBOSE, 1), "CURLOPT_VERBOSE"); |
497 | } | ||
485 | 498 | ||
486 | /* print everything on stdout like check_http would do */ | 499 | /* print everything on stdout like check_http would do */ |
487 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_STDERR, stdout), "CURLOPT_STDERR"); | 500 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_STDERR, stdout), "CURLOPT_STDERR"); |
@@ -494,16 +507,18 @@ int check_http(void) { | |||
494 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 6) */ | 507 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 6) */ |
495 | 508 | ||
496 | /* initialize buffer for body of the answer */ | 509 | /* initialize buffer for body of the answer */ |
497 | if (curlhelp_initwritebuffer(&body_buf) < 0) | 510 | if (curlhelp_initwritebuffer(&body_buf) < 0) { |
498 | die(STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for body\n"); | 511 | die(STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for body\n"); |
512 | } | ||
499 | body_buf_initialized = true; | 513 | body_buf_initialized = true; |
500 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)curlhelp_buffer_write_callback), | 514 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)curlhelp_buffer_write_callback), |
501 | "CURLOPT_WRITEFUNCTION"); | 515 | "CURLOPT_WRITEFUNCTION"); |
502 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&body_buf), "CURLOPT_WRITEDATA"); | 516 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&body_buf), "CURLOPT_WRITEDATA"); |
503 | 517 | ||
504 | /* initialize buffer for header of the answer */ | 518 | /* initialize buffer for header of the answer */ |
505 | if (curlhelp_initwritebuffer(&header_buf) < 0) | 519 | if (curlhelp_initwritebuffer(&header_buf) < 0) { |
506 | die(STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for header\n"); | 520 | die(STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for header\n"); |
521 | } | ||
507 | header_buf_initialized = true; | 522 | header_buf_initialized = true; |
508 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, (curl_write_callback)curlhelp_buffer_write_callback), | 523 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, (curl_write_callback)curlhelp_buffer_write_callback), |
509 | "CURLOPT_HEADERFUNCTION"); | 524 | "CURLOPT_HEADERFUNCTION"); |
@@ -532,8 +547,9 @@ int check_http(void) { | |||
532 | snprintf(dnscache, DEFAULT_BUFFER_SIZE, "%s:%d:%s", host_name, server_port, addrstr); | 547 | snprintf(dnscache, DEFAULT_BUFFER_SIZE, "%s:%d:%s", host_name, server_port, addrstr); |
533 | host = curl_slist_append(NULL, dnscache); | 548 | host = curl_slist_append(NULL, dnscache); |
534 | curl_easy_setopt(curl, CURLOPT_RESOLVE, host); | 549 | curl_easy_setopt(curl, CURLOPT_RESOLVE, host); |
535 | if (verbose >= 1) | 550 | if (verbose >= 1) { |
536 | printf("* curl CURLOPT_RESOLVE: %s\n", dnscache); | 551 | printf("* curl CURLOPT_RESOLVE: %s\n", dnscache); |
552 | } | ||
537 | } | 553 | } |
538 | 554 | ||
539 | // If server_address is an IPv6 address it must be surround by square brackets | 555 | // If server_address is an IPv6 address it must be surround by square brackets |
@@ -552,16 +568,18 @@ int check_http(void) { | |||
552 | snprintf(url, DEFAULT_BUFFER_SIZE, "%s://%s:%d%s", use_ssl ? "https" : "http", | 568 | snprintf(url, DEFAULT_BUFFER_SIZE, "%s://%s:%d%s", use_ssl ? "https" : "http", |
553 | (use_ssl & (host_name != NULL)) ? host_name : server_address, server_port, server_url); | 569 | (use_ssl & (host_name != NULL)) ? host_name : server_address, server_port, server_url); |
554 | 570 | ||
555 | if (verbose >= 1) | 571 | if (verbose >= 1) { |
556 | printf("* curl CURLOPT_URL: %s\n", url); | 572 | printf("* curl CURLOPT_URL: %s\n", url); |
573 | } | ||
557 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_URL, url), "CURLOPT_URL"); | 574 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_URL, url), "CURLOPT_URL"); |
558 | 575 | ||
559 | /* extract proxy information for legacy proxy https requests */ | 576 | /* extract proxy information for legacy proxy https requests */ |
560 | if (!strcmp(http_method, "CONNECT") || strstr(server_url, "http") == server_url) { | 577 | if (!strcmp(http_method, "CONNECT") || strstr(server_url, "http") == server_url) { |
561 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_PROXY, server_address), "CURLOPT_PROXY"); | 578 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_PROXY, server_address), "CURLOPT_PROXY"); |
562 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_PROXYPORT, (long)server_port), "CURLOPT_PROXYPORT"); | 579 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_PROXYPORT, (long)server_port), "CURLOPT_PROXYPORT"); |
563 | if (verbose >= 2) | 580 | if (verbose >= 2) { |
564 | printf("* curl CURLOPT_PROXY: %s:%d\n", server_address, server_port); | 581 | printf("* curl CURLOPT_PROXY: %s:%d\n", server_address, server_port); |
582 | } | ||
565 | http_method = "GET"; | 583 | http_method = "GET"; |
566 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_URL, server_url), "CURLOPT_URL"); | 584 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_URL, server_url), "CURLOPT_URL"); |
567 | } | 585 | } |
@@ -576,12 +594,13 @@ int check_http(void) { | |||
576 | 594 | ||
577 | /* set HTTP method */ | 595 | /* set HTTP method */ |
578 | if (http_method) { | 596 | if (http_method) { |
579 | if (!strcmp(http_method, "POST")) | 597 | if (!strcmp(http_method, "POST")) { |
580 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_POST, 1), "CURLOPT_POST"); | 598 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_POST, 1), "CURLOPT_POST"); |
581 | else if (!strcmp(http_method, "PUT")) | 599 | } else if (!strcmp(http_method, "PUT")) { |
582 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_UPLOAD, 1), "CURLOPT_UPLOAD"); | 600 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_UPLOAD, 1), "CURLOPT_UPLOAD"); |
583 | else | 601 | } else { |
584 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, http_method), "CURLOPT_CUSTOMREQUEST"); | 602 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, http_method), "CURLOPT_CUSTOMREQUEST"); |
603 | } | ||
585 | } | 604 | } |
586 | 605 | ||
587 | /* check if Host header is explicitly set in options */ | 606 | /* check if Host header is explicitly set in options */ |
@@ -629,10 +648,12 @@ int check_http(void) { | |||
629 | } | 648 | } |
630 | 649 | ||
631 | /* client certificate and key to present to server (SSL) */ | 650 | /* client certificate and key to present to server (SSL) */ |
632 | if (client_cert) | 651 | if (client_cert) { |
633 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSLCERT, client_cert), "CURLOPT_SSLCERT"); | 652 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSLCERT, client_cert), "CURLOPT_SSLCERT"); |
634 | if (client_privkey) | 653 | } |
654 | if (client_privkey) { | ||
635 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSLKEY, client_privkey), "CURLOPT_SSLKEY"); | 655 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSLKEY, client_privkey), "CURLOPT_SSLKEY"); |
656 | } | ||
636 | if (ca_cert) { | 657 | if (ca_cert) { |
637 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_CAINFO, ca_cert), "CURLOPT_CAINFO"); | 658 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_CAINFO, ca_cert), "CURLOPT_CAINFO"); |
638 | } | 659 | } |
@@ -700,18 +721,20 @@ int check_http(void) { | |||
700 | } | 721 | } |
701 | # else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) */ | 722 | # else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) */ |
702 | /* old libcurl, our only hope is OpenSSL, otherwise we are out of luck */ | 723 | /* old libcurl, our only hope is OpenSSL, otherwise we are out of luck */ |
703 | if (ssl_library == CURLHELP_SSL_LIBRARY_OPENSSL || ssl_library == CURLHELP_SSL_LIBRARY_LIBRESSL) | 724 | if (ssl_library == CURLHELP_SSL_LIBRARY_OPENSSL || ssl_library == CURLHELP_SSL_LIBRARY_LIBRESSL) { |
704 | add_sslctx_verify_fun = true; | 725 | add_sslctx_verify_fun = true; |
705 | else | 726 | } else { |
706 | die(STATE_CRITICAL, "HTTP CRITICAL - Cannot retrieve certificates (no CURLOPT_SSL_CTX_FUNCTION, no OpenSSL library or libcurl " | 727 | die(STATE_CRITICAL, "HTTP CRITICAL - Cannot retrieve certificates (no CURLOPT_SSL_CTX_FUNCTION, no OpenSSL library or libcurl " |
707 | "too old and has no CURLOPT_CERTINFO)\n"); | 728 | "too old and has no CURLOPT_CERTINFO)\n"); |
729 | } | ||
708 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) */ | 730 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) */ |
709 | } | 731 | } |
710 | 732 | ||
711 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 10, 6) /* required for CURLOPT_SSL_CTX_FUNCTION */ | 733 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 10, 6) /* required for CURLOPT_SSL_CTX_FUNCTION */ |
712 | // ssl ctx function is not available with all ssl backends | 734 | // ssl ctx function is not available with all ssl backends |
713 | if (curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, NULL) != CURLE_UNKNOWN_OPTION) | 735 | if (curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, NULL) != CURLE_UNKNOWN_OPTION) { |
714 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun), "CURLOPT_SSL_CTX_FUNCTION"); | 736 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun), "CURLOPT_SSL_CTX_FUNCTION"); |
737 | } | ||
715 | # endif | 738 | # endif |
716 | 739 | ||
717 | #endif /* LIBCURL_FEATURE_SSL */ | 740 | #endif /* LIBCURL_FEATURE_SSL */ |
@@ -720,12 +743,14 @@ int check_http(void) { | |||
720 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent), "CURLOPT_USERAGENT"); | 743 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent), "CURLOPT_USERAGENT"); |
721 | 744 | ||
722 | /* proxy-authentication */ | 745 | /* proxy-authentication */ |
723 | if (strcmp(proxy_auth, "")) | 746 | if (strcmp(proxy_auth, "")) { |
724 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxy_auth), "CURLOPT_PROXYUSERPWD"); | 747 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxy_auth), "CURLOPT_PROXYUSERPWD"); |
748 | } | ||
725 | 749 | ||
726 | /* authentication */ | 750 | /* authentication */ |
727 | if (strcmp(user_auth, "")) | 751 | if (strcmp(user_auth, "")) { |
728 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_USERPWD, user_auth), "CURLOPT_USERPWD"); | 752 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_USERPWD, user_auth), "CURLOPT_USERPWD"); |
753 | } | ||
729 | 754 | ||
730 | /* TODO: parameter auth method, bitfield of following methods: | 755 | /* TODO: parameter auth method, bitfield of following methods: |
731 | * CURLAUTH_BASIC (default) | 756 | * CURLAUTH_BASIC (default) |
@@ -774,20 +799,23 @@ int check_http(void) { | |||
774 | } | 799 | } |
775 | 800 | ||
776 | /* no-body */ | 801 | /* no-body */ |
777 | if (no_body) | 802 | if (no_body) { |
778 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_NOBODY, 1), "CURLOPT_NOBODY"); | 803 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_NOBODY, 1), "CURLOPT_NOBODY"); |
804 | } | ||
779 | 805 | ||
780 | /* IPv4 or IPv6 forced DNS resolution */ | 806 | /* IPv4 or IPv6 forced DNS resolution */ |
781 | if (address_family == AF_UNSPEC) | 807 | if (address_family == AF_UNSPEC) { |
782 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER), | 808 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER), |
783 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_WHATEVER)"); | 809 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_WHATEVER)"); |
784 | else if (address_family == AF_INET) | 810 | } else if (address_family == AF_INET) { |
785 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4), | 811 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4), |
786 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V4)"); | 812 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V4)"); |
813 | } | ||
787 | #if defined(USE_IPV6) && defined(LIBCURL_FEATURE_IPV6) | 814 | #if defined(USE_IPV6) && defined(LIBCURL_FEATURE_IPV6) |
788 | else if (address_family == AF_INET6) | 815 | else if (address_family == AF_INET6) { |
789 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6), | 816 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6), |
790 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V6)"); | 817 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V6)"); |
818 | } | ||
791 | #endif | 819 | #endif |
792 | 820 | ||
793 | /* either send http POST data (any data, not only POST)*/ | 821 | /* either send http POST data (any data, not only POST)*/ |
@@ -799,16 +827,18 @@ int check_http(void) { | |||
799 | } | 827 | } |
800 | /* NULL indicates "HTTP Continue" in libcurl, provide an empty string | 828 | /* NULL indicates "HTTP Continue" in libcurl, provide an empty string |
801 | * in case of no POST/PUT data */ | 829 | * in case of no POST/PUT data */ |
802 | if (!http_post_data) | 830 | if (!http_post_data) { |
803 | http_post_data = ""; | 831 | http_post_data = ""; |
832 | } | ||
804 | if (!strcmp(http_method, "POST")) { | 833 | if (!strcmp(http_method, "POST")) { |
805 | /* POST method, set payload with CURLOPT_POSTFIELDS */ | 834 | /* POST method, set payload with CURLOPT_POSTFIELDS */ |
806 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_POSTFIELDS, http_post_data), "CURLOPT_POSTFIELDS"); | 835 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_POSTFIELDS, http_post_data), "CURLOPT_POSTFIELDS"); |
807 | } else if (!strcmp(http_method, "PUT")) { | 836 | } else if (!strcmp(http_method, "PUT")) { |
808 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_READFUNCTION, (curl_read_callback)curlhelp_buffer_read_callback), | 837 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_READFUNCTION, (curl_read_callback)curlhelp_buffer_read_callback), |
809 | "CURLOPT_READFUNCTION"); | 838 | "CURLOPT_READFUNCTION"); |
810 | if (curlhelp_initreadbuffer(&put_buf, http_post_data, strlen(http_post_data)) < 0) | 839 | if (curlhelp_initreadbuffer(&put_buf, http_post_data, strlen(http_post_data)) < 0) { |
811 | die(STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating read buffer for PUT\n"); | 840 | die(STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating read buffer for PUT\n"); |
841 | } | ||
812 | put_buf_initialized = true; | 842 | put_buf_initialized = true; |
813 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_READDATA, (void *)&put_buf), "CURLOPT_READDATA"); | 843 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_READDATA, (void *)&put_buf), "CURLOPT_READDATA"); |
814 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_INFILESIZE, (curl_off_t)strlen(http_post_data)), | 844 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_INFILESIZE, (curl_off_t)strlen(http_post_data)), |
@@ -821,15 +851,17 @@ int check_http(void) { | |||
821 | /* enable reading cookies from a file, and if the filename is an empty string, only enable the curl cookie engine */ | 851 | /* enable reading cookies from a file, and if the filename is an empty string, only enable the curl cookie engine */ |
822 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_jar_file), "CURLOPT_COOKIEFILE"); | 852 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_jar_file), "CURLOPT_COOKIEFILE"); |
823 | /* now enable saving cookies to a file, but only if the filename is not an empty string, since writing it would fail */ | 853 | /* now enable saving cookies to a file, but only if the filename is not an empty string, since writing it would fail */ |
824 | if (*cookie_jar_file) | 854 | if (*cookie_jar_file) { |
825 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_jar_file), "CURLOPT_COOKIEJAR"); | 855 | handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_jar_file), "CURLOPT_COOKIEJAR"); |
856 | } | ||
826 | } | 857 | } |
827 | 858 | ||
828 | /* do the request */ | 859 | /* do the request */ |
829 | res = curl_easy_perform(curl); | 860 | res = curl_easy_perform(curl); |
830 | 861 | ||
831 | if (verbose >= 2 && http_post_data) | 862 | if (verbose >= 2 && http_post_data) { |
832 | printf("**** REQUEST CONTENT ****\n%s\n", http_post_data); | 863 | printf("**** REQUEST CONTENT ****\n%s\n", http_post_data); |
864 | } | ||
833 | 865 | ||
834 | /* free header and server IP resolve lists, we don't need it anymore */ | 866 | /* free header and server IP resolve lists, we don't need it anymore */ |
835 | curl_slist_free_all(header_list); | 867 | curl_slist_free_all(header_list); |
@@ -879,8 +911,9 @@ int check_http(void) { | |||
879 | const char *raw_cert = NULL; | 911 | const char *raw_cert = NULL; |
880 | for (i = 0; i < cert_ptr.to_certinfo->num_of_certs; i++) { | 912 | for (i = 0; i < cert_ptr.to_certinfo->num_of_certs; i++) { |
881 | for (slist = cert_ptr.to_certinfo->certinfo[i]; slist; slist = slist->next) { | 913 | for (slist = cert_ptr.to_certinfo->certinfo[i]; slist; slist = slist->next) { |
882 | if (verbose >= 2) | 914 | if (verbose >= 2) { |
883 | printf("%d ** %s\n", i, slist->data); | 915 | printf("%d ** %s\n", i, slist->data); |
916 | } | ||
884 | if (strncmp(slist->data, "Cert:", 5) == 0) { | 917 | if (strncmp(slist->data, "Cert:", 5) == 0) { |
885 | raw_cert = &slist->data[5]; | 918 | raw_cert = &slist->data[5]; |
886 | goto GOT_FIRST_CERT; | 919 | goto GOT_FIRST_CERT; |
@@ -944,8 +977,9 @@ int check_http(void) { | |||
944 | } | 977 | } |
945 | 978 | ||
946 | /* return a CRITICAL status if we couldn't read any data */ | 979 | /* return a CRITICAL status if we couldn't read any data */ |
947 | if (strlen(header_buf.buf) == 0 && strlen(body_buf.buf) == 0) | 980 | if (strlen(header_buf.buf) == 0 && strlen(body_buf.buf) == 0) { |
948 | die(STATE_CRITICAL, _("HTTP CRITICAL - No header received from host\n")); | 981 | die(STATE_CRITICAL, _("HTTP CRITICAL - No header received from host\n")); |
982 | } | ||
949 | 983 | ||
950 | /* get status line of answer, check sanity of HTTP code */ | 984 | /* get status line of answer, check sanity of HTTP code */ |
951 | if (curlhelp_parse_statusline(header_buf.buf, &status_line) < 0) { | 985 | if (curlhelp_parse_statusline(header_buf.buf, &status_line) < 0) { |
@@ -957,8 +991,9 @@ int check_http(void) { | |||
957 | 991 | ||
958 | /* get result code from cURL */ | 992 | /* get result code from cURL */ |
959 | handle_curl_option_return_code(curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code), "CURLINFO_RESPONSE_CODE"); | 993 | handle_curl_option_return_code(curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code), "CURLINFO_RESPONSE_CODE"); |
960 | if (verbose >= 2) | 994 | if (verbose >= 2) { |
961 | printf("* curl CURLINFO_RESPONSE_CODE is %ld\n", code); | 995 | printf("* curl CURLINFO_RESPONSE_CODE is %ld\n", code); |
996 | } | ||
962 | 997 | ||
963 | /* print status line, header, body if verbose */ | 998 | /* print status line, header, body if verbose */ |
964 | if (verbose >= 2) { | 999 | if (verbose >= 2) { |
@@ -967,18 +1002,20 @@ int check_http(void) { | |||
967 | 1002 | ||
968 | /* make sure the status line matches the response we are looking for */ | 1003 | /* make sure the status line matches the response we are looking for */ |
969 | if (!expected_statuscode(status_line.first_line, server_expect)) { | 1004 | if (!expected_statuscode(status_line.first_line, server_expect)) { |
970 | if (server_port == HTTP_PORT) | 1005 | if (server_port == HTTP_PORT) { |
971 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Invalid HTTP response received from host: %s\n"), status_line.first_line); | 1006 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Invalid HTTP response received from host: %s\n"), status_line.first_line); |
972 | else | 1007 | } else { |
973 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Invalid HTTP response received from host on port %d: %s\n"), server_port, | 1008 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Invalid HTTP response received from host on port %d: %s\n"), server_port, |
974 | status_line.first_line); | 1009 | status_line.first_line); |
1010 | } | ||
975 | die(STATE_CRITICAL, "HTTP CRITICAL - %s%s%s", msg, show_body ? "\n" : "", show_body ? body_buf.buf : ""); | 1011 | die(STATE_CRITICAL, "HTTP CRITICAL - %s%s%s", msg, show_body ? "\n" : "", show_body ? body_buf.buf : ""); |
976 | } | 1012 | } |
977 | 1013 | ||
978 | if (server_expect_yn) { | 1014 | if (server_expect_yn) { |
979 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Status line output matched \"%s\" - "), server_expect); | 1015 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Status line output matched \"%s\" - "), server_expect); |
980 | if (verbose) | 1016 | if (verbose) { |
981 | printf("%s\n", msg); | 1017 | printf("%s\n", msg); |
1018 | } | ||
982 | result = STATE_OK; | 1019 | result = STATE_OK; |
983 | } else { | 1020 | } else { |
984 | /* illegal return codes result in a critical state */ | 1021 | /* illegal return codes result in a critical state */ |
@@ -1017,8 +1054,9 @@ int check_http(void) { | |||
1017 | /* libcurl redirection internally, handle error states here */ | 1054 | /* libcurl redirection internally, handle error states here */ |
1018 | if (followmethod == FOLLOW_LIBCURL) { | 1055 | if (followmethod == FOLLOW_LIBCURL) { |
1019 | handle_curl_option_return_code(curl_easy_getinfo(curl, CURLINFO_REDIRECT_COUNT, &redir_depth), "CURLINFO_REDIRECT_COUNT"); | 1056 | handle_curl_option_return_code(curl_easy_getinfo(curl, CURLINFO_REDIRECT_COUNT, &redir_depth), "CURLINFO_REDIRECT_COUNT"); |
1020 | if (verbose >= 2) | 1057 | if (verbose >= 2) { |
1021 | printf(_("* curl LIBINFO_REDIRECT_COUNT is %d\n"), redir_depth); | 1058 | printf(_("* curl LIBINFO_REDIRECT_COUNT is %d\n"), redir_depth); |
1059 | } | ||
1022 | if (redir_depth > max_depth) { | 1060 | if (redir_depth > max_depth) { |
1023 | snprintf(msg, DEFAULT_BUFFER_SIZE, "maximum redirection depth %d exceeded in libcurl", max_depth); | 1061 | snprintf(msg, DEFAULT_BUFFER_SIZE, "maximum redirection depth %d exceeded in libcurl", max_depth); |
1024 | die(STATE_WARNING, "HTTP WARNING - %s", msg); | 1062 | die(STATE_WARNING, "HTTP WARNING - %s", msg); |
@@ -1130,10 +1168,11 @@ int check_http(void) { | |||
1130 | 1168 | ||
1131 | /* Cut-off trailing characters */ | 1169 | /* Cut-off trailing characters */ |
1132 | if (strlen(msg) >= 2) { | 1170 | if (strlen(msg) >= 2) { |
1133 | if (msg[strlen(msg) - 2] == ',') | 1171 | if (msg[strlen(msg) - 2] == ',') { |
1134 | msg[strlen(msg) - 2] = '\0'; | 1172 | msg[strlen(msg) - 2] = '\0'; |
1135 | else | 1173 | } else { |
1136 | msg[strlen(msg) - 3] = '\0'; | 1174 | msg[strlen(msg) - 3] = '\0'; |
1175 | } | ||
1137 | } | 1176 | } |
1138 | 1177 | ||
1139 | /* TODO: separate _() msg and status code: die (result, "HTTP %s: %s\n", state_text(result), msg); */ | 1178 | /* TODO: separate _() msg and status code: die (result, "HTTP %s: %s\n", state_text(result), msg); */ |
@@ -1146,16 +1185,19 @@ int check_http(void) { | |||
1146 | } | 1185 | } |
1147 | 1186 | ||
1148 | int uri_strcmp(const UriTextRangeA range, const char *s) { | 1187 | int uri_strcmp(const UriTextRangeA range, const char *s) { |
1149 | if (!range.first) | 1188 | if (!range.first) { |
1150 | return -1; | 1189 | return -1; |
1151 | if ((size_t)(range.afterLast - range.first) < strlen(s)) | 1190 | } |
1191 | if ((size_t)(range.afterLast - range.first) < strlen(s)) { | ||
1152 | return -1; | 1192 | return -1; |
1193 | } | ||
1153 | return strncmp(s, range.first, min((size_t)(range.afterLast - range.first), strlen(s))); | 1194 | return strncmp(s, range.first, min((size_t)(range.afterLast - range.first), strlen(s))); |
1154 | } | 1195 | } |
1155 | 1196 | ||
1156 | char *uri_string(const UriTextRangeA range, char *buf, size_t buflen) { | 1197 | char *uri_string(const UriTextRangeA range, char *buf, size_t buflen) { |
1157 | if (!range.first) | 1198 | if (!range.first) { |
1158 | return "(null)"; | 1199 | return "(null)"; |
1200 | } | ||
1159 | strncpy(buf, range.first, max(buflen - 1, (size_t)(range.afterLast - range.first))); | 1201 | strncpy(buf, range.first, max(buflen - 1, (size_t)(range.afterLast - range.first))); |
1160 | buf[max(buflen - 1, (size_t)(range.afterLast - range.first))] = '\0'; | 1202 | buf[max(buflen - 1, (size_t)(range.afterLast - range.first))] = '\0'; |
1161 | buf[range.afterLast - range.first] = '\0'; | 1203 | buf[range.afterLast - range.first] = '\0'; |
@@ -1183,12 +1225,14 @@ void redir(curlhelp_write_curlbuf *header_buf) { | |||
1183 | 1225 | ||
1184 | location = get_header_value(headers, nof_headers, "location"); | 1226 | location = get_header_value(headers, nof_headers, "location"); |
1185 | 1227 | ||
1186 | if (verbose >= 2) | 1228 | if (verbose >= 2) { |
1187 | printf(_("* Seen redirect location %s\n"), location); | 1229 | printf(_("* Seen redirect location %s\n"), location); |
1230 | } | ||
1188 | 1231 | ||
1189 | if (++redir_depth > max_depth) | 1232 | if (++redir_depth > max_depth) { |
1190 | die(STATE_WARNING, _("HTTP WARNING - maximum redirection depth %d exceeded - %s%s\n"), max_depth, location, | 1233 | die(STATE_WARNING, _("HTTP WARNING - maximum redirection depth %d exceeded - %s%s\n"), max_depth, location, |
1191 | (display_html ? "</A>" : "")); | 1234 | (display_html ? "</A>" : "")); |
1235 | } | ||
1192 | 1236 | ||
1193 | UriParserStateA state; | 1237 | UriParserStateA state; |
1194 | UriUriA uri; | 1238 | UriUriA uri; |
@@ -1230,10 +1274,11 @@ void redir(curlhelp_write_curlbuf *header_buf) { | |||
1230 | } | 1274 | } |
1231 | 1275 | ||
1232 | if (uri.scheme.first) { | 1276 | if (uri.scheme.first) { |
1233 | if (!uri_strcmp(uri.scheme, "https")) | 1277 | if (!uri_strcmp(uri.scheme, "https")) { |
1234 | use_ssl = true; | 1278 | use_ssl = true; |
1235 | else | 1279 | } else { |
1236 | use_ssl = false; | 1280 | use_ssl = false; |
1281 | } | ||
1237 | } | 1282 | } |
1238 | 1283 | ||
1239 | /* we do a sloppy test here only, because uriparser would have failed | 1284 | /* we do a sloppy test here only, because uriparser would have failed |
@@ -1243,11 +1288,13 @@ void redir(curlhelp_write_curlbuf *header_buf) { | |||
1243 | new_port = atoi(uri_string(uri.portText, buf, DEFAULT_BUFFER_SIZE)); | 1288 | new_port = atoi(uri_string(uri.portText, buf, DEFAULT_BUFFER_SIZE)); |
1244 | } else { | 1289 | } else { |
1245 | new_port = HTTP_PORT; | 1290 | new_port = HTTP_PORT; |
1246 | if (use_ssl) | 1291 | if (use_ssl) { |
1247 | new_port = HTTPS_PORT; | 1292 | new_port = HTTPS_PORT; |
1293 | } | ||
1248 | } | 1294 | } |
1249 | if (new_port > MAX_PORT) | 1295 | if (new_port > MAX_PORT) { |
1250 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Redirection to port above %d - %s%s\n"), MAX_PORT, location, display_html ? "</A>" : ""); | 1296 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Redirection to port above %d - %s%s\n"), MAX_PORT, location, display_html ? "</A>" : ""); |
1297 | } | ||
1251 | 1298 | ||
1252 | /* by RFC 7231 relative URLs in Location should be taken relative to | 1299 | /* by RFC 7231 relative URLs in Location should be taken relative to |
1253 | * the original URL, so we try to form a new absolute URL here | 1300 | * the original URL, so we try to form a new absolute URL here |
@@ -1255,8 +1302,9 @@ void redir(curlhelp_write_curlbuf *header_buf) { | |||
1255 | if (!uri.scheme.first && !uri.hostText.first) { | 1302 | if (!uri.scheme.first && !uri.hostText.first) { |
1256 | new_host = strdup(host_name ? host_name : server_address); | 1303 | new_host = strdup(host_name ? host_name : server_address); |
1257 | new_port = server_port; | 1304 | new_port = server_port; |
1258 | if (use_ssl) | 1305 | if (use_ssl) { |
1259 | uri_string(uri.scheme, "https", DEFAULT_BUFFER_SIZE); | 1306 | uri_string(uri.scheme, "https", DEFAULT_BUFFER_SIZE); |
1307 | } | ||
1260 | } else { | 1308 | } else { |
1261 | new_host = strdup(uri_string(uri.hostText, buf, DEFAULT_BUFFER_SIZE)); | 1309 | new_host = strdup(uri_string(uri.hostText, buf, DEFAULT_BUFFER_SIZE)); |
1262 | } | 1310 | } |
@@ -1273,9 +1321,10 @@ void redir(curlhelp_write_curlbuf *header_buf) { | |||
1273 | } | 1321 | } |
1274 | 1322 | ||
1275 | if (server_port == new_port && !strncmp(server_address, new_host, MAX_IPV4_HOSTLENGTH) && | 1323 | if (server_port == new_port && !strncmp(server_address, new_host, MAX_IPV4_HOSTLENGTH) && |
1276 | (host_name && !strncmp(host_name, new_host, MAX_IPV4_HOSTLENGTH)) && !strcmp(server_url, new_url)) | 1324 | (host_name && !strncmp(host_name, new_host, MAX_IPV4_HOSTLENGTH)) && !strcmp(server_url, new_url)) { |
1277 | die(STATE_CRITICAL, _("HTTP CRITICAL - redirection creates an infinite loop - %s://%s:%d%s%s\n"), use_ssl ? "https" : "http", | 1325 | die(STATE_CRITICAL, _("HTTP CRITICAL - redirection creates an infinite loop - %s://%s:%d%s%s\n"), use_ssl ? "https" : "http", |
1278 | new_host, new_port, new_url, (display_html ? "</A>" : "")); | 1326 | new_host, new_port, new_url, (display_html ? "</A>" : "")); |
1327 | } | ||
1279 | 1328 | ||
1280 | /* set new values for redirected request */ | 1329 | /* set new values for redirected request */ |
1281 | 1330 | ||
@@ -1299,9 +1348,10 @@ void redir(curlhelp_write_curlbuf *header_buf) { | |||
1299 | 1348 | ||
1300 | uriFreeUriMembersA(&uri); | 1349 | uriFreeUriMembersA(&uri); |
1301 | 1350 | ||
1302 | if (verbose) | 1351 | if (verbose) { |
1303 | printf(_("Redirection to %s://%s:%d%s\n"), use_ssl ? "https" : "http", host_name ? host_name : server_address, server_port, | 1352 | printf(_("Redirection to %s://%s:%d%s\n"), use_ssl ? "https" : "http", host_name ? host_name : server_address, server_port, |
1304 | server_url); | 1353 | server_url); |
1354 | } | ||
1305 | 1355 | ||
1306 | /* TODO: the hash component MUST be taken from the original URL and | 1356 | /* TODO: the hash component MUST be taken from the original URL and |
1307 | * attached to the URL in Location | 1357 | * attached to the URL in Location |
@@ -1313,8 +1363,9 @@ void redir(curlhelp_write_curlbuf *header_buf) { | |||
1313 | 1363 | ||
1314 | /* check whether a file exists */ | 1364 | /* check whether a file exists */ |
1315 | void test_file(char *path) { | 1365 | void test_file(char *path) { |
1316 | if (access(path, R_OK) == 0) | 1366 | if (access(path, R_OK) == 0) { |
1317 | return; | 1367 | return; |
1368 | } | ||
1318 | usage2(_("file does not exist or is not readable"), path); | 1369 | usage2(_("file does not exist or is not readable"), path); |
1319 | } | 1370 | } |
1320 | 1371 | ||
@@ -1383,29 +1434,36 @@ bool process_arguments(int argc, char **argv) { | |||
1383 | {"haproxy-protocol", no_argument, 0, HAPROXY_PROTOCOL}, | 1434 | {"haproxy-protocol", no_argument, 0, HAPROXY_PROTOCOL}, |
1384 | {0, 0, 0, 0}}; | 1435 | {0, 0, 0, 0}}; |
1385 | 1436 | ||
1386 | if (argc < 2) | 1437 | if (argc < 2) { |
1387 | return false; | 1438 | return false; |
1439 | } | ||
1388 | 1440 | ||
1389 | /* support check_http compatible arguments */ | 1441 | /* support check_http compatible arguments */ |
1390 | for (c = 1; c < argc; c++) { | 1442 | for (c = 1; c < argc; c++) { |
1391 | if (strcmp("-to", argv[c]) == 0) | 1443 | if (strcmp("-to", argv[c]) == 0) { |
1392 | strcpy(argv[c], "-t"); | 1444 | strcpy(argv[c], "-t"); |
1393 | if (strcmp("-hn", argv[c]) == 0) | 1445 | } |
1446 | if (strcmp("-hn", argv[c]) == 0) { | ||
1394 | strcpy(argv[c], "-H"); | 1447 | strcpy(argv[c], "-H"); |
1395 | if (strcmp("-wt", argv[c]) == 0) | 1448 | } |
1449 | if (strcmp("-wt", argv[c]) == 0) { | ||
1396 | strcpy(argv[c], "-w"); | 1450 | strcpy(argv[c], "-w"); |
1397 | if (strcmp("-ct", argv[c]) == 0) | 1451 | } |
1452 | if (strcmp("-ct", argv[c]) == 0) { | ||
1398 | strcpy(argv[c], "-c"); | 1453 | strcpy(argv[c], "-c"); |
1399 | if (strcmp("-nohtml", argv[c]) == 0) | 1454 | } |
1455 | if (strcmp("-nohtml", argv[c]) == 0) { | ||
1400 | strcpy(argv[c], "-n"); | 1456 | strcpy(argv[c], "-n"); |
1457 | } | ||
1401 | } | 1458 | } |
1402 | 1459 | ||
1403 | server_url = strdup(DEFAULT_SERVER_URL); | 1460 | server_url = strdup(DEFAULT_SERVER_URL); |
1404 | 1461 | ||
1405 | while (1) { | 1462 | while (1) { |
1406 | c = getopt_long(argc, argv, "Vvh46t:c:w:A:k:H:P:j:T:I:a:b:d:e:p:s:R:r:u:f:C:J:K:DnlLS::m:M:NEB", longopts, &option); | 1463 | c = getopt_long(argc, argv, "Vvh46t:c:w:A:k:H:P:j:T:I:a:b:d:e:p:s:R:r:u:f:C:J:K:DnlLS::m:M:NEB", longopts, &option); |
1407 | if (c == -1 || c == EOF || c == 1) | 1464 | if (c == -1 || c == EOF || c == 1) { |
1408 | break; | 1465 | break; |
1466 | } | ||
1409 | 1467 | ||
1410 | switch (c) { | 1468 | switch (c) { |
1411 | case 'h': | 1469 | case 'h': |
@@ -1421,10 +1479,11 @@ bool process_arguments(int argc, char **argv) { | |||
1421 | verbose++; | 1479 | verbose++; |
1422 | break; | 1480 | break; |
1423 | case 't': /* timeout period */ | 1481 | case 't': /* timeout period */ |
1424 | if (!is_intnonneg(optarg)) | 1482 | if (!is_intnonneg(optarg)) { |
1425 | usage2(_("Timeout interval must be a positive integer"), optarg); | 1483 | usage2(_("Timeout interval must be a positive integer"), optarg); |
1426 | else | 1484 | } else { |
1427 | socket_timeout = (int)strtol(optarg, NULL, 10); | 1485 | socket_timeout = (int)strtol(optarg, NULL, 10); |
1486 | } | ||
1428 | break; | 1487 | break; |
1429 | case 'c': /* critical time threshold */ | 1488 | case 'c': /* critical time threshold */ |
1430 | critical_thresholds = optarg; | 1489 | critical_thresholds = optarg; |
@@ -1457,11 +1516,12 @@ bool process_arguments(int argc, char **argv) { | |||
1457 | server_url = strdup(optarg); | 1516 | server_url = strdup(optarg); |
1458 | break; | 1517 | break; |
1459 | case 'p': /* Server port */ | 1518 | case 'p': /* Server port */ |
1460 | if (!is_intnonneg(optarg)) | 1519 | if (!is_intnonneg(optarg)) { |
1461 | usage2(_("Invalid port number, expecting a non-negative number"), optarg); | 1520 | usage2(_("Invalid port number, expecting a non-negative number"), optarg); |
1462 | else { | 1521 | } else { |
1463 | if (strtol(optarg, NULL, 10) > MAX_PORT) | 1522 | if (strtol(optarg, NULL, 10) > MAX_PORT) { |
1464 | usage2(_("Invalid port number, supplied port number is too big"), optarg); | 1523 | usage2(_("Invalid port number, supplied port number is too big"), optarg); |
1524 | } | ||
1465 | server_port = (unsigned short)strtol(optarg, NULL, 10); | 1525 | server_port = (unsigned short)strtol(optarg, NULL, 10); |
1466 | specify_port = true; | 1526 | specify_port = true; |
1467 | } | 1527 | } |
@@ -1475,14 +1535,17 @@ bool process_arguments(int argc, char **argv) { | |||
1475 | proxy_auth[MAX_INPUT_BUFFER - 1] = 0; | 1535 | proxy_auth[MAX_INPUT_BUFFER - 1] = 0; |
1476 | break; | 1536 | break; |
1477 | case 'P': /* HTTP POST data in URL encoded format; ignored if settings already */ | 1537 | case 'P': /* HTTP POST data in URL encoded format; ignored if settings already */ |
1478 | if (!http_post_data) | 1538 | if (!http_post_data) { |
1479 | http_post_data = strdup(optarg); | 1539 | http_post_data = strdup(optarg); |
1480 | if (!http_method) | 1540 | } |
1541 | if (!http_method) { | ||
1481 | http_method = strdup("POST"); | 1542 | http_method = strdup("POST"); |
1543 | } | ||
1482 | break; | 1544 | break; |
1483 | case 'j': /* Set HTTP method */ | 1545 | case 'j': /* Set HTTP method */ |
1484 | if (http_method) | 1546 | if (http_method) { |
1485 | free(http_method); | 1547 | free(http_method); |
1548 | } | ||
1486 | http_method = strdup(optarg); | 1549 | http_method = strdup(optarg); |
1487 | break; | 1550 | break; |
1488 | case 'A': /* useragent */ | 1551 | case 'A': /* useragent */ |
@@ -1490,10 +1553,11 @@ bool process_arguments(int argc, char **argv) { | |||
1490 | user_agent[DEFAULT_BUFFER_SIZE - 1] = '\0'; | 1553 | user_agent[DEFAULT_BUFFER_SIZE - 1] = '\0'; |
1491 | break; | 1554 | break; |
1492 | case 'k': /* Additional headers */ | 1555 | case 'k': /* Additional headers */ |
1493 | if (http_opt_headers_count == 0) | 1556 | if (http_opt_headers_count == 0) { |
1494 | http_opt_headers = malloc(sizeof(char *) * (++http_opt_headers_count)); | 1557 | http_opt_headers = malloc(sizeof(char *) * (++http_opt_headers_count)); |
1495 | else | 1558 | } else { |
1496 | http_opt_headers = realloc(http_opt_headers, sizeof(char *) * (++http_opt_headers_count)); | 1559 | http_opt_headers = realloc(http_opt_headers, sizeof(char *) * (++http_opt_headers_count)); |
1560 | } | ||
1497 | http_opt_headers[http_opt_headers_count - 1] = optarg; | 1561 | http_opt_headers[http_opt_headers_count - 1] = optarg; |
1498 | break; | 1562 | break; |
1499 | case 'L': /* show html link */ | 1563 | case 'L': /* show html link */ |
@@ -1506,18 +1570,21 @@ bool process_arguments(int argc, char **argv) { | |||
1506 | #ifdef LIBCURL_FEATURE_SSL | 1570 | #ifdef LIBCURL_FEATURE_SSL |
1507 | if ((temp = strchr(optarg, ',')) != NULL) { | 1571 | if ((temp = strchr(optarg, ',')) != NULL) { |
1508 | *temp = '\0'; | 1572 | *temp = '\0'; |
1509 | if (!is_intnonneg(optarg)) | 1573 | if (!is_intnonneg(optarg)) { |
1510 | usage2(_("Invalid certificate expiration period"), optarg); | 1574 | usage2(_("Invalid certificate expiration period"), optarg); |
1575 | } | ||
1511 | days_till_exp_warn = atoi(optarg); | 1576 | days_till_exp_warn = atoi(optarg); |
1512 | *temp = ','; | 1577 | *temp = ','; |
1513 | temp++; | 1578 | temp++; |
1514 | if (!is_intnonneg(temp)) | 1579 | if (!is_intnonneg(temp)) { |
1515 | usage2(_("Invalid certificate expiration period"), temp); | 1580 | usage2(_("Invalid certificate expiration period"), temp); |
1581 | } | ||
1516 | days_till_exp_crit = atoi(temp); | 1582 | days_till_exp_crit = atoi(temp); |
1517 | } else { | 1583 | } else { |
1518 | days_till_exp_crit = 0; | 1584 | days_till_exp_crit = 0; |
1519 | if (!is_intnonneg(optarg)) | 1585 | if (!is_intnonneg(optarg)) { |
1520 | usage2(_("Invalid certificate expiration period"), optarg); | 1586 | usage2(_("Invalid certificate expiration period"), optarg); |
1587 | } | ||
1521 | days_till_exp_warn = atoi(optarg); | 1588 | days_till_exp_warn = atoi(optarg); |
1522 | } | 1589 | } |
1523 | check_cert = true; | 1590 | check_cert = true; |
@@ -1566,11 +1633,11 @@ bool process_arguments(int argc, char **argv) { | |||
1566 | *plus_ptr = '\0'; | 1633 | *plus_ptr = '\0'; |
1567 | } | 1634 | } |
1568 | 1635 | ||
1569 | if (optarg[0] == '2') | 1636 | if (optarg[0] == '2') { |
1570 | ssl_version = CURL_SSLVERSION_SSLv2; | 1637 | ssl_version = CURL_SSLVERSION_SSLv2; |
1571 | else if (optarg[0] == '3') | 1638 | } else if (optarg[0] == '3') { |
1572 | ssl_version = CURL_SSLVERSION_SSLv3; | 1639 | ssl_version = CURL_SSLVERSION_SSLv3; |
1573 | else if (!strcmp(optarg, "1") || !strcmp(optarg, "1.0")) | 1640 | } else if (!strcmp(optarg, "1") || !strcmp(optarg, "1.0")) |
1574 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | 1641 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) |
1575 | ssl_version = CURL_SSLVERSION_TLSv1_0; | 1642 | ssl_version = CURL_SSLVERSION_TLSv1_0; |
1576 | # else | 1643 | # else |
@@ -1594,8 +1661,9 @@ bool process_arguments(int argc, char **argv) { | |||
1594 | # else | 1661 | # else |
1595 | ssl_version = CURL_SSLVERSION_DEFAULT; | 1662 | ssl_version = CURL_SSLVERSION_DEFAULT; |
1596 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 52, 0) */ | 1663 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 52, 0) */ |
1597 | else | 1664 | else { |
1598 | usage4(_("Invalid option - Valid SSL/TLS versions: 2, 3, 1, 1.1, 1.2, 1.3 (with optional '+' suffix)")); | 1665 | usage4(_("Invalid option - Valid SSL/TLS versions: 2, 3, 1, 1.1, 1.2, 1.3 (with optional '+' suffix)")); |
1666 | } | ||
1599 | } | 1667 | } |
1600 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 54, 0) | 1668 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 54, 0) |
1601 | if (got_plus) { | 1669 | if (got_plus) { |
@@ -1626,10 +1694,12 @@ bool process_arguments(int argc, char **argv) { | |||
1626 | } | 1694 | } |
1627 | } | 1695 | } |
1628 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 54, 0) */ | 1696 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 54, 0) */ |
1629 | if (verbose >= 2) | 1697 | if (verbose >= 2) { |
1630 | printf(_("* Set SSL/TLS version to %d\n"), ssl_version); | 1698 | printf(_("* Set SSL/TLS version to %d\n"), ssl_version); |
1631 | if (!specify_port) | 1699 | } |
1700 | if (!specify_port) { | ||
1632 | server_port = HTTPS_PORT; | 1701 | server_port = HTTPS_PORT; |
1702 | } | ||
1633 | break; | 1703 | break; |
1634 | #else /* LIBCURL_FEATURE_SSL */ | 1704 | #else /* LIBCURL_FEATURE_SSL */ |
1635 | /* -C -J and -K fall through to here without SSL */ | 1705 | /* -C -J and -K fall through to here without SSL */ |
@@ -1640,35 +1710,37 @@ bool process_arguments(int argc, char **argv) { | |||
1640 | break; | 1710 | break; |
1641 | #endif /* LIBCURL_FEATURE_SSL */ | 1711 | #endif /* LIBCURL_FEATURE_SSL */ |
1642 | case MAX_REDIRS_OPTION: | 1712 | case MAX_REDIRS_OPTION: |
1643 | if (!is_intnonneg(optarg)) | 1713 | if (!is_intnonneg(optarg)) { |
1644 | usage2(_("Invalid max_redirs count"), optarg); | 1714 | usage2(_("Invalid max_redirs count"), optarg); |
1645 | else { | 1715 | } else { |
1646 | max_depth = atoi(optarg); | 1716 | max_depth = atoi(optarg); |
1647 | } | 1717 | } |
1648 | break; | 1718 | break; |
1649 | case 'f': /* onredirect */ | 1719 | case 'f': /* onredirect */ |
1650 | if (!strcmp(optarg, "ok")) | 1720 | if (!strcmp(optarg, "ok")) { |
1651 | onredirect = STATE_OK; | 1721 | onredirect = STATE_OK; |
1652 | else if (!strcmp(optarg, "warning")) | 1722 | } else if (!strcmp(optarg, "warning")) { |
1653 | onredirect = STATE_WARNING; | 1723 | onredirect = STATE_WARNING; |
1654 | else if (!strcmp(optarg, "critical")) | 1724 | } else if (!strcmp(optarg, "critical")) { |
1655 | onredirect = STATE_CRITICAL; | 1725 | onredirect = STATE_CRITICAL; |
1656 | else if (!strcmp(optarg, "unknown")) | 1726 | } else if (!strcmp(optarg, "unknown")) { |
1657 | onredirect = STATE_UNKNOWN; | 1727 | onredirect = STATE_UNKNOWN; |
1658 | else if (!strcmp(optarg, "follow")) | 1728 | } else if (!strcmp(optarg, "follow")) { |
1659 | onredirect = STATE_DEPENDENT; | 1729 | onredirect = STATE_DEPENDENT; |
1660 | else if (!strcmp(optarg, "stickyport")) | 1730 | } else if (!strcmp(optarg, "stickyport")) { |
1661 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_HTTP_CURL, followsticky = STICKY_HOST | STICKY_PORT; | 1731 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_HTTP_CURL, followsticky = STICKY_HOST | STICKY_PORT; |
1662 | else if (!strcmp(optarg, "sticky")) | 1732 | } else if (!strcmp(optarg, "sticky")) { |
1663 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_HTTP_CURL, followsticky = STICKY_HOST; | 1733 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_HTTP_CURL, followsticky = STICKY_HOST; |
1664 | else if (!strcmp(optarg, "follow")) | 1734 | } else if (!strcmp(optarg, "follow")) { |
1665 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_HTTP_CURL, followsticky = STICKY_NONE; | 1735 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_HTTP_CURL, followsticky = STICKY_NONE; |
1666 | else if (!strcmp(optarg, "curl")) | 1736 | } else if (!strcmp(optarg, "curl")) { |
1667 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_LIBCURL; | 1737 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_LIBCURL; |
1668 | else | 1738 | } else { |
1669 | usage2(_("Invalid onredirect option"), optarg); | 1739 | usage2(_("Invalid onredirect option"), optarg); |
1670 | if (verbose >= 2) | 1740 | } |
1741 | if (verbose >= 2) { | ||
1671 | printf(_("* Following redirects set to %s\n"), state_text(onredirect)); | 1742 | printf(_("* Following redirects set to %s\n"), state_text(onredirect)); |
1743 | } | ||
1672 | break; | 1744 | break; |
1673 | case 'd': /* string or substring */ | 1745 | case 'd': /* string or substring */ |
1674 | strncpy(header_expect, optarg, MAX_INPUT_BUFFER - 1); | 1746 | strncpy(header_expect, optarg, MAX_INPUT_BUFFER - 1); |
@@ -1706,12 +1778,13 @@ bool process_arguments(int argc, char **argv) { | |||
1706 | invert_regex = true; | 1778 | invert_regex = true; |
1707 | break; | 1779 | break; |
1708 | case STATE_REGEX: | 1780 | case STATE_REGEX: |
1709 | if (!strcasecmp(optarg, "critical")) | 1781 | if (!strcasecmp(optarg, "critical")) { |
1710 | state_regex = STATE_CRITICAL; | 1782 | state_regex = STATE_CRITICAL; |
1711 | else if (!strcasecmp(optarg, "warning")) | 1783 | } else if (!strcasecmp(optarg, "warning")) { |
1712 | state_regex = STATE_WARNING; | 1784 | state_regex = STATE_WARNING; |
1713 | else | 1785 | } else { |
1714 | usage2(_("Invalid state-regex option"), optarg); | 1786 | usage2(_("Invalid state-regex option"), optarg); |
1787 | } | ||
1715 | break; | 1788 | break; |
1716 | case '4': | 1789 | case '4': |
1717 | address_family = AF_INET; | 1790 | address_family = AF_INET; |
@@ -1732,17 +1805,20 @@ bool process_arguments(int argc, char **argv) { | |||
1732 | if (tmp == NULL) { | 1805 | if (tmp == NULL) { |
1733 | printf("Bad format: try \"-m min:max\"\n"); | 1806 | printf("Bad format: try \"-m min:max\"\n"); |
1734 | exit(STATE_WARNING); | 1807 | exit(STATE_WARNING); |
1735 | } else | 1808 | } else { |
1736 | min_page_len = atoi(tmp); | 1809 | min_page_len = atoi(tmp); |
1810 | } | ||
1737 | 1811 | ||
1738 | tmp = strtok(NULL, ":"); | 1812 | tmp = strtok(NULL, ":"); |
1739 | if (tmp == NULL) { | 1813 | if (tmp == NULL) { |
1740 | printf("Bad format: try \"-m min:max\"\n"); | 1814 | printf("Bad format: try \"-m min:max\"\n"); |
1741 | exit(STATE_WARNING); | 1815 | exit(STATE_WARNING); |
1742 | } else | 1816 | } else { |
1743 | max_page_len = atoi(tmp); | 1817 | max_page_len = atoi(tmp); |
1744 | } else | 1818 | } |
1819 | } else { | ||
1745 | min_page_len = atoi(optarg); | 1820 | min_page_len = atoi(optarg); |
1821 | } | ||
1746 | break; | 1822 | break; |
1747 | } | 1823 | } |
1748 | case 'N': /* no-body */ | 1824 | case 'N': /* no-body */ |
@@ -1751,20 +1827,21 @@ bool process_arguments(int argc, char **argv) { | |||
1751 | case 'M': /* max-age */ | 1827 | case 'M': /* max-age */ |
1752 | { | 1828 | { |
1753 | int L = strlen(optarg); | 1829 | int L = strlen(optarg); |
1754 | if (L && optarg[L - 1] == 'm') | 1830 | if (L && optarg[L - 1] == 'm') { |
1755 | maximum_age = atoi(optarg) * 60; | 1831 | maximum_age = atoi(optarg) * 60; |
1756 | else if (L && optarg[L - 1] == 'h') | 1832 | } else if (L && optarg[L - 1] == 'h') { |
1757 | maximum_age = atoi(optarg) * 60 * 60; | 1833 | maximum_age = atoi(optarg) * 60 * 60; |
1758 | else if (L && optarg[L - 1] == 'd') | 1834 | } else if (L && optarg[L - 1] == 'd') { |
1759 | maximum_age = atoi(optarg) * 60 * 60 * 24; | 1835 | maximum_age = atoi(optarg) * 60 * 60 * 24; |
1760 | else if (L && (optarg[L - 1] == 's' || isdigit(optarg[L - 1]))) | 1836 | } else if (L && (optarg[L - 1] == 's' || isdigit(optarg[L - 1]))) { |
1761 | maximum_age = atoi(optarg); | 1837 | maximum_age = atoi(optarg); |
1762 | else { | 1838 | } else { |
1763 | fprintf(stderr, "unparsable max-age: %s\n", optarg); | 1839 | fprintf(stderr, "unparsable max-age: %s\n", optarg); |
1764 | exit(STATE_WARNING); | 1840 | exit(STATE_WARNING); |
1765 | } | 1841 | } |
1766 | if (verbose >= 2) | 1842 | if (verbose >= 2) { |
1767 | printf("* Maximal age of document set to %d seconds\n", maximum_age); | 1843 | printf("* Maximal age of document set to %d seconds\n", maximum_age); |
1844 | } | ||
1768 | } break; | 1845 | } break; |
1769 | case 'E': /* show extended perfdata */ | 1846 | case 'E': /* show extended perfdata */ |
1770 | show_extended_perfdata = true; | 1847 | show_extended_perfdata = true; |
@@ -1807,38 +1884,47 @@ bool process_arguments(int argc, char **argv) { | |||
1807 | 1884 | ||
1808 | c = optind; | 1885 | c = optind; |
1809 | 1886 | ||
1810 | if (server_address == NULL && c < argc) | 1887 | if (server_address == NULL && c < argc) { |
1811 | server_address = strdup(argv[c++]); | 1888 | server_address = strdup(argv[c++]); |
1889 | } | ||
1812 | 1890 | ||
1813 | if (host_name == NULL && c < argc) | 1891 | if (host_name == NULL && c < argc) { |
1814 | host_name = strdup(argv[c++]); | 1892 | host_name = strdup(argv[c++]); |
1893 | } | ||
1815 | 1894 | ||
1816 | if (server_address == NULL) { | 1895 | if (server_address == NULL) { |
1817 | if (host_name == NULL) | 1896 | if (host_name == NULL) { |
1818 | usage4(_("You must specify a server address or host name")); | 1897 | usage4(_("You must specify a server address or host name")); |
1819 | else | 1898 | } else { |
1820 | server_address = strdup(host_name); | 1899 | server_address = strdup(host_name); |
1900 | } | ||
1821 | } | 1901 | } |
1822 | 1902 | ||
1823 | set_thresholds(&thlds, warning_thresholds, critical_thresholds); | 1903 | set_thresholds(&thlds, warning_thresholds, critical_thresholds); |
1824 | 1904 | ||
1825 | if (critical_thresholds && thlds->critical->end > (double)socket_timeout) | 1905 | if (critical_thresholds && thlds->critical->end > (double)socket_timeout) { |
1826 | socket_timeout = (int)thlds->critical->end + 1; | 1906 | socket_timeout = (int)thlds->critical->end + 1; |
1827 | if (verbose >= 2) | 1907 | } |
1908 | if (verbose >= 2) { | ||
1828 | printf("* Socket timeout set to %ld seconds\n", socket_timeout); | 1909 | printf("* Socket timeout set to %ld seconds\n", socket_timeout); |
1910 | } | ||
1829 | 1911 | ||
1830 | if (http_method == NULL) | 1912 | if (http_method == NULL) { |
1831 | http_method = strdup("GET"); | 1913 | http_method = strdup("GET"); |
1914 | } | ||
1832 | 1915 | ||
1833 | if (client_cert && !client_privkey) | 1916 | if (client_cert && !client_privkey) { |
1834 | usage4(_("If you use a client certificate you must also specify a private key file")); | 1917 | usage4(_("If you use a client certificate you must also specify a private key file")); |
1918 | } | ||
1835 | 1919 | ||
1836 | if (virtual_port == 0) | 1920 | if (virtual_port == 0) { |
1837 | virtual_port = server_port; | 1921 | virtual_port = server_port; |
1838 | else { | 1922 | } else { |
1839 | if ((use_ssl && server_port == HTTPS_PORT) || (!use_ssl && server_port == HTTP_PORT)) | 1923 | if ((use_ssl && server_port == HTTPS_PORT) || (!use_ssl && server_port == HTTP_PORT)) { |
1840 | if (!specify_port) | 1924 | if (!specify_port) { |
1841 | server_port = virtual_port; | 1925 | server_port = virtual_port; |
1926 | } | ||
1927 | } | ||
1842 | } | 1928 | } |
1843 | 1929 | ||
1844 | return true; | 1930 | return true; |
@@ -2113,8 +2199,9 @@ int curlhelp_initwritebuffer(curlhelp_write_curlbuf *buf) { | |||
2113 | buf->bufsize = DEFAULT_BUFFER_SIZE; | 2199 | buf->bufsize = DEFAULT_BUFFER_SIZE; |
2114 | buf->buflen = 0; | 2200 | buf->buflen = 0; |
2115 | buf->buf = (char *)malloc((size_t)buf->bufsize); | 2201 | buf->buf = (char *)malloc((size_t)buf->bufsize); |
2116 | if (buf->buf == NULL) | 2202 | if (buf->buf == NULL) { |
2117 | return -1; | 2203 | return -1; |
2204 | } | ||
2118 | return 0; | 2205 | return 0; |
2119 | } | 2206 | } |
2120 | 2207 | ||
@@ -2156,8 +2243,9 @@ void curlhelp_freewritebuffer(curlhelp_write_curlbuf *buf) { | |||
2156 | int curlhelp_initreadbuffer(curlhelp_read_curlbuf *buf, const char *data, size_t datalen) { | 2243 | int curlhelp_initreadbuffer(curlhelp_read_curlbuf *buf, const char *data, size_t datalen) { |
2157 | buf->buflen = datalen; | 2244 | buf->buflen = datalen; |
2158 | buf->buf = (char *)malloc((size_t)buf->buflen); | 2245 | buf->buf = (char *)malloc((size_t)buf->buflen); |
2159 | if (buf->buf == NULL) | 2246 | if (buf->buf == NULL) { |
2160 | return -1; | 2247 | return -1; |
2248 | } | ||
2161 | memcpy(buf->buf, data, datalen); | 2249 | memcpy(buf->buf, data, datalen); |
2162 | buf->pos = 0; | 2250 | buf->pos = 0; |
2163 | return 0; | 2251 | return 0; |
@@ -2176,11 +2264,13 @@ const char *strrstr2(const char *haystack, const char *needle) { | |||
2176 | const char *prev_pos; | 2264 | const char *prev_pos; |
2177 | const char *pos; | 2265 | const char *pos; |
2178 | 2266 | ||
2179 | if (haystack == NULL || needle == NULL) | 2267 | if (haystack == NULL || needle == NULL) { |
2180 | return NULL; | 2268 | return NULL; |
2269 | } | ||
2181 | 2270 | ||
2182 | if (haystack[0] == '\0' || needle[0] == '\0') | 2271 | if (haystack[0] == '\0' || needle[0] == '\0') { |
2183 | return NULL; | 2272 | return NULL; |
2273 | } | ||
2184 | 2274 | ||
2185 | counter = 0; | 2275 | counter = 0; |
2186 | prev_pos = NULL; | 2276 | prev_pos = NULL; |
@@ -2189,15 +2279,17 @@ const char *strrstr2(const char *haystack, const char *needle) { | |||
2189 | for (;;) { | 2279 | for (;;) { |
2190 | pos = strstr(pos, needle); | 2280 | pos = strstr(pos, needle); |
2191 | if (pos == NULL) { | 2281 | if (pos == NULL) { |
2192 | if (counter == 0) | 2282 | if (counter == 0) { |
2193 | return NULL; | 2283 | return NULL; |
2284 | } | ||
2194 | return prev_pos; | 2285 | return prev_pos; |
2195 | } | 2286 | } |
2196 | counter++; | 2287 | counter++; |
2197 | prev_pos = pos; | 2288 | prev_pos = pos; |
2198 | pos += len; | 2289 | pos += len; |
2199 | if (*pos == '\0') | 2290 | if (*pos == '\0') { |
2200 | return prev_pos; | 2291 | return prev_pos; |
2292 | } | ||
2201 | } | 2293 | } |
2202 | } | 2294 | } |
2203 | 2295 | ||
@@ -2217,13 +2309,15 @@ int curlhelp_parse_statusline(const char *buf, curlhelp_statusline *status_line) | |||
2217 | } | 2309 | } |
2218 | 2310 | ||
2219 | first_line_end = strstr(buf, "\r\n"); | 2311 | first_line_end = strstr(buf, "\r\n"); |
2220 | if (first_line_end == NULL) | 2312 | if (first_line_end == NULL) { |
2221 | return -1; | 2313 | return -1; |
2314 | } | ||
2222 | 2315 | ||
2223 | first_line_len = (size_t)(first_line_end - buf); | 2316 | first_line_len = (size_t)(first_line_end - buf); |
2224 | status_line->first_line = (char *)malloc(first_line_len + 1); | 2317 | status_line->first_line = (char *)malloc(first_line_len + 1); |
2225 | if (status_line->first_line == NULL) | 2318 | if (status_line->first_line == NULL) { |
2226 | return -1; | 2319 | return -1; |
2320 | } | ||
2227 | memcpy(status_line->first_line, buf, first_line_len); | 2321 | memcpy(status_line->first_line, buf, first_line_len); |
2228 | status_line->first_line[first_line_len] = '\0'; | 2322 | status_line->first_line[first_line_len] = '\0'; |
2229 | first_line_buf = strdup(status_line->first_line); | 2323 | first_line_buf = strdup(status_line->first_line); |
@@ -2362,8 +2456,9 @@ int check_document_dates(const curlhelp_write_curlbuf *header_buf, char (*msg)[D | |||
2362 | } else { | 2456 | } else { |
2363 | time_t srv_data = curl_getdate(server_date, NULL); | 2457 | time_t srv_data = curl_getdate(server_date, NULL); |
2364 | time_t doc_data = curl_getdate(document_date, NULL); | 2458 | time_t doc_data = curl_getdate(document_date, NULL); |
2365 | if (verbose >= 2) | 2459 | if (verbose >= 2) { |
2366 | printf("* server date: '%s' (%d), doc_date: '%s' (%d)\n", server_date, (int)srv_data, document_date, (int)doc_data); | 2460 | printf("* server date: '%s' (%d), doc_date: '%s' (%d)\n", server_date, (int)srv_data, document_date, (int)doc_data); |
2461 | } | ||
2367 | if (srv_data <= 0) { | 2462 | if (srv_data <= 0) { |
2368 | char tmp[DEFAULT_BUFFER_SIZE]; | 2463 | char tmp[DEFAULT_BUFFER_SIZE]; |
2369 | 2464 | ||
@@ -2405,10 +2500,12 @@ int check_document_dates(const curlhelp_write_curlbuf *header_buf, char (*msg)[D | |||
2405 | } | 2500 | } |
2406 | } | 2501 | } |
2407 | 2502 | ||
2408 | if (server_date) | 2503 | if (server_date) { |
2409 | free(server_date); | 2504 | free(server_date); |
2410 | if (document_date) | 2505 | } |
2506 | if (document_date) { | ||
2411 | free(document_date); | 2507 | free(document_date); |
2508 | } | ||
2412 | 2509 | ||
2413 | return date_result; | 2510 | return date_result; |
2414 | } | 2511 | } |
@@ -2438,8 +2535,9 @@ int get_content_length(const curlhelp_write_curlbuf *header_buf, const curlhelp_ | |||
2438 | /* TODO: should we warn if the actual and the reported body length don't match? */ | 2535 | /* TODO: should we warn if the actual and the reported body length don't match? */ |
2439 | } | 2536 | } |
2440 | 2537 | ||
2441 | if (content_length_s) | 2538 | if (content_length_s) { |
2442 | free(content_length_s); | 2539 | free(content_length_s); |
2540 | } | ||
2443 | 2541 | ||
2444 | return header_buf->buflen + body_buf->buflen; | 2542 | return header_buf->buflen + body_buf->buflen; |
2445 | } | 2543 | } |
@@ -2452,28 +2550,33 @@ curlhelp_ssl_library curlhelp_get_ssl_library(void) { | |||
2452 | curlhelp_ssl_library ssl_library = CURLHELP_SSL_LIBRARY_UNKNOWN; | 2550 | curlhelp_ssl_library ssl_library = CURLHELP_SSL_LIBRARY_UNKNOWN; |
2453 | 2551 | ||
2454 | version_data = curl_version_info(CURLVERSION_NOW); | 2552 | version_data = curl_version_info(CURLVERSION_NOW); |
2455 | if (version_data == NULL) | 2553 | if (version_data == NULL) { |
2456 | return CURLHELP_SSL_LIBRARY_UNKNOWN; | 2554 | return CURLHELP_SSL_LIBRARY_UNKNOWN; |
2555 | } | ||
2457 | 2556 | ||
2458 | ssl_version = strdup(version_data->ssl_version); | 2557 | ssl_version = strdup(version_data->ssl_version); |
2459 | if (ssl_version == NULL) | 2558 | if (ssl_version == NULL) { |
2460 | return CURLHELP_SSL_LIBRARY_UNKNOWN; | 2559 | return CURLHELP_SSL_LIBRARY_UNKNOWN; |
2560 | } | ||
2461 | 2561 | ||
2462 | library = strtok(ssl_version, "/"); | 2562 | library = strtok(ssl_version, "/"); |
2463 | if (library == NULL) | 2563 | if (library == NULL) { |
2464 | return CURLHELP_SSL_LIBRARY_UNKNOWN; | 2564 | return CURLHELP_SSL_LIBRARY_UNKNOWN; |
2565 | } | ||
2465 | 2566 | ||
2466 | if (strcmp(library, "OpenSSL") == 0) | 2567 | if (strcmp(library, "OpenSSL") == 0) { |
2467 | ssl_library = CURLHELP_SSL_LIBRARY_OPENSSL; | 2568 | ssl_library = CURLHELP_SSL_LIBRARY_OPENSSL; |
2468 | else if (strcmp(library, "LibreSSL") == 0) | 2569 | } else if (strcmp(library, "LibreSSL") == 0) { |
2469 | ssl_library = CURLHELP_SSL_LIBRARY_LIBRESSL; | 2570 | ssl_library = CURLHELP_SSL_LIBRARY_LIBRESSL; |
2470 | else if (strcmp(library, "GnuTLS") == 0) | 2571 | } else if (strcmp(library, "GnuTLS") == 0) { |
2471 | ssl_library = CURLHELP_SSL_LIBRARY_GNUTLS; | 2572 | ssl_library = CURLHELP_SSL_LIBRARY_GNUTLS; |
2472 | else if (strcmp(library, "NSS") == 0) | 2573 | } else if (strcmp(library, "NSS") == 0) { |
2473 | ssl_library = CURLHELP_SSL_LIBRARY_NSS; | 2574 | ssl_library = CURLHELP_SSL_LIBRARY_NSS; |
2575 | } | ||
2474 | 2576 | ||
2475 | if (verbose >= 2) | 2577 | if (verbose >= 2) { |
2476 | printf("* SSL library string is : %s %s (%d)\n", version_data->ssl_version, library, ssl_library); | 2578 | printf("* SSL library string is : %s %s (%d)\n", version_data->ssl_version, library, ssl_library); |
2579 | } | ||
2477 | 2580 | ||
2478 | free(ssl_version); | 2581 | free(ssl_version); |
2479 | 2582 | ||
@@ -2503,14 +2606,16 @@ time_t parse_cert_date(const char *s) { | |||
2503 | time_t date; | 2606 | time_t date; |
2504 | char *res; | 2607 | char *res; |
2505 | 2608 | ||
2506 | if (!s) | 2609 | if (!s) { |
2507 | return -1; | 2610 | return -1; |
2611 | } | ||
2508 | 2612 | ||
2509 | /* Jan 17 14:25:12 2020 GMT */ | 2613 | /* Jan 17 14:25:12 2020 GMT */ |
2510 | res = strptime(s, "%Y-%m-%d %H:%M:%S GMT", &tm); | 2614 | res = strptime(s, "%Y-%m-%d %H:%M:%S GMT", &tm); |
2511 | /* Sep 11 12:00:00 2020 GMT */ | 2615 | /* Sep 11 12:00:00 2020 GMT */ |
2512 | if (res == NULL) | 2616 | if (res == NULL) { |
2513 | strptime(s, "%Y %m %d %H:%M:%S GMT", &tm); | 2617 | strptime(s, "%Y %m %d %H:%M:%S GMT", &tm); |
2618 | } | ||
2514 | date = mktime(&tm); | 2619 | date = mktime(&tm); |
2515 | 2620 | ||
2516 | return date; | 2621 | return date; |
@@ -2534,8 +2639,9 @@ int net_noopenssl_check_certificate(cert_ptr_union *cert_ptr, int days_till_exp_ | |||
2534 | char timestamp[50] = ""; | 2639 | char timestamp[50] = ""; |
2535 | int status = STATE_UNKNOWN; | 2640 | int status = STATE_UNKNOWN; |
2536 | 2641 | ||
2537 | if (verbose >= 2) | 2642 | if (verbose >= 2) { |
2538 | printf("**** REQUEST CERTIFICATES ****\n"); | 2643 | printf("**** REQUEST CERTIFICATES ****\n"); |
2644 | } | ||
2539 | 2645 | ||
2540 | for (i = 0; i < cert_ptr->to_certinfo->num_of_certs; i++) { | 2646 | for (i = 0; i < cert_ptr->to_certinfo->num_of_certs; i++) { |
2541 | for (slist = cert_ptr->to_certinfo->certinfo[i]; slist; slist = slist->next) { | 2647 | for (slist = cert_ptr->to_certinfo->certinfo[i]; slist; slist = slist->next) { |
@@ -2563,14 +2669,16 @@ int net_noopenssl_check_certificate(cert_ptr_union *cert_ptr, int days_till_exp_ | |||
2563 | } else if (strncasecmp(slist->data, "Cert:", 5) == 0) { | 2669 | } else if (strncasecmp(slist->data, "Cert:", 5) == 0) { |
2564 | goto HAVE_FIRST_CERT; | 2670 | goto HAVE_FIRST_CERT; |
2565 | } | 2671 | } |
2566 | if (verbose >= 2) | 2672 | if (verbose >= 2) { |
2567 | printf("%d ** %s\n", i, slist->data); | 2673 | printf("%d ** %s\n", i, slist->data); |
2674 | } | ||
2568 | } | 2675 | } |
2569 | } | 2676 | } |
2570 | HAVE_FIRST_CERT: | 2677 | HAVE_FIRST_CERT: |
2571 | 2678 | ||
2572 | if (verbose >= 2) | 2679 | if (verbose >= 2) { |
2573 | printf("**** REQUEST CERTIFICATES ****\n"); | 2680 | printf("**** REQUEST CERTIFICATES ****\n"); |
2681 | } | ||
2574 | 2682 | ||
2575 | if (!cname_found) { | 2683 | if (!cname_found) { |
2576 | printf("%s\n", _("CRITICAL - Cannot retrieve certificate subject.")); | 2684 | printf("%s\n", _("CRITICAL - Cannot retrieve certificate subject.")); |
@@ -2597,42 +2705,47 @@ HAVE_FIRST_CERT: | |||
2597 | setenv("TZ", "GMT", 1); | 2705 | setenv("TZ", "GMT", 1); |
2598 | tzset(); | 2706 | tzset(); |
2599 | strftime(timestamp, 50, "%c %z", localtime(&end_date)); | 2707 | strftime(timestamp, 50, "%c %z", localtime(&end_date)); |
2600 | if (tz) | 2708 | if (tz) { |
2601 | setenv("TZ", tz, 1); | 2709 | setenv("TZ", tz, 1); |
2602 | else | 2710 | } else { |
2603 | unsetenv("TZ"); | 2711 | unsetenv("TZ"); |
2712 | } | ||
2604 | tzset(); | 2713 | tzset(); |
2605 | 2714 | ||
2606 | if (days_left > 0 && days_left <= days_till_exp_warn) { | 2715 | if (days_left > 0 && days_left <= days_till_exp_warn) { |
2607 | printf(_("%s - Certificate '%s' expires in %d day(s) (%s).\n"), (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", | 2716 | printf(_("%s - Certificate '%s' expires in %d day(s) (%s).\n"), (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", |
2608 | host_name, days_left, timestamp); | 2717 | host_name, days_left, timestamp); |
2609 | if (days_left > days_till_exp_crit) | 2718 | if (days_left > days_till_exp_crit) { |
2610 | status = STATE_WARNING; | 2719 | status = STATE_WARNING; |
2611 | else | 2720 | } else { |
2612 | status = STATE_CRITICAL; | 2721 | status = STATE_CRITICAL; |
2722 | } | ||
2613 | } else if (days_left == 0 && time_left > 0) { | 2723 | } else if (days_left == 0 && time_left > 0) { |
2614 | if (time_left >= 3600) | 2724 | if (time_left >= 3600) { |
2615 | time_remaining = (int)time_left / 3600; | 2725 | time_remaining = (int)time_left / 3600; |
2616 | else | 2726 | } else { |
2617 | time_remaining = (int)time_left / 60; | 2727 | time_remaining = (int)time_left / 60; |
2728 | } | ||
2618 | 2729 | ||
2619 | printf(_("%s - Certificate '%s' expires in %u %s (%s)\n"), (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", host_name, | 2730 | printf(_("%s - Certificate '%s' expires in %u %s (%s)\n"), (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", host_name, |
2620 | time_remaining, time_left >= 3600 ? "hours" : "minutes", timestamp); | 2731 | time_remaining, time_left >= 3600 ? "hours" : "minutes", timestamp); |
2621 | 2732 | ||
2622 | if (days_left > days_till_exp_crit) | 2733 | if (days_left > days_till_exp_crit) { |
2623 | status = STATE_WARNING; | 2734 | status = STATE_WARNING; |
2624 | else | 2735 | } else { |
2625 | status = STATE_CRITICAL; | 2736 | status = STATE_CRITICAL; |
2737 | } | ||
2626 | } else if (time_left < 0) { | 2738 | } else if (time_left < 0) { |
2627 | printf(_("CRITICAL - Certificate '%s' expired on %s.\n"), host_name, timestamp); | 2739 | printf(_("CRITICAL - Certificate '%s' expired on %s.\n"), host_name, timestamp); |
2628 | status = STATE_CRITICAL; | 2740 | status = STATE_CRITICAL; |
2629 | } else if (days_left == 0) { | 2741 | } else if (days_left == 0) { |
2630 | printf(_("%s - Certificate '%s' just expired (%s).\n"), (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", host_name, | 2742 | printf(_("%s - Certificate '%s' just expired (%s).\n"), (days_left > days_till_exp_crit) ? "WARNING" : "CRITICAL", host_name, |
2631 | timestamp); | 2743 | timestamp); |
2632 | if (days_left > days_till_exp_crit) | 2744 | if (days_left > days_till_exp_crit) { |
2633 | status = STATE_WARNING; | 2745 | status = STATE_WARNING; |
2634 | else | 2746 | } else { |
2635 | status = STATE_CRITICAL; | 2747 | status = STATE_CRITICAL; |
2748 | } | ||
2636 | } else { | 2749 | } else { |
2637 | printf(_("OK - Certificate '%s' will expire on %s.\n"), host_name, timestamp); | 2750 | printf(_("OK - Certificate '%s' will expire on %s.\n"), host_name, timestamp); |
2638 | status = STATE_OK; | 2751 | status = STATE_OK; |