1 const char parsers_rcs[] = "$Id: parsers.c,v 1.9 2001/05/28 17:26:33 jongfoster 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.9 2001/05/28 17:26:33 jongfoster
45 * Fixing segfault if last header was crunched.
46 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
47 * can use the cross-platform sprintf() instead.)
49 * Revision 1.8 2001/05/27 22:17:04 oes
51 * - re_process_buffer no longer writes the modified buffer
52 * to the client, which was very ugly. It now returns the
53 * buffer, which it is then written by chat.
55 * - content_length now adjusts the Content-Length: header
56 * for modified documents rather than crunch()ing it.
57 * (Length info in csp->content_length, which is 0 for
58 * unmodified documents)
60 * - For this to work, sed() is called twice when filtering.
62 * Revision 1.7 2001/05/27 13:19:06 oes
63 * Patched Joergs solution for the content-length in.
65 * Revision 1.6 2001/05/26 13:39:32 jongfoster
66 * Only crunches Content-Length header if applying RE filtering.
67 * Without this fix, Microsoft Windows Update wouldn't work.
69 * Revision 1.5 2001/05/26 00:28:36 jongfoster
70 * Automatic reloading of config file.
71 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
72 * Most of the global variables have been moved to a new
73 * struct configuration_spec, accessed through csp->config->globalname
74 * Most of the globals remaining are used by the Win32 GUI.
76 * Revision 1.4 2001/05/22 18:46:04 oes
78 * - Enabled filtering banners by size rather than URL
79 * by adding patterns that replace all standard banner
80 * sizes with the "Junkbuster" gif to the re_filterfile
82 * - Enabled filtering WebBugs by providing a pattern
83 * which kills all 1x1 images
85 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
86 * which is selected by the (nonstandard and therefore
87 * capital) letter 'U' in the option string.
88 * It causes the quantifiers to be ungreedy by default.
89 * Appending a ? turns back to greedy (!).
91 * - Added a new interceptor ijb-send-banner, which
92 * sends back the "Junkbuster" gif. Without imagelist or
93 * MSIE detection support, or if tinygif = 1, or the
94 * URL isn't recognized as an imageurl, a lame HTML
95 * explanation is sent instead.
97 * - Added new feature, which permits blocking remote
98 * script redirects and firing back a local redirect
100 * The feature is conditionally compiled, i.e. it
101 * can be disabled with --disable-fast-redirects,
102 * plus it must be activated by a "fast-redirects"
103 * line in the config file, has its own log level
104 * and of course wants to be displayed by show-proxy-args
105 * Note: Boy, all the #ifdefs in 1001 locations and
106 * all the fumbling with configure.in and acconfig.h
107 * were *way* more work than the feature itself :-(
109 * - Because a generic redirect template was needed for
110 * this, tinygif = 3 now uses the same.
112 * - Moved GIFs, and other static HTTP response templates
117 * - Removed some >400 CRs again (Jon, you really worked
120 * Revision 1.3 2001/05/20 01:21:20 jongfoster
121 * Version 2.9.4 checkin.
122 * - Merged popupfile and cookiefile, and added control over PCRS
123 * filtering, in new "permissionsfile".
124 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
125 * file error you now get a message box (in the Win32 GUI) rather
126 * than the program exiting with no explanation.
127 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
129 * - Removed tabs from "config"
130 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
131 * - Bumped up version number.
133 * Revision 1.2 2001/05/17 23:02:36 oes
134 * - Made referrer option accept 'L' as a substitute for 'ยง'
136 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
137 * Initial import of version 2.9.3 source tree
140 *********************************************************************/
146 #include <sys/types.h>
160 #include "showargs.h"
164 #include "jbsockets.h"
165 #include "miscutil.h"
167 const char parsers_h_rcs[] = PARSERS_H_VERSION;
169 /* Fix a problem with Solaris. There should be no effect on other
171 * Solaris's isspace() is a macro which uses it's argument directly
172 * as an array index. Therefore we need to make sure that high-bit
173 * characters generate +ve values, and ideally we also want to make
174 * the argument match the declared parameter type of "int".
176 * Why did they write a character function that can't take a simple
177 * "char" argument? Doh!
179 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
180 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
183 const struct parsers client_patterns[] = {
184 { "referer:", 8, client_referrer },
185 { "user-agent:", 11, client_uagent },
186 { "ua-", 3, client_ua },
187 { "from:", 5, client_from },
188 { "cookie:", 7, client_send_cookie },
189 { "x-forwarded-for:", 16, client_x_forwarded },
190 { "proxy-connection:", 17, crumble },
192 { "Accept-Encoding: gzip", 21, crumble },
193 #endif /* def DENY_GZIP */
194 #if defined(DETECT_MSIE_IMAGES)
195 { "Accept:", 7, client_accept },
196 #endif /* defined(DETECT_MSIE_IMAGES) */
198 { "Host:", 5, client_host },
199 #endif /* def FORCE_LOAD */
200 /* { "if-modified-since:", 18, crumble }, */
204 const struct interceptors intercept_patterns[] = {
205 { "show-proxy-args", 14, show_proxy_args },
206 { "ijb-send-banner", 14, ijb_send_banner },
208 { "ij-untrusted-url", 15, ij_untrusted_url },
209 #endif /* def TRUST_FILES */
210 { "show-url-info", 13, ijb_show_url_info },
214 const struct parsers server_patterns[] = {
215 { "set-cookie:", 11, server_set_cookie },
216 { "connection:", 11, crumble },
217 #if defined(PCRS) || defined(KILLPOPUPS)
218 { "Content-Type:", 13, content_type },
219 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
221 { "Content-Length:", 15, content_length },
222 #endif /* def PCRS */
227 void (* const add_client_headers[])(struct client_state *) = {
229 client_x_forwarded_adder,
235 void (* const add_server_headers[])(struct client_state *) = {
240 /*********************************************************************
242 * Function : flush_socket
244 * Description : Write any pending "buffered" content.
247 * 1 : fd = file descriptor of the socket to read
248 * 2 : csp = Current client state (buffers, headers, etc...)
250 * Returns : On success, the number of bytes written are returned (zero
251 * indicates nothing was written). On error, -1 is returned,
252 * and errno is set appropriately. If count is zero and the
253 * file descriptor refers to a regular file, 0 will be
254 * returned without causing any other effect. For a special
255 * file, the results are not portable.
257 *********************************************************************/
258 int flush_socket(int fd, struct client_state *csp)
260 struct iob *iob = csp->iob;
261 int n = iob->eod - iob->cur;
268 n = write_socket(fd, iob->cur, n);
269 iob->eod = iob->cur = iob->buf;
275 /*********************************************************************
277 * Function : add_to_iob
279 * Description : Add content to the buffered page.
282 * 1 : csp = Current client state (buffers, headers, etc...)
283 * 2 : buf = holds the content to be added to the page
284 * 3 : n = number of bytes to be added
286 * Returns : Number of bytes in the content buffer.
288 *********************************************************************/
289 int add_to_iob(struct client_state *csp, char *buf, int n)
291 struct iob *iob = csp->iob;
295 have = iob->eod - iob->cur;
304 if ((p = (char *)malloc(need + 1)) == NULL)
306 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
312 /* there is something in the buffer - save it */
313 memcpy(p, iob->cur, have);
315 /* replace the buffer with the new space */
319 /* point to the end of the data */
324 /* the buffer is empty, free it and reinitialize */
329 /* copy the new data into the iob buffer */
332 /* point to the end of the data */
335 /* null terminate == cheap insurance */
338 /* set the pointers to the new values */
347 /*********************************************************************
349 * Function : get_header
351 * Description : This (odd) routine will parse the csp->iob
354 * 1 : csp = Current client state (buffers, headers, etc...)
356 * Returns : Any one of the following:
358 * 1) a pointer to a dynamically allocated string that contains a header line
359 * 2) NULL indicating that the end of the header was reached
360 * 3) "" indicating that the end of the iob was reached before finding
361 * a complete header line.
363 *********************************************************************/
364 char *get_header(struct client_state *csp)
370 if ((iob->cur == NULL)
371 || ((p = strchr(iob->cur, '\n')) == NULL))
373 return(""); /* couldn't find a complete header */
378 ret = strdup(iob->cur);
382 if ((q = strchr(ret, '\r'))) *q = '\0';
384 /* is this a blank linke (i.e. the end of the header) ? */
396 /*********************************************************************
400 * Description : Append a string into a specified string list.
403 * 1 : h = pointer to list 'dummy' header
404 * 2 : s = string to add to the list
408 *********************************************************************/
409 void enlist(struct list *h, const char *s)
411 struct list *n = (struct list *)malloc(sizeof(*n));
434 /*********************************************************************
436 * Function : destroy_list
438 * Description : Destroy a string list (opposite of enlist)
441 * 1 : h = pointer to list 'dummy' header
445 *********************************************************************/
446 void destroy_list(struct list *h)
450 for (p = h->next; p ; p = n)
457 memset(h, '\0', sizeof(*h));
462 /*********************************************************************
464 * Function : list_to_text
466 * Description : "Flaten" a string list into 1 long \r\n delimited string.
469 * 1 : h = pointer to list 'dummy' header
471 * Returns : NULL on malloc error, else new long string.
473 *********************************************************************/
474 static char *list_to_text(struct list *h)
483 for (p = h->next; p ; p = p->next)
487 size += strlen(p->str) + 2;
491 if ((ret = (char *)malloc(size + 1)) == NULL)
500 for (p = h->next; p ; p = p->next)
506 *s++ = '\r'; *s++ = '\n';
515 /*********************************************************************
519 * Description : add, delete or modify lines in the HTTP header streams.
520 * On entry, it receives a linked list of headers space
521 * that was allocated dynamically (both the list nodes
522 * and the header contents).
524 * As a side effect it frees the space used by the original
528 * 1 : pats = list of patterns to match against headers
529 * 2 : more_headers = list of functions to add more
530 * headers (client or server)
531 * 3 : csp = Current client state (buffers, headers, etc...)
533 * Returns : Single pointer to a fully formed header.
535 *********************************************************************/
536 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
539 const struct parsers *v;
543 for (v = pats; v->str ; v++)
545 for (p = csp->headers->next; p ; p = p->next)
547 /* Header crunch()ed in previous run? -> ignore */
548 if (p->str == NULL) continue;
550 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
552 if (strncmpic(p->str, v->str, v->len) == 0)
554 hdr = v->parser(v, p->str, csp);
561 /* place any additional headers on the csp->headers list */
562 for (f = more_headers; *f ; f++)
567 /* add the blank line at the end of the header, if necessary */
568 if ( (csp->headers->last == NULL)
569 || (csp->headers->last->str == NULL)
570 || (*csp->headers->last->str != '\0') )
572 enlist(csp->headers, "");
575 hdr = list_to_text(csp->headers);
582 /*********************************************************************
584 * Function : free_http_request
586 * Description : Freez a http_request structure
589 * 1 : http = points to a http_request structure to free
593 *********************************************************************/
594 void free_http_request(struct http_request *http)
599 freez(http->hostport);
606 /*********************************************************************
608 * Function : parse_http_request
610 * Description : Parse out the host and port from the URL. Find the
611 * hostname & path, port (if ':'), and/or password (if '@')
614 * 1 : req = URL (or is it URI?) to break down
615 * 2 : http = pointer to the http structure to hold elements
616 * 3 : csp = Current client state (buffers, headers, etc...)
620 *********************************************************************/
621 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
623 char *buf, *v[10], *url, *p;
626 memset(http, '\0', sizeof(*http));
628 http->cmd = strdup(req);
632 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
636 /* this could be a CONNECT request */
637 if (strcmpic(v[0], "connect") == 0)
640 http->gpc = strdup(v[0]);
641 http->hostport = strdup(v[1]);
642 http->ver = strdup(v[2]);
647 /* This next line is a little ugly, but it simplifies the if statement below. */
648 /* Basically if using webDAV, we want the OR condition to use these too. */
652 * These are the headers as defined in RFC2518 to add webDAV support
655 #define OR_WEBDAV || \
656 (0 == strcmpic(v[0], "propfind")) || \
657 (0 == strcmpic(v[0], "proppatch")) || \
658 (0 == strcmpic(v[0], "move")) || \
659 (0 == strcmpic(v[0], "copy")) || \
660 (0 == strcmpic(v[0], "mkcol")) || \
661 (0 == strcmpic(v[0], "lock")) || \
662 (0 == strcmpic(v[0], "unlock"))
664 #else /* No webDAV support is enabled. Provide an empty OR_WEBDAV macro. */
670 /* or it could be a GET or a POST (possibly webDAV too) */
671 if ((strcmpic(v[0], "get") == 0) ||
672 (strcmpic(v[0], "head") == 0) OR_WEBDAV ||
673 (strcmpic(v[0], "post") == 0))
676 http->gpc = strdup(v[0]);
678 http->ver = strdup(v[2]);
680 if (strncmpic(url, "http://", 7) == 0)
684 else if (strncmpic(url, "https://", 8) == 0)
693 if (url && (p = strchr(url, '/')))
695 http->path = strdup(p);
697 http->hostport = strdup(url);
705 if (http->hostport == NULL)
707 free_http_request(http);
711 buf = strdup(http->hostport);
714 /* check if url contains password */
715 n = ssplit(buf, "@", v, SZ(v), 1, 1);
718 char * newbuf = NULL;
719 newbuf = strdup(v[1]);
724 n = ssplit(buf, ":", v, SZ(v), 1, 1);
728 http->host = strdup(v[0]);
734 http->host = strdup(v[0]);
735 http->port = atoi(v[1]);
740 if (http->host == NULL)
742 free_http_request(http);
745 if (http->path == NULL)
747 http->path = strdup("");
753 /* here begins the family of parser functions that reformat header lines */
756 /*********************************************************************
760 * Description : This is called if a header matches a pattern to "crunch"
763 * 1 : v = Pointer to parsers structure, which basically holds
764 * headers (client or server) that we want to "crunch"
765 * 2 : s = header (from sed) to "crunch"
766 * 3 : csp = Current client state (buffers, headers, etc...)
768 * Returns : Always NULL.
770 *********************************************************************/
771 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
773 log_error(LOG_LEVEL_HEADER, "crunch!");
779 #if defined(PCRS) || defined(KILLPOPUPS)
781 /*********************************************************************
783 * Function : content_type
785 * Description : Is this a text/* or javascript MIME Type?
789 * 2 : s = header string we are "considering"
790 * 3 : csp = Current client state (buffers, headers, etc...)
792 * Returns : A duplicate string pointer to this header (ie. pass thru)
794 *********************************************************************/
795 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
797 if (strstr (s, " text/") || strstr (s, "application/x-javascript"))
805 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
809 /*********************************************************************
811 * Function : content_length
813 * Description : Crunch Content-Length header if & only if we are
814 * filtering this page through PCRS.
818 * 2 : s = header string we are "considering"
819 * 3 : csp = Current client state (buffers, headers, etc...)
821 * Returns : A duplicate string pointer to this header (ie. pass thru)
823 *********************************************************************/
824 char *content_length(const struct parsers *v, char *s, struct client_state *csp)
826 if (csp->content_length != 0) /* Content has been modified */
828 s = (char *) zalloc(100);
829 sprintf(s, "Content-Length: %d", csp->content_length);
830 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
839 #endif /* def PCRS */
842 /*********************************************************************
844 * Function : client_referrer
846 * Description : Handle the "referer" config setting properly.
851 * 2 : s = header (from sed) to "crunch"
852 * 3 : csp = Current client state (buffers, headers, etc...)
854 * Returns : NULL if crunched, or a malloc'ed string with the original
857 *********************************************************************/
858 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
861 /* Since the referrer can include the prefix even
862 * even if the request itself is non-forced, we must
863 * clean it unconditionally
865 strclean(s, FORCE_PREFIX);
866 #endif /* def FORCE_LOAD */
869 csp->referrer = strdup(s);
870 #endif /* def TRUST_FILES */
\r
873 * Check permissionsfile. If we have allowed this site to get the
874 * referer, then send it and we're done.
876 if (csp->permissions & PERMIT_REFERER)
882 * Check configfile. Are we blocking referer?
884 if ( (csp->config->referrer == NULL)
885 || (*csp->config->referrer == '@') )
887 log_error(LOG_LEVEL_HEADER, "crunch!");
892 * Check configfile. Are we always sending referer?
894 if (*csp->config->referrer == '.')
900 * New option ยง or L: Forge a referer as http://[hostname:port of REQUEST]/
901 * to fool stupid checks for in-site links
903 if (*csp->config->referrer == 'ยง' || *csp->config->referrer == 'L')
905 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
906 s = strsav(NULL, "Referer: ");
907 s = strsav(s, "http://");
908 s = strsav(s, csp->http->hostport);
914 * We have a specific (fixed) referer we want to send.
917 log_error(LOG_LEVEL_HEADER, "modified");
919 s = strsav( NULL, "Referer: " );
920 s = strsav( s, csp->config->referrer );
926 /*********************************************************************
928 * Function : client_uagent
930 * Description : Handle the "user-agent" config setting properly.
935 * 2 : s = header (from sed) to "crunch"
936 * 3 : csp = Current client state (buffers, headers, etc...)
938 * Returns : A malloc'ed pointer to the default agent, or
939 * a malloc'ed string pointer to this header (ie. pass thru).
941 *********************************************************************/
942 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
944 #ifdef DETECT_MSIE_IMAGES
945 if (strstr (s, "MSIE "))
947 /* This is Microsoft Internet Explorer.
948 * Enable auto-detect.
950 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
952 #endif /* def DETECT_MSIE_IMAGES */
954 if (csp->config->uagent == NULL)
956 log_error(LOG_LEVEL_HEADER, "default");
957 return(strdup(DEFAULT_USER_AGENT));
960 if (*csp->config->uagent == '.')
965 if (*csp->config->uagent == '@')
967 if (csp->permissions & PERMIT_USER_AGENT)
973 log_error(LOG_LEVEL_HEADER, "default");
974 return(strdup(DEFAULT_USER_AGENT));
978 log_error(LOG_LEVEL_HEADER, "modified");
980 s = strsav( NULL, "User-Agent: " );
981 s = strsav( s, csp->config->uagent );
987 /*********************************************************************
989 * Function : client_ua
991 * Description : Handle "ua-" headers properly. Called from `sed'.
995 * 2 : s = header (from sed) to "crunch"
996 * 3 : csp = Current client state (buffers, headers, etc...)
998 * Returns : NULL if crunched, or a malloc'ed string to original header
1000 *********************************************************************/
1001 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
1003 if (csp->config->uagent == NULL)
1005 log_error(LOG_LEVEL_HEADER, "crunch!");
1009 if (*csp->config->uagent == '.')
1014 if (*csp->config->uagent == '@')
1016 if (csp->permissions & PERMIT_USER_AGENT)
1022 log_error(LOG_LEVEL_HEADER, "crunch!");
1027 log_error(LOG_LEVEL_HEADER, "crunch!");
1033 /*********************************************************************
1035 * Function : client_from
1037 * Description : Handle the "from" config setting properly.
1038 * Called from `sed'.
1042 * 2 : s = header (from sed) to "crunch"
1043 * 3 : csp = Current client state (buffers, headers, etc...)
1045 * Returns : NULL if crunched, or a malloc'ed string to
1046 * modified/original header.
1048 *********************************************************************/
1049 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
1051 /* if not set, zap it */
1052 if (csp->config->from == NULL)
1054 log_error(LOG_LEVEL_HEADER, "crunch!");
1058 if (*csp->config->from == '.')
1063 log_error(LOG_LEVEL_HEADER, " modified");
1065 s = strsav( NULL, "From: " );
1066 s = strsav( s, csp->config->from );
1072 /*********************************************************************
1074 * Function : client_send_cookie
1076 * Description : Handle the "cookie" header properly. Called from `sed'.
1077 * If cookie is accepted, add it to the cookie_list,
1078 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1081 * 1 : v = pattern of cookie `sed' found matching
1082 * 2 : s = header (from sed) to "crunch"
1083 * 3 : csp = Current client state (buffers, headers, etc...)
1085 * Returns : Always NULL.
1087 *********************************************************************/
1088 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
1090 if (csp->permissions & PERMIT_COOKIE_READ)
1092 enlist(csp->cookie_list, s + v->len + 1);
1096 log_error(LOG_LEVEL_HEADER, " crunch!");
1100 * Always return NULL here. The cookie header
1101 * will be sent at the end of the header.
1108 /*********************************************************************
1110 * Function : client_x_forwarded
1112 * Description : Handle the "x-forwarded-for" config setting properly,
1113 * also used in the add_client_headers list. Called from `sed'.
1117 * 2 : s = header (from sed) to "crunch"
1118 * 3 : csp = Current client state (buffers, headers, etc...)
1120 * Returns : Always NULL.
1122 *********************************************************************/
1123 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1125 if (csp->config->add_forwarded)
1127 csp->x_forwarded = strdup(s);
1131 * Always return NULL, since this information
1132 * will be sent at the end of the header.
1139 #if defined(DETECT_MSIE_IMAGES)
1140 /*********************************************************************
1142 * Function : client_accept
1144 * Description : Detect whether the client wants HTML or an image.
1145 * Clients do not always make this information available
1146 * in a sane way. Always passes the header through
1147 * the proxy unchanged.
1151 * 2 : s = Header string. Null terminated.
1152 * 3 : csp = Current client state (buffers, headers, etc...)
1154 * Returns : Duplicate of argument s.
1156 *********************************************************************/
1157 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1159 #ifdef DETECT_MSIE_IMAGES
1160 if (strstr (s, "image/gif"))
1162 /* Client will accept HTML. If this seems counterintuitive,
1165 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1169 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1171 #endif /* def DETECT_MSIE_IMAGES */
1176 #endif /* defined(DETECT_MSIE_IMAGES) */
1180 /* the following functions add headers directly to the header list */
1183 /*********************************************************************
1185 * Function : client_cookie_adder
1187 * Description : Used in the add_client_headers list. Called from `sed'.
1190 * 1 : csp = Current client state (buffers, headers, etc...)
1194 *********************************************************************/
1195 void client_cookie_adder(struct client_state *csp)
1201 for (l = csp->cookie_list->next; l ; l = l->next)
1205 tmp = strsav(tmp, "; ");
1207 tmp = strsav(tmp, l->str);
1210 for (l = csp->config->wafer_list->next; l ; l = l->next)
1214 tmp = strsav(tmp, "; ");
1217 if ((e = cookie_encode(l->str)))
1219 tmp = strsav(tmp, e);
1228 ret = strdup("Cookie: ");
1229 ret = strsav(ret, tmp);
1230 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1231 enlist(csp->headers, ret);
1239 /*********************************************************************
1241 * Function : client_xtra_adder
1243 * Description : Used in the add_client_headers list. Called from `sed'.
1246 * 1 : csp = Current client state (buffers, headers, etc...)
1250 *********************************************************************/
1251 void client_xtra_adder(struct client_state *csp)
1255 for (l = csp->config->xtra_list->next; l ; l = l->next)
1257 log_error(LOG_LEVEL_HEADER, "addh: %s", l->str);
1258 enlist(csp->headers, l->str);
1264 /*********************************************************************
1266 * Function : client_x_forwarded_adder
1268 * Description : Used in the add_client_headers list. Called from `sed'.
1271 * 1 : csp = Current client state (buffers, headers, etc...)
1275 *********************************************************************/
1276 void client_x_forwarded_adder(struct client_state *csp)
1280 if (csp->config->add_forwarded == 0)
1285 if (csp->x_forwarded)
1287 p = strsav(p, csp->x_forwarded);
1288 p = strsav(p, ", ");
1289 p = strsav(p, csp->ip_addr_str);
1293 p = strsav(p, "X-Forwarded-For: ");
1294 p = strsav(p, csp->ip_addr_str);
1297 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1298 enlist(csp->headers, p);
1303 /*********************************************************************
1305 * Function : server_set_cookie
1307 * Description : Handle the server "cookie" header properly.
1308 * Log cookie to the jar file. Then "crunch" it,
1309 * or accept it. Called from `sed'.
1312 * 1 : v = parser pattern that matched this header
1313 * 2 : s = header that matched this pattern
1314 * 3 : csp = Current client state (buffers, headers, etc...)
1316 * Returns : `crumble' or a newly malloc'ed string.
1318 *********************************************************************/
1319 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1322 if (csp->config->jar)
1324 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1326 #endif /* def JAR_FILES */
1328 if (!(csp->permissions & PERMIT_COOKIE_SET))
1330 return(crumble(v, s, csp));
1339 /*********************************************************************
1341 * Function : client_host
1343 * Description : Clean the FORCE_PREFIX out of the 'host' http
1344 * header, if we use force
1348 * 2 : s = header (from sed) to clean
1349 * 3 : csp = Current client state (buffers, headers, etc...)
1351 * Returns : A malloc'ed pointer to the cleaned host header
1353 *********************************************************************/
1354 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1356 char *cleanhost = strdup(s);
1359 strclean(cleanhost, FORCE_PREFIX);
1363 #endif /* def FORCE_LOAD */
1367 /*********************************************************************
1369 * Function : strclean
1371 * Description : In-Situ-Eliminate all occurances of substring in
1375 * 1 : string = string to clean
1376 * 2 : substring = substring to eliminate
1378 * Returns : Number of eliminations
1380 *********************************************************************/
1381 int strclean(const char *string, const char *substring)
1383 int hits = 0, len = strlen(substring);
1386 while((pos = strstr(string, substring)))
1393 while (*p++ != '\0');
1400 #endif /* def FORCE_LOAD */