1 const char parsers_rcs[] = "$Id: parsers.c,v 1.11 2001/05/29 20:11:19 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.11 2001/05/29 20:11:19 joergs
45 * '/* inside comment' warning removed.
47 * Revision 1.10 2001/05/29 09:50:24 jongfoster
48 * Unified blocklist/imagelist/permissionslist.
49 * File format is still under discussion, but the internal changes
52 * Also modified interceptor behaviour:
53 * - We now intercept all URLs beginning with one of the following
54 * prefixes (and *only* these prefixes):
56 * * http://ijbswa.sf.net/config/
57 * * http://ijbswa.sourceforge.net/config/
58 * - New interceptors "home page" - go to http://i.j.b/ to see it.
59 * - Internal changes so that intercepted and fast redirect pages
60 * are not replaced with an image.
61 * - Interceptors now have the option to send a binary page direct
62 * to the client. (i.e. ijb-send-banner uses this)
63 * - Implemented show-url-info interceptor. (Which is why I needed
64 * the above interceptors changes - a typical URL is
65 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
66 * The previous mechanism would not have intercepted that, and
67 * if it had been intercepted then it then it would have replaced
70 * Revision 1.9 2001/05/28 17:26:33 jongfoster
71 * Fixing segfault if last header was crunched.
72 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
73 * can use the cross-platform sprintf() instead.)
75 * Revision 1.8 2001/05/27 22:17:04 oes
77 * - re_process_buffer no longer writes the modified buffer
78 * to the client, which was very ugly. It now returns the
79 * buffer, which it is then written by chat.
81 * - content_length now adjusts the Content-Length: header
82 * for modified documents rather than crunch()ing it.
83 * (Length info in csp->content_length, which is 0 for
84 * unmodified documents)
86 * - For this to work, sed() is called twice when filtering.
88 * Revision 1.7 2001/05/27 13:19:06 oes
89 * Patched Joergs solution for the content-length in.
91 * Revision 1.6 2001/05/26 13:39:32 jongfoster
92 * Only crunches Content-Length header if applying RE filtering.
93 * Without this fix, Microsoft Windows Update wouldn't work.
95 * Revision 1.5 2001/05/26 00:28:36 jongfoster
96 * Automatic reloading of config file.
97 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
98 * Most of the global variables have been moved to a new
99 * struct configuration_spec, accessed through csp->config->globalname
100 * Most of the globals remaining are used by the Win32 GUI.
102 * Revision 1.4 2001/05/22 18:46:04 oes
104 * - Enabled filtering banners by size rather than URL
105 * by adding patterns that replace all standard banner
106 * sizes with the "Junkbuster" gif to the re_filterfile
108 * - Enabled filtering WebBugs by providing a pattern
109 * which kills all 1x1 images
111 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
112 * which is selected by the (nonstandard and therefore
113 * capital) letter 'U' in the option string.
114 * It causes the quantifiers to be ungreedy by default.
115 * Appending a ? turns back to greedy (!).
117 * - Added a new interceptor ijb-send-banner, which
118 * sends back the "Junkbuster" gif. Without imagelist or
119 * MSIE detection support, or if tinygif = 1, or the
120 * URL isn't recognized as an imageurl, a lame HTML
121 * explanation is sent instead.
123 * - Added new feature, which permits blocking remote
124 * script redirects and firing back a local redirect
126 * The feature is conditionally compiled, i.e. it
127 * can be disabled with --disable-fast-redirects,
128 * plus it must be activated by a "fast-redirects"
129 * line in the config file, has its own log level
130 * and of course wants to be displayed by show-proxy-args
131 * Note: Boy, all the #ifdefs in 1001 locations and
132 * all the fumbling with configure.in and acconfig.h
133 * were *way* more work than the feature itself :-(
135 * - Because a generic redirect template was needed for
136 * this, tinygif = 3 now uses the same.
138 * - Moved GIFs, and other static HTTP response templates
143 * - Removed some >400 CRs again (Jon, you really worked
146 * Revision 1.3 2001/05/20 01:21:20 jongfoster
147 * Version 2.9.4 checkin.
148 * - Merged popupfile and cookiefile, and added control over PCRS
149 * filtering, in new "permissionsfile".
150 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
151 * file error you now get a message box (in the Win32 GUI) rather
152 * than the program exiting with no explanation.
153 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
155 * - Removed tabs from "config"
156 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
157 * - Bumped up version number.
159 * Revision 1.2 2001/05/17 23:02:36 oes
160 * - Made referrer option accept 'L' as a substitute for 'ยง'
162 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
163 * Initial import of version 2.9.3 source tree
166 *********************************************************************/
172 #include <sys/types.h>
186 #include "showargs.h"
190 #include "jbsockets.h"
191 #include "miscutil.h"
193 const char parsers_h_rcs[] = PARSERS_H_VERSION;
195 /* Fix a problem with Solaris. There should be no effect on other
197 * Solaris's isspace() is a macro which uses it's argument directly
198 * as an array index. Therefore we need to make sure that high-bit
199 * characters generate +ve values, and ideally we also want to make
200 * the argument match the declared parameter type of "int".
202 * Why did they write a character function that can't take a simple
203 * "char" argument? Doh!
205 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
206 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
209 const struct parsers client_patterns[] = {
210 { "referer:", 8, client_referrer },
211 { "user-agent:", 11, client_uagent },
212 { "ua-", 3, client_ua },
213 { "from:", 5, client_from },
214 { "cookie:", 7, client_send_cookie },
215 { "x-forwarded-for:", 16, client_x_forwarded },
216 { "proxy-connection:", 17, crumble },
218 { "Accept-Encoding: gzip", 21, crumble },
219 #endif /* def DENY_GZIP */
220 #if defined(DETECT_MSIE_IMAGES)
221 { "Accept:", 7, client_accept },
222 #endif /* defined(DETECT_MSIE_IMAGES) */
224 { "Host:", 5, client_host },
225 #endif /* def FORCE_LOAD */
226 /* { "if-modified-since:", 18, crumble }, */
230 const struct interceptors intercept_patterns[] = {
231 { "show-proxy-args", 14, show_proxy_args },
232 { "ijb-send-banner", 14, ijb_send_banner },
234 { "ij-untrusted-url", 15, ij_untrusted_url },
235 #endif /* def TRUST_FILES */
236 { "show-url-info", 13, ijb_show_url_info },
240 const struct parsers server_patterns[] = {
241 { "set-cookie:", 11, server_set_cookie },
242 { "connection:", 11, crumble },
243 #if defined(PCRS) || defined(KILLPOPUPS)
244 { "Content-Type:", 13, content_type },
245 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
247 { "Content-Length:", 15, content_length },
248 #endif /* def PCRS */
253 void (* const add_client_headers[])(struct client_state *) = {
255 client_x_forwarded_adder,
261 void (* const add_server_headers[])(struct client_state *) = {
266 /*********************************************************************
268 * Function : flush_socket
270 * Description : Write any pending "buffered" content.
273 * 1 : fd = file descriptor of the socket to read
274 * 2 : csp = Current client state (buffers, headers, etc...)
276 * Returns : On success, the number of bytes written are returned (zero
277 * indicates nothing was written). On error, -1 is returned,
278 * and errno is set appropriately. If count is zero and the
279 * file descriptor refers to a regular file, 0 will be
280 * returned without causing any other effect. For a special
281 * file, the results are not portable.
283 *********************************************************************/
284 int flush_socket(int fd, struct client_state *csp)
286 struct iob *iob = csp->iob;
287 int n = iob->eod - iob->cur;
294 n = write_socket(fd, iob->cur, n);
295 iob->eod = iob->cur = iob->buf;
301 /*********************************************************************
303 * Function : add_to_iob
305 * Description : Add content to the buffered page.
308 * 1 : csp = Current client state (buffers, headers, etc...)
309 * 2 : buf = holds the content to be added to the page
310 * 3 : n = number of bytes to be added
312 * Returns : Number of bytes in the content buffer.
314 *********************************************************************/
315 int add_to_iob(struct client_state *csp, char *buf, int n)
317 struct iob *iob = csp->iob;
321 have = iob->eod - iob->cur;
330 if ((p = (char *)malloc(need + 1)) == NULL)
332 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
338 /* there is something in the buffer - save it */
339 memcpy(p, iob->cur, have);
341 /* replace the buffer with the new space */
345 /* point to the end of the data */
350 /* the buffer is empty, free it and reinitialize */
355 /* copy the new data into the iob buffer */
358 /* point to the end of the data */
361 /* null terminate == cheap insurance */
364 /* set the pointers to the new values */
373 /*********************************************************************
375 * Function : get_header
377 * Description : This (odd) routine will parse the csp->iob
380 * 1 : csp = Current client state (buffers, headers, etc...)
382 * Returns : Any one of the following:
384 * 1) a pointer to a dynamically allocated string that contains a header line
385 * 2) NULL indicating that the end of the header was reached
386 * 3) "" indicating that the end of the iob was reached before finding
387 * a complete header line.
389 *********************************************************************/
390 char *get_header(struct client_state *csp)
396 if ((iob->cur == NULL)
397 || ((p = strchr(iob->cur, '\n')) == NULL))
399 return(""); /* couldn't find a complete header */
404 ret = strdup(iob->cur);
408 if ((q = strchr(ret, '\r'))) *q = '\0';
410 /* is this a blank linke (i.e. the end of the header) ? */
422 /*********************************************************************
426 * Description : Append a string into a specified string list.
429 * 1 : h = pointer to list 'dummy' header
430 * 2 : s = string to add to the list
434 *********************************************************************/
435 void enlist(struct list *h, const char *s)
437 struct list *n = (struct list *)malloc(sizeof(*n));
460 /*********************************************************************
462 * Function : destroy_list
464 * Description : Destroy a string list (opposite of enlist)
467 * 1 : h = pointer to list 'dummy' header
471 *********************************************************************/
472 void destroy_list(struct list *h)
476 for (p = h->next; p ; p = n)
483 memset(h, '\0', sizeof(*h));
488 /*********************************************************************
490 * Function : list_to_text
492 * Description : "Flaten" a string list into 1 long \r\n delimited string.
495 * 1 : h = pointer to list 'dummy' header
497 * Returns : NULL on malloc error, else new long string.
499 *********************************************************************/
500 static char *list_to_text(struct list *h)
509 for (p = h->next; p ; p = p->next)
513 size += strlen(p->str) + 2;
517 if ((ret = (char *)malloc(size + 1)) == NULL)
526 for (p = h->next; p ; p = p->next)
532 *s++ = '\r'; *s++ = '\n';
541 /*********************************************************************
545 * Description : add, delete or modify lines in the HTTP header streams.
546 * On entry, it receives a linked list of headers space
547 * that was allocated dynamically (both the list nodes
548 * and the header contents).
550 * As a side effect it frees the space used by the original
554 * 1 : pats = list of patterns to match against headers
555 * 2 : more_headers = list of functions to add more
556 * headers (client or server)
557 * 3 : csp = Current client state (buffers, headers, etc...)
559 * Returns : Single pointer to a fully formed header.
561 *********************************************************************/
562 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
565 const struct parsers *v;
569 for (v = pats; v->str ; v++)
571 for (p = csp->headers->next; p ; p = p->next)
573 /* Header crunch()ed in previous run? -> ignore */
574 if (p->str == NULL) continue;
576 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
578 if (strncmpic(p->str, v->str, v->len) == 0)
580 hdr = v->parser(v, p->str, csp);
587 /* place any additional headers on the csp->headers list */
588 for (f = more_headers; *f ; f++)
593 /* add the blank line at the end of the header, if necessary */
594 if ( (csp->headers->last == NULL)
595 || (csp->headers->last->str == NULL)
596 || (*csp->headers->last->str != '\0') )
598 enlist(csp->headers, "");
601 hdr = list_to_text(csp->headers);
608 /*********************************************************************
610 * Function : free_http_request
612 * Description : Freez a http_request structure
615 * 1 : http = points to a http_request structure to free
619 *********************************************************************/
620 void free_http_request(struct http_request *http)
625 freez(http->hostport);
632 /*********************************************************************
634 * Function : parse_http_request
636 * Description : Parse out the host and port from the URL. Find the
637 * hostname & path, port (if ':'), and/or password (if '@')
640 * 1 : req = URL (or is it URI?) to break down
641 * 2 : http = pointer to the http structure to hold elements
642 * 3 : csp = Current client state (buffers, headers, etc...)
646 *********************************************************************/
647 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
649 char *buf, *v[10], *url, *p;
652 memset(http, '\0', sizeof(*http));
654 http->cmd = strdup(req);
658 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
662 /* this could be a CONNECT request */
663 if (strcmpic(v[0], "connect") == 0)
666 http->gpc = strdup(v[0]);
667 http->hostport = strdup(v[1]);
668 http->ver = strdup(v[2]);
673 /* This next line is a little ugly, but it simplifies the if statement below. */
674 /* Basically if using webDAV, we want the OR condition to use these too. */
678 * These are the headers as defined in RFC2518 to add webDAV support
681 #define OR_WEBDAV || \
682 (0 == strcmpic(v[0], "propfind")) || \
683 (0 == strcmpic(v[0], "proppatch")) || \
684 (0 == strcmpic(v[0], "move")) || \
685 (0 == strcmpic(v[0], "copy")) || \
686 (0 == strcmpic(v[0], "mkcol")) || \
687 (0 == strcmpic(v[0], "lock")) || \
688 (0 == strcmpic(v[0], "unlock"))
690 #else /* No webDAV support is enabled. Provide an empty OR_WEBDAV macro. */
696 /* or it could be a GET or a POST (possibly webDAV too) */
697 if ((strcmpic(v[0], "get") == 0) ||
698 (strcmpic(v[0], "head") == 0) OR_WEBDAV ||
699 (strcmpic(v[0], "post") == 0))
702 http->gpc = strdup(v[0]);
704 http->ver = strdup(v[2]);
706 if (strncmpic(url, "http://", 7) == 0)
710 else if (strncmpic(url, "https://", 8) == 0)
719 if (url && (p = strchr(url, '/')))
721 http->path = strdup(p);
723 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, char *s, struct client_state *csp)
799 log_error(LOG_LEVEL_HEADER, "crunch!");
805 #if defined(PCRS) || defined(KILLPOPUPS)
807 /*********************************************************************
809 * Function : content_type
811 * Description : Is this a text/.* or javascript MIME Type?
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 *content_type(const struct parsers *v, char *s, struct client_state *csp)
823 if (strstr (s, " text/") || strstr (s, "application/x-javascript"))
831 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
835 /*********************************************************************
837 * Function : content_length
839 * Description : Crunch Content-Length header if & only if we are
840 * filtering this page through PCRS.
844 * 2 : s = header string we are "considering"
845 * 3 : csp = Current client state (buffers, headers, etc...)
847 * Returns : A duplicate string pointer to this header (ie. pass thru)
849 *********************************************************************/
850 char *content_length(const struct parsers *v, char *s, struct client_state *csp)
852 if (csp->content_length != 0) /* Content has been modified */
854 s = (char *) zalloc(100);
855 sprintf(s, "Content-Length: %d", csp->content_length);
856 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
865 #endif /* def PCRS */
868 /*********************************************************************
870 * Function : client_referrer
872 * Description : Handle the "referer" config setting properly.
877 * 2 : s = header (from sed) to "crunch"
878 * 3 : csp = Current client state (buffers, headers, etc...)
880 * Returns : NULL if crunched, or a malloc'ed string with the original
883 *********************************************************************/
884 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
887 /* Since the referrer can include the prefix even
888 * even if the request itself is non-forced, we must
889 * clean it unconditionally
891 strclean(s, FORCE_PREFIX);
892 #endif /* def FORCE_LOAD */
895 csp->referrer = strdup(s);
896 #endif /* def TRUST_FILES */
899 * Check permissionsfile. If we have allowed this site to get the
900 * referer, then send it and we're done.
902 if (csp->permissions & PERMIT_REFERER)
908 * Check configfile. Are we blocking referer?
910 if ( (csp->config->referrer == NULL)
911 || (*csp->config->referrer == '@') )
913 log_error(LOG_LEVEL_HEADER, "crunch!");
918 * Check configfile. Are we always sending referer?
920 if (*csp->config->referrer == '.')
926 * New option ยง or L: Forge a referer as http://[hostname:port of REQUEST]/
927 * to fool stupid checks for in-site links
929 if (*csp->config->referrer == 'ยง' || *csp->config->referrer == 'L')
931 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
932 s = strsav(NULL, "Referer: ");
933 s = strsav(s, "http://");
934 s = strsav(s, csp->http->hostport);
940 * We have a specific (fixed) referer we want to send.
943 log_error(LOG_LEVEL_HEADER, "modified");
945 s = strsav( NULL, "Referer: " );
946 s = strsav( s, csp->config->referrer );
952 /*********************************************************************
954 * Function : client_uagent
956 * Description : Handle the "user-agent" config setting properly.
961 * 2 : s = header (from sed) to "crunch"
962 * 3 : csp = Current client state (buffers, headers, etc...)
964 * Returns : A malloc'ed pointer to the default agent, or
965 * a malloc'ed string pointer to this header (ie. pass thru).
967 *********************************************************************/
968 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
970 #ifdef DETECT_MSIE_IMAGES
971 if (strstr (s, "MSIE "))
973 /* This is Microsoft Internet Explorer.
974 * Enable auto-detect.
976 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
978 #endif /* def DETECT_MSIE_IMAGES */
980 if (csp->config->uagent == NULL)
982 log_error(LOG_LEVEL_HEADER, "default");
983 return(strdup(DEFAULT_USER_AGENT));
986 if (*csp->config->uagent == '.')
991 if (*csp->config->uagent == '@')
993 if (csp->permissions & PERMIT_USER_AGENT)
999 log_error(LOG_LEVEL_HEADER, "default");
1000 return(strdup(DEFAULT_USER_AGENT));
1004 log_error(LOG_LEVEL_HEADER, "modified");
1006 s = strsav( NULL, "User-Agent: " );
1007 s = strsav( s, csp->config->uagent );
1013 /*********************************************************************
1015 * Function : client_ua
1017 * Description : Handle "ua-" headers properly. Called from `sed'.
1021 * 2 : s = header (from sed) to "crunch"
1022 * 3 : csp = Current client state (buffers, headers, etc...)
1024 * Returns : NULL if crunched, or a malloc'ed string to original header
1026 *********************************************************************/
1027 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
1029 if (csp->config->uagent == NULL)
1031 log_error(LOG_LEVEL_HEADER, "crunch!");
1035 if (*csp->config->uagent == '.')
1040 if (*csp->config->uagent == '@')
1042 if (csp->permissions & PERMIT_USER_AGENT)
1048 log_error(LOG_LEVEL_HEADER, "crunch!");
1053 log_error(LOG_LEVEL_HEADER, "crunch!");
1059 /*********************************************************************
1061 * Function : client_from
1063 * Description : Handle the "from" config setting properly.
1064 * Called from `sed'.
1068 * 2 : s = header (from sed) to "crunch"
1069 * 3 : csp = Current client state (buffers, headers, etc...)
1071 * Returns : NULL if crunched, or a malloc'ed string to
1072 * modified/original header.
1074 *********************************************************************/
1075 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
1077 /* if not set, zap it */
1078 if (csp->config->from == NULL)
1080 log_error(LOG_LEVEL_HEADER, "crunch!");
1084 if (*csp->config->from == '.')
1089 log_error(LOG_LEVEL_HEADER, " modified");
1091 s = strsav( NULL, "From: " );
1092 s = strsav( s, csp->config->from );
1098 /*********************************************************************
1100 * Function : client_send_cookie
1102 * Description : Handle the "cookie" header properly. Called from `sed'.
1103 * If cookie is accepted, add it to the cookie_list,
1104 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1107 * 1 : v = pattern of cookie `sed' found matching
1108 * 2 : s = header (from sed) to "crunch"
1109 * 3 : csp = Current client state (buffers, headers, etc...)
1111 * Returns : Always NULL.
1113 *********************************************************************/
1114 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
1116 if (csp->permissions & PERMIT_COOKIE_READ)
1118 enlist(csp->cookie_list, s + v->len + 1);
1122 log_error(LOG_LEVEL_HEADER, " crunch!");
1126 * Always return NULL here. The cookie header
1127 * will be sent at the end of the header.
1134 /*********************************************************************
1136 * Function : client_x_forwarded
1138 * Description : Handle the "x-forwarded-for" config setting properly,
1139 * also used in the add_client_headers list. Called from `sed'.
1143 * 2 : s = header (from sed) to "crunch"
1144 * 3 : csp = Current client state (buffers, headers, etc...)
1146 * Returns : Always NULL.
1148 *********************************************************************/
1149 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1151 if (csp->config->add_forwarded)
1153 csp->x_forwarded = strdup(s);
1157 * Always return NULL, since this information
1158 * will be sent at the end of the header.
1165 #if defined(DETECT_MSIE_IMAGES)
1166 /*********************************************************************
1168 * Function : client_accept
1170 * Description : Detect whether the client wants HTML or an image.
1171 * Clients do not always make this information available
1172 * in a sane way. Always passes the header through
1173 * the proxy unchanged.
1177 * 2 : s = Header string. Null terminated.
1178 * 3 : csp = Current client state (buffers, headers, etc...)
1180 * Returns : Duplicate of argument s.
1182 *********************************************************************/
1183 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1185 #ifdef DETECT_MSIE_IMAGES
1186 if (strstr (s, "image/gif"))
1188 /* Client will accept HTML. If this seems counterintuitive,
1191 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1195 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1197 #endif /* def DETECT_MSIE_IMAGES */
1202 #endif /* defined(DETECT_MSIE_IMAGES) */
1206 /* the following functions add headers directly to the header list */
1209 /*********************************************************************
1211 * Function : client_cookie_adder
1213 * Description : Used in the add_client_headers list. Called from `sed'.
1216 * 1 : csp = Current client state (buffers, headers, etc...)
1220 *********************************************************************/
1221 void client_cookie_adder(struct client_state *csp)
1227 for (l = csp->cookie_list->next; l ; l = l->next)
1231 tmp = strsav(tmp, "; ");
1233 tmp = strsav(tmp, l->str);
1236 for (l = csp->config->wafer_list->next; l ; l = l->next)
1240 tmp = strsav(tmp, "; ");
1243 if ((e = cookie_encode(l->str)))
1245 tmp = strsav(tmp, e);
1254 ret = strdup("Cookie: ");
1255 ret = strsav(ret, tmp);
1256 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1257 enlist(csp->headers, ret);
1265 /*********************************************************************
1267 * Function : client_xtra_adder
1269 * Description : Used in the add_client_headers list. Called from `sed'.
1272 * 1 : csp = Current client state (buffers, headers, etc...)
1276 *********************************************************************/
1277 void client_xtra_adder(struct client_state *csp)
1281 for (l = csp->config->xtra_list->next; l ; l = l->next)
1283 log_error(LOG_LEVEL_HEADER, "addh: %s", l->str);
1284 enlist(csp->headers, l->str);
1290 /*********************************************************************
1292 * Function : client_x_forwarded_adder
1294 * Description : Used in the add_client_headers list. Called from `sed'.
1297 * 1 : csp = Current client state (buffers, headers, etc...)
1301 *********************************************************************/
1302 void client_x_forwarded_adder(struct client_state *csp)
1306 if (csp->config->add_forwarded == 0)
1311 if (csp->x_forwarded)
1313 p = strsav(p, csp->x_forwarded);
1314 p = strsav(p, ", ");
1315 p = strsav(p, csp->ip_addr_str);
1319 p = strsav(p, "X-Forwarded-For: ");
1320 p = strsav(p, csp->ip_addr_str);
1323 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1324 enlist(csp->headers, p);
1329 /*********************************************************************
1331 * Function : server_set_cookie
1333 * Description : Handle the server "cookie" header properly.
1334 * Log cookie to the jar file. Then "crunch" it,
1335 * or accept it. Called from `sed'.
1338 * 1 : v = parser pattern that matched this header
1339 * 2 : s = header that matched this pattern
1340 * 3 : csp = Current client state (buffers, headers, etc...)
1342 * Returns : `crumble' or a newly malloc'ed string.
1344 *********************************************************************/
1345 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1348 if (csp->config->jar)
1350 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1352 #endif /* def JAR_FILES */
1354 if (!(csp->permissions & PERMIT_COOKIE_SET))
1356 return(crumble(v, s, csp));
1365 /*********************************************************************
1367 * Function : client_host
1369 * Description : Clean the FORCE_PREFIX out of the 'host' http
1370 * header, if we use force
1374 * 2 : s = header (from sed) to clean
1375 * 3 : csp = Current client state (buffers, headers, etc...)
1377 * Returns : A malloc'ed pointer to the cleaned host header
1379 *********************************************************************/
1380 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1382 char *cleanhost = strdup(s);
1385 strclean(cleanhost, FORCE_PREFIX);
1389 #endif /* def FORCE_LOAD */
1393 /*********************************************************************
1395 * Function : strclean
1397 * Description : In-Situ-Eliminate all occurances of substring in
1401 * 1 : string = string to clean
1402 * 2 : substring = substring to eliminate
1404 * Returns : Number of eliminations
1406 *********************************************************************/
1407 int strclean(const char *string, const char *substring)
1409 int hits = 0, len = strlen(substring);
1412 while((pos = strstr(string, substring)))
1419 while (*p++ != '\0');
1426 #endif /* def FORCE_LOAD */