1 const char filters_rcs[] = "$Id: filters.c,v 1.27 2001/08/05 16:06:20 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.27 2001/08/05 16:06:20 jongfoster
42 * Modifiying "struct map" so that there are now separate header and
43 * "map_entry" structures. This means that functions which modify a
44 * map no longer need to return a pointer to the modified map.
45 * Also, it no longer reverses the order of the entries (which may be
46 * important with some advanced template substitutions).
48 * Revision 1.26 2001/07/30 22:08:36 jongfoster
49 * Tidying up #defines:
50 * - All feature #defines are now of the form FEATURE_xxx
51 * - Permanently turned off WIN_GUI_EDIT
52 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
54 * Revision 1.25 2001/07/26 10:09:46 oes
55 * Made browser detection a little less naive
57 * Revision 1.24 2001/07/25 17:22:51 oes
58 * Added workaround for Netscape bug that prevents display of page when loading a component fails.
60 * Revision 1.23 2001/07/23 13:40:12 oes
61 * Fixed bug that caused document body to be dropped when pcrs joblist was empty.
63 * Revision 1.22 2001/07/18 12:29:34 oes
64 * - Made gif_deanimate_response respect
65 * csp->action->string[ACTION_STRING_DEANIMATE]
68 * Revision 1.21 2001/07/13 13:59:53 oes
69 * - Introduced gif_deanimate_response which shares the
70 * generic content modification interface of pcrs_filter_response
71 * and acts as a wrapper to deanimate.c:gif_deanimate()
72 * - Renamed re_process_buffer to pcrs_filter_response
73 * - pcrs_filter_response now returns NULL on failiure
74 * - Removed all #ifdef PCRS
76 * Revision 1.20 2001/07/01 17:01:04 oes
77 * Added comments and missing return statement in is_untrusted_url()
79 * Revision 1.19 2001/06/29 21:45:41 oes
80 * Indentation, CRLF->LF, Tab-> Space
82 * Revision 1.18 2001/06/29 13:27:38 oes
83 * - Cleaned up, renamed and reorderd functions
84 * and improved comments
87 * - Ported to CGI platform. Now delivers
88 * http_response or NULL
89 * - Unified HTML and GIF generation (moved image detection
90 * and GIF generation here from jcc.c:chat())
91 * - Fixed HTTP status to:
92 * - 403 (Forbidden) for the "blocked" HTML message
93 * - 200 (OK) for GIF answers
94 * - 302 (Redirect) for redirect to GIF
97 * - Ported to CGI platform. Now delivers
98 * http_response or NULL
99 * - Separated detection of untrusted URL into
100 * (bool)is_untrusted_url
101 * - Added enforcement of untrusted requests
103 * - Moved redirect_url() from cgi.c to here
104 * and ported it to the CGI platform
106 * - Removed logentry from cancelled commit
108 * Revision 1.17 2001/06/09 10:55:28 jongfoster
109 * Changing BUFSIZ ==> BUFFER_SIZE
111 * Revision 1.16 2001/06/07 23:10:26 jongfoster
112 * Allowing unanchored domain patterns to back off and retry
113 * if they partially match. Optimized right-anchored patterns.
114 * Moving ACL and forward files into config file.
115 * Replacing struct gateway with struct forward_spec
117 * Revision 1.15 2001/06/03 19:12:00 oes
118 * extracted-CGI relevant stuff
120 * Revision 1.14 2001/06/01 10:30:55 oes
121 * Added optional left-anchoring to domaincmp
123 * Revision 1.13 2001/05/31 21:21:30 jongfoster
124 * Permissionsfile / actions file changes:
125 * - Changed "permission" to "action" throughout
126 * - changes to file format to allow string parameters
127 * - Moved helper functions to actions.c
129 * Revision 1.12 2001/05/31 17:35:20 oes
131 * - Enhanced domain part globbing with infix and prefix asterisk
132 * matching and optional unanchored operation
134 * Revision 1.11 2001/05/29 11:53:23 oes
135 * "See why" link added to "blocked" page
137 * Revision 1.10 2001/05/29 09:50:24 jongfoster
138 * Unified blocklist/imagelist/permissionslist.
139 * File format is still under discussion, but the internal changes
142 * Also modified interceptor behaviour:
143 * - We now intercept all URLs beginning with one of the following
144 * prefixes (and *only* these prefixes):
146 * * http://ijbswa.sf.net/config/
147 * * http://ijbswa.sourceforge.net/config/
148 * - New interceptors "home page" - go to http://i.j.b/ to see it.
149 * - Internal changes so that intercepted and fast redirect pages
150 * are not replaced with an image.
151 * - Interceptors now have the option to send a binary page direct
152 * to the client. (i.e. ijb-send-banner uses this)
153 * - Implemented show-url-info interceptor. (Which is why I needed
154 * the above interceptors changes - a typical URL is
155 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
156 * The previous mechanism would not have intercepted that, and
157 * if it had been intercepted then it then it would have replaced
160 * Revision 1.9 2001/05/27 22:17:04 oes
162 * - re_process_buffer no longer writes the modified buffer
163 * to the client, which was very ugly. It now returns the
164 * buffer, which it is then written by chat.
166 * - content_length now adjusts the Content-Length: header
167 * for modified documents rather than crunch()ing it.
168 * (Length info in csp->content_length, which is 0 for
169 * unmodified documents)
171 * - For this to work, sed() is called twice when filtering.
173 * Revision 1.8 2001/05/26 17:13:28 jongfoster
174 * Filled in a function comment.
176 * Revision 1.7 2001/05/26 15:26:15 jongfoster
177 * ACL feature now provides more security by immediately dropping
178 * connections from untrusted hosts.
180 * Revision 1.6 2001/05/26 00:28:36 jongfoster
181 * Automatic reloading of config file.
182 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
183 * Most of the global variables have been moved to a new
184 * struct configuration_spec, accessed through csp->config->globalname
185 * Most of the globals remaining are used by the Win32 GUI.
187 * Revision 1.5 2001/05/25 22:34:30 jongfoster
190 * Revision 1.4 2001/05/22 18:46:04 oes
192 * - Enabled filtering banners by size rather than URL
193 * by adding patterns that replace all standard banner
194 * sizes with the "Junkbuster" gif to the re_filterfile
196 * - Enabled filtering WebBugs by providing a pattern
197 * which kills all 1x1 images
199 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
200 * which is selected by the (nonstandard and therefore
201 * capital) letter 'U' in the option string.
202 * It causes the quantifiers to be ungreedy by default.
203 * Appending a ? turns back to greedy (!).
205 * - Added a new interceptor ijb-send-banner, which
206 * sends back the "Junkbuster" gif. Without imagelist or
207 * MSIE detection support, or if tinygif = 1, or the
208 * URL isn't recognized as an imageurl, a lame HTML
209 * explanation is sent instead.
211 * - Added new feature, which permits blocking remote
212 * script redirects and firing back a local redirect
214 * The feature is conditionally compiled, i.e. it
215 * can be disabled with --disable-fast-redirects,
216 * plus it must be activated by a "fast-redirects"
217 * line in the config file, has its own log level
218 * and of course wants to be displayed by show-proxy-args
219 * Note: Boy, all the #ifdefs in 1001 locations and
220 * all the fumbling with configure.in and acconfig.h
221 * were *way* more work than the feature itself :-(
223 * - Because a generic redirect template was needed for
224 * this, tinygif = 3 now uses the same.
226 * - Moved GIFs, and other static HTTP response templates
231 * - Removed some >400 CRs again (Jon, you really worked
234 * Revision 1.3 2001/05/20 16:44:47 jongfoster
235 * Removing last hardcoded JunkBusters.com URLs.
237 * Revision 1.2 2001/05/20 01:21:20 jongfoster
238 * Version 2.9.4 checkin.
239 * - Merged popupfile and cookiefile, and added control over PCRS
240 * filtering, in new "permissionsfile".
241 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
242 * file error you now get a message box (in the Win32 GUI) rather
243 * than the program exiting with no explanation.
244 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
246 * - Removed tabs from "config"
247 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
248 * - Bumped up version number.
250 * Revision 1.1.1.1 2001/05/15 13:58:52 oes
251 * Initial import of version 2.9.3 source tree
254 *********************************************************************/
260 #include <sys/types.h>
267 #include <netinet/in.h>
269 #include <winsock2.h>
276 #include "showargs.h"
280 #include "jbsockets.h"
282 #include "jbsockets.h"
283 #include "miscutil.h"
287 #include "deanimate.h"
293 const char filters_h_rcs[] = FILTERS_H_VERSION;
295 /* Fix a problem with Solaris. There should be no effect on other
297 * Solaris's isspace() is a macro which uses it's argument directly
298 * as an array index. Therefore we need to make sure that high-bit
299 * characters generate +ve values, and ideally we also want to make
300 * the argument match the declared parameter type of "int".
302 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
306 /*********************************************************************
308 * Function : block_acl
310 * Description : Block this request?
311 * Decide yes or no based on ACL file.
314 * 1 : dst = The proxy or gateway address this is going to.
315 * Or NULL to check all possible targets.
316 * 2 : csp = Current client state (buffers, headers, etc...)
317 * Also includes the client IP address.
319 * Returns : 0 = FALSE (don't block) and 1 = TRUE (do block)
321 *********************************************************************/
322 int block_acl(struct access_control_addr *dst, struct client_state *csp)
324 struct access_control_list *acl = csp->config->acl;
326 /* if not using an access control list, then permit the connection */
332 /* search the list */
335 if ((csp->ip_addr_long & acl->src->mask) == acl->src->addr)
339 /* Just want to check if they have any access */
340 if (acl->action == ACL_PERMIT)
345 else if ( ((dst->addr & acl->dst->mask) == acl->dst->addr)
346 && ((dst->port == acl->dst->port) || (acl->dst->port == 0)))
348 if (acl->action == ACL_PERMIT)
366 /*********************************************************************
368 * Function : acl_addr
370 * Description : Called from `load_aclfile' to parse an ACL address.
373 * 1 : aspec = String specifying ACL address.
374 * 2 : aca = struct access_control_addr to fill in.
376 * Returns : 0 => Ok, everything else is an error.
378 *********************************************************************/
379 int acl_addr(char *aspec, struct access_control_addr *aca)
381 int i, masklength, port;
387 if ((p = strchr(aspec, '/')))
391 if (ijb_isdigit(*p) == 0)
395 masklength = atoi(p);
398 if ((masklength < 0) || (masklength > 32))
403 if ((p = strchr(aspec, ':')))
407 if (ijb_isdigit(*p) == 0)
416 aca->addr = ntohl(resolve_hostname_to_ip(aspec));
420 log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec);
424 /* build the netmask */
426 for (i=1; i <= masklength ; i++)
428 aca->mask |= (1 << (32 - i));
431 /* now mask off the host portion of the ip address
432 * (i.e. save on the network portion of the address).
434 aca->addr = aca->addr & aca->mask;
439 #endif /* def FEATURE_ACL */
442 /*********************************************************************
444 * Function : block_url
446 * Description : Called from `chat'. Check to see if we need to block this.
449 * 1 : csp = Current client state (buffers, headers, etc...)
451 * Returns : NULL => unblocked, else HTTP block response
453 *********************************************************************/
454 struct http_response *block_url(struct client_state *csp)
456 #ifdef FEATURE_IMAGE_BLOCKING
458 #endif /* def FEATURE_IMAGE_BLOCKING */
459 struct http_response *rsp;
462 * If it's not blocked, don't block it ;-)
464 if ((csp->action->flags & ACTION_BLOCK) == 0)
470 * Else, prepare a response
472 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
478 * If it's an image-url, send back an image or redirect
479 * as specified by the relevant +image action
481 #ifdef FEATURE_IMAGE_BLOCKING
482 if (((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
485 /* determine HOW images should be blocked */
486 p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
488 /* and handle accordingly: */
489 if ((p == NULL) || (0 == strcmpic(p, "logo")))
491 rsp->body = bindup(JBGIF, sizeof(JBGIF));
492 rsp->content_length = sizeof(JBGIF);
493 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
496 else if (0 == strcmpic(p, "blank"))
498 rsp->body = bindup(BLANKGIF, sizeof(BLANKGIF));
499 rsp->content_length = sizeof(BLANKGIF);
500 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
505 rsp->status = strdup("302 Local Redirect from Junkbuster");
506 enlist_unique_header(rsp->headers, "Location", p);
510 #endif /* def FEATURE_IMAGE_BLOCKING */
513 * Else, generate an HTML "blocked" message:
516 struct map * exports = default_exports(csp, NULL);
517 #ifdef FEATURE_FORCE_LOAD
518 map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
519 #else /* ifndef FEATURE_FORCE_LOAD */
520 map_block_killer(exports, "force-support");
521 #endif /* ndef FEATURE_FORCE_LOAD */
523 map(exports, "hostport", 1, csp->http->hostport, 1);
524 map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
525 map(exports, "path", 1, csp->http->path, 1);
526 map(exports, "path-html", 1, html_encode(csp->http->path), 0);
528 rsp->body = fill_template(csp, "blocked", exports);
532 * Workaround for stupid Netscape bug which prevents
533 * pages from being displayed if loading a referenced
534 * JavaScript or style sheet fails. So make it appear
535 * as if it succeeded.
537 if (csp->http->user_agent
538 && !strncmpic(csp->http->user_agent, "mozilla", 7)
539 && !strstr(csp->http->user_agent, "compatible")
540 && !strstr(csp->http->user_agent, "Opera"))
542 rsp->status = strdup("200 Request for blocked URL");
546 rsp->status = strdup("404 Request for blocked URL");
551 return(finish_http_response(rsp));
557 /*********************************************************************
559 * Function : trust_url FIXME: I should be called distrust_url
561 * Description : Calls is_untrusted_url to determine if the URL is trusted
562 * and if not, returns a HTTP 304 response with a reject message.
565 * 1 : csp = Current client state (buffers, headers, etc...)
567 * Returns : NULL => trusted, else http_response.
569 *********************************************************************/
570 struct http_response *trust_url(struct client_state *csp)
572 struct http_response *rsp;
573 struct map * exports;
574 char buf[BUFFER_SIZE], *p = NULL;
575 struct url_spec **tl, *t;
578 * Don't bother to work on trusted URLs
580 if (!is_untrusted_url(csp))
586 * Else, prepare a response:
588 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
593 exports = default_exports(csp, NULL);
596 * Export the host, port, and referrer information
598 map(exports, "hostport", 1, csp->http->hostport, 1);
599 map(exports, "path", 1, csp->http->path, 1);
600 map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
601 map(exports, "path-html", 1, html_encode(csp->http->path), 0);
603 if (csp->referrer && strlen(csp->referrer) > 9)
605 map(exports, "referrer", 1, csp->referrer + 9, 1);
606 map(exports, "referrer-html", 1, html_encode(csp->referrer + 9), 0);
610 map(exports, "referrer", 1, "unknown", 1);
611 map(exports, "referrer-html", 1, "unknown", 1);
615 * Export the trust list
617 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
619 sprintf(buf, "<li>%s</li>\n", t->spec);
622 map(exports, "trusted-referrers", 1, p, 0);
626 * Export the trust info, if available
628 if (csp->config->trust_info->next)
632 for (l = csp->config->trust_info->next; l ; l = l->next)
634 sprintf(buf, "<li> <a href=%s>%s</a><br>\n",l->str, l->str);
637 map(exports, "trust-info", 1, p, 0);
641 map_block_killer(exports, "have-trust-info");
645 * Export the force prefix or the force conditional block killer
647 #ifdef FEATURE_FORCE_LOAD
648 map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
649 #else /* ifndef FEATURE_FORCE_LOAD */
650 map_block_killer(exports, "force-support");
651 #endif /* ndef FEATURE_FORCE_LOAD */
656 rsp->body = fill_template(csp, "untrusted", exports);
659 return(finish_http_response(rsp));
662 #endif /* def FEATURE_TRUST */
665 #ifdef FEATURE_FAST_REDIRECTS
666 /*********************************************************************
668 * Function : redirect_url
670 * Description : Checks for redirection URLs and returns a HTTP redirect
671 * to the destination URL, if necessary
674 * 1 : csp = Current client state (buffers, headers, etc...)
676 * Returns : NULL if URL was clean, HTTP redirect otherwise.
678 *********************************************************************/
679 struct http_response *redirect_url(struct client_state *csp)
682 struct http_response *rsp;
684 p = q = csp->http->path;
685 log_error(LOG_LEVEL_REDIRECTS, "checking path for redirects: %s", p);
688 * find the last URL encoded in the request
690 while ((p = strstr(p, "http://")))
696 * if there was any, generate and return a HTTP redirect
698 if (q != csp->http->path)
700 log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
702 if (NULL == ( rsp = zalloc(sizeof(*rsp))))
707 rsp->status = strdup("302 Local Redirect from Junkbuster");
708 enlist_unique_header(rsp->headers, "Location", q);
710 return(finish_http_response(rsp));
718 #endif /* def FEATURE_FAST_REDIRECTS */
721 #ifdef FEATURE_IMAGE_BLOCKING
722 /*********************************************************************
724 * Function : is_imageurl
726 * Description : Given a URL, decide whether it is an image or not,
727 * using either the info from a previous +image action
728 * or, #ifdef FEATURE_IMAGE_DETECT_MSIE, the info from
729 * the browser's accept header.
732 * 1 : csp = Current client state (buffers, headers, etc...)
734 * Returns : True (nonzero) if URL is an image, false (0)
737 *********************************************************************/
738 int is_imageurl(struct client_state *csp)
740 #ifdef FEATURE_IMAGE_DETECT_MSIE
741 if ((csp->accept_types
742 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
743 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE))
747 else if ((csp->accept_types
748 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
749 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_HTML))
753 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
755 return ((csp->action->flags & ACTION_IMAGE) != 0);
758 #endif /* def FEATURE_IMAGE_BLOCKING */
761 #ifdef FEATURE_COOKIE_JAR
762 /*********************************************************************
764 * Function : is_untrusted_url
766 * Description : Should we "distrust" this URL (and block it)?
768 * Yes if it matches a line in the trustfile, or if the
769 * referrer matches a line starting with "+" in the
774 * 1 : csp = Current client state (buffers, headers, etc...)
776 * Returns : 0 => trusted, 1 => untrusted
778 *********************************************************************/
779 int is_untrusted_url(struct client_state *csp)
781 struct file_list *fl;
782 struct block_spec *b;
783 struct url_spec url[1], **tl, *t;
784 struct http_request rhttp[1];
788 * If we don't have a trustlist, we trust everybody
790 if (((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL))
797 * Do we trust the request URL itself?
799 *url = dsplit(csp->http->host);
801 /* if splitting the domain fails, punt */
802 if (url->dbuf == NULL) return(0);
804 memset(rhttp, '\0', sizeof(*rhttp));
806 for (b = b->next; b ; b = b->next)
808 if ((b->url->port == 0) || (b->url->port == csp->http->port))
810 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
812 if ((b->url->path == NULL) ||
814 (regexec(b->url->preg, csp->http->path, 0, NULL, 0) == 0)
816 (strncmp(b->url->path, csp->http->path, b->url->pathlen) == 0)
823 if (b->reject == 0) return(0);
834 if ((csp->referrer == NULL)|| (strlen(csp->referrer) <= 9))
836 /* no referrer was supplied */
840 /* forge a URL from the referrer so we can use
841 * convert_url() to parse it into its components.
845 p = strsav(p, "GET ");
846 p = strsav(p, csp->referrer + 9); /* skip over "Referer: " */
847 p = strsav(p, " HTTP/1.0");
849 parse_http_request(p, rhttp, csp);
852 if (rhttp->cmd == NULL)
859 * If not, do we maybe trust its referrer?
861 *url = dsplit(rhttp->host);
863 /* if splitting the domain fails, punt */
864 if (url->dbuf == NULL) return(1);
866 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
868 if ((t->port == 0) || (t->port == rhttp->port))
870 if ((t->domain[0] == '\0') || domaincmp(t, url) == 0)
872 if ((t->path == NULL) ||
874 (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0)
876 (strncmp(t->path, rhttp->path, t->pathlen) == 0)
880 /* if the URL's referrer is from a trusted referrer, then
881 * add the target spec to the trustfile as an unblocked
882 * domain and return NULL (which means it's OK).
890 if ((fp = fopen(csp->config->trustfile, "a")))
895 h = strsav(h, csp->http->hostport);
901 /* since this path points into a user's home space
902 * be sure to include this spec in the trustfile.
904 if ((p = strchr(p, '/')))
907 h = strsav(h, csp->http->path); /* FIXME: p?! */
912 fprintf(fp, "%s\n", h);
923 #endif /* def FEATURE_COOKIE_JAR */
926 /*********************************************************************
928 * Function : pcrs_filter_response
930 * Description : Apply all the pcrs jobs from the joblist (re_filterfile)
931 * to the text buffer that's been accumulated in
932 * csp->iob->buf and set csp->content_length to the modified
936 * 1 : csp = Current client state (buffers, headers, etc...)
938 * Returns : a pointer to the (newly allocated) modified buffer.
939 * or NULL in case something went wrong
941 *********************************************************************/
942 char *pcrs_filter_response(struct client_state *csp)
945 int size = csp->iob->eod - csp->iob->cur;
947 char *old = csp->iob->cur, *new = NULL;
950 struct file_list *fl;
951 struct re_filterfile_spec *b;
953 /* Sanity first ;-) */
959 if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
961 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
965 if ( NULL == b->joblist )
967 log_error(LOG_LEVEL_RE_FILTER, "Empty joblist. Nothing to do.");
971 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
972 csp->http->hostport, csp->http->path, size);
974 /* Apply all jobs from the joblist */
975 for (job = b->joblist; NULL != job; job = job->next)
977 hits += pcrs_execute(job, old, size, &new, &size);
978 if (old != csp->iob->cur) free(old);
982 log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
984 csp->content_length = size;
986 /* fwiw, reset the iob */
993 /*********************************************************************
995 * Function : gif_deanimate_response
997 * Description : Deanimate the GIF image that has been accumulated in
998 * csp->iob->buf and set csp->content_length to the modified
1002 * 1 : csp = Current client state (buffers, headers, etc...)
1004 * Returns : a pointer to the (newly allocated) modified buffer.
1005 * or NULL in case something went wrong.
1007 *********************************************************************/
1008 char *gif_deanimate_response(struct client_state *csp)
1010 struct binbuffer *in, *out;
1012 int size = csp->iob->eod - csp->iob->cur;
1014 if ( (NULL == (in = (struct binbuffer *)zalloc(sizeof *in )))
1015 || (NULL == (out = (struct binbuffer *)zalloc(sizeof *out))) )
1017 log_error(LOG_LEVEL_DEANIMATE, "failed! (no mem)");
1021 in->buffer = csp->iob->cur;
1024 if (gif_deanimate(in, out, strncmp("last", csp->action->string[ACTION_STRING_DEANIMATE], 4)))
1026 log_error(LOG_LEVEL_DEANIMATE, "failed! (gif parsing)");
1033 log_error(LOG_LEVEL_DEANIMATE, "Success! GIF shrunk from %d bytes to %d.", size, out->offset);
1034 csp->content_length = out->offset;
1044 /*********************************************************************
1046 * Function : url_actions
1048 * Description : Gets the actions for this URL.
1051 * 1 : http = http_request request for blocked URLs
1052 * 2 : csp = Current client state (buffers, headers, etc...)
1056 *********************************************************************/
1057 void url_actions(struct http_request *http,
1058 struct client_state *csp)
1060 struct file_list *fl;
1061 struct url_actions *b;
1063 init_current_action(csp->action);
1065 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
1070 apply_url_actions(csp->action, http, b);
1075 /*********************************************************************
1077 * Function : apply_url_actions
1079 * Description : Applies a list of URL actions.
1082 * 1 : action = Destination.
1083 * 2 : http = Current URL
1084 * 3 : b = list of URL actions to apply
1088 *********************************************************************/
1089 void apply_url_actions(struct current_action_spec *action,
1090 struct http_request *http,
1091 struct url_actions *b)
1093 struct url_spec url[1];
1097 /* Should never happen */
1101 *url = dsplit(http->host);
1103 /* if splitting the domain fails, punt */
1104 if (url->dbuf == NULL)
1109 for (b = b->next; NULL != b; b = b->next)
1111 if ((b->url->port == 0) || (b->url->port == http->port))
1113 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
1115 if ((b->url->path == NULL) ||
1117 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
1119 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
1123 merge_current_action(action, b->action);
1134 /*********************************************************************
1136 * Function : forward_url
1138 * Description : Should we forward this to another proxy?
1141 * 1 : http = http_request request for current URL
1142 * 2 : csp = Current client state (buffers, headers, etc...)
1144 * Returns : Pointer to forwarding information.
1146 *********************************************************************/
1147 const struct forward_spec * forward_url(struct http_request *http,
1148 struct client_state *csp)
1150 static const struct forward_spec fwd_default[1];
1151 struct forward_spec *fwd = csp->config->forward;
1152 struct url_spec url[1];
1154 memset(&fwd_default, '\0', sizeof(struct forward_spec));
1158 return(fwd_default);
1161 *url = dsplit(http->host);
1163 /* if splitting the domain fails, punt */
1164 if (url->dbuf == NULL)
1166 return(fwd_default);
1171 if ((fwd->url->port == 0) || (fwd->url->port == http->port))
1173 if ((fwd->url->domain[0] == '\0') || (domaincmp(fwd->url, url) == 0))
1175 if ((fwd->url->path == NULL) ||
1177 (regexec(fwd->url->preg, http->path, 0, NULL, 0) == 0)
1179 (strncmp(fwd->url->path, http->path, fwd->url->pathlen) == 0)
1194 return(fwd_default);
1199 /*********************************************************************
1203 * Description : Takes a domain and returns a pointer to a url_spec
1204 * structure populated with dbuf, dcnt and dvec. The
1205 * other fields in the structure that is returned are zero.
1208 * 1 : domain = a URL address
1210 * Returns : url_spec structure populated with dbuf, dcnt and dvec.
1212 *********************************************************************/
1213 struct url_spec dsplit(char *domain)
1215 struct url_spec ret[1];
1216 char *v[BUFFER_SIZE];
1220 memset(ret, '\0', sizeof(*ret));
1222 if (domain[strlen(domain) - 1] == '.')
1224 ret->unanchored |= ANCHOR_RIGHT;
1227 if (domain[0] == '.')
1229 ret->unanchored |= ANCHOR_LEFT;
1232 ret->dbuf = strdup(domain);
1234 /* map to lower case */
1235 for (p = ret->dbuf; *p ; p++) *p = tolower(*p);
1237 /* split the domain name into components */
1238 ret->dcnt = ssplit(ret->dbuf, ".", v, SZ(v), 1, 1);
1242 memset(ret, '\0', sizeof(ret));
1246 /* save a copy of the pointers in dvec */
1247 size = ret->dcnt * sizeof(*ret->dvec);
1249 if ((ret->dvec = (char **)malloc(size)))
1251 memcpy(ret->dvec, v, size);
1259 /*********************************************************************
1261 * Function : simple_domaincmp
1263 * Description : Domain-wise Compare fqdn's. The comparison is
1264 * both left- and right-anchored. The individual
1265 * domain names are compared with simplematch().
1266 * This is only used by domaincmp.
1269 * 1 : pv = array of patterns to compare
1270 * 2 : fv = array of domain components to compare
1271 * 3 : len = length of the arrays (both arrays are the
1272 * same length - if they weren't, it couldn't
1273 * possibly be a match).
1275 * Returns : 0 => domains are equivalent, else no match.
1277 *********************************************************************/
1278 static int simple_domaincmp(char **pv, char **fv, int len)
1282 for (n = 0; n < len; n++)
1284 if (simplematch(pv[n], fv[n]))
1295 /*********************************************************************
1297 * Function : domaincmp
1299 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
1300 * pattern->unachored, the comparison is un-, left-,
1301 * right-anchored, or both.
1302 * The individual domain names are compared with
1306 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1307 * 2 : fqdn = domain name against which the patterns are compared.
1309 * Returns : 0 => domains are equivalent, else no match.
1311 *********************************************************************/
1312 int domaincmp(struct url_spec *pattern, struct url_spec *fqdn)
1314 char **pv, **fv; /* vectors */
1316 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
1318 plen = pattern->dcnt;
1323 /* fqdn is too short to match this pattern */
1330 if (unanchored == ANCHOR_LEFT)
1335 * Convert this into a fully anchored pattern with
1336 * the fqdn and pattern the same length
1338 fv += (flen - plen); /* flen - plen >= 0 due to check above */
1339 return simple_domaincmp(pv, fv, plen);
1341 else if (unanchored == 0)
1343 /* Fully anchored, check length */
1348 return simple_domaincmp(pv, fv, plen);
1350 else if (unanchored == ANCHOR_RIGHT)
1352 /* Left anchored, ignore all extra in fqdn */
1353 return simple_domaincmp(pv, fv, plen);
1359 int maxn = flen - plen;
1360 for (n = 0; n <= maxn; n++)
1362 if (!simple_domaincmp(pv, fv, plen))
1367 * Doesn't match from start of fqdn
1368 * Try skipping first part of fqdn