1 const char parsers_rcs[] = "$Id: parsers.c,v 1.16 2001/06/29 13:32:42 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.16 2001/06/29 13:32:42 oes
46 * - Adapted free_http_request
47 * - Removed logentry from cancelled commit
49 * Revision 1.15 2001/06/03 19:12:38 oes
50 * deleted const struct interceptors
52 * Revision 1.14 2001/06/01 18:49:17 jongfoster
53 * Replaced "list_share" with "list" - the tiny memory gain was not
54 * worth the extra complexity.
56 * Revision 1.13 2001/05/31 21:30:33 jongfoster
57 * Removed list code - it's now in list.[ch]
58 * Renamed "permission" to "action", and changed many features
59 * to use the actions file rather than the global config.
61 * Revision 1.12 2001/05/31 17:33:13 oes
65 * Revision 1.11 2001/05/29 20:11:19 joergs
66 * '/* inside comment' warning removed.
68 * Revision 1.10 2001/05/29 09:50:24 jongfoster
69 * Unified blocklist/imagelist/permissionslist.
70 * File format is still under discussion, but the internal changes
73 * Also modified interceptor behaviour:
74 * - We now intercept all URLs beginning with one of the following
75 * prefixes (and *only* these prefixes):
77 * * http://ijbswa.sf.net/config/
78 * * http://ijbswa.sourceforge.net/config/
79 * - New interceptors "home page" - go to http://i.j.b/ to see it.
80 * - Internal changes so that intercepted and fast redirect pages
81 * are not replaced with an image.
82 * - Interceptors now have the option to send a binary page direct
83 * to the client. (i.e. ijb-send-banner uses this)
84 * - Implemented show-url-info interceptor. (Which is why I needed
85 * the above interceptors changes - a typical URL is
86 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
87 * The previous mechanism would not have intercepted that, and
88 * if it had been intercepted then it then it would have replaced
91 * Revision 1.9 2001/05/28 17:26:33 jongfoster
92 * Fixing segfault if last header was crunched.
93 * Fixing Windows build (snprintf() is _snprintf() under Win32, but we
94 * can use the cross-platform sprintf() instead.)
96 * Revision 1.8 2001/05/27 22:17:04 oes
98 * - re_process_buffer no longer writes the modified buffer
99 * to the client, which was very ugly. It now returns the
100 * buffer, which it is then written by chat.
102 * - content_length now adjusts the Content-Length: header
103 * for modified documents rather than crunch()ing it.
104 * (Length info in csp->content_length, which is 0 for
105 * unmodified documents)
107 * - For this to work, sed() is called twice when filtering.
109 * Revision 1.7 2001/05/27 13:19:06 oes
110 * Patched Joergs solution for the content-length in.
112 * Revision 1.6 2001/05/26 13:39:32 jongfoster
113 * Only crunches Content-Length header if applying RE filtering.
114 * Without this fix, Microsoft Windows Update wouldn't work.
116 * Revision 1.5 2001/05/26 00:28:36 jongfoster
117 * Automatic reloading of config file.
118 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
119 * Most of the global variables have been moved to a new
120 * struct configuration_spec, accessed through csp->config->globalname
121 * Most of the globals remaining are used by the Win32 GUI.
123 * Revision 1.4 2001/05/22 18:46:04 oes
125 * - Enabled filtering banners by size rather than URL
126 * by adding patterns that replace all standard banner
127 * sizes with the "Junkbuster" gif to the re_filterfile
129 * - Enabled filtering WebBugs by providing a pattern
130 * which kills all 1x1 images
132 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
133 * which is selected by the (nonstandard and therefore
134 * capital) letter 'U' in the option string.
135 * It causes the quantifiers to be ungreedy by default.
136 * Appending a ? turns back to greedy (!).
138 * - Added a new interceptor ijb-send-banner, which
139 * sends back the "Junkbuster" gif. Without imagelist or
140 * MSIE detection support, or if tinygif = 1, or the
141 * URL isn't recognized as an imageurl, a lame HTML
142 * explanation is sent instead.
144 * - Added new feature, which permits blocking remote
145 * script redirects and firing back a local redirect
147 * The feature is conditionally compiled, i.e. it
148 * can be disabled with --disable-fast-redirects,
149 * plus it must be activated by a "fast-redirects"
150 * line in the config file, has its own log level
151 * and of course wants to be displayed by show-proxy-args
152 * Note: Boy, all the #ifdefs in 1001 locations and
153 * all the fumbling with configure.in and acconfig.h
154 * were *way* more work than the feature itself :-(
156 * - Because a generic redirect template was needed for
157 * this, tinygif = 3 now uses the same.
159 * - Moved GIFs, and other static HTTP response templates
164 * - Removed some >400 CRs again (Jon, you really worked
167 * Revision 1.3 2001/05/20 01:21:20 jongfoster
168 * Version 2.9.4 checkin.
169 * - Merged popupfile and cookiefile, and added control over PCRS
170 * filtering, in new "permissionsfile".
171 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
172 * file error you now get a message box (in the Win32 GUI) rather
173 * than the program exiting with no explanation.
174 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
176 * - Removed tabs from "config"
177 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
178 * - Bumped up version number.
180 * Revision 1.2 2001/05/17 23:02:36 oes
181 * - Made referrer option accept 'L' as a substitute for 'ยง'
183 * Revision 1.1.1.1 2001/05/15 13:59:01 oes
184 * Initial import of version 2.9.3 source tree
187 *********************************************************************/
193 #include <sys/types.h>
208 #include "showargs.h"
212 #include "jbsockets.h"
213 #include "miscutil.h"
216 const char parsers_h_rcs[] = PARSERS_H_VERSION;
218 /* Fix a problem with Solaris. There should be no effect on other
220 * Solaris's isspace() is a macro which uses it's argument directly
221 * as an array index. Therefore we need to make sure that high-bit
222 * characters generate +ve values, and ideally we also want to make
223 * the argument match the declared parameter type of "int".
225 * Why did they write a character function that can't take a simple
226 * "char" argument? Doh!
228 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
229 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
232 const struct parsers client_patterns[] = {
233 { "referer:", 8, client_referrer },
234 { "user-agent:", 11, client_uagent },
235 { "ua-", 3, client_ua },
236 { "from:", 5, client_from },
237 { "cookie:", 7, client_send_cookie },
238 { "x-forwarded-for:", 16, client_x_forwarded },
239 { "proxy-connection:", 17, crumble },
241 { "Accept-Encoding: gzip", 21, crumble },
242 #endif /* def DENY_GZIP */
243 #if defined(DETECT_MSIE_IMAGES)
244 { "Accept:", 7, client_accept },
245 #endif /* defined(DETECT_MSIE_IMAGES) */
247 { "Host:", 5, client_host },
248 #endif /* def FORCE_LOAD */
249 /* { "if-modified-since:", 18, crumble }, */
254 const struct parsers server_patterns[] = {
255 { "set-cookie:", 11, server_set_cookie },
256 { "connection:", 11, crumble },
257 #if defined(PCRS) || defined(KILLPOPUPS)
258 { "Content-Type:", 13, content_type },
259 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
261 { "Content-Length:", 15, content_length },
262 #endif /* def PCRS */
267 void (* const add_client_headers[])(struct client_state *) = {
269 client_x_forwarded_adder,
275 void (* const add_server_headers[])(struct client_state *) = {
280 /*********************************************************************
282 * Function : flush_socket
284 * Description : Write any pending "buffered" content.
287 * 1 : fd = file descriptor of the socket to read
288 * 2 : csp = Current client state (buffers, headers, etc...)
290 * Returns : On success, the number of bytes written are returned (zero
291 * indicates nothing was written). On error, -1 is returned,
292 * and errno is set appropriately. If count is zero and the
293 * file descriptor refers to a regular file, 0 will be
294 * returned without causing any other effect. For a special
295 * file, the results are not portable.
297 *********************************************************************/
298 int flush_socket(int fd, struct client_state *csp)
300 struct iob *iob = csp->iob;
301 int n = iob->eod - iob->cur;
308 n = write_socket(fd, iob->cur, n);
309 iob->eod = iob->cur = iob->buf;
315 /*********************************************************************
317 * Function : add_to_iob
319 * Description : Add content to the buffered page.
322 * 1 : csp = Current client state (buffers, headers, etc...)
323 * 2 : buf = holds the content to be added to the page
324 * 3 : n = number of bytes to be added
326 * Returns : Number of bytes in the content buffer.
328 *********************************************************************/
329 int add_to_iob(struct client_state *csp, char *buf, int n)
331 struct iob *iob = csp->iob;
335 have = iob->eod - iob->cur;
344 if ((p = (char *)malloc(need + 1)) == NULL)
346 log_error(LOG_LEVEL_ERROR, "malloc() iob failed: %E");
352 /* there is something in the buffer - save it */
353 memcpy(p, iob->cur, have);
355 /* replace the buffer with the new space */
359 /* point to the end of the data */
364 /* the buffer is empty, free it and reinitialize */
369 /* copy the new data into the iob buffer */
372 /* point to the end of the data */
375 /* null terminate == cheap insurance */
378 /* set the pointers to the new values */
387 /*********************************************************************
389 * Function : get_header
391 * Description : This (odd) routine will parse the csp->iob
394 * 1 : csp = Current client state (buffers, headers, etc...)
396 * Returns : Any one of the following:
398 * 1) a pointer to a dynamically allocated string that contains a header line
399 * 2) NULL indicating that the end of the header was reached
400 * 3) "" indicating that the end of the iob was reached before finding
401 * a complete header line.
403 *********************************************************************/
404 char *get_header(struct client_state *csp)
410 if ((iob->cur == NULL)
411 || ((p = strchr(iob->cur, '\n')) == NULL))
413 return(""); /* couldn't find a complete header */
418 ret = strdup(iob->cur);
422 if ((q = strchr(ret, '\r'))) *q = '\0';
424 /* is this a blank linke (i.e. the end of the header) ? */
436 /*********************************************************************
440 * Description : add, delete or modify lines in the HTTP header streams.
441 * On entry, it receives a linked list of headers space
442 * that was allocated dynamically (both the list nodes
443 * and the header contents).
445 * As a side effect it frees the space used by the original
449 * 1 : pats = list of patterns to match against headers
450 * 2 : more_headers = list of functions to add more
451 * headers (client or server)
452 * 3 : csp = Current client state (buffers, headers, etc...)
454 * Returns : Single pointer to a fully formed header.
456 *********************************************************************/
457 char *sed(const struct parsers pats[], void (* const more_headers[])(struct client_state *), struct client_state *csp)
460 const struct parsers *v;
464 for (v = pats; v->str ; v++)
466 for (p = csp->headers->next; p ; p = p->next)
468 /* Header crunch()ed in previous run? -> ignore */
469 if (p->str == NULL) continue;
471 if (v == pats) log_error(LOG_LEVEL_HEADER, "scan: %s", p->str);
473 if (strncmpic(p->str, v->str, v->len) == 0)
475 hdr = v->parser(v, p->str, csp);
482 /* place any additional headers on the csp->headers list */
483 for (f = more_headers; *f ; f++)
488 /* add the blank line at the end of the header, if necessary */
489 if ( (csp->headers->last == NULL)
490 || (csp->headers->last->str == NULL)
491 || (*csp->headers->last->str != '\0') )
493 enlist(csp->headers, "");
496 hdr = list_to_text(csp->headers);
503 /*********************************************************************
505 * Function : free_http_request
507 * Description : Freez a http_request structure
510 * 1 : http = points to a http_request structure to free
514 *********************************************************************/
515 void free_http_request(struct http_request *http)
520 freez(http->hostport);
523 freez(http->host_ip_addr_str);
528 /*********************************************************************
530 * Function : parse_http_request
532 * Description : Parse out the host and port from the URL. Find the
533 * hostname & path, port (if ':'), and/or password (if '@')
536 * 1 : req = URL (or is it URI?) to break down
537 * 2 : http = pointer to the http structure to hold elements
538 * 3 : csp = Current client state (buffers, headers, etc...)
542 *********************************************************************/
543 void parse_http_request(char *req, struct http_request *http, struct client_state *csp)
545 char *buf, *v[10], *url, *p;
548 memset(http, '\0', sizeof(*http));
550 http->cmd = strdup(req);
554 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
558 /* this could be a CONNECT request */
559 if (strcmpic(v[0], "connect") == 0)
562 http->gpc = strdup(v[0]);
563 http->hostport = strdup(v[1]);
564 http->ver = strdup(v[2]);
569 /* This next line is a little ugly, but it simplifies the if statement below. */
570 /* Basically if using webDAV, we want the OR condition to use these too. */
574 * These are the headers as defined in RFC2518 to add webDAV support
577 #define OR_WEBDAV || \
578 (0 == strcmpic(v[0], "propfind")) || \
579 (0 == strcmpic(v[0], "proppatch")) || \
580 (0 == strcmpic(v[0], "move")) || \
581 (0 == strcmpic(v[0], "copy")) || \
582 (0 == strcmpic(v[0], "mkcol")) || \
583 (0 == strcmpic(v[0], "lock")) || \
584 (0 == strcmpic(v[0], "unlock"))
586 #else /* No webDAV support is enabled. Provide an empty OR_WEBDAV macro. */
592 /* or it could be a GET or a POST (possibly webDAV too) */
593 if ((strcmpic(v[0], "get") == 0) ||
594 (strcmpic(v[0], "head") == 0) OR_WEBDAV ||
595 (strcmpic(v[0], "post") == 0))
598 http->gpc = strdup(v[0]);
600 http->ver = strdup(v[2]);
602 if (strncmpic(url, "http://", 7) == 0)
606 else if (strncmpic(url, "https://", 8) == 0)
615 if (url && (p = strchr(url, '/')))
617 http->path = strdup(p);
619 http->hostport = strdup(url);
627 if (http->hostport == NULL)
629 free_http_request(http);
633 buf = strdup(http->hostport);
636 /* check if url contains password */
637 n = ssplit(buf, "@", v, SZ(v), 1, 1);
640 char * newbuf = NULL;
641 newbuf = strdup(v[1]);
646 n = ssplit(buf, ":", v, SZ(v), 1, 1);
650 http->host = strdup(v[0]);
656 http->host = strdup(v[0]);
657 http->port = atoi(v[1]);
662 if (http->host == NULL)
664 free_http_request(http);
667 if (http->path == NULL)
669 http->path = strdup("");
675 /* here begins the family of parser functions that reformat header lines */
678 /*********************************************************************
682 * Description : This is called if a header matches a pattern to "crunch"
685 * 1 : v = Pointer to parsers structure, which basically holds
686 * headers (client or server) that we want to "crunch"
687 * 2 : s = header (from sed) to "crunch"
688 * 3 : csp = Current client state (buffers, headers, etc...)
690 * Returns : Always NULL.
692 *********************************************************************/
693 char *crumble(const struct parsers *v, char *s, struct client_state *csp)
695 log_error(LOG_LEVEL_HEADER, "crunch!");
701 #if defined(PCRS) || defined(KILLPOPUPS)
703 /*********************************************************************
705 * Function : content_type
707 * Description : Is this a text/.* or javascript MIME Type?
711 * 2 : s = header string we are "considering"
712 * 3 : csp = Current client state (buffers, headers, etc...)
714 * Returns : A duplicate string pointer to this header (ie. pass thru)
716 *********************************************************************/
717 char *content_type(const struct parsers *v, char *s, struct client_state *csp)
719 if (strstr (s, " text/") || strstr (s, "application/x-javascript"))
727 #endif /* defined(PCRS) || defined(KILLPOPUPS) */
731 /*********************************************************************
733 * Function : content_length
735 * Description : Adjust Content-Length header if we have
736 * filtered this page through PCRS.
740 * 2 : s = header string we are "considering"
741 * 3 : csp = Current client state (buffers, headers, etc...)
743 * Returns : A duplicate string pointer to this header (ie. pass thru)
745 *********************************************************************/
746 char *content_length(const struct parsers *v, char *s, struct client_state *csp)
748 if (csp->content_length != 0) /* Content has been modified */
750 s = (char *) zalloc(100);
751 sprintf(s, "Content-Length: %d", csp->content_length);
752 log_error(LOG_LEVEL_HEADER, "Adjust Content-Length to %d", csp->content_length);
761 #endif /* def PCRS */
764 /*********************************************************************
766 * Function : client_referrer
768 * Description : Handle the "referer" config setting properly.
773 * 2 : s = header (from sed) to "crunch"
774 * 3 : csp = Current client state (buffers, headers, etc...)
776 * Returns : NULL if crunched, or a malloc'ed string with the original
779 *********************************************************************/
780 char *client_referrer(const struct parsers *v, char *s, struct client_state *csp)
784 /* Since the referrer can include the prefix even
785 * even if the request itself is non-forced, we must
786 * clean it unconditionally
788 strclean(s, FORCE_PREFIX);
789 #endif /* def FORCE_LOAD */
792 csp->referrer = strdup(s);
793 #endif /* def TRUST_FILES */
796 * Are we sending referer?
798 if ((csp->action->flags & ACTION_HIDE_REFERER) == 0)
803 newval = csp->action->string[ACTION_STRING_REFERER];
806 * Are we blocking referer?
808 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
810 log_error(LOG_LEVEL_HEADER, "crunch!");
815 * Are we forging referer?
817 if (0 == strcmpic(newval, "forge"))
820 * Forge a referer as http://[hostname:port of REQUEST]/
821 * to fool stupid checks for in-site links
823 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
824 s = strsav(NULL, "Referer: ");
825 s = strsav(s, "http://");
826 s = strsav(s, csp->http->hostport);
832 * Have we got a fixed referer?
834 if (0 == strncmpic(newval, "http://", 7))
837 * We have a specific (fixed) referer we want to send.
840 log_error(LOG_LEVEL_HEADER, "modified");
842 s = strsav( NULL, "Referer: " );
843 s = strsav( s, newval );
847 /* Should never get here! */
848 log_error(LOG_LEVEL_ERROR, "Bad parameter: +referer{%s}", newval);
851 * Forge is probably the best default.
853 * Forge a referer as http://[hostname:port of REQUEST]/
854 * to fool stupid checks for in-site links
856 log_error(LOG_LEVEL_HEADER, "crunch+forge!");
857 s = strsav(NULL, "Referer: ");
858 s = strsav(s, "http://");
859 s = strsav(s, csp->http->hostport);
865 /*********************************************************************
867 * Function : client_uagent
869 * Description : Handle the "user-agent" config setting properly.
874 * 2 : s = header (from sed) to "crunch"
875 * 3 : csp = Current client state (buffers, headers, etc...)
877 * Returns : A malloc'ed pointer to the default agent, or
878 * a malloc'ed string pointer to this header (ie. pass thru).
880 *********************************************************************/
881 char *client_uagent(const struct parsers *v, char *s, struct client_state *csp)
885 #ifdef DETECT_MSIE_IMAGES
886 if (strstr (s, "MSIE "))
888 /* This is Microsoft Internet Explorer.
889 * Enable auto-detect.
891 csp->accept_types |= ACCEPT_TYPE_IS_MSIE;
893 #endif /* def DETECT_MSIE_IMAGES */
895 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
900 newval = csp->action->string[ACTION_STRING_USER_AGENT];
906 log_error(LOG_LEVEL_HEADER, "modified");
908 s = strsav( NULL, "User-Agent: " );
909 s = strsav( s, newval );
915 /*********************************************************************
917 * Function : client_ua
919 * Description : Handle "ua-" headers properly. Called from `sed'.
923 * 2 : s = header (from sed) to "crunch"
924 * 3 : csp = Current client state (buffers, headers, etc...)
926 * Returns : NULL if crunched, or a malloc'ed string to original header
928 *********************************************************************/
929 char *client_ua(const struct parsers *v, char *s, struct client_state *csp)
931 if ((csp->action->flags & ACTION_HIDE_USER_AGENT) == 0)
937 log_error(LOG_LEVEL_HEADER, "crunch!");
943 /*********************************************************************
945 * Function : client_from
947 * Description : Handle the "from" config setting properly.
952 * 2 : s = header (from sed) to "crunch"
953 * 3 : csp = Current client state (buffers, headers, etc...)
955 * Returns : NULL if crunched, or a malloc'ed string to
956 * modified/original header.
958 *********************************************************************/
959 char *client_from(const struct parsers *v, char *s, struct client_state *csp)
963 if ((csp->action->flags & ACTION_HIDE_FROM) == 0)
968 newval = csp->action->string[ACTION_STRING_FROM];
971 * Are we blocking referer?
973 if ((newval == NULL) || (0 == strcmpic(newval, "block")) )
975 log_error(LOG_LEVEL_HEADER, "crunch!");
979 log_error(LOG_LEVEL_HEADER, " modified");
981 s = strsav( NULL, "From: " );
982 s = strsav( s, newval );
988 /*********************************************************************
990 * Function : client_send_cookie
992 * Description : Handle the "cookie" header properly. Called from `sed'.
993 * If cookie is accepted, add it to the cookie_list,
994 * else we crunch it. Mmmmmmmmmmm ... cookie ......
997 * 1 : v = pattern of cookie `sed' found matching
998 * 2 : s = header (from sed) to "crunch"
999 * 3 : csp = Current client state (buffers, headers, etc...)
1001 * Returns : Always NULL.
1003 *********************************************************************/
1004 char *client_send_cookie(const struct parsers *v, char *s, struct client_state *csp)
1006 if ((csp->action->flags & ACTION_NO_COOKIE_READ) == 0)
1008 enlist(csp->cookie_list, s + v->len + 1);
1012 log_error(LOG_LEVEL_HEADER, " crunch!");
1016 * Always return NULL here. The cookie header
1017 * will be sent at the end of the header.
1024 /*********************************************************************
1026 * Function : client_x_forwarded
1028 * Description : Handle the "x-forwarded-for" config setting properly,
1029 * also used in the add_client_headers list. Called from `sed'.
1033 * 2 : s = header (from sed) to "crunch"
1034 * 3 : csp = Current client state (buffers, headers, etc...)
1036 * Returns : Always NULL.
1038 *********************************************************************/
1039 char *client_x_forwarded(const struct parsers *v, char *s, struct client_state *csp)
1041 if ((csp->action->flags & ACTION_HIDE_FORWARDED) == 0)
1043 /* Save it so we can re-add it later */
1044 csp->x_forwarded = strdup(s);
1048 * Always return NULL, since this information
1049 * will be sent at the end of the header.
1056 #if defined(DETECT_MSIE_IMAGES)
1057 /*********************************************************************
1059 * Function : client_accept
1061 * Description : Detect whether the client wants HTML or an image.
1062 * Clients do not always make this information available
1063 * in a sane way. Always passes the header through
1064 * the proxy unchanged.
1068 * 2 : s = Header string. Null terminated.
1069 * 3 : csp = Current client state (buffers, headers, etc...)
1071 * Returns : Duplicate of argument s.
1073 *********************************************************************/
1074 char *client_accept(const struct parsers *v, char *s, struct client_state *csp)
1076 #ifdef DETECT_MSIE_IMAGES
1077 if (strstr (s, "image/gif"))
1079 /* Client will accept HTML. If this seems counterintuitive,
1082 csp->accept_types |= ACCEPT_TYPE_MSIE_HTML;
1086 csp->accept_types |= ACCEPT_TYPE_MSIE_IMAGE;
1088 #endif /* def DETECT_MSIE_IMAGES */
1093 #endif /* defined(DETECT_MSIE_IMAGES) */
1097 /* the following functions add headers directly to the header list */
1100 /*********************************************************************
1102 * Function : client_cookie_adder
1104 * Description : Used in the add_client_headers list. Called from `sed'.
1107 * 1 : csp = Current client state (buffers, headers, etc...)
1111 *********************************************************************/
1112 void client_cookie_adder(struct client_state *csp)
1118 for (lst = csp->cookie_list->next; lst ; lst = lst->next)
1122 tmp = strsav(tmp, "; ");
1124 tmp = strsav(tmp, lst->str);
1127 for (lst = csp->action->multi[ACTION_MULTI_WAFER]->next; lst ; lst = lst->next)
1131 tmp = strsav(tmp, "; ");
1134 if ((e = cookie_encode(lst->str)))
1136 tmp = strsav(tmp, e);
1145 ret = strdup("Cookie: ");
1146 ret = strsav(ret, tmp);
1147 log_error(LOG_LEVEL_HEADER, "addh: %s", ret);
1148 enlist(csp->headers, ret);
1156 /*********************************************************************
1158 * Function : client_xtra_adder
1160 * Description : Used in the add_client_headers list. Called from `sed'.
1163 * 1 : csp = Current client state (buffers, headers, etc...)
1167 *********************************************************************/
1168 void client_xtra_adder(struct client_state *csp)
1170 struct list *lst = csp->action->multi[ACTION_MULTI_ADD_HEADER];
1172 for (lst = lst->next; lst ; lst = lst->next)
1174 log_error(LOG_LEVEL_HEADER, "addh: %s", lst->str);
1175 enlist(csp->headers, lst->str);
1181 /*********************************************************************
1183 * Function : client_x_forwarded_adder
1185 * Description : Used in the add_client_headers list. Called from `sed'.
1188 * 1 : csp = Current client state (buffers, headers, etc...)
1192 *********************************************************************/
1193 void client_x_forwarded_adder(struct client_state *csp)
1197 if ((csp->action->flags & ACTION_HIDE_FORWARDED) != 0)
1202 if (csp->x_forwarded)
1204 p = strsav(p, csp->x_forwarded);
1205 p = strsav(p, ", ");
1206 p = strsav(p, csp->ip_addr_str);
1210 p = strsav(p, "X-Forwarded-For: ");
1211 p = strsav(p, csp->ip_addr_str);
1214 log_error(LOG_LEVEL_HEADER, "addh: %s", p);
1215 enlist(csp->headers, p);
1220 /*********************************************************************
1222 * Function : server_set_cookie
1224 * Description : Handle the server "cookie" header properly.
1225 * Log cookie to the jar file. Then "crunch" it,
1226 * or accept it. Called from `sed'.
1229 * 1 : v = parser pattern that matched this header
1230 * 2 : s = header that matched this pattern
1231 * 3 : csp = Current client state (buffers, headers, etc...)
1233 * Returns : `crumble' or a newly malloc'ed string.
1235 *********************************************************************/
1236 char *server_set_cookie(const struct parsers *v, char *s, struct client_state *csp)
1239 if (csp->config->jar)
1241 fprintf(csp->config->jar, "%s\t%s\n", csp->http->host, (s + v->len + 1));
1243 #endif /* def JAR_FILES */
1245 if ((csp->action->flags & ACTION_NO_COOKIE_SET) != 0)
1247 return(crumble(v, s, csp));
1256 /*********************************************************************
1258 * Function : client_host
1260 * Description : Clean the FORCE_PREFIX out of the 'host' http
1261 * header, if we use force
1265 * 2 : s = header (from sed) to clean
1266 * 3 : csp = Current client state (buffers, headers, etc...)
1268 * Returns : A malloc'ed pointer to the cleaned host header
1270 *********************************************************************/
1271 char *client_host(const struct parsers *v, char *s, struct client_state *csp)
1273 char *cleanhost = strdup(s);
1276 strclean(cleanhost, FORCE_PREFIX);
1280 #endif /* def FORCE_LOAD */
1284 /*********************************************************************
1286 * Function : strclean
1288 * Description : In-Situ-Eliminate all occurances of substring in
1292 * 1 : string = string to clean
1293 * 2 : substring = substring to eliminate
1295 * Returns : Number of eliminations
1297 *********************************************************************/
1298 int strclean(const char *string, const char *substring)
1300 int hits = 0, len = strlen(substring);
1303 while((pos = strstr(string, substring)))
1310 while (*p++ != '\0');
1317 #endif /* def FORCE_LOAD */