1 const char filters_rcs[] = "$Id: filters.c,v 1.24 2001/07/25 17:22:51 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.24 2001/07/25 17:22:51 oes
42 * Added workaround for Netscape bug that prevents display of page when loading a component fails.
44 * Revision 1.23 2001/07/23 13:40:12 oes
45 * Fixed bug that caused document body to be dropped when pcrs joblist was empty.
47 * Revision 1.22 2001/07/18 12:29:34 oes
48 * - Made gif_deanimate_response respect
49 * csp->action->string[ACTION_STRING_DEANIMATE]
52 * Revision 1.21 2001/07/13 13:59:53 oes
53 * - Introduced gif_deanimate_response which shares the
54 * generic content modification interface of pcrs_filter_response
55 * and acts as a wrapper to deanimate.c:gif_deanimate()
56 * - Renamed re_process_buffer to pcrs_filter_response
57 * - pcrs_filter_response now returns NULL on failiure
58 * - Removed all #ifdef PCRS
60 * Revision 1.20 2001/07/01 17:01:04 oes
61 * Added comments and missing return statement in is_untrusted_url()
63 * Revision 1.19 2001/06/29 21:45:41 oes
64 * Indentation, CRLF->LF, Tab-> Space
66 * Revision 1.18 2001/06/29 13:27:38 oes
67 * - Cleaned up, renamed and reorderd functions
68 * and improved comments
71 * - Ported to CGI platform. Now delivers
72 * http_response or NULL
73 * - Unified HTML and GIF generation (moved image detection
74 * and GIF generation here from jcc.c:chat())
75 * - Fixed HTTP status to:
76 * - 403 (Forbidden) for the "blocked" HTML message
77 * - 200 (OK) for GIF answers
78 * - 302 (Redirect) for redirect to GIF
81 * - Ported to CGI platform. Now delivers
82 * http_response or NULL
83 * - Separated detection of untrusted URL into
84 * (bool)is_untrusted_url
85 * - Added enforcement of untrusted requests
87 * - Moved redirect_url() from cgi.c to here
88 * and ported it to the CGI platform
90 * - Removed logentry from cancelled commit
92 * Revision 1.17 2001/06/09 10:55:28 jongfoster
93 * Changing BUFSIZ ==> BUFFER_SIZE
95 * Revision 1.16 2001/06/07 23:10:26 jongfoster
96 * Allowing unanchored domain patterns to back off and retry
97 * if they partially match. Optimized right-anchored patterns.
98 * Moving ACL and forward files into config file.
99 * Replacing struct gateway with struct forward_spec
101 * Revision 1.15 2001/06/03 19:12:00 oes
102 * extracted-CGI relevant stuff
104 * Revision 1.14 2001/06/01 10:30:55 oes
105 * Added optional left-anchoring to domaincmp
107 * Revision 1.13 2001/05/31 21:21:30 jongfoster
108 * Permissionsfile / actions file changes:
109 * - Changed "permission" to "action" throughout
110 * - changes to file format to allow string parameters
111 * - Moved helper functions to actions.c
113 * Revision 1.12 2001/05/31 17:35:20 oes
115 * - Enhanced domain part globbing with infix and prefix asterisk
116 * matching and optional unanchored operation
118 * Revision 1.11 2001/05/29 11:53:23 oes
119 * "See why" link added to "blocked" page
121 * Revision 1.10 2001/05/29 09:50:24 jongfoster
122 * Unified blocklist/imagelist/permissionslist.
123 * File format is still under discussion, but the internal changes
126 * Also modified interceptor behaviour:
127 * - We now intercept all URLs beginning with one of the following
128 * prefixes (and *only* these prefixes):
130 * * http://ijbswa.sf.net/config/
131 * * http://ijbswa.sourceforge.net/config/
132 * - New interceptors "home page" - go to http://i.j.b/ to see it.
133 * - Internal changes so that intercepted and fast redirect pages
134 * are not replaced with an image.
135 * - Interceptors now have the option to send a binary page direct
136 * to the client. (i.e. ijb-send-banner uses this)
137 * - Implemented show-url-info interceptor. (Which is why I needed
138 * the above interceptors changes - a typical URL is
139 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
140 * The previous mechanism would not have intercepted that, and
141 * if it had been intercepted then it then it would have replaced
144 * Revision 1.9 2001/05/27 22:17:04 oes
146 * - re_process_buffer no longer writes the modified buffer
147 * to the client, which was very ugly. It now returns the
148 * buffer, which it is then written by chat.
150 * - content_length now adjusts the Content-Length: header
151 * for modified documents rather than crunch()ing it.
152 * (Length info in csp->content_length, which is 0 for
153 * unmodified documents)
155 * - For this to work, sed() is called twice when filtering.
157 * Revision 1.8 2001/05/26 17:13:28 jongfoster
158 * Filled in a function comment.
160 * Revision 1.7 2001/05/26 15:26:15 jongfoster
161 * ACL feature now provides more security by immediately dropping
162 * connections from untrusted hosts.
164 * Revision 1.6 2001/05/26 00:28:36 jongfoster
165 * Automatic reloading of config file.
166 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
167 * Most of the global variables have been moved to a new
168 * struct configuration_spec, accessed through csp->config->globalname
169 * Most of the globals remaining are used by the Win32 GUI.
171 * Revision 1.5 2001/05/25 22:34:30 jongfoster
174 * Revision 1.4 2001/05/22 18:46:04 oes
176 * - Enabled filtering banners by size rather than URL
177 * by adding patterns that replace all standard banner
178 * sizes with the "Junkbuster" gif to the re_filterfile
180 * - Enabled filtering WebBugs by providing a pattern
181 * which kills all 1x1 images
183 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
184 * which is selected by the (nonstandard and therefore
185 * capital) letter 'U' in the option string.
186 * It causes the quantifiers to be ungreedy by default.
187 * Appending a ? turns back to greedy (!).
189 * - Added a new interceptor ijb-send-banner, which
190 * sends back the "Junkbuster" gif. Without imagelist or
191 * MSIE detection support, or if tinygif = 1, or the
192 * URL isn't recognized as an imageurl, a lame HTML
193 * explanation is sent instead.
195 * - Added new feature, which permits blocking remote
196 * script redirects and firing back a local redirect
198 * The feature is conditionally compiled, i.e. it
199 * can be disabled with --disable-fast-redirects,
200 * plus it must be activated by a "fast-redirects"
201 * line in the config file, has its own log level
202 * and of course wants to be displayed by show-proxy-args
203 * Note: Boy, all the #ifdefs in 1001 locations and
204 * all the fumbling with configure.in and acconfig.h
205 * were *way* more work than the feature itself :-(
207 * - Because a generic redirect template was needed for
208 * this, tinygif = 3 now uses the same.
210 * - Moved GIFs, and other static HTTP response templates
215 * - Removed some >400 CRs again (Jon, you really worked
218 * Revision 1.3 2001/05/20 16:44:47 jongfoster
219 * Removing last hardcoded JunkBusters.com URLs.
221 * Revision 1.2 2001/05/20 01:21:20 jongfoster
222 * Version 2.9.4 checkin.
223 * - Merged popupfile and cookiefile, and added control over PCRS
224 * filtering, in new "permissionsfile".
225 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
226 * file error you now get a message box (in the Win32 GUI) rather
227 * than the program exiting with no explanation.
228 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
230 * - Removed tabs from "config"
231 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
232 * - Bumped up version number.
234 * Revision 1.1.1.1 2001/05/15 13:58:52 oes
235 * Initial import of version 2.9.3 source tree
238 *********************************************************************/
244 #include <sys/types.h>
251 #include <netinet/in.h>
253 #include <winsock2.h>
260 #include "showargs.h"
264 #include "jbsockets.h"
266 #include "jbsockets.h"
267 #include "miscutil.h"
271 #include "deanimate.h"
277 const char filters_h_rcs[] = FILTERS_H_VERSION;
279 /* Fix a problem with Solaris. There should be no effect on other
281 * Solaris's isspace() is a macro which uses it's argument directly
282 * as an array index. Therefore we need to make sure that high-bit
283 * characters generate +ve values, and ideally we also want to make
284 * the argument match the declared parameter type of "int".
286 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
290 /*********************************************************************
292 * Function : block_acl
294 * Description : Block this request?
295 * Decide yes or no based on ACL file.
298 * 1 : dst = The proxy or gateway address this is going to.
299 * Or NULL to check all possible targets.
300 * 2 : csp = Current client state (buffers, headers, etc...)
301 * Also includes the client IP address.
303 * Returns : 0 = FALSE (don't block) and 1 = TRUE (do block)
305 *********************************************************************/
306 int block_acl(struct access_control_addr *dst, struct client_state *csp)
308 struct access_control_list *acl = csp->config->acl;
310 /* if not using an access control list, then permit the connection */
316 /* search the list */
319 if ((csp->ip_addr_long & acl->src->mask) == acl->src->addr)
323 /* Just want to check if they have any access */
324 if (acl->action == ACL_PERMIT)
329 else if ( ((dst->addr & acl->dst->mask) == acl->dst->addr)
330 && ((dst->port == acl->dst->port) || (acl->dst->port == 0)))
332 if (acl->action == ACL_PERMIT)
350 /*********************************************************************
352 * Function : acl_addr
354 * Description : Called from `load_aclfile' to parse an ACL address.
357 * 1 : aspec = String specifying ACL address.
358 * 2 : aca = struct access_control_addr to fill in.
360 * Returns : 0 => Ok, everything else is an error.
362 *********************************************************************/
363 int acl_addr(char *aspec, struct access_control_addr *aca)
365 int i, masklength, port;
371 if ((p = strchr(aspec, '/')))
375 if (ijb_isdigit(*p) == 0)
379 masklength = atoi(p);
382 if ((masklength < 0) || (masklength > 32))
387 if ((p = strchr(aspec, ':')))
391 if (ijb_isdigit(*p) == 0)
400 aca->addr = ntohl(resolve_hostname_to_ip(aspec));
404 log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec);
408 /* build the netmask */
410 for (i=1; i <= masklength ; i++)
412 aca->mask |= (1 << (32 - i));
415 /* now mask off the host portion of the ip address
416 * (i.e. save on the network portion of the address).
418 aca->addr = aca->addr & aca->mask;
423 #endif /* def ACL_FILES */
426 /*********************************************************************
428 * Function : block_url
430 * Description : Called from `chat'. Check to see if we need to block this.
433 * 1 : csp = Current client state (buffers, headers, etc...)
435 * Returns : NULL => unblocked, else HTTP block response
437 *********************************************************************/
438 struct http_response *block_url(struct client_state *csp)
441 struct http_response *rsp;
442 struct map *exports = NULL;
445 * If it's not blocked, don't block it ;-)
447 if ((csp->action->flags & ACTION_BLOCK) == 0)
453 * Else, prepare a response
455 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
461 * If it's an image-url, send back an image or redirect
462 * as specified by the relevant +image action
464 #ifdef IMAGE_BLOCKING
465 if (((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
468 /* determine HOW images should be blocked */
469 p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
471 /* and handle accordingly: */
472 if ((p == NULL) || (0 == strcmpic(p, "logo")))
474 rsp->body = bindup(JBGIF, sizeof(JBGIF));
475 rsp->content_length = sizeof(JBGIF);
476 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
479 else if (0 == strcmpic(p, "blank"))
481 rsp->body = bindup(BLANKGIF, sizeof(BLANKGIF));
482 rsp->content_length = sizeof(BLANKGIF);
483 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
488 rsp->status = strdup("302 Local Redirect from Junkbuster");
489 enlist_unique_header(rsp->headers, "Location", p);
493 #endif /* def IMAGE_BLOCKING */
496 * Else, generate an HTML "blocked" message:
500 exports = default_exports(csp, NULL);
502 exports = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
504 exports = map_block_killer(exports, "force-support");
505 #endif /* ndef FORCE_LOAD */
507 exports = map(exports, "hostport", 1, csp->http->hostport, 1);
508 exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
509 exports = map(exports, "path", 1, csp->http->path, 1);
510 exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
512 rsp->body = fill_template(csp, "blocked", exports);
516 * Workaround for stupid Netscape bug which prevents
517 * pages from being displayed if loading a referenced
518 * JavaScript or style sheet fails. So make it appear
519 * as if it succeeded.
521 if (csp->http->user_agent
522 && !strncmpic(csp->http->user_agent, "mozilla", 7)
523 && !strstr(csp->http->user_agent, "compatible")
524 && !strstr(csp->http->user_agent, "Opera"))
526 rsp->status = strdup("200 Request for blocked URL");
530 rsp->status = strdup("404 Request for blocked URL");
535 return(finish_http_response(rsp));
541 /*********************************************************************
543 * Function : trust_url FIXME: I should be called distrust_url
545 * Description : Calls is_untrusted_url to determine if the URL is trusted
546 * and if not, returns a HTTP 304 response with a reject message.
549 * 1 : csp = Current client state (buffers, headers, etc...)
551 * Returns : NULL => trusted, else http_response.
553 *********************************************************************/
554 struct http_response *trust_url(struct client_state *csp)
556 struct http_response *rsp;
557 struct map *exports = NULL;
558 char buf[BUFFER_SIZE], *p = NULL;
559 struct url_spec **tl, *t;
562 * Don't bother to work on trusted URLs
564 if (!is_untrusted_url(csp))
570 * Else, prepare a response:
572 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
576 exports = default_exports(csp, NULL);
579 * Export the host, port, and referrer information
581 exports = map(exports, "hostport", 1, csp->http->hostport, 1);
582 exports = map(exports, "path", 1, csp->http->path, 1);
583 exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
584 exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
586 if (csp->referrer && strlen(csp->referrer) > 9)
588 exports = map(exports, "referrer", 1, csp->referrer + 9, 1);
589 exports = map(exports, "referrer-html", 1, html_encode(csp->referrer + 9), 0);
593 exports = map(exports, "referrer", 1, "unknown", 1);
594 exports = map(exports, "referrer-html", 1, "unknown", 1);
598 * Export the trust list
600 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
602 sprintf(buf, "<li>%s</li>\n", t->spec);
605 exports = map(exports, "trusted-referrers", 1, p, 0);
609 * Export the trust info, if available
611 if (csp->config->trust_info->next)
615 for (l = csp->config->trust_info->next; l ; l = l->next)
617 sprintf(buf, "<li> <a href=%s>%s</a><br>\n",l->str, l->str);
620 exports = map(exports, "trust-info", 1, p, 0);
624 exports = map_block_killer(exports, "have-trust-info");
628 * Export the force prefix or the force conditional block killer
631 exports = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
633 exports = map_block_killer(exports, "force-support");
634 #endif /* ndef FORCE_LOAD */
639 rsp->body = fill_template(csp, "untrusted", exports);
642 return(finish_http_response(rsp));
645 #endif /* def TRUST_FILES */
648 #ifdef FAST_REDIRECTS
649 /*********************************************************************
651 * Function : redirect_url
653 * Description : Checks for redirection URLs and returns a HTTP redirect
654 * to the destination URL, if necessary
657 * 1 : csp = Current client state (buffers, headers, etc...)
659 * Returns : NULL if URL was clean, HTTP redirect otherwise.
661 *********************************************************************/
662 struct http_response *redirect_url(struct client_state *csp)
665 struct http_response *rsp;
667 p = q = csp->http->path;
668 log_error(LOG_LEVEL_REDIRECTS, "checking path for redirects: %s", p);
671 * find the last URL encoded in the request
673 while (p = strstr(p, "http://"))
679 * if there was any, generate and return a HTTP redirect
681 if (q != csp->http->path)
683 log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
685 if (NULL == ( rsp = zalloc(sizeof(*rsp))))
690 rsp->status = strdup("302 Local Redirect from Junkbuster");
691 enlist_unique_header(rsp->headers, "Location", q);
693 return(finish_http_response(rsp));
701 #endif /* def FAST_REDIRECTS */
704 #ifdef IMAGE_BLOCKING
705 /*********************************************************************
707 * Function : is_imageurl
709 * Description : Given a URL, decide whether it is an image or not,
710 * using either the info from a previous +image action
711 * or, #ifdef DETECT_MSIE_IMAGES, the info from the
712 * browser's accept header.
715 * 1 : csp = Current client state (buffers, headers, etc...)
717 * Returns : True (nonzero) if URL is an image, false (0)
720 *********************************************************************/
721 int is_imageurl(struct client_state *csp)
723 #ifdef DETECT_MSIE_IMAGES
724 if ((csp->accept_types
725 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
726 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE))
730 else if ((csp->accept_types
731 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
732 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_HTML))
738 return ((csp->action->flags & ACTION_IMAGE) != 0);
741 #endif /* def IMAGE_BLOCKING */
745 /*********************************************************************
747 * Function : is_untrusted_url
749 * Description : Should we "distrust" this URL (and block it)?
751 * Yes if it matches a line in the trustfile, or if the
752 * referrer matches a line starting with "+" in the
757 * 1 : csp = Current client state (buffers, headers, etc...)
759 * Returns : 0 => trusted, 1 => untrusted
761 *********************************************************************/
762 int is_untrusted_url(struct client_state *csp)
764 struct file_list *fl;
765 struct block_spec *b;
766 struct url_spec url[1], **tl, *t;
767 struct http_request rhttp[1];
771 * If we don't have a trustlist, we trust everybody
773 if (((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL))
780 * Do we trust the request URL itself?
782 *url = dsplit(csp->http->host);
784 /* if splitting the domain fails, punt */
785 if (url->dbuf == NULL) return(0);
787 memset(rhttp, '\0', sizeof(*rhttp));
789 for (b = b->next; b ; b = b->next)
791 if ((b->url->port == 0) || (b->url->port == csp->http->port))
793 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
795 if ((b->url->path == NULL) ||
797 (regexec(b->url->preg, csp->http->path, 0, NULL, 0) == 0)
799 (strncmp(b->url->path, csp->http->path, b->url->pathlen) == 0)
806 if (b->reject == 0) return(0);
817 if ((csp->referrer == NULL)|| (strlen(csp->referrer) <= 9))
819 /* no referrer was supplied */
823 /* forge a URL from the referrer so we can use
824 * convert_url() to parse it into its components.
828 p = strsav(p, "GET ");
829 p = strsav(p, csp->referrer + 9); /* skip over "Referer: " */
830 p = strsav(p, " HTTP/1.0");
832 parse_http_request(p, rhttp, csp);
835 if (rhttp->cmd == NULL)
842 * If not, do we maybe trust its referrer?
844 *url = dsplit(rhttp->host);
846 /* if splitting the domain fails, punt */
847 if (url->dbuf == NULL) return(1);
849 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
851 if ((t->port == 0) || (t->port == rhttp->port))
853 if ((t->domain[0] == '\0') || domaincmp(t, url) == 0)
855 if ((t->path == NULL) ||
857 (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0)
859 (strncmp(t->path, rhttp->path, t->pathlen) == 0)
863 /* if the URL's referrer is from a trusted referrer, then
864 * add the target spec to the trustfile as an unblocked
865 * domain and return NULL (which means it's OK).
873 if ((fp = fopen(csp->config->trustfile, "a")))
878 h = strsav(h, csp->http->hostport);
884 /* since this path points into a user's home space
885 * be sure to include this spec in the trustfile.
887 if ((p = strchr(p, '/')))
890 h = strsav(h, csp->http->path); /* FIXME: p?! */
895 fprintf(fp, "%s\n", h);
906 #endif /* def TRUST_FILES */
909 /*********************************************************************
911 * Function : pcrs_filter_response
913 * Description : Apply all the pcrs jobs from the joblist (re_filterfile)
914 * to the text buffer that's been accumulated in
915 * csp->iob->buf and set csp->content_length to the modified
919 * 1 : csp = Current client state (buffers, headers, etc...)
921 * Returns : a pointer to the (newly allocated) modified buffer.
922 * or NULL in case something went wrong
924 *********************************************************************/
925 char *pcrs_filter_response(struct client_state *csp)
928 int size = csp->iob->eod - csp->iob->cur;
930 char *old = csp->iob->cur, *new = NULL;
933 struct file_list *fl;
934 struct re_filterfile_spec *b;
936 /* Sanity first ;-) */
942 if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
944 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
948 if ( NULL == b->joblist )
950 log_error(LOG_LEVEL_RE_FILTER, "Empty joblist. Nothing to do.");
954 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
955 csp->http->hostport, csp->http->path, size);
957 /* Apply all jobs from the joblist */
958 for (job = b->joblist; NULL != job; job = job->next)
960 hits += pcrs_execute(job, old, size, &new, &size);
961 if (old != csp->iob->cur) free(old);
965 log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
967 csp->content_length = size;
969 /* fwiw, reset the iob */
976 /*********************************************************************
978 * Function : gif_deanimate_response
980 * Description : Deanimate the GIF image that has been accumulated in
981 * csp->iob->buf and set csp->content_length to the modified
985 * 1 : csp = Current client state (buffers, headers, etc...)
987 * Returns : a pointer to the (newly allocated) modified buffer.
988 * or NULL in case something went wrong.
990 *********************************************************************/
991 char *gif_deanimate_response(struct client_state *csp)
993 struct binbuffer *in, *out;
995 int size = csp->iob->eod - csp->iob->cur;
997 if ( (NULL == (in = (struct binbuffer *)zalloc(sizeof *in )))
998 || (NULL == (out = (struct binbuffer *)zalloc(sizeof *out))) )
1000 log_error(LOG_LEVEL_DEANIMATE, "failed! (no mem)");
1004 in->buffer = csp->iob->cur;
1007 if (gif_deanimate(in, out, strncmp("last", csp->action->string[ACTION_STRING_DEANIMATE], 4)))
1009 log_error(LOG_LEVEL_DEANIMATE, "failed! (gif parsing)");
1016 log_error(LOG_LEVEL_DEANIMATE, "Success! GIF shrunk from %d bytes to %d.", size, out->offset);
1017 csp->content_length = out->offset;
1027 /*********************************************************************
1029 * Function : url_actions
1031 * Description : Gets the actions for this URL.
1034 * 1 : http = http_request request for blocked URLs
1035 * 2 : csp = Current client state (buffers, headers, etc...)
1039 *********************************************************************/
1040 void url_actions(struct http_request *http,
1041 struct client_state *csp)
1043 struct file_list *fl;
1044 struct url_actions *b;
1046 init_current_action(csp->action);
1048 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
1053 apply_url_actions(csp->action, http, b);
1058 /*********************************************************************
1060 * Function : apply_url_actions
1062 * Description : Applies a list of URL actions.
1065 * 1 : action = Destination.
1066 * 2 : http = Current URL
1067 * 3 : b = list of URL actions to apply
1071 *********************************************************************/
1072 void apply_url_actions(struct current_action_spec *action,
1073 struct http_request *http,
1074 struct url_actions *b)
1076 struct url_spec url[1];
1080 /* Should never happen */
1084 *url = dsplit(http->host);
1086 /* if splitting the domain fails, punt */
1087 if (url->dbuf == NULL)
1092 for (b = b->next; NULL != b; b = b->next)
1094 if ((b->url->port == 0) || (b->url->port == http->port))
1096 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
1098 if ((b->url->path == NULL) ||
1100 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
1102 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
1106 merge_current_action(action, b->action);
1117 /*********************************************************************
1119 * Function : forward_url
1121 * Description : Should we forward this to another proxy?
1124 * 1 : http = http_request request for current URL
1125 * 2 : csp = Current client state (buffers, headers, etc...)
1127 * Returns : Pointer to forwarding information.
1129 *********************************************************************/
1130 const struct forward_spec * forward_url(struct http_request *http,
1131 struct client_state *csp)
1133 static const struct forward_spec fwd_default[1] = { 0 }; /* All zeroes */
1134 struct forward_spec *fwd = csp->config->forward;
1135 struct url_spec url[1];
1139 return(fwd_default);
1142 *url = dsplit(http->host);
1144 /* if splitting the domain fails, punt */
1145 if (url->dbuf == NULL)
1147 return(fwd_default);
1152 if ((fwd->url->port == 0) || (fwd->url->port == http->port))
1154 if ((fwd->url->domain[0] == '\0') || (domaincmp(fwd->url, url) == 0))
1156 if ((fwd->url->path == NULL) ||
1158 (regexec(fwd->url->preg, http->path, 0, NULL, 0) == 0)
1160 (strncmp(fwd->url->path, http->path, fwd->url->pathlen) == 0)
1175 return(fwd_default);
1180 /*********************************************************************
1184 * Description : Takes a domain and returns a pointer to a url_spec
1185 * structure populated with dbuf, dcnt and dvec. The
1186 * other fields in the structure that is returned are zero.
1189 * 1 : domain = a URL address
1191 * Returns : url_spec structure populated with dbuf, dcnt and dvec.
1193 *********************************************************************/
1194 struct url_spec dsplit(char *domain)
1196 struct url_spec ret[1];
1197 char *v[BUFFER_SIZE];
1201 memset(ret, '\0', sizeof(*ret));
1203 if (domain[strlen(domain) - 1] == '.')
1205 ret->unanchored |= ANCHOR_RIGHT;
1208 if (domain[0] == '.')
1210 ret->unanchored |= ANCHOR_LEFT;
1213 ret->dbuf = strdup(domain);
1215 /* map to lower case */
1216 for (p = ret->dbuf; *p ; p++) *p = tolower(*p);
1218 /* split the domain name into components */
1219 ret->dcnt = ssplit(ret->dbuf, ".", v, SZ(v), 1, 1);
1223 memset(ret, '\0', sizeof(ret));
1227 /* save a copy of the pointers in dvec */
1228 size = ret->dcnt * sizeof(*ret->dvec);
1230 if ((ret->dvec = (char **)malloc(size)))
1232 memcpy(ret->dvec, v, size);
1240 /*********************************************************************
1242 * Function : simple_domaincmp
1244 * Description : Domain-wise Compare fqdn's. The comparison is
1245 * both left- and right-anchored. The individual
1246 * domain names are compared with simplematch().
1247 * This is only used by domaincmp.
1250 * 1 : pv = array of patterns to compare
1251 * 2 : fv = array of domain components to compare
1252 * 3 : len = length of the arrays (both arrays are the
1253 * same length - if they weren't, it couldn't
1254 * possibly be a match).
1256 * Returns : 0 => domains are equivalent, else no match.
1258 *********************************************************************/
1259 static int simple_domaincmp(char **pv, char **fv, int len)
1263 for (n = 0; n < len; n++)
1265 if (simplematch(pv[n], fv[n]))
1276 /*********************************************************************
1278 * Function : domaincmp
1280 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
1281 * pattern->unachored, the comparison is un-, left-,
1282 * right-anchored, or both.
1283 * The individual domain names are compared with
1287 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1288 * 2 : fqdn = domain name against which the patterns are compared.
1290 * Returns : 0 => domains are equivalent, else no match.
1292 *********************************************************************/
1293 int domaincmp(struct url_spec *pattern, struct url_spec *fqdn)
1295 char **pv, **fv; /* vectors */
1297 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
1299 plen = pattern->dcnt;
1304 /* fqdn is too short to match this pattern */
1311 if (unanchored == ANCHOR_LEFT)
1316 * Convert this into a fully anchored pattern with
1317 * the fqdn and pattern the same length
1319 fv += (flen - plen); /* flen - plen >= 0 due to check above */
1320 return simple_domaincmp(pv, fv, plen);
1322 else if (unanchored == 0)
1324 /* Fully anchored, check length */
1329 return simple_domaincmp(pv, fv, plen);
1331 else if (unanchored == ANCHOR_RIGHT)
1333 /* Left anchored, ignore all extra in fqdn */
1334 return simple_domaincmp(pv, fv, plen);
1340 int maxn = flen - plen;
1341 for (n = 0; n <= maxn; n++)
1343 if (!simple_domaincmp(pv, fv, plen))
1348 * Doesn't match from start of fqdn
1349 * Try skipping first part of fqdn