1 const char parsers_rcs[] = "$Id: parsers.c,v 1.7 2001/05/27 13:19:06 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.7 2001/05/27 13:19:06 oes
45 * Patched Joergs solution for the content-length in.
47 * Revision 1.6 2001/05/26 13:39:32 jongfoster
48 * Only crunches Content-Length header if applying RE filtering.
49 * Without this fix, Microsoft Windows Update wouldn't work.
51 * Revision 1.5 2001/05/26 00:28:36 jongfoster
52 * Automatic reloading of config file.
53 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
54 * Most of the global variables have been moved to a new
55 * struct configuration_spec, accessed through csp->config->globalname
56 * Most of the globals remaining are used by the Win32 GUI.
58 * Revision 1.4 2001/05/22 18:46:04 oes
60 * - Enabled filtering banners by size rather than URL
61 * by adding patterns that replace all standard banner
62 * sizes with the "Junkbuster" gif to the re_filterfile
64 * - Enabled filtering WebBugs by providing a pattern
65 * which kills all 1x1 images
67 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
68 * which is selected by the (nonstandard and therefore
69 * capital) letter 'U' in the option string.
70 * It causes the quantifiers to be ungreedy by default.
71 * Appending a ? turns back to greedy (!).
73 * - Added a new interceptor ijb-send-banner, which
74 * sends back the "Junkbuster" gif. Without imagelist or
75 * MSIE detection support, or if tinygif = 1, or the
76 * URL isn't recognized as an imageurl, a lame HTML
77 * explanation is sent instead.
79 * - Added new feature, which permits blocking remote
80 * script redirects and firing back a local redirect
82 * The feature is conditionally compiled, i.e. it
83 * can be disabled with --disable-fast-redirects,
84 * plus it must be activated by a "fast-redirects"
85 * line in the config file, has its own log level
86 * and of course wants to be displayed by show-proxy-args
87 * Note: Boy, all the #ifdefs in 1001 locations and
88 * all the fumbling with configure.in and acconfig.h
89 * were *way* more work than the feature itself :-(
91 * - Because a generic redirect template was needed for
92 * this, tinygif = 3 now uses the same.
94 * - Moved GIFs, and other static HTTP response templates
99 * - Removed some >400 CRs again (Jon, you really worked
102 * Revision 1.3 2001/05/20 01:21:20 jongfoster
103 * Version 2.9.4 checkin.
104 * - Merged popupfile and cookiefile, and added control over PCRS
105 * filtering, in new "permissionsfile".
106 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
107 * file error you now get a message box (in the Win32 GUI) rather
108 * than the program exiting with no explanation.
109 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
111 * - Removed tabs from "config"
112 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
113 * - Bumped up version number.
115 * Revision 1.2 2001/05/17 23:02:36 oes
116 * - Made referrer option accept 'L' as a substitute for 'ยง'
118 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
119 * Initial import of version 2.9.3 source tree
122 *********************************************************************/
128 #include <sys/types.h>
142 #include "showargs.h"
146 #include "jbsockets.h"
147 #include "miscutil.h"
149 const char parsers_h_rcs[] = PARSERS_H_VERSION;
151 /* Fix a problem with Solaris. There should be no effect on other
153 * Solaris's isspace() is a macro which uses it's argument directly
154 * as an array index. Therefore we need to make sure that high-bit
155 * characters generate +ve values, and ideally we also want to make
156 * the argument match the declared parameter type of "int".
158 * Why did they write a character function that can't take a simple
159 * "char" argument? Doh!
161 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
162 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
165 const struct parsers client_patterns[] = {
166 { "referer:", 8, client_referrer },
167 { "user-agent:", 11, client_uagent },
168 { "ua-", 3, client_ua },
169 { "from:", 5, client_from },
170 { "cookie:", 7, client_send_cookie },
171 { "x-forwarded-for:", 16, client_x_forwarded },
172 { "proxy-connection:", 17, crumble },
174 { "Accept-Encoding: gzip", 21, crumble },
175 #endif /* def DENY_GZIP */
176 #if defined(DETECT_MSIE_IMAGES)
177 { "Accept:", 7, client_accept },
178 #endif /* defined(DETECT_MSIE_IMAGES) */
180 { "Host:", 5, client_host },
181 #endif /* def FORCE_LOAD */
182 /* { "if-modified-since:", 18, crumble }, */
186 const struct interceptors intercept_patterns[] = {
187 { "show-proxy-args", 14, show_proxy_args },
188 { "ijb-send-banner", 14, ijb_send_banner },
190 { "ij-untrusted-url", 15, ij_untrusted_url },
191 #endif /* def TRUST_FILES */
195 const struct parsers server_patterns[] = {
196 { "set-cookie:", 11, server_set_cookie },
197 { "connection:", 11, crumble },
198 #if defined(PCRS) || defined(KILLPOPUPS)
199 { "Content-Type:", 13, content_type },
200 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
202 { "Content-Length:", 15, content_length },
203 #endif /* def PCRS */
208 void (* const add_client_headers[])(struct client_state *) = {
210 client_x_forwarded_adder,
216 void (* const add_server_headers[])(struct client_state *) = {
221 /*********************************************************************
223 * Function : flush_socket
225 * Description : Write any pending "buffered" content.
228 * 1 : fd = file descriptor of the socket to read
229 * 2 : csp = Current client state (buffers, headers, etc...)
231 * Returns : On success, the number of bytes written are returned (zero
232 * indicates nothing was written). On error, -1 is returned,
233 * and errno is set appropriately. If count is zero and the
234 * file descriptor refers to a regular file, 0 will be
235 * returned without causing any other effect. For a special
236 * file, the results are not portable.
238 *********************************************************************/
239 int flush_socket(int fd, struct client_state *csp)
241 struct iob *iob = csp->iob;
242 int n = iob->eod - iob->cur;
249 n = write_socket(fd, iob->cur, n);
250 iob->eod = iob->cur = iob->buf;
256 /*********************************************************************
258 * Function : add_to_iob
260 * Description : Add content to the buffered page.
263 * 1 : csp = Current client state (buffers, headers, etc...)
264 * 2 : buf = holds the content to be added to the page
265 * 3 : n = number of bytes to be added
267 * Returns : Number of bytes in the content buffer.
269 *********************************************************************/
270 int add_to_iob(struct client_state *csp, char *buf, int n)
272 struct iob *iob = csp->iob;
276 have = iob->eod - iob->cur;
285 if ((p = (char *)malloc(need + 1)) == NULL)
287 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
293 /* there is something in the buffer - save it */
294 memcpy(p, iob->cur, have);
296 /* replace the buffer with the new space */
300 /* point to the end of the data */
305 /* the buffer is empty, free it and reinitialize */
310 /* copy the new data into the iob buffer */
313 /* point to the end of the data */
316 /* null terminate == cheap insurance */
319 /* set the pointers to the new values */
328 /*********************************************************************
330 * Function : get_header
332 * Description : This (odd) routine will parse the csp->iob
335 * 1 : csp = Current client state (buffers, headers, etc...)
337 * Returns : Any one of the following:
339 * 1) a pointer to a dynamically allocated string that contains a header line
340 * 2) NULL indicating that the end of the header was reached
341 * 3) "" indicating that the end of the iob was reached before finding
342 * a complete header line.
344 *********************************************************************/
345 char *get_header(struct client_state *csp)
351 if ((iob->cur == NULL)
352 || ((p = strchr(iob->cur, '\n')) == NULL))
354 return(""); /* couldn't find a complete header */
359 ret = strdup(iob->cur);
363 if ((q = strchr(ret, '\r'))) *q = '\0';
365 /* is this a blank linke (i.e. the end of the header) ? */
377 /*********************************************************************
381 * Description : Append a string into a specified string list.
384 * 1 : h = pointer to list 'dummy' header
385 * 2 : s = string to add to the list
389 *********************************************************************/
390 void enlist(struct list *h, const char *s)
392 struct list *n = (struct list *)malloc(sizeof(*n));
415 /*********************************************************************
417 * Function : destroy_list
419 * Description : Destroy a string list (opposite of enlist)
422 * 1 : h = pointer to list 'dummy' header
426 *********************************************************************/
427 void destroy_list(struct list *h)
431 for (p = h->next; p ; p = n)
438 memset(h, '\0', sizeof(*h));
443 /*********************************************************************
445 * Function : list_to_text
447 * Description : "Flaten" a string list into 1 long \r\n delimited string.
450 * 1 : h = pointer to list 'dummy' header
452 * Returns : NULL on malloc error, else new long string.
454 *********************************************************************/
455 static char *list_to_text(struct list *h)
464 for (p = h->next; p ; p = p->next)
468 size += strlen(p->str) + 2;
472 if ((ret = (char *)malloc(size + 1)) == NULL)
481 for (p = h->next; p ; p = p->next)
487 *s++ = '\r'; *s++ = '\n';
496 /*********************************************************************
500 * Description : add, delete or modify lines in the HTTP header streams.
501 * On entry, it receives a linked list of headers space
502 * that was allocated dynamically (both the list nodes
503 * and the header contents).
505 * As a side effect it frees the space used by the original
509 * 1 : pats = list of patterns to match against headers
510 * 2 : more_headers = list of functions to add more
511 * headers (client or server)
512 * 3 : csp = Current client state (buffers, headers, etc...)
514 * Returns : Single pointer to a fully formed header.
516 *********************************************************************/
517 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
520 const struct parsers *v;
524 for (v = pats; v->str ; v++)
526 for (p = csp->headers->next; p ; p = p->next)
528 /* Header crunch()ed in previous run? -> ignore */
529 if (p->str == NULL) continue;
531 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
533 if (strncmpic(p->str, v->str, v->len) == 0)
535 hdr = v->parser(v, p->str, csp);
542 /* place any additional headers on the csp->headers list */
543 for (f = more_headers; *f ; f++)
548 /* add the blank line at the end of the header, if necessary */
549 if(strlen(csp->headers->last->str) != 0)
551 enlist(csp->headers, "");
554 hdr = list_to_text(csp->headers);
561 /*********************************************************************
563 * Function : free_http_request
565 * Description : Freez a http_request structure
568 * 1 : http = points to a http_request structure to free
572 *********************************************************************/
573 void free_http_request(struct http_request *http)
578 freez(http->hostport);
585 /*********************************************************************
587 * Function : parse_http_request
589 * Description : Parse out the host and port from the URL. Find the
590 * hostname & path, port (if ':'), and/or password (if '@')
593 * 1 : req = URL (or is it URI?) to break down
594 * 2 : http = pointer to the http structure to hold elements
595 * 3 : csp = Current client state (buffers, headers, etc...)
599 *********************************************************************/
600 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
602 char *buf, *v[10], *url, *p;
605 memset(http, '\0', sizeof(*http));
607 http->cmd = strdup(req);
611 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
615 /* this could be a CONNECT request */
616 if (strcmpic(v[0], "connect") == 0)
619 http->gpc = strdup(v[0]);
620 http->hostport = strdup(v[1]);
621 http->ver = strdup(v[2]);
626 /* This next line is a little ugly, but it simplifies the if statement below. */
627 /* Basically if using webDAV, we want the OR condition to use these too. */
631 * These are the headers as defined in RFC2518 to add webDAV support
634 #define OR_WEBDAV || \
635 (0 == strcmpic(v[0], "propfind")) || \
636 (0 == strcmpic(v[0], "proppatch")) || \
637 (0 == strcmpic(v[0], "move")) || \
638 (0 == strcmpic(v[0], "copy")) || \
639 (0 == strcmpic(v[0], "mkcol")) || \
640 (0 == strcmpic(v[0], "lock")) || \
641 (0 == strcmpic(v[0], "unlock"))
643 #else /* No webDAV support is enabled. Provide an empty OR_WEBDAV macro. */
649 /* or it could be a GET or a POST (possibly webDAV too) */
650 if ((strcmpic(v[0], "get") == 0) ||
651 (strcmpic(v[0], "head") == 0) OR_WEBDAV ||
652 (strcmpic(v[0], "post") == 0))
655 http->gpc = strdup(v[0]);
657 http->ver = strdup(v[2]);
659 if (strncmpic(url, "http://", 7) == 0)
663 else if (strncmpic(url, "https://", 8) == 0)
672 if (url && (p = strchr(url, '/')))
674 http->path = strdup(p);
676 http->hostport = strdup(url);
684 if (http->hostport == NULL)
686 free_http_request(http);
690 buf = strdup(http->hostport);
693 /* check if url contains password */
694 n = ssplit(buf, "@", v, SZ(v), 1, 1);
697 char * newbuf = NULL;
698 newbuf = strdup(v[1]);
703 n = ssplit(buf, ":", v, SZ(v), 1, 1);
707 http->host = strdup(v[0]);
713 http->host = strdup(v[0]);
714 http->port = atoi(v[1]);
719 if (http->host == NULL)
721 free_http_request(http);
724 if (http->path == NULL)
726 http->path = strdup("");
732 /* here begins the family of parser functions that reformat header lines */
735 /*********************************************************************
739 * Description : This is called if a header matches a pattern to "crunch"
742 * 1 : v = Pointer to parsers structure, which basically holds
743 * headers (client or server) that we want to "crunch"
744 * 2 : s = header (from sed) to "crunch"
745 * 3 : csp = Current client state (buffers, headers, etc...)
747 * Returns : Always NULL.
749 *********************************************************************/
750 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
752 log_error(LOG_LEVEL_HEADER, "crunch!");
758 #if defined(PCRS) || defined(KILLPOPUPS)
760 /*********************************************************************
762 * Function : content_type
764 * Description : Is this a text/* or javascript MIME Type?
768 * 2 : s = header string we are "considering"
769 * 3 : csp = Current client state (buffers, headers, etc...)
771 * Returns : A duplicate string pointer to this header (ie. pass thru)
773 *********************************************************************/
774 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
776 if (strstr (s, " text/") || strstr (s, "application/x-javascript"))
784 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
788 /*********************************************************************
790 * Function : content_length
792 * Description : Crunch Content-Length header if & only if we are
793 * filtering this page through PCRS.
797 * 2 : s = header string we are "considering"
798 * 3 : csp = Current client state (buffers, headers, etc...)
800 * Returns : A duplicate string pointer to this header (ie. pass thru)
802 *********************************************************************/
803 char *content_length(const struct parsers *v, char *s, struct client_state *csp)
805 if (csp->content_length != 0) /* Content has been modified */
807 s = (char *) zalloc(100);
808 snprintf(s, 100, "Content-Length: %d", csp->content_length);
809 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
818 #endif /* def PCRS */
821 /*********************************************************************
823 * Function : client_referrer
825 * Description : Handle the "referer" config setting properly.
830 * 2 : s = header (from sed) to "crunch"
831 * 3 : csp = Current client state (buffers, headers, etc...)
833 * Returns : NULL if crunched, or a malloc'ed string with the original
836 *********************************************************************/
837 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
840 /* Since the referrer can include the prefix even
841 * even if the request itself is non-forced, we must
842 * clean it unconditionally
844 strclean(s, FORCE_PREFIX);
845 #endif /* def FORCE_LOAD */
847 csp->referrer = strdup(s);
849 if (csp->config->referrer == NULL)
851 log_error(LOG_LEVEL_HEADER, "crunch!");
855 if (*csp->config->referrer == '.')
860 if (*csp->config->referrer == '@')
862 if (csp->permissions & PERMIT_COOKIE_READ)
868 log_error(LOG_LEVEL_HEADER, "crunch!");
874 * New option ยง or L: Forge a referer as http://[hostname:port of REQUEST]/
875 * to fool stupid checks for in-site links
878 if (*csp->config->referrer == 'ยง' || *csp->config->referrer == 'L')
880 if (csp->permissions & PERMIT_COOKIE_READ)
886 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
887 s = strsav(NULL, "Referer: ");
888 s = strsav(s, "http://");
889 s = strsav(s, csp->http->hostport);
895 log_error(LOG_LEVEL_HEADER, "modified");
897 s = strsav( NULL, "Referer: " );
898 s = strsav( s, csp->config->referrer );
904 /*********************************************************************
906 * Function : client_uagent
908 * Description : Handle the "user-agent" config setting properly.
913 * 2 : s = header (from sed) to "crunch"
914 * 3 : csp = Current client state (buffers, headers, etc...)
916 * Returns : A malloc'ed pointer to the default agent, or
917 * a malloc'ed string pointer to this header (ie. pass thru).
919 *********************************************************************/
920 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
922 #ifdef DETECT_MSIE_IMAGES
923 if (strstr (s, "MSIE "))
925 /* This is Microsoft Internet Explorer.
926 * Enable auto-detect.
928 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
930 #endif /* def DETECT_MSIE_IMAGES */
932 if (csp->config->uagent == NULL)
934 log_error(LOG_LEVEL_HEADER, "default");
935 return(strdup(DEFAULT_USER_AGENT));
938 if (*csp->config->uagent == '.')
943 if (*csp->config->uagent == '@')
945 if (csp->permissions & PERMIT_COOKIE_READ)
951 log_error(LOG_LEVEL_HEADER, "default");
952 return(strdup(DEFAULT_USER_AGENT));
956 log_error(LOG_LEVEL_HEADER, "modified");
958 s = strsav( NULL, "User-Agent: " );
959 s = strsav( s, csp->config->uagent );
965 /*********************************************************************
967 * Function : client_ua
969 * Description : Handle "ua-" headers properly. Called from `sed'.
973 * 2 : s = header (from sed) to "crunch"
974 * 3 : csp = Current client state (buffers, headers, etc...)
976 * Returns : NULL if crunched, or a malloc'ed string to original header
978 *********************************************************************/
979 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
981 if (csp->config->uagent == NULL)
983 log_error(LOG_LEVEL_HEADER, "crunch!");
987 if (*csp->config->uagent == '.')
992 if (*csp->config->uagent == '@')
994 if (csp->permissions & PERMIT_COOKIE_READ)
1000 log_error(LOG_LEVEL_HEADER, "crunch!");
1005 log_error(LOG_LEVEL_HEADER, "crunch!");
1011 /*********************************************************************
1013 * Function : client_from
1015 * Description : Handle the "from" config setting properly.
1016 * Called from `sed'.
1020 * 2 : s = header (from sed) to "crunch"
1021 * 3 : csp = Current client state (buffers, headers, etc...)
1023 * Returns : NULL if crunched, or a malloc'ed string to
1024 * modified/original header.
1026 *********************************************************************/
1027 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
1029 /* if not set, zap it */
1030 if (csp->config->from == NULL)
1032 log_error(LOG_LEVEL_HEADER, "crunch!");
1036 if (*csp->config->from == '.')
1041 log_error(LOG_LEVEL_HEADER, " modified");
1043 s = strsav( NULL, "From: " );
1044 s = strsav( s, csp->config->from );
1050 /*********************************************************************
1052 * Function : client_send_cookie
1054 * Description : Handle the "cookie" header properly. Called from `sed'.
1055 * If cookie is accepted, add it to the cookie_list,
1056 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1059 * 1 : v = pattern of cookie `sed' found matching
1060 * 2 : s = header (from sed) to "crunch"
1061 * 3 : csp = Current client state (buffers, headers, etc...)
1063 * Returns : Always NULL.
1065 *********************************************************************/
1066 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
1068 if (csp->permissions & PERMIT_COOKIE_READ)
1070 enlist(csp->cookie_list, s + v->len + 1);
1074 log_error(LOG_LEVEL_HEADER, " crunch!");
1078 * Always return NULL here. The cookie header
1079 * will be sent at the end of the header.
1086 /*********************************************************************
1088 * Function : client_x_forwarded
1090 * Description : Handle the "x-forwarded-for" config setting properly,
1091 * also used in the add_client_headers list. Called from `sed'.
1095 * 2 : s = header (from sed) to "crunch"
1096 * 3 : csp = Current client state (buffers, headers, etc...)
1098 * Returns : Always NULL.
1100 *********************************************************************/
1101 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1103 if (csp->config->add_forwarded)
1105 csp->x_forwarded = strdup(s);
1109 * Always return NULL, since this information
1110 * will be sent at the end of the header.
1117 #if defined(DETECT_MSIE_IMAGES)
1118 /*********************************************************************
1120 * Function : client_accept
1122 * Description : Detect whether the client wants HTML or an image.
1123 * Clients do not always make this information available
1124 * in a sane way. Always passes the header through
1125 * the proxy unchanged.
1129 * 2 : s = Header string. Null terminated.
1130 * 3 : csp = Current client state (buffers, headers, etc...)
1132 * Returns : Duplicate of argument s.
1134 *********************************************************************/
1135 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1137 #ifdef DETECT_MSIE_IMAGES
1138 if (strstr (s, "image/gif"))
1140 /* Client will accept HTML. If this seems counterintuitive,
1143 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1147 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1149 #endif /* def DETECT_MSIE_IMAGES */
1154 #endif /* defined(DETECT_MSIE_IMAGES) */
1158 /* the following functions add headers directly to the header list */
1161 /*********************************************************************
1163 * Function : client_cookie_adder
1165 * Description : Used in the add_client_headers list. Called from `sed'.
1168 * 1 : csp = Current client state (buffers, headers, etc...)
1172 *********************************************************************/
1173 void client_cookie_adder(struct client_state *csp)
1179 for (l = csp->cookie_list->next; l ; l = l->next)
1183 tmp = strsav(tmp, "; ");
1185 tmp = strsav(tmp, l->str);
1188 for (l = csp->config->wafer_list->next; l ; l = l->next)
1192 tmp = strsav(tmp, "; ");
1195 if ((e = cookie_encode(l->str)))
1197 tmp = strsav(tmp, e);
1206 ret = strdup("Cookie: ");
1207 ret = strsav(ret, tmp);
1208 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1209 enlist(csp->headers, ret);
1217 /*********************************************************************
1219 * Function : client_xtra_adder
1221 * Description : Used in the add_client_headers list. Called from `sed'.
1224 * 1 : csp = Current client state (buffers, headers, etc...)
1228 *********************************************************************/
1229 void client_xtra_adder(struct client_state *csp)
1233 for (l = csp->config->xtra_list->next; l ; l = l->next)
1235 log_error(LOG_LEVEL_HEADER, "addh: %s", l->str);
1236 enlist(csp->headers, l->str);
1242 /*********************************************************************
1244 * Function : client_x_forwarded_adder
1246 * Description : Used in the add_client_headers list. Called from `sed'.
1249 * 1 : csp = Current client state (buffers, headers, etc...)
1253 *********************************************************************/
1254 void client_x_forwarded_adder(struct client_state *csp)
1258 if (csp->config->add_forwarded == 0)
1263 if (csp->x_forwarded)
1265 p = strsav(p, csp->x_forwarded);
1266 p = strsav(p, ", ");
1267 p = strsav(p, csp->ip_addr_str);
1271 p = strsav(p, "X-Forwarded-For: ");
1272 p = strsav(p, csp->ip_addr_str);
1275 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1276 enlist(csp->headers, p);
1281 /*********************************************************************
1283 * Function : server_set_cookie
1285 * Description : Handle the server "cookie" header properly.
1286 * Log cookie to the jar file. Then "crunch" it,
1287 * or accept it. Called from `sed'.
1290 * 1 : v = parser pattern that matched this header
1291 * 2 : s = header that matched this pattern
1292 * 3 : csp = Current client state (buffers, headers, etc...)
1294 * Returns : `crumble' or a newly malloc'ed string.
1296 *********************************************************************/
1297 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1300 if (csp->config->jar)
1302 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1304 #endif /* def JAR_FILES */
1306 if (!(csp->permissions & PERMIT_COOKIE_SET))
1308 return(crumble(v, s, csp));
1317 /*********************************************************************
1319 * Function : client_host
1321 * Description : Clean the FORCE_PREFIX out of the 'host' http
1322 * header, if we use force
1326 * 2 : s = header (from sed) to clean
1327 * 3 : csp = Current client state (buffers, headers, etc...)
1329 * Returns : A malloc'ed pointer to the cleaned host header
1331 *********************************************************************/
1332 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1334 char *cleanhost = strdup(s);
1337 strclean(cleanhost, FORCE_PREFIX);
1341 #endif /* def FORCE_LOAD */
1345 /*********************************************************************
1347 * Function : strclean
1349 * Description : In-Situ-Eliminate all occurances of substring in
1353 * 1 : string = string to clean
1354 * 2 : substring = substring to eliminate
1356 * Returns : Number of eliminations
1358 *********************************************************************/
1359 int strclean(const char *string, const char *substring)
1361 int hits = 0, len = strlen(substring);
1364 while((pos = strstr(string, substring)))
1371 while (*p++ != '\0');
1378 #endif /* def FORCE_LOAD */