1 const char parsers_rcs[] = "$Id: parsers.c,v 1.15 2001/06/03 19:12:38 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 19:12:38 oes
45 * deleted const struct interceptors
47 * Revision 1.14 2001/06/01 18:49:17 jongfoster
48 * Replaced "list_share" with "list" - the tiny memory gain was not
49 * worth the extra complexity.
51 * Revision 1.13 2001/05/31 21:30:33 jongfoster
52 * Removed list code - it's now in list.[ch]
53 * Renamed "permission" to "action", and changed many features
54 * to use the actions file rather than the global config.
56 * Revision 1.12 2001/05/31 17:33:13 oes
60 * Revision 1.11 2001/05/29 20:11:19 joergs
61 * '/* inside comment' warning removed.
63 * Revision 1.10 2001/05/29 09:50:24 jongfoster
64 * Unified blocklist/imagelist/permissionslist.
65 * File format is still under discussion, but the internal changes
68 * Also modified interceptor behaviour:
69 * - We now intercept all URLs beginning with one of the following
70 * prefixes (and *only* these prefixes):
72 * * http://ijbswa.sf.net/config/
73 * * http://ijbswa.sourceforge.net/config/
74 * - New interceptors "home page" - go to http://i.j.b/ to see it.
75 * - Internal changes so that intercepted and fast redirect pages
76 * are not replaced with an image.
77 * - Interceptors now have the option to send a binary page direct
78 * to the client. (i.e. ijb-send-banner uses this)
79 * - Implemented show-url-info interceptor. (Which is why I needed
80 * the above interceptors changes - a typical URL is
81 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
82 * The previous mechanism would not have intercepted that, and
83 * if it had been intercepted then it then it would have replaced
86 * Revision 1.9 2001/05/28 17:26:33 jongfoster
87 * Fixing segfault if last header was crunched.
88 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
89 * can use the cross-platform sprintf() instead.)
91 * Revision 1.8 2001/05/27 22:17:04 oes
93 * - re_process_buffer no longer writes the modified buffer
94 * to the client, which was very ugly. It now returns the
95 * buffer, which it is then written by chat.
97 * - content_length now adjusts the Content-Length: header
98 * for modified documents rather than crunch()ing it.
99 * (Length info in csp->content_length, which is 0 for
100 * unmodified documents)
102 * - For this to work, sed() is called twice when filtering.
104 * Revision 1.7 2001/05/27 13:19:06 oes
105 * Patched Joergs solution for the content-length in.
107 * Revision 1.6 2001/05/26 13:39:32 jongfoster
108 * Only crunches Content-Length header if applying RE filtering.
109 * Without this fix, Microsoft Windows Update wouldn't work.
111 * Revision 1.5 2001/05/26 00:28:36 jongfoster
112 * Automatic reloading of config file.
113 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
114 * Most of the global variables have been moved to a new
115 * struct configuration_spec, accessed through csp->config->globalname
116 * Most of the globals remaining are used by the Win32 GUI.
118 * Revision 1.4 2001/05/22 18:46:04 oes
120 * - Enabled filtering banners by size rather than URL
121 * by adding patterns that replace all standard banner
122 * sizes with the "Junkbuster" gif to the re_filterfile
124 * - Enabled filtering WebBugs by providing a pattern
125 * which kills all 1x1 images
127 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
128 * which is selected by the (nonstandard and therefore
129 * capital) letter 'U' in the option string.
130 * It causes the quantifiers to be ungreedy by default.
131 * Appending a ? turns back to greedy (!).
133 * - Added a new interceptor ijb-send-banner, which
134 * sends back the "Junkbuster" gif. Without imagelist or
135 * MSIE detection support, or if tinygif = 1, or the
136 * URL isn't recognized as an imageurl, a lame HTML
137 * explanation is sent instead.
139 * - Added new feature, which permits blocking remote
140 * script redirects and firing back a local redirect
142 * The feature is conditionally compiled, i.e. it
143 * can be disabled with --disable-fast-redirects,
144 * plus it must be activated by a "fast-redirects"
145 * line in the config file, has its own log level
146 * and of course wants to be displayed by show-proxy-args
147 * Note: Boy, all the #ifdefs in 1001 locations and
148 * all the fumbling with configure.in and acconfig.h
149 * were *way* more work than the feature itself :-(
151 * - Because a generic redirect template was needed for
152 * this, tinygif = 3 now uses the same.
154 * - Moved GIFs, and other static HTTP response templates
159 * - Removed some >400 CRs again (Jon, you really worked
162 * Revision 1.3 2001/05/20 01:21:20 jongfoster
163 * Version 2.9.4 checkin.
164 * - Merged popupfile and cookiefile, and added control over PCRS
165 * filtering, in new "permissionsfile".
166 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
167 * file error you now get a message box (in the Win32 GUI) rather
168 * than the program exiting with no explanation.
169 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
171 * - Removed tabs from "config"
172 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
173 * - Bumped up version number.
175 * Revision 1.2 2001/05/17 23:02:36 oes
176 * - Made referrer option accept 'L' as a substitute for 'ยง'
178 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
179 * Initial import of version 2.9.3 source tree
182 *********************************************************************/
188 #include <sys/types.h>
203 #include "showargs.h"
207 #include "jbsockets.h"
208 #include "miscutil.h"
211 const char parsers_h_rcs[] = PARSERS_H_VERSION;
213 /* Fix a problem with Solaris. There should be no effect on other
215 * Solaris's isspace() is a macro which uses it's argument directly
216 * as an array index. Therefore we need to make sure that high-bit
217 * characters generate +ve values, and ideally we also want to make
218 * the argument match the declared parameter type of "int".
220 * Why did they write a character function that can't take a simple
221 * "char" argument? Doh!
223 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
224 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
227 const struct parsers client_patterns[] = {
228 { "referer:", 8, client_referrer },
229 { "user-agent:", 11, client_uagent },
230 { "ua-", 3, client_ua },
231 { "from:", 5, client_from },
232 { "cookie:", 7, client_send_cookie },
233 { "x-forwarded-for:", 16, client_x_forwarded },
234 { "proxy-connection:", 17, crumble },
236 { "Accept-Encoding: gzip", 21, crumble },
237 #endif /* def DENY_GZIP */
238 #if defined(DETECT_MSIE_IMAGES)
239 { "Accept:", 7, client_accept },
240 #endif /* defined(DETECT_MSIE_IMAGES) */
242 { "Host:", 5, client_host },
243 #endif /* def FORCE_LOAD */
244 /* { "if-modified-since:", 18, crumble }, */
249 const struct parsers server_patterns[] = {
250 { "set-cookie:", 11, server_set_cookie },
251 { "connection:", 11, crumble },
252 #if defined(PCRS) || defined(KILLPOPUPS)
253 { "Content-Type:", 13, content_type },
254 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
256 { "Content-Length:", 15, content_length },
257 #endif /* def PCRS */
262 void (* const add_client_headers[])(struct client_state *) = {
264 client_x_forwarded_adder,
270 void (* const add_server_headers[])(struct client_state *) = {
275 /*********************************************************************
277 * Function : flush_socket
279 * Description : Write any pending "buffered" content.
282 * 1 : fd = file descriptor of the socket to read
283 * 2 : csp = Current client state (buffers, headers, etc...)
285 * Returns : On success, the number of bytes written are returned (zero
286 * indicates nothing was written). On error, -1 is returned,
287 * and errno is set appropriately. If count is zero and the
288 * file descriptor refers to a regular file, 0 will be
289 * returned without causing any other effect. For a special
290 * file, the results are not portable.
292 *********************************************************************/
293 int flush_socket(int fd, struct client_state *csp)
295 struct iob *iob = csp->iob;
296 int n = iob->eod - iob->cur;
303 n = write_socket(fd, iob->cur, n);
304 iob->eod = iob->cur = iob->buf;
310 /*********************************************************************
312 * Function : add_to_iob
314 * Description : Add content to the buffered page.
317 * 1 : csp = Current client state (buffers, headers, etc...)
318 * 2 : buf = holds the content to be added to the page
319 * 3 : n = number of bytes to be added
321 * Returns : Number of bytes in the content buffer.
323 *********************************************************************/
324 int add_to_iob(struct client_state *csp, char *buf, int n)
326 struct iob *iob = csp->iob;
330 have = iob->eod - iob->cur;
339 if ((p = (char *)malloc(need + 1)) == NULL)
341 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
347 /* there is something in the buffer - save it */
348 memcpy(p, iob->cur, have);
350 /* replace the buffer with the new space */
354 /* point to the end of the data */
359 /* the buffer is empty, free it and reinitialize */
364 /* copy the new data into the iob buffer */
367 /* point to the end of the data */
370 /* null terminate == cheap insurance */
373 /* set the pointers to the new values */
382 /*********************************************************************
384 * Function : get_header
386 * Description : This (odd) routine will parse the csp->iob
389 * 1 : csp = Current client state (buffers, headers, etc...)
391 * Returns : Any one of the following:
393 * 1) a pointer to a dynamically allocated string that contains a header line
394 * 2) NULL indicating that the end of the header was reached
395 * 3) "" indicating that the end of the iob was reached before finding
396 * a complete header line.
398 *********************************************************************/
399 char *get_header(struct client_state *csp)
405 if ((iob->cur == NULL)
406 || ((p = strchr(iob->cur, '\n')) == NULL))
408 return(""); /* couldn't find a complete header */
413 ret = strdup(iob->cur);
417 if ((q = strchr(ret, '\r'))) *q = '\0';
419 /* is this a blank linke (i.e. the end of the header) ? */
431 /*********************************************************************
435 * Description : add, delete or modify lines in the HTTP header streams.
436 * On entry, it receives a linked list of headers space
437 * that was allocated dynamically (both the list nodes
438 * and the header contents).
440 * As a side effect it frees the space used by the original
444 * 1 : pats = list of patterns to match against headers
445 * 2 : more_headers = list of functions to add more
446 * headers (client or server)
447 * 3 : csp = Current client state (buffers, headers, etc...)
449 * Returns : Single pointer to a fully formed header.
451 *********************************************************************/
452 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
455 const struct parsers *v;
459 for (v = pats; v->str ; v++)
461 for (p = csp->headers->next; p ; p = p->next)
463 /* Header crunch()ed in previous run? -> ignore */
464 if (p->str == NULL) continue;
466 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
468 if (strncmpic(p->str, v->str, v->len) == 0)
470 hdr = v->parser(v, p->str, csp);
477 /* place any additional headers on the csp->headers list */
478 for (f = more_headers; *f ; f++)
483 /* add the blank line at the end of the header, if necessary */
484 if ( (csp->headers->last == NULL)
485 || (csp->headers->last->str == NULL)
486 || (*csp->headers->last->str != '\0') )
488 enlist(csp->headers, "");
491 hdr = list_to_text(csp->headers);
498 /*********************************************************************
500 * Function : free_http_request
502 * Description : Freez a http_request structure
505 * 1 : http = points to a http_request structure to free
509 *********************************************************************/
510 void free_http_request(struct http_request *http)
515 freez(http->hostport);
518 freez(http->host_ip_addr_str);
523 /*********************************************************************
525 * Function : parse_http_request
527 * Description : Parse out the host and port from the URL. Find the
528 * hostname & path, port (if ':'), and/or password (if '@')
531 * 1 : req = URL (or is it URI?) to break down
532 * 2 : http = pointer to the http structure to hold elements
533 * 3 : csp = Current client state (buffers, headers, etc...)
537 *********************************************************************/
538 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
540 char *buf, *v[10], *url, *p;
543 memset(http, '\0', sizeof(*http));
545 http->cmd = strdup(req);
549 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
553 /* this could be a CONNECT request */
554 if (strcmpic(v[0], "connect") == 0)
557 http->gpc = strdup(v[0]);
558 http->hostport = strdup(v[1]);
559 http->ver = strdup(v[2]);
564 /* This next line is a little ugly, but it simplifies the if statement below. */
565 /* Basically if using webDAV, we want the OR condition to use these too. */
569 * These are the headers as defined in RFC2518 to add webDAV support
572 #define OR_WEBDAV || \
573 (0 == strcmpic(v[0], "propfind")) || \
574 (0 == strcmpic(v[0], "proppatch")) || \
575 (0 == strcmpic(v[0], "move")) || \
576 (0 == strcmpic(v[0], "copy")) || \
577 (0 == strcmpic(v[0], "mkcol")) || \
578 (0 == strcmpic(v[0], "lock")) || \
579 (0 == strcmpic(v[0], "unlock"))
581 #else /* No webDAV support is enabled. Provide an empty OR_WEBDAV macro. */
587 /* or it could be a GET or a POST (possibly webDAV too) */
588 if ((strcmpic(v[0], "get") == 0) ||
589 (strcmpic(v[0], "head") == 0) OR_WEBDAV ||
590 (strcmpic(v[0], "post") == 0))
593 http->gpc = strdup(v[0]);
595 http->ver = strdup(v[2]);
597 if (strncmpic(url, "http://", 7) == 0)
601 else if (strncmpic(url, "https://", 8) == 0)
610 if (url && (p = strchr(url, '/')))
612 http->path = strdup(p);
614 http->hostport = strdup(url);
622 if (http->hostport == NULL)
624 free_http_request(http);
628 buf = strdup(http->hostport);
631 /* check if url contains password */
632 n = ssplit(buf, "@", v, SZ(v), 1, 1);
635 char * newbuf = NULL;
636 newbuf = strdup(v[1]);
641 n = ssplit(buf, ":", v, SZ(v), 1, 1);
645 http->host = strdup(v[0]);
651 http->host = strdup(v[0]);
652 http->port = atoi(v[1]);
657 if (http->host == NULL)
659 free_http_request(http);
662 if (http->path == NULL)
664 http->path = strdup("");
670 /* here begins the family of parser functions that reformat header lines */
673 /*********************************************************************
677 * Description : This is called if a header matches a pattern to "crunch"
680 * 1 : v = Pointer to parsers structure, which basically holds
681 * headers (client or server) that we want to "crunch"
682 * 2 : s = header (from sed) to "crunch"
683 * 3 : csp = Current client state (buffers, headers, etc...)
685 * Returns : Always NULL.
687 *********************************************************************/
688 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
690 log_error(LOG_LEVEL_HEADER, "crunch!");
696 #if defined(PCRS) || defined(KILLPOPUPS)
698 /*********************************************************************
700 * Function : content_type
702 * Description : Is this a text/.* or javascript MIME Type?
706 * 2 : s = header string we are "considering"
707 * 3 : csp = Current client state (buffers, headers, etc...)
709 * Returns : A duplicate string pointer to this header (ie. pass thru)
711 *********************************************************************/
712 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
714 if (strstr (s, " text/") || strstr (s, "application/x-javascript"))
722 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
726 /*********************************************************************
728 * Function : content_length
730 * Description : Adjust Content-Length header if we have
731 * filtered this page through PCRS.
735 * 2 : s = header string we are "considering"
736 * 3 : csp = Current client state (buffers, headers, etc...)
738 * Returns : A duplicate string pointer to this header (ie. pass thru)
740 *********************************************************************/
741 char *content_length(const struct parsers *v, char *s, struct client_state *csp)
743 if (csp->content_length != 0) /* Content has been modified */
745 s = (char *) zalloc(100);
746 sprintf(s, "Content-Length: %d", csp->content_length);
747 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
756 #endif /* def PCRS */
759 /*********************************************************************
761 * Function : client_referrer
763 * Description : Handle the "referer" config setting properly.
768 * 2 : s = header (from sed) to "crunch"
769 * 3 : csp = Current client state (buffers, headers, etc...)
771 * Returns : NULL if crunched, or a malloc'ed string with the original
774 *********************************************************************/
775 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
779 /* Since the referrer can include the prefix even
780 * even if the request itself is non-forced, we must
781 * clean it unconditionally
783 strclean(s, FORCE_PREFIX);
784 #endif /* def FORCE_LOAD */
787 csp->referrer = strdup(s);
788 #endif /* def TRUST_FILES */
791 * Are we sending referer?
793 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
798 newval = csp->action->string[ACTION_STRING_REFERER];
801 * Are we blocking referer?
803 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
805 log_error(LOG_LEVEL_HEADER, "crunch!");
810 * Are we forging referer?
812 if (0 == strcmpic(newval, "forge"))
815 * Forge a referer as http://[hostname:port of REQUEST]/
816 * to fool stupid checks for in-site links
818 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
819 s = strsav(NULL, "Referer: ");
820 s = strsav(s, "http://");
821 s = strsav(s, csp->http->hostport);
827 * Have we got a fixed referer?
829 if (0 == strncmpic(newval, "http://", 7))
832 * We have a specific (fixed) referer we want to send.
835 log_error(LOG_LEVEL_HEADER, "modified");
837 s = strsav( NULL, "Referer: " );
838 s = strsav( s, newval );
842 /* Should never get here! */
843 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
846 * Forge is probably the best default.
848 * Forge a referer as http://[hostname:port of REQUEST]/
849 * to fool stupid checks for in-site links
851 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
852 s = strsav(NULL, "Referer: ");
853 s = strsav(s, "http://");
854 s = strsav(s, csp->http->hostport);
860 /*********************************************************************
862 * Function : client_uagent
864 * Description : Handle the "user-agent" config setting properly.
869 * 2 : s = header (from sed) to "crunch"
870 * 3 : csp = Current client state (buffers, headers, etc...)
872 * Returns : A malloc'ed pointer to the default agent, or
873 * a malloc'ed string pointer to this header (ie. pass thru).
875 *********************************************************************/
876 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
880 #ifdef DETECT_MSIE_IMAGES
881 if (strstr (s, "MSIE "))
883 /* This is Microsoft Internet Explorer.
884 * Enable auto-detect.
886 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
888 #endif /* def DETECT_MSIE_IMAGES */
890 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
895 newval = csp->action->string[ACTION_STRING_USER_AGENT];
901 log_error(LOG_LEVEL_HEADER, "modified");
903 s = strsav( NULL, "User-Agent: " );
904 s = strsav( s, newval );
910 /*********************************************************************
912 * Function : client_ua
914 * Description : Handle "ua-" headers properly. Called from `sed'.
918 * 2 : s = header (from sed) to "crunch"
919 * 3 : csp = Current client state (buffers, headers, etc...)
921 * Returns : NULL if crunched, or a malloc'ed string to original header
923 *********************************************************************/
924 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
926 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
932 log_error(LOG_LEVEL_HEADER, "crunch!");
938 /*********************************************************************
940 * Function : client_from
942 * Description : Handle the "from" config setting properly.
947 * 2 : s = header (from sed) to "crunch"
948 * 3 : csp = Current client state (buffers, headers, etc...)
950 * Returns : NULL if crunched, or a malloc'ed string to
951 * modified/original header.
953 *********************************************************************/
954 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
958 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
963 newval = csp->action->string[ACTION_STRING_FROM];
966 * Are we blocking referer?
968 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
970 log_error(LOG_LEVEL_HEADER, "crunch!");
974 log_error(LOG_LEVEL_HEADER, " modified");
976 s = strsav( NULL, "From: " );
977 s = strsav( s, newval );
983 /*********************************************************************
985 * Function : client_send_cookie
987 * Description : Handle the "cookie" header properly. Called from `sed'.
988 * If cookie is accepted, add it to the cookie_list,
989 * else we crunch it. Mmmmmmmmmmm ... cookie ......
992 * 1 : v = pattern of cookie `sed' found matching
993 * 2 : s = header (from sed) to "crunch"
994 * 3 : csp = Current client state (buffers, headers, etc...)
996 * Returns : Always NULL.
998 *********************************************************************/
999 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
1001 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1003 enlist(csp->cookie_list, s + v->len + 1);
1007 log_error(LOG_LEVEL_HEADER, " crunch!");
1011 * Always return NULL here. The cookie header
1012 * will be sent at the end of the header.
1019 /*********************************************************************
1021 * Function : client_x_forwarded
1023 * Description : Handle the "x-forwarded-for" config setting properly,
1024 * also used in the add_client_headers list. Called from `sed'.
1028 * 2 : s = header (from sed) to "crunch"
1029 * 3 : csp = Current client state (buffers, headers, etc...)
1031 * Returns : Always NULL.
1033 *********************************************************************/
1034 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1036 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1038 /* Save it so we can re-add it later */
1039 csp->x_forwarded = strdup(s);
1043 * Always return NULL, since this information
1044 * will be sent at the end of the header.
1051 #if defined(DETECT_MSIE_IMAGES)
1052 /*********************************************************************
1054 * Function : client_accept
1056 * Description : Detect whether the client wants HTML or an image.
1057 * Clients do not always make this information available
1058 * in a sane way. Always passes the header through
1059 * the proxy unchanged.
1063 * 2 : s = Header string. Null terminated.
1064 * 3 : csp = Current client state (buffers, headers, etc...)
1066 * Returns : Duplicate of argument s.
1068 *********************************************************************/
1069 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1071 #ifdef DETECT_MSIE_IMAGES
1072 if (strstr (s, "image/gif"))
1074 /* Client will accept HTML. If this seems counterintuitive,
1077 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1081 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1083 #endif /* def DETECT_MSIE_IMAGES */
1088 #endif /* defined(DETECT_MSIE_IMAGES) */
1092 /* the following functions add headers directly to the header list */
1095 /*********************************************************************
1097 * Function : client_cookie_adder
1099 * Description : Used in the add_client_headers list. Called from `sed'.
1102 * 1 : csp = Current client state (buffers, headers, etc...)
1106 *********************************************************************/
1107 void client_cookie_adder(struct client_state *csp)
1113 for (lst = csp->cookie_list->next; lst ; lst = lst->next)
1117 tmp = strsav(tmp, "; ");
1119 tmp = strsav(tmp, lst->str);
1122 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->next; lst ; lst = lst->next)
1126 tmp = strsav(tmp, "; ");
1129 if ((e = cookie_encode(lst->str)))
1131 tmp = strsav(tmp, e);
1140 ret = strdup("Cookie: ");
1141 ret = strsav(ret, tmp);
1142 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1143 enlist(csp->headers, ret);
1151 /*********************************************************************
1153 * Function : client_xtra_adder
1155 * Description : Used in the add_client_headers list. Called from `sed'.
1158 * 1 : csp = Current client state (buffers, headers, etc...)
1162 *********************************************************************/
1163 void client_xtra_adder(struct client_state *csp)
1165 struct list *lst = csp->action->multi[ACTION_MULTI_ADD_HEADER];
1167 for (lst = lst->next; lst ; lst = lst->next)
1169 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1170 enlist(csp->headers, lst->str);
1176 /*********************************************************************
1178 * Function : client_x_forwarded_adder
1180 * Description : Used in the add_client_headers list. Called from `sed'.
1183 * 1 : csp = Current client state (buffers, headers, etc...)
1187 *********************************************************************/
1188 void client_x_forwarded_adder(struct client_state *csp)
1192 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1197 if (csp->x_forwarded)
1199 p = strsav(p, csp->x_forwarded);
1200 p = strsav(p, ", ");
1201 p = strsav(p, csp->ip_addr_str);
1205 p = strsav(p, "X-Forwarded-For: ");
1206 p = strsav(p, csp->ip_addr_str);
1209 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1210 enlist(csp->headers, p);
1215 /*********************************************************************
1217 * Function : server_set_cookie
1219 * Description : Handle the server "cookie" header properly.
1220 * Log cookie to the jar file. Then "crunch" it,
1221 * or accept it. Called from `sed'.
1224 * 1 : v = parser pattern that matched this header
1225 * 2 : s = header that matched this pattern
1226 * 3 : csp = Current client state (buffers, headers, etc...)
1228 * Returns : `crumble' or a newly malloc'ed string.
1230 *********************************************************************/
1231 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1234 if (csp->config->jar)
1236 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1238 #endif /* def JAR_FILES */
1240 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1242 return(crumble(v, s, csp));
1251 /*********************************************************************
1253 * Function : client_host
1255 * Description : Clean the FORCE_PREFIX out of the 'host' http
1256 * header, if we use force
1260 * 2 : s = header (from sed) to clean
1261 * 3 : csp = Current client state (buffers, headers, etc...)
1263 * Returns : A malloc'ed pointer to the cleaned host header
1265 *********************************************************************/
1266 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1268 char *cleanhost = strdup(s);
1271 strclean(cleanhost, FORCE_PREFIX);
1275 #endif /* def FORCE_LOAD */
1279 /*********************************************************************
1281 * Function : strclean
1283 * Description : In-Situ-Eliminate all occurances of substring in
1287 * 1 : string = string to clean
1288 * 2 : substring = substring to eliminate
1290 * Returns : Number of eliminations
1292 *********************************************************************/
1293 int strclean(const char *string, const char *substring)
1295 int hits = 0, len = strlen(substring);
1298 while((pos = strstr(string, substring)))
1305 while (*p++ != '\0');
1312 #endif /* def FORCE_LOAD */