summaryrefslogtreecommitdiffstats
path: root/web/attachments/57050-check_http.cookies_patch
blob: d75778c92ee4281add417607f7fa9793b550a386 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
--- nagios-plugins-1.3.1/plugins/check_http.c	2003-06-30 04:56:08.000000000 -0700
+++ nagios-plugins-1.3.1.DS/plugins/check_http.c	2003-07-27 15:26:14.792132000 -0700
@@ -44,7 +44,7 @@
 (-H <vhost> | -I <IP-address>) [-u <uri>] [-p <port>]\n\
             [-w <warn time>] [-c <critical time>] [-t <timeout>] [-L]\n\
             [-a auth] [-f <ok | warn | critcal | follow>] [-e <expect>]\n\
-            [-s string] [-l] [-r <regex> | -R <case-insensitive regex>]\n\
+            [-s string] [-l] [-k] [-r <regex> | -R <case-insensitive regex>]\n\
             [-P string]"
 
 #define LONGOPTIONS "\
@@ -75,6 +75,8 @@
    Wrap output in HTML link (obsoleted by urlize)\n\
  -f, --onredirect=<ok|warning|critical|follow>\n\
    How to handle redirected pages\n%s%s\
+ -k, --keepalive\n\
+   Use Keep-Alive header in every request request\n\
  -v, --verbose\n\
     Show details for command-line debugging (do not use with nagios server)\n\
  -h, --help\n\
@@ -186,6 +188,10 @@
 #define URI_HOST "%[-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]"
 #define URI_PORT ":%[0123456789]"
 #define URI_PATH "%[-_.!~*'();/?:@&=+$,%#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]"
+#define HDR_SETCOOKIE "%*[Ss]%*[Ee]%*[Tt]%*[-]%*[Cc]%*[Oo]%*[Kk]%*[Ii]%*[Ee]: "
+#define COOKIE_BODY "%[/a-zA-Z0-9._-=@,: ]"
+#define COOKIE_PATH ";%[/a-zA-Z0-9._-=@,: ]"
+#define COOKIE_DATE ";%[/a-zA-Z0-9._-=@,: ]"
 enum {
 	MAX_IPV4_HOSTLENGTH = 255,
 	HTTP_PORT = 80,
@@ -206,6 +212,7 @@
 char *server_url = "";
 int server_url_length;
 int server_expect_yn = 0;
+char *cookies=NULL;
 char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT;
 char string_expect[MAX_INPUT_BUFFER] = "";
 double warning_time = 0;
@@ -216,6 +223,7 @@
 int display_html = FALSE;
 int onredirect = STATE_OK;
 int use_ssl = FALSE;
+int keepalive = FALSE;
 int verbose = FALSE;
 int sd;
 char *http_method = "GET";
@@ -309,6 +317,7 @@
 		{"ereg", required_argument, 0, 'r'},
 		{"eregi", required_argument, 0, 'R'},
  		{"linespan", no_argument, 0, 'l'},
+ 		{"keepalive", no_argument, 0, 'k'},
 		{"onredirect", required_argument, 0, 'f'},
 		{"certificate", required_argument, 0, 'C'},
 		{0, 0, 0, 0}
@@ -331,7 +340,7 @@
 			strcpy (argv[c], "-n");
 	}
 
-#define OPTCHARS "Vvht:c:w:H:P:I:a:e:p:s:R:r:u:f:C:nlLS"
+#define OPTCHARS "Vvht:c:w:H:P:I:a:e:p:s:R:r:u:f:C:nlkLS"
 
 	while (1) {
 #ifdef HAVE_GETOPT_H
@@ -466,6 +475,9 @@
 			}
 			break;
 #endif
+		case 'k': /* Keep-Alive */
+			keepalive = TRUE;
+			break;
 		case 'v': /* verbose */
 			verbose = TRUE;
 			break;
@@ -550,6 +562,12 @@
 	char *x = NULL;
 	char *orig_url = NULL;
 	double elapsed_time;
+	char *cpos = "";
+	char cookie_body[255];
+	char cookie_path[255];
+	char cookie_date[255];
+	struct tm tm;
+	char date[255],rest[128];
 #ifdef HAVE_SSL
 	int sslerr;
 #endif
@@ -589,6 +607,16 @@
 	asprintf (&buf, "%sUser-Agent: check_http/%s (nagios-plugins %s)\r\n",
 	          buf, clean_revstring (REVISION), PACKAGE_VERSION);
 
+	/* send Collected Cookies */
+	if(cookies)
+	{
+		asprintf (&buf, "%s%s\r\n", buf,cookies);
+	}
+
+	/* send "Connection: Keep-Alive" if need */
+	if(keepalive)
+		asprintf (&buf, "%sConnection: Keep-Alive\r\n", buf); 
+
 	/* optionally send the authentication info */
 	if (strcmp (user_auth, "")) {
 		auth = base64 (user_auth, strlen (user_auth));
@@ -615,6 +643,9 @@
 	}
 	else {
 #endif
+	if (verbose)
+		printf ("**** REQUEST ****\n%s\n", buf);
+
 		send (sd, buf, strlen (buf), 0);
 #ifdef HAVE_SSL
 	}
@@ -737,6 +768,44 @@
 			if (onredirect == STATE_DEPENDENT) {
 
 				asprintf (&orig_url, "%s", server_url);
+                                cpos = header;
+                                cookies=NULL;
+                                while (*cpos) {
+                                        cookie_date[0]='\0';
+                                        if (sscanf(cpos, HDR_SETCOOKIE COOKIE_BODY COOKIE_PATH COOKIE_DATE, cookie_body, cookie_path, cookie_date)) {
+                                                if(verbose)
+                                                        printf("Cookie parameters: body=%s path=%s date=%s\n", cookie_body, cookie_path, cookie_date);
+
+                                                if(strlen(cookie_date)>1)
+                                                {
+                                                        sscanf(cookie_date, "%*[Ee]%*[Xx]%*[Pp]%*[Ii]%*[Rr]%*[Ee]%*[Ss]=%*s %s %s %*s", date, rest);
+                                                        strcat(date, " ");
+                                                        strcat(date, rest);
+                                                        strptime(date,"%d-%b-%Y %T",&tm);
+
+                                                        if(verbose)
+                                                                 printf("Cookie Date %d-%d-%d Time  %d:%d:%d\n", tm.tm_mday, tm.tm_mon, tm.tm_year, tm.tm_hour, tm.tm_min, tm.tm_sec);
+                                                if(mktime(&tm) < time(0))
+                                                        {
+                                                                if(verbose)
+                                                                        printf("Ignoring cookie since expired\n");
+
+                                                                cpos += (size_t) strcspn (cpos, "\r\n");
+                                                                cpos += (size_t) strspn (cpos, "\r\n");
+                                                                continue;
+                                                        }
+                                                }
+
+                                                if(cookies)
+                                                        asprintf (&cookies, "%s;%s", cookies, cookie_body);
+                                                else
+                                                        asprintf (&cookies, "Cookie: %s", cookie_body);
+                                        }
+
+                                        cpos += (size_t) strcspn (cpos, "\r\n");
+                                        cpos += (size_t) strspn (cpos, "\r\n");
+                                } /* end while (cpos) */
+
 				pos = header;
 				while (pos) {
 					server_address = realloc (server_address, MAX_IPV4_HOSTLENGTH + 1);