1 const char parsers_rcs[] = "$Id: parsers.c,v 1.5 2001/05/26 00:28:36 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', `match', `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.5 2001/05/26 00:28:36 jongfoster
45 * Automatic reloading of config file.
46 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
47 * Most of the global variables have been moved to a new
48 * struct configuration_spec, accessed through csp->config->globalname
49 * Most of the globals remaining are used by the Win32 GUI.
51 * Revision 1.4 2001/05/22 18:46:04 oes
53 * - Enabled filtering banners by size rather than URL
54 * by adding patterns that replace all standard banner
55 * sizes with the "Junkbuster" gif to the re_filterfile
57 * - Enabled filtering WebBugs by providing a pattern
58 * which kills all 1x1 images
60 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
61 * which is selected by the (nonstandard and therefore
62 * capital) letter 'U' in the option string.
63 * It causes the quantifiers to be ungreedy by default.
64 * Appending a ? turns back to greedy (!).
66 * - Added a new interceptor ijb-send-banner, which
67 * sends back the "Junkbuster" gif. Without imagelist or
68 * MSIE detection support, or if tinygif = 1, or the
69 * URL isn't recognized as an imageurl, a lame HTML
70 * explanation is sent instead.
72 * - Added new feature, which permits blocking remote
73 * script redirects and firing back a local redirect
75 * The feature is conditionally compiled, i.e. it
76 * can be disabled with --disable-fast-redirects,
77 * plus it must be activated by a "fast-redirects"
78 * line in the config file, has its own log level
79 * and of course wants to be displayed by show-proxy-args
80 * Note: Boy, all the #ifdefs in 1001 locations and
81 * all the fumbling with configure.in and acconfig.h
82 * were *way* more work than the feature itself :-(
84 * - Because a generic redirect template was needed for
85 * this, tinygif = 3 now uses the same.
87 * - Moved GIFs, and other static HTTP response templates
92 * - Removed some >400 CRs again (Jon, you really worked
95 * Revision 1.3 2001/05/20 01:21:20 jongfoster
96 * Version 2.9.4 checkin.
97 * - Merged popupfile and cookiefile, and added control over PCRS
98 * filtering, in new "permissionsfile".
99 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
100 * file error you now get a message box (in the Win32 GUI) rather
101 * than the program exiting with no explanation.
102 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
104 * - Removed tabs from "config"
105 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
106 * - Bumped up version number.
108 * Revision 1.2 2001/05/17 23:02:36 oes
109 * - Made referrer option accept 'L' as a substitute for 'ยง'
111 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
112 * Initial import of version 2.9.3 source tree
115 *********************************************************************/
121 #include <sys/types.h>
135 #include "showargs.h"
139 #include "jbsockets.h"
140 #include "miscutil.h"
142 const char parsers_h_rcs[] = PARSERS_H_VERSION;
144 /* Fix a problem with Solaris. There should be no effect on other
146 * Solaris's isspace() is a macro which uses it's argument directly
147 * as an array index. Therefore we need to make sure that high-bit
148 * characters generate +ve values, and ideally we also want to make
149 * the argument match the declared parameter type of "int".
151 * Why did they write a character function that can't take a simple
152 * "char" argument? Doh!
154 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
155 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
158 const struct parsers client_patterns[] = {
159 { "referer:", 8, client_referrer },
160 { "user-agent:", 11, client_uagent },
161 { "ua-", 3, client_ua },
162 { "from:", 5, client_from },
163 { "cookie:", 7, client_send_cookie },
164 { "x-forwarded-for:", 16, client_x_forwarded },
165 { "proxy-connection:", 17, crumble },
167 { "Accept-Encoding: gzip", 21, crumble },
168 #endif /* def DENY_GZIP */
169 #if defined(DETECT_MSIE_IMAGES)
170 { "Accept:", 7, client_accept },
171 #endif /* defined(DETECT_MSIE_IMAGES) */
173 { "Host:", 5, client_host },
174 #endif /* def FORCE_LOAD */
175 /* { "if-modified-since:", 18, crumble }, */
179 const struct interceptors intercept_patterns[] = {
180 { "show-proxy-args", 14, show_proxy_args },
181 { "ijb-send-banner", 14, ijb_send_banner },
183 { "ij-untrusted-url", 15, ij_untrusted_url },
184 #endif /* def TRUST_FILES */
188 const struct parsers server_patterns[] = {
189 { "set-cookie:", 11, server_set_cookie },
190 { "connection:", 11, crumble },
191 #if defined(PCRS) || defined(KILLPOPUPS)
192 { "Content-Type:", 13, content_type },
193 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
195 { "Content-Length:", 15, content_length },
196 #endif /* def PCRS */
201 void (* const add_client_headers[])(struct client_state *) = {
203 client_x_forwarded_adder,
209 void (* const add_server_headers[])(struct client_state *) = {
214 /*********************************************************************
218 * Description : Do a `strncmpic' on every pattern in pats.
221 * 1 : buf = a string to match to a list of patterns
222 * 2 : pats = list of strings to compare against buf.
224 * Returns : Return the matching "struct parsers *",
225 * or NULL if no pattern matches.
227 *********************************************************************/
228 static const struct parsers *match(char *buf, const struct parsers *pats)
230 const struct parsers *v;
235 log_error(LOG_LEVEL_ERROR, "NULL parameter to match()");
239 for (v = pats; v->str ; v++)
241 if (strncmpic(buf, v->str, v->len) == 0)
251 /*********************************************************************
253 * Function : flush_socket
255 * Description : Write any pending "buffered" content.
258 * 1 : fd = file descriptor of the socket to read
259 * 2 : csp = Current client state (buffers, headers, etc...)
261 * Returns : On success, the number of bytes written are returned (zero
262 * indicates nothing was written). On error, -1 is returned,
263 * and errno is set appropriately. If count is zero and the
264 * file descriptor refers to a regular file, 0 will be
265 * returned without causing any other effect. For a special
266 * file, the results are not portable.
268 *********************************************************************/
269 int flush_socket(int fd, struct client_state *csp)
271 struct iob *iob = csp->iob;
272 int n = iob->eod - iob->cur;
279 n = write_socket(fd, iob->cur, n);
280 iob->eod = iob->cur = iob->buf;
286 /*********************************************************************
288 * Function : add_to_iob
290 * Description : Add content to the buffered page.
293 * 1 : csp = Current client state (buffers, headers, etc...)
294 * 2 : buf = holds the content to be added to the page
295 * 3 : n = number of bytes to be added
297 * Returns : Number of bytes in the content buffer.
299 *********************************************************************/
300 int add_to_iob(struct client_state *csp, char *buf, int n)
302 struct iob *iob = csp->iob;
306 have = iob->eod - iob->cur;
315 if ((p = (char *)malloc(need + 1)) == NULL)
317 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
323 /* there is something in the buffer - save it */
324 memcpy(p, iob->cur, have);
326 /* replace the buffer with the new space */
330 /* point to the end of the data */
335 /* the buffer is empty, free it and reinitialize */
340 /* copy the new data into the iob buffer */
343 /* point to the end of the data */
346 /* null terminate == cheap insurance */
349 /* set the pointers to the new values */
358 /*********************************************************************
360 * Function : get_header
362 * Description : This (odd) routine will parse the csp->iob
365 * 1 : csp = Current client state (buffers, headers, etc...)
367 * Returns : Any one of the following:
369 * 1) a pointer to a dynamically allocated string that contains a header line
370 * 2) NULL indicating that the end of the header was reached
371 * 3) "" indicating that the end of the iob was reached before finding
372 * a complete header line.
374 *********************************************************************/
375 char *get_header(struct client_state *csp)
381 if ((iob->cur == NULL)
382 || ((p = strchr(iob->cur, '\n')) == NULL))
384 return(""); /* couldn't find a complete header */
389 ret = strdup(iob->cur);
393 if ((q = strchr(ret, '\r'))) *q = '\0';
395 /* is this a blank linke (i.e. the end of the header) ? */
407 /*********************************************************************
411 * Description : Append a string into a specified string list.
414 * 1 : h = pointer to list 'dummy' header
415 * 2 : s = string to add to the list
419 *********************************************************************/
420 void enlist(struct list *h, const char *s)
422 struct list *n = (struct list *)malloc(sizeof(*n));
445 /*********************************************************************
447 * Function : destroy_list
449 * Description : Destroy a string list (opposite of enlist)
452 * 1 : h = pointer to list 'dummy' header
456 *********************************************************************/
457 void destroy_list(struct list *h)
461 for (p = h->next; p ; p = n)
468 memset(h, '\0', sizeof(*h));
473 /*********************************************************************
475 * Function : list_to_text
477 * Description : "Flaten" a string list into 1 long \r\n delimited string.
480 * 1 : h = pointer to list 'dummy' header
482 * Returns : NULL on malloc error, else new long string.
484 *********************************************************************/
485 static char *list_to_text(struct list *h)
494 for (p = h->next; p ; p = p->next)
498 size += strlen(p->str) + 2;
502 if ((ret = (char *)malloc(size + 1)) == NULL)
511 for (p = h->next; p ; p = p->next)
517 *s++ = '\r'; *s++ = '\n';
526 /*********************************************************************
530 * Description : add, delete or modify lines in the HTTP header streams.
531 * On entry, it receives a linked list of headers space
532 * that was allocated dynamically (both the list nodes
533 * and the header contents).
535 * As a side effect it frees the space used by the original
539 * 1 : pats = list of patterns to match against headers
540 * 2 : more_headers = list of functions to add more
541 * headers (client or server)
542 * 3 : csp = Current client state (buffers, headers, etc...)
544 * Returns : Single pointer to a fully formed header.
546 *********************************************************************/
547 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
550 const struct parsers *v;
554 for (p = csp->headers->next; p ; p = p->next)
556 log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
558 if ((v = match(p->str, pats)))
560 hdr = v->parser(v, p->str, csp);
567 /* place any additional headers on the csp->headers list */
568 for (f = more_headers; *f ; f++)
573 /* add the blank line at the end of the header */
574 enlist(csp->headers, "");
576 hdr = list_to_text(csp->headers);
583 /*********************************************************************
585 * Function : free_http_request
587 * Description : Freez a http_request structure
590 * 1 : http = points to a http_request structure to free
594 *********************************************************************/
595 void free_http_request(struct http_request *http)
600 freez(http->hostport);
607 /*********************************************************************
609 * Function : parse_http_request
611 * Description : Parse out the host and port from the URL. Find the
612 * hostname & path, port (if ':'), and/or password (if '@')
615 * 1 : req = URL (or is it URI?) to break down
616 * 2 : http = pointer to the http structure to hold elements
617 * 3 : csp = Current client state (buffers, headers, etc...)
621 *********************************************************************/
622 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
624 char *buf, *v[10], *url, *p;
627 memset(http, '\0', sizeof(*http));
629 http->cmd = strdup(req);
633 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
637 /* this could be a CONNECT request */
638 if (strcmpic(v[0], "connect") == 0)
641 http->gpc = strdup(v[0]);
642 http->hostport = strdup(v[1]);
643 http->ver = strdup(v[2]);
648 /* This next line is a little ugly, but it simplifies the if statement below. */
649 /* Basically if using webDAV, we want the OR condition to use these too. */
653 * These are the headers as defined in RFC2518 to add webDAV support
656 #define OR_WEBDAV || \
657 (0 == strcmpic(v[0], "propfind")) || \
658 (0 == strcmpic(v[0], "proppatch")) || \
659 (0 == strcmpic(v[0], "move")) || \
660 (0 == strcmpic(v[0], "copy")) || \
661 (0 == strcmpic(v[0], "mkcol")) || \
662 (0 == strcmpic(v[0], "lock")) || \
663 (0 == strcmpic(v[0], "unlock"))
665 #else /* No webDAV support is enabled. Provide an empty OR_WEBDAV macro. */
671 /* or it could be a GET or a POST (possibly webDAV too) */
672 if ((strcmpic(v[0], "get") == 0) ||
673 (strcmpic(v[0], "head") == 0) OR_WEBDAV ||
674 (strcmpic(v[0], "post") == 0))
677 http->gpc = strdup(v[0]);
679 http->ver = strdup(v[2]);
681 if (strncmpic(url, "http://", 7) == 0)
685 else if (strncmpic(url, "https://", 8) == 0)
694 if (url && (p = strchr(url, '/')))
696 http->path = strdup(p);
698 http->hostport = strdup(url);
706 if (http->hostport == NULL)
708 free_http_request(http);
712 buf = strdup(http->hostport);
715 /* check if url contains password */
716 n = ssplit(buf, "@", v, SZ(v), 1, 1);
719 char * newbuf = NULL;
720 newbuf = strdup(v[1]);
725 n = ssplit(buf, ":", v, SZ(v), 1, 1);
729 http->host = strdup(v[0]);
735 http->host = strdup(v[0]);
736 http->port = atoi(v[1]);
741 if (http->host == NULL)
743 free_http_request(http);
746 if (http->path == NULL)
748 http->path = strdup("");
754 /* here begins the family of parser functions that reformat header lines */
757 /*********************************************************************
761 * Description : This is called if a header matches a pattern to "crunch"
764 * 1 : v = Pointer to parsers structure, which basically holds
765 * headers (client or server) that we want to "crunch"
766 * 2 : s = header (from sed) to "crunch"
767 * 3 : csp = Current client state (buffers, headers, etc...)
769 * Returns : Always NULL.
771 *********************************************************************/
772 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
774 log_error(LOG_LEVEL_HEADER, "crunch!");
780 #if defined(PCRS) || defined(KILLPOPUPS)
782 /*********************************************************************
784 * Function : content_type
786 * Description : Is this a text/* or javascript MIME Type?
790 * 2 : s = header string we are "considering"
791 * 3 : csp = Current client state (buffers, headers, etc...)
793 * Returns : A duplicate string pointer to this header (ie. pass thru)
795 *********************************************************************/
796 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
798 if (strstr (s, " text/") || strstr (s, "application/x-javascript"))
806 #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->permissions & PERMIT_RE_FILTER) != 0)
828 log_error(LOG_LEVEL_HEADER, "crunch!");
837 #endif /* def PCRS */
840 /*********************************************************************
842 * Function : client_referrer
844 * Description : Handle the "referer" config setting properly.
849 * 2 : s = header (from sed) to "crunch"
850 * 3 : csp = Current client state (buffers, headers, etc...)
852 * Returns : NULL if crunched, or a malloc'ed string with the original
855 *********************************************************************/
856 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
859 /* Since the referrer can include the prefix even
860 * even if the request itself is non-forced, we must
861 * clean it unconditionally
863 strclean(s, FORCE_PREFIX);
864 #endif /* def FORCE_LOAD */
866 csp->referrer = strdup(s);
868 if (csp->config->referrer == NULL)
870 log_error(LOG_LEVEL_HEADER, "crunch!");
874 if (*csp->config->referrer == '.')
879 if (*csp->config->referrer == '@')
881 if (csp->permissions & PERMIT_COOKIE_READ)
887 log_error(LOG_LEVEL_HEADER, "crunch!");
893 * New option ยง or L: Forge a referer as http://[hostname:port of REQUEST]/
894 * to fool stupid checks for in-site links
897 if (*csp->config->referrer == 'ยง' || *csp->config->referrer == 'L')
899 if (csp->permissions & PERMIT_COOKIE_READ)
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 log_error(LOG_LEVEL_HEADER, "modified");
916 s = strsav( NULL, "Referer: " );
917 s = strsav( s, csp->config->referrer );
923 /*********************************************************************
925 * Function : client_uagent
927 * Description : Handle the "user-agent" config setting properly.
932 * 2 : s = header (from sed) to "crunch"
933 * 3 : csp = Current client state (buffers, headers, etc...)
935 * Returns : A malloc'ed pointer to the default agent, or
936 * a malloc'ed string pointer to this header (ie. pass thru).
938 *********************************************************************/
939 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
941 #ifdef DETECT_MSIE_IMAGES
942 if (strstr (s, "MSIE "))
944 /* This is Microsoft Internet Explorer.
945 * Enable auto-detect.
947 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
949 #endif /* def DETECT_MSIE_IMAGES */
951 if (csp->config->uagent == NULL)
953 log_error(LOG_LEVEL_HEADER, "default");
954 return(strdup(DEFAULT_USER_AGENT));
957 if (*csp->config->uagent == '.')
962 if (*csp->config->uagent == '@')
964 if (csp->permissions & PERMIT_COOKIE_READ)
970 log_error(LOG_LEVEL_HEADER, "default");
971 return(strdup(DEFAULT_USER_AGENT));
975 log_error(LOG_LEVEL_HEADER, "modified");
977 s = strsav( NULL, "User-Agent: " );
978 s = strsav( s, csp->config->uagent );
984 /*********************************************************************
986 * Function : client_ua
988 * Description : Handle "ua-" headers properly. Called from `sed'.
992 * 2 : s = header (from sed) to "crunch"
993 * 3 : csp = Current client state (buffers, headers, etc...)
995 * Returns : NULL if crunched, or a malloc'ed string to original header
997 *********************************************************************/
998 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
1000 if (csp->config->uagent == NULL)
1002 log_error(LOG_LEVEL_HEADER, "crunch!");
1006 if (*csp->config->uagent == '.')
1011 if (*csp->config->uagent == '@')
1013 if (csp->permissions & PERMIT_COOKIE_READ)
1019 log_error(LOG_LEVEL_HEADER, "crunch!");
1024 log_error(LOG_LEVEL_HEADER, "crunch!");
1030 /*********************************************************************
1032 * Function : client_from
1034 * Description : Handle the "from" config setting properly.
1035 * Called from `sed'.
1039 * 2 : s = header (from sed) to "crunch"
1040 * 3 : csp = Current client state (buffers, headers, etc...)
1042 * Returns : NULL if crunched, or a malloc'ed string to
1043 * modified/original header.
1045 *********************************************************************/
1046 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
1048 /* if not set, zap it */
1049 if (csp->config->from == NULL)
1051 log_error(LOG_LEVEL_HEADER, "crunch!");
1055 if (*csp->config->from == '.')
1060 log_error(LOG_LEVEL_HEADER, " modified");
1062 s = strsav( NULL, "From: " );
1063 s = strsav( s, csp->config->from );
1069 /*********************************************************************
1071 * Function : client_send_cookie
1073 * Description : Handle the "cookie" header properly. Called from `sed'.
1074 * If cookie is accepted, add it to the cookie_list,
1075 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1078 * 1 : v = pattern of cookie `sed' found matching
1079 * 2 : s = header (from sed) to "crunch"
1080 * 3 : csp = Current client state (buffers, headers, etc...)
1082 * Returns : Always NULL.
1084 *********************************************************************/
1085 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
1087 if (csp->permissions & PERMIT_COOKIE_READ)
1089 enlist(csp->cookie_list, s + v->len + 1);
1093 log_error(LOG_LEVEL_HEADER, " crunch!");
1097 * Always return NULL here. The cookie header
1098 * will be sent at the end of the header.
1105 /*********************************************************************
1107 * Function : client_x_forwarded
1109 * Description : Handle the "x-forwarded-for" config setting properly,
1110 * also used in the add_client_headers list. Called from `sed'.
1114 * 2 : s = header (from sed) to "crunch"
1115 * 3 : csp = Current client state (buffers, headers, etc...)
1117 * Returns : Always NULL.
1119 *********************************************************************/
1120 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1122 if (csp->config->add_forwarded)
1124 csp->x_forwarded = strdup(s);
1128 * Always return NULL, since this information
1129 * will be sent at the end of the header.
1136 #if defined(DETECT_MSIE_IMAGES)
1137 /*********************************************************************
1139 * Function : client_accept
1141 * Description : Detect whether the client wants HTML or an image.
1142 * Clients do not always make this information available
1143 * in a sane way. Always passes the header through
1144 * the proxy unchanged.
1148 * 2 : s = Header string. Null terminated.
1149 * 3 : csp = Current client state (buffers, headers, etc...)
1151 * Returns : Duplicate of argument s.
1153 *********************************************************************/
1154 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1156 #ifdef DETECT_MSIE_IMAGES
1157 if (strstr (s, "image/gif"))
1159 /* Client will accept HTML. If this seems counterintuitive,
1162 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1166 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1168 #endif /* def DETECT_MSIE_IMAGES */
1173 #endif /* defined(DETECT_MSIE_IMAGES) */
1177 /* the following functions add headers directly to the header list */
1180 /*********************************************************************
1182 * Function : client_cookie_adder
1184 * Description : Used in the add_client_headers list. Called from `sed'.
1187 * 1 : csp = Current client state (buffers, headers, etc...)
1191 *********************************************************************/
1192 void client_cookie_adder(struct client_state *csp)
1198 for (l = csp->cookie_list->next; l ; l = l->next)
1202 tmp = strsav(tmp, "; ");
1204 tmp = strsav(tmp, l->str);
1207 for (l = csp->config->wafer_list->next; l ; l = l->next)
1211 tmp = strsav(tmp, "; ");
1214 if ((e = cookie_encode(l->str)))
1216 tmp = strsav(tmp, e);
1225 ret = strdup("Cookie: ");
1226 ret = strsav(ret, tmp);
1227 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1228 enlist(csp->headers, ret);
1236 /*********************************************************************
1238 * Function : client_xtra_adder
1240 * Description : Used in the add_client_headers list. Called from `sed'.
1243 * 1 : csp = Current client state (buffers, headers, etc...)
1247 *********************************************************************/
1248 void client_xtra_adder(struct client_state *csp)
1252 for (l = csp->config->xtra_list->next; l ; l = l->next)
1254 log_error(LOG_LEVEL_HEADER, "addh: %s", l->str);
1255 enlist(csp->headers, l->str);
1261 /*********************************************************************
1263 * Function : client_x_forwarded_adder
1265 * Description : Used in the add_client_headers list. Called from `sed'.
1268 * 1 : csp = Current client state (buffers, headers, etc...)
1272 *********************************************************************/
1273 void client_x_forwarded_adder(struct client_state *csp)
1277 if (csp->config->add_forwarded == 0)
1282 if (csp->x_forwarded)
1284 p = strsav(p, csp->x_forwarded);
1285 p = strsav(p, ", ");
1286 p = strsav(p, csp->ip_addr_str);
1290 p = strsav(p, "X-Forwarded-For: ");
1291 p = strsav(p, csp->ip_addr_str);
1294 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1295 enlist(csp->headers, p);
1300 /*********************************************************************
1302 * Function : server_set_cookie
1304 * Description : Handle the server "cookie" header properly.
1305 * Log cookie to the jar file. Then "crunch" it,
1306 * or accept it. Called from `sed'.
1309 * 1 : v = parser pattern that matched this header
1310 * 2 : s = header that matched this pattern
1311 * 3 : csp = Current client state (buffers, headers, etc...)
1313 * Returns : `crumble' or a newly malloc'ed string.
1315 *********************************************************************/
1316 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1319 if (csp->config->jar)
1321 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1323 #endif /* def JAR_FILES */
1325 if (!(csp->permissions & PERMIT_COOKIE_SET))
1327 return(crumble(v, s, csp));
1336 /*********************************************************************
1338 * Function : client_host
1340 * Description : Clean the FORCE_PREFIX out of the 'host' http
1341 * header, if we use force
1345 * 2 : s = header (from sed) to clean
1346 * 3 : csp = Current client state (buffers, headers, etc...)
1348 * Returns : A malloc'ed pointer to the cleaned host header
1350 *********************************************************************/
1351 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1353 char *cleanhost = strdup(s);
1356 strclean(cleanhost, FORCE_PREFIX);
1360 #endif /* def FORCE_LOAD */
1364 /*********************************************************************
1366 * Function : strclean
1368 * Description : In-Situ-Eliminate all occurances of substring in
1372 * 1 : string = string to clean
1373 * 2 : substring = substring to eliminate
1375 * Returns : Number of eliminations
1377 *********************************************************************/
1378 int strclean(const char *string, const char *substring)
1380 int hits = 0, len = strlen(substring);
1383 while((pos = strstr(string, substring)))
1390 while (*p++ != '\0');
1397 #endif /* def FORCE_LOAD */