Debian Codegraph @

src/ICAPHeader.cpp

raw
1 //ll support, instructions and copyright go to:
2 // http://e2guardian.org/
3 // Released under the GPL v2, with the OpenSSL exception described in the README file.
4
5 // INCLUDES
6
7 #ifdef HAVE_CONFIG_H
8 #include "e2config.h"
9 #endif
10 #include "ICAPHeader.hpp"
11 #include "Socket.hpp"
12 #include "OptionContainer.hpp"
13 #include "FDTunnel.hpp"
14 #include "Logger.hpp"
15
16 #include <unistd.h>
17 #include <sys/socket.h>
18 #include <exception>
19 #include <time.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <cerrno>
23 #include <zlib.h>
24
25 // GLOBALS
26 extern OptionContainer o;
27
28 // IMPLEMENTATION
29
30 // set timeout for socket operations
31 void ICAPHeader::setTimeout(int t)
32 {
33 timeout = t;
34 }
35
36 bool ICAPHeader::setEncapRecs() {
37 String t = *pencapsulated;
38 DEBUG_icap("pencapsulated is ", t);
39 t = t.after(": ");
40 while ( t.length() ) {
41 String t1 = t.before(",");
42 if (t1 == "")
43 t1 = t;
44 struct encap_rec erec;
45 erec.name = t1.before("=");
46 String t2 = t1.after("=");
47 if (erec.name == "" || t2 == "")
48 return false;
49 erec.value = t2.toInteger();
50 encap_recs.push_back(erec);
51 t = t.after(", ");
52 }
53 return true;
54 }
55
56 // reset header object for future use
57 void ICAPHeader::reset()
58 {
59 HTTPresponse.setType(__HEADER_RESPONSE);
60 HTTPresponse.icap = true;
61 HTTPrequest.icap = true;
62 if (dirty) {
63 header.clear();
64 encap_recs.clear();
65 waspersistent = false;
66 ispersistent = false;
67
68 pproxyconnection = NULL;
69 pencapsulated= NULL;
70 pauthorization = NULL;
71 pallow= NULL;
72 allow_204 = false;
73 pfrom= NULL;
74 phost = NULL;
75 preferer = NULL;
76 puseragent = NULL;
77 pxforwardedfor = NULL;
78 pkeepalive = NULL;
79 pupgrade = NULL;
80 pencapsulated = NULL;
81
82 pproxyauthorization = NULL;
83 pproxyauthenticate = NULL;
84 pcontentdisposition = NULL;
85 pclientip= NULL;
86 pclientuser= NULL;
87
88 req_hdr_flag = false;
89 res_hdr_flag = false;
90 out_req_hdr_flag = false;
91 out_res_hdr_flag = false;
92 out_req_body_flag = false;
93 out_res_body_flag = false;
94 req_body_flag = false;
95 res_body_flag = false;
96 opt_body_flag = false;
97 null_body_flag = false;
98 service_reqmod = false;
99 service_resmod = false;
100 service_options = false;
101 icap_reqmod_service = false;
102 icap_resmod_service = false;
103
104 icap_com.filtergroup = -1;
105 username = "";
106
107 dirty = false;
108
109 }
110 }
111
112 // *
113 // *
114 // * header value and type checks
115 // *
116 // *
117
118 // grab request type (REQMOD, RESPMOD, OPTIONS)
119 String ICAPHeader::requestType()
120 {
121 return header.front().before(" ");
122 }
123
124 // grab return code
125 int ICAPHeader::returnCode() // does not apply to ICAP ? May do if we use for ICAP client
126 {
127 if (header.size() > 0) {
128 return header.front().after(" ").before(" ").toInteger();
129 }else {
130 return 0;
131 }
132 }
133
134 // *
135 // *
136 // * detailed header checks & fixes
137 // *
138 // *
139
140 void ICAPHeader::checkheader(bool allowpersistent)
141 {
142 if (header.size() > 1) {
143 for (std::deque<String>::iterator i = header.begin() + 1; i != header.end(); i++) { // check each line in the headers
144 // index headers - try to perform the checks in the order the average browser sends the headers.
145 // also only do the necessary checks for the header type (sent/received).
146 // Sequencial if else
147 if ((phost == NULL) && i->startsWithLower("host:")) {
148 phost = &(*i);
149 } else if ((pauthorization == NULL) && i->startsWithLower("authorization:")) {
150 pauthorization = &(*i);
151 } else if ((pallow == NULL) && i->startsWithLower("allow:")) {
152 pallow = &(*i);
153 allow_204 = pallow->contains("204");
154 allow_206 = pallow->contains("206");
155 } else if ((pfrom == NULL) && i->startsWithLower("from:")) {
156 pfrom = &(*i);
157 } else if ((phost == NULL) && i->startsWithLower("host:")) {
158 phost = &(*i);
159 } else if ((ppreview == NULL) && i->startsWithLower("preview:")) {
160 ppreview = &(*i);
161 allow_204 = true;
162 } else if ((pkeepalive == NULL) && i->startsWithLower("keep-alive:")) {
163 pkeepalive = &(*i);
164 } else if (i->startsWithLower("encapsulated:")) {
165 pencapsulated = &(*i);
166 setEncapRecs();
167 //i->assign("X-E2G-IgnoreMe: encapuslated always regenerated\r");
168 } else if (i->startsWithLower("x-client-ip:")) {
169 pclientip = &(*i);
170 String t = pclientip->after(": ");
171 t.chop();
172 setClientIP(t);
173 } else if (i->startsWithLower("x-client-username:")) {
174 pclientuser = &(*i);
175 username = pclientuser->after(": ");
176 username.chop();
177 } else if (i->startsWithLower("x-icap-e2g:")) {
178 String t = *i;
179 t.chop(); // remove '\r'
180 t = t.after(":");
181 icap_com.user = t.before(",");
182 t = t.after(",");
183 icap_com.EBG = t.before(",");
184 t = t.after(",");
185 icap_com.filtergroup = t.before(",").toInteger();
186 t = t.after(",");
187 icap_com.mess_no = t.before(",").toInteger();
188 t = t.after(",");
189 icap_com.log_mess_no = t.before(",").toInteger();
190 icap_com.mess_string = t.after(",");
191 }
192
193 String t2 = *i;
194 DEBUG_icap("Header value from ICAP client: ", t2, "allow_204 is ", allow_204, " allow_206 is ", allow_206 );
195
196 }
197 }
198 }
199
200 #ifdef NOTDEF
201 String ICAPHeader::getUrl()
202 {
203 // Version of URL *with* port is not cached,
204 // as vast majority of our code doesn't like
205 // port numbers in URLs.
206 port = 80;
207 bool https = false;
208 String hostname;
209 String userpassword;
210 String answer(header.front().after(" "));
211 answer.removeMultiChar(' ');
212 if (answer.after(" ").startsWith("ICAP/")) {
213 answer = answer.before(" ICAP/");
214 } else {
215 answer = answer.before(" icap/"); // just in case!
216 }
217 if (answer.length()) {
218 if (answer[0] == '/') { // must be the latter above
219 if (phost != NULL) {
220 hostname = phost->after(" ");
221 hostname.removeWhiteSpace();
222 if (hostname.contains(":")) {
223 port = hostname.after(":").toInteger();
224 hostname = hostname.before(":");
225 }
226 while (hostname.endsWith("."))
227 hostname.chop();
228 hostname = "icap://" + hostname;
229 answer = hostname + answer;
230 }
231 header.front() = requestType() + " " + answer + " ICAP/" + header.front().after(" ICAP/");
232 } else { // must be in the form GET http://foo.bar:80/ HTML/1.0
233 if (!answer.after("://").contains("/")) {
234 answer += "/"; // needed later on so correct host is extracted
235 }
236 String protocol(answer.before("://"));
237 hostname = answer.after("://");
238 String url(hostname.after("/"));
239 url.removeWhiteSpace(); // remove rubbish like ^M and blanks
240 if (hostname.endsWith(".")) {
241 hostname.chop();
242 }
243 if (url.length() > 0) {
244 url = "/" + url;
245 }
246 hostname = hostname.before("/"); // extra / was added 4 here
247 if (hostname.contains("@")) { // Contains a username:password combo
248 userpassword = hostname.before("@");
249 hostname = hostname.after("@");
250 }
251 if (hostname.contains(":")) {
252 port = hostname.after(":").toInteger();
253 if (port == 0 || port > 65535) {
254 port = (https ? 443 : 80);
255 }
256 hostname = hostname.before(":"); // chop off the port bit
257 }
258 while (hostname.endsWith("."))
259 hostname.chop();
260 if (userpassword.length())
261 answer = protocol + "://" + userpassword + "@" + hostname + url;
262 else
263 answer = protocol + "://" + hostname + url;
264 }
265 }
266
267 DEBUG_icap("from header url:", answer);
268
269 return answer;
270 }
271
272 String ICAPHeader::url()
273 {
274 return getUrl();
275 }
276 #endif
277
278 // *
279 // *
280 // * URL and Base64 decoding funcs
281 // *
282 // *
283
284
285 // turn %xx back into original character
286 String ICAPHeader::hexToChar(const String &n, bool all)
287 {
288 if (n.length() < 2) {
289 return String(n);
290 }
291 static char buf[2];
292 unsigned int a, b;
293 unsigned char c;
294 a = n[0];
295 b = n[1];
296 if (a >= 'a' && a <= 'f') {
297 a -= 87;
298 } else if (a >= 'A' && a <= 'F') {
299 a -= 55;
300 } else if (a >= '0' && a <= '9') {
301 a -= 48;
302 } else {
303 return String("%") + n;
304 }
305 if (b >= 'a' && b <= 'f') {
306 b -= 87;
307 } else if (b >= 'A' && b <= 'F') {
308 b -= 55;
309 } else if (b >= '0' && b <= '9') {
310 b -= 48;
311 } else {
312 return String("%") + n;
313 }
314 c = a * 16 + b;
315 if (all || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c == '-')) {
316 buf[0] = c;
317 buf[1] = '\0';
318 return String(buf);
319 } else {
320 return String("%") + n;
321 }
322 }
323
324
325
326 // *
327 // *
328 // * network send/receive funcs
329 // *
330 // *
331
332 // send headers out over the given socket
333 bool ICAPHeader::respond(Socket &sock, String res_code, bool echo, bool encap)
334 {
335 DEBUG_icap("ICAP response starting - RCode ", res_code, " echo is ", echo);
336 String l; // for amalgamating to avoid conflict with the Nagel algorithm
337 if(echo) {
338 if (service_reqmod && !(out_res_hdr_flag || out_req_hdr_flag)) {
339 out_req_header = HTTPrequest.stringHeader();
340 DEBUG_icap("out_req_header copied from HTTPrequest :", out_req_header);
341 out_req_hdr_flag = true;
342 out_req_body_flag = req_body_flag;
343 if (req_body > 0)
344 size_req_body = req_body; // TODO Check this!
345 }
346
347 if (service_resmod && !(out_res_hdr_flag)) {
348 out_res_header = HTTPresponse.stringHeader();
349 DEBUG_icap("out_res_header is ", out_res_header);
350 out_res_hdr_flag = true;
351 out_res_body_flag = res_body_flag;
352 if (res_body > 0) {
353 size_res_body = res_body;// TODO Check this!
354 }
355 }
356 }
357
358
359 l = "ICAP/1.0 " + res_code + "\r\n";
360 l += "ISTag:";
361 l += ISTag;
362 l += "\r\n";
363
364 // add ICAP communication header
365 if (service_reqmod && icap_com.filtergroup > -1) {
366 l += "X-ICAP-E2G:";
367 l += icap_com.user;
368 l += ",";
369 l += icap_com.EBG;
370 l += ",";
371 l += std::to_string(icap_com.filtergroup);
372 l += ",";
373 l += std::to_string(icap_com.mess_no);
374 l += ",";
375 l += std::to_string(icap_com.log_mess_no);
376 l += ",";
377 l += icap_com.mess_string;
378 l += "\r\n";
379 }
380
381 if (encap) {
382 // add Encapsulated header logic
383 int offset = 0;
384 String soffset(offset);
385 String sep = " ";
386 l += "Encapsulated:";
387 if (out_req_hdr_flag && (out_req_header.size() > 0)) {
388 l += sep;
389 sep = ", ";
390 l += "req-hdr=";
391 l += soffset;
392 offset += out_req_header.size();
393 soffset = offset;
394 }
395 if (out_res_hdr_flag && out_res_header.size() > 0) {
396 l += sep;
397 sep = ", ";
398 l += "res-hdr=";
399 l += soffset;
400 offset += out_res_header.size();
401 soffset = offset;
402 }
403 if (out_req_body_flag) {
404 l += sep;
405 l += "req-body=";
406 l += soffset;
407 } else if (out_res_body_flag) {
408 l += sep;
409 l += "res-body=";
410 l += soffset;
411 } else {
412 l += sep;
413 l += "null-body=";
414 l += soffset;
415 }
416 l += "\r\n";
417 }
418 l += "\r\n";
419
420 if(encap) {
421 // send headers to the output stream
422 // need exception for bad write
423 if (out_req_hdr_flag) {
424 String temp = out_req_header.toCharArray();
425 l += temp;
426 }
427
428 if (out_res_hdr_flag) {
429 String temp = out_res_header.toCharArray();
430 l += temp;
431 }
432 }
433
434 DEBUG_icap("Icap response header is: ", l);
435 if (!sock.writeToSocket(l.toCharArray(), l.length(), 0, timeout)) {
436 return false;
437 }
438
439 DEBUG_icap("Returning from icapheader:respond");
440
441 return true;
442 }
443
444 bool ICAPHeader::errorResponse(Socket &peerconn, String &res_header, String &res_body) {
445 // set IAP outs and then output ICAP header and res_header/body
446 out_res_header = res_header;
447 out_res_hdr_flag = true;
448 out_req_body_flag = false;
449 if (res_body.length() > 0) {
450 out_res_body = res_body;
451 out_res_body_flag = true;
452 }
453
454 DEBUG_icap("out_res_header: ",out_res_header);
455 DEBUG_icap("out_res_body: ", out_res_body);
456
457
458
459 if (!respond(peerconn))
460 return false;
461 if(out_res_body_flag) {
462 if (!peerconn.writeChunk((char *) res_body.toCharArray(), res_body.length(), timeout))
463 return false;
464 String n;
465 if (!peerconn.writeChunk((char*) n.toCharArray(),0, timeout))
466 return false;
467 }
468 return true;
469 }
470
471 void ICAPHeader::setClientIP(String &ip) {
472 clientip = ip;
473 HTTPrequest.setClientIP(ip);
474 }
475
476 bool ICAPHeader::in(Socket *sock, bool allowpersistent)
477 {
478 if (dirty)
479 reset();
480 dirty = true;
481
482 #ifdef DEBUG_HIGH
483 if(is_response) {
484 DEBUG_icap("Start of response ICAPheader:in");
485 } else {
486 DEBUG_icap("Start of request ICAPheader:in");
487 }
488 #endif
489
490 // the RFCs don't specify a max header line length so this should be
491 // dynamic really. Pointed out (well reminded actually) by Daniel Robbins
492 char buff[32768]; // setup a buffer to hold the incomming ICAP line
493 String line; // temp store to hold the line after processing
494 line = "----"; // so we get past the first while
495 bool firsttime = true;
496 bool discard = false;
497 while (line.length() > 3 || discard) { // loop until the stream is
498 // failed or we get to the end of the header (a line by itself)
499
500 // get a line of header from the stream
501 bool truncated = false;
502 int rc;
503 if (firsttime) {
504
505 DEBUG_icap("ICAPheader:in before getLine - timeout: ", timeout);
506
507 rc = sock->getLine(buff, 32768, timeout, NULL, &truncated);
508 DEBUG_icap("firstime: ICAPheader:in after getLine ");
509
510 if (rc == 0) return false;
511 if (rc < 0 || truncated) {
512 ispersistent = false;
513 DEBUG_icap("firstime: ICAPheader:in after getLine: rc: ", rc, " truncated: ", truncated );
514 return false;
515 }
516 } else {
517 rc = sock->getLine(buff, 32768, timeout, NULL,
518 &truncated); // timeout reduced to 100ms for lines after first
519 if (rc == 0) return false;
520 if (rc < 0 || truncated) {
521 ispersistent = false;
522 DEBUG_icap("not firstime: ICAPheader:in after getLine: rc: ", rc, " truncated: ", truncated );
523 return false; // do not allow non-terminated headers
524 }
525
526 }
527
528 if (header.size() > o.header.max_header_lines) {
529 DEBUG_icap("ICAP header:size too big = ", header.size() );
530 E2LOGGER_info(" header:size too big: ", header.size(), ", see maxheaderlines");
531 ispersistent = false;
532 return false;
533 }
534
535 // throw std::exception();
536
537 // getline will throw an exception if there is an error which will
538 // only be caught by HandleConnection() ?????????????????????
539
540 if (rc > 0) line = buff;
541 else line = "";// convert the line to a String
542
543 if (firsttime) {
544 // check first line header
545 if (is_response) {
546 if (!(line.length() > 11 && line.startsWith("ICAP/") &&
547 (line.after(" ").before(" ").toInteger() > 99))) {
548 if (o.conn.logconerror) {
549 E2LOGGER_error("Server did not respond with ICAP");
550 }
551 DEBUG_icap("Returning from header:in Server did not respond with ICAP length: ", line.length(), " content: ", line );
552 return false;
553 }
554 } else {
555 method = line.before(" ");
556 DEBUG_icap("Returning from header:in client requests with ICAP length: ", line.length(), " content: ", line);
557 String t = line.after(" ").before(" ");
558 DEBUG_icap("Request is ", t, " size: ", line.length(), " content: ", line);
559 if (t.startsWith("icap://")) {
560 // valid protocol
561 } else {
562 icap_error = "400 Bad Request";
563 DEBUG_icap("Request error is: ", icap_error, " Line: ", t);
564 return false;
565 }
566 t = t.after("//").after("/");
567 if (t == o.icap.icap_reqmod_url) {
568 icap_reqmod_service = true;
569 } else if (t == o.icap.icap_resmod_url) {
570 icap_resmod_service = true;
571 } else {
572 icap_error = "404 ICAP Service not found";
573 }
574 if (method == "OPTIONS") {
575 service_options = true;
576 } else if (icap_reqmod_service && method == "REQMOD") {
577 service_reqmod = true;
578 } else if (icap_resmod_service && method == "RESPMOD") {
579 service_resmod = true;
580 } else {
581 icap_error = "405 Method not allowed for service";
582 }
583
584 DEBUG_icap("Request method is: ", method, " error?: ", icap_error, " url value: ", t);
585 }
586 }
587 // ignore crap left in buffer from old pconns (in particular, the IE "extra CRLF after POST" bug)
588 discard = false;
589 if (not(firsttime && line.length() <= 3)) {
590 header.push_back(line); // stick the line in the deque that holds the header
591 } else {
592 discard = true;
593 DEBUG_icap("Discarding unwanted bytes at head of request (pconn closed or IE multipart POST bug)");
594 }
595 firsttime = false;
596 // End of while
597 }
598
599
600 if (header.size() == 0) {
601 DEBUG_icap("ICAP header:size = 0 ");
602 return false;
603 }
604
605 header.pop_back(); // remove the final blank line of a header
606 checkheader(allowpersistent); // sort out a few bits in the header
607 DEBUG_icap("checkheader done- ", encap_recs.size(), " encap_recs");
608 //now need to get http req and res headers - if present
609 HTTPrequest.reset();
610 HTTPresponse.reset();
611 if(encap_recs.size() > 0) {
612 for (std::deque<encap_rec>::iterator i = encap_recs.begin(); i < encap_recs.end(); i++) {
613 if (i->name == "req-hdr") {
614 req_hdr_flag = HTTPrequest.in(sock);
615 if (!req_hdr_flag)
616 return false;
617 req_hdr = i->value;
618 } else if (i->name == "res-hdr") {
619 res_hdr_flag = HTTPresponse.in(sock);
620 if (!res_hdr_flag)
621 return false;
622 res_hdr = i->value;
623 } else if (i->name == "req-body") {
624 req_body_flag = true;
625 req_body = i->value;
626 } else if (i->name == "res-body") {
627 res_body_flag = true;
628 res_body = i->value;
629 } else if (i->name == "null-body") {
630 null_body_flag = true;
631 null_body = i->value;
632 } else if (i->name == "opt-body") { // may not need this as only sent in respone
633 opt_body_flag = true;
634 opt_body = i->value;
635 }
636 // add further checking in here for REQMOD, RESPMOD and OPTIONS
637 }
638 }
639
640
641 return true;
642 }
643
644 void ICAPHeader::set_icap_com(std::string &user, String EBG, int &filtergroup, int &mess_no, int &log_mess_no, std::string &mess) {
645 icap_com.user = user;
646 icap_com.EBG = EBG;
647 icap_com.filtergroup = filtergroup;
648 icap_com.mess_no = mess_no;
649 icap_com.log_mess_no = log_mess_no;
650 icap_com.mess_string = mess;
651 }