1 const char filters_rcs[] = "$Id: filters.c,v 1.6 2001/05/26 00:28:36 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/filters.c,v $
6 * Purpose : Declares functions to parse/crunch headers and pages.
7 * Functions declared include:
8 * `acl_addr', `add_stats', `block_acl', `block_imageurl',
9 * `block_url', `url_permissions', `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.6 2001/05/26 00:28:36 jongfoster
42 * Automatic reloading of config file.
43 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
44 * Most of the global variables have been moved to a new
45 * struct configuration_spec, accessed through csp->config->globalname
46 * Most of the globals remaining are used by the Win32 GUI.
48 * Revision 1.5 2001/05/25 22:34:30 jongfoster
51 * Revision 1.4 2001/05/22 18:46:04 oes
53 * - Enabled filtering banners by size rather than URL
54 * by adding patterns that replace all standard banner
55 * sizes with the "Junkbuster" gif to the re_filterfile
57 * - Enabled filtering WebBugs by providing a pattern
58 * which kills all 1x1 images
60 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
61 * which is selected by the (nonstandard and therefore
62 * capital) letter 'U' in the option string.
63 * It causes the quantifiers to be ungreedy by default.
64 * Appending a ? turns back to greedy (!).
66 * - Added a new interceptor ijb-send-banner, which
67 * sends back the "Junkbuster" gif. Without imagelist or
68 * MSIE detection support, or if tinygif = 1, or the
69 * URL isn't recognized as an imageurl, a lame HTML
70 * explanation is sent instead.
72 * - Added new feature, which permits blocking remote
73 * script redirects and firing back a local redirect
75 * The feature is conditionally compiled, i.e. it
76 * can be disabled with --disable-fast-redirects,
77 * plus it must be activated by a "fast-redirects"
78 * line in the config file, has its own log level
79 * and of course wants to be displayed by show-proxy-args
80 * Note: Boy, all the #ifdefs in 1001 locations and
81 * all the fumbling with configure.in and acconfig.h
82 * were *way* more work than the feature itself :-(
84 * - Because a generic redirect template was needed for
85 * this, tinygif = 3 now uses the same.
87 * - Moved GIFs, and other static HTTP response templates
92 * - Removed some >400 CRs again (Jon, you really worked
95 * Revision 1.3 2001/05/20 16:44:47 jongfoster
96 * Removing last hardcoded JunkBusters.com URLs.
98 * Revision 1.2 2001/05/20 01:21:20 jongfoster
99 * Version 2.9.4 checkin.
100 * - Merged popupfile and cookiefile, and added control over PCRS
101 * filtering, in new "permissionsfile".
102 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
103 * file error you now get a message box (in the Win32 GUI) rather
104 * than the program exiting with no explanation.
105 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
107 * - Removed tabs from "config"
108 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
109 * - Bumped up version number.
111 * Revision 1.1.1.1 2001/05/15 13:58:52 oes
112 * Initial import of version 2.9.3 source tree
115 *********************************************************************/
121 #include <sys/types.h>
128 #include <netinet/in.h>
130 #include <winsock2.h>
137 #include "showargs.h"
141 #include "jbsockets.h"
143 #include "jbsockets.h"
149 const char filters_h_rcs[] = FILTERS_H_VERSION;
151 /* Fix a problem with Solaris. There should be no effect on other
153 * Solaris's isspace() is a macro which uses it's argument directly
154 * as an array index. Therefore we need to make sure that high-bit
155 * characters generate +ve values, and ideally we also want to make
156 * the argument match the declared parameter type of "int".
158 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
161 static const char CBLOCK[] =
163 "HTTP/1.0 403 Request for blocked URL\n"
164 #else /* ifndef AMIGA */
165 "HTTP/1.0 202 Request for blocked URL\n"
166 #endif /* ndef AMIGA */
168 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
169 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
170 "Content-Type: text/html\n\n"
173 "<title>Internet Junkbuster: Request for blocked URL</title>\n"
179 "<p align=center>Your request for <b>%s%s</b><br>\n"
180 "was blocked because it matches the following pattern "
181 "in the blockfile: <b>%s</b>\n</p>"
183 "<p align=center><a href=\"http://%s" FORCE_PREFIX
184 "%s\">Go there anyway.</a></p>"
185 #endif /* def FORCE_LOAD */
190 static const char CTRUST[] =
192 "HTTP/1.0 403 Request for untrusted URL\n"
193 #else /* ifndef AMIGA */
194 "HTTP/1.0 202 Request for untrusted URL\n"
195 #endif /* ndef AMIGA */
197 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
198 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
199 "Content-Type: text/html\n\n"
202 "<title>Internet Junkbuster: Request for untrusted URL</title>\n"
206 "<a href=http://internet.junkbuster.com/ij-untrusted-url?%s+%s+%s>"
212 #endif /* def TRUST_FILES */
216 /*********************************************************************
218 * Function : block_acl
220 * Description : Block this request?
221 * Decide yes or no based on ACL file.
224 * 1 : dst = The proxy or gateway address this is going to.
225 * Or NULL to check all possible targets.
226 * 2 : csp = Current client state (buffers, headers, etc...)
227 * Also includes the client IP address.
229 * Returns : 0 = FALSE (don't block) and 1 = TRUE (do block)
231 *********************************************************************/
232 int block_acl(struct access_control_addr *dst,
233 struct client_state *csp)
235 struct file_list *fl;
236 struct access_control_list *a, *acl;
238 /* if not using an access control list, then permit the connection */
239 if (((fl = csp->alist) == NULL) ||
240 ((acl = (struct access_control_list *) fl->f) == NULL))
245 /* search the list */
246 for (a = acl->next ; a ; a = a->next)
248 if ((csp->ip_addr_long & a->src->mask) == a->src->addr)
252 /* Just want to check if they have any access */
253 if (a->action == ACL_PERMIT)
258 else if ( ((dst->addr & a->dst->mask) == a->dst->addr)
259 && ((dst->port == a->dst->port) || (a->dst->port == 0)))
261 if (a->action == ACL_PERMIT)
278 /*********************************************************************
280 * Function : acl_addr
282 * Description : Called from `load_aclfile'. FIXME: I can't say more.
285 * 1 : aspec = (what?)
288 * Returns : 0 => Ok, everything else is an error.
290 *********************************************************************/
291 int acl_addr(char *aspec, struct access_control_addr *aca)
293 int i, masklength, port;
299 if ((p = strchr(aspec, '/')))
303 if (ijb_isdigit(*p) == 0)
307 masklength = atoi(p);
310 if ((masklength < 0) || (masklength > 32))
315 if ((p = strchr(aspec, ':')))
319 if (ijb_isdigit(*p) == 0)
328 aca->addr = ntohl(resolve_hostname_to_ip(aspec));
332 log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec);
336 /* build the netmask */
338 for (i=1; i <= masklength ; i++)
340 aca->mask |= (1 << (32 - i));
343 /* now mask off the host portion of the ip address
344 * (i.e. save on the network portion of the address).
346 aca->addr = aca->addr & aca->mask;
351 #endif /* def ACL_FILES */
354 /*********************************************************************
356 * Function : block_url
358 * Description : Called from `chat'. Check to see if we need to block this.
361 * 1 : http = http_request request to "check" for blocked
362 * 2 : csp = Current client state (buffers, headers, etc...)
364 * Returns : NULL => unblocked, else string to HTML block description.
366 *********************************************************************/
367 char *block_url(struct http_request *http, struct client_state *csp)
369 struct file_list *fl;
370 struct block_spec *b;
371 struct url_spec url[1];
375 if (((fl = csp->blist) == NULL) || ((b = fl->f) == NULL))
380 *url = dsplit(http->host);
382 /* if splitting the domain fails, punt */
383 if (url->dbuf == NULL) return(NULL);
385 for (b = b->next; b ; b = b->next)
387 if ((b->url->port == 0) || (b->url->port == http->port))
389 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
391 if ((b->url->path == NULL) ||
393 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
395 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
402 if (b->reject == 0) return(NULL);
405 n += strlen(http->hostport);
406 n += strlen(http->path);
407 n += strlen(b->url->spec);
409 n += strlen(http->hostport);
410 n += strlen(http->path);
411 #endif /* def FORCE_LOAD */
413 p = (char *)malloc(n);
416 sprintf(p, CBLOCK, http->hostport, http->path, b->url->spec, http->hostport, http->path);
418 sprintf(p, CBLOCK, http->hostport, http->path, b->url->spec);
419 #endif /* def FORCE_LOAD */
433 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
434 /*********************************************************************
436 * Function : block_imageurl
438 * Description : Given a URL which is blocked, decide whether to
439 * send the "blocked" image or HTML.
442 * 1 : http = URL to check.
443 * 2 : csp = Current client state (buffers, headers, etc...)
445 * Returns : True (nonzero) if URL is in image list, false (0)
448 *********************************************************************/
449 int block_imageurl(struct http_request *http, struct client_state *csp)
451 #ifdef DETECT_MSIE_IMAGES
452 if ((csp->accept_types
453 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
454 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE))
458 else if ((csp->accept_types
459 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
460 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_HTML))
466 #if defined(USE_IMAGE_LIST)
467 return block_imageurl_using_imagelist(http, csp);
469 /* Don't know - assume HTML */
473 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
476 #ifdef USE_IMAGE_LIST
477 /*********************************************************************
479 * Function : block_imageurl
481 * Description : Test if a URL is in the imagelist.
484 * 1 : http = URL to check.
485 * 2 : csp = Current client state (buffers, headers, etc...)
487 * Returns : True (nonzero) if URL is in image list, false (0)
490 *********************************************************************/
491 int block_imageurl_using_imagelist(struct http_request *http, struct client_state *csp)
493 struct file_list *fl;
494 struct block_spec *b;
495 struct url_spec url[1];
497 if (((fl = csp->ilist) == NULL) || ((b = fl->f) == NULL))
502 *url = dsplit(http->host);
504 /* if splitting the domain fails, punt */
505 if (url->dbuf == NULL) return(0);
507 for (b = b->next; b ; b = b->next)
510 if ((b->url->port == 0) || (b->url->port == http->port))
512 /* port matches, check domain */
513 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
515 /* domain matches, check path */
516 if ((b->url->path == NULL) ||
518 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
520 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
528 if (b->reject == 0) return(0);
540 #endif /* def USE_IMAGE_LIST */
544 /*********************************************************************
546 * Function : re_process_buffer
548 * Description : Apply all jobs from the joblist (aka. Perl regexp's) to
549 * the text buffer that's been accumulated in csp->iob->buf.
550 * Then, write the modified buffer out to the client
551 * (Maybe this should happen from jcc.c via flush_socket
552 * for better readability).
555 * 1 : csp = Current client state (buffers, headers, etc...)
559 *********************************************************************/
560 void re_process_buffer(struct client_state *csp)
563 int size = csp->iob->eod - csp->iob->cur;
564 char *old=csp->iob->cur, *new = NULL;
565 pcrs_job *job, *joblist;
567 struct file_list *fl;
568 struct re_filterfile_spec *b;
570 /* Sanity first ;-) */
576 if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
578 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
582 joblist = b->joblist;
585 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
586 csp->http->hostport, csp->http->path, size);
588 /* Apply all jobs from the joblist */
589 for (job = joblist; NULL != job; job = job->next)
591 hits += pcrs_exec_substitution(job, old, size, &new, &size);
592 if (old != csp->iob->cur) free(old);
596 log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
598 if (write_socket(csp->cfd, old, size) != size)
600 log_error(LOG_LEVEL_ERROR, "write to client failed.");
603 /* fwiw, reset the iob */
609 #endif /* def PCRS */
613 /*********************************************************************
615 * Function : trust_url
617 * Description : Should we "trust" this URL? See "trustfile" line in config.
620 * 1 : http = http_request request for requested URL
621 * 2 : csp = Current client state (buffers, headers, etc...)
623 * Returns : NULL => trusted, else string to HTML "untrusted" description.
625 *********************************************************************/
626 char *trust_url(struct http_request *http, struct client_state *csp)
628 struct file_list *fl;
629 struct block_spec *b;
630 struct url_spec url[1], **tl, *t;
632 char *hostport, *path, *refer;
633 struct http_request rhttp[1];
636 if (((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL))
641 *url = dsplit(http->host);
643 /* if splitting the domain fails, punt */
644 if (url->dbuf == NULL) return(NULL);
646 memset(rhttp, '\0', sizeof(*rhttp));
648 for (b = b->next; b ; b = b->next)
650 if ((b->url->port == 0) || (b->url->port == http->port))
652 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
654 if ((b->url->path == NULL) ||
656 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
658 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
665 if (b->reject == 0) return(NULL);
667 hostport = url_encode(http->hostport);
668 path = url_encode(http->path);
672 refer = url_encode(csp->referrer);
676 refer = url_encode("undefined");
680 n += strlen(hostport);
684 p = (char *)malloc(n);
686 sprintf(p, CTRUST, hostport, path, refer);
701 if ((csp->referrer == NULL)|| (strlen(csp->referrer) <= 9))
703 /* no referrer was supplied */
704 goto trust_url_not_trusted;
707 /* forge a URL from the referrer so we can use
708 * convert_url() to parse it into its components.
712 p = strsav(p, "GET ");
713 p = strsav(p, csp->referrer + 9); /* skip over "Referer: " */
714 p = strsav(p, " HTTP/1.0");
716 parse_http_request(p, rhttp, csp);
718 if (rhttp->cmd == NULL)
721 goto trust_url_not_trusted;
726 *url = dsplit(rhttp->host);
728 /* if splitting the domain fails, punt */
729 if (url->dbuf == NULL) goto trust_url_not_trusted;
731 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
733 if ((t->port == 0) || (t->port == rhttp->port))
735 if ((t->domain[0] == '\0') || domaincmp(t, url) == 0)
737 if ((t->path == NULL) ||
739 (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0)
741 (strncmp(t->path, rhttp->path, t->pathlen) == 0)
745 /* if the URL's referrer is from a trusted referrer, then
746 * add the target spec to the trustfile as an unblocked
747 * domain and return NULL (which means it's OK).
755 if ((fp = fopen(csp->config->trustfile, "a")))
760 h = strsav(h, http->hostport);
766 /* since this path points into a user's home space
767 * be sure to include this spec in the trustfile.
769 if ((p = strchr(p, '/')))
772 h = strsav(h, http->path);
777 free_http_request(rhttp);
779 fprintf(fp, "%s\n", h);
789 trust_url_not_trusted:
790 free_http_request(rhttp);
792 hostport = url_encode(http->hostport);
793 path = url_encode(http->path);
797 refer = url_encode(csp->referrer);
801 refer = url_encode("undefined");
805 n += strlen(hostport);
809 p = (char *)malloc(n);
810 sprintf(p, CTRUST, hostport, path, refer);
819 #endif /* def TRUST_FILES */
822 /*********************************************************************
824 * Function : intercept_url
826 * Description : checks the URL `basename' against a list of URLs to
827 * snarf. If it matches, it calls the associated function
828 * which returns an HTML page to send back to the client.
829 * Right now, we snarf:
830 * "show-proxy-args", and
831 * "ij-untrusted-url" (optional w/TRUST_FILES)
834 * 1 : http = http_request request, check `basename's of blocklist
835 * 2 : csp = Current client state (buffers, headers, etc...)
837 * Returns : NULL for no recognized URLs, or an HTML description page.
839 *********************************************************************/
840 char *intercept_url(struct http_request *http, struct client_state *csp)
843 const struct interceptors *v;
845 basename = strrchr(http->path, '/');
847 if (basename == NULL) return(NULL);
849 basename ++; /* first char past the last slash */
853 for (v = intercept_patterns; v->str; v++)
855 if (strncmp(basename, v->str, v->len) == 0)
857 return((v->interceptor)(http, csp));
866 #ifdef FAST_REDIRECTS
867 /*********************************************************************
869 * Function : redirect_url
871 * Description : Checks for redirection URLs and returns a HTTP redirect
872 * to the destination URL.
875 * 1 : http = http_request request, check `basename's of blocklist
876 * 2 : csp = Current client state (buffers, headers, etc...)
878 * Returns : NULL if URL was clean, HTTP redirect otherwise.
880 *********************************************************************/
881 char *redirect_url(struct http_request *http, struct client_state *csp)
885 p = q = csp->http->path;
886 log_error(LOG_LEVEL_REDIRECTS, "checking path: %s", p);
888 /* find the last URL encoded in the request */
889 while (p = strstr(p, "http://"))
894 /* if there was any, generate and return a HTTP redirect */
895 if (q != csp->http->path)
897 log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
899 p = (char *)malloc(strlen(HTTP_REDIRECT_TEMPLATE) + strlen(q));
900 sprintf(p, HTTP_REDIRECT_TEMPLATE, q);
909 #endif /* def FAST_REDIRECTS */
911 /*********************************************************************
913 * Function : url_permissions
915 * Description : Gets the permissions for this URL.
918 * 1 : http = http_request request for blocked URLs
919 * 2 : csp = Current client state (buffers, headers, etc...)
921 * Returns : permissions bitmask specifiying what this URL can do.
922 * If not on list, will be default_permissions.
924 *********************************************************************/
925 int url_permissions(struct http_request *http, struct client_state *csp)
927 struct file_list *fl;
928 struct permissions_spec *b;
929 struct url_spec url[1];
931 if (((fl = csp->permissions_list) == NULL) || ((b = fl->f) == NULL))
933 return(csp->config->default_permissions);
936 *url = dsplit(http->host);
938 /* if splitting the domain fails, punt */
939 if (url->dbuf == NULL)
941 return(csp->config->default_permissions);
944 for (b = b->next; NULL != b; b = b->next)
946 if ((b->url->port == 0) || (b->url->port == http->port))
948 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
950 if ((b->url->path == NULL) ||
952 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
954 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
960 return(b->permissions);
968 return(csp->config->default_permissions);
973 /*********************************************************************
975 * Function : forward_url
977 * Description : Should we forward this to another proxy?
980 * 1 : http = http_request request for current URL
981 * 2 : csp = Current client state (buffers, headers, etc...)
983 * Returns : Return gw_default for no forward match,
984 * else a gateway pointer to a specific forwarding proxy.
986 *********************************************************************/
987 const struct gateway *forward_url(struct http_request *http, struct client_state *csp)
989 struct file_list *fl;
990 struct forward_spec *b;
991 struct url_spec url[1];
993 if (((fl = csp->flist) == NULL) || ((b = fl->f) == NULL))
998 *url = dsplit(http->host);
1000 /* if splitting the domain fails, punt */
1001 if (url->dbuf == NULL) return(gw_default);
1003 for (b = b->next; b ; b = b->next)
1005 if ((b->url->port == 0) || (b->url->port == http->port))
1007 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
1009 if ((b->url->path == NULL) ||
1011 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
1013 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
1032 /*********************************************************************
1036 * Description : Takes a domain and returns a pointer to a url_spec
1037 * structure populated with dbuf, dcnt and dvec. The
1038 * other fields in the structure that is returned are zero.
1041 * 1 : domain = a URL address
1043 * Returns : url_spec structure populated with dbuf, dcnt and dvec.
1045 *********************************************************************/
1046 struct url_spec dsplit(char *domain)
1048 struct url_spec ret[1];
1053 memset(ret, '\0', sizeof(*ret));
1055 if ((p = strrchr(domain, '.')))
1063 ret->dbuf = strdup(domain);
1065 /* map to lower case */
1066 for (p = ret->dbuf; *p ; p++) *p = tolower(*p);
1068 /* split the domain name into components */
1069 ret->dcnt = ssplit(ret->dbuf, ".", v, SZ(v), 1, 1);
1073 memset(ret, '\0', sizeof(ret));
1077 /* save a copy of the pointers in dvec */
1078 size = ret->dcnt * sizeof(*ret->dvec);
1080 if ((ret->dvec = (char **)malloc(size)))
1082 memcpy(ret->dvec, v, size);
1090 /*********************************************************************
1092 * Function : domaincmp
1094 * Description : Compare domain names.
1095 * domaincmp("a.b.c" , "a.b.c") => 0 (MATCH)
1096 * domaincmp("a*.b.c", "a.b.c") => 0 (MATCH)
1097 * domaincmp("b.c" , "a.b.c") => 0 (MATCH)
1098 * domaincmp("" , "a.b.c") => 0 (MATCH)
1101 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1102 * 2 : fqdn = domain name against which the patterns are compared.
1104 * Returns : 0 => domains are equivalent, else no match.
1106 *********************************************************************/
1107 int domaincmp(struct url_spec *pattern, struct url_spec *fqdn)
1109 char **pv, **fv; /* vectors */
1110 int pn, fn; /* counters */
1111 char *p, *f; /* chars */
1119 while ((pn > 0) && (fn > 0))
1124 while (*p && *f && (*p == tolower(*f)))
1129 if ((*p != tolower(*f)) && (*p != '*')) return(1);
1132 if (pn > 0) return(1);
1139 /* intercept functions */
1141 /*********************************************************************
1143 * Function : show_proxy_args
1145 * Description : This "crunch"es "http:/any.thing/show-proxy-args" and
1146 * returns a web page describing the current status of IJB.
1149 * 1 : http = ignored
1150 * 2 : csp = Current client state (buffers, headers, etc...)
1152 * Returns : A string that contains the current status of IJB.
1154 *********************************************************************/
1155 char *show_proxy_args(struct http_request *http, struct client_state *csp)
1159 #ifdef SPLIT_PROXY_ARGS
1163 const char * filename = NULL;
1164 const char * file_description = NULL;
1165 char * query_string = strrchr(http->path, '?');
1166 char which_file = '\0';
1169 if (query_string != NULL)
1171 /* first char past the last '?' (maybe '\0')*/
1172 which_file = query_string[1];
1179 filename = csp->blist->filename;
1180 file_description = "Block List";
1184 if (csp->permissions_list)
1186 filename = csp->permissions_list->filename;
1187 file_description = "Permissions List";
1193 filename = csp->flist->filename;
1194 file_description = "Forward List";
1202 filename = csp->alist->filename;
1203 file_description = "Access Control List";
1206 #endif /* def ACL_FILES */
1208 #ifdef USE_IMAGE_LIST
1212 filename = csp->ilist->filename;
1213 file_description = "Image List";
1216 #endif /* def USE_IMAGE_LIST */
1222 filename = csp->rlist->filename;
1223 file_description = "RE Filter List";
1226 #endif /* def PCRS */
1232 filename = csp->tlist->filename;
1233 file_description = "Trust List";
1236 #endif /* def TRUST_FILES */
1241 /* Display specified file */
1242 /* FIXME: Add HTTP headers so this isn't cached */
1245 "Server: IJ/" VERSION "\n"
1246 "Content-type: text/html\n"
1247 "Pragma: no-cache\n"
1248 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1249 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1254 "<title>Internet Junkbuster Proxy Status - ");
1255 s = strsav(s, file_description);
1259 "<body bgcolor=\"#f8f8f0\" link=\"#000078\" alink=\"#ff0022\" vlink=\"#787878\">\n"
1261 "<h1>" BANNER "\n");
1262 s = strsav(s, file_description);
1265 "<p><a href=\"show-proxy-args\">Back to proxy status</a></p>\n"
1267 s = strsav(s, file_description);
1270 "Contents of file "<code>");
1271 p = html_encode(filename);
1275 "</code>":<br>\n"
1279 if ((fp = fopen(filename, "r")) == NULL)
1281 s = strsav(s, "</pre><h1>ERROR OPENING FILE!</h1><pre>");
1285 while (fgets(buf, sizeof(buf), fp))
1287 p = html_encode(buf);
1292 s = strsav(s, "<br>");
1301 "<p><a href=\"show-proxy-args\">Back to proxy status</a></p>\n"
1303 "<small><small><p>\n"
1304 "The " BANNER " Proxy - \n"
1305 "<a href=\"" HOME_PAGE_URL "\">" HOME_PAGE_URL "</a>\n"
1307 "</body></html>\n");
1310 #endif /* def SPLIT_PROXY_ARGS */
1312 s = strsav(s, csp->config->proxy_args->header);
1313 s = strsav(s, csp->config->proxy_args->invocation);
1316 #endif /* def STATISTICS */
1317 s = strsav(s, csp->config->proxy_args->gateways);
1319 #ifdef SPLIT_PROXY_ARGS
1321 "<h2>The following files are in use:</h2>\n"
1322 "<p>(Click a filename to view it)</p>\n"
1327 s = strsav(s, "<li>Block List: <a href=\"show-proxy-args?block\"><code>");
1328 s = strsav(s, csp->blist->filename);
1329 s = strsav(s, "</code></a></li>\n");
1332 if (csp->permissions_list)
1334 s = strsav(s, "<li>Permissions List: <a href=\"show-proxy-args?permit\"><code>");
1335 s = strsav(s, csp->permissions_list->filename);
1336 s = strsav(s, "</code></a></li>\n");
1341 s = strsav(s, "<li>Forward List: <a href=\"show-proxy-args?forward\"><code>");
1342 s = strsav(s, csp->flist->filename);
1343 s = strsav(s, "</code></a></li>\n");
1349 s = strsav(s, "<li>Access Control List: <a href=\"show-proxy-args?acl\"><code>");
1350 s = strsav(s, csp->alist->filename);
1351 s = strsav(s, "</code></a></li>\n");
1353 #endif /* def ACL_FILES */
1355 #ifdef USE_IMAGE_LIST
1358 s = strsav(s, "<li>Image List: <a href=\"show-proxy-args?image\"><code>");
1359 s = strsav(s, csp->ilist->filename);
1360 s = strsav(s, "</code></a></li>\n");
1362 #endif /* def USE_IMAGE_LIST */
1367 s = strsav(s, "<li>RE Filter List: <a href=\"show-proxy-args?re\"><code>");
1368 s = strsav(s, csp->rlist->filename);
1369 s = strsav(s, "</code></a></li>\n");
1371 #endif /* def PCRS */
1376 s = strsav(s, "<li>Trust List: <a href=\"show-proxy-args?trust\"><code>");
1377 s = strsav(s, csp->tlist->filename);
1378 s = strsav(s, "</code></a></li>\n");
1380 #endif /* def TRUST_FILES */
1382 s = strsav(s, "</ul>");
1384 #else /* ifndef SPLIT_PROXY_ARGS */
1387 s = strsav(s, csp->blist->proxy_args);
1392 s = strsav(s, csp->clist->proxy_args);
1397 s = strsav(s, csp->flist->proxy_args);
1403 s = strsav(s, csp->alist->proxy_args);
1405 #endif /* def ACL_FILES */
1407 #ifdef USE_IMAGE_LIST
1410 s = strsav(s, csp->ilist->proxy_args);
1412 #endif /* def USE_IMAGE_LIST */
1417 s = strsav(s, csp->rlist->proxy_args);
1419 #endif /* def PCRS */
1424 s = strsav(s, csp->tlist->proxy_args);
1426 #endif /* def TRUST_FILES */
1428 #endif /* ndef SPLIT_PROXY_ARGS */
1430 s = strsav(s, csp->config->proxy_args->trailer);
1437 /*********************************************************************
1439 * Function : ijb_send_banner
1441 * Description : This "crunch"es "http:/any.thing/ijb-send-banner and
1442 * thus triggers sending the image in jcc.c:chat.
1443 * For the unlikely case, that the imagefile/MSIE
1444 * mechanism is not used, or tinygif = 0, a page
1445 * describing the reson of the interception is generated.
1448 * 1 : http = http_request request for crunched URL
1449 * 2 : csp = Current client state (buffers, headers, etc...)
1451 * Returns : A string that contains why this was intercepted.
1453 *********************************************************************/
1454 char *ijb_send_banner(struct http_request *http, struct client_state *csp)
1456 return(strdup(CNOBANNER));
1460 /*********************************************************************
1462 * Function : ij_untrusted_url
1464 * Description : This "crunch"es "http:/any.thing/ij-untrusted-url" and
1465 * returns a web page describing why it was untrusted.
1468 * 1 : http = http_request request for crunched URL
1469 * 2 : csp = Current client state (buffers, headers, etc...)
1471 * Returns : A string that contains why this was untrusted.
1473 *********************************************************************/
1474 char *ij_untrusted_url(struct http_request *http, struct client_state *csp)
1477 char *hostport, *path, *refer, *p, *v[9];
1479 struct url_spec **tl, *t;
1482 static const char format[] =
1483 "HTTP/1.0 200 OK\r\n"
1484 "Pragma: no-cache\n"
1485 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1486 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1487 "Content-Type: text/html\n\n"
1490 "<title>Internet Junkbuster: Request for untrusted URL</title>\n"
1496 "The " BANNER " Proxy "
1497 "<A href=\"" HOME_PAGE_URL "\">"
1498 "(" HOME_PAGE_URL ") </A>"
1499 "intercepted the request for %s%s\n"
1500 "because the URL is not trusted.\n"
1503 if ((n = ssplit(http->path, "?+", v, SZ(v), 0, 0)) == 4)
1505 hostport = url_decode(v[1]);
1506 path = url_decode(v[2]);
1507 refer = url_decode(v[3]);
1511 hostport = strdup("undefined_host");
1512 path = strdup("/undefined_path");
1513 refer = strdup("undefined");
1517 n += strlen(hostport);
1520 if ((p = (char *)malloc(n)))
1522 sprintf(p, format, hostport, path);
1525 strsav(p, "The referrer in this request was <strong>");
1527 strsav(p, "</strong><br>\n");
1533 p = strsav(p, "<h3>The following referrers are trusted</h3>\n");
1535 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
1537 sprintf(buf, "%s<br>\n", t->spec);
1541 if (csp->config->trust_info->next)
1547 "You can learn more about what this means "
1548 "and what you may be able to do about it by "
1549 "reading the following documents:<br>\n"
1555 for (l = csp->config->trust_info->next; l ; l = l->next)
1558 "<li> <a href=%s>%s</a><br>\n",
1563 p = strsav(p, "</ol>\n");
1566 p = strsav(p, "</body>\n" "</html>\n");
1571 #endif /* def TRUST_FILES */
1575 /*********************************************************************
1577 * Function : add_stats
1579 * Description : Statistics function of JB. Called by `show_proxy_args'.
1582 * 1 : s = string that holds the proxy args description page
1584 * Returns : A pointer to the descriptive status web page.
1586 *********************************************************************/
1587 char *add_stats(char *s)
1590 * Output details of the number of requests rejected and
1591 * accepted. This is switchable in the junkbuster config.
1592 * Does nothing if this option is not enabled.
1595 float perc_rej; /* Percentage of http requests rejected */
1597 int local_urls_read = urls_read;
1598 int local_urls_rejected = urls_rejected;
1601 * Need to alter the stats not to include the fetch of this
1604 * Can't do following thread safely! doh!
1607 * urls_rejected--; * This will be incremented subsequently *
1610 s = strsav(s,"<h2>Statistics for this " BANNER ":</h2>\n");
1612 if (local_urls_read == 0)
1615 s = strsav(s,"No activity so far!\n");
1621 perc_rej = (float)local_urls_rejected * 100.0F /
1622 (float)local_urls_read;
1625 "%d requests received, %d filtered "
1628 local_urls_rejected, perc_rej);
1630 s = strsav(s,out_str);
1635 #endif /* def STATISTICS */