1 const char parsers_rcs[] = "$Id: parsers.c,v 1.36 2001/10/13 12:51:51 joergs Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/parsers.c,v $
6 * Purpose : Declares functions to parse/crunch headers and pages.
7 * Functions declared include:
8 * `add_to_iob', `client_cookie_adder', `client_from',
9 * `client_referrer', `client_send_cookie', `client_ua',
10 * `client_uagent', `client_x_forwarded',
11 * `client_x_forwarded_adder', `client_xtra_adder',
12 * `content_type', `crumble', `destroy_list', `enlist',
13 * `flush_socket', `free_http_request', `get_header',
14 * `list_to_text', `parse_http_request', `sed',
15 * and `server_set_cookie'.
17 * Copyright : Written by and Copyright (C) 2001 the SourceForge
18 * IJBSWA team. http://ijbswa.sourceforge.net
20 * Based on the Internet Junkbuster originally written
21 * by and Copyright (C) 1997 Anonymous Coders and
22 * Junkbusters Corporation. http://www.junkbusters.com
24 * This program is free software; you can redistribute it
25 * and/or modify it under the terms of the GNU General
26 * Public License as published by the Free Software
27 * Foundation; either version 2 of the License, or (at
28 * your option) any later version.
30 * This program is distributed in the hope that it will
31 * be useful, but WITHOUT ANY WARRANTY; without even the
32 * implied warranty of MERCHANTABILITY or FITNESS FOR A
33 * PARTICULAR PURPOSE. See the GNU General Public
34 * License for more details.
36 * The GNU General Public License should be included with
37 * this file. If not, you can view it at
38 * http://www.gnu.org/copyleft/gpl.html
39 * or write to the Free Software Foundation, Inc., 59
40 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 * Revision 1.36 2001/10/13 12:51:51 joergs
45 * Removed client_host, (was only required for the old 2.0.2-11 http://noijb.
46 * force-load), instead crumble Host: and add it (again) in client_host_adder
47 * (in case we get a HTTP/1.0 request without Host: header and forward it to
48 * a HTTP/1.1 server/proxy).
50 * Revision 1.35 2001/10/09 22:39:21 jongfoster
51 * assert.h is also required under Win32, so moving out of #ifndef _WIN32
54 * Revision 1.34 2001/10/07 18:50:55 oes
55 * Added server_content_encoding, renamed server_transfer_encoding
57 * Revision 1.33 2001/10/07 18:04:49 oes
58 * Changed server_http11 to server_http and its pattern to "HTTP".
59 * Additional functionality: it now saves the HTTP status into
60 * csp->http->status and sets CT_TABOO for Status 206 (partial range)
62 * Revision 1.32 2001/10/07 15:43:28 oes
63 * Removed FEATURE_DENY_GZIP and replaced it with client_accept_encoding,
64 * client_te and client_accept_encoding_adder, triggered by the new
65 * +no-compression action. For HTTP/1.1 the Accept-Encoding header is
66 * changed to allow only identity and chunked, and the TE header is
67 * crunched. For HTTP/1.0, Accept-Encoding is crunched.
69 * parse_http_request no longer does anything than parsing. The rewriting
70 * of http->cmd and version mangling are gone. It now also recognizes
71 * the put and delete methods and saves the url in http->url. Removed
74 * renamed content_type and content_length to have the server_ prefix
76 * server_content_type now only works if csp->content_type != CT_TABOO
78 * added server_transfer_encoding, which
79 * - Sets CT_TABOO to prohibit filtering if encoding compresses
80 * - Raises the CSP_FLAG_CHUNKED flag if Encoding is "chunked"
81 * - Change from "chunked" to "identity" if body was chunked
82 * but has been de-chunked for filtering.
84 * added server_content_md5 which crunches any Content-MD5 headers
85 * if the body was modified.
87 * made server_http11 conditional on +downgrade action
89 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
91 * Revision 1.31 2001/10/05 14:25:02 oes
92 * Crumble Keep-Alive from Server
94 * Revision 1.30 2001/09/29 12:56:03 joergs
95 * IJB now changes HTTP/1.1 to HTTP/1.0 in requests and answers.
97 * Revision 1.29 2001/09/24 21:09:24 jongfoster
98 * Fixing 2 memory leaks that Guy spotted, where the paramater to
99 * enlist() was not being free()d.
101 * Revision 1.28 2001/09/22 16:32:28 jongfoster
102 * Removing unused #includes.
104 * Revision 1.27 2001/09/20 15:45:25 steudten
106 * add casting from size_t to int for printf()
107 * remove local variable shadow s2
109 * Revision 1.26 2001/09/16 17:05:14 jongfoster
110 * Removing unused #include showarg.h
112 * Revision 1.25 2001/09/16 13:21:27 jongfoster
113 * Changes to use new list functions.
115 * Revision 1.24 2001/09/13 23:05:50 jongfoster
116 * Changing the string paramater to the header parsers a "const".
118 * Revision 1.23 2001/09/12 18:08:19 steudten
120 * In parse_http_request() header rewriting miss the host value, so
121 * from http://www.mydomain.com the result was just " / " not
122 * http://www.mydomain.com/ in case we forward.
124 * Revision 1.22 2001/09/10 10:58:53 oes
125 * Silenced compiler warnings
127 * Revision 1.21 2001/07/31 14:46:00 oes
128 * - Persistant connections now suppressed
129 * - sed() no longer appends empty header to csp->headers
131 * Revision 1.20 2001/07/30 22:08:36 jongfoster
132 * Tidying up #defines:
133 * - All feature #defines are now of the form FEATURE_xxx
134 * - Permanently turned off WIN_GUI_EDIT
135 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
137 * Revision 1.19 2001/07/25 17:21:54 oes
138 * client_uagent now saves copy of User-Agent: header value
140 * Revision 1.18 2001/07/13 14:02:46 oes
141 * - Included fix to repair broken HTTP requests that
142 * don't contain a path, not even '/'.
143 * - Removed all #ifdef PCRS
144 * - content_type now always inspected and classified as
145 * text, gif or other.
146 * - formatting / comments
148 * Revision 1.17 2001/06/29 21:45:41 oes
149 * Indentation, CRLF->LF, Tab-> Space
151 * Revision 1.16 2001/06/29 13:32:42 oes
153 * - Adapted free_http_request
154 * - Removed logentry from cancelled commit
156 * Revision 1.15 2001/06/03 19:12:38 oes
157 * deleted const struct interceptors
159 * Revision 1.14 2001/06/01 18:49:17 jongfoster
160 * Replaced "list_share" with "list" - the tiny memory gain was not
161 * worth the extra complexity.
163 * Revision 1.13 2001/05/31 21:30:33 jongfoster
164 * Removed list code - it's now in list.[ch]
165 * Renamed "permission" to "action", and changed many features
166 * to use the actions file rather than the global config.
168 * Revision 1.12 2001/05/31 17:33:13 oes
172 * Revision 1.11 2001/05/29 20:11:19 joergs
173 * '/ * inside comment' warning removed.
175 * Revision 1.10 2001/05/29 09:50:24 jongfoster
176 * Unified blocklist/imagelist/permissionslist.
177 * File format is still under discussion, but the internal changes
180 * Also modified interceptor behaviour:
181 * - We now intercept all URLs beginning with one of the following
182 * prefixes (and *only* these prefixes):
184 * * http://ijbswa.sf.net/config/
185 * * http://ijbswa.sourceforge.net/config/
186 * - New interceptors "home page" - go to http://i.j.b/ to see it.
187 * - Internal changes so that intercepted and fast redirect pages
188 * are not replaced with an image.
189 * - Interceptors now have the option to send a binary page direct
190 * to the client. (i.e. ijb-send-banner uses this)
191 * - Implemented show-url-info interceptor. (Which is why I needed
192 * the above interceptors changes - a typical URL is
193 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
194 * The previous mechanism would not have intercepted that, and
195 * if it had been intercepted then it then it would have replaced
198 * Revision 1.9 2001/05/28 17:26:33 jongfoster
199 * Fixing segfault if last header was crunched.
200 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
201 * can use the cross-platform sprintf() instead.)
203 * Revision 1.8 2001/05/27 22:17:04 oes
205 * - re_process_buffer no longer writes the modified buffer
206 * to the client, which was very ugly. It now returns the
207 * buffer, which it is then written by chat.
209 * - content_length now adjusts the Content-Length: header
210 * for modified documents rather than crunch()ing it.
211 * (Length info in csp->content_length, which is 0 for
212 * unmodified documents)
214 * - For this to work, sed() is called twice when filtering.
216 * Revision 1.7 2001/05/27 13:19:06 oes
217 * Patched Joergs solution for the content-length in.
219 * Revision 1.6 2001/05/26 13:39:32 jongfoster
220 * Only crunches Content-Length header if applying RE filtering.
221 * Without this fix, Microsoft Windows Update wouldn't work.
223 * Revision 1.5 2001/05/26 00:28:36 jongfoster
224 * Automatic reloading of config file.
225 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
226 * Most of the global variables have been moved to a new
227 * struct configuration_spec, accessed through csp->config->globalname
228 * Most of the globals remaining are used by the Win32 GUI.
230 * Revision 1.4 2001/05/22 18:46:04 oes
232 * - Enabled filtering banners by size rather than URL
233 * by adding patterns that replace all standard banner
234 * sizes with the "Junkbuster" gif to the re_filterfile
236 * - Enabled filtering WebBugs by providing a pattern
237 * which kills all 1x1 images
239 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
240 * which is selected by the (nonstandard and therefore
241 * capital) letter 'U' in the option string.
242 * It causes the quantifiers to be ungreedy by default.
243 * Appending a ? turns back to greedy (!).
245 * - Added a new interceptor ijb-send-banner, which
246 * sends back the "Junkbuster" gif. Without imagelist or
247 * MSIE detection support, or if tinygif = 1, or the
248 * URL isn't recognized as an imageurl, a lame HTML
249 * explanation is sent instead.
251 * - Added new feature, which permits blocking remote
252 * script redirects and firing back a local redirect
254 * The feature is conditionally compiled, i.e. it
255 * can be disabled with --disable-fast-redirects,
256 * plus it must be activated by a "fast-redirects"
257 * line in the config file, has its own log level
258 * and of course wants to be displayed by show-proxy-args
259 * Note: Boy, all the #ifdefs in 1001 locations and
260 * all the fumbling with configure.in and acconfig.h
261 * were *way* more work than the feature itself :-(
263 * - Because a generic redirect template was needed for
264 * this, tinygif = 3 now uses the same.
266 * - Moved GIFs, and other static HTTP response templates
271 * - Removed some >400 CRs again (Jon, you really worked
274 * Revision 1.3 2001/05/20 01:21:20 jongfoster
275 * Version 2.9.4 checkin.
276 * - Merged popupfile and cookiefile, and added control over PCRS
277 * filtering, in new "permissionsfile".
278 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
279 * file error you now get a message box (in the Win32 GUI) rather
280 * than the program exiting with no explanation.
281 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
283 * - Removed tabs from "config"
284 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
285 * - Bumped up version number.
287 * Revision 1.2 2001/05/17 23:02:36 oes
288 * - Made referrer option accept 'L' as a substitute for '§'
290 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
291 * Initial import of version 2.9.3 source tree
294 *********************************************************************/
301 #include <sys/types.h>
319 #include "jbsockets.h"
320 #include "miscutil.h"
322 const char parsers_h_rcs[] = PARSERS_H_VERSION;
324 /* Fix a problem with Solaris. There should be no effect on other
326 * Solaris's isspace() is a macro which uses it's argument directly
327 * as an array index. Therefore we need to make sure that high-bit
328 * characters generate +ve values, and ideally we also want to make
329 * the argument match the declared parameter type of "int".
331 * Why did they write a character function that can't take a simple
332 * "char" argument? Doh!
334 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
335 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
338 const struct parsers client_patterns[] = {
339 { "referer:", 8, client_referrer },
340 { "user-agent:", 11, client_uagent },
341 { "ua-", 3, client_ua },
342 { "from:", 5, client_from },
343 { "cookie:", 7, client_send_cookie },
344 { "x-forwarded-for:", 16, client_x_forwarded },
345 { "Accept-Encoding:", 16, client_accept_encoding },
346 { "TE:", 3, client_te },
347 #if defined(FEATURE_IMAGE_DETECT_MSIE)
348 { "Accept:", 7, client_accept },
349 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
350 { "Host:", 5, crumble },
351 /* { "if-modified-since:", 18, crumble }, */
352 { "Keep-Alive:", 11, crumble },
353 { "connection:", 11, crumble },
354 { "proxy-connection:", 17, crumble },
359 const struct parsers server_patterns[] = {
360 { "HTTP", 4, server_http },
361 { "set-cookie:", 11, server_set_cookie },
362 { "connection:", 11, crumble },
363 { "Content-Type:", 13, server_content_type },
364 { "Content-Length:", 15, server_content_length },
365 { "Content-MD5:", 12, server_content_md5 },
366 { "Content-Encoding:", 17, server_content_encoding },
367 { "Transfer-Encoding:", 18, server_transfer_coding },
368 { "Keep-Alive:", 11, crumble },
373 void (* const add_client_headers[])(struct client_state *) = {
376 client_x_forwarded_adder,
378 client_accept_encoding_adder,
379 connection_close_adder,
384 void (* const add_server_headers[])(struct client_state *) = {
385 connection_close_adder,
390 /*********************************************************************
392 * Function : flush_socket
394 * Description : Write any pending "buffered" content.
397 * 1 : fd = file descriptor of the socket to read
398 * 2 : csp = Current client state (buffers, headers, etc...)
400 * Returns : On success, the number of bytes written are returned (zero
401 * indicates nothing was written). On error, -1 is returned,
402 * and errno is set appropriately. If count is zero and the
403 * file descriptor refers to a regular file, 0 will be
404 * returned without causing any other effect. For a special
405 * file, the results are not portable.
407 *********************************************************************/
408 int flush_socket(int fd, struct client_state *csp)
410 struct iob *iob = csp->iob;
411 int n = iob->eod - iob->cur;
418 n = write_socket(fd, iob->cur, n);
419 iob->eod = iob->cur = iob->buf;
425 /*********************************************************************
427 * Function : add_to_iob
429 * Description : Add content to the buffered page.
432 * 1 : csp = Current client state (buffers, headers, etc...)
433 * 2 : buf = holds the content to be added to the page
434 * 3 : n = number of bytes to be added
436 * Returns : Number of bytes in the content buffer.
438 *********************************************************************/
439 int add_to_iob(struct client_state *csp, char *buf, int n)
441 struct iob *iob = csp->iob;
445 have = iob->eod - iob->cur;
454 if ((p = (char *)malloc(need + 1)) == NULL)
456 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
462 /* there is something in the buffer - save it */
463 memcpy(p, iob->cur, have);
465 /* replace the buffer with the new space */
469 /* point to the end of the data */
474 /* the buffer is empty, free it and reinitialize */
479 /* copy the new data into the iob buffer */
482 /* point to the end of the data */
485 /* null terminate == cheap insurance */
488 /* set the pointers to the new values */
497 /*********************************************************************
499 * Function : get_header
501 * Description : This (odd) routine will parse the csp->iob
504 * 1 : csp = Current client state (buffers, headers, etc...)
506 * Returns : Any one of the following:
508 * 1) a pointer to a dynamically allocated string that contains a header line
509 * 2) NULL indicating that the end of the header was reached
510 * 3) "" indicating that the end of the iob was reached before finding
511 * a complete header line.
513 *********************************************************************/
514 char *get_header(struct client_state *csp)
520 if ((iob->cur == NULL)
521 || ((p = strchr(iob->cur, '\n')) == NULL))
523 return(""); /* couldn't find a complete header */
528 ret = strdup(iob->cur);
532 if ((q = strchr(ret, '\r'))) *q = '\0';
534 /* is this a blank linke (i.e. the end of the header) ? */
546 /*********************************************************************
550 * Description : add, delete or modify lines in the HTTP header streams.
551 * On entry, it receives a linked list of headers space
552 * that was allocated dynamically (both the list nodes
553 * and the header contents).
555 * As a side effect it frees the space used by the original
559 * 1 : pats = list of patterns to match against headers
560 * 2 : more_headers = list of functions to add more
561 * headers (client or server)
562 * 3 : csp = Current client state (buffers, headers, etc...)
564 * Returns : Single pointer to a fully formed header, or NULL
565 * on out-of-memory error.
567 *********************************************************************/
568 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
570 struct list_entry *p;
571 const struct parsers *v;
575 for (v = pats; v->str ; v++)
577 for (p = csp->headers->first; p ; p = p->next)
579 /* Header crunch()ed in previous run? -> ignore */
580 if (p->str == NULL) continue;
582 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
584 if (strncmpic(p->str, v->str, v->len) == 0)
586 hdr = v->parser(v, p->str, csp);
587 freez((char *)p->str); /* FIXME: Yuck! patching a list...*/
593 /* place any additional headers on the csp->headers list */
594 for (f = more_headers; *f ; f++)
599 hdr = list_to_text(csp->headers);
606 /*********************************************************************
608 * Function : free_http_request
610 * Description : Freez a http_request structure
613 * 1 : http = points to a http_request structure to free
617 *********************************************************************/
618 void free_http_request(struct http_request *http)
626 freez(http->hostport);
629 freez(http->host_ip_addr_str);
630 freez(http->user_agent);
635 /*********************************************************************
637 * Function : parse_http_request
639 * Description : Parse out the host and port from the URL. Find the
640 * hostname & path, port (if ':'), and/or password (if '@')
643 * 1 : req = URL (or is it URI?) to break down
644 * 2 : http = pointer to the http structure to hold elements
645 * 3 : csp = Current client state (buffers, headers, etc...)
649 *********************************************************************/
650 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
652 char *buf, *v[10], *url, *p;
655 memset(http, '\0', sizeof(*http));
656 http->cmd = strdup(req);
659 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
663 /* this could be a CONNECT request */
664 if (strcmpic(v[0], "connect") == 0)
667 http->gpc = strdup(v[0]);
668 http->hostport = strdup(v[1]);
669 http->ver = strdup(v[2]);
672 /* or it could be any other basic HTTP request type */
673 if ((0 == strcmpic(v[0], "get"))
674 || (0 == strcmpic(v[0], "head"))
675 || (0 == strcmpic(v[0], "post"))
676 || (0 == strcmpic(v[0], "put"))
677 || (0 == strcmpic(v[0], "delete"))
679 /* or a webDAV extension (RFC2518) */
680 || (0 == strcmpic(v[0], "propfind"))
681 || (0 == strcmpic(v[0], "proppatch"))
682 || (0 == strcmpic(v[0], "move"))
683 || (0 == strcmpic(v[0], "copy"))
684 || (0 == strcmpic(v[0], "mkcol"))
685 || (0 == strcmpic(v[0], "lock"))
686 || (0 == strcmpic(v[0], "unlock"))
690 http->gpc = strdup(v[0]);
691 http->url = strdup(v[1]);
692 http->ver = strdup(v[2]);
695 if (strncmpic(url, "http://", 7) == 0)
699 else if (strncmpic(url, "https://", 8) == 0)
710 if ((p = strchr(url, '/')))
712 http->path = strdup(p);
714 http->hostport = strdup(url);
717 * Repair broken HTTP requests that don't contain a path
721 http->path = strdup("/");
722 http->hostport = strdup(url);
731 if (http->hostport == NULL)
733 free_http_request(http);
737 buf = strdup(http->hostport);
740 /* check if url contains password */
741 n = ssplit(buf, "@", v, SZ(v), 1, 1);
744 char * newbuf = NULL;
745 newbuf = strdup(v[1]);
750 n = ssplit(buf, ":", v, SZ(v), 1, 1);
754 http->host = strdup(v[0]);
760 http->host = strdup(v[0]);
761 http->port = atoi(v[1]);
766 if (http->host == NULL)
768 free_http_request(http);
771 if (http->path == NULL)
773 http->path = strdup("/");
779 /* here begins the family of parser functions that reformat header lines */
782 /*********************************************************************
786 * Description : This is called if a header matches a pattern to "crunch"
789 * 1 : v = Pointer to parsers structure, which basically holds
790 * headers (client or server) that we want to "crunch"
791 * 2 : s = header (from sed) to "crunch"
792 * 3 : csp = Current client state (buffers, headers, etc...)
794 * Returns : Always NULL.
796 *********************************************************************/
797 char *crumble(const struct parsers *v, const char *s, struct client_state *csp)
799 log_error(LOG_LEVEL_HEADER, "crunch!");
805 /*********************************************************************
807 * Function : server_content_type
809 * Description : Set the content-type for filterable types (text/.*,
810 * javascript and image/gif) unless filtering has been
811 * forbidden (CT_TABOO) while parsing earlier headers.
815 * 2 : s = header string we are "considering"
816 * 3 : csp = Current client state (buffers, headers, etc...)
818 * Returns : A duplicate string pointer to this header (ie. pass thru)
820 *********************************************************************/
821 char *server_content_type(const struct parsers *v, const char *s, struct client_state *csp)
823 if (csp->content_type != CT_TABOO)
825 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
826 csp->content_type = CT_TEXT;
827 else if (strstr(s, " image/gif"))
828 csp->content_type = CT_GIF;
830 csp->content_type = 0;
838 /*********************************************************************
840 * Function : server_transfer_coding
842 * Description : - Prohibit filtering (CT_TABOO) if transfer coding compresses
843 * - Raise the CSP_FLAG_CHUNKED flag if coding is "chunked"
844 * - Change from "chunked" to "identity" if body was chunked
845 * but has been de-chunked for filtering.
849 * 2 : s = header string we are "considering"
850 * 3 : csp = Current client state (buffers, headers, etc...)
852 * Returns : A duplicate string pointer to this header (ie. pass thru)
854 *********************************************************************/
855 char *server_transfer_coding(const struct parsers *v, const char *s, struct client_state *csp)
858 * Turn off pcrs and gif filtering if body compressed
860 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
862 csp->content_type = CT_TABOO;
866 * Raise flag if body chunked
868 if (strstr(s, "chunked"))
870 csp->flags |= CSP_FLAG_CHUNKED;
873 * If the body was modified, it has been
874 * de-chunked first, so adjust the header:
876 if (csp->flags & CSP_FLAG_MODIFIED)
878 return(strdup("Transfer-Encoding: identity"));
887 /*********************************************************************
889 * Function : server_content_encoding
891 * Description : Prohibit filtering (CT_TABOO) if content encoding compresses
895 * 2 : s = header string we are "considering"
896 * 3 : csp = Current client state (buffers, headers, etc...)
898 * Returns : A duplicate string pointer to this header (ie. pass thru)
900 *********************************************************************/
901 char *server_content_encoding(const struct parsers *v, const char *s, struct client_state *csp)
904 * Turn off pcrs and gif filtering if body compressed
906 if (strstr(s, "gzip") || strstr(s, "compress") || strstr(s, "deflate"))
908 csp->content_type = CT_TABOO;
916 /*********************************************************************
918 * Function : server_content_length
920 * Description : Adjust Content-Length header if we modified
925 * 2 : s = header string we are "considering"
926 * 3 : csp = Current client state (buffers, headers, etc...)
928 * Returns : A duplicate string pointer to this header (ie. pass thru)
930 *********************************************************************/
931 char *server_content_length(const struct parsers *v, const char *s, struct client_state *csp)
933 if (csp->content_length != 0) /* Content length has been modified */
935 char * s2 = (char *) zalloc(100);
936 sprintf(s2, "Content-Length: %d", (int) csp->content_length);
938 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", (int) csp->content_length);
949 /*********************************************************************
951 * Function : server_content_md5
953 * Description : Crumble any Content-MD5 headers if the document was
954 * modified. FIXME: Should we re-compute instead?
958 * 2 : s = header string we are "considering"
959 * 3 : csp = Current client state (buffers, headers, etc...)
961 * Returns : A duplicate string pointer to this header (ie. pass thru)
963 *********************************************************************/
964 char *server_content_md5(const struct parsers *v, const char *s, struct client_state *csp)
966 if (csp->flags & CSP_FLAG_MODIFIED)
968 log_error(LOG_LEVEL_HEADER, "Crunching Content-MD5");
979 /*********************************************************************
981 * Function : client_accept_encoding
983 * Description : Rewrite the client's Accept-Encoding header so that
984 * if doesn't allow compression, if the action applies.
985 * Note: For HTTP/1.0 the absence of the header is enough.
989 * 2 : s = header string we are "considering"
990 * 3 : csp = Current client state (buffers, headers, etc...)
992 * Returns : A copy of the client's original or the modified header.
994 *********************************************************************/
995 char *client_accept_encoding(const struct parsers *v, const char *s, struct client_state *csp)
997 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1003 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress content");
1005 if (!strcmpic(csp->http->ver, "HTTP/1.1"))
1007 return(strdup("Accept-Encoding: identity;q=1.0, *;q=0"));
1018 /*********************************************************************
1020 * Function : client_te
1022 * Description : Rewrite the client's TE header so that
1023 * if doesn't allow compression, if the action applies.
1027 * 2 : s = header string we are "considering"
1028 * 3 : csp = Current client state (buffers, headers, etc...)
1030 * Returns : A copy of the client's original or the modified header.
1032 *********************************************************************/
1033 char *client_te(const struct parsers *v, const char *s, struct client_state *csp)
1035 if ((csp->action->flags & ACTION_NO_COMPRESSION) == 0)
1041 log_error(LOG_LEVEL_HEADER, "Supressed offer to compress transfer");
1047 /*********************************************************************
1049 * Function : client_referrer
1051 * Description : Handle the "referer" config setting properly.
1052 * Called from `sed'.
1056 * 2 : s = header (from sed) to "crunch"
1057 * 3 : csp = Current client state (buffers, headers, etc...)
1059 * Returns : NULL if crunched, or a malloc'ed string with the original
1060 * or modified header
1062 *********************************************************************/
1063 char *client_referrer(const struct parsers *v, const char *s, struct client_state *csp)
1065 const char * newval;
1067 #ifdef FEATURE_FORCE_LOAD
1068 /* Since the referrer can include the prefix even
1069 * even if the request itself is non-forced, we must
1070 * clean it unconditionally
1072 strclean(s, FORCE_PREFIX);
1073 #endif /* def FEATURE_FORCE_LOAD */
1075 #ifdef FEATURE_TRUST
1076 csp->referrer = strdup(s);
1077 #endif /* def FEATURE_TRUST */
1080 * Are we sending referer?
1082 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
1087 newval = csp->action->string[ACTION_STRING_REFERER];
1090 * Are we blocking referer?
1092 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1094 log_error(LOG_LEVEL_HEADER, "crunch!");
1099 * Are we forging referer?
1101 if (0 == strcmpic(newval, "forge"))
1104 * Forge a referer as http://[hostname:port of REQUEST]/
1105 * to fool stupid checks for in-site links
1107 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1108 s2 = strsav(NULL, "Referer: ");
1109 s2 = strsav(s2, "http://");
1110 s2 = strsav(s2, csp->http->hostport);
1111 s2 = strsav(s2, "/");
1116 * Have we got a fixed referer?
1118 if (0 == strncmpic(newval, "http://", 7))
1121 * We have a specific (fixed) referer we want to send.
1125 log_error(LOG_LEVEL_HEADER, "modified");
1127 s3 = strsav( NULL, "Referer: " );
1128 s3 = strsav( s3, newval );
1132 /* Should never get here! */
1133 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
1136 * Forge is probably the best default.
1138 * Forge a referer as http://[hostname:port of REQUEST]/
1139 * to fool stupid checks for in-site links
1141 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
1142 s2 = strsav(NULL, "Referer: ");
1143 s2 = strsav(s2, "http://");
1144 s2 = strsav(s2, csp->http->hostport);
1145 s2 = strsav(s2, "/");
1150 /*********************************************************************
1152 * Function : client_uagent
1154 * Description : Handle the "user-agent" config setting properly
1155 * and remember its original value to enable browser
1156 * bug workarounds. Called from `sed'.
1160 * 2 : s = header (from sed) to "crunch"
1161 * 3 : csp = Current client state (buffers, headers, etc...)
1163 * Returns : A malloc'ed pointer to the default agent, or
1164 * a malloc'ed string pointer to this header (ie. pass thru).
1166 *********************************************************************/
1167 char *client_uagent(const struct parsers *v, const char *s, struct client_state *csp)
1169 const char * newval;
1172 /* Save the client's User-Agent: value */
1173 if (strlen(s) >= 12)
1175 csp->http->user_agent = strdup(s + 12);
1178 #ifdef FEATURE_IMAGE_DETECT_MSIE
1179 if (strstr (s, "MSIE "))
1181 /* This is Microsoft Internet Explorer.
1182 * Enable auto-detect.
1184 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
1186 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1188 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1193 newval = csp->action->string[ACTION_STRING_USER_AGENT];
1199 log_error(LOG_LEVEL_HEADER, "modified");
1201 s2 = strsav( NULL, "User-Agent: " );
1202 s2 = strsav( s2, newval );
1208 /*********************************************************************
1210 * Function : client_ua
1212 * Description : Handle "ua-" headers properly. Called from `sed'.
1216 * 2 : s = header (from sed) to "crunch"
1217 * 3 : csp = Current client state (buffers, headers, etc...)
1219 * Returns : NULL if crunched, or a malloc'ed string to original header
1221 *********************************************************************/
1222 char *client_ua(const struct parsers *v, const char *s, struct client_state *csp)
1224 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
1230 log_error(LOG_LEVEL_HEADER, "crunch!");
1236 /*********************************************************************
1238 * Function : client_from
1240 * Description : Handle the "from" config setting properly.
1241 * Called from `sed'.
1245 * 2 : s = header (from sed) to "crunch"
1246 * 3 : csp = Current client state (buffers, headers, etc...)
1248 * Returns : NULL if crunched, or a malloc'ed string to
1249 * modified/original header.
1251 *********************************************************************/
1252 char *client_from(const struct parsers *v, const char *s, struct client_state *csp)
1254 const char * newval;
1257 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1262 newval = csp->action->string[ACTION_STRING_FROM];
1265 * Are we blocking referer?
1267 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1269 log_error(LOG_LEVEL_HEADER, "crunch!");
1273 log_error(LOG_LEVEL_HEADER, " modified");
1275 s2 = strsav( NULL, "From: " );
1276 s2 = strsav( s2, newval );
1282 /*********************************************************************
1284 * Function : client_send_cookie
1286 * Description : Handle the "cookie" header properly. Called from `sed'.
1287 * If cookie is accepted, add it to the cookie_list,
1288 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1291 * 1 : v = pattern of cookie `sed' found matching
1292 * 2 : s = header (from sed) to "crunch"
1293 * 3 : csp = Current client state (buffers, headers, etc...)
1295 * Returns : Always NULL.
1297 *********************************************************************/
1298 char *client_send_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1300 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1302 enlist(csp->cookie_list, s + v->len + 1);
1306 log_error(LOG_LEVEL_HEADER, " crunch!");
1310 * Always return NULL here. The cookie header
1311 * will be sent at the end of the header.
1318 /*********************************************************************
1320 * Function : client_x_forwarded
1322 * Description : Handle the "x-forwarded-for" config setting properly,
1323 * also used in the add_client_headers list. Called from `sed'.
1327 * 2 : s = header (from sed) to "crunch"
1328 * 3 : csp = Current client state (buffers, headers, etc...)
1330 * Returns : Always NULL.
1332 *********************************************************************/
1333 char *client_x_forwarded(const struct parsers *v, const char *s, struct client_state *csp)
1335 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1337 /* Save it so we can re-add it later */
1338 csp->x_forwarded = strdup(s);
1342 * Always return NULL, since this information
1343 * will be sent at the end of the header.
1350 #if defined(FEATURE_IMAGE_DETECT_MSIE)
1351 /*********************************************************************
1353 * Function : client_accept
1355 * Description : Detect whether the client wants HTML or an image.
1356 * Clients do not always make this information available
1357 * in a sane way. Always passes the header through
1358 * the proxy unchanged.
1362 * 2 : s = Header string. Null terminated.
1363 * 3 : csp = Current client state (buffers, headers, etc...)
1365 * Returns : Duplicate of argument s.
1367 *********************************************************************/
1368 char *client_accept(const struct parsers *v, const char *s, struct client_state *csp)
1370 #ifdef FEATURE_IMAGE_DETECT_MSIE
1371 if (strstr (s, "image/gif"))
1373 /* Client will accept HTML. If this seems counterintuitive,
1376 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1380 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1382 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1387 #endif /* defined(FEATURE_IMAGE_DETECT_MSIE) */
1391 /* the following functions add headers directly to the header list */
1394 /*********************************************************************
1396 * Function : client_host_adder
1398 * Description : (re)adds the host header. Called from `sed'.
1401 * 1 : csp = Current client state (buffers, headers, etc...)
1405 *********************************************************************/
1406 void client_host_adder(struct client_state *csp)
1410 p = strsav(p, "Host: ");
1411 p = strsav(p, csp->http->hostport);
1413 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1414 enlist(csp->headers, p);
1420 /*********************************************************************
1422 * Function : client_cookie_adder
1424 * Description : Used in the add_client_headers list. Called from `sed'.
1427 * 1 : csp = Current client state (buffers, headers, etc...)
1431 *********************************************************************/
1432 void client_cookie_adder(struct client_state *csp)
1434 struct list_entry *lst;
1438 for (lst = csp->cookie_list->first; lst ; lst = lst->next)
1442 tmp = strsav(tmp, "; ");
1444 tmp = strsav(tmp, lst->str);
1447 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->first; lst ; lst = lst->next)
1451 tmp = strsav(tmp, "; ");
1454 if ((e = cookie_encode(lst->str)))
1456 tmp = strsav(tmp, e);
1465 ret = strdup("Cookie: ");
1466 ret = strsav(ret, tmp);
1467 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1468 enlist(csp->headers, ret);
1476 /*********************************************************************
1478 * Function : client_accept_encoding_adder
1480 * Description : Add an Accept-Encoding header to the client's request
1481 * that disables compression if the action applies, and
1482 * the header is not already there. Called from `sed'.
1483 * Note: For HTTP/1.0, the absence of the header is enough.
1486 * 1 : csp = Current client state (buffers, headers, etc...)
1490 *********************************************************************/
1491 void client_accept_encoding_adder(struct client_state *csp)
1493 if ( ((csp->action->flags & ACTION_NO_COMPRESSION) != 0)
1494 && (!strcmpic(csp->http->ver, "HTTP/1.1")) )
1496 enlist_unique(csp->headers, "Accept-Encoding: identity;q=1.0, *;q=0", 16);
1502 /*********************************************************************
1504 * Function : client_xtra_adder
1506 * Description : Used in the add_client_headers list. Called from `sed'.
1509 * 1 : csp = Current client state (buffers, headers, etc...)
1513 *********************************************************************/
1514 void client_xtra_adder(struct client_state *csp)
1516 struct list_entry *lst;
1518 for (lst = csp->action->multi[ACTION_MULTI_ADD_HEADER]->first;
1519 lst ; lst = lst->next)
1521 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1522 enlist(csp->headers, lst->str);
1528 /*********************************************************************
1530 * Function : client_x_forwarded_adder
1532 * Description : Used in the add_client_headers list. Called from `sed'.
1535 * 1 : csp = Current client state (buffers, headers, etc...)
1539 *********************************************************************/
1540 void client_x_forwarded_adder(struct client_state *csp)
1544 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1549 if (csp->x_forwarded)
1551 p = strsav(p, csp->x_forwarded);
1552 p = strsav(p, ", ");
1553 p = strsav(p, csp->ip_addr_str);
1557 p = strsav(p, "X-Forwarded-For: ");
1558 p = strsav(p, csp->ip_addr_str);
1561 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1562 enlist(csp->headers, p);
1568 /*********************************************************************
1570 * Function : connection_close_adder
1572 * Description : Adds a "Connection: close" header to csp->headers
1573 * as a temporary fix for the needed but missing HTTP/1.1
1574 * support. Called from `sed'.
1575 * FIXME: This whole function shouldn't be neccessary!
1578 * 1 : csp = Current client state (buffers, headers, etc...)
1582 *********************************************************************/
1583 void connection_close_adder(struct client_state *csp)
1585 enlist(csp->headers, "Connection: close");
1589 /*********************************************************************
1591 * Function : server_http
1593 * Description : - Save the HTTP Status into csp->http->status
1594 * - Set CT_TABOO to prevent filtering if the answer
1595 * is a partial range (HTTP status 206)
1596 * - Rewrite HTTP/1.1 answers to HTTP/1.0 if +downgrade
1600 * 1 : v = parser pattern that matched this header
1601 * 2 : s = header that matched this pattern
1602 * 3 : csp = Current client state (buffers, headers, etc...)
1604 * Returns : Copy of changed or original answer.
1606 *********************************************************************/
1607 char *server_http(const struct parsers *v, const char *s, struct client_state *csp)
1609 char *ret = strdup(s);
1611 sscanf(ret, "HTTP/%*d.%*d %d", &(csp->http->status));
1612 if (csp->http->status == 206)
1614 csp->content_type = CT_TABOO;
1617 if ((csp->action->flags & ACTION_DOWNGRADE) != 0)
1620 log_error(LOG_LEVEL_HEADER, "Downgraded answer to HTTP/1.0");
1627 /*********************************************************************
1629 * Function : server_set_cookie
1631 * Description : Handle the server "cookie" header properly.
1632 * Log cookie to the jar file. Then "crunch" it,
1633 * or accept it. Called from `sed'.
1636 * 1 : v = parser pattern that matched this header
1637 * 2 : s = header that matched this pattern
1638 * 3 : csp = Current client state (buffers, headers, etc...)
1640 * Returns : `crumble' or a newly malloc'ed string.
1642 *********************************************************************/
1643 char *server_set_cookie(const struct parsers *v, const char *s, struct client_state *csp)
1645 #ifdef FEATURE_COOKIE_JAR
1646 if (csp->config->jar)
1648 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1650 #endif /* def FEATURE_COOKIE_JAR */
1652 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1654 return(crumble(v, s, csp));
1662 #ifdef FEATURE_FORCE_LOAD
1663 /*********************************************************************
1665 * Function : strclean
1667 * Description : In-Situ-Eliminate all occurances of substring in
1671 * 1 : string = string to clean
1672 * 2 : substring = substring to eliminate
1674 * Returns : Number of eliminations
1676 *********************************************************************/
1677 int strclean(const char *string, const char *substring)
1679 int hits = 0, len = strlen(substring);
1682 while((pos = strstr(string, substring)))
1689 while (*p++ != '\0');
1696 #endif /* def FEATURE_FORCE_LOAD */