1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.16 2007/02/13 13:59:24 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-2007 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.16 2007/02/13 13:59:24 fabiankeil
37 * Remove redundant log message.
39 * Revision 1.15 2007/01/28 16:11:23 fabiankeil
40 * Accept WebDAV methods for subversion
41 * in parse_http_request(). Closes FR 1581425.
43 * Revision 1.14 2007/01/06 14:23:56 fabiankeil
44 * Fix gcc43 warnings. Mark *csp as immutable
45 * for parse_http_url() and url_match().
46 * Replace a sprintf call with snprintf.
48 * Revision 1.13 2006/12/06 19:50:54 fabiankeil
49 * parse_http_url() now handles intercepted
50 * HTTP request lines as well. Moved parts
51 * of parse_http_url()'s code into
52 * init_domain_components() so that it can
53 * be reused in chat().
55 * Revision 1.12 2006/07/18 14:48:47 david__schmidt
56 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
57 * with what was really the latest development (the v_3_0_branch branch)
59 * Revision 1.10.2.7 2003/05/17 15:57:24 oes
60 * - parse_http_url now checks memory allocation failure for
61 * duplication of "*" URL and rejects "*something" URLs
63 * - Added a comment to what might look like a bug in
64 * create_url_spec (see !bug #736931)
67 * Revision 1.10.2.6 2003/05/07 12:39:48 oes
68 * Fix typo: Default port for https URLs is 443, not 143.
69 * Thanks to Scott Tregear for spotting this one.
71 * Revision 1.10.2.5 2003/02/28 13:09:29 oes
72 * Fixed a rare double free condition as per Bug #694713
74 * Revision 1.10.2.4 2003/02/28 12:57:44 oes
75 * Moved freeing of http request structure to its owner
76 * as per Dan Price's observations in Bug #694713
78 * Revision 1.10.2.3 2002/11/12 16:50:40 oes
79 * Fixed memory leak in parse_http_request() reported by Oliver Stoeneberg. Fixes bug #637073
81 * Revision 1.10.2.2 2002/09/25 14:53:15 oes
82 * Added basic support for OPTIONS and TRACE HTTP methods:
83 * parse_http_url now recognizes the "*" URI as well as
84 * the OPTIONS and TRACE method keywords.
86 * Revision 1.10.2.1 2002/06/06 19:06:44 jongfoster
87 * Adding support for proprietary Microsoft WebDAV extensions
89 * Revision 1.10 2002/05/12 21:40:37 jongfoster
90 * - Removing some unused code
92 * Revision 1.9 2002/04/04 00:36:36 gliptak
93 * always use pcre for matching
95 * Revision 1.8 2002/04/03 23:32:47 jongfoster
96 * Fixing memory leak on error
98 * Revision 1.7 2002/03/26 22:29:55 swa
99 * we have a new homepage!
101 * Revision 1.6 2002/03/24 13:25:43 swa
102 * name change related issues
104 * Revision 1.5 2002/03/13 00:27:05 jongfoster
107 * Revision 1.4 2002/03/07 03:46:17 oes
108 * Fixed compiler warnings
110 * Revision 1.3 2002/03/03 14:51:11 oes
111 * Fixed CLF logging: Added ocmd member for client's request to struct http_request
113 * Revision 1.2 2002/01/21 00:14:09 jongfoster
114 * Correcting comment style
115 * Fixing an uninitialized memory bug in create_url_spec()
117 * Revision 1.1 2002/01/17 20:53:46 jongfoster
118 * Moving all our URL and URL pattern parsing code to the same file - it
119 * was scattered around in filters.c, loaders.c and parsers.c.
121 * Providing a single, simple url_match(pattern,url) function - rather than
122 * the 3-line match routine which was repeated all over the place.
124 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
126 * Providing parse_http_url() so that URLs can be parsed without faking a
127 * HTTP request line for parse_http_request() or repeating the parsing
128 * code (both of which were techniques that were actually in use).
130 * Standardizing that struct http_request is used to represent a URL, and
131 * struct url_spec is used to represent a URL pattern. (Before, URLs were
132 * represented as seperate variables and a partially-filled-in url_spec).
135 *********************************************************************/
142 #include <sys/types.h>
150 #if !defined(_WIN32) && !defined(__OS2__)
155 #include "urlmatch.h"
157 #include "miscutil.h"
160 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
163 /*********************************************************************
165 * Function : free_http_request
167 * Description : Freez a http_request structure
170 * 1 : http = points to a http_request structure to free
174 *********************************************************************/
175 void free_http_request(struct http_request *http)
184 freez(http->hostport);
187 freez(http->host_ip_addr_str);
188 freez(http->dbuffer);
193 /*********************************************************************
195 * Function : init_domain_components
197 * Description : Splits the domain name so we can compare it
198 * against wildcards. It used to be part of
199 * parse_http_url, but was separated because the
200 * same code is required in chat in case of
201 * intercepted requests.
204 * 1 : http = pointer to the http structure to hold elements.
206 * Returns : JB_ERR_OK on success
207 * JB_ERR_MEMORY on out of memory
208 * JB_ERR_PARSE on malformed command/URL
209 * or >100 domains deep.
211 *********************************************************************/
212 jb_err init_domain_components(struct http_request *http)
214 char *vec[BUFFER_SIZE];
218 http->dbuffer = strdup(http->host);
219 if (NULL == http->dbuffer)
221 return JB_ERR_MEMORY;
224 /* map to lower case */
225 for (p = http->dbuffer; *p ; p++)
227 *p = (char)tolower((int)(unsigned char)*p);
230 /* split the domain name into components */
231 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
233 if (http->dcount <= 0)
236 * Error: More than SZ(vec) components in domain
237 * or: no components in domain
239 log_error(LOG_LEVEL_ERROR, "More than SZ(vec) components in domain or none at all.");
243 /* save a copy of the pointers in dvec */
244 size = (size_t)http->dcount * sizeof(*http->dvec);
246 http->dvec = (char **)malloc(size);
247 if (NULL == http->dvec)
249 return JB_ERR_MEMORY;
252 memcpy(http->dvec, vec, size);
257 /*********************************************************************
259 * Function : parse_http_url
261 * Description : Parse out the host and port from the URL. Find the
262 * hostname & path, port (if ':'), and/or password (if '@')
265 * 1 : url = URL (or is it URI?) to break down
266 * 2 : http = pointer to the http structure to hold elements.
267 * Will be zeroed before use. Note that this
268 * function sets the http->gpc and http->ver
270 * 3 : csp = Current client state (buffers, headers, etc...)
272 * Returns : JB_ERR_OK on success
273 * JB_ERR_MEMORY on out of memory
274 * JB_ERR_PARSE on malformed command/URL
275 * or >100 domains deep.
277 *********************************************************************/
278 jb_err parse_http_url(const char * url,
279 struct http_request *http,
280 const struct client_state *csp)
282 int host_available = 1; /* A proxy can dream. */
285 * Zero out the results structure
287 memset(http, '\0', sizeof(*http));
291 * Save our initial URL
293 http->url = strdup(url);
294 if (http->url == NULL)
296 return JB_ERR_MEMORY;
301 * Check for * URI. If found, we're done.
303 if (*http->url == '*')
305 if ( NULL == (http->path = strdup("*"))
306 || NULL == (http->hostport = strdup("")) )
308 return JB_ERR_MEMORY;
310 if (http->url[1] != '\0')
319 * Split URL into protocol,hostport,path.
329 return JB_ERR_MEMORY;
332 /* Find the start of the URL in our scratch space */
334 if (strncmpic(url_noproto, "http://", 7) == 0)
339 else if (strncmpic(url_noproto, "https://", 8) == 0)
344 else if (*url_noproto == '/')
347 * Short request line without protocol and host.
348 * Most likely because the client's request
349 * was intercepted and redirected into Privoxy.
360 url_path = strchr(url_noproto, '/');
361 if (url_path != NULL)
366 * NOTE: The following line ignores the path for HTTPS URLS.
367 * This means that you get consistent behaviour if you type a
368 * https URL in and it's parsed by the function. (When the
369 * URL is actually retrieved, SSL hides the path part).
371 http->path = strdup(http->ssl ? "/" : url_path);
373 http->hostport = strdup(url_noproto);
378 * Repair broken HTTP requests that don't contain a path,
379 * or CONNECT requests
381 http->path = strdup("/");
382 http->hostport = strdup(url_noproto);
387 if ( (http->path == NULL)
388 || (http->hostport == NULL))
390 return JB_ERR_MEMORY;
396 /* Without host, there is nothing left to do here */
401 * Split hostport into user/password (ignored), host, port.
408 buf = strdup(http->hostport);
411 return JB_ERR_MEMORY;
414 /* check if url contains username and/or password */
415 host = strchr(buf, '@');
418 /* Contains username/password, skip it and the @ sign. */
423 /* No username or password. */
427 /* check if url contains port */
428 port = strchr(host, ':');
432 /* Terminate hostname and point to start of port string */
434 http->port = atoi(port);
438 /* No port specified. */
439 http->port = (http->ssl ? 443 : 80);
442 http->host = strdup(host);
446 if (http->host == NULL)
448 return JB_ERR_MEMORY;
453 * Split domain name so we can compare it against wildcards
455 return init_domain_components(http);
460 /*********************************************************************
462 * Function : parse_http_request
464 * Description : Parse out the host and port from the URL. Find the
465 * hostname & path, port (if ':'), and/or password (if '@')
468 * 1 : req = HTTP request line to break down
469 * 2 : http = pointer to the http structure to hold elements
470 * 3 : csp = Current client state (buffers, headers, etc...)
472 * Returns : JB_ERR_OK on success
473 * JB_ERR_MEMORY on out of memory
474 * JB_ERR_CGI_PARAMS on malformed command/URL
475 * or >100 domains deep.
477 *********************************************************************/
478 jb_err parse_http_request(const char *req,
479 struct http_request *http,
480 const struct client_state *csp)
488 memset(http, '\0', sizeof(*http));
493 return JB_ERR_MEMORY;
496 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
503 /* this could be a CONNECT request */
504 if (strcmpic(v[0], "connect") == 0)
509 /* or it could be any other basic HTTP request type */
510 else if ((0 == strcmpic(v[0], "get"))
511 || (0 == strcmpic(v[0], "head"))
512 || (0 == strcmpic(v[0], "post"))
513 || (0 == strcmpic(v[0], "put"))
514 || (0 == strcmpic(v[0], "delete"))
515 || (0 == strcmpic(v[0], "options"))
516 || (0 == strcmpic(v[0], "trace"))
518 /* or a webDAV extension (RFC2518) */
519 || (0 == strcmpic(v[0], "propfind"))
520 || (0 == strcmpic(v[0], "proppatch"))
521 || (0 == strcmpic(v[0], "move"))
522 || (0 == strcmpic(v[0], "copy"))
523 || (0 == strcmpic(v[0], "mkcol"))
524 || (0 == strcmpic(v[0], "lock"))
525 || (0 == strcmpic(v[0], "unlock"))
527 /* Or a Microsoft webDAV extension for Exchange 2000. See: */
528 /* http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html */
529 /* http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp */
530 || (0 == strcmpic(v[0], "bcopy"))
531 || (0 == strcmpic(v[0], "bmove"))
532 || (0 == strcmpic(v[0], "bdelete"))
533 || (0 == strcmpic(v[0], "bpropfind"))
534 || (0 == strcmpic(v[0], "bproppatch"))
536 /* Or another Microsoft webDAV extension for Exchange 2000. See: */
537 /* http://systems.cs.colorado.edu/grunwald/MobileComputing/Papers/draft-cohen-gena-p-base-00.txt */
538 /* http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html */
539 /* http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp */
540 || (0 == strcmpic(v[0], "subscribe"))
541 || (0 == strcmpic(v[0], "unsubscribe"))
542 || (0 == strcmpic(v[0], "notify"))
543 || (0 == strcmpic(v[0], "poll"))
546 * Or yet another WebDAV extension, this time for
547 * Web Distributed Authoring and Versioning (RFC3253)
549 || (0 == strcmpic(v[0], "version-control"))
550 || (0 == strcmpic(v[0], "report"))
551 || (0 == strcmpic(v[0], "checkout"))
552 || (0 == strcmpic(v[0], "checkin"))
553 || (0 == strcmpic(v[0], "uncheckout"))
554 || (0 == strcmpic(v[0], "mkworkspace"))
555 || (0 == strcmpic(v[0], "update"))
556 || (0 == strcmpic(v[0], "label"))
557 || (0 == strcmpic(v[0], "merge"))
558 || (0 == strcmpic(v[0], "baseline-control"))
559 || (0 == strcmpic(v[0], "mkactivity"))
567 /* Unknown HTTP method */
568 log_error(LOG_LEVEL_ERROR, "Unknown HTTP method detected: %s", v[0]);
573 err = parse_http_url(v[1], http, csp);
581 * Copy the details into the structure
583 http->ssl = is_connect;
584 http->cmd = strdup(req);
585 http->gpc = strdup(v[0]);
586 http->ver = strdup(v[2]);
588 if ( (http->cmd == NULL)
589 || (http->gpc == NULL)
590 || (http->ver == NULL) )
593 return JB_ERR_MEMORY;
602 /*********************************************************************
604 * Function : simple_domaincmp
606 * Description : Domain-wise Compare fqdn's. The comparison is
607 * both left- and right-anchored. The individual
608 * domain names are compared with simplematch().
609 * This is only used by domain_match.
612 * 1 : pv = array of patterns to compare
613 * 2 : fv = array of domain components to compare
614 * 3 : len = length of the arrays (both arrays are the
615 * same length - if they weren't, it couldn't
616 * possibly be a match).
618 * Returns : 0 => domains are equivalent, else no match.
620 *********************************************************************/
621 static int simple_domaincmp(char **pv, char **fv, int len)
625 for (n = 0; n < len; n++)
627 if (simplematch(pv[n], fv[n]))
638 /*********************************************************************
640 * Function : domain_match
642 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
643 * pattern->unachored, the comparison is un-, left-,
644 * right-anchored, or both.
645 * The individual domain names are compared with
649 * 1 : pattern = a domain that may contain a '*' as a wildcard.
650 * 2 : fqdn = domain name against which the patterns are compared.
652 * Returns : 0 => domains are equivalent, else no match.
654 *********************************************************************/
655 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
657 char **pv, **fv; /* vectors */
659 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
661 plen = pattern->dcount;
666 /* fqdn is too short to match this pattern */
673 if (unanchored == ANCHOR_LEFT)
678 * Convert this into a fully anchored pattern with
679 * the fqdn and pattern the same length
681 fv += (flen - plen); /* flen - plen >= 0 due to check above */
682 return simple_domaincmp(pv, fv, plen);
684 else if (unanchored == 0)
686 /* Fully anchored, check length */
691 return simple_domaincmp(pv, fv, plen);
693 else if (unanchored == ANCHOR_RIGHT)
695 /* Left anchored, ignore all extra in fqdn */
696 return simple_domaincmp(pv, fv, plen);
702 int maxn = flen - plen;
703 for (n = 0; n <= maxn; n++)
705 if (!simple_domaincmp(pv, fv, plen))
710 * Doesn't match from start of fqdn
711 * Try skipping first part of fqdn
721 /*********************************************************************
723 * Function : create_url_spec
725 * Description : Creates a "url_spec" structure from a string.
726 * When finished, free with free_url_spec().
729 * 1 : url = Target url_spec to be filled in. Will be
731 * 2 : buf = Source pattern, null terminated. NOTE: The
732 * contents of this buffer are destroyed by this
733 * function. If this function succeeds, the
734 * buffer is copied to url->spec. If this
735 * function fails, the contents of the buffer
738 * Returns : JB_ERR_OK - Success
739 * JB_ERR_MEMORY - Out of memory
740 * JB_ERR_PARSE - Cannot parse regex (Detailed message
741 * written to system log)
743 *********************************************************************/
744 jb_err create_url_spec(struct url_spec * url, const char * buf)
749 char rebuf[BUFFER_SIZE];
757 memset(url, '\0', sizeof(*url));
760 * Save a copy of the orignal specification
762 if ((url->spec = strdup(buf)) == NULL)
764 return JB_ERR_MEMORY;
767 /* Is it tag pattern? */
768 if (0 == strncmpic("TAG:", url->spec, 4))
770 if (NULL == (url->tag_regex = zalloc(sizeof(*url->tag_regex))))
773 return JB_ERR_MEMORY;
776 /* buf + 4 to skip "TAG:" */
777 errcode = regcomp(url->tag_regex, buf + 4, (REG_EXTENDED|REG_NOSUB|REG_ICASE));
780 errlen = regerror(errcode, url->preg, rebuf, sizeof(rebuf));
781 if (errlen > (sizeof(rebuf) - 1))
783 errlen = sizeof(rebuf) - 1;
785 rebuf[errlen] = '\0';
787 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s", url->spec, rebuf);
790 regfree(url->tag_regex);
791 freez(url->tag_regex);
798 /* Only reached for URL patterns */
799 if ((p = strchr(buf, '/')) != NULL)
801 if (NULL == (url->path = strdup(p)))
804 return JB_ERR_MEMORY;
806 url->pathlen = strlen(url->path);
816 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
820 return JB_ERR_MEMORY;
823 snprintf(rebuf, sizeof(rebuf), "^(%s)", url->path);
825 errcode = regcomp(url->preg, rebuf,
826 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
829 errlen = regerror(errcode, url->preg, rebuf, sizeof(rebuf));
831 if (errlen > (sizeof(rebuf) - (size_t)1))
833 errlen = sizeof(rebuf) - (size_t)1;
835 rebuf[errlen] = '\0';
837 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
848 if ((p = strchr(buf, ':')) == NULL)
866 if (buf[strlen(buf) - 1] == '.')
868 url->unanchored |= ANCHOR_RIGHT;
872 url->unanchored |= ANCHOR_LEFT;
876 * Split domain into components
878 url->dbuffer = strdup(buf);
879 if (NULL == url->dbuffer)
885 return JB_ERR_MEMORY;
891 for (p = url->dbuffer; *p ; p++)
893 *p = (char)tolower((int)(unsigned char)*p);
897 * Split the domain name into components
899 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
909 return JB_ERR_MEMORY;
911 else if (url->dcount != 0)
915 * Save a copy of the pointers in dvec
917 size = (size_t)url->dcount * sizeof(*url->dvec);
919 url->dvec = (char **)malloc(size);
920 if (NULL == url->dvec)
928 return JB_ERR_MEMORY;
931 memcpy(url->dvec, v, size);
934 * else dcount == 0 in which case we needn't do anything,
935 * since dvec will never be accessed and the pattern will
945 /*********************************************************************
947 * Function : free_url_spec
949 * Description : Called from the "unloaders". Freez the url
950 * structure elements.
953 * 1 : url = pointer to a url_spec structure.
957 *********************************************************************/
958 void free_url_spec(struct url_spec *url)
960 if (url == NULL) return;
973 regfree(url->tag_regex);
974 freez(url->tag_regex);
979 /*********************************************************************
981 * Function : url_match
983 * Description : Compare a URL against a URL pattern.
986 * 1 : pattern = a URL pattern
987 * 2 : url = URL to match
989 * Returns : Nonzero if the URL matches the pattern, else 0.
991 *********************************************************************/
992 int url_match(const struct url_spec *pattern,
993 const struct http_request *url)
995 if (pattern->tag_regex != NULL)
997 /* It's a tag pattern and shouldn't be matched against URLs */
1001 return ((pattern->port == 0) || (pattern->port == url->port))
1002 && ((pattern->dbuffer == NULL) || (domain_match(pattern, url) == 0))
1003 && ((pattern->path == NULL) ||
1004 (regexec(pattern->preg, url->path, 0, NULL, 0) == 0)