1 const char filters_rcs[] = "$Id: filters.c,v 1.21 2001/07/13 13:59:53 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.21 2001/07/13 13:59:53 oes
42 * - Introduced gif_deanimate_response which shares the
43 * generic content modification interface of pcrs_filter_response
44 * and acts as a wrapper to deanimate.c:gif_deanimate()
45 * - Renamed re_process_buffer to pcrs_filter_response
46 * - pcrs_filter_response now returns NULL on failiure
47 * - Removed all #ifdef PCRS
49 * Revision 1.20 2001/07/01 17:01:04 oes
50 * Added comments and missing return statement in is_untrusted_url()
52 * Revision 1.19 2001/06/29 21:45:41 oes
53 * Indentation, CRLF->LF, Tab-> Space
55 * Revision 1.18 2001/06/29 13:27:38 oes
56 * - Cleaned up, renamed and reorderd functions
57 * and improved comments
60 * - Ported to CGI platform. Now delivers
61 * http_response or NULL
62 * - Unified HTML and GIF generation (moved image detection
63 * and GIF generation here from jcc.c:chat())
64 * - Fixed HTTP status to:
65 * - 403 (Forbidden) for the "blocked" HTML message
66 * - 200 (OK) for GIF answers
67 * - 302 (Redirect) for redirect to GIF
70 * - Ported to CGI platform. Now delivers
71 * http_response or NULL
72 * - Separated detection of untrusted URL into
73 * (bool)is_untrusted_url
74 * - Added enforcement of untrusted requests
76 * - Moved redirect_url() from cgi.c to here
77 * and ported it to the CGI platform
79 * - Removed logentry from cancelled commit
81 * Revision 1.17 2001/06/09 10:55:28 jongfoster
82 * Changing BUFSIZ ==> BUFFER_SIZE
84 * Revision 1.16 2001/06/07 23:10:26 jongfoster
85 * Allowing unanchored domain patterns to back off and retry
86 * if they partially match. Optimized right-anchored patterns.
87 * Moving ACL and forward files into config file.
88 * Replacing struct gateway with struct forward_spec
90 * Revision 1.15 2001/06/03 19:12:00 oes
91 * extracted-CGI relevant stuff
93 * Revision 1.14 2001/06/01 10:30:55 oes
94 * Added optional left-anchoring to domaincmp
96 * Revision 1.13 2001/05/31 21:21:30 jongfoster
97 * Permissionsfile / actions file changes:
98 * - Changed "permission" to "action" throughout
99 * - changes to file format to allow string parameters
100 * - Moved helper functions to actions.c
102 * Revision 1.12 2001/05/31 17:35:20 oes
104 * - Enhanced domain part globbing with infix and prefix asterisk
105 * matching and optional unanchored operation
107 * Revision 1.11 2001/05/29 11:53:23 oes
108 * "See why" link added to "blocked" page
110 * Revision 1.10 2001/05/29 09:50:24 jongfoster
111 * Unified blocklist/imagelist/permissionslist.
112 * File format is still under discussion, but the internal changes
115 * Also modified interceptor behaviour:
116 * - We now intercept all URLs beginning with one of the following
117 * prefixes (and *only* these prefixes):
119 * * http://ijbswa.sf.net/config/
120 * * http://ijbswa.sourceforge.net/config/
121 * - New interceptors "home page" - go to http://i.j.b/ to see it.
122 * - Internal changes so that intercepted and fast redirect pages
123 * are not replaced with an image.
124 * - Interceptors now have the option to send a binary page direct
125 * to the client. (i.e. ijb-send-banner uses this)
126 * - Implemented show-url-info interceptor. (Which is why I needed
127 * the above interceptors changes - a typical URL is
128 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
129 * The previous mechanism would not have intercepted that, and
130 * if it had been intercepted then it then it would have replaced
133 * Revision 1.9 2001/05/27 22:17:04 oes
135 * - re_process_buffer no longer writes the modified buffer
136 * to the client, which was very ugly. It now returns the
137 * buffer, which it is then written by chat.
139 * - content_length now adjusts the Content-Length: header
140 * for modified documents rather than crunch()ing it.
141 * (Length info in csp->content_length, which is 0 for
142 * unmodified documents)
144 * - For this to work, sed() is called twice when filtering.
146 * Revision 1.8 2001/05/26 17:13:28 jongfoster
147 * Filled in a function comment.
149 * Revision 1.7 2001/05/26 15:26:15 jongfoster
150 * ACL feature now provides more security by immediately dropping
151 * connections from untrusted hosts.
153 * Revision 1.6 2001/05/26 00:28:36 jongfoster
154 * Automatic reloading of config file.
155 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
156 * Most of the global variables have been moved to a new
157 * struct configuration_spec, accessed through csp->config->globalname
158 * Most of the globals remaining are used by the Win32 GUI.
160 * Revision 1.5 2001/05/25 22:34:30 jongfoster
163 * Revision 1.4 2001/05/22 18:46:04 oes
165 * - Enabled filtering banners by size rather than URL
166 * by adding patterns that replace all standard banner
167 * sizes with the "Junkbuster" gif to the re_filterfile
169 * - Enabled filtering WebBugs by providing a pattern
170 * which kills all 1x1 images
172 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
173 * which is selected by the (nonstandard and therefore
174 * capital) letter 'U' in the option string.
175 * It causes the quantifiers to be ungreedy by default.
176 * Appending a ? turns back to greedy (!).
178 * - Added a new interceptor ijb-send-banner, which
179 * sends back the "Junkbuster" gif. Without imagelist or
180 * MSIE detection support, or if tinygif = 1, or the
181 * URL isn't recognized as an imageurl, a lame HTML
182 * explanation is sent instead.
184 * - Added new feature, which permits blocking remote
185 * script redirects and firing back a local redirect
187 * The feature is conditionally compiled, i.e. it
188 * can be disabled with --disable-fast-redirects,
189 * plus it must be activated by a "fast-redirects"
190 * line in the config file, has its own log level
191 * and of course wants to be displayed by show-proxy-args
192 * Note: Boy, all the #ifdefs in 1001 locations and
193 * all the fumbling with configure.in and acconfig.h
194 * were *way* more work than the feature itself :-(
196 * - Because a generic redirect template was needed for
197 * this, tinygif = 3 now uses the same.
199 * - Moved GIFs, and other static HTTP response templates
204 * - Removed some >400 CRs again (Jon, you really worked
207 * Revision 1.3 2001/05/20 16:44:47 jongfoster
208 * Removing last hardcoded JunkBusters.com URLs.
210 * Revision 1.2 2001/05/20 01:21:20 jongfoster
211 * Version 2.9.4 checkin.
212 * - Merged popupfile and cookiefile, and added control over PCRS
213 * filtering, in new "permissionsfile".
214 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
215 * file error you now get a message box (in the Win32 GUI) rather
216 * than the program exiting with no explanation.
217 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
219 * - Removed tabs from "config"
220 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
221 * - Bumped up version number.
223 * Revision 1.1.1.1 2001/05/15 13:58:52 oes
224 * Initial import of version 2.9.3 source tree
227 *********************************************************************/
233 #include <sys/types.h>
240 #include <netinet/in.h>
242 #include <winsock2.h>
249 #include "showargs.h"
253 #include "jbsockets.h"
255 #include "jbsockets.h"
256 #include "miscutil.h"
260 #include "deanimate.h"
266 const char filters_h_rcs[] = FILTERS_H_VERSION;
268 /* Fix a problem with Solaris. There should be no effect on other
270 * Solaris's isspace() is a macro which uses it's argument directly
271 * as an array index. Therefore we need to make sure that high-bit
272 * characters generate +ve values, and ideally we also want to make
273 * the argument match the declared parameter type of "int".
275 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
279 /*********************************************************************
281 * Function : block_acl
283 * Description : Block this request?
284 * Decide yes or no based on ACL file.
287 * 1 : dst = The proxy or gateway address this is going to.
288 * Or NULL to check all possible targets.
289 * 2 : csp = Current client state (buffers, headers, etc...)
290 * Also includes the client IP address.
292 * Returns : 0 = FALSE (don't block) and 1 = TRUE (do block)
294 *********************************************************************/
295 int block_acl(struct access_control_addr *dst, struct client_state *csp)
297 struct access_control_list *acl = csp->config->acl;
299 /* if not using an access control list, then permit the connection */
305 /* search the list */
308 if ((csp->ip_addr_long & acl->src->mask) == acl->src->addr)
312 /* Just want to check if they have any access */
313 if (acl->action == ACL_PERMIT)
318 else if ( ((dst->addr & acl->dst->mask) == acl->dst->addr)
319 && ((dst->port == acl->dst->port) || (acl->dst->port == 0)))
321 if (acl->action == ACL_PERMIT)
339 /*********************************************************************
341 * Function : acl_addr
343 * Description : Called from `load_aclfile' to parse an ACL address.
346 * 1 : aspec = String specifying ACL address.
347 * 2 : aca = struct access_control_addr to fill in.
349 * Returns : 0 => Ok, everything else is an error.
351 *********************************************************************/
352 int acl_addr(char *aspec, struct access_control_addr *aca)
354 int i, masklength, port;
360 if ((p = strchr(aspec, '/')))
364 if (ijb_isdigit(*p) == 0)
368 masklength = atoi(p);
371 if ((masklength < 0) || (masklength > 32))
376 if ((p = strchr(aspec, ':')))
380 if (ijb_isdigit(*p) == 0)
389 aca->addr = ntohl(resolve_hostname_to_ip(aspec));
393 log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec);
397 /* build the netmask */
399 for (i=1; i <= masklength ; i++)
401 aca->mask |= (1 << (32 - i));
404 /* now mask off the host portion of the ip address
405 * (i.e. save on the network portion of the address).
407 aca->addr = aca->addr & aca->mask;
412 #endif /* def ACL_FILES */
415 /*********************************************************************
417 * Function : block_url
419 * Description : Called from `chat'. Check to see if we need to block this.
422 * 1 : csp = Current client state (buffers, headers, etc...)
424 * Returns : NULL => unblocked, else HTTP block response
426 *********************************************************************/
427 struct http_response *block_url(struct client_state *csp)
430 struct http_response *rsp;
431 struct map *exports = NULL;
434 * If it's not blocked, don't block it ;-)
436 if ((csp->action->flags & ACTION_BLOCK) == 0)
442 * Else, prepare a response
444 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
450 * If it's an image-url, send back an image or redirect
451 * as specified by the relevant +image action
453 #ifdef IMAGE_BLOCKING
454 if (((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
457 /* determine HOW images should be blocked */
458 p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
460 /* and handle accordingly: */
461 if ((p == NULL) || (0 == strcmpic(p, "logo")))
463 rsp->body = bindup(JBGIF, sizeof(JBGIF));
464 rsp->content_length = sizeof(JBGIF);
465 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
468 else if (0 == strcmpic(p, "blank"))
470 rsp->body = bindup(BLANKGIF, sizeof(BLANKGIF));
471 rsp->content_length = sizeof(BLANKGIF);
472 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
477 rsp->status = strdup("302 Local Redirect from Junkbuster");
478 enlist_unique_header(rsp->headers, "Location", p);
482 #endif /* def IMAGE_BLOCKING */
485 * Else, generate an HTML "blocked" message:
489 exports = default_exports(csp, NULL);
491 exports = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
493 exports = map_block_killer(exports, "force-support");
494 #endif /* ndef FORCE_LOAD */
496 exports = map(exports, "hostport", 1, csp->http->hostport, 1);
497 exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
498 exports = map(exports, "path", 1, csp->http->path, 1);
499 exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
501 rsp->body = fill_template(csp, "blocked", exports);
504 rsp->status = strdup("403 Request for blocked URL");
507 return(finish_http_response(rsp));
513 /*********************************************************************
515 * Function : trust_url FIXME: I should be called distrust_url
517 * Description : Calls is_untrusted_url to determine if the URL is trusted
518 * and if not, returns a HTTP 304 response with a reject message.
521 * 1 : csp = Current client state (buffers, headers, etc...)
523 * Returns : NULL => trusted, else http_response.
525 *********************************************************************/
526 struct http_response *trust_url(struct client_state *csp)
528 struct http_response *rsp;
529 struct map *exports = NULL;
530 char buf[BUFFER_SIZE], *p = NULL;
531 struct url_spec **tl, *t;
534 * Don't bother to work on trusted URLs
536 if (!is_untrusted_url(csp))
542 * Else, prepare a response:
544 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
548 exports = default_exports(csp, NULL);
551 * Export the host, port, and referrer information
553 exports = map(exports, "hostport", 1, csp->http->hostport, 1);
554 exports = map(exports, "path", 1, csp->http->path, 1);
555 exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
556 exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
558 if (csp->referrer && strlen(csp->referrer) > 9)
560 exports = map(exports, "referrer", 1, csp->referrer + 9, 1);
561 exports = map(exports, "referrer-html", 1, html_encode(csp->referrer + 9), 0);
565 exports = map(exports, "referrer", 1, "unknown", 1);
566 exports = map(exports, "referrer-html", 1, "unknown", 1);
570 * Export the trust list
572 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
574 sprintf(buf, "<li>%s</li>\n", t->spec);
577 exports = map(exports, "trusted-referrers", 1, p, 0);
581 * Export the trust info, if available
583 if (csp->config->trust_info->next)
587 for (l = csp->config->trust_info->next; l ; l = l->next)
589 sprintf(buf, "<li> <a href=%s>%s</a><br>\n",l->str, l->str);
592 exports = map(exports, "trust-info", 1, p, 0);
596 exports = map_block_killer(exports, "have-trust-info");
600 * Export the force prefix or the force conditional block killer
603 exports = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
605 exports = map_block_killer(exports, "force-support");
606 #endif /* ndef FORCE_LOAD */
611 rsp->body = fill_template(csp, "untrusted", exports);
614 return(finish_http_response(rsp));
617 #endif /* def TRUST_FILES */
620 #ifdef FAST_REDIRECTS
621 /*********************************************************************
623 * Function : redirect_url
625 * Description : Checks for redirection URLs and returns a HTTP redirect
626 * to the destination URL, if necessary
629 * 1 : csp = Current client state (buffers, headers, etc...)
631 * Returns : NULL if URL was clean, HTTP redirect otherwise.
633 *********************************************************************/
634 struct http_response *redirect_url(struct client_state *csp)
637 struct http_response *rsp;
639 p = q = csp->http->path;
640 log_error(LOG_LEVEL_REDIRECTS, "checking path for redirects: %s", p);
643 * find the last URL encoded in the request
645 while (p = strstr(p, "http://"))
651 * if there was any, generate and return a HTTP redirect
653 if (q != csp->http->path)
655 log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
657 if (NULL == ( rsp = zalloc(sizeof(*rsp))))
662 rsp->status = strdup("302 Local Redirect from Junkbuster");
663 enlist_unique_header(rsp->headers, "Location", q);
665 return(finish_http_response(rsp));
673 #endif /* def FAST_REDIRECTS */
676 #ifdef IMAGE_BLOCKING
677 /*********************************************************************
679 * Function : is_imageurl
681 * Description : Given a URL, decide whether it is an image or not,
682 * using either the info from a previous +image action
683 * or, #ifdef DETECT_MSIE_IMAGES, the info from the
684 * browser's accept header.
687 * 1 : csp = Current client state (buffers, headers, etc...)
689 * Returns : True (nonzero) if URL is an image, false (0)
692 *********************************************************************/
693 int is_imageurl(struct client_state *csp)
695 #ifdef DETECT_MSIE_IMAGES
696 if ((csp->accept_types
697 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
698 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE))
702 else if ((csp->accept_types
703 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
704 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_HTML))
710 return ((csp->action->flags & ACTION_IMAGE) != 0);
713 #endif /* def IMAGE_BLOCKING */
717 /*********************************************************************
719 * Function : is_untrusted_url
721 * Description : Should we "distrust" this URL (and block it)?
723 * Yes if it matches a line in the trustfile, or if the
724 * referrer matches a line starting with "+" in the
729 * 1 : csp = Current client state (buffers, headers, etc...)
731 * Returns : 0 => trusted, 1 => untrusted
733 *********************************************************************/
734 int is_untrusted_url(struct client_state *csp)
736 struct file_list *fl;
737 struct block_spec *b;
738 struct url_spec url[1], **tl, *t;
739 struct http_request rhttp[1];
743 * If we don't have a trustlist, we trust everybody
745 if (((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL))
752 * Do we trust the request URL itself?
754 *url = dsplit(csp->http->host);
756 /* if splitting the domain fails, punt */
757 if (url->dbuf == NULL) return(0);
759 memset(rhttp, '\0', sizeof(*rhttp));
761 for (b = b->next; b ; b = b->next)
763 if ((b->url->port == 0) || (b->url->port == csp->http->port))
765 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
767 if ((b->url->path == NULL) ||
769 (regexec(b->url->preg, csp->http->path, 0, NULL, 0) == 0)
771 (strncmp(b->url->path, csp->http->path, b->url->pathlen) == 0)
778 if (b->reject == 0) return(0);
789 if ((csp->referrer == NULL)|| (strlen(csp->referrer) <= 9))
791 /* no referrer was supplied */
795 /* forge a URL from the referrer so we can use
796 * convert_url() to parse it into its components.
800 p = strsav(p, "GET ");
801 p = strsav(p, csp->referrer + 9); /* skip over "Referer: " */
802 p = strsav(p, " HTTP/1.0");
804 parse_http_request(p, rhttp, csp);
807 if (rhttp->cmd == NULL)
814 * If not, do we maybe trust its referrer?
816 *url = dsplit(rhttp->host);
818 /* if splitting the domain fails, punt */
819 if (url->dbuf == NULL) return(1);
821 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
823 if ((t->port == 0) || (t->port == rhttp->port))
825 if ((t->domain[0] == '\0') || domaincmp(t, url) == 0)
827 if ((t->path == NULL) ||
829 (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0)
831 (strncmp(t->path, rhttp->path, t->pathlen) == 0)
835 /* if the URL's referrer is from a trusted referrer, then
836 * add the target spec to the trustfile as an unblocked
837 * domain and return NULL (which means it's OK).
845 if ((fp = fopen(csp->config->trustfile, "a")))
850 h = strsav(h, csp->http->hostport);
856 /* since this path points into a user's home space
857 * be sure to include this spec in the trustfile.
859 if ((p = strchr(p, '/')))
862 h = strsav(h, csp->http->path); /* FIXME: p?! */
867 fprintf(fp, "%s\n", h);
878 #endif /* def TRUST_FILES */
881 /*********************************************************************
883 * Function : pcrs_filter_response
885 * Description : Apply all the pcrs jobs from the joblist (re_filterfile)
886 * to the text buffer that's been accumulated in
887 * csp->iob->buf and set csp->content_length to the modified
891 * 1 : csp = Current client state (buffers, headers, etc...)
893 * Returns : a pointer to the (newly allocated) modified buffer.
894 * or NULL in case something went wrong
896 *********************************************************************/
897 char *pcrs_filter_response(struct client_state *csp)
900 int size = csp->iob->eod - csp->iob->cur;
902 char *old = csp->iob->cur, *new = NULL;
905 struct file_list *fl;
906 struct re_filterfile_spec *b;
908 /* Sanity first ;-) */
914 if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
916 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
920 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
921 csp->http->hostport, csp->http->path, size);
923 /* Apply all jobs from the joblist */
924 for (job = b->joblist; NULL != job; job = job->next)
926 hits += pcrs_execute(job, old, size, &new, &size);
927 if (old != csp->iob->cur) free(old);
931 log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
933 csp->content_length = size;
935 /* fwiw, reset the iob */
942 /*********************************************************************
944 * Function : gif_deanimate_response
946 * Description : Deanimate the GIF image that has been accumulated in
947 * csp->iob->buf and set csp->content_length to the modified
951 * 1 : csp = Current client state (buffers, headers, etc...)
953 * Returns : a pointer to the (newly allocated) modified buffer.
954 * or NULL in case something went wrong.
956 *********************************************************************/
957 char *gif_deanimate_response(struct client_state *csp)
959 struct binbuffer *in, *out;
961 int size = csp->iob->eod - csp->iob->cur;
963 if ( (NULL == (in = (struct binbuffer *)zalloc(sizeof *in )))
964 || (NULL == (out = (struct binbuffer *)zalloc(sizeof *out))) )
966 log_error(LOG_LEVEL_DEANIMATE, "failed! (no mem)");
970 in->buffer = csp->iob->cur;
973 if (gif_deanimate(in, out, strncmp("last", csp->action->string[ACTION_STRING_DEANIMATE], 4)))
975 log_error(LOG_LEVEL_DEANIMATE, "failed! (gif parsing)");
982 log_error(LOG_LEVEL_DEANIMATE, "Success! GIF shrunk from %d bytes to %d.", size, out->offset);
983 csp->content_length = out->offset;
993 /*********************************************************************
995 * Function : url_actions
997 * Description : Gets the actions for this URL.
1000 * 1 : http = http_request request for blocked URLs
1001 * 2 : csp = Current client state (buffers, headers, etc...)
1005 *********************************************************************/
1006 void url_actions(struct http_request *http,
1007 struct client_state *csp)
1009 struct file_list *fl;
1010 struct url_actions *b;
1012 init_current_action(csp->action);
1014 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
1019 apply_url_actions(csp->action, http, b);
1024 /*********************************************************************
1026 * Function : apply_url_actions
1028 * Description : Applies a list of URL actions.
1031 * 1 : action = Destination.
1032 * 2 : http = Current URL
1033 * 3 : b = list of URL actions to apply
1037 *********************************************************************/
1038 void apply_url_actions(struct current_action_spec *action,
1039 struct http_request *http,
1040 struct url_actions *b)
1042 struct url_spec url[1];
1046 /* Should never happen */
1050 *url = dsplit(http->host);
1052 /* if splitting the domain fails, punt */
1053 if (url->dbuf == NULL)
1058 for (b = b->next; NULL != b; b = b->next)
1060 if ((b->url->port == 0) || (b->url->port == http->port))
1062 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
1064 if ((b->url->path == NULL) ||
1066 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
1068 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
1072 merge_current_action(action, b->action);
1083 /*********************************************************************
1085 * Function : forward_url
1087 * Description : Should we forward this to another proxy?
1090 * 1 : http = http_request request for current URL
1091 * 2 : csp = Current client state (buffers, headers, etc...)
1093 * Returns : Pointer to forwarding information.
1095 *********************************************************************/
1096 const struct forward_spec * forward_url(struct http_request *http,
1097 struct client_state *csp)
1099 static const struct forward_spec fwd_default[1] = { 0 }; /* All zeroes */
1100 struct forward_spec *fwd = csp->config->forward;
1101 struct url_spec url[1];
1105 return(fwd_default);
1108 *url = dsplit(http->host);
1110 /* if splitting the domain fails, punt */
1111 if (url->dbuf == NULL)
1113 return(fwd_default);
1118 if ((fwd->url->port == 0) || (fwd->url->port == http->port))
1120 if ((fwd->url->domain[0] == '\0') || (domaincmp(fwd->url, url) == 0))
1122 if ((fwd->url->path == NULL) ||
1124 (regexec(fwd->url->preg, http->path, 0, NULL, 0) == 0)
1126 (strncmp(fwd->url->path, http->path, fwd->url->pathlen) == 0)
1141 return(fwd_default);
1146 /*********************************************************************
1150 * Description : Takes a domain and returns a pointer to a url_spec
1151 * structure populated with dbuf, dcnt and dvec. The
1152 * other fields in the structure that is returned are zero.
1155 * 1 : domain = a URL address
1157 * Returns : url_spec structure populated with dbuf, dcnt and dvec.
1159 *********************************************************************/
1160 struct url_spec dsplit(char *domain)
1162 struct url_spec ret[1];
1163 char *v[BUFFER_SIZE];
1167 memset(ret, '\0', sizeof(*ret));
1169 if (domain[strlen(domain) - 1] == '.')
1171 ret->unanchored |= ANCHOR_RIGHT;
1174 if (domain[0] == '.')
1176 ret->unanchored |= ANCHOR_LEFT;
1179 ret->dbuf = strdup(domain);
1181 /* map to lower case */
1182 for (p = ret->dbuf; *p ; p++) *p = tolower(*p);
1184 /* split the domain name into components */
1185 ret->dcnt = ssplit(ret->dbuf, ".", v, SZ(v), 1, 1);
1189 memset(ret, '\0', sizeof(ret));
1193 /* save a copy of the pointers in dvec */
1194 size = ret->dcnt * sizeof(*ret->dvec);
1196 if ((ret->dvec = (char **)malloc(size)))
1198 memcpy(ret->dvec, v, size);
1206 /*********************************************************************
1208 * Function : simple_domaincmp
1210 * Description : Domain-wise Compare fqdn's. The comparison is
1211 * both left- and right-anchored. The individual
1212 * domain names are compared with simplematch().
1213 * This is only used by domaincmp.
1216 * 1 : pv = array of patterns to compare
1217 * 2 : fv = array of domain components to compare
1218 * 3 : len = length of the arrays (both arrays are the
1219 * same length - if they weren't, it couldn't
1220 * possibly be a match).
1222 * Returns : 0 => domains are equivalent, else no match.
1224 *********************************************************************/
1225 static int simple_domaincmp(char **pv, char **fv, int len)
1229 for (n = 0; n < len; n++)
1231 if (simplematch(pv[n], fv[n]))
1242 /*********************************************************************
1244 * Function : domaincmp
1246 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
1247 * pattern->unachored, the comparison is un-, left-,
1248 * right-anchored, or both.
1249 * The individual domain names are compared with
1253 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1254 * 2 : fqdn = domain name against which the patterns are compared.
1256 * Returns : 0 => domains are equivalent, else no match.
1258 *********************************************************************/
1259 int domaincmp(struct url_spec *pattern, struct url_spec *fqdn)
1261 char **pv, **fv; /* vectors */
1263 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
1265 plen = pattern->dcnt;
1270 /* fqdn is too short to match this pattern */
1277 if (unanchored == ANCHOR_LEFT)
1282 * Convert this into a fully anchored pattern with
1283 * the fqdn and pattern the same length
1285 fv += (flen - plen); /* flen - plen >= 0 due to check above */
1286 return simple_domaincmp(pv, fv, plen);
1288 else if (unanchored == 0)
1290 /* Fully anchored, check length */
1295 return simple_domaincmp(pv, fv, plen);
1297 else if (unanchored == ANCHOR_RIGHT)
1299 /* Left anchored, ignore all extra in fqdn */
1300 return simple_domaincmp(pv, fv, plen);
1306 int maxn = flen - plen;
1307 for (n = 0; n <= maxn; n++)
1309 if (!simple_domaincmp(pv, fv, plen))
1314 * Doesn't match from start of fqdn
1315 * Try skipping first part of fqdn