1 const char filters_rcs[] = "$Id: filters.c,v 1.4 2001/05/22 18:46:04 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_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.4 2001/05/22 18:46:04 oes
43 * - Enabled filtering banners by size rather than URL
44 * by adding patterns that replace all standard banner
45 * sizes with the "Junkbuster" gif to the re_filterfile
47 * - Enabled filtering WebBugs by providing a pattern
48 * which kills all 1x1 images
50 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
51 * which is selected by the (nonstandard and therefore
52 * capital) letter 'U' in the option string.
53 * It causes the quantifiers to be ungreedy by default.
54 * Appending a ? turns back to greedy (!).
56 * - Added a new interceptor ijb-send-banner, which
57 * sends back the "Junkbuster" gif. Without imagelist or
58 * MSIE detection support, or if tinygif = 1, or the
59 * URL isn't recognized as an imageurl, a lame HTML
60 * explanation is sent instead.
62 * - Added new feature, which permits blocking remote
63 * script redirects and firing back a local redirect
65 * The feature is conditionally compiled, i.e. it
66 * can be disabled with --disable-fast-redirects,
67 * plus it must be activated by a "fast-redirects"
68 * line in the config file, has its own log level
69 * and of course wants to be displayed by show-proxy-args
70 * Note: Boy, all the #ifdefs in 1001 locations and
71 * all the fumbling with configure.in and acconfig.h
72 * were *way* more work than the feature itself :-(
74 * - Because a generic redirect template was needed for
75 * this, tinygif = 3 now uses the same.
77 * - Moved GIFs, and other static HTTP response templates
82 * - Removed some >400 CRs again (Jon, you really worked
85 * Revision 1.3 2001/05/20 16:44:47 jongfoster
86 * Removing last hardcoded JunkBusters.com URLs.
88 * Revision 1.2 2001/05/20 01:21:20 jongfoster
89 * Version 2.9.4 checkin.
90 * - Merged popupfile and cookiefile, and added control over PCRS
91 * filtering, in new "permissionsfile".
92 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
93 * file error you now get a message box (in the Win32 GUI) rather
94 * than the program exiting with no explanation.
95 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
97 * - Removed tabs from "config"
98 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
99 * - Bumped up version number.
101 * Revision 1.1.1.1 2001/05/15 13:58:52 oes
102 * Initial import of version 2.9.3 source tree
105 *********************************************************************/
111 #include <sys/types.h>
118 #include <netinet/in.h>
120 #include <winsock2.h>
127 #include "showargs.h"
131 #include "jbsockets.h"
133 #include "jbsockets.h"
139 const char filters_h_rcs[] = FILTERS_H_VERSION;
141 /* Fix a problem with Solaris. There should be no effect on other
143 * Solaris's isspace() is a macro which uses it's argument directly
144 * as an array index. Therefore we need to make sure that high-bit
145 * characters generate +ve values, and ideally we also want to make
146 * the argument match the declared parameter type of "int".
148 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
151 static const char CBLOCK[] =
153 "HTTP/1.0 403 Request for blocked URL\n"
154 #else /* ifndef AMIGA */
155 "HTTP/1.0 202 Request for blocked URL\n"
156 #endif /* ndef AMIGA */
158 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
159 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
160 "Content-Type: text/html\n\n"
163 "<title>Internet Junkbuster: Request for blocked URL</title>\n"
169 "<p align=center>Your request for <b>%s%s</b><br>\n"
170 "was blocked because it matches the following pattern "
171 "in the blockfile: <b>%s</b>\n</p>"
173 "<p align=center><a href=\"http://%s" FORCE_PREFIX
174 "%s\">Go there anyway.</a></p>"
175 #endif /* def FORCE_LOAD */
180 static const char CTRUST[] =
182 "HTTP/1.0 403 Request for untrusted URL\n"
183 #else /* ifndef AMIGA */
184 "HTTP/1.0 202 Request for untrusted URL\n"
185 #endif /* ndef AMIGA */
187 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
188 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
189 "Content-Type: text/html\n\n"
192 "<title>Internet Junkbuster: Request for untrusted URL</title>\n"
196 "<a href=http://internet.junkbuster.com/ij-untrusted-url?%s+%s+%s>"
202 #endif /* def TRUST_FILES */
206 /*********************************************************************
208 * Function : block_acl
210 * Description : Block this request?
211 * Decide yes or no based on ACL file.
214 * 1 : src = Address the browser/user agent is requesting.
215 * 2 : dst = The proxy or gateway address this is going to.
216 * 3 : csp = Current client state (buffers, headers, etc...)
218 * Returns : 0 = FALSE (don't block) and 1 = TRUE (do block)
220 *********************************************************************/
221 int block_acl(struct access_control_addr *src, struct access_control_addr *dst, struct client_state *csp)
223 struct file_list *fl;
224 struct access_control_list *a, *acl;
225 struct access_control_addr s[1], d[1];
227 /* if not using an access control list, then permit the connection */
228 if (((fl = csp->alist) == NULL) || ((acl = fl->f) == NULL))
233 /* search the list */
234 for (a = acl->next ; a ; a = a->next)
239 s->addr &= a->src->mask;
240 d->addr &= a->dst->mask;
242 if ((s->addr == a->src->addr)
243 && (d->addr == a->dst->addr)
244 && ((s->port == a->src->port)
246 || (a->src->port == 0))
247 && ((d->port == a->dst->port)
249 || (a->dst->port == 0)))
251 if (a->action == ACL_PERMIT)
267 /*********************************************************************
269 * Function : acl_addr
271 * Description : Called from `load_aclfile'. FIXME: I can't say more.
274 * 1 : aspec = (what?)
277 * Returns : 0 => Ok, everything else is an error.
279 *********************************************************************/
280 int acl_addr(char *aspec, struct access_control_addr *aca)
282 int i, masklength, port;
288 if ((p = strchr(aspec, '/')))
292 if (ijb_isdigit(*p) == 0)
296 masklength = atoi(p);
299 if ((masklength < 0) || (masklength > 32))
304 if ((p = strchr(aspec, ':')))
308 if (ijb_isdigit(*p) == 0)
317 aca->addr = ntohl(resolve_hostname_to_ip(aspec));
321 log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec);
325 /* build the netmask */
327 for (i=1; i <= masklength ; i++)
329 aca->mask |= (1 << (32 - i));
332 /* now mask off the host portion of the ip address
333 * (i.e. save on the network portion of the address).
335 aca->addr = aca->addr & aca->mask;
340 #endif /* def ACL_FILES */
343 /*********************************************************************
345 * Function : block_url
347 * Description : Called from `chat'. Check to see if we need to block this.
350 * 1 : http = http_request request to "check" for blocked
351 * 2 : csp = Current client state (buffers, headers, etc...)
353 * Returns : NULL => unblocked, else string to HTML block description.
355 *********************************************************************/
356 char *block_url(struct http_request *http, struct client_state *csp)
358 struct file_list *fl;
359 struct block_spec *b;
360 struct url_spec url[1];
364 if (((fl = csp->blist) == NULL) || ((b = fl->f) == NULL))
369 *url = dsplit(http->host);
371 /* if splitting the domain fails, punt */
372 if (url->dbuf == NULL) return(NULL);
374 for (b = b->next; b ; b = b->next)
376 if ((b->url->port == 0) || (b->url->port == http->port))
378 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
380 if ((b->url->path == NULL) ||
382 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
384 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
391 if (b->reject == 0) return(NULL);
394 n += strlen(http->hostport);
395 n += strlen(http->path);
396 n += strlen(b->url->spec);
398 n += strlen(http->hostport);
399 n += strlen(http->path);
400 #endif /* def FORCE_LOAD */
402 p = (char *)malloc(n);
405 sprintf(p, CBLOCK, http->hostport, http->path, b->url->spec, http->hostport, http->path);
407 sprintf(p, CBLOCK, http->hostport, http->path, b->url->spec);
408 #endif /* def FORCE_LOAD */
422 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
423 /*********************************************************************
425 * Function : block_imageurl
427 * Description : Given a URL which is blocked, decide whether to
428 * send the "blocked" image or HTML.
431 * 1 : http = URL to check.
432 * 2 : csp = Current client state (buffers, headers, etc...)
434 * Returns : True (nonzero) if URL is in image list, false (0)
437 *********************************************************************/
438 int block_imageurl(struct http_request *http, struct client_state *csp)
440 #ifdef DETECT_MSIE_IMAGES
441 if ((csp->accept_types
442 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
443 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE))
447 else if ((csp->accept_types
448 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
449 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_HTML))
455 #if defined(USE_IMAGE_LIST)
456 return block_imageurl_using_imagelist(http, csp);
458 /* Don't know - assume HTML */
462 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
465 #ifdef USE_IMAGE_LIST
466 /*********************************************************************
468 * Function : block_imageurl
470 * Description : Test if a URL is in the imagelist.
473 * 1 : http = URL to check.
474 * 2 : csp = Current client state (buffers, headers, etc...)
476 * Returns : True (nonzero) if URL is in image list, false (0)
479 *********************************************************************/
480 int block_imageurl_using_imagelist(struct http_request *http, struct client_state *csp)
482 struct file_list *fl;
483 struct block_spec *b;
484 struct url_spec url[1];
486 if (((fl = csp->ilist) == NULL) || ((b = fl->f) == NULL))
491 *url = dsplit(http->host);
493 /* if splitting the domain fails, punt */
494 if (url->dbuf == NULL) return(0);
496 for (b = b->next; b ; b = b->next)
499 if ((b->url->port == 0) || (b->url->port == http->port))
501 /* port matches, check domain */
502 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
504 /* domain matches, check path */
505 if ((b->url->path == NULL) ||
507 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
509 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
517 if (b->reject == 0) return(0);
529 #endif /* def USE_IMAGE_LIST */
533 /*********************************************************************
535 * Function : re_process_buffer
537 * Description : Apply all jobs from the joblist (aka. Perl regexp's) to
538 * the text buffer that's been accumulated in csp->iob->buf.
539 * Then, write the modified buffer out to the client
540 * (Maybe this should happen from jcc.c via flush_socket
541 * for better readability).
544 * 1 : csp = Current client state (buffers, headers, etc...)
548 *********************************************************************/
549 void re_process_buffer(struct client_state *csp)
552 int size = csp->iob->eod - csp->iob->cur;
553 char *old=csp->iob->cur, *new = NULL;
554 pcrs_job *job, *joblist;
556 struct file_list *fl;
557 struct re_filterfile_spec *b;
559 /* Sanity first ;-) */
565 if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
567 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
571 joblist = b->joblist;
574 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
575 csp->http->hostport, csp->http->path, size);
577 /* Apply all jobs from the joblist */
578 for (job = joblist; NULL != job; job = job->next)
580 hits += pcrs_exec_substitution(job, old, size, &new, &size);
581 if (old != csp->iob->cur) free(old);
585 log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
587 if (write_socket(csp->cfd, old, size) != size)
589 log_error(LOG_LEVEL_ERROR, "write to client failed.");
592 /* fwiw, reset the iob */
598 #endif /* def PCRS */
602 /*********************************************************************
604 * Function : trust_url
606 * Description : Should we "trust" this URL? See "trustfile" line in config.
609 * 1 : http = http_request request for requested URL
610 * 2 : csp = Current client state (buffers, headers, etc...)
612 * Returns : NULL => trusted, else string to HTML "untrusted" description.
614 *********************************************************************/
615 char *trust_url(struct http_request *http, struct client_state *csp)
617 struct file_list *fl;
618 struct block_spec *b;
619 struct url_spec url[1], **tl, *t;
621 char *hostport, *path, *refer;
622 struct http_request rhttp[1];
625 if (((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL))
630 *url = dsplit(http->host);
632 /* if splitting the domain fails, punt */
633 if (url->dbuf == NULL) return(NULL);
635 memset(rhttp, '\0', sizeof(*rhttp));
637 for (b = b->next; b ; b = b->next)
639 if ((b->url->port == 0) || (b->url->port == http->port))
641 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
643 if ((b->url->path == NULL) ||
645 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
647 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
654 if (b->reject == 0) return(NULL);
656 hostport = url_encode(http->hostport);
657 path = url_encode(http->path);
661 refer = url_encode(csp->referrer);
665 refer = url_encode("undefined");
669 n += strlen(hostport);
673 p = (char *)malloc(n);
675 sprintf(p, CTRUST, hostport, path, refer);
690 if ((csp->referrer == NULL)|| (strlen(csp->referrer) <= 9))
692 /* no referrer was supplied */
693 goto trust_url_not_trusted;
696 /* forge a URL from the referrer so we can use
697 * convert_url() to parse it into its components.
701 p = strsav(p, "GET ");
702 p = strsav(p, csp->referrer + 9); /* skip over "Referer: " */
703 p = strsav(p, " HTTP/1.0");
705 parse_http_request(p, rhttp, csp);
707 if (rhttp->cmd == NULL)
710 goto trust_url_not_trusted;
715 *url = dsplit(rhttp->host);
717 /* if splitting the domain fails, punt */
718 if (url->dbuf == NULL) goto trust_url_not_trusted;
720 for (tl = trust_list; (t = *tl) ; tl++)
722 if ((t->port == 0) || (t->port == rhttp->port))
724 if ((t->domain[0] == '\0') || domaincmp(t, url) == 0)
726 if ((t->path == NULL) ||
728 (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0)
730 (strncmp(t->path, rhttp->path, t->pathlen) == 0)
734 /* if the URL's referrer is from a trusted referrer, then
735 * add the target spec to the trustfile as an unblocked
736 * domain and return NULL (which means it's OK).
744 if ((fp = fopen(trustfile, "a")))
749 h = strsav(h, http->hostport);
755 /* since this path points into a user's home space
756 * be sure to include this spec in the trustfile.
758 if ((p = strchr(p, '/')))
761 h = strsav(h, http->path);
766 free_http_request(rhttp);
768 fprintf(fp, "%s\n", h);
778 trust_url_not_trusted:
779 free_http_request(rhttp);
781 hostport = url_encode(http->hostport);
782 path = url_encode(http->path);
786 refer = url_encode(csp->referrer);
790 refer = url_encode("undefined");
794 n += strlen(hostport);
798 p = (char *)malloc(n);
799 sprintf(p, CTRUST, hostport, path, refer);
808 #endif /* def TRUST_FILES */
811 /*********************************************************************
813 * Function : intercept_url
815 * Description : checks the URL `basename' against a list of URLs to
816 * snarf. If it matches, it calls the associated function
817 * which returns an HTML page to send back to the client.
818 * Right now, we snarf:
819 * "show-proxy-args", and
820 * "ij-untrusted-url" (optional w/TRUST_FILES)
823 * 1 : http = http_request request, check `basename's of blocklist
824 * 2 : csp = Current client state (buffers, headers, etc...)
826 * Returns : NULL for no recognized URLs, or an HTML description page.
828 *********************************************************************/
829 char *intercept_url(struct http_request *http, struct client_state *csp)
832 const struct interceptors *v;
834 basename = strrchr(http->path, '/');
836 if (basename == NULL) return(NULL);
838 basename ++; /* first char past the last slash */
842 for (v = intercept_patterns; v->str; v++)
844 if (strncmp(basename, v->str, v->len) == 0)
846 return((v->interceptor)(http, csp));
855 #ifdef FAST_REDIRECTS
856 /*********************************************************************
858 * Function : redirect_url
860 * Description : Checks for redirection URLs and returns a HTTP redirect
861 * to the destination URL.
864 * 1 : http = http_request request, check `basename's of blocklist
865 * 2 : csp = Current client state (buffers, headers, etc...)
867 * Returns : NULL if URL was clean, HTTP redirect otherwise.
869 *********************************************************************/
870 char *redirect_url(struct http_request *http, struct client_state *csp)
874 p = q = csp->http->path;
875 log_error(LOG_LEVEL_REDIRECTS, "checking path: %s", p);
877 /* find the last URL encoded in the request */
878 while (p = strstr(p, "http://"))
883 /* if there was any, generate and return a HTTP redirect */
884 if (q != csp->http->path)
886 log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
888 p = (char *)malloc(strlen(HTTP_REDIRECT_TEMPLATE) + strlen(q));
889 sprintf(p, HTTP_REDIRECT_TEMPLATE, q);
898 #endif /* def FAST_REDIRECTS */
900 /*********************************************************************
902 * Function : url_permissions
904 * Description : Gets the permissions for this URL.
907 * 1 : http = http_request request for blocked URLs
908 * 2 : csp = Current client state (buffers, headers, etc...)
910 * Returns : permissions bitmask specifiying what this URL can do.
911 * If not on list, will be default_permissions.
913 *********************************************************************/
914 int url_permissions(struct http_request *http, struct client_state *csp)
916 struct file_list *fl;
917 struct permissions_spec *b;
918 struct url_spec url[1];
920 if (((fl = csp->permissions_list) == NULL) || ((b = fl->f) == NULL))
922 return(default_permissions);
925 *url = dsplit(http->host);
927 /* if splitting the domain fails, punt */
928 if (url->dbuf == NULL)
930 return(default_permissions);
933 for (b = b->next; NULL != b; b = b->next)
935 if ((b->url->port == 0) || (b->url->port == http->port))
937 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
939 if ((b->url->path == NULL) ||
941 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
943 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
949 return(b->permissions);
957 return(default_permissions);
962 /*********************************************************************
964 * Function : forward_url
966 * Description : Should we forward this to another proxy?
969 * 1 : http = http_request request for current URL
970 * 2 : csp = Current client state (buffers, headers, etc...)
972 * Returns : Return gw_default for no forward match,
973 * else a gateway pointer to a specific forwarding proxy.
975 *********************************************************************/
976 const struct gateway *forward_url(struct http_request *http, struct client_state *csp)
978 struct file_list *fl;
979 struct forward_spec *b;
980 struct url_spec url[1];
982 if (((fl = csp->flist) == NULL) || ((b = fl->f) == NULL))
987 *url = dsplit(http->host);
989 /* if splitting the domain fails, punt */
990 if (url->dbuf == NULL) return(gw_default);
992 for (b = b->next; b ; b = b->next)
994 if ((b->url->port == 0) || (b->url->port == http->port))
996 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
998 if ((b->url->path == NULL) ||
1000 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
1002 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
1021 /*********************************************************************
1025 * Description : Takes a domain and returns a pointer to a url_spec
1026 * structure populated with dbuf, dcnt and dvec. The
1027 * other fields in the structure that is returned are zero.
1030 * 1 : domain = a URL address
1032 * Returns : url_spec structure populated with dbuf, dcnt and dvec.
1034 *********************************************************************/
1035 struct url_spec dsplit(char *domain)
1037 struct url_spec ret[1];
1042 memset(ret, '\0', sizeof(*ret));
1044 if ((p = strrchr(domain, '.')))
1052 ret->dbuf = strdup(domain);
1054 /* map to lower case */
1055 for (p = ret->dbuf; *p ; p++) *p = tolower(*p);
1057 /* split the domain name into components */
1058 ret->dcnt = ssplit(ret->dbuf, ".", v, SZ(v), 1, 1);
1062 memset(ret, '\0', sizeof(ret));
1066 /* save a copy of the pointers in dvec */
1067 size = ret->dcnt * sizeof(*ret->dvec);
1069 if ((ret->dvec = (char **)malloc(size)))
1071 memcpy(ret->dvec, v, size);
1079 /*********************************************************************
1081 * Function : domaincmp
1083 * Description : Compare domain names.
1084 * domaincmp("a.b.c" , "a.b.c") => 0 (MATCH)
1085 * domaincmp("a*.b.c", "a.b.c") => 0 (MATCH)
1086 * domaincmp("b.c" , "a.b.c") => 0 (MATCH)
1087 * domaincmp("" , "a.b.c") => 0 (MATCH)
1090 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1091 * 2 : fqdn = domain name against which the patterns are compared.
1093 * Returns : 0 => domains are equivalent, else no match.
1095 *********************************************************************/
1096 int domaincmp(struct url_spec *pattern, struct url_spec *fqdn)
1098 char **pv, **fv; /* vectors */
1099 int pn, fn; /* counters */
1100 char *p, *f; /* chars */
1108 while ((pn > 0) && (fn > 0))
1113 while (*p && *f && (*p == tolower(*f)))
1118 if ((*p != tolower(*f)) && (*p != '*')) return(1);
1121 if (pn > 0) return(1);
1128 /* intercept functions */
1130 /*********************************************************************
1132 * Function : show_proxy_args
1134 * Description : This "crunch"es "http:/any.thing/show-proxy-args" and
1135 * returns a web page describing the current status of IJB.
1138 * 1 : http = ignored
1139 * 2 : csp = Current client state (buffers, headers, etc...)
1141 * Returns : A string that contains the current status of IJB.
1143 *********************************************************************/
1144 char *show_proxy_args(struct http_request *http, struct client_state *csp)
1148 #ifdef SPLIT_PROXY_ARGS
1152 const char * filename = NULL;
1153 const char * file_description = NULL;
1154 char * query_string = strrchr(http->path, '?');
1155 char which_file = '\0';
1158 if (query_string != NULL)
1160 /* first char past the last '?' (maybe '\0')*/
1161 which_file = query_string[1];
1168 filename = csp->blist->filename;
1169 file_description = "Block List";
1173 if (csp->permissions_list)
1175 filename = csp->permissions_list->filename;
1176 file_description = "Permissions List";
1182 filename = csp->flist->filename;
1183 file_description = "Forward List";
1191 filename = csp->alist->filename;
1192 file_description = "Access Control List";
1195 #endif /* def ACL_FILES */
1197 #ifdef USE_IMAGE_LIST
1201 filename = csp->ilist->filename;
1202 file_description = "Image List";
1205 #endif /* def USE_IMAGE_LIST */
1211 filename = csp->rlist->filename;
1212 file_description = "RE Filter List";
1215 #endif /* def PCRS */
1221 filename = csp->tlist->filename;
1222 file_description = "Trust List";
1225 #endif /* def TRUST_FILES */
1230 /* Display specified file */
1231 /* FIXME: Add HTTP headers so this isn't cached */
1234 "Server: IJ/" VERSION "\n"
1235 "Content-type: text/html\n"
1236 "Pragma: no-cache\n"
1237 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1238 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1243 "<title>Internet Junkbuster Proxy Status - ");
1244 s = strsav(s, file_description);
1248 "<body bgcolor=\"#f8f8f0\" link=\"#000078\" alink=\"#ff0022\" vlink=\"#787878\">\n"
1250 "<h1>" BANNER "\n");
1251 s = strsav(s, file_description);
1254 "<p><a href=\"show-proxy-args\">Back to proxy status</a></p>\n"
1256 s = strsav(s, file_description);
1259 "Contents of file "<code>");
1260 p = html_encode(filename);
1264 "</code>":<br>\n"
1268 if ((fp = fopen(filename, "r")) == NULL)
1270 s = strsav(s, "</pre><h1>ERROR OPENING FILE!</h1><pre>");
1274 while (fgets(buf, sizeof(buf), fp))
1276 p = html_encode(buf);
1281 s = strsav(s, "<br>");
1290 "<p><a href=\"show-proxy-args\">Back to proxy status</a></p>\n"
1292 "<small><small><p>\n"
1293 "The " BANNER " Proxy - \n"
1294 "<a href=\"" HOME_PAGE_URL "\">" HOME_PAGE_URL "</a>\n"
1296 "</body></html>\n");
1299 #endif /* def SPLIT_PROXY_ARGS */
1301 s = strsav(s, proxy_args->header);
1302 s = strsav(s, proxy_args->invocation);
1305 #endif /* def STATISTICS */
1306 s = strsav(s, proxy_args->gateways);
1308 #ifdef SPLIT_PROXY_ARGS
1310 "<h2>The following files are in use:</h2>\n"
1311 "<p>(Click a filename to view it)</p>\n"
1316 s = strsav(s, "<li>Block List: <a href=\"show-proxy-args?block\"><code>");
1317 s = strsav(s, csp->blist->filename);
1318 s = strsav(s, "</code></a></li>\n");
1321 if (csp->permissions_list)
1323 s = strsav(s, "<li>Permissions List: <a href=\"show-proxy-args?permit\"><code>");
1324 s = strsav(s, csp->permissions_list->filename);
1325 s = strsav(s, "</code></a></li>\n");
1330 s = strsav(s, "<li>Forward List: <a href=\"show-proxy-args?forward\"><code>");
1331 s = strsav(s, csp->flist->filename);
1332 s = strsav(s, "</code></a></li>\n");
1338 s = strsav(s, "<li>Access Control List: <a href=\"show-proxy-args?acl\"><code>");
1339 s = strsav(s, csp->alist->filename);
1340 s = strsav(s, "</code></a></li>\n");
1342 #endif /* def ACL_FILES */
1344 #ifdef USE_IMAGE_LIST
1347 s = strsav(s, "<li>Image List: <a href=\"show-proxy-args?image\"><code>");
1348 s = strsav(s, csp->ilist->filename);
1349 s = strsav(s, "</code></a></li>\n");
1351 #endif /* def USE_IMAGE_LIST */
1356 s = strsav(s, "<li>RE Filter List: <a href=\"show-proxy-args?re\"><code>");
1357 s = strsav(s, csp->rlist->filename);
1358 s = strsav(s, "</code></a></li>\n");
1360 #endif /* def PCRS */
1365 s = strsav(s, "<li>Trust List: <a href=\"show-proxy-args?trust\"><code>");
1366 s = strsav(s, csp->tlist->filename);
1367 s = strsav(s, "</code></a></li>\n");
1369 #endif /* def TRUST_FILES */
1371 s = strsav(s, "</ul>");
1373 #else /* ifndef SPLIT_PROXY_ARGS */
1376 s = strsav(s, csp->blist->proxy_args);
1381 s = strsav(s, csp->clist->proxy_args);
1386 s = strsav(s, csp->flist->proxy_args);
1392 s = strsav(s, csp->alist->proxy_args);
1394 #endif /* def ACL_FILES */
1396 #ifdef USE_IMAGE_LIST
1399 s = strsav(s, csp->ilist->proxy_args);
1401 #endif /* def USE_IMAGE_LIST */
1406 s = strsav(s, csp->rlist->proxy_args);
1408 #endif /* def PCRS */
1413 s = strsav(s, csp->tlist->proxy_args);
1415 #endif /* def TRUST_FILES */
1417 #endif /* ndef SPLIT_PROXY_ARGS */
1419 s = strsav(s, proxy_args->trailer);
1426 /*********************************************************************
1428 * Function : ijb_send_banner
1430 * Description : This "crunch"es "http:/any.thing/ijb-send-banner and
1431 * thus triggers sending the image in jcc.c:chat.
1432 * For the unlikely case, that the imagefile/MSIE
1433 * mechanism is not used, or tinygif = 0, a page
1434 * describing the reson of the interception is generated.
1437 * 1 : http = http_request request for crunched URL
1438 * 2 : csp = Current client state (buffers, headers, etc...)
1440 * Returns : A string that contains why this was intercepted.
1442 *********************************************************************/
1443 char *ijb_send_banner(struct http_request *http, struct client_state *csp)
1445 return(strdup(CNOBANNER));
1449 /*********************************************************************
1451 * Function : ij_untrusted_url
1453 * Description : This "crunch"es "http:/any.thing/ij-untrusted-url" and
1454 * returns a web page describing why it was untrusted.
1457 * 1 : http = http_request request for crunched URL
1458 * 2 : csp = Current client state (buffers, headers, etc...)
1460 * Returns : A string that contains why this was untrusted.
1462 *********************************************************************/
1463 char *ij_untrusted_url(struct http_request *http, struct client_state *csp)
1466 char *hostport, *path, *refer, *p, *v[9];
1468 struct url_spec **tl, *t;
1471 static const char format[] =
1472 "HTTP/1.0 200 OK\r\n"
1473 "Pragma: no-cache\n"
1474 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1475 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1476 "Content-Type: text/html\n\n"
1479 "<title>Internet Junkbuster: Request for untrusted URL</title>\n"
1485 "The " BANNER " Proxy "
1486 "<A href=\"" HOME_PAGE_URL "\">"
1487 "(" HOME_PAGE_URL ") </A>"
1488 "intercepted the request for %s%s\n"
1489 "because the URL is not trusted.\n"
1492 if ((n = ssplit(http->path, "?+", v, SZ(v), 0, 0)) == 4)
1494 hostport = url_decode(v[1]);
1495 path = url_decode(v[2]);
1496 refer = url_decode(v[3]);
1500 hostport = strdup("undefined_host");
1501 path = strdup("/undefined_path");
1502 refer = strdup("undefined");
1506 n += strlen(hostport);
1509 if ((p = (char *)malloc(n)))
1511 sprintf(p, format, hostport, path);
1514 strsav(p, "The referrer in this request was <strong>");
1516 strsav(p, "</strong><br>\n");
1522 p = strsav(p, "<h3>The following referrers are trusted</h3>\n");
1524 for (tl = trust_list; (t = *tl) ; tl++)
1526 sprintf(buf, "%s<br>\n", t->spec);
1530 if (trust_info->next)
1536 "You can learn more about what this means "
1537 "and what you may be able to do about it by "
1538 "reading the following documents:<br>\n"
1544 for (l = trust_info->next; l ; l = l->next)
1547 "<li> <a href=%s>%s</a><br>\n",
1552 p = strsav(p, "</ol>\n");
1555 p = strsav(p, "</body>\n" "</html>\n");
1560 #endif /* def TRUST_FILES */
1564 /*********************************************************************
1566 * Function : add_stats
1568 * Description : Statistics function of JB. Called by `show_proxy_args'.
1571 * 1 : s = string that holds the proxy args description page
1573 * Returns : A pointer to the descriptive status web page.
1575 *********************************************************************/
1576 char *add_stats(char *s)
1579 * Output details of the number of requests rejected and
1580 * accepted. This is switchable in the junkbuster config.
1581 * Does nothing if this option is not enabled.
1584 float perc_rej; /* Percentage of http requests rejected */
1586 int local_urls_read = urls_read;
1587 int local_urls_rejected = urls_rejected;
1590 * Need to alter the stats not to include the fetch of this
1593 * Can't do following thread safely! doh!
1596 * urls_rejected--; * This will be incremented subsequently *
1599 s = strsav(s,"<h2>Statistics for this " BANNER ":</h2>\n");
1601 if (local_urls_read == 0)
1604 s = strsav(s,"No activity so far!\n");
1610 perc_rej = (float)local_urls_rejected * 100.0F /
1611 (float)local_urls_read;
1614 "%d requests received, %d filtered "
1617 local_urls_rejected, perc_rej);
1619 s = strsav(s,out_str);
1624 #endif /* def STATISTICS */