1 const char parsers_rcs[] = "$Id: parsers.c,v 1.17 2001/06/29 21:45:41 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.17 2001/06/29 21:45:41 oes
45 * Indentation, CRLF->LF, Tab-> Space
47 * Revision 1.16 2001/06/29 13:32:42 oes
49 * - Adapted free_http_request
50 * - Removed logentry from cancelled commit
52 * Revision 1.15 2001/06/03 19:12:38 oes
53 * deleted const struct interceptors
55 * Revision 1.14 2001/06/01 18:49:17 jongfoster
56 * Replaced "list_share" with "list" - the tiny memory gain was not
57 * worth the extra complexity.
59 * Revision 1.13 2001/05/31 21:30:33 jongfoster
60 * Removed list code - it's now in list.[ch]
61 * Renamed "permission" to "action", and changed many features
62 * to use the actions file rather than the global config.
64 * Revision 1.12 2001/05/31 17:33:13 oes
68 * Revision 1.11 2001/05/29 20:11:19 joergs
69 * '/* inside comment' warning removed.
71 * Revision 1.10 2001/05/29 09:50:24 jongfoster
72 * Unified blocklist/imagelist/permissionslist.
73 * File format is still under discussion, but the internal changes
76 * Also modified interceptor behaviour:
77 * - We now intercept all URLs beginning with one of the following
78 * prefixes (and *only* these prefixes):
80 * * http://ijbswa.sf.net/config/
81 * * http://ijbswa.sourceforge.net/config/
82 * - New interceptors "home page" - go to http://i.j.b/ to see it.
83 * - Internal changes so that intercepted and fast redirect pages
84 * are not replaced with an image.
85 * - Interceptors now have the option to send a binary page direct
86 * to the client. (i.e. ijb-send-banner uses this)
87 * - Implemented show-url-info interceptor. (Which is why I needed
88 * the above interceptors changes - a typical URL is
89 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
90 * The previous mechanism would not have intercepted that, and
91 * if it had been intercepted then it then it would have replaced
94 * Revision 1.9 2001/05/28 17:26:33 jongfoster
95 * Fixing segfault if last header was crunched.
96 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
97 * can use the cross-platform sprintf() instead.)
99 * Revision 1.8 2001/05/27 22:17:04 oes
101 * - re_process_buffer no longer writes the modified buffer
102 * to the client, which was very ugly. It now returns the
103 * buffer, which it is then written by chat.
105 * - content_length now adjusts the Content-Length: header
106 * for modified documents rather than crunch()ing it.
107 * (Length info in csp->content_length, which is 0 for
108 * unmodified documents)
110 * - For this to work, sed() is called twice when filtering.
112 * Revision 1.7 2001/05/27 13:19:06 oes
113 * Patched Joergs solution for the content-length in.
115 * Revision 1.6 2001/05/26 13:39:32 jongfoster
116 * Only crunches Content-Length header if applying RE filtering.
117 * Without this fix, Microsoft Windows Update wouldn't work.
119 * Revision 1.5 2001/05/26 00:28:36 jongfoster
120 * Automatic reloading of config file.
121 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
122 * Most of the global variables have been moved to a new
123 * struct configuration_spec, accessed through csp->config->globalname
124 * Most of the globals remaining are used by the Win32 GUI.
126 * Revision 1.4 2001/05/22 18:46:04 oes
128 * - Enabled filtering banners by size rather than URL
129 * by adding patterns that replace all standard banner
130 * sizes with the "Junkbuster" gif to the re_filterfile
132 * - Enabled filtering WebBugs by providing a pattern
133 * which kills all 1x1 images
135 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
136 * which is selected by the (nonstandard and therefore
137 * capital) letter 'U' in the option string.
138 * It causes the quantifiers to be ungreedy by default.
139 * Appending a ? turns back to greedy (!).
141 * - Added a new interceptor ijb-send-banner, which
142 * sends back the "Junkbuster" gif. Without imagelist or
143 * MSIE detection support, or if tinygif = 1, or the
144 * URL isn't recognized as an imageurl, a lame HTML
145 * explanation is sent instead.
147 * - Added new feature, which permits blocking remote
148 * script redirects and firing back a local redirect
150 * The feature is conditionally compiled, i.e. it
151 * can be disabled with --disable-fast-redirects,
152 * plus it must be activated by a "fast-redirects"
153 * line in the config file, has its own log level
154 * and of course wants to be displayed by show-proxy-args
155 * Note: Boy, all the #ifdefs in 1001 locations and
156 * all the fumbling with configure.in and acconfig.h
157 * were *way* more work than the feature itself :-(
159 * - Because a generic redirect template was needed for
160 * this, tinygif = 3 now uses the same.
162 * - Moved GIFs, and other static HTTP response templates
167 * - Removed some >400 CRs again (Jon, you really worked
170 * Revision 1.3 2001/05/20 01:21:20 jongfoster
171 * Version 2.9.4 checkin.
172 * - Merged popupfile and cookiefile, and added control over PCRS
173 * filtering, in new "permissionsfile".
174 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
175 * file error you now get a message box (in the Win32 GUI) rather
176 * than the program exiting with no explanation.
177 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
179 * - Removed tabs from "config"
180 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
181 * - Bumped up version number.
183 * Revision 1.2 2001/05/17 23:02:36 oes
184 * - Made referrer option accept 'L' as a substitute for 'ยง'
186 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
187 * Initial import of version 2.9.3 source tree
190 *********************************************************************/
196 #include <sys/types.h>
211 #include "showargs.h"
215 #include "jbsockets.h"
216 #include "miscutil.h"
219 const char parsers_h_rcs[] = PARSERS_H_VERSION;
221 /* Fix a problem with Solaris. There should be no effect on other
223 * Solaris's isspace() is a macro which uses it's argument directly
224 * as an array index. Therefore we need to make sure that high-bit
225 * characters generate +ve values, and ideally we also want to make
226 * the argument match the declared parameter type of "int".
228 * Why did they write a character function that can't take a simple
229 * "char" argument? Doh!
231 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
232 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
235 const struct parsers client_patterns[] = {
236 { "referer:", 8, client_referrer },
237 { "user-agent:", 11, client_uagent },
238 { "ua-", 3, client_ua },
239 { "from:", 5, client_from },
240 { "cookie:", 7, client_send_cookie },
241 { "x-forwarded-for:", 16, client_x_forwarded },
242 { "proxy-connection:", 17, crumble },
244 { "Accept-Encoding: gzip", 21, crumble },
245 #endif /* def DENY_GZIP */
246 #if defined(DETECT_MSIE_IMAGES)
247 { "Accept:", 7, client_accept },
248 #endif /* defined(DETECT_MSIE_IMAGES) */
250 { "Host:", 5, client_host },
251 #endif /* def FORCE_LOAD */
252 /* { "if-modified-since:", 18, crumble }, */
257 const struct parsers server_patterns[] = {
258 { "set-cookie:", 11, server_set_cookie },
259 { "connection:", 11, crumble },
260 { "Content-Type:", 13, content_type },
261 { "Content-Length:", 15, content_length },
266 void (* const add_client_headers[])(struct client_state *) = {
268 client_x_forwarded_adder,
274 void (* const add_server_headers[])(struct client_state *) = {
279 /*********************************************************************
281 * Function : flush_socket
283 * Description : Write any pending "buffered" content.
286 * 1 : fd = file descriptor of the socket to read
287 * 2 : csp = Current client state (buffers, headers, etc...)
289 * Returns : On success, the number of bytes written are returned (zero
290 * indicates nothing was written). On error, -1 is returned,
291 * and errno is set appropriately. If count is zero and the
292 * file descriptor refers to a regular file, 0 will be
293 * returned without causing any other effect. For a special
294 * file, the results are not portable.
296 *********************************************************************/
297 int flush_socket(int fd, struct client_state *csp)
299 struct iob *iob = csp->iob;
300 int n = iob->eod - iob->cur;
307 n = write_socket(fd, iob->cur, n);
308 iob->eod = iob->cur = iob->buf;
314 /*********************************************************************
316 * Function : add_to_iob
318 * Description : Add content to the buffered page.
321 * 1 : csp = Current client state (buffers, headers, etc...)
322 * 2 : buf = holds the content to be added to the page
323 * 3 : n = number of bytes to be added
325 * Returns : Number of bytes in the content buffer.
327 *********************************************************************/
328 int add_to_iob(struct client_state *csp, char *buf, int n)
330 struct iob *iob = csp->iob;
334 have = iob->eod - iob->cur;
343 if ((p = (char *)malloc(need + 1)) == NULL)
345 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
351 /* there is something in the buffer - save it */
352 memcpy(p, iob->cur, have);
354 /* replace the buffer with the new space */
358 /* point to the end of the data */
363 /* the buffer is empty, free it and reinitialize */
368 /* copy the new data into the iob buffer */
371 /* point to the end of the data */
374 /* null terminate == cheap insurance */
377 /* set the pointers to the new values */
386 /*********************************************************************
388 * Function : get_header
390 * Description : This (odd) routine will parse the csp->iob
393 * 1 : csp = Current client state (buffers, headers, etc...)
395 * Returns : Any one of the following:
397 * 1) a pointer to a dynamically allocated string that contains a header line
398 * 2) NULL indicating that the end of the header was reached
399 * 3) "" indicating that the end of the iob was reached before finding
400 * a complete header line.
402 *********************************************************************/
403 char *get_header(struct client_state *csp)
409 if ((iob->cur == NULL)
410 || ((p = strchr(iob->cur, '\n')) == NULL))
412 return(""); /* couldn't find a complete header */
417 ret = strdup(iob->cur);
421 if ((q = strchr(ret, '\r'))) *q = '\0';
423 /* is this a blank linke (i.e. the end of the header) ? */
435 /*********************************************************************
439 * Description : add, delete or modify lines in the HTTP header streams.
440 * On entry, it receives a linked list of headers space
441 * that was allocated dynamically (both the list nodes
442 * and the header contents).
444 * As a side effect it frees the space used by the original
448 * 1 : pats = list of patterns to match against headers
449 * 2 : more_headers = list of functions to add more
450 * headers (client or server)
451 * 3 : csp = Current client state (buffers, headers, etc...)
453 * Returns : Single pointer to a fully formed header.
455 *********************************************************************/
456 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
459 const struct parsers *v;
463 for (v = pats; v->str ; v++)
465 for (p = csp->headers->next; p ; p = p->next)
467 /* Header crunch()ed in previous run? -> ignore */
468 if (p->str == NULL) continue;
470 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
472 if (strncmpic(p->str, v->str, v->len) == 0)
474 hdr = v->parser(v, p->str, csp);
481 /* place any additional headers on the csp->headers list */
482 for (f = more_headers; *f ; f++)
487 /* add the blank line at the end of the header, if necessary */
488 if ( (csp->headers->last == NULL)
489 || (csp->headers->last->str == NULL)
490 || (*csp->headers->last->str != '\0') )
492 enlist(csp->headers, "");
495 hdr = list_to_text(csp->headers);
502 /*********************************************************************
504 * Function : free_http_request
506 * Description : Freez a http_request structure
509 * 1 : http = points to a http_request structure to free
513 *********************************************************************/
514 void free_http_request(struct http_request *http)
519 freez(http->hostport);
522 freez(http->host_ip_addr_str);
527 /*********************************************************************
529 * Function : parse_http_request
531 * Description : Parse out the host and port from the URL. Find the
532 * hostname & path, port (if ':'), and/or password (if '@')
535 * 1 : req = URL (or is it URI?) to break down
536 * 2 : http = pointer to the http structure to hold elements
537 * 3 : csp = Current client state (buffers, headers, etc...)
541 *********************************************************************/
542 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
544 char *buf, *v[10], *url, *p;
547 memset(http, '\0', sizeof(*http));
549 http->cmd = strdup(req);
552 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
556 /* this could be a CONNECT request */
557 if (strcmpic(v[0], "connect") == 0)
560 http->gpc = strdup(v[0]);
561 http->hostport = strdup(v[1]);
562 http->ver = strdup(v[2]);
567 /* This next line is a little ugly, but it simplifies the if statement below. */
568 /* Basically if using webDAV, we want the OR condition to use these too. */
572 * These are the headers as defined in RFC2518 to add webDAV support
575 #define OR_WEBDAV || \
576 (0 == strcmpic(v[0], "propfind")) || \
577 (0 == strcmpic(v[0], "proppatch")) || \
578 (0 == strcmpic(v[0], "move")) || \
579 (0 == strcmpic(v[0], "copy")) || \
580 (0 == strcmpic(v[0], "mkcol")) || \
581 (0 == strcmpic(v[0], "lock")) || \
582 (0 == strcmpic(v[0], "unlock"))
584 #else /* No webDAV support is enabled. Provide an empty OR_WEBDAV macro. */
590 /* or it could be a GET or a POST (possibly webDAV too) */
591 if ((strcmpic(v[0], "get") == 0) ||
592 (strcmpic(v[0], "head") == 0) OR_WEBDAV ||
593 (strcmpic(v[0], "post") == 0))
596 http->gpc = strdup(v[0]);
598 http->ver = strdup(v[2]);
600 if (strncmpic(url, "http://", 7) == 0)
604 else if (strncmpic(url, "https://", 8) == 0)
615 if (p = strchr(url, '/'))
617 http->path = strdup(p);
619 http->hostport = strdup(url);
622 * Repair broken HTTP requests that don't contain a path
626 /* Repair hostport & path */
627 http->path = strdup("/");
628 http->hostport = strdup(url);
630 /* Even repair cmd in case we're just forwarding. Boy are we nice ;-) */
632 http->cmd = strsav(http->cmd, http->gpc);
633 http->cmd = strsav(http->cmd, " / ");
634 http->cmd = strsav(http->cmd, http->ver);
643 if (http->hostport == NULL)
645 free_http_request(http);
649 buf = strdup(http->hostport);
652 /* check if url contains password */
653 n = ssplit(buf, "@", v, SZ(v), 1, 1);
656 char * newbuf = NULL;
657 newbuf = strdup(v[1]);
662 n = ssplit(buf, ":", v, SZ(v), 1, 1);
666 http->host = strdup(v[0]);
672 http->host = strdup(v[0]);
673 http->port = atoi(v[1]);
678 if (http->host == NULL)
680 free_http_request(http);
683 if (http->path == NULL)
685 http->path = strdup("");
691 /* here begins the family of parser functions that reformat header lines */
694 /*********************************************************************
698 * Description : This is called if a header matches a pattern to "crunch"
701 * 1 : v = Pointer to parsers structure, which basically holds
702 * headers (client or server) that we want to "crunch"
703 * 2 : s = header (from sed) to "crunch"
704 * 3 : csp = Current client state (buffers, headers, etc...)
706 * Returns : Always NULL.
708 *********************************************************************/
709 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
711 log_error(LOG_LEVEL_HEADER, "crunch!");
717 /*********************************************************************
719 * Function : content_type
721 * Description : Is this a text/.* or javascript MIME Type?
725 * 2 : s = header string we are "considering"
726 * 3 : csp = Current client state (buffers, headers, etc...)
728 * Returns : A duplicate string pointer to this header (ie. pass thru)
730 *********************************************************************/
731 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
733 if (strstr(s, " text/") || strstr(s, "application/x-javascript"))
734 csp->content_type = CT_TEXT;
735 else if (strstr(s, " image/gif"))
736 csp->content_type = CT_GIF;
738 csp->content_type = 0;
745 /*********************************************************************
747 * Function : content_length
749 * Description : Adjust Content-Length header if we modified
754 * 2 : s = header string we are "considering"
755 * 3 : csp = Current client state (buffers, headers, etc...)
757 * Returns : A duplicate string pointer to this header (ie. pass thru)
759 *********************************************************************/
760 char *content_length(const struct parsers *v, char *s, struct client_state *csp)
762 if (csp->content_length != 0) /* Content has been modified */
764 s = (char *) zalloc(100);
765 sprintf(s, "Content-Length: %d", csp->content_length);
767 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
778 /*********************************************************************
780 * Function : client_referrer
782 * Description : Handle the "referer" config setting properly.
787 * 2 : s = header (from sed) to "crunch"
788 * 3 : csp = Current client state (buffers, headers, etc...)
790 * Returns : NULL if crunched, or a malloc'ed string with the original
793 *********************************************************************/
794 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
798 /* Since the referrer can include the prefix even
799 * even if the request itself is non-forced, we must
800 * clean it unconditionally
802 strclean(s, FORCE_PREFIX);
803 #endif /* def FORCE_LOAD */
806 csp->referrer = strdup(s);
807 #endif /* def TRUST_FILES */
810 * Are we sending referer?
812 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
817 newval = csp->action->string[ACTION_STRING_REFERER];
820 * Are we blocking referer?
822 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
824 log_error(LOG_LEVEL_HEADER, "crunch!");
829 * Are we forging referer?
831 if (0 == strcmpic(newval, "forge"))
834 * Forge a referer as http://[hostname:port of REQUEST]/
835 * to fool stupid checks for in-site links
837 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
838 s = strsav(NULL, "Referer: ");
839 s = strsav(s, "http://");
840 s = strsav(s, csp->http->hostport);
846 * Have we got a fixed referer?
848 if (0 == strncmpic(newval, "http://", 7))
851 * We have a specific (fixed) referer we want to send.
854 log_error(LOG_LEVEL_HEADER, "modified");
856 s = strsav( NULL, "Referer: " );
857 s = strsav( s, newval );
861 /* Should never get here! */
862 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
865 * Forge is probably the best default.
867 * Forge a referer as http://[hostname:port of REQUEST]/
868 * to fool stupid checks for in-site links
870 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
871 s = strsav(NULL, "Referer: ");
872 s = strsav(s, "http://");
873 s = strsav(s, csp->http->hostport);
879 /*********************************************************************
881 * Function : client_uagent
883 * Description : Handle the "user-agent" config setting properly.
888 * 2 : s = header (from sed) to "crunch"
889 * 3 : csp = Current client state (buffers, headers, etc...)
891 * Returns : A malloc'ed pointer to the default agent, or
892 * a malloc'ed string pointer to this header (ie. pass thru).
894 *********************************************************************/
895 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
899 #ifdef DETECT_MSIE_IMAGES
900 if (strstr (s, "MSIE "))
902 /* This is Microsoft Internet Explorer.
903 * Enable auto-detect.
905 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
907 #endif /* def DETECT_MSIE_IMAGES */
909 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
914 newval = csp->action->string[ACTION_STRING_USER_AGENT];
920 log_error(LOG_LEVEL_HEADER, "modified");
922 s = strsav( NULL, "User-Agent: " );
923 s = strsav( s, newval );
929 /*********************************************************************
931 * Function : client_ua
933 * Description : Handle "ua-" headers properly. Called from `sed'.
937 * 2 : s = header (from sed) to "crunch"
938 * 3 : csp = Current client state (buffers, headers, etc...)
940 * Returns : NULL if crunched, or a malloc'ed string to original header
942 *********************************************************************/
943 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
945 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
951 log_error(LOG_LEVEL_HEADER, "crunch!");
957 /*********************************************************************
959 * Function : client_from
961 * Description : Handle the "from" config setting properly.
966 * 2 : s = header (from sed) to "crunch"
967 * 3 : csp = Current client state (buffers, headers, etc...)
969 * Returns : NULL if crunched, or a malloc'ed string to
970 * modified/original header.
972 *********************************************************************/
973 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
977 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
982 newval = csp->action->string[ACTION_STRING_FROM];
985 * Are we blocking referer?
987 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
989 log_error(LOG_LEVEL_HEADER, "crunch!");
993 log_error(LOG_LEVEL_HEADER, " modified");
995 s = strsav( NULL, "From: " );
996 s = strsav( s, newval );
1002 /*********************************************************************
1004 * Function : client_send_cookie
1006 * Description : Handle the "cookie" header properly. Called from `sed'.
1007 * If cookie is accepted, add it to the cookie_list,
1008 * else we crunch it. Mmmmmmmmmmm ... cookie ......
1011 * 1 : v = pattern of cookie `sed' found matching
1012 * 2 : s = header (from sed) to "crunch"
1013 * 3 : csp = Current client state (buffers, headers, etc...)
1015 * Returns : Always NULL.
1017 *********************************************************************/
1018 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
1020 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1022 enlist(csp->cookie_list, s + v->len + 1);
1026 log_error(LOG_LEVEL_HEADER, " crunch!");
1030 * Always return NULL here. The cookie header
1031 * will be sent at the end of the header.
1038 /*********************************************************************
1040 * Function : client_x_forwarded
1042 * Description : Handle the "x-forwarded-for" config setting properly,
1043 * also used in the add_client_headers list. Called from `sed'.
1047 * 2 : s = header (from sed) to "crunch"
1048 * 3 : csp = Current client state (buffers, headers, etc...)
1050 * Returns : Always NULL.
1052 *********************************************************************/
1053 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1055 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1057 /* Save it so we can re-add it later */
1058 csp->x_forwarded = strdup(s);
1062 * Always return NULL, since this information
1063 * will be sent at the end of the header.
1070 #if defined(DETECT_MSIE_IMAGES)
1071 /*********************************************************************
1073 * Function : client_accept
1075 * Description : Detect whether the client wants HTML or an image.
1076 * Clients do not always make this information available
1077 * in a sane way. Always passes the header through
1078 * the proxy unchanged.
1082 * 2 : s = Header string. Null terminated.
1083 * 3 : csp = Current client state (buffers, headers, etc...)
1085 * Returns : Duplicate of argument s.
1087 *********************************************************************/
1088 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1090 #ifdef DETECT_MSIE_IMAGES
1091 if (strstr (s, "image/gif"))
1093 /* Client will accept HTML. If this seems counterintuitive,
1096 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1100 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1102 #endif /* def DETECT_MSIE_IMAGES */
1107 #endif /* defined(DETECT_MSIE_IMAGES) */
1111 /* the following functions add headers directly to the header list */
1114 /*********************************************************************
1116 * Function : client_cookie_adder
1118 * Description : Used in the add_client_headers list. Called from `sed'.
1121 * 1 : csp = Current client state (buffers, headers, etc...)
1125 *********************************************************************/
1126 void client_cookie_adder(struct client_state *csp)
1132 for (lst = csp->cookie_list->next; lst ; lst = lst->next)
1136 tmp = strsav(tmp, "; ");
1138 tmp = strsav(tmp, lst->str);
1141 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->next; lst ; lst = lst->next)
1145 tmp = strsav(tmp, "; ");
1148 if ((e = cookie_encode(lst->str)))
1150 tmp = strsav(tmp, e);
1159 ret = strdup("Cookie: ");
1160 ret = strsav(ret, tmp);
1161 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1162 enlist(csp->headers, ret);
1170 /*********************************************************************
1172 * Function : client_xtra_adder
1174 * Description : Used in the add_client_headers list. Called from `sed'.
1177 * 1 : csp = Current client state (buffers, headers, etc...)
1181 *********************************************************************/
1182 void client_xtra_adder(struct client_state *csp)
1184 struct list *lst = csp->action->multi[ACTION_MULTI_ADD_HEADER];
1186 for (lst = lst->next; lst ; lst = lst->next)
1188 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1189 enlist(csp->headers, lst->str);
1195 /*********************************************************************
1197 * Function : client_x_forwarded_adder
1199 * Description : Used in the add_client_headers list. Called from `sed'.
1202 * 1 : csp = Current client state (buffers, headers, etc...)
1206 *********************************************************************/
1207 void client_x_forwarded_adder(struct client_state *csp)
1211 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1216 if (csp->x_forwarded)
1218 p = strsav(p, csp->x_forwarded);
1219 p = strsav(p, ", ");
1220 p = strsav(p, csp->ip_addr_str);
1224 p = strsav(p, "X-Forwarded-For: ");
1225 p = strsav(p, csp->ip_addr_str);
1228 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1229 enlist(csp->headers, p);
1234 /*********************************************************************
1236 * Function : server_set_cookie
1238 * Description : Handle the server "cookie" header properly.
1239 * Log cookie to the jar file. Then "crunch" it,
1240 * or accept it. Called from `sed'.
1243 * 1 : v = parser pattern that matched this header
1244 * 2 : s = header that matched this pattern
1245 * 3 : csp = Current client state (buffers, headers, etc...)
1247 * Returns : `crumble' or a newly malloc'ed string.
1249 *********************************************************************/
1250 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1253 if (csp->config->jar)
1255 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1257 #endif /* def JAR_FILES */
1259 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1261 return(crumble(v, s, csp));
1270 /*********************************************************************
1272 * Function : client_host
1274 * Description : Clean the FORCE_PREFIX out of the 'host' http
1275 * header, if we use force
1279 * 2 : s = header (from sed) to clean
1280 * 3 : csp = Current client state (buffers, headers, etc...)
1282 * Returns : A malloc'ed pointer to the cleaned host header
1284 *********************************************************************/
1285 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1287 char *cleanhost = strdup(s);
1290 strclean(cleanhost, FORCE_PREFIX);
1294 #endif /* def FORCE_LOAD */
1298 /*********************************************************************
1300 * Function : strclean
1302 * Description : In-Situ-Eliminate all occurances of substring in
1306 * 1 : string = string to clean
1307 * 2 : substring = substring to eliminate
1309 * Returns : Number of eliminations
1311 *********************************************************************/
1312 int strclean(const char *string, const char *substring)
1314 int hits = 0, len = strlen(substring);
1317 while((pos = strstr(string, substring)))
1324 while (*p++ != '\0');
1331 #endif /* def FORCE_LOAD */