1 const char urlmatch_rcs[] = "$Id: urlmatch.c JGF $";
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 the SourceForge
10 * IJBSWA team. http://ijbswa.sourceforge.net
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 $
37 *********************************************************************/
44 #include <sys/types.h>
52 #if !defined(_WIN32) && !defined(__OS2__)
62 const char urlmatch_h_rcs[] = URLMATCH_H_VERSION;
64 /* Fix a problem with Solaris. There should be no effect on other
66 * Solaris's isspace() is a macro which uses it's argument directly
67 * as an array index. Therefore we need to make sure that high-bit
68 * characters generate +ve values, and ideally we also want to make
69 * the argument match the declared parameter type of "int".
71 * Why did they write a character function that can't take a simple
72 * "char" argument? Doh!
74 #define ijb_isupper(__X) isupper((int)(unsigned char)(__X))
75 #define ijb_tolower(__X) tolower((int)(unsigned char)(__X))
78 /*********************************************************************
80 * Function : free_http_request
82 * Description : Freez a http_request structure
85 * 1 : http = points to a http_request structure to free
89 *********************************************************************/
90 void free_http_request(struct http_request *http)
98 freez(http->hostport);
101 freez(http->host_ip_addr_str);
102 freez(http->dbuffer);
108 /*********************************************************************
110 * Function : parse_http_url
112 * Description : Parse out the host and port from the URL. Find the
113 * hostname & path, port (if ':'), and/or password (if '@')
116 * 1 : url = URL (or is it URI?) to break down
117 * 2 : http = pointer to the http structure to hold elements.
118 * Will be zeroed before use. Note that this
119 * function sets the http->gpc and http->ver
121 * 3 : csp = Current client state (buffers, headers, etc...)
123 * Returns : JB_ERR_OK on success
124 * JB_ERR_MEMORY on out of memory
125 * JB_ERR_CGI_PARAMS on malformed command/URL
126 * or >100 domains deep.
128 *********************************************************************/
129 jb_err parse_http_url(const char * url,
130 struct http_request *http,
131 struct client_state *csp)
134 * Zero out the results structure
136 memset(http, '\0', sizeof(*http));
140 * Save our initial URL
142 http->url = strdup(url);
143 if (http->url == NULL)
145 return JB_ERR_MEMORY;
150 * Split URL into protocol,hostport,path.
160 return JB_ERR_MEMORY;
163 /* Find the start of the URL in our scratch space */
165 if (strncmpic(url_noproto, "http://", 7) == 0)
170 else if (strncmpic(url_noproto, "https://", 8) == 0)
180 url_path = strchr(url_noproto, '/');
181 if (url_path != NULL)
186 * NOTE: The following line ignores the path for HTTPS URLS.
187 * This means that you get consistent behaviour if you type a
188 * https URL in and it's parsed by the function. (When the
189 * URL is actually retrieved, SSL hides the path part).
191 http->path = strdup(http->ssl ? "/" : url_path);
193 http->hostport = strdup(url_noproto);
198 * Repair broken HTTP requests that don't contain a path,
199 * or CONNECT requests
201 http->path = strdup("/");
202 http->hostport = strdup(url_noproto);
207 if ( (http->path == NULL)
208 || (http->hostport == NULL))
211 free_http_request(http);
212 return JB_ERR_MEMORY;
218 * Split hostport into user/password (ignored), host, port.
225 buf = strdup(http->hostport);
228 free_http_request(http);
229 return JB_ERR_MEMORY;
232 /* check if url contains username and/or password */
233 host = strchr(buf, '@');
236 /* Contains username/password, skip it and the @ sign. */
241 /* No username or password. */
245 /* check if url contains port */
246 port = strchr(host, ':');
250 /* Terminate hostname and point to start of port string */
252 http->port = atoi(port);
256 /* No port specified. */
257 http->port = (http->ssl ? 143 : 80);
260 http->host = strdup(host);
264 if (http->host == NULL)
266 free_http_request(http);
267 return JB_ERR_MEMORY;
273 * Split domain name so we can compare it against wildcards
276 char *vec[BUFFER_SIZE];
280 http->dbuffer = strdup(http->host);
281 if (NULL == http->dbuffer)
283 free_http_request(http);
284 return JB_ERR_MEMORY;
287 /* map to lower case */
288 for (p = http->dbuffer; *p ; p++)
290 *p = tolower((int)(unsigned char)*p);
293 /* split the domain name into components */
294 http->dcount = ssplit(http->dbuffer, ".", vec, SZ(vec), 1, 1);
296 if (http->dcount <= 0)
298 // Error: More than SZ(vec) components in domain
299 // or: no components in domain
300 free_http_request(http);
304 /* save a copy of the pointers in dvec */
305 size = http->dcount * sizeof(*http->dvec);
307 http->dvec = (char **)malloc(size);
308 if (NULL == http->dvec)
310 free_http_request(http);
311 return JB_ERR_MEMORY;
314 memcpy(http->dvec, vec, size);
322 /*********************************************************************
324 * Function : parse_http_request
326 * Description : Parse out the host and port from the URL. Find the
327 * hostname & path, port (if ':'), and/or password (if '@')
330 * 1 : req = HTTP request line to break down
331 * 2 : http = pointer to the http structure to hold elements
332 * 3 : csp = Current client state (buffers, headers, etc...)
334 * Returns : JB_ERR_OK on success
335 * JB_ERR_MEMORY on out of memory
336 * JB_ERR_CGI_PARAMS on malformed command/URL
337 * or >100 domains deep.
339 *********************************************************************/
340 jb_err parse_http_request(const char *req,
341 struct http_request *http,
342 struct client_state *csp)
350 memset(http, '\0', sizeof(*http));
355 return JB_ERR_MEMORY;
358 n = ssplit(buf, " \r\n", v, SZ(v), 1, 1);
365 /* this could be a CONNECT request */
366 if (strcmpic(v[0], "connect") == 0)
371 /* or it could be any other basic HTTP request type */
372 else if ((0 == strcmpic(v[0], "get"))
373 || (0 == strcmpic(v[0], "head"))
374 || (0 == strcmpic(v[0], "post"))
375 || (0 == strcmpic(v[0], "put"))
376 || (0 == strcmpic(v[0], "delete"))
378 /* or a webDAV extension (RFC2518) */
379 || (0 == strcmpic(v[0], "propfind"))
380 || (0 == strcmpic(v[0], "proppatch"))
381 || (0 == strcmpic(v[0], "move"))
382 || (0 == strcmpic(v[0], "copy"))
383 || (0 == strcmpic(v[0], "mkcol"))
384 || (0 == strcmpic(v[0], "lock"))
385 || (0 == strcmpic(v[0], "unlock"))
393 /* Unknown HTTP method */
398 err = parse_http_url(v[1], http, csp);
406 * Copy the details into the structure
408 http->ssl = is_connect;
409 http->cmd = strdup(req);
410 http->gpc = strdup(v[0]);
411 http->ver = strdup(v[2]);
413 if ( (http->cmd == NULL)
414 || (http->gpc == NULL)
415 || (http->ver == NULL) )
418 free_http_request(http);
419 return JB_ERR_MEMORY;
426 /*********************************************************************
428 * Function : simple_domaincmp
430 * Description : Domain-wise Compare fqdn's. The comparison is
431 * both left- and right-anchored. The individual
432 * domain names are compared with simplematch().
433 * This is only used by domain_match.
436 * 1 : pv = array of patterns to compare
437 * 2 : fv = array of domain components to compare
438 * 3 : len = length of the arrays (both arrays are the
439 * same length - if they weren't, it couldn't
440 * possibly be a match).
442 * Returns : 0 => domains are equivalent, else no match.
444 *********************************************************************/
445 static int simple_domaincmp(char **pv, char **fv, int len)
449 for (n = 0; n < len; n++)
451 if (simplematch(pv[n], fv[n]))
462 /*********************************************************************
464 * Function : domain_match
466 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
467 * pattern->unachored, the comparison is un-, left-,
468 * right-anchored, or both.
469 * The individual domain names are compared with
473 * 1 : pattern = a domain that may contain a '*' as a wildcard.
474 * 2 : fqdn = domain name against which the patterns are compared.
476 * Returns : 0 => domains are equivalent, else no match.
478 *********************************************************************/
479 static int domain_match(const struct url_spec *pattern, const struct http_request *fqdn)
481 char **pv, **fv; /* vectors */
483 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
485 plen = pattern->dcount;
490 /* fqdn is too short to match this pattern */
497 if (unanchored == ANCHOR_LEFT)
502 * Convert this into a fully anchored pattern with
503 * the fqdn and pattern the same length
505 fv += (flen - plen); /* flen - plen >= 0 due to check above */
506 return simple_domaincmp(pv, fv, plen);
508 else if (unanchored == 0)
510 /* Fully anchored, check length */
515 return simple_domaincmp(pv, fv, plen);
517 else if (unanchored == ANCHOR_RIGHT)
519 /* Left anchored, ignore all extra in fqdn */
520 return simple_domaincmp(pv, fv, plen);
526 int maxn = flen - plen;
527 for (n = 0; n <= maxn; n++)
529 if (!simple_domaincmp(pv, fv, plen))
534 * Doesn't match from start of fqdn
535 * Try skipping first part of fqdn
545 /*********************************************************************
547 * Function : create_url_spec
549 * Description : Creates a "url_spec" structure from a string.
550 * When finished, free with unload_url().
553 * 1 : url = Target url_spec to be filled in. Must be
554 * zeroed out before the call (e.g. using zalloc).
555 * 2 : buf = Source pattern, null terminated. NOTE: The
556 * contents of this buffer are destroyed by this
557 * function. If this function succeeds, the
558 * buffer is copied to url->spec. If this
559 * function fails, the contents of the buffer
562 * Returns : JB_ERR_OK - Success
563 * JB_ERR_MEMORY - Out of memory
564 * JB_ERR_PARSE - Cannot parse regex (Detailed message
565 * written to system log)
567 *********************************************************************/
568 jb_err create_url_spec(struct url_spec * url, const char * buf)
575 /* save a copy of the orignal specification */
576 if ((url->spec = strdup(buf)) == NULL)
578 return JB_ERR_MEMORY;
581 if ((p = strchr(buf, '/')))
583 if (NULL == (url->path = strdup(p)))
586 return JB_ERR_MEMORY;
588 url->pathlen = strlen(url->path);
600 char rebuf[BUFFER_SIZE];
602 if (NULL == (url->preg = zalloc(sizeof(*url->preg))))
606 return JB_ERR_MEMORY;
609 sprintf(rebuf, "^(%s)", url->path);
611 errcode = regcomp(url->preg, rebuf,
612 (REG_EXTENDED|REG_NOSUB|REG_ICASE));
615 size_t errlen = regerror(errcode,
616 url->preg, rebuf, sizeof(rebuf));
618 if (errlen > (sizeof(rebuf) - (size_t)1))
620 errlen = sizeof(rebuf) - (size_t)1;
622 rebuf[errlen] = '\0';
624 log_error(LOG_LEVEL_ERROR, "error compiling %s: %s",
635 if ((p = strchr(buf, ':')) == NULL)
650 /* Parse domain part */
651 if (buf[strlen(buf) - 1] == '.')
653 url->unanchored |= ANCHOR_RIGHT;
657 url->unanchored |= ANCHOR_LEFT;
660 /* split domain into components */
662 url->dbuffer = strdup(buf);
663 if (NULL == url->dbuffer)
669 #endif /* def REGEX */
670 return JB_ERR_MEMORY;
673 /* map to lower case */
674 for (p = url->dbuffer; *p ; p++)
676 *p = tolower((int)(unsigned char)*p);
679 /* split the domain name into components */
680 url->dcount = ssplit(url->dbuffer, ".", v, SZ(v), 1, 1);
688 #endif /* def REGEX */
691 return JB_ERR_MEMORY;
693 else if (url->dcount != 0)
696 /* save a copy of the pointers in dvec */
697 size = url->dcount * sizeof(*url->dvec);
699 url->dvec = (char **)malloc(size);
700 if (NULL == url->dvec)
706 #endif /* def REGEX */
709 return JB_ERR_MEMORY;
712 memcpy(url->dvec, v, size);
721 /*********************************************************************
723 * Function : free_url_spec
725 * Description : Called from the "unloaders". Freez the url
726 * structure elements.
729 * 1 : url = pointer to a url_spec structure.
733 *********************************************************************/
734 void free_url_spec(struct url_spec *url)
736 if (url == NULL) return;
753 /*********************************************************************
755 * Function : url_match
757 * Description : Compare a URL against a URL pattern.
760 * 1 : pattern = a URL pattern
761 * 2 : url = URL to match
763 * Returns : 0 iff the URL matches the pattern, else nonzero.
765 *********************************************************************/
766 int url_match(const struct url_spec *pattern,
767 const struct http_request *url)
769 return ((pattern->port == 0) || (pattern->port == url->port))
770 && ((pattern->dbuffer == NULL) || (domain_match(pattern, url) == 0))
771 && ((pattern->path == NULL) ||
773 (regexec(pattern->preg, url->path, 0, NULL, 0) == 0)
775 (strncmp(pattern->path, url->path, pattern->pathlen) == 0)