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