1 const char filters_rcs[] = "$Id: filters.c,v 1.15 2001/06/03 19:12:00 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', `re_process_buffer',
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.15 2001/06/03 19:12:00 oes
42 * extracted-CGI relevant stuff
44 * Revision 1.15 2001/06/03 11:03:48 oes
51 * adapted to new enlist_unique arg format
55 * introduced confdir option
57 * filters.c filtrers.h
59 * extracted-CGI relevant stuff
67 * support for new cgi mechansim
71 * functions for new list type: "map"
72 * extended enlist_unique
79 * deleted const struct interceptors
87 * added struct http_response
88 * changes struct interceptors to struct cgi_dispatcher
89 * moved HTML stuff to cgi.h
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"
269 const char filters_h_rcs[] = FILTERS_H_VERSION;
271 /* Fix a problem with Solaris. There should be no effect on other
273 * Solaris's isspace() is a macro which uses it's argument directly
274 * as an array index. Therefore we need to make sure that high-bit
275 * characters generate +ve values, and ideally we also want to make
276 * the argument match the declared parameter type of "int".
278 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
282 /*********************************************************************
284 * Function : block_acl
286 * Description : Block this request?
287 * Decide yes or no based on ACL file.
290 * 1 : dst = The proxy or gateway address this is going to.
291 * Or NULL to check all possible targets.
292 * 2 : csp = Current client state (buffers, headers, etc...)
293 * Also includes the client IP address.
295 * Returns : 0 = FALSE (don't block) and 1 = TRUE (do block)
297 *********************************************************************/
298 int block_acl(struct access_control_addr *dst,
299 struct client_state *csp)
301 struct access_control_list *acl = csp->config->acl;
303 /* if not using an access control list, then permit the connection */
309 /* search the list */
312 if ((csp->ip_addr_long & acl->src->mask) == acl->src->addr)
316 /* Just want to check if they have any access */
317 if (acl->action == ACL_PERMIT)
322 else if ( ((dst->addr & acl->dst->mask) == acl->dst->addr)
323 && ((dst->port == acl->dst->port) || (acl->dst->port == 0)))
325 if (acl->action == ACL_PERMIT)
343 /*********************************************************************
345 * Function : acl_addr
347 * Description : Called from `load_aclfile' to parse an ACL address.
350 * 1 : aspec = String specifying ACL address.
351 * 2 : aca = struct access_control_addr to fill in.
353 * Returns : 0 => Ok, everything else is an error.
355 *********************************************************************/
356 int acl_addr(char *aspec, struct access_control_addr *aca)
358 int i, masklength, port;
364 if ((p = strchr(aspec, '/')))
368 if (ijb_isdigit(*p) == 0)
372 masklength = atoi(p);
375 if ((masklength < 0) || (masklength > 32))
380 if ((p = strchr(aspec, ':')))
384 if (ijb_isdigit(*p) == 0)
393 aca->addr = ntohl(resolve_hostname_to_ip(aspec));
397 log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec);
401 /* build the netmask */
403 for (i=1; i <= masklength ; i++)
405 aca->mask |= (1 << (32 - i));
408 /* now mask off the host portion of the ip address
409 * (i.e. save on the network portion of the address).
411 aca->addr = aca->addr & aca->mask;
416 #endif /* def ACL_FILES */
419 /*********************************************************************
421 * Function : block_url
423 * Description : Called from `chat'. Check to see if we need to block this.
426 * 1 : http = http_request request to "check" for blocked
427 * 2 : csp = Current client state (buffers, headers, etc...)
429 * Returns : NULL => unblocked, else string to HTML block description.
431 *********************************************************************/
432 char *block_url(struct http_request *http, struct client_state *csp)
438 if ((csp->action->flags & ACTION_BLOCK) == 0)
446 #endif /* def FORCE_LOAD */
449 n += factor * strlen(http->hostport);
450 n += factor * strlen(http->path);
452 p = (char *)malloc(n);
455 sprintf(p, CBLOCK, http->hostport, http->path, http->hostport, http->path,
456 http->hostport, http->path);
458 sprintf(p, CBLOCK, http->hostport, http->path, http->hostport, http->path);
459 #endif /* def FORCE_LOAD */
466 #ifdef IMAGE_BLOCKING
467 /*********************************************************************
469 * Function : block_imageurl
471 * Description : Given a URL which is blocked, decide whether to
472 * send the "blocked" image or HTML.
475 * 1 : http = URL to check.
476 * 2 : csp = Current client state (buffers, headers, etc...)
478 * Returns : True (nonzero) if URL is in image list, false (0)
481 *********************************************************************/
482 int block_imageurl(struct http_request *http, struct client_state *csp)
484 #ifdef DETECT_MSIE_IMAGES
485 if ((csp->accept_types
486 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
487 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE))
491 else if ((csp->accept_types
492 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
493 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_HTML))
499 return ((csp->action->flags & ACTION_IMAGE) != 0);
501 #endif /* def IMAGE_BLOCKING */
505 /*********************************************************************
507 * Function : trust_url
509 * Description : Should we "trust" this URL? See "trustfile" line in config.
512 * 1 : http = http_request request for requested URL
513 * 2 : csp = Current client state (buffers, headers, etc...)
515 * Returns : NULL => trusted, else string to HTML "untrusted" description.
517 *********************************************************************/
518 char *trust_url(struct http_request *http, struct client_state *csp)
520 struct file_list *fl;
521 struct block_spec *b;
522 struct url_spec url[1], **tl, *t;
524 char *hostport, *path, *refer;
525 struct http_request rhttp[1];
528 if (((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL))
533 *url = dsplit(http->host);
535 /* if splitting the domain fails, punt */
536 if (url->dbuf == NULL) return(NULL);
538 memset(rhttp, '\0', sizeof(*rhttp));
540 for (b = b->next; b ; b = b->next)
542 if ((b->url->port == 0) || (b->url->port == http->port))
544 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
546 if ((b->url->path == NULL) ||
548 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
550 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
557 if (b->reject == 0) return(NULL);
559 hostport = url_encode(http->hostport);
560 path = url_encode(http->path);
564 refer = url_encode(csp->referrer);
568 refer = url_encode("undefined");
572 n += strlen(hostport);
576 p = (char *)malloc(n);
578 sprintf(p, CTRUST, hostport, path, refer);
593 if ((csp->referrer == NULL)|| (strlen(csp->referrer) <= 9))
595 /* no referrer was supplied */
596 goto trust_url_not_trusted;
599 /* forge a URL from the referrer so we can use
600 * convert_url() to parse it into its components.
604 p = strsav(p, "GET ");
605 p = strsav(p, csp->referrer + 9); /* skip over "Referer: " */
606 p = strsav(p, " HTTP/1.0");
608 parse_http_request(p, rhttp, csp);
610 if (rhttp->cmd == NULL)
613 goto trust_url_not_trusted;
618 *url = dsplit(rhttp->host);
620 /* if splitting the domain fails, punt */
621 if (url->dbuf == NULL) goto trust_url_not_trusted;
623 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
625 if ((t->port == 0) || (t->port == rhttp->port))
627 if ((t->domain[0] == '\0') || domaincmp(t, url) == 0)
629 if ((t->path == NULL) ||
631 (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0)
633 (strncmp(t->path, rhttp->path, t->pathlen) == 0)
637 /* if the URL's referrer is from a trusted referrer, then
638 * add the target spec to the trustfile as an unblocked
639 * domain and return NULL (which means it's OK).
647 if ((fp = fopen(csp->config->trustfile, "a")))
652 h = strsav(h, http->hostport);
658 /* since this path points into a user's home space
659 * be sure to include this spec in the trustfile.
661 if ((p = strchr(p, '/')))
664 h = strsav(h, http->path);
669 free_http_request(rhttp);
671 fprintf(fp, "%s\n", h);
681 trust_url_not_trusted:
682 free_http_request(rhttp);
684 hostport = url_encode(http->hostport);
685 path = url_encode(http->path);
689 refer = url_encode(csp->referrer);
693 refer = url_encode("undefined");
697 n += strlen(hostport);
701 p = (char *)malloc(n);
702 sprintf(p, CTRUST, hostport, path, refer);
711 #endif /* def TRUST_FILES */
715 /*********************************************************************
717 * Function : re_process_buffer
719 * Description : Apply all jobs from the joblist (aka. Perl regexp's) to
720 * the text buffer that's been accumulated in csp->iob->buf
721 * and set csp->content_length to the modified size.
724 * 1 : csp = Current client state (buffers, headers, etc...)
726 * Returns : a pointer to the (newly allocated) modified buffer.
729 *********************************************************************/
730 char *re_process_buffer(struct client_state *csp)
733 int size = csp->iob->eod - csp->iob->cur;
734 char *old=csp->iob->cur, *new = NULL;
735 pcrs_job *job, *joblist;
737 struct file_list *fl;
738 struct re_filterfile_spec *b;
740 /* Sanity first ;-) */
746 if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
748 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
752 joblist = b->joblist;
755 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
756 csp->http->hostport, csp->http->path, size);
758 /* Apply all jobs from the joblist */
759 for (job = joblist; NULL != job; job = job->next)
761 hits += pcrs_exec_substitution(job, old, size, &new, &size);
762 if (old != csp->iob->cur) free(old);
766 log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
768 csp->content_length = size;
770 /* fwiw, reset the iob */
775 #endif /* def PCRS */
778 /*********************************************************************
780 * Function : url_actions
782 * Description : Gets the actions for this URL.
785 * 1 : http = http_request request for blocked URLs
786 * 2 : csp = Current client state (buffers, headers, etc...)
790 *********************************************************************/
791 void url_actions(struct http_request *http,
792 struct client_state *csp)
794 struct file_list *fl;
795 struct url_actions *b;
797 init_current_action(csp->action);
799 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
804 apply_url_actions(csp->action, http, b);
808 /*********************************************************************
810 * Function : apply_url_actions
812 * Description : Applies a list of URL actions.
815 * 1 : action = Destination.
816 * 2 : http = Current URL
817 * 3 : b = list of URL actions to apply
821 *********************************************************************/
822 void apply_url_actions(struct current_action_spec *action,
823 struct http_request *http,
824 struct url_actions *b)
826 struct url_spec url[1];
830 /* Should never happen */
834 *url = dsplit(http->host);
836 /* if splitting the domain fails, punt */
837 if (url->dbuf == NULL)
842 for (b = b->next; NULL != b; b = b->next)
844 if ((b->url->port == 0) || (b->url->port == http->port))
846 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
848 if ((b->url->path == NULL) ||
850 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
852 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
856 merge_current_action(action, b->action);
867 /*********************************************************************
869 * Function : forward_url
871 * Description : Should we forward this to another proxy?
874 * 1 : http = http_request request for current URL
875 * 2 : csp = Current client state (buffers, headers, etc...)
877 * Returns : Pointer to forwarding information.
879 *********************************************************************/
880 const struct forward_spec * forward_url(struct http_request *http,
881 struct client_state *csp)
883 static const struct forward_spec fwd_default[1] = { 0 }; /* All zeroes */
884 struct forward_spec *fwd = csp->config->forward;
885 struct url_spec url[1];
892 *url = dsplit(http->host);
894 /* if splitting the domain fails, punt */
895 if (url->dbuf == NULL)
902 if ((fwd->url->port == 0) || (fwd->url->port == http->port))
904 if ((fwd->url->domain[0] == '\0') || (domaincmp(fwd->url, url) == 0))
906 if ((fwd->url->path == NULL) ||
908 (regexec(fwd->url->preg, http->path, 0, NULL, 0) == 0)
910 (strncmp(fwd->url->path, http->path, fwd->url->pathlen) == 0)
930 /*********************************************************************
934 * Description : Takes a domain and returns a pointer to a url_spec
935 * structure populated with dbuf, dcnt and dvec. The
936 * other fields in the structure that is returned are zero.
939 * 1 : domain = a URL address
941 * Returns : url_spec structure populated with dbuf, dcnt and dvec.
943 *********************************************************************/
944 struct url_spec dsplit(char *domain)
946 struct url_spec ret[1];
951 memset(ret, '\0', sizeof(*ret));
953 if (domain[strlen(domain) - 1] == '.')
955 ret->unanchored |= ANCHOR_RIGHT;
957 if (domain[0] == '.')
959 ret->unanchored |= ANCHOR_LEFT;
962 ret->dbuf = strdup(domain);
964 /* map to lower case */
965 for (p = ret->dbuf; *p ; p++) *p = tolower(*p);
967 /* split the domain name into components */
968 ret->dcnt = ssplit(ret->dbuf, ".", v, SZ(v), 1, 1);
972 memset(ret, '\0', sizeof(ret));
976 /* save a copy of the pointers in dvec */
977 size = ret->dcnt * sizeof(*ret->dvec);
979 if ((ret->dvec = (char **)malloc(size)))
981 memcpy(ret->dvec, v, size);
990 /*********************************************************************
992 * Function : simple_domaincmp
994 * Description : Domain-wise Compare fqdn's. The comparison is
995 * both left- and right-anchored. The individual
996 * domain names are compared with trivialmatch().
997 * This is only used by domaincmp.
1000 * 1 : pv = array of patterns to compare
1001 * 2 : fv = array of domain components to compare
1002 * 3 : len = length of the arrays (both arrays are the
1003 * same length - if they weren't, it couldn't
1004 * possibly be a match).
1006 * Returns : 0 => domains are equivalent, else no match.
1008 *********************************************************************/
1009 static int simple_domaincmp(char **pv, char **fv, int len)
1013 for (n = 0; n < len; n++)
1015 if (simplematch(pv[n], fv[n]))
1025 /*********************************************************************
1027 * Function : domaincmp
1029 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
1030 * pattern->unachored, the comparison is un-, left-,
1031 * right-anchored, or both.
1032 * The individual domain names are compared with
1036 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1037 * 2 : fqdn = domain name against which the patterns are compared.
1039 * Returns : 0 => domains are equivalent, else no match.
1041 *********************************************************************/
1042 int domaincmp(struct url_spec *pattern, struct url_spec *fqdn)
1044 char **pv, **fv; /* vectors */
1046 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
1048 plen = pattern->dcnt;
1053 /* fqdn is too short to match this pattern */
1060 if (unanchored == ANCHOR_LEFT)
1065 * Convert this into a fully anchored pattern with
1066 * the fqdn and pattern the same length
1068 fv += (flen - plen); /* flen - plen >= 0 due to check above */
1069 return simple_domaincmp(pv, fv, plen);
1071 else if (unanchored == 0)
1073 /* Fully anchored, check length */
1078 return simple_domaincmp(pv, fv, plen);
1080 else if (unanchored == ANCHOR_RIGHT)
1082 /* Left anchored, ignore all extra in fqdn */
1083 return simple_domaincmp(pv, fv, plen);
1089 int maxn = flen - plen;
1090 for (n = 0; n <= maxn; n++)
1092 if (!simple_domaincmp(pv, fv, plen))
1097 * Doesn't match from start of fqdn
1098 * Try skipping first part of fqdn