1 const char filters_rcs[] = "$Id: filters.c,v 1.7 2001/05/26 15:26:15 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.7 2001/05/26 15:26:15 jongfoster
42 * ACL feature now provides more security by immediately dropping
43 * connections from untrusted hosts.
45 * Revision 1.6 2001/05/26 00:28:36 jongfoster
46 * Automatic reloading of config file.
47 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
48 * Most of the global variables have been moved to a new
49 * struct configuration_spec, accessed through csp->config->globalname
50 * Most of the globals remaining are used by the Win32 GUI.
52 * Revision 1.5 2001/05/25 22:34:30 jongfoster
55 * Revision 1.4 2001/05/22 18:46:04 oes
57 * - Enabled filtering banners by size rather than URL
58 * by adding patterns that replace all standard banner
59 * sizes with the "Junkbuster" gif to the re_filterfile
61 * - Enabled filtering WebBugs by providing a pattern
62 * which kills all 1x1 images
64 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
65 * which is selected by the (nonstandard and therefore
66 * capital) letter 'U' in the option string.
67 * It causes the quantifiers to be ungreedy by default.
68 * Appending a ? turns back to greedy (!).
70 * - Added a new interceptor ijb-send-banner, which
71 * sends back the "Junkbuster" gif. Without imagelist or
72 * MSIE detection support, or if tinygif = 1, or the
73 * URL isn't recognized as an imageurl, a lame HTML
74 * explanation is sent instead.
76 * - Added new feature, which permits blocking remote
77 * script redirects and firing back a local redirect
79 * The feature is conditionally compiled, i.e. it
80 * can be disabled with --disable-fast-redirects,
81 * plus it must be activated by a "fast-redirects"
82 * line in the config file, has its own log level
83 * and of course wants to be displayed by show-proxy-args
84 * Note: Boy, all the #ifdefs in 1001 locations and
85 * all the fumbling with configure.in and acconfig.h
86 * were *way* more work than the feature itself :-(
88 * - Because a generic redirect template was needed for
89 * this, tinygif = 3 now uses the same.
91 * - Moved GIFs, and other static HTTP response templates
96 * - Removed some >400 CRs again (Jon, you really worked
99 * Revision 1.3 2001/05/20 16:44:47 jongfoster
100 * Removing last hardcoded JunkBusters.com URLs.
102 * Revision 1.2 2001/05/20 01:21:20 jongfoster
103 * Version 2.9.4 checkin.
104 * - Merged popupfile and cookiefile, and added control over PCRS
105 * filtering, in new "permissionsfile".
106 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
107 * file error you now get a message box (in the Win32 GUI) rather
108 * than the program exiting with no explanation.
109 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
111 * - Removed tabs from "config"
112 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
113 * - Bumped up version number.
115 * Revision 1.1.1.1 2001/05/15 13:58:52 oes
116 * Initial import of version 2.9.3 source tree
119 *********************************************************************/
125 #include <sys/types.h>
132 #include <netinet/in.h>
134 #include <winsock2.h>
141 #include "showargs.h"
145 #include "jbsockets.h"
147 #include "jbsockets.h"
153 const char filters_h_rcs[] = FILTERS_H_VERSION;
155 /* Fix a problem with Solaris. There should be no effect on other
157 * Solaris's isspace() is a macro which uses it's argument directly
158 * as an array index. Therefore we need to make sure that high-bit
159 * characters generate +ve values, and ideally we also want to make
160 * the argument match the declared parameter type of "int".
162 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
165 static const char CBLOCK[] =
167 "HTTP/1.0 403 Request for blocked URL\n"
168 #else /* ifndef AMIGA */
169 "HTTP/1.0 202 Request for blocked URL\n"
170 #endif /* ndef AMIGA */
172 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
173 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
174 "Content-Type: text/html\n\n"
177 "<title>Internet Junkbuster: Request for blocked URL</title>\n"
183 "<p align=center>Your request for <b>%s%s</b><br>\n"
184 "was blocked because it matches the following pattern "
185 "in the blockfile: <b>%s</b>\n</p>"
187 "<p align=center><a href=\"http://%s" FORCE_PREFIX
188 "%s\">Go there anyway.</a></p>"
189 #endif /* def FORCE_LOAD */
194 static const char CTRUST[] =
196 "HTTP/1.0 403 Request for untrusted URL\n"
197 #else /* ifndef AMIGA */
198 "HTTP/1.0 202 Request for untrusted URL\n"
199 #endif /* ndef AMIGA */
201 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
202 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
203 "Content-Type: text/html\n\n"
206 "<title>Internet Junkbuster: Request for untrusted URL</title>\n"
210 "<a href=http://internet.junkbuster.com/ij-untrusted-url?%s+%s+%s>"
216 #endif /* def TRUST_FILES */
220 /*********************************************************************
222 * Function : block_acl
224 * Description : Block this request?
225 * Decide yes or no based on ACL file.
228 * 1 : dst = The proxy or gateway address this is going to.
229 * Or NULL to check all possible targets.
230 * 2 : csp = Current client state (buffers, headers, etc...)
231 * Also includes the client IP address.
233 * Returns : 0 = FALSE (don't block) and 1 = TRUE (do block)
235 *********************************************************************/
236 int block_acl(struct access_control_addr *dst,
237 struct client_state *csp)
239 struct file_list *fl;
240 struct access_control_list *a, *acl;
242 /* if not using an access control list, then permit the connection */
243 if (((fl = csp->alist) == NULL) ||
244 ((acl = (struct access_control_list *) fl->f) == NULL))
249 /* search the list */
250 for (a = acl->next ; a ; a = a->next)
252 if ((csp->ip_addr_long & a->src->mask) == a->src->addr)
256 /* Just want to check if they have any access */
257 if (a->action == ACL_PERMIT)
262 else if ( ((dst->addr & a->dst->mask) == a->dst->addr)
263 && ((dst->port == a->dst->port) || (a->dst->port == 0)))
265 if (a->action == ACL_PERMIT)
282 /*********************************************************************
284 * Function : acl_addr
286 * Description : Called from `load_aclfile' to parse an ACL address.
289 * 1 : aspec = String specifying ACL address.
290 * 2 : aca = struct access_control_addr to fill in.
292 * Returns : 0 => Ok, everything else is an error.
294 *********************************************************************/
295 int acl_addr(char *aspec, struct access_control_addr *aca)
297 int i, masklength, port;
303 if ((p = strchr(aspec, '/')))
307 if (ijb_isdigit(*p) == 0)
311 masklength = atoi(p);
314 if ((masklength < 0) || (masklength > 32))
319 if ((p = strchr(aspec, ':')))
323 if (ijb_isdigit(*p) == 0)
332 aca->addr = ntohl(resolve_hostname_to_ip(aspec));
336 log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec);
340 /* build the netmask */
342 for (i=1; i <= masklength ; i++)
344 aca->mask |= (1 << (32 - i));
347 /* now mask off the host portion of the ip address
348 * (i.e. save on the network portion of the address).
350 aca->addr = aca->addr & aca->mask;
355 #endif /* def ACL_FILES */
358 /*********************************************************************
360 * Function : block_url
362 * Description : Called from `chat'. Check to see if we need to block this.
365 * 1 : http = http_request request to "check" for blocked
366 * 2 : csp = Current client state (buffers, headers, etc...)
368 * Returns : NULL => unblocked, else string to HTML block description.
370 *********************************************************************/
371 char *block_url(struct http_request *http, struct client_state *csp)
373 struct file_list *fl;
374 struct block_spec *b;
375 struct url_spec url[1];
379 if (((fl = csp->blist) == NULL) || ((b = fl->f) == NULL))
384 *url = dsplit(http->host);
386 /* if splitting the domain fails, punt */
387 if (url->dbuf == NULL) return(NULL);
389 for (b = b->next; b ; b = b->next)
391 if ((b->url->port == 0) || (b->url->port == http->port))
393 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
395 if ((b->url->path == NULL) ||
397 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
399 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
406 if (b->reject == 0) return(NULL);
409 n += strlen(http->hostport);
410 n += strlen(http->path);
411 n += strlen(b->url->spec);
413 n += strlen(http->hostport);
414 n += strlen(http->path);
415 #endif /* def FORCE_LOAD */
417 p = (char *)malloc(n);
420 sprintf(p, CBLOCK, http->hostport, http->path, b->url->spec, http->hostport, http->path);
422 sprintf(p, CBLOCK, http->hostport, http->path, b->url->spec);
423 #endif /* def FORCE_LOAD */
437 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
438 /*********************************************************************
440 * Function : block_imageurl
442 * Description : Given a URL which is blocked, decide whether to
443 * send the "blocked" image or HTML.
446 * 1 : http = URL to check.
447 * 2 : csp = Current client state (buffers, headers, etc...)
449 * Returns : True (nonzero) if URL is in image list, false (0)
452 *********************************************************************/
453 int block_imageurl(struct http_request *http, struct client_state *csp)
455 #ifdef DETECT_MSIE_IMAGES
456 if ((csp->accept_types
457 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
458 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE))
462 else if ((csp->accept_types
463 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
464 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_HTML))
470 #if defined(USE_IMAGE_LIST)
471 return block_imageurl_using_imagelist(http, csp);
473 /* Don't know - assume HTML */
477 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
480 #ifdef USE_IMAGE_LIST
481 /*********************************************************************
483 * Function : block_imageurl
485 * Description : Test if a URL is in the imagelist.
488 * 1 : http = URL to check.
489 * 2 : csp = Current client state (buffers, headers, etc...)
491 * Returns : True (nonzero) if URL is in image list, false (0)
494 *********************************************************************/
495 int block_imageurl_using_imagelist(struct http_request *http, struct client_state *csp)
497 struct file_list *fl;
498 struct block_spec *b;
499 struct url_spec url[1];
501 if (((fl = csp->ilist) == NULL) || ((b = fl->f) == NULL))
506 *url = dsplit(http->host);
508 /* if splitting the domain fails, punt */
509 if (url->dbuf == NULL) return(0);
511 for (b = b->next; b ; b = b->next)
514 if ((b->url->port == 0) || (b->url->port == http->port))
516 /* port matches, check domain */
517 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
519 /* domain matches, check path */
520 if ((b->url->path == NULL) ||
522 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
524 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
532 if (b->reject == 0) return(0);
544 #endif /* def USE_IMAGE_LIST */
548 /*********************************************************************
550 * Function : re_process_buffer
552 * Description : Apply all jobs from the joblist (aka. Perl regexp's) to
553 * the text buffer that's been accumulated in csp->iob->buf.
554 * Then, write the modified buffer out to the client
555 * (Maybe this should happen from jcc.c via flush_socket
556 * for better readability).
559 * 1 : csp = Current client state (buffers, headers, etc...)
563 *********************************************************************/
564 void re_process_buffer(struct client_state *csp)
567 int size = csp->iob->eod - csp->iob->cur;
568 char *old=csp->iob->cur, *new = NULL;
569 pcrs_job *job, *joblist;
571 struct file_list *fl;
572 struct re_filterfile_spec *b;
574 /* Sanity first ;-) */
580 if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
582 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
586 joblist = b->joblist;
589 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
590 csp->http->hostport, csp->http->path, size);
592 /* Apply all jobs from the joblist */
593 for (job = joblist; NULL != job; job = job->next)
595 hits += pcrs_exec_substitution(job, old, size, &new, &size);
596 if (old != csp->iob->cur) free(old);
600 log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
602 if (write_socket(csp->cfd, old, size) != size)
604 log_error(LOG_LEVEL_ERROR, "write to client failed.");
607 /* fwiw, reset the iob */
613 #endif /* def PCRS */
617 /*********************************************************************
619 * Function : trust_url
621 * Description : Should we "trust" this URL? See "trustfile" line in config.
624 * 1 : http = http_request request for requested URL
625 * 2 : csp = Current client state (buffers, headers, etc...)
627 * Returns : NULL => trusted, else string to HTML "untrusted" description.
629 *********************************************************************/
630 char *trust_url(struct http_request *http, struct client_state *csp)
632 struct file_list *fl;
633 struct block_spec *b;
634 struct url_spec url[1], **tl, *t;
636 char *hostport, *path, *refer;
637 struct http_request rhttp[1];
640 if (((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL))
645 *url = dsplit(http->host);
647 /* if splitting the domain fails, punt */
648 if (url->dbuf == NULL) return(NULL);
650 memset(rhttp, '\0', sizeof(*rhttp));
652 for (b = b->next; b ; b = b->next)
654 if ((b->url->port == 0) || (b->url->port == http->port))
656 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
658 if ((b->url->path == NULL) ||
660 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
662 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
669 if (b->reject == 0) return(NULL);
671 hostport = url_encode(http->hostport);
672 path = url_encode(http->path);
676 refer = url_encode(csp->referrer);
680 refer = url_encode("undefined");
684 n += strlen(hostport);
688 p = (char *)malloc(n);
690 sprintf(p, CTRUST, hostport, path, refer);
705 if ((csp->referrer == NULL)|| (strlen(csp->referrer) <= 9))
707 /* no referrer was supplied */
708 goto trust_url_not_trusted;
711 /* forge a URL from the referrer so we can use
712 * convert_url() to parse it into its components.
716 p = strsav(p, "GET ");
717 p = strsav(p, csp->referrer + 9); /* skip over "Referer: " */
718 p = strsav(p, " HTTP/1.0");
720 parse_http_request(p, rhttp, csp);
722 if (rhttp->cmd == NULL)
725 goto trust_url_not_trusted;
730 *url = dsplit(rhttp->host);
732 /* if splitting the domain fails, punt */
733 if (url->dbuf == NULL) goto trust_url_not_trusted;
735 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
737 if ((t->port == 0) || (t->port == rhttp->port))
739 if ((t->domain[0] == '\0') || domaincmp(t, url) == 0)
741 if ((t->path == NULL) ||
743 (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0)
745 (strncmp(t->path, rhttp->path, t->pathlen) == 0)
749 /* if the URL's referrer is from a trusted referrer, then
750 * add the target spec to the trustfile as an unblocked
751 * domain and return NULL (which means it's OK).
759 if ((fp = fopen(csp->config->trustfile, "a")))
764 h = strsav(h, http->hostport);
770 /* since this path points into a user's home space
771 * be sure to include this spec in the trustfile.
773 if ((p = strchr(p, '/')))
776 h = strsav(h, http->path);
781 free_http_request(rhttp);
783 fprintf(fp, "%s\n", h);
793 trust_url_not_trusted:
794 free_http_request(rhttp);
796 hostport = url_encode(http->hostport);
797 path = url_encode(http->path);
801 refer = url_encode(csp->referrer);
805 refer = url_encode("undefined");
809 n += strlen(hostport);
813 p = (char *)malloc(n);
814 sprintf(p, CTRUST, hostport, path, refer);
823 #endif /* def TRUST_FILES */
826 /*********************************************************************
828 * Function : intercept_url
830 * Description : checks the URL `basename' against a list of URLs to
831 * snarf. If it matches, it calls the associated function
832 * which returns an HTML page to send back to the client.
833 * Right now, we snarf:
834 * "show-proxy-args", and
835 * "ij-untrusted-url" (optional w/TRUST_FILES)
838 * 1 : http = http_request request, check `basename's of blocklist
839 * 2 : csp = Current client state (buffers, headers, etc...)
841 * Returns : NULL for no recognized URLs, or an HTML description page.
843 *********************************************************************/
844 char *intercept_url(struct http_request *http, struct client_state *csp)
847 const struct interceptors *v;
849 basename = strrchr(http->path, '/');
851 if (basename == NULL) return(NULL);
853 basename ++; /* first char past the last slash */
857 for (v = intercept_patterns; v->str; v++)
859 if (strncmp(basename, v->str, v->len) == 0)
861 return((v->interceptor)(http, csp));
870 #ifdef FAST_REDIRECTS
871 /*********************************************************************
873 * Function : redirect_url
875 * Description : Checks for redirection URLs and returns a HTTP redirect
876 * to the destination URL.
879 * 1 : http = http_request request, check `basename's of blocklist
880 * 2 : csp = Current client state (buffers, headers, etc...)
882 * Returns : NULL if URL was clean, HTTP redirect otherwise.
884 *********************************************************************/
885 char *redirect_url(struct http_request *http, struct client_state *csp)
889 p = q = csp->http->path;
890 log_error(LOG_LEVEL_REDIRECTS, "checking path: %s", p);
892 /* find the last URL encoded in the request */
893 while (p = strstr(p, "http://"))
898 /* if there was any, generate and return a HTTP redirect */
899 if (q != csp->http->path)
901 log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
903 p = (char *)malloc(strlen(HTTP_REDIRECT_TEMPLATE) + strlen(q));
904 sprintf(p, HTTP_REDIRECT_TEMPLATE, q);
913 #endif /* def FAST_REDIRECTS */
915 /*********************************************************************
917 * Function : url_permissions
919 * Description : Gets the permissions for this URL.
922 * 1 : http = http_request request for blocked URLs
923 * 2 : csp = Current client state (buffers, headers, etc...)
925 * Returns : permissions bitmask specifiying what this URL can do.
926 * If not on list, will be default_permissions.
928 *********************************************************************/
929 int url_permissions(struct http_request *http, struct client_state *csp)
931 struct file_list *fl;
932 struct permissions_spec *b;
933 struct url_spec url[1];
935 if (((fl = csp->permissions_list) == NULL) || ((b = fl->f) == NULL))
937 return(csp->config->default_permissions);
940 *url = dsplit(http->host);
942 /* if splitting the domain fails, punt */
943 if (url->dbuf == NULL)
945 return(csp->config->default_permissions);
948 for (b = b->next; NULL != b; b = b->next)
950 if ((b->url->port == 0) || (b->url->port == http->port))
952 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
954 if ((b->url->path == NULL) ||
956 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
958 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
964 return(b->permissions);
972 return(csp->config->default_permissions);
977 /*********************************************************************
979 * Function : forward_url
981 * Description : Should we forward this to another proxy?
984 * 1 : http = http_request request for current URL
985 * 2 : csp = Current client state (buffers, headers, etc...)
987 * Returns : Return gw_default for no forward match,
988 * else a gateway pointer to a specific forwarding proxy.
990 *********************************************************************/
991 const struct gateway *forward_url(struct http_request *http, struct client_state *csp)
993 struct file_list *fl;
994 struct forward_spec *b;
995 struct url_spec url[1];
997 if (((fl = csp->flist) == NULL) || ((b = fl->f) == NULL))
1002 *url = dsplit(http->host);
1004 /* if splitting the domain fails, punt */
1005 if (url->dbuf == NULL) return(gw_default);
1007 for (b = b->next; b ; b = b->next)
1009 if ((b->url->port == 0) || (b->url->port == http->port))
1011 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
1013 if ((b->url->path == NULL) ||
1015 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
1017 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
1036 /*********************************************************************
1040 * Description : Takes a domain and returns a pointer to a url_spec
1041 * structure populated with dbuf, dcnt and dvec. The
1042 * other fields in the structure that is returned are zero.
1045 * 1 : domain = a URL address
1047 * Returns : url_spec structure populated with dbuf, dcnt and dvec.
1049 *********************************************************************/
1050 struct url_spec dsplit(char *domain)
1052 struct url_spec ret[1];
1057 memset(ret, '\0', sizeof(*ret));
1059 if ((p = strrchr(domain, '.')))
1067 ret->dbuf = strdup(domain);
1069 /* map to lower case */
1070 for (p = ret->dbuf; *p ; p++) *p = tolower(*p);
1072 /* split the domain name into components */
1073 ret->dcnt = ssplit(ret->dbuf, ".", v, SZ(v), 1, 1);
1077 memset(ret, '\0', sizeof(ret));
1081 /* save a copy of the pointers in dvec */
1082 size = ret->dcnt * sizeof(*ret->dvec);
1084 if ((ret->dvec = (char **)malloc(size)))
1086 memcpy(ret->dvec, v, size);
1094 /*********************************************************************
1096 * Function : domaincmp
1098 * Description : Compare domain names.
1099 * domaincmp("a.b.c" , "a.b.c") => 0 (MATCH)
1100 * domaincmp("a*.b.c", "a.b.c") => 0 (MATCH)
1101 * domaincmp("b.c" , "a.b.c") => 0 (MATCH)
1102 * domaincmp("" , "a.b.c") => 0 (MATCH)
1105 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1106 * 2 : fqdn = domain name against which the patterns are compared.
1108 * Returns : 0 => domains are equivalent, else no match.
1110 *********************************************************************/
1111 int domaincmp(struct url_spec *pattern, struct url_spec *fqdn)
1113 char **pv, **fv; /* vectors */
1114 int pn, fn; /* counters */
1115 char *p, *f; /* chars */
1123 while ((pn > 0) && (fn > 0))
1128 while (*p && *f && (*p == tolower(*f)))
1133 if ((*p != tolower(*f)) && (*p != '*')) return(1);
1136 if (pn > 0) return(1);
1143 /* intercept functions */
1145 /*********************************************************************
1147 * Function : show_proxy_args
1149 * Description : This "crunch"es "http:/any.thing/show-proxy-args" and
1150 * returns a web page describing the current status of IJB.
1153 * 1 : http = ignored
1154 * 2 : csp = Current client state (buffers, headers, etc...)
1156 * Returns : A string that contains the current status of IJB.
1158 *********************************************************************/
1159 char *show_proxy_args(struct http_request *http, struct client_state *csp)
1163 #ifdef SPLIT_PROXY_ARGS
1167 const char * filename = NULL;
1168 const char * file_description = NULL;
1169 char * query_string = strrchr(http->path, '?');
1170 char which_file = '\0';
1173 if (query_string != NULL)
1175 /* first char past the last '?' (maybe '\0')*/
1176 which_file = query_string[1];
1183 filename = csp->blist->filename;
1184 file_description = "Block List";
1188 if (csp->permissions_list)
1190 filename = csp->permissions_list->filename;
1191 file_description = "Permissions List";
1197 filename = csp->flist->filename;
1198 file_description = "Forward List";
1206 filename = csp->alist->filename;
1207 file_description = "Access Control List";
1210 #endif /* def ACL_FILES */
1212 #ifdef USE_IMAGE_LIST
1216 filename = csp->ilist->filename;
1217 file_description = "Image List";
1220 #endif /* def USE_IMAGE_LIST */
1226 filename = csp->rlist->filename;
1227 file_description = "RE Filter List";
1230 #endif /* def PCRS */
1236 filename = csp->tlist->filename;
1237 file_description = "Trust List";
1240 #endif /* def TRUST_FILES */
1245 /* Display specified file */
1246 /* FIXME: Add HTTP headers so this isn't cached */
1249 "Server: IJ/" VERSION "\n"
1250 "Content-type: text/html\n"
1251 "Pragma: no-cache\n"
1252 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1253 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1258 "<title>Internet Junkbuster Proxy Status - ");
1259 s = strsav(s, file_description);
1263 "<body bgcolor=\"#f8f8f0\" link=\"#000078\" alink=\"#ff0022\" vlink=\"#787878\">\n"
1265 "<h1>" BANNER "\n");
1266 s = strsav(s, file_description);
1269 "<p><a href=\"show-proxy-args\">Back to proxy status</a></p>\n"
1271 s = strsav(s, file_description);
1274 "Contents of file "<code>");
1275 p = html_encode(filename);
1279 "</code>":<br>\n"
1283 if ((fp = fopen(filename, "r")) == NULL)
1285 s = strsav(s, "</pre><h1>ERROR OPENING FILE!</h1><pre>");
1289 while (fgets(buf, sizeof(buf), fp))
1291 p = html_encode(buf);
1296 s = strsav(s, "<br>");
1305 "<p><a href=\"show-proxy-args\">Back to proxy status</a></p>\n"
1307 "<small><small><p>\n"
1308 "The " BANNER " Proxy - \n"
1309 "<a href=\"" HOME_PAGE_URL "\">" HOME_PAGE_URL "</a>\n"
1311 "</body></html>\n");
1314 #endif /* def SPLIT_PROXY_ARGS */
1316 s = strsav(s, csp->config->proxy_args->header);
1317 s = strsav(s, csp->config->proxy_args->invocation);
1320 #endif /* def STATISTICS */
1321 s = strsav(s, csp->config->proxy_args->gateways);
1323 #ifdef SPLIT_PROXY_ARGS
1325 "<h2>The following files are in use:</h2>\n"
1326 "<p>(Click a filename to view it)</p>\n"
1331 s = strsav(s, "<li>Block List: <a href=\"show-proxy-args?block\"><code>");
1332 s = strsav(s, csp->blist->filename);
1333 s = strsav(s, "</code></a></li>\n");
1336 if (csp->permissions_list)
1338 s = strsav(s, "<li>Permissions List: <a href=\"show-proxy-args?permit\"><code>");
1339 s = strsav(s, csp->permissions_list->filename);
1340 s = strsav(s, "</code></a></li>\n");
1345 s = strsav(s, "<li>Forward List: <a href=\"show-proxy-args?forward\"><code>");
1346 s = strsav(s, csp->flist->filename);
1347 s = strsav(s, "</code></a></li>\n");
1353 s = strsav(s, "<li>Access Control List: <a href=\"show-proxy-args?acl\"><code>");
1354 s = strsav(s, csp->alist->filename);
1355 s = strsav(s, "</code></a></li>\n");
1357 #endif /* def ACL_FILES */
1359 #ifdef USE_IMAGE_LIST
1362 s = strsav(s, "<li>Image List: <a href=\"show-proxy-args?image\"><code>");
1363 s = strsav(s, csp->ilist->filename);
1364 s = strsav(s, "</code></a></li>\n");
1366 #endif /* def USE_IMAGE_LIST */
1371 s = strsav(s, "<li>RE Filter List: <a href=\"show-proxy-args?re\"><code>");
1372 s = strsav(s, csp->rlist->filename);
1373 s = strsav(s, "</code></a></li>\n");
1375 #endif /* def PCRS */
1380 s = strsav(s, "<li>Trust List: <a href=\"show-proxy-args?trust\"><code>");
1381 s = strsav(s, csp->tlist->filename);
1382 s = strsav(s, "</code></a></li>\n");
1384 #endif /* def TRUST_FILES */
1386 s = strsav(s, "</ul>");
1388 #else /* ifndef SPLIT_PROXY_ARGS */
1391 s = strsav(s, csp->blist->proxy_args);
1396 s = strsav(s, csp->clist->proxy_args);
1401 s = strsav(s, csp->flist->proxy_args);
1407 s = strsav(s, csp->alist->proxy_args);
1409 #endif /* def ACL_FILES */
1411 #ifdef USE_IMAGE_LIST
1414 s = strsav(s, csp->ilist->proxy_args);
1416 #endif /* def USE_IMAGE_LIST */
1421 s = strsav(s, csp->rlist->proxy_args);
1423 #endif /* def PCRS */
1428 s = strsav(s, csp->tlist->proxy_args);
1430 #endif /* def TRUST_FILES */
1432 #endif /* ndef SPLIT_PROXY_ARGS */
1434 s = strsav(s, csp->config->proxy_args->trailer);
1441 /*********************************************************************
1443 * Function : ijb_send_banner
1445 * Description : This "crunch"es "http:/any.thing/ijb-send-banner and
1446 * thus triggers sending the image in jcc.c:chat.
1447 * For the unlikely case, that the imagefile/MSIE
1448 * mechanism is not used, or tinygif = 0, a page
1449 * describing the reson of the interception is generated.
1452 * 1 : http = http_request request for crunched URL
1453 * 2 : csp = Current client state (buffers, headers, etc...)
1455 * Returns : A string that contains why this was intercepted.
1457 *********************************************************************/
1458 char *ijb_send_banner(struct http_request *http, struct client_state *csp)
1460 return(strdup(CNOBANNER));
1464 /*********************************************************************
1466 * Function : ij_untrusted_url
1468 * Description : This "crunch"es "http:/any.thing/ij-untrusted-url" and
1469 * returns a web page describing why it was untrusted.
1472 * 1 : http = http_request request for crunched URL
1473 * 2 : csp = Current client state (buffers, headers, etc...)
1475 * Returns : A string that contains why this was untrusted.
1477 *********************************************************************/
1478 char *ij_untrusted_url(struct http_request *http, struct client_state *csp)
1481 char *hostport, *path, *refer, *p, *v[9];
1483 struct url_spec **tl, *t;
1486 static const char format[] =
1487 "HTTP/1.0 200 OK\r\n"
1488 "Pragma: no-cache\n"
1489 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1490 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1491 "Content-Type: text/html\n\n"
1494 "<title>Internet Junkbuster: Request for untrusted URL</title>\n"
1500 "The " BANNER " Proxy "
1501 "<A href=\"" HOME_PAGE_URL "\">"
1502 "(" HOME_PAGE_URL ") </A>"
1503 "intercepted the request for %s%s\n"
1504 "because the URL is not trusted.\n"
1507 if ((n = ssplit(http->path, "?+", v, SZ(v), 0, 0)) == 4)
1509 hostport = url_decode(v[1]);
1510 path = url_decode(v[2]);
1511 refer = url_decode(v[3]);
1515 hostport = strdup("undefined_host");
1516 path = strdup("/undefined_path");
1517 refer = strdup("undefined");
1521 n += strlen(hostport);
1524 if ((p = (char *)malloc(n)))
1526 sprintf(p, format, hostport, path);
1529 strsav(p, "The referrer in this request was <strong>");
1531 strsav(p, "</strong><br>\n");
1537 p = strsav(p, "<h3>The following referrers are trusted</h3>\n");
1539 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
1541 sprintf(buf, "%s<br>\n", t->spec);
1545 if (csp->config->trust_info->next)
1551 "You can learn more about what this means "
1552 "and what you may be able to do about it by "
1553 "reading the following documents:<br>\n"
1559 for (l = csp->config->trust_info->next; l ; l = l->next)
1562 "<li> <a href=%s>%s</a><br>\n",
1567 p = strsav(p, "</ol>\n");
1570 p = strsav(p, "</body>\n" "</html>\n");
1575 #endif /* def TRUST_FILES */
1579 /*********************************************************************
1581 * Function : add_stats
1583 * Description : Statistics function of JB. Called by `show_proxy_args'.
1586 * 1 : s = string that holds the proxy args description page
1588 * Returns : A pointer to the descriptive status web page.
1590 *********************************************************************/
1591 char *add_stats(char *s)
1594 * Output details of the number of requests rejected and
1595 * accepted. This is switchable in the junkbuster config.
1596 * Does nothing if this option is not enabled.
1599 float perc_rej; /* Percentage of http requests rejected */
1601 int local_urls_read = urls_read;
1602 int local_urls_rejected = urls_rejected;
1605 * Need to alter the stats not to include the fetch of this
1608 * Can't do following thread safely! doh!
1611 * urls_rejected--; * This will be incremented subsequently *
1614 s = strsav(s,"<h2>Statistics for this " BANNER ":</h2>\n");
1616 if (local_urls_read == 0)
1619 s = strsav(s,"No activity so far!\n");
1625 perc_rej = (float)local_urls_rejected * 100.0F /
1626 (float)local_urls_read;
1629 "%d requests received, %d filtered "
1632 local_urls_rejected, perc_rej);
1634 s = strsav(s,out_str);
1639 #endif /* def STATISTICS */