1 const char filters_rcs[] = "$Id: filters.c,v 1.17 2001/06/09 10:55:28 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_actions', `domaincmp', `dsplit',
10 * `filter_popups', `forward_url', 'redirect_url',
11 * `ij_untrusted_url', `intercept_url', `re_process_buffer',
12 * `show_proxy_args', 'ijb_send_banner', and `trust_url'
14 * Copyright : Written by and Copyright (C) 2001 the SourceForge
15 * IJBSWA team. http://ijbswa.sourceforge.net
17 * Based on the Internet Junkbuster originally written
18 * by and Copyright (C) 1997 Anonymous Coders and
19 * Junkbusters Corporation. http://www.junkbusters.com
21 * This program is free software; you can redistribute it
22 * and/or modify it under the terms of the GNU General
23 * Public License as published by the Free Software
24 * Foundation; either version 2 of the License, or (at
25 * your option) any later version.
27 * This program is distributed in the hope that it will
28 * be useful, but WITHOUT ANY WARRANTY; without even the
29 * implied warranty of MERCHANTABILITY or FITNESS FOR A
30 * PARTICULAR PURPOSE. See the GNU General Public
31 * License for more details.
33 * The GNU General Public License should be included with
34 * this file. If not, you can view it at
35 * http://www.gnu.org/copyleft/gpl.html
36 * or write to the Free Software Foundation, Inc., 59
37 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 * Revision 1.17 2001/06/09 10:55:28 jongfoster
42 * Changing BUFSIZ ==> BUFFER_SIZE
44 * Revision 1.16 2001/06/07 23:10:26 jongfoster
45 * Allowing unanchored domain patterns to back off and retry
46 * if they partially match. Optimized right-anchored patterns.
47 * Moving ACL and forward files into config file.
48 * Replacing struct gateway with struct forward_spec
50 * Revision 1.15 2001/06/03 19:12:00 oes
51 * extracted-CGI relevant stuff
53 * Revision 1.14 2001/06/01 10:30:55 oes
54 * Added optional left-anchoring to domaincmp
56 * Revision 1.13 2001/05/31 21:21:30 jongfoster
57 * Permissionsfile / actions file changes:
58 * - Changed "permission" to "action" throughout
59 * - changes to file format to allow string parameters
60 * - Moved helper functions to actions.c
62 * Revision 1.12 2001/05/31 17:35:20 oes
64 * - Enhanced domain part globbing with infix and prefix asterisk
65 * matching and optional unanchored operation
67 * Revision 1.11 2001/05/29 11:53:23 oes
68 * "See why" link added to "blocked" page
70 * Revision 1.10 2001/05/29 09:50:24 jongfoster
71 * Unified blocklist/imagelist/permissionslist.
72 * File format is still under discussion, but the internal changes
75 * Also modified interceptor behaviour:
76 * - We now intercept all URLs beginning with one of the following
77 * prefixes (and *only* these prefixes):
79 * * http://ijbswa.sf.net/config/
80 * * http://ijbswa.sourceforge.net/config/
81 * - New interceptors "home page" - go to http://i.j.b/ to see it.
82 * - Internal changes so that intercepted and fast redirect pages
83 * are not replaced with an image.
84 * - Interceptors now have the option to send a binary page direct
85 * to the client. (i.e. ijb-send-banner uses this)
86 * - Implemented show-url-info interceptor. (Which is why I needed
87 * the above interceptors changes - a typical URL is
88 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
89 * The previous mechanism would not have intercepted that, and
90 * if it had been intercepted then it then it would have replaced
93 * Revision 1.9 2001/05/27 22:17:04 oes
95 * - re_process_buffer no longer writes the modified buffer
96 * to the client, which was very ugly. It now returns the
97 * buffer, which it is then written by chat.
99 * - content_length now adjusts the Content-Length: header
100 * for modified documents rather than crunch()ing it.
101 * (Length info in csp->content_length, which is 0 for
102 * unmodified documents)
104 * - For this to work, sed() is called twice when filtering.
106 * Revision 1.8 2001/05/26 17:13:28 jongfoster
107 * Filled in a function comment.
109 * Revision 1.7 2001/05/26 15:26:15 jongfoster
110 * ACL feature now provides more security by immediately dropping
111 * connections from untrusted hosts.
113 * Revision 1.6 2001/05/26 00:28:36 jongfoster
114 * Automatic reloading of config file.
115 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
116 * Most of the global variables have been moved to a new
117 * struct configuration_spec, accessed through csp->config->globalname
118 * Most of the globals remaining are used by the Win32 GUI.
120 * Revision 1.5 2001/05/25 22:34:30 jongfoster
123 * Revision 1.4 2001/05/22 18:46:04 oes
125 * - Enabled filtering banners by size rather than URL
126 * by adding patterns that replace all standard banner
127 * sizes with the "Junkbuster" gif to the re_filterfile
129 * - Enabled filtering WebBugs by providing a pattern
130 * which kills all 1x1 images
132 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
133 * which is selected by the (nonstandard and therefore
134 * capital) letter 'U' in the option string.
135 * It causes the quantifiers to be ungreedy by default.
136 * Appending a ? turns back to greedy (!).
138 * - Added a new interceptor ijb-send-banner, which
139 * sends back the "Junkbuster" gif. Without imagelist or
140 * MSIE detection support, or if tinygif = 1, or the
141 * URL isn't recognized as an imageurl, a lame HTML
142 * explanation is sent instead.
144 * - Added new feature, which permits blocking remote
145 * script redirects and firing back a local redirect
147 * The feature is conditionally compiled, i.e. it
148 * can be disabled with --disable-fast-redirects,
149 * plus it must be activated by a "fast-redirects"
150 * line in the config file, has its own log level
151 * and of course wants to be displayed by show-proxy-args
152 * Note: Boy, all the #ifdefs in 1001 locations and
153 * all the fumbling with configure.in and acconfig.h
154 * were *way* more work than the feature itself :-(
156 * - Because a generic redirect template was needed for
157 * this, tinygif = 3 now uses the same.
159 * - Moved GIFs, and other static HTTP response templates
164 * - Removed some >400 CRs again (Jon, you really worked
167 * Revision 1.3 2001/05/20 16:44:47 jongfoster
168 * Removing last hardcoded JunkBusters.com URLs.
170 * Revision 1.2 2001/05/20 01:21:20 jongfoster
171 * Version 2.9.4 checkin.
172 * - Merged popupfile and cookiefile, and added control over PCRS
173 * filtering, in new "permissionsfile".
174 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
175 * file error you now get a message box (in the Win32 GUI) rather
176 * than the program exiting with no explanation.
177 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
179 * - Removed tabs from "config"
180 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
181 * - Bumped up version number.
183 * Revision 1.1.1.1 2001/05/15 13:58:52 oes
184 * Initial import of version 2.9.3 source tree
187 *********************************************************************/
193 #include <sys/types.h>
200 #include <netinet/in.h>
202 #include <winsock2.h>
209 #include "showargs.h"
213 #include "jbsockets.h"
215 #include "jbsockets.h"
216 #include "miscutil.h"
225 const char filters_h_rcs[] = FILTERS_H_VERSION;
227 /* Fix a problem with Solaris. There should be no effect on other
229 * Solaris's isspace() is a macro which uses it's argument directly
230 * as an array index. Therefore we need to make sure that high-bit
231 * characters generate +ve values, and ideally we also want to make
232 * the argument match the declared parameter type of "int".
234 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
238 /*********************************************************************
240 * Function : block_acl
242 * Description : Block this request?
243 * Decide yes or no based on ACL file.
246 * 1 : dst = The proxy or gateway address this is going to.
247 * Or NULL to check all possible targets.
248 * 2 : csp = Current client state (buffers, headers, etc...)
249 * Also includes the client IP address.
251 * Returns : 0 = FALSE (don't block) and 1 = TRUE (do block)
253 *********************************************************************/
254 int block_acl(struct access_control_addr *dst,
255 struct client_state *csp)
257 struct access_control_list *acl = csp->config->acl;
259 /* if not using an access control list, then permit the connection */
265 /* search the list */
268 if ((csp->ip_addr_long & acl->src->mask) == acl->src->addr)
272 /* Just want to check if they have any access */
273 if (acl->action == ACL_PERMIT)
278 else if ( ((dst->addr & acl->dst->mask) == acl->dst->addr)
279 && ((dst->port == acl->dst->port) || (acl->dst->port == 0)))
281 if (acl->action == ACL_PERMIT)
299 /*********************************************************************
301 * Function : acl_addr
303 * Description : Called from `load_aclfile' to parse an ACL address.
306 * 1 : aspec = String specifying ACL address.
307 * 2 : aca = struct access_control_addr to fill in.
309 * Returns : 0 => Ok, everything else is an error.
311 *********************************************************************/
312 int acl_addr(char *aspec, struct access_control_addr *aca)
314 int i, masklength, port;
320 if ((p = strchr(aspec, '/')))
324 if (ijb_isdigit(*p) == 0)
328 masklength = atoi(p);
331 if ((masklength < 0) || (masklength > 32))
336 if ((p = strchr(aspec, ':')))
340 if (ijb_isdigit(*p) == 0)
349 aca->addr = ntohl(resolve_hostname_to_ip(aspec));
353 log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec);
357 /* build the netmask */
359 for (i=1; i <= masklength ; i++)
361 aca->mask |= (1 << (32 - i));
364 /* now mask off the host portion of the ip address
365 * (i.e. save on the network portion of the address).
367 aca->addr = aca->addr & aca->mask;
372 #endif /* def ACL_FILES */
375 /*********************************************************************
377 * Function : block_url
379 * Description : Called from `chat'. Check to see if we need to block this.
382 * 1 : csp = Current client state (buffers, headers, etc...)
384 * Returns : NULL => unblocked, else HTTP block response
386 *********************************************************************/
387 struct http_response *block_url(struct client_state *csp)
390 struct http_response *rsp;
391 struct map *exports = NULL;
394 * If it's not blocked, don't block it ;-)
396 if ((csp->action->flags & ACTION_BLOCK) == 0)
402 * Else, prepare a response
404 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
410 * If it's an image-url, send back an image or redirect
411 * as specified by the relevant +image action
413 #ifdef IMAGE_BLOCKING
414 if (((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
417 /* determine HOW images should be blocked */
418 p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
420 /* and handle accordingly: */
421 if ((p == NULL) || (0 == strcmpic(p, "logo")))
423 rsp->body = bindup(JBGIF, sizeof(JBGIF));
424 rsp->content_length = sizeof(JBGIF);
425 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
428 else if (0 == strcmpic(p, "blank"))
430 rsp->body = bindup(BLANKGIF, sizeof(BLANKGIF));
431 rsp->content_length = sizeof(BLANKGIF);
432 enlist_unique_header(rsp->headers, "Content-Type", "image/gif");
437 rsp->status = strdup("302 Local Redirect from Junkbuster");
438 enlist_unique_header(rsp->headers, "Location", p);
442 #endif /* def IMAGE_BLOCKING */
445 * Else, generate an HTML "blocked" message:
449 exports = default_exports(csp, NULL);
451 exports = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
453 exports = map_block_killer(exports, "force-support");
454 #endif /* ndef FORCE_LOAD */
456 exports = map(exports, "hostport", 1, csp->http->hostport, 1);
457 exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
458 exports = map(exports, "path", 1, csp->http->path, 1);
459 exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
461 rsp->body = fill_template(csp, "blocked", exports);
464 rsp->status = strdup("403 Request for blocked URL");
467 return(finish_http_response(rsp));
473 /*********************************************************************
475 * Function : trust_url FIXME: I should be called distrust_url
477 * Description : Calls is_untrusted_url to determine if the URL is trusted
478 * and if not, returns a HTTP 304 response with a reject message.
481 * 1 : csp = Current client state (buffers, headers, etc...)
483 * Returns : NULL => trusted, else http_response.
485 *********************************************************************/
486 struct http_response *trust_url(struct client_state *csp)
488 struct http_response *rsp;
489 struct map *exports = NULL;
490 char buf[BUFFER_SIZE], *p = NULL;
491 struct url_spec **tl, *t;
494 * Don't bother to work on trusted URLs
496 if (!is_untrusted_url(csp))
502 * Else, prepare a response:
504 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
508 exports = default_exports(csp, NULL);
511 * Export the host, port, and referrer information
513 exports = map(exports, "hostport", 1, csp->http->hostport, 1);
514 exports = map(exports, "path", 1, csp->http->path, 1);
515 exports = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
516 exports = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
518 if (csp->referrer && strlen(csp->referrer) > 9)
520 exports = map(exports, "referrer", 1, csp->referrer + 9, 1);
521 exports = map(exports, "referrer-html", 1, html_encode(csp->referrer + 9), 0);
525 exports = map(exports, "referrer", 1, "unknown", 1);
526 exports = map(exports, "referrer-html", 1, "unknown", 1);
530 * Export the trust list
532 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
534 sprintf(buf, "<li>%s</li>\n", t->spec);
537 exports = map(exports, "trusted-referrers", 1, p, 0);
541 * Export the trust info, if available
543 if (csp->config->trust_info->next)
547 for (l = csp->config->trust_info->next; l ; l = l->next)
550 "<li> <a href=%s>%s</a><br>\n",
554 exports = map(exports, "trust-info", 1, p, 0);
558 exports = map_block_killer(exports, "have-trust-info");
562 * Export the force prefix or the force conditional block killer
565 exports = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
567 exports = map_block_killer(exports, "force-support");
568 #endif /* ndef FORCE_LOAD */
573 rsp->body = fill_template(csp, "untrusted", exports);
576 return(finish_http_response(rsp));
579 #endif /* def TRUST_FILES */
582 #ifdef FAST_REDIRECTS
583 /*********************************************************************
585 * Function : redirect_url
587 * Description : Checks for redirection URLs and returns a HTTP redirect
588 * to the destination URL, if necessary
591 * 1 : csp = Current client state (buffers, headers, etc...)
593 * Returns : NULL if URL was clean, HTTP redirect otherwise.
595 *********************************************************************/
596 struct http_response *redirect_url(struct client_state *csp)
599 struct http_response *rsp;
601 p = q = csp->http->path;
602 log_error(LOG_LEVEL_REDIRECTS, "checking path for redirects: %s", p);
605 * find the last URL encoded in the request
607 while (p = strstr(p, "http://"))
613 * if there was any, generate and return a HTTP redirect
615 if (q != csp->http->path)
617 log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
619 if (NULL == ( rsp = zalloc(sizeof(*rsp))))
624 rsp->status = strdup("302 Local Redirect from Junkbuster");
625 enlist_unique_header(rsp->headers, "Location", q);
627 return(finish_http_response(rsp));
635 #endif /* def FAST_REDIRECTS */
638 #ifdef IMAGE_BLOCKING
639 /*********************************************************************
641 * Function : is_imageurl
643 * Description : Given a URL, decide whether it is an image or not,
644 * using either the info from a previous +image action
645 * or, #ifdef DETECT_MSIE_IMAGES, the info from the
646 * browser's accept header.
649 * 1 : csp = Current client state (buffers, headers, etc...)
651 * Returns : True (nonzero) if URL is an image, false (0)
654 *********************************************************************/
655 int is_imageurl(struct client_state *csp)
657 #ifdef DETECT_MSIE_IMAGES
658 if ((csp->accept_types
659 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
660 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE))
664 else if ((csp->accept_types
665 & (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_IMAGE|ACCEPT_TYPE_MSIE_HTML))
666 == (ACCEPT_TYPE_IS_MSIE|ACCEPT_TYPE_MSIE_HTML))
672 return ((csp->action->flags & ACTION_IMAGE) != 0);
675 #endif /* def IMAGE_BLOCKING */
679 /*********************************************************************
681 * Function : is_untrusted_url
683 * Description : Should we "distrust" this URL (and block it)?
685 * Yes if it matches a line in the trustfile, or if the
686 * referrer matches a line starting with "+" in the
691 * 1 : csp = Current client state (buffers, headers, etc...)
693 * Returns : 0 => trusted, 1 => untrusted
695 *********************************************************************/
696 int is_untrusted_url(struct client_state *csp)
698 struct file_list *fl;
699 struct block_spec *b;
700 struct url_spec url[1], **tl, *t;
701 struct http_request rhttp[1];
704 if (((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL))
709 *url = dsplit(csp->http->host);
711 /* if splitting the domain fails, punt */
712 if (url->dbuf == NULL) return(0);
714 memset(rhttp, '\0', sizeof(*rhttp));
716 for (b = b->next; b ; b = b->next)
718 if ((b->url->port == 0) || (b->url->port == csp->http->port))
720 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
722 if ((b->url->path == NULL) ||
724 (regexec(b->url->preg, csp->http->path, 0, NULL, 0) == 0)
726 (strncmp(b->url->path, csp->http->path, b->url->pathlen) == 0)
733 if (b->reject == 0) return(0);
744 if ((csp->referrer == NULL)|| (strlen(csp->referrer) <= 9))
746 /* no referrer was supplied */
750 /* forge a URL from the referrer so we can use
751 * convert_url() to parse it into its components.
755 p = strsav(p, "GET ");
756 p = strsav(p, csp->referrer + 9); /* skip over "Referer: " */
757 p = strsav(p, " HTTP/1.0");
759 parse_http_request(p, rhttp, csp);
762 if (rhttp->cmd == NULL)
767 *url = dsplit(rhttp->host);
769 /* if splitting the domain fails, punt */
770 if (url->dbuf == NULL) return(1);
772 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
774 if ((t->port == 0) || (t->port == rhttp->port))
776 if ((t->domain[0] == '\0') || domaincmp(t, url) == 0)
778 if ((t->path == NULL) ||
780 (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0)
782 (strncmp(t->path, rhttp->path, t->pathlen) == 0)
786 /* if the URL's referrer is from a trusted referrer, then
787 * add the target spec to the trustfile as an unblocked
788 * domain and return NULL (which means it's OK).
796 if ((fp = fopen(csp->config->trustfile, "a")))
801 h = strsav(h, csp->http->hostport);
807 /* since this path points into a user's home space
808 * be sure to include this spec in the trustfile.
810 if ((p = strchr(p, '/')))
813 h = strsav(h, csp->http->path); /* FIXME: p?! */
818 fprintf(fp, "%s\n", h);
829 #endif /* def TRUST_FILES */
833 /*********************************************************************
835 * Function : re_process_buffer
837 * Description : Apply all the pcrs jobs from the joblist (re_filterfile)
838 * to the text buffer that's been accumulated in
839 * csp->iob->buf and set csp->content_length to the modified
843 * 1 : csp = Current client state (buffers, headers, etc...)
845 * Returns : a pointer to the (newly allocated) modified buffer.
846 * or an empty string in case something went wrong
848 *********************************************************************/
849 char *re_process_buffer(struct client_state *csp)
852 int size = csp->iob->eod - csp->iob->cur;
854 char *old = csp->iob->cur, *new = NULL;
857 struct file_list *fl;
858 struct re_filterfile_spec *b;
860 /* Sanity first ;-) */
866 if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
868 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
872 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
873 csp->http->hostport, csp->http->path, size);
875 /* Apply all jobs from the joblist */
876 for (job = b->joblist; NULL != job; job = job->next)
878 hits += pcrs_execute(job, old, size, &new, &size);
879 if (old != csp->iob->cur) free(old);
883 log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
885 csp->content_length = size;
887 /* fwiw, reset the iob */
892 #endif /* def PCRS */
895 /*********************************************************************
897 * Function : url_actions
899 * Description : Gets the actions for this URL.
902 * 1 : http = http_request request for blocked URLs
903 * 2 : csp = Current client state (buffers, headers, etc...)
907 *********************************************************************/
908 void url_actions(struct http_request *http,
909 struct client_state *csp)
911 struct file_list *fl;
912 struct url_actions *b;
914 init_current_action(csp->action);
916 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
921 apply_url_actions(csp->action, http, b);
925 /*********************************************************************
927 * Function : apply_url_actions
929 * Description : Applies a list of URL actions.
932 * 1 : action = Destination.
933 * 2 : http = Current URL
934 * 3 : b = list of URL actions to apply
938 *********************************************************************/
939 void apply_url_actions(struct current_action_spec *action,
940 struct http_request *http,
941 struct url_actions *b)
943 struct url_spec url[1];
947 /* Should never happen */
951 *url = dsplit(http->host);
953 /* if splitting the domain fails, punt */
954 if (url->dbuf == NULL)
959 for (b = b->next; NULL != b; b = b->next)
961 if ((b->url->port == 0) || (b->url->port == http->port))
963 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
965 if ((b->url->path == NULL) ||
967 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
969 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
973 merge_current_action(action, b->action);
984 /*********************************************************************
986 * Function : forward_url
988 * Description : Should we forward this to another proxy?
991 * 1 : http = http_request request for current URL
992 * 2 : csp = Current client state (buffers, headers, etc...)
994 * Returns : Pointer to forwarding information.
996 *********************************************************************/
997 const struct forward_spec * forward_url(struct http_request *http,
998 struct client_state *csp)
1000 static const struct forward_spec fwd_default[1] = { 0 }; /* All zeroes */
1001 struct forward_spec *fwd = csp->config->forward;
1002 struct url_spec url[1];
1006 return(fwd_default);
1009 *url = dsplit(http->host);
1011 /* if splitting the domain fails, punt */
1012 if (url->dbuf == NULL)
1014 return(fwd_default);
1019 if ((fwd->url->port == 0) || (fwd->url->port == http->port))
1021 if ((fwd->url->domain[0] == '\0') || (domaincmp(fwd->url, url) == 0))
1023 if ((fwd->url->path == NULL) ||
1025 (regexec(fwd->url->preg, http->path, 0, NULL, 0) == 0)
1027 (strncmp(fwd->url->path, http->path, fwd->url->pathlen) == 0)
1042 return(fwd_default);
1047 /*********************************************************************
1051 * Description : Takes a domain and returns a pointer to a url_spec
1052 * structure populated with dbuf, dcnt and dvec. The
1053 * other fields in the structure that is returned are zero.
1056 * 1 : domain = a URL address
1058 * Returns : url_spec structure populated with dbuf, dcnt and dvec.
1060 *********************************************************************/
1061 struct url_spec dsplit(char *domain)
1063 struct url_spec ret[1];
1064 char *v[BUFFER_SIZE];
1068 memset(ret, '\0', sizeof(*ret));
1070 if (domain[strlen(domain) - 1] == '.')
1072 ret->unanchored |= ANCHOR_RIGHT;
1074 if (domain[0] == '.')
1076 ret->unanchored |= ANCHOR_LEFT;
1079 ret->dbuf = strdup(domain);
1081 /* map to lower case */
1082 for (p = ret->dbuf; *p ; p++) *p = tolower(*p);
1084 /* split the domain name into components */
1085 ret->dcnt = ssplit(ret->dbuf, ".", v, SZ(v), 1, 1);
1089 memset(ret, '\0', sizeof(ret));
1093 /* save a copy of the pointers in dvec */
1094 size = ret->dcnt * sizeof(*ret->dvec);
1096 if ((ret->dvec = (char **)malloc(size)))
1098 memcpy(ret->dvec, v, size);
1107 /*********************************************************************
1109 * Function : simple_domaincmp
1111 * Description : Domain-wise Compare fqdn's. The comparison is
1112 * both left- and right-anchored. The individual
1113 * domain names are compared with simplematch().
1114 * This is only used by domaincmp.
1117 * 1 : pv = array of patterns to compare
1118 * 2 : fv = array of domain components to compare
1119 * 3 : len = length of the arrays (both arrays are the
1120 * same length - if they weren't, it couldn't
1121 * possibly be a match).
1123 * Returns : 0 => domains are equivalent, else no match.
1125 *********************************************************************/
1126 static int simple_domaincmp(char **pv, char **fv, int len)
1130 for (n = 0; n < len; n++)
1132 if (simplematch(pv[n], fv[n]))
1142 /*********************************************************************
1144 * Function : domaincmp
1146 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
1147 * pattern->unachored, the comparison is un-, left-,
1148 * right-anchored, or both.
1149 * The individual domain names are compared with
1153 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1154 * 2 : fqdn = domain name against which the patterns are compared.
1156 * Returns : 0 => domains are equivalent, else no match.
1158 *********************************************************************/
1159 int domaincmp(struct url_spec *pattern, struct url_spec *fqdn)
1161 char **pv, **fv; /* vectors */
1163 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
1165 plen = pattern->dcnt;
1170 /* fqdn is too short to match this pattern */
1177 if (unanchored == ANCHOR_LEFT)
1182 * Convert this into a fully anchored pattern with
1183 * the fqdn and pattern the same length
1185 fv += (flen - plen); /* flen - plen >= 0 due to check above */
1186 return simple_domaincmp(pv, fv, plen);
1188 else if (unanchored == 0)
1190 /* Fully anchored, check length */
1195 return simple_domaincmp(pv, fv, plen);
1197 else if (unanchored == ANCHOR_RIGHT)
1199 /* Left anchored, ignore all extra in fqdn */
1200 return simple_domaincmp(pv, fv, plen);
1206 int maxn = flen - plen;
1207 for (n = 0; n <= maxn; n++)
1209 if (!simple_domaincmp(pv, fv, plen))
1214 * Doesn't match from start of fqdn
1215 * Try skipping first part of fqdn