1 const char parsers_rcs[] = "$Id: parsers.c,v 1.15 2001/06/03 11:03:48 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.15 2001/06/03 11:03:48 oes
51 * adapted to new enlist_unique arg format
55 * introduced confdir option
57 * filters.c filtrers.h
59 * extracted-CGI relevant stuff
67 * support for new cgi mechansim
71 * functions for new list type: "map"
72 * extended enlist_unique
79 * deleted const struct interceptors
87 * added struct http_response
88 * changes struct interceptors to struct cgi_dispatcher
89 * moved HTML stuff to cgi.h
98 * Revision 1.14 2001/06/01 18:49:17 jongfoster
99 * Replaced "list_share" with "list" - the tiny memory gain was not
100 * worth the extra complexity.
102 * Revision 1.13 2001/05/31 21:30:33 jongfoster
103 * Removed list code - it's now in list.[ch]
104 * Renamed "permission" to "action", and changed many features
105 * to use the actions file rather than the global config.
107 * Revision 1.12 2001/05/31 17:33:13 oes
111 * Revision 1.11 2001/05/29 20:11:19 joergs
112 * '/* inside comment' warning removed.
114 * Revision 1.10 2001/05/29 09:50:24 jongfoster
115 * Unified blocklist/imagelist/permissionslist.
116 * File format is still under discussion, but the internal changes
119 * Also modified interceptor behaviour:
120 * - We now intercept all URLs beginning with one of the following
121 * prefixes (and *only* these prefixes):
123 * * http://ijbswa.sf.net/config/
124 * * http://ijbswa.sourceforge.net/config/
125 * - New interceptors "home page" - go to http://i.j.b/ to see it.
126 * - Internal changes so that intercepted and fast redirect pages
127 * are not replaced with an image.
128 * - Interceptors now have the option to send a binary page direct
129 * to the client. (i.e. ijb-send-banner uses this)
130 * - Implemented show-url-info interceptor. (Which is why I needed
131 * the above interceptors changes - a typical URL is
132 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
133 * The previous mechanism would not have intercepted that, and
134 * if it had been intercepted then it then it would have replaced
137 * Revision 1.9 2001/05/28 17:26:33 jongfoster
138 * Fixing segfault if last header was crunched.
139 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
140 * can use the cross-platform sprintf() instead.)
142 * Revision 1.8 2001/05/27 22:17:04 oes
144 * - re_process_buffer no longer writes the modified buffer
145 * to the client, which was very ugly. It now returns the
146 * buffer, which it is then written by chat.
148 * - content_length now adjusts the Content-Length: header
149 * for modified documents rather than crunch()ing it.
150 * (Length info in csp->content_length, which is 0 for
151 * unmodified documents)
153 * - For this to work, sed() is called twice when filtering.
155 * Revision 1.7 2001/05/27 13:19:06 oes
156 * Patched Joergs solution for the content-length in.
158 * Revision 1.6 2001/05/26 13:39:32 jongfoster
159 * Only crunches Content-Length header if applying RE filtering.
160 * Without this fix, Microsoft Windows Update wouldn't work.
162 * Revision 1.5 2001/05/26 00:28:36 jongfoster
163 * Automatic reloading of config file.
164 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
165 * Most of the global variables have been moved to a new
166 * struct configuration_spec, accessed through csp->config->globalname
167 * Most of the globals remaining are used by the Win32 GUI.
169 * Revision 1.4 2001/05/22 18:46:04 oes
171 * - Enabled filtering banners by size rather than URL
172 * by adding patterns that replace all standard banner
173 * sizes with the "Junkbuster" gif to the re_filterfile
175 * - Enabled filtering WebBugs by providing a pattern
176 * which kills all 1x1 images
178 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
179 * which is selected by the (nonstandard and therefore
180 * capital) letter 'U' in the option string.
181 * It causes the quantifiers to be ungreedy by default.
182 * Appending a ? turns back to greedy (!).
184 * - Added a new interceptor ijb-send-banner, which
185 * sends back the "Junkbuster" gif. Without imagelist or
186 * MSIE detection support, or if tinygif = 1, or the
187 * URL isn't recognized as an imageurl, a lame HTML
188 * explanation is sent instead.
190 * - Added new feature, which permits blocking remote
191 * script redirects and firing back a local redirect
193 * The feature is conditionally compiled, i.e. it
194 * can be disabled with --disable-fast-redirects,
195 * plus it must be activated by a "fast-redirects"
196 * line in the config file, has its own log level
197 * and of course wants to be displayed by show-proxy-args
198 * Note: Boy, all the #ifdefs in 1001 locations and
199 * all the fumbling with configure.in and acconfig.h
200 * were *way* more work than the feature itself :-(
202 * - Because a generic redirect template was needed for
203 * this, tinygif = 3 now uses the same.
205 * - Moved GIFs, and other static HTTP response templates
210 * - Removed some >400 CRs again (Jon, you really worked
213 * Revision 1.3 2001/05/20 01:21:20 jongfoster
214 * Version 2.9.4 checkin.
215 * - Merged popupfile and cookiefile, and added control over PCRS
216 * filtering, in new "permissionsfile".
217 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
218 * file error you now get a message box (in the Win32 GUI) rather
219 * than the program exiting with no explanation.
220 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
222 * - Removed tabs from "config"
223 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
224 * - Bumped up version number.
226 * Revision 1.2 2001/05/17 23:02:36 oes
227 * - Made referrer option accept 'L' as a substitute for '§'
229 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
230 * Initial import of version 2.9.3 source tree
233 *********************************************************************/
239 #include <sys/types.h>
254 #include "showargs.h"
258 #include "jbsockets.h"
259 #include "miscutil.h"
262 const char parsers_h_rcs[] = PARSERS_H_VERSION;
264 /* Fix a problem with Solaris. There should be no effect on other
266 * Solaris's isspace() is a macro which uses it's argument directly
267 * as an array index. Therefore we need to make sure that high-bit
268 * characters generate +ve values, and ideally we also want to make
269 * the argument match the declared parameter type of "int".
271 * Why did they write a character function that can't take a simple
272 * "char" argument? Doh!
274 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
275 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
278 const struct parsers client_patterns[] = {
279 { "referer:", 8, client_referrer },
280 { "user-agent:", 11, client_uagent },
281 { "ua-", 3, client_ua },
282 { "from:", 5, client_from },
283 { "cookie:", 7, client_send_cookie },
284 { "x-forwarded-for:", 16, client_x_forwarded },
285 { "proxy-connection:", 17, crumble },
287 { "Accept-Encoding: gzip", 21, crumble },
288 #endif /* def DENY_GZIP */
289 #if defined(DETECT_MSIE_IMAGES)
290 { "Accept:", 7, client_accept },
291 #endif /* defined(DETECT_MSIE_IMAGES) */
293 { "Host:", 5, client_host },
294 #endif /* def FORCE_LOAD */
295 /* { "if-modified-since:", 18, crumble }, */
300 const struct parsers server_patterns[] = {
301 { "set-cookie:", 11, server_set_cookie },
302 { "connection:", 11, crumble },
303 #if defined(PCRS) || defined(KILLPOPUPS)
304 { "Content-Type:", 13, content_type },
305 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
307 { "Content-Length:", 15, content_length },
308 #endif /* def PCRS */
313 void (* const add_client_headers[])(struct client_state *) = {
315 client_x_forwarded_adder,
321 void (* const add_server_headers[])(struct client_state *) = {
326 /*********************************************************************
328 * Function : flush_socket
330 * Description : Write any pending "buffered" content.
333 * 1 : fd = file descriptor of the socket to read
334 * 2 : csp = Current client state (buffers, headers, etc...)
336 * Returns : On success, the number of bytes written are returned (zero
337 * indicates nothing was written). On error, -1 is returned,
338 * and errno is set appropriately. If count is zero and the
339 * file descriptor refers to a regular file, 0 will be
340 * returned without causing any other effect. For a special
341 * file, the results are not portable.
343 *********************************************************************/
344 int flush_socket(int fd, struct client_state *csp)
346 struct iob *iob = csp->iob;
347 int n = iob->eod - iob->cur;
354 n = write_socket(fd, iob->cur, n);
355 iob->eod = iob->cur = iob->buf;
361 /*********************************************************************
363 * Function : add_to_iob
365 * Description : Add content to the buffered page.
368 * 1 : csp = Current client state (buffers, headers, etc...)
369 * 2 : buf = holds the content to be added to the page
370 * 3 : n = number of bytes to be added
372 * Returns : Number of bytes in the content buffer.
374 *********************************************************************/
375 int add_to_iob(struct client_state *csp, char *buf, int n)
377 struct iob *iob = csp->iob;
381 have = iob->eod - iob->cur;
390 if ((p = (char *)malloc(need + 1)) == NULL)
392 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
398 /* there is something in the buffer - save it */
399 memcpy(p, iob->cur, have);
401 /* replace the buffer with the new space */
405 /* point to the end of the data */
410 /* the buffer is empty, free it and reinitialize */
415 /* copy the new data into the iob buffer */
418 /* point to the end of the data */
421 /* null terminate == cheap insurance */
424 /* set the pointers to the new values */
433 /*********************************************************************
435 * Function : get_header
437 * Description : This (odd) routine will parse the csp->iob
440 * 1 : csp = Current client state (buffers, headers, etc...)
442 * Returns : Any one of the following:
444 * 1) a pointer to a dynamically allocated string that contains a header line
445 * 2) NULL indicating that the end of the header was reached
446 * 3) "" indicating that the end of the iob was reached before finding
447 * a complete header line.
449 *********************************************************************/
450 char *get_header(struct client_state *csp)
456 if ((iob->cur == NULL)
457 || ((p = strchr(iob->cur, '\n')) == NULL))
459 return(""); /* couldn't find a complete header */
464 ret = strdup(iob->cur);
468 if ((q = strchr(ret, '\r'))) *q = '\0';
470 /* is this a blank linke (i.e. the end of the header) ? */
482 /*********************************************************************
486 * Description : add, delete or modify lines in the HTTP header streams.
487 * On entry, it receives a linked list of headers space
488 * that was allocated dynamically (both the list nodes
489 * and the header contents).
491 * As a side effect it frees the space used by the original
495 * 1 : pats = list of patterns to match against headers
496 * 2 : more_headers = list of functions to add more
497 * headers (client or server)
498 * 3 : csp = Current client state (buffers, headers, etc...)
500 * Returns : Single pointer to a fully formed header.
502 *********************************************************************/
503 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
506 const struct parsers *v;
510 for (v = pats; v->str ; v++)
512 for (p = csp->headers->next; p ; p = p->next)
514 /* Header crunch()ed in previous run? -> ignore */
515 if (p->str == NULL) continue;
517 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
519 if (strncmpic(p->str, v->str, v->len) == 0)
521 hdr = v->parser(v, p->str, csp);
528 /* place any additional headers on the csp->headers list */
529 for (f = more_headers; *f ; f++)
534 /* add the blank line at the end of the header, if necessary */
535 if ( (csp->headers->last == NULL)
536 || (csp->headers->last->str == NULL)
537 || (*csp->headers->last->str != '\0') )
539 enlist(csp->headers, "");
542 hdr = list_to_text(csp->headers);
549 /*********************************************************************
551 * Function : free_http_request
553 * Description : Freez a http_request structure
556 * 1 : http = points to a http_request structure to free
560 *********************************************************************/
561 void free_http_request(struct http_request *http)
566 freez(http->hostport);
573 /*********************************************************************
575 * Function : parse_http_request
577 * Description : Parse out the host and port from the URL. Find the
578 * hostname & path, port (if ':'), and/or password (if '@')
581 * 1 : req = URL (or is it URI?) to break down
582 * 2 : http = pointer to the http structure to hold elements
583 * 3 : csp = Current client state (buffers, headers, etc...)
587 *********************************************************************/
588 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
590 char *buf, *v[10], *url, *p;
593 memset(http, '\0', sizeof(*http));
595 http->cmd = strdup(req);
599 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
603 /* this could be a CONNECT request */
604 if (strcmpic(v[0], "connect") == 0)
607 http->gpc = strdup(v[0]);
608 http->hostport = strdup(v[1]);
609 http->ver = strdup(v[2]);
614 /* This next line is a little ugly, but it simplifies the if statement below. */
615 /* Basically if using webDAV, we want the OR condition to use these too. */
619 * These are the headers as defined in RFC2518 to add webDAV support
622 #define OR_WEBDAV || \
623 (0 == strcmpic(v[0], "propfind")) || \
624 (0 == strcmpic(v[0], "proppatch")) || \
625 (0 == strcmpic(v[0], "move")) || \
626 (0 == strcmpic(v[0], "copy")) || \
627 (0 == strcmpic(v[0], "mkcol")) || \
628 (0 == strcmpic(v[0], "lock")) || \
629 (0 == strcmpic(v[0], "unlock"))
631 #else /* No webDAV support is enabled. Provide an empty OR_WEBDAV macro. */
637 /* or it could be a GET or a POST (possibly webDAV too) */
638 if ((strcmpic(v[0], "get") == 0) ||
639 (strcmpic(v[0], "head") == 0) OR_WEBDAV ||
640 (strcmpic(v[0], "post") == 0))
643 http->gpc = strdup(v[0]);
645 http->ver = strdup(v[2]);
647 if (strncmpic(url, "http://", 7) == 0)
651 else if (strncmpic(url, "https://", 8) == 0)
660 if (url && (p = strchr(url, '/')))
662 http->path = strdup(p);
664 http->hostport = strdup(url);
672 if (http->hostport == NULL)
674 free_http_request(http);
678 buf = strdup(http->hostport);
681 /* check if url contains password */
682 n = ssplit(buf, "@", v, SZ(v), 1, 1);
685 char * newbuf = NULL;
686 newbuf = strdup(v[1]);
691 n = ssplit(buf, ":", v, SZ(v), 1, 1);
695 http->host = strdup(v[0]);
701 http->host = strdup(v[0]);
702 http->port = atoi(v[1]);
707 if (http->host == NULL)
709 free_http_request(http);
712 if (http->path == NULL)
714 http->path = strdup("");
720 /* here begins the family of parser functions that reformat header lines */
723 /*********************************************************************
727 * Description : This is called if a header matches a pattern to "crunch"
730 * 1 : v = Pointer to parsers structure, which basically holds
731 * headers (client or server) that we want to "crunch"
732 * 2 : s = header (from sed) to "crunch"
733 * 3 : csp = Current client state (buffers, headers, etc...)
735 * Returns : Always NULL.
737 *********************************************************************/
738 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
740 log_error(LOG_LEVEL_HEADER, "crunch!");
746 #if defined(PCRS) || defined(KILLPOPUPS)
748 /*********************************************************************
750 * Function : content_type
752 * Description : Is this a text/.* or javascript MIME Type?
756 * 2 : s = header string we are "considering"
757 * 3 : csp = Current client state (buffers, headers, etc...)
759 * Returns : A duplicate string pointer to this header (ie. pass thru)
761 *********************************************************************/
762 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
764 if (strstr (s, " text/") || strstr (s, "application/x-javascript"))
772 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
776 /*********************************************************************
778 * Function : content_length
780 * Description : Crunch Content-Length header if & only if we are
781 * filtering this page through PCRS.
785 * 2 : s = header string we are "considering"
786 * 3 : csp = Current client state (buffers, headers, etc...)
788 * Returns : A duplicate string pointer to this header (ie. pass thru)
790 *********************************************************************/
791 char *content_length(const struct parsers *v, char *s, struct client_state *csp)
793 if (csp->content_length != 0) /* Content has been modified */
795 s = (char *) zalloc(100);
796 sprintf(s, "Content-Length: %d", csp->content_length);
797 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
806 #endif /* def PCRS */
809 /*********************************************************************
811 * Function : client_referrer
813 * Description : Handle the "referer" config setting properly.
818 * 2 : s = header (from sed) to "crunch"
819 * 3 : csp = Current client state (buffers, headers, etc...)
821 * Returns : NULL if crunched, or a malloc'ed string with the original
824 *********************************************************************/
825 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
829 /* Since the referrer can include the prefix even
830 * even if the request itself is non-forced, we must
831 * clean it unconditionally
833 strclean(s, FORCE_PREFIX);
834 #endif /* def FORCE_LOAD */
837 csp->referrer = strdup(s);
838 #endif /* def TRUST_FILES */
841 * Are we sending referer?
843 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
848 newval = csp->action->string[ACTION_STRING_REFERER];
851 * Are we blocking referer?
853 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
855 log_error(LOG_LEVEL_HEADER, "crunch!");
860 * Are we forging referer?
862 if (0 == strcmpic(newval, "forge"))
865 * Forge a referer as http://[hostname:port of REQUEST]/
866 * to fool stupid checks for in-site links
868 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
869 s = strsav(NULL, "Referer: ");
870 s = strsav(s, "http://");
871 s = strsav(s, csp->http->hostport);
877 * Have we got a fixed referer?
879 if (0 == strncmpic(newval, "http://", 7))
882 * We have a specific (fixed) referer we want to send.
885 log_error(LOG_LEVEL_HEADER, "modified");
887 s = strsav( NULL, "Referer: " );
888 s = strsav( s, newval );
892 /* Should never get here! */
893 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
896 * Forge is probably the best default.
898 * Forge a referer as http://[hostname:port of REQUEST]/
899 * to fool stupid checks for in-site links
901 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
902 s = strsav(NULL, "Referer: ");
903 s = strsav(s, "http://");
904 s = strsav(s, csp->http->hostport);
910 /*********************************************************************
912 * Function : client_uagent
914 * Description : Handle the "user-agent" config setting properly.
919 * 2 : s = header (from sed) to "crunch"
920 * 3 : csp = Current client state (buffers, headers, etc...)
922 * Returns : A malloc'ed pointer to the default agent, or
923 * a malloc'ed string pointer to this header (ie. pass thru).
925 *********************************************************************/
926 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
930 #ifdef DETECT_MSIE_IMAGES
931 if (strstr (s, "MSIE "))
933 /* This is Microsoft Internet Explorer.
934 * Enable auto-detect.
936 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
938 #endif /* def DETECT_MSIE_IMAGES */
940 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
945 newval = csp->action->string[ACTION_STRING_USER_AGENT];
951 log_error(LOG_LEVEL_HEADER, "modified");
953 s = strsav( NULL, "User-Agent: " );
954 s = strsav( s, newval );
960 /*********************************************************************
962 * Function : client_ua
964 * Description : Handle "ua-" headers properly. Called from `sed'.
968 * 2 : s = header (from sed) to "crunch"
969 * 3 : csp = Current client state (buffers, headers, etc...)
971 * Returns : NULL if crunched, or a malloc'ed string to original header
973 *********************************************************************/
974 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
976 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
982 log_error(LOG_LEVEL_HEADER, "crunch!");
988 /*********************************************************************
990 * Function : client_from
992 * Description : Handle the "from" config setting properly.
997 * 2 : s = header (from sed) to "crunch"
998 * 3 : csp = Current client state (buffers, headers, etc...)
1000 * Returns : NULL if crunched, or a malloc'ed string to
1001 * modified/original header.
1003 *********************************************************************/
1004 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
1006 const char * newval;
1008 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
1013 newval = csp->action->string[ACTION_STRING_FROM];
1016 * Are we blocking referer?
1018 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
1020 log_error(LOG_LEVEL_HEADER, "crunch!");
1024 log_error(LOG_LEVEL_HEADER, " modified");
1026 s = strsav( NULL, "From: " );
1027 s = strsav( s, newval );
1033 /*********************************************************************
1035 * Function : client_send_cookie
1037 * Description : Handle the "cookie" header properly. Called from `sed'.
1038 * If cookie is accepted, add it to the cookie_list,
1039 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1042 * 1 : v = pattern of cookie `sed' found matching
1043 * 2 : s = header (from sed) to "crunch"
1044 * 3 : csp = Current client state (buffers, headers, etc...)
1046 * Returns : Always NULL.
1048 *********************************************************************/
1049 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
1051 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1053 enlist(csp->cookie_list, s + v->len + 1);
1057 log_error(LOG_LEVEL_HEADER, " crunch!");
1061 * Always return NULL here. The cookie header
1062 * will be sent at the end of the header.
1069 /*********************************************************************
1071 * Function : client_x_forwarded
1073 * Description : Handle the "x-forwarded-for" config setting properly,
1074 * also used in the add_client_headers list. Called from `sed'.
1078 * 2 : s = header (from sed) to "crunch"
1079 * 3 : csp = Current client state (buffers, headers, etc...)
1081 * Returns : Always NULL.
1083 *********************************************************************/
1084 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1086 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1088 /* Save it so we can re-add it later */
1089 csp->x_forwarded = strdup(s);
1093 * Always return NULL, since this information
1094 * will be sent at the end of the header.
1101 #if defined(DETECT_MSIE_IMAGES)
1102 /*********************************************************************
1104 * Function : client_accept
1106 * Description : Detect whether the client wants HTML or an image.
1107 * Clients do not always make this information available
1108 * in a sane way. Always passes the header through
1109 * the proxy unchanged.
1113 * 2 : s = Header string. Null terminated.
1114 * 3 : csp = Current client state (buffers, headers, etc...)
1116 * Returns : Duplicate of argument s.
1118 *********************************************************************/
1119 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1121 #ifdef DETECT_MSIE_IMAGES
1122 if (strstr (s, "image/gif"))
1124 /* Client will accept HTML. If this seems counterintuitive,
1127 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1131 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1133 #endif /* def DETECT_MSIE_IMAGES */
1138 #endif /* defined(DETECT_MSIE_IMAGES) */
1142 /* the following functions add headers directly to the header list */
1145 /*********************************************************************
1147 * Function : client_cookie_adder
1149 * Description : Used in the add_client_headers list. Called from `sed'.
1152 * 1 : csp = Current client state (buffers, headers, etc...)
1156 *********************************************************************/
1157 void client_cookie_adder(struct client_state *csp)
1163 for (lst = csp->cookie_list->next; lst ; lst = lst->next)
1167 tmp = strsav(tmp, "; ");
1169 tmp = strsav(tmp, lst->str);
1172 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->next; lst ; lst = lst->next)
1176 tmp = strsav(tmp, "; ");
1179 if ((e = cookie_encode(lst->str)))
1181 tmp = strsav(tmp, e);
1190 ret = strdup("Cookie: ");
1191 ret = strsav(ret, tmp);
1192 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1193 enlist(csp->headers, ret);
1201 /*********************************************************************
1203 * Function : client_xtra_adder
1205 * Description : Used in the add_client_headers list. Called from `sed'.
1208 * 1 : csp = Current client state (buffers, headers, etc...)
1212 *********************************************************************/
1213 void client_xtra_adder(struct client_state *csp)
1215 struct list *lst = csp->action->multi[ACTION_MULTI_ADD_HEADER];
1217 for (lst = lst->next; lst ; lst = lst->next)
1219 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1220 enlist(csp->headers, lst->str);
1226 /*********************************************************************
1228 * Function : client_x_forwarded_adder
1230 * Description : Used in the add_client_headers list. Called from `sed'.
1233 * 1 : csp = Current client state (buffers, headers, etc...)
1237 *********************************************************************/
1238 void client_x_forwarded_adder(struct client_state *csp)
1242 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1247 if (csp->x_forwarded)
1249 p = strsav(p, csp->x_forwarded);
1250 p = strsav(p, ", ");
1251 p = strsav(p, csp->ip_addr_str);
1255 p = strsav(p, "X-Forwarded-For: ");
1256 p = strsav(p, csp->ip_addr_str);
1259 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1260 enlist(csp->headers, p);
1265 /*********************************************************************
1267 * Function : server_set_cookie
1269 * Description : Handle the server "cookie" header properly.
1270 * Log cookie to the jar file. Then "crunch" it,
1271 * or accept it. Called from `sed'.
1274 * 1 : v = parser pattern that matched this header
1275 * 2 : s = header that matched this pattern
1276 * 3 : csp = Current client state (buffers, headers, etc...)
1278 * Returns : `crumble' or a newly malloc'ed string.
1280 *********************************************************************/
1281 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1284 if (csp->config->jar)
1286 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1288 #endif /* def JAR_FILES */
1290 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1292 return(crumble(v, s, csp));
1301 /*********************************************************************
1303 * Function : client_host
1305 * Description : Clean the FORCE_PREFIX out of the 'host' http
1306 * header, if we use force
1310 * 2 : s = header (from sed) to clean
1311 * 3 : csp = Current client state (buffers, headers, etc...)
1313 * Returns : A malloc'ed pointer to the cleaned host header
1315 *********************************************************************/
1316 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1318 char *cleanhost = strdup(s);
1321 strclean(cleanhost, FORCE_PREFIX);
1325 #endif /* def FORCE_LOAD */
1329 /*********************************************************************
1331 * Function : strclean
1333 * Description : In-Situ-Eliminate all occurances of substring in
1337 * 1 : string = string to clean
1338 * 2 : substring = substring to eliminate
1340 * Returns : Number of eliminations
1342 *********************************************************************/
1343 int strclean(const char *string, const char *substring)
1345 int hits = 0, len = strlen(substring);
1348 while((pos = strstr(string, substring)))
1355 while (*p++ != '\0');
1362 #endif /* def FORCE_LOAD */