1 const char filters_rcs[] = "$Id: filters.c,v 1.22 2001/07/18 12:29:34 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.22 2001/07/18 12:29:34 oes
42 * - Made gif_deanimate_response respect
43 * csp->action->string[ACTION_STRING_DEANIMATE]
46 * Revision 1.21 2001/07/13 13:59:53 oes
47 * - Introduced gif_deanimate_response which shares the
48 * generic content modification interface of pcrs_filter_response
49 * and acts as a wrapper to deanimate.c:gif_deanimate()
50 * - Renamed re_process_buffer to pcrs_filter_response
51 * - pcrs_filter_response now returns NULL on failiure
52 * - Removed all #ifdef PCRS
54 * Revision 1.20 2001/07/01 17:01:04 oes
55 * Added comments and missing return statement in is_untrusted_url()
57 * Revision 1.19 2001/06/29 21:45:41 oes
58 * Indentation, CRLF->LF, Tab-> Space
60 * Revision 1.18 2001/06/29 13:27:38 oes
61 * - Cleaned up, renamed and reorderd functions
62 * and improved comments
65 * - Ported to CGI platform. Now delivers
66 * http_response or NULL
67 * - Unified HTML and GIF generation (moved image detection
68 * and GIF generation here from jcc.c:chat())
69 * - Fixed HTTP status to:
70 * - 403 (Forbidden) for the "blocked" HTML message
71 * - 200 (OK) for GIF answers
72 * - 302 (Redirect) for redirect to GIF
75 * - Ported to CGI platform. Now delivers
76 * http_response or NULL
77 * - Separated detection of untrusted URL into
78 * (bool)is_untrusted_url
79 * - Added enforcement of untrusted requests
81 * - Moved redirect_url() from cgi.c to here
82 * and ported it to the CGI platform
84 * - Removed logentry from cancelled commit
86 * Revision 1.17 2001/06/09 10:55:28 jongfoster
87 * Changing BUFSIZ ==> BUFFER_SIZE
89 * Revision 1.16 2001/06/07 23:10:26 jongfoster
90 * Allowing unanchored domain patterns to back off and retry
91 * if they partially match. Optimized right-anchored patterns.
92 * Moving ACL and forward files into config file.
93 * Replacing struct gateway with struct forward_spec
95 * Revision 1.15 2001/06/03 19:12:00 oes
96 * extracted-CGI relevant stuff
98 * Revision 1.14 2001/06/01 10:30:55 oes
99 * Added optional left-anchoring to domaincmp
101 * Revision 1.13 2001/05/31 21:21:30 jongfoster
102 * Permissionsfile / actions file changes:
103 * - Changed "permission" to "action" throughout
104 * - changes to file format to allow string parameters
105 * - Moved helper functions to actions.c
107 * Revision 1.12 2001/05/31 17:35:20 oes
109 * - Enhanced domain part globbing with infix and prefix asterisk
110 * matching and optional unanchored operation
112 * Revision 1.11 2001/05/29 11:53:23 oes
113 * "See why" link added to "blocked" page
115 * Revision 1.10 2001/05/29 09:50:24 jongfoster
116 * Unified blocklist/imagelist/permissionslist.
117 * File format is still under discussion, but the internal changes
120 * Also modified interceptor behaviour:
121 * - We now intercept all URLs beginning with one of the following
122 * prefixes (and *only* these prefixes):
124 * * http://ijbswa.sf.net/config/
125 * * http://ijbswa.sourceforge.net/config/
126 * - New interceptors "home page" - go to http://i.j.b/ to see it.
127 * - Internal changes so that intercepted and fast redirect pages
128 * are not replaced with an image.
129 * - Interceptors now have the option to send a binary page direct
130 * to the client. (i.e. ijb-send-banner uses this)
131 * - Implemented show-url-info interceptor. (Which is why I needed
132 * the above interceptors changes - a typical URL is
133 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
134 * The previous mechanism would not have intercepted that, and
135 * if it had been intercepted then it then it would have replaced
138 * Revision 1.9 2001/05/27 22:17:04 oes
140 * - re_process_buffer no longer writes the modified buffer
141 * to the client, which was very ugly. It now returns the
142 * buffer, which it is then written by chat.
144 * - content_length now adjusts the Content-Length: header
145 * for modified documents rather than crunch()ing it.
146 * (Length info in csp->content_length, which is 0 for
147 * unmodified documents)
149 * - For this to work, sed() is called twice when filtering.
151 * Revision 1.8 2001/05/26 17:13:28 jongfoster
152 * Filled in a function comment.
154 * Revision 1.7 2001/05/26 15:26:15 jongfoster
155 * ACL feature now provides more security by immediately dropping
156 * connections from untrusted hosts.
158 * Revision 1.6 2001/05/26 00:28:36 jongfoster
159 * Automatic reloading of config file.
160 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
161 * Most of the global variables have been moved to a new
162 * struct configuration_spec, accessed through csp->config->globalname
163 * Most of the globals remaining are used by the Win32 GUI.
165 * Revision 1.5 2001/05/25 22:34:30 jongfoster
168 * Revision 1.4 2001/05/22 18:46:04 oes
170 * - Enabled filtering banners by size rather than URL
171 * by adding patterns that replace all standard banner
172 * sizes with the "Junkbuster" gif to the re_filterfile
174 * - Enabled filtering WebBugs by providing a pattern
175 * which kills all 1x1 images
177 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
178 * which is selected by the (nonstandard and therefore
179 * capital) letter 'U' in the option string.
180 * It causes the quantifiers to be ungreedy by default.
181 * Appending a ? turns back to greedy (!).
183 * - Added a new interceptor ijb-send-banner, which
184 * sends back the "Junkbuster" gif. Without imagelist or
185 * MSIE detection support, or if tinygif = 1, or the
186 * URL isn't recognized as an imageurl, a lame HTML
187 * explanation is sent instead.
189 * - Added new feature, which permits blocking remote
190 * script redirects and firing back a local redirect
192 * The feature is conditionally compiled, i.e. it
193 * can be disabled with --disable-fast-redirects,
194 * plus it must be activated by a "fast-redirects"
195 * line in the config file, has its own log level
196 * and of course wants to be displayed by show-proxy-args
197 * Note: Boy, all the #ifdefs in 1001 locations and
198 * all the fumbling with configure.in and acconfig.h
199 * were *way* more work than the feature itself :-(
201 * - Because a generic redirect template was needed for
202 * this, tinygif = 3 now uses the same.
204 * - Moved GIFs, and other static HTTP response templates
209 * - Removed some >400 CRs again (Jon, you really worked
212 * Revision 1.3 2001/05/20 16:44:47 jongfoster
213 * Removing last hardcoded JunkBusters.com URLs.
215 * Revision 1.2 2001/05/20 01:21:20 jongfoster
216 * Version 2.9.4 checkin.
217 * - Merged popupfile and cookiefile, and added control over PCRS
218 * filtering, in new "permissionsfile".
219 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
220 * file error you now get a message box (in the Win32 GUI) rather
221 * than the program exiting with no explanation.
222 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
224 * - Removed tabs from "config"
225 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
226 * - Bumped up version number.
228 * Revision 1.1.1.1 2001/05/15 13:58:52 oes
229 * Initial import of version 2.9.3 source tree
232 *********************************************************************/
238 #include <sys/types.h>
245 #include <netinet/in.h>
247 #include <winsock2.h>
254 #include "showargs.h"
258 #include "jbsockets.h"
260 #include "jbsockets.h"
261 #include "miscutil.h"
265 #include "deanimate.h"
271 const char filters_h_rcs[] = FILTERS_H_VERSION;
273 /* Fix a problem with Solaris. There should be no effect on other
275 * Solaris's isspace() is a macro which uses it's argument directly
276 * as an array index. Therefore we need to make sure that high-bit
277 * characters generate +ve values, and ideally we also want to make
278 * the argument match the declared parameter type of "int".
280 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
284 /*********************************************************************
286 * Function : block_acl
288 * Description : Block this request?
289 * Decide yes or no based on ACL file.
292 * 1 : dst = The proxy or gateway address this is going to.
293 * Or NULL to check all possible targets.
294 * 2 : csp = Current client state (buffers, headers, etc...)
295 * Also includes the client IP address.
297 * Returns : 0 = FALSE (don't block) and 1 = TRUE (do block)
299 *********************************************************************/
300 int block_acl(struct access_control_addr *dst, struct client_state *csp)
302 struct access_control_list *acl = csp->config->acl;
304 /* if not using an access control list, then permit the connection */
310 /* search the list */
313 if ((csp->ip_addr_long & acl->src->mask) == acl->src->addr)
317 /* Just want to check if they have any access */
318 if (acl->action == ACL_PERMIT)
323 else if ( ((dst->addr & acl->dst->mask) == acl->dst->addr)
324 && ((dst->port == acl->dst->port) || (acl->dst->port == 0)))
326 if (acl->action == ACL_PERMIT)
344 /*********************************************************************
346 * Function : acl_addr
348 * Description : Called from `load_aclfile' to parse an ACL address.
351 * 1 : aspec = String specifying ACL address.
352 * 2 : aca = struct access_control_addr to fill in.
354 * Returns : 0 => Ok, everything else is an error.
356 *********************************************************************/
357 int acl_addr(char *aspec, struct access_control_addr *aca)
359 int i, masklength, port;
365 if ((p = strchr(aspec, '/')))
369 if (ijb_isdigit(*p) == 0)
373 masklength = atoi(p);
376 if ((masklength < 0) || (masklength > 32))
381 if ((p = strchr(aspec, ':')))
385 if (ijb_isdigit(*p) == 0)
394 aca->addr = ntohl(resolve_hostname_to_ip(aspec));
398 log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec);
402 /* build the netmask */
404 for (i=1; i <= masklength ; i++)
406 aca->mask |= (1 << (32 - i));
409 /* now mask off the host portion of the ip address
410 * (i.e. save on the network portion of the address).
412 aca->addr = aca->addr & aca->mask;
417 #endif /* def ACL_FILES */
420 /*********************************************************************
422 * Function : block_url
424 * Description : Called from `chat'. Check to see if we need to block this.
427 * 1 : csp = Current client state (buffers, headers, etc...)
429 * Returns : NULL => unblocked, else HTTP block response
431 *********************************************************************/
432 struct http_response *block_url(struct client_state *csp)
435 struct http_response *rsp;
436 struct map *exports = NULL;
439 * If it's not blocked, don't block it ;-)
441 if ((csp->action->flags & ACTION_BLOCK) == 0)
447 * Else, prepare a response
449 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
455 * If it's an image-url, send back an image or redirect
456 * as specified by the relevant +image action
458 #ifdef IMAGE_BLOCKING
459 if (((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
462 /* determine HOW images should be blocked */
463 p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
465 /* and handle accordingly: */
466 if ((p == NULL) || (0 == strcmpic(p, "logo")))
468 rsp->body = bindup(JBGIF, sizeof(JBGIF));
469 rsp->content_length = sizeof(JBGIF);
470 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
473 else if (0 == strcmpic(p, "blank"))
475 rsp->body = bindup(BLANKGIF, sizeof(BLANKGIF));
476 rsp->content_length = sizeof(BLANKGIF);
477 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
482 rsp->status = strdup("302 Local Redirect from Junkbuster");
483 enlist_unique_header(rsp->headers, "Location", p);
487 #endif /* def IMAGE_BLOCKING */
490 * Else, generate an HTML "blocked" message:
494 exports = default_exports(csp, NULL);
496 exports = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
498 exports = map_block_killer(exports, "force-support");
499 #endif /* ndef FORCE_LOAD */
501 exports = map(exports, "hostport", 1, csp->http->hostport, 1);
502 exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
503 exports = map(exports, "path", 1, csp->http->path, 1);
504 exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
506 rsp->body = fill_template(csp, "blocked", exports);
509 rsp->status = strdup("403 Request for blocked URL");
512 return(finish_http_response(rsp));
518 /*********************************************************************
520 * Function : trust_url FIXME: I should be called distrust_url
522 * Description : Calls is_untrusted_url to determine if the URL is trusted
523 * and if not, returns a HTTP 304 response with a reject message.
526 * 1 : csp = Current client state (buffers, headers, etc...)
528 * Returns : NULL => trusted, else http_response.
530 *********************************************************************/
531 struct http_response *trust_url(struct client_state *csp)
533 struct http_response *rsp;
534 struct map *exports = NULL;
535 char buf[BUFFER_SIZE], *p = NULL;
536 struct url_spec **tl, *t;
539 * Don't bother to work on trusted URLs
541 if (!is_untrusted_url(csp))
547 * Else, prepare a response:
549 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
553 exports = default_exports(csp, NULL);
556 * Export the host, port, and referrer information
558 exports = map(exports, "hostport", 1, csp->http->hostport, 1);
559 exports = map(exports, "path", 1, csp->http->path, 1);
560 exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
561 exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
563 if (csp->referrer && strlen(csp->referrer) > 9)
565 exports = map(exports, "referrer", 1, csp->referrer + 9, 1);
566 exports = map(exports, "referrer-html", 1, html_encode(csp->referrer + 9), 0);
570 exports = map(exports, "referrer", 1, "unknown", 1);
571 exports = map(exports, "referrer-html", 1, "unknown", 1);
575 * Export the trust list
577 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
579 sprintf(buf, "<li>%s</li>\n", t->spec);
582 exports = map(exports, "trusted-referrers", 1, p, 0);
586 * Export the trust info, if available
588 if (csp->config->trust_info->next)
592 for (l = csp->config->trust_info->next; l ; l = l->next)
594 sprintf(buf, "<li> <a href=%s>%s</a><br>\n",l->str, l->str);
597 exports = map(exports, "trust-info", 1, p, 0);
601 exports = map_block_killer(exports, "have-trust-info");
605 * Export the force prefix or the force conditional block killer
608 exports = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
610 exports = map_block_killer(exports, "force-support");
611 #endif /* ndef FORCE_LOAD */
616 rsp->body = fill_template(csp, "untrusted", exports);
619 return(finish_http_response(rsp));
622 #endif /* def TRUST_FILES */
625 #ifdef FAST_REDIRECTS
626 /*********************************************************************
628 * Function : redirect_url
630 * Description : Checks for redirection URLs and returns a HTTP redirect
631 * to the destination URL, if necessary
634 * 1 : csp = Current client state (buffers, headers, etc...)
636 * Returns : NULL if URL was clean, HTTP redirect otherwise.
638 *********************************************************************/
639 struct http_response *redirect_url(struct client_state *csp)
642 struct http_response *rsp;
644 p = q = csp->http->path;
645 log_error(LOG_LEVEL_REDIRECTS, "checking path for redirects: %s", p);
648 * find the last URL encoded in the request
650 while (p = strstr(p, "http://"))
656 * if there was any, generate and return a HTTP redirect
658 if (q != csp->http->path)
660 log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
662 if (NULL == ( rsp = zalloc(sizeof(*rsp))))
667 rsp->status = strdup("302 Local Redirect from Junkbuster");
668 enlist_unique_header(rsp->headers, "Location", q);
670 return(finish_http_response(rsp));
678 #endif /* def FAST_REDIRECTS */
681 #ifdef IMAGE_BLOCKING
682 /*********************************************************************
684 * Function : is_imageurl
686 * Description : Given a URL, decide whether it is an image or not,
687 * using either the info from a previous +image action
688 * or, #ifdef DETECT_MSIE_IMAGES, the info from the
689 * browser's accept header.
692 * 1 : csp = Current client state (buffers, headers, etc...)
694 * Returns : True (nonzero) if URL is an image, false (0)
697 *********************************************************************/
698 int is_imageurl(struct client_state *csp)
700 #ifdef DETECT_MSIE_IMAGES
701 if ((csp->accept_types
702 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
703 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE))
707 else if ((csp->accept_types
708 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
709 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_HTML))
715 return ((csp->action->flags & ACTION_IMAGE) != 0);
718 #endif /* def IMAGE_BLOCKING */
722 /*********************************************************************
724 * Function : is_untrusted_url
726 * Description : Should we "distrust" this URL (and block it)?
728 * Yes if it matches a line in the trustfile, or if the
729 * referrer matches a line starting with "+" in the
734 * 1 : csp = Current client state (buffers, headers, etc...)
736 * Returns : 0 => trusted, 1 => untrusted
738 *********************************************************************/
739 int is_untrusted_url(struct client_state *csp)
741 struct file_list *fl;
742 struct block_spec *b;
743 struct url_spec url[1], **tl, *t;
744 struct http_request rhttp[1];
748 * If we don't have a trustlist, we trust everybody
750 if (((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL))
757 * Do we trust the request URL itself?
759 *url = dsplit(csp->http->host);
761 /* if splitting the domain fails, punt */
762 if (url->dbuf == NULL) return(0);
764 memset(rhttp, '\0', sizeof(*rhttp));
766 for (b = b->next; b ; b = b->next)
768 if ((b->url->port == 0) || (b->url->port == csp->http->port))
770 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
772 if ((b->url->path == NULL) ||
774 (regexec(b->url->preg, csp->http->path, 0, NULL, 0) == 0)
776 (strncmp(b->url->path, csp->http->path, b->url->pathlen) == 0)
783 if (b->reject == 0) return(0);
794 if ((csp->referrer == NULL)|| (strlen(csp->referrer) <= 9))
796 /* no referrer was supplied */
800 /* forge a URL from the referrer so we can use
801 * convert_url() to parse it into its components.
805 p = strsav(p, "GET ");
806 p = strsav(p, csp->referrer + 9); /* skip over "Referer: " */
807 p = strsav(p, " HTTP/1.0");
809 parse_http_request(p, rhttp, csp);
812 if (rhttp->cmd == NULL)
819 * If not, do we maybe trust its referrer?
821 *url = dsplit(rhttp->host);
823 /* if splitting the domain fails, punt */
824 if (url->dbuf == NULL) return(1);
826 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
828 if ((t->port == 0) || (t->port == rhttp->port))
830 if ((t->domain[0] == '\0') || domaincmp(t, url) == 0)
832 if ((t->path == NULL) ||
834 (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0)
836 (strncmp(t->path, rhttp->path, t->pathlen) == 0)
840 /* if the URL's referrer is from a trusted referrer, then
841 * add the target spec to the trustfile as an unblocked
842 * domain and return NULL (which means it's OK).
850 if ((fp = fopen(csp->config->trustfile, "a")))
855 h = strsav(h, csp->http->hostport);
861 /* since this path points into a user's home space
862 * be sure to include this spec in the trustfile.
864 if ((p = strchr(p, '/')))
867 h = strsav(h, csp->http->path); /* FIXME: p?! */
872 fprintf(fp, "%s\n", h);
883 #endif /* def TRUST_FILES */
886 /*********************************************************************
888 * Function : pcrs_filter_response
890 * Description : Apply all the pcrs jobs from the joblist (re_filterfile)
891 * to the text buffer that's been accumulated in
892 * csp->iob->buf and set csp->content_length to the modified
896 * 1 : csp = Current client state (buffers, headers, etc...)
898 * Returns : a pointer to the (newly allocated) modified buffer.
899 * or NULL in case something went wrong
901 *********************************************************************/
902 char *pcrs_filter_response(struct client_state *csp)
905 int size = csp->iob->eod - csp->iob->cur;
907 char *old = csp->iob->cur, *new = NULL;
910 struct file_list *fl;
911 struct re_filterfile_spec *b;
913 /* Sanity first ;-) */
919 if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
921 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
925 if ( NULL == b->joblist )
927 log_error(LOG_LEVEL_RE_FILTER, "Empty joblist. Nothing to do.");
931 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
932 csp->http->hostport, csp->http->path, size);
934 /* Apply all jobs from the joblist */
935 for (job = b->joblist; NULL != job; job = job->next)
937 hits += pcrs_execute(job, old, size, &new, &size);
938 if (old != csp->iob->cur) free(old);
942 log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
944 csp->content_length = size;
946 /* fwiw, reset the iob */
953 /*********************************************************************
955 * Function : gif_deanimate_response
957 * Description : Deanimate the GIF image that has been accumulated in
958 * csp->iob->buf and set csp->content_length to the modified
962 * 1 : csp = Current client state (buffers, headers, etc...)
964 * Returns : a pointer to the (newly allocated) modified buffer.
965 * or NULL in case something went wrong.
967 *********************************************************************/
968 char *gif_deanimate_response(struct client_state *csp)
970 struct binbuffer *in, *out;
972 int size = csp->iob->eod - csp->iob->cur;
974 if ( (NULL == (in = (struct binbuffer *)zalloc(sizeof *in )))
975 || (NULL == (out = (struct binbuffer *)zalloc(sizeof *out))) )
977 log_error(LOG_LEVEL_DEANIMATE, "failed! (no mem)");
981 in->buffer = csp->iob->cur;
984 if (gif_deanimate(in, out, strncmp("last", csp->action->string[ACTION_STRING_DEANIMATE], 4)))
986 log_error(LOG_LEVEL_DEANIMATE, "failed! (gif parsing)");
993 log_error(LOG_LEVEL_DEANIMATE, "Success! GIF shrunk from %d bytes to %d.", size, out->offset);
994 csp->content_length = out->offset;
1004 /*********************************************************************
1006 * Function : url_actions
1008 * Description : Gets the actions for this URL.
1011 * 1 : http = http_request request for blocked URLs
1012 * 2 : csp = Current client state (buffers, headers, etc...)
1016 *********************************************************************/
1017 void url_actions(struct http_request *http,
1018 struct client_state *csp)
1020 struct file_list *fl;
1021 struct url_actions *b;
1023 init_current_action(csp->action);
1025 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
1030 apply_url_actions(csp->action, http, b);
1035 /*********************************************************************
1037 * Function : apply_url_actions
1039 * Description : Applies a list of URL actions.
1042 * 1 : action = Destination.
1043 * 2 : http = Current URL
1044 * 3 : b = list of URL actions to apply
1048 *********************************************************************/
1049 void apply_url_actions(struct current_action_spec *action,
1050 struct http_request *http,
1051 struct url_actions *b)
1053 struct url_spec url[1];
1057 /* Should never happen */
1061 *url = dsplit(http->host);
1063 /* if splitting the domain fails, punt */
1064 if (url->dbuf == NULL)
1069 for (b = b->next; NULL != b; b = b->next)
1071 if ((b->url->port == 0) || (b->url->port == http->port))
1073 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
1075 if ((b->url->path == NULL) ||
1077 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
1079 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
1083 merge_current_action(action, b->action);
1094 /*********************************************************************
1096 * Function : forward_url
1098 * Description : Should we forward this to another proxy?
1101 * 1 : http = http_request request for current URL
1102 * 2 : csp = Current client state (buffers, headers, etc...)
1104 * Returns : Pointer to forwarding information.
1106 *********************************************************************/
1107 const struct forward_spec * forward_url(struct http_request *http,
1108 struct client_state *csp)
1110 static const struct forward_spec fwd_default[1] = { 0 }; /* All zeroes */
1111 struct forward_spec *fwd = csp->config->forward;
1112 struct url_spec url[1];
1116 return(fwd_default);
1119 *url = dsplit(http->host);
1121 /* if splitting the domain fails, punt */
1122 if (url->dbuf == NULL)
1124 return(fwd_default);
1129 if ((fwd->url->port == 0) || (fwd->url->port == http->port))
1131 if ((fwd->url->domain[0] == '\0') || (domaincmp(fwd->url, url) == 0))
1133 if ((fwd->url->path == NULL) ||
1135 (regexec(fwd->url->preg, http->path, 0, NULL, 0) == 0)
1137 (strncmp(fwd->url->path, http->path, fwd->url->pathlen) == 0)
1152 return(fwd_default);
1157 /*********************************************************************
1161 * Description : Takes a domain and returns a pointer to a url_spec
1162 * structure populated with dbuf, dcnt and dvec. The
1163 * other fields in the structure that is returned are zero.
1166 * 1 : domain = a URL address
1168 * Returns : url_spec structure populated with dbuf, dcnt and dvec.
1170 *********************************************************************/
1171 struct url_spec dsplit(char *domain)
1173 struct url_spec ret[1];
1174 char *v[BUFFER_SIZE];
1178 memset(ret, '\0', sizeof(*ret));
1180 if (domain[strlen(domain) - 1] == '.')
1182 ret->unanchored |= ANCHOR_RIGHT;
1185 if (domain[0] == '.')
1187 ret->unanchored |= ANCHOR_LEFT;
1190 ret->dbuf = strdup(domain);
1192 /* map to lower case */
1193 for (p = ret->dbuf; *p ; p++) *p = tolower(*p);
1195 /* split the domain name into components */
1196 ret->dcnt = ssplit(ret->dbuf, ".", v, SZ(v), 1, 1);
1200 memset(ret, '\0', sizeof(ret));
1204 /* save a copy of the pointers in dvec */
1205 size = ret->dcnt * sizeof(*ret->dvec);
1207 if ((ret->dvec = (char **)malloc(size)))
1209 memcpy(ret->dvec, v, size);
1217 /*********************************************************************
1219 * Function : simple_domaincmp
1221 * Description : Domain-wise Compare fqdn's. The comparison is
1222 * both left- and right-anchored. The individual
1223 * domain names are compared with simplematch().
1224 * This is only used by domaincmp.
1227 * 1 : pv = array of patterns to compare
1228 * 2 : fv = array of domain components to compare
1229 * 3 : len = length of the arrays (both arrays are the
1230 * same length - if they weren't, it couldn't
1231 * possibly be a match).
1233 * Returns : 0 => domains are equivalent, else no match.
1235 *********************************************************************/
1236 static int simple_domaincmp(char **pv, char **fv, int len)
1240 for (n = 0; n < len; n++)
1242 if (simplematch(pv[n], fv[n]))
1253 /*********************************************************************
1255 * Function : domaincmp
1257 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
1258 * pattern->unachored, the comparison is un-, left-,
1259 * right-anchored, or both.
1260 * The individual domain names are compared with
1264 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1265 * 2 : fqdn = domain name against which the patterns are compared.
1267 * Returns : 0 => domains are equivalent, else no match.
1269 *********************************************************************/
1270 int domaincmp(struct url_spec *pattern, struct url_spec *fqdn)
1272 char **pv, **fv; /* vectors */
1274 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
1276 plen = pattern->dcnt;
1281 /* fqdn is too short to match this pattern */
1288 if (unanchored == ANCHOR_LEFT)
1293 * Convert this into a fully anchored pattern with
1294 * the fqdn and pattern the same length
1296 fv += (flen - plen); /* flen - plen >= 0 due to check above */
1297 return simple_domaincmp(pv, fv, plen);
1299 else if (unanchored == 0)
1301 /* Fully anchored, check length */
1306 return simple_domaincmp(pv, fv, plen);
1308 else if (unanchored == ANCHOR_RIGHT)
1310 /* Left anchored, ignore all extra in fqdn */
1311 return simple_domaincmp(pv, fv, plen);
1317 int maxn = flen - plen;
1318 for (n = 0; n <= maxn; n++)
1320 if (!simple_domaincmp(pv, fv, plen))
1325 * Doesn't match from start of fqdn
1326 * Try skipping first part of fqdn