diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-12 01:14:14 +0200 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-12 01:14:14 +0200 |
| commit | ab66b41d235c3ec38eeb8dfd2091e3b76fc1ffa3 (patch) | |
| tree | 94d048bc490950d3c6101e2275e3fdd7d1a42693 /plugins/check_curl.d | |
| parent | 684602ddec1ae3c136eec47aa078aa3fc69bbbcf (diff) | |
| download | monitoring-plugins-ab66b41d235c3ec38eeb8dfd2091e3b76fc1ffa3.tar.gz | |
check_curl: create outsourced helpers in extra files
Diffstat (limited to 'plugins/check_curl.d')
| -rw-r--r-- | plugins/check_curl.d/check_curl_helpers.c | 1217 | ||||
| -rw-r--r-- | plugins/check_curl.d/check_curl_helpers.h | 125 | ||||
| -rw-r--r-- | plugins/check_curl.d/config.h | 76 |
3 files changed, 1344 insertions, 74 deletions
diff --git a/plugins/check_curl.d/check_curl_helpers.c b/plugins/check_curl.d/check_curl_helpers.c new file mode 100644 index 00000000..42d63bc4 --- /dev/null +++ b/plugins/check_curl.d/check_curl_helpers.c | |||
| @@ -0,0 +1,1217 @@ | |||
| 1 | #include "./check_curl_helpers.h" | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <arpa/inet.h> | ||
| 4 | #include <netinet/in.h> | ||
| 5 | #include <netdb.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include "../utils.h" | ||
| 8 | |||
| 9 | extern int verbose; | ||
| 10 | char msg[DEFAULT_BUFFER_SIZE]; | ||
| 11 | char errbuf[MAX_INPUT_BUFFER]; | ||
| 12 | bool is_openssl_callback = false; | ||
| 13 | bool add_sslctx_verify_fun = false; | ||
| 14 | |||
| 15 | check_curl_configure_curl_wrapper | ||
| 16 | check_curl_configure_curl(const check_curl_static_curl_config config, | ||
| 17 | check_curl_working_state working_state, bool check_cert, | ||
| 18 | bool on_redirect_dependent, int follow_method, int max_depth) { | ||
| 19 | check_curl_configure_curl_wrapper result = { | ||
| 20 | .errorcode = OK, | ||
| 21 | .curl_state = | ||
| 22 | { | ||
| 23 | .curl_global_initialized = false, | ||
| 24 | .curl_easy_initialized = false, | ||
| 25 | .curl = NULL, | ||
| 26 | |||
| 27 | .body_buf_initialized = false, | ||
| 28 | .body_buf = NULL, | ||
| 29 | .header_buf_initialized = false, | ||
| 30 | .header_buf = NULL, | ||
| 31 | .status_line_initialized = false, | ||
| 32 | .status_line = NULL, | ||
| 33 | .put_buf_initialized = false, | ||
| 34 | .put_buf = NULL, | ||
| 35 | |||
| 36 | .header_list = NULL, | ||
| 37 | .host = NULL, | ||
| 38 | }, | ||
| 39 | }; | ||
| 40 | |||
| 41 | if ((result.curl_state.status_line = calloc(1, sizeof(curlhelp_statusline))) == NULL) { | ||
| 42 | die(STATE_UNKNOWN, "HTTP UNKNOWN - allocation of statusline failed\n"); | ||
| 43 | } | ||
| 44 | |||
| 45 | if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) { | ||
| 46 | die(STATE_UNKNOWN, "HTTP UNKNOWN - curl_global_init failed\n"); | ||
| 47 | } | ||
| 48 | result.curl_state.curl_global_initialized = true; | ||
| 49 | |||
| 50 | if ((result.curl_state.curl = curl_easy_init()) == NULL) { | ||
| 51 | die(STATE_UNKNOWN, "HTTP UNKNOWN - curl_easy_init failed\n"); | ||
| 52 | } | ||
| 53 | result.curl_state.curl_easy_initialized = true; | ||
| 54 | |||
| 55 | if (verbose >= 1) { | ||
| 56 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_VERBOSE, 1), | ||
| 57 | "CURLOPT_VERBOSE"); | ||
| 58 | } | ||
| 59 | |||
| 60 | /* print everything on stdout like check_http would do */ | ||
| 61 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_STDERR, stdout), | ||
| 62 | "CURLOPT_STDERR"); | ||
| 63 | |||
| 64 | if (config.automatic_decompression) { | ||
| 65 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 6) | ||
| 66 | handle_curl_option_return_code( | ||
| 67 | curl_easy_setopt(result.curl_state.curl, CURLOPT_ACCEPT_ENCODING, ""), | ||
| 68 | "CURLOPT_ACCEPT_ENCODING"); | ||
| 69 | #else | ||
| 70 | handle_curl_option_return_code( | ||
| 71 | curl_easy_setopt(result.curl_state.curl, CURLOPT_ENCODING, ""), "CURLOPT_ENCODING"); | ||
| 72 | #endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 21, 6) */ | ||
| 73 | } | ||
| 74 | |||
| 75 | /* initialize buffer for body of the answer */ | ||
| 76 | if (curlhelp_initwritebuffer(&result.curl_state.body_buf) < 0) { | ||
| 77 | die(STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for body\n"); | ||
| 78 | } | ||
| 79 | result.curl_state.body_buf_initialized = true; | ||
| 80 | |||
| 81 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_WRITEFUNCTION, | ||
| 82 | curlhelp_buffer_write_callback), | ||
| 83 | "CURLOPT_WRITEFUNCTION"); | ||
| 84 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_WRITEDATA, | ||
| 85 | (void *)result.curl_state.body_buf), | ||
| 86 | "CURLOPT_WRITEDATA"); | ||
| 87 | |||
| 88 | /* initialize buffer for header of the answer */ | ||
| 89 | if (curlhelp_initwritebuffer(&result.curl_state.header_buf) < 0) { | ||
| 90 | die(STATE_UNKNOWN, "HTTP CRITICAL - out of memory allocating buffer for header\n"); | ||
| 91 | } | ||
| 92 | result.curl_state.header_buf_initialized = true; | ||
| 93 | |||
| 94 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_HEADERFUNCTION, | ||
| 95 | curlhelp_buffer_write_callback), | ||
| 96 | "CURLOPT_HEADERFUNCTION"); | ||
| 97 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_WRITEHEADER, | ||
| 98 | (void *)result.curl_state.header_buf), | ||
| 99 | "CURLOPT_WRITEHEADER"); | ||
| 100 | |||
| 101 | /* set the error buffer */ | ||
| 102 | handle_curl_option_return_code( | ||
| 103 | curl_easy_setopt(result.curl_state.curl, CURLOPT_ERRORBUFFER, errbuf), | ||
| 104 | "CURLOPT_ERRORBUFFER"); | ||
| 105 | |||
| 106 | /* set timeouts */ | ||
| 107 | handle_curl_option_return_code( | ||
| 108 | curl_easy_setopt(result.curl_state.curl, CURLOPT_CONNECTTIMEOUT, config.socket_timeout), | ||
| 109 | "CURLOPT_CONNECTTIMEOUT"); | ||
| 110 | |||
| 111 | handle_curl_option_return_code( | ||
| 112 | curl_easy_setopt(result.curl_state.curl, CURLOPT_TIMEOUT, config.socket_timeout), | ||
| 113 | "CURLOPT_TIMEOUT"); | ||
| 114 | |||
| 115 | /* enable haproxy protocol */ | ||
| 116 | if (config.haproxy_protocol) { | ||
| 117 | handle_curl_option_return_code( | ||
| 118 | curl_easy_setopt(result.curl_state.curl, CURLOPT_HAPROXYPROTOCOL, 1L), | ||
| 119 | "CURLOPT_HAPROXYPROTOCOL"); | ||
| 120 | } | ||
| 121 | |||
| 122 | // fill dns resolve cache to make curl connect to the given server_address instead of the | ||
| 123 | // host_name, only required for ssl, because we use the host_name later on to make SNI happy | ||
| 124 | char dnscache[DEFAULT_BUFFER_SIZE]; | ||
| 125 | char addrstr[DEFAULT_BUFFER_SIZE / 2]; | ||
| 126 | if (working_state.use_ssl && working_state.host_name != NULL) { | ||
| 127 | int res; | ||
| 128 | if ((res = lookup_host(working_state.server_address, addrstr, DEFAULT_BUFFER_SIZE / 2, | ||
| 129 | config.sin_family)) != 0) { | ||
| 130 | snprintf(msg, DEFAULT_BUFFER_SIZE, | ||
| 131 | _("Unable to lookup IP address for '%s': getaddrinfo returned %d - %s"), | ||
| 132 | working_state.server_address, res, gai_strerror(res)); | ||
| 133 | die(STATE_CRITICAL, "HTTP CRITICAL - %s\n", msg); | ||
| 134 | } | ||
| 135 | snprintf(dnscache, DEFAULT_BUFFER_SIZE, "%s:%d:%s", working_state.host_name, | ||
| 136 | working_state.serverPort, addrstr); | ||
| 137 | result.curl_state.host = curl_slist_append(NULL, dnscache); | ||
| 138 | curl_easy_setopt(result.curl_state.curl, CURLOPT_RESOLVE, result.curl_state.host); | ||
| 139 | if (verbose >= 1) { | ||
| 140 | printf("* curl CURLOPT_RESOLVE: %s\n", dnscache); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | // If server_address is an IPv6 address it must be surround by square brackets | ||
| 145 | struct in6_addr tmp_in_addr; | ||
| 146 | if (inet_pton(AF_INET6, working_state.server_address, &tmp_in_addr) == 1) { | ||
| 147 | char *new_server_address = calloc(strlen(working_state.server_address) + 3, sizeof(char)); | ||
| 148 | if (new_server_address == NULL) { | ||
| 149 | die(STATE_UNKNOWN, "HTTP UNKNOWN - Unable to allocate memory\n"); | ||
| 150 | } | ||
| 151 | snprintf(new_server_address, strlen(working_state.server_address) + 3, "[%s]", | ||
| 152 | working_state.server_address); | ||
| 153 | working_state.server_address = new_server_address; | ||
| 154 | } | ||
| 155 | |||
| 156 | /* compose URL: use the address we want to connect to, set Host: header later */ | ||
| 157 | char url[DEFAULT_BUFFER_SIZE]; | ||
| 158 | snprintf(url, DEFAULT_BUFFER_SIZE, "%s://%s:%d%s", working_state.use_ssl ? "https" : "http", | ||
| 159 | (working_state.use_ssl & (working_state.host_name != NULL)) | ||
| 160 | ? working_state.host_name | ||
| 161 | : working_state.server_address, | ||
| 162 | working_state.serverPort, working_state.server_url); | ||
| 163 | |||
| 164 | if (verbose >= 1) { | ||
| 165 | printf("* curl CURLOPT_URL: %s\n", url); | ||
| 166 | } | ||
| 167 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_URL, url), | ||
| 168 | "CURLOPT_URL"); | ||
| 169 | |||
| 170 | /* extract proxy information for legacy proxy https requests */ | ||
| 171 | if (!strcmp(working_state.http_method, "CONNECT") || | ||
| 172 | strstr(working_state.server_url, "http") == working_state.server_url) { | ||
| 173 | handle_curl_option_return_code( | ||
| 174 | curl_easy_setopt(result.curl_state.curl, CURLOPT_PROXY, working_state.server_address), | ||
| 175 | "CURLOPT_PROXY"); | ||
| 176 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_PROXYPORT, | ||
| 177 | (long)working_state.serverPort), | ||
| 178 | "CURLOPT_PROXYPORT"); | ||
| 179 | if (verbose >= 2) { | ||
| 180 | printf("* curl CURLOPT_PROXY: %s:%d\n", working_state.server_address, | ||
| 181 | working_state.serverPort); | ||
| 182 | } | ||
| 183 | working_state.http_method = "GET"; | ||
| 184 | handle_curl_option_return_code( | ||
| 185 | curl_easy_setopt(result.curl_state.curl, CURLOPT_URL, working_state.server_url), | ||
| 186 | "CURLOPT_URL"); | ||
| 187 | } | ||
| 188 | |||
| 189 | /* disable body for HEAD request */ | ||
| 190 | if (working_state.http_method && !strcmp(working_state.http_method, "HEAD")) { | ||
| 191 | working_state.no_body = true; | ||
| 192 | } | ||
| 193 | |||
| 194 | /* set HTTP protocol version */ | ||
| 195 | handle_curl_option_return_code( | ||
| 196 | curl_easy_setopt(result.curl_state.curl, CURLOPT_HTTP_VERSION, config.curl_http_version), | ||
| 197 | "CURLOPT_HTTP_VERSION"); | ||
| 198 | |||
| 199 | /* set HTTP method */ | ||
| 200 | if (working_state.http_method) { | ||
| 201 | if (!strcmp(working_state.http_method, "POST")) { | ||
| 202 | handle_curl_option_return_code( | ||
| 203 | curl_easy_setopt(result.curl_state.curl, CURLOPT_POST, 1), "CURLOPT_POST"); | ||
| 204 | } else if (!strcmp(working_state.http_method, "PUT")) { | ||
| 205 | handle_curl_option_return_code( | ||
| 206 | curl_easy_setopt(result.curl_state.curl, CURLOPT_UPLOAD, 1), "CURLOPT_UPLOAD"); | ||
| 207 | } else { | ||
| 208 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, | ||
| 209 | CURLOPT_CUSTOMREQUEST, | ||
| 210 | working_state.http_method), | ||
| 211 | "CURLOPT_CUSTOMREQUEST"); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | char *force_host_header = NULL; | ||
| 216 | /* check if Host header is explicitly set in options */ | ||
| 217 | if (config.http_opt_headers_count) { | ||
| 218 | for (size_t i = 0; i < config.http_opt_headers_count; i++) { | ||
| 219 | if (strncmp(config.http_opt_headers[i], "Host:", 5) == 0) { | ||
| 220 | force_host_header = config.http_opt_headers[i]; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | /* set hostname (virtual hosts), not needed if CURLOPT_CONNECT_TO is used, but left in | ||
| 226 | * anyway */ | ||
| 227 | char http_header[DEFAULT_BUFFER_SIZE]; | ||
| 228 | if (working_state.host_name != NULL && force_host_header == NULL) { | ||
| 229 | if ((working_state.virtualPort != HTTP_PORT && !working_state.use_ssl) || | ||
| 230 | (working_state.virtualPort != HTTPS_PORT && working_state.use_ssl)) { | ||
| 231 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Host: %s:%d", working_state.host_name, | ||
| 232 | working_state.virtualPort); | ||
| 233 | } else { | ||
| 234 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Host: %s", working_state.host_name); | ||
| 235 | } | ||
| 236 | result.curl_state.header_list = | ||
| 237 | curl_slist_append(result.curl_state.header_list, http_header); | ||
| 238 | } | ||
| 239 | |||
| 240 | /* always close connection, be nice to servers */ | ||
| 241 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Connection: close"); | ||
| 242 | result.curl_state.header_list = curl_slist_append(result.curl_state.header_list, http_header); | ||
| 243 | |||
| 244 | /* attach additional headers supplied by the user */ | ||
| 245 | /* optionally send any other header tag */ | ||
| 246 | if (config.http_opt_headers_count) { | ||
| 247 | for (size_t i = 0; i < config.http_opt_headers_count; i++) { | ||
| 248 | result.curl_state.header_list = | ||
| 249 | curl_slist_append(result.curl_state.header_list, config.http_opt_headers[i]); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | /* set HTTP headers */ | ||
| 254 | handle_curl_option_return_code( | ||
| 255 | curl_easy_setopt(result.curl_state.curl, CURLOPT_HTTPHEADER, result.curl_state.header_list), | ||
| 256 | "CURLOPT_HTTPHEADER"); | ||
| 257 | |||
| 258 | #ifdef LIBCURL_FEATURE_SSL | ||
| 259 | /* set SSL version, warn about insecure or unsupported versions */ | ||
| 260 | if (working_state.use_ssl) { | ||
| 261 | handle_curl_option_return_code( | ||
| 262 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSLVERSION, config.ssl_version), | ||
| 263 | "CURLOPT_SSLVERSION"); | ||
| 264 | } | ||
| 265 | |||
| 266 | /* client certificate and key to present to server (SSL) */ | ||
| 267 | if (config.client_cert) { | ||
| 268 | handle_curl_option_return_code( | ||
| 269 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSLCERT, config.client_cert), | ||
| 270 | "CURLOPT_SSLCERT"); | ||
| 271 | } | ||
| 272 | |||
| 273 | if (config.client_privkey) { | ||
| 274 | handle_curl_option_return_code( | ||
| 275 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSLKEY, config.client_privkey), | ||
| 276 | "CURLOPT_SSLKEY"); | ||
| 277 | } | ||
| 278 | |||
| 279 | if (config.ca_cert) { | ||
| 280 | handle_curl_option_return_code( | ||
| 281 | curl_easy_setopt(result.curl_state.curl, CURLOPT_CAINFO, config.ca_cert), | ||
| 282 | "CURLOPT_CAINFO"); | ||
| 283 | } | ||
| 284 | |||
| 285 | if (config.ca_cert || config.verify_peer_and_host) { | ||
| 286 | /* per default if we have a CA verify both the peer and the | ||
| 287 | * hostname in the certificate, can be switched off later */ | ||
| 288 | handle_curl_option_return_code( | ||
| 289 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSL_VERIFYPEER, 1), | ||
| 290 | "CURLOPT_SSL_VERIFYPEER"); | ||
| 291 | handle_curl_option_return_code( | ||
| 292 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSL_VERIFYHOST, 2), | ||
| 293 | "CURLOPT_SSL_VERIFYHOST"); | ||
| 294 | } else { | ||
| 295 | /* backward-compatible behaviour, be tolerant in checks | ||
| 296 | * TODO: depending on more options have aspects we want | ||
| 297 | * to be less tolerant about ssl verfications | ||
| 298 | */ | ||
| 299 | handle_curl_option_return_code( | ||
| 300 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSL_VERIFYPEER, 0), | ||
| 301 | "CURLOPT_SSL_VERIFYPEER"); | ||
| 302 | handle_curl_option_return_code( | ||
| 303 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSL_VERIFYHOST, 0), | ||
| 304 | "CURLOPT_SSL_VERIFYHOST"); | ||
| 305 | } | ||
| 306 | |||
| 307 | /* detect SSL library used by libcurl */ | ||
| 308 | curlhelp_ssl_library ssl_library = curlhelp_get_ssl_library(); | ||
| 309 | |||
| 310 | /* try hard to get a stack of certificates to verify against */ | ||
| 311 | if (check_cert) { | ||
| 312 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) | ||
| 313 | /* inform curl to report back certificates */ | ||
| 314 | switch (ssl_library) { | ||
| 315 | case CURLHELP_SSL_LIBRARY_OPENSSL: | ||
| 316 | case CURLHELP_SSL_LIBRARY_LIBRESSL: | ||
| 317 | /* set callback to extract certificate with OpenSSL context function (works with | ||
| 318 | * OpenSSL-style libraries only!) */ | ||
| 319 | # ifdef USE_OPENSSL | ||
| 320 | /* libcurl and monitoring plugins built with OpenSSL, good */ | ||
| 321 | add_sslctx_verify_fun = true; | ||
| 322 | is_openssl_callback = true; | ||
| 323 | # endif /* USE_OPENSSL */ | ||
| 324 | /* libcurl is built with OpenSSL, monitoring plugins, so falling | ||
| 325 | * back to manually extracting certificate information */ | ||
| 326 | handle_curl_option_return_code( | ||
| 327 | curl_easy_setopt(result.curl_state.curl, CURLOPT_CERTINFO, 1L), "CURLOPT_CERTINFO"); | ||
| 328 | break; | ||
| 329 | |||
| 330 | case CURLHELP_SSL_LIBRARY_NSS: | ||
| 331 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) | ||
| 332 | /* NSS: support for CERTINFO is implemented since 7.34.0 */ | ||
| 333 | handle_curl_option_return_code( | ||
| 334 | curl_easy_setopt(result.curl_state.curl, CURLOPT_CERTINFO, 1L), "CURLOPT_CERTINFO"); | ||
| 335 | # else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
| 336 | die(STATE_CRITICAL, | ||
| 337 | "HTTP CRITICAL - Cannot retrieve certificates (libcurl linked with SSL library " | ||
| 338 | "'%s' is too old)\n", | ||
| 339 | curlhelp_get_ssl_library_string(ssl_library)); | ||
| 340 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 34, 0) */ | ||
| 341 | break; | ||
| 342 | |||
| 343 | case CURLHELP_SSL_LIBRARY_GNUTLS: | ||
| 344 | # if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 42, 0) | ||
| 345 | /* GnuTLS: support for CERTINFO is implemented since 7.42.0 */ | ||
| 346 | handle_curl_option_return_code( | ||
| 347 | curl_easy_setopt(result.curl_state.curl, CURLOPT_CERTINFO, 1L), "CURLOPT_CERTINFO"); | ||
| 348 | # else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 42, 0) */ | ||
| 349 | die(STATE_CRITICAL, | ||
| 350 | "HTTP CRITICAL - Cannot retrieve certificates (libcurl linked with SSL library " | ||
| 351 | "'%s' is too old)\n", | ||
| 352 | curlhelp_get_ssl_library_string(ssl_library)); | ||
| 353 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 42, 0) */ | ||
| 354 | break; | ||
| 355 | |||
| 356 | case CURLHELP_SSL_LIBRARY_UNKNOWN: | ||
| 357 | default: | ||
| 358 | die(STATE_CRITICAL, | ||
| 359 | "HTTP CRITICAL - Cannot retrieve certificates (unknown SSL library '%s', must " | ||
| 360 | "implement first)\n", | ||
| 361 | curlhelp_get_ssl_library_string(ssl_library)); | ||
| 362 | break; | ||
| 363 | } | ||
| 364 | # else /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) */ | ||
| 365 | /* old libcurl, our only hope is OpenSSL, otherwise we are out of luck */ | ||
| 366 | if (ssl_library == CURLHELP_SSL_LIBRARY_OPENSSL || | ||
| 367 | ssl_library == CURLHELP_SSL_LIBRARY_LIBRESSL) { | ||
| 368 | add_sslctx_verify_fun = true; | ||
| 369 | } else { | ||
| 370 | die(STATE_CRITICAL, "HTTP CRITICAL - Cannot retrieve certificates (no " | ||
| 371 | "CURLOPT_SSL_CTX_FUNCTION, no OpenSSL library or libcurl " | ||
| 372 | "too old and has no CURLOPT_CERTINFO)\n"); | ||
| 373 | } | ||
| 374 | # endif /* LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 1) */ | ||
| 375 | } | ||
| 376 | |||
| 377 | # if LIBCURL_VERSION_NUM >= \ | ||
| 378 | MAKE_LIBCURL_VERSION(7, 10, 6) /* required for CURLOPT_SSL_CTX_FUNCTION */ | ||
| 379 | // ssl ctx function is not available with all ssl backends | ||
| 380 | if (curl_easy_setopt(result.curl_state.curl, CURLOPT_SSL_CTX_FUNCTION, NULL) != | ||
| 381 | CURLE_UNKNOWN_OPTION) { | ||
| 382 | handle_curl_option_return_code( | ||
| 383 | curl_easy_setopt(result.curl_state.curl, CURLOPT_SSL_CTX_FUNCTION, sslctxfun), | ||
| 384 | "CURLOPT_SSL_CTX_FUNCTION"); | ||
| 385 | } | ||
| 386 | # endif | ||
| 387 | #endif /* LIBCURL_FEATURE_SSL */ | ||
| 388 | |||
| 389 | /* set default or user-given user agent identification */ | ||
| 390 | handle_curl_option_return_code( | ||
| 391 | curl_easy_setopt(result.curl_state.curl, CURLOPT_USERAGENT, config.user_agent), | ||
| 392 | "CURLOPT_USERAGENT"); | ||
| 393 | |||
| 394 | /* proxy-authentication */ | ||
| 395 | if (strcmp(config.proxy_auth, "")) { | ||
| 396 | handle_curl_option_return_code( | ||
| 397 | curl_easy_setopt(result.curl_state.curl, CURLOPT_PROXYUSERPWD, config.proxy_auth), | ||
| 398 | "CURLOPT_PROXYUSERPWD"); | ||
| 399 | } | ||
| 400 | |||
| 401 | /* authentication */ | ||
| 402 | if (strcmp(config.user_auth, "")) { | ||
| 403 | handle_curl_option_return_code( | ||
| 404 | curl_easy_setopt(result.curl_state.curl, CURLOPT_USERPWD, config.user_auth), | ||
| 405 | "CURLOPT_USERPWD"); | ||
| 406 | } | ||
| 407 | /* TODO: parameter auth method, bitfield of following methods: | ||
| 408 | * CURLAUTH_BASIC (default) | ||
| 409 | * CURLAUTH_DIGEST | ||
| 410 | * CURLAUTH_DIGEST_IE | ||
| 411 | * CURLAUTH_NEGOTIATE | ||
| 412 | * CURLAUTH_NTLM | ||
| 413 | * CURLAUTH_NTLM_WB | ||
| 414 | * | ||
| 415 | * convenience tokens for typical sets of methods: | ||
| 416 | * CURLAUTH_ANYSAFE: most secure, without BASIC | ||
| 417 | * or CURLAUTH_ANY: most secure, even BASIC if necessary | ||
| 418 | * | ||
| 419 | * handle_curl_option_return_code (curl_easy_setopt( curl, CURLOPT_HTTPAUTH, | ||
| 420 | * (long)CURLAUTH_DIGEST ), "CURLOPT_HTTPAUTH"); | ||
| 421 | */ | ||
| 422 | |||
| 423 | /* handle redirections */ | ||
| 424 | if (on_redirect_dependent) { | ||
| 425 | if (follow_method == FOLLOW_LIBCURL) { | ||
| 426 | handle_curl_option_return_code( | ||
| 427 | curl_easy_setopt(result.curl_state.curl, CURLOPT_FOLLOWLOCATION, 1), | ||
| 428 | "CURLOPT_FOLLOWLOCATION"); | ||
| 429 | |||
| 430 | /* default -1 is infinite, not good, could lead to zombie plugins! | ||
| 431 | Setting it to one bigger than maximal limit to handle errors nicely below | ||
| 432 | */ | ||
| 433 | handle_curl_option_return_code( | ||
| 434 | curl_easy_setopt(result.curl_state.curl, CURLOPT_MAXREDIRS, max_depth + 1), | ||
| 435 | "CURLOPT_MAXREDIRS"); | ||
| 436 | |||
| 437 | /* for now allow only http and https (we are a http(s) check plugin in the end) */ | ||
| 438 | #if LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 85, 0) | ||
| 439 | handle_curl_option_return_code( | ||
| 440 | curl_easy_setopt(result.curl_state.curl, CURLOPT_REDIR_PROTOCOLS_STR, "http,https"), | ||
| 441 | "CURLOPT_REDIR_PROTOCOLS_STR"); | ||
| 442 | #elif LIBCURL_VERSION_NUM >= MAKE_LIBCURL_VERSION(7, 19, 4) | ||
| 443 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, | ||
| 444 | CURLOPT_REDIR_PROTOCOLS, | ||
| 445 | CURLPROTO_HTTP | CURLPROTO_HTTPS), | ||
| 446 | "CURLOPT_REDIRECT_PROTOCOLS"); | ||
| 447 | #endif | ||
| 448 | |||
| 449 | /* TODO: handle the following aspects of redirection, make them | ||
| 450 | * command line options too later: | ||
| 451 | CURLOPT_POSTREDIR: method switch | ||
| 452 | CURLINFO_REDIRECT_URL: custom redirect option | ||
| 453 | CURLOPT_REDIRECT_PROTOCOLS: allow people to step outside safe protocols | ||
| 454 | CURLINFO_REDIRECT_COUNT: get the number of redirects, print it, maybe a range | ||
| 455 | option here is nice like for expected page size? | ||
| 456 | */ | ||
| 457 | } else { | ||
| 458 | /* old style redirection*/ | ||
| 459 | } | ||
| 460 | } | ||
| 461 | /* no-body */ | ||
| 462 | if (working_state.no_body) { | ||
| 463 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, CURLOPT_NOBODY, 1), | ||
| 464 | "CURLOPT_NOBODY"); | ||
| 465 | } | ||
| 466 | |||
| 467 | /* IPv4 or IPv6 forced DNS resolution */ | ||
| 468 | if (config.sin_family == AF_UNSPEC) { | ||
| 469 | handle_curl_option_return_code( | ||
| 470 | curl_easy_setopt(result.curl_state.curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER), | ||
| 471 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_WHATEVER)"); | ||
| 472 | } else if (config.sin_family == AF_INET) { | ||
| 473 | handle_curl_option_return_code( | ||
| 474 | curl_easy_setopt(result.curl_state.curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4), | ||
| 475 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V4)"); | ||
| 476 | } | ||
| 477 | #if defined(USE_IPV6) && defined(LIBCURL_FEATURE_IPV6) | ||
| 478 | else if (config.sin_family == AF_INET6) { | ||
| 479 | handle_curl_option_return_code( | ||
| 480 | curl_easy_setopt(result.curl_state.curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6), | ||
| 481 | "CURLOPT_IPRESOLVE(CURL_IPRESOLVE_V6)"); | ||
| 482 | } | ||
| 483 | #endif | ||
| 484 | |||
| 485 | /* either send http POST data (any data, not only POST)*/ | ||
| 486 | if (!strcmp(working_state.http_method, "POST") || !strcmp(working_state.http_method, "PUT")) { | ||
| 487 | /* set content of payload for POST and PUT */ | ||
| 488 | if (config.http_content_type) { | ||
| 489 | snprintf(http_header, DEFAULT_BUFFER_SIZE, "Content-Type: %s", | ||
| 490 | config.http_content_type); | ||
| 491 | result.curl_state.header_list = | ||
| 492 | curl_slist_append(result.curl_state.header_list, http_header); | ||
| 493 | } | ||
| 494 | /* NULL indicates "HTTP Continue" in libcurl, provide an empty string | ||
| 495 | * in case of no POST/PUT data */ | ||
| 496 | if (!working_state.http_post_data) { | ||
| 497 | working_state.http_post_data = ""; | ||
| 498 | } | ||
| 499 | |||
| 500 | if (!strcmp(working_state.http_method, "POST")) { | ||
| 501 | /* POST method, set payload with CURLOPT_POSTFIELDS */ | ||
| 502 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, | ||
| 503 | CURLOPT_POSTFIELDS, | ||
| 504 | working_state.http_post_data), | ||
| 505 | "CURLOPT_POSTFIELDS"); | ||
| 506 | } else if (!strcmp(working_state.http_method, "PUT")) { | ||
| 507 | handle_curl_option_return_code( | ||
| 508 | curl_easy_setopt(result.curl_state.curl, CURLOPT_READFUNCTION, | ||
| 509 | (curl_read_callback)curlhelp_buffer_read_callback), | ||
| 510 | "CURLOPT_READFUNCTION"); | ||
| 511 | if (curlhelp_initreadbuffer(&result.curl_state.put_buf, working_state.http_post_data, | ||
| 512 | strlen(working_state.http_post_data)) < 0) { | ||
| 513 | die(STATE_UNKNOWN, | ||
| 514 | "HTTP CRITICAL - out of memory allocating read buffer for PUT\n"); | ||
| 515 | } | ||
| 516 | result.curl_state.put_buf_initialized = true; | ||
| 517 | handle_curl_option_return_code(curl_easy_setopt(result.curl_state.curl, | ||
| 518 | CURLOPT_READDATA, | ||
| 519 | (void *)&result.curl_state.put_buf), | ||
| 520 | "CURLOPT_READDATA"); | ||
| 521 | handle_curl_option_return_code( | ||
| 522 | curl_easy_setopt(result.curl_state.curl, CURLOPT_INFILESIZE, | ||
| 523 | (curl_off_t)strlen(working_state.http_post_data)), | ||
| 524 | "CURLOPT_INFILESIZE"); | ||
| 525 | } | ||
| 526 | } | ||
| 527 | |||
| 528 | /* cookie handling */ | ||
| 529 | if (config.cookie_jar_file != NULL) { | ||
| 530 | /* enable reading cookies from a file, and if the filename is an empty string, only | ||
| 531 | * enable the curl cookie engine */ | ||
| 532 | handle_curl_option_return_code( | ||
| 533 | curl_easy_setopt(result.curl_state.curl, CURLOPT_COOKIEFILE, config.cookie_jar_file), | ||
| 534 | "CURLOPT_COOKIEFILE"); | ||
| 535 | /* now enable saving cookies to a file, but only if the filename is not an empty string, | ||
| 536 | * since writing it would fail */ | ||
| 537 | if (*config.cookie_jar_file) { | ||
| 538 | handle_curl_option_return_code( | ||
| 539 | curl_easy_setopt(result.curl_state.curl, CURLOPT_COOKIEJAR, config.cookie_jar_file), | ||
| 540 | "CURLOPT_COOKIEJAR"); | ||
| 541 | } | ||
| 542 | } | ||
| 543 | |||
| 544 | result.working_state = working_state; | ||
| 545 | |||
| 546 | return result; | ||
| 547 | } | ||
| 548 | |||
| 549 | void handle_curl_option_return_code(CURLcode res, const char *option) { | ||
| 550 | if (res != CURLE_OK) { | ||
| 551 | snprintf(msg, DEFAULT_BUFFER_SIZE, | ||
| 552 | _("Error while setting cURL option '%s': cURL returned %d - %s"), option, res, | ||
| 553 | curl_easy_strerror(res)); | ||
| 554 | die(STATE_CRITICAL, "HTTP CRITICAL - %s\n", msg); | ||
| 555 | } | ||
| 556 | } | ||
| 557 | |||
| 558 | char *get_header_value(const struct phr_header *headers, const size_t nof_headers, | ||
| 559 | const char *header) { | ||
| 560 | for (size_t i = 0; i < nof_headers; i++) { | ||
| 561 | if (headers[i].name != NULL && | ||
| 562 | strncasecmp(header, headers[i].name, max(headers[i].name_len, 4)) == 0) { | ||
| 563 | return strndup(headers[i].value, headers[i].value_len); | ||
| 564 | } | ||
| 565 | } | ||
| 566 | return NULL; | ||
| 567 | } | ||
| 568 | |||
| 569 | check_curl_working_state check_curl_working_state_init() { | ||
| 570 | check_curl_working_state result = { | ||
| 571 | .server_address = NULL, | ||
| 572 | .server_url = DEFAULT_SERVER_URL, | ||
| 573 | .host_name = NULL, | ||
| 574 | .http_method = NULL, | ||
| 575 | .http_post_data = NULL, | ||
| 576 | .virtualPort = 0, | ||
| 577 | .serverPort = HTTP_PORT, | ||
| 578 | .use_ssl = false, | ||
| 579 | .no_body = false, | ||
| 580 | }; | ||
| 581 | return result; | ||
| 582 | } | ||
| 583 | |||
| 584 | check_curl_config check_curl_config_init() { | ||
| 585 | check_curl_config tmp = { | ||
| 586 | .initial_config = check_curl_working_state_init(), | ||
| 587 | |||
| 588 | .curl_config = | ||
| 589 | { | ||
| 590 | .automatic_decompression = false, | ||
| 591 | .socket_timeout = DEFAULT_SOCKET_TIMEOUT, | ||
| 592 | .haproxy_protocol = false, | ||
| 593 | .sin_family = AF_UNSPEC, | ||
| 594 | .curl_http_version = CURL_HTTP_VERSION_NONE, | ||
| 595 | .http_opt_headers = NULL, | ||
| 596 | .http_opt_headers_count = 0, | ||
| 597 | .ssl_version = CURL_SSLVERSION_DEFAULT, | ||
| 598 | .client_cert = NULL, | ||
| 599 | .client_privkey = NULL, | ||
| 600 | .ca_cert = NULL, | ||
| 601 | .verify_peer_and_host = false, | ||
| 602 | .user_agent = {'\0'}, | ||
| 603 | .proxy_auth = "", | ||
| 604 | .user_auth = "", | ||
| 605 | .http_content_type = NULL, | ||
| 606 | .cookie_jar_file = NULL, | ||
| 607 | }, | ||
| 608 | .max_depth = DEFAULT_MAX_REDIRS, | ||
| 609 | .followmethod = FOLLOW_HTTP_CURL, | ||
| 610 | .followsticky = STICKY_NONE, | ||
| 611 | |||
| 612 | .maximum_age = -1, | ||
| 613 | .regexp = {}, | ||
| 614 | .compiled_regex = {}, | ||
| 615 | .state_regex = STATE_CRITICAL, | ||
| 616 | .invert_regex = false, | ||
| 617 | .check_cert = false, | ||
| 618 | .continue_after_check_cert = false, | ||
| 619 | .days_till_exp_warn = 0, | ||
| 620 | .days_till_exp_crit = 0, | ||
| 621 | .thlds = NULL, | ||
| 622 | .min_page_len = 0, | ||
| 623 | .max_page_len = 0, | ||
| 624 | .server_expect = | ||
| 625 | { | ||
| 626 | .string = HTTP_EXPECT, | ||
| 627 | .is_present = false, | ||
| 628 | }, | ||
| 629 | .string_expect = "", | ||
| 630 | .header_expect = "", | ||
| 631 | .on_redirect_result_state = STATE_OK, | ||
| 632 | .on_redirect_dependent = true, | ||
| 633 | |||
| 634 | .show_extended_perfdata = false, | ||
| 635 | .show_body = false, | ||
| 636 | .display_html = false, | ||
| 637 | }; | ||
| 638 | |||
| 639 | snprintf(tmp.curl_config.user_agent, DEFAULT_BUFFER_SIZE, "%s/v%s (monitoring-plugins %s, %s)", | ||
| 640 | "check_curl", NP_VERSION, VERSION, curl_version()); | ||
| 641 | |||
| 642 | return tmp; | ||
| 643 | } | ||
| 644 | |||
| 645 | /* TODO: is there a better way in libcurl to check for the SSL library? */ | ||
| 646 | curlhelp_ssl_library curlhelp_get_ssl_library(void) { | ||
| 647 | curlhelp_ssl_library ssl_library = CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
| 648 | |||
| 649 | curl_version_info_data *version_data = curl_version_info(CURLVERSION_NOW); | ||
| 650 | if (version_data == NULL) { | ||
| 651 | return CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
| 652 | } | ||
| 653 | |||
| 654 | char *ssl_version = strdup(version_data->ssl_version); | ||
| 655 | if (ssl_version == NULL) { | ||
| 656 | return CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
| 657 | } | ||
| 658 | |||
| 659 | char *library = strtok(ssl_version, "/"); | ||
| 660 | if (library == NULL) { | ||
| 661 | return CURLHELP_SSL_LIBRARY_UNKNOWN; | ||
| 662 | } | ||
| 663 | |||
| 664 | if (strcmp(library, "OpenSSL") == 0) { | ||
| 665 | ssl_library = CURLHELP_SSL_LIBRARY_OPENSSL; | ||
| 666 | } else if (strcmp(library, "LibreSSL") == 0) { | ||
| 667 | ssl_library = CURLHELP_SSL_LIBRARY_LIBRESSL; | ||
| 668 | } else if (strcmp(library, "GnuTLS") == 0) { | ||
| 669 | ssl_library = CURLHELP_SSL_LIBRARY_GNUTLS; | ||
| 670 | } else if (strcmp(library, "NSS") == 0) { | ||
| 671 | ssl_library = CURLHELP_SSL_LIBRARY_NSS; | ||
| 672 | } | ||
| 673 | |||
| 674 | if (verbose >= 2) { | ||
| 675 | printf("* SSL library string is : %s %s (%d)\n", version_data->ssl_version, library, | ||
| 676 | ssl_library); | ||
| 677 | } | ||
| 678 | |||
| 679 | free(ssl_version); | ||
| 680 | |||
| 681 | return ssl_library; | ||
| 682 | } | ||
| 683 | |||
| 684 | const char *curlhelp_get_ssl_library_string(const curlhelp_ssl_library ssl_library) { | ||
| 685 | switch (ssl_library) { | ||
| 686 | case CURLHELP_SSL_LIBRARY_OPENSSL: | ||
| 687 | return "OpenSSL"; | ||
| 688 | case CURLHELP_SSL_LIBRARY_LIBRESSL: | ||
| 689 | return "LibreSSL"; | ||
| 690 | case CURLHELP_SSL_LIBRARY_GNUTLS: | ||
| 691 | return "GnuTLS"; | ||
| 692 | case CURLHELP_SSL_LIBRARY_NSS: | ||
| 693 | return "NSS"; | ||
| 694 | case CURLHELP_SSL_LIBRARY_UNKNOWN: | ||
| 695 | default: | ||
| 696 | return "unknown"; | ||
| 697 | } | ||
| 698 | } | ||
| 699 | |||
| 700 | size_t get_content_length(const curlhelp_write_curlbuf *header_buf, | ||
| 701 | const curlhelp_write_curlbuf *body_buf) { | ||
| 702 | struct phr_header headers[255]; | ||
| 703 | size_t nof_headers = 255; | ||
| 704 | size_t msglen; | ||
| 705 | curlhelp_statusline status_line; | ||
| 706 | int res = phr_parse_response(header_buf->buf, header_buf->buflen, &status_line.http_major, | ||
| 707 | &status_line.http_minor, &status_line.http_code, &status_line.msg, | ||
| 708 | &msglen, headers, &nof_headers, 0); | ||
| 709 | |||
| 710 | if (res == -1) { | ||
| 711 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Failed to parse Response\n")); | ||
| 712 | } | ||
| 713 | |||
| 714 | char *content_length_s = get_header_value(headers, nof_headers, "content-length"); | ||
| 715 | if (!content_length_s) { | ||
| 716 | return header_buf->buflen + body_buf->buflen; | ||
| 717 | } | ||
| 718 | |||
| 719 | content_length_s += strspn(content_length_s, " \t"); | ||
| 720 | size_t content_length = atoi(content_length_s); | ||
| 721 | if (content_length != body_buf->buflen) { | ||
| 722 | /* TODO: should we warn if the actual and the reported body length don't match? */ | ||
| 723 | } | ||
| 724 | |||
| 725 | if (content_length_s) { | ||
| 726 | free(content_length_s); | ||
| 727 | } | ||
| 728 | |||
| 729 | return header_buf->buflen + body_buf->buflen; | ||
| 730 | } | ||
| 731 | |||
| 732 | mp_state_enum check_document_dates(const curlhelp_write_curlbuf *header_buf, | ||
| 733 | const char msg[static DEFAULT_BUFFER_SIZE], | ||
| 734 | const int maximum_age) { | ||
| 735 | struct phr_header headers[255]; | ||
| 736 | size_t nof_headers = 255; | ||
| 737 | curlhelp_statusline status_line; | ||
| 738 | size_t msglen; | ||
| 739 | int res = phr_parse_response(header_buf->buf, header_buf->buflen, &status_line.http_major, | ||
| 740 | &status_line.http_minor, &status_line.http_code, &status_line.msg, | ||
| 741 | &msglen, headers, &nof_headers, 0); | ||
| 742 | |||
| 743 | if (res == -1) { | ||
| 744 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Failed to parse Response\n")); | ||
| 745 | } | ||
| 746 | |||
| 747 | char *server_date = get_header_value(headers, nof_headers, "date"); | ||
| 748 | char *document_date = get_header_value(headers, nof_headers, "last-modified"); | ||
| 749 | |||
| 750 | mp_state_enum date_result = STATE_OK; | ||
| 751 | if (!server_date || !*server_date) { | ||
| 752 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
| 753 | |||
| 754 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sServer date unknown, "), msg); | ||
| 755 | strcpy(msg, tmp); | ||
| 756 | |||
| 757 | date_result = max_state_alt(STATE_UNKNOWN, date_result); | ||
| 758 | |||
| 759 | } else if (!document_date || !*document_date) { | ||
| 760 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
| 761 | |||
| 762 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sDocument modification date unknown, "), msg); | ||
| 763 | strcpy(msg, tmp); | ||
| 764 | |||
| 765 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
| 766 | |||
| 767 | } else { | ||
| 768 | time_t srv_data = curl_getdate(server_date, NULL); | ||
| 769 | time_t doc_data = curl_getdate(document_date, NULL); | ||
| 770 | if (verbose >= 2) { | ||
| 771 | printf("* server date: '%s' (%d), doc_date: '%s' (%d)\n", server_date, (int)srv_data, | ||
| 772 | document_date, (int)doc_data); | ||
| 773 | } | ||
| 774 | if (srv_data <= 0) { | ||
| 775 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
| 776 | |||
| 777 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sServer date \"%100s\" unparsable, "), msg, | ||
| 778 | server_date); | ||
| 779 | strcpy(msg, tmp); | ||
| 780 | |||
| 781 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
| 782 | } else if (doc_data <= 0) { | ||
| 783 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
| 784 | |||
| 785 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sDocument date \"%100s\" unparsable, "), msg, | ||
| 786 | document_date); | ||
| 787 | strcpy(msg, tmp); | ||
| 788 | |||
| 789 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
| 790 | } else if (doc_data > srv_data + 30) { | ||
| 791 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
| 792 | |||
| 793 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sDocument is %d seconds in the future, "), msg, | ||
| 794 | (int)doc_data - (int)srv_data); | ||
| 795 | strcpy(msg, tmp); | ||
| 796 | |||
| 797 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
| 798 | } else if (doc_data < srv_data - maximum_age) { | ||
| 799 | time_t last_modified = (srv_data - doc_data); | ||
| 800 | if (last_modified > (60 * 60 * 24 * 2)) { | ||
| 801 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
| 802 | |||
| 803 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sLast modified %.1f days ago, "), msg, | ||
| 804 | ((float)last_modified) / (60 * 60 * 24)); | ||
| 805 | strcpy(msg, tmp); | ||
| 806 | |||
| 807 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
| 808 | } else { | ||
| 809 | char tmp[DEFAULT_BUFFER_SIZE]; | ||
| 810 | |||
| 811 | snprintf(tmp, DEFAULT_BUFFER_SIZE, _("%sLast modified %ld:%02ld:%02ld ago, "), msg, | ||
| 812 | last_modified / (60 * 60), (last_modified / 60) % 60, last_modified % 60); | ||
| 813 | strcpy(msg, tmp); | ||
| 814 | |||
| 815 | date_result = max_state_alt(STATE_CRITICAL, date_result); | ||
| 816 | } | ||
| 817 | } | ||
| 818 | } | ||
| 819 | |||
| 820 | if (server_date) { | ||
| 821 | free(server_date); | ||
| 822 | } | ||
| 823 | if (document_date) { | ||
| 824 | free(document_date); | ||
| 825 | } | ||
| 826 | |||
| 827 | return date_result; | ||
| 828 | } | ||
| 829 | |||
| 830 | void curlhelp_free_statusline(curlhelp_statusline *status_line) { free(status_line->first_line); } | ||
| 831 | |||
| 832 | int curlhelp_parse_statusline(const char *buf, curlhelp_statusline *status_line) { | ||
| 833 | /* find last start of a new header */ | ||
| 834 | const char *start = strrstr2(buf, "\r\nHTTP/"); | ||
| 835 | if (start != NULL) { | ||
| 836 | start += 2; | ||
| 837 | buf = start; | ||
| 838 | } | ||
| 839 | |||
| 840 | char *first_line_end = strstr(buf, "\r\n"); | ||
| 841 | if (first_line_end == NULL) { | ||
| 842 | return -1; | ||
| 843 | } | ||
| 844 | |||
| 845 | size_t first_line_len = (size_t)(first_line_end - buf); | ||
| 846 | status_line->first_line = (char *)calloc(first_line_len + 1, sizeof(char)); | ||
| 847 | if (status_line->first_line == NULL) { | ||
| 848 | return -1; | ||
| 849 | } | ||
| 850 | memcpy(status_line->first_line, buf, first_line_len); | ||
| 851 | status_line->first_line[first_line_len] = '\0'; | ||
| 852 | char *first_line_buf = strdup(status_line->first_line); | ||
| 853 | |||
| 854 | /* protocol and version: "HTTP/x.x" SP or "HTTP/2" SP */ | ||
| 855 | char *temp_string = strtok(first_line_buf, "/"); | ||
| 856 | if (temp_string == NULL) { | ||
| 857 | free(first_line_buf); | ||
| 858 | return -1; | ||
| 859 | } | ||
| 860 | if (strcmp(temp_string, "HTTP") != 0) { | ||
| 861 | free(first_line_buf); | ||
| 862 | return -1; | ||
| 863 | } | ||
| 864 | |||
| 865 | temp_string = strtok(NULL, " "); | ||
| 866 | if (temp_string == NULL) { | ||
| 867 | free(first_line_buf); | ||
| 868 | return -1; | ||
| 869 | } | ||
| 870 | |||
| 871 | char *temp_string_2; | ||
| 872 | if (strchr(temp_string, '.') != NULL) { | ||
| 873 | |||
| 874 | /* HTTP 1.x case */ | ||
| 875 | strtok(temp_string, "."); | ||
| 876 | status_line->http_major = (int)strtol(temp_string, &temp_string_2, 10); | ||
| 877 | if (*temp_string_2 != '\0') { | ||
| 878 | free(first_line_buf); | ||
| 879 | return -1; | ||
| 880 | } | ||
| 881 | strtok(NULL, " "); | ||
| 882 | status_line->http_minor = (int)strtol(temp_string, &temp_string_2, 10); | ||
| 883 | if (*temp_string_2 != '\0') { | ||
| 884 | free(first_line_buf); | ||
| 885 | return -1; | ||
| 886 | } | ||
| 887 | temp_string += 4; /* 1.x SP */ | ||
| 888 | } else { | ||
| 889 | /* HTTP 2 case */ | ||
| 890 | status_line->http_major = (int)strtol(temp_string, &temp_string_2, 10); | ||
| 891 | status_line->http_minor = 0; | ||
| 892 | temp_string += 2; /* 2 SP */ | ||
| 893 | } | ||
| 894 | |||
| 895 | /* status code: "404" or "404.1", then SP */ | ||
| 896 | temp_string = strtok(temp_string, " "); | ||
| 897 | if (temp_string == NULL) { | ||
| 898 | free(first_line_buf); | ||
| 899 | return -1; | ||
| 900 | } | ||
| 901 | if (strchr(temp_string, '.') != NULL) { | ||
| 902 | char *ppp; | ||
| 903 | ppp = strtok(temp_string, "."); | ||
| 904 | status_line->http_code = (int)strtol(ppp, &temp_string_2, 10); | ||
| 905 | if (*temp_string_2 != '\0') { | ||
| 906 | free(first_line_buf); | ||
| 907 | return -1; | ||
| 908 | } | ||
| 909 | ppp = strtok(NULL, ""); | ||
| 910 | status_line->http_subcode = (int)strtol(ppp, &temp_string_2, 10); | ||
| 911 | if (*temp_string_2 != '\0') { | ||
| 912 | free(first_line_buf); | ||
| 913 | return -1; | ||
| 914 | } | ||
| 915 | temp_string += 6; /* 400.1 SP */ | ||
| 916 | } else { | ||
| 917 | status_line->http_code = (int)strtol(temp_string, &temp_string_2, 10); | ||
| 918 | status_line->http_subcode = -1; | ||
| 919 | if (*temp_string_2 != '\0') { | ||
| 920 | free(first_line_buf); | ||
| 921 | return -1; | ||
| 922 | } | ||
| 923 | temp_string += 4; /* 400 SP */ | ||
| 924 | } | ||
| 925 | |||
| 926 | /* Human readable message: "Not Found" CRLF */ | ||
| 927 | |||
| 928 | temp_string = strtok(temp_string, ""); | ||
| 929 | if (temp_string == NULL) { | ||
| 930 | status_line->msg = ""; | ||
| 931 | return 0; | ||
| 932 | } | ||
| 933 | status_line->msg = status_line->first_line + (temp_string - first_line_buf); | ||
| 934 | free(first_line_buf); | ||
| 935 | |||
| 936 | return 0; | ||
| 937 | } | ||
| 938 | |||
| 939 | /* TODO: where to put this, it's actually part of sstrings2 (logically)? | ||
| 940 | */ | ||
| 941 | const char *strrstr2(const char *haystack, const char *needle) { | ||
| 942 | if (haystack == NULL || needle == NULL) { | ||
| 943 | return NULL; | ||
| 944 | } | ||
| 945 | |||
| 946 | if (haystack[0] == '\0' || needle[0] == '\0') { | ||
| 947 | return NULL; | ||
| 948 | } | ||
| 949 | |||
| 950 | int counter = 0; | ||
| 951 | const char *prev_pos = NULL; | ||
| 952 | const char *pos = haystack; | ||
| 953 | size_t len = strlen(needle); | ||
| 954 | for (;;) { | ||
| 955 | pos = strstr(pos, needle); | ||
| 956 | if (pos == NULL) { | ||
| 957 | if (counter == 0) { | ||
| 958 | return NULL; | ||
| 959 | } | ||
| 960 | return prev_pos; | ||
| 961 | } | ||
| 962 | counter++; | ||
| 963 | prev_pos = pos; | ||
| 964 | pos += len; | ||
| 965 | if (*pos == '\0') { | ||
| 966 | return prev_pos; | ||
| 967 | } | ||
| 968 | } | ||
| 969 | } | ||
| 970 | |||
| 971 | void curlhelp_freereadbuffer(curlhelp_read_curlbuf *buf) { | ||
| 972 | free(buf->buf); | ||
| 973 | buf->buf = NULL; | ||
| 974 | } | ||
| 975 | |||
| 976 | void curlhelp_freewritebuffer(curlhelp_write_curlbuf *buf) { | ||
| 977 | free(buf->buf); | ||
| 978 | buf->buf = NULL; | ||
| 979 | } | ||
| 980 | |||
| 981 | int curlhelp_initreadbuffer(curlhelp_read_curlbuf **buf, const char *data, size_t datalen) { | ||
| 982 | if ((*buf = calloc(1, sizeof(curlhelp_read_curlbuf))) == NULL) { | ||
| 983 | return 1; | ||
| 984 | } | ||
| 985 | |||
| 986 | (*buf)->buflen = datalen; | ||
| 987 | (*buf)->buf = (char *)calloc((*buf)->buflen, sizeof(char)); | ||
| 988 | if ((*buf)->buf == NULL) { | ||
| 989 | return -1; | ||
| 990 | } | ||
| 991 | memcpy((*buf)->buf, data, datalen); | ||
| 992 | (*buf)->pos = 0; | ||
| 993 | return 0; | ||
| 994 | } | ||
| 995 | |||
| 996 | size_t curlhelp_buffer_read_callback(void *buffer, size_t size, size_t nmemb, void *stream) { | ||
| 997 | curlhelp_read_curlbuf *buf = (curlhelp_read_curlbuf *)stream; | ||
| 998 | |||
| 999 | size_t minimalSize = min(nmemb * size, buf->buflen - buf->pos); | ||
| 1000 | |||
| 1001 | memcpy(buffer, buf->buf + buf->pos, minimalSize); | ||
| 1002 | buf->pos += minimalSize; | ||
| 1003 | |||
| 1004 | return minimalSize; | ||
| 1005 | } | ||
| 1006 | |||
| 1007 | int curlhelp_initwritebuffer(curlhelp_write_curlbuf **buf) { | ||
| 1008 | if ((*buf = calloc(1, sizeof(curlhelp_write_curlbuf))) == NULL) { | ||
| 1009 | return 1; | ||
| 1010 | } | ||
| 1011 | (*buf)->bufsize = DEFAULT_BUFFER_SIZE * sizeof(char); | ||
| 1012 | (*buf)->buflen = 0; | ||
| 1013 | (*buf)->buf = (char *)calloc((*buf)->bufsize, sizeof(char)); | ||
| 1014 | if ((*buf)->buf == NULL) { | ||
| 1015 | return -1; | ||
| 1016 | } | ||
| 1017 | return 0; | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | size_t curlhelp_buffer_write_callback(void *buffer, size_t size, size_t nmemb, void *stream) { | ||
| 1021 | curlhelp_write_curlbuf *buf = (curlhelp_write_curlbuf *)stream; | ||
| 1022 | |||
| 1023 | while (buf->bufsize < buf->buflen + size * nmemb + 1) { | ||
| 1024 | buf->bufsize = buf->bufsize * 2; | ||
| 1025 | buf->buf = (char *)realloc(buf->buf, buf->bufsize); | ||
| 1026 | if (buf->buf == NULL) { | ||
| 1027 | fprintf(stderr, "malloc failed (%d) %s\n", errno, strerror(errno)); | ||
| 1028 | return 0; | ||
| 1029 | } | ||
| 1030 | } | ||
| 1031 | |||
| 1032 | memcpy(buf->buf + buf->buflen, buffer, size * nmemb); | ||
| 1033 | buf->buflen += size * nmemb; | ||
| 1034 | buf->buf[buf->buflen] = '\0'; | ||
| 1035 | |||
| 1036 | return size * nmemb; | ||
| 1037 | } | ||
| 1038 | |||
| 1039 | void cleanup(check_curl_global_state global_state) { | ||
| 1040 | if (global_state.status_line_initialized) { | ||
| 1041 | curlhelp_free_statusline(global_state.status_line); | ||
| 1042 | } | ||
| 1043 | global_state.status_line_initialized = false; | ||
| 1044 | |||
| 1045 | if (global_state.curl_easy_initialized) { | ||
| 1046 | curl_easy_cleanup(global_state.curl); | ||
| 1047 | } | ||
| 1048 | global_state.curl_easy_initialized = false; | ||
| 1049 | |||
| 1050 | if (global_state.curl_global_initialized) { | ||
| 1051 | curl_global_cleanup(); | ||
| 1052 | } | ||
| 1053 | global_state.curl_global_initialized = false; | ||
| 1054 | |||
| 1055 | if (global_state.body_buf_initialized) { | ||
| 1056 | curlhelp_freewritebuffer(global_state.body_buf); | ||
| 1057 | } | ||
| 1058 | global_state.body_buf_initialized = false; | ||
| 1059 | |||
| 1060 | if (global_state.header_buf_initialized) { | ||
| 1061 | curlhelp_freewritebuffer(global_state.header_buf); | ||
| 1062 | } | ||
| 1063 | global_state.header_buf_initialized = false; | ||
| 1064 | |||
| 1065 | if (global_state.put_buf_initialized) { | ||
| 1066 | curlhelp_freereadbuffer(global_state.put_buf); | ||
| 1067 | } | ||
| 1068 | global_state.put_buf_initialized = false; | ||
| 1069 | |||
| 1070 | if (global_state.header_list) { | ||
| 1071 | curl_slist_free_all(global_state.header_list); | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | if (global_state.host) { | ||
| 1075 | curl_slist_free_all(global_state.host); | ||
| 1076 | } | ||
| 1077 | } | ||
| 1078 | |||
| 1079 | int lookup_host(const char *host, char *buf, size_t buflen, sa_family_t addr_family) { | ||
| 1080 | struct addrinfo hints = { | ||
| 1081 | .ai_family = addr_family, | ||
| 1082 | .ai_socktype = SOCK_STREAM, | ||
| 1083 | .ai_flags = AI_CANONNAME, | ||
| 1084 | }; | ||
| 1085 | |||
| 1086 | struct addrinfo *result; | ||
| 1087 | int errcode = getaddrinfo(host, NULL, &hints, &result); | ||
| 1088 | if (errcode != 0) { | ||
| 1089 | return errcode; | ||
| 1090 | } | ||
| 1091 | |||
| 1092 | strcpy(buf, ""); | ||
| 1093 | struct addrinfo *res = result; | ||
| 1094 | |||
| 1095 | size_t buflen_remaining = buflen - 1; | ||
| 1096 | size_t addrstr_len; | ||
| 1097 | char addrstr[100]; | ||
| 1098 | void *ptr = {0}; | ||
| 1099 | while (res) { | ||
| 1100 | switch (res->ai_family) { | ||
| 1101 | case AF_INET: | ||
| 1102 | ptr = &((struct sockaddr_in *)res->ai_addr)->sin_addr; | ||
| 1103 | break; | ||
| 1104 | case AF_INET6: | ||
| 1105 | ptr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; | ||
| 1106 | break; | ||
| 1107 | } | ||
| 1108 | |||
| 1109 | inet_ntop(res->ai_family, ptr, addrstr, 100); | ||
| 1110 | if (verbose >= 1) { | ||
| 1111 | printf("* getaddrinfo IPv%d address: %s\n", res->ai_family == PF_INET6 ? 6 : 4, | ||
| 1112 | addrstr); | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | // Append all IPs to buf as a comma-separated string | ||
| 1116 | addrstr_len = strlen(addrstr); | ||
| 1117 | if (buflen_remaining > addrstr_len + 1) { | ||
| 1118 | if (buf[0] != '\0') { | ||
| 1119 | strncat(buf, ",", buflen_remaining); | ||
| 1120 | buflen_remaining -= 1; | ||
| 1121 | } | ||
| 1122 | strncat(buf, addrstr, buflen_remaining); | ||
| 1123 | buflen_remaining -= addrstr_len; | ||
| 1124 | } | ||
| 1125 | |||
| 1126 | res = res->ai_next; | ||
| 1127 | } | ||
| 1128 | |||
| 1129 | freeaddrinfo(result); | ||
| 1130 | |||
| 1131 | return 0; | ||
| 1132 | } | ||
| 1133 | |||
| 1134 | /* Checks if the server 'reply' is one of the expected 'statuscodes' */ | ||
| 1135 | bool expected_statuscode(const char *reply, const char *statuscodes) { | ||
| 1136 | char *expected; | ||
| 1137 | |||
| 1138 | if ((expected = strdup(statuscodes)) == NULL) { | ||
| 1139 | die(STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n")); | ||
| 1140 | } | ||
| 1141 | |||
| 1142 | bool result = false; | ||
| 1143 | for (char *code = strtok(expected, ","); code != NULL; code = strtok(NULL, ",")) { | ||
| 1144 | if (strstr(reply, code) != NULL) { | ||
| 1145 | result = true; | ||
| 1146 | break; | ||
| 1147 | } | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | free(expected); | ||
| 1151 | return result; | ||
| 1152 | } | ||
| 1153 | |||
| 1154 | /* returns a string "HTTP/1.x" or "HTTP/2" */ | ||
| 1155 | char *string_statuscode(int major, int minor) { | ||
| 1156 | static char buf[10]; | ||
| 1157 | |||
| 1158 | switch (major) { | ||
| 1159 | case 1: | ||
| 1160 | snprintf(buf, sizeof(buf), "HTTP/%d.%d", major, minor); | ||
| 1161 | break; | ||
| 1162 | case 2: | ||
| 1163 | case 3: | ||
| 1164 | snprintf(buf, sizeof(buf), "HTTP/%d", major); | ||
| 1165 | break; | ||
| 1166 | default: | ||
| 1167 | /* assuming here HTTP/N with N>=4 */ | ||
| 1168 | snprintf(buf, sizeof(buf), "HTTP/%d", major); | ||
| 1169 | break; | ||
| 1170 | } | ||
| 1171 | |||
| 1172 | return buf; | ||
| 1173 | } | ||
| 1174 | |||
| 1175 | char *perfd_time(double elapsed_time, thresholds *thlds, long socket_timeout) { | ||
| 1176 | return fperfdata("time", elapsed_time, "s", (thlds->warning != NULL), | ||
| 1177 | thlds->warning ? thlds->warning->end : 0, (thlds->critical != NULL), | ||
| 1178 | thlds->critical ? thlds->critical->end : 0, true, 0, true, socket_timeout); | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | char *perfd_time_connect(double elapsed_time_connect, long socket_timeout) { | ||
| 1182 | return fperfdata("time_connect", elapsed_time_connect, "s", false, 0, false, 0, false, 0, true, | ||
| 1183 | socket_timeout); | ||
| 1184 | } | ||
| 1185 | |||
| 1186 | char *perfd_time_ssl(double elapsed_time_ssl, long socket_timeout) { | ||
| 1187 | return fperfdata("time_ssl", elapsed_time_ssl, "s", false, 0, false, 0, false, 0, true, | ||
| 1188 | socket_timeout); | ||
| 1189 | } | ||
| 1190 | |||
| 1191 | char *perfd_time_headers(double elapsed_time_headers, long socket_timeout) { | ||
| 1192 | return fperfdata("time_headers", elapsed_time_headers, "s", false, 0, false, 0, false, 0, true, | ||
| 1193 | socket_timeout); | ||
| 1194 | } | ||
| 1195 | |||
| 1196 | char *perfd_time_firstbyte(double elapsed_time_firstbyte, long socket_timeout) { | ||
| 1197 | return fperfdata("time_firstbyte", elapsed_time_firstbyte, "s", false, 0, false, 0, false, 0, | ||
| 1198 | true, socket_timeout); | ||
| 1199 | } | ||
| 1200 | |||
| 1201 | char *perfd_time_transfer(double elapsed_time_transfer, long socket_timeout) { | ||
| 1202 | return fperfdata("time_transfer", elapsed_time_transfer, "s", false, 0, false, 0, false, 0, | ||
| 1203 | true, socket_timeout); | ||
| 1204 | } | ||
| 1205 | |||
| 1206 | char *perfd_size(size_t page_len, int min_page_len) { | ||
| 1207 | return perfdata("size", page_len, "B", (min_page_len > 0), min_page_len, (min_page_len > 0), 0, | ||
| 1208 | true, 0, false, 0); | ||
| 1209 | } | ||
| 1210 | |||
| 1211 | /* check whether a file exists */ | ||
| 1212 | void test_file(char *path) { | ||
| 1213 | if (access(path, R_OK) == 0) { | ||
| 1214 | return; | ||
| 1215 | } | ||
| 1216 | usage2(_("file does not exist or is not readable"), path); | ||
| 1217 | } | ||
diff --git a/plugins/check_curl.d/check_curl_helpers.h b/plugins/check_curl.d/check_curl_helpers.h new file mode 100644 index 00000000..0f43ab90 --- /dev/null +++ b/plugins/check_curl.d/check_curl_helpers.h | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | #include "./config.h" | ||
| 2 | #include <curl/curl.h> | ||
| 3 | #include "../picohttpparser/picohttpparser.h" | ||
| 4 | // #include "curl/easy.h" | ||
| 5 | |||
| 6 | /* for buffers for header and body */ | ||
| 7 | typedef struct { | ||
| 8 | size_t buflen; | ||
| 9 | size_t bufsize; | ||
| 10 | char *buf; | ||
| 11 | } curlhelp_write_curlbuf; | ||
| 12 | |||
| 13 | /* for buffering the data sent in PUT */ | ||
| 14 | typedef struct { | ||
| 15 | size_t buflen; | ||
| 16 | off_t pos; | ||
| 17 | char *buf; | ||
| 18 | } curlhelp_read_curlbuf; | ||
| 19 | |||
| 20 | /* for parsing the HTTP status line */ | ||
| 21 | typedef struct { | ||
| 22 | int http_major; /* major version of the protocol, always 1 (HTTP/0.9 | ||
| 23 | * never reached the big internet most likely) */ | ||
| 24 | int http_minor; /* minor version of the protocol, usually 0 or 1 */ | ||
| 25 | int http_code; /* HTTP return code as in RFC 2145 */ | ||
| 26 | int http_subcode; /* Microsoft IIS extension, HTTP subcodes, see | ||
| 27 | * http://support.microsoft.com/kb/318380/en-us */ | ||
| 28 | const char *msg; /* the human readable message */ | ||
| 29 | char *first_line; /* a copy of the first line */ | ||
| 30 | } curlhelp_statusline; | ||
| 31 | |||
| 32 | typedef struct { | ||
| 33 | bool curl_global_initialized; | ||
| 34 | bool curl_easy_initialized; | ||
| 35 | |||
| 36 | bool body_buf_initialized; | ||
| 37 | curlhelp_write_curlbuf *body_buf; | ||
| 38 | |||
| 39 | bool header_buf_initialized; | ||
| 40 | curlhelp_write_curlbuf *header_buf; | ||
| 41 | |||
| 42 | bool status_line_initialized; | ||
| 43 | curlhelp_statusline *status_line; | ||
| 44 | |||
| 45 | bool put_buf_initialized; | ||
| 46 | curlhelp_read_curlbuf *put_buf; | ||
| 47 | |||
| 48 | CURL *curl; | ||
| 49 | |||
| 50 | struct curl_slist *header_list; | ||
| 51 | struct curl_slist *host; | ||
| 52 | } check_curl_global_state; | ||
| 53 | |||
| 54 | /* to know the underlying SSL library used by libcurl */ | ||
| 55 | typedef enum curlhelp_ssl_library { | ||
| 56 | CURLHELP_SSL_LIBRARY_UNKNOWN, | ||
| 57 | CURLHELP_SSL_LIBRARY_OPENSSL, | ||
| 58 | CURLHELP_SSL_LIBRARY_LIBRESSL, | ||
| 59 | CURLHELP_SSL_LIBRARY_GNUTLS, | ||
| 60 | CURLHELP_SSL_LIBRARY_NSS | ||
| 61 | } curlhelp_ssl_library; | ||
| 62 | |||
| 63 | #define MAKE_LIBCURL_VERSION(major, minor, patch) ((major) * 0x10000 + (minor) * 0x100 + (patch)) | ||
| 64 | |||
| 65 | typedef struct { | ||
| 66 | int errorcode; | ||
| 67 | check_curl_global_state curl_state; | ||
| 68 | check_curl_working_state working_state; | ||
| 69 | } check_curl_configure_curl_wrapper; | ||
| 70 | |||
| 71 | check_curl_configure_curl_wrapper check_curl_configure_curl(check_curl_static_curl_config config, | ||
| 72 | check_curl_working_state working_state, | ||
| 73 | bool check_cert, | ||
| 74 | bool on_redirect_dependent, | ||
| 75 | int follow_method, int max_depth); | ||
| 76 | |||
| 77 | void handle_curl_option_return_code(CURLcode res, const char *option); | ||
| 78 | |||
| 79 | int curlhelp_initwritebuffer(curlhelp_write_curlbuf **buf); | ||
| 80 | size_t curlhelp_buffer_write_callback(void * /*buffer*/, size_t /*size*/, size_t /*nmemb*/, | ||
| 81 | void * /*stream*/); | ||
| 82 | void curlhelp_freewritebuffer(curlhelp_write_curlbuf * /*buf*/); | ||
| 83 | |||
| 84 | int curlhelp_initreadbuffer(curlhelp_read_curlbuf **buf, const char * /*data*/, size_t /*datalen*/); | ||
| 85 | size_t curlhelp_buffer_read_callback(void * /*buffer*/, size_t /*size*/, size_t /*nmemb*/, | ||
| 86 | void * /*stream*/); | ||
| 87 | void curlhelp_freereadbuffer(curlhelp_read_curlbuf * /*buf*/); | ||
| 88 | |||
| 89 | curlhelp_ssl_library curlhelp_get_ssl_library(void); | ||
| 90 | const char *curlhelp_get_ssl_library_string(curlhelp_ssl_library /*ssl_library*/); | ||
| 91 | |||
| 92 | typedef union { | ||
| 93 | struct curl_slist *to_info; | ||
| 94 | struct curl_certinfo *to_certinfo; | ||
| 95 | } cert_ptr_union; | ||
| 96 | int net_noopenssl_check_certificate(cert_ptr_union *, int, int); | ||
| 97 | |||
| 98 | int curlhelp_parse_statusline(const char * /*buf*/, curlhelp_statusline * /*status_line*/); | ||
| 99 | void curlhelp_free_statusline(curlhelp_statusline * /*status_line*/); | ||
| 100 | |||
| 101 | char *get_header_value(const struct phr_header *headers, size_t nof_headers, const char *header); | ||
| 102 | mp_state_enum check_document_dates(const curlhelp_write_curlbuf * /*header_buf*/, | ||
| 103 | const char msg[static DEFAULT_BUFFER_SIZE], int /*maximum_age*/); | ||
| 104 | size_t get_content_length(const curlhelp_write_curlbuf *header_buf, | ||
| 105 | const curlhelp_write_curlbuf *body_buf); | ||
| 106 | int lookup_host(const char *host, char *buf, size_t buflen, sa_family_t addr_family); | ||
| 107 | CURLcode sslctxfun(CURL *curl, SSL_CTX *sslctx, void *parm); | ||
| 108 | |||
| 109 | #define INET_ADDR_MAX_SIZE INET6_ADDRSTRLEN | ||
| 110 | const char *strrstr2(const char *haystack, const char *needle); | ||
| 111 | |||
| 112 | void cleanup(check_curl_global_state global_state); | ||
| 113 | |||
| 114 | bool expected_statuscode(const char *reply, const char *statuscodes); | ||
| 115 | char *string_statuscode(int major, int minor); | ||
| 116 | |||
| 117 | char *perfd_time(double elapsed_time, thresholds * /*thlds*/, long /*socket_timeout*/); | ||
| 118 | char *perfd_time_connect(double elapsed_time_connect, long /*socket_timeout*/); | ||
| 119 | char *perfd_time_ssl(double elapsed_time_ssl, long /*socket_timeout*/); | ||
| 120 | char *perfd_time_firstbyte(double elapsed_time_firstbyte, long /*socket_timeout*/); | ||
| 121 | char *perfd_time_headers(double elapsed_time_headers, long /*socket_timeout*/); | ||
| 122 | char *perfd_time_transfer(double elapsed_time_transfer, long /*socket_timeout*/); | ||
| 123 | char *perfd_size(size_t page_len, int /*min_page_len*/); | ||
| 124 | |||
| 125 | void test_file(char *path); | ||
diff --git a/plugins/check_curl.d/config.h b/plugins/check_curl.d/config.h index be25d1bb..a4b1eecf 100644 --- a/plugins/check_curl.d/config.h +++ b/plugins/check_curl.d/config.h | |||
| @@ -49,20 +49,7 @@ typedef struct { | |||
| 49 | bool no_body; | 49 | bool no_body; |
| 50 | } check_curl_working_state; | 50 | } check_curl_working_state; |
| 51 | 51 | ||
| 52 | check_curl_working_state check_curl_working_state_init() { | 52 | check_curl_working_state check_curl_working_state_init(); |
| 53 | check_curl_working_state result = { | ||
| 54 | .server_address = NULL, | ||
| 55 | .server_url = DEFAULT_SERVER_URL, | ||
| 56 | .host_name = NULL, | ||
| 57 | .http_method = NULL, | ||
| 58 | .http_post_data = NULL, | ||
| 59 | .virtualPort = 0, | ||
| 60 | .serverPort = HTTP_PORT, | ||
| 61 | .use_ssl = false, | ||
| 62 | .no_body = false, | ||
| 63 | }; | ||
| 64 | return result; | ||
| 65 | } | ||
| 66 | 53 | ||
| 67 | typedef struct { | 54 | typedef struct { |
| 68 | bool automatic_decompression; | 55 | bool automatic_decompression; |
| @@ -123,63 +110,4 @@ typedef struct { | |||
| 123 | bool display_html; | 110 | bool display_html; |
| 124 | } check_curl_config; | 111 | } check_curl_config; |
| 125 | 112 | ||
| 126 | check_curl_config check_curl_config_init() { | 113 | check_curl_config check_curl_config_init(); |
| 127 | check_curl_config tmp = { | ||
| 128 | .initial_config = check_curl_working_state_init(), | ||
| 129 | |||
| 130 | .curl_config = | ||
| 131 | { | ||
| 132 | .automatic_decompression = false, | ||
| 133 | .socket_timeout = DEFAULT_SOCKET_TIMEOUT, | ||
| 134 | .haproxy_protocol = false, | ||
| 135 | .sin_family = AF_UNSPEC, | ||
| 136 | .curl_http_version = CURL_HTTP_VERSION_NONE, | ||
| 137 | .http_opt_headers = NULL, | ||
| 138 | .http_opt_headers_count = 0, | ||
| 139 | .ssl_version = CURL_SSLVERSION_DEFAULT, | ||
| 140 | .client_cert = NULL, | ||
| 141 | .client_privkey = NULL, | ||
| 142 | .ca_cert = NULL, | ||
| 143 | .verify_peer_and_host = false, | ||
| 144 | .user_agent = {'\0'}, | ||
| 145 | .proxy_auth = "", | ||
| 146 | .user_auth = "", | ||
| 147 | .http_content_type = NULL, | ||
| 148 | .cookie_jar_file = NULL, | ||
| 149 | }, | ||
| 150 | .max_depth = DEFAULT_MAX_REDIRS, | ||
| 151 | .followmethod = FOLLOW_HTTP_CURL, | ||
| 152 | .followsticky = STICKY_NONE, | ||
| 153 | |||
| 154 | .maximum_age = -1, | ||
| 155 | .regexp = {}, | ||
| 156 | .compiled_regex = {}, | ||
| 157 | .state_regex = STATE_CRITICAL, | ||
| 158 | .invert_regex = false, | ||
| 159 | .check_cert = false, | ||
| 160 | .continue_after_check_cert = false, | ||
| 161 | .days_till_exp_warn = 0, | ||
| 162 | .days_till_exp_crit = 0, | ||
| 163 | .thlds = NULL, | ||
| 164 | .min_page_len = 0, | ||
| 165 | .max_page_len = 0, | ||
| 166 | .server_expect = | ||
| 167 | { | ||
| 168 | .string = HTTP_EXPECT, | ||
| 169 | .is_present = false, | ||
| 170 | }, | ||
| 171 | .string_expect = "", | ||
| 172 | .header_expect = "", | ||
| 173 | .on_redirect_result_state = STATE_OK, | ||
| 174 | .on_redirect_dependent = true, | ||
| 175 | |||
| 176 | .show_extended_perfdata = false, | ||
| 177 | .show_body = false, | ||
| 178 | .display_html = false, | ||
| 179 | }; | ||
| 180 | |||
| 181 | snprintf(tmp.curl_config.user_agent, DEFAULT_BUFFER_SIZE, "%s/v%s (monitoring-plugins %s, %s)", | ||
| 182 | "check_curl", NP_VERSION, VERSION, curl_version()); | ||
| 183 | |||
| 184 | return tmp; | ||
| 185 | } | ||
