File: 19_check_curl_cookie_handling

package info (click to toggle)
monitoring-plugins 2.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 11,704 kB
  • sloc: ansic: 76,177; sh: 13,717; perl: 7,655; makefile: 489
file content (46 lines) | stat: -rw-r--r-- 2,355 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
From 191d15354ba32a483fd4f8017595c7eb82ee5650 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andre=20Kl=C3=A4rner?= <kandre@ak-online.be>
Date: Fri, 29 Nov 2024 10:34:16 +0100
Subject: [PATCH] check_curl: enable internal cookie handling

This enables us to enable curl cookie engine by specifying an empty
filename as the cookie jar file.

This works, since curl's CURLOPT_COOKIEFILE option allows passing an
empty string as filename, which it interprets as a request to enable the
cookie processing. But since CURLOPT_COOKIEJAR would now attempt to
write to a file named by an empty filename, it would break again (or at
least produce a warning in verbose output).

Overall this is allows to handle checking URLs with cookie based
sessions without persisting the cookies to disk, by using the
curl-internal redirect following.
---
 plugins/check_curl.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

--- a/plugins/check_curl.c
+++ b/plugins/check_curl.c
@@ -831,8 +831,11 @@
 
   /* cookie handling */
   if (cookie_jar_file != NULL) {
-    handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_COOKIEJAR, cookie_jar_file), "CURLOPT_COOKIEJAR");
+    /* enable reading cookies from a file, and if the filename is an empty string, only enable the curl cookie engine */
     handle_curl_option_return_code (curl_easy_setopt (curl, CURLOPT_COOKIEFILE, cookie_jar_file), "CURLOPT_COOKIEFILE");
+    /* now enable saving cookies to a file, but only if the filename is not an empty string, since writing it would fail */
+    if (*cookie_jar_file)
+      handle_curl_option_return_code(curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_jar_file), "CURLOPT_COOKIEJAR");
   }
 
   /* do the request */
@@ -2111,6 +2114,9 @@
   printf(" %s\n", "--haproxy-protocol");
   printf("    %s\n", _("Send HAProxy proxy protocol v1 header (CURLOPT_HAPROXYPROTOCOL)."));
   printf (" %s\n", "--cookie-jar=FILE");
+  printf ("    %s\n", _("Specify an empty string as FILE to enable curl's cookie engine without saving"));
+  printf ("    %s\n", _("the cookies to disk. Only enabling the engine without saving to disk requires"));
+  printf ("    %s\n", _("handling multiple requests internally to curl, so use it with --onredirect=curl"));
   printf ("    %s\n", _("Store cookies in the cookie jar and send them out when requested."));
   printf ("\n");