1 const char filters_rcs[] = "$Id: filters.c,v 1.5 2001/05/25 22:34:30 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.5 2001/05/25 22:34:30 jongfoster
44 * Revision 1.4 2001/05/22 18:46:04 oes
46 * - Enabled filtering banners by size rather than URL
47 * by adding patterns that replace all standard banner
48 * sizes with the "Junkbuster" gif to the re_filterfile
50 * - Enabled filtering WebBugs by providing a pattern
51 * which kills all 1x1 images
53 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
54 * which is selected by the (nonstandard and therefore
55 * capital) letter 'U' in the option string.
56 * It causes the quantifiers to be ungreedy by default.
57 * Appending a ? turns back to greedy (!).
59 * - Added a new interceptor ijb-send-banner, which
60 * sends back the "Junkbuster" gif. Without imagelist or
61 * MSIE detection support, or if tinygif = 1, or the
62 * URL isn't recognized as an imageurl, a lame HTML
63 * explanation is sent instead.
65 * - Added new feature, which permits blocking remote
66 * script redirects and firing back a local redirect
68 * The feature is conditionally compiled, i.e. it
69 * can be disabled with --disable-fast-redirects,
70 * plus it must be activated by a "fast-redirects"
71 * line in the config file, has its own log level
72 * and of course wants to be displayed by show-proxy-args
73 * Note: Boy, all the #ifdefs in 1001 locations and
74 * all the fumbling with configure.in and acconfig.h
75 * were *way* more work than the feature itself :-(
77 * - Because a generic redirect template was needed for
78 * this, tinygif = 3 now uses the same.
80 * - Moved GIFs, and other static HTTP response templates
85 * - Removed some >400 CRs again (Jon, you really worked
88 * Revision 1.3 2001/05/20 16:44:47 jongfoster
89 * Removing last hardcoded JunkBusters.com URLs.
91 * Revision 1.2 2001/05/20 01:21:20 jongfoster
92 * Version 2.9.4 checkin.
93 * - Merged popupfile and cookiefile, and added control over PCRS
94 * filtering, in new "permissionsfile".
95 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
96 * file error you now get a message box (in the Win32 GUI) rather
97 * than the program exiting with no explanation.
98 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
100 * - Removed tabs from "config"
101 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
102 * - Bumped up version number.
104 * Revision 1.1.1.1 2001/05/15 13:58:52 oes
105 * Initial import of version 2.9.3 source tree
108 *********************************************************************/
114 #include <sys/types.h>
121 #include <netinet/in.h>
123 #include <winsock2.h>
130 #include "showargs.h"
134 #include "jbsockets.h"
136 #include "jbsockets.h"
142 const char filters_h_rcs[] = FILTERS_H_VERSION;
144 /* Fix a problem with Solaris. There should be no effect on other
146 * Solaris's isspace() is a macro which uses it's argument directly
147 * as an array index. Therefore we need to make sure that high-bit
148 * characters generate +ve values, and ideally we also want to make
149 * the argument match the declared parameter type of "int".
151 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
154 static const char CBLOCK[] =
156 "HTTP/1.0 403 Request for blocked URL\n"
157 #else /* ifndef AMIGA */
158 "HTTP/1.0 202 Request for blocked URL\n"
159 #endif /* ndef AMIGA */
161 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
162 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
163 "Content-Type: text/html\n\n"
166 "<title>Internet Junkbuster: Request for blocked URL</title>\n"
172 "<p align=center>Your request for <b>%s%s</b><br>\n"
173 "was blocked because it matches the following pattern "
174 "in the blockfile: <b>%s</b>\n</p>"
176 "<p align=center><a href=\"http://%s" FORCE_PREFIX
177 "%s\">Go there anyway.</a></p>"
178 #endif /* def FORCE_LOAD */
183 static const char CTRUST[] =
185 "HTTP/1.0 403 Request for untrusted URL\n"
186 #else /* ifndef AMIGA */
187 "HTTP/1.0 202 Request for untrusted URL\n"
188 #endif /* ndef AMIGA */
190 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
191 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
192 "Content-Type: text/html\n\n"
195 "<title>Internet Junkbuster: Request for untrusted URL</title>\n"
199 "<a href=http://internet.junkbuster.com/ij-untrusted-url?%s+%s+%s>"
205 #endif /* def TRUST_FILES */
209 /*********************************************************************
211 * Function : block_acl
213 * Description : Block this request?
214 * Decide yes or no based on ACL file.
217 * 1 : src = Address the browser/user agent is requesting.
218 * 2 : dst = The proxy or gateway address this is going to.
219 * 3 : csp = Current client state (buffers, headers, etc...)
221 * Returns : 0 = FALSE (don't block) and 1 = TRUE (do block)
223 *********************************************************************/
224 int block_acl(struct access_control_addr *src, struct access_control_addr *dst, struct client_state *csp)
226 struct file_list *fl;
227 struct access_control_list *a, *acl;
228 struct access_control_addr s[1], d[1];
230 /* if not using an access control list, then permit the connection */
231 if (((fl = csp->alist) == NULL) || ((acl = fl->f) == NULL))
236 /* search the list */
237 for (a = acl->next ; a ; a = a->next)
242 s->addr &= a->src->mask;
243 d->addr &= a->dst->mask;
245 if ((s->addr == a->src->addr)
246 && (d->addr == a->dst->addr)
247 && ((s->port == a->src->port)
249 || (a->src->port == 0))
250 && ((d->port == a->dst->port)
252 || (a->dst->port == 0)))
254 if (a->action == ACL_PERMIT)
270 /*********************************************************************
272 * Function : acl_addr
274 * Description : Called from `load_aclfile'. FIXME: I can't say more.
277 * 1 : aspec = (what?)
280 * Returns : 0 => Ok, everything else is an error.
282 *********************************************************************/
283 int acl_addr(char *aspec, struct access_control_addr *aca)
285 int i, masklength, port;
291 if ((p = strchr(aspec, '/')))
295 if (ijb_isdigit(*p) == 0)
299 masklength = atoi(p);
302 if ((masklength < 0) || (masklength > 32))
307 if ((p = strchr(aspec, ':')))
311 if (ijb_isdigit(*p) == 0)
320 aca->addr = ntohl(resolve_hostname_to_ip(aspec));
324 log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec);
328 /* build the netmask */
330 for (i=1; i <= masklength ; i++)
332 aca->mask |= (1 << (32 - i));
335 /* now mask off the host portion of the ip address
336 * (i.e. save on the network portion of the address).
338 aca->addr = aca->addr & aca->mask;
343 #endif /* def ACL_FILES */
346 /*********************************************************************
348 * Function : block_url
350 * Description : Called from `chat'. Check to see if we need to block this.
353 * 1 : http = http_request request to "check" for blocked
354 * 2 : csp = Current client state (buffers, headers, etc...)
356 * Returns : NULL => unblocked, else string to HTML block description.
358 *********************************************************************/
359 char *block_url(struct http_request *http, struct client_state *csp)
361 struct file_list *fl;
362 struct block_spec *b;
363 struct url_spec url[1];
367 if (((fl = csp->blist) == NULL) || ((b = fl->f) == NULL))
372 *url = dsplit(http->host);
374 /* if splitting the domain fails, punt */
375 if (url->dbuf == NULL) return(NULL);
377 for (b = b->next; b ; b = b->next)
379 if ((b->url->port == 0) || (b->url->port == http->port))
381 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
383 if ((b->url->path == NULL) ||
385 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
387 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
394 if (b->reject == 0) return(NULL);
397 n += strlen(http->hostport);
398 n += strlen(http->path);
399 n += strlen(b->url->spec);
401 n += strlen(http->hostport);
402 n += strlen(http->path);
403 #endif /* def FORCE_LOAD */
405 p = (char *)malloc(n);
408 sprintf(p, CBLOCK, http->hostport, http->path, b->url->spec, http->hostport, http->path);
410 sprintf(p, CBLOCK, http->hostport, http->path, b->url->spec);
411 #endif /* def FORCE_LOAD */
425 #if defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST)
426 /*********************************************************************
428 * Function : block_imageurl
430 * Description : Given a URL which is blocked, decide whether to
431 * send the "blocked" image or HTML.
434 * 1 : http = URL to check.
435 * 2 : csp = Current client state (buffers, headers, etc...)
437 * Returns : True (nonzero) if URL is in image list, false (0)
440 *********************************************************************/
441 int block_imageurl(struct http_request *http, struct client_state *csp)
443 #ifdef DETECT_MSIE_IMAGES
444 if ((csp->accept_types
445 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
446 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE))
450 else if ((csp->accept_types
451 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
452 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_HTML))
458 #if defined(USE_IMAGE_LIST)
459 return block_imageurl_using_imagelist(http, csp);
461 /* Don't know - assume HTML */
465 #endif /* defined(DETECT_MSIE_IMAGES) || defined(USE_IMAGE_LIST) */
468 #ifdef USE_IMAGE_LIST
469 /*********************************************************************
471 * Function : block_imageurl
473 * Description : Test if a URL is in the imagelist.
476 * 1 : http = URL to check.
477 * 2 : csp = Current client state (buffers, headers, etc...)
479 * Returns : True (nonzero) if URL is in image list, false (0)
482 *********************************************************************/
483 int block_imageurl_using_imagelist(struct http_request *http, struct client_state *csp)
485 struct file_list *fl;
486 struct block_spec *b;
487 struct url_spec url[1];
489 if (((fl = csp->ilist) == NULL) || ((b = fl->f) == NULL))
494 *url = dsplit(http->host);
496 /* if splitting the domain fails, punt */
497 if (url->dbuf == NULL) return(0);
499 for (b = b->next; b ; b = b->next)
502 if ((b->url->port == 0) || (b->url->port == http->port))
504 /* port matches, check domain */
505 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
507 /* domain matches, check path */
508 if ((b->url->path == NULL) ||
510 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
512 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
520 if (b->reject == 0) return(0);
532 #endif /* def USE_IMAGE_LIST */
536 /*********************************************************************
538 * Function : re_process_buffer
540 * Description : Apply all jobs from the joblist (aka. Perl regexp's) to
541 * the text buffer that's been accumulated in csp->iob->buf.
542 * Then, write the modified buffer out to the client
543 * (Maybe this should happen from jcc.c via flush_socket
544 * for better readability).
547 * 1 : csp = Current client state (buffers, headers, etc...)
551 *********************************************************************/
552 void re_process_buffer(struct client_state *csp)
555 int size = csp->iob->eod - csp->iob->cur;
556 char *old=csp->iob->cur, *new = NULL;
557 pcrs_job *job, *joblist;
559 struct file_list *fl;
560 struct re_filterfile_spec *b;
562 /* Sanity first ;-) */
568 if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
570 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
574 joblist = b->joblist;
577 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
578 csp->http->hostport, csp->http->path, size);
580 /* Apply all jobs from the joblist */
581 for (job = joblist; NULL != job; job = job->next)
583 hits += pcrs_exec_substitution(job, old, size, &new, &size);
584 if (old != csp->iob->cur) free(old);
588 log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
590 if (write_socket(csp->cfd, old, size) != size)
592 log_error(LOG_LEVEL_ERROR, "write to client failed.");
595 /* fwiw, reset the iob */
601 #endif /* def PCRS */
605 /*********************************************************************
607 * Function : trust_url
609 * Description : Should we "trust" this URL? See "trustfile" line in config.
612 * 1 : http = http_request request for requested URL
613 * 2 : csp = Current client state (buffers, headers, etc...)
615 * Returns : NULL => trusted, else string to HTML "untrusted" description.
617 *********************************************************************/
618 char *trust_url(struct http_request *http, struct client_state *csp)
620 struct file_list *fl;
621 struct block_spec *b;
622 struct url_spec url[1], **tl, *t;
624 char *hostport, *path, *refer;
625 struct http_request rhttp[1];
628 if (((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL))
633 *url = dsplit(http->host);
635 /* if splitting the domain fails, punt */
636 if (url->dbuf == NULL) return(NULL);
638 memset(rhttp, '\0', sizeof(*rhttp));
640 for (b = b->next; b ; b = b->next)
642 if ((b->url->port == 0) || (b->url->port == http->port))
644 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
646 if ((b->url->path == NULL) ||
648 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
650 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
657 if (b->reject == 0) return(NULL);
659 hostport = url_encode(http->hostport);
660 path = url_encode(http->path);
664 refer = url_encode(csp->referrer);
668 refer = url_encode("undefined");
672 n += strlen(hostport);
676 p = (char *)malloc(n);
678 sprintf(p, CTRUST, hostport, path, refer);
693 if ((csp->referrer == NULL)|| (strlen(csp->referrer) <= 9))
695 /* no referrer was supplied */
696 goto trust_url_not_trusted;
699 /* forge a URL from the referrer so we can use
700 * convert_url() to parse it into its components.
704 p = strsav(p, "GET ");
705 p = strsav(p, csp->referrer + 9); /* skip over "Referer: " */
706 p = strsav(p, " HTTP/1.0");
708 parse_http_request(p, rhttp, csp);
710 if (rhttp->cmd == NULL)
713 goto trust_url_not_trusted;
718 *url = dsplit(rhttp->host);
720 /* if splitting the domain fails, punt */
721 if (url->dbuf == NULL) goto trust_url_not_trusted;
723 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
725 if ((t->port == 0) || (t->port == rhttp->port))
727 if ((t->domain[0] == '\0') || domaincmp(t, url) == 0)
729 if ((t->path == NULL) ||
731 (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0)
733 (strncmp(t->path, rhttp->path, t->pathlen) == 0)
737 /* if the URL's referrer is from a trusted referrer, then
738 * add the target spec to the trustfile as an unblocked
739 * domain and return NULL (which means it's OK).
747 if ((fp = fopen(csp->config->trustfile, "a")))
752 h = strsav(h, http->hostport);
758 /* since this path points into a user's home space
759 * be sure to include this spec in the trustfile.
761 if ((p = strchr(p, '/')))
764 h = strsav(h, http->path);
769 free_http_request(rhttp);
771 fprintf(fp, "%s\n", h);
781 trust_url_not_trusted:
782 free_http_request(rhttp);
784 hostport = url_encode(http->hostport);
785 path = url_encode(http->path);
789 refer = url_encode(csp->referrer);
793 refer = url_encode("undefined");
797 n += strlen(hostport);
801 p = (char *)malloc(n);
802 sprintf(p, CTRUST, hostport, path, refer);
811 #endif /* def TRUST_FILES */
814 /*********************************************************************
816 * Function : intercept_url
818 * Description : checks the URL `basename' against a list of URLs to
819 * snarf. If it matches, it calls the associated function
820 * which returns an HTML page to send back to the client.
821 * Right now, we snarf:
822 * "show-proxy-args", and
823 * "ij-untrusted-url" (optional w/TRUST_FILES)
826 * 1 : http = http_request request, check `basename's of blocklist
827 * 2 : csp = Current client state (buffers, headers, etc...)
829 * Returns : NULL for no recognized URLs, or an HTML description page.
831 *********************************************************************/
832 char *intercept_url(struct http_request *http, struct client_state *csp)
835 const struct interceptors *v;
837 basename = strrchr(http->path, '/');
839 if (basename == NULL) return(NULL);
841 basename ++; /* first char past the last slash */
845 for (v = intercept_patterns; v->str; v++)
847 if (strncmp(basename, v->str, v->len) == 0)
849 return((v->interceptor)(http, csp));
858 #ifdef FAST_REDIRECTS
859 /*********************************************************************
861 * Function : redirect_url
863 * Description : Checks for redirection URLs and returns a HTTP redirect
864 * to the destination URL.
867 * 1 : http = http_request request, check `basename's of blocklist
868 * 2 : csp = Current client state (buffers, headers, etc...)
870 * Returns : NULL if URL was clean, HTTP redirect otherwise.
872 *********************************************************************/
873 char *redirect_url(struct http_request *http, struct client_state *csp)
877 p = q = csp->http->path;
878 log_error(LOG_LEVEL_REDIRECTS, "checking path: %s", p);
880 /* find the last URL encoded in the request */
881 while (p = strstr(p, "http://"))
886 /* if there was any, generate and return a HTTP redirect */
887 if (q != csp->http->path)
889 log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
891 p = (char *)malloc(strlen(HTTP_REDIRECT_TEMPLATE) + strlen(q));
892 sprintf(p, HTTP_REDIRECT_TEMPLATE, q);
901 #endif /* def FAST_REDIRECTS */
903 /*********************************************************************
905 * Function : url_permissions
907 * Description : Gets the permissions for this URL.
910 * 1 : http = http_request request for blocked URLs
911 * 2 : csp = Current client state (buffers, headers, etc...)
913 * Returns : permissions bitmask specifiying what this URL can do.
914 * If not on list, will be default_permissions.
916 *********************************************************************/
917 int url_permissions(struct http_request *http, struct client_state *csp)
919 struct file_list *fl;
920 struct permissions_spec *b;
921 struct url_spec url[1];
923 if (((fl = csp->permissions_list) == NULL) || ((b = fl->f) == NULL))
925 return(csp->config->default_permissions);
928 *url = dsplit(http->host);
930 /* if splitting the domain fails, punt */
931 if (url->dbuf == NULL)
933 return(csp->config->default_permissions);
936 for (b = b->next; NULL != b; b = b->next)
938 if ((b->url->port == 0) || (b->url->port == http->port))
940 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
942 if ((b->url->path == NULL) ||
944 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
946 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
952 return(b->permissions);
960 return(csp->config->default_permissions);
965 /*********************************************************************
967 * Function : forward_url
969 * Description : Should we forward this to another proxy?
972 * 1 : http = http_request request for current URL
973 * 2 : csp = Current client state (buffers, headers, etc...)
975 * Returns : Return gw_default for no forward match,
976 * else a gateway pointer to a specific forwarding proxy.
978 *********************************************************************/
979 const struct gateway *forward_url(struct http_request *http, struct client_state *csp)
981 struct file_list *fl;
982 struct forward_spec *b;
983 struct url_spec url[1];
985 if (((fl = csp->flist) == NULL) || ((b = fl->f) == NULL))
990 *url = dsplit(http->host);
992 /* if splitting the domain fails, punt */
993 if (url->dbuf == NULL) return(gw_default);
995 for (b = b->next; b ; b = b->next)
997 if ((b->url->port == 0) || (b->url->port == http->port))
999 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
1001 if ((b->url->path == NULL) ||
1003 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
1005 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
1024 /*********************************************************************
1028 * Description : Takes a domain and returns a pointer to a url_spec
1029 * structure populated with dbuf, dcnt and dvec. The
1030 * other fields in the structure that is returned are zero.
1033 * 1 : domain = a URL address
1035 * Returns : url_spec structure populated with dbuf, dcnt and dvec.
1037 *********************************************************************/
1038 struct url_spec dsplit(char *domain)
1040 struct url_spec ret[1];
1045 memset(ret, '\0', sizeof(*ret));
1047 if ((p = strrchr(domain, '.')))
1055 ret->dbuf = strdup(domain);
1057 /* map to lower case */
1058 for (p = ret->dbuf; *p ; p++) *p = tolower(*p);
1060 /* split the domain name into components */
1061 ret->dcnt = ssplit(ret->dbuf, ".", v, SZ(v), 1, 1);
1065 memset(ret, '\0', sizeof(ret));
1069 /* save a copy of the pointers in dvec */
1070 size = ret->dcnt * sizeof(*ret->dvec);
1072 if ((ret->dvec = (char **)malloc(size)))
1074 memcpy(ret->dvec, v, size);
1082 /*********************************************************************
1084 * Function : domaincmp
1086 * Description : Compare domain names.
1087 * domaincmp("a.b.c" , "a.b.c") => 0 (MATCH)
1088 * domaincmp("a*.b.c", "a.b.c") => 0 (MATCH)
1089 * domaincmp("b.c" , "a.b.c") => 0 (MATCH)
1090 * domaincmp("" , "a.b.c") => 0 (MATCH)
1093 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1094 * 2 : fqdn = domain name against which the patterns are compared.
1096 * Returns : 0 => domains are equivalent, else no match.
1098 *********************************************************************/
1099 int domaincmp(struct url_spec *pattern, struct url_spec *fqdn)
1101 char **pv, **fv; /* vectors */
1102 int pn, fn; /* counters */
1103 char *p, *f; /* chars */
1111 while ((pn > 0) && (fn > 0))
1116 while (*p && *f && (*p == tolower(*f)))
1121 if ((*p != tolower(*f)) && (*p != '*')) return(1);
1124 if (pn > 0) return(1);
1131 /* intercept functions */
1133 /*********************************************************************
1135 * Function : show_proxy_args
1137 * Description : This "crunch"es "http:/any.thing/show-proxy-args" and
1138 * returns a web page describing the current status of IJB.
1141 * 1 : http = ignored
1142 * 2 : csp = Current client state (buffers, headers, etc...)
1144 * Returns : A string that contains the current status of IJB.
1146 *********************************************************************/
1147 char *show_proxy_args(struct http_request *http, struct client_state *csp)
1151 #ifdef SPLIT_PROXY_ARGS
1155 const char * filename = NULL;
1156 const char * file_description = NULL;
1157 char * query_string = strrchr(http->path, '?');
1158 char which_file = '\0';
1161 if (query_string != NULL)
1163 /* first char past the last '?' (maybe '\0')*/
1164 which_file = query_string[1];
1171 filename = csp->blist->filename;
1172 file_description = "Block List";
1176 if (csp->permissions_list)
1178 filename = csp->permissions_list->filename;
1179 file_description = "Permissions List";
1185 filename = csp->flist->filename;
1186 file_description = "Forward List";
1194 filename = csp->alist->filename;
1195 file_description = "Access Control List";
1198 #endif /* def ACL_FILES */
1200 #ifdef USE_IMAGE_LIST
1204 filename = csp->ilist->filename;
1205 file_description = "Image List";
1208 #endif /* def USE_IMAGE_LIST */
1214 filename = csp->rlist->filename;
1215 file_description = "RE Filter List";
1218 #endif /* def PCRS */
1224 filename = csp->tlist->filename;
1225 file_description = "Trust List";
1228 #endif /* def TRUST_FILES */
1233 /* Display specified file */
1234 /* FIXME: Add HTTP headers so this isn't cached */
1237 "Server: IJ/" VERSION "\n"
1238 "Content-type: text/html\n"
1239 "Pragma: no-cache\n"
1240 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1241 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1246 "<title>Internet Junkbuster Proxy Status - ");
1247 s = strsav(s, file_description);
1251 "<body bgcolor=\"#f8f8f0\" link=\"#000078\" alink=\"#ff0022\" vlink=\"#787878\">\n"
1253 "<h1>" BANNER "\n");
1254 s = strsav(s, file_description);
1257 "<p><a href=\"show-proxy-args\">Back to proxy status</a></p>\n"
1259 s = strsav(s, file_description);
1262 "Contents of file "<code>");
1263 p = html_encode(filename);
1267 "</code>":<br>\n"
1271 if ((fp = fopen(filename, "r")) == NULL)
1273 s = strsav(s, "</pre><h1>ERROR OPENING FILE!</h1><pre>");
1277 while (fgets(buf, sizeof(buf), fp))
1279 p = html_encode(buf);
1284 s = strsav(s, "<br>");
1293 "<p><a href=\"show-proxy-args\">Back to proxy status</a></p>\n"
1295 "<small><small><p>\n"
1296 "The " BANNER " Proxy - \n"
1297 "<a href=\"" HOME_PAGE_URL "\">" HOME_PAGE_URL "</a>\n"
1299 "</body></html>\n");
1302 #endif /* def SPLIT_PROXY_ARGS */
1304 s = strsav(s, csp->config->proxy_args->header);
1305 s = strsav(s, csp->config->proxy_args->invocation);
1308 #endif /* def STATISTICS */
1309 s = strsav(s, csp->config->proxy_args->gateways);
1311 #ifdef SPLIT_PROXY_ARGS
1313 "<h2>The following files are in use:</h2>\n"
1314 "<p>(Click a filename to view it)</p>\n"
1319 s = strsav(s, "<li>Block List: <a href=\"show-proxy-args?block\"><code>");
1320 s = strsav(s, csp->blist->filename);
1321 s = strsav(s, "</code></a></li>\n");
1324 if (csp->permissions_list)
1326 s = strsav(s, "<li>Permissions List: <a href=\"show-proxy-args?permit\"><code>");
1327 s = strsav(s, csp->permissions_list->filename);
1328 s = strsav(s, "</code></a></li>\n");
1333 s = strsav(s, "<li>Forward List: <a href=\"show-proxy-args?forward\"><code>");
1334 s = strsav(s, csp->flist->filename);
1335 s = strsav(s, "</code></a></li>\n");
1341 s = strsav(s, "<li>Access Control List: <a href=\"show-proxy-args?acl\"><code>");
1342 s = strsav(s, csp->alist->filename);
1343 s = strsav(s, "</code></a></li>\n");
1345 #endif /* def ACL_FILES */
1347 #ifdef USE_IMAGE_LIST
1350 s = strsav(s, "<li>Image List: <a href=\"show-proxy-args?image\"><code>");
1351 s = strsav(s, csp->ilist->filename);
1352 s = strsav(s, "</code></a></li>\n");
1354 #endif /* def USE_IMAGE_LIST */
1359 s = strsav(s, "<li>RE Filter List: <a href=\"show-proxy-args?re\"><code>");
1360 s = strsav(s, csp->rlist->filename);
1361 s = strsav(s, "</code></a></li>\n");
1363 #endif /* def PCRS */
1368 s = strsav(s, "<li>Trust List: <a href=\"show-proxy-args?trust\"><code>");
1369 s = strsav(s, csp->tlist->filename);
1370 s = strsav(s, "</code></a></li>\n");
1372 #endif /* def TRUST_FILES */
1374 s = strsav(s, "</ul>");
1376 #else /* ifndef SPLIT_PROXY_ARGS */
1379 s = strsav(s, csp->blist->proxy_args);
1384 s = strsav(s, csp->clist->proxy_args);
1389 s = strsav(s, csp->flist->proxy_args);
1395 s = strsav(s, csp->alist->proxy_args);
1397 #endif /* def ACL_FILES */
1399 #ifdef USE_IMAGE_LIST
1402 s = strsav(s, csp->ilist->proxy_args);
1404 #endif /* def USE_IMAGE_LIST */
1409 s = strsav(s, csp->rlist->proxy_args);
1411 #endif /* def PCRS */
1416 s = strsav(s, csp->tlist->proxy_args);
1418 #endif /* def TRUST_FILES */
1420 #endif /* ndef SPLIT_PROXY_ARGS */
1422 s = strsav(s, csp->config->proxy_args->trailer);
1429 /*********************************************************************
1431 * Function : ijb_send_banner
1433 * Description : This "crunch"es "http:/any.thing/ijb-send-banner and
1434 * thus triggers sending the image in jcc.c:chat.
1435 * For the unlikely case, that the imagefile/MSIE
1436 * mechanism is not used, or tinygif = 0, a page
1437 * describing the reson of the interception is generated.
1440 * 1 : http = http_request request for crunched URL
1441 * 2 : csp = Current client state (buffers, headers, etc...)
1443 * Returns : A string that contains why this was intercepted.
1445 *********************************************************************/
1446 char *ijb_send_banner(struct http_request *http, struct client_state *csp)
1448 return(strdup(CNOBANNER));
1452 /*********************************************************************
1454 * Function : ij_untrusted_url
1456 * Description : This "crunch"es "http:/any.thing/ij-untrusted-url" and
1457 * returns a web page describing why it was untrusted.
1460 * 1 : http = http_request request for crunched URL
1461 * 2 : csp = Current client state (buffers, headers, etc...)
1463 * Returns : A string that contains why this was untrusted.
1465 *********************************************************************/
1466 char *ij_untrusted_url(struct http_request *http, struct client_state *csp)
1469 char *hostport, *path, *refer, *p, *v[9];
1471 struct url_spec **tl, *t;
1474 static const char format[] =
1475 "HTTP/1.0 200 OK\r\n"
1476 "Pragma: no-cache\n"
1477 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1478 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
1479 "Content-Type: text/html\n\n"
1482 "<title>Internet Junkbuster: Request for untrusted URL</title>\n"
1488 "The " BANNER " Proxy "
1489 "<A href=\"" HOME_PAGE_URL "\">"
1490 "(" HOME_PAGE_URL ") </A>"
1491 "intercepted the request for %s%s\n"
1492 "because the URL is not trusted.\n"
1495 if ((n = ssplit(http->path, "?+", v, SZ(v), 0, 0)) == 4)
1497 hostport = url_decode(v[1]);
1498 path = url_decode(v[2]);
1499 refer = url_decode(v[3]);
1503 hostport = strdup("undefined_host");
1504 path = strdup("/undefined_path");
1505 refer = strdup("undefined");
1509 n += strlen(hostport);
1512 if ((p = (char *)malloc(n)))
1514 sprintf(p, format, hostport, path);
1517 strsav(p, "The referrer in this request was <strong>");
1519 strsav(p, "</strong><br>\n");
1525 p = strsav(p, "<h3>The following referrers are trusted</h3>\n");
1527 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
1529 sprintf(buf, "%s<br>\n", t->spec);
1533 if (csp->config->trust_info->next)
1539 "You can learn more about what this means "
1540 "and what you may be able to do about it by "
1541 "reading the following documents:<br>\n"
1547 for (l = csp->config->trust_info->next; l ; l = l->next)
1550 "<li> <a href=%s>%s</a><br>\n",
1555 p = strsav(p, "</ol>\n");
1558 p = strsav(p, "</body>\n" "</html>\n");
1563 #endif /* def TRUST_FILES */
1567 /*********************************************************************
1569 * Function : add_stats
1571 * Description : Statistics function of JB. Called by `show_proxy_args'.
1574 * 1 : s = string that holds the proxy args description page
1576 * Returns : A pointer to the descriptive status web page.
1578 *********************************************************************/
1579 char *add_stats(char *s)
1582 * Output details of the number of requests rejected and
1583 * accepted. This is switchable in the junkbuster config.
1584 * Does nothing if this option is not enabled.
1587 float perc_rej; /* Percentage of http requests rejected */
1589 int local_urls_read = urls_read;
1590 int local_urls_rejected = urls_rejected;
1593 * Need to alter the stats not to include the fetch of this
1596 * Can't do following thread safely! doh!
1599 * urls_rejected--; * This will be incremented subsequently *
1602 s = strsav(s,"<h2>Statistics for this " BANNER ":</h2>\n");
1604 if (local_urls_read == 0)
1607 s = strsav(s,"No activity so far!\n");
1613 perc_rej = (float)local_urls_rejected * 100.0F /
1614 (float)local_urls_read;
1617 "%d requests received, %d filtered "
1620 local_urls_rejected, perc_rej);
1622 s = strsav(s,out_str);
1627 #endif /* def STATISTICS */