1 const char filters_rcs[] = "$Id: filters.c,v 1.35 2001/10/07 15:41:23 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/filters.c,v $
6 * Purpose : Declares functions to parse/crunch headers and pages.
7 * Functions declared include:
8 * `acl_addr', `add_stats', `block_acl', `block_imageurl',
9 * `block_url', `url_actions', `domaincmp', `dsplit',
10 * `filter_popups', `forward_url', 'redirect_url',
11 * `ij_untrusted_url', `intercept_url', `pcrs_filter_respose',
12 * `show_proxy_args', 'ijb_send_banner', and `trust_url'
14 * Copyright : Written by and Copyright (C) 2001 the SourceForge
15 * IJBSWA team. http://ijbswa.sourceforge.net
17 * Based on the Internet Junkbuster originally written
18 * by and Copyright (C) 1997 Anonymous Coders and
19 * Junkbusters Corporation. http://www.junkbusters.com
21 * This program is free software; you can redistribute it
22 * and/or modify it under the terms of the GNU General
23 * Public License as published by the Free Software
24 * Foundation; either version 2 of the License, or (at
25 * your option) any later version.
27 * This program is distributed in the hope that it will
28 * be useful, but WITHOUT ANY WARRANTY; without even the
29 * implied warranty of MERCHANTABILITY or FITNESS FOR A
30 * PARTICULAR PURPOSE. See the GNU General Public
31 * License for more details.
33 * The GNU General Public License should be included with
34 * this file. If not, you can view it at
35 * http://www.gnu.org/copyleft/gpl.html
36 * or write to the Free Software Foundation, Inc., 59
37 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 * Revision 1.35 2001/10/07 15:41:23 oes
42 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
44 * New function remove_chunked_transfer_coding that strips chunked
45 * transfer coding to plain and is called by pcrs_filter_response
46 * and gif_deanimate_response if neccessary
48 * Improved handling of zero-change re_filter runs
50 * pcrs_filter_response and gif_deanimate_response now remove
51 * chunked transfer codeing before processing the body.
53 * Revision 1.34 2001/09/20 15:49:36 steudten
55 * Fix BUG: Change int size to size_t size in pcrs_filter_response().
56 * See cgi.c fill_template().
58 * Revision 1.33 2001/09/16 17:05:14 jongfoster
59 * Removing unused #include showarg.h
61 * Revision 1.32 2001/09/16 13:21:27 jongfoster
62 * Changes to use new list functions.
64 * Revision 1.31 2001/09/16 11:38:02 jongfoster
65 * Splitting fill_template() into 2 functions:
66 * template_load() loads the file
67 * template_fill() performs the PCRS regexps.
68 * This is because the CGI edit interface has a "table row"
69 * template which is used many times in the page - this
70 * change means it's only loaded from disk once.
72 * Revision 1.30 2001/09/16 11:00:10 jongfoster
73 * New function alloc_http_response, for symmetry with free_http_response
75 * Revision 1.29 2001/09/13 23:32:40 jongfoster
76 * Moving image data to cgi.c rather than cgi.h
77 * Fixing a GPF under Win32 (and any other OS that protects global
78 * constants from being written to).
80 * Revision 1.28 2001/09/10 10:18:51 oes
81 * Silenced compiler warnings
83 * Revision 1.27 2001/08/05 16:06:20 jongfoster
84 * Modifiying "struct map" so that there are now separate header and
85 * "map_entry" structures. This means that functions which modify a
86 * map no longer need to return a pointer to the modified map.
87 * Also, it no longer reverses the order of the entries (which may be
88 * important with some advanced template substitutions).
90 * Revision 1.26 2001/07/30 22:08:36 jongfoster
91 * Tidying up #defines:
92 * - All feature #defines are now of the form FEATURE_xxx
93 * - Permanently turned off WIN_GUI_EDIT
94 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
96 * Revision 1.25 2001/07/26 10:09:46 oes
97 * Made browser detection a little less naive
99 * Revision 1.24 2001/07/25 17:22:51 oes
100 * Added workaround for Netscape bug that prevents display of page when loading a component fails.
102 * Revision 1.23 2001/07/23 13:40:12 oes
103 * Fixed bug that caused document body to be dropped when pcrs joblist was empty.
105 * Revision 1.22 2001/07/18 12:29:34 oes
106 * - Made gif_deanimate_response respect
107 * csp->action->string[ACTION_STRING_DEANIMATE]
108 * - Logging cosmetics
110 * Revision 1.21 2001/07/13 13:59:53 oes
111 * - Introduced gif_deanimate_response which shares the
112 * generic content modification interface of pcrs_filter_response
113 * and acts as a wrapper to deanimate.c:gif_deanimate()
114 * - Renamed re_process_buffer to pcrs_filter_response
115 * - pcrs_filter_response now returns NULL on failiure
116 * - Removed all #ifdef PCRS
118 * Revision 1.20 2001/07/01 17:01:04 oes
119 * Added comments and missing return statement in is_untrusted_url()
121 * Revision 1.19 2001/06/29 21:45:41 oes
122 * Indentation, CRLF->LF, Tab-> Space
124 * Revision 1.18 2001/06/29 13:27:38 oes
125 * - Cleaned up, renamed and reorderd functions
126 * and improved comments
129 * - Ported to CGI platform. Now delivers
130 * http_response or NULL
131 * - Unified HTML and GIF generation (moved image detection
132 * and GIF generation here from jcc.c:chat())
133 * - Fixed HTTP status to:
134 * - 403 (Forbidden) for the "blocked" HTML message
135 * - 200 (OK) for GIF answers
136 * - 302 (Redirect) for redirect to GIF
139 * - Ported to CGI platform. Now delivers
140 * http_response or NULL
141 * - Separated detection of untrusted URL into
142 * (bool)is_untrusted_url
143 * - Added enforcement of untrusted requests
145 * - Moved redirect_url() from cgi.c to here
146 * and ported it to the CGI platform
148 * - Removed logentry from cancelled commit
150 * Revision 1.17 2001/06/09 10:55:28 jongfoster
151 * Changing BUFSIZ ==> BUFFER_SIZE
153 * Revision 1.16 2001/06/07 23:10:26 jongfoster
154 * Allowing unanchored domain patterns to back off and retry
155 * if they partially match. Optimized right-anchored patterns.
156 * Moving ACL and forward files into config file.
157 * Replacing struct gateway with struct forward_spec
159 * Revision 1.15 2001/06/03 19:12:00 oes
160 * extracted-CGI relevant stuff
162 * Revision 1.14 2001/06/01 10:30:55 oes
163 * Added optional left-anchoring to domaincmp
165 * Revision 1.13 2001/05/31 21:21:30 jongfoster
166 * Permissionsfile / actions file changes:
167 * - Changed "permission" to "action" throughout
168 * - changes to file format to allow string parameters
169 * - Moved helper functions to actions.c
171 * Revision 1.12 2001/05/31 17:35:20 oes
173 * - Enhanced domain part globbing with infix and prefix asterisk
174 * matching and optional unanchored operation
176 * Revision 1.11 2001/05/29 11:53:23 oes
177 * "See why" link added to "blocked" page
179 * Revision 1.10 2001/05/29 09:50:24 jongfoster
180 * Unified blocklist/imagelist/permissionslist.
181 * File format is still under discussion, but the internal changes
184 * Also modified interceptor behaviour:
185 * - We now intercept all URLs beginning with one of the following
186 * prefixes (and *only* these prefixes):
188 * * http://ijbswa.sf.net/config/
189 * * http://ijbswa.sourceforge.net/config/
190 * - New interceptors "home page" - go to http://i.j.b/ to see it.
191 * - Internal changes so that intercepted and fast redirect pages
192 * are not replaced with an image.
193 * - Interceptors now have the option to send a binary page direct
194 * to the client. (i.e. ijb-send-banner uses this)
195 * - Implemented show-url-info interceptor. (Which is why I needed
196 * the above interceptors changes - a typical URL is
197 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
198 * The previous mechanism would not have intercepted that, and
199 * if it had been intercepted then it then it would have replaced
202 * Revision 1.9 2001/05/27 22:17:04 oes
204 * - re_process_buffer no longer writes the modified buffer
205 * to the client, which was very ugly. It now returns the
206 * buffer, which it is then written by chat.
208 * - content_length now adjusts the Content-Length: header
209 * for modified documents rather than crunch()ing it.
210 * (Length info in csp->content_length, which is 0 for
211 * unmodified documents)
213 * - For this to work, sed() is called twice when filtering.
215 * Revision 1.8 2001/05/26 17:13:28 jongfoster
216 * Filled in a function comment.
218 * Revision 1.7 2001/05/26 15:26:15 jongfoster
219 * ACL feature now provides more security by immediately dropping
220 * connections from untrusted hosts.
222 * Revision 1.6 2001/05/26 00:28:36 jongfoster
223 * Automatic reloading of config file.
224 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
225 * Most of the global variables have been moved to a new
226 * struct configuration_spec, accessed through csp->config->globalname
227 * Most of the globals remaining are used by the Win32 GUI.
229 * Revision 1.5 2001/05/25 22:34:30 jongfoster
232 * Revision 1.4 2001/05/22 18:46:04 oes
234 * - Enabled filtering banners by size rather than URL
235 * by adding patterns that replace all standard banner
236 * sizes with the "Junkbuster" gif to the re_filterfile
238 * - Enabled filtering WebBugs by providing a pattern
239 * which kills all 1x1 images
241 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
242 * which is selected by the (nonstandard and therefore
243 * capital) letter 'U' in the option string.
244 * It causes the quantifiers to be ungreedy by default.
245 * Appending a ? turns back to greedy (!).
247 * - Added a new interceptor ijb-send-banner, which
248 * sends back the "Junkbuster" gif. Without imagelist or
249 * MSIE detection support, or if tinygif = 1, or the
250 * URL isn't recognized as an imageurl, a lame HTML
251 * explanation is sent instead.
253 * - Added new feature, which permits blocking remote
254 * script redirects and firing back a local redirect
256 * The feature is conditionally compiled, i.e. it
257 * can be disabled with --disable-fast-redirects,
258 * plus it must be activated by a "fast-redirects"
259 * line in the config file, has its own log level
260 * and of course wants to be displayed by show-proxy-args
261 * Note: Boy, all the #ifdefs in 1001 locations and
262 * all the fumbling with configure.in and acconfig.h
263 * were *way* more work than the feature itself :-(
265 * - Because a generic redirect template was needed for
266 * this, tinygif = 3 now uses the same.
268 * - Moved GIFs, and other static HTTP response templates
273 * - Removed some >400 CRs again (Jon, you really worked
276 * Revision 1.3 2001/05/20 16:44:47 jongfoster
277 * Removing last hardcoded JunkBusters.com URLs.
279 * Revision 1.2 2001/05/20 01:21:20 jongfoster
280 * Version 2.9.4 checkin.
281 * - Merged popupfile and cookiefile, and added control over PCRS
282 * filtering, in new "permissionsfile".
283 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
284 * file error you now get a message box (in the Win32 GUI) rather
285 * than the program exiting with no explanation.
286 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
288 * - Removed tabs from "config"
289 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
290 * - Bumped up version number.
292 * Revision 1.1.1.1 2001/05/15 13:58:52 oes
293 * Initial import of version 2.9.3 source tree
296 *********************************************************************/
302 #include <sys/types.h>
310 #include <netinet/in.h>
312 #include <winsock2.h>
321 #include "jbsockets.h"
322 #include "miscutil.h"
326 #include "deanimate.h"
332 const char filters_h_rcs[] = FILTERS_H_VERSION;
334 /* Fix a problem with Solaris. There should be no effect on other
336 * Solaris's isspace() is a macro which uses it's argument directly
337 * as an array index. Therefore we need to make sure that high-bit
338 * characters generate +ve values, and ideally we also want to make
339 * the argument match the declared parameter type of "int".
341 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
345 /*********************************************************************
347 * Function : block_acl
349 * Description : Block this request?
350 * Decide yes or no based on ACL file.
353 * 1 : dst = The proxy or gateway address this is going to.
354 * Or NULL to check all possible targets.
355 * 2 : csp = Current client state (buffers, headers, etc...)
356 * Also includes the client IP address.
358 * Returns : 0 = FALSE (don't block) and 1 = TRUE (do block)
360 *********************************************************************/
361 int block_acl(struct access_control_addr *dst, struct client_state *csp)
363 struct access_control_list *acl = csp->config->acl;
365 /* if not using an access control list, then permit the connection */
371 /* search the list */
374 if ((csp->ip_addr_long & acl->src->mask) == acl->src->addr)
378 /* Just want to check if they have any access */
379 if (acl->action == ACL_PERMIT)
384 else if ( ((dst->addr & acl->dst->mask) == acl->dst->addr)
385 && ((dst->port == acl->dst->port) || (acl->dst->port == 0)))
387 if (acl->action == ACL_PERMIT)
405 /*********************************************************************
407 * Function : acl_addr
409 * Description : Called from `load_aclfile' to parse an ACL address.
412 * 1 : aspec = String specifying ACL address.
413 * 2 : aca = struct access_control_addr to fill in.
415 * Returns : 0 => Ok, everything else is an error.
417 *********************************************************************/
418 int acl_addr(char *aspec, struct access_control_addr *aca)
420 int i, masklength, port;
426 if ((p = strchr(aspec, '/')))
430 if (ijb_isdigit(*p) == 0)
434 masklength = atoi(p);
437 if ((masklength < 0) || (masklength > 32))
442 if ((p = strchr(aspec, ':')))
446 if (ijb_isdigit(*p) == 0)
455 aca->addr = ntohl(resolve_hostname_to_ip(aspec));
459 log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec);
463 /* build the netmask */
465 for (i=1; i <= masklength ; i++)
467 aca->mask |= (1 << (32 - i));
470 /* now mask off the host portion of the ip address
471 * (i.e. save on the network portion of the address).
473 aca->addr = aca->addr & aca->mask;
478 #endif /* def FEATURE_ACL */
481 /*********************************************************************
483 * Function : match_portlist
485 * Description : Check if a given number is covered by a comma
486 * separated list of numbers and ranges (a,b-c,d,..)
489 * 1 : portlist = String with list
490 * 2 : port = port to check
492 * Returns : 0 => no match
495 *********************************************************************/
496 int match_portlist(const char *portlist, int port)
498 char *min, *max, *next, *portlist_copy;
500 min = next = portlist_copy = strdup(portlist);
503 * Zero-terminate first item and remember offset for next
505 if (NULL != (next = strchr(portlist_copy, (int) ',')))
511 * Loop through all items, checking for match
515 if (NULL == (max = strchr(min, (int) '-')))
518 * No dash, check for equality
520 if (port == atoi(min))
529 * This is a range, so check if between min and max,
530 * or, if max was omitted, between min and 65K
533 if(port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
547 * Zero-terminate next item and remember offset for n+1
549 if ((NULL != next) && (NULL != (next = strchr(next, (int) ','))))
561 /*********************************************************************
563 * Function : block_url
565 * Description : Called from `chat'. Check to see if we need to block this.
568 * 1 : csp = Current client state (buffers, headers, etc...)
570 * Returns : NULL => unblocked, else HTTP block response
572 *********************************************************************/
573 struct http_response *block_url(struct client_state *csp)
575 #ifdef FEATURE_IMAGE_BLOCKING
577 #endif /* def FEATURE_IMAGE_BLOCKING */
578 struct http_response *rsp;
581 * If it's not blocked, don't block it ;-)
583 if ((csp->action->flags & ACTION_BLOCK) == 0)
589 * Else, prepare a response
591 if (NULL == (rsp = alloc_http_response()))
597 * If it's an image-url, send back an image or redirect
598 * as specified by the relevant +image action
600 #ifdef FEATURE_IMAGE_BLOCKING
601 if (((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
604 /* determine HOW images should be blocked */
605 p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
607 /* and handle accordingly: */
608 if ((p == NULL) || (0 == strcmpic(p, "logo")))
610 rsp->body = bindup(image_junkbuster_gif_data, image_junkbuster_gif_length);
611 rsp->content_length = image_junkbuster_gif_length;
612 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
615 else if (0 == strcmpic(p, "blank"))
617 rsp->body = bindup(image_blank_gif_data, image_blank_gif_length);
618 rsp->content_length = image_blank_gif_length;
619 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
624 rsp->status = strdup("302 Local Redirect from Junkbuster");
625 enlist_unique_header(rsp->headers, "Location", p);
629 #endif /* def FEATURE_IMAGE_BLOCKING */
632 * Else, generate an HTML "blocked" message:
635 struct map * exports = default_exports(csp, NULL);
636 #ifdef FEATURE_FORCE_LOAD
637 map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
638 #else /* ifndef FEATURE_FORCE_LOAD */
639 map_block_killer(exports, "force-support");
640 #endif /* ndef FEATURE_FORCE_LOAD */
642 map(exports, "hostport", 1, csp->http->hostport, 1);
643 map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
644 map(exports, "path", 1, csp->http->path, 1);
645 map(exports, "path-html", 1, html_encode(csp->http->path), 0);
647 rsp->body = template_load(csp, "blocked");
648 template_fill(&rsp->body, exports);
652 * Workaround for stupid Netscape bug which prevents
653 * pages from being displayed if loading a referenced
654 * JavaScript or style sheet fails. So make it appear
655 * as if it succeeded.
657 if (csp->http->user_agent
658 && !strncmpic(csp->http->user_agent, "mozilla", 7)
659 && !strstr(csp->http->user_agent, "compatible")
660 && !strstr(csp->http->user_agent, "Opera"))
662 rsp->status = strdup("200 Request for blocked URL");
666 rsp->status = strdup("404 Request for blocked URL");
671 return(finish_http_response(rsp));
677 /*********************************************************************
679 * Function : trust_url FIXME: I should be called distrust_url
681 * Description : Calls is_untrusted_url to determine if the URL is trusted
682 * and if not, returns a HTTP 304 response with a reject message.
685 * 1 : csp = Current client state (buffers, headers, etc...)
687 * Returns : NULL => trusted, else http_response.
689 *********************************************************************/
690 struct http_response *trust_url(struct client_state *csp)
692 struct http_response *rsp;
693 struct map * exports;
694 char buf[BUFFER_SIZE], *p = NULL;
695 struct url_spec **tl, *t;
698 * Don't bother to work on trusted URLs
700 if (!is_untrusted_url(csp))
706 * Else, prepare a response:
708 if (NULL == (rsp = alloc_http_response()))
713 exports = default_exports(csp, NULL);
716 * Export the host, port, and referrer information
718 map(exports, "hostport", 1, csp->http->hostport, 1);
719 map(exports, "path", 1, csp->http->path, 1);
720 map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
721 map(exports, "path-html", 1, html_encode(csp->http->path), 0);
723 if (csp->referrer && strlen(csp->referrer) > 9)
725 map(exports, "referrer", 1, csp->referrer + 9, 1);
726 map(exports, "referrer-html", 1, html_encode(csp->referrer + 9), 0);
730 map(exports, "referrer", 1, "unknown", 1);
731 map(exports, "referrer-html", 1, "unknown", 1);
735 * Export the trust list
737 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
739 sprintf(buf, "<li>%s</li>\n", t->spec);
742 map(exports, "trusted-referrers", 1, p, 0);
746 * Export the trust info, if available
748 if (csp->config->trust_info->first)
750 struct list_entry *l;
752 for (l = csp->config->trust_info->first; l ; l = l->next)
754 sprintf(buf, "<li> <a href=%s>%s</a><br>\n",l->str, l->str);
757 map(exports, "trust-info", 1, p, 0);
761 map_block_killer(exports, "have-trust-info");
765 * Export the force prefix or the force conditional block killer
767 #ifdef FEATURE_FORCE_LOAD
768 map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
769 #else /* ifndef FEATURE_FORCE_LOAD */
770 map_block_killer(exports, "force-support");
771 #endif /* ndef FEATURE_FORCE_LOAD */
776 rsp->body = template_load(csp, "untrusted");
777 template_fill(&rsp->body, exports);
780 return(finish_http_response(rsp));
783 #endif /* def FEATURE_TRUST */
786 #ifdef FEATURE_FAST_REDIRECTS
787 /*********************************************************************
789 * Function : redirect_url
791 * Description : Checks for redirection URLs and returns a HTTP redirect
792 * to the destination URL, if necessary
795 * 1 : csp = Current client state (buffers, headers, etc...)
797 * Returns : NULL if URL was clean, HTTP redirect otherwise.
799 *********************************************************************/
800 struct http_response *redirect_url(struct client_state *csp)
803 struct http_response *rsp;
805 p = q = csp->http->path;
806 log_error(LOG_LEVEL_REDIRECTS, "checking path for redirects: %s", p);
809 * find the last URL encoded in the request
811 while ((p = strstr(p, "http://")))
817 * if there was any, generate and return a HTTP redirect
819 if (q != csp->http->path)
821 log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
823 if (NULL == (rsp = alloc_http_response()))
828 rsp->status = strdup("302 Local Redirect from Junkbuster");
829 enlist_unique_header(rsp->headers, "Location", q);
831 return(finish_http_response(rsp));
839 #endif /* def FEATURE_FAST_REDIRECTS */
842 #ifdef FEATURE_IMAGE_BLOCKING
843 /*********************************************************************
845 * Function : is_imageurl
847 * Description : Given a URL, decide whether it is an image or not,
848 * using either the info from a previous +image action
849 * or, #ifdef FEATURE_IMAGE_DETECT_MSIE, the info from
850 * the browser's accept header.
853 * 1 : csp = Current client state (buffers, headers, etc...)
855 * Returns : True (nonzero) if URL is an image, false (0)
858 *********************************************************************/
859 int is_imageurl(struct client_state *csp)
861 #ifdef FEATURE_IMAGE_DETECT_MSIE
862 if ((csp->accept_types
863 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
864 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE))
868 else if ((csp->accept_types
869 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
870 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_HTML))
874 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
876 return ((csp->action->flags & ACTION_IMAGE) != 0);
879 #endif /* def FEATURE_IMAGE_BLOCKING */
882 #ifdef FEATURE_COOKIE_JAR
883 /*********************************************************************
885 * Function : is_untrusted_url
887 * Description : Should we "distrust" this URL (and block it)?
889 * Yes if it matches a line in the trustfile, or if the
890 * referrer matches a line starting with "+" in the
895 * 1 : csp = Current client state (buffers, headers, etc...)
897 * Returns : 0 => trusted, 1 => untrusted
899 *********************************************************************/
900 int is_untrusted_url(struct client_state *csp)
902 struct file_list *fl;
903 struct block_spec *b;
904 struct url_spec url[1], **tl, *t;
905 struct http_request rhttp[1];
909 * If we don't have a trustlist, we trust everybody
911 if (((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL))
918 * Do we trust the request URL itself?
920 *url = dsplit(csp->http->host);
922 /* if splitting the domain fails, punt */
923 if (url->dbuf == NULL) return(0);
925 memset(rhttp, '\0', sizeof(*rhttp));
927 for (b = b->next; b ; b = b->next)
929 if ((b->url->port == 0) || (b->url->port == csp->http->port))
931 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
933 if ((b->url->path == NULL) ||
935 (regexec(b->url->preg, csp->http->path, 0, NULL, 0) == 0)
937 (strncmp(b->url->path, csp->http->path, b->url->pathlen) == 0)
944 if (b->reject == 0) return(0);
955 if ((csp->referrer == NULL)|| (strlen(csp->referrer) <= 9))
957 /* no referrer was supplied */
961 /* forge a URL from the referrer so we can use
962 * convert_url() to parse it into its components.
966 p = strsav(p, "GET ");
967 p = strsav(p, csp->referrer + 9); /* skip over "Referer: " */
968 p = strsav(p, " HTTP/1.0");
970 parse_http_request(p, rhttp, csp);
973 if (rhttp->cmd == NULL)
980 * If not, do we maybe trust its referrer?
982 *url = dsplit(rhttp->host);
984 /* if splitting the domain fails, punt */
985 if (url->dbuf == NULL) return(1);
987 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
989 if ((t->port == 0) || (t->port == rhttp->port))
991 if ((t->domain[0] == '\0') || domaincmp(t, url) == 0)
993 if ((t->path == NULL) ||
995 (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0)
997 (strncmp(t->path, rhttp->path, t->pathlen) == 0)
1001 /* if the URL's referrer is from a trusted referrer, then
1002 * add the target spec to the trustfile as an unblocked
1003 * domain and return NULL (which means it's OK).
1011 if ((fp = fopen(csp->config->trustfile, "a")))
1016 h = strsav(h, csp->http->hostport);
1018 p = csp->http->path;
1022 /* since this path points into a user's home space
1023 * be sure to include this spec in the trustfile.
1025 if ((p = strchr(p, '/')))
1028 h = strsav(h, csp->http->path); /* FIXME: p?! */
1033 fprintf(fp, "%s\n", h);
1044 #endif /* def FEATURE_COOKIE_JAR */
1047 /*********************************************************************
1049 * Function : pcrs_filter_response
1051 * Description : Apply all the pcrs jobs from the joblist (re_filterfile)
1052 * to the text buffer that's been accumulated in
1053 * csp->iob->buf and set csp->content_length to the modified
1054 * size and raise the CSP_FLAG_MODIFIED flag if appropriate.
1057 * 1 : csp = Current client state (buffers, headers, etc...)
1059 * Returns : a pointer to the (newly allocated) modified buffer.
1060 * or NULL in case something went wrong
1062 *********************************************************************/
1063 char *pcrs_filter_response(struct client_state *csp)
1068 char *old = csp->iob->cur, *new = NULL;
1071 struct file_list *fl;
1072 struct re_filterfile_spec *b;
1075 if (csp->iob->cur >= csp->iob->eod)
1079 size = csp->iob->eod - csp->iob->cur;
1082 * If the body has a "chunked" transfer-encoding,
1083 * get rid of it first, adjusting size and iob->eod
1085 if (csp->flags & CSP_FLAG_CHUNKED)
1087 log_error(LOG_LEVEL_RE_FILTER, "Need to de-chunk first");
1088 if (0 == (size = remove_chunked_transfer_coding(csp->iob->cur, size)))
1092 csp->iob->eod = csp->iob->cur + size;
1093 csp->flags |= CSP_FLAG_MODIFIED;
1096 if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
1098 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
1102 if ( NULL == b->joblist )
1104 log_error(LOG_LEVEL_RE_FILTER, "Empty joblist. Nothing to do.");
1108 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
1109 csp->http->hostport, csp->http->path, size);
1111 /* Apply all jobs from the joblist */
1112 for (job = b->joblist; NULL != job; job = job->next)
1114 hits += pcrs_execute(job, old, size, &new, &size);
1115 if (old != csp->iob->cur) free(old);
1119 log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
1122 * If there were no hits, destroy our copy and let
1123 * chat() use the original in csp->iob
1131 csp->flags |= CSP_FLAG_MODIFIED;
1132 csp->content_length = size;
1140 /*********************************************************************
1142 * Function : gif_deanimate_response
1144 * Description : Deanimate the GIF image that has been accumulated in
1145 * csp->iob->buf, set csp->content_length to the modified
1146 * size and raise the CSP_FLAG_MODIFIED flag.
1149 * 1 : csp = Current client state (buffers, headers, etc...)
1151 * Returns : a pointer to the (newly allocated) modified buffer.
1152 * or NULL in case something went wrong.
1154 *********************************************************************/
1155 char *gif_deanimate_response(struct client_state *csp)
1157 struct binbuffer *in, *out;
1159 int size = csp->iob->eod - csp->iob->cur;
1162 * If the body has a "chunked" transfer-encoding,
1163 * get rid of it first, adjusting size and iob->eod
1165 if (csp->flags & CSP_FLAG_CHUNKED)
1167 log_error(LOG_LEVEL_DEANIMATE, "Need to de-chunk first");
1168 if (0 == (size = remove_chunked_transfer_coding(csp->iob->cur, size)))
1172 csp->iob->eod = csp->iob->cur + size;
1173 csp->flags |= CSP_FLAG_MODIFIED;
1176 if ( (NULL == (in = (struct binbuffer *)zalloc(sizeof *in )))
1177 || (NULL == (out = (struct binbuffer *)zalloc(sizeof *out))) )
1179 log_error(LOG_LEVEL_DEANIMATE, "failed! (no mem)");
1183 in->buffer = csp->iob->cur;
1186 if (gif_deanimate(in, out, strncmp("last", csp->action->string[ACTION_STRING_DEANIMATE], 4)))
1188 log_error(LOG_LEVEL_DEANIMATE, "failed! (gif parsing)");
1195 log_error(LOG_LEVEL_DEANIMATE, "Success! GIF shrunk from %d bytes to %d.", size, out->offset);
1196 csp->content_length = out->offset;
1197 csp->flags |= CSP_FLAG_MODIFIED;
1207 /*********************************************************************
1209 * Function : remove_chunked_transfer_coding
1211 * Description : In-situ remove the "chunked" transfer coding as defined
1212 * in rfc2616 from a buffer.
1215 * 1 : buffer = Pointer to the text buffer
1216 * 2 : size = Number of bytes to be processed
1218 * Returns : The new size, i.e. the number of bytes from buffer which
1219 * are occupied by the stripped body, or 0 in case something
1222 *********************************************************************/
1223 int remove_chunked_transfer_coding(char *buffer, const size_t size)
1226 unsigned int chunksize = 0;
1227 char *from_p, *to_p;
1230 from_p = to_p = buffer;
1232 if (sscanf(buffer, "%x", &chunksize) != 1)
1234 log_error(LOG_LEVEL_ERROR, "Invalid first chunksize while stripping \"chunked\" transfer coding");
1238 while (chunksize > 0)
1240 if (NULL == (from_p = strstr(from_p, "\r\n")))
1242 log_error(LOG_LEVEL_ERROR, "Parse error while stripping \"chunked\" transfer coding");
1245 newsize += chunksize;
1248 memmove(to_p, from_p, (size_t) chunksize);
1249 to_p = buffer + newsize;
1250 from_p += chunksize + 2;
1252 if (sscanf(from_p, "%x", &chunksize) != 1)
1254 log_error(LOG_LEVEL_ERROR, "Parse error while stripping \"chunked\" transfer coding");
1259 /* FIXME: Should this get its own loglevel? */
1260 log_error(LOG_LEVEL_RE_FILTER, "De-chunking successful. Shrunk from %d to %d\n", size, newsize);
1266 /*********************************************************************
1268 * Function : url_actions
1270 * Description : Gets the actions for this URL.
1273 * 1 : http = http_request request for blocked URLs
1274 * 2 : csp = Current client state (buffers, headers, etc...)
1278 *********************************************************************/
1279 void url_actions(struct http_request *http,
1280 struct client_state *csp)
1282 struct file_list *fl;
1283 struct url_actions *b;
1285 init_current_action(csp->action);
1287 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
1292 apply_url_actions(csp->action, http, b);
1297 /*********************************************************************
1299 * Function : apply_url_actions
1301 * Description : Applies a list of URL actions.
1304 * 1 : action = Destination.
1305 * 2 : http = Current URL
1306 * 3 : b = list of URL actions to apply
1310 *********************************************************************/
1311 void apply_url_actions(struct current_action_spec *action,
1312 struct http_request *http,
1313 struct url_actions *b)
1315 struct url_spec url[1];
1319 /* Should never happen */
1323 *url = dsplit(http->host);
1325 /* if splitting the domain fails, punt */
1326 if (url->dbuf == NULL)
1331 for (b = b->next; NULL != b; b = b->next)
1333 if ((b->url->port == 0) || (b->url->port == http->port))
1335 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
1337 if ((b->url->path == NULL) ||
1339 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
1341 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
1345 merge_current_action(action, b->action);
1356 /*********************************************************************
1358 * Function : forward_url
1360 * Description : Should we forward this to another proxy?
1363 * 1 : http = http_request request for current URL
1364 * 2 : csp = Current client state (buffers, headers, etc...)
1366 * Returns : Pointer to forwarding information.
1368 *********************************************************************/
1369 const struct forward_spec * forward_url(struct http_request *http,
1370 struct client_state *csp)
1372 static const struct forward_spec fwd_default[1] = { FORWARD_SPEC_INITIALIZER };
1373 struct forward_spec *fwd = csp->config->forward;
1374 struct url_spec url[1];
1378 return(fwd_default);
1381 *url = dsplit(http->host);
1383 /* if splitting the domain fails, punt */
1384 if (url->dbuf == NULL)
1386 return(fwd_default);
1391 if ((fwd->url->port == 0) || (fwd->url->port == http->port))
1393 if ((fwd->url->domain[0] == '\0') || (domaincmp(fwd->url, url) == 0))
1395 if ((fwd->url->path == NULL) ||
1397 (regexec(fwd->url->preg, http->path, 0, NULL, 0) == 0)
1399 (strncmp(fwd->url->path, http->path, fwd->url->pathlen) == 0)
1414 return(fwd_default);
1419 /*********************************************************************
1423 * Description : Takes a domain and returns a pointer to a url_spec
1424 * structure populated with dbuf, dcnt and dvec. The
1425 * other fields in the structure that is returned are zero.
1428 * 1 : domain = a URL address
1430 * Returns : url_spec structure populated with dbuf, dcnt and dvec.
1432 *********************************************************************/
1433 struct url_spec dsplit(char *domain)
1435 struct url_spec ret[1];
1436 char *v[BUFFER_SIZE];
1440 memset(ret, '\0', sizeof(*ret));
1442 if (domain[strlen(domain) - 1] == '.')
1444 ret->unanchored |= ANCHOR_RIGHT;
1447 if (domain[0] == '.')
1449 ret->unanchored |= ANCHOR_LEFT;
1452 ret->dbuf = strdup(domain);
1454 /* map to lower case */
1455 for (p = ret->dbuf; *p ; p++) *p = tolower(*p);
1457 /* split the domain name into components */
1458 ret->dcnt = ssplit(ret->dbuf, ".", v, SZ(v), 1, 1);
1462 memset(ret, '\0', sizeof(ret));
1466 /* save a copy of the pointers in dvec */
1467 size = ret->dcnt * sizeof(*ret->dvec);
1469 if ((ret->dvec = (char **)malloc(size)))
1471 memcpy(ret->dvec, v, size);
1479 /*********************************************************************
1481 * Function : simple_domaincmp
1483 * Description : Domain-wise Compare fqdn's. The comparison is
1484 * both left- and right-anchored. The individual
1485 * domain names are compared with simplematch().
1486 * This is only used by domaincmp.
1489 * 1 : pv = array of patterns to compare
1490 * 2 : fv = array of domain components to compare
1491 * 3 : len = length of the arrays (both arrays are the
1492 * same length - if they weren't, it couldn't
1493 * possibly be a match).
1495 * Returns : 0 => domains are equivalent, else no match.
1497 *********************************************************************/
1498 static int simple_domaincmp(char **pv, char **fv, int len)
1502 for (n = 0; n < len; n++)
1504 if (simplematch(pv[n], fv[n]))
1515 /*********************************************************************
1517 * Function : domaincmp
1519 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
1520 * pattern->unachored, the comparison is un-, left-,
1521 * right-anchored, or both.
1522 * The individual domain names are compared with
1526 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1527 * 2 : fqdn = domain name against which the patterns are compared.
1529 * Returns : 0 => domains are equivalent, else no match.
1531 *********************************************************************/
1532 int domaincmp(struct url_spec *pattern, struct url_spec *fqdn)
1534 char **pv, **fv; /* vectors */
1536 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
1538 plen = pattern->dcnt;
1543 /* fqdn is too short to match this pattern */
1550 if (unanchored == ANCHOR_LEFT)
1555 * Convert this into a fully anchored pattern with
1556 * the fqdn and pattern the same length
1558 fv += (flen - plen); /* flen - plen >= 0 due to check above */
1559 return simple_domaincmp(pv, fv, plen);
1561 else if (unanchored == 0)
1563 /* Fully anchored, check length */
1568 return simple_domaincmp(pv, fv, plen);
1570 else if (unanchored == ANCHOR_RIGHT)
1572 /* Left anchored, ignore all extra in fqdn */
1573 return simple_domaincmp(pv, fv, plen);
1579 int maxn = flen - plen;
1580 for (n = 0; n <= maxn; n++)
1582 if (!simple_domaincmp(pv, fv, plen))
1587 * Doesn't match from start of fqdn
1588 * Try skipping first part of fqdn