1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.38 2008/04/18 05:17:18 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/urlmatch.c,v $
6 * Purpose : Declares functions to match URLs against URL
9 * Copyright : Written by and Copyright (C) 2001-2003, 2006-2008 the SourceForge
10 * Privoxy team. http://www.privoxy.org/
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will
23 * be useful, but WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A
25 * PARTICULAR PURPOSE. See the GNU General Public
26 * License for more details.
28 * The GNU General Public License should be included with
29 * this file. If not, you can view it at
30 * http://www.gnu.org/copyleft/gpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 * $Log: urlmatch.c,v $
36 * Revision 1.38 2008/04/18 05:17:18 fabiankeil
37 * Mark simplematch()'s parameters as immutable.
39 * Revision 1.37 2008/04/17 14:53:29 fabiankeil
40 * Move simplematch() into urlmatch.c as it's only
41 * used to match (old-school) domain patterns.
43 * Revision 1.36 2008/04/14 18:19:48 fabiankeil
44 * Remove now-pointless cast in create_url_spec().
46 * Revision 1.35 2008/04/14 18:11:21 fabiankeil
47 * The compiler might not notice it, but the buffer passed to
48 * create_url_spec() is modified later on and thus shouldn't
49 * be declared immutable.
51 * Revision 1.34 2008/04/13 13:32:07 fabiankeil
52 * Factor URL pattern compilation out of create_url_spec().
54 * Revision 1.33 2008/04/12 14:03:13 fabiankeil
55 * Remove an obvious comment and improve another one.
57 * Revision 1.32 2008/04/12 12:38:06 fabiankeil
58 * Factor out duplicated code to compile host, path and tag patterns.
60 * Revision 1.31 2008/04/10 14:41:04 fabiankeil
61 * Ditch url_spec's path member now that it's no longer used.
63 * Revision 1.30 2008/04/10 04:24:24 fabiankeil
64 * Stop duplicating the plain text representation of the path regex
65 * (and keeping the copy around). Once the regex is compiled it's no
68 * Revision 1.29 2008/04/10 04:17:56 fabiankeil
69 * In url_match(), check the right member for NULL when determining
70 * whether there's a path regex to execute. Looking for a plain-text
71 * representation works as well, but it looks "interesting" and that
72 * member will be removed soonish anyway.
74 * Revision 1.28 2008/04/08 16:07:39 fabiankeil
75 * Make it harder to mistake url_match()'s
76 * second parameter for an url_spec.
78 * Revision 1.27 2008/04/08 15:44:33 fabiankeil
79 * Save a bit of memory (and a few cpu cycles) by not bothering to
80 * compile slash-only path regexes that don't affect the result.
82 * Revision 1.26 2008/04/07 16:57:18 fabiankeil
83 * - Use free_url_spec() more consistently.
84 * - Let it reset url->dcount just in case.
86 * Revision 1.25 2008/04/06 15:18:38 fabiankeil
87 * Oh well, rename the --enable-pcre-host-patterns option to
88 * --enable-extended-host-patterns as it's not really PCRE syntax.
90 * Revision 1.24 2008/04/06 14:54:26 fabiankeil
91 * Use PCRE syntax in host patterns when configured
92 * with --enable-pcre-host-patterns.
94 * Revision 1.23 2008/04/05 12:19:20 fabiankeil
95 * Factor compile_host_pattern() out of create_url_spec().
97 * Revision 1.22 2008/03/30 15:02:32 fabiankeil
98 * SZitify unknown_method().
100 * Revision 1.21 2007/12/24 16:34:23 fabiankeil
101 * Band-aid (and micro-optimization) that makes it less likely to run out of
102 * stack space with overly-complex path patterns. Probably masks the problem
103 * reported by Lee in #1856679. Hohoho.
105 * Revision 1.20 2007/09/02 15:31:20 fabiankeil
106 * Move match_portlist() from filter.c to urlmatch.c.
107 * It's used for url matching, not for filtering.
109 * Revision 1.19 2007/09/02 13:42:11 fabiankeil
110 * - Allow port lists in url patterns.
111 * - Ditch unused url_spec member pathlen.
113 * Revision 1.18 2007/07/30 16:42:21 fabiankeil
114 * Move the method check into unknown_method()
115 * and loop through the known methods instead
116 * of using a screen-long OR chain.
118 * Revision 1.17 2007/04/15 16:39:21 fabiankeil
119 * Introduce tags as alternative way to specify which
120 * actions apply to a request. At the moment tags can be
121 * created based on client and server headers.
123 * Revision 1.16 2007/02/13 13:59:24 fabiankeil
124 * Remove redundant log message.
126 * Revision 1.15 2007/01/28 16:11:23 fabiankeil
127 * Accept WebDAV methods for subversion
128 * in parse_http_request(). Closes FR 1581425.
130 * Revision 1.14 2007/01/06 14:23:56 fabiankeil
131 * Fix gcc43 warnings. Mark *csp as immutable
132 * for parse_http_url() and url_match().
133 * Replace a sprintf call with snprintf.
135 * Revision 1.13 2006/12/06 19:50:54 fabiankeil
136 * parse_http_url() now handles intercepted
137 * HTTP request lines as well. Moved parts
138 * of parse_http_url()'s code into
139 * init_domain_components() so that it can
140 * be reused in chat().
142 * Revision 1.12 2006/07/18 14:48:47 david__schmidt
143 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
144 * with what was really the latest development (the v_3_0_branch branch)
146 * Revision 1.10.2.7 2003/05/17 15:57:24 oes
147 * - parse_http_url now checks memory allocation failure for
148 * duplication of "*" URL and rejects "*something" URLs
150 * - Added a comment to what might look like a bug in
151 * create_url_spec (see !bug #736931)
152 * - Comment cosmetics
154 * Revision 1.10.2.6 2003/05/07 12:39:48 oes
155 * Fix typo: Default port for https URLs is 443, not 143.
156 * Thanks to Scott Tregear for spotting this one.
158 * Revision 1.10.2.5 2003/02/28 13:09:29 oes
159 * Fixed a rare double free condition as per Bug #694713
161 * Revision 1.10.2.4 2003/02/28 12:57:44 oes
162 * Moved freeing of http request structure to its owner
163 * as per Dan Price's observations in Bug #694713
165 * Revision 1.10.2.3 2002/11/12 16:50:40 oes
166 * Fixed memory leak in parse_http_request() reported by Oliver Stoeneberg. Fixes bug #637073
168 * Revision 1.10.2.2 2002/09/25 14:53:15 oes
169 * Added basic support for OPTIONS and TRACE HTTP methods:
170 * parse_http_url now recognizes the "*" URI as well as
171 * the OPTIONS and TRACE method keywords.
173 * Revision 1.10.2.1 2002/06/06 19:06:44 jongfoster
174 * Adding support for proprietary Microsoft WebDAV extensions
176 * Revision 1.10 2002/05/12 21:40:37 jongfoster
177 * - Removing some unused code
179 * Revision 1.9 2002/04/04 00:36:36 gliptak
180 * always use pcre for matching
182 * Revision 1.8 2002/04/03 23:32:47 jongfoster
183 * Fixing memory leak on error
185 * Revision 1.7 2002/03/26 22:29:55 swa
186 * we have a new homepage!
188 * Revision 1.6 2002/03/24 13:25:43 swa
189 * name change related issues
191 * Revision 1.5 2002/03/13 00:27:05 jongfoster
194 * Revision 1.4 2002/03/07 03:46:17 oes
195 * Fixed compiler warnings
197 * Revision 1.3 2002/03/03 14:51:11 oes
198 * Fixed CLF logging: Added ocmd member for client's request to struct http_request
200 * Revision 1.2 2002/01/21 00:14:09 jongfoster
201 * Correcting comment style
202 * Fixing an uninitialized memory bug in create_url_spec()
204 * Revision 1.1 2002/01/17 20:53:46 jongfoster
205 * Moving all our URL and URL pattern parsing code to the same file - it
206 * was scattered around in filters.c, loaders.c and parsers.c.
208 * Providing a single, simple url_match(pattern,url) function - rather than
209 * the 3-line match routine which was repeated all over the place.
211 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
213 * Providing parse_http_url() so that URLs can be parsed without faking a
214 * HTTP request line for parse_http_request() or repeating the parsing
215 * code (both of which were techniques that were actually in use).
217 * Standardizing that struct http_request is used to represent a URL, and
218 * struct url_spec is used to represent a URL pattern. (Before, URLs were
219 * represented as seperate variables and a partially-filled-in url_spec).
222 *********************************************************************/
229 #include <sys/types.h>
237 #if !defined(_WIN32) && !defined(__OS2__)
242 #include "urlmatch.h"
244 #include "miscutil.h"
247 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
249 enum regex_anchoring {NO_ANCHORING, LEFT_ANCHORED, RIGHT_ANCHORED};
250 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern);
252 /*********************************************************************
254 * Function : free_http_request
256 * Description : Freez a http_request structure
259 * 1 : http = points to a http_request structure to free
263 *********************************************************************/
264 void free_http_request(struct http_request *http)
273 freez(http->hostport);
276 freez(http->host_ip_addr_str);
277 freez(http->dbuffer);
283 /*********************************************************************
285 * Function : init_domain_components
287 * Description : Splits the domain name so we can compare it
288 * against wildcards. It used to be part of
289 * parse_http_url, but was separated because the
290 * same code is required in chat in case of
291 * intercepted requests.
294 * 1 : http = pointer to the http structure to hold elements.
296 * Returns : JB_ERR_OK on success
297 * JB_ERR_MEMORY on out of memory
298 * JB_ERR_PARSE on malformed command/URL
299 * or >100 domains deep.
301 *********************************************************************/
302 jb_err init_domain_components(struct http_request *http)
304 char *vec[BUFFER_SIZE];
308 http->dbuffer = strdup(http->host);
309 if (NULL == http->dbuffer)
311 return JB_ERR_MEMORY;
314 /* map to lower case */
315 for (p = http->dbuffer; *p ; p++)
317 *p = (char)tolower((int)(unsigned char)*p);
320 /* split the domain name into components */
321 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
323 if (http->dcount <= 0)
326 * Error: More than SZ(vec) components in domain
327 * or: no components in domain
329 log_error(LOG_LEVEL_ERROR, "More than SZ(vec) components in domain or none at all.");
333 /* save a copy of the pointers in dvec */
334 size = (size_t)http->dcount * sizeof(*http->dvec);
336 http->dvec = (char **)malloc(size);
337 if (NULL == http->dvec)
339 return JB_ERR_MEMORY;
342 memcpy(http->dvec, vec, size);
348 /*********************************************************************
350 * Function : parse_http_url
352 * Description : Parse out the host and port from the URL. Find the
353 * hostname & path, port (if ':'), and/or password (if '@')
356 * 1 : url = URL (or is it URI?) to break down
357 * 2 : http = pointer to the http structure to hold elements.
358 * Will be zeroed before use. Note that this
359 * function sets the http->gpc and http->ver
361 * 3 : csp = Current client state (buffers, headers, etc...)
363 * Returns : JB_ERR_OK on success
364 * JB_ERR_MEMORY on out of memory
365 * JB_ERR_PARSE on malformed command/URL
366 * or >100 domains deep.
368 *********************************************************************/
369 jb_err parse_http_url(const char * url,
370 struct http_request *http,
371 const struct client_state *csp)
373 int host_available = 1; /* A proxy can dream. */
376 * Zero out the results structure
378 memset(http, '\0', sizeof(*http));
382 * Save our initial URL
384 http->url = strdup(url);
385 if (http->url == NULL)
387 return JB_ERR_MEMORY;
392 * Check for * URI. If found, we're done.
394 if (*http->url == '*')
396 if ( NULL == (http->path = strdup("*"))
397 || NULL == (http->hostport = strdup("")) )
399 return JB_ERR_MEMORY;
401 if (http->url[1] != '\0')
410 * Split URL into protocol,hostport,path.
420 return JB_ERR_MEMORY;
423 /* Find the start of the URL in our scratch space */
425 if (strncmpic(url_noproto, "http://", 7) == 0)
430 else if (strncmpic(url_noproto, "https://", 8) == 0)
435 else if (*url_noproto == '/')
438 * Short request line without protocol and host.
439 * Most likely because the client's request
440 * was intercepted and redirected into Privoxy.
451 url_path = strchr(url_noproto, '/');
452 if (url_path != NULL)
457 * NOTE: The following line ignores the path for HTTPS URLS.
458 * This means that you get consistent behaviour if you type a
459 * https URL in and it's parsed by the function. (When the
460 * URL is actually retrieved, SSL hides the path part).
462 http->path = strdup(http->ssl ? "/" : url_path);
464 http->hostport = strdup(url_noproto);
469 * Repair broken HTTP requests that don't contain a path,
470 * or CONNECT requests
472 http->path = strdup("/");
473 http->hostport = strdup(url_noproto);
478 if ( (http->path == NULL)
479 || (http->hostport == NULL))
481 return JB_ERR_MEMORY;
487 /* Without host, there is nothing left to do here */
492 * Split hostport into user/password (ignored), host, port.
499 buf = strdup(http->hostport);
502 return JB_ERR_MEMORY;
505 /* check if url contains username and/or password */
506 host = strchr(buf, '@');
509 /* Contains username/password, skip it and the @ sign. */
514 /* No username or password. */
518 /* check if url contains port */
519 port = strchr(host, ':');
523 /* Terminate hostname and point to start of port string */
525 http->port = atoi(port);
529 /* No port specified. */
530 http->port = (http->ssl ? 443 : 80);
533 http->host = strdup(host);
537 if (http->host == NULL)
539 return JB_ERR_MEMORY;
544 * Split domain name so we can compare it against wildcards
546 return init_domain_components(http);
551 /*********************************************************************
553 * Function : unknown_method
555 * Description : Checks whether a method is unknown.
558 * 1 : method = points to a http method
560 * Returns : TRUE if it's unknown, FALSE otherwise.
562 *********************************************************************/
563 static int unknown_method(const char *method)
565 static const char *known_http_methods[] = {
566 /* Basic HTTP request type */
567 "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "TRACE", "CONNECT",
568 /* webDAV extensions (RFC2518) */
569 "PROPFIND", "PROPPATCH", "MOVE", "COPY", "MKCOL", "LOCK", "UNLOCK",
571 * Microsoft webDAV extension for Exchange 2000. See:
572 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
573 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
575 "BCOPY", "BMOVE", "BDELETE", "BPROPFIND", "BPROPPATCH",
577 * Another Microsoft webDAV extension for Exchange 2000. See:
578 * http://systems.cs.colorado.edu/grunwald/MobileComputing/Papers/draft-cohen-gena-p-base-00.txt
579 * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
580 * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
582 "SUBSCRIBE", "UNSUBSCRIBE", "NOTIFY", "POLL",
584 * Yet another WebDAV extension, this time for
585 * Web Distributed Authoring and Versioning (RFC3253)
587 "VERSION-CONTROL", "REPORT", "CHECKOUT", "CHECKIN", "UNCHECKOUT",
588 "MKWORKSPACE", "UPDATE", "LABEL", "MERGE", "BASELINE-CONTROL", "MKACTIVITY",
592 for (i = 0; i < SZ(known_http_methods); i++)
594 if (0 == strcmpic(method, known_http_methods[i]))
605 /*********************************************************************
607 * Function : parse_http_request
609 * Description : Parse out the host and port from the URL. Find the
610 * hostname & path, port (if ':'), and/or password (if '@')
613 * 1 : req = HTTP request line to break down
614 * 2 : http = pointer to the http structure to hold elements
615 * 3 : csp = Current client state (buffers, headers, etc...)
617 * Returns : JB_ERR_OK on success
618 * JB_ERR_MEMORY on out of memory
619 * JB_ERR_CGI_PARAMS on malformed command/URL
620 * or >100 domains deep.
622 *********************************************************************/
623 jb_err parse_http_request(const char *req,
624 struct http_request *http,
625 const struct client_state *csp)
628 char *v[10]; /* XXX: Why 10? We should only need three. */
632 memset(http, '\0', sizeof(*http));
637 return JB_ERR_MEMORY;
640 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
648 * Fail in case of unknown methods
649 * which we might not handle correctly.
651 * XXX: There should be a config option
652 * to forward requests with unknown methods
653 * anyway. Most of them don't need special
656 if (unknown_method(v[0]))
658 log_error(LOG_LEVEL_ERROR, "Unknown HTTP method detected: %s", v[0]);
663 err = parse_http_url(v[1], http, csp);
671 * Copy the details into the structure
673 http->ssl = !strcmpic(v[0], "CONNECT");
674 http->cmd = strdup(req);
675 http->gpc = strdup(v[0]);
676 http->ver = strdup(v[2]);
680 if ( (http->cmd == NULL)
681 || (http->gpc == NULL)
682 || (http->ver == NULL) )
684 return JB_ERR_MEMORY;
692 /*********************************************************************
694 * Function : compile_pattern
696 * Description : Compiles a host, domain or TAG pattern.
699 * 1 : pattern = The pattern to compile.
700 * 2 : anchoring = How the regex should be anchored.
701 * Can be either one of NO_ANCHORING,
702 * LEFT_ANCHORED or RIGHT_ANCHORED.
703 * 3 : url = In case of failures, the spec member is
704 * logged and the structure freed.
705 * 4 : regex = Where the compiled regex should be stored.
707 * Returns : JB_ERR_OK - Success
708 * JB_ERR_MEMORY - Out of memory
709 * JB_ERR_PARSE - Cannot parse regex
711 *********************************************************************/
712 static jb_err compile_pattern(const char *pattern, enum regex_anchoring anchoring,
713 struct url_spec *url, regex_t **regex)
716 char rebuf[BUFFER_SIZE];
720 assert(strlen(pattern) < sizeof(rebuf) - 2);
722 if (pattern[0] == '\0')
740 log_error(LOG_LEVEL_FATAL,
741 "Invalid anchoring in compile_pattern %d", anchoring);
744 *regex = zalloc(sizeof(**regex));
748 return JB_ERR_MEMORY;
751 snprintf(rebuf, sizeof(rebuf), fmt, pattern);
753 errcode = regcomp(*regex, rebuf, (REG_EXTENDED|REG_NOSUB|REG_ICASE));
757 size_t errlen = regerror(errcode, *regex, rebuf, sizeof(rebuf));
758 if (errlen > (sizeof(rebuf) - (size_t)1))
760 errlen = sizeof(rebuf) - (size_t)1;
762 rebuf[errlen] = '\0';
763 log_error(LOG_LEVEL_ERROR, "error compiling %s from %s: %s",
764 pattern, url->spec, rebuf);
775 /*********************************************************************
777 * Function : compile_url_pattern
779 * Description : Compiles the three parts of an URL pattern.
782 * 1 : url = Target url_spec to be filled in.
783 * 2 : buf = The url pattern to compile. Will be messed up.
785 * Returns : JB_ERR_OK - Success
786 * JB_ERR_MEMORY - Out of memory
787 * JB_ERR_PARSE - Cannot parse regex
789 *********************************************************************/
790 static jb_err compile_url_pattern(struct url_spec *url, char *buf)
794 p = strchr(buf, '/');
798 * Only compile the regex if it consists of more than
799 * a single slash, otherwise it wouldn't affect the result.
804 * XXX: does it make sense to compile the slash at the beginning?
806 jb_err err = compile_pattern(p, LEFT_ANCHORED, url, &url->preg);
808 if (JB_ERR_OK != err)
816 p = strchr(buf, ':');
820 url->port_list = strdup(p);
821 if (NULL == url->port_list)
823 return JB_ERR_MEMORY;
828 url->port_list = NULL;
833 return compile_host_pattern(url, buf);
841 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
842 /*********************************************************************
844 * Function : compile_host_pattern
846 * Description : Parses and compiles a host pattern..
849 * 1 : url = Target url_spec to be filled in.
850 * 2 : host_pattern = Host pattern to compile.
852 * Returns : JB_ERR_OK - Success
853 * JB_ERR_MEMORY - Out of memory
854 * JB_ERR_PARSE - Cannot parse regex
856 *********************************************************************/
857 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
859 return compile_pattern(host_pattern, RIGHT_ANCHORED, url, &url->host_regex);
864 /*********************************************************************
866 * Function : compile_host_pattern
868 * Description : Parses and "compiles" an old-school host pattern.
871 * 1 : url = Target url_spec to be filled in.
872 * 2 : host_pattern = Host pattern to parse.
874 * Returns : JB_ERR_OK - Success
875 * JB_ERR_MEMORY - Out of memory
876 * JB_ERR_PARSE - Cannot parse regex
878 *********************************************************************/
879 static jb_err compile_host_pattern(struct url_spec *url, const char *host_pattern)
888 if (host_pattern[strlen(host_pattern) - 1] == '.')
890 url->unanchored |= ANCHOR_RIGHT;
892 if (host_pattern[0] == '.')
894 url->unanchored |= ANCHOR_LEFT;
898 * Split domain into components
900 url->dbuffer = strdup(host_pattern);
901 if (NULL == url->dbuffer)
904 return JB_ERR_MEMORY;
910 for (p = url->dbuffer; *p ; p++)
912 *p = (char)tolower((int)(unsigned char)*p);
916 * Split the domain name into components
918 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
923 return JB_ERR_MEMORY;
925 else if (url->dcount != 0)
928 * Save a copy of the pointers in dvec
930 size = (size_t)url->dcount * sizeof(*url->dvec);
932 url->dvec = (char **)malloc(size);
933 if (NULL == url->dvec)
936 return JB_ERR_MEMORY;
939 memcpy(url->dvec, v, size);
942 * else dcount == 0 in which case we needn't do anything,
943 * since dvec will never be accessed and the pattern will
950 /*********************************************************************
952 * Function : simplematch
954 * Description : String matching, with a (greedy) '*' wildcard that
955 * stands for zero or more arbitrary characters and
956 * character classes in [], which take both enumerations
960 * 1 : pattern = pattern for matching
961 * 2 : text = text to be matched
963 * Returns : 0 if match, else nonzero
965 *********************************************************************/
966 static int simplematch(const char *pattern, const char *text)
968 const unsigned char *pat = (const unsigned char *)pattern;
969 const unsigned char *txt = (const unsigned char *)text;
970 const unsigned char *fallback = pat;
973 unsigned char lastchar = 'a';
975 unsigned char charmap[32];
980 /* EOF pattern but !EOF text? */
993 /* '*' in the pattern? */
997 /* The pattern ends afterwards? Speed up the return. */
1003 /* Else, set wildcard mode and remember position after '*' */
1008 /* Character range specification? */
1011 memset(charmap, '\0', sizeof(charmap));
1013 while (*++pat != ']')
1019 else if (*pat == '-')
1021 if ((*++pat == ']') || *pat == '\0')
1025 for (i = lastchar; i <= *pat; i++)
1027 charmap[i / 8] |= (unsigned char)(1 << (i % 8));
1032 charmap[*pat / 8] |= (unsigned char)(1 << (*pat % 8));
1036 } /* -END- if Character range specification */
1040 * Char match, or char range match?
1044 || ((*pat == ']') && (charmap[*txt / 8] & (1 << (*txt % 8)))) )
1054 * No match && no wildcard: No luck
1058 else if (pat != fallback)
1061 * Increment text pointer if in char range matching
1068 * Wildcard mode && nonmatch beyond fallback: Rewind pattern
1072 * Restart matching from current text pointer
1079 /* Cut off extra '*'s */
1080 if(*pat == '*') pat++;
1082 /* If this is the pattern's end, fine! */
1088 /*********************************************************************
1090 * Function : simple_domaincmp
1092 * Description : Domain-wise Compare fqdn's. The comparison is
1093 * both left- and right-anchored. The individual
1094 * domain names are compared with simplematch().
1095 * This is only used by domain_match.
1098 * 1 : pv = array of patterns to compare
1099 * 2 : fv = array of domain components to compare
1100 * 3 : len = length of the arrays (both arrays are the
1101 * same length - if they weren't, it couldn't
1102 * possibly be a match).
1104 * Returns : 0 => domains are equivalent, else no match.
1106 *********************************************************************/
1107 static int simple_domaincmp(char **pv, char **fv, int len)
1111 for (n = 0; n < len; n++)
1113 if (simplematch(pv[n], fv[n]))
1124 /*********************************************************************
1126 * Function : domain_match
1128 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
1129 * pattern->unachored, the comparison is un-, left-,
1130 * right-anchored, or both.
1131 * The individual domain names are compared with
1135 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1136 * 2 : fqdn = domain name against which the patterns are compared.
1138 * Returns : 0 => domains are equivalent, else no match.
1140 *********************************************************************/
1141 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
1143 char **pv, **fv; /* vectors */
1145 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
1147 plen = pattern->dcount;
1148 flen = fqdn->dcount;
1152 /* fqdn is too short to match this pattern */
1159 if (unanchored == ANCHOR_LEFT)
1164 * Convert this into a fully anchored pattern with
1165 * the fqdn and pattern the same length
1167 fv += (flen - plen); /* flen - plen >= 0 due to check above */
1168 return simple_domaincmp(pv, fv, plen);
1170 else if (unanchored == 0)
1172 /* Fully anchored, check length */
1177 return simple_domaincmp(pv, fv, plen);
1179 else if (unanchored == ANCHOR_RIGHT)
1181 /* Left anchored, ignore all extra in fqdn */
1182 return simple_domaincmp(pv, fv, plen);
1188 int maxn = flen - plen;
1189 for (n = 0; n <= maxn; n++)
1191 if (!simple_domaincmp(pv, fv, plen))
1196 * Doesn't match from start of fqdn
1197 * Try skipping first part of fqdn
1205 #endif /* def FEATURE_EXTENDED_HOST_PATTERNS */
1208 /*********************************************************************
1210 * Function : create_url_spec
1212 * Description : Creates a "url_spec" structure from a string.
1213 * When finished, free with free_url_spec().
1216 * 1 : url = Target url_spec to be filled in. Will be
1217 * zeroed before use.
1218 * 2 : buf = Source pattern, null terminated. NOTE: The
1219 * contents of this buffer are destroyed by this
1220 * function. If this function succeeds, the
1221 * buffer is copied to url->spec. If this
1222 * function fails, the contents of the buffer
1225 * Returns : JB_ERR_OK - Success
1226 * JB_ERR_MEMORY - Out of memory
1227 * JB_ERR_PARSE - Cannot parse regex (Detailed message
1228 * written to system log)
1230 *********************************************************************/
1231 jb_err create_url_spec(struct url_spec *url, char *buf)
1236 memset(url, '\0', sizeof(*url));
1238 /* Remember the original specification for the CGI pages. */
1239 url->spec = strdup(buf);
1240 if (NULL == url->spec)
1242 return JB_ERR_MEMORY;
1245 /* Is it tag pattern? */
1246 if (0 == strncmpic("TAG:", url->spec, 4))
1248 /* The pattern starts with the first character after "TAG:" */
1249 const char *tag_pattern = buf + 4;
1250 return compile_pattern(tag_pattern, NO_ANCHORING, url, &url->tag_regex);
1253 /* If it isn't a tag pattern it must be a URL pattern. */
1254 return compile_url_pattern(url, buf);
1258 /*********************************************************************
1260 * Function : free_url_spec
1262 * Description : Called from the "unloaders". Freez the url
1263 * structure elements.
1266 * 1 : url = pointer to a url_spec structure.
1270 *********************************************************************/
1271 void free_url_spec(struct url_spec *url)
1273 if (url == NULL) return;
1276 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1277 if (url->host_regex)
1279 regfree(url->host_regex);
1280 freez(url->host_regex);
1283 freez(url->dbuffer);
1286 #endif /* ndef FEATURE_EXTENDED_HOST_PATTERNS */
1287 freez(url->port_list);
1295 regfree(url->tag_regex);
1296 freez(url->tag_regex);
1301 /*********************************************************************
1303 * Function : url_match
1305 * Description : Compare a URL against a URL pattern.
1308 * 1 : pattern = a URL pattern
1309 * 2 : url = URL to match
1311 * Returns : Nonzero if the URL matches the pattern, else 0.
1313 *********************************************************************/
1314 int url_match(const struct url_spec *pattern,
1315 const struct http_request *http)
1317 /* XXX: these should probably be functions. */
1318 #define PORT_MATCHES ((NULL == pattern->port_list) || match_portlist(pattern->port_list, http->port))
1319 #ifdef FEATURE_EXTENDED_HOST_PATTERNS
1320 #define DOMAIN_MATCHES ((NULL == pattern->host_regex) || (0 == regexec(pattern->host_regex, http->host, 0, NULL, 0)))
1322 #define DOMAIN_MATCHES ((NULL == pattern->dbuffer) || (0 == domain_match(pattern, http)))
1324 #define PATH_MATCHES ((NULL == pattern->preg) || (0 == regexec(pattern->preg, http->path, 0, NULL, 0)))
1326 if (pattern->tag_regex != NULL)
1328 /* It's a tag pattern and shouldn't be matched against URLs */
1332 return (PORT_MATCHES && DOMAIN_MATCHES && PATH_MATCHES);
1337 /*********************************************************************
1339 * Function : match_portlist
1341 * Description : Check if a given number is covered by a comma
1342 * separated list of numbers and ranges (a,b-c,d,..)
1345 * 1 : portlist = String with list
1346 * 2 : port = port to check
1348 * Returns : 0 => no match
1351 *********************************************************************/
1352 int match_portlist(const char *portlist, int port)
1354 char *min, *max, *next, *portlist_copy;
1356 min = next = portlist_copy = strdup(portlist);
1359 * Zero-terminate first item and remember offset for next
1361 if (NULL != (next = strchr(portlist_copy, (int) ',')))
1367 * Loop through all items, checking for match
1371 if (NULL == (max = strchr(min, (int) '-')))
1374 * No dash, check for equality
1376 if (port == atoi(min))
1378 free(portlist_copy);
1385 * This is a range, so check if between min and max,
1386 * or, if max was omitted, between min and 65K
1389 if(port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
1391 free(portlist_copy);
1403 * Zero-terminate next item and remember offset for n+1
1405 if ((NULL != next) && (NULL != (next = strchr(next, (int) ','))))
1411 free(portlist_copy);