1 const char parsers_rcs[] = "$Id: parsers.c,v 1.12 2001/05/31 17:33:13 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.12 2001/05/31 17:33:13 oes
48 * Revision 1.11 2001/05/29 20:11:19 joergs
49 * '/* inside comment' warning removed.
51 * Revision 1.10 2001/05/29 09:50:24 jongfoster
52 * Unified blocklist/imagelist/permissionslist.
53 * File format is still under discussion, but the internal changes
56 * Also modified interceptor behaviour:
57 * - We now intercept all URLs beginning with one of the following
58 * prefixes (and *only* these prefixes):
60 * * http://ijbswa.sf.net/config/
61 * * http://ijbswa.sourceforge.net/config/
62 * - New interceptors "home page" - go to http://i.j.b/ to see it.
63 * - Internal changes so that intercepted and fast redirect pages
64 * are not replaced with an image.
65 * - Interceptors now have the option to send a binary page direct
66 * to the client. (i.e. ijb-send-banner uses this)
67 * - Implemented show-url-info interceptor. (Which is why I needed
68 * the above interceptors changes - a typical URL is
69 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
70 * The previous mechanism would not have intercepted that, and
71 * if it had been intercepted then it then it would have replaced
74 * Revision 1.9 2001/05/28 17:26:33 jongfoster
75 * Fixing segfault if last header was crunched.
76 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
77 * can use the cross-platform sprintf() instead.)
79 * Revision 1.8 2001/05/27 22:17:04 oes
81 * - re_process_buffer no longer writes the modified buffer
82 * to the client, which was very ugly. It now returns the
83 * buffer, which it is then written by chat.
85 * - content_length now adjusts the Content-Length: header
86 * for modified documents rather than crunch()ing it.
87 * (Length info in csp->content_length, which is 0 for
88 * unmodified documents)
90 * - For this to work, sed() is called twice when filtering.
92 * Revision 1.7 2001/05/27 13:19:06 oes
93 * Patched Joergs solution for the content-length in.
95 * Revision 1.6 2001/05/26 13:39:32 jongfoster
96 * Only crunches Content-Length header if applying RE filtering.
97 * Without this fix, Microsoft Windows Update wouldn't work.
99 * Revision 1.5 2001/05/26 00:28:36 jongfoster
100 * Automatic reloading of config file.
101 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
102 * Most of the global variables have been moved to a new
103 * struct configuration_spec, accessed through csp->config->globalname
104 * Most of the globals remaining are used by the Win32 GUI.
106 * Revision 1.4 2001/05/22 18:46:04 oes
108 * - Enabled filtering banners by size rather than URL
109 * by adding patterns that replace all standard banner
110 * sizes with the "Junkbuster" gif to the re_filterfile
112 * - Enabled filtering WebBugs by providing a pattern
113 * which kills all 1x1 images
115 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
116 * which is selected by the (nonstandard and therefore
117 * capital) letter 'U' in the option string.
118 * It causes the quantifiers to be ungreedy by default.
119 * Appending a ? turns back to greedy (!).
121 * - Added a new interceptor ijb-send-banner, which
122 * sends back the "Junkbuster" gif. Without imagelist or
123 * MSIE detection support, or if tinygif = 1, or the
124 * URL isn't recognized as an imageurl, a lame HTML
125 * explanation is sent instead.
127 * - Added new feature, which permits blocking remote
128 * script redirects and firing back a local redirect
130 * The feature is conditionally compiled, i.e. it
131 * can be disabled with --disable-fast-redirects,
132 * plus it must be activated by a "fast-redirects"
133 * line in the config file, has its own log level
134 * and of course wants to be displayed by show-proxy-args
135 * Note: Boy, all the #ifdefs in 1001 locations and
136 * all the fumbling with configure.in and acconfig.h
137 * were *way* more work than the feature itself :-(
139 * - Because a generic redirect template was needed for
140 * this, tinygif = 3 now uses the same.
142 * - Moved GIFs, and other static HTTP response templates
147 * - Removed some >400 CRs again (Jon, you really worked
150 * Revision 1.3 2001/05/20 01:21:20 jongfoster
151 * Version 2.9.4 checkin.
152 * - Merged popupfile and cookiefile, and added control over PCRS
153 * filtering, in new "permissionsfile".
154 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
155 * file error you now get a message box (in the Win32 GUI) rather
156 * than the program exiting with no explanation.
157 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
159 * - Removed tabs from "config"
160 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
161 * - Bumped up version number.
163 * Revision 1.2 2001/05/17 23:02:36 oes
164 * - Made referrer option accept 'L' as a substitute for 'ยง'
166 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
167 * Initial import of version 2.9.3 source tree
170 *********************************************************************/
176 #include <sys/types.h>
191 #include "showargs.h"
195 #include "jbsockets.h"
196 #include "miscutil.h"
198 const char parsers_h_rcs[] = PARSERS_H_VERSION;
200 /* Fix a problem with Solaris. There should be no effect on other
202 * Solaris's isspace() is a macro which uses it's argument directly
203 * as an array index. Therefore we need to make sure that high-bit
204 * characters generate +ve values, and ideally we also want to make
205 * the argument match the declared parameter type of "int".
207 * Why did they write a character function that can't take a simple
208 * "char" argument? Doh!
210 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
211 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
214 const struct parsers client_patterns[] = {
215 { "referer:", 8, client_referrer },
216 { "user-agent:", 11, client_uagent },
217 { "ua-", 3, client_ua },
218 { "from:", 5, client_from },
219 { "cookie:", 7, client_send_cookie },
220 { "x-forwarded-for:", 16, client_x_forwarded },
221 { "proxy-connection:", 17, crumble },
223 { "Accept-Encoding: gzip", 21, crumble },
224 #endif /* def DENY_GZIP */
225 #if defined(DETECT_MSIE_IMAGES)
226 { "Accept:", 7, client_accept },
227 #endif /* defined(DETECT_MSIE_IMAGES) */
229 { "Host:", 5, client_host },
230 #endif /* def FORCE_LOAD */
231 /* { "if-modified-since:", 18, crumble }, */
235 const struct interceptors intercept_patterns[] = {
236 { "show-proxy-args", 14, show_proxy_args },
237 { "ijb-send-banner", 14, ijb_send_banner },
239 { "ij-untrusted-url", 15, ij_untrusted_url },
240 #endif /* def TRUST_FILES */
241 { "show-url-info", 13, ijb_show_url_info },
245 const struct parsers server_patterns[] = {
246 { "set-cookie:", 11, server_set_cookie },
247 { "connection:", 11, crumble },
248 #if defined(PCRS) || defined(KILLPOPUPS)
249 { "Content-Type:", 13, content_type },
250 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
252 { "Content-Length:", 15, content_length },
253 #endif /* def PCRS */
258 void (* const add_client_headers[])(struct client_state *) = {
260 client_x_forwarded_adder,
266 void (* const add_server_headers[])(struct client_state *) = {
271 /*********************************************************************
273 * Function : flush_socket
275 * Description : Write any pending "buffered" content.
278 * 1 : fd = file descriptor of the socket to read
279 * 2 : csp = Current client state (buffers, headers, etc...)
281 * Returns : On success, the number of bytes written are returned (zero
282 * indicates nothing was written). On error, -1 is returned,
283 * and errno is set appropriately. If count is zero and the
284 * file descriptor refers to a regular file, 0 will be
285 * returned without causing any other effect. For a special
286 * file, the results are not portable.
288 *********************************************************************/
289 int flush_socket(int fd, struct client_state *csp)
291 struct iob *iob = csp->iob;
292 int n = iob->eod - iob->cur;
299 n = write_socket(fd, iob->cur, n);
300 iob->eod = iob->cur = iob->buf;
306 /*********************************************************************
308 * Function : add_to_iob
310 * Description : Add content to the buffered page.
313 * 1 : csp = Current client state (buffers, headers, etc...)
314 * 2 : buf = holds the content to be added to the page
315 * 3 : n = number of bytes to be added
317 * Returns : Number of bytes in the content buffer.
319 *********************************************************************/
320 int add_to_iob(struct client_state *csp, char *buf, int n)
322 struct iob *iob = csp->iob;
326 have = iob->eod - iob->cur;
335 if ((p = (char *)malloc(need + 1)) == NULL)
337 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
343 /* there is something in the buffer - save it */
344 memcpy(p, iob->cur, have);
346 /* replace the buffer with the new space */
350 /* point to the end of the data */
355 /* the buffer is empty, free it and reinitialize */
360 /* copy the new data into the iob buffer */
363 /* point to the end of the data */
366 /* null terminate == cheap insurance */
369 /* set the pointers to the new values */
378 /*********************************************************************
380 * Function : get_header
382 * Description : This (odd) routine will parse the csp->iob
385 * 1 : csp = Current client state (buffers, headers, etc...)
387 * Returns : Any one of the following:
389 * 1) a pointer to a dynamically allocated string that contains a header line
390 * 2) NULL indicating that the end of the header was reached
391 * 3) "" indicating that the end of the iob was reached before finding
392 * a complete header line.
394 *********************************************************************/
395 char *get_header(struct client_state *csp)
401 if ((iob->cur == NULL)
402 || ((p = strchr(iob->cur, '\n')) == NULL))
404 return(""); /* couldn't find a complete header */
409 ret = strdup(iob->cur);
413 if ((q = strchr(ret, '\r'))) *q = '\0';
415 /* is this a blank linke (i.e. the end of the header) ? */
427 /*********************************************************************
431 * Description : add, delete or modify lines in the HTTP header streams.
432 * On entry, it receives a linked list of headers space
433 * that was allocated dynamically (both the list nodes
434 * and the header contents).
436 * As a side effect it frees the space used by the original
440 * 1 : pats = list of patterns to match against headers
441 * 2 : more_headers = list of functions to add more
442 * headers (client or server)
443 * 3 : csp = Current client state (buffers, headers, etc...)
445 * Returns : Single pointer to a fully formed header.
447 *********************************************************************/
448 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
451 const struct parsers *v;
455 for (v = pats; v->str ; v++)
457 for (p = csp->headers->next; p ; p = p->next)
459 /* Header crunch()ed in previous run? -> ignore */
460 if (p->str == NULL) continue;
462 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
464 if (strncmpic(p->str, v->str, v->len) == 0)
466 hdr = v->parser(v, p->str, csp);
473 /* place any additional headers on the csp->headers list */
474 for (f = more_headers; *f ; f++)
479 /* add the blank line at the end of the header, if necessary */
480 if ( (csp->headers->last == NULL)
481 || (csp->headers->last->str == NULL)
482 || (*csp->headers->last->str != '\0') )
484 enlist(csp->headers, "");
487 hdr = list_to_text(csp->headers);
494 /*********************************************************************
496 * Function : free_http_request
498 * Description : Freez a http_request structure
501 * 1 : http = points to a http_request structure to free
505 *********************************************************************/
506 void free_http_request(struct http_request *http)
511 freez(http->hostport);
518 /*********************************************************************
520 * Function : parse_http_request
522 * Description : Parse out the host and port from the URL. Find the
523 * hostname & path, port (if ':'), and/or password (if '@')
526 * 1 : req = URL (or is it URI?) to break down
527 * 2 : http = pointer to the http structure to hold elements
528 * 3 : csp = Current client state (buffers, headers, etc...)
532 *********************************************************************/
533 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
535 char *buf, *v[10], *url, *p;
538 memset(http, '\0', sizeof(*http));
540 http->cmd = strdup(req);
544 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
548 /* this could be a CONNECT request */
549 if (strcmpic(v[0], "connect") == 0)
552 http->gpc = strdup(v[0]);
553 http->hostport = strdup(v[1]);
554 http->ver = strdup(v[2]);
559 /* This next line is a little ugly, but it simplifies the if statement below. */
560 /* Basically if using webDAV, we want the OR condition to use these too. */
564 * These are the headers as defined in RFC2518 to add webDAV support
567 #define OR_WEBDAV || \
568 (0 == strcmpic(v[0], "propfind")) || \
569 (0 == strcmpic(v[0], "proppatch")) || \
570 (0 == strcmpic(v[0], "move")) || \
571 (0 == strcmpic(v[0], "copy")) || \
572 (0 == strcmpic(v[0], "mkcol")) || \
573 (0 == strcmpic(v[0], "lock")) || \
574 (0 == strcmpic(v[0], "unlock"))
576 #else /* No webDAV support is enabled. Provide an empty OR_WEBDAV macro. */
582 /* or it could be a GET or a POST (possibly webDAV too) */
583 if ((strcmpic(v[0], "get") == 0) ||
584 (strcmpic(v[0], "head") == 0) OR_WEBDAV ||
585 (strcmpic(v[0], "post") == 0))
588 http->gpc = strdup(v[0]);
590 http->ver = strdup(v[2]);
592 if (strncmpic(url, "http://", 7) == 0)
596 else if (strncmpic(url, "https://", 8) == 0)
605 if (url && (p = strchr(url, '/')))
607 http->path = strdup(p);
609 http->hostport = strdup(url);
617 if (http->hostport == NULL)
619 free_http_request(http);
623 buf = strdup(http->hostport);
626 /* check if url contains password */
627 n = ssplit(buf, "@", v, SZ(v), 1, 1);
630 char * newbuf = NULL;
631 newbuf = strdup(v[1]);
636 n = ssplit(buf, ":", v, SZ(v), 1, 1);
640 http->host = strdup(v[0]);
646 http->host = strdup(v[0]);
647 http->port = atoi(v[1]);
652 if (http->host == NULL)
654 free_http_request(http);
657 if (http->path == NULL)
659 http->path = strdup("");
665 /* here begins the family of parser functions that reformat header lines */
668 /*********************************************************************
672 * Description : This is called if a header matches a pattern to "crunch"
675 * 1 : v = Pointer to parsers structure, which basically holds
676 * headers (client or server) that we want to "crunch"
677 * 2 : s = header (from sed) to "crunch"
678 * 3 : csp = Current client state (buffers, headers, etc...)
680 * Returns : Always NULL.
682 *********************************************************************/
683 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
685 log_error(LOG_LEVEL_HEADER, "crunch!");
691 #if defined(PCRS) || defined(KILLPOPUPS)
693 /*********************************************************************
695 * Function : content_type
697 * Description : Is this a text/.* or javascript MIME Type?
701 * 2 : s = header string we are "considering"
702 * 3 : csp = Current client state (buffers, headers, etc...)
704 * Returns : A duplicate string pointer to this header (ie. pass thru)
706 *********************************************************************/
707 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
709 if (strstr (s, " text/") || strstr (s, "application/x-javascript"))
717 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
721 /*********************************************************************
723 * Function : content_length
725 * Description : Crunch Content-Length header if & only if we are
726 * filtering this page through PCRS.
730 * 2 : s = header string we are "considering"
731 * 3 : csp = Current client state (buffers, headers, etc...)
733 * Returns : A duplicate string pointer to this header (ie. pass thru)
735 *********************************************************************/
736 char *content_length(const struct parsers *v, char *s, struct client_state *csp)
738 if (csp->content_length != 0) /* Content has been modified */
740 s = (char *) zalloc(100);
741 sprintf(s, "Content-Length: %d", csp->content_length);
742 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
751 #endif /* def PCRS */
754 /*********************************************************************
756 * Function : client_referrer
758 * Description : Handle the "referer" config setting properly.
763 * 2 : s = header (from sed) to "crunch"
764 * 3 : csp = Current client state (buffers, headers, etc...)
766 * Returns : NULL if crunched, or a malloc'ed string with the original
769 *********************************************************************/
770 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
774 /* Since the referrer can include the prefix even
775 * even if the request itself is non-forced, we must
776 * clean it unconditionally
778 strclean(s, FORCE_PREFIX);
779 #endif /* def FORCE_LOAD */
782 csp->referrer = strdup(s);
783 #endif /* def TRUST_FILES */
786 * Are we sending referer?
788 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
793 newval = csp->action->string[ACTION_STRING_REFERER];
796 * Are we blocking referer?
798 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
800 log_error(LOG_LEVEL_HEADER, "crunch!");
805 * Are we forging referer?
807 if (0 == strcmpic(newval, "forge"))
810 * Forge a referer as http://[hostname:port of REQUEST]/
811 * to fool stupid checks for in-site links
813 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
814 s = strsav(NULL, "Referer: ");
815 s = strsav(s, "http://");
816 s = strsav(s, csp->http->hostport);
822 * Have we got a fixed referer?
824 if (0 == strncmpic(newval, "http://", 7))
827 * We have a specific (fixed) referer we want to send.
830 log_error(LOG_LEVEL_HEADER, "modified");
832 s = strsav( NULL, "Referer: " );
833 s = strsav( s, newval );
837 /* Should never get here! */
838 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
841 * Forge is probably the best default.
843 * Forge a referer as http://[hostname:port of REQUEST]/
844 * to fool stupid checks for in-site links
846 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
847 s = strsav(NULL, "Referer: ");
848 s = strsav(s, "http://");
849 s = strsav(s, csp->http->hostport);
855 /*********************************************************************
857 * Function : client_uagent
859 * Description : Handle the "user-agent" config setting properly.
864 * 2 : s = header (from sed) to "crunch"
865 * 3 : csp = Current client state (buffers, headers, etc...)
867 * Returns : A malloc'ed pointer to the default agent, or
868 * a malloc'ed string pointer to this header (ie. pass thru).
870 *********************************************************************/
871 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
875 #ifdef DETECT_MSIE_IMAGES
876 if (strstr (s, "MSIE "))
878 /* This is Microsoft Internet Explorer.
879 * Enable auto-detect.
881 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
883 #endif /* def DETECT_MSIE_IMAGES */
885 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
890 newval = csp->action->string[ACTION_STRING_USER_AGENT];
896 log_error(LOG_LEVEL_HEADER, "modified");
898 s = strsav( NULL, "User-Agent: " );
899 s = strsav( s, newval );
905 /*********************************************************************
907 * Function : client_ua
909 * Description : Handle "ua-" headers properly. Called from `sed'.
913 * 2 : s = header (from sed) to "crunch"
914 * 3 : csp = Current client state (buffers, headers, etc...)
916 * Returns : NULL if crunched, or a malloc'ed string to original header
918 *********************************************************************/
919 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
921 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
927 log_error(LOG_LEVEL_HEADER, "crunch!");
933 /*********************************************************************
935 * Function : client_from
937 * Description : Handle the "from" config setting properly.
942 * 2 : s = header (from sed) to "crunch"
943 * 3 : csp = Current client state (buffers, headers, etc...)
945 * Returns : NULL if crunched, or a malloc'ed string to
946 * modified/original header.
948 *********************************************************************/
949 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
953 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
958 newval = csp->action->string[ACTION_STRING_FROM];
961 * Are we blocking referer?
963 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
965 log_error(LOG_LEVEL_HEADER, "crunch!");
969 log_error(LOG_LEVEL_HEADER, " modified");
971 s = strsav( NULL, "From: " );
972 s = strsav( s, newval );
978 /*********************************************************************
980 * Function : client_send_cookie
982 * Description : Handle the "cookie" header properly. Called from `sed'.
983 * If cookie is accepted, add it to the cookie_list,
984 * else we crunch it. Mmmmmmmmmmm ... cookie ......
987 * 1 : v = pattern of cookie `sed' found matching
988 * 2 : s = header (from sed) to "crunch"
989 * 3 : csp = Current client state (buffers, headers, etc...)
991 * Returns : Always NULL.
993 *********************************************************************/
994 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
996 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
998 enlist(csp->cookie_list, s + v->len + 1);
1002 log_error(LOG_LEVEL_HEADER, " crunch!");
1006 * Always return NULL here. The cookie header
1007 * will be sent at the end of the header.
1014 /*********************************************************************
1016 * Function : client_x_forwarded
1018 * Description : Handle the "x-forwarded-for" config setting properly,
1019 * also used in the add_client_headers list. Called from `sed'.
1023 * 2 : s = header (from sed) to "crunch"
1024 * 3 : csp = Current client state (buffers, headers, etc...)
1026 * Returns : Always NULL.
1028 *********************************************************************/
1029 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1031 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1033 /* Save it so we can re-add it later */
1034 csp->x_forwarded = strdup(s);
1038 * Always return NULL, since this information
1039 * will be sent at the end of the header.
1046 #if defined(DETECT_MSIE_IMAGES)
1047 /*********************************************************************
1049 * Function : client_accept
1051 * Description : Detect whether the client wants HTML or an image.
1052 * Clients do not always make this information available
1053 * in a sane way. Always passes the header through
1054 * the proxy unchanged.
1058 * 2 : s = Header string. Null terminated.
1059 * 3 : csp = Current client state (buffers, headers, etc...)
1061 * Returns : Duplicate of argument s.
1063 *********************************************************************/
1064 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1066 #ifdef DETECT_MSIE_IMAGES
1067 if (strstr (s, "image/gif"))
1069 /* Client will accept HTML. If this seems counterintuitive,
1072 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1076 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1078 #endif /* def DETECT_MSIE_IMAGES */
1083 #endif /* defined(DETECT_MSIE_IMAGES) */
1087 /* the following functions add headers directly to the header list */
1090 /*********************************************************************
1092 * Function : client_cookie_adder
1094 * Description : Used in the add_client_headers list. Called from `sed'.
1097 * 1 : csp = Current client state (buffers, headers, etc...)
1101 *********************************************************************/
1102 void client_cookie_adder(struct client_state *csp)
1105 struct list_share *lsts;
1109 for (lst = csp->cookie_list->next; lst ; lst = lst->next)
1113 tmp = strsav(tmp, "; ");
1115 tmp = strsav(tmp, lst->str);
1118 for (lsts = csp->action->multi[ACTION_MULTI_WAFER]->next; lsts ; lsts = lsts->next)
1122 tmp = strsav(tmp, "; ");
1125 if ((e = cookie_encode(lsts->str)))
1127 tmp = strsav(tmp, e);
1136 ret = strdup("Cookie: ");
1137 ret = strsav(ret, tmp);
1138 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1139 enlist(csp->headers, ret);
1147 /*********************************************************************
1149 * Function : client_xtra_adder
1151 * Description : Used in the add_client_headers list. Called from `sed'.
1154 * 1 : csp = Current client state (buffers, headers, etc...)
1158 *********************************************************************/
1159 void client_xtra_adder(struct client_state *csp)
1161 struct list_share *l = csp->action->multi[ACTION_MULTI_ADD_HEADER];
1163 for (l = l->next; l ; l = l->next)
1165 log_error(LOG_LEVEL_HEADER, "addh: %s", l->str);
1166 enlist(csp->headers, l->str);
1172 /*********************************************************************
1174 * Function : client_x_forwarded_adder
1176 * Description : Used in the add_client_headers list. Called from `sed'.
1179 * 1 : csp = Current client state (buffers, headers, etc...)
1183 *********************************************************************/
1184 void client_x_forwarded_adder(struct client_state *csp)
1188 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1193 if (csp->x_forwarded)
1195 p = strsav(p, csp->x_forwarded);
1196 p = strsav(p, ", ");
1197 p = strsav(p, csp->ip_addr_str);
1201 p = strsav(p, "X-Forwarded-For: ");
1202 p = strsav(p, csp->ip_addr_str);
1205 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1206 enlist(csp->headers, p);
1211 /*********************************************************************
1213 * Function : server_set_cookie
1215 * Description : Handle the server "cookie" header properly.
1216 * Log cookie to the jar file. Then "crunch" it,
1217 * or accept it. Called from `sed'.
1220 * 1 : v = parser pattern that matched this header
1221 * 2 : s = header that matched this pattern
1222 * 3 : csp = Current client state (buffers, headers, etc...)
1224 * Returns : `crumble' or a newly malloc'ed string.
1226 *********************************************************************/
1227 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1230 if (csp->config->jar)
1232 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1234 #endif /* def JAR_FILES */
1236 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1238 return(crumble(v, s, csp));
1247 /*********************************************************************
1249 * Function : client_host
1251 * Description : Clean the FORCE_PREFIX out of the 'host' http
1252 * header, if we use force
1256 * 2 : s = header (from sed) to clean
1257 * 3 : csp = Current client state (buffers, headers, etc...)
1259 * Returns : A malloc'ed pointer to the cleaned host header
1261 *********************************************************************/
1262 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1264 char *cleanhost = strdup(s);
1267 strclean(cleanhost, FORCE_PREFIX);
1271 #endif /* def FORCE_LOAD */
1275 /*********************************************************************
1277 * Function : strclean
1279 * Description : In-Situ-Eliminate all occurances of substring in
1283 * 1 : string = string to clean
1284 * 2 : substring = substring to eliminate
1286 * Returns : Number of eliminations
1288 *********************************************************************/
1289 int strclean(const char *string, const char *substring)
1291 int hits = 0, len = strlen(substring);
1294 while((pos = strstr(string, substring)))
1301 while (*p++ != '\0');
1308 #endif /* def FORCE_LOAD */