]> granicus.if.org Git - apache/blob - server/util.c
Fix two spelling and syntax problems in ap_get_local_host().
[apache] / server / util.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon public domain software
55  * originally written at the National Center for Supercomputing Applications,
56  * University of Illinois, Urbana-Champaign.
57  */
58
59 /*
60  * util.c: string utility things
61  * 
62  * 3/21/93 Rob McCool
63  * 1995-96 Many changes by the Apache Software Foundation
64  * 
65  */
66
67 /* Debugging aid:
68  * #define DEBUG            to trace all cfg_open*()/cfg_closefile() calls
69  * #define DEBUG_CFG_LINES  to trace every line read from the config files
70  */
71
72 #define CORE_PRIVATE
73
74 #include "apr.h"
75 #include "apr_strings.h"
76
77 #if APR_HAVE_STDIO_H
78 #include <stdio.h>              /* for EOF */
79 #endif
80 #if APR_HAVE_NETINET_IN_H
81 #include <netinet/in.h>
82 #endif
83 #if APR_HAVE_SYS_SOCKET_H
84 #include <sys/socket.h>
85 #endif
86
87 #include "ap_config.h"
88 #include "ap_base64.h"
89 #include "httpd.h"
90 #include "http_main.h"
91 #include "http_log.h"
92 #include "http_protocol.h"
93 #include "http_config.h"
94 #include "util_ebcdic.h"
95
96 #ifdef HAVE_UNISTD_H
97 #include <unistd.h>
98 #endif
99 #ifdef HAVE_NETDB_H
100 #include <netdb.h>
101 #endif
102 #ifdef HAVE_ARPA_INET_H
103 #include <arpa/inet.h>
104 #endif
105 #ifdef HAVE_PWD_H
106 #include <pwd.h>
107 #endif
108 #ifdef HAVE_GRP_H
109 #include <grp.h>
110 #endif
111 #ifdef HAVE_STRINGS_H
112 #include <strings.h>
113 #endif
114
115 /* A bunch of functions in util.c scan strings looking for certain characters.
116  * To make that more efficient we encode a lookup table.  The test_char_table
117  * is generated automatically by gen_test_char.c.
118  */
119 #include "test_char.h"
120
121 /* we assume the folks using this ensure 0 <= c < 256... which means
122  * you need a cast to (unsigned char) first, you can't just plug a
123  * char in here and get it to work, because if char is signed then it
124  * will first be sign extended.
125  */
126 #define TEST_CHAR(c, f) (test_char_table[(unsigned)(c)] & (f))
127
128 /*
129  * Examine a field value (such as a media-/content-type) string and return
130  * it sans any parameters; e.g., strip off any ';charset=foo' and the like.
131  */
132 AP_DECLARE(char *) ap_field_noparam(apr_pool_t *p, const char *intype)
133 {
134     const char *semi;
135
136     if (intype == NULL) return NULL;
137
138     semi = ap_strchr_c(intype, ';');
139     if (semi == NULL) {
140         return apr_pstrdup(p, intype);
141     } 
142     else {
143         while ((semi > intype) && apr_isspace(semi[-1])) {
144             semi--;
145         }
146         return apr_pstrndup(p, intype, semi - intype);
147     }
148 }
149
150 AP_DECLARE(char *) ap_ht_time(apr_pool_t *p, apr_time_t t, const char *fmt, int gmt)
151 {
152     apr_size_t retcode;
153     char ts[MAX_STRING_LEN];
154     char tf[MAX_STRING_LEN];
155     apr_exploded_time_t xt;
156
157     if (gmt) {
158         const char *f;
159         char *strp;
160
161         apr_explode_gmt(&xt, t);
162         /* Convert %Z to "GMT" and %z to "+0000";
163          * on hosts that do not have a time zone string in struct tm,
164          * strftime must assume its argument is local time.
165          */
166         for(strp = tf, f = fmt; strp < tf + sizeof(tf) - 6 && (*strp = *f)
167             ; f++, strp++) {
168             if (*f != '%') continue;
169             switch (f[1]) {
170             case '%':
171                 *++strp = *++f;
172                 break;
173             case 'Z':
174                 *strp++ = 'G';
175                 *strp++ = 'M';
176                 *strp = 'T';
177                 f++;
178                 break;
179             case 'z': /* common extension */
180                 *strp++ = '+';
181                 *strp++ = '0';
182                 *strp++ = '0';
183                 *strp++ = '0';
184                 *strp = '0';
185                 f++;
186                 break;
187             }
188         }
189         *strp = '\0';
190         fmt = tf;
191     }
192     else {
193         apr_explode_localtime(&xt, t);
194     }
195
196     /* check return code? */
197     apr_strftime(ts, &retcode, MAX_STRING_LEN, fmt, &xt);
198     ts[MAX_STRING_LEN - 1] = '\0';
199     return apr_pstrdup(p, ts);
200 }
201
202 /* Roy owes Rob beer. */
203 /* Rob owes Roy dinner. */
204
205 /* These legacy comments would make a lot more sense if Roy hadn't
206  * replaced the old later_than() routine with util_date.c.
207  *
208  * Well, okay, they still wouldn't make any sense.
209  */
210
211 /* Match = 0, NoMatch = 1, Abort = -1
212  * Based loosely on sections of wildmat.c by Rich Salz
213  * Hmmm... shouldn't this really go component by component?
214  */
215 AP_DECLARE(int) ap_strcmp_match(const char *str, const char *exp)
216 {
217     int x, y;
218
219     for (x = 0, y = 0; exp[y]; ++y, ++x) {
220         if ((!str[x]) && (exp[y] != '*'))
221             return -1;
222         if (exp[y] == '*') {
223             while (exp[++y] == '*');
224             if (!exp[y])
225                 return 0;
226             while (str[x]) {
227                 int ret;
228                 if ((ret = ap_strcmp_match(&str[x++], &exp[y])) != 1)
229                     return ret;
230             }
231             return -1;
232         }
233         else if ((exp[y] != '?') && (str[x] != exp[y]))
234             return 1;
235     }
236     return (str[x] != '\0');
237 }
238
239 AP_DECLARE(int) ap_strcasecmp_match(const char *str, const char *exp)
240 {
241     int x, y;
242
243     for (x = 0, y = 0; exp[y]; ++y, ++x) {
244         if ((!str[x]) && (exp[y] != '*'))
245             return -1;
246         if (exp[y] == '*') {
247             while (exp[++y] == '*');
248             if (!exp[y])
249                 return 0;
250             while (str[x]) {
251                 int ret;
252                 if ((ret = ap_strcasecmp_match(&str[x++], &exp[y])) != 1)
253                     return ret;
254             }
255             return -1;
256         }
257         else if ((exp[y] != '?') && (apr_tolower(str[x]) != apr_tolower(exp[y])))
258             return 1;
259     }
260     return (str[x] != '\0');
261 }
262
263 AP_DECLARE(int) ap_is_matchexp(const char *str)
264 {
265     register int x;
266
267     for (x = 0; str[x]; x++)
268         if ((str[x] == '*') || (str[x] == '?'))
269             return 1;
270     return 0;
271 }
272
273 /*
274  * Here's a pool-based interface to POSIX regex's regcomp().
275  * Note that we return regex_t instead of being passed one.
276  * The reason is that if you use an already-used regex_t structure,
277  * the memory that you've already allocated gets forgotten, and
278  * regfree() doesn't clear it. So we don't allow it.
279  */
280
281 static apr_status_t regex_cleanup(void *preg)
282 {
283     regfree((regex_t *) preg);
284     return APR_SUCCESS;
285 }
286
287 AP_DECLARE(regex_t *) ap_pregcomp(apr_pool_t *p, const char *pattern,
288                                    int cflags)
289 {
290     regex_t *preg = apr_palloc(p, sizeof(regex_t));
291
292     if (regcomp(preg, pattern, cflags)) {
293         return NULL;
294     }
295
296     apr_register_cleanup(p, (void *) preg, regex_cleanup, regex_cleanup);
297
298     return preg;
299 }
300
301 AP_DECLARE(void) ap_pregfree(apr_pool_t *p, regex_t * reg)
302 {
303     regfree(reg);
304     apr_kill_cleanup(p, (void *) reg, regex_cleanup);
305 }
306
307 /*
308  * Similar to standard strstr() but we ignore case in this version.
309  * Based on the strstr() implementation further below.
310  */
311 AP_DECLARE(char *) ap_strcasestr(const char *s1, const char *s2)
312 {
313     char *p1, *p2;
314     if (*s2 == '\0') {
315         /* an empty s2 */
316         return((char *)s1);
317     }
318     while(1) {
319         for ( ; (*s1 != '\0') && (apr_tolower(*s1) != apr_tolower(*s2)); s1++);
320         if (*s1 == '\0') return(NULL);
321         /* found first character of s2, see if the rest matches */
322         p1 = (char *)s1;
323         p2 = (char *)s2;
324         while (apr_tolower(*++p1) == apr_tolower(*++p2)) {
325             if (*p1 == '\0') {
326                 /* both strings ended together */
327                 return((char *)s1);
328             }
329         }
330         if (*p2 == '\0') {
331             /* second string ended, a match */
332             break;
333         }
334         /* didn't find a match here, try starting at next character in s1 */
335         s1++;
336     }
337     return((char *)s1);
338 }
339
340 /*
341  * Returns an offsetted pointer in bigstring immediately after
342  * prefix. Returns bigstring if bigstring doesn't start with
343  * prefix or if prefix is longer than bigstring while still matching.
344  * NOTE: pointer returned is relative to bigstring, so we
345  * can use standard pointer comparisons in the calling function
346  * (eg: test if ap_stripprefix(a,b) == a)
347  */
348 AP_DECLARE(const char *) ap_stripprefix(const char *bigstring,
349                                         const char *prefix)
350 {
351     const char *p1;
352
353     if (*prefix == '\0')
354         return bigstring;
355
356     p1 = bigstring;
357     while (*p1 && *prefix) {
358         if (*p1++ != *prefix++)
359             return bigstring;
360     }
361     if (*prefix == '\0')
362         return p1;
363
364     /* hit the end of bigstring! */
365     return bigstring;
366 }
367
368 /* 
369  * Apache stub function for the regex libraries regexec() to make sure the
370  * whole regex(3) API is available through the Apache (exported) namespace.
371  * This is especially important for the DSO situations of modules.
372  * DO NOT MAKE A MACRO OUT OF THIS FUNCTION!
373  */
374 AP_DECLARE(int) ap_regexec(regex_t *preg, const char *string,
375                            size_t nmatch, regmatch_t pmatch[], int eflags)
376 {
377     return regexec(preg, string, nmatch, pmatch, eflags);
378 }
379
380 AP_DECLARE(size_t) ap_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
381 {
382     return regerror(errcode, preg, errbuf, errbuf_size);
383 }
384
385
386 /* This function substitutes for $0-$9, filling in regular expression
387  * submatches. Pass it the same nmatch and pmatch arguments that you
388  * passed ap_regexec(). pmatch should not be greater than the maximum number
389  * of subexpressions - i.e. one more than the re_nsub member of regex_t.
390  *
391  * input should be the string with the $-expressions, source should be the
392  * string that was matched against.
393  *
394  * It returns the substituted string, or NULL on error.
395  *
396  * Parts of this code are based on Henry Spencer's regsub(), from his
397  * AT&T V8 regexp package.
398  */
399
400 AP_DECLARE(char *) ap_pregsub(apr_pool_t *p, const char *input, const char *source,
401                            size_t nmatch, regmatch_t pmatch[])
402 {
403     const char *src = input;
404     char *dest, *dst;
405     char c;
406     size_t no;
407     int len;
408
409     if (!source)
410         return NULL;
411     if (!nmatch)
412         return apr_pstrdup(p, src);
413
414     /* First pass, find the size */
415
416     len = 0;
417
418     while ((c = *src++) != '\0') {
419         if (c == '&')
420             no = 0;
421         else if (c == '$' && apr_isdigit(*src))
422             no = *src++ - '0';
423         else
424             no = 10;
425
426         if (no > 9) {           /* Ordinary character. */
427             if (c == '\\' && (*src == '$' || *src == '&'))
428                 c = *src++;
429             len++;
430         }
431         else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
432             len += pmatch[no].rm_eo - pmatch[no].rm_so;
433         }
434
435     }
436
437     dest = dst = apr_pcalloc(p, len + 1);
438
439     /* Now actually fill in the string */
440
441     src = input;
442
443     while ((c = *src++) != '\0') {
444         if (c == '&')
445             no = 0;
446         else if (c == '$' && apr_isdigit(*src))
447             no = *src++ - '0';
448         else
449             no = 10;
450
451         if (no > 9) {           /* Ordinary character. */
452             if (c == '\\' && (*src == '$' || *src == '&'))
453                 c = *src++;
454             *dst++ = c;
455         }
456         else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
457             len = pmatch[no].rm_eo - pmatch[no].rm_so;
458             memcpy(dst, source + pmatch[no].rm_so, len);
459             dst += len;
460         }
461
462     }
463     *dst = '\0';
464
465     return dest;
466 }
467
468 /*
469  * Parse .. so we don't compromise security
470  */
471 AP_DECLARE(void) ap_getparents(char *name)
472 {
473     int l, w;
474
475     /* Four paseses, as per RFC 1808 */
476     /* a) remove ./ path segments */
477
478     for (l = 0, w = 0; name[l] != '\0';) {
479         if (name[l] == '.' && name[l + 1] == '/' && (l == 0 || name[l - 1] == '/'))
480             l += 2;
481         else
482             name[w++] = name[l++];
483     }
484
485     /* b) remove trailing . path, segment */
486     if (w == 1 && name[0] == '.')
487         w--;
488     else if (w > 1 && name[w - 1] == '.' && name[w - 2] == '/')
489         w--;
490     name[w] = '\0';
491
492     /* c) remove all xx/../ segments. (including leading ../ and /../) */
493     l = 0;
494
495     while (name[l] != '\0') {
496         if (name[l] == '.' && name[l + 1] == '.' && name[l + 2] == '/' &&
497             (l == 0 || name[l - 1] == '/')) {
498             register int m = l + 3, n;
499
500             l = l - 2;
501             if (l >= 0) {
502                 while (l >= 0 && name[l] != '/')
503                     l--;
504                 l++;
505             }
506             else
507                 l = 0;
508             n = l;
509             while ((name[n] = name[m]))
510                 (++n, ++m);
511         }
512         else
513             ++l;
514     }
515
516     /* d) remove trailing xx/.. segment. */
517     if (l == 2 && name[0] == '.' && name[1] == '.')
518         name[0] = '\0';
519     else if (l > 2 && name[l - 1] == '.' && name[l - 2] == '.' && name[l - 3] == '/') {
520         l = l - 4;
521         if (l >= 0) {
522             while (l >= 0 && name[l] != '/')
523                 l--;
524             l++;
525         }
526         else
527             l = 0;
528         name[l] = '\0';
529     }
530 }
531
532 AP_DECLARE(void) ap_no2slash(char *name)
533 {
534     char *d, *s;
535
536     s = d = name;
537
538 #ifdef HAVE_UNC_PATHS
539     /* Check for UNC names.  Leave leading two slashes. */
540     if (s[0] == '/' && s[1] == '/')
541         *d++ = *s++;
542 #endif
543
544     while (*s) {
545         if ((*d++ = *s) == '/') {
546             do {
547                 ++s;
548             } while (*s == '/');
549         }
550         else {
551             ++s;
552         }
553     }
554     *d = '\0';
555 }
556
557
558 /*
559  * copy at most n leading directories of s into d
560  * d should be at least as large as s plus 1 extra byte
561  * assumes n > 0
562  * the return value is the ever useful pointer to the trailing \0 of d
563  *
564  * MODIFIED FOR HAVE_DRIVE_LETTERS and NETWARE environments, 
565  * so that if n == 0, "/" is returned in d with n == 1 
566  * and s == "e:/test.html", "e:/" is returned in d
567  * *** See also directory_walk in src/main/http_request.c
568
569  * examples:
570  *    /a/b, 0  ==> /  (true for all platforms)
571  *    /a/b, 1  ==> /
572  *    /a/b, 2  ==> /a/
573  *    /a/b, 3  ==> /a/b/
574  *    /a/b, 4  ==> /a/b/
575  *
576  *    c:/a/b 0 ==> /
577  *    c:/a/b 1 ==> c:/
578  *    c:/a/b 2 ==> c:/a/
579  *    c:/a/b 3 ==> c:/a/b
580  *    c:/a/b 4 ==> c:/a/b
581  */
582 AP_DECLARE(char *) ap_make_dirstr_prefix(char *d, const char *s, int n)
583 {
584     if (n < 1) {
585         *d = '/';
586         *++d = '\0';
587         return (d);
588     }
589
590     for (;;) {
591         if (*s == '\0' || (*s == '/' && (--n) == 0)) {
592             *d = '/';
593             break;
594         }
595         *d++ = *s++;
596     }
597     *++d = 0;
598     return (d);
599 }
600
601
602 /*
603  * return the parent directory name including trailing / of the file s
604  */
605 AP_DECLARE(char *) ap_make_dirstr_parent(apr_pool_t *p, const char *s)
606 {
607     const char *last_slash = ap_strrchr_c(s, '/');
608     char *d;
609     int l;
610
611     if (last_slash == NULL) {
612         /* XXX: well this is really broken if this happens */
613         return (apr_pstrdup(p, "/"));
614     }
615     l = (last_slash - s) + 1;
616     d = apr_palloc(p, l + 1);
617     memcpy(d, s, l);
618     d[l] = 0;
619     return (d);
620 }
621
622
623 AP_DECLARE(int) ap_count_dirs(const char *path)
624 {
625     register int x, n;
626
627     for (x = 0, n = 0; path[x]; x++)
628         if (path[x] == '/')
629             n++;
630     return n;
631 }
632
633
634 AP_DECLARE(void) ap_chdir_file(const char *file)
635 {
636     const char *x;
637     char buf[HUGE_STRING_LEN];
638
639     x = ap_strrchr_c(file, '/');
640     if (x == NULL) {
641         chdir(file);
642     }
643     else if (x - file < sizeof(buf) - 1) {
644         memcpy(buf, file, x - file);
645         buf[x - file] = '\0';
646         chdir(buf);
647     }
648     /* XXX: well, this is a silly function, no method of reporting an
649      * error... ah well. */
650 }
651
652 AP_DECLARE(char *) ap_getword_nc(apr_pool_t *atrans, char **line, char stop)
653 {
654     return ap_getword(atrans, (const char **) line, stop);
655 }
656
657 AP_DECLARE(char *) ap_getword(apr_pool_t *atrans, const char **line, char stop)
658 {
659     const char *pos = ap_strchr_c(*line, stop);
660     char *res;
661
662     if (!pos) {
663         res = apr_pstrdup(atrans, *line);
664         *line += strlen(*line);
665         return res;
666     }
667
668     res = apr_pstrndup(atrans, *line, pos - *line);
669
670     while (*pos == stop) {
671         ++pos;
672     }
673
674     *line = pos;
675
676     return res;
677 }
678
679 AP_DECLARE(char *) ap_getword_white_nc(apr_pool_t *atrans, char **line)
680 {
681     return ap_getword_white(atrans, (const char **) line);
682 }
683
684 AP_DECLARE(char *) ap_getword_white(apr_pool_t *atrans, const char **line)
685 {
686     int pos = -1, x;
687     char *res;
688
689     for (x = 0; (*line)[x]; x++) {
690         if (apr_isspace((*line)[x])) {
691             pos = x;
692             break;
693         }
694     }
695
696     if (pos == -1) {
697         res = apr_pstrdup(atrans, *line);
698         *line += strlen(*line);
699         return res;
700     }
701
702     res = apr_palloc(atrans, pos + 1);
703     apr_cpystrn(res, *line, pos + 1);
704
705     while (apr_isspace((*line)[pos]))
706         ++pos;
707
708     *line += pos;
709
710     return res;
711 }
712
713 AP_DECLARE(char *) ap_getword_nulls_nc(apr_pool_t *atrans, char **line, char stop)
714 {
715     return ap_getword_nulls(atrans, (const char **) line, stop);
716 }
717
718 AP_DECLARE(char *) ap_getword_nulls(apr_pool_t *atrans, const char **line, char stop)
719 {
720     const char *pos = ap_strchr_c(*line, stop);
721     char *res;
722
723     if (!pos) {
724         res = apr_pstrdup(atrans, *line);
725         *line += strlen(*line);
726         return res;
727     }
728
729     res = apr_pstrndup(atrans, *line, pos - *line);
730
731     ++pos;
732
733     *line = pos;
734
735     return res;
736 }
737
738 /* Get a word, (new) config-file style --- quoted strings and backslashes
739  * all honored
740  */
741
742 static char *substring_conf(apr_pool_t *p, const char *start, int len, char quote)
743 {
744     char *result = apr_palloc(p, len + 2);
745     char *resp = result;
746     int i;
747
748     for (i = 0; i < len; ++i) {
749         if (start[i] == '\\' && (start[i + 1] == '\\'
750                                  || (quote && start[i + 1] == quote)))
751             *resp++ = start[++i];
752         else
753             *resp++ = start[i];
754     }
755
756     *resp++ = '\0';
757 #if RESOLVE_ENV_PER_TOKEN
758     return ap_resolve_env(p,result);
759 #else
760     return result;
761 #endif
762 }
763
764 AP_DECLARE(char *) ap_getword_conf_nc(apr_pool_t *p, char **line)
765 {
766     return ap_getword_conf(p, (const char **) line);
767 }
768
769 AP_DECLARE(char *) ap_getword_conf(apr_pool_t *p, const char **line)
770 {
771     const char *str = *line, *strend;
772     char *res;
773     char quote;
774
775     while (*str && apr_isspace(*str))
776         ++str;
777
778     if (!*str) {
779         *line = str;
780         return "";
781     }
782
783     if ((quote = *str) == '"' || quote == '\'') {
784         strend = str + 1;
785         while (*strend && *strend != quote) {
786             if (*strend == '\\' && strend[1] && strend[1] == quote)
787                 strend += 2;
788             else
789                 ++strend;
790         }
791         res = substring_conf(p, str + 1, strend - str - 1, quote);
792
793         if (*strend == quote)
794             ++strend;
795     }
796     else {
797         strend = str;
798         while (*strend && !apr_isspace(*strend))
799             ++strend;
800
801         res = substring_conf(p, str, strend - str, 0);
802     }
803
804     while (*strend && apr_isspace(*strend))
805         ++strend;
806     *line = strend;
807     return res;
808 }
809
810 /* Check a string for any ${ENV} environment variable
811  * construct and replace each them by the value of
812  * that environment variable, if it exists. If the
813  * environment value does not exist, leave the ${ENV}
814  * construct alone; it means something else.
815  */
816 AP_DECLARE(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
817 {
818        char tmp[ MAX_STRING_LEN ];
819        const char *s, *e;
820        tmp[0] = '\0';
821
822        if (!(s=ap_strchr_c(word,'$')))
823                return word;
824
825        do {
826                /* XXX - relies on strncat() to add '\0'
827                 */
828                strncat(tmp,word,s - word);
829                if ((s[1] == '{') && (e=ap_strchr_c(s,'}'))) {
830                        const char *e2 = e;
831                        word = e + 1;
832                        e = getenv(s+2);
833                        if (e) {
834                            strcat(tmp,e);
835                        } else {
836                            strncat(tmp, s, e2-s);
837                            strcat(tmp,"}");
838                        }
839                } else {
840                        /* ignore invalid strings */
841                        word = s+1;
842                        strcat(tmp,"$");
843                };
844        } while ((s=ap_strchr_c(word,'$')));
845        strcat(tmp,word);
846
847        return apr_pstrdup(p,tmp);
848 }
849 AP_DECLARE(int) ap_cfg_closefile(configfile_t *cfp)
850 {
851 #ifdef DEBUG
852     ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL, 
853         "Done with config file %s", cfp->name);
854 #endif
855     return (cfp->close == NULL) ? 0 : cfp->close(cfp->param);
856 }
857
858 static apr_status_t cfg_close(void *param)
859 {
860     apr_file_t *cfp = (apr_file_t *) param;
861     return (apr_close(cfp));
862 }
863
864 static int cfg_getch(void *param)
865 {
866     char ch;
867     apr_file_t *cfp = (apr_file_t *) param;
868     if (apr_getc(&ch, cfp) == APR_SUCCESS)
869         return ch;
870     return (int)EOF;
871 }
872
873 static void *cfg_getstr(void *buf, size_t bufsiz, void *param)
874 {
875     apr_file_t *cfp = (apr_file_t *) param;
876     apr_status_t rv;
877     rv = apr_fgets(buf, bufsiz, cfp);
878     if (rv == APR_SUCCESS || (rv == APR_EOF && strcmp(buf, "")))
879         return buf;
880     return NULL;
881 }
882
883 /* Open a configfile_t as FILE, return open configfile_t struct pointer */
884 AP_DECLARE(apr_status_t) ap_pcfg_openfile(configfile_t **ret_cfg, apr_pool_t *p, const char *name)
885 {
886     configfile_t *new_cfg;
887     apr_file_t *file = NULL;
888     apr_finfo_t finfo;
889     apr_status_t status;
890 #ifdef DEBUG
891     char buf[120];
892 #endif
893
894     if (name == NULL) {
895         ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
896                "Internal error: pcfg_openfile() called with NULL filename");
897         return APR_EBADF;
898     }
899
900     /* ### We no longer need the test ap_os_is_filename_valid() here
901      * The directory was already walked on a segment by segment basis,
902      * so we should never be called with a bad path element, and device 
903      * names as access file names never posed as security threats, since 
904      * it was the admin's choice to assign the .htaccess file's name.
905      */
906
907     status = apr_open(&file, name, APR_READ | APR_BUFFERED, APR_OS_DEFAULT, p);
908 #ifdef DEBUG
909     ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, NULL,
910                 "Opening config file %s (%s)",
911                 name, (status != APR_SUCCESS) ? 
912                 apr_strerror(status, buf, sizeof(buf)) : "successful");
913 #endif
914     if (status != APR_SUCCESS)
915         return status;
916
917     status = apr_getfileinfo(&finfo, file);
918     if (status != APR_SUCCESS)
919         return status;
920
921     if (finfo.filetype != APR_REG &&
922 #if defined(WIN32) || defined(OS2)
923         !(strcasecmp(name, "nul") == 0 ||
924           (strlen(name) >= 4 &&
925            strcasecmp(name + strlen(name) - 4, "/nul") == 0))) {
926 #else
927         strcmp(name, "/dev/null") != 0) {
928 #endif /* WIN32 || OS2 */
929         ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, NULL,
930                     "Access to file %s denied by server: not a regular file",
931                     name);
932         apr_close(file);
933         return APR_EBADF;
934     }
935
936     new_cfg = apr_palloc(p, sizeof(*new_cfg));
937     new_cfg->param = file;
938     new_cfg->name = apr_pstrdup(p, name);
939     new_cfg->getch = (int (*)(void *)) cfg_getch;
940     new_cfg->getstr = (void *(*)(void *, size_t, void *)) cfg_getstr;
941     new_cfg->close = (int (*)(void *)) cfg_close;
942     new_cfg->line_number = 0;
943     *ret_cfg = new_cfg;
944     return APR_SUCCESS;
945 }
946
947
948 /* Allocate a configfile_t handle with user defined functions and params */
949 AP_DECLARE(configfile_t *) ap_pcfg_open_custom(apr_pool_t *p, const char *descr,
950     void *param,
951     int(*getch)(void *param),
952     void *(*getstr) (void *buf, size_t bufsiz, void *param),
953     int(*close_func)(void *param))
954 {
955     configfile_t *new_cfg = apr_palloc(p, sizeof(*new_cfg));
956 #ifdef DEBUG
957     ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, NULL, "Opening config handler %s", descr);
958 #endif
959     new_cfg->param = param;
960     new_cfg->name = descr;
961     new_cfg->getch = getch;
962     new_cfg->getstr = getstr;
963     new_cfg->close = close_func;
964     new_cfg->line_number = 0;
965     return new_cfg;
966 }
967
968
969 /* Read one character from a configfile_t */
970 AP_DECLARE(int) ap_cfg_getc(configfile_t *cfp)
971 {
972     register int ch = cfp->getch(cfp->param);
973     if (ch == LF) 
974         ++cfp->line_number;
975     return ch;
976 }
977
978
979 /* Read one line from open configfile_t, strip LF, increase line number */
980 /* If custom handler does not define a getstr() function, read char by char */
981 AP_DECLARE(int) ap_cfg_getline(char *buf, size_t bufsize, configfile_t *cfp)
982 {
983     /* If a "get string" function is defined, use it */
984     if (cfp->getstr != NULL) {
985         char *src, *dst;
986         char *cp;
987         char *cbuf = buf;
988         size_t cbufsize = bufsize;
989
990         while (1) {
991             ++cfp->line_number;
992             if (cfp->getstr(cbuf, cbufsize, cfp->param) == NULL)
993                 return 1;
994
995             /*
996              *  check for line continuation,
997              *  i.e. match [^\\]\\[\r]\n only
998              */
999             cp = cbuf;
1000             while (cp < cbuf+cbufsize && *cp != '\0')
1001                 cp++;
1002             if (cp > cbuf && cp[-1] == LF) {
1003                 cp--;
1004                 if (cp > cbuf && cp[-1] == CR)
1005                     cp--;
1006                 if (cp > cbuf && cp[-1] == '\\') {
1007                     cp--;
1008                     if (!(cp > cbuf && cp[-1] == '\\')) {
1009                         /*
1010                          * line continuation requested -
1011                          * then remove backslash and continue
1012                          */
1013                         cbufsize -= (cp-cbuf);
1014                         cbuf = cp;
1015                         continue;
1016                     }
1017                     else {
1018                         /* 
1019                          * no real continuation because escaped -
1020                          * then just remove escape character
1021                          */
1022                         for ( ; cp < cbuf+cbufsize && *cp != '\0'; cp++)
1023                             cp[0] = cp[1];
1024                     }   
1025                 }
1026             }
1027             break;
1028         }
1029
1030         /*
1031          * Leading and trailing white space is eliminated completely
1032          */
1033         src = buf;
1034         while (apr_isspace(*src))
1035             ++src;
1036         /* blast trailing whitespace */
1037         dst = &src[strlen(src)];
1038         while (--dst >= src && apr_isspace(*dst))
1039             *dst = '\0';
1040         /* Zap leading whitespace by shifting */
1041         if (src != buf)
1042             for (dst = buf; (*dst++ = *src++) != '\0'; )
1043                 ;
1044
1045 #ifdef DEBUG_CFG_LINES
1046         ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL, "Read config: %s", buf);
1047 #endif
1048         return 0;
1049     } else {
1050         /* No "get string" function defined; read character by character */
1051         register int c;
1052         register size_t i = 0;
1053
1054         buf[0] = '\0';
1055         /* skip leading whitespace */
1056         do {
1057             c = cfp->getch(cfp->param);
1058         } while (c == '\t' || c == ' ');
1059
1060         if (c == EOF)
1061             return 1;
1062         
1063         if(bufsize < 2) {
1064             /* too small, assume caller is crazy */
1065             return 1;
1066         }
1067
1068         while (1) {
1069             if ((c == '\t') || (c == ' ')) {
1070                 buf[i++] = ' ';
1071                 while ((c == '\t') || (c == ' '))
1072                     c = cfp->getch(cfp->param);
1073             }
1074             if (c == CR) {
1075                 /* silently ignore CR (_assume_ that a LF follows) */
1076                 c = cfp->getch(cfp->param);
1077             }
1078             if (c == LF) {
1079                 /* increase line number and return on LF */
1080                 ++cfp->line_number;
1081             }
1082             if (c == EOF || c == 0x4 || c == LF || i >= (bufsize - 2)) {
1083                 /* 
1084                  *  check for line continuation
1085                  */
1086                 if (i > 0 && buf[i-1] == '\\') {
1087                     i--;
1088                     if (!(i > 0 && buf[i-1] == '\\')) {
1089                         /* line is continued */
1090                         c = cfp->getch(cfp->param);
1091                         continue;
1092                     }
1093                     /* else nothing needs be done because
1094                      * then the backslash is escaped and
1095                      * we just strip to a single one
1096                      */
1097                 }
1098                 /* blast trailing whitespace */
1099                 while (i > 0 && apr_isspace(buf[i - 1]))
1100                     --i;
1101                 buf[i] = '\0';
1102 #ifdef DEBUG_CFG_LINES
1103                 ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, NULL, "Read config: %s", buf);
1104 #endif
1105                 return 0;
1106             }
1107             buf[i] = c;
1108             ++i;
1109             c = cfp->getch(cfp->param);
1110         }
1111     }
1112 }
1113
1114 /* Size an HTTP header field list item, as separated by a comma.
1115  * The return value is a pointer to the beginning of the non-empty list item
1116  * within the original string (or NULL if there is none) and the address
1117  * of field is shifted to the next non-comma, non-whitespace character.
1118  * len is the length of the item excluding any beginning whitespace.
1119  */
1120 AP_DECLARE(const char *) ap_size_list_item(const char **field, int *len)
1121 {
1122     const unsigned char *ptr = (const unsigned char *)*field;
1123     const unsigned char *token;
1124     int in_qpair, in_qstr, in_com;
1125
1126     /* Find first non-comma, non-whitespace byte */
1127
1128     while (*ptr == ',' || apr_isspace(*ptr))
1129         ++ptr;
1130
1131     token = ptr;
1132
1133     /* Find the end of this item, skipping over dead bits */
1134
1135     for (in_qpair = in_qstr = in_com = 0;
1136          *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1137          ++ptr) {
1138
1139         if (in_qpair) {
1140             in_qpair = 0;
1141         }
1142         else {
1143             switch (*ptr) {
1144                 case '\\': in_qpair = 1;      /* quoted-pair         */
1145                            break;
1146                 case '"' : if (!in_com)       /* quoted string delim */
1147                                in_qstr = !in_qstr;
1148                            break;
1149                 case '(' : if (!in_qstr)      /* comment (may nest)  */
1150                                ++in_com;
1151                            break;
1152                 case ')' : if (in_com)        /* end comment         */
1153                                --in_com;
1154                            break;
1155                 default  : break;
1156             }
1157         }
1158     }
1159
1160     if ((*len = (ptr - token)) == 0) {
1161         *field = (const char *)ptr;
1162         return NULL;
1163     }
1164
1165     /* Advance field pointer to the next non-comma, non-white byte */
1166
1167     while (*ptr == ',' || apr_isspace(*ptr))
1168         ++ptr;
1169
1170     *field = (const char *)ptr;
1171     return (const char *)token;
1172 }
1173
1174 /* Retrieve an HTTP header field list item, as separated by a comma,
1175  * while stripping insignificant whitespace and lowercasing anything not in
1176  * a quoted string or comment.  The return value is a new string containing
1177  * the converted list item (or NULL if none) and the address pointed to by
1178  * field is shifted to the next non-comma, non-whitespace.
1179  */
1180 AP_DECLARE(char *) ap_get_list_item(apr_pool_t *p, const char **field)
1181 {
1182     const char *tok_start;
1183     const unsigned char *ptr;
1184     unsigned char *pos;
1185     char *token;
1186     int addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0, tok_len = 0;
1187
1188     /* Find the beginning and maximum length of the list item so that
1189      * we can allocate a buffer for the new string and reset the field.
1190      */
1191     if ((tok_start = ap_size_list_item(field, &tok_len)) == NULL) {
1192         return NULL;
1193     }
1194     token = apr_palloc(p, tok_len + 1);
1195
1196     /* Scan the token again, but this time copy only the good bytes.
1197      * We skip extra whitespace and any whitespace around a '=', '/',
1198      * or ';' and lowercase normal characters not within a comment,
1199      * quoted-string or quoted-pair.
1200      */
1201     for (ptr = (const unsigned char *)tok_start, pos = (unsigned char *)token;
1202          *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1203          ++ptr) {
1204
1205         if (in_qpair) {
1206             in_qpair = 0;
1207             *pos++ = *ptr;
1208         }
1209         else {
1210             switch (*ptr) {
1211                 case '\\': in_qpair = 1;
1212                            if (addspace == 1)
1213                                *pos++ = ' ';
1214                            *pos++ = *ptr;
1215                            addspace = 0;
1216                            break;
1217                 case '"' : if (!in_com)
1218                                in_qstr = !in_qstr;
1219                            if (addspace == 1)
1220                                *pos++ = ' ';
1221                            *pos++ = *ptr;
1222                            addspace = 0;
1223                            break;
1224                 case '(' : if (!in_qstr)
1225                                ++in_com;
1226                            if (addspace == 1)
1227                                *pos++ = ' ';
1228                            *pos++ = *ptr;
1229                            addspace = 0;
1230                            break;
1231                 case ')' : if (in_com)
1232                                --in_com;
1233                            *pos++ = *ptr;
1234                            addspace = 0;
1235                            break;
1236                 case ' ' :
1237                 case '\t': if (addspace)
1238                                break;
1239                            if (in_com || in_qstr)
1240                                *pos++ = *ptr;
1241                            else
1242                                addspace = 1;
1243                            break;
1244                 case '=' :
1245                 case '/' :
1246                 case ';' : if (!(in_com || in_qstr))
1247                                addspace = -1;
1248                            *pos++ = *ptr;
1249                            break;
1250                 default  : if (addspace == 1)
1251                                *pos++ = ' ';
1252                            *pos++ = (in_com || in_qstr) ? *ptr
1253                                                         : apr_tolower(*ptr);
1254                            addspace = 0;
1255                            break;
1256             }
1257         }
1258     }
1259     *pos = '\0';
1260
1261     return token;
1262 }
1263
1264 /* Find an item in canonical form (lowercase, no extra spaces) within
1265  * an HTTP field value list.  Returns 1 if found, 0 if not found.
1266  * This would be much more efficient if we stored header fields as
1267  * an array of list items as they are received instead of a plain string.
1268  */
1269 AP_DECLARE(int) ap_find_list_item(apr_pool_t *p, const char *line, const char *tok)
1270 {
1271     const unsigned char *pos;
1272     const unsigned char *ptr = (const unsigned char *)line;
1273     int good = 0, addspace = 0, in_qpair = 0, in_qstr = 0, in_com = 0;
1274
1275     if (!line || !tok)
1276         return 0;
1277
1278     do {  /* loop for each item in line's list */
1279
1280         /* Find first non-comma, non-whitespace byte */
1281
1282         while (*ptr == ',' || apr_isspace(*ptr))
1283             ++ptr;
1284
1285         if (*ptr)
1286             good = 1;  /* until proven otherwise for this item */
1287         else
1288             break;     /* no items left and nothing good found */
1289
1290         /* We skip extra whitespace and any whitespace around a '=', '/',
1291          * or ';' and lowercase normal characters not within a comment,
1292          * quoted-string or quoted-pair.
1293          */
1294         for (pos = (const unsigned char *)tok;
1295              *ptr && (in_qpair || in_qstr || in_com || *ptr != ',');
1296              ++ptr) {
1297
1298             if (in_qpair) {
1299                 in_qpair = 0;
1300                 if (good)
1301                     good = (*pos++ == *ptr);
1302             }
1303             else {
1304                 switch (*ptr) {
1305                     case '\\': in_qpair = 1;
1306                                if (addspace == 1)
1307                                    good = good && (*pos++ == ' ');
1308                                good = good && (*pos++ == *ptr);
1309                                addspace = 0;
1310                                break;
1311                     case '"' : if (!in_com)
1312                                    in_qstr = !in_qstr;
1313                                if (addspace == 1)
1314                                    good = good && (*pos++ == ' ');
1315                                good = good && (*pos++ == *ptr);
1316                                addspace = 0;
1317                                break;
1318                     case '(' : if (!in_qstr)
1319                                    ++in_com;
1320                                if (addspace == 1)
1321                                    good = good && (*pos++ == ' ');
1322                                good = good && (*pos++ == *ptr);
1323                                addspace = 0;
1324                                break;
1325                     case ')' : if (in_com)
1326                                    --in_com;
1327                                good = good && (*pos++ == *ptr);
1328                                addspace = 0;
1329                                break;
1330                     case ' ' :
1331                     case '\t': if (addspace || !good)
1332                                    break;
1333                                if (in_com || in_qstr)
1334                                    good = (*pos++ == *ptr);
1335                                else
1336                                    addspace = 1;
1337                                break;
1338                     case '=' :
1339                     case '/' :
1340                     case ';' : if (!(in_com || in_qstr))
1341                                    addspace = -1;
1342                                good = good && (*pos++ == *ptr);
1343                                break;
1344                     default  : if (!good)
1345                                    break;
1346                                if (addspace == 1)
1347                                    good = (*pos++ == ' ');
1348                                if (in_com || in_qstr)
1349                                    good = good && (*pos++ == *ptr);
1350                                else
1351                                    good = good && (*pos++ == apr_tolower(*ptr));
1352                                addspace = 0;
1353                                break;
1354                 }
1355             }
1356         }
1357         if (good && *pos)
1358             good = 0;          /* not good if only a prefix was matched */
1359
1360     } while (*ptr && !good);
1361
1362     return good;
1363 }
1364
1365
1366 /* Retrieve a token, spacing over it and returning a pointer to
1367  * the first non-white byte afterwards.  Note that these tokens
1368  * are delimited by semis and commas; and can also be delimited
1369  * by whitespace at the caller's option.
1370  */
1371
1372 AP_DECLARE(char *) ap_get_token(apr_pool_t *p, const char **accept_line, int accept_white)
1373 {
1374     const char *ptr = *accept_line;
1375     const char *tok_start;
1376     char *token;
1377     int tok_len;
1378
1379     /* Find first non-white byte */
1380
1381     while (*ptr && apr_isspace(*ptr))
1382         ++ptr;
1383
1384     tok_start = ptr;
1385
1386     /* find token end, skipping over quoted strings.
1387      * (comments are already gone).
1388      */
1389
1390     while (*ptr && (accept_white || !apr_isspace(*ptr))
1391            && *ptr != ';' && *ptr != ',') {
1392         if (*ptr++ == '"')
1393             while (*ptr)
1394                 if (*ptr++ == '"')
1395                     break;
1396     }
1397
1398     tok_len = ptr - tok_start;
1399     token = apr_pstrndup(p, tok_start, tok_len);
1400
1401     /* Advance accept_line pointer to the next non-white byte */
1402
1403     while (*ptr && apr_isspace(*ptr))
1404         ++ptr;
1405
1406     *accept_line = ptr;
1407     return token;
1408 }
1409
1410
1411 /* find http tokens, see the definition of token from RFC2068 */
1412 AP_DECLARE(int) ap_find_token(apr_pool_t *p, const char *line, const char *tok)
1413 {
1414     const unsigned char *start_token;
1415     const unsigned char *s;
1416
1417     if (!line)
1418         return 0;
1419
1420     s = (const unsigned char *)line;
1421     for (;;) {
1422         /* find start of token, skip all stop characters, note NUL
1423          * isn't a token stop, so we don't need to test for it
1424          */
1425         while (TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1426             ++s;
1427         }
1428         if (!*s) {
1429             return 0;
1430         }
1431         start_token = s;
1432         /* find end of the token */
1433         while (*s && !TEST_CHAR(*s, T_HTTP_TOKEN_STOP)) {
1434             ++s;
1435         }
1436         if (!strncasecmp((const char *)start_token, (const char *)tok, s - start_token)) {
1437             return 1;
1438         }
1439         if (!*s) {
1440             return 0;
1441         }
1442     }
1443 }
1444
1445
1446 AP_DECLARE(int) ap_find_last_token(apr_pool_t *p, const char *line, const char *tok)
1447 {
1448     int llen, tlen, lidx;
1449
1450     if (!line)
1451         return 0;
1452
1453     llen = strlen(line);
1454     tlen = strlen(tok);
1455     lidx = llen - tlen;
1456
1457     if ((lidx < 0) ||
1458         ((lidx > 0) && !(apr_isspace(line[lidx - 1]) || line[lidx - 1] == ',')))
1459         return 0;
1460
1461     return (strncasecmp(&line[lidx], tok, tlen) == 0);
1462 }
1463
1464 AP_DECLARE(char *) ap_escape_shell_cmd(apr_pool_t *p, const char *str)
1465 {
1466     char *cmd;
1467     unsigned char *d;
1468     const unsigned char *s;
1469
1470     cmd = apr_palloc(p, 2 * strlen(str) + 1);   /* Be safe */
1471     d = (unsigned char *)cmd;
1472     s = (const unsigned char *)str;
1473     for (; *s; ++s) {
1474
1475 #if defined(OS2) || defined(WIN32)
1476         /* Don't allow '&' in parameters under OS/2. */
1477         /* This can be used to send commands to the shell. */
1478         if (*s == '&') {
1479             *d++ = ' ';
1480             continue;
1481         }
1482 #endif
1483
1484         if (TEST_CHAR(*s, T_ESCAPE_SHELL_CMD)) {
1485             *d++ = '\\';
1486         }
1487         *d++ = *s;
1488     }
1489     *d = '\0';
1490
1491     return cmd;
1492 }
1493
1494 static char x2c(const char *what)
1495 {
1496     register char digit;
1497
1498 #ifndef CHARSET_EBCDIC
1499     digit = ((what[0] >= 'A') ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
1500     digit *= 16;
1501     digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
1502 #else /*CHARSET_EBCDIC*/
1503     char xstr[5];
1504     xstr[0]='0';
1505     xstr[1]='x';
1506     xstr[2]=what[0];
1507     xstr[3]=what[1];
1508     xstr[4]='\0';
1509     digit = apr_xlate_conv_byte(ap_hdrs_from_ascii, 0xFF & strtol(xstr, NULL, 16));
1510 #endif /*CHARSET_EBCDIC*/
1511     return (digit);
1512 }
1513
1514 /*
1515  * Unescapes a URL.
1516  * Returns 0 on success, non-zero on error
1517  * Failure is due to
1518  *   bad % escape       returns HTTP_BAD_REQUEST
1519  *
1520  *   decoding %00 -> \0
1521  *   decoding %2f -> /   (a special character)
1522  *                      returns HTTP_NOT_FOUND
1523  */
1524 AP_DECLARE(int) ap_unescape_url(char *url)
1525 {
1526     register int badesc, badpath;
1527     char *x, *y;
1528
1529     badesc = 0;
1530     badpath = 0;
1531     /* Initial scan for first '%'. Don't bother writing values before
1532      * seeing a '%' */
1533     y = strchr(url, '%');
1534     if (y == NULL) {
1535         return OK;
1536     }
1537     for (x = y; *y; ++x, ++y) {
1538         if (*y != '%')
1539             *x = *y;
1540         else {
1541             if (!apr_isxdigit(*(y + 1)) || !apr_isxdigit(*(y + 2))) {
1542                 badesc = 1;
1543                 *x = '%';
1544             }
1545             else {
1546                 *x = x2c(y + 1);
1547                 y += 2;
1548                 if (*x == '/' || *x == '\0')
1549                     badpath = 1;
1550             }
1551         }
1552     }
1553     *x = '\0';
1554     if (badesc)
1555         return HTTP_BAD_REQUEST;
1556     else if (badpath)
1557         return HTTP_NOT_FOUND;
1558     else
1559         return OK;
1560 }
1561
1562 AP_DECLARE(char *) ap_construct_server(apr_pool_t *p, const char *hostname,
1563                                     apr_port_t port, const request_rec *r)
1564 {
1565     if (ap_is_default_port(port, r))
1566         return apr_pstrdup(p, hostname);
1567     else {
1568         return apr_psprintf(p, "%s:%u", hostname, port);
1569     }
1570 }
1571
1572 /* c2x takes an unsigned, and expects the caller has guaranteed that
1573  * 0 <= what < 256... which usually means that you have to cast to
1574  * unsigned char first, because (unsigned)(char)(x) first goes through
1575  * signed extension to an int before the unsigned cast.
1576  *
1577  * The reason for this assumption is to assist gcc code generation --
1578  * the unsigned char -> unsigned extension is already done earlier in
1579  * both uses of this code, so there's no need to waste time doing it
1580  * again.
1581  */
1582 static const char c2x_table[] = "0123456789abcdef";
1583
1584 static apr_inline unsigned char *c2x(unsigned what, unsigned char *where)
1585 {
1586 #ifdef CHARSET_EBCDIC
1587     what = apr_xlate_conv_byte(ap_hdrs_to_ascii, (unsigned char)what);
1588 #endif /*CHARSET_EBCDIC*/
1589     *where++ = '%';
1590     *where++ = c2x_table[what >> 4];
1591     *where++ = c2x_table[what & 0xf];
1592     return where;
1593 }
1594
1595 /*
1596  * escape_path_segment() escapes a path segment, as defined in RFC 1808. This
1597  * routine is (should be) OS independent.
1598  *
1599  * os_escape_path() converts an OS path to a URL, in an OS dependent way. In all
1600  * cases if a ':' occurs before the first '/' in the URL, the URL should be
1601  * prefixed with "./" (or the ':' escaped). In the case of Unix, this means
1602  * leaving '/' alone, but otherwise doing what escape_path_segment() does. For
1603  * efficiency reasons, we don't use escape_path_segment(), which is provided for
1604  * reference. Again, RFC 1808 is where this stuff is defined.
1605  *
1606  * If partial is set, os_escape_path() assumes that the path will be appended to
1607  * something with a '/' in it (and thus does not prefix "./").
1608  */
1609
1610 AP_DECLARE(char *) ap_escape_path_segment(apr_pool_t *p, const char *segment)
1611 {
1612     char *copy = apr_palloc(p, 3 * strlen(segment) + 1);
1613     const unsigned char *s = (const unsigned char *)segment;
1614     unsigned char *d = (unsigned char *)copy;
1615     unsigned c;
1616
1617     while ((c = *s)) {
1618         if (TEST_CHAR(c, T_ESCAPE_PATH_SEGMENT)) {
1619             d = c2x(c, d);
1620         }
1621         else {
1622             *d++ = c;
1623         }
1624         ++s;
1625     }
1626     *d = '\0';
1627     return copy;
1628 }
1629
1630 AP_DECLARE(char *) ap_os_escape_path(apr_pool_t *p, const char *path, int partial)
1631 {
1632     char *copy = apr_palloc(p, 3 * strlen(path) + 3);
1633     const unsigned char *s = (const unsigned char *)path;
1634     unsigned char *d = (unsigned char *)copy;
1635     unsigned c;
1636
1637     if (!partial) {
1638         const char *colon = ap_strchr_c(path, ':');
1639         const char *slash = ap_strchr_c(path, '/');
1640
1641         if (colon && (!slash || colon < slash)) {
1642             *d++ = '.';
1643             *d++ = '/';
1644         }
1645     }
1646     while ((c = *s)) {
1647         if (TEST_CHAR(c, T_OS_ESCAPE_PATH)) {
1648             d = c2x(c, d);
1649         }
1650         else {
1651             *d++ = c;
1652         }
1653         ++s;
1654     }
1655     *d = '\0';
1656     return copy;
1657 }
1658
1659 /* ap_escape_uri is now a macro for os_escape_path */
1660
1661 AP_DECLARE(char *) ap_escape_html(apr_pool_t *p, const char *s)
1662 {
1663     int i, j;
1664     char *x;
1665
1666     /* first, count the number of extra characters */
1667     for (i = 0, j = 0; s[i] != '\0'; i++)
1668         if (s[i] == '<' || s[i] == '>')
1669             j += 3;
1670         else if (s[i] == '&')
1671             j += 4;
1672
1673     if (j == 0)
1674         return apr_pstrndup(p, s, i);
1675
1676     x = apr_palloc(p, i + j + 1);
1677     for (i = 0, j = 0; s[i] != '\0'; i++, j++)
1678         if (s[i] == '<') {
1679             memcpy(&x[j], "&lt;", 4);
1680             j += 3;
1681         }
1682         else if (s[i] == '>') {
1683             memcpy(&x[j], "&gt;", 4);
1684             j += 3;
1685         }
1686         else if (s[i] == '&') {
1687             memcpy(&x[j], "&amp;", 5);
1688             j += 4;
1689         }
1690         else
1691             x[j] = s[i];
1692
1693     x[j] = '\0';
1694     return x;
1695 }
1696
1697 AP_DECLARE(int) ap_is_directory(const char *path)
1698 {
1699     apr_finfo_t finfo;
1700
1701     if (apr_stat(&finfo, path, NULL) == -1)
1702         return 0;               /* in error condition, just return no */
1703
1704     return (finfo.filetype == APR_DIR);
1705 }
1706
1707 AP_DECLARE(int) ap_is_rdirectory(const char *path)
1708 {
1709     apr_finfo_t finfo;
1710
1711     if (apr_lstat(&finfo, path, NULL) == -1)
1712         return 0;               /* in error condition, just return no */
1713
1714     return (finfo.filetype == APR_DIR);
1715 }
1716
1717 AP_DECLARE(char *) ap_make_full_path(apr_pool_t *a, const char *src1,
1718                                   const char *src2)
1719 {
1720     register int x;
1721
1722     x = strlen(src1);
1723     if (x == 0)
1724         return apr_pstrcat(a, "/", src2, NULL);
1725
1726     if (src1[x - 1] != '/')
1727         return apr_pstrcat(a, src1, "/", src2, NULL);
1728     else
1729         return apr_pstrcat(a, src1, src2, NULL);
1730 }
1731
1732 /*
1733  * Check for an absoluteURI syntax (see section 3.2 in RFC2068).
1734  */
1735 AP_DECLARE(int) ap_is_url(const char *u)
1736 {
1737     register int x;
1738
1739     for (x = 0; u[x] != ':'; x++) {
1740         if ((!u[x]) ||
1741             ((!apr_isalpha(u[x])) && (!apr_isdigit(u[x])) &&
1742              (u[x] != '+') && (u[x] != '-') && (u[x] != '.'))) {
1743             return 0;
1744         }
1745     }
1746
1747     return (x ? 1 : 0);         /* If the first character is ':', it's broken, too */
1748 }
1749
1750 #ifndef HAVE_INITGROUPS
1751 int initgroups(const char *name, gid_t basegid)
1752 {
1753 #if defined(QNX) || defined(MPE) || defined(BEOS) || defined(_OSD_POSIX) || defined(TPF) || defined(__TANDEM) || defined(OS2) || defined(WIN32)
1754 /* QNX, MPE and BeOS do not appear to support supplementary groups. */
1755     return 0;
1756 #else /* ndef QNX */
1757     gid_t groups[NGROUPS_MAX];
1758     struct group *g;
1759     int index = 0;
1760
1761     setgrent();
1762
1763     groups[index++] = basegid;
1764
1765     while (index < NGROUPS_MAX && ((g = getgrent()) != NULL))
1766         if (g->gr_gid != basegid) {
1767             char **names;
1768
1769             for (names = g->gr_mem; *names != NULL; ++names)
1770                 if (!strcmp(*names, name))
1771                     groups[index++] = g->gr_gid;
1772         }
1773
1774     endgrent();
1775
1776     return setgroups(index, groups);
1777 #endif /* def QNX */
1778 }
1779 #endif /* def NEED_INITGROUPS */
1780
1781 AP_DECLARE(int) ap_ind(const char *s, char c)
1782 {
1783     register int x;
1784
1785     for (x = 0; s[x]; x++)
1786         if (s[x] == c)
1787             return x;
1788
1789     return -1;
1790 }
1791
1792 AP_DECLARE(int) ap_rind(const char *s, char c)
1793 {
1794     register int x;
1795
1796     for (x = strlen(s) - 1; x != -1; x--)
1797         if (s[x] == c)
1798             return x;
1799
1800     return -1;
1801 }
1802
1803 AP_DECLARE(void) ap_str_tolower(char *str)
1804 {
1805     while (*str) {
1806         *str = apr_tolower(*str);
1807         ++str;
1808     }
1809 }
1810
1811 AP_DECLARE(uid_t) ap_uname2id(const char *name)
1812 {
1813 #ifdef WIN32
1814     return (1);
1815 #else
1816     struct passwd *ent;
1817
1818     if (name[0] == '#')
1819         return (atoi(&name[1]));
1820
1821     if (!(ent = getpwnam(name))) {
1822         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad user name %s", ap_server_argv0, name);
1823         exit(1);
1824     }
1825     return (ent->pw_uid);
1826 #endif
1827 }
1828
1829 AP_DECLARE(gid_t) ap_gname2id(const char *name)
1830 {
1831 #ifdef WIN32
1832     return (1);
1833 #else
1834     struct group *ent;
1835
1836     if (name[0] == '#')
1837         return (atoi(&name[1]));
1838
1839     if (!(ent = getgrnam(name))) {
1840         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "%s: bad group name %s", ap_server_argv0, name);
1841         exit(1);
1842     }
1843     return (ent->gr_gid);
1844 #endif
1845 }
1846
1847
1848 /*
1849  * Parses a host of the form <address>[:port]
1850  * :port is permitted if 'port' is not NULL
1851  */
1852 unsigned long ap_get_virthost_addr(char *w, apr_port_t *ports)
1853 {
1854     struct hostent *hep;
1855     unsigned long my_addr;
1856     char *p;
1857
1858     p = strchr(w, ':');
1859     if (ports != NULL) {
1860         *ports = 0;
1861         if (p != NULL && strcmp(p + 1, "*") != 0)
1862             *ports = atoi(p + 1);
1863     }
1864
1865     if (p != NULL)
1866         *p = '\0';
1867     if (strcmp(w, "*") == 0) {
1868         if (p != NULL)
1869             *p = ':';
1870         return htonl(INADDR_ANY);
1871     }
1872
1873     my_addr = apr_inet_addr((char *)w);
1874     if (my_addr != APR_INADDR_NONE) {
1875         if (p != NULL)
1876             *p = ':';
1877         return my_addr;
1878     }
1879
1880     hep = gethostbyname(w);
1881
1882     if ((!hep) || (hep->h_addrtype != APR_INET || !hep->h_addr_list[0])) {
1883         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Cannot resolve host name %s --- exiting!", w);
1884         exit(1);
1885     }
1886
1887     if (hep->h_addr_list[1]) {
1888         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Host %s has multiple addresses ---", w);
1889         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "you must choose one explicitly for use as");
1890         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "a virtual host.  Exiting!!!");
1891         exit(1);
1892     }
1893
1894     if (p != NULL)
1895         *p = ':';
1896
1897     return ((struct in_addr *) (hep->h_addr))->s_addr;
1898 }
1899
1900
1901 static char *find_fqdn(apr_pool_t *a, struct hostent *p)
1902 {
1903     int x;
1904
1905     if (!strchr(p->h_name, '.')) {
1906         for (x = 0; p->h_aliases[x]; ++x) {
1907             if (strchr(p->h_aliases[x], '.') &&
1908                 (!strncasecmp(p->h_aliases[x], p->h_name, strlen(p->h_name))))
1909                 return apr_pstrdup(a, p->h_aliases[x]);
1910         }
1911         return NULL;
1912     }
1913     return apr_pstrdup(a, (void *) p->h_name);
1914 }
1915
1916 char *ap_get_local_host(apr_pool_t *a)
1917 {
1918 #ifndef MAXHOSTNAMELEN
1919 #define MAXHOSTNAMELEN 256
1920 #endif
1921     char str[MAXHOSTNAMELEN + 1];
1922     char *server_hostname = NULL;
1923     struct hostent *p;
1924
1925 #ifdef BEOS
1926     if (gethostname(str, sizeof(str) - 1) == 0)
1927 #else
1928     if (gethostname(str, sizeof(str) - 1) != 0)
1929 #endif
1930     {
1931         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_WARNING, 0, NULL,
1932                      "%s: gethostname() failed to determine ServerName",
1933                      ap_server_argv0);
1934     }
1935     else 
1936     {
1937         str[sizeof(str) - 1] = '\0';
1938         if ((!(p = gethostbyname(str))) 
1939             || (!(server_hostname = find_fqdn(a, p)))) {
1940             /* Recovery - return the default servername by IP: */
1941             if (!str && p->h_addr_list[0]) {
1942                 apr_snprintf(str, sizeof(str), "%pA", p->h_addr_list[0]);
1943                 server_hostname = apr_pstrdup(a, str);
1944                 /* We will drop through to report the IP-named server */
1945             }
1946         }
1947         else {
1948             /* Since we found a fdqn, return it with no logged message. */
1949             return server_hostname;
1950         }
1951     }
1952
1953     if (!server_hostname) 
1954         server_hostname = apr_pstrdup(a, "127.0.0.1");
1955
1956     ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO|APLOG_STARTUP, 0, NULL,
1957                  "%s: Could not determine the server's fully qualified "
1958                  "domain name, using %s for ServerName",
1959                  ap_server_argv0, server_hostname);
1960              
1961     return server_hostname;
1962 }
1963
1964 /* simple 'pool' alloc()ing glue to ap_base64.c
1965  */
1966 AP_DECLARE(char *) ap_pbase64decode(apr_pool_t *p, const char *bufcoded)
1967 {
1968     char *decoded;
1969     int l;
1970
1971     decoded = (char *) apr_palloc(p, 1 + ap_base64decode_len(bufcoded));
1972     l = ap_base64decode(decoded, bufcoded);
1973     decoded[l] = '\0'; /* make binary sequence into string */
1974
1975     return decoded;
1976 }
1977
1978 AP_DECLARE(char *) ap_pbase64encode(apr_pool_t *p, char *string) 
1979
1980     char *encoded;
1981     int l = strlen(string);
1982
1983     encoded = (char *) apr_palloc(p, 1 + ap_base64encode_len(l));
1984     l = ap_base64encode(encoded, string, l);
1985     encoded[l] = '\0'; /* make binary sequence into string */
1986
1987     return encoded;
1988 }
1989
1990 /* we want to downcase the type/subtype for comparison purposes
1991  * but nothing else because ;parameter=foo values are case sensitive.
1992  * XXX: in truth we want to downcase parameter names... but really,
1993  * apache has never handled parameters and such correctly.  You
1994  * also need to compress spaces and such to be able to compare
1995  * properly. -djg
1996  */
1997 AP_DECLARE(void) ap_content_type_tolower(char *str)
1998 {
1999     char *semi;
2000
2001     semi = strchr(str, ';');
2002     if (semi) {
2003         *semi = '\0';
2004     }
2005     while (*str) {
2006         *str = apr_tolower(*str);
2007         ++str;
2008     }
2009     if (semi) {
2010         *semi = ';';
2011     }
2012 }
2013
2014 /*
2015  * Given a string, replace any bare " with \" .
2016  */
2017 AP_DECLARE(char *) ap_escape_quotes (apr_pool_t *p, const char *instring)
2018 {
2019     int newlen = 0;
2020     const char *inchr = instring;
2021     char *outchr, *outstring;
2022
2023     /*
2024      * Look through the input string, jogging the length of the output
2025      * string up by an extra byte each time we find an unescaped ".
2026      */
2027     while (*inchr != '\0') {
2028         newlen++;
2029         if (*inchr == '"') {
2030             newlen++;
2031         }
2032         /*
2033          * If we find a slosh, and it's not the last byte in the string,
2034          * it's escaping something - advance past both bytes.
2035          */
2036         if ((*inchr == '\\') && (inchr[1] != '\0')) {
2037             inchr++;
2038             newlen++;
2039         }
2040         inchr++;
2041     }
2042     outstring = apr_palloc(p, newlen + 1);
2043     inchr = instring;
2044     outchr = outstring;
2045     /*
2046      * Now copy the input string to the output string, inserting a slosh
2047      * in front of every " that doesn't already have one.
2048      */
2049     while (*inchr != '\0') {
2050         if ((*inchr == '\\') && (inchr[1] != '\0')) {
2051             *outchr++ = *inchr++;
2052             *outchr++ = *inchr++;
2053         }
2054         if (*inchr == '"') {
2055             *outchr++ = '\\';
2056         }
2057         if (*inchr != '\0') {
2058             *outchr++ = *inchr++;
2059         }
2060     }
2061     *outchr = '\0';
2062     return outstring;
2063 }