Remove the treat-forbidden-connects-like-blocks action. We now
[privoxy.git] / urlmatch.c
1 const char urlmatch_rcs[] = "$Id: urlmatch.c,v 1.20 2007/09/02 15:31:20 fabiankeil Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/urlmatch.c,v $
5  *
6  * Purpose     :  Declares functions to match URLs against URL
7  *                patterns.
8  *
9  * Copyright   :  Written by and Copyright (C) 2001-2003, 2006-2007 the SourceForge
10  *                Privoxy team. http://www.privoxy.org/
11  *
12  *                Based on the Internet Junkbuster originally written
13  *                by and Copyright (C) 1997 Anonymous Coders and
14  *                Junkbusters Corporation.  http://www.junkbusters.com
15  *
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.
21  *
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.
27  *
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.
33  *
34  * Revisions   :
35  *    $Log: urlmatch.c,v $
36  *    Revision 1.20  2007/09/02 15:31:20  fabiankeil
37  *    Move match_portlist() from filter.c to urlmatch.c.
38  *    It's used for url matching, not for filtering.
39  *
40  *    Revision 1.19  2007/09/02 13:42:11  fabiankeil
41  *    - Allow port lists in url patterns.
42  *    - Ditch unused url_spec member pathlen.
43  *
44  *    Revision 1.18  2007/07/30 16:42:21  fabiankeil
45  *    Move the method check into unknown_method()
46  *    and loop through the known methods instead
47  *    of using a screen-long OR chain.
48  *
49  *    Revision 1.17  2007/04/15 16:39:21  fabiankeil
50  *    Introduce tags as alternative way to specify which
51  *    actions apply to a request. At the moment tags can be
52  *    created based on client and server headers.
53  *
54  *    Revision 1.16  2007/02/13 13:59:24  fabiankeil
55  *    Remove redundant log message.
56  *
57  *    Revision 1.15  2007/01/28 16:11:23  fabiankeil
58  *    Accept WebDAV methods for subversion
59  *    in parse_http_request(). Closes FR 1581425.
60  *
61  *    Revision 1.14  2007/01/06 14:23:56  fabiankeil
62  *    Fix gcc43 warnings. Mark *csp as immutable
63  *    for parse_http_url() and url_match().
64  *    Replace a sprintf call with snprintf.
65  *
66  *    Revision 1.13  2006/12/06 19:50:54  fabiankeil
67  *    parse_http_url() now handles intercepted
68  *    HTTP request lines as well. Moved parts
69  *    of parse_http_url()'s code into
70  *    init_domain_components() so that it can
71  *    be reused in chat().
72  *
73  *    Revision 1.12  2006/07/18 14:48:47  david__schmidt
74  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
75  *    with what was really the latest development (the v_3_0_branch branch)
76  *
77  *    Revision 1.10.2.7  2003/05/17 15:57:24  oes
78  *     - parse_http_url now checks memory allocation failure for
79  *       duplication of "*" URL and rejects "*something" URLs
80  *       Closes bug #736344
81  *     - Added a comment to what might look like a bug in
82  *       create_url_spec (see !bug #736931)
83  *     - Comment cosmetics
84  *
85  *    Revision 1.10.2.6  2003/05/07 12:39:48  oes
86  *    Fix typo: Default port for https URLs is 443, not 143.
87  *    Thanks to Scott Tregear for spotting this one.
88  *
89  *    Revision 1.10.2.5  2003/02/28 13:09:29  oes
90  *    Fixed a rare double free condition as per Bug #694713
91  *
92  *    Revision 1.10.2.4  2003/02/28 12:57:44  oes
93  *    Moved freeing of http request structure to its owner
94  *    as per Dan Price's observations in Bug #694713
95  *
96  *    Revision 1.10.2.3  2002/11/12 16:50:40  oes
97  *    Fixed memory leak in parse_http_request() reported by Oliver Stoeneberg. Fixes bug #637073
98  *
99  *    Revision 1.10.2.2  2002/09/25 14:53:15  oes
100  *    Added basic support for OPTIONS and TRACE HTTP methods:
101  *    parse_http_url now recognizes the "*" URI as well as
102  *    the OPTIONS and TRACE method keywords.
103  *
104  *    Revision 1.10.2.1  2002/06/06 19:06:44  jongfoster
105  *    Adding support for proprietary Microsoft WebDAV extensions
106  *
107  *    Revision 1.10  2002/05/12 21:40:37  jongfoster
108  *    - Removing some unused code
109  *
110  *    Revision 1.9  2002/04/04 00:36:36  gliptak
111  *    always use pcre for matching
112  *
113  *    Revision 1.8  2002/04/03 23:32:47  jongfoster
114  *    Fixing memory leak on error
115  *
116  *    Revision 1.7  2002/03/26 22:29:55  swa
117  *    we have a new homepage!
118  *
119  *    Revision 1.6  2002/03/24 13:25:43  swa
120  *    name change related issues
121  *
122  *    Revision 1.5  2002/03/13 00:27:05  jongfoster
123  *    Killing warnings
124  *
125  *    Revision 1.4  2002/03/07 03:46:17  oes
126  *    Fixed compiler warnings
127  *
128  *    Revision 1.3  2002/03/03 14:51:11  oes
129  *    Fixed CLF logging: Added ocmd member for client's request to struct http_request
130  *
131  *    Revision 1.2  2002/01/21 00:14:09  jongfoster
132  *    Correcting comment style
133  *    Fixing an uninitialized memory bug in create_url_spec()
134  *
135  *    Revision 1.1  2002/01/17 20:53:46  jongfoster
136  *    Moving all our URL and URL pattern parsing code to the same file - it
137  *    was scattered around in filters.c, loaders.c and parsers.c.
138  *
139  *    Providing a single, simple url_match(pattern,url) function - rather than
140  *    the 3-line match routine which was repeated all over the place.
141  *
142  *    Renaming free_url to free_url_spec, since it frees a struct url_spec.
143  *
144  *    Providing parse_http_url() so that URLs can be parsed without faking a
145  *    HTTP request line for parse_http_request() or repeating the parsing
146  *    code (both of which were techniques that were actually in use).
147  *
148  *    Standardizing that struct http_request is used to represent a URL, and
149  *    struct url_spec is used to represent a URL pattern.  (Before, URLs were
150  *    represented as seperate variables and a partially-filled-in url_spec).
151  *
152  *
153  *********************************************************************/
154 \f
155
156 #include "config.h"
157
158 #ifndef _WIN32
159 #include <stdio.h>
160 #include <sys/types.h>
161 #endif
162
163 #include <stdlib.h>
164 #include <ctype.h>
165 #include <assert.h>
166 #include <string.h>
167
168 #if !defined(_WIN32) && !defined(__OS2__)
169 #include <unistd.h>
170 #endif
171
172 #include "project.h"
173 #include "urlmatch.h"
174 #include "ssplit.h"
175 #include "miscutil.h"
176 #include "errlog.h"
177
178 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
179
180
181 /*********************************************************************
182  *
183  * Function    :  free_http_request
184  *
185  * Description :  Freez a http_request structure
186  *
187  * Parameters  :
188  *          1  :  http = points to a http_request structure to free
189  *
190  * Returns     :  N/A
191  *
192  *********************************************************************/
193 void free_http_request(struct http_request *http)
194 {
195    assert(http);
196
197    freez(http->cmd);
198    freez(http->ocmd);
199    freez(http->gpc);
200    freez(http->host);
201    freez(http->url);
202    freez(http->hostport);
203    freez(http->path);
204    freez(http->ver);
205    freez(http->host_ip_addr_str);
206    freez(http->dbuffer);
207    freez(http->dvec);
208    http->dcount = 0;
209 }
210
211 /*********************************************************************
212  *
213  * Function    :  init_domain_components
214  *
215  * Description :  Splits the domain name so we can compare it
216  *                against wildcards. It used to be part of
217  *                parse_http_url, but was separated because the
218  *                same code is required in chat in case of
219  *                intercepted requests.
220  *
221  * Parameters  :
222  *          1  :  http = pointer to the http structure to hold elements.
223  *
224  * Returns     :  JB_ERR_OK on success
225  *                JB_ERR_MEMORY on out of memory
226  *                JB_ERR_PARSE on malformed command/URL
227  *                             or >100 domains deep.
228  *
229  *********************************************************************/
230 jb_err init_domain_components(struct http_request *http)
231 {
232    char *vec[BUFFER_SIZE];
233    size_t size;
234    char *p;
235
236    http->dbuffer = strdup(http->host);
237    if (NULL == http->dbuffer)
238    {
239       return JB_ERR_MEMORY;
240    }
241
242    /* map to lower case */
243    for (p = http->dbuffer; *p ; p++)
244    {
245       *p = (char)tolower((int)(unsigned char)*p);
246    }
247
248    /* split the domain name into components */
249    http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
250
251    if (http->dcount <= 0)
252    {
253       /*
254        * Error: More than SZ(vec) components in domain
255        *    or: no components in domain
256        */
257       log_error(LOG_LEVEL_ERROR, "More than SZ(vec) components in domain or none at all.");
258       return JB_ERR_PARSE;
259    }
260
261    /* save a copy of the pointers in dvec */
262    size = (size_t)http->dcount * sizeof(*http->dvec);
263
264    http->dvec = (char **)malloc(size);
265    if (NULL == http->dvec)
266    {
267       return JB_ERR_MEMORY;
268    }
269
270    memcpy(http->dvec, vec, size);
271
272    return JB_ERR_OK;
273 }
274
275 /*********************************************************************
276  *
277  * Function    :  parse_http_url
278  *
279  * Description :  Parse out the host and port from the URL.  Find the
280  *                hostname & path, port (if ':'), and/or password (if '@')
281  *
282  * Parameters  :
283  *          1  :  url = URL (or is it URI?) to break down
284  *          2  :  http = pointer to the http structure to hold elements.
285  *                       Will be zeroed before use.  Note that this
286  *                       function sets the http->gpc and http->ver
287  *                       members to NULL.
288  *          3  :  csp = Current client state (buffers, headers, etc...)
289  *
290  * Returns     :  JB_ERR_OK on success
291  *                JB_ERR_MEMORY on out of memory
292  *                JB_ERR_PARSE on malformed command/URL
293  *                             or >100 domains deep.
294  *
295  *********************************************************************/
296 jb_err parse_http_url(const char * url,
297                       struct http_request *http,
298                       const struct client_state *csp)
299 {
300    int host_available = 1; /* A proxy can dream. */
301
302    /*
303     * Zero out the results structure
304     */
305    memset(http, '\0', sizeof(*http));
306
307
308    /*
309     * Save our initial URL
310     */
311    http->url = strdup(url);
312    if (http->url == NULL)
313    {
314       return JB_ERR_MEMORY;
315    }
316
317
318    /*
319     * Check for * URI. If found, we're done.
320     */  
321    if (*http->url == '*')
322    {
323       if  ( NULL == (http->path = strdup("*"))
324          || NULL == (http->hostport = strdup("")) ) 
325       {
326          return JB_ERR_MEMORY;
327       }
328       if (http->url[1] != '\0')
329       {
330          return JB_ERR_PARSE;
331       }
332       return JB_ERR_OK;
333    }
334
335
336    /*
337     * Split URL into protocol,hostport,path.
338     */
339    {
340       char *buf;
341       char *url_noproto;
342       char *url_path;
343
344       buf = strdup(url);
345       if (buf == NULL)
346       {
347          return JB_ERR_MEMORY;
348       }
349
350       /* Find the start of the URL in our scratch space */
351       url_noproto = buf;
352       if (strncmpic(url_noproto, "http://",  7) == 0)
353       {
354          url_noproto += 7;
355          http->ssl = 0;
356       }
357       else if (strncmpic(url_noproto, "https://", 8) == 0)
358       {
359          url_noproto += 8;
360          http->ssl = 1;
361       }
362       else if (*url_noproto == '/')
363       {
364         /*
365          * Short request line without protocol and host.
366          * Most likely because the client's request
367          * was intercepted and redirected into Privoxy.
368          */
369          http->ssl = 0;
370          http->host = NULL;
371          host_available = 0;
372       }
373       else
374       {
375          http->ssl = 0;
376       }
377
378       url_path = strchr(url_noproto, '/');
379       if (url_path != NULL)
380       {
381          /*
382           * Got a path.
383           *
384           * NOTE: The following line ignores the path for HTTPS URLS.
385           * This means that you get consistent behaviour if you type a
386           * https URL in and it's parsed by the function.  (When the
387           * URL is actually retrieved, SSL hides the path part).
388           */
389          http->path = strdup(http->ssl ? "/" : url_path);
390          *url_path = '\0';
391          http->hostport = strdup(url_noproto);
392       }
393       else
394       {
395          /*
396           * Repair broken HTTP requests that don't contain a path,
397           * or CONNECT requests
398           */
399          http->path = strdup("/");
400          http->hostport = strdup(url_noproto);
401       }
402
403       freez(buf);
404
405       if ( (http->path == NULL)
406         || (http->hostport == NULL))
407       {
408          return JB_ERR_MEMORY;
409       }
410    }
411
412    if (!host_available)
413    {
414       /* Without host, there is nothing left to do here */
415       return JB_ERR_OK;
416    }
417
418    /*
419     * Split hostport into user/password (ignored), host, port.
420     */
421    {
422       char *buf;
423       char *host;
424       char *port;
425
426       buf = strdup(http->hostport);
427       if (buf == NULL)
428       {
429          return JB_ERR_MEMORY;
430       }
431
432       /* check if url contains username and/or password */
433       host = strchr(buf, '@');
434       if (host != NULL)
435       {
436          /* Contains username/password, skip it and the @ sign. */
437          host++;
438       }
439       else
440       {
441          /* No username or password. */
442          host = buf;
443       }
444
445       /* check if url contains port */
446       port = strchr(host, ':');
447       if (port != NULL)
448       {
449          /* Contains port */
450          /* Terminate hostname and point to start of port string */
451          *port++ = '\0';
452          http->port = atoi(port);
453       }
454       else
455       {
456          /* No port specified. */
457          http->port = (http->ssl ? 443 : 80);
458       }
459
460       http->host = strdup(host);
461
462       free(buf);
463
464       if (http->host == NULL)
465       {
466          return JB_ERR_MEMORY;
467       }
468    }
469
470    /*
471     * Split domain name so we can compare it against wildcards
472     */
473    return init_domain_components(http);
474
475 }
476
477
478 /*********************************************************************
479  *
480  * Function    :  unknown_method
481  *
482  * Description :  Checks whether a method is unknown.
483  *
484  * Parameters  :
485  *          1  :  method = points to a http method
486  *
487  * Returns     :  TRUE if it's unknown, FALSE otherwise.
488  *
489  *********************************************************************/
490 static int unknown_method(const char *method)
491 {
492    static const char *known_http_methods[] = {
493       /* Basic HTTP request type */
494       "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "TRACE", "CONNECT",
495       /* webDAV extensions (RFC2518) */
496       "PROPFIND", "PROPPATCH", "MOVE", "COPY", "MKCOL", "LOCK", "UNLOCK",
497       /*
498        * Microsoft webDAV extension for Exchange 2000.  See:
499        * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
500        * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
501        */ 
502       "BCOPY", "BMOVE", "BDELETE", "BPROPFIND", "BPROPPATCH",
503       /*
504        * Another Microsoft webDAV extension for Exchange 2000.  See:
505        * http://systems.cs.colorado.edu/grunwald/MobileComputing/Papers/draft-cohen-gena-p-base-00.txt
506        * http://lists.w3.org/Archives/Public/w3c-dist-auth/2002JanMar/0001.html
507        * http://msdn.microsoft.com/library/en-us/wss/wss/_webdav_methods.asp
508        */ 
509       "SUBSCRIBE", "UNSUBSCRIBE", "NOTIFY", "POLL",
510       /*
511        * Yet another WebDAV extension, this time for
512        * Web Distributed Authoring and Versioning (RFC3253)
513        */
514       "VERSION-CONTROL", "REPORT", "CHECKOUT", "CHECKIN", "UNCHECKOUT",
515       "MKWORKSPACE", "UPDATE", "LABEL", "MERGE", "BASELINE-CONTROL", "MKACTIVITY",
516       NULL
517    };
518    int i;
519
520    for (i = 0; NULL != known_http_methods[i]; i++)
521    {
522       if (0 == strcmpic(method, known_http_methods[i]))
523       {
524          return FALSE;
525       }
526    }
527
528    return TRUE;
529
530 }
531
532
533 /*********************************************************************
534  *
535  * Function    :  parse_http_request
536  *
537  * Description :  Parse out the host and port from the URL.  Find the
538  *                hostname & path, port (if ':'), and/or password (if '@')
539  *
540  * Parameters  :
541  *          1  :  req = HTTP request line to break down
542  *          2  :  http = pointer to the http structure to hold elements
543  *          3  :  csp = Current client state (buffers, headers, etc...)
544  *
545  * Returns     :  JB_ERR_OK on success
546  *                JB_ERR_MEMORY on out of memory
547  *                JB_ERR_CGI_PARAMS on malformed command/URL
548  *                                  or >100 domains deep.
549  *
550  *********************************************************************/
551 jb_err parse_http_request(const char *req,
552                           struct http_request *http,
553                           const struct client_state *csp)
554 {
555    char *buf;
556    char *v[10]; /* XXX: Why 10? We should only need three. */
557    int n;
558    jb_err err;
559    int is_connect = 0;
560
561    memset(http, '\0', sizeof(*http));
562
563    buf = strdup(req);
564    if (buf == NULL)
565    {
566       return JB_ERR_MEMORY;
567    }
568
569    n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
570    if (n != 3)
571    {
572       free(buf);
573       return JB_ERR_PARSE;
574    }
575
576    /*
577     * Fail in case of unknown methods
578     * which we might not handle correctly.
579     *
580     * XXX: There should be a config option
581     * to forward requests with unknown methods
582     * anyway. Most of them don't need special
583     * steps.
584     */
585    if (unknown_method(v[0]))
586    {
587       log_error(LOG_LEVEL_ERROR, "Unknown HTTP method detected: %s", v[0]);
588       free(buf);
589       return JB_ERR_PARSE;
590    }
591
592    if (strcmpic(v[0], "CONNECT") == 0)
593    {
594       is_connect = 1;
595    }
596
597    err = parse_http_url(v[1], http, csp);
598    if (err)
599    {
600       free(buf);
601       return err;
602    }
603
604    /*
605     * Copy the details into the structure
606     */
607    http->ssl = is_connect;
608    http->cmd = strdup(req);
609    http->gpc = strdup(v[0]);
610    http->ver = strdup(v[2]);
611
612    if ( (http->cmd == NULL)
613      || (http->gpc == NULL)
614      || (http->ver == NULL) )
615    {
616       free(buf);
617       return JB_ERR_MEMORY;
618    }
619
620    free(buf);
621    return JB_ERR_OK;
622
623 }
624
625
626 /*********************************************************************
627  *
628  * Function    :  simple_domaincmp
629  *
630  * Description :  Domain-wise Compare fqdn's.  The comparison is
631  *                both left- and right-anchored.  The individual
632  *                domain names are compared with simplematch().
633  *                This is only used by domain_match.
634  *
635  * Parameters  :
636  *          1  :  pv = array of patterns to compare
637  *          2  :  fv = array of domain components to compare
638  *          3  :  len = length of the arrays (both arrays are the
639  *                      same length - if they weren't, it couldn't
640  *                      possibly be a match).
641  *
642  * Returns     :  0 => domains are equivalent, else no match.
643  *
644  *********************************************************************/
645 static int simple_domaincmp(char **pv, char **fv, int len)
646 {
647    int n;
648
649    for (n = 0; n < len; n++)
650    {
651       if (simplematch(pv[n], fv[n]))
652       {
653          return 1;
654       }
655    }
656
657    return 0;
658
659 }
660
661
662 /*********************************************************************
663  *
664  * Function    :  domain_match
665  *
666  * Description :  Domain-wise Compare fqdn's. Governed by the bimap in
667  *                pattern->unachored, the comparison is un-, left-,
668  *                right-anchored, or both.
669  *                The individual domain names are compared with
670  *                simplematch().
671  *
672  * Parameters  :
673  *          1  :  pattern = a domain that may contain a '*' as a wildcard.
674  *          2  :  fqdn = domain name against which the patterns are compared.
675  *
676  * Returns     :  0 => domains are equivalent, else no match.
677  *
678  *********************************************************************/
679 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
680 {
681    char **pv, **fv;  /* vectors  */
682    int    plen, flen;
683    int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
684
685    plen = pattern->dcount;
686    flen = fqdn->dcount;
687
688    if (flen < plen)
689    {
690       /* fqdn is too short to match this pattern */
691       return 1;
692    }
693
694    pv   = pattern->dvec;
695    fv   = fqdn->dvec;
696
697    if (unanchored == ANCHOR_LEFT)
698    {
699       /*
700        * Right anchored.
701        *
702        * Convert this into a fully anchored pattern with
703        * the fqdn and pattern the same length
704        */
705       fv += (flen - plen); /* flen - plen >= 0 due to check above */
706       return simple_domaincmp(pv, fv, plen);
707    }
708    else if (unanchored == 0)
709    {
710       /* Fully anchored, check length */
711       if (flen != plen)
712       {
713          return 1;
714       }
715       return simple_domaincmp(pv, fv, plen);
716    }
717    else if (unanchored == ANCHOR_RIGHT)
718    {
719       /* Left anchored, ignore all extra in fqdn */
720       return simple_domaincmp(pv, fv, plen);
721    }
722    else
723    {
724       /* Unanchored */
725       int n;
726       int maxn = flen - plen;
727       for (n = 0; n <= maxn; n++)
728       {
729          if (!simple_domaincmp(pv, fv, plen))
730          {
731             return 0;
732          }
733          /*
734           * Doesn't match from start of fqdn
735           * Try skipping first part of fqdn
736           */
737          fv++;
738       }
739       return 1;
740    }
741
742 }
743
744
745 /*********************************************************************
746  *
747  * Function    :  create_url_spec
748  *
749  * Description :  Creates a "url_spec" structure from a string.
750  *                When finished, free with free_url_spec().
751  *
752  * Parameters  :
753  *          1  :  url = Target url_spec to be filled in.  Will be
754  *                      zeroed before use.
755  *          2  :  buf = Source pattern, null terminated.  NOTE: The
756  *                      contents of this buffer are destroyed by this
757  *                      function.  If this function succeeds, the
758  *                      buffer is copied to url->spec.  If this
759  *                      function fails, the contents of the buffer
760  *                      are lost forever.
761  *
762  * Returns     :  JB_ERR_OK - Success
763  *                JB_ERR_MEMORY - Out of memory
764  *                JB_ERR_PARSE - Cannot parse regex (Detailed message
765  *                               written to system log)
766  *
767  *********************************************************************/
768 jb_err create_url_spec(struct url_spec * url, const char * buf)
769 {
770    char *p;
771    int errcode;
772    size_t errlen;
773    char rebuf[BUFFER_SIZE];
774
775    assert(url);
776    assert(buf);
777
778    /*
779     * Zero memory
780     */
781    memset(url, '\0', sizeof(*url));
782
783    /*
784     * Save a copy of the orignal specification
785     */
786    if ((url->spec = strdup(buf)) == NULL)
787    {
788       return JB_ERR_MEMORY;
789    }
790
791    /* Is it tag pattern? */
792    if (0 == strncmpic("TAG:", url->spec, 4))
793    {
794       if (NULL == (url->tag_regex = zalloc(sizeof(*url->tag_regex))))
795       {
796          freez(url->spec);
797          return JB_ERR_MEMORY;
798       }
799
800       /* buf + 4 to skip "TAG:" */
801       errcode = regcomp(url->tag_regex, buf + 4, (REG_EXTENDED|REG_NOSUB|REG_ICASE));
802       if (errcode)
803       {
804          errlen = regerror(errcode, url->preg, rebuf, sizeof(rebuf));
805          if (errlen > (sizeof(rebuf) - 1))
806          {
807             errlen = sizeof(rebuf) - 1;
808          }
809          rebuf[errlen] = '\0';
810
811          log_error(LOG_LEVEL_ERROR, "error compiling %s: %s", url->spec, rebuf);
812
813          freez(url->spec);
814          regfree(url->tag_regex);
815          freez(url->tag_regex);
816
817          return JB_ERR_PARSE;
818       }
819       return JB_ERR_OK;
820    }
821
822    /* Only reached for URL patterns */
823    p = strchr(buf, '/');
824    if (NULL != p)
825    {
826       url->path = strdup(p);
827       if (NULL == url->path)
828       {
829          freez(url->spec);
830          return JB_ERR_MEMORY;
831       }
832       *p = '\0';
833    }
834    else
835    {
836       url->path = NULL;
837    }
838    if (url->path)
839    {
840       if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
841       {
842          freez(url->spec);
843          freez(url->path);
844          return JB_ERR_MEMORY;
845       }
846
847       snprintf(rebuf, sizeof(rebuf), "^(%s)", url->path);
848
849       errcode = regcomp(url->preg, rebuf,
850             (REG_EXTENDED|REG_NOSUB|REG_ICASE));
851       if (errcode)
852       {
853          errlen = regerror(errcode, url->preg, rebuf, sizeof(rebuf));
854
855          if (errlen > (sizeof(rebuf) - (size_t)1))
856          {
857             errlen = sizeof(rebuf) - (size_t)1;
858          }
859          rebuf[errlen] = '\0';
860
861          log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
862             url->spec, rebuf);
863
864          freez(url->spec);
865          freez(url->path);
866          regfree(url->preg);
867          freez(url->preg);
868
869          return JB_ERR_PARSE;
870       }
871    }
872
873    p = strchr(buf, ':');
874    if (NULL != p)
875    {
876       *p++ = '\0';
877       url->port_list = strdup(p);
878       if (NULL == url->port_list)
879       {
880          return JB_ERR_MEMORY;
881       }
882    }
883    else
884    {
885       url->port_list = NULL;
886    }
887
888    if (buf[0] != '\0')
889    {
890       char *v[150];
891       size_t size;
892
893       /*
894        * Parse domain part
895        */
896       if (buf[strlen(buf) - 1] == '.')
897       {
898          url->unanchored |= ANCHOR_RIGHT;
899       }
900       if (buf[0] == '.')
901       {
902          url->unanchored |= ANCHOR_LEFT;
903       }
904
905       /* 
906        * Split domain into components
907        */
908       url->dbuffer = strdup(buf);
909       if (NULL == url->dbuffer)
910       {
911          freez(url->spec);
912          freez(url->path);
913          regfree(url->preg);
914          freez(url->preg);
915          return JB_ERR_MEMORY;
916       }
917
918       /* 
919        * Map to lower case
920        */
921       for (p = url->dbuffer; *p ; p++)
922       {
923          *p = (char)tolower((int)(unsigned char)*p);
924       }
925
926       /* 
927        * Split the domain name into components
928        */
929       url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
930
931       if (url->dcount < 0)
932       {
933          freez(url->spec);
934          freez(url->path);
935          regfree(url->preg);
936          freez(url->preg);
937          freez(url->dbuffer);
938          url->dcount = 0;
939          return JB_ERR_MEMORY;
940       }
941       else if (url->dcount != 0)
942       {
943
944          /* 
945           * Save a copy of the pointers in dvec
946           */
947          size = (size_t)url->dcount * sizeof(*url->dvec);
948
949          url->dvec = (char **)malloc(size);
950          if (NULL == url->dvec)
951          {
952             freez(url->spec);
953             freez(url->path);
954             regfree(url->preg);
955             freez(url->preg);
956             freez(url->dbuffer);
957             url->dcount = 0;
958             return JB_ERR_MEMORY;
959          }
960
961          memcpy(url->dvec, v, size);
962       }
963       /*
964        * else dcount == 0 in which case we needn't do anything,
965        * since dvec will never be accessed and the pattern will
966        * match all domains.
967        */
968    }
969
970    return JB_ERR_OK;
971
972 }
973
974
975 /*********************************************************************
976  *
977  * Function    :  free_url_spec
978  *
979  * Description :  Called from the "unloaders".  Freez the url
980  *                structure elements.
981  *
982  * Parameters  :
983  *          1  :  url = pointer to a url_spec structure.
984  *
985  * Returns     :  N/A
986  *
987  *********************************************************************/
988 void free_url_spec(struct url_spec *url)
989 {
990    if (url == NULL) return;
991
992    freez(url->spec);
993    freez(url->dbuffer);
994    freez(url->dvec);
995    freez(url->path);
996    freez(url->port_list);
997    if (url->preg)
998    {
999       regfree(url->preg);
1000       freez(url->preg);
1001    }
1002    if (url->tag_regex)
1003    {
1004       regfree(url->tag_regex);
1005       freez(url->tag_regex);
1006    }
1007 }
1008
1009
1010 /*********************************************************************
1011  *
1012  * Function    :  url_match
1013  *
1014  * Description :  Compare a URL against a URL pattern.
1015  *
1016  * Parameters  :
1017  *          1  :  pattern = a URL pattern
1018  *          2  :  url = URL to match
1019  *
1020  * Returns     :  Nonzero if the URL matches the pattern, else 0.
1021  *
1022  *********************************************************************/
1023 int url_match(const struct url_spec *pattern,
1024               const struct http_request *url)
1025 {
1026    /* XXX: these should probably be functions. */
1027 #define PORT_MATCHES ((NULL == pattern->port_list) || match_portlist(pattern->port_list, url->port))
1028 #define DOMAIN_MATCHES ((NULL == pattern->dbuffer) || (0 == domain_match(pattern, url)))
1029 #define PATH_MATCHES ((NULL == pattern->path) || (0 == regexec(pattern->preg, url->path, 0, NULL, 0)))
1030
1031    if (pattern->tag_regex != NULL)
1032    {
1033       /* It's a tag pattern and shouldn't be matched against URLs */
1034       return 0;
1035    } 
1036
1037    return (PORT_MATCHES && DOMAIN_MATCHES && PATH_MATCHES);
1038
1039 }
1040
1041
1042 /*********************************************************************
1043  *
1044  * Function    :  match_portlist
1045  *
1046  * Description :  Check if a given number is covered by a comma
1047  *                separated list of numbers and ranges (a,b-c,d,..)
1048  *
1049  * Parameters  :
1050  *          1  :  portlist = String with list
1051  *          2  :  port = port to check
1052  *
1053  * Returns     :  0 => no match
1054  *                1 => match
1055  *
1056  *********************************************************************/
1057 int match_portlist(const char *portlist, int port)
1058 {
1059    char *min, *max, *next, *portlist_copy;
1060
1061    min = next = portlist_copy = strdup(portlist);
1062
1063    /*
1064     * Zero-terminate first item and remember offset for next
1065     */
1066    if (NULL != (next = strchr(portlist_copy, (int) ',')))
1067    {
1068       *next++ = '\0';
1069    }
1070
1071    /*
1072     * Loop through all items, checking for match
1073     */
1074    while(min)
1075    {
1076       if (NULL == (max = strchr(min, (int) '-')))
1077       {
1078          /*
1079           * No dash, check for equality
1080           */
1081          if (port == atoi(min))
1082          {
1083             free(portlist_copy);
1084             return(1);
1085          }
1086       }
1087       else
1088       {
1089          /*
1090           * This is a range, so check if between min and max,
1091           * or, if max was omitted, between min and 65K
1092           */
1093          *max++ = '\0';
1094          if(port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
1095          {
1096             free(portlist_copy);
1097             return(1);
1098          }
1099
1100       }
1101
1102       /*
1103        * Jump to next item
1104        */
1105       min = next;
1106
1107       /*
1108        * Zero-terminate next item and remember offset for n+1
1109        */
1110       if ((NULL != next) && (NULL != (next = strchr(next, (int) ','))))
1111       {
1112          *next++ = '\0';
1113       }
1114    }
1115
1116    free(portlist_copy);
1117    return 0;
1118
1119 }
1120
1121
1122 /*
1123   Local Variables:
1124   tab-width: 3
1125   end:
1126 */