1 const char parsers_rcs[] = "$Id: parsers.c,v 1.18 2001/07/13 14:02:46 oes 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.18 2001/07/13 14:02:46 oes
45 * - Included fix to repair broken HTTP requests that
46 * don't contain a path, not even '/'.
47 * - Removed all #ifdef PCRS
48 * - content_type now always inspected and classified as
50 * - formatting / comments
52 * Revision 1.17 2001/06/29 21:45:41 oes
53 * Indentation, CRLF->LF, Tab-> Space
55 * Revision 1.16 2001/06/29 13:32:42 oes
57 * - Adapted free_http_request
58 * - Removed logentry from cancelled commit
60 * Revision 1.15 2001/06/03 19:12:38 oes
61 * deleted const struct interceptors
63 * Revision 1.14 2001/06/01 18:49:17 jongfoster
64 * Replaced "list_share" with "list" - the tiny memory gain was not
65 * worth the extra complexity.
67 * Revision 1.13 2001/05/31 21:30:33 jongfoster
68 * Removed list code - it's now in list.[ch]
69 * Renamed "permission" to "action", and changed many features
70 * to use the actions file rather than the global config.
72 * Revision 1.12 2001/05/31 17:33:13 oes
76 * Revision 1.11 2001/05/29 20:11:19 joergs
77 * '/* inside comment' warning removed.
79 * Revision 1.10 2001/05/29 09:50:24 jongfoster
80 * Unified blocklist/imagelist/permissionslist.
81 * File format is still under discussion, but the internal changes
84 * Also modified interceptor behaviour:
85 * - We now intercept all URLs beginning with one of the following
86 * prefixes (and *only* these prefixes):
88 * * http://ijbswa.sf.net/config/
89 * * http://ijbswa.sourceforge.net/config/
90 * - New interceptors "home page" - go to http://i.j.b/ to see it.
91 * - Internal changes so that intercepted and fast redirect pages
92 * are not replaced with an image.
93 * - Interceptors now have the option to send a binary page direct
94 * to the client. (i.e. ijb-send-banner uses this)
95 * - Implemented show-url-info interceptor. (Which is why I needed
96 * the above interceptors changes - a typical URL is
97 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
98 * The previous mechanism would not have intercepted that, and
99 * if it had been intercepted then it then it would have replaced
102 * Revision 1.9 2001/05/28 17:26:33 jongfoster
103 * Fixing segfault if last header was crunched.
104 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
105 * can use the cross-platform sprintf() instead.)
107 * Revision 1.8 2001/05/27 22:17:04 oes
109 * - re_process_buffer no longer writes the modified buffer
110 * to the client, which was very ugly. It now returns the
111 * buffer, which it is then written by chat.
113 * - content_length now adjusts the Content-Length: header
114 * for modified documents rather than crunch()ing it.
115 * (Length info in csp->content_length, which is 0 for
116 * unmodified documents)
118 * - For this to work, sed() is called twice when filtering.
120 * Revision 1.7 2001/05/27 13:19:06 oes
121 * Patched Joergs solution for the content-length in.
123 * Revision 1.6 2001/05/26 13:39:32 jongfoster
124 * Only crunches Content-Length header if applying RE filtering.
125 * Without this fix, Microsoft Windows Update wouldn't work.
127 * Revision 1.5 2001/05/26 00:28:36 jongfoster
128 * Automatic reloading of config file.
129 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
130 * Most of the global variables have been moved to a new
131 * struct configuration_spec, accessed through csp->config->globalname
132 * Most of the globals remaining are used by the Win32 GUI.
134 * Revision 1.4 2001/05/22 18:46:04 oes
136 * - Enabled filtering banners by size rather than URL
137 * by adding patterns that replace all standard banner
138 * sizes with the "Junkbuster" gif to the re_filterfile
140 * - Enabled filtering WebBugs by providing a pattern
141 * which kills all 1x1 images
143 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
144 * which is selected by the (nonstandard and therefore
145 * capital) letter 'U' in the option string.
146 * It causes the quantifiers to be ungreedy by default.
147 * Appending a ? turns back to greedy (!).
149 * - Added a new interceptor ijb-send-banner, which
150 * sends back the "Junkbuster" gif. Without imagelist or
151 * MSIE detection support, or if tinygif = 1, or the
152 * URL isn't recognized as an imageurl, a lame HTML
153 * explanation is sent instead.
155 * - Added new feature, which permits blocking remote
156 * script redirects and firing back a local redirect
158 * The feature is conditionally compiled, i.e. it
159 * can be disabled with --disable-fast-redirects,
160 * plus it must be activated by a "fast-redirects"
161 * line in the config file, has its own log level
162 * and of course wants to be displayed by show-proxy-args
163 * Note: Boy, all the #ifdefs in 1001 locations and
164 * all the fumbling with configure.in and acconfig.h
165 * were *way* more work than the feature itself :-(
167 * - Because a generic redirect template was needed for
168 * this, tinygif = 3 now uses the same.
170 * - Moved GIFs, and other static HTTP response templates
175 * - Removed some >400 CRs again (Jon, you really worked
178 * Revision 1.3 2001/05/20 01:21:20 jongfoster
179 * Version 2.9.4 checkin.
180 * - Merged popupfile and cookiefile, and added control over PCRS
181 * filtering, in new "permissionsfile".
182 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
183 * file error you now get a message box (in the Win32 GUI) rather
184 * than the program exiting with no explanation.
185 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
187 * - Removed tabs from "config"
188 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
189 * - Bumped up version number.
191 * Revision 1.2 2001/05/17 23:02:36 oes
192 * - Made referrer option accept 'L' as a substitute for 'ยง'
194 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
195 * Initial import of version 2.9.3 source tree
198 *********************************************************************/
204 #include <sys/types.h>
219 #include "showargs.h"
223 #include "jbsockets.h"
224 #include "miscutil.h"
227 const char parsers_h_rcs[] = PARSERS_H_VERSION;
229 /* Fix a problem with Solaris. There should be no effect on other
231 * Solaris's isspace() is a macro which uses it's argument directly
232 * as an array index. Therefore we need to make sure that high-bit
233 * characters generate +ve values, and ideally we also want to make
234 * the argument match the declared parameter type of "int".
236 * Why did they write a character function that can't take a simple
237 * "char" argument? Doh!
239 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
240 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
243 const struct parsers client_patterns[] = {
244 { "referer:", 8, client_referrer },
245 { "user-agent:", 11, client_uagent },
246 { "ua-", 3, client_ua },
247 { "from:", 5, client_from },
248 { "cookie:", 7, client_send_cookie },
249 { "x-forwarded-for:", 16, client_x_forwarded },
250 { "proxy-connection:", 17, crumble },
252 { "Accept-Encoding: gzip", 21, crumble },
253 #endif /* def DENY_GZIP */
254 #if defined(DETECT_MSIE_IMAGES)
255 { "Accept:", 7, client_accept },
256 #endif /* defined(DETECT_MSIE_IMAGES) */
258 { "Host:", 5, client_host },
259 #endif /* def FORCE_LOAD */
260 /* { "if-modified-since:", 18, crumble }, */
265 const struct parsers server_patterns[] = {
266 { "set-cookie:", 11, server_set_cookie },
267 { "connection:", 11, crumble },
268 { "Content-Type:", 13, content_type },
269 { "Content-Length:", 15, content_length },
274 void (* const add_client_headers[])(struct client_state *) = {
276 client_x_forwarded_adder,
282 void (* const add_server_headers[])(struct client_state *) = {
287 /*********************************************************************
289 * Function : flush_socket
291 * Description : Write any pending "buffered" content.
294 * 1 : fd = file descriptor of the socket to read
295 * 2 : csp = Current client state (buffers, headers, etc...)
297 * Returns : On success, the number of bytes written are returned (zero
298 * indicates nothing was written). On error, -1 is returned,
299 * and errno is set appropriately. If count is zero and the
300 * file descriptor refers to a regular file, 0 will be
301 * returned without causing any other effect. For a special
302 * file, the results are not portable.
304 *********************************************************************/
305 int flush_socket(int fd, struct client_state *csp)
307 struct iob *iob = csp->iob;
308 int n = iob->eod - iob->cur;
315 n = write_socket(fd, iob->cur, n);
316 iob->eod = iob->cur = iob->buf;
322 /*********************************************************************
324 * Function : add_to_iob
326 * Description : Add content to the buffered page.
329 * 1 : csp = Current client state (buffers, headers, etc...)
330 * 2 : buf = holds the content to be added to the page
331 * 3 : n = number of bytes to be added
333 * Returns : Number of bytes in the content buffer.
335 *********************************************************************/
336 int add_to_iob(struct client_state *csp, char *buf, int n)
338 struct iob *iob = csp->iob;
342 have = iob->eod - iob->cur;
351 if ((p = (char *)malloc(need + 1)) == NULL)
353 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
359 /* there is something in the buffer - save it */
360 memcpy(p, iob->cur, have);
362 /* replace the buffer with the new space */
366 /* point to the end of the data */
371 /* the buffer is empty, free it and reinitialize */
376 /* copy the new data into the iob buffer */
379 /* point to the end of the data */
382 /* null terminate == cheap insurance */
385 /* set the pointers to the new values */
394 /*********************************************************************
396 * Function : get_header
398 * Description : This (odd) routine will parse the csp->iob
401 * 1 : csp = Current client state (buffers, headers, etc...)
403 * Returns : Any one of the following:
405 * 1) a pointer to a dynamically allocated string that contains a header line
406 * 2) NULL indicating that the end of the header was reached
407 * 3) "" indicating that the end of the iob was reached before finding
408 * a complete header line.
410 *********************************************************************/
411 char *get_header(struct client_state *csp)
417 if ((iob->cur == NULL)
418 || ((p = strchr(iob->cur, '\n')) == NULL))
420 return(""); /* couldn't find a complete header */
425 ret = strdup(iob->cur);
429 if ((q = strchr(ret, '\r'))) *q = '\0';
431 /* is this a blank linke (i.e. the end of the header) ? */
443 /*********************************************************************
447 * Description : add, delete or modify lines in the HTTP header streams.
448 * On entry, it receives a linked list of headers space
449 * that was allocated dynamically (both the list nodes
450 * and the header contents).
452 * As a side effect it frees the space used by the original
456 * 1 : pats = list of patterns to match against headers
457 * 2 : more_headers = list of functions to add more
458 * headers (client or server)
459 * 3 : csp = Current client state (buffers, headers, etc...)
461 * Returns : Single pointer to a fully formed header.
463 *********************************************************************/
464 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
467 const struct parsers *v;
471 for (v = pats; v->str ; v++)
473 for (p = csp->headers->next; p ; p = p->next)
475 /* Header crunch()ed in previous run? -> ignore */
476 if (p->str == NULL) continue;
478 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
480 if (strncmpic(p->str, v->str, v->len) == 0)
482 hdr = v->parser(v, p->str, csp);
489 /* place any additional headers on the csp->headers list */
490 for (f = more_headers; *f ; f++)
495 /* add the blank line at the end of the header, if necessary */
496 if ( (csp->headers->last == NULL)
497 || (csp->headers->last->str == NULL)
498 || (*csp->headers->last->str != '\0') )
500 enlist(csp->headers, "");
503 hdr = list_to_text(csp->headers);
510 /*********************************************************************
512 * Function : free_http_request
514 * Description : Freez a http_request structure
517 * 1 : http = points to a http_request structure to free
521 *********************************************************************/
522 void free_http_request(struct http_request *http)
527 freez(http->hostport);
530 freez(http->host_ip_addr_str);
531 freez(http->user_agent);
536 /*********************************************************************
538 * Function : parse_http_request
540 * Description : Parse out the host and port from the URL. Find the
541 * hostname & path, port (if ':'), and/or password (if '@')
544 * 1 : req = URL (or is it URI?) to break down
545 * 2 : http = pointer to the http structure to hold elements
546 * 3 : csp = Current client state (buffers, headers, etc...)
550 *********************************************************************/
551 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
553 char *buf, *v[10], *url, *p;
556 memset(http, '\0', sizeof(*http));
558 http->cmd = strdup(req);
561 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
565 /* this could be a CONNECT request */
566 if (strcmpic(v[0], "connect") == 0)
569 http->gpc = strdup(v[0]);
570 http->hostport = strdup(v[1]);
571 http->ver = strdup(v[2]);
576 /* This next line is a little ugly, but it simplifies the if statement below. */
577 /* Basically if using webDAV, we want the OR condition to use these too. */
581 * These are the headers as defined in RFC2518 to add webDAV support
584 #define OR_WEBDAV || \
585 (0 == strcmpic(v[0], "propfind")) || \
586 (0 == strcmpic(v[0], "proppatch")) || \
587 (0 == strcmpic(v[0], "move")) || \
588 (0 == strcmpic(v[0], "copy")) || \
589 (0 == strcmpic(v[0], "mkcol")) || \
590 (0 == strcmpic(v[0], "lock")) || \
591 (0 == strcmpic(v[0], "unlock"))
593 #else /* No webDAV support is enabled. Provide an empty OR_WEBDAV macro. */
599 /* or it could be a GET or a POST (possibly webDAV too) */
600 if ((strcmpic(v[0], "get") == 0) ||
601 (strcmpic(v[0], "head") == 0) OR_WEBDAV ||
602 (strcmpic(v[0], "post") == 0))
605 http->gpc = strdup(v[0]);
607 http->ver = strdup(v[2]);
609 if (strncmpic(url, "http://", 7) == 0)
613 else if (strncmpic(url, "https://", 8) == 0)
624 if (p = strchr(url, '/'))
626 http->path = strdup(p);
628 http->hostport = strdup(url);
631 * Repair broken HTTP requests that don't contain a path
635 /* Repair hostport & path */
636 http->path = strdup("/");
637 http->hostport = strdup(url);
639 /* Even repair cmd in case we're just forwarding. Boy are we nice ;-) */
641 http->cmd = strsav(http->cmd, http->gpc);
642 http->cmd = strsav(http->cmd, " / ");
643 http->cmd = strsav(http->cmd, http->ver);
652 if (http->hostport == NULL)
654 free_http_request(http);
658 buf = strdup(http->hostport);
661 /* check if url contains password */
662 n = ssplit(buf, "@", v, SZ(v), 1, 1);
665 char * newbuf = NULL;
666 newbuf = strdup(v[1]);
671 n = ssplit(buf, ":", v, SZ(v), 1, 1);
675 http->host = strdup(v[0]);
681 http->host = strdup(v[0]);
682 http->port = atoi(v[1]);
687 if (http->host == NULL)
689 free_http_request(http);
692 if (http->path == NULL)
694 http->path = strdup("");
700 /* here begins the family of parser functions that reformat header lines */
703 /*********************************************************************
707 * Description : This is called if a header matches a pattern to "crunch"
710 * 1 : v = Pointer to parsers structure, which basically holds
711 * headers (client or server) that we want to "crunch"
712 * 2 : s = header (from sed) to "crunch"
713 * 3 : csp = Current client state (buffers, headers, etc...)
715 * Returns : Always NULL.
717 *********************************************************************/
718 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
720 log_error(LOG_LEVEL_HEADER, "crunch!");
726 /*********************************************************************
728 * Function : content_type
730 * Description : Is this a text/.* or javascript MIME Type?
734 * 2 : s = header string we are "considering"
735 * 3 : csp = Current client state (buffers, headers, etc...)
737 * Returns : A duplicate string pointer to this header (ie. pass thru)
739 *********************************************************************/
740 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
742 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
743 csp->content_type = CT_TEXT;
744 else if (strstr(s, " image/gif"))
745 csp->content_type = CT_GIF;
747 csp->content_type = 0;
754 /*********************************************************************
756 * Function : content_length
758 * Description : Adjust Content-Length header if we modified
763 * 2 : s = header string we are "considering"
764 * 3 : csp = Current client state (buffers, headers, etc...)
766 * Returns : A duplicate string pointer to this header (ie. pass thru)
768 *********************************************************************/
769 char *content_length(const struct parsers *v, char *s, struct client_state *csp)
771 if (csp->content_length != 0) /* Content has been modified */
773 s = (char *) zalloc(100);
774 sprintf(s, "Content-Length: %d", csp->content_length);
776 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
787 /*********************************************************************
789 * Function : client_referrer
791 * Description : Handle the "referer" config setting properly.
796 * 2 : s = header (from sed) to "crunch"
797 * 3 : csp = Current client state (buffers, headers, etc...)
799 * Returns : NULL if crunched, or a malloc'ed string with the original
802 *********************************************************************/
803 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
807 /* Since the referrer can include the prefix even
808 * even if the request itself is non-forced, we must
809 * clean it unconditionally
811 strclean(s, FORCE_PREFIX);
812 #endif /* def FORCE_LOAD */
815 csp->referrer = strdup(s);
816 #endif /* def TRUST_FILES */
819 * Are we sending referer?
821 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
826 newval = csp->action->string[ACTION_STRING_REFERER];
829 * Are we blocking referer?
831 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
833 log_error(LOG_LEVEL_HEADER, "crunch!");
838 * Are we forging referer?
840 if (0 == strcmpic(newval, "forge"))
843 * Forge a referer as http://[hostname:port of REQUEST]/
844 * to fool stupid checks for in-site links
846 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
847 s = strsav(NULL, "Referer: ");
848 s = strsav(s, "http://");
849 s = strsav(s, csp->http->hostport);
855 * Have we got a fixed referer?
857 if (0 == strncmpic(newval, "http://", 7))
860 * We have a specific (fixed) referer we want to send.
863 log_error(LOG_LEVEL_HEADER, "modified");
865 s = strsav( NULL, "Referer: " );
866 s = strsav( s, newval );
870 /* Should never get here! */
871 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
874 * Forge is probably the best default.
876 * Forge a referer as http://[hostname:port of REQUEST]/
877 * to fool stupid checks for in-site links
879 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
880 s = strsav(NULL, "Referer: ");
881 s = strsav(s, "http://");
882 s = strsav(s, csp->http->hostport);
888 /*********************************************************************
890 * Function : client_uagent
892 * Description : Handle the "user-agent" config setting properly
893 * and remember its original value to enable browser
894 * bug workarounds. Called from `sed'.
898 * 2 : s = header (from sed) to "crunch"
899 * 3 : csp = Current client state (buffers, headers, etc...)
901 * Returns : A malloc'ed pointer to the default agent, or
902 * a malloc'ed string pointer to this header (ie. pass thru).
904 *********************************************************************/
905 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
909 /* Save the client's User-Agent: value */
912 csp->http->user_agent = strdup(s + 12);
915 #ifdef DETECT_MSIE_IMAGES
916 if (strstr (s, "MSIE "))
918 /* This is Microsoft Internet Explorer.
919 * Enable auto-detect.
921 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
923 #endif /* def DETECT_MSIE_IMAGES */
925 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
930 newval = csp->action->string[ACTION_STRING_USER_AGENT];
936 log_error(LOG_LEVEL_HEADER, "modified");
938 s = strsav( NULL, "User-Agent: " );
939 s = strsav( s, newval );
945 /*********************************************************************
947 * Function : client_ua
949 * Description : Handle "ua-" headers properly. Called from `sed'.
953 * 2 : s = header (from sed) to "crunch"
954 * 3 : csp = Current client state (buffers, headers, etc...)
956 * Returns : NULL if crunched, or a malloc'ed string to original header
958 *********************************************************************/
959 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
961 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
967 log_error(LOG_LEVEL_HEADER, "crunch!");
973 /*********************************************************************
975 * Function : client_from
977 * Description : Handle the "from" config setting properly.
982 * 2 : s = header (from sed) to "crunch"
983 * 3 : csp = Current client state (buffers, headers, etc...)
985 * Returns : NULL if crunched, or a malloc'ed string to
986 * modified/original header.
988 *********************************************************************/
989 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
993 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
998 newval = csp->action->string[ACTION_STRING_FROM];
1001 * Are we blocking referer?
1003 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1005 log_error(LOG_LEVEL_HEADER, "crunch!");
1009 log_error(LOG_LEVEL_HEADER, " modified");
1011 s = strsav( NULL, "From: " );
1012 s = strsav( s, newval );
1018 /*********************************************************************
1020 * Function : client_send_cookie
1022 * Description : Handle the "cookie" header properly. Called from `sed'.
1023 * If cookie is accepted, add it to the cookie_list,
1024 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1027 * 1 : v = pattern of cookie `sed' found matching
1028 * 2 : s = header (from sed) to "crunch"
1029 * 3 : csp = Current client state (buffers, headers, etc...)
1031 * Returns : Always NULL.
1033 *********************************************************************/
1034 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
1036 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1038 enlist(csp->cookie_list, s + v->len + 1);
1042 log_error(LOG_LEVEL_HEADER, " crunch!");
1046 * Always return NULL here. The cookie header
1047 * will be sent at the end of the header.
1054 /*********************************************************************
1056 * Function : client_x_forwarded
1058 * Description : Handle the "x-forwarded-for" config setting properly,
1059 * also used in the add_client_headers list. Called from `sed'.
1063 * 2 : s = header (from sed) to "crunch"
1064 * 3 : csp = Current client state (buffers, headers, etc...)
1066 * Returns : Always NULL.
1068 *********************************************************************/
1069 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1071 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1073 /* Save it so we can re-add it later */
1074 csp->x_forwarded = strdup(s);
1078 * Always return NULL, since this information
1079 * will be sent at the end of the header.
1086 #if defined(DETECT_MSIE_IMAGES)
1087 /*********************************************************************
1089 * Function : client_accept
1091 * Description : Detect whether the client wants HTML or an image.
1092 * Clients do not always make this information available
1093 * in a sane way. Always passes the header through
1094 * the proxy unchanged.
1098 * 2 : s = Header string. Null terminated.
1099 * 3 : csp = Current client state (buffers, headers, etc...)
1101 * Returns : Duplicate of argument s.
1103 *********************************************************************/
1104 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1106 #ifdef DETECT_MSIE_IMAGES
1107 if (strstr (s, "image/gif"))
1109 /* Client will accept HTML. If this seems counterintuitive,
1112 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1116 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1118 #endif /* def DETECT_MSIE_IMAGES */
1123 #endif /* defined(DETECT_MSIE_IMAGES) */
1127 /* the following functions add headers directly to the header list */
1130 /*********************************************************************
1132 * Function : client_cookie_adder
1134 * Description : Used in the add_client_headers list. Called from `sed'.
1137 * 1 : csp = Current client state (buffers, headers, etc...)
1141 *********************************************************************/
1142 void client_cookie_adder(struct client_state *csp)
1148 for (lst = csp->cookie_list->next; lst ; lst = lst->next)
1152 tmp = strsav(tmp, "; ");
1154 tmp = strsav(tmp, lst->str);
1157 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->next; lst ; lst = lst->next)
1161 tmp = strsav(tmp, "; ");
1164 if ((e = cookie_encode(lst->str)))
1166 tmp = strsav(tmp, e);
1175 ret = strdup("Cookie: ");
1176 ret = strsav(ret, tmp);
1177 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1178 enlist(csp->headers, ret);
1186 /*********************************************************************
1188 * Function : client_xtra_adder
1190 * Description : Used in the add_client_headers list. Called from `sed'.
1193 * 1 : csp = Current client state (buffers, headers, etc...)
1197 *********************************************************************/
1198 void client_xtra_adder(struct client_state *csp)
1200 struct list *lst = csp->action->multi[ACTION_MULTI_ADD_HEADER];
1202 for (lst = lst->next; lst ; lst = lst->next)
1204 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1205 enlist(csp->headers, lst->str);
1211 /*********************************************************************
1213 * Function : client_x_forwarded_adder
1215 * Description : Used in the add_client_headers list. Called from `sed'.
1218 * 1 : csp = Current client state (buffers, headers, etc...)
1222 *********************************************************************/
1223 void client_x_forwarded_adder(struct client_state *csp)
1227 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1232 if (csp->x_forwarded)
1234 p = strsav(p, csp->x_forwarded);
1235 p = strsav(p, ", ");
1236 p = strsav(p, csp->ip_addr_str);
1240 p = strsav(p, "X-Forwarded-For: ");
1241 p = strsav(p, csp->ip_addr_str);
1244 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1245 enlist(csp->headers, p);
1250 /*********************************************************************
1252 * Function : server_set_cookie
1254 * Description : Handle the server "cookie" header properly.
1255 * Log cookie to the jar file. Then "crunch" it,
1256 * or accept it. Called from `sed'.
1259 * 1 : v = parser pattern that matched this header
1260 * 2 : s = header that matched this pattern
1261 * 3 : csp = Current client state (buffers, headers, etc...)
1263 * Returns : `crumble' or a newly malloc'ed string.
1265 *********************************************************************/
1266 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1269 if (csp->config->jar)
1271 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1273 #endif /* def JAR_FILES */
1275 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1277 return(crumble(v, s, csp));
1286 /*********************************************************************
1288 * Function : client_host
1290 * Description : Clean the FORCE_PREFIX out of the 'host' http
1291 * header, if we use force
1295 * 2 : s = header (from sed) to clean
1296 * 3 : csp = Current client state (buffers, headers, etc...)
1298 * Returns : A malloc'ed pointer to the cleaned host header
1300 *********************************************************************/
1301 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1303 char *cleanhost = strdup(s);
1306 strclean(cleanhost, FORCE_PREFIX);
1310 #endif /* def FORCE_LOAD */
1314 /*********************************************************************
1316 * Function : strclean
1318 * Description : In-Situ-Eliminate all occurances of substring in
1322 * 1 : string = string to clean
1323 * 2 : substring = substring to eliminate
1325 * Returns : Number of eliminations
1327 *********************************************************************/
1328 int strclean(const char *string, const char *substring)
1330 int hits = 0, len = strlen(substring);
1333 while((pos = strstr(string, substring)))
1340 while (*p++ != '\0');
1347 #endif /* def FORCE_LOAD */