1 const char filters_rcs[] = "$Id: filters.c,v 1.30 2001/09/16 11:00:10 jongfoster 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.30 2001/09/16 11:00:10 jongfoster
42 * New function alloc_http_response, for symmetry with free_http_response
44 * Revision 1.29 2001/09/13 23:32:40 jongfoster
45 * Moving image data to cgi.c rather than cgi.h
46 * Fixing a GPF under Win32 (and any other OS that protects global
47 * constants from being written to).
49 * Revision 1.28 2001/09/10 10:18:51 oes
50 * Silenced compiler warnings
52 * Revision 1.27 2001/08/05 16:06:20 jongfoster
53 * Modifiying "struct map" so that there are now separate header and
54 * "map_entry" structures. This means that functions which modify a
55 * map no longer need to return a pointer to the modified map.
56 * Also, it no longer reverses the order of the entries (which may be
57 * important with some advanced template substitutions).
59 * Revision 1.26 2001/07/30 22:08:36 jongfoster
60 * Tidying up #defines:
61 * - All feature #defines are now of the form FEATURE_xxx
62 * - Permanently turned off WIN_GUI_EDIT
63 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
65 * Revision 1.25 2001/07/26 10:09:46 oes
66 * Made browser detection a little less naive
68 * Revision 1.24 2001/07/25 17:22:51 oes
69 * Added workaround for Netscape bug that prevents display of page when loading a component fails.
71 * Revision 1.23 2001/07/23 13:40:12 oes
72 * Fixed bug that caused document body to be dropped when pcrs joblist was empty.
74 * Revision 1.22 2001/07/18 12:29:34 oes
75 * - Made gif_deanimate_response respect
76 * csp->action->string[ACTION_STRING_DEANIMATE]
79 * Revision 1.21 2001/07/13 13:59:53 oes
80 * - Introduced gif_deanimate_response which shares the
81 * generic content modification interface of pcrs_filter_response
82 * and acts as a wrapper to deanimate.c:gif_deanimate()
83 * - Renamed re_process_buffer to pcrs_filter_response
84 * - pcrs_filter_response now returns NULL on failiure
85 * - Removed all #ifdef PCRS
87 * Revision 1.20 2001/07/01 17:01:04 oes
88 * Added comments and missing return statement in is_untrusted_url()
90 * Revision 1.19 2001/06/29 21:45:41 oes
91 * Indentation, CRLF->LF, Tab-> Space
93 * Revision 1.18 2001/06/29 13:27:38 oes
94 * - Cleaned up, renamed and reorderd functions
95 * and improved comments
98 * - Ported to CGI platform. Now delivers
99 * http_response or NULL
100 * - Unified HTML and GIF generation (moved image detection
101 * and GIF generation here from jcc.c:chat())
102 * - Fixed HTTP status to:
103 * - 403 (Forbidden) for the "blocked" HTML message
104 * - 200 (OK) for GIF answers
105 * - 302 (Redirect) for redirect to GIF
108 * - Ported to CGI platform. Now delivers
109 * http_response or NULL
110 * - Separated detection of untrusted URL into
111 * (bool)is_untrusted_url
112 * - Added enforcement of untrusted requests
114 * - Moved redirect_url() from cgi.c to here
115 * and ported it to the CGI platform
117 * - Removed logentry from cancelled commit
119 * Revision 1.17 2001/06/09 10:55:28 jongfoster
120 * Changing BUFSIZ ==> BUFFER_SIZE
122 * Revision 1.16 2001/06/07 23:10:26 jongfoster
123 * Allowing unanchored domain patterns to back off and retry
124 * if they partially match. Optimized right-anchored patterns.
125 * Moving ACL and forward files into config file.
126 * Replacing struct gateway with struct forward_spec
128 * Revision 1.15 2001/06/03 19:12:00 oes
129 * extracted-CGI relevant stuff
131 * Revision 1.14 2001/06/01 10:30:55 oes
132 * Added optional left-anchoring to domaincmp
134 * Revision 1.13 2001/05/31 21:21:30 jongfoster
135 * Permissionsfile / actions file changes:
136 * - Changed "permission" to "action" throughout
137 * - changes to file format to allow string parameters
138 * - Moved helper functions to actions.c
140 * Revision 1.12 2001/05/31 17:35:20 oes
142 * - Enhanced domain part globbing with infix and prefix asterisk
143 * matching and optional unanchored operation
145 * Revision 1.11 2001/05/29 11:53:23 oes
146 * "See why" link added to "blocked" page
148 * Revision 1.10 2001/05/29 09:50:24 jongfoster
149 * Unified blocklist/imagelist/permissionslist.
150 * File format is still under discussion, but the internal changes
153 * Also modified interceptor behaviour:
154 * - We now intercept all URLs beginning with one of the following
155 * prefixes (and *only* these prefixes):
157 * * http://ijbswa.sf.net/config/
158 * * http://ijbswa.sourceforge.net/config/
159 * - New interceptors "home page" - go to http://i.j.b/ to see it.
160 * - Internal changes so that intercepted and fast redirect pages
161 * are not replaced with an image.
162 * - Interceptors now have the option to send a binary page direct
163 * to the client. (i.e. ijb-send-banner uses this)
164 * - Implemented show-url-info interceptor. (Which is why I needed
165 * the above interceptors changes - a typical URL is
166 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
167 * The previous mechanism would not have intercepted that, and
168 * if it had been intercepted then it then it would have replaced
171 * Revision 1.9 2001/05/27 22:17:04 oes
173 * - re_process_buffer no longer writes the modified buffer
174 * to the client, which was very ugly. It now returns the
175 * buffer, which it is then written by chat.
177 * - content_length now adjusts the Content-Length: header
178 * for modified documents rather than crunch()ing it.
179 * (Length info in csp->content_length, which is 0 for
180 * unmodified documents)
182 * - For this to work, sed() is called twice when filtering.
184 * Revision 1.8 2001/05/26 17:13:28 jongfoster
185 * Filled in a function comment.
187 * Revision 1.7 2001/05/26 15:26:15 jongfoster
188 * ACL feature now provides more security by immediately dropping
189 * connections from untrusted hosts.
191 * Revision 1.6 2001/05/26 00:28:36 jongfoster
192 * Automatic reloading of config file.
193 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
194 * Most of the global variables have been moved to a new
195 * struct configuration_spec, accessed through csp->config->globalname
196 * Most of the globals remaining are used by the Win32 GUI.
198 * Revision 1.5 2001/05/25 22:34:30 jongfoster
201 * Revision 1.4 2001/05/22 18:46:04 oes
203 * - Enabled filtering banners by size rather than URL
204 * by adding patterns that replace all standard banner
205 * sizes with the "Junkbuster" gif to the re_filterfile
207 * - Enabled filtering WebBugs by providing a pattern
208 * which kills all 1x1 images
210 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
211 * which is selected by the (nonstandard and therefore
212 * capital) letter 'U' in the option string.
213 * It causes the quantifiers to be ungreedy by default.
214 * Appending a ? turns back to greedy (!).
216 * - Added a new interceptor ijb-send-banner, which
217 * sends back the "Junkbuster" gif. Without imagelist or
218 * MSIE detection support, or if tinygif = 1, or the
219 * URL isn't recognized as an imageurl, a lame HTML
220 * explanation is sent instead.
222 * - Added new feature, which permits blocking remote
223 * script redirects and firing back a local redirect
225 * The feature is conditionally compiled, i.e. it
226 * can be disabled with --disable-fast-redirects,
227 * plus it must be activated by a "fast-redirects"
228 * line in the config file, has its own log level
229 * and of course wants to be displayed by show-proxy-args
230 * Note: Boy, all the #ifdefs in 1001 locations and
231 * all the fumbling with configure.in and acconfig.h
232 * were *way* more work than the feature itself :-(
234 * - Because a generic redirect template was needed for
235 * this, tinygif = 3 now uses the same.
237 * - Moved GIFs, and other static HTTP response templates
242 * - Removed some >400 CRs again (Jon, you really worked
245 * Revision 1.3 2001/05/20 16:44:47 jongfoster
246 * Removing last hardcoded JunkBusters.com URLs.
248 * Revision 1.2 2001/05/20 01:21:20 jongfoster
249 * Version 2.9.4 checkin.
250 * - Merged popupfile and cookiefile, and added control over PCRS
251 * filtering, in new "permissionsfile".
252 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
253 * file error you now get a message box (in the Win32 GUI) rather
254 * than the program exiting with no explanation.
255 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
257 * - Removed tabs from "config"
258 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
259 * - Bumped up version number.
261 * Revision 1.1.1.1 2001/05/15 13:58:52 oes
262 * Initial import of version 2.9.3 source tree
265 *********************************************************************/
271 #include <sys/types.h>
278 #include <netinet/in.h>
280 #include <winsock2.h>
287 #include "showargs.h"
291 #include "jbsockets.h"
293 #include "jbsockets.h"
294 #include "miscutil.h"
298 #include "deanimate.h"
304 const char filters_h_rcs[] = FILTERS_H_VERSION;
306 /* Fix a problem with Solaris. There should be no effect on other
308 * Solaris's isspace() is a macro which uses it's argument directly
309 * as an array index. Therefore we need to make sure that high-bit
310 * characters generate +ve values, and ideally we also want to make
311 * the argument match the declared parameter type of "int".
313 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
317 /*********************************************************************
319 * Function : block_acl
321 * Description : Block this request?
322 * Decide yes or no based on ACL file.
325 * 1 : dst = The proxy or gateway address this is going to.
326 * Or NULL to check all possible targets.
327 * 2 : csp = Current client state (buffers, headers, etc...)
328 * Also includes the client IP address.
330 * Returns : 0 = FALSE (don't block) and 1 = TRUE (do block)
332 *********************************************************************/
333 int block_acl(struct access_control_addr *dst, struct client_state *csp)
335 struct access_control_list *acl = csp->config->acl;
337 /* if not using an access control list, then permit the connection */
343 /* search the list */
346 if ((csp->ip_addr_long & acl->src->mask) == acl->src->addr)
350 /* Just want to check if they have any access */
351 if (acl->action == ACL_PERMIT)
356 else if ( ((dst->addr & acl->dst->mask) == acl->dst->addr)
357 && ((dst->port == acl->dst->port) || (acl->dst->port == 0)))
359 if (acl->action == ACL_PERMIT)
377 /*********************************************************************
379 * Function : acl_addr
381 * Description : Called from `load_aclfile' to parse an ACL address.
384 * 1 : aspec = String specifying ACL address.
385 * 2 : aca = struct access_control_addr to fill in.
387 * Returns : 0 => Ok, everything else is an error.
389 *********************************************************************/
390 int acl_addr(char *aspec, struct access_control_addr *aca)
392 int i, masklength, port;
398 if ((p = strchr(aspec, '/')))
402 if (ijb_isdigit(*p) == 0)
406 masklength = atoi(p);
409 if ((masklength < 0) || (masklength > 32))
414 if ((p = strchr(aspec, ':')))
418 if (ijb_isdigit(*p) == 0)
427 aca->addr = ntohl(resolve_hostname_to_ip(aspec));
431 log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec);
435 /* build the netmask */
437 for (i=1; i <= masklength ; i++)
439 aca->mask |= (1 << (32 - i));
442 /* now mask off the host portion of the ip address
443 * (i.e. save on the network portion of the address).
445 aca->addr = aca->addr & aca->mask;
450 #endif /* def FEATURE_ACL */
453 /*********************************************************************
455 * Function : block_url
457 * Description : Called from `chat'. Check to see if we need to block this.
460 * 1 : csp = Current client state (buffers, headers, etc...)
462 * Returns : NULL => unblocked, else HTTP block response
464 *********************************************************************/
465 struct http_response *block_url(struct client_state *csp)
467 #ifdef FEATURE_IMAGE_BLOCKING
469 #endif /* def FEATURE_IMAGE_BLOCKING */
470 struct http_response *rsp;
473 * If it's not blocked, don't block it ;-)
475 if ((csp->action->flags & ACTION_BLOCK) == 0)
481 * Else, prepare a response
483 if (NULL == (rsp = alloc_http_response()))
489 * If it's an image-url, send back an image or redirect
490 * as specified by the relevant +image action
492 #ifdef FEATURE_IMAGE_BLOCKING
493 if (((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
496 /* determine HOW images should be blocked */
497 p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
499 /* and handle accordingly: */
500 if ((p == NULL) || (0 == strcmpic(p, "logo")))
502 rsp->body = bindup(image_junkbuster_gif_data, image_junkbuster_gif_length);
503 rsp->content_length = image_junkbuster_gif_length;
504 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
507 else if (0 == strcmpic(p, "blank"))
509 rsp->body = bindup(image_blank_gif_data, image_blank_gif_length);
510 rsp->content_length = image_blank_gif_length;
511 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
516 rsp->status = strdup("302 Local Redirect from Junkbuster");
517 enlist_unique_header(rsp->headers, "Location", p);
521 #endif /* def FEATURE_IMAGE_BLOCKING */
524 * Else, generate an HTML "blocked" message:
527 struct map * exports = default_exports(csp, NULL);
528 #ifdef FEATURE_FORCE_LOAD
529 map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
530 #else /* ifndef FEATURE_FORCE_LOAD */
531 map_block_killer(exports, "force-support");
532 #endif /* ndef FEATURE_FORCE_LOAD */
534 map(exports, "hostport", 1, csp->http->hostport, 1);
535 map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
536 map(exports, "path", 1, csp->http->path, 1);
537 map(exports, "path-html", 1, html_encode(csp->http->path), 0);
539 rsp->body = template_load(csp, "blocked");
540 template_fill(&rsp->body, exports);
544 * Workaround for stupid Netscape bug which prevents
545 * pages from being displayed if loading a referenced
546 * JavaScript or style sheet fails. So make it appear
547 * as if it succeeded.
549 if (csp->http->user_agent
550 && !strncmpic(csp->http->user_agent, "mozilla", 7)
551 && !strstr(csp->http->user_agent, "compatible")
552 && !strstr(csp->http->user_agent, "Opera"))
554 rsp->status = strdup("200 Request for blocked URL");
558 rsp->status = strdup("404 Request for blocked URL");
563 return(finish_http_response(rsp));
569 /*********************************************************************
571 * Function : trust_url FIXME: I should be called distrust_url
573 * Description : Calls is_untrusted_url to determine if the URL is trusted
574 * and if not, returns a HTTP 304 response with a reject message.
577 * 1 : csp = Current client state (buffers, headers, etc...)
579 * Returns : NULL => trusted, else http_response.
581 *********************************************************************/
582 struct http_response *trust_url(struct client_state *csp)
584 struct http_response *rsp;
585 struct map * exports;
586 char buf[BUFFER_SIZE], *p = NULL;
587 struct url_spec **tl, *t;
590 * Don't bother to work on trusted URLs
592 if (!is_untrusted_url(csp))
598 * Else, prepare a response:
600 if (NULL == (rsp = alloc_http_response()))
605 exports = default_exports(csp, NULL);
608 * Export the host, port, and referrer information
610 map(exports, "hostport", 1, csp->http->hostport, 1);
611 map(exports, "path", 1, csp->http->path, 1);
612 map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
613 map(exports, "path-html", 1, html_encode(csp->http->path), 0);
615 if (csp->referrer && strlen(csp->referrer) > 9)
617 map(exports, "referrer", 1, csp->referrer + 9, 1);
618 map(exports, "referrer-html", 1, html_encode(csp->referrer + 9), 0);
622 map(exports, "referrer", 1, "unknown", 1);
623 map(exports, "referrer-html", 1, "unknown", 1);
627 * Export the trust list
629 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
631 sprintf(buf, "<li>%s</li>\n", t->spec);
634 map(exports, "trusted-referrers", 1, p, 0);
638 * Export the trust info, if available
640 if (csp->config->trust_info->next)
644 for (l = csp->config->trust_info->next; l ; l = l->next)
646 sprintf(buf, "<li> <a href=%s>%s</a><br>\n",l->str, l->str);
649 map(exports, "trust-info", 1, p, 0);
653 map_block_killer(exports, "have-trust-info");
657 * Export the force prefix or the force conditional block killer
659 #ifdef FEATURE_FORCE_LOAD
660 map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
661 #else /* ifndef FEATURE_FORCE_LOAD */
662 map_block_killer(exports, "force-support");
663 #endif /* ndef FEATURE_FORCE_LOAD */
668 rsp->body = template_load(csp, "untrusted");
669 template_fill(&rsp->body, exports);
672 return(finish_http_response(rsp));
675 #endif /* def FEATURE_TRUST */
678 #ifdef FEATURE_FAST_REDIRECTS
679 /*********************************************************************
681 * Function : redirect_url
683 * Description : Checks for redirection URLs and returns a HTTP redirect
684 * to the destination URL, if necessary
687 * 1 : csp = Current client state (buffers, headers, etc...)
689 * Returns : NULL if URL was clean, HTTP redirect otherwise.
691 *********************************************************************/
692 struct http_response *redirect_url(struct client_state *csp)
695 struct http_response *rsp;
697 p = q = csp->http->path;
698 log_error(LOG_LEVEL_REDIRECTS, "checking path for redirects: %s", p);
701 * find the last URL encoded in the request
703 while ((p = strstr(p, "http://")))
709 * if there was any, generate and return a HTTP redirect
711 if (q != csp->http->path)
713 log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
715 if (NULL == (rsp = alloc_http_response()))
720 rsp->status = strdup("302 Local Redirect from Junkbuster");
721 enlist_unique_header(rsp->headers, "Location", q);
723 return(finish_http_response(rsp));
731 #endif /* def FEATURE_FAST_REDIRECTS */
734 #ifdef FEATURE_IMAGE_BLOCKING
735 /*********************************************************************
737 * Function : is_imageurl
739 * Description : Given a URL, decide whether it is an image or not,
740 * using either the info from a previous +image action
741 * or, #ifdef FEATURE_IMAGE_DETECT_MSIE, the info from
742 * the browser's accept header.
745 * 1 : csp = Current client state (buffers, headers, etc...)
747 * Returns : True (nonzero) if URL is an image, false (0)
750 *********************************************************************/
751 int is_imageurl(struct client_state *csp)
753 #ifdef FEATURE_IMAGE_DETECT_MSIE
754 if ((csp->accept_types
755 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
756 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE))
760 else if ((csp->accept_types
761 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
762 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_HTML))
766 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
768 return ((csp->action->flags & ACTION_IMAGE) != 0);
771 #endif /* def FEATURE_IMAGE_BLOCKING */
774 #ifdef FEATURE_COOKIE_JAR
775 /*********************************************************************
777 * Function : is_untrusted_url
779 * Description : Should we "distrust" this URL (and block it)?
781 * Yes if it matches a line in the trustfile, or if the
782 * referrer matches a line starting with "+" in the
787 * 1 : csp = Current client state (buffers, headers, etc...)
789 * Returns : 0 => trusted, 1 => untrusted
791 *********************************************************************/
792 int is_untrusted_url(struct client_state *csp)
794 struct file_list *fl;
795 struct block_spec *b;
796 struct url_spec url[1], **tl, *t;
797 struct http_request rhttp[1];
801 * If we don't have a trustlist, we trust everybody
803 if (((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL))
810 * Do we trust the request URL itself?
812 *url = dsplit(csp->http->host);
814 /* if splitting the domain fails, punt */
815 if (url->dbuf == NULL) return(0);
817 memset(rhttp, '\0', sizeof(*rhttp));
819 for (b = b->next; b ; b = b->next)
821 if ((b->url->port == 0) || (b->url->port == csp->http->port))
823 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
825 if ((b->url->path == NULL) ||
827 (regexec(b->url->preg, csp->http->path, 0, NULL, 0) == 0)
829 (strncmp(b->url->path, csp->http->path, b->url->pathlen) == 0)
836 if (b->reject == 0) return(0);
847 if ((csp->referrer == NULL)|| (strlen(csp->referrer) <= 9))
849 /* no referrer was supplied */
853 /* forge a URL from the referrer so we can use
854 * convert_url() to parse it into its components.
858 p = strsav(p, "GET ");
859 p = strsav(p, csp->referrer + 9); /* skip over "Referer: " */
860 p = strsav(p, " HTTP/1.0");
862 parse_http_request(p, rhttp, csp);
865 if (rhttp->cmd == NULL)
872 * If not, do we maybe trust its referrer?
874 *url = dsplit(rhttp->host);
876 /* if splitting the domain fails, punt */
877 if (url->dbuf == NULL) return(1);
879 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
881 if ((t->port == 0) || (t->port == rhttp->port))
883 if ((t->domain[0] == '\0') || domaincmp(t, url) == 0)
885 if ((t->path == NULL) ||
887 (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0)
889 (strncmp(t->path, rhttp->path, t->pathlen) == 0)
893 /* if the URL's referrer is from a trusted referrer, then
894 * add the target spec to the trustfile as an unblocked
895 * domain and return NULL (which means it's OK).
903 if ((fp = fopen(csp->config->trustfile, "a")))
908 h = strsav(h, csp->http->hostport);
914 /* since this path points into a user's home space
915 * be sure to include this spec in the trustfile.
917 if ((p = strchr(p, '/')))
920 h = strsav(h, csp->http->path); /* FIXME: p?! */
925 fprintf(fp, "%s\n", h);
936 #endif /* def FEATURE_COOKIE_JAR */
939 /*********************************************************************
941 * Function : pcrs_filter_response
943 * Description : Apply all the pcrs jobs from the joblist (re_filterfile)
944 * to the text buffer that's been accumulated in
945 * csp->iob->buf and set csp->content_length to the modified
949 * 1 : csp = Current client state (buffers, headers, etc...)
951 * Returns : a pointer to the (newly allocated) modified buffer.
952 * or NULL in case something went wrong
954 *********************************************************************/
955 char *pcrs_filter_response(struct client_state *csp)
958 int size = csp->iob->eod - csp->iob->cur;
960 char *old = csp->iob->cur, *new = NULL;
963 struct file_list *fl;
964 struct re_filterfile_spec *b;
966 /* Sanity first ;-) */
972 if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
974 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
978 if ( NULL == b->joblist )
980 log_error(LOG_LEVEL_RE_FILTER, "Empty joblist. Nothing to do.");
984 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
985 csp->http->hostport, csp->http->path, size);
987 /* Apply all jobs from the joblist */
988 for (job = b->joblist; NULL != job; job = job->next)
990 hits += pcrs_execute(job, old, size, &new, &size);
991 if (old != csp->iob->cur) free(old);
995 log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
997 csp->content_length = size;
999 /* fwiw, reset the iob */
1006 /*********************************************************************
1008 * Function : gif_deanimate_response
1010 * Description : Deanimate the GIF image that has been accumulated in
1011 * csp->iob->buf and set csp->content_length to the modified
1015 * 1 : csp = Current client state (buffers, headers, etc...)
1017 * Returns : a pointer to the (newly allocated) modified buffer.
1018 * or NULL in case something went wrong.
1020 *********************************************************************/
1021 char *gif_deanimate_response(struct client_state *csp)
1023 struct binbuffer *in, *out;
1025 int size = csp->iob->eod - csp->iob->cur;
1027 if ( (NULL == (in = (struct binbuffer *)zalloc(sizeof *in )))
1028 || (NULL == (out = (struct binbuffer *)zalloc(sizeof *out))) )
1030 log_error(LOG_LEVEL_DEANIMATE, "failed! (no mem)");
1034 in->buffer = csp->iob->cur;
1037 if (gif_deanimate(in, out, strncmp("last", csp->action->string[ACTION_STRING_DEANIMATE], 4)))
1039 log_error(LOG_LEVEL_DEANIMATE, "failed! (gif parsing)");
1046 log_error(LOG_LEVEL_DEANIMATE, "Success! GIF shrunk from %d bytes to %d.", size, out->offset);
1047 csp->content_length = out->offset;
1057 /*********************************************************************
1059 * Function : url_actions
1061 * Description : Gets the actions for this URL.
1064 * 1 : http = http_request request for blocked URLs
1065 * 2 : csp = Current client state (buffers, headers, etc...)
1069 *********************************************************************/
1070 void url_actions(struct http_request *http,
1071 struct client_state *csp)
1073 struct file_list *fl;
1074 struct url_actions *b;
1076 init_current_action(csp->action);
1078 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
1083 apply_url_actions(csp->action, http, b);
1088 /*********************************************************************
1090 * Function : apply_url_actions
1092 * Description : Applies a list of URL actions.
1095 * 1 : action = Destination.
1096 * 2 : http = Current URL
1097 * 3 : b = list of URL actions to apply
1101 *********************************************************************/
1102 void apply_url_actions(struct current_action_spec *action,
1103 struct http_request *http,
1104 struct url_actions *b)
1106 struct url_spec url[1];
1110 /* Should never happen */
1114 *url = dsplit(http->host);
1116 /* if splitting the domain fails, punt */
1117 if (url->dbuf == NULL)
1122 for (b = b->next; NULL != b; b = b->next)
1124 if ((b->url->port == 0) || (b->url->port == http->port))
1126 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
1128 if ((b->url->path == NULL) ||
1130 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
1132 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
1136 merge_current_action(action, b->action);
1147 /*********************************************************************
1149 * Function : forward_url
1151 * Description : Should we forward this to another proxy?
1154 * 1 : http = http_request request for current URL
1155 * 2 : csp = Current client state (buffers, headers, etc...)
1157 * Returns : Pointer to forwarding information.
1159 *********************************************************************/
1160 const struct forward_spec * forward_url(struct http_request *http,
1161 struct client_state *csp)
1163 static const struct forward_spec fwd_default[1] = { FORWARD_SPEC_INITIALIZER };
1164 struct forward_spec *fwd = csp->config->forward;
1165 struct url_spec url[1];
1169 return(fwd_default);
1172 *url = dsplit(http->host);
1174 /* if splitting the domain fails, punt */
1175 if (url->dbuf == NULL)
1177 return(fwd_default);
1182 if ((fwd->url->port == 0) || (fwd->url->port == http->port))
1184 if ((fwd->url->domain[0] == '\0') || (domaincmp(fwd->url, url) == 0))
1186 if ((fwd->url->path == NULL) ||
1188 (regexec(fwd->url->preg, http->path, 0, NULL, 0) == 0)
1190 (strncmp(fwd->url->path, http->path, fwd->url->pathlen) == 0)
1205 return(fwd_default);
1210 /*********************************************************************
1214 * Description : Takes a domain and returns a pointer to a url_spec
1215 * structure populated with dbuf, dcnt and dvec. The
1216 * other fields in the structure that is returned are zero.
1219 * 1 : domain = a URL address
1221 * Returns : url_spec structure populated with dbuf, dcnt and dvec.
1223 *********************************************************************/
1224 struct url_spec dsplit(char *domain)
1226 struct url_spec ret[1];
1227 char *v[BUFFER_SIZE];
1231 memset(ret, '\0', sizeof(*ret));
1233 if (domain[strlen(domain) - 1] == '.')
1235 ret->unanchored |= ANCHOR_RIGHT;
1238 if (domain[0] == '.')
1240 ret->unanchored |= ANCHOR_LEFT;
1243 ret->dbuf = strdup(domain);
1245 /* map to lower case */
1246 for (p = ret->dbuf; *p ; p++) *p = tolower(*p);
1248 /* split the domain name into components */
1249 ret->dcnt = ssplit(ret->dbuf, ".", v, SZ(v), 1, 1);
1253 memset(ret, '\0', sizeof(ret));
1257 /* save a copy of the pointers in dvec */
1258 size = ret->dcnt * sizeof(*ret->dvec);
1260 if ((ret->dvec = (char **)malloc(size)))
1262 memcpy(ret->dvec, v, size);
1270 /*********************************************************************
1272 * Function : simple_domaincmp
1274 * Description : Domain-wise Compare fqdn's. The comparison is
1275 * both left- and right-anchored. The individual
1276 * domain names are compared with simplematch().
1277 * This is only used by domaincmp.
1280 * 1 : pv = array of patterns to compare
1281 * 2 : fv = array of domain components to compare
1282 * 3 : len = length of the arrays (both arrays are the
1283 * same length - if they weren't, it couldn't
1284 * possibly be a match).
1286 * Returns : 0 => domains are equivalent, else no match.
1288 *********************************************************************/
1289 static int simple_domaincmp(char **pv, char **fv, int len)
1293 for (n = 0; n < len; n++)
1295 if (simplematch(pv[n], fv[n]))
1306 /*********************************************************************
1308 * Function : domaincmp
1310 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
1311 * pattern->unachored, the comparison is un-, left-,
1312 * right-anchored, or both.
1313 * The individual domain names are compared with
1317 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1318 * 2 : fqdn = domain name against which the patterns are compared.
1320 * Returns : 0 => domains are equivalent, else no match.
1322 *********************************************************************/
1323 int domaincmp(struct url_spec *pattern, struct url_spec *fqdn)
1325 char **pv, **fv; /* vectors */
1327 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
1329 plen = pattern->dcnt;
1334 /* fqdn is too short to match this pattern */
1341 if (unanchored == ANCHOR_LEFT)
1346 * Convert this into a fully anchored pattern with
1347 * the fqdn and pattern the same length
1349 fv += (flen - plen); /* flen - plen >= 0 due to check above */
1350 return simple_domaincmp(pv, fv, plen);
1352 else if (unanchored == 0)
1354 /* Fully anchored, check length */
1359 return simple_domaincmp(pv, fv, plen);
1361 else if (unanchored == ANCHOR_RIGHT)
1363 /* Left anchored, ignore all extra in fqdn */
1364 return simple_domaincmp(pv, fv, plen);
1370 int maxn = flen - plen;
1371 for (n = 0; n <= maxn; n++)
1373 if (!simple_domaincmp(pv, fv, plen))
1378 * Doesn't match from start of fqdn
1379 * Try skipping first part of fqdn