1 const char parsers_rcs[] = "$Id: parsers.c,v 1.10 2001/05/29 09:50:24 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.10 2001/05/29 09:50:24 jongfoster
45 * Unified blocklist/imagelist/permissionslist.
46 * File format is still under discussion, but the internal changes
49 * Also modified interceptor behaviour:
50 * - We now intercept all URLs beginning with one of the following
51 * prefixes (and *only* these prefixes):
53 * * http://ijbswa.sf.net/config/
54 * * http://ijbswa.sourceforge.net/config/
55 * - New interceptors "home page" - go to http://i.j.b/ to see it.
56 * - Internal changes so that intercepted and fast redirect pages
57 * are not replaced with an image.
58 * - Interceptors now have the option to send a binary page direct
59 * to the client. (i.e. ijb-send-banner uses this)
60 * - Implemented show-url-info interceptor. (Which is why I needed
61 * the above interceptors changes - a typical URL is
62 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
63 * The previous mechanism would not have intercepted that, and
64 * if it had been intercepted then it then it would have replaced
67 * Revision 1.9 2001/05/28 17:26:33 jongfoster
68 * Fixing segfault if last header was crunched.
69 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
70 * can use the cross-platform sprintf() instead.)
72 * Revision 1.8 2001/05/27 22:17:04 oes
74 * - re_process_buffer no longer writes the modified buffer
75 * to the client, which was very ugly. It now returns the
76 * buffer, which it is then written by chat.
78 * - content_length now adjusts the Content-Length: header
79 * for modified documents rather than crunch()ing it.
80 * (Length info in csp->content_length, which is 0 for
81 * unmodified documents)
83 * - For this to work, sed() is called twice when filtering.
85 * Revision 1.7 2001/05/27 13:19:06 oes
86 * Patched Joergs solution for the content-length in.
88 * Revision 1.6 2001/05/26 13:39:32 jongfoster
89 * Only crunches Content-Length header if applying RE filtering.
90 * Without this fix, Microsoft Windows Update wouldn't work.
92 * Revision 1.5 2001/05/26 00:28:36 jongfoster
93 * Automatic reloading of config file.
94 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
95 * Most of the global variables have been moved to a new
96 * struct configuration_spec, accessed through csp->config->globalname
97 * Most of the globals remaining are used by the Win32 GUI.
99 * Revision 1.4 2001/05/22 18:46:04 oes
101 * - Enabled filtering banners by size rather than URL
102 * by adding patterns that replace all standard banner
103 * sizes with the "Junkbuster" gif to the re_filterfile
105 * - Enabled filtering WebBugs by providing a pattern
106 * which kills all 1x1 images
108 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
109 * which is selected by the (nonstandard and therefore
110 * capital) letter 'U' in the option string.
111 * It causes the quantifiers to be ungreedy by default.
112 * Appending a ? turns back to greedy (!).
114 * - Added a new interceptor ijb-send-banner, which
115 * sends back the "Junkbuster" gif. Without imagelist or
116 * MSIE detection support, or if tinygif = 1, or the
117 * URL isn't recognized as an imageurl, a lame HTML
118 * explanation is sent instead.
120 * - Added new feature, which permits blocking remote
121 * script redirects and firing back a local redirect
123 * The feature is conditionally compiled, i.e. it
124 * can be disabled with --disable-fast-redirects,
125 * plus it must be activated by a "fast-redirects"
126 * line in the config file, has its own log level
127 * and of course wants to be displayed by show-proxy-args
128 * Note: Boy, all the #ifdefs in 1001 locations and
129 * all the fumbling with configure.in and acconfig.h
130 * were *way* more work than the feature itself :-(
132 * - Because a generic redirect template was needed for
133 * this, tinygif = 3 now uses the same.
135 * - Moved GIFs, and other static HTTP response templates
140 * - Removed some >400 CRs again (Jon, you really worked
143 * Revision 1.3 2001/05/20 01:21:20 jongfoster
144 * Version 2.9.4 checkin.
145 * - Merged popupfile and cookiefile, and added control over PCRS
146 * filtering, in new "permissionsfile".
147 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
148 * file error you now get a message box (in the Win32 GUI) rather
149 * than the program exiting with no explanation.
150 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
152 * - Removed tabs from "config"
153 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
154 * - Bumped up version number.
156 * Revision 1.2 2001/05/17 23:02:36 oes
157 * - Made referrer option accept 'L' as a substitute for 'ยง'
159 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
160 * Initial import of version 2.9.3 source tree
163 *********************************************************************/
169 #include <sys/types.h>
183 #include "showargs.h"
187 #include "jbsockets.h"
188 #include "miscutil.h"
190 const char parsers_h_rcs[] = PARSERS_H_VERSION;
192 /* Fix a problem with Solaris. There should be no effect on other
194 * Solaris's isspace() is a macro which uses it's argument directly
195 * as an array index. Therefore we need to make sure that high-bit
196 * characters generate +ve values, and ideally we also want to make
197 * the argument match the declared parameter type of "int".
199 * Why did they write a character function that can't take a simple
200 * "char" argument? Doh!
202 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
203 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
206 const struct parsers client_patterns[] = {
207 { "referer:", 8, client_referrer },
208 { "user-agent:", 11, client_uagent },
209 { "ua-", 3, client_ua },
210 { "from:", 5, client_from },
211 { "cookie:", 7, client_send_cookie },
212 { "x-forwarded-for:", 16, client_x_forwarded },
213 { "proxy-connection:", 17, crumble },
215 { "Accept-Encoding: gzip", 21, crumble },
216 #endif /* def DENY_GZIP */
217 #if defined(DETECT_MSIE_IMAGES)
218 { "Accept:", 7, client_accept },
219 #endif /* defined(DETECT_MSIE_IMAGES) */
221 { "Host:", 5, client_host },
222 #endif /* def FORCE_LOAD */
223 /* { "if-modified-since:", 18, crumble }, */
227 const struct interceptors intercept_patterns[] = {
228 { "show-proxy-args", 14, show_proxy_args },
229 { "ijb-send-banner", 14, ijb_send_banner },
231 { "ij-untrusted-url", 15, ij_untrusted_url },
232 #endif /* def TRUST_FILES */
233 { "show-url-info", 13, ijb_show_url_info },
237 const struct parsers server_patterns[] = {
238 { "set-cookie:", 11, server_set_cookie },
239 { "connection:", 11, crumble },
240 #if defined(PCRS) || defined(KILLPOPUPS)
241 { "Content-Type:", 13, content_type },
242 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
244 { "Content-Length:", 15, content_length },
245 #endif /* def PCRS */
250 void (* const add_client_headers[])(struct client_state *) = {
252 client_x_forwarded_adder,
258 void (* const add_server_headers[])(struct client_state *) = {
263 /*********************************************************************
265 * Function : flush_socket
267 * Description : Write any pending "buffered" content.
270 * 1 : fd = file descriptor of the socket to read
271 * 2 : csp = Current client state (buffers, headers, etc...)
273 * Returns : On success, the number of bytes written are returned (zero
274 * indicates nothing was written). On error, -1 is returned,
275 * and errno is set appropriately. If count is zero and the
276 * file descriptor refers to a regular file, 0 will be
277 * returned without causing any other effect. For a special
278 * file, the results are not portable.
280 *********************************************************************/
281 int flush_socket(int fd, struct client_state *csp)
283 struct iob *iob = csp->iob;
284 int n = iob->eod - iob->cur;
291 n = write_socket(fd, iob->cur, n);
292 iob->eod = iob->cur = iob->buf;
298 /*********************************************************************
300 * Function : add_to_iob
302 * Description : Add content to the buffered page.
305 * 1 : csp = Current client state (buffers, headers, etc...)
306 * 2 : buf = holds the content to be added to the page
307 * 3 : n = number of bytes to be added
309 * Returns : Number of bytes in the content buffer.
311 *********************************************************************/
312 int add_to_iob(struct client_state *csp, char *buf, int n)
314 struct iob *iob = csp->iob;
318 have = iob->eod - iob->cur;
327 if ((p = (char *)malloc(need + 1)) == NULL)
329 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
335 /* there is something in the buffer - save it */
336 memcpy(p, iob->cur, have);
338 /* replace the buffer with the new space */
342 /* point to the end of the data */
347 /* the buffer is empty, free it and reinitialize */
352 /* copy the new data into the iob buffer */
355 /* point to the end of the data */
358 /* null terminate == cheap insurance */
361 /* set the pointers to the new values */
370 /*********************************************************************
372 * Function : get_header
374 * Description : This (odd) routine will parse the csp->iob
377 * 1 : csp = Current client state (buffers, headers, etc...)
379 * Returns : Any one of the following:
381 * 1) a pointer to a dynamically allocated string that contains a header line
382 * 2) NULL indicating that the end of the header was reached
383 * 3) "" indicating that the end of the iob was reached before finding
384 * a complete header line.
386 *********************************************************************/
387 char *get_header(struct client_state *csp)
393 if ((iob->cur == NULL)
394 || ((p = strchr(iob->cur, '\n')) == NULL))
396 return(""); /* couldn't find a complete header */
401 ret = strdup(iob->cur);
405 if ((q = strchr(ret, '\r'))) *q = '\0';
407 /* is this a blank linke (i.e. the end of the header) ? */
419 /*********************************************************************
423 * Description : Append a string into a specified string list.
426 * 1 : h = pointer to list 'dummy' header
427 * 2 : s = string to add to the list
431 *********************************************************************/
432 void enlist(struct list *h, const char *s)
434 struct list *n = (struct list *)malloc(sizeof(*n));
457 /*********************************************************************
459 * Function : destroy_list
461 * Description : Destroy a string list (opposite of enlist)
464 * 1 : h = pointer to list 'dummy' header
468 *********************************************************************/
469 void destroy_list(struct list *h)
473 for (p = h->next; p ; p = n)
480 memset(h, '\0', sizeof(*h));
485 /*********************************************************************
487 * Function : list_to_text
489 * Description : "Flaten" a string list into 1 long \r\n delimited string.
492 * 1 : h = pointer to list 'dummy' header
494 * Returns : NULL on malloc error, else new long string.
496 *********************************************************************/
497 static char *list_to_text(struct list *h)
506 for (p = h->next; p ; p = p->next)
510 size += strlen(p->str) + 2;
514 if ((ret = (char *)malloc(size + 1)) == NULL)
523 for (p = h->next; p ; p = p->next)
529 *s++ = '\r'; *s++ = '\n';
538 /*********************************************************************
542 * Description : add, delete or modify lines in the HTTP header streams.
543 * On entry, it receives a linked list of headers space
544 * that was allocated dynamically (both the list nodes
545 * and the header contents).
547 * As a side effect it frees the space used by the original
551 * 1 : pats = list of patterns to match against headers
552 * 2 : more_headers = list of functions to add more
553 * headers (client or server)
554 * 3 : csp = Current client state (buffers, headers, etc...)
556 * Returns : Single pointer to a fully formed header.
558 *********************************************************************/
559 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
562 const struct parsers *v;
566 for (v = pats; v->str ; v++)
568 for (p = csp->headers->next; p ; p = p->next)
570 /* Header crunch()ed in previous run? -> ignore */
571 if (p->str == NULL) continue;
573 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
575 if (strncmpic(p->str, v->str, v->len) == 0)
577 hdr = v->parser(v, p->str, csp);
584 /* place any additional headers on the csp->headers list */
585 for (f = more_headers; *f ; f++)
590 /* add the blank line at the end of the header, if necessary */
591 if ( (csp->headers->last == NULL)
592 || (csp->headers->last->str == NULL)
593 || (*csp->headers->last->str != '\0') )
595 enlist(csp->headers, "");
598 hdr = list_to_text(csp->headers);
605 /*********************************************************************
607 * Function : free_http_request
609 * Description : Freez a http_request structure
612 * 1 : http = points to a http_request structure to free
616 *********************************************************************/
617 void free_http_request(struct http_request *http)
622 freez(http->hostport);
629 /*********************************************************************
631 * Function : parse_http_request
633 * Description : Parse out the host and port from the URL. Find the
634 * hostname & path, port (if ':'), and/or password (if '@')
637 * 1 : req = URL (or is it URI?) to break down
638 * 2 : http = pointer to the http structure to hold elements
639 * 3 : csp = Current client state (buffers, headers, etc...)
643 *********************************************************************/
644 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
646 char *buf, *v[10], *url, *p;
649 memset(http, '\0', sizeof(*http));
651 http->cmd = strdup(req);
655 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
659 /* this could be a CONNECT request */
660 if (strcmpic(v[0], "connect") == 0)
663 http->gpc = strdup(v[0]);
664 http->hostport = strdup(v[1]);
665 http->ver = strdup(v[2]);
670 /* This next line is a little ugly, but it simplifies the if statement below. */
671 /* Basically if using webDAV, we want the OR condition to use these too. */
675 * These are the headers as defined in RFC2518 to add webDAV support
678 #define OR_WEBDAV || \
679 (0 == strcmpic(v[0], "propfind")) || \
680 (0 == strcmpic(v[0], "proppatch")) || \
681 (0 == strcmpic(v[0], "move")) || \
682 (0 == strcmpic(v[0], "copy")) || \
683 (0 == strcmpic(v[0], "mkcol")) || \
684 (0 == strcmpic(v[0], "lock")) || \
685 (0 == strcmpic(v[0], "unlock"))
687 #else /* No webDAV support is enabled. Provide an empty OR_WEBDAV macro. */
693 /* or it could be a GET or a POST (possibly webDAV too) */
694 if ((strcmpic(v[0], "get") == 0) ||
695 (strcmpic(v[0], "head") == 0) OR_WEBDAV ||
696 (strcmpic(v[0], "post") == 0))
699 http->gpc = strdup(v[0]);
701 http->ver = strdup(v[2]);
703 if (strncmpic(url, "http://", 7) == 0)
707 else if (strncmpic(url, "https://", 8) == 0)
716 if (url && (p = strchr(url, '/')))
718 http->path = strdup(p);
720 http->hostport = strdup(url);
728 if (http->hostport == NULL)
730 free_http_request(http);
734 buf = strdup(http->hostport);
737 /* check if url contains password */
738 n = ssplit(buf, "@", v, SZ(v), 1, 1);
741 char * newbuf = NULL;
742 newbuf = strdup(v[1]);
747 n = ssplit(buf, ":", v, SZ(v), 1, 1);
751 http->host = strdup(v[0]);
757 http->host = strdup(v[0]);
758 http->port = atoi(v[1]);
763 if (http->host == NULL)
765 free_http_request(http);
768 if (http->path == NULL)
770 http->path = strdup("");
776 /* here begins the family of parser functions that reformat header lines */
779 /*********************************************************************
783 * Description : This is called if a header matches a pattern to "crunch"
786 * 1 : v = Pointer to parsers structure, which basically holds
787 * headers (client or server) that we want to "crunch"
788 * 2 : s = header (from sed) to "crunch"
789 * 3 : csp = Current client state (buffers, headers, etc...)
791 * Returns : Always NULL.
793 *********************************************************************/
794 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
796 log_error(LOG_LEVEL_HEADER, "crunch!");
802 #if defined(PCRS) || defined(KILLPOPUPS)
804 /*********************************************************************
806 * Function : content_type
808 * Description : Is this a text/.* or javascript MIME Type?
812 * 2 : s = header string we are "considering"
813 * 3 : csp = Current client state (buffers, headers, etc...)
815 * Returns : A duplicate string pointer to this header (ie. pass thru)
817 *********************************************************************/
818 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
820 if (strstr (s, " text/") || strstr (s, "application/x-javascript"))
828 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
832 /*********************************************************************
834 * Function : content_length
836 * Description : Crunch Content-Length header if & only if we are
837 * filtering this page through PCRS.
841 * 2 : s = header string we are "considering"
842 * 3 : csp = Current client state (buffers, headers, etc...)
844 * Returns : A duplicate string pointer to this header (ie. pass thru)
846 *********************************************************************/
847 char *content_length(const struct parsers *v, char *s, struct client_state *csp)
849 if (csp->content_length != 0) /* Content has been modified */
851 s = (char *) zalloc(100);
852 sprintf(s, "Content-Length: %d", csp->content_length);
853 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
862 #endif /* def PCRS */
865 /*********************************************************************
867 * Function : client_referrer
869 * Description : Handle the "referer" config setting properly.
874 * 2 : s = header (from sed) to "crunch"
875 * 3 : csp = Current client state (buffers, headers, etc...)
877 * Returns : NULL if crunched, or a malloc'ed string with the original
880 *********************************************************************/
881 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
884 /* Since the referrer can include the prefix even
885 * even if the request itself is non-forced, we must
886 * clean it unconditionally
888 strclean(s, FORCE_PREFIX);
889 #endif /* def FORCE_LOAD */
892 csp->referrer = strdup(s);
893 #endif /* def TRUST_FILES */
\r
896 * Check permissionsfile. If we have allowed this site to get the
897 * referer, then send it and we're done.
899 if (csp->permissions & PERMIT_REFERER)
905 * Check configfile. Are we blocking referer?
907 if ( (csp->config->referrer == NULL)
908 || (*csp->config->referrer == '@') )
910 log_error(LOG_LEVEL_HEADER, "crunch!");
915 * Check configfile. Are we always sending referer?
917 if (*csp->config->referrer == '.')
923 * New option ยง or L: Forge a referer as http://[hostname:port of REQUEST]/
924 * to fool stupid checks for in-site links
926 if (*csp->config->referrer == 'ยง' || *csp->config->referrer == 'L')
928 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
929 s = strsav(NULL, "Referer: ");
930 s = strsav(s, "http://");
931 s = strsav(s, csp->http->hostport);
937 * We have a specific (fixed) referer we want to send.
940 log_error(LOG_LEVEL_HEADER, "modified");
942 s = strsav( NULL, "Referer: " );
943 s = strsav( s, csp->config->referrer );
949 /*********************************************************************
951 * Function : client_uagent
953 * Description : Handle the "user-agent" config setting properly.
958 * 2 : s = header (from sed) to "crunch"
959 * 3 : csp = Current client state (buffers, headers, etc...)
961 * Returns : A malloc'ed pointer to the default agent, or
962 * a malloc'ed string pointer to this header (ie. pass thru).
964 *********************************************************************/
965 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
967 #ifdef DETECT_MSIE_IMAGES
968 if (strstr (s, "MSIE "))
970 /* This is Microsoft Internet Explorer.
971 * Enable auto-detect.
973 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
975 #endif /* def DETECT_MSIE_IMAGES */
977 if (csp->config->uagent == NULL)
979 log_error(LOG_LEVEL_HEADER, "default");
980 return(strdup(DEFAULT_USER_AGENT));
983 if (*csp->config->uagent == '.')
988 if (*csp->config->uagent == '@')
990 if (csp->permissions & PERMIT_USER_AGENT)
996 log_error(LOG_LEVEL_HEADER, "default");
997 return(strdup(DEFAULT_USER_AGENT));
1001 log_error(LOG_LEVEL_HEADER, "modified");
1003 s = strsav( NULL, "User-Agent: " );
1004 s = strsav( s, csp->config->uagent );
1010 /*********************************************************************
1012 * Function : client_ua
1014 * Description : Handle "ua-" headers properly. Called from `sed'.
1018 * 2 : s = header (from sed) to "crunch"
1019 * 3 : csp = Current client state (buffers, headers, etc...)
1021 * Returns : NULL if crunched, or a malloc'ed string to original header
1023 *********************************************************************/
1024 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
1026 if (csp->config->uagent == NULL)
1028 log_error(LOG_LEVEL_HEADER, "crunch!");
1032 if (*csp->config->uagent == '.')
1037 if (*csp->config->uagent == '@')
1039 if (csp->permissions & PERMIT_USER_AGENT)
1045 log_error(LOG_LEVEL_HEADER, "crunch!");
1050 log_error(LOG_LEVEL_HEADER, "crunch!");
1056 /*********************************************************************
1058 * Function : client_from
1060 * Description : Handle the "from" config setting properly.
1061 * Called from `sed'.
1065 * 2 : s = header (from sed) to "crunch"
1066 * 3 : csp = Current client state (buffers, headers, etc...)
1068 * Returns : NULL if crunched, or a malloc'ed string to
1069 * modified/original header.
1071 *********************************************************************/
1072 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
1074 /* if not set, zap it */
1075 if (csp->config->from == NULL)
1077 log_error(LOG_LEVEL_HEADER, "crunch!");
1081 if (*csp->config->from == '.')
1086 log_error(LOG_LEVEL_HEADER, " modified");
1088 s = strsav( NULL, "From: " );
1089 s = strsav( s, csp->config->from );
1095 /*********************************************************************
1097 * Function : client_send_cookie
1099 * Description : Handle the "cookie" header properly. Called from `sed'.
1100 * If cookie is accepted, add it to the cookie_list,
1101 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1104 * 1 : v = pattern of cookie `sed' found matching
1105 * 2 : s = header (from sed) to "crunch"
1106 * 3 : csp = Current client state (buffers, headers, etc...)
1108 * Returns : Always NULL.
1110 *********************************************************************/
1111 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
1113 if (csp->permissions & PERMIT_COOKIE_READ)
1115 enlist(csp->cookie_list, s + v->len + 1);
1119 log_error(LOG_LEVEL_HEADER, " crunch!");
1123 * Always return NULL here. The cookie header
1124 * will be sent at the end of the header.
1131 /*********************************************************************
1133 * Function : client_x_forwarded
1135 * Description : Handle the "x-forwarded-for" config setting properly,
1136 * also used in the add_client_headers list. Called from `sed'.
1140 * 2 : s = header (from sed) to "crunch"
1141 * 3 : csp = Current client state (buffers, headers, etc...)
1143 * Returns : Always NULL.
1145 *********************************************************************/
1146 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1148 if (csp->config->add_forwarded)
1150 csp->x_forwarded = strdup(s);
1154 * Always return NULL, since this information
1155 * will be sent at the end of the header.
1162 #if defined(DETECT_MSIE_IMAGES)
1163 /*********************************************************************
1165 * Function : client_accept
1167 * Description : Detect whether the client wants HTML or an image.
1168 * Clients do not always make this information available
1169 * in a sane way. Always passes the header through
1170 * the proxy unchanged.
1174 * 2 : s = Header string. Null terminated.
1175 * 3 : csp = Current client state (buffers, headers, etc...)
1177 * Returns : Duplicate of argument s.
1179 *********************************************************************/
1180 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1182 #ifdef DETECT_MSIE_IMAGES
1183 if (strstr (s, "image/gif"))
1185 /* Client will accept HTML. If this seems counterintuitive,
1188 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1192 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1194 #endif /* def DETECT_MSIE_IMAGES */
1199 #endif /* defined(DETECT_MSIE_IMAGES) */
1203 /* the following functions add headers directly to the header list */
1206 /*********************************************************************
1208 * Function : client_cookie_adder
1210 * Description : Used in the add_client_headers list. Called from `sed'.
1213 * 1 : csp = Current client state (buffers, headers, etc...)
1217 *********************************************************************/
1218 void client_cookie_adder(struct client_state *csp)
1224 for (l = csp->cookie_list->next; l ; l = l->next)
1228 tmp = strsav(tmp, "; ");
1230 tmp = strsav(tmp, l->str);
1233 for (l = csp->config->wafer_list->next; l ; l = l->next)
1237 tmp = strsav(tmp, "; ");
1240 if ((e = cookie_encode(l->str)))
1242 tmp = strsav(tmp, e);
1251 ret = strdup("Cookie: ");
1252 ret = strsav(ret, tmp);
1253 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1254 enlist(csp->headers, ret);
1262 /*********************************************************************
1264 * Function : client_xtra_adder
1266 * Description : Used in the add_client_headers list. Called from `sed'.
1269 * 1 : csp = Current client state (buffers, headers, etc...)
1273 *********************************************************************/
1274 void client_xtra_adder(struct client_state *csp)
1278 for (l = csp->config->xtra_list->next; l ; l = l->next)
1280 log_error(LOG_LEVEL_HEADER, "addh: %s", l->str);
1281 enlist(csp->headers, l->str);
1287 /*********************************************************************
1289 * Function : client_x_forwarded_adder
1291 * Description : Used in the add_client_headers list. Called from `sed'.
1294 * 1 : csp = Current client state (buffers, headers, etc...)
1298 *********************************************************************/
1299 void client_x_forwarded_adder(struct client_state *csp)
1303 if (csp->config->add_forwarded == 0)
1308 if (csp->x_forwarded)
1310 p = strsav(p, csp->x_forwarded);
1311 p = strsav(p, ", ");
1312 p = strsav(p, csp->ip_addr_str);
1316 p = strsav(p, "X-Forwarded-For: ");
1317 p = strsav(p, csp->ip_addr_str);
1320 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1321 enlist(csp->headers, p);
1326 /*********************************************************************
1328 * Function : server_set_cookie
1330 * Description : Handle the server "cookie" header properly.
1331 * Log cookie to the jar file. Then "crunch" it,
1332 * or accept it. Called from `sed'.
1335 * 1 : v = parser pattern that matched this header
1336 * 2 : s = header that matched this pattern
1337 * 3 : csp = Current client state (buffers, headers, etc...)
1339 * Returns : `crumble' or a newly malloc'ed string.
1341 *********************************************************************/
1342 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1345 if (csp->config->jar)
1347 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1349 #endif /* def JAR_FILES */
1351 if (!(csp->permissions & PERMIT_COOKIE_SET))
1353 return(crumble(v, s, csp));
1362 /*********************************************************************
1364 * Function : client_host
1366 * Description : Clean the FORCE_PREFIX out of the 'host' http
1367 * header, if we use force
1371 * 2 : s = header (from sed) to clean
1372 * 3 : csp = Current client state (buffers, headers, etc...)
1374 * Returns : A malloc'ed pointer to the cleaned host header
1376 *********************************************************************/
1377 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1379 char *cleanhost = strdup(s);
1382 strclean(cleanhost, FORCE_PREFIX);
1386 #endif /* def FORCE_LOAD */
1390 /*********************************************************************
1392 * Function : strclean
1394 * Description : In-Situ-Eliminate all occurances of substring in
1398 * 1 : string = string to clean
1399 * 2 : substring = substring to eliminate
1401 * Returns : Number of eliminations
1403 *********************************************************************/
1404 int strclean(const char *string, const char *substring)
1406 int hits = 0, len = strlen(substring);
1409 while((pos = strstr(string, substring)))
1416 while (*p++ != '\0');
1423 #endif /* def FORCE_LOAD */