diff options
Diffstat (limited to 'plugins/check_curl.c')
| -rw-r--r-- | plugins/check_curl.c | 2268 |
1 files changed, 2268 insertions, 0 deletions
diff --git a/plugins/check_curl.c b/plugins/check_curl.c new file mode 100644 index 00000000..637e9ba3 --- /dev/null +++ b/plugins/check_curl.c | |||
| @@ -0,0 +1,2268 @@ | |||
| 1 | /***************************************************************************** | ||
| 2 | * | ||
| 3 | * Monitoring check_curl plugin | ||
| 4 | * | ||
| 5 | * License: GPL | ||
| 6 | * Copyright (c) 1999-2018 Monitoring Plugins Development Team | ||
| 7 | * | ||
| 8 | * Description: | ||
| 9 | * | ||
| 10 | * This file contains the check_curl plugin | ||
| 11 | * | ||
| 12 | * This plugin tests the HTTP service on the specified host. It can test | ||
| 13 | * normal (http) and secure (https) servers, follow redirects, search for | ||
| 14 | * strings and regular expressions, check connection times, and report on | ||
| 15 | * certificate expiration times. | ||
| 16 | * | ||
| 17 | * This plugin uses functions from the curl library, see | ||
| 18 | * http://curl.haxx.se | ||
| 19 | * | ||
| 20 | * This program is free software: you can redistribute it and/or modify | ||
| 21 | * it under the terms of the GNU General Public License as published by | ||
| 22 | * the Free Software Foundation, either version 3 of the License, or | ||
| 23 | * (at your option) any later version. | ||
| 24 | * | ||
| 25 | * This program is distributed in the hope that it will be useful, | ||
| 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 28 | * GNU General Public License for more details. | ||
| 29 | * | ||
| 30 | * You should have received a copy of the GNU General Public License | ||
| 31 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 32 | * | ||
| 33 | * | ||
| 34 | *****************************************************************************/ | ||
| 35 | const char *progname = "check_http"; | ||
| 36 | |||
| 37 | const char *copyright = "2006-2018"; | ||
| 38 | const char *email = "devel@monitoring-plugins.org"; | ||
| 39 | |||
| 40 | #include <ctype.h> | ||
| 41 | |||
| 42 | #include "common.h" | ||
| 43 | #include "utils.h" | ||
| 44 | |||
| 45 | #ifndef LIBCURL_PROTOCOL_HTTP | ||
| 46 | #error libcurl compiled without HTTP support, compiling check_curl plugin does not makes a lot of sense | ||
| 47 | #endif | ||
| 48 | |||
| 49 | #include "curl/curl.h" | ||
| 50 | #include "curl/easy.h" | ||
| 51 | |||
| 52 | #include "picohttpparser.h" | ||
| 53 | |||
| 54 | #include "uriparser/Uri.h" | ||
| 55 | |||
| 56 | #include <arpa/inet.h> | ||
| 57 | |||
| 58 | #define MAKE_LIBCURL_VERSION(major, minor, patch) ((major)*0x10000 + (minor)*0x100 + (patch)) | ||
| 59 | |||
| 60 | #define DEFAULT_BUFFER_SIZE 2048 | ||
| 61 | #define DEFAULT_SERVER_URL "/" | ||
| 62 | #define HTTP_EXPECT "HTTP/1." | ||
| 63 | #define DEFAULT_MAX_REDIRS 15 | ||
| 64 | #define INET_ADDR_MAX_SIZE INET6_ADDRSTRLEN | ||
| 65 | enum { | ||
| 66 | MAX_IPV4_HOSTLENGTH = 255, | ||
| 67 | HTTP_PORT = 80, | ||
| 68 | HTTPS_PORT = 443, | ||
| 69 | MAX_PORT = 65535 | ||
| 70 | }; | ||
| 71 | |||
| 72 | enum { | ||
| 73 | STICKY_NONE = 0, | ||
| 74 | STICKY_HOST = 1, | ||
| 75 | STICKY_PORT = 2 | ||
| 76 | }; | ||
| 77 | |||
| 78 | enum { | ||
| 79 | FOLLOW_HTTP_CURL = 0, | ||
| 80 | FOLLOW_LIBCURL = 1 | ||
| 81 | }; | ||
| 82 | |||
| 83 | /* for buffers for header and body */ | ||
| 84 | typedef struct { | ||
| 85 | char *buf; | ||
| 86 | size_t buflen; | ||
| 87 | size_t bufsize; | ||
| 88 | } curlhelp_write_curlbuf; | ||
| 89 | |||
| 90 | /* for buffering the data sent in PUT */ | ||
| 91 | typedef struct { | ||
| 92 | char *buf; | ||
| 93 | size_t buflen; | ||
| 94 | off_t pos; | ||
| 95 | } curlhelp_read_curlbuf; | ||
| 96 | |||
| 97 | /* for parsing the HTTP status line */ | ||
| 98 | typedef struct { | ||
| 99 | int http_major; /* major version of the protocol, always 1 (HTTP/0.9 | ||
| 100 | * never reached the big internet most likely) */ | ||
| 101 | int http_minor; /* minor version of the protocol, usually 0 or 1 */ | ||
| 102 | int http_code; /* HTTP return code as in RFC 2145 */ | ||
| 103 | int http_subcode; /* Microsoft IIS extension, HTTP subcodes, see | ||
| 104 | * http://support.microsoft.com/kb/318380/en-us */ | ||
| 105 | const char *msg; /* the human readable message */ | ||
| 106 | char *first_line; /* a copy of the first line */ | ||
| 107 | } curlhelp_statusline; | ||
| 108 | |||
| 109 | /* to know the underlying SSL library used by libcurl */ | ||
| 110 | typedef enum curlhelp_ssl_library { | ||
| 111 | CURLHELP_SSL_LIBRARY_UNKNOWN, | ||
| 112 | CURLHELP_SSL_LIBRARY_OPENSSL, | ||
| 113 | CURLHELP_SSL_LIBRARY_LIBRESSL, | ||
| 114 | CURLHELP_SSL_LIBRARY_GNUTLS, | ||
| 115 | CURLHELP_SSL_LIBRARY_NSS | ||
| 116 | } curlhelp_ssl_library; | ||
| 117 | |||
| 118 | enum { | ||
| 119 | REGS = 2, | ||
| 120 | MAX_RE_SIZE = 256 | ||
| 121 | }; | ||
| 122 | #include "regex.h" | ||
| 123 | regex_t preg; | ||
| 124 | regmatch_t pmatch[REGS]; | ||
| 125 | char regexp[MAX_RE_SIZE]; | ||
| 126 | int cflags = REG_NOSUB | REG_EXTENDED | REG_NEWLINE; | ||
| 127 | int errcode; | ||
| 128 | int invert_regex = 0; | ||
| 129 | |||
| 130 | char *server_address; | ||
| 131 | char *host_name; | ||
| 132 | char *server_url = 0; | ||
| 133 | char server_ip[DEFAULT_BUFFER_SIZE]; | ||
| 134 | struct curl_slist *server_ips = NULL; | ||
| 135 | int specify_port = FALSE; | ||
| 136 | unsigned short server_port = HTTP_PORT; | ||
| 137 | unsigned short virtual_port = 0; | ||
| 138 | int host_name_length; | ||
| 139 | char output_header_search[30] = ""; | ||
| 140 | char output_string_search[30] = ""; | ||
| 141 | char *warning_thresholds = NULL; | ||
| 142 | char *critical_thresholds = NULL; | ||
| 143 | int days_till_exp_warn, days_till_exp_crit; | ||
| 144 | thresholds *thlds; | ||
| 145 | char user_agent[DEFAULT_BUFFER_SIZE]; | ||
| 146 | int verbose = 0; | ||
| 147 | int show_extended_perfdata = FALSE; | ||
| 148 | int min_page_len = 0; | ||
| 149 | int max_page_len = 0; | ||
| 150 | int redir_depth = 0; | ||
| 151 | int max_depth = DEFAULT_MAX_REDIRS; | ||
| 152 | char *http_method = NULL; | ||
| 153 | char *http_post_data = NULL; | ||
| 154 | char *http_content_type = NULL; | ||
| 155 | CURL *curl; | ||
| 156 | struct curl_slist *header_list = NULL; | ||
| 157 | curlhelp_write_curlbuf body_buf; | ||
| 158 | curlhelp_write_curlbuf header_buf; | ||
| 159 | curlhelp_statusline status_line; | ||
| 160 | curlhelp_read_curlbuf put_buf; | ||
| 161 | char http_header[DEFAULT_BUFFER_SIZE]; | ||
| 162 | long code; | ||
| 163 | long socket_timeout = DEFAULT_SOCKET_TIMEOUT; | ||
| 164 | double total_time; | ||
| 165 | double time_connect; | ||
| 166 | double time_appconnect; | ||
| 167 | double time_headers; | ||
| 168 | double time_firstbyte; | ||
| 169 | char errbuf[CURL_ERROR_SIZE+1]; | ||
| 170 | CURLcode res; | ||
| 171 | char url[DEFAULT_BUFFER_SIZE]; | ||
| 172 | char msg[DEFAULT_BUFFER_SIZE]; | ||
| 173 | char perfstring[DEFAULT_BUFFER_SIZE]; | ||
| 174 | char header_expect[MAX_INPUT_BUFFER] = ""; | ||
| 175 | char string_expect[MAX_INPUT_BUFFER] = ""; | ||
| 176 | char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT; | ||
| 177 | int server_expect_yn = 0; | ||
| 178 | char user_auth[MAX_INPUT_BUFFER] = ""; | ||
| 179 | char proxy_auth[MAX_INPUT_BUFFER] = ""; | ||
| 180 | char **http_opt_headers; | ||
| 181 | int http_opt_headers_count = 0; | ||
| 182 | int display_html = FALSE; | ||
| 183 | int onredirect = STATE_OK; | ||
| 184 | int followmethod = FOLLOW_HTTP_CURL; | ||
| 185 | int followsticky = STICKY_NONE; | ||
| 186 | int use_ssl = FALSE; | ||
| 187 | int use_sni = TRUE; | ||
| 188 | int check_cert = FALSE; | ||
| 189 | typedef union { | ||
| 190 | struct curl_slist* to_info; | ||
| 191 | struct curl_certinfo* to_certinfo; | ||
| 192 | } cert_ptr_union; | ||
| 193 | cert_ptr_union cert_ptr; | ||
| 194 | int ssl_version = CURL_SSLVERSION_DEFAULT; | ||
| 195 | char *client_cert = NULL; | ||
| 196 | char *client_privkey = NULL; | ||
| 197 | char *ca_cert = NULL; | ||
| 198 | int is_openssl_callback = FALSE; | ||
| 199 | #if defined(HAVE_SSL) && defined(USE_OPENSSL) | ||
| 200 | X509 *cert = NULL; | ||
| 201 | #endif /* defined(HAVE_SSL) && defined(USE_OPENSSL) */ | ||
| 202 | int no_body = FALSE; | ||
| 203 | int maximum_age = -1; | ||
| 204 | int address_family = AF_UNSPEC; | ||
| 205 | curlhelp_ssl_library ssl_library = CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
| 206 | |||
| 207 | int process_arguments (int, char**); | ||
| 208 | void handle_curl_option_return_code (CURLcode res, const char* option); | ||
| 209 | int check_http (void); | ||
| 210 | void redir (curlhelp_write_curlbuf*); | ||
| 211 | char *perfd_time (double microsec); | ||
| 212 | char *perfd_time_connect (double microsec); | ||
| 213 | char *perfd_time_ssl (double microsec); | ||
| 214 | char *perfd_time_firstbyte (double microsec); | ||
| 215 | char *perfd_time_headers (double microsec); | ||
| 216 | char *perfd_time_transfer (double microsec); | ||
| 217 | char *perfd_size (int page_len); | ||
| 218 | void print_help (void); | ||
| 219 | void print_usage (void); | ||
| 220 | void print_curl_version (void); | ||
| 221 | int curlhelp_initwritebuffer (curlhelp_write_curlbuf*); | ||
| 222 | int curlhelp_buffer_write_callback (void*, size_t , size_t , void*); | ||
| 223 | void curlhelp_freewritebuffer (curlhelp_write_curlbuf*); | ||
| 224 | int curlhelp_initreadbuffer (curlhelp_read_curlbuf *, const char *, size_t); | ||
| 225 | int curlhelp_buffer_read_callback (void *, size_t , size_t , void *); | ||
| 226 | void curlhelp_freereadbuffer (curlhelp_read_curlbuf *); | ||
| 227 | curlhelp_ssl_library curlhelp_get_ssl_library (CURL*); | ||
| 228 | const char* curlhelp_get_ssl_library_string (curlhelp_ssl_library); | ||
| 229 | int net_noopenssl_check_certificate (cert_ptr_union*, int, int); | ||
| 230 | |||
| 231 | int curlhelp_parse_statusline (const char*, curlhelp_statusline *); | ||
| 232 | void curlhelp_free_statusline (curlhelp_statusline *); | ||
| 233 | char *get_header_value (const struct phr_header* headers, const size_t nof_headers, const char* header); | ||
| 234 | int check_document_dates (const curlhelp_write_curlbuf *, char (*msg)[DEFAULT_BUFFER_SIZE]); | ||
| 235 | int get_content_length (const curlhelp_write_curlbuf* header_buf, const curlhelp_write_curlbuf* body_buf); | ||
| 236 | |||
| 237 | #if defined(HAVE_SSL) && defined(USE_OPENSSL) | ||
| 238 | int np_net_ssl_check_certificate(X509 *certificate, int days_till_exp_warn, int days_till_exp_crit); | ||
| 239 | #endif /* defined(HAVE_SSL) && defined(USE_OPENSSL) */ | ||
| 240 | |||
| 241 | void remove_newlines (char *); | ||
| 242 | void test_file (char *); | ||
| 243 | |||
| 244 | int | ||
| 245 | main (int argc, char **argv) | ||
| 246 | { | ||
| 247 | int result = STATE_UNKNOWN; | ||
| 248 | |||
| 249 | setlocale (LC_ALL, ""); | ||
| 250 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 251 | textdomain (PACKAGE); | ||
| 252 | |||
| 253 | /* Parse extra opts if any */ | ||
| 254 | argv = np_extra_opts (&argc, argv, progname); | ||
| 255 | |||
| 256 | /* set defaults */ | ||
| 257 | snprintf( user_agent, DEFAULT_BUFFER_SIZE, "%s/v%s (monitoring-plugins %s, %s)", | ||
| 258 | progname, NP_VERSION, VERSION, curl_version()); | ||
| 259 | |||
| 260 | /* parse arguments */ | ||
| 261 | if (process_arguments (argc, argv) == ERROR) | ||
| 262 | usage4 (_("Could not parse arguments")); | ||
| 263 | |||
| 264 | if (display_html == TRUE) | ||
| 265 | printf ("<A HREF=\"%s://%s:%d%s\" target=\"_blank\">", | ||
| 266 | use_ssl ? "https" : "http", | ||
| 267 | host_name ? host_name : server_address, | ||
| 268 | virtual_port ? virtual_port : server_port, | ||
| 269 | server_url); | ||
| 270 | |||
| 271 | result = check_http (); | ||
| 272 | return result; | ||
| 273 | } | ||
| 274 | |||
| 275 | #ifdef HAVE_SSL | ||
| 276 | #ifdef USE_OPENSSL | ||
| 277 | |||
| 278 | int verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) | ||
| 279 | { | ||
| 280 | /* TODO: we get all certificates of the chain, so which ones | ||
| 281 | * should we test? | ||
| 282 | * TODO: is the last certificate always the server certificate? | ||
| 283 | */ | ||
| 284 | cert = X509_STORE_CTX_get_current_cert(x509_ctx); | ||
| 285 | return 1; | ||
| 286 | } | ||
| 287 | |||
| 288 | CURLcode sslctxfun(CURL *curl, SSL_CTX *sslctx, void *parm) | ||
| 289 | { | ||
| 290 | SSL_CTX_set_verify(sslctx, SSL_VERIFY_PEER, verify_callback); | ||
| 291 | |||
| 292 | return CURLE_OK; | ||
| 293 | } | ||
| 294 | |||
| 295 | #endif /* USE_OPENSSL */ | ||
| 296 | #endif /* HAVE_SSL */ | ||
| 297 | |||
| 298 | /* Checks if the server 'reply' is one of the expected 'statuscodes' */ | ||
| 299 | static int | ||
| 300 | expected_statuscode (const char *reply, const char *statuscodes) | ||
| 301 | { | ||
| 302 | char *expected, *code; | ||
| 303 | int result = 0; | ||
| 304 | |||
| 305 | if ((expected = strdup (statuscodes)) == NULL) | ||
| 306 | die (STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n")); | ||
| 307 | |||
| 308 | for (code = strtok (expected, ","); code != NULL; code = strtok (NULL, ",")) | ||
| 309 | if (strstr (reply, code) != NULL) { | ||
| 310 | result = 1; | ||
| 311 | break; | ||
| 312 | } | ||
| 313 | |||
| 314 | free (expected); | ||
| 315 | return result; | ||
| 316 | } | ||
| 317 | |||
| 318 | void | ||
| 319 | handle_curl_option_return_code (CURLcode res, const char* option) | ||
| 320 | { | ||
| 321 | if (res != CURLE_OK) { | ||
| 322 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("Error while setting cURL option '%s': cURL returned %d - %s"), | ||
| 323 | option, res, curl_easy_strerror(res)); | ||
| 324 | die (STATE_CRITICAL, "HTTP CRITICAL - %s\n", msg); | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | int | ||
| 329 | check_http (void) | ||
| 330 | { | ||
| 331 | int result = STATE_OK; | ||
| 332 | int page_len = 0; | ||
| 333 | int i; | ||
| 334 | char *force_host_header = NULL; | ||
| 335 | |||
| 336 | /* initialize curl */ | ||
| 337 | if (curl_global_init (CURL_GLOBAL_DEFAULT) != CURLE_OK) | ||
| 338 | die (STATE_UNKNOWN, "HTTP UNKNOWN - curl_global_init failed\n"); | ||
| 339 | |||
| 340 | if ((curl = curl_easy_init()) == NULL) | ||
| 341 | die (STATE_UNKNOWN, "HTTP UNKNOWN - curl_easy_init failed\n"); | ||
| 342 | |||
| 343 | if (verbose >= 1) | ||
| 344 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_VERBOSE, TRUE), "CURLOPT_VERBOSE"); | ||
| 345 | |||
| 346 | /* print everything on stdout like check_http would do */ | ||
| 347 | handle_curl_option_return_code (curl_easy_setopt(curl, CURLOPT_STDERR, stdout), "CURLOPT_STDERR"); | ||
| 348 | |||
| 349 | /* initialize buffer for body of the answer */ | ||
| 350 | if (curlhelp_initwritebuffer(&body_buf) < 0) | ||
| 351 | die (STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for body\n"); | ||
| 352 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)curlhelp_buffer_write_callback), "CURLOPT_WRITEFUNCTION"); | ||
| 353 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *)&body_buf), "CURLOPT_WRITEDATA"); | ||
| 354 | |||
| 355 | /* initialize buffer for header of the answer */ | ||
| 356 | if (curlhelp_initwritebuffer( &header_buf ) < 0) | ||
| 357 | die (STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for header\n" ); | ||
| 358 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_HEADERFUNCTION, (curl_write_callback)curlhelp_buffer_write_callback), "CURLOPT_HEADERFUNCTION"); | ||
| 359 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_WRITEHEADER, (void *)&header_buf), "CURLOPT_WRITEHEADER"); | ||
| 360 | |||
| 361 | /* set the error buffer */ | ||
| 362 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, errbuf), "CURLOPT_ERRORBUFFER"); | ||
| 363 | |||
| 364 | /* set timeouts */ | ||
| 365 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_CONNECTTIMEOUT, socket_timeout), "CURLOPT_CONNECTTIMEOUT"); | ||
| 366 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_TIMEOUT, socket_timeout), "CURLOPT_TIMEOUT"); | ||
| 367 | |||
| 368 | /* compose URL: use the address we want to connect to, set Host: header later */ | ||
| 369 | snprintf (url, DEFAULT_BUFFER_SIZE, "%s://%s:%d%s", | ||
| 370 | use_ssl ? "https" : "http", | ||
| 371 | use_ssl ? host_name : server_address, | ||
| 372 | server_port, | ||
| 373 | server_url | ||
| 374 | ); | ||
| 375 | |||
| 376 | if (verbose>=1) | ||
| 377 | printf ("* curl CURLOPT_URL: %s\n", url); | ||
| 378 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_URL, url), "CURLOPT_URL"); | ||
| 379 | |||
| 380 | /* extract proxy information for legacy proxy https requests */ | ||
| 381 | if (!strcmp(http_method, "CONNECT") || strstr(server_url, "http") == server_url) { | ||
| 382 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_PROXY, server_address), "CURLOPT_PROXY"); | ||
| 383 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_PROXYPORT, (long)server_port), "CURLOPT_PROXYPORT"); | ||
| 384 | if (verbose>=2) | ||
| 385 | printf ("* curl CURLOPT_PROXY: %s:%d\n", server_address, server_port); | ||
| 386 | http_method = "GET"; | ||
| 387 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_URL, server_url), "CURLOPT_URL"); | ||
| 388 | } | ||
| 389 | |||
| 390 | /* set HTTP method */ | ||
| 391 | if (http_method) { | ||
| 392 | if (!strcmp(http_method, "POST")) | ||
| 393 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_POST, 1), "CURLOPT_POST"); | ||
| 394 | else if (!strcmp(http_method, "PUT")) | ||
| 395 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_UPLOAD, 1), "CURLOPT_UPLOAD"); | ||
| 396 | else | ||
| 397 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_CUSTOMREQUEST, http_method), "CURLOPT_CUSTOMREQUEST"); | ||
| 398 | } | ||
| 399 | |||
| 400 | /* check if Host header is explicitly set in options */ | ||
| 401 | if (http_opt_headers_count) { | ||
| 402 | for (i = 0; i < http_opt_headers_count ; i++) { | ||
| 403 | if (strncmp(http_opt_headers[i], "Host:", 5) == 0) { | ||
| 404 | force_host_header = http_opt_headers[i]; | ||
| 405 | } | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | /* set hostname (virtual hosts), not needed if CURLOPT_CONNECT_TO is used, but left in anyway */ | ||
| 410 | if(host_name != NULL && force_host_header == NULL) { | ||
| 411 | if((virtual_port != HTTP_PORT && !use_ssl) || (virtual_port != HTTPS_PORT && use_ssl)) { | ||
| 412 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Host: %s:%d", host_name, virtual_port); | ||
| 413 | } else { | ||
| 414 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Host: %s", host_name); | ||
| 415 | } | ||
| 416 | header_list = curl_slist_append (header_list, http_header); | ||
| 417 | } | ||
| 418 | |||
| 419 | /* always close connection, be nice to servers */ | ||
| 420 | snprintf (http_header, DEFAULT_BUFFER_SIZE, "Connection: close"); | ||
| 421 | header_list = curl_slist_append (header_list, http_header); | ||
| 422 | |||
| 423 | /* attach additional headers supplied by the user */ | ||
| 424 | /* optionally send any other header tag */ | ||
| 425 | if (http_opt_headers_count) { | ||
| 426 | for (i = 0; i < http_opt_headers_count ; i++) { | ||
| 427 | header_list = curl_slist_append (header_list, http_opt_headers[i]); | ||
| 428 | } | ||
| 429 | /* This cannot be free'd here because a redirection will then try to access this and segfault */ | ||
| 430 | /* Covered in a testcase in tests/check_http.t */ | ||
| 431 | /* free(http_opt_headers); */ | ||
| 432 | } | ||
| 433 | |||
| 434 | /* set HTTP headers */ | ||
| 435 | handle_curl_option_return_code (curl_easy_setopt( curl, CURLOPT_HTTPHEADER, header_list ), "CURLOPT_HTTPHEADER"); | ||
| 436 | |||
| 437 | #ifdef LIBCURL_FEATURE_SSL | ||
| 438 | |||
| 439 | /* set SSL version, warn about unsecure or unsupported versions */ | ||
| 440 | if (use_ssl) { | ||
| 441 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_SSLVERSION, ssl_version), "CURLOPT_SSLVERSION"); | ||
| 442 | } | ||
| 443 | |||
| 444 | /* client certificate and key to present to server (SSL) */ | ||
| 445 | if (client_cert) | ||
| 446 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_SSLCERT, client_cert), "CURLOPT_SSLCERT"); | ||
| 447 | if (client_privkey) | ||
| 448 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_SSLKEY, client_privkey), "CURLOPT_SSLKEY"); | ||
| 449 | if (ca_cert) { | ||
| 450 | /* per default if we have a CA verify both the peer and the | ||
| 451 | * hostname in the certificate, can be switched off later */ | ||
| 452 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_CAINFO, ca_cert), "CURLOPT_CAINFO"); | ||
| 453 | handle_curl_option_return_code (curl_easy_setopt( curl, CURLOPT_SSL_VERIFYPEER, 1), "CURLOPT_SSL_VERIFYPEER"); | ||
| 454 | handle_curl_option_return_code (curl_easy_setopt( curl, CURLOPT_SSL_VERIFYHOST, 2), "CURLOPT_SSL_VERIFYHOST"); | ||
| 455 | } else { | ||
| 456 | /* backward-compatible behaviour, be tolerant in checks | ||
| 457 | * TODO: depending on more options have aspects we want | ||
| 458 | * to be less tolerant about ssl verfications | ||
| 459 | */ | ||
| 460 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0), "CURLOPT_SSL_VERIFYPEER"); | ||
| 461 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0), "CURLOPT_SSL_VERIFYHOST"); | ||
| 462 | } | ||
| 463 | |||
| 464 | /* detect SSL library used by libcurl */ | ||
| 465 | ssl_library = curlhelp_get_ssl_library (curl); | ||
| 466 | |||
| 467 | /* try hard to get a stack of certificates to verify against */ | ||
| 468 | if (check_cert) { | ||
| 469 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) | ||
| 470 | /* inform curl to report back certificates */ | ||
| 471 | switch (ssl_library) { | ||
| 472 | case CURLHELP_SSL_LIBRARY_OPENSSL: | ||
| 473 | case CURLHELP_SSL_LIBRARY_LIBRESSL: | ||
| 474 | /* set callback to extract certificate with OpenSSL context function (works with | ||
| 475 | * OpenSSL-style libraries only!) */ | ||
| 476 | #ifdef USE_OPENSSL | ||
| 477 | /* libcurl and monitoring plugins built with OpenSSL, good */ | ||
| 478 | handle_curl_option_return_code (curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun), "CURLOPT_SSL_CTX_FUNCTION"); | ||
| 479 | is_openssl_callback = TRUE; | ||
| 480 | #else /* USE_OPENSSL */ | ||
| 481 | #endif /* USE_OPENSSL */ | ||
| 482 | /* libcurl is built with OpenSSL, monitoring plugins, so falling | ||
| 483 | * back to manually extracting certificate information */ | ||
| 484 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_CERTINFO, 1L), "CURLOPT_CERTINFO"); | ||
| 485 | break; | ||
| 486 | |||
| 487 | case CURLHELP_SSL_LIBRARY_NSS: | ||
| 488 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | ||
| 489 | /* NSS: support for CERTINFO is implemented since 7.34.0 */ | ||
| 490 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_CERTINFO, 1L), "CURLOPT_CERTINFO"); | ||
| 491 | #else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
| 492 | die (STATE_CRITICAL, "HTTP CRITICAL - Cannot retrieve certificates (libcurl linked with SSL library '%s' is too old)\n", curlhelp_get_ssl_library_string (ssl_library)); | ||
| 493 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
| 494 | break; | ||
| 495 | |||
| 496 | case CURLHELP_SSL_LIBRARY_GNUTLS: | ||
| 497 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 42, 0) | ||
| 498 | /* GnuTLS: support for CERTINFO is implemented since 7.42.0 */ | ||
| 499 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_CERTINFO, 1L), "CURLOPT_CERTINFO"); | ||
| 500 | #else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 42, 0) */ | ||
| 501 | die (STATE_CRITICAL, "HTTP CRITICAL - Cannot retrieve certificates (libcurl linked with SSL library '%s' is too old)\n", curlhelp_get_ssl_library_string (ssl_library)); | ||
| 502 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 42, 0) */ | ||
| 503 | break; | ||
| 504 | |||
| 505 | case CURLHELP_SSL_LIBRARY_UNKNOWN: | ||
| 506 | default: | ||
| 507 | die (STATE_CRITICAL, "HTTP CRITICAL - Cannot retrieve certificates (unknown SSL library '%s', must implement first)\n", curlhelp_get_ssl_library_string (ssl_library)); | ||
| 508 | break; | ||
| 509 | } | ||
| 510 | #else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) */ | ||
| 511 | /* old libcurl, our only hope is OpenSSL, otherwise we are out of luck */ | ||
| 512 | if (ssl_library == CURLHELP_SSL_LIBRARY_OPENSSL || ssl_library == CURLHELP_SSL_LIBRARY_LIBRESSL) | ||
| 513 | handle_curl_option_return_code (curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun), "CURLOPT_SSL_CTX_FUNCTION"); | ||
| 514 | else | ||
| 515 | die (STATE_CRITICAL, "HTTP CRITICAL - Cannot retrieve certificates (no CURLOPT_SSL_CTX_FUNCTION, no OpenSSL library or libcurl too old and has no CURLOPT_CERTINFO)\n"); | ||
| 516 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) */ | ||
| 517 | } | ||
| 518 | |||
| 519 | #endif /* LIBCURL_FEATURE_SSL */ | ||
| 520 | |||
| 521 | /* set default or user-given user agent identification */ | ||
| 522 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_USERAGENT, user_agent), "CURLOPT_USERAGENT"); | ||
| 523 | |||
| 524 | /* proxy-authentication */ | ||
| 525 | if (strcmp(proxy_auth, "")) | ||
| 526 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_PROXYUSERPWD, proxy_auth), "CURLOPT_PROXYUSERPWD"); | ||
| 527 | |||
| 528 | /* authentication */ | ||
| 529 | if (strcmp(user_auth, "")) | ||
| 530 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_USERPWD, user_auth), "CURLOPT_USERPWD"); | ||
| 531 | |||
| 532 | /* TODO: parameter auth method, bitfield of following methods: | ||
| 533 | * CURLAUTH_BASIC (default) | ||
| 534 | * CURLAUTH_DIGEST | ||
| 535 | * CURLAUTH_DIGEST_IE | ||
| 536 | * CURLAUTH_NEGOTIATE | ||
| 537 | * CURLAUTH_NTLM | ||
| 538 | * CURLAUTH_NTLM_WB | ||
| 539 | * | ||
| 540 | * convenience tokens for typical sets of methods: | ||
| 541 | * CURLAUTH_ANYSAFE: most secure, without BASIC | ||
| 542 | * or CURLAUTH_ANY: most secure, even BASIC if necessary | ||
| 543 | * | ||
| 544 | * handle_curl_option_return_code (curl_easy_setopt( curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST ), "CURLOPT_HTTPAUTH"); | ||
| 545 | */ | ||
| 546 | |||
| 547 | /* handle redirections */ | ||
| 548 | if (onredirect == STATE_DEPENDENT) { | ||
| 549 | if( followmethod == FOLLOW_LIBCURL ) { | ||
| 550 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1), "CURLOPT_FOLLOWLOCATION"); | ||
| 551 | |||
| 552 | /* default -1 is infinite, not good, could lead to zombie plugins! | ||
| 553 | Setting it to one bigger than maximal limit to handle errors nicely below | ||
| 554 | */ | ||
| 555 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_MAXREDIRS, max_depth+1), "CURLOPT_MAXREDIRS"); | ||
| 556 | |||
| 557 | /* for now allow only http and https (we are a http(s) check plugin in the end) */ | ||
| 558 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 4) | ||
| 559 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS), "CURLOPT_REDIRECT_PROTOCOLS"); | ||
| 560 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 4) */ | ||
| 561 | |||
| 562 | /* TODO: handle the following aspects of redirection, make them | ||
| 563 | * command line options too later: | ||
| 564 | CURLOPT_POSTREDIR: method switch | ||
| 565 | CURLINFO_REDIRECT_URL: custom redirect option | ||
| 566 | CURLOPT_REDIRECT_PROTOCOLS: allow people to step outside safe protocols | ||
| 567 | CURLINFO_REDIRECT_COUNT: get the number of redirects, print it, maybe a range option here is nice like for expected page size? | ||
| 568 | */ | ||
| 569 | } else { | ||
| 570 | /* old style redirection is handled below */ | ||
| 571 | } | ||
| 572 | } | ||
| 573 | |||
| 574 | /* no-body */ | ||
| 575 | if (no_body) | ||
| 576 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_NOBODY, 1), "CURLOPT_NOBODY"); | ||
| 577 | |||
| 578 | /* IPv4 or IPv6 forced DNS resolution */ | ||
| 579 | if (address_family == AF_UNSPEC) | ||
| 580 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER), "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_WHATEVER)"); | ||
| 581 | else if (address_family == AF_INET) | ||
| 582 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4), "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V4)"); | ||
| 583 | #if defined (USE_IPV6) && defined (LIBCURL_FEATURE_IPV6) | ||
| 584 | else if (address_family == AF_INET6) | ||
| 585 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6), "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V6)"); | ||
| 586 | #endif | ||
| 587 | |||
| 588 | /* either send http POST data (any data, not only POST)*/ | ||
| 589 | if (!strcmp(http_method, "POST") ||!strcmp(http_method, "PUT")) { | ||
| 590 | /* set content of payload for POST and PUT */ | ||
| 591 | if (http_content_type) { | ||
| 592 | snprintf (http_header, DEFAULT_BUFFER_SIZE, "Content-Type: %s", http_content_type); | ||
| 593 | header_list = curl_slist_append (header_list, http_header); | ||
| 594 | } | ||
| 595 | /* NULL indicates "HTTP Continue" in libcurl, provide an empty string | ||
| 596 | * in case of no POST/PUT data */ | ||
| 597 | if (!http_post_data) | ||
| 598 | http_post_data = ""; | ||
| 599 | if (!strcmp(http_method, "POST")) { | ||
| 600 | /* POST method, set payload with CURLOPT_POSTFIELDS */ | ||
| 601 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_POSTFIELDS, http_post_data), "CURLOPT_POSTFIELDS"); | ||
| 602 | } else if (!strcmp(http_method, "PUT")) { | ||
| 603 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_READFUNCTION, (curl_read_callback)curlhelp_buffer_read_callback), "CURLOPT_READFUNCTION"); | ||
| 604 | curlhelp_initreadbuffer (&put_buf, http_post_data, strlen (http_post_data)); | ||
| 605 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_READDATA, (void *)&put_buf), "CURLOPT_READDATA"); | ||
| 606 | handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_INFILESIZE, (curl_off_t)strlen (http_post_data)), "CURLOPT_INFILESIZE"); | ||
| 607 | } | ||
| 608 | } | ||
| 609 | |||
| 610 | /* do the request */ | ||
| 611 | res = curl_easy_perform(curl); | ||
| 612 | |||
| 613 | if (verbose>=2 && http_post_data) | ||
| 614 | printf ("**** REQUEST CONTENT ****\n%s\n", http_post_data); | ||
| 615 | |||
| 616 | /* free header and server IP resolve lists, we don't need it anymore */ | ||
| 617 | curl_slist_free_all (header_list); header_list = NULL; | ||
| 618 | curl_slist_free_all (server_ips); server_ips = NULL; | ||
| 619 | |||
| 620 | /* Curl errors, result in critical Nagios state */ | ||
| 621 | if (res != CURLE_OK) { | ||
| 622 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("Invalid HTTP response received from host on port %d: cURL returned %d - %s"), | ||
| 623 | server_port, res, curl_easy_strerror(res)); | ||
| 624 | die (STATE_CRITICAL, "HTTP CRITICAL - %s\n", msg); | ||
| 625 | } | ||
| 626 | |||
| 627 | /* certificate checks */ | ||
| 628 | #ifdef LIBCURL_FEATURE_SSL | ||
| 629 | if (use_ssl == TRUE) { | ||
| 630 | if (check_cert == TRUE) { | ||
| 631 | if (is_openssl_callback) { | ||
| 632 | #ifdef USE_OPENSSL | ||
| 633 | /* check certificate with OpenSSL functions, curl has been built against OpenSSL | ||
| 634 | * and we actually have OpenSSL in the monitoring tools | ||
| 635 | */ | ||
| 636 | result = np_net_ssl_check_certificate(cert, days_till_exp_warn, days_till_exp_crit); | ||
| 637 | return result; | ||
| 638 | #else /* USE_OPENSSL */ | ||
| 639 | die (STATE_CRITICAL, "HTTP CRITICAL - Cannot retrieve certificates - OpenSSL callback used and not linked against OpenSSL\n"); | ||
| 640 | #endif /* USE_OPENSSL */ | ||
| 641 | } else { | ||
| 642 | int i; | ||
| 643 | struct curl_slist *slist; | ||
| 644 | |||
| 645 | cert_ptr.to_info = NULL; | ||
| 646 | res = curl_easy_getinfo (curl, CURLINFO_CERTINFO, &cert_ptr.to_info); | ||
| 647 | if (!res && cert_ptr.to_info) { | ||
| 648 | #ifdef USE_OPENSSL | ||
| 649 | /* We have no OpenSSL in libcurl, but we can use OpenSSL for X509 cert parsing | ||
| 650 | * We only check the first certificate and assume it's the one of the server | ||
| 651 | */ | ||
| 652 | const char* raw_cert = NULL; | ||
| 653 | for (i = 0; i < cert_ptr.to_certinfo->num_of_certs; i++) { | ||
| 654 | for (slist = cert_ptr.to_certinfo->certinfo[i]; slist; slist = slist->next) { | ||
| 655 | if (verbose >= 2) | ||
| 656 | printf ("%d ** %s\n", i, slist->data); | ||
| 657 | if (strncmp (slist->data, "Cert:", 5) == 0) { | ||
| 658 | raw_cert = &slist->data[5]; | ||
| 659 | goto GOT_FIRST_CERT; | ||
| 660 | } | ||
| 661 | } | ||
| 662 | } | ||
| 663 | GOT_FIRST_CERT: | ||
| 664 | if (!raw_cert) { | ||
| 665 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("Cannot retrieve certificates from CERTINFO information - certificate data was empty")); | ||
| 666 | die (STATE_CRITICAL, "HTTP CRITICAL - %s\n", msg); | ||
| 667 | } | ||
| 668 | BIO* cert_BIO = BIO_new (BIO_s_mem()); | ||
| 669 | BIO_write (cert_BIO, raw_cert, strlen(raw_cert)); | ||
| 670 | cert = PEM_read_bio_X509 (cert_BIO, NULL, NULL, NULL); | ||
| 671 | if (!cert) { | ||
| 672 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("Cannot read certificate from CERTINFO information - BIO error")); | ||
| 673 | die (STATE_CRITICAL, "HTTP CRITICAL - %s\n", msg); | ||
| 674 | } | ||
| 675 | BIO_free (cert_BIO); | ||
| 676 | result = np_net_ssl_check_certificate(cert, days_till_exp_warn, days_till_exp_crit); | ||
| 677 | return result; | ||
| 678 | #else /* USE_OPENSSL */ | ||
| 679 | /* We assume we don't have OpenSSL and np_net_ssl_check_certificate at our disposal, | ||
| 680 | * so we use the libcurl CURLINFO data | ||
| 681 | */ | ||
| 682 | result = net_noopenssl_check_certificate(&cert_ptr, days_till_exp_warn, days_till_exp_crit); | ||
| 683 | return result; | ||
| 684 | #endif /* USE_OPENSSL */ | ||
| 685 | } else { | ||
| 686 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("Cannot retrieve certificates - cURL returned %d - %s"), | ||
| 687 | res, curl_easy_strerror(res)); | ||
| 688 | die (STATE_CRITICAL, "HTTP CRITICAL - %s\n", msg); | ||
| 689 | } | ||
| 690 | } | ||
| 691 | } | ||
| 692 | } | ||
| 693 | #endif /* LIBCURL_FEATURE_SSL */ | ||
| 694 | |||
| 695 | /* we got the data and we executed the request in a given time, so we can append | ||
| 696 | * performance data to the answer always | ||
| 697 | */ | ||
| 698 | handle_curl_option_return_code (curl_easy_getinfo (curl, CURLINFO_TOTAL_TIME, &total_time), "CURLINFO_TOTAL_TIME"); | ||
| 699 | page_len = get_content_length(&header_buf, &body_buf); | ||
| 700 | if(show_extended_perfdata) { | ||
| 701 | handle_curl_option_return_code (curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &time_connect), "CURLINFO_CONNECT_TIME"); | ||
| 702 | handle_curl_option_return_code (curl_easy_getinfo(curl, CURLINFO_APPCONNECT_TIME, &time_appconnect), "CURLINFO_APPCONNECT_TIME"); | ||
| 703 | handle_curl_option_return_code (curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &time_headers), "CURLINFO_PRETRANSFER_TIME"); | ||
| 704 | handle_curl_option_return_code (curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME, &time_firstbyte), "CURLINFO_STARTTRANSFER_TIME"); | ||
| 705 | snprintf(perfstring, DEFAULT_BUFFER_SIZE, "%s %s %s %s %s %s %s", | ||
| 706 | perfd_time(total_time), | ||
| 707 | perfd_size(page_len), | ||
| 708 | perfd_time_connect(time_connect), | ||
| 709 | use_ssl == TRUE ? perfd_time_ssl (time_appconnect-time_connect) : "", | ||
| 710 | perfd_time_headers(time_headers - time_appconnect), | ||
| 711 | perfd_time_firstbyte(time_firstbyte - time_headers), | ||
| 712 | perfd_time_transfer(total_time-time_firstbyte) | ||
| 713 | ); | ||
| 714 | } else { | ||
| 715 | snprintf(perfstring, DEFAULT_BUFFER_SIZE, "%s %s", | ||
| 716 | perfd_time(total_time), | ||
| 717 | perfd_size(page_len) | ||
| 718 | ); | ||
| 719 | } | ||
| 720 | |||
| 721 | /* return a CRITICAL status if we couldn't read any data */ | ||
| 722 | if (strlen(header_buf.buf) == 0 && strlen(body_buf.buf) == 0) | ||
| 723 | die (STATE_CRITICAL, _("HTTP CRITICAL - No header received from host\n")); | ||
| 724 | |||
| 725 | /* get status line of answer, check sanity of HTTP code */ | ||
| 726 | if (curlhelp_parse_statusline (header_buf.buf, &status_line) < 0) { | ||
| 727 | snprintf (msg, DEFAULT_BUFFER_SIZE, "Unparsable status line in %.3g seconds response time|%s\n", | ||
| 728 | total_time, perfstring); | ||
| 729 | die (STATE_CRITICAL, "HTTP CRITICAL HTTP/1.x %ld unknown - %s", code, msg); | ||
| 730 | } | ||
| 731 | |||
| 732 | /* get result code from cURL */ | ||
| 733 | handle_curl_option_return_code (curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &code), "CURLINFO_RESPONSE_CODE"); | ||
| 734 | if (verbose>=2) | ||
| 735 | printf ("* curl CURLINFO_RESPONSE_CODE is %ld\n", code); | ||
| 736 | |||
| 737 | /* print status line, header, body if verbose */ | ||
| 738 | if (verbose >= 2) { | ||
| 739 | printf ("**** HEADER ****\n%s\n**** CONTENT ****\n%s\n", header_buf.buf, | ||
| 740 | (no_body ? " [[ skipped ]]" : body_buf.buf)); | ||
| 741 | } | ||
| 742 | |||
| 743 | /* make sure the status line matches the response we are looking for */ | ||
| 744 | if (!expected_statuscode(status_line.first_line, server_expect)) { | ||
| 745 | if (server_port == HTTP_PORT) | ||
| 746 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Invalid HTTP response received from host: %s\n"), status_line.first_line); | ||
| 747 | else | ||
| 748 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Invalid HTTP response received from host on port %d: %s\n"), server_port, status_line.first_line); | ||
| 749 | die (STATE_CRITICAL, "HTTP CRITICAL - %s", msg); | ||
| 750 | } | ||
| 751 | |||
| 752 | if( server_expect_yn ) { | ||
| 753 | snprintf(msg, DEFAULT_BUFFER_SIZE, _("Status line output matched \"%s\" - "), server_expect); | ||
| 754 | if (verbose) | ||
| 755 | printf ("%s\n",msg); | ||
| 756 | result = STATE_OK; | ||
| 757 | } | ||
| 758 | else { | ||
| 759 | /* illegal return codes result in a critical state */ | ||
| 760 | if (code >= 600 || code < 100) { | ||
| 761 | die (STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status (%d, %.40s)\n"), status_line.http_code, status_line.msg); | ||
| 762 | /* server errors result in a critical state */ | ||
| 763 | } else if (code >= 500) { | ||
| 764 | result = STATE_CRITICAL; | ||
| 765 | /* client errors result in a warning state */ | ||
| 766 | } else if (code >= 400) { | ||
| 767 | result = STATE_WARNING; | ||
| 768 | /* check redirected page if specified */ | ||
| 769 | } else if (code >= 300) { | ||
| 770 | if (onredirect == STATE_DEPENDENT) { | ||
| 771 | if( followmethod == FOLLOW_LIBCURL ) { | ||
| 772 | code = status_line.http_code; | ||
| 773 | } else { | ||
| 774 | /* old check_http style redirection, if we come | ||
| 775 | * back here, we are in the same status as with | ||
| 776 | * the libcurl method | ||
| 777 | */ | ||
| 778 | redir (&header_buf); | ||
| 779 | } | ||
| 780 | } else { | ||
| 781 | /* this is a specific code in the command line to | ||
| 782 | * be returned when a redirection is encoutered | ||
| 783 | */ | ||
| 784 | } | ||
| 785 | result = max_state_alt (onredirect, result); | ||
| 786 | /* all other codes are considered ok */ | ||
| 787 | } else { | ||
| 788 | result = STATE_OK; | ||
| 789 | } | ||
| 790 | } | ||
| 791 | |||
| 792 | /* libcurl redirection internally, handle error states here */ | ||
| 793 | if( followmethod == FOLLOW_LIBCURL ) { | ||
| 794 | handle_curl_option_return_code (curl_easy_getinfo (curl, CURLINFO_REDIRECT_COUNT, &redir_depth), "CURLINFO_REDIRECT_COUNT"); | ||
| 795 | if (verbose >= 2) | ||
| 796 | printf(_("* curl LIBINFO_REDIRECT_COUNT is %d\n"), redir_depth); | ||
| 797 | if (redir_depth > max_depth) { | ||
| 798 | snprintf (msg, DEFAULT_BUFFER_SIZE, "maximum redirection depth %d exceeded in libcurl", | ||
| 799 | max_depth); | ||
| 800 | die (STATE_WARNING, "HTTP WARNING - %s", msg); | ||
| 801 | } | ||
| 802 | } | ||
| 803 | |||
| 804 | /* check status codes, set exit status accordingly */ | ||
| 805 | if( status_line.http_code != code ) { | ||
| 806 | die (STATE_CRITICAL, _("HTTP CRITICAL HTTP/%d.%d %d %s - different HTTP codes (cUrl has %ld)\n"), | ||
| 807 | status_line.http_major, status_line.http_minor, | ||
| 808 | status_line.http_code, status_line.msg, code); | ||
| 809 | } | ||
| 810 | |||
| 811 | if (maximum_age >= 0) { | ||
| 812 | result = max_state_alt(check_document_dates(&header_buf, &msg), result); | ||
| 813 | } | ||
| 814 | |||
| 815 | /* Page and Header content checks go here */ | ||
| 816 | |||
| 817 | if (strlen (header_expect)) { | ||
| 818 | if (!strstr (header_buf.buf, header_expect)) { | ||
| 819 | strncpy(&output_header_search[0],header_expect,sizeof(output_header_search)); | ||
| 820 | if(output_header_search[sizeof(output_header_search)-1]!='\0') { | ||
| 821 | bcopy("...",&output_header_search[sizeof(output_header_search)-4],4); | ||
| 822 | } | ||
| 823 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("%sheader '%s' not found on '%s://%s:%d%s', "), msg, output_header_search, use_ssl ? "https" : "http", host_name ? host_name : server_address, server_port, server_url); | ||
| 824 | result = STATE_CRITICAL; | ||
| 825 | } | ||
| 826 | } | ||
| 827 | |||
| 828 | if (strlen (string_expect)) { | ||
| 829 | if (!strstr (body_buf.buf, string_expect)) { | ||
| 830 | strncpy(&output_string_search[0],string_expect,sizeof(output_string_search)); | ||
| 831 | if(output_string_search[sizeof(output_string_search)-1]!='\0') { | ||
| 832 | bcopy("...",&output_string_search[sizeof(output_string_search)-4],4); | ||
| 833 | } | ||
| 834 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("%sstring '%s' not found on '%s://%s:%d%s', "), msg, output_string_search, use_ssl ? "https" : "http", host_name ? host_name : server_address, server_port, server_url); | ||
| 835 | result = STATE_CRITICAL; | ||
| 836 | } | ||
| 837 | } | ||
| 838 | |||
| 839 | if (strlen (regexp)) { | ||
| 840 | errcode = regexec (&preg, body_buf.buf, REGS, pmatch, 0); | ||
| 841 | if ((errcode == 0 && invert_regex == 0) || (errcode == REG_NOMATCH && invert_regex == 1)) { | ||
| 842 | /* OK - No-op to avoid changing the logic around it */ | ||
| 843 | result = max_state_alt(STATE_OK, result); | ||
| 844 | } | ||
| 845 | else if ((errcode == REG_NOMATCH && invert_regex == 0) || (errcode == 0 && invert_regex == 1)) { | ||
| 846 | if (invert_regex == 0) | ||
| 847 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("%spattern not found, "), msg); | ||
| 848 | else | ||
| 849 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("%spattern found, "), msg); | ||
| 850 | result = STATE_CRITICAL; | ||
| 851 | } | ||
| 852 | else { | ||
| 853 | regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); | ||
| 854 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("%sExecute Error: %s, "), msg, errbuf); | ||
| 855 | result = STATE_UNKNOWN; | ||
| 856 | } | ||
| 857 | } | ||
| 858 | |||
| 859 | /* make sure the page is of an appropriate size */ | ||
| 860 | if ((max_page_len > 0) && (page_len > max_page_len)) { | ||
| 861 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("%spage size %d too large, "), msg, page_len); | ||
| 862 | result = max_state_alt(STATE_WARNING, result); | ||
| 863 | } else if ((min_page_len > 0) && (page_len < min_page_len)) { | ||
| 864 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("%spage size %d too small, "), msg, page_len); | ||
| 865 | result = max_state_alt(STATE_WARNING, result); | ||
| 866 | } | ||
| 867 | |||
| 868 | /* -w, -c: check warning and critical level */ | ||
| 869 | result = max_state_alt(get_status(total_time, thlds), result); | ||
| 870 | |||
| 871 | /* Cut-off trailing characters */ | ||
| 872 | if(msg[strlen(msg)-2] == ',') | ||
| 873 | msg[strlen(msg)-2] = '\0'; | ||
| 874 | else | ||
| 875 | msg[strlen(msg)-3] = '\0'; | ||
| 876 | |||
| 877 | /* TODO: separate _() msg and status code: die (result, "HTTP %s: %s\n", state_text(result), msg); */ | ||
| 878 | die (result, "HTTP %s: HTTP/%d.%d %d %s%s%s - %d bytes in %.3f second response time %s|%s\n", | ||
| 879 | state_text(result), status_line.http_major, status_line.http_minor, | ||
| 880 | status_line.http_code, status_line.msg, | ||
| 881 | strlen(msg) > 0 ? " - " : "", | ||
| 882 | msg, page_len, total_time, | ||
| 883 | (display_html ? "</A>" : ""), | ||
| 884 | perfstring); | ||
| 885 | |||
| 886 | /* proper cleanup after die? */ | ||
| 887 | curlhelp_free_statusline(&status_line); | ||
| 888 | curl_easy_cleanup (curl); | ||
| 889 | curl_global_cleanup (); | ||
| 890 | curlhelp_freewritebuffer (&body_buf); | ||
| 891 | curlhelp_freewritebuffer (&header_buf); | ||
| 892 | if (!strcmp (http_method, "PUT")) { | ||
| 893 | curlhelp_freereadbuffer (&put_buf); | ||
| 894 | } | ||
| 895 | |||
| 896 | return result; | ||
| 897 | } | ||
| 898 | |||
| 899 | int | ||
| 900 | uri_strcmp (const UriTextRangeA range, const char* s) | ||
| 901 | { | ||
| 902 | if (!range.first) return -1; | ||
| 903 | if (range.afterLast - range.first < strlen (s)) return -1; | ||
| 904 | return strncmp (s, range.first, min( range.afterLast - range.first, strlen (s))); | ||
| 905 | } | ||
| 906 | |||
| 907 | char* | ||
| 908 | uri_string (const UriTextRangeA range, char* buf, size_t buflen) | ||
| 909 | { | ||
| 910 | if (!range.first) return "(null)"; | ||
| 911 | strncpy (buf, range.first, max (buflen, range.afterLast - range.first)); | ||
| 912 | buf[max (buflen, range.afterLast - range.first)] = '\0'; | ||
| 913 | buf[range.afterLast - range.first] = '\0'; | ||
| 914 | return buf; | ||
| 915 | } | ||
| 916 | |||
| 917 | void | ||
| 918 | redir (curlhelp_write_curlbuf* header_buf) | ||
| 919 | { | ||
| 920 | char *location = NULL; | ||
| 921 | curlhelp_statusline status_line; | ||
| 922 | struct phr_header headers[255]; | ||
| 923 | size_t nof_headers = 255; | ||
| 924 | size_t msglen; | ||
| 925 | char buf[DEFAULT_BUFFER_SIZE]; | ||
| 926 | char ipstr[INET_ADDR_MAX_SIZE]; | ||
| 927 | int new_port; | ||
| 928 | char *new_host; | ||
| 929 | char *new_url; | ||
| 930 | |||
| 931 | int res = phr_parse_response (header_buf->buf, header_buf->buflen, | ||
| 932 | &status_line.http_minor, &status_line.http_code, &status_line.msg, &msglen, | ||
| 933 | headers, &nof_headers, 0); | ||
| 934 | |||
| 935 | location = get_header_value (headers, nof_headers, "location"); | ||
| 936 | |||
| 937 | if (verbose >= 2) | ||
| 938 | printf(_("* Seen redirect location %s\n"), location); | ||
| 939 | |||
| 940 | if (++redir_depth > max_depth) | ||
| 941 | die (STATE_WARNING, | ||
| 942 | _("HTTP WARNING - maximum redirection depth %d exceeded - %s%s\n"), | ||
| 943 | max_depth, location, (display_html ? "</A>" : "")); | ||
| 944 | |||
| 945 | UriParserStateA state; | ||
| 946 | UriUriA uri; | ||
| 947 | state.uri = &uri; | ||
| 948 | if (uriParseUriA (&state, location) != URI_SUCCESS) { | ||
| 949 | if (state.errorCode == URI_ERROR_SYNTAX) { | ||
| 950 | die (STATE_UNKNOWN, | ||
| 951 | _("HTTP UNKNOWN - Could not parse redirect location '%s'%s\n"), | ||
| 952 | location, (display_html ? "</A>" : "")); | ||
| 953 | } else if (state.errorCode == URI_ERROR_MALLOC) { | ||
| 954 | die (STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate URL\n")); | ||
| 955 | } | ||
| 956 | } | ||
| 957 | |||
| 958 | if (verbose >= 2) { | ||
| 959 | printf (_("** scheme: %s\n"), | ||
| 960 | uri_string (uri.scheme, buf, DEFAULT_BUFFER_SIZE)); | ||
| 961 | printf (_("** host: %s\n"), | ||
| 962 | uri_string (uri.hostText, buf, DEFAULT_BUFFER_SIZE)); | ||
| 963 | printf (_("** port: %s\n"), | ||
| 964 | uri_string (uri.portText, buf, DEFAULT_BUFFER_SIZE)); | ||
| 965 | if (uri.hostData.ip4) { | ||
| 966 | inet_ntop (AF_INET, uri.hostData.ip4->data, ipstr, sizeof (ipstr)); | ||
| 967 | printf (_("** IPv4: %s\n"), ipstr); | ||
| 968 | } | ||
| 969 | if (uri.hostData.ip6) { | ||
| 970 | inet_ntop (AF_INET, uri.hostData.ip6->data, ipstr, sizeof (ipstr)); | ||
| 971 | printf (_("** IPv6: %s\n"), ipstr); | ||
| 972 | } | ||
| 973 | if (uri.pathHead) { | ||
| 974 | printf (_("** path: ")); | ||
| 975 | const UriPathSegmentA* p = uri.pathHead; | ||
| 976 | for (; p; p = p->next) { | ||
| 977 | printf ("/%s", uri_string (p->text, buf, DEFAULT_BUFFER_SIZE)); | ||
| 978 | } | ||
| 979 | puts (""); | ||
| 980 | } | ||
| 981 | if (uri.query.first) { | ||
| 982 | printf (_("** query: %s\n"), | ||
| 983 | uri_string (uri.query, buf, DEFAULT_BUFFER_SIZE)); | ||
| 984 | } | ||
| 985 | if (uri.fragment.first) { | ||
| 986 | printf (_("** fragment: %s\n"), | ||
| 987 | uri_string (uri.fragment, buf, DEFAULT_BUFFER_SIZE)); | ||
| 988 | } | ||
| 989 | } | ||
| 990 | |||
| 991 | use_ssl = !uri_strcmp (uri.scheme, "https"); | ||
| 992 | |||
| 993 | /* we do a sloppy test here only, because uriparser would have failed | ||
| 994 | * above, if the port would be invalid, we just check for MAX_PORT | ||
| 995 | */ | ||
| 996 | if (uri.portText.first) { | ||
| 997 | new_port = atoi (uri_string (uri.portText, buf, DEFAULT_BUFFER_SIZE)); | ||
| 998 | } else { | ||
| 999 | new_port = HTTP_PORT; | ||
| 1000 | if (use_ssl) | ||
| 1001 | new_port = HTTPS_PORT; | ||
| 1002 | } | ||
| 1003 | if (new_port > MAX_PORT) | ||
| 1004 | die (STATE_UNKNOWN, | ||
| 1005 | _("HTTP UNKNOWN - Redirection to port above %d - %s%s\n"), | ||
| 1006 | MAX_PORT, location, display_html ? "</A>" : ""); | ||
| 1007 | |||
| 1008 | /* by RFC 7231 relative URLs in Location should be taken relative to | ||
| 1009 | * the original URL, so wy try to form a new absolute URL here | ||
| 1010 | */ | ||
| 1011 | if (!uri.scheme.first && !uri.hostText.first) { | ||
| 1012 | new_host = strdup (host_name ? host_name : server_address); | ||
| 1013 | } else { | ||
| 1014 | new_host = strdup (uri_string (uri.hostText, buf, DEFAULT_BUFFER_SIZE)); | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | /* compose new path */ | ||
| 1018 | /* TODO: handle fragments and query part of URL */ | ||
| 1019 | new_url = (char *)calloc( 1, DEFAULT_BUFFER_SIZE); | ||
| 1020 | if (uri.pathHead) { | ||
| 1021 | const UriPathSegmentA* p = uri.pathHead; | ||
| 1022 | for (; p; p = p->next) { | ||
| 1023 | strncat (new_url, "/", DEFAULT_BUFFER_SIZE); | ||
| 1024 | strncat (new_url, uri_string (p->text, buf, DEFAULT_BUFFER_SIZE), DEFAULT_BUFFER_SIZE); | ||
| 1025 | } | ||
| 1026 | } | ||
| 1027 | |||
| 1028 | if (server_port==new_port && | ||
| 1029 | !strncmp(server_address, new_host, MAX_IPV4_HOSTLENGTH) && | ||
| 1030 | (host_name && !strncmp(host_name, new_host, MAX_IPV4_HOSTLENGTH)) && | ||
| 1031 | !strcmp(server_url, new_url)) | ||
| 1032 | die (STATE_WARNING, | ||
| 1033 | _("HTTP WARNING - redirection creates an infinite loop - %s://%s:%d%s%s\n"), | ||
| 1034 | use_ssl ? "https" : "http", new_host, new_port, new_url, (display_html ? "</A>" : "")); | ||
| 1035 | |||
| 1036 | /* set new values for redirected request */ | ||
| 1037 | |||
| 1038 | if (!(followsticky & STICKY_HOST)) { | ||
| 1039 | free (server_address); | ||
| 1040 | server_address = strndup (new_host, MAX_IPV4_HOSTLENGTH); | ||
| 1041 | } | ||
| 1042 | if (!(followsticky & STICKY_PORT)) { | ||
| 1043 | server_port = (unsigned short)new_port; | ||
| 1044 | } | ||
| 1045 | |||
| 1046 | free (host_name); | ||
| 1047 | host_name = strndup (new_host, MAX_IPV4_HOSTLENGTH); | ||
| 1048 | |||
| 1049 | /* reset virtual port */ | ||
| 1050 | virtual_port = server_port; | ||
| 1051 | |||
| 1052 | free(new_host); | ||
| 1053 | free (server_url); | ||
| 1054 | server_url = new_url; | ||
| 1055 | |||
| 1056 | uriFreeUriMembersA (&uri); | ||
| 1057 | |||
| 1058 | if (verbose) | ||
| 1059 | printf (_("Redirection to %s://%s:%d%s\n"), use_ssl ? "https" : "http", | ||
| 1060 | host_name ? host_name : server_address, server_port, server_url); | ||
| 1061 | |||
| 1062 | /* TODO: the hash component MUST be taken from the original URL and | ||
| 1063 | * attached to the URL in Location | ||
| 1064 | */ | ||
| 1065 | |||
| 1066 | check_http (); | ||
| 1067 | } | ||
| 1068 | |||
| 1069 | /* check whether a file exists */ | ||
| 1070 | void | ||
| 1071 | test_file (char *path) | ||
| 1072 | { | ||
| 1073 | if (access(path, R_OK) == 0) | ||
| 1074 | return; | ||
| 1075 | usage2 (_("file does not exist or is not readable"), path); | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | int | ||
| 1079 | process_arguments (int argc, char **argv) | ||
| 1080 | { | ||
| 1081 | char *p; | ||
| 1082 | int c = 1; | ||
| 1083 | char *temp; | ||
| 1084 | |||
| 1085 | enum { | ||
| 1086 | INVERT_REGEX = CHAR_MAX + 1, | ||
| 1087 | SNI_OPTION, | ||
| 1088 | CA_CERT_OPTION | ||
| 1089 | }; | ||
| 1090 | |||
| 1091 | int option = 0; | ||
| 1092 | int got_plus = 0; | ||
| 1093 | static struct option longopts[] = { | ||
| 1094 | STD_LONG_OPTS, | ||
| 1095 | {"link", no_argument, 0, 'L'}, | ||
| 1096 | {"nohtml", no_argument, 0, 'n'}, | ||
| 1097 | {"ssl", optional_argument, 0, 'S'}, | ||
| 1098 | {"sni", no_argument, 0, SNI_OPTION}, | ||
| 1099 | {"post", required_argument, 0, 'P'}, | ||
| 1100 | {"method", required_argument, 0, 'j'}, | ||
| 1101 | {"IP-address", required_argument, 0, 'I'}, | ||
| 1102 | {"url", required_argument, 0, 'u'}, | ||
| 1103 | {"port", required_argument, 0, 'p'}, | ||
| 1104 | {"authorization", required_argument, 0, 'a'}, | ||
| 1105 | {"proxy-authorization", required_argument, 0, 'b'}, | ||
| 1106 | {"header-string", required_argument, 0, 'd'}, | ||
| 1107 | {"string", required_argument, 0, 's'}, | ||
| 1108 | {"expect", required_argument, 0, 'e'}, | ||
| 1109 | {"regex", required_argument, 0, 'r'}, | ||
| 1110 | {"ereg", required_argument, 0, 'r'}, | ||
| 1111 | {"eregi", required_argument, 0, 'R'}, | ||
| 1112 | {"linespan", no_argument, 0, 'l'}, | ||
| 1113 | {"onredirect", required_argument, 0, 'f'}, | ||
| 1114 | {"certificate", required_argument, 0, 'C'}, | ||
| 1115 | {"client-cert", required_argument, 0, 'J'}, | ||
| 1116 | {"private-key", required_argument, 0, 'K'}, | ||
| 1117 | {"ca-cert", required_argument, 0, CA_CERT_OPTION}, | ||
| 1118 | {"useragent", required_argument, 0, 'A'}, | ||
| 1119 | {"header", required_argument, 0, 'k'}, | ||
| 1120 | {"no-body", no_argument, 0, 'N'}, | ||
| 1121 | {"max-age", required_argument, 0, 'M'}, | ||
| 1122 | {"content-type", required_argument, 0, 'T'}, | ||
| 1123 | {"pagesize", required_argument, 0, 'm'}, | ||
| 1124 | {"invert-regex", no_argument, NULL, INVERT_REGEX}, | ||
| 1125 | {"use-ipv4", no_argument, 0, '4'}, | ||
| 1126 | {"use-ipv6", no_argument, 0, '6'}, | ||
| 1127 | {"extended-perfdata", no_argument, 0, 'E'}, | ||
| 1128 | {0, 0, 0, 0} | ||
| 1129 | }; | ||
| 1130 | |||
| 1131 | if (argc < 2) | ||
| 1132 | return ERROR; | ||
| 1133 | |||
| 1134 | /* support check_http compatible arguments */ | ||
| 1135 | for (c = 1; c < argc; c++) { | ||
| 1136 | if (strcmp ("-to", argv[c]) == 0) | ||
| 1137 | strcpy (argv[c], "-t"); | ||
| 1138 | if (strcmp ("-hn", argv[c]) == 0) | ||
| 1139 | strcpy (argv[c], "-H"); | ||
| 1140 | if (strcmp ("-wt", argv[c]) == 0) | ||
| 1141 | strcpy (argv[c], "-w"); | ||
| 1142 | if (strcmp ("-ct", argv[c]) == 0) | ||
| 1143 | strcpy (argv[c], "-c"); | ||
| 1144 | if (strcmp ("-nohtml", argv[c]) == 0) | ||
| 1145 | strcpy (argv[c], "-n"); | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | server_url = strdup(DEFAULT_SERVER_URL); | ||
| 1149 | |||
| 1150 | while (1) { | ||
| 1151 | 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:nlLS::m:M:NE", longopts, &option); | ||
| 1152 | if (c == -1 || c == EOF || c == 1) | ||
| 1153 | break; | ||
| 1154 | |||
| 1155 | switch (c) { | ||
| 1156 | case 'h': | ||
| 1157 | print_help(); | ||
| 1158 | exit(STATE_UNKNOWN); | ||
| 1159 | break; | ||
| 1160 | case 'V': | ||
| 1161 | print_revision(progname, NP_VERSION); | ||
| 1162 | print_curl_version(); | ||
| 1163 | exit(STATE_UNKNOWN); | ||
| 1164 | break; | ||
| 1165 | case 'v': | ||
| 1166 | verbose++; | ||
| 1167 | break; | ||
| 1168 | case 't': /* timeout period */ | ||
| 1169 | if (!is_intnonneg (optarg)) | ||
| 1170 | usage2 (_("Timeout interval must be a positive integer"), optarg); | ||
| 1171 | else | ||
| 1172 | socket_timeout = (int)strtol (optarg, NULL, 10); | ||
| 1173 | break; | ||
| 1174 | case 'c': /* critical time threshold */ | ||
| 1175 | critical_thresholds = optarg; | ||
| 1176 | break; | ||
| 1177 | case 'w': /* warning time threshold */ | ||
| 1178 | warning_thresholds = optarg; | ||
| 1179 | break; | ||
| 1180 | case 'H': /* virtual host */ | ||
| 1181 | host_name = strdup (optarg); | ||
| 1182 | if (host_name[0] == '[') { | ||
| 1183 | if ((p = strstr (host_name, "]:")) != NULL) { /* [IPv6]:port */ | ||
| 1184 | virtual_port = atoi (p + 2); | ||
| 1185 | /* cut off the port */ | ||
| 1186 | host_name_length = strlen (host_name) - strlen (p) - 1; | ||
| 1187 | free (host_name); | ||
| 1188 | host_name = strndup (optarg, host_name_length); | ||
| 1189 | } | ||
| 1190 | } else if ((p = strchr (host_name, ':')) != NULL | ||
| 1191 | && strchr (++p, ':') == NULL) { /* IPv4:port or host:port */ | ||
| 1192 | virtual_port = atoi (p); | ||
| 1193 | /* cut off the port */ | ||
| 1194 | host_name_length = strlen (host_name) - strlen (p) - 1; | ||
| 1195 | free (host_name); | ||
| 1196 | host_name = strndup (optarg, host_name_length); | ||
| 1197 | } | ||
| 1198 | break; | ||
| 1199 | case 'I': /* internet address */ | ||
| 1200 | server_address = strdup (optarg); | ||
| 1201 | break; | ||
| 1202 | case 'u': /* URL path */ | ||
| 1203 | server_url = strdup (optarg); | ||
| 1204 | break; | ||
| 1205 | case 'p': /* Server port */ | ||
| 1206 | if (!is_intnonneg (optarg)) | ||
| 1207 | usage2 (_("Invalid port number, expecting a non-negative number"), optarg); | ||
| 1208 | else { | ||
| 1209 | if( strtol(optarg, NULL, 10) > MAX_PORT) | ||
| 1210 | usage2 (_("Invalid port number, supplied port number is too big"), optarg); | ||
| 1211 | server_port = (unsigned short)strtol(optarg, NULL, 10); | ||
| 1212 | specify_port = TRUE; | ||
| 1213 | } | ||
| 1214 | break; | ||
| 1215 | case 'a': /* authorization info */ | ||
| 1216 | strncpy (user_auth, optarg, MAX_INPUT_BUFFER - 1); | ||
| 1217 | user_auth[MAX_INPUT_BUFFER - 1] = 0; | ||
| 1218 | break; | ||
| 1219 | case 'b': /* proxy-authorization info */ | ||
| 1220 | strncpy (proxy_auth, optarg, MAX_INPUT_BUFFER - 1); | ||
| 1221 | proxy_auth[MAX_INPUT_BUFFER - 1] = 0; | ||
| 1222 | break; | ||
| 1223 | case 'P': /* HTTP POST data in URL encoded format; ignored if settings already */ | ||
| 1224 | if (! http_post_data) | ||
| 1225 | http_post_data = strdup (optarg); | ||
| 1226 | if (! http_method) | ||
| 1227 | http_method = strdup("POST"); | ||
| 1228 | break; | ||
| 1229 | case 'j': /* Set HTTP method */ | ||
| 1230 | if (http_method) | ||
| 1231 | free(http_method); | ||
| 1232 | http_method = strdup (optarg); | ||
| 1233 | break; | ||
| 1234 | case 'A': /* useragent */ | ||
| 1235 | strncpy (user_agent, optarg, DEFAULT_BUFFER_SIZE); | ||
| 1236 | user_agent[DEFAULT_BUFFER_SIZE-1] = '\0'; | ||
| 1237 | break; | ||
| 1238 | case 'k': /* Additional headers */ | ||
| 1239 | if (http_opt_headers_count == 0) | ||
| 1240 | http_opt_headers = malloc (sizeof (char *) * (++http_opt_headers_count)); | ||
| 1241 | else | ||
| 1242 | http_opt_headers = realloc (http_opt_headers, sizeof (char *) * (++http_opt_headers_count)); | ||
| 1243 | http_opt_headers[http_opt_headers_count - 1] = optarg; | ||
| 1244 | break; | ||
| 1245 | case 'L': /* show html link */ | ||
| 1246 | display_html = TRUE; | ||
| 1247 | break; | ||
| 1248 | case 'n': /* do not show html link */ | ||
| 1249 | display_html = FALSE; | ||
| 1250 | break; | ||
| 1251 | case 'C': /* Check SSL cert validity */ | ||
| 1252 | #ifdef LIBCURL_FEATURE_SSL | ||
| 1253 | if ((temp=strchr(optarg,','))!=NULL) { | ||
| 1254 | *temp='\0'; | ||
| 1255 | if (!is_intnonneg (optarg)) | ||
| 1256 | usage2 (_("Invalid certificate expiration period"), optarg); | ||
| 1257 | days_till_exp_warn = atoi(optarg); | ||
| 1258 | *temp=','; | ||
| 1259 | temp++; | ||
| 1260 | if (!is_intnonneg (temp)) | ||
| 1261 | usage2 (_("Invalid certificate expiration period"), temp); | ||
| 1262 | days_till_exp_crit = atoi (temp); | ||
| 1263 | } | ||
| 1264 | else { | ||
| 1265 | days_till_exp_crit=0; | ||
| 1266 | if (!is_intnonneg (optarg)) | ||
| 1267 | usage2 (_("Invalid certificate expiration period"), optarg); | ||
| 1268 | days_till_exp_warn = atoi (optarg); | ||
| 1269 | } | ||
| 1270 | check_cert = TRUE; | ||
| 1271 | goto enable_ssl; | ||
| 1272 | #endif | ||
| 1273 | case 'J': /* use client certificate */ | ||
| 1274 | #ifdef LIBCURL_FEATURE_SSL | ||
| 1275 | test_file(optarg); | ||
| 1276 | client_cert = optarg; | ||
| 1277 | goto enable_ssl; | ||
| 1278 | #endif | ||
| 1279 | case 'K': /* use client private key */ | ||
| 1280 | #ifdef LIBCURL_FEATURE_SSL | ||
| 1281 | test_file(optarg); | ||
| 1282 | client_privkey = optarg; | ||
| 1283 | goto enable_ssl; | ||
| 1284 | #endif | ||
| 1285 | #ifdef LIBCURL_FEATURE_SSL | ||
| 1286 | case CA_CERT_OPTION: /* use CA chain file */ | ||
| 1287 | test_file(optarg); | ||
| 1288 | ca_cert = optarg; | ||
| 1289 | goto enable_ssl; | ||
| 1290 | #endif | ||
| 1291 | case 'S': /* use SSL */ | ||
| 1292 | #ifdef LIBCURL_FEATURE_SSL | ||
| 1293 | enable_ssl: | ||
| 1294 | use_ssl = TRUE; | ||
| 1295 | /* ssl_version initialized to CURL_SSLVERSION_DEFAULT as a default. | ||
| 1296 | * Only set if it's non-zero. This helps when we include multiple | ||
| 1297 | * parameters, like -S and -C combinations */ | ||
| 1298 | ssl_version = CURL_SSLVERSION_DEFAULT; | ||
| 1299 | if (c=='S' && optarg != NULL) { | ||
| 1300 | char *plus_ptr = strchr(optarg, '+'); | ||
| 1301 | if (plus_ptr) { | ||
| 1302 | got_plus = 1; | ||
| 1303 | *plus_ptr = '\0'; | ||
| 1304 | } | ||
| 1305 | |||
| 1306 | if (optarg[0] == '2') | ||
| 1307 | ssl_version = CURL_SSLVERSION_SSLv2; | ||
| 1308 | else if (optarg[0] == '3') | ||
| 1309 | ssl_version = CURL_SSLVERSION_SSLv3; | ||
| 1310 | else if (!strcmp (optarg, "1") || !strcmp (optarg, "1.0")) | ||
| 1311 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | ||
| 1312 | ssl_version = CURL_SSLVERSION_TLSv1_0; | ||
| 1313 | #else | ||
| 1314 | ssl_version = CURL_SSLVERSION_DEFAULT; | ||
| 1315 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
| 1316 | else if (!strcmp (optarg, "1.1")) | ||
| 1317 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | ||
| 1318 | ssl_version = CURL_SSLVERSION_TLSv1_1; | ||
| 1319 | #else | ||
| 1320 | ssl_version = CURL_SSLVERSION_DEFAULT; | ||
| 1321 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
| 1322 | else if (!strcmp (optarg, "1.2")) | ||
| 1323 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | ||
| 1324 | ssl_version = CURL_SSLVERSION_TLSv1_2; | ||
| 1325 | #else | ||
| 1326 | ssl_version = CURL_SSLVERSION_DEFAULT; | ||
| 1327 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
| 1328 | else if (!strcmp (optarg, "1.3")) | ||
| 1329 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 52, 0) | ||
| 1330 | ssl_version = CURL_SSLVERSION_TLSv1_3; | ||
| 1331 | #else | ||
| 1332 | ssl_version = CURL_SSLVERSION_DEFAULT; | ||
| 1333 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 52, 0) */ | ||
| 1334 | else | ||
| 1335 | usage4 (_("Invalid option - Valid SSL/TLS versions: 2, 3, 1, 1.1, 1.2 (with optional '+' suffix)")); | ||
| 1336 | } | ||
| 1337 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 54, 0) | ||
| 1338 | if (got_plus) { | ||
| 1339 | switch (ssl_version) { | ||
| 1340 | case CURL_SSLVERSION_TLSv1_3: | ||
| 1341 | ssl_version |= CURL_SSLVERSION_MAX_TLSv1_3; | ||
| 1342 | break; | ||
| 1343 | case CURL_SSLVERSION_TLSv1_2: | ||
| 1344 | case CURL_SSLVERSION_TLSv1_1: | ||
| 1345 | case CURL_SSLVERSION_TLSv1_0: | ||
| 1346 | ssl_version |= CURL_SSLVERSION_MAX_DEFAULT; | ||
| 1347 | break; | ||
| 1348 | } | ||
| 1349 | } else { | ||
| 1350 | switch (ssl_version) { | ||
| 1351 | case CURL_SSLVERSION_TLSv1_3: | ||
| 1352 | ssl_version |= CURL_SSLVERSION_MAX_TLSv1_3; | ||
| 1353 | break; | ||
| 1354 | case CURL_SSLVERSION_TLSv1_2: | ||
| 1355 | ssl_version |= CURL_SSLVERSION_MAX_TLSv1_2; | ||
| 1356 | break; | ||
| 1357 | case CURL_SSLVERSION_TLSv1_1: | ||
| 1358 | ssl_version |= CURL_SSLVERSION_MAX_TLSv1_1; | ||
| 1359 | break; | ||
| 1360 | case CURL_SSLVERSION_TLSv1_0: | ||
| 1361 | ssl_version |= CURL_SSLVERSION_MAX_TLSv1_0; | ||
| 1362 | break; | ||
| 1363 | } | ||
| 1364 | } | ||
| 1365 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 54, 0) */ | ||
| 1366 | if (verbose >= 2) | ||
| 1367 | printf(_("* Set SSL/TLS version to %d\n"), ssl_version); | ||
| 1368 | if (specify_port == FALSE) | ||
| 1369 | server_port = HTTPS_PORT; | ||
| 1370 | break; | ||
| 1371 | #else /* LIBCURL_FEATURE_SSL */ | ||
| 1372 | /* -C -J and -K fall through to here without SSL */ | ||
| 1373 | usage4 (_("Invalid option - SSL is not available")); | ||
| 1374 | break; | ||
| 1375 | case SNI_OPTION: /* --sni is parsed, but ignored, the default is TRUE with libcurl */ | ||
| 1376 | use_sni = TRUE; | ||
| 1377 | break; | ||
| 1378 | #endif /* LIBCURL_FEATURE_SSL */ | ||
| 1379 | case 'f': /* onredirect */ | ||
| 1380 | if (!strcmp (optarg, "ok")) | ||
| 1381 | onredirect = STATE_OK; | ||
| 1382 | else if (!strcmp (optarg, "warning")) | ||
| 1383 | onredirect = STATE_WARNING; | ||
| 1384 | else if (!strcmp (optarg, "critical")) | ||
| 1385 | onredirect = STATE_CRITICAL; | ||
| 1386 | else if (!strcmp (optarg, "unknown")) | ||
| 1387 | onredirect = STATE_UNKNOWN; | ||
| 1388 | else if (!strcmp (optarg, "follow")) | ||
| 1389 | onredirect = STATE_DEPENDENT; | ||
| 1390 | else if (!strcmp (optarg, "stickyport")) | ||
| 1391 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_HTTP_CURL, followsticky = STICKY_HOST|STICKY_PORT; | ||
| 1392 | else if (!strcmp (optarg, "sticky")) | ||
| 1393 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_HTTP_CURL, followsticky = STICKY_HOST; | ||
| 1394 | else if (!strcmp (optarg, "follow")) | ||
| 1395 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_HTTP_CURL, followsticky = STICKY_NONE; | ||
| 1396 | else if (!strcmp (optarg, "curl")) | ||
| 1397 | onredirect = STATE_DEPENDENT, followmethod = FOLLOW_LIBCURL; | ||
| 1398 | else usage2 (_("Invalid onredirect option"), optarg); | ||
| 1399 | if (verbose >= 2) | ||
| 1400 | printf(_("* Following redirects set to %s\n"), state_text(onredirect)); | ||
| 1401 | break; | ||
| 1402 | case 'd': /* string or substring */ | ||
| 1403 | strncpy (header_expect, optarg, MAX_INPUT_BUFFER - 1); | ||
| 1404 | header_expect[MAX_INPUT_BUFFER - 1] = 0; | ||
| 1405 | break; | ||
| 1406 | case 's': /* string or substring */ | ||
| 1407 | strncpy (string_expect, optarg, MAX_INPUT_BUFFER - 1); | ||
| 1408 | string_expect[MAX_INPUT_BUFFER - 1] = 0; | ||
| 1409 | break; | ||
| 1410 | case 'e': /* string or substring */ | ||
| 1411 | strncpy (server_expect, optarg, MAX_INPUT_BUFFER - 1); | ||
| 1412 | server_expect[MAX_INPUT_BUFFER - 1] = 0; | ||
| 1413 | server_expect_yn = 1; | ||
| 1414 | break; | ||
| 1415 | case 'T': /* Content-type */ | ||
| 1416 | http_content_type = strdup (optarg); | ||
| 1417 | break; | ||
| 1418 | case 'l': /* linespan */ | ||
| 1419 | cflags &= ~REG_NEWLINE; | ||
| 1420 | break; | ||
| 1421 | case 'R': /* regex */ | ||
| 1422 | cflags |= REG_ICASE; | ||
| 1423 | case 'r': /* regex */ | ||
| 1424 | strncpy (regexp, optarg, MAX_RE_SIZE - 1); | ||
| 1425 | regexp[MAX_RE_SIZE - 1] = 0; | ||
| 1426 | errcode = regcomp (&preg, regexp, cflags); | ||
| 1427 | if (errcode != 0) { | ||
| 1428 | (void) regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); | ||
| 1429 | printf (_("Could Not Compile Regular Expression: %s"), errbuf); | ||
| 1430 | return ERROR; | ||
| 1431 | } | ||
| 1432 | break; | ||
| 1433 | case INVERT_REGEX: | ||
| 1434 | invert_regex = 1; | ||
| 1435 | break; | ||
| 1436 | case '4': | ||
| 1437 | address_family = AF_INET; | ||
| 1438 | break; | ||
| 1439 | case '6': | ||
| 1440 | #if defined (USE_IPV6) && defined (LIBCURL_FEATURE_IPV6) | ||
| 1441 | address_family = AF_INET6; | ||
| 1442 | #else | ||
| 1443 | usage4 (_("IPv6 support not available")); | ||
| 1444 | #endif | ||
| 1445 | break; | ||
| 1446 | case 'm': /* min_page_length */ | ||
| 1447 | { | ||
| 1448 | char *tmp; | ||
| 1449 | if (strchr(optarg, ':') != (char *)NULL) { | ||
| 1450 | /* range, so get two values, min:max */ | ||
| 1451 | tmp = strtok(optarg, ":"); | ||
| 1452 | if (tmp == NULL) { | ||
| 1453 | printf("Bad format: try \"-m min:max\"\n"); | ||
| 1454 | exit (STATE_WARNING); | ||
| 1455 | } else | ||
| 1456 | min_page_len = atoi(tmp); | ||
| 1457 | |||
| 1458 | tmp = strtok(NULL, ":"); | ||
| 1459 | if (tmp == NULL) { | ||
| 1460 | printf("Bad format: try \"-m min:max\"\n"); | ||
| 1461 | exit (STATE_WARNING); | ||
| 1462 | } else | ||
| 1463 | max_page_len = atoi(tmp); | ||
| 1464 | } else | ||
| 1465 | min_page_len = atoi (optarg); | ||
| 1466 | break; | ||
| 1467 | } | ||
| 1468 | case 'N': /* no-body */ | ||
| 1469 | no_body = TRUE; | ||
| 1470 | break; | ||
| 1471 | case 'M': /* max-age */ | ||
| 1472 | { | ||
| 1473 | int L = strlen(optarg); | ||
| 1474 | if (L && optarg[L-1] == 'm') | ||
| 1475 | maximum_age = atoi (optarg) * 60; | ||
| 1476 | else if (L && optarg[L-1] == 'h') | ||
| 1477 | maximum_age = atoi (optarg) * 60 * 60; | ||
| 1478 | else if (L && optarg[L-1] == 'd') | ||
| 1479 | maximum_age = atoi (optarg) * 60 * 60 * 24; | ||
| 1480 | else if (L && (optarg[L-1] == 's' || | ||
| 1481 | isdigit (optarg[L-1]))) | ||
| 1482 | maximum_age = atoi (optarg); | ||
| 1483 | else { | ||
| 1484 | fprintf (stderr, "unparsable max-age: %s\n", optarg); | ||
| 1485 | exit (STATE_WARNING); | ||
| 1486 | } | ||
| 1487 | if (verbose >= 2) | ||
| 1488 | printf ("* Maximal age of document set to %d seconds\n", maximum_age); | ||
| 1489 | } | ||
| 1490 | break; | ||
| 1491 | case 'E': /* show extended perfdata */ | ||
| 1492 | show_extended_perfdata = TRUE; | ||
| 1493 | break; | ||
| 1494 | case '?': | ||
| 1495 | /* print short usage statement if args not parsable */ | ||
| 1496 | usage5 (); | ||
| 1497 | break; | ||
| 1498 | } | ||
| 1499 | } | ||
| 1500 | |||
| 1501 | c = optind; | ||
| 1502 | |||
| 1503 | if (server_address == NULL && c < argc) | ||
| 1504 | server_address = strdup (argv[c++]); | ||
| 1505 | |||
| 1506 | if (host_name == NULL && c < argc) | ||
| 1507 | host_name = strdup (argv[c++]); | ||
| 1508 | |||
| 1509 | if (server_address == NULL) { | ||
| 1510 | if (host_name == NULL) | ||
| 1511 | usage4 (_("You must specify a server address or host name")); | ||
| 1512 | else | ||
| 1513 | server_address = strdup (host_name); | ||
| 1514 | } | ||
| 1515 | |||
| 1516 | set_thresholds(&thlds, warning_thresholds, critical_thresholds); | ||
| 1517 | |||
| 1518 | if (critical_thresholds && thlds->critical->end>(double)socket_timeout) | ||
| 1519 | socket_timeout = (int)thlds->critical->end + 1; | ||
| 1520 | if (verbose >= 2) | ||
| 1521 | printf ("* Socket timeout set to %ld seconds\n", socket_timeout); | ||
| 1522 | |||
| 1523 | if (http_method == NULL) | ||
| 1524 | http_method = strdup ("GET"); | ||
| 1525 | |||
| 1526 | if (client_cert && !client_privkey) | ||
| 1527 | usage4 (_("If you use a client certificate you must also specify a private key file")); | ||
| 1528 | |||
| 1529 | if (virtual_port == 0) | ||
| 1530 | virtual_port = server_port; | ||
| 1531 | else { | ||
| 1532 | if ((use_ssl && server_port == HTTPS_PORT) || (!use_ssl && server_port == HTTP_PORT)) | ||
| 1533 | if(specify_port == FALSE) | ||
| 1534 | server_port = virtual_port; | ||
| 1535 | } | ||
| 1536 | |||
| 1537 | return TRUE; | ||
| 1538 | } | ||
| 1539 | |||
| 1540 | char *perfd_time (double elapsed_time) | ||
| 1541 | { | ||
| 1542 | return fperfdata ("time", elapsed_time, "s", | ||
| 1543 | thlds->warning?TRUE:FALSE, thlds->warning?thlds->warning->end:0, | ||
| 1544 | thlds->critical?TRUE:FALSE, thlds->critical?thlds->critical->end:0, | ||
| 1545 | TRUE, 0, TRUE, socket_timeout); | ||
| 1546 | } | ||
| 1547 | |||
| 1548 | char *perfd_time_connect (double elapsed_time_connect) | ||
| 1549 | { | ||
| 1550 | return fperfdata ("time_connect", elapsed_time_connect, "s", FALSE, 0, FALSE, 0, FALSE, 0, TRUE, socket_timeout); | ||
| 1551 | } | ||
| 1552 | |||
| 1553 | char *perfd_time_ssl (double elapsed_time_ssl) | ||
| 1554 | { | ||
| 1555 | return fperfdata ("time_ssl", elapsed_time_ssl, "s", FALSE, 0, FALSE, 0, FALSE, 0, TRUE, socket_timeout); | ||
| 1556 | } | ||
| 1557 | |||
| 1558 | char *perfd_time_headers (double elapsed_time_headers) | ||
| 1559 | { | ||
| 1560 | return fperfdata ("time_headers", elapsed_time_headers, "s", FALSE, 0, FALSE, 0, FALSE, 0, TRUE, socket_timeout); | ||
| 1561 | } | ||
| 1562 | |||
| 1563 | char *perfd_time_firstbyte (double elapsed_time_firstbyte) | ||
| 1564 | { | ||
| 1565 | return fperfdata ("time_firstbyte", elapsed_time_firstbyte, "s", FALSE, 0, FALSE, 0, FALSE, 0, TRUE, socket_timeout); | ||
| 1566 | } | ||
| 1567 | |||
| 1568 | char *perfd_time_transfer (double elapsed_time_transfer) | ||
| 1569 | { | ||
| 1570 | return fperfdata ("time_transfer", elapsed_time_transfer, "s", FALSE, 0, FALSE, 0, FALSE, 0, TRUE, socket_timeout); | ||
| 1571 | } | ||
| 1572 | |||
| 1573 | char *perfd_size (int page_len) | ||
| 1574 | { | ||
| 1575 | return perfdata ("size", page_len, "B", | ||
| 1576 | (min_page_len>0?TRUE:FALSE), min_page_len, | ||
| 1577 | (min_page_len>0?TRUE:FALSE), 0, | ||
| 1578 | TRUE, 0, FALSE, 0); | ||
| 1579 | } | ||
| 1580 | |||
| 1581 | void | ||
| 1582 | print_help (void) | ||
| 1583 | { | ||
| 1584 | print_revision (progname, NP_VERSION); | ||
| 1585 | |||
| 1586 | printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); | ||
| 1587 | printf (COPYRIGHT, copyright, email); | ||
| 1588 | |||
| 1589 | printf ("%s\n", _("This plugin tests the HTTP service on the specified host. It can test")); | ||
| 1590 | printf ("%s\n", _("normal (http) and secure (https) servers, follow redirects, search for")); | ||
| 1591 | printf ("%s\n", _("strings and regular expressions, check connection times, and report on")); | ||
| 1592 | printf ("%s\n", _("certificate expiration times.")); | ||
| 1593 | printf ("\n"); | ||
| 1594 | printf ("%s\n", _("It makes use of libcurl to do so. It tries to be as compatible to check_http")); | ||
| 1595 | printf ("%s\n", _("as possible.")); | ||
| 1596 | |||
| 1597 | printf ("\n\n"); | ||
| 1598 | |||
| 1599 | print_usage (); | ||
| 1600 | |||
| 1601 | printf (_("NOTE: One or both of -H and -I must be specified")); | ||
| 1602 | |||
| 1603 | printf ("\n"); | ||
| 1604 | |||
| 1605 | printf (UT_HELP_VRSN); | ||
| 1606 | printf (UT_EXTRA_OPTS); | ||
| 1607 | |||
| 1608 | printf (" %s\n", "-H, --hostname=ADDRESS"); | ||
| 1609 | printf (" %s\n", _("Host name argument for servers using host headers (virtual host)")); | ||
| 1610 | printf (" %s\n", _("Append a port to include it in the header (eg: example.com:5000)")); | ||
| 1611 | printf (" %s\n", "-I, --IP-address=ADDRESS"); | ||
| 1612 | printf (" %s\n", _("IP address or name (use numeric address if possible to bypass DNS lookup).")); | ||
| 1613 | printf (" %s\n", "-p, --port=INTEGER"); | ||
| 1614 | printf (" %s", _("Port number (default: ")); | ||
| 1615 | printf ("%d)\n", HTTP_PORT); | ||
| 1616 | |||
| 1617 | printf (UT_IPv46); | ||
| 1618 | |||
| 1619 | #ifdef LIBCURL_FEATURE_SSL | ||
| 1620 | printf (" %s\n", "-S, --ssl=VERSION[+]"); | ||
| 1621 | printf (" %s\n", _("Connect via SSL. Port defaults to 443. VERSION is optional, and prevents")); | ||
| 1622 | printf (" %s\n", _("auto-negotiation (2 = SSLv2, 3 = SSLv3, 1 = TLSv1, 1.1 = TLSv1.1,")); | ||
| 1623 | printf (" %s\n", _("1.2 = TLSv1.2). With a '+' suffix, newer versions are also accepted.")); | ||
| 1624 | printf (" %s\n", _("Note: SSLv2 and SSLv3 are deprecated and are usually disabled in libcurl")); | ||
| 1625 | printf (" %s\n", "--sni"); | ||
| 1626 | printf (" %s\n", _("Enable SSL/TLS hostname extension support (SNI)")); | ||
| 1627 | #if LIBCURL_VERSION_NUM >= 0x071801 | ||
| 1628 | printf (" %s\n", _("Note: --sni is the default in libcurl as SSLv2 and SSLV3 are deprecated and")); | ||
| 1629 | printf (" %s\n", _(" SNI only really works since TLSv1.0")); | ||
| 1630 | #else | ||
| 1631 | printf (" %s\n", _("Note: SNI is not supported in libcurl before 7.18.1")); | ||
| 1632 | #endif | ||
| 1633 | printf (" %s\n", "-C, --certificate=INTEGER[,INTEGER]"); | ||
| 1634 | printf (" %s\n", _("Minimum number of days a certificate has to be valid. Port defaults to 443")); | ||
| 1635 | printf (" %s\n", _("(when this option is used the URL is not checked.)")); | ||
| 1636 | printf (" %s\n", "-J, --client-cert=FILE"); | ||
| 1637 | printf (" %s\n", _("Name of file that contains the client certificate (PEM format)")); | ||
| 1638 | printf (" %s\n", _("to be used in establishing the SSL session")); | ||
| 1639 | printf (" %s\n", "-K, --private-key=FILE"); | ||
| 1640 | printf (" %s\n", _("Name of file containing the private key (PEM format)")); | ||
| 1641 | printf (" %s\n", _("matching the client certificate")); | ||
| 1642 | printf (" %s\n", "--ca-cert=FILE"); | ||
| 1643 | printf (" %s\n", _("CA certificate file to verify peer against")); | ||
| 1644 | #endif | ||
| 1645 | |||
| 1646 | printf (" %s\n", "-e, --expect=STRING"); | ||
| 1647 | printf (" %s\n", _("Comma-delimited list of strings, at least one of them is expected in")); | ||
| 1648 | printf (" %s", _("the first (status) line of the server response (default: ")); | ||
| 1649 | printf ("%s)\n", HTTP_EXPECT); | ||
| 1650 | printf (" %s\n", _("If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)")); | ||
| 1651 | printf (" %s\n", "-d, --header-string=STRING"); | ||
| 1652 | printf (" %s\n", _("String to expect in the response headers")); | ||
| 1653 | printf (" %s\n", "-s, --string=STRING"); | ||
| 1654 | printf (" %s\n", _("String to expect in the content")); | ||
| 1655 | printf (" %s\n", "-u, --url=PATH"); | ||
| 1656 | printf (" %s\n", _("URL to GET or POST (default: /)")); | ||
| 1657 | printf (" %s\n", "-P, --post=STRING"); | ||
| 1658 | printf (" %s\n", _("URL encoded http POST data")); | ||
| 1659 | printf (" %s\n", "-j, --method=STRING (for example: HEAD, OPTIONS, TRACE, PUT, DELETE, CONNECT)"); | ||
| 1660 | printf (" %s\n", _("Set HTTP method.")); | ||
| 1661 | printf (" %s\n", "-N, --no-body"); | ||
| 1662 | printf (" %s\n", _("Don't wait for document body: stop reading after headers.")); | ||
| 1663 | printf (" %s\n", _("(Note that this still does an HTTP GET or POST, not a HEAD.)")); | ||
| 1664 | printf (" %s\n", "-M, --max-age=SECONDS"); | ||
| 1665 | printf (" %s\n", _("Warn if document is more than SECONDS old. the number can also be of")); | ||
| 1666 | printf (" %s\n", _("the form \"10m\" for minutes, \"10h\" for hours, or \"10d\" for days.")); | ||
| 1667 | printf (" %s\n", "-T, --content-type=STRING"); | ||
| 1668 | printf (" %s\n", _("specify Content-Type header media type when POSTing\n")); | ||
| 1669 | printf (" %s\n", "-l, --linespan"); | ||
| 1670 | printf (" %s\n", _("Allow regex to span newlines (must precede -r or -R)")); | ||
| 1671 | printf (" %s\n", "-r, --regex, --ereg=STRING"); | ||
| 1672 | printf (" %s\n", _("Search page for regex STRING")); | ||
| 1673 | printf (" %s\n", "-R, --eregi=STRING"); | ||
| 1674 | printf (" %s\n", _("Search page for case-insensitive regex STRING")); | ||
| 1675 | printf (" %s\n", "--invert-regex"); | ||
| 1676 | printf (" %s\n", _("Return CRITICAL if found, OK if not\n")); | ||
| 1677 | printf (" %s\n", "-a, --authorization=AUTH_PAIR"); | ||
| 1678 | printf (" %s\n", _("Username:password on sites with basic authentication")); | ||
| 1679 | printf (" %s\n", "-b, --proxy-authorization=AUTH_PAIR"); | ||
| 1680 | printf (" %s\n", _("Username:password on proxy-servers with basic authentication")); | ||
| 1681 | printf (" %s\n", "-A, --useragent=STRING"); | ||
| 1682 | printf (" %s\n", _("String to be sent in http header as \"User Agent\"")); | ||
| 1683 | printf (" %s\n", "-k, --header=STRING"); | ||
| 1684 | printf (" %s\n", _("Any other tags to be sent in http header. Use multiple times for additional headers")); | ||
| 1685 | printf (" %s\n", "-E, --extended-perfdata"); | ||
| 1686 | printf (" %s\n", _("Print additional performance data")); | ||
| 1687 | printf (" %s\n", "-L, --link"); | ||
| 1688 | printf (" %s\n", _("Wrap output in HTML link (obsoleted by urlize)")); | ||
| 1689 | printf (" %s\n", "-f, --onredirect=<ok|warning|critical|follow|sticky|stickyport|curl>"); | ||
| 1690 | printf (" %s\n", _("How to handle redirected pages. sticky is like follow but stick to the")); | ||
| 1691 | printf (" %s\n", _("specified IP address. stickyport also ensures port stays the same.")); | ||
| 1692 | printf (" %s\n", _("follow uses the old redirection algorithm of check_http.")); | ||
| 1693 | printf (" %s\n", _("curl uses CURL_FOLLOWLOCATION built into libcurl.")); | ||
| 1694 | printf (" %s\n", "-m, --pagesize=INTEGER<:INTEGER>"); | ||
| 1695 | printf (" %s\n", _("Minimum page size required (bytes) : Maximum page size required (bytes)")); | ||
| 1696 | |||
| 1697 | printf (UT_WARN_CRIT); | ||
| 1698 | |||
| 1699 | printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | ||
| 1700 | |||
| 1701 | printf (UT_VERBOSE); | ||
| 1702 | |||
| 1703 | printf ("\n"); | ||
| 1704 | printf ("%s\n", _("Notes:")); | ||
| 1705 | printf (" %s\n", _("This plugin will attempt to open an HTTP connection with the host.")); | ||
| 1706 | printf (" %s\n", _("Successful connects return STATE_OK, refusals and timeouts return STATE_CRITICAL")); | ||
| 1707 | printf (" %s\n", _("other errors return STATE_UNKNOWN. Successful connects, but incorrect response")); | ||
| 1708 | printf (" %s\n", _("messages from the host result in STATE_WARNING return values. If you are")); | ||
| 1709 | printf (" %s\n", _("checking a virtual server that uses 'host headers' you must supply the FQDN")); | ||
| 1710 | printf (" %s\n", _("(fully qualified domain name) as the [host_name] argument.")); | ||
| 1711 | |||
| 1712 | #ifdef LIBCURL_FEATURE_SSL | ||
| 1713 | printf ("\n"); | ||
| 1714 | printf (" %s\n", _("This plugin can also check whether an SSL enabled web server is able to")); | ||
| 1715 | printf (" %s\n", _("serve content (optionally within a specified time) or whether the X509 ")); | ||
| 1716 | printf (" %s\n", _("certificate is still valid for the specified number of days.")); | ||
| 1717 | printf ("\n"); | ||
| 1718 | printf (" %s\n", _("Please note that this plugin does not check if the presented server")); | ||
| 1719 | printf (" %s\n", _("certificate matches the hostname of the server, or if the certificate")); | ||
| 1720 | printf (" %s\n", _("has a valid chain of trust to one of the locally installed CAs.")); | ||
| 1721 | printf ("\n"); | ||
| 1722 | printf ("%s\n", _("Examples:")); | ||
| 1723 | printf (" %s\n\n", "CHECK CONTENT: check_curl -w 5 -c 10 --ssl -H www.verisign.com"); | ||
| 1724 | printf (" %s\n", _("When the 'www.verisign.com' server returns its content within 5 seconds,")); | ||
| 1725 | printf (" %s\n", _("a STATE_OK will be returned. When the server returns its content but exceeds")); | ||
| 1726 | printf (" %s\n", _("the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,")); | ||
| 1727 | printf (" %s\n", _("a STATE_CRITICAL will be returned.")); | ||
| 1728 | printf ("\n"); | ||
| 1729 | printf (" %s\n\n", "CHECK CERTIFICATE: check_curl -H www.verisign.com -C 14"); | ||
| 1730 | printf (" %s\n", _("When the certificate of 'www.verisign.com' is valid for more than 14 days,")); | ||
| 1731 | printf (" %s\n", _("a STATE_OK is returned. When the certificate is still valid, but for less than")); | ||
| 1732 | printf (" %s\n", _("14 days, a STATE_WARNING is returned. A STATE_CRITICAL will be returned when")); | ||
| 1733 | printf (" %s\n\n", _("the certificate is expired.")); | ||
| 1734 | printf ("\n"); | ||
| 1735 | printf (" %s\n\n", "CHECK CERTIFICATE: check_curl -H www.verisign.com -C 30,14"); | ||
| 1736 | printf (" %s\n", _("When the certificate of 'www.verisign.com' is valid for more than 30 days,")); | ||
| 1737 | printf (" %s\n", _("a STATE_OK is returned. When the certificate is still valid, but for less than")); | ||
| 1738 | printf (" %s\n", _("30 days, but more than 14 days, a STATE_WARNING is returned.")); | ||
| 1739 | printf (" %s\n", _("A STATE_CRITICAL will be returned when certificate expires in less than 14 days")); | ||
| 1740 | #endif | ||
| 1741 | |||
| 1742 | printf ("\n %s\n", "CHECK WEBSERVER CONTENT VIA PROXY:"); | ||
| 1743 | printf (" %s\n", _("It is recommended to use an environment proxy like:")); | ||
| 1744 | printf (" %s\n", _("http_proxy=http://192.168.100.35:3128 ./check_curl -H www.monitoring-plugins.org")); | ||
| 1745 | printf (" %s\n", _("legacy proxy requests in check_http style still work:")); | ||
| 1746 | printf (" %s\n", _("check_curl -I 192.168.100.35 -p 3128 -u http://www.monitoring-plugins.org/ -H www.monitoring-plugins.org")); | ||
| 1747 | |||
| 1748 | #ifdef LIBCURL_FEATURE_SSL | ||
| 1749 | printf ("\n %s\n", "CHECK SSL WEBSERVER CONTENT VIA PROXY USING HTTP 1.1 CONNECT: "); | ||
| 1750 | printf (" %s\n", _("It is recommended to use an environment proxy like:")); | ||
| 1751 | printf (" %s\n", _("https_proxy=http://192.168.100.35:3128 ./check_curl -H www.verisign.com -S")); | ||
| 1752 | printf (" %s\n", _("legacy proxy requests in check_http style still work:")); | ||
| 1753 | printf (" %s\n", _("check_curl -I 192.168.100.35 -p 3128 -u https://www.verisign.com/ -S -j CONNECT -H www.verisign.com ")); | ||
| 1754 | printf (" %s\n", _("all these options are needed: -I <proxy> -p <proxy-port> -u <check-url> -S(sl) -j CONNECT -H <webserver>")); | ||
| 1755 | printf (" %s\n", _("a STATE_OK will be returned. When the server returns its content but exceeds")); | ||
| 1756 | printf (" %s\n", _("the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,")); | ||
| 1757 | printf (" %s\n", _("a STATE_CRITICAL will be returned.")); | ||
| 1758 | |||
| 1759 | #endif | ||
| 1760 | |||
| 1761 | printf (UT_SUPPORT); | ||
| 1762 | |||
| 1763 | } | ||
| 1764 | |||
| 1765 | |||
| 1766 | |||
| 1767 | void | ||
| 1768 | print_usage (void) | ||
| 1769 | { | ||
| 1770 | printf ("%s\n", _("Usage:")); | ||
| 1771 | printf (" %s -H <vhost> | -I <IP-address> [-u <uri>] [-p <port>]\n",progname); | ||
| 1772 | printf (" [-J <client certificate file>] [-K <private key>] [--ca-cert <CA certificate file>]\n"); | ||
| 1773 | printf (" [-w <warn time>] [-c <critical time>] [-t <timeout>] [-L] [-E] [-a auth]\n"); | ||
| 1774 | printf (" [-b proxy_auth] [-f <ok|warning|critcal|follow|sticky|stickyport|curl>]\n"); | ||
| 1775 | printf (" [-e <expect>] [-d string] [-s string] [-l] [-r <regex> | -R <case-insensitive regex>]\n"); | ||
| 1776 | printf (" [-P string] [-m <min_pg_size>:<max_pg_size>] [-4|-6] [-N] [-M <age>]\n"); | ||
| 1777 | printf (" [-A string] [-k string] [-S <version>] [--sni] [-C <warn_age>[,<crit_age>]]\n"); | ||
| 1778 | printf (" [-T <content-type>] [-j method]\n"); | ||
| 1779 | printf ("\n"); | ||
| 1780 | printf ("%s\n", _("WARNING: check_curl is experimental. Please use")); | ||
| 1781 | printf ("%s\n\n", _("check_http if you need a stable version.")); | ||
| 1782 | } | ||
| 1783 | |||
| 1784 | void | ||
| 1785 | print_curl_version (void) | ||
| 1786 | { | ||
| 1787 | printf( "%s\n", curl_version()); | ||
| 1788 | } | ||
| 1789 | |||
| 1790 | int | ||
| 1791 | curlhelp_initwritebuffer (curlhelp_write_curlbuf *buf) | ||
| 1792 | { | ||
| 1793 | buf->bufsize = DEFAULT_BUFFER_SIZE; | ||
| 1794 | buf->buflen = 0; | ||
| 1795 | buf->buf = (char *)malloc ((size_t)buf->bufsize); | ||
| 1796 | if (buf->buf == NULL) return -1; | ||
| 1797 | return 0; | ||
| 1798 | } | ||
| 1799 | |||
| 1800 | int | ||
| 1801 | curlhelp_buffer_write_callback (void *buffer, size_t size, size_t nmemb, void *stream) | ||
| 1802 | { | ||
| 1803 | curlhelp_write_curlbuf *buf = (curlhelp_write_curlbuf *)stream; | ||
| 1804 | |||
| 1805 | while (buf->bufsize < buf->buflen + size * nmemb + 1) { | ||
| 1806 | buf->bufsize *= buf->bufsize * 2; | ||
| 1807 | buf->buf = (char *)realloc (buf->buf, buf->bufsize); | ||
| 1808 | if (buf->buf == NULL) return -1; | ||
| 1809 | } | ||
| 1810 | |||
| 1811 | memcpy (buf->buf + buf->buflen, buffer, size * nmemb); | ||
| 1812 | buf->buflen += size * nmemb; | ||
| 1813 | buf->buf[buf->buflen] = '\0'; | ||
| 1814 | |||
| 1815 | return (int)(size * nmemb); | ||
| 1816 | } | ||
| 1817 | |||
| 1818 | int | ||
| 1819 | curlhelp_buffer_read_callback (void *buffer, size_t size, size_t nmemb, void *stream) | ||
| 1820 | { | ||
| 1821 | curlhelp_read_curlbuf *buf = (curlhelp_read_curlbuf *)stream; | ||
| 1822 | |||
| 1823 | size_t n = min (nmemb * size, buf->buflen - buf->pos); | ||
| 1824 | |||
| 1825 | memcpy (buffer, buf->buf + buf->pos, n); | ||
| 1826 | buf->pos += n; | ||
| 1827 | |||
| 1828 | return (int)n; | ||
| 1829 | } | ||
| 1830 | |||
| 1831 | void | ||
| 1832 | curlhelp_freewritebuffer (curlhelp_write_curlbuf *buf) | ||
| 1833 | { | ||
| 1834 | free (buf->buf); | ||
| 1835 | buf->buf = NULL; | ||
| 1836 | } | ||
| 1837 | |||
| 1838 | int | ||
| 1839 | curlhelp_initreadbuffer (curlhelp_read_curlbuf *buf, const char *data, size_t datalen) | ||
| 1840 | { | ||
| 1841 | buf->buflen = datalen; | ||
| 1842 | buf->buf = (char *)malloc ((size_t)buf->buflen); | ||
| 1843 | if (buf->buf == NULL) return -1; | ||
| 1844 | memcpy (buf->buf, data, datalen); | ||
| 1845 | buf->pos = 0; | ||
| 1846 | return 0; | ||
| 1847 | } | ||
| 1848 | |||
| 1849 | void | ||
| 1850 | curlhelp_freereadbuffer (curlhelp_read_curlbuf *buf) | ||
| 1851 | { | ||
| 1852 | free (buf->buf); | ||
| 1853 | buf->buf = NULL; | ||
| 1854 | } | ||
| 1855 | |||
| 1856 | /* TODO: where to put this, it's actually part of sstrings2 (logically)? | ||
| 1857 | */ | ||
| 1858 | const char* | ||
| 1859 | strrstr2(const char *haystack, const char *needle) | ||
| 1860 | { | ||
| 1861 | int counter; | ||
| 1862 | size_t len; | ||
| 1863 | const char *prev_pos; | ||
| 1864 | const char *pos; | ||
| 1865 | |||
| 1866 | if (haystack == NULL || needle == NULL) | ||
| 1867 | return NULL; | ||
| 1868 | |||
| 1869 | if (haystack[0] == '\0' || needle[0] == '\0') | ||
| 1870 | return NULL; | ||
| 1871 | |||
| 1872 | counter = 0; | ||
| 1873 | prev_pos = NULL; | ||
| 1874 | pos = haystack; | ||
| 1875 | len = strlen (needle); | ||
| 1876 | for (;;) { | ||
| 1877 | pos = strstr (pos, needle); | ||
| 1878 | if (pos == NULL) { | ||
| 1879 | if (counter == 0) | ||
| 1880 | return NULL; | ||
| 1881 | else | ||
| 1882 | return prev_pos; | ||
| 1883 | } | ||
| 1884 | counter++; | ||
| 1885 | prev_pos = pos; | ||
| 1886 | pos += len; | ||
| 1887 | if (*pos == '\0') return prev_pos; | ||
| 1888 | } | ||
| 1889 | } | ||
| 1890 | |||
| 1891 | int | ||
| 1892 | curlhelp_parse_statusline (const char *buf, curlhelp_statusline *status_line) | ||
| 1893 | { | ||
| 1894 | char *first_line_end; | ||
| 1895 | char *p; | ||
| 1896 | size_t first_line_len; | ||
| 1897 | char *pp; | ||
| 1898 | const char *start; | ||
| 1899 | char *first_line_buf; | ||
| 1900 | |||
| 1901 | /* find last start of a new header */ | ||
| 1902 | start = strrstr2 (buf, "\r\nHTTP"); | ||
| 1903 | if (start != NULL) { | ||
| 1904 | start += 2; | ||
| 1905 | buf = start; | ||
| 1906 | } | ||
| 1907 | |||
| 1908 | first_line_end = strstr(buf, "\r\n"); | ||
| 1909 | if (first_line_end == NULL) return -1; | ||
| 1910 | |||
| 1911 | first_line_len = (size_t)(first_line_end - buf); | ||
| 1912 | status_line->first_line = (char *)malloc (first_line_len + 1); | ||
| 1913 | if (status_line->first_line == NULL) return -1; | ||
| 1914 | memcpy (status_line->first_line, buf, first_line_len); | ||
| 1915 | status_line->first_line[first_line_len] = '\0'; | ||
| 1916 | first_line_buf = strdup( status_line->first_line ); | ||
| 1917 | |||
| 1918 | /* protocol and version: "HTTP/x.x" SP */ | ||
| 1919 | |||
| 1920 | p = strtok(first_line_buf, "/"); | ||
| 1921 | if( p == NULL ) { free( first_line_buf ); return -1; } | ||
| 1922 | if( strcmp( p, "HTTP" ) != 0 ) { free( first_line_buf ); return -1; } | ||
| 1923 | |||
| 1924 | p = strtok( NULL, "." ); | ||
| 1925 | if( p == NULL ) { free( first_line_buf ); return -1; } | ||
| 1926 | status_line->http_major = (int)strtol( p, &pp, 10 ); | ||
| 1927 | if( *pp != '\0' ) { free( first_line_buf ); return -1; } | ||
| 1928 | |||
| 1929 | p = strtok( NULL, " " ); | ||
| 1930 | if( p == NULL ) { free( first_line_buf ); return -1; } | ||
| 1931 | status_line->http_minor = (int)strtol( p, &pp, 10 ); | ||
| 1932 | if( *pp != '\0' ) { free( first_line_buf ); return -1; } | ||
| 1933 | |||
| 1934 | /* status code: "404" or "404.1", then SP */ | ||
| 1935 | |||
| 1936 | p = strtok( NULL, " ." ); | ||
| 1937 | if( p == NULL ) { free( first_line_buf ); return -1; } | ||
| 1938 | if( strchr( p, '.' ) != NULL ) { | ||
| 1939 | char *ppp; | ||
| 1940 | ppp = strtok( p, "." ); | ||
| 1941 | status_line->http_code = (int)strtol( ppp, &pp, 10 ); | ||
| 1942 | if( *pp != '\0' ) { free( first_line_buf ); return -1; } | ||
| 1943 | |||
| 1944 | ppp = strtok( NULL, "" ); | ||
| 1945 | status_line->http_subcode = (int)strtol( ppp, &pp, 10 ); | ||
| 1946 | if( *pp != '\0' ) { free( first_line_buf ); return -1; } | ||
| 1947 | } else { | ||
| 1948 | status_line->http_code = (int)strtol( p, &pp, 10 ); | ||
| 1949 | status_line->http_subcode = -1; | ||
| 1950 | if( *pp != '\0' ) { free( first_line_buf ); return -1; } | ||
| 1951 | } | ||
| 1952 | |||
| 1953 | /* Human readable message: "Not Found" CRLF */ | ||
| 1954 | |||
| 1955 | p = strtok( NULL, "" ); | ||
| 1956 | if( p == NULL ) { status_line->msg = ""; return 0; } | ||
| 1957 | status_line->msg = status_line->first_line + ( p - first_line_buf ); | ||
| 1958 | free( first_line_buf ); | ||
| 1959 | |||
| 1960 | return 0; | ||
| 1961 | } | ||
| 1962 | |||
| 1963 | void | ||
| 1964 | curlhelp_free_statusline (curlhelp_statusline *status_line) | ||
| 1965 | { | ||
| 1966 | free (status_line->first_line); | ||
| 1967 | } | ||
| 1968 | |||
| 1969 | void | ||
| 1970 | remove_newlines (char *s) | ||
| 1971 | { | ||
| 1972 | char *p; | ||
| 1973 | |||
| 1974 | for (p = s; *p != '\0'; p++) | ||
| 1975 | if (*p == '\r' || *p == '\n') | ||
| 1976 | *p = ' '; | ||
| 1977 | } | ||
| 1978 | |||
| 1979 | char * | ||
| 1980 | get_header_value (const struct phr_header* headers, const size_t nof_headers, const char* header) | ||
| 1981 | { | ||
| 1982 | int i; | ||
| 1983 | for( i = 0; i < nof_headers; i++ ) { | ||
| 1984 | if( strncasecmp( header, headers[i].name, max( headers[i].name_len, 4 ) ) == 0 ) { | ||
| 1985 | return strndup( headers[i].value, headers[i].value_len ); | ||
| 1986 | } | ||
| 1987 | } | ||
| 1988 | return NULL; | ||
| 1989 | } | ||
| 1990 | |||
| 1991 | int | ||
| 1992 | check_document_dates (const curlhelp_write_curlbuf *header_buf, char (*msg)[DEFAULT_BUFFER_SIZE]) | ||
| 1993 | { | ||
| 1994 | char *server_date = NULL; | ||
| 1995 | char *document_date = NULL; | ||
| 1996 | int date_result = STATE_OK; | ||
| 1997 | curlhelp_statusline status_line; | ||
| 1998 | struct phr_header headers[255]; | ||
| 1999 | size_t nof_headers = 255; | ||
| 2000 | size_t msglen; | ||
| 2001 | |||
| 2002 | int res = phr_parse_response (header_buf->buf, header_buf->buflen, | ||
| 2003 | &status_line.http_minor, &status_line.http_code, &status_line.msg, &msglen, | ||
| 2004 | headers, &nof_headers, 0); | ||
| 2005 | |||
| 2006 | server_date = get_header_value (headers, nof_headers, "date"); | ||
| 2007 | document_date = get_header_value (headers, nof_headers, "last-modified"); | ||
| 2008 | |||
| 2009 | if (!server_date || !*server_date) { | ||
| 2010 | snprintf (*msg, DEFAULT_BUFFER_SIZE, _("%sServer date unknown, "), *msg); | ||
| 2011 | date_result = max_state_alt(STATE_UNKNOWN, date_result); | ||
| 2012 | } else if (!document_date || !*document_date) { | ||
| 2013 | snprintf (*msg, DEFAULT_BUFFER_SIZE, _("%sDocument modification date unknown, "), *msg); | ||
| 2014 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
| 2015 | } else { | ||
| 2016 | time_t srv_data = curl_getdate (server_date, NULL); | ||
| 2017 | time_t doc_data = curl_getdate (document_date, NULL); | ||
| 2018 | if (verbose >= 2) | ||
| 2019 | printf ("* server date: '%s' (%d), doc_date: '%s' (%d)\n", server_date, (int)srv_data, document_date, (int)doc_data); | ||
| 2020 | if (srv_data <= 0) { | ||
| 2021 | snprintf (*msg, DEFAULT_BUFFER_SIZE, _("%sServer date \"%100s\" unparsable, "), *msg, server_date); | ||
| 2022 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
| 2023 | } else if (doc_data <= 0) { | ||
| 2024 | snprintf (*msg, DEFAULT_BUFFER_SIZE, _("%sDocument date \"%100s\" unparsable, "), *msg, document_date); | ||
| 2025 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
| 2026 | } else if (doc_data > srv_data + 30) { | ||
| 2027 | snprintf (*msg, DEFAULT_BUFFER_SIZE, _("%sDocument is %d seconds in the future, "), *msg, (int)doc_data - (int)srv_data); | ||
| 2028 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
| 2029 | } else if (doc_data < srv_data - maximum_age) { | ||
| 2030 | int n = (srv_data - doc_data); | ||
| 2031 | if (n > (60 * 60 * 24 * 2)) { | ||
| 2032 | snprintf (*msg, DEFAULT_BUFFER_SIZE, _("%sLast modified %.1f days ago, "), *msg, ((float) n) / (60 * 60 * 24)); | ||
| 2033 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
| 2034 | } else { | ||
| 2035 | snprintf (*msg, DEFAULT_BUFFER_SIZE, _("%sLast modified %d:%02d:%02d ago, "), *msg, n / (60 * 60), (n / 60) % 60, n % 60); | ||
| 2036 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
| 2037 | } | ||
| 2038 | } | ||
| 2039 | } | ||
| 2040 | |||
| 2041 | if (server_date) free (server_date); | ||
| 2042 | if (document_date) free (document_date); | ||
| 2043 | |||
| 2044 | return date_result; | ||
| 2045 | } | ||
| 2046 | |||
| 2047 | |||
| 2048 | int | ||
| 2049 | get_content_length (const curlhelp_write_curlbuf* header_buf, const curlhelp_write_curlbuf* body_buf) | ||
| 2050 | { | ||
| 2051 | const char *s; | ||
| 2052 | int content_length = 0; | ||
| 2053 | char *copy; | ||
| 2054 | struct phr_header headers[255]; | ||
| 2055 | size_t nof_headers = 255; | ||
| 2056 | size_t msglen; | ||
| 2057 | char *content_length_s = NULL; | ||
| 2058 | curlhelp_statusline status_line; | ||
| 2059 | |||
| 2060 | int res = phr_parse_response (header_buf->buf, header_buf->buflen, | ||
| 2061 | &status_line.http_minor, &status_line.http_code, &status_line.msg, &msglen, | ||
| 2062 | headers, &nof_headers, 0); | ||
| 2063 | |||
| 2064 | content_length_s = get_header_value (headers, nof_headers, "content-length"); | ||
| 2065 | if (!content_length_s) { | ||
| 2066 | return header_buf->buflen + body_buf->buflen; | ||
| 2067 | } | ||
| 2068 | content_length_s += strspn (content_length_s, " \t"); | ||
| 2069 | content_length = atoi (content_length_s); | ||
| 2070 | if (content_length != body_buf->buflen) { | ||
| 2071 | /* TODO: should we warn if the actual and the reported body length don't match? */ | ||
| 2072 | } | ||
| 2073 | |||
| 2074 | if (content_length_s) free (content_length_s); | ||
| 2075 | |||
| 2076 | return header_buf->buflen + body_buf->buflen; | ||
| 2077 | } | ||
| 2078 | |||
| 2079 | /* TODO: is there a better way in libcurl to check for the SSL library? */ | ||
| 2080 | curlhelp_ssl_library | ||
| 2081 | curlhelp_get_ssl_library (CURL* curl) | ||
| 2082 | { | ||
| 2083 | curl_version_info_data* version_data; | ||
| 2084 | char *ssl_version; | ||
| 2085 | char *library; | ||
| 2086 | curlhelp_ssl_library ssl_library = CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
| 2087 | |||
| 2088 | version_data = curl_version_info (CURLVERSION_NOW); | ||
| 2089 | if (version_data == NULL) return CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
| 2090 | |||
| 2091 | ssl_version = strdup (version_data->ssl_version); | ||
| 2092 | if (ssl_version == NULL ) return CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
| 2093 | |||
| 2094 | library = strtok (ssl_version, "/"); | ||
| 2095 | if (library == NULL) return CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
| 2096 | |||
| 2097 | if (strcmp (library, "OpenSSL") == 0) | ||
| 2098 | ssl_library = CURLHELP_SSL_LIBRARY_OPENSSL; | ||
| 2099 | else if (strcmp (library, "LibreSSL") == 0) | ||
| 2100 | ssl_library = CURLHELP_SSL_LIBRARY_LIBRESSL; | ||
| 2101 | else if (strcmp (library, "GnuTLS") == 0) | ||
| 2102 | ssl_library = CURLHELP_SSL_LIBRARY_GNUTLS; | ||
| 2103 | else if (strcmp (library, "NSS") == 0) | ||
| 2104 | ssl_library = CURLHELP_SSL_LIBRARY_NSS; | ||
| 2105 | |||
| 2106 | if (verbose >= 2) | ||
| 2107 | printf ("* SSL library string is : %s %s (%d)\n", version_data->ssl_version, library, ssl_library); | ||
| 2108 | |||
| 2109 | free (ssl_version); | ||
| 2110 | |||
| 2111 | return ssl_library; | ||
| 2112 | } | ||
| 2113 | |||
| 2114 | const char* | ||
| 2115 | curlhelp_get_ssl_library_string (curlhelp_ssl_library ssl_library) | ||
| 2116 | { | ||
| 2117 | switch (ssl_library) { | ||
| 2118 | case CURLHELP_SSL_LIBRARY_OPENSSL: | ||
| 2119 | return "OpenSSL"; | ||
| 2120 | case CURLHELP_SSL_LIBRARY_LIBRESSL: | ||
| 2121 | return "LibreSSL"; | ||
| 2122 | case CURLHELP_SSL_LIBRARY_GNUTLS: | ||
| 2123 | return "GnuTLS"; | ||
| 2124 | case CURLHELP_SSL_LIBRARY_NSS: | ||
| 2125 | return "NSS"; | ||
| 2126 | case CURLHELP_SSL_LIBRARY_UNKNOWN: | ||
| 2127 | default: | ||
| 2128 | return "unknown"; | ||
| 2129 | } | ||
| 2130 | } | ||
| 2131 | |||
| 2132 | #ifdef LIBCURL_FEATURE_SSL | ||
| 2133 | #ifndef USE_OPENSSL | ||
| 2134 | time_t | ||
| 2135 | parse_cert_date (const char *s) | ||
| 2136 | { | ||
| 2137 | struct tm tm; | ||
| 2138 | time_t date; | ||
| 2139 | |||
| 2140 | if (!s) return -1; | ||
| 2141 | |||
| 2142 | strptime (s, "%Y-%m-%d %H:%M:%S GMT", &tm); | ||
| 2143 | date = mktime (&tm); | ||
| 2144 | |||
| 2145 | return date; | ||
| 2146 | } | ||
| 2147 | |||
| 2148 | /* TODO: this needs cleanup in the sslutils.c, maybe we the #else case to | ||
| 2149 | * OpenSSL could be this function | ||
| 2150 | */ | ||
| 2151 | int | ||
| 2152 | net_noopenssl_check_certificate (cert_ptr_union* cert_ptr, int days_till_exp_warn, int days_till_exp_crit) | ||
| 2153 | { | ||
| 2154 | int i; | ||
| 2155 | struct curl_slist* slist; | ||
| 2156 | int cname_found = 0; | ||
| 2157 | char* start_date_str = NULL; | ||
| 2158 | char* end_date_str = NULL; | ||
| 2159 | time_t start_date; | ||
| 2160 | time_t end_date; | ||
| 2161 | char *tz; | ||
| 2162 | float time_left; | ||
| 2163 | int days_left; | ||
| 2164 | int time_remaining; | ||
| 2165 | char timestamp[50] = ""; | ||
| 2166 | int status = STATE_UNKNOWN; | ||
| 2167 | |||
| 2168 | if (verbose >= 2) | ||
| 2169 | printf ("**** REQUEST CERTIFICATES ****\n"); | ||
| 2170 | |||
| 2171 | for (i = 0; i < cert_ptr->to_certinfo->num_of_certs; i++) { | ||
| 2172 | for (slist = cert_ptr->to_certinfo->certinfo[i]; slist; slist = slist->next) { | ||
| 2173 | /* find first common name in subject, TODO: check alternative subjects for | ||
| 2174 | * multi-host certificate, check wildcards | ||
| 2175 | */ | ||
| 2176 | if (strncasecmp (slist->data, "Subject:", 8) == 0) { | ||
| 2177 | char* p = strstr (slist->data, "CN="); | ||
| 2178 | if (p != NULL) { | ||
| 2179 | if (strncmp (host_name, p+3, strlen (host_name)) == 0) { | ||
| 2180 | cname_found = 1; | ||
| 2181 | } | ||
| 2182 | } | ||
| 2183 | } else if (strncasecmp (slist->data, "Start Date:", 11) == 0) { | ||
| 2184 | start_date_str = &slist->data[11]; | ||
| 2185 | } else if (strncasecmp (slist->data, "Expire Date:", 12) == 0) { | ||
| 2186 | end_date_str = &slist->data[12]; | ||
| 2187 | } else if (strncasecmp (slist->data, "Cert:", 5) == 0) { | ||
| 2188 | goto HAVE_FIRST_CERT; | ||
| 2189 | } | ||
| 2190 | if (verbose >= 2) | ||
| 2191 | printf ("%d ** %s\n", i, slist->data); | ||
| 2192 | } | ||
| 2193 | } | ||
| 2194 | HAVE_FIRST_CERT: | ||
| 2195 | |||
| 2196 | if (verbose >= 2) | ||
| 2197 | printf ("**** REQUEST CERTIFICATES ****\n"); | ||
| 2198 | |||
| 2199 | if (!cname_found) { | ||
| 2200 | printf("%s\n",_("CRITICAL - Cannot retrieve certificate subject.")); | ||
| 2201 | return STATE_CRITICAL; | ||
| 2202 | } | ||
| 2203 | |||
| 2204 | start_date = parse_cert_date (start_date_str); | ||
| 2205 | if (start_date <= 0) { | ||
| 2206 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("WARNING - Unparsable 'Start Date' in certificate: '%s'"), | ||
| 2207 | start_date_str); | ||
| 2208 | puts (msg); | ||
| 2209 | return STATE_WARNING; | ||
| 2210 | } | ||
| 2211 | |||
| 2212 | end_date = parse_cert_date (end_date_str); | ||
| 2213 | if (end_date <= 0) { | ||
| 2214 | snprintf (msg, DEFAULT_BUFFER_SIZE, _("WARNING - Unparsable 'Expire Date' in certificate: '%s'"), | ||
| 2215 | start_date_str); | ||
| 2216 | puts (msg); | ||
| 2217 | return STATE_WARNING; | ||
| 2218 | } | ||
| 2219 | |||
| 2220 | time_left = difftime (end_date, time(NULL)); | ||
| 2221 | days_left = time_left / 86400; | ||
| 2222 | tz = getenv("TZ"); | ||
| 2223 | setenv("TZ", "GMT", 1); | ||
| 2224 | tzset(); | ||
| 2225 | strftime(timestamp, 50, "%c %z", localtime(&end_date)); | ||
| 2226 | if (tz) | ||
| 2227 | setenv("TZ", tz, 1); | ||
| 2228 | else | ||
| 2229 | unsetenv("TZ"); | ||
| 2230 | tzset(); | ||
| 2231 | |||
| 2232 | if (days_left > 0 && days_left <= days_till_exp_warn) { | ||
| 2233 | printf (_("%s - Certificate '%s' expires in %d day(s) (%s).\n"), (days_left>days_till_exp_crit)?"WARNING":"CRITICAL", host_name, days_left, timestamp); | ||
| 2234 | if (days_left > days_till_exp_crit) | ||
| 2235 | status = STATE_WARNING; | ||
| 2236 | else | ||
| 2237 | status = STATE_CRITICAL; | ||
| 2238 | } else if (days_left == 0 && time_left > 0) { | ||
| 2239 | if (time_left >= 3600) | ||
| 2240 | time_remaining = (int) time_left / 3600; | ||
| 2241 | else | ||
| 2242 | time_remaining = (int) time_left / 60; | ||
| 2243 | |||
| 2244 | printf (_("%s - Certificate '%s' expires in %u %s (%s)\n"), | ||
| 2245 | (days_left>days_till_exp_crit) ? "WARNING" : "CRITICAL", host_name, time_remaining, | ||
| 2246 | time_left >= 3600 ? "hours" : "minutes", timestamp); | ||
| 2247 | |||
| 2248 | if ( days_left > days_till_exp_crit) | ||
| 2249 | status = STATE_WARNING; | ||
| 2250 | else | ||
| 2251 | status = STATE_CRITICAL; | ||
| 2252 | } else if (time_left < 0) { | ||
| 2253 | printf(_("CRITICAL - Certificate '%s' expired on %s.\n"), host_name, timestamp); | ||
| 2254 | status=STATE_CRITICAL; | ||
| 2255 | } else if (days_left == 0) { | ||
| 2256 | printf (_("%s - Certificate '%s' just expired (%s).\n"), (days_left>days_till_exp_crit)?"WARNING":"CRITICAL", host_name, timestamp); | ||
| 2257 | if (days_left > days_till_exp_crit) | ||
| 2258 | status = STATE_WARNING; | ||
| 2259 | else | ||
| 2260 | status = STATE_CRITICAL; | ||
| 2261 | } else { | ||
| 2262 | printf(_("OK - Certificate '%s' will expire on %s.\n"), host_name, timestamp); | ||
| 2263 | status = STATE_OK; | ||
| 2264 | } | ||
| 2265 | return status; | ||
| 2266 | } | ||
| 2267 | #endif /* USE_OPENSSL */ | ||
| 2268 | #endif /* LIBCURL_FEATURE_SSL */ | ||
