1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.10.2.5 2003/02/28 13:09:29 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/Attic/urlmatch.c,v $
6 * Purpose : Declares functions to match URLs against URL
9 * Copyright : Written by and Copyright (C) 2001 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.10.2.5 2003/02/28 13:09:29 oes
37 * Fixed a rare double free condition as per Bug #694713
39 * Revision 1.10.2.4 2003/02/28 12:57:44 oes
40 * Moved freeing of http request structure to its owner
41 * as per Dan Price's observations in Bug #694713
43 * Revision 1.10.2.3 2002/11/12 16:50:40 oes
44 * Fixed memory leak in parse_http_request() reported by Oliver Stoeneberg. Fixes bug #637073
46 * Revision 1.10.2.2 2002/09/25 14:53:15 oes
47 * Added basic support for OPTIONS and TRACE HTTP methods:
48 * parse_http_url now recognizes the "*" URI as well as
49 * the OPTIONS and TRACE method keywords.
51 * Revision 1.10.2.1 2002/06/06 19:06:44 jongfoster
52 * Adding support for proprietary Microsoft WebDAV extensions
54 * Revision 1.10 2002/05/12 21:40:37 jongfoster
55 * - Removing some unused code
57 * Revision 1.9 2002/04/04 00:36:36 gliptak
58 * always use pcre for matching
60 * Revision 1.8 2002/04/03 23:32:47 jongfoster
61 * Fixing memory leak on error
63 * Revision 1.7 2002/03/26 22:29:55 swa
64 * we have a new homepage!
66 * Revision 1.6 2002/03/24 13:25:43 swa
67 * name change related issues
69 * Revision 1.5 2002/03/13 00:27:05 jongfoster
72 * Revision 1.4 2002/03/07 03:46:17 oes
73 * Fixed compiler warnings
75 * Revision 1.3 2002/03/03 14:51:11 oes
76 * Fixed CLF logging: Added ocmd member for client's request to struct http_request
78 * Revision 1.2 2002/01/21 00:14:09 jongfoster
79 * Correcting comment style
80 * Fixing an uninitialized memory bug in create_url_spec()
82 * Revision 1.1 2002/01/17 20:53:46 jongfoster
83 * Moving all our URL and URL pattern parsing code to the same file - it
84 * was scattered around in filters.c, loaders.c and parsers.c.
86 * Providing a single, simple url_match(pattern,url) function - rather than
87 * the 3-line match routine which was repeated all over the place.
89 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
91 * Providing parse_http_url() so that URLs can be parsed without faking a
92 * HTTP request line for parse_http_request() or repeating the parsing
93 * code (both of which were techniques that were actually in use).
95 * Standardizing that struct http_request is used to represent a URL, and
96 * struct url_spec is used to represent a URL pattern. (Before, URLs were
97 * represented as seperate variables and a partially-filled-in url_spec).
100 *********************************************************************/
107 #include <sys/types.h>
115 #if !defined(_WIN32) && !defined(__OS2__)
120 #include "urlmatch.h"
122 #include "miscutil.h"
125 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
128 /*********************************************************************
130 * Function : free_http_request
132 * Description : Freez a http_request structure
135 * 1 : http = points to a http_request structure to free
139 *********************************************************************/
140 void free_http_request(struct http_request *http)
149 freez(http->hostport);
152 freez(http->host_ip_addr_str);
153 freez(http->dbuffer);
159 /*********************************************************************
161 * Function : parse_http_url
163 * Description : Parse out the host and port from the URL. Find the
164 * hostname & path, port (if ':'), and/or password (if '@')
167 * 1 : url = URL (or is it URI?) to break down
168 * 2 : http = pointer to the http structure to hold elements.
169 * Will be zeroed before use. Note that this
170 * function sets the http->gpc and http->ver
172 * 3 : csp = Current client state (buffers, headers, etc...)
174 * Returns : JB_ERR_OK on success
175 * JB_ERR_MEMORY on out of memory
176 * JB_ERR_PARSE on malformed command/URL
177 * or >100 domains deep.
179 *********************************************************************/
180 jb_err parse_http_url(const char * url,
181 struct http_request *http,
182 struct client_state *csp)
185 * Zero out the results structure
187 memset(http, '\0', sizeof(*http));
191 * Save our initial URL
193 http->url = strdup(url);
194 if (http->url == NULL)
196 return JB_ERR_MEMORY;
201 * Check for * URI. If found, we're done.
203 if (*http->url == '*')
205 http->path = strdup("*");
206 http->hostport = strdup("");
212 * Split URL into protocol,hostport,path.
222 return JB_ERR_MEMORY;
225 /* Find the start of the URL in our scratch space */
227 if (strncmpic(url_noproto, "http://", 7) == 0)
232 else if (strncmpic(url_noproto, "https://", 8) == 0)
242 url_path = strchr(url_noproto, '/');
243 if (url_path != NULL)
248 * NOTE: The following line ignores the path for HTTPS URLS.
249 * This means that you get consistent behaviour if you type a
250 * https URL in and it's parsed by the function. (When the
251 * URL is actually retrieved, SSL hides the path part).
253 http->path = strdup(http->ssl ? "/" : url_path);
255 http->hostport = strdup(url_noproto);
260 * Repair broken HTTP requests that don't contain a path,
261 * or CONNECT requests
263 http->path = strdup("/");
264 http->hostport = strdup(url_noproto);
269 if ( (http->path == NULL)
270 || (http->hostport == NULL))
272 return JB_ERR_MEMORY;
278 * Split hostport into user/password (ignored), host, port.
285 buf = strdup(http->hostport);
288 return JB_ERR_MEMORY;
291 /* check if url contains username and/or password */
292 host = strchr(buf, '@');
295 /* Contains username/password, skip it and the @ sign. */
300 /* No username or password. */
304 /* check if url contains port */
305 port = strchr(host, ':');
309 /* Terminate hostname and point to start of port string */
311 http->port = atoi(port);
315 /* No port specified. */
316 http->port = (http->ssl ? 443 : 80);
319 http->host = strdup(host);
323 if (http->host == NULL)
325 return JB_ERR_MEMORY;
330 * Split domain name so we can compare it against wildcards
334 char *vec[BUFFER_SIZE];
338 http->dbuffer = strdup(http->host);
339 if (NULL == http->dbuffer)
341 return JB_ERR_MEMORY;
344 /* map to lower case */
345 for (p = http->dbuffer; *p ; p++)
347 *p = tolower((int)(unsigned char)*p);
350 /* split the domain name into components */
351 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
353 if (http->dcount <= 0)
356 * Error: More than SZ(vec) components in domain
357 * or: no components in domain
362 /* save a copy of the pointers in dvec */
363 size = http->dcount * sizeof(*http->dvec);
365 http->dvec = (char **)malloc(size);
366 if (NULL == http->dvec)
368 return JB_ERR_MEMORY;
371 memcpy(http->dvec, vec, size);
379 /*********************************************************************
381 * Function : parse_http_request
383 * Description : Parse out the host and port from the URL. Find the
384 * hostname & path, port (if ':'), and/or password (if '@')
387 * 1 : req = HTTP request line to break down
388 * 2 : http = pointer to the http structure to hold elements
389 * 3 : csp = Current client state (buffers, headers, etc...)
391 * Returns : JB_ERR_OK on success
392 * JB_ERR_MEMORY on out of memory
393 * JB_ERR_CGI_PARAMS on malformed command/URL
394 * or >100 domains deep.
396 *********************************************************************/
397 jb_err parse_http_request(const char *req,
398 struct http_request *http,
399 struct client_state *csp)
407 memset(http, '\0', sizeof(*http));
412 return JB_ERR_MEMORY;
415 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
422 /* this could be a CONNECT request */
423 if (strcmpic(v[0], "connect") == 0)
428 /* or it could be any other basic HTTP request type */
429 else if ((0 == strcmpic(v[0], "get"))
430 || (0 == strcmpic(v[0], "head"))
431 || (0 == strcmpic(v[0], "post"))
432 || (0 == strcmpic(v[0], "put"))
433 || (0 == strcmpic(v[0], "delete"))
434 || (0 == strcmpic(v[0], "options"))
435 || (0 == strcmpic(v[0], "trace"))
437 /* or a webDAV extension (RFC2518) */
438 || (0 == strcmpic(v[0], "propfind"))
439 || (0 == strcmpic(v[0], "proppatch"))
440 || (0 == strcmpic(v[0], "move"))
441 || (0 == strcmpic(v[0], "copy"))
442 || (0 == strcmpic(v[0], "mkcol"))
443 || (0 == strcmpic(v[0], "lock"))
444 || (0 == strcmpic(v[0], "unlock"))
446 /* Or a Microsoft webDAV extension for Exchange 2000. See: */
447 /* http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html */
448 /* http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp */
449 || (0 == strcmpic(v[0], "bcopy"))
450 || (0 == strcmpic(v[0], "bmove"))
451 || (0 == strcmpic(v[0], "bdelete"))
452 || (0 == strcmpic(v[0], "bpropfind"))
453 || (0 == strcmpic(v[0], "bproppatch"))
455 /* Or another Microsoft webDAV extension for Exchange 2000. See: */
456 /* http://systems.cs.colorado.edu/grunwald/MobileComputing/Papers/draft-cohen-gena-p-base-00.txt */
457 /* http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html */
458 /* http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp */
459 || (0 == strcmpic(v[0], "subscribe"))
460 || (0 == strcmpic(v[0], "unsubscribe"))
461 || (0 == strcmpic(v[0], "notify"))
462 || (0 == strcmpic(v[0], "poll"))
470 /* Unknown HTTP method */
475 err = parse_http_url(v[1], http, csp);
483 * Copy the details into the structure
485 http->ssl = is_connect;
486 http->cmd = strdup(req);
487 http->gpc = strdup(v[0]);
488 http->ver = strdup(v[2]);
490 if ( (http->cmd == NULL)
491 || (http->gpc == NULL)
492 || (http->ver == NULL) )
495 return JB_ERR_MEMORY;
504 /*********************************************************************
506 * Function : simple_domaincmp
508 * Description : Domain-wise Compare fqdn's. The comparison is
509 * both left- and right-anchored. The individual
510 * domain names are compared with simplematch().
511 * This is only used by domain_match.
514 * 1 : pv = array of patterns to compare
515 * 2 : fv = array of domain components to compare
516 * 3 : len = length of the arrays (both arrays are the
517 * same length - if they weren't, it couldn't
518 * possibly be a match).
520 * Returns : 0 => domains are equivalent, else no match.
522 *********************************************************************/
523 static int simple_domaincmp(char **pv, char **fv, int len)
527 for (n = 0; n < len; n++)
529 if (simplematch(pv[n], fv[n]))
540 /*********************************************************************
542 * Function : domain_match
544 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
545 * pattern->unachored, the comparison is un-, left-,
546 * right-anchored, or both.
547 * The individual domain names are compared with
551 * 1 : pattern = a domain that may contain a '*' as a wildcard.
552 * 2 : fqdn = domain name against which the patterns are compared.
554 * Returns : 0 => domains are equivalent, else no match.
556 *********************************************************************/
557 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
559 char **pv, **fv; /* vectors */
561 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
563 plen = pattern->dcount;
568 /* fqdn is too short to match this pattern */
575 if (unanchored == ANCHOR_LEFT)
580 * Convert this into a fully anchored pattern with
581 * the fqdn and pattern the same length
583 fv += (flen - plen); /* flen - plen >= 0 due to check above */
584 return simple_domaincmp(pv, fv, plen);
586 else if (unanchored == 0)
588 /* Fully anchored, check length */
593 return simple_domaincmp(pv, fv, plen);
595 else if (unanchored == ANCHOR_RIGHT)
597 /* Left anchored, ignore all extra in fqdn */
598 return simple_domaincmp(pv, fv, plen);
604 int maxn = flen - plen;
605 for (n = 0; n <= maxn; n++)
607 if (!simple_domaincmp(pv, fv, plen))
612 * Doesn't match from start of fqdn
613 * Try skipping first part of fqdn
623 /*********************************************************************
625 * Function : create_url_spec
627 * Description : Creates a "url_spec" structure from a string.
628 * When finished, free with unload_url().
631 * 1 : url = Target url_spec to be filled in. Will be
633 * 2 : buf = Source pattern, null terminated. NOTE: The
634 * contents of this buffer are destroyed by this
635 * function. If this function succeeds, the
636 * buffer is copied to url->spec. If this
637 * function fails, the contents of the buffer
640 * Returns : JB_ERR_OK - Success
641 * JB_ERR_MEMORY - Out of memory
642 * JB_ERR_PARSE - Cannot parse regex (Detailed message
643 * written to system log)
645 *********************************************************************/
646 jb_err create_url_spec(struct url_spec * url, const char * buf)
654 memset(url, '\0', sizeof(*url));
656 /* save a copy of the orignal specification */
657 if ((url->spec = strdup(buf)) == NULL)
659 return JB_ERR_MEMORY;
662 if ((p = strchr(buf, '/')) != NULL)
664 if (NULL == (url->path = strdup(p)))
667 return JB_ERR_MEMORY;
669 url->pathlen = strlen(url->path);
680 char rebuf[BUFFER_SIZE];
682 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
686 return JB_ERR_MEMORY;
689 sprintf(rebuf, "^(%s)", url->path);
691 errcode = regcomp(url->preg, rebuf,
692 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
695 size_t errlen = regerror(errcode,
696 url->preg, rebuf, sizeof(rebuf));
698 if (errlen > (sizeof(rebuf) - (size_t)1))
700 errlen = sizeof(rebuf) - (size_t)1;
702 rebuf[errlen] = '\0';
704 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
715 if ((p = strchr(buf, ':')) == NULL)
730 /* Parse domain part */
731 if (buf[strlen(buf) - 1] == '.')
733 url->unanchored |= ANCHOR_RIGHT;
737 url->unanchored |= ANCHOR_LEFT;
740 /* split domain into components */
742 url->dbuffer = strdup(buf);
743 if (NULL == url->dbuffer)
749 return JB_ERR_MEMORY;
752 /* map to lower case */
753 for (p = url->dbuffer; *p ; p++)
755 *p = tolower((int)(unsigned char)*p);
758 /* split the domain name into components */
759 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
769 return JB_ERR_MEMORY;
771 else if (url->dcount != 0)
774 /* save a copy of the pointers in dvec */
775 size = url->dcount * sizeof(*url->dvec);
777 url->dvec = (char **)malloc(size);
778 if (NULL == url->dvec)
786 return JB_ERR_MEMORY;
789 memcpy(url->dvec, v, size);
798 /*********************************************************************
800 * Function : free_url_spec
802 * Description : Called from the "unloaders". Freez the url
803 * structure elements.
806 * 1 : url = pointer to a url_spec structure.
810 *********************************************************************/
811 void free_url_spec(struct url_spec *url)
813 if (url == NULL) return;
827 /*********************************************************************
829 * Function : url_match
831 * Description : Compare a URL against a URL pattern.
834 * 1 : pattern = a URL pattern
835 * 2 : url = URL to match
837 * Returns : 0 iff the URL matches the pattern, else nonzero.
839 *********************************************************************/
840 int url_match(const struct url_spec *pattern,
841 const struct http_request *url)
843 return ((pattern->port == 0) || (pattern->port == url->port))
844 && ((pattern->dbuffer == NULL) || (domain_match(pattern, url) == 0))
845 && ((pattern->path == NULL) ||
846 (regexec(pattern->preg, url->path, 0, NULL, 0) == 0)