]> granicus.if.org Git - apache/blob - modules/mappers/mod_rewrite.c
Restore functionality broken by the mod_rewrite security fix:
[apache] / modules / mappers / mod_rewrite.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 **   _ __ ___   ___   __| |    _ __ _____      ___ __(_) |_ ___
61 **  | '_ ` _ \ / _ \ / _` |   | '__/ _ \ \ /\ / / '__| | __/ _ \
62 **  | | | | | | (_) | (_| |   | | |  __/\ V  V /| |  | | ||  __/
63 **  |_| |_| |_|\___/ \__,_|___|_|  \___| \_/\_/ |_|  |_|\__\___|
64 **                       |_____|
65 **
66 **  URL Rewriting Module
67 **
68 **  This module uses a rule-based rewriting engine (based on a
69 **  regular-expression parser) to rewrite requested URLs on the fly.
70 **
71 **  It supports an unlimited number of additional rule conditions (which can
72 **  operate on a lot of variables, even on HTTP headers) for granular
73 **  matching and even external database lookups (either via plain text
74 **  tables, DBM hash files or even external processes) for advanced URL
75 **  substitution.
76 **
77 **  It operates on the full URLs (including the PATH_INFO part) both in
78 **  per-server context (httpd.conf) and per-dir context (.htaccess) and even
79 **  can generate QUERY_STRING parts on result.   The rewriting result finally
80 **  can lead to internal subprocessing, external request redirection or even
81 **  to internal proxy throughput.
82 **
83 **  This module was originally written in April 1996 and
84 **  gifted exclusively to the The Apache Software Foundation in July 1997 by
85 **
86 **      Ralf S. Engelschall
87 **      rse@engelschall.com
88 **      www.engelschall.com
89 */
90
91 #include "ap_config.h"
92 #include "httpd.h"
93 #include "http_config.h"
94 #include "http_request.h"
95 #include "http_core.h"
96 #include "http_log.h"
97 #include "http_protocol.h"
98 #include "mod_rewrite.h"
99 #include "apr_strings.h"
100
101 #if !defined(OS2) && !defined(WIN32)
102 #include "unixd.h"
103 #endif
104
105 #ifndef NO_WRITEV
106 #ifndef NETWARE
107 #ifdef HAVE_SYS_TYPES_H
108 #include <sys/types.h>
109 #endif
110 #endif
111 #ifdef HAVE_SYS_UIO_H
112 #include <sys/uio.h>
113 #endif
114 #endif
115 #ifdef HAVE_PWD_H
116 #include <pwd.h>
117 #endif
118 #ifdef HAVE_GRP_H
119 #include <grp.h>
120 #endif
121 #ifdef HAVE_UNISTD_H
122 #include <unistd.h>
123 #endif
124
125 /*
126 ** +-------------------------------------------------------+
127 ** |                                                       |
128 ** |             static module configuration
129 ** |                                                       |
130 ** +-------------------------------------------------------+
131 */
132
133
134 /*
135 **  Our interface to the Apache server kernel:
136 **
137 **  o  Runtime logic of a request is as following:
138 **       while(request or subrequest)
139 **           foreach(stage #0...#9)
140 **               foreach(module) (**)
141 **                   try to run hook
142 **
143 **  o  the order of modules at (**) is the inverted order as
144 **     given in the "Configuration" file, i.e. the last module
145 **     specified is the first one called for each hook!
146 **     The core module is always the last!
147 **
148 **  o  there are two different types of result checking and
149 **     continue processing:
150 **     for hook #0,#1,#4,#5,#6,#8:
151 **         hook run loop stops on first modules which gives
152 **         back a result != DECLINED, i.e. it usually returns OK
153 **         which says "OK, module has handled this _stage_" and for #1
154 **         this have not to mean "Ok, the filename is now valid".
155 **     for hook #2,#3,#7,#9:
156 **         all hooks are run, independend of result
157 **
158 **  o  at the last stage, the core module always
159 **       - says "HTTP_BAD_REQUEST" if r->filename does not begin with "/"
160 **       - prefix URL with document_root or replaced server_root
161 **         with document_root and sets r->filename
162 **       - always return a "OK" independed if the file really exists
163 **         or not!
164 */
165
166     /* The section for the Configure script:
167     * XXX: this needs updating for apache-2.0 configuration method
168      * MODULE-DEFINITION-START
169      * Name: rewrite_module
170      * ConfigStart
171     . ./helpers/find-dbm-lib
172     if [ "x$found_dbm" = "x1" ]; then
173         echo "      enabling DBM support for mod_rewrite"
174     else
175         echo "      disabling DBM support for mod_rewrite"
176         echo "      (perhaps you need to add -ldbm, -lndbm or -lgdbm to EXTRA_LIBS)"
177         CFLAGS="$CFLAGS -DNO_DBM_REWRITEMAP"
178     fi
179      * ConfigEnd
180      * MODULE-DEFINITION-END
181      */
182
183     /* the apr_table_t of commands we provide */
184 static const command_rec command_table[] = {
185     AP_INIT_FLAG(    "RewriteEngine",   cmd_rewriteengine,  NULL, OR_FILEINFO,
186                      "On or Off to enable or disable (default) the whole "
187                      "rewriting engine"),
188     AP_INIT_ITERATE( "RewriteOptions",  cmd_rewriteoptions,  NULL, OR_FILEINFO,
189                      "List of option strings to set"),
190     AP_INIT_TAKE1(   "RewriteBase",     cmd_rewritebase,     NULL, OR_FILEINFO, 
191                      "the base URL of the per-directory context"),
192     AP_INIT_RAW_ARGS("RewriteCond",     cmd_rewritecond,     NULL, OR_FILEINFO,
193                      "an input string and a to be applied regexp-pattern"),
194     AP_INIT_RAW_ARGS("RewriteRule",     cmd_rewriterule,     NULL, OR_FILEINFO,
195                      "an URL-applied regexp-pattern and a substitution URL"),
196     AP_INIT_TAKE2(   "RewriteMap",      cmd_rewritemap,      NULL, RSRC_CONF,
197                      "a mapname and a filename"),
198     AP_INIT_TAKE1(   "RewriteLock",     cmd_rewritelock,     NULL, RSRC_CONF,
199                      "the filename of a lockfile used for inter-process "
200                      "synchronization"),
201     AP_INIT_TAKE1(   "RewriteLog",      cmd_rewritelog,      NULL, RSRC_CONF,
202                      "the filename of the rewriting logfile"),
203     AP_INIT_TAKE1(   "RewriteLogLevel", cmd_rewriteloglevel, NULL, RSRC_CONF,
204                      "the level of the rewriting logfile verbosity "
205                      "(0=none, 1=std, .., 9=max)"),
206     { NULL }
207 };
208
209     /* the apr_table_t of content handlers we provide */
210 static const handler_rec handler_table[] = {
211     { "redirect-handler", handler_redirect },
212     { NULL }
213 };
214
215 static void register_hooks(void)
216 {
217     ap_hook_post_config(init_module,NULL,NULL,AP_HOOK_MIDDLE);
218     ap_hook_child_init(init_child,NULL,NULL,AP_HOOK_MIDDLE);
219
220     ap_hook_fixups(hook_fixup,NULL,NULL,AP_HOOK_FIRST);
221     ap_hook_translate_name(hook_uri2file,NULL,NULL,AP_HOOK_FIRST);
222     ap_hook_type_checker(hook_mimetype,NULL,NULL,AP_HOOK_MIDDLE);
223 }
224
225     /* the main config structure */
226 module AP_MODULE_DECLARE_DATA rewrite_module = {
227    STANDARD20_MODULE_STUFF,
228    config_perdir_create,        /* create per-dir    config structures */
229    config_perdir_merge,         /* merge  per-dir    config structures */
230    config_server_create,        /* create per-server config structures */
231    config_server_merge,         /* merge  per-server config structures */
232    command_table,               /* apr_table_t of config file commands  */
233    handler_table,               /* [#8] MIME-typed-dispatched handlers */
234    register_hooks               /* register hooks                      */
235 };
236
237     /* the cache */
238 static cache *cachep;
239
240     /* whether proxy module is available or not */
241 static int proxy_available;
242 static int once_through = 0;
243
244 static const char *lockname;
245 static apr_lock_t *rewrite_mapr_lock = NULL;
246 static apr_lock_t *rewrite_log_lock = NULL;
247
248 /*
249 ** +-------------------------------------------------------+
250 ** |                                                       |
251 ** |           configuration directive handling
252 ** |                                                       |
253 ** +-------------------------------------------------------+
254 */
255
256 /*
257 **
258 **  per-server configuration structure handling
259 **
260 */
261
262 static void *config_server_create(apr_pool_t *p, server_rec *s)
263 {
264     rewrite_server_conf *a;
265
266     a = (rewrite_server_conf *)apr_pcalloc(p, sizeof(rewrite_server_conf));
267
268     a->state           = ENGINE_DISABLED;
269     a->options         = OPTION_NONE;
270     a->rewritelogfile  = NULL;
271     a->rewritelogfp    = NULL;
272     a->rewriteloglevel = 0;
273     a->rewritemaps     = apr_make_array(p, 2, sizeof(rewritemap_entry));
274     a->rewriteconds    = apr_make_array(p, 2, sizeof(rewritecond_entry));
275     a->rewriterules    = apr_make_array(p, 2, sizeof(rewriterule_entry));
276     a->server          = s;
277
278     return (void *)a;
279 }
280
281 static void *config_server_merge(apr_pool_t *p, void *basev, void *overridesv)
282 {
283     rewrite_server_conf *a, *base, *overrides;
284
285     a         = (rewrite_server_conf *)apr_pcalloc(p, sizeof(rewrite_server_conf));
286     base      = (rewrite_server_conf *)basev;
287     overrides = (rewrite_server_conf *)overridesv;
288
289     a->state   = overrides->state;
290     a->options = overrides->options;
291     a->server  = overrides->server;
292
293     if (a->options & OPTION_INHERIT) {
294         /*
295          *  local directives override
296          *  and anything else is inherited
297          */
298         a->rewriteloglevel = overrides->rewriteloglevel != 0 
299                              ? overrides->rewriteloglevel
300                              : base->rewriteloglevel;
301         a->rewritelogfile  = overrides->rewritelogfile != NULL 
302                              ? overrides->rewritelogfile
303                              : base->rewritelogfile;
304         a->rewritelogfp    = overrides->rewritelogfp != NULL 
305                              ? overrides->rewritelogfp 
306                              : base->rewritelogfp;
307         a->rewritemaps     = apr_append_arrays(p, overrides->rewritemaps,
308                                               base->rewritemaps);
309         a->rewriteconds    = apr_append_arrays(p, overrides->rewriteconds,
310                                               base->rewriteconds);
311         a->rewriterules    = apr_append_arrays(p, overrides->rewriterules,
312                                               base->rewriterules);
313     }
314     else {
315         /*
316          *  local directives override
317          *  and anything else gets defaults
318          */
319         a->rewriteloglevel = overrides->rewriteloglevel;
320         a->rewritelogfile  = overrides->rewritelogfile;
321         a->rewritelogfp    = overrides->rewritelogfp;
322         a->rewritemaps     = overrides->rewritemaps;
323         a->rewriteconds    = overrides->rewriteconds;
324         a->rewriterules    = overrides->rewriterules;
325     }
326
327     return (void *)a;
328 }
329
330
331 /*
332 **
333 **  per-directory configuration structure handling
334 **
335 */
336
337 static void *config_perdir_create(apr_pool_t *p, char *path)
338 {
339     rewrite_perdir_conf *a;
340
341     a = (rewrite_perdir_conf *)apr_pcalloc(p, sizeof(rewrite_perdir_conf));
342
343     a->state           = ENGINE_DISABLED;
344     a->options         = OPTION_NONE;
345     a->baseurl         = NULL;
346     a->rewriteconds    = apr_make_array(p, 2, sizeof(rewritecond_entry));
347     a->rewriterules    = apr_make_array(p, 2, sizeof(rewriterule_entry));
348
349     if (path == NULL) {
350         a->directory = NULL;
351     }
352     else {
353         /* make sure it has a trailing slash */
354         if (path[strlen(path)-1] == '/') {
355             a->directory = apr_pstrdup(p, path);
356         }
357         else {
358             a->directory = apr_pstrcat(p, path, "/", NULL);
359         }
360     }
361
362     return (void *)a;
363 }
364
365 static void *config_perdir_merge(apr_pool_t *p, void *basev, void *overridesv)
366 {
367     rewrite_perdir_conf *a, *base, *overrides;
368
369     a         = (rewrite_perdir_conf *)apr_pcalloc(p,
370                                                   sizeof(rewrite_perdir_conf));
371     base      = (rewrite_perdir_conf *)basev;
372     overrides = (rewrite_perdir_conf *)overridesv;
373
374     a->state     = overrides->state;
375     a->options   = overrides->options;
376     a->directory = overrides->directory;
377     a->baseurl   = overrides->baseurl;
378
379     if (a->options & OPTION_INHERIT) {
380         a->rewriteconds = apr_append_arrays(p, overrides->rewriteconds,
381                                            base->rewriteconds);
382         a->rewriterules = apr_append_arrays(p, overrides->rewriterules,
383                                            base->rewriterules);
384     }
385     else {
386         a->rewriteconds = overrides->rewriteconds;
387         a->rewriterules = overrides->rewriterules;
388     }
389
390     return (void *)a;
391 }
392
393
394 /*
395 **
396 **  the configuration commands
397 **
398 */
399
400 static const char *cmd_rewriteengine(cmd_parms *cmd,
401                                      void *in_dconf, int flag)
402 {
403     rewrite_perdir_conf *dconf = in_dconf;
404     rewrite_server_conf *sconf;
405
406     sconf = 
407         (rewrite_server_conf *)ap_get_module_config(cmd->server->module_config,
408                                                     &rewrite_module);
409
410     if (cmd->path == NULL) { /* is server command */
411         sconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
412     }
413     else                   /* is per-directory command */ {
414         dconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
415     }
416
417     return NULL;
418 }
419
420 static const char *cmd_rewriteoptions(cmd_parms *cmd,
421                                       void *in_dconf, const char *option)
422 {
423     rewrite_perdir_conf *dconf = in_dconf;
424     rewrite_server_conf *sconf;
425     const char *err;
426
427     sconf = (rewrite_server_conf *)
428             ap_get_module_config(cmd->server->module_config, &rewrite_module);
429
430     if (cmd->path == NULL) { /* is server command */
431         err = cmd_rewriteoptions_setoption(cmd->pool,
432                                            &(sconf->options), option);
433     }
434     else {                 /* is per-directory command */
435         err = cmd_rewriteoptions_setoption(cmd->pool,
436                                            &(dconf->options), option);
437     }
438
439     return err;
440 }
441
442 static const char *cmd_rewriteoptions_setoption(apr_pool_t *p, int *options,
443                                                 const char *name)
444 {
445     if (strcasecmp(name, "inherit") == 0) {
446         *options |= OPTION_INHERIT;
447     }
448     else {
449         return apr_pstrcat(p, "RewriteOptions: unknown option '",
450                           name, "'", NULL);
451     }
452     return NULL;
453 }
454
455 static const char *cmd_rewritelog(cmd_parms *cmd, void *dconf, const char *a1)
456 {
457     rewrite_server_conf *sconf;
458
459     sconf = (rewrite_server_conf *)
460             ap_get_module_config(cmd->server->module_config, &rewrite_module);
461
462     sconf->rewritelogfile = a1;
463
464     return NULL;
465 }
466
467 static const char *cmd_rewriteloglevel(cmd_parms *cmd, void *dconf, const char *a1)
468 {
469     rewrite_server_conf *sconf;
470
471     sconf = (rewrite_server_conf *)
472             ap_get_module_config(cmd->server->module_config, &rewrite_module);
473
474     sconf->rewriteloglevel = atoi(a1);
475
476     return NULL;
477 }
478
479 static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
480                                   const char *a2)
481 {
482     rewrite_server_conf *sconf;
483     rewritemap_entry *newmap;
484     apr_finfo_t st;
485
486     sconf = (rewrite_server_conf *)
487             ap_get_module_config(cmd->server->module_config, &rewrite_module);
488
489     newmap = apr_push_array(sconf->rewritemaps);
490
491     newmap->name = a1;
492     newmap->func = NULL;
493     if (strncmp(a2, "txt:", 4) == 0) {
494         newmap->type      = MAPTYPE_TXT;
495         newmap->datafile  = a2+4;
496         newmap->checkfile = a2+4;
497     }
498     else if (strncmp(a2, "rnd:", 4) == 0) {
499         newmap->type      = MAPTYPE_RND;
500         newmap->datafile  = a2+4;
501         newmap->checkfile = a2+4;
502     }
503     else if (strncmp(a2, "dbm:", 4) == 0) {
504 #ifndef NO_DBM_REWRITEMAP
505         newmap->type      = MAPTYPE_DBM;
506         newmap->datafile  = a2+4;
507         newmap->checkfile = apr_pstrcat(cmd->pool, a2+4, NDBM_FILE_SUFFIX, NULL);
508 #else
509         return apr_pstrdup(cmd->pool, "RewriteMap: cannot use NDBM mapfile, "
510                           "because no NDBM support is compiled in");
511 #endif
512     }
513     else if (strncmp(a2, "prg:", 4) == 0) {
514         newmap->type = MAPTYPE_PRG;
515         newmap->datafile = a2+4;
516         newmap->checkfile = a2+4;
517     }
518     else if (strncmp(a2, "int:", 4) == 0) {
519         newmap->type      = MAPTYPE_INT;
520         newmap->datafile  = NULL;
521         newmap->checkfile = NULL;
522         if (strcmp(a2+4, "tolower") == 0) {
523             newmap->func = rewrite_mapfunc_tolower;
524         }
525         else if (strcmp(a2+4, "toupper") == 0) {
526             newmap->func = rewrite_mapfunc_toupper;
527         }
528         else if (strcmp(a2+4, "escape") == 0) {
529             newmap->func = rewrite_mapfunc_escape;
530         }
531         else if (strcmp(a2+4, "unescape") == 0) {
532             newmap->func = rewrite_mapfunc_unescape;
533         }
534         else if (sconf->state == ENGINE_ENABLED) {
535             return apr_pstrcat(cmd->pool, "RewriteMap: internal map not found:",
536                               a2+4, NULL);
537         }
538     }
539     else {
540         newmap->type      = MAPTYPE_TXT;
541         newmap->datafile  = a2;
542         newmap->checkfile = a2;
543     }
544     newmap->fpin  = NULL;
545     newmap->fpout = NULL;
546
547     if (newmap->checkfile && (sconf->state == ENGINE_ENABLED)
548         && (apr_stat(&st, newmap->checkfile, cmd->pool) != APR_SUCCESS)) {
549         return apr_pstrcat(cmd->pool,
550                           "RewriteMap: map file or program not found:",
551                           newmap->checkfile, NULL);
552     }
553
554     return NULL;
555 }
556
557 static const char *cmd_rewritelock(cmd_parms *cmd, void *dconf, const char *a1)
558 {
559     const char *error;
560
561     if ((error = ap_check_cmd_context(cmd, GLOBAL_ONLY)) != NULL)
562         return error;
563
564     lockname = a1;
565
566     return NULL;
567 }
568
569 static const char *cmd_rewritebase(cmd_parms *cmd, void *in_dconf,
570                                    const char *a1)
571 {
572     rewrite_perdir_conf *dconf = in_dconf;
573
574     if (cmd->path == NULL || dconf == NULL) {
575         return "RewriteBase: only valid in per-directory config files";
576     }
577     if (a1[0] == '\0') {
578         return "RewriteBase: empty URL not allowed";
579     }
580     if (a1[0] != '/') {
581         return "RewriteBase: argument is not a valid URL";
582     }
583
584     dconf->baseurl = a1;
585
586     return NULL;
587 }
588
589 static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
590                                    const char *in_str)
591 {
592     rewrite_perdir_conf *dconf = in_dconf;
593     char *str = apr_pstrdup(cmd->pool, in_str);
594     rewrite_server_conf *sconf;
595     rewritecond_entry *newcond;
596     regex_t *regexp;
597     char *a1;
598     char *a2;
599     char *a3;
600     char *cp;
601     const char *err;
602     int rc;
603
604     sconf = (rewrite_server_conf *)
605             ap_get_module_config(cmd->server->module_config, &rewrite_module);
606
607     /*  make a new entry in the internal temporary rewrite rule list */
608     if (cmd->path == NULL) {   /* is server command */
609         newcond = apr_push_array(sconf->rewriteconds);
610     }
611     else {                     /* is per-directory command */
612         newcond = apr_push_array(dconf->rewriteconds);
613     }
614
615     /*  parse the argument line ourself */
616     if (parseargline(str, &a1, &a2, &a3)) {
617         return apr_pstrcat(cmd->pool, "RewriteCond: bad argument line '", str,
618                           "'", NULL);
619     }
620
621     /*  arg1: the input string */
622     newcond->input = apr_pstrdup(cmd->pool, a1);
623
624     /* arg3: optional flags field
625        (this have to be first parsed, because we need to
626         know if the regex should be compiled with ICASE!) */
627     newcond->flags = CONDFLAG_NONE;
628     if (a3 != NULL) {
629         if ((err = cmd_rewritecond_parseflagfield(cmd->pool, newcond,
630                                                   a3)) != NULL) {
631             return err;
632         }
633     }
634
635     /*  arg2: the pattern
636         try to compile the regexp to test if is ok */
637     cp = a2;
638     if (cp[0] == '!') {
639         newcond->flags |= CONDFLAG_NOTMATCH;
640         cp++;
641     }
642
643     /* now be careful: Under the POSIX regex library
644        we can compile the pattern for case insensitive matching,
645        under the old V8 library we have to do it self via a hack */
646     if (newcond->flags & CONDFLAG_NOCASE) {
647         rc = ((regexp = ap_pregcomp(cmd->pool, cp, REG_EXTENDED|REG_ICASE))
648               == NULL);
649     }
650     else {
651         rc = ((regexp = ap_pregcomp(cmd->pool, cp, REG_EXTENDED)) == NULL);
652     }
653     if (rc) {
654         return apr_pstrcat(cmd->pool,
655                           "RewriteCond: cannot compile regular expression '",
656                           a2, "'", NULL);
657     }
658
659     newcond->pattern = apr_pstrdup(cmd->pool, cp);
660     newcond->regexp  = regexp;
661
662     return NULL;
663 }
664
665 static const char *cmd_rewritecond_parseflagfield(apr_pool_t *p,
666                                                   rewritecond_entry *cfg,
667                                                   char *str)
668 {
669     char *cp;
670     char *cp1;
671     char *cp2;
672     char *cp3;
673     char *key;
674     char *val;
675     const char *err;
676
677     if (str[0] != '[' || str[strlen(str)-1] != ']') {
678         return "RewriteCond: bad flag delimiters";
679     }
680
681     cp = str+1;
682     str[strlen(str)-1] = ','; /* for simpler parsing */
683     for ( ; *cp != '\0'; ) {
684         /* skip whitespaces */
685         for ( ; (*cp == ' ' || *cp == '\t') && *cp != '\0'; cp++)
686             ;
687         if (*cp == '\0') {
688             break;
689         }
690         cp1 = cp;
691         if ((cp2 = strchr(cp, ',')) != NULL) {
692             cp = cp2+1;
693             for ( ; (*(cp2-1) == ' ' || *(cp2-1) == '\t'); cp2--)
694                 ;
695             *cp2 = '\0';
696             if ((cp3 = strchr(cp1, '=')) != NULL) {
697                 *cp3 = '\0';
698                 key = cp1;
699                 val = cp3+1;
700             }
701             else {
702                 key = cp1;
703                 val = "";
704             }
705             if ((err = cmd_rewritecond_setflag(p, cfg, key, val)) != NULL) {
706                 return err;
707             }
708         }
709         else {
710             break;
711         }
712     }
713
714     return NULL;
715 }
716
717 static const char *cmd_rewritecond_setflag(apr_pool_t *p, rewritecond_entry *cfg,
718                                            char *key, char *val)
719 {
720     if (   strcasecmp(key, "nocase") == 0
721         || strcasecmp(key, "NC") == 0    ) {
722         cfg->flags |= CONDFLAG_NOCASE;
723     }
724     else if (   strcasecmp(key, "ornext") == 0
725              || strcasecmp(key, "OR") == 0    ) {
726         cfg->flags |= CONDFLAG_ORNEXT;
727     }
728     else {
729         return apr_pstrcat(p, "RewriteCond: unknown flag '", key, "'", NULL);
730     }
731     return NULL;
732 }
733
734 static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
735                                    const char *in_str)
736 {
737     rewrite_perdir_conf *dconf = in_dconf;
738     char *str = apr_pstrdup(cmd->pool, in_str);
739     rewrite_server_conf *sconf;
740     rewriterule_entry *newrule;
741     regex_t *regexp;
742     char *a1;
743     char *a2;
744     char *a3;
745     char *cp;
746     const char *err;
747     int mode;
748
749     sconf = (rewrite_server_conf *)
750             ap_get_module_config(cmd->server->module_config, &rewrite_module);
751
752     /*  make a new entry in the internal rewrite rule list */
753     if (cmd->path == NULL) {   /* is server command */
754         newrule = apr_push_array(sconf->rewriterules);
755     }
756     else {                     /* is per-directory command */
757         newrule = apr_push_array(dconf->rewriterules);
758     }
759
760     /*  parse the argument line ourself */
761     if (parseargline(str, &a1, &a2, &a3)) {
762         return apr_pstrcat(cmd->pool, "RewriteRule: bad argument line '", str,
763                           "'", NULL);
764     }
765
766     /* arg3: optional flags field */
767     newrule->forced_mimetype     = NULL;
768     newrule->forced_responsecode = HTTP_MOVED_TEMPORARILY;
769     newrule->flags  = RULEFLAG_NONE;
770     newrule->env[0] = NULL;
771     newrule->skip   = 0;
772     if (a3 != NULL) {
773         if ((err = cmd_rewriterule_parseflagfield(cmd->pool, newrule,
774                                                   a3)) != NULL) {
775             return err;
776         }
777     }
778
779     /*  arg1: the pattern
780      *  try to compile the regexp to test if is ok
781      */
782     cp = a1;
783     if (cp[0] == '!') {
784         newrule->flags |= RULEFLAG_NOTMATCH;
785         cp++;
786     }
787     mode = REG_EXTENDED;
788     if (newrule->flags & RULEFLAG_NOCASE) {
789         mode |= REG_ICASE;
790     }
791     if ((regexp = ap_pregcomp(cmd->pool, cp, mode)) == NULL) {
792         return apr_pstrcat(cmd->pool,
793                           "RewriteRule: cannot compile regular expression '",
794                           a1, "'", NULL);
795     }
796     newrule->pattern = apr_pstrdup(cmd->pool, cp);
797     newrule->regexp  = regexp;
798
799     /*  arg2: the output string
800      *  replace the $<N> by \<n> which is needed by the currently
801      *  used Regular Expression library
802      *
803      * TODO: Is this still required for PCRE?  If not, does it *work* with PCRE?
804      */
805     newrule->output = apr_pstrdup(cmd->pool, a2);
806
807     /* now, if the server or per-dir config holds an
808      * array of RewriteCond entries, we take it for us
809      * and clear the array
810      */
811     if (cmd->path == NULL) {  /* is server command */
812         newrule->rewriteconds   = sconf->rewriteconds;
813         sconf->rewriteconds = apr_make_array(cmd->pool, 2,
814                                             sizeof(rewritecond_entry));
815     }
816     else {                    /* is per-directory command */
817         newrule->rewriteconds   = dconf->rewriteconds;
818         dconf->rewriteconds = apr_make_array(cmd->pool, 2,
819                                             sizeof(rewritecond_entry));
820     }
821
822     return NULL;
823 }
824
825 static const char *cmd_rewriterule_parseflagfield(apr_pool_t *p,
826                                                   rewriterule_entry *cfg,
827                                                   char *str)
828 {
829     char *cp;
830     char *cp1;
831     char *cp2;
832     char *cp3;
833     char *key;
834     char *val;
835     const char *err;
836
837     if (str[0] != '[' || str[strlen(str)-1] != ']') {
838         return "RewriteRule: bad flag delimiters";
839     }
840
841     cp = str+1;
842     str[strlen(str)-1] = ','; /* for simpler parsing */
843     for ( ; *cp != '\0'; ) {
844         /* skip whitespaces */
845         for ( ; (*cp == ' ' || *cp == '\t') && *cp != '\0'; cp++)
846             ;
847         if (*cp == '\0') {
848             break;
849         }
850         cp1 = cp;
851         if ((cp2 = strchr(cp, ',')) != NULL) {
852             cp = cp2+1;
853             for ( ; (*(cp2-1) == ' ' || *(cp2-1) == '\t'); cp2--)
854                 ;
855             *cp2 = '\0';
856             if ((cp3 = strchr(cp1, '=')) != NULL) {
857                 *cp3 = '\0';
858                 key = cp1;
859                 val = cp3+1;
860             }
861             else {
862                 key = cp1;
863                 val = "";
864             }
865             if ((err = cmd_rewriterule_setflag(p, cfg, key, val)) != NULL) {
866                 return err;
867             }
868         }
869         else {
870             break;
871         }
872     }
873
874     return NULL;
875 }
876
877 static const char *cmd_rewriterule_setflag(apr_pool_t *p, rewriterule_entry *cfg,
878                                            char *key, char *val)
879 {
880     int status = 0;
881     int i;
882
883     if (   strcasecmp(key, "redirect") == 0
884         || strcasecmp(key, "R") == 0       ) {
885         cfg->flags |= RULEFLAG_FORCEREDIRECT;
886         if (strlen(val) > 0) {
887             if (strcasecmp(val, "permanent") == 0) {
888                 status = HTTP_MOVED_PERMANENTLY;
889             }
890             else if (strcasecmp(val, "temp") == 0) {
891                 status = HTTP_MOVED_TEMPORARILY;
892             }
893             else if (strcasecmp(val, "seeother") == 0) {
894                 status = HTTP_SEE_OTHER;
895             }
896             else if (apr_isdigit(*val)) {
897                 status = atoi(val);
898             }
899             if (!ap_is_HTTP_REDIRECT(status)) {
900                 return "RewriteRule: invalid HTTP response code "
901                        "for flag 'R'";
902             }
903             cfg->forced_responsecode = status;
904         }
905     }
906     else if (   strcasecmp(key, "last") == 0
907              || strcasecmp(key, "L") == 0   ) {
908         cfg->flags |= RULEFLAG_LASTRULE;
909     }
910     else if (   strcasecmp(key, "next") == 0
911              || strcasecmp(key, "N") == 0   ) {
912         cfg->flags |= RULEFLAG_NEWROUND;
913     }
914     else if (   strcasecmp(key, "chain") == 0
915              || strcasecmp(key, "C") == 0    ) {
916         cfg->flags |= RULEFLAG_CHAIN;
917     }
918     else if (   strcasecmp(key, "type") == 0
919              || strcasecmp(key, "T") == 0   ) {
920         cfg->forced_mimetype = apr_pstrdup(p, val);
921         ap_str_tolower(cfg->forced_mimetype);
922     }
923     else if (   strcasecmp(key, "env") == 0
924              || strcasecmp(key, "E") == 0   ) {
925         for (i = 0; (cfg->env[i] != NULL) && (i < MAX_ENV_FLAGS); i++)
926             ;
927         if (i < MAX_ENV_FLAGS) {
928             cfg->env[i] = apr_pstrdup(p, val);
929             cfg->env[i+1] = NULL;
930         }
931         else {
932             return "RewriteRule: too many environment flags 'E'";
933         }
934     }
935     else if (   strcasecmp(key, "nosubreq") == 0
936              || strcasecmp(key, "NS") == 0      ) {
937         cfg->flags |= RULEFLAG_IGNOREONSUBREQ;
938     }
939     else if (   strcasecmp(key, "proxy") == 0
940              || strcasecmp(key, "P") == 0      ) {
941         cfg->flags |= RULEFLAG_PROXY;
942     }
943     else if (   strcasecmp(key, "passthrough") == 0
944              || strcasecmp(key, "PT") == 0      ) {
945         cfg->flags |= RULEFLAG_PASSTHROUGH;
946     }
947     else if (   strcasecmp(key, "skip") == 0
948              || strcasecmp(key, "S") == 0   ) {
949         cfg->skip = atoi(val);
950     }
951     else if (   strcasecmp(key, "forbidden") == 0
952              || strcasecmp(key, "F") == 0   ) {
953         cfg->flags |= RULEFLAG_FORBIDDEN;
954     }
955     else if (   strcasecmp(key, "gone") == 0
956              || strcasecmp(key, "G") == 0   ) {
957         cfg->flags |= RULEFLAG_GONE;
958     }
959     else if (   strcasecmp(key, "qsappend") == 0
960              || strcasecmp(key, "QSA") == 0   ) {
961         cfg->flags |= RULEFLAG_QSAPPEND;
962     }
963     else if (   strcasecmp(key, "nocase") == 0
964              || strcasecmp(key, "NC") == 0    ) {
965         cfg->flags |= RULEFLAG_NOCASE;
966     }
967     else {
968         return apr_pstrcat(p, "RewriteRule: unknown flag '", key, "'", NULL);
969     }
970     return NULL;
971 }
972
973
974 /*
975 **
976 **  Global Module Initialization
977 **  [called from read_config() after all
978 **  config commands were already called]
979 **
980 */
981
982 static void init_module(apr_pool_t *p,
983                         apr_pool_t *plog,
984                         apr_pool_t *ptemp,
985                         server_rec *s)
986 {
987     apr_status_t rv;
988
989     /* check if proxy module is available */
990     proxy_available = (ap_find_linked_module("mod_proxy.c") != NULL);
991
992     /* create the rewriting lockfiles in the parent */
993     if ((rv = apr_create_lock (&rewrite_log_lock, APR_MUTEX, APR_LOCKALL,
994                                NULL, NULL)) != APR_SUCCESS) {
995         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
996                      "mod_rewrite: could not create rewrite_log_lock");
997         exit(1);
998     }
999
1000     rewritelock_create(s, p);
1001     apr_register_cleanup(p, (void *)s, rewritelock_remove, apr_null_cleanup);
1002
1003     /* step through the servers and
1004      * - open each rewriting logfile
1005      * - open the RewriteMap prg:xxx programs
1006      */
1007     for (; s; s = s->next) {
1008         open_rewritelog(s, p);
1009         if (once_through > 0)
1010            run_rewritemap_programs(s, p);
1011     }
1012
1013     once_through++;
1014 }
1015
1016
1017 /*
1018 **
1019 **  Per-Child Module Initialization
1020 **  [called after a child process is spawned]
1021 **
1022 */
1023
1024 static void init_child(apr_pool_t *p, server_rec *s)
1025 {
1026     apr_status_t rv;
1027
1028     if (lockname != NULL && *(lockname) != '\0')
1029     {
1030         rv = apr_child_init_lock (&rewrite_mapr_lock, lockname, p);
1031         if (rv != APR_SUCCESS) {
1032             ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
1033                          "mod_rewrite: could not init rewrite_mapr_lock "
1034                          "in child");
1035         }
1036     }
1037
1038     /* create the lookup cache */
1039     cachep = init_cache(p);
1040 }
1041
1042
1043 /*
1044 ** +-------------------------------------------------------+
1045 ** |                                                       |
1046 ** |                     runtime hooks
1047 ** |                                                       |
1048 ** +-------------------------------------------------------+
1049 */
1050
1051 /*
1052 **
1053 **  URI-to-filename hook
1054 **
1055 **  [used for the rewriting engine triggered by
1056 **  the per-server 'RewriteRule' directives]
1057 **
1058 */
1059
1060 static int hook_uri2file(request_rec *r)
1061 {
1062     void *sconf;
1063     rewrite_server_conf *conf;
1064     const char *var;
1065     const char *thisserver;
1066     char *thisport;
1067     const char *thisurl;
1068     char buf[512];
1069     char docroot[512];
1070     char *cp, *cp2;
1071     const char *ccp;
1072     apr_finfo_t finfo;
1073     unsigned int port;
1074     int n;
1075     int l;
1076
1077     /*
1078      *  retrieve the config structures
1079      */
1080     sconf = r->server->module_config;
1081     conf  = (rewrite_server_conf *)ap_get_module_config(sconf,
1082                                                         &rewrite_module);
1083
1084     /*
1085      *  only do something under runtime if the engine is really enabled,
1086      *  else return immediately!
1087      */
1088     if (conf->state == ENGINE_DISABLED) {
1089         return DECLINED;
1090     }
1091
1092     /*
1093      *  check for the ugly API case of a virtual host section where no
1094      *  mod_rewrite directives exists. In this situation we became no chance
1095      *  by the API to setup our default per-server config so we have to
1096      *  on-the-fly assume we have the default config. But because the default
1097      *  config has a disabled rewriting engine we are lucky because can
1098      *  just stop operating now.
1099      */
1100     if (conf->server != r->server) {
1101         return DECLINED;
1102     }
1103
1104     /*
1105      *  add the SCRIPT_URL variable to the env. this is a bit complicated
1106      *  due to the fact that apache uses subrequests and internal redirects
1107      */
1108
1109     if (r->main == NULL) {
1110          var = apr_pstrcat(r->pool, "REDIRECT_", ENVVAR_SCRIPT_URL, NULL);
1111          var = apr_table_get(r->subprocess_env, var);
1112          if (var == NULL) {
1113              apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, r->uri);
1114          }
1115          else {
1116              apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var);
1117          }
1118     }
1119     else {
1120          var = apr_table_get(r->main->subprocess_env, ENVVAR_SCRIPT_URL);
1121          apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var);
1122     }
1123
1124     /*
1125      *  create the SCRIPT_URI variable for the env
1126      */
1127
1128     /* add the canonical URI of this URL */
1129     thisserver = ap_get_server_name(r);
1130     port = ap_get_server_port(r);
1131     if (ap_is_default_port(port, r)) {
1132         thisport = "";
1133     }
1134     else {
1135         apr_snprintf(buf, sizeof(buf), ":%u", port);
1136         thisport = buf;
1137     }
1138     thisurl = apr_table_get(r->subprocess_env, ENVVAR_SCRIPT_URL);
1139
1140     /* set the variable */
1141     var = apr_pstrcat(r->pool, ap_http_method(r), "://", thisserver, thisport,
1142                      thisurl, NULL);
1143     apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URI, var);
1144
1145     /* if filename was not initially set,
1146      * we start with the requested URI
1147      */
1148     if (r->filename == NULL) {
1149         r->filename = apr_pstrdup(r->pool, r->uri);
1150         rewritelog(r, 2, "init rewrite engine with requested uri %s",
1151                    r->filename);
1152     }
1153
1154     /*
1155      *  now apply the rules ...
1156      */
1157     if (apply_rewrite_list(r, conf->rewriterules, NULL)) {
1158
1159         if (strlen(r->filename) > 6 &&
1160             strncmp(r->filename, "proxy:", 6) == 0) {
1161             /* it should be go on as an internal proxy request */
1162
1163             /* check if the proxy module is enabled, so
1164              * we can actually use it!
1165              */
1166             if (!proxy_available) {
1167                 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1168                              "attempt to make remote request from mod_rewrite "
1169                              "without proxy enabled: %s", r->filename);
1170                 return HTTP_FORBIDDEN;
1171             }
1172
1173             /* make sure the QUERY_STRING and
1174              * PATH_INFO parts get incorporated
1175              */
1176             if (r->path_info != NULL) {
1177                 r->filename = apr_pstrcat(r->pool, r->filename,
1178                                          r->path_info, NULL);
1179             }
1180             if (r->args != NULL &&
1181                 r->uri == r->unparsed_uri) {
1182                 /* see proxy_http:proxy_http_canon() */
1183                 r->filename = apr_pstrcat(r->pool, r->filename,
1184                                          "?", r->args, NULL);
1185             }
1186
1187             /* now make sure the request gets handled by the proxy handler */
1188             r->proxyreq = 1;
1189             r->handler  = "proxy-server";
1190
1191             rewritelog(r, 1, "go-ahead with proxy request %s [OK]",
1192                        r->filename);
1193             return OK;
1194         }
1195         else if (is_absolute_uri(r->filename)) {
1196             /* it was finally rewritten to a remote URL */
1197
1198             /* skip 'scheme:' */
1199             for (cp = r->filename; *cp != ':' && *cp != '\0'; cp++)
1200                 ;
1201             /* skip '://' */
1202             cp += 3;
1203             /* skip host part */
1204             for ( ; *cp != '/' && *cp != '\0'; cp++)
1205                 ;
1206             if (*cp != '\0') {
1207                 rewritelog(r, 1, "escaping %s for redirect", r->filename);
1208                 cp2 = ap_escape_uri(r->pool, cp);
1209                 *cp = '\0';
1210                 r->filename = apr_pstrcat(r->pool, r->filename, cp2, NULL);
1211             }
1212
1213             /* append the QUERY_STRING part */
1214             if (r->args != NULL) {
1215                 r->filename = apr_pstrcat(r->pool, r->filename, "?", 
1216                                          ap_escape_uri(r->pool, r->args), NULL);
1217             }
1218
1219             /* determine HTTP redirect response code */
1220             if (ap_is_HTTP_REDIRECT(r->status)) {
1221                 n = r->status;
1222                 r->status = HTTP_OK; /* make Apache kernel happy */
1223             }
1224             else {
1225                 n = HTTP_MOVED_TEMPORARILY;
1226             }
1227
1228             /* now do the redirection */
1229             apr_table_setn(r->headers_out, "Location", r->filename);
1230             rewritelog(r, 1, "redirect to %s [REDIRECT/%d]", r->filename, n);
1231             return n;
1232         }
1233         else if (strlen(r->filename) > 10 &&
1234                  strncmp(r->filename, "forbidden:", 10) == 0) {
1235             /* This URLs is forced to be forbidden for the requester */
1236             return HTTP_FORBIDDEN;
1237         }
1238         else if (strlen(r->filename) > 5 &&
1239                  strncmp(r->filename, "gone:", 5) == 0) {
1240             /* This URLs is forced to be gone */
1241             return HTTP_GONE;
1242         }
1243         else if (strlen(r->filename) > 12 &&
1244                  strncmp(r->filename, "passthrough:", 12) == 0) {
1245             /*
1246              * Hack because of underpowered API: passing the current
1247              * rewritten filename through to other URL-to-filename handlers
1248              * just as it were the requested URL. This is to enable
1249              * post-processing by mod_alias, etc.  which always act on
1250              * r->uri! The difference here is: We do not try to
1251              * add the document root
1252              */
1253             r->uri = apr_pstrdup(r->pool, r->filename+12);
1254             return DECLINED;
1255         }
1256         else {
1257             /* it was finally rewritten to a local path */
1258
1259             /* expand "/~user" prefix */
1260 #if !defined(WIN32) && !defined(NETWARE)
1261             r->filename = expand_tildepaths(r, r->filename);
1262 #endif
1263             rewritelog(r, 2, "local path result: %s", r->filename);
1264
1265             /* the filename has to start with a slash! */
1266             if (r->filename[0] != '/') {
1267                 return HTTP_BAD_REQUEST;
1268             }
1269
1270             /* if there is no valid prefix, we have
1271              * to emulate the translator from the core and
1272              * prefix the filename with document_root
1273              *
1274              * NOTICE:
1275              * We cannot leave out the prefix_stat because
1276              * - when we always prefix with document_root
1277              *   then no absolute path can be created, e.g. via
1278              *   emulating a ScriptAlias directive, etc.
1279              * - when we always NOT prefix with document_root
1280              *   then the files under document_root have to
1281              *   be references directly and document_root
1282              *   gets never used and will be a dummy parameter -
1283              *   this is also bad
1284              *
1285              * BUT:
1286              * Under real Unix systems this is no problem,
1287              * because we only do stat() on the first directory
1288              * and this gets cached by the kernel for along time!
1289              */
1290             n = prefix_stat(r->filename, &finfo);
1291             if (n == 0) {
1292                 if ((ccp = ap_document_root(r)) != NULL) {
1293                     l = apr_cpystrn(docroot, ccp, sizeof(docroot)) - docroot;
1294
1295                     /* always NOT have a trailing slash */
1296                     if (docroot[l-1] == '/') {
1297                         docroot[l-1] = '\0';
1298                     }
1299                     if (r->server->path
1300                         && !strncmp(r->filename, r->server->path,
1301                                     r->server->pathlen)) {
1302                         r->filename = apr_pstrcat(r->pool, docroot,
1303                                                  (r->filename +
1304                                                   r->server->pathlen), NULL);
1305                     }
1306                     else {
1307                         r->filename = apr_pstrcat(r->pool, docroot, 
1308                                                  r->filename, NULL);
1309                     }
1310                     rewritelog(r, 2, "prefixed with document_root to %s",
1311                                r->filename);
1312                 }
1313             }
1314
1315             rewritelog(r, 1, "go-ahead with %s [OK]", r->filename);
1316             return OK;
1317         }
1318     }
1319     else {
1320         rewritelog(r, 1, "pass through %s", r->filename);
1321         return DECLINED;
1322     }
1323 }
1324
1325
1326 /*
1327 **
1328 **  MIME-type hook
1329 **
1330 **  [used to support the forced-MIME-type feature]
1331 **
1332 */
1333
1334 static int hook_mimetype(request_rec *r)
1335 {
1336     const char *t;
1337
1338     /* now check if we have to force a MIME-type */
1339     t = apr_table_get(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR);
1340     if (t == NULL) {
1341         return DECLINED;
1342     }
1343     else {
1344         rewritelog(r, 1, "force filename %s to have MIME-type '%s'",
1345                    r->filename, t);
1346         r->content_type = t;
1347         return OK;
1348     }
1349 }
1350
1351
1352 /*
1353 **
1354 **  Fixup hook
1355 **
1356 **  [used for the rewriting engine triggered by
1357 **  the per-directory 'RewriteRule' directives]
1358 **
1359 */
1360
1361 static int hook_fixup(request_rec *r)
1362 {
1363     rewrite_perdir_conf *dconf;
1364     char *cp;
1365     char *cp2;
1366     const char *ccp;
1367     char *prefix;
1368     int l;
1369     int n;
1370     char *ofilename;
1371
1372     dconf = (rewrite_perdir_conf *)ap_get_module_config(r->per_dir_config,
1373                                                         &rewrite_module);
1374
1375     /* if there is no per-dir config we return immediately */
1376     if (dconf == NULL) {
1377         return DECLINED;
1378     }
1379
1380     /* we shouldn't do anything in subrequests */
1381     if (r->main != NULL) {
1382         return DECLINED;
1383     }
1384
1385     /* if there are no real (i.e. no RewriteRule directives!)
1386        per-dir config of us, we return also immediately */
1387     if (dconf->directory == NULL) {
1388         return DECLINED;
1389     }
1390
1391     /*
1392      *  only do something under runtime if the engine is really enabled,
1393      *  for this directory, else return immediately!
1394      */
1395     if (!(ap_allow_options(r) & (OPT_SYM_LINKS | OPT_SYM_OWNER))) {
1396         /* FollowSymLinks is mandatory! */
1397         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1398                      "Options FollowSymLinks or SymLinksIfOwnerMatch is off "
1399                      "which implies that RewriteRule directive is forbidden: "
1400                      "%s", r->filename);
1401         return HTTP_FORBIDDEN;
1402     }
1403     else {
1404         /* FollowSymLinks is given, but the user can
1405          * still turn off the rewriting engine
1406          */
1407         if (dconf->state == ENGINE_DISABLED) {
1408             return DECLINED;
1409         }
1410     }
1411
1412     /*
1413      *  remember the current filename before rewriting for later check
1414      *  to prevent deadlooping because of internal redirects
1415      *  on final URL/filename which can be equal to the inital one.
1416      */
1417     ofilename = r->filename;
1418
1419     /*
1420      *  now apply the rules ...
1421      */
1422     if (apply_rewrite_list(r, dconf->rewriterules, dconf->directory)) {
1423
1424         if (strlen(r->filename) > 6 &&
1425             strncmp(r->filename, "proxy:", 6) == 0) {
1426             /* it should go on as an internal proxy request */
1427
1428             /* make sure the QUERY_STRING and
1429              * PATH_INFO parts get incorporated
1430              * (r->path_info was already appended by the
1431              * rewriting engine because of the per-dir context!)
1432              */
1433             if (r->args != NULL) {
1434                 r->filename = apr_pstrcat(r->pool, r->filename,
1435                                          "?", r->args, NULL);
1436             }
1437
1438             /* now make sure the request gets handled by the proxy handler */
1439             r->proxyreq = 1;
1440             r->handler  = "proxy-server";
1441
1442             rewritelog(r, 1, "[per-dir %s] go-ahead with proxy request "
1443                        "%s [OK]", dconf->directory, r->filename);
1444             return OK;
1445         }
1446         else if (is_absolute_uri(r->filename)) {
1447             /* it was finally rewritten to a remote URL */
1448
1449             /* because we are in a per-dir context
1450              * first try to replace the directory with its base-URL
1451              * if there is a base-URL available
1452              */
1453             if (dconf->baseurl != NULL) {
1454                 /* skip 'scheme:' */
1455                 for (cp = r->filename; *cp != ':' && *cp != '\0'; cp++)
1456                     ;
1457                 /* skip '://' */
1458                 cp += 3;
1459                 if ((cp = strchr(cp, '/')) != NULL) {
1460                     rewritelog(r, 2,
1461                                "[per-dir %s] trying to replace "
1462                                "prefix %s with %s",
1463                                dconf->directory, dconf->directory,
1464                                dconf->baseurl);
1465                     cp2 = subst_prefix_path(r, cp, dconf->directory,
1466                                             dconf->baseurl);
1467                     if (strcmp(cp2, cp) != 0) {
1468                         *cp = '\0';
1469                         r->filename = apr_pstrcat(r->pool, r->filename,
1470                                                  cp2, NULL);
1471                     }
1472                 }
1473             }
1474
1475             /* now prepare the redirect... */
1476
1477             /* skip 'scheme:' */
1478             for (cp = r->filename; *cp != ':' && *cp != '\0'; cp++)
1479                 ;
1480             /* skip '://' */
1481             cp += 3;
1482             /* skip host part */
1483             for ( ; *cp != '/' && *cp != '\0'; cp++)
1484                 ;
1485             if (*cp != '\0') {
1486                 rewritelog(r, 1, "[per-dir %s] escaping %s for redirect",
1487                            dconf->directory, r->filename);
1488                 cp2 = ap_escape_uri(r->pool, cp);
1489                 *cp = '\0';
1490                 r->filename = apr_pstrcat(r->pool, r->filename, cp2, NULL);
1491             }
1492
1493             /* append the QUERY_STRING part */
1494             if (r->args != NULL) {
1495                 r->filename = apr_pstrcat(r->pool, r->filename, "?", 
1496                                          ap_escape_uri(r->pool, r->args), NULL);
1497             }
1498
1499             /* determine HTTP redirect response code */
1500             if (ap_is_HTTP_REDIRECT(r->status)) {
1501                 n = r->status;
1502                 r->status = HTTP_OK; /* make Apache kernel happy */
1503             }
1504             else {
1505                 n = HTTP_MOVED_TEMPORARILY;
1506             }
1507
1508             /* now do the redirection */
1509             apr_table_setn(r->headers_out, "Location", r->filename);
1510             rewritelog(r, 1, "[per-dir %s] redirect to %s [REDIRECT/%d]",
1511                        dconf->directory, r->filename, n);
1512             return n;
1513         }
1514         else if (strlen(r->filename) > 10 &&
1515                  strncmp(r->filename, "forbidden:", 10) == 0) {
1516             /* This URL is forced to be forbidden for the requester */
1517             return HTTP_FORBIDDEN;
1518         }
1519         else if (strlen(r->filename) > 5 &&
1520                  strncmp(r->filename, "gone:", 5) == 0) {
1521             /* This URL is forced to be gone */
1522             return HTTP_GONE;
1523         }
1524         else {
1525             /* it was finally rewritten to a local path */
1526
1527             /* if someone used the PASSTHROUGH flag in per-dir
1528              * context we just ignore it. It is only useful
1529              * in per-server context
1530              */
1531             if (strlen(r->filename) > 12 &&
1532                 strncmp(r->filename, "passthrough:", 12) == 0) {
1533                 r->filename = apr_pstrdup(r->pool, r->filename+12);
1534             }
1535
1536             /* the filename has to start with a slash! */
1537             if (r->filename[0] != '/') {
1538                 return HTTP_BAD_REQUEST;
1539             }
1540
1541             /* Check for deadlooping:
1542              * At this point we KNOW that at least one rewriting
1543              * rule was applied, but when the resulting URL is
1544              * the same as the initial URL, we are not allowed to
1545              * use the following internal redirection stuff because
1546              * this would lead to a deadloop.
1547              */
1548             if (strcmp(r->filename, ofilename) == 0) {
1549                 rewritelog(r, 1, "[per-dir %s] initial URL equal rewritten "
1550                            "URL: %s [IGNORING REWRITE]",
1551                            dconf->directory, r->filename);
1552                 return OK;
1553             }
1554
1555             /* if there is a valid base-URL then substitute
1556              * the per-dir prefix with this base-URL if the
1557              * current filename still is inside this per-dir
1558              * context. If not then treat the result as a
1559              * plain URL
1560              */
1561             if (dconf->baseurl != NULL) {
1562                 rewritelog(r, 2,
1563                            "[per-dir %s] trying to replace prefix %s with %s",
1564                            dconf->directory, dconf->directory, dconf->baseurl);
1565                 r->filename = subst_prefix_path(r, r->filename,
1566                                                 dconf->directory,
1567                                                 dconf->baseurl);
1568             }
1569             else {
1570                 /* if no explicit base-URL exists we assume
1571                  * that the directory prefix is also a valid URL
1572                  * for this webserver and only try to remove the
1573                  * document_root if it is prefix
1574                  */
1575                 if ((ccp = ap_document_root(r)) != NULL) {
1576                     prefix = apr_pstrdup(r->pool, ccp);
1577                     /* always NOT have a trailing slash */
1578                     l = strlen(prefix);
1579                     if (prefix[l-1] == '/') {
1580                         prefix[l-1] = '\0';
1581                         l--;
1582                     }
1583                     if (strncmp(r->filename, prefix, l) == 0) {
1584                         rewritelog(r, 2,
1585                                    "[per-dir %s] strip document_root "
1586                                    "prefix: %s -> %s",
1587                                    dconf->directory, r->filename,
1588                                    r->filename+l);
1589                         r->filename = apr_pstrdup(r->pool, r->filename+l);
1590                     }
1591                 }
1592             }
1593
1594             /* now initiate the internal redirect */
1595             rewritelog(r, 1, "[per-dir %s] internal redirect with %s "
1596                        "[INTERNAL REDIRECT]", dconf->directory, r->filename);
1597             r->filename = apr_pstrcat(r->pool, "redirect:", r->filename, NULL);
1598             r->handler = "redirect-handler";
1599             return OK;
1600         }
1601     }
1602     else {
1603         rewritelog(r, 1, "[per-dir %s] pass through %s", 
1604                    dconf->directory, r->filename);
1605         return DECLINED;
1606     }
1607 }
1608
1609
1610 /*
1611 **
1612 **  Content-Handlers
1613 **
1614 **  [used for redirect support]
1615 **
1616 */
1617
1618 static int handler_redirect(request_rec *r)
1619 {
1620     /* just make sure that we are really meant! */
1621     if (strncmp(r->filename, "redirect:", 9) != 0) {
1622         return DECLINED;
1623     }
1624
1625     /* now do the internal redirect */
1626     ap_internal_redirect(apr_pstrcat(r->pool, r->filename+9,
1627                                     r->args ? "?" : NULL, r->args, NULL), r);
1628
1629     /* and return gracefully */
1630     return OK;
1631 }
1632
1633
1634 /*
1635 ** +-------------------------------------------------------+
1636 ** |                                                       |
1637 ** |                  the rewriting engine
1638 ** |                                                       |
1639 ** +-------------------------------------------------------+
1640 */
1641
1642 /*
1643  *  Apply a complete rule set,
1644  *  i.e. a list of rewrite rules
1645  */
1646 static int apply_rewrite_list(request_rec *r, apr_array_header_t *rewriterules,
1647                               char *perdir)
1648 {
1649     rewriterule_entry *entries;
1650     rewriterule_entry *p;
1651     int i;
1652     int changed;
1653     int rc;
1654     int s;
1655
1656     /*
1657      *  Iterate over all existing rules
1658      */
1659     entries = (rewriterule_entry *)rewriterules->elts;
1660     changed = 0;
1661     loop:
1662     for (i = 0; i < rewriterules->nelts; i++) {
1663         p = &entries[i];
1664
1665         /*
1666          *  Ignore this rule on subrequests if we are explicitly
1667          *  asked to do so or this is a proxy-throughput or a
1668          *  forced redirect rule.
1669          */
1670         if (r->main != NULL &&
1671             (p->flags & RULEFLAG_IGNOREONSUBREQ ||
1672              p->flags & RULEFLAG_PROXY          ||
1673              p->flags & RULEFLAG_FORCEREDIRECT    )) {
1674             continue;
1675         }
1676
1677         /*
1678          *  Apply the current rule.
1679          */
1680         rc = apply_rewrite_rule(r, p, perdir);
1681         if (rc) {
1682             /*
1683              *  Indicate a change if this was not a match-only rule.
1684              */
1685             if (rc != 2) {
1686                 changed = 1;
1687             }
1688
1689             /*
1690              *  Pass-Through Feature (`RewriteRule .. .. [PT]'):
1691              *  Because the Apache 1.x API is very limited we
1692              *  need this hack to pass the rewritten URL to other
1693              *  modules like mod_alias, mod_userdir, etc.
1694              */
1695             if (p->flags & RULEFLAG_PASSTHROUGH) {
1696                 rewritelog(r, 2, "forcing '%s' to get passed through "
1697                            "to next API URI-to-filename handler", r->filename);
1698                 r->filename = apr_pstrcat(r->pool, "passthrough:",
1699                                          r->filename, NULL);
1700                 changed = 1;
1701                 break;
1702             }
1703
1704             /*
1705              *  Rule has the "forbidden" flag set which means that
1706              *  we stop processing and indicate this to the caller.
1707              */
1708             if (p->flags & RULEFLAG_FORBIDDEN) {
1709                 rewritelog(r, 2, "forcing '%s' to be forbidden", r->filename);
1710                 r->filename = apr_pstrcat(r->pool, "forbidden:",
1711                                          r->filename, NULL);
1712                 changed = 1;
1713                 break;
1714             }
1715
1716             /*
1717              *  Rule has the "gone" flag set which means that
1718              *  we stop processing and indicate this to the caller.
1719              */
1720             if (p->flags & RULEFLAG_GONE) {
1721                 rewritelog(r, 2, "forcing '%s' to be gone", r->filename);
1722                 r->filename = apr_pstrcat(r->pool, "gone:", r->filename, NULL);
1723                 changed = 1;
1724                 break;
1725             }
1726
1727             /*
1728              *  Stop processing also on proxy pass-through and
1729              *  last-rule and new-round flags.
1730              */
1731             if (p->flags & RULEFLAG_PROXY) {
1732                 break;
1733             }
1734             if (p->flags & RULEFLAG_LASTRULE) {
1735                 break;
1736             }
1737
1738             /*
1739              *  On "new-round" flag we just start from the top of
1740              *  the rewriting ruleset again.
1741              */
1742             if (p->flags & RULEFLAG_NEWROUND) {
1743                 goto loop;
1744             }
1745
1746             /*
1747              *  If we are forced to skip N next rules, do it now.
1748              */
1749             if (p->skip > 0) {
1750                 s = p->skip;
1751                 while (   i < rewriterules->nelts
1752                        && s > 0) {
1753                     i++;
1754                     p = &entries[i];
1755                     s--;
1756                 }
1757             }
1758         }
1759         else {
1760             /*
1761              *  If current rule is chained with next rule(s),
1762              *  skip all this next rule(s)
1763              */
1764             while (   i < rewriterules->nelts
1765                    && p->flags & RULEFLAG_CHAIN) {
1766                 i++;
1767                 p = &entries[i];
1768             }
1769         }
1770     }
1771     return changed;
1772 }
1773
1774 /*
1775  *  Apply a single(!) rewrite rule
1776  */
1777 static int apply_rewrite_rule(request_rec *r, rewriterule_entry *p,
1778                               char *perdir)
1779 {
1780     char *uri;
1781     char *output;
1782     const char *vary;
1783     char newuri[MAX_STRING_LEN];
1784     regex_t *regexp;
1785     regmatch_t regmatch[MAX_NMATCH];
1786     backrefinfo *briRR = NULL;
1787     backrefinfo *briRC = NULL;
1788     int prefixstrip;
1789     int failed;
1790     apr_array_header_t *rewriteconds;
1791     rewritecond_entry *conds;
1792     rewritecond_entry *c;
1793     int i;
1794     int rc;
1795
1796     /*
1797      *  Initialisation
1798      */
1799     uri     = r->filename;
1800     regexp  = p->regexp;
1801     output  = p->output;
1802
1803     /*
1804      *  Add (perhaps splitted away) PATH_INFO postfix to URL to
1805      *  make sure we really match against the complete URL.
1806      */
1807     if (perdir != NULL && r->path_info != NULL && r->path_info[0] != '\0') {
1808         rewritelog(r, 3, "[per-dir %s] add path info postfix: %s -> %s%s",
1809                    perdir, uri, uri, r->path_info);
1810         uri = apr_pstrcat(r->pool, uri, r->path_info, NULL);
1811     }
1812
1813     /*
1814      *  On per-directory context (.htaccess) strip the location
1815      *  prefix from the URL to make sure patterns apply only to
1816      *  the local part.  Additionally indicate this special
1817      *  threatment in the logfile.
1818      */
1819     prefixstrip = 0;
1820     if (perdir != NULL) {
1821         if (   strlen(uri) >= strlen(perdir)
1822             && strncmp(uri, perdir, strlen(perdir)) == 0) {
1823             rewritelog(r, 3, "[per-dir %s] strip per-dir prefix: %s -> %s",
1824                        perdir, uri, uri+strlen(perdir));
1825             uri = uri+strlen(perdir);
1826             prefixstrip = 1;
1827         }
1828     }
1829
1830     /*
1831      *  Try to match the URI against the RewriteRule pattern
1832      *  and exit immeddiately if it didn't apply.
1833      */
1834     if (perdir == NULL) {
1835         rewritelog(r, 3, "applying pattern '%s' to uri '%s'",
1836                    p->pattern, uri);
1837     }
1838     else {
1839         rewritelog(r, 3, "[per-dir %s] applying pattern '%s' to uri '%s'",
1840                    perdir, p->pattern, uri);
1841     }
1842     rc = (ap_regexec(regexp, uri, regexp->re_nsub+1, regmatch, 0) == 0);
1843     if (! (( rc && !(p->flags & RULEFLAG_NOTMATCH)) ||
1844            (!rc &&  (p->flags & RULEFLAG_NOTMATCH))   ) ) {
1845         return 0;
1846     }
1847
1848     /*
1849      *  Else create the RewriteRule `regsubinfo' structure which
1850      *  holds the substitution information.
1851      */
1852     briRR = (backrefinfo *)apr_palloc(r->pool, sizeof(backrefinfo));
1853     if (!rc && (p->flags & RULEFLAG_NOTMATCH)) {
1854         /*  empty info on negative patterns  */
1855         briRR->source = "";
1856         briRR->nsub   = 0;
1857     }
1858     else {
1859         briRR->source = apr_pstrdup(r->pool, uri);
1860         briRR->nsub   = regexp->re_nsub;
1861         memcpy((void *)(briRR->regmatch), (void *)(regmatch),
1862                sizeof(regmatch));
1863     }
1864
1865     /*
1866      *  Initiallally create the RewriteCond backrefinfo with
1867      *  empty backrefinfo, i.e. not subst parts
1868      *  (this one is adjusted inside apply_rewrite_cond() later!!)
1869      */
1870     briRC = (backrefinfo *)apr_pcalloc(r->pool, sizeof(backrefinfo));
1871     briRC->source = "";
1872     briRC->nsub   = 0;
1873
1874     /*
1875      *  Ok, we already know the pattern has matched, but we now
1876      *  additionally have to check for all existing preconditions
1877      *  (RewriteCond) which have to be also true. We do this at
1878      *  this very late stage to avoid unnessesary checks which
1879      *  would slow down the rewriting engine!!
1880      */
1881     rewriteconds = p->rewriteconds;
1882     conds = (rewritecond_entry *)rewriteconds->elts;
1883     failed = 0;
1884     for (i = 0; i < rewriteconds->nelts; i++) {
1885         c = &conds[i];
1886         rc = apply_rewrite_cond(r, c, perdir, briRR, briRC);
1887         if (c->flags & CONDFLAG_ORNEXT) {
1888             /*
1889              *  The "OR" case
1890              */
1891             if (rc == 0) {
1892                 /*  One condition is false, but another can be
1893                  *  still true, so we have to continue...
1894                  */
1895                 apr_table_unset(r->notes, VARY_KEY_THIS);
1896                 continue;
1897             }
1898             else {
1899                 /*  One true condition is enough in "or" case, so
1900                  *  skip the other conditions which are "ornext"
1901                  *  chained
1902                  */
1903                 while (   i < rewriteconds->nelts
1904                        && c->flags & CONDFLAG_ORNEXT) {
1905                     i++;
1906                     c = &conds[i];
1907                 }
1908                 continue;
1909             }
1910         }
1911         else {
1912             /*
1913              *  The "AND" case, i.e. no "or" flag,
1914              *  so a single failure means total failure.
1915              */
1916             if (rc == 0) {
1917                 failed = 1;
1918                 break;
1919             }
1920         }
1921         vary = apr_table_get(r->notes, VARY_KEY_THIS);
1922         if (vary != NULL) {
1923             apr_table_merge(r->notes, VARY_KEY, vary);
1924             apr_table_unset(r->notes, VARY_KEY_THIS);
1925         }
1926     }
1927     /*  if any condition fails the complete rule fails  */
1928     if (failed) {
1929         apr_table_unset(r->notes, VARY_KEY);
1930         apr_table_unset(r->notes, VARY_KEY_THIS);
1931         return 0;
1932     }
1933
1934     /*
1935      * Regardless of what we do next, we've found a match.  Check to see
1936      * if any of the request header fields were involved, and add them
1937      * to the Vary field of the response.
1938      */
1939     if ((vary = apr_table_get(r->notes, VARY_KEY)) != NULL) {
1940         apr_table_merge(r->headers_out, "Vary", vary);
1941         apr_table_unset(r->notes, VARY_KEY);
1942     }
1943
1944     /*
1945      *  If this is a pure matching rule (`RewriteRule <pat> -')
1946      *  we stop processing and return immediately. The only thing
1947      *  we have not to forget are the environment variables
1948      *  (`RewriteRule <pat> - [E=...]')
1949      */
1950     if (strcmp(output, "-") == 0) {
1951         do_expand_env(r, p->env, briRR, briRC);
1952         if (p->forced_mimetype != NULL) {
1953             if (perdir == NULL) {
1954                 /* In the per-server context we can force the MIME-type
1955                  * the correct way by notifying our MIME-type hook handler
1956                  * to do the job when the MIME-type API stage is reached.
1957                  */
1958                 rewritelog(r, 2, "remember %s to have MIME-type '%s'",
1959                            r->filename, p->forced_mimetype);
1960                 apr_table_setn(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
1961                               p->forced_mimetype);
1962             }
1963             else {
1964                 /* In per-directory context we operate in the Fixup API hook
1965                  * which is after the MIME-type hook, so our MIME-type handler
1966                  * has no chance to set r->content_type. And because we are
1967                  * in the situation where no substitution takes place no
1968                  * sub-request will happen (which could solve the
1969                  * restriction). As a workaround we do it ourself now
1970                  * immediately although this is not strictly API-conforming.
1971                  * But it's the only chance we have...
1972                  */
1973                 rewritelog(r, 1, "[per-dir %s] force %s to have MIME-type "
1974                            "'%s'", perdir, r->filename, p->forced_mimetype);
1975                 r->content_type = p->forced_mimetype;
1976             }
1977         }
1978         return 2;
1979     }
1980
1981     /*
1982      *  Ok, now we finally know all patterns have matched and
1983      *  that there is something to replace, so we create the
1984      *  substitution URL string in `newuri'.
1985      */
1986     do_expand(r, output, newuri, sizeof(newuri), briRR, briRC);
1987     if (perdir == NULL) {
1988         rewritelog(r, 2, "rewrite %s -> %s", uri, newuri);
1989     }
1990     else {
1991         rewritelog(r, 2, "[per-dir %s] rewrite %s -> %s", perdir, uri, newuri);
1992     }
1993
1994     /*
1995      *  Additionally do expansion for the environment variable
1996      *  strings (`RewriteRule .. .. [E=<string>]').
1997      */
1998     do_expand_env(r, p->env, briRR, briRC);
1999
2000     /*
2001      *  Now replace API's knowledge of the current URI:
2002      *  Replace r->filename with the new URI string and split out
2003      *  an on-the-fly generated QUERY_STRING part into r->args
2004      */
2005     r->filename = apr_pstrdup(r->pool, newuri);
2006     splitout_queryargs(r, p->flags & RULEFLAG_QSAPPEND);
2007
2008     /*
2009      *   Again add the previously stripped per-directory location
2010      *   prefix if the new URI is not a new one for this
2011      *   location, i.e. if it's not starting with either a slash
2012      *   or a fully qualified URL scheme.
2013      */
2014     if (prefixstrip && r->filename[0] != '/'
2015         && !is_absolute_uri(r->filename)) {
2016         rewritelog(r, 3, "[per-dir %s] add per-dir prefix: %s -> %s%s",
2017                    perdir, r->filename, perdir, r->filename);
2018         r->filename = apr_pstrcat(r->pool, perdir, r->filename, NULL);
2019     }
2020
2021     /*
2022      *  If this rule is forced for proxy throughput
2023      *  (`RewriteRule ... ... [P]') then emulate mod_proxy's
2024      *  URL-to-filename handler to be sure mod_proxy is triggered
2025      *  for this URL later in the Apache API. But make sure it is
2026      *  a fully-qualified URL. (If not it is qualified with
2027      *  ourself).
2028      */
2029     if (p->flags & RULEFLAG_PROXY) {
2030         fully_qualify_uri(r);
2031         if (perdir == NULL) {
2032             rewritelog(r, 2, "forcing proxy-throughput with %s", r->filename);
2033         }
2034         else {
2035             rewritelog(r, 2, "[per-dir %s] forcing proxy-throughput with %s",
2036                        perdir, r->filename);
2037         }
2038         r->filename = apr_pstrcat(r->pool, "proxy:", r->filename, NULL);
2039         return 1;
2040     }
2041
2042     /*
2043      *  If this rule is explicitly forced for HTTP redirection
2044      *  (`RewriteRule .. .. [R]') then force an external HTTP
2045      *  redirect. But make sure it is a fully-qualified URL. (If
2046      *  not it is qualified with ourself).
2047      */
2048     if (p->flags & RULEFLAG_FORCEREDIRECT) {
2049         fully_qualify_uri(r);
2050         if (perdir == NULL) {
2051             rewritelog(r, 2,
2052                        "explicitly forcing redirect with %s", r->filename);
2053         }
2054         else {
2055             rewritelog(r, 2,
2056                        "[per-dir %s] explicitly forcing redirect with %s",
2057                        perdir, r->filename);
2058         }
2059         r->status = p->forced_responsecode;
2060         return 1;
2061     }
2062
2063     /*
2064      *  Special Rewriting Feature: Self-Reduction
2065      *  We reduce the URL by stripping a possible
2066      *  http[s]://<ourhost>[:<port>] prefix, i.e. a prefix which
2067      *  corresponds to ourself. This is to simplify rewrite maps
2068      *  and to avoid recursion, etc. When this prefix is not a
2069      *  coincidence then the user has to use [R] explicitly (see
2070      *  above).
2071      */
2072     reduce_uri(r);
2073
2074     /*
2075      *  If this rule is still implicitly forced for HTTP
2076      *  redirection (`RewriteRule .. <scheme>://...') then
2077      *  directly force an external HTTP redirect.
2078      */
2079     if (is_absolute_uri(r->filename)) {
2080         if (perdir == NULL) {
2081             rewritelog(r, 2,
2082                        "implicitly forcing redirect (rc=%d) with %s",
2083                        p->forced_responsecode, r->filename);
2084         }
2085         else {
2086             rewritelog(r, 2, "[per-dir %s] implicitly forcing redirect "
2087                        "(rc=%d) with %s", perdir, p->forced_responsecode,
2088                        r->filename);
2089         }
2090         r->status = p->forced_responsecode;
2091         return 1;
2092     }
2093
2094     /*
2095      *  Now we are sure it is not a fully qualified URL.  But
2096      *  there is still one special case left: A local rewrite in
2097      *  per-directory context, i.e. a substitution URL which does
2098      *  not start with a slash. Here we add again the initially
2099      *  stripped per-directory prefix.
2100      */
2101     if (prefixstrip && r->filename[0] != '/') {
2102         rewritelog(r, 3, "[per-dir %s] add per-dir prefix: %s -> %s%s",
2103                    perdir, r->filename, perdir, r->filename);
2104         r->filename = apr_pstrcat(r->pool, perdir, r->filename, NULL);
2105     }
2106
2107     /*
2108      *  Finally we had to remember if a MIME-type should be
2109      *  forced for this URL (`RewriteRule .. .. [T=<type>]')
2110      *  Later in the API processing phase this is forced by our
2111      *  MIME API-hook function. This time its no problem even for
2112      *  the per-directory context (where the MIME-type hook was
2113      *  already processed) because a sub-request happens ;-)
2114      */
2115     if (p->forced_mimetype != NULL) {
2116         apr_table_setn(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
2117                       p->forced_mimetype);
2118         if (perdir == NULL) {
2119             rewritelog(r, 2, "remember %s to have MIME-type '%s'",
2120                        r->filename, p->forced_mimetype);
2121         }
2122         else {
2123             rewritelog(r, 2,
2124                        "[per-dir %s] remember %s to have MIME-type '%s'",
2125                        perdir, r->filename, p->forced_mimetype);
2126         }
2127     }
2128
2129     /*
2130      *  Puuhhhhhhhh... WHAT COMPLICATED STUFF ;_)
2131      *  But now we're done for this particular rule.
2132      */
2133     return 1;
2134 }
2135
2136 static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p,
2137                               char *perdir, backrefinfo *briRR,
2138                               backrefinfo *briRC)
2139 {
2140     char input[MAX_STRING_LEN];
2141     apr_finfo_t sb;
2142     request_rec *rsub;
2143     regmatch_t regmatch[MAX_NMATCH];
2144     int rc;
2145
2146     /*
2147      *   Construct the string we match against
2148      */
2149
2150     do_expand(r, p->input, input, sizeof(input), briRR, briRC);
2151
2152     /*
2153      *   Apply the patterns
2154      */
2155
2156     rc = 0;
2157     if (strcmp(p->pattern, "-f") == 0) {
2158         if (apr_stat(&sb, input, r->pool) == APR_SUCCESS) {
2159             if (sb.filetype == APR_REG) {
2160                 rc = 1;
2161             }
2162         }
2163     }
2164     else if (strcmp(p->pattern, "-s") == 0) {
2165         if (apr_stat(&sb, input, r->pool) == APR_SUCCESS) {
2166             if ((sb.filetype == APR_REG) && sb.size > 0) {
2167                 rc = 1;
2168             }
2169         }
2170     }
2171     else if (strcmp(p->pattern, "-l") == 0) {
2172 #if !defined(OS2) && !defined(WIN32)
2173         if (apr_lstat(&sb, input, r->pool) == APR_SUCCESS) {
2174             if (sb.filetype == APR_LNK) {
2175                 rc = 1;
2176             }
2177         }
2178 #endif
2179     }
2180     else if (strcmp(p->pattern, "-d") == 0) {
2181         if (apr_stat(&sb, input, r->pool) == APR_SUCCESS) {
2182             if (sb.filetype == APR_DIR) {
2183                 rc = 1;
2184             }
2185         }
2186     }
2187     else if (strcmp(p->pattern, "-U") == 0) {
2188         /* avoid infinite subrequest recursion */
2189         if (strlen(input) > 0 && subreq_ok(r)) {
2190
2191             /* run a URI-based subrequest */
2192             rsub = ap_sub_req_lookup_uri(input, r);
2193
2194             /* URI exists for any result up to 3xx, redirects allowed */
2195             if (rsub->status < 400)
2196                 rc = 1;
2197
2198             /* log it */
2199             rewritelog(r, 5, "RewriteCond URI (-U) check: "
2200                        "path=%s -> status=%d", input, rsub->status);
2201
2202             /* cleanup by destroying the subrequest */
2203             ap_destroy_sub_req(rsub);
2204         }
2205     }
2206     else if (strcmp(p->pattern, "-F") == 0) {
2207         /* avoid infinite subrequest recursion */
2208         if (strlen(input) > 0 && subreq_ok(r)) {
2209
2210             /* process a file-based subrequest:
2211              * this differs from -U in that no path translation is done.
2212              */
2213             rsub = ap_sub_req_lookup_file(input, r);
2214
2215             /* file exists for any result up to 2xx, no redirects */
2216             if (rsub->status < 300 &&
2217                 /* double-check that file exists since default result is 200 */
2218                 apr_stat(&sb, rsub->filename, r->pool) == APR_SUCCESS) {
2219                 rc = 1;
2220             }
2221
2222             /* log it */
2223             rewritelog(r, 5, "RewriteCond file (-F) check: path=%s "
2224                        "-> file=%s status=%d", input, rsub->filename, 
2225                        rsub->status);
2226
2227             /* cleanup by destroying the subrequest */
2228             ap_destroy_sub_req(rsub);
2229         }
2230     }
2231     else if (strlen(p->pattern) > 1 && *(p->pattern) == '>') {
2232         rc = (compare_lexicography(input, p->pattern+1) == 1 ? 1 : 0);
2233     }
2234     else if (strlen(p->pattern) > 1 && *(p->pattern) == '<') {
2235         rc = (compare_lexicography(input, p->pattern+1) == -1 ? 1 : 0);
2236     }
2237     else if (strlen(p->pattern) > 1 && *(p->pattern) == '=') {
2238         if (strcmp(p->pattern+1, "\"\"") == 0) {
2239             rc = (*input == '\0');
2240         }
2241         else {
2242             rc = (strcmp(input, p->pattern+1) == 0 ? 1 : 0);
2243         }
2244     }
2245     else {
2246         /* it is really a regexp pattern, so apply it */
2247         rc = (ap_regexec(p->regexp, input,
2248                          p->regexp->re_nsub+1, regmatch,0) == 0);
2249
2250         /* if it isn't a negated pattern and really matched
2251            we update the passed-through regex subst info structure */
2252         if (rc && !(p->flags & CONDFLAG_NOTMATCH)) {
2253             briRC->source = apr_pstrdup(r->pool, input);
2254             briRC->nsub   = p->regexp->re_nsub;
2255             memcpy((void *)(briRC->regmatch), (void *)(regmatch),
2256                    sizeof(regmatch));
2257         }
2258     }
2259
2260     /* if this is a non-matching regexp, just negate the result */
2261     if (p->flags & CONDFLAG_NOTMATCH) {
2262         rc = !rc;
2263     }
2264
2265     rewritelog(r, 4, "RewriteCond: input='%s' pattern='%s%s' => %s",
2266                input, (p->flags & CONDFLAG_NOTMATCH ? "!" : ""),
2267                p->pattern, rc ? "matched" : "not-matched");
2268
2269     /* end just return the result */
2270     return rc;
2271 }
2272
2273
2274 /*
2275 ** +-------------------------------------------------------+
2276 ** |                                                       |
2277 ** |              URL transformation functions
2278 ** |                                                       |
2279 ** +-------------------------------------------------------+
2280 */
2281
2282
2283 /*
2284 **
2285 **  perform all the expansions on the input string
2286 **  leaving the result in the supplied buffer
2287 **
2288 */
2289
2290 static void do_expand(request_rec *r, char *input, char *buffer, int nbuf,
2291                        backrefinfo *briRR, backrefinfo *briRC)
2292 {
2293     char *inp, *outp;
2294     size_t span, space;
2295
2296     /*
2297      * for security reasons this expansion must be perfomed in a
2298      * single pass, otherwise an attacker can arrange for the result
2299      * of an earlier expansion to include expansion specifiers that
2300      * are interpreted by a later expansion, producing results that
2301      * were not intended by the administrator.
2302      */
2303
2304     inp = input;
2305     outp = buffer;
2306     space = nbuf - 1; /* room for '\0' */
2307
2308     for (;;) {
2309         span = strcspn(inp, "$%");
2310         if (span > space) {
2311             span = space;
2312         }
2313         memcpy(outp, inp, span);
2314         inp += span;
2315         outp += span;
2316         space -= span;
2317         if (space == 0 || *inp == '\0') {
2318             break;
2319         }
2320         /* now we have a '$' or a '%' */
2321         if (inp[1] == '{') {
2322             char *endp;
2323             endp = find_closing_bracket(inp+2, '{', '}');
2324             if (endp == NULL) {
2325                 goto skip;
2326             }
2327             *endp = '\0';
2328             if (inp[0] == '$') {
2329                 /* ${...} map lookup expansion */
2330                 /*
2331                  * To make rewrite maps useful the lookup key and
2332                  * default values must be expanded, so we make
2333                  * recursive calls to do the work. For security
2334                  * reasons we must never expand a string that includes
2335                  * verbatim data from the network. The recursion here
2336                  * isn't a problem because the result of expansion is
2337                  * only passed to lookup_map() so it cannot be
2338                  * re-expanded, only re-looked-up. Another way of
2339                  * looking at it is that the recursion is entirely
2340                  * driven by the syntax of the nested curly brackets.
2341                  */
2342                 char *key, *dflt, *result;
2343                 char xkey[MAX_STRING_LEN];
2344                 char xdflt[MAX_STRING_LEN];
2345                 char *empty = "";
2346                 key = strchr(inp, ':');
2347                 if (key == NULL) {
2348                     goto skip;
2349                 }
2350                 *key++ = '\0';
2351                 dflt = strchr(key, '|');
2352                 if (dflt == NULL) {
2353                     dflt = empty;
2354                 }
2355                 else
2356                     *dflt++ = '\0';
2357                 }
2358                 do_expand(r, key,  xkey,  sizeof(xkey),  briRR, briRC);
2359                 do_expand(r, dflt, xdflt, sizeof(xdflt), briRR, briRC);
2360                 result = lookup_map(r, inp+2, xkey);
2361                 if (result == NULL) {
2362                     result = xdflt;
2363                 }
2364                 span = apr_cpystrn(outp, result, space) - outp;
2365                 key[-1] = ':';
2366                 if (dflt != empty) {
2367                     dflt[-1] = '|';
2368                 }
2369             }
2370             else if (inp[0] == '%') {
2371                 /* %{...} variable lookup expansion */
2372                 span = apr_cpystrn(outp, lookup_variable(r, inp+2), space) - outp;
2373             }
2374             else {
2375                 span = 0;
2376             }
2377             *endp = '}';
2378             inp = endp+1;
2379             outp += span;
2380             space -= span;
2381             continue;
2382         }
2383         else if (apr_isdigit(inp[1])) {
2384             int n = inp[1] - '0';
2385             backrefinfo *bri = NULL;
2386             if (inp[0] == '$') {
2387                 /* $N RewriteRule regexp backref expansion */
2388                 bri = briRR;
2389             }
2390             else if (inp[0] == '%') {
2391                 /* %N RewriteCond regexp backref expansion */
2392                 bri = briRC;
2393             }
2394             /* see ap_pregsub() in src/main/util.c */
2395             if (bri && n <= bri->nsub &&
2396                 bri->regmatch[n].rm_eo > bri->regmatch[n].rm_so) {
2397                 span = bri->regmatch[n].rm_eo - bri->regmatch[n].rm_so;
2398                 if (span > space) {
2399                     span = space;
2400                 }
2401                 memcpy(outp, bri->source + bri->regmatch[n].rm_so, span);
2402                 outp += span;
2403                 space -= span;
2404             }
2405             inp += 2;
2406             continue;
2407         }
2408     skip:
2409         *outp++ = *inp++;
2410         space--;
2411     }
2412     *outp++ = '\0';
2413 }
2414
2415
2416 /*
2417 **
2418 **  perform all the expansions on the environment variables
2419 **
2420 */
2421
2422 static void do_expand_env(request_rec *r, char *env[],
2423                           backrefinfo *briRR, backrefinfo *briRC)
2424 {
2425     int i;
2426     char buf[MAX_STRING_LEN];
2427
2428     for (i = 0; env[i] != NULL; i++) {
2429         do_expand(r, env[i], buf, sizeof(buf), briRR, briRC);
2430         add_env_variable(r, buf);
2431     }
2432 }
2433
2434
2435 /*
2436 **
2437 **  split out a QUERY_STRING part from
2438 **  the current URI string
2439 **
2440 */
2441
2442 static void splitout_queryargs(request_rec *r, int qsappend)
2443 {
2444     char *q;
2445     char *olduri;
2446
2447     q = strchr(r->filename, '?');
2448     if (q != NULL) {
2449         olduri = apr_pstrdup(r->pool, r->filename);
2450         *q++ = '\0';
2451         if (qsappend) {
2452             r->args = apr_pstrcat(r->pool, q, "&", r->args, NULL);
2453         }
2454         else {
2455             r->args = apr_pstrdup(r->pool, q);
2456         }
2457         if (strlen(r->args) == 0) {
2458             r->args = NULL;
2459             rewritelog(r, 3, "split uri=%s -> uri=%s, args=<none>", olduri,
2460                        r->filename);
2461         }
2462         else {
2463             if (r->args[strlen(r->args)-1] == '&') {
2464                 r->args[strlen(r->args)-1] = '\0';
2465             }
2466             rewritelog(r, 3, "split uri=%s -> uri=%s, args=%s", olduri,
2467                        r->filename, r->args);
2468         }
2469     }
2470     return;
2471 }
2472
2473
2474 /*
2475 **
2476 **  strip 'http[s]://ourhost/' from URI
2477 **
2478 */
2479
2480 static void reduce_uri(request_rec *r)
2481 {
2482     char *cp;
2483     unsigned short port;
2484     char *portp;
2485     char *hostp;
2486     char *url;
2487     char c;
2488     char host[LONG_STRING_LEN];
2489     char buf[MAX_STRING_LEN];
2490     char *olduri;
2491     int l;
2492
2493     cp = (char *)ap_http_method(r);
2494     l  = strlen(cp);
2495     if (   strlen(r->filename) > l+3 
2496         && strncasecmp(r->filename, cp, l) == 0
2497         && r->filename[l]   == ':'
2498         && r->filename[l+1] == '/'
2499         && r->filename[l+2] == '/'             ) {
2500         /* there was really a rewrite to a remote path */
2501
2502         olduri = apr_pstrdup(r->pool, r->filename); /* save for logging */
2503
2504         /* cut the hostname and port out of the URI */
2505         apr_cpystrn(buf, r->filename+(l+3), sizeof(buf));
2506         hostp = buf;
2507         for (cp = hostp; *cp != '\0' && *cp != '/' && *cp != ':'; cp++)
2508             ;
2509         if (*cp == ':') {
2510             /* set host */
2511             *cp++ = '\0';
2512             apr_cpystrn(host, hostp, sizeof(host));
2513             /* set port */
2514             portp = cp;
2515             for (; *cp != '\0' && *cp != '/'; cp++)
2516                 ;
2517             c = *cp;
2518             *cp = '\0';
2519             port = atoi(portp);
2520             *cp = c;
2521             /* set remaining url */
2522             url = cp;
2523         }
2524         else if (*cp == '/') {
2525             /* set host */
2526             *cp = '\0';
2527             apr_cpystrn(host, hostp, sizeof(host));
2528             *cp = '/';
2529             /* set port */
2530             port = ap_default_port(r);
2531             /* set remaining url */
2532             url = cp;
2533         }
2534         else {
2535             /* set host */
2536             apr_cpystrn(host, hostp, sizeof(host));
2537             /* set port */
2538             port = ap_default_port(r);
2539             /* set remaining url */
2540             url = "/";
2541         }
2542
2543         /* now check whether we could reduce it to a local path... */
2544         if (ap_matches_request_vhost(r, host, port)) {
2545             /* this is our host, so only the URL remains */
2546             r->filename = apr_pstrdup(r->pool, url);
2547             rewritelog(r, 3, "reduce %s -> %s", olduri, r->filename);
2548         }
2549     }
2550     return;
2551 }
2552
2553
2554 /*
2555 **
2556 **  add 'http[s]://ourhost[:ourport]/' to URI
2557 **  if URI is still not fully qualified
2558 **
2559 */
2560
2561 static void fully_qualify_uri(request_rec *r)
2562 {
2563     char buf[32];
2564     const char *thisserver;
2565     char *thisport;
2566     int port;
2567
2568     if (!is_absolute_uri(r->filename)) {
2569
2570         thisserver = ap_get_server_name(r);
2571         port = ap_get_server_port(r);
2572         if (ap_is_default_port(port,r)) {
2573             thisport = "";
2574         }
2575         else {
2576             apr_snprintf(buf, sizeof(buf), ":%u", port);
2577             thisport = buf;
2578         }
2579
2580         if (r->filename[0] == '/') {
2581             r->filename = apr_psprintf(r->pool, "%s://%s%s%s",
2582                                       ap_http_method(r), thisserver,
2583                                       thisport, r->filename);
2584         }
2585         else {
2586             r->filename = apr_psprintf(r->pool, "%s://%s%s/%s",
2587                                       ap_http_method(r), thisserver,
2588                                       thisport, r->filename);
2589         }
2590     }
2591     return;
2592 }
2593
2594
2595 /*
2596 **
2597 **  return non-zero if the URI is absolute (includes a scheme etc.)
2598 **
2599 */
2600
2601 static int is_absolute_uri(char *uri)
2602 {
2603     int i = strlen(uri);
2604     if (   (i > 7 && strncasecmp(uri, "http://",   7) == 0)
2605         || (i > 8 && strncasecmp(uri, "https://",  8) == 0)
2606         || (i > 9 && strncasecmp(uri, "gopher://", 9) == 0)
2607         || (i > 6 && strncasecmp(uri, "ftp://",    6) == 0)
2608         || (i > 5 && strncasecmp(uri, "ldap:",     5) == 0)
2609         || (i > 5 && strncasecmp(uri, "news:",     5) == 0)
2610         || (i > 7 && strncasecmp(uri, "mailto:",   7) == 0) ) {
2611         return 1;
2612     }
2613     else {
2614         return 0;
2615     }
2616 }
2617
2618
2619 /*
2620 **
2621 **  Expand tilde-paths (/~user) through
2622 **  Unix /etc/passwd database information
2623 **
2624 */
2625 #if !defined(WIN32) && !defined(NETWARE)
2626 static char *expand_tildepaths(request_rec *r, char *uri)
2627 {
2628     char user[LONG_STRING_LEN];
2629     struct passwd *pw;
2630     char *newuri;
2631     int i, j;
2632
2633     newuri = uri;
2634     if (uri != NULL && strlen(uri) > 2 && uri[0] == '/' && uri[1] == '~') {
2635         /* cut out the username */
2636         for (j = 0, i = 2; j < sizeof(user)-1
2637                && uri[i] != '\0'
2638                && uri[i] != '/'  ; ) {
2639             user[j++] = uri[i++];
2640         }
2641         user[j] = '\0';
2642
2643         /* lookup username in systems passwd file */
2644         if ((pw = getpwnam(user)) != NULL) {
2645             /* ok, user was found, so expand the ~user string */
2646             if (uri[i] != '\0') {
2647                 /* ~user/anything...  has to be expanded */
2648                 if (pw->pw_dir[strlen(pw->pw_dir)-1] == '/') {
2649                     pw->pw_dir[strlen(pw->pw_dir)-1] = '\0';
2650                 }
2651                 newuri = apr_pstrcat(r->pool, pw->pw_dir, uri+i, NULL);
2652             }
2653             else {
2654                 /* only ~user has to be expanded */
2655                 newuri = apr_pstrdup(r->pool, pw->pw_dir);
2656             }
2657         }
2658     }
2659     return newuri;
2660 }
2661 #endif
2662
2663
2664
2665 /*
2666 ** +-------------------------------------------------------+
2667 ** |                                                       |
2668 ** |              DBM hashfile support
2669 ** |                                                       |
2670 ** +-------------------------------------------------------+
2671 */
2672
2673
2674 static char *lookup_map(request_rec *r, char *name, char *key)
2675 {
2676     void *sconf;
2677     rewrite_server_conf *conf;
2678     apr_array_header_t *rewritemaps;
2679     rewritemap_entry *entries;
2680     rewritemap_entry *s;
2681     char *value;
2682     apr_finfo_t st;
2683     apr_status_t rv;
2684     int i;
2685
2686     /* get map configuration */
2687     sconf = r->server->module_config;
2688     conf  = (rewrite_server_conf *)ap_get_module_config(sconf, 
2689                                                         &rewrite_module);
2690     rewritemaps = conf->rewritemaps;
2691
2692     entries = (rewritemap_entry *)rewritemaps->elts;
2693     for (i = 0; i < rewritemaps->nelts; i++) {
2694         s = &entries[i];
2695         if (strcmp(s->name, name) == 0) {
2696             if (s->type == MAPTYPE_TXT) {
2697                 if ((rv = apr_stat(&st, s->checkfile, r->pool)) != APR_SUCCESS) {
2698                     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
2699                                  "mod_rewrite: can't access text RewriteMap "
2700                                  "file %s", s->checkfile);
2701                     rewritelog(r, 1, "can't open RewriteMap file, "
2702                                "see error log");
2703                     return NULL;
2704                 }
2705                 value = get_cache_string(cachep, s->name, CACHEMODE_TS,
2706                                          st.mtime, key);
2707                 if (value == NULL) {
2708                     rewritelog(r, 6, "cache lookup FAILED, forcing new "
2709                                "map lookup");
2710                     if ((value =
2711                          lookup_map_txtfile(r, s->datafile, key)) != NULL) {
2712                         rewritelog(r, 5, "map lookup OK: map=%s key=%s[txt] "
2713                                    "-> val=%s", s->name, key, value);
2714                         set_cache_string(cachep, s->name, CACHEMODE_TS,
2715                                          st.mtime, key, value);
2716                         return value;
2717                     }
2718                     else {
2719                         rewritelog(r, 5, "map lookup FAILED: map=%s[txt] "
2720                                    "key=%s", s->name, key);
2721                         set_cache_string(cachep, s->name, CACHEMODE_TS,
2722                                          st.mtime, key, "");
2723                         return NULL;
2724                     }
2725                 }
2726                 else {
2727                     rewritelog(r, 5, "cache lookup OK: map=%s[txt] key=%s "
2728                                "-> val=%s", s->name, key, value);
2729                     return value[0] != '\0' ? value : NULL;
2730                 }
2731             }
2732             else if (s->type == MAPTYPE_DBM) {
2733 #ifndef NO_DBM_REWRITEMAP
2734                 if ((rv = apr_stat(&st, s->checkfile, r->pool)) != APR_SUCCESS) {
2735                     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
2736                                  "mod_rewrite: can't access DBM RewriteMap "
2737                                  "file %s", s->checkfile);
2738                     rewritelog(r, 1, "can't open DBM RewriteMap file, "
2739                                "see error log");
2740                     return NULL;
2741                 }
2742                 value = get_cache_string(cachep, s->name, CACHEMODE_TS,
2743                                          st.mtime, key);
2744                 if (value == NULL) {
2745                     rewritelog(r, 6,
2746                                "cache lookup FAILED, forcing new map lookup");
2747                     if ((value =
2748                          lookup_map_dbmfile(r, s->datafile, key)) != NULL) {
2749                         rewritelog(r, 5, "map lookup OK: map=%s[dbm] key=%s "
2750                                    "-> val=%s", s->name, key, value);
2751                         set_cache_string(cachep, s->name, CACHEMODE_TS,
2752                                          st.mtime, key, value);
2753                         return value;
2754                     }
2755                     else {
2756                         rewritelog(r, 5, "map lookup FAILED: map=%s[dbm] "
2757                                    "key=%s", s->name, key);
2758                         set_cache_string(cachep, s->name, CACHEMODE_TS,
2759                                          st.mtime, key, "");
2760                         return NULL;
2761                     }
2762                 }
2763                 else {
2764                     rewritelog(r, 5, "cache lookup OK: map=%s[dbm] key=%s "
2765                                "-> val=%s", s->name, key, value);
2766                     return value[0] != '\0' ? value : NULL;
2767                 }
2768 #else
2769                 return NULL;
2770 #endif
2771             }
2772             else if (s->type == MAPTYPE_PRG) {
2773                 if ((value =
2774                      lookup_map_program(r, s->fpin, s->fpout, key)) != NULL) {
2775                     rewritelog(r, 5, "map lookup OK: map=%s key=%s -> val=%s",
2776                                s->name, key, value);
2777                     return value;
2778                 }
2779                 else {
2780                     rewritelog(r, 5, "map lookup FAILED: map=%s key=%s",
2781                                s->name, key);
2782                 }
2783             }
2784             else if (s->type == MAPTYPE_INT) {
2785                 if ((value = lookup_map_internal(r, s->func, key)) != NULL) {
2786                     rewritelog(r, 5, "map lookup OK: map=%s key=%s -> val=%s",
2787                                s->name, key, value);
2788                     return value;
2789                 }
2790                 else {
2791                     rewritelog(r, 5, "map lookup FAILED: map=%s key=%s",
2792                                s->name, key);
2793                 }
2794             }
2795             else if (s->type == MAPTYPE_RND) {
2796                 if ((rv = apr_stat(&st, s->checkfile, r->pool)) != APR_SUCCESS) {
2797                     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
2798                                  "mod_rewrite: can't access text RewriteMap "
2799                                  "file %s", s->checkfile);
2800                     rewritelog(r, 1, "can't open RewriteMap file, "
2801                                "see error log");
2802                     return NULL;
2803                 }
2804                 value = get_cache_string(cachep, s->name, CACHEMODE_TS,
2805                                          st.mtime, key);
2806                 if (value == NULL) {
2807                     rewritelog(r, 6, "cache lookup FAILED, forcing new "
2808                                "map lookup");
2809                     if ((value =
2810                          lookup_map_txtfile(r, s->datafile, key)) != NULL) {
2811                         rewritelog(r, 5, "map lookup OK: map=%s key=%s[txt] "
2812                                    "-> val=%s", s->name, key, value);
2813                         set_cache_string(cachep, s->name, CACHEMODE_TS,
2814                                          st.mtime, key, value);
2815                     }
2816                     else {
2817                         rewritelog(r, 5, "map lookup FAILED: map=%s[txt] "
2818                                    "key=%s", s->name, key);
2819                         set_cache_string(cachep, s->name, CACHEMODE_TS,
2820                                          st.mtime, key, "");
2821                         return NULL;
2822                     }
2823                 }
2824                 else {
2825                     rewritelog(r, 5, "cache lookup OK: map=%s[txt] key=%s "
2826                                "-> val=%s", s->name, key, value);
2827                 }
2828                 if (value[0] != '\0') {
2829                    value = select_random_value_part(r, value);
2830                    rewritelog(r, 5, "randomly choosen the subvalue `%s'", value);
2831                 }
2832                 else {
2833                     value = NULL;
2834                 }
2835                 return value;
2836             }
2837         }
2838     }
2839     return NULL;
2840 }
2841
2842 static char *lookup_map_txtfile(request_rec *r, const char *file, char *key)
2843 {
2844     apr_file_t *fp = NULL;
2845     apr_status_t rc;
2846     char line[1024];
2847     char *value = NULL;
2848     char *cpT;
2849     size_t skip;
2850     char *curkey;
2851     char *curval;
2852
2853     rc = apr_open(&fp, file, APR_READ, APR_OS_DEFAULT, r->pool);
2854     if (rc != APR_SUCCESS) {
2855        return NULL;
2856     }
2857
2858     while (apr_fgets(line, sizeof(line), fp) == APR_SUCCESS) {
2859         if (line[0] == '#')
2860             continue; /* ignore comments */
2861         cpT = line;
2862         curkey = cpT;
2863         skip = strcspn(cpT," \t\r\n");
2864         if (skip == 0)
2865             continue; /* ignore lines that start with a space, tab, CR, or LF */
2866         cpT += skip;
2867         *cpT = '\0';
2868         if (strcmp(curkey, key) != 0)
2869             continue; /* key does not match... */
2870             
2871         /* found a matching key; now extract and return the value */
2872         ++cpT;
2873         skip = strspn(cpT, " \t\r\n");
2874         cpT += skip;
2875         curval = cpT;
2876         skip = strcspn(cpT, " \t\r\n");
2877         if (skip == 0)
2878             continue; /* no value... */
2879         cpT += skip;
2880         *cpT = '\0';
2881         value = apr_pstrdup(r->pool, curval);
2882         break;
2883     }
2884     apr_close(fp);
2885     return value;
2886 }
2887
2888 #ifndef NO_DBM_REWRITEMAP
2889 static char *lookup_map_dbmfile(request_rec *r, const char *file, char *key)
2890 {
2891     DBM *dbmfp = NULL;
2892     datum dbmkey;
2893     datum dbmval;
2894     char *value = NULL;
2895     char buf[MAX_STRING_LEN];
2896
2897     dbmkey.dptr  = key;
2898     dbmkey.dsize = strlen(key);
2899     if ((dbmfp = dbm_open(file, O_RDONLY, 0666)) != NULL) {
2900         dbmval = dbm_fetch(dbmfp, dbmkey);
2901         if (dbmval.dptr != NULL) {
2902             memcpy(buf, dbmval.dptr, 
2903                    dbmval.dsize < sizeof(buf)-1 ? 
2904                    dbmval.dsize : sizeof(buf)-1  );
2905             buf[dbmval.dsize] = '\0';
2906             value = apr_pstrdup(r->pool, buf);
2907         }
2908         dbm_close(dbmfp);
2909     }
2910     return value;
2911 }
2912 #endif
2913
2914 static char *lookup_map_program(request_rec *r, apr_file_t *fpin,
2915                                 apr_file_t *fpout, char *key)
2916 {
2917     char buf[LONG_STRING_LEN];
2918     char c;
2919     int i;
2920     apr_ssize_t nbytes;
2921
2922 #ifndef NO_WRITEV
2923     struct iovec iova[2];
2924     apr_size_t niov;
2925 #endif
2926
2927     /* when `RewriteEngine off' was used in the per-server
2928      * context then the rewritemap-programs were not spawned.
2929      * In this case using such a map (usually in per-dir context)
2930      * is useless because it is not available.
2931      */
2932     if (fpin == NULL || fpout == NULL) {
2933         return NULL;
2934     }
2935
2936     /* take the lock */
2937
2938     if (rewrite_mapr_lock) {
2939         apr_lock(rewrite_mapr_lock);
2940     }
2941
2942     /* write out the request key */
2943 #ifdef NO_WRITEV
2944     nbytes = strlen(key);
2945     apr_write(fpin, key, &nbytes);
2946     nbytes = 1;
2947     apr_write(fpin, "\n", &nbytes);
2948 #else
2949     iova[0].iov_base = key;
2950     iova[0].iov_len = strlen(key);
2951     iova[1].iov_base = "\n";
2952     iova[1].iov_len = 1;
2953
2954     niov = 2;
2955     apr_writev(fpin, iova, niov, &nbytes);
2956 #endif
2957
2958     /* read in the response value */
2959     i = 0;
2960     nbytes = 1;
2961     apr_read(fpout, &c, &nbytes);
2962     while (nbytes == 1 && (i < LONG_STRING_LEN-1)) {
2963         if (c == '\n') {
2964             break;
2965         }
2966         buf[i++] = c;
2967
2968         apr_read(fpout, &c, &nbytes);
2969     }
2970     buf[i] = '\0';
2971
2972     /* give the lock back */
2973     if (rewrite_mapr_lock) {
2974         apr_unlock(rewrite_mapr_lock);
2975     }
2976
2977     if (strcasecmp(buf, "NULL") == 0) {
2978         return NULL;
2979     }
2980     else {
2981         return apr_pstrdup(r->pool, buf);
2982     }
2983 }
2984
2985 static char *lookup_map_internal(request_rec *r,
2986                                  char *(*func)(request_rec *, char *),
2987                                  char *key)
2988 {
2989     /* currently we just let the function convert
2990        the key to a corresponding value */
2991     return func(r, key);
2992 }
2993
2994 static char *rewrite_mapfunc_toupper(request_rec *r, char *key)
2995 {
2996     char *value, *cp;
2997
2998     for (cp = value = apr_pstrdup(r->pool, key); cp != NULL && *cp != '\0';
2999          cp++) {
3000         *cp = apr_toupper(*cp);
3001     }
3002     return value;
3003 }
3004
3005 static char *rewrite_mapfunc_tolower(request_rec *r, char *key)
3006 {
3007     char *value, *cp;
3008
3009     for (cp = value = apr_pstrdup(r->pool, key); cp != NULL && *cp != '\0';
3010          cp++) {
3011         *cp = apr_tolower(*cp);
3012     }
3013     return value;
3014 }
3015
3016 static char *rewrite_mapfunc_escape(request_rec *r, char *key)
3017 {
3018     char *value;
3019
3020     value = ap_escape_uri(r->pool, key);
3021     return value;
3022 }
3023
3024 static char *rewrite_mapfunc_unescape(request_rec *r, char *key)
3025 {
3026     char *value;
3027
3028     value = apr_pstrdup(r->pool, key);
3029     ap_unescape_url(value);
3030     return value;
3031 }
3032
3033 static int rewrite_rand_init_done = 0;
3034
3035 static void rewrite_rand_init(void)
3036 {
3037     if (!rewrite_rand_init_done) {
3038         srand((unsigned)(getpid()));
3039         rewrite_rand_init_done = 1;
3040     }
3041     return;
3042 }
3043
3044 static int rewrite_rand(int l, int h)
3045 {
3046     rewrite_rand_init();
3047
3048     /* Get [0,1) and then scale to the appropriate range. Note that using
3049      * a floating point value ensures that we use all bits of the rand()
3050      * result. Doing an integer modulus would only use the lower-order bits
3051      * which may not be as uniformly random.
3052      */
3053     return ((double)(rand() % RAND_MAX) / RAND_MAX) * (h - l + 1) + l;
3054 }
3055
3056 static char *select_random_value_part(request_rec *r, char *value)
3057 {
3058     char *buf;
3059     int n, i, k;
3060
3061     /*  count number of distinct values  */
3062     for (n = 1, i = 0; value[i] != '\0'; i++) {
3063         if (value[i] == '|') {
3064             n++;
3065         }
3066     }
3067
3068     /*  when only one value we have no option to choose  */
3069     if (n == 1) {
3070         return value;
3071     }
3072
3073     /*  else randomly select one  */
3074     k = rewrite_rand(1, n);
3075
3076     /*  and grep it out  */
3077     for (n = 1, i = 0; value[i] != '\0'; i++) {
3078         if (n == k) {
3079             break;
3080         }
3081         if (value[i] == '|') {
3082             n++;
3083         }
3084     }
3085     buf = apr_pstrdup(r->pool, &value[i]);
3086     for (i = 0; buf[i] != '\0' && buf[i] != '|'; i++)
3087         ;
3088     buf[i] = '\0';
3089     return buf;
3090 }
3091
3092
3093 /*
3094 ** +-------------------------------------------------------+
3095 ** |                                                       |
3096 ** |              rewriting logfile support
3097 ** |                                                       |
3098 ** +-------------------------------------------------------+
3099 */
3100
3101
3102 static void open_rewritelog(server_rec *s, apr_pool_t *p)
3103 {
3104     rewrite_server_conf *conf;
3105     const char *fname;
3106     apr_status_t rc;
3107     piped_log *pl;
3108     int    rewritelog_flags = ( APR_WRITE | APR_APPEND | APR_CREATE );
3109     mode_t rewritelog_mode  = ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD );
3110
3111     conf = ap_get_module_config(s->module_config, &rewrite_module);
3112
3113     if (conf->rewritelogfile == NULL) {
3114         return;
3115     }
3116     if (*(conf->rewritelogfile) == '\0') {
3117         return;
3118     }
3119     if (conf->rewritelogfp != NULL) {
3120         return; /* virtual log shared w/ main server */
3121     }
3122
3123     fname = ap_server_root_relative(p, conf->rewritelogfile);
3124
3125     if (*conf->rewritelogfile == '|') {
3126         if ((pl = ap_open_piped_log(p, conf->rewritelogfile+1)) == NULL) {
3127             ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, s, 
3128                          "mod_rewrite: could not open reliable pipe "
3129                          "to RewriteLog filter %s", conf->rewritelogfile+1);
3130             exit(1);
3131         }
3132         conf->rewritelogfp = ap_piped_log_write_fd(pl);
3133     }
3134     else if (*conf->rewritelogfile != '\0') {
3135         rc = apr_open(&conf->rewritelogfp, fname, rewritelog_flags, rewritelog_mode, p);
3136         if (rc != APR_SUCCESS)  {
3137             ap_log_error(APLOG_MARK, APLOG_ERR, rc, s, 
3138                          "mod_rewrite: could not open RewriteLog "
3139                          "file %s", fname);
3140             exit(1);
3141         }
3142     }
3143     return;
3144 }
3145
3146 static void rewritelog(request_rec *r, int level, const char *text, ...)
3147 {
3148     rewrite_server_conf *conf;
3149     conn_rec *conn;
3150     char *str1;
3151     char str2[512];
3152     char str3[1024];
3153     char type[20];
3154     char redir[20];
3155     va_list ap;
3156     int i;
3157     apr_ssize_t nbytes;
3158     request_rec *req;
3159     char *ruser;
3160     const char *rhost;
3161
3162     va_start(ap, text);
3163     conf = ap_get_module_config(r->server->module_config, &rewrite_module);
3164     conn = r->connection;
3165
3166     if (conf->rewritelogfp == NULL) {
3167         return;
3168     }
3169     if (conf->rewritelogfile == NULL) {
3170         return;
3171     }
3172     if (*(conf->rewritelogfile) == '\0') {
3173         return;
3174     }
3175
3176     if (level > conf->rewriteloglevel) {
3177         return;
3178     }
3179
3180     if (r->user == NULL) {
3181         ruser = "-";
3182     }
3183     else if (strlen(r->user) != 0) {
3184         ruser = r->user;
3185     }
3186     else {
3187         ruser = "\"\"";
3188     }
3189
3190     rhost = ap_get_remote_host(conn, r->server->module_config, 
3191                                REMOTE_NOLOOKUP);
3192     if (rhost == NULL) {
3193         rhost = "UNKNOWN-HOST";
3194     }
3195
3196     str1 = apr_pstrcat(r->pool, rhost, " ",
3197                       (conn->remote_logname != NULL ?
3198                       conn->remote_logname : "-"), " ",
3199                       ruser, NULL);
3200     apr_vsnprintf(str2, sizeof(str2), text, ap);
3201
3202     if (r->main == NULL) {
3203         strcpy(type, "initial");
3204     }
3205     else {
3206         strcpy(type, "subreq");
3207     }
3208
3209     for (i = 0, req = r; req->prev != NULL; req = req->prev) {
3210         i++;
3211     }
3212     if (i == 0) {
3213         redir[0] = '\0';
3214     }
3215     else {
3216         apr_snprintf(redir, sizeof(redir), "/redir#%d", i);
3217     }
3218
3219     apr_snprintf(str3, sizeof(str3),
3220                 "%s %s [%s/sid#%lx][rid#%lx/%s%s] (%d) %s\n", str1,
3221                 current_logtime(r), ap_get_server_name(r),
3222                 (unsigned long)(r->server), (unsigned long)r,
3223                 type, redir, level, str2);
3224
3225     apr_lock(rewrite_log_lock);
3226     nbytes = strlen(str3);
3227     apr_write(conf->rewritelogfp, str3, &nbytes);
3228     apr_unlock(rewrite_log_lock);
3229
3230     va_end(ap);
3231     return;
3232 }
3233
3234 static char *current_logtime(request_rec *r)
3235 {
3236     apr_exploded_time_t t;
3237     char tstr[80];
3238     apr_size_t len;
3239
3240     apr_explode_localtime(&t, apr_now());
3241
3242     apr_strftime(tstr, &len, 80, "[%d/%b/%Y:%H:%M:%S ", &t);
3243     apr_snprintf(tstr + strlen(tstr), 80-strlen(tstr), "%c%.2d%.2d]",
3244                 t.tm_gmtoff < 0 ? '-' : '+',
3245                 t.tm_gmtoff / (60*60), t.tm_gmtoff % (60*60));
3246     return apr_pstrdup(r->pool, tstr);
3247 }
3248
3249
3250
3251
3252 /*
3253 ** +-------------------------------------------------------+
3254 ** |                                                       |
3255 ** |              rewriting lockfile support
3256 ** |                                                       |
3257 ** +-------------------------------------------------------+
3258 */
3259
3260 #define REWRITELOCK_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
3261
3262 static void rewritelock_create(server_rec *s, apr_pool_t *p)
3263 {
3264     apr_status_t rc;
3265
3266     /* only operate if a lockfile is used */
3267     if (lockname == NULL || *(lockname) == '\0') {
3268         return;
3269     }
3270
3271     /* fixup the path, especially for rewritelock_remove() */
3272     lockname = ap_server_root_relative(p, lockname);
3273
3274     /* create the lockfile */
3275     rc = apr_create_lock (&rewrite_mapr_lock, APR_MUTEX, APR_LOCKALL, lockname, p);
3276     if (rc != APR_SUCCESS) {
3277         ap_log_error(APLOG_MARK, APLOG_ERR, rc, s,
3278                      "mod_rewrite: Parent could not create RewriteLock "
3279                      "file %s", lockname);
3280         exit(1);
3281     }
3282
3283     return;
3284 }
3285
3286 static apr_status_t rewritelock_remove(void *data)
3287 {
3288     /* only operate if a lockfile is used */
3289     if (lockname == NULL || *(lockname) == '\0') {
3290         return APR_SUCCESS;
3291     }
3292
3293     /* destroy the rewritelock */
3294     apr_destroy_lock (rewrite_mapr_lock);
3295     rewrite_mapr_lock = NULL;
3296     lockname = NULL;
3297     return(0);
3298 }
3299
3300
3301 /*
3302 ** +-------------------------------------------------------+
3303 ** |                                                       |
3304 ** |                  program map support
3305 ** |                                                       |
3306 ** +-------------------------------------------------------+
3307 */
3308
3309 static void run_rewritemap_programs(server_rec *s, apr_pool_t *p)
3310 {
3311     rewrite_server_conf *conf;
3312     apr_file_t *fpin = NULL;
3313     apr_file_t *fpout = NULL;
3314     apr_file_t *fperr = NULL;
3315     apr_array_header_t *rewritemaps;
3316     rewritemap_entry *entries;
3317     rewritemap_entry *map;
3318     int i;
3319     apr_status_t rc;
3320
3321     conf = ap_get_module_config(s->module_config, &rewrite_module);
3322
3323     /*  If the engine isn't turned on,
3324      *  don't even try to do anything.
3325      */
3326     if (conf->state == ENGINE_DISABLED) {
3327         return;
3328     }
3329
3330     rewritemaps = conf->rewritemaps;
3331     entries = (rewritemap_entry *)rewritemaps->elts;
3332     for (i = 0; i < rewritemaps->nelts; i++) {
3333         map = &entries[i];
3334         if (map->type != MAPTYPE_PRG) {
3335             continue;
3336         }
3337         if (map->datafile == NULL
3338             || *(map->datafile) == '\0'
3339             || map->fpin  != NULL
3340             || map->fpout != NULL        ) {
3341             continue;
3342         }
3343         fpin  = NULL;
3344         fpout = NULL;
3345         rc = rewritemap_program_child(p, map->datafile,
3346                                      &fpout, &fpin, &fperr);
3347         if (rc != APR_SUCCESS || fpin == NULL || fpout == NULL) {
3348             ap_log_error(APLOG_MARK, APLOG_ERR, rc, s,
3349                          "mod_rewrite: could not fork child for "
3350                          "RewriteMap process");
3351             exit(1);
3352         }
3353         map->fpin  = fpin;
3354         map->fpout = fpout;
3355         map->fperr = fperr;
3356     }
3357     return;
3358 }
3359
3360 /* child process code */
3361 static apr_status_t rewritemap_program_child(apr_pool_t *p, const char *progname,
3362                                              apr_file_t **fpout, apr_file_t **fpin,
3363                                              apr_file_t **fperr)
3364 {
3365     apr_status_t rc;
3366     apr_procattr_t *procattr;
3367     apr_proc_t *procnew;
3368
3369 #ifdef SIGHUP
3370     apr_signal(SIGHUP, SIG_IGN);
3371 #endif
3372
3373     
3374     if (((rc = apr_createprocattr_init(&procattr, p)) != APR_SUCCESS) ||
3375         ((rc = apr_setprocattr_io(procattr, APR_FULL_BLOCK,
3376                                   APR_FULL_NONBLOCK,
3377                                   APR_FULL_NONBLOCK)) != APR_SUCCESS) ||
3378         ((rc = apr_setprocattr_dir(procattr, 
3379                                    ap_make_dirstr_parent(p, progname)))
3380          != APR_SUCCESS) ||
3381         ((rc = apr_setprocattr_cmdtype(procattr, APR_PROGRAM)) != APR_SUCCESS)) {
3382         /* Something bad happened, give up and go away. */
3383     }
3384     else {
3385         procnew = apr_pcalloc(p, sizeof(*procnew));
3386         rc = apr_create_process(procnew, progname, NULL, NULL, procattr, p);
3387     
3388         if (rc == APR_SUCCESS) {
3389             apr_note_subprocess(p, procnew, kill_after_timeout);
3390
3391             if (fpin) {
3392                 (*fpin) = procnew->in;
3393             }
3394
3395             if (fpout) {
3396                 (*fpout) = procnew->out;
3397             }
3398
3399             if (fperr) {
3400                 (*fperr) = procnew->err;
3401             }
3402         }
3403     }
3404
3405     return (rc);
3406 }
3407
3408
3409
3410
3411 /*
3412 ** +-------------------------------------------------------+
3413 ** |                                                       |
3414 ** |             environment variable support
3415 ** |                                                       |
3416 ** +-------------------------------------------------------+
3417 */
3418
3419
3420 static char *lookup_variable(request_rec *r, char *var)
3421 {
3422     const char *result;
3423     char resultbuf[LONG_STRING_LEN];
3424     apr_exploded_time_t tm;
3425     request_rec *rsub;
3426 #ifndef WIN32
3427     struct passwd *pw;
3428     struct group *gr;
3429     apr_finfo_t finfo;
3430 #endif
3431
3432     result = NULL;
3433
3434     /* HTTP headers */
3435     if (strcasecmp(var, "HTTP_USER_AGENT") == 0) {
3436         result = lookup_header(r, "User-Agent");
3437     }
3438     else if (strcasecmp(var, "HTTP_REFERER") == 0) {
3439         result = lookup_header(r, "Referer");
3440     }
3441     else if (strcasecmp(var, "HTTP_COOKIE") == 0) {
3442         result = lookup_header(r, "Cookie");
3443     }
3444     else if (strcasecmp(var, "HTTP_FORWARDED") == 0) {
3445         result = lookup_header(r, "Forwarded");
3446     }
3447     else if (strcasecmp(var, "HTTP_HOST") == 0) {
3448         result = lookup_header(r, "Host");
3449     }
3450     else if (strcasecmp(var, "HTTP_PROXY_CONNECTION") == 0) {
3451         result = lookup_header(r, "Proxy-Connection");
3452     }
3453     else if (strcasecmp(var, "HTTP_ACCEPT") == 0) {
3454         result = lookup_header(r, "Accept");
3455     }
3456     /* all other headers from which we are still not know about */
3457     else if (strlen(var) > 5 && strncasecmp(var, "HTTP:", 5) == 0) {
3458         result = lookup_header(r, var+5);
3459     }
3460
3461     /* connection stuff */
3462     else if (strcasecmp(var, "REMOTE_ADDR") == 0) {
3463         result = r->connection->remote_ip;
3464     }
3465     else if (strcasecmp(var, "REMOTE_HOST") == 0) {
3466         result = (char *)ap_get_remote_host(r->connection,
3467                                          r->per_dir_config, REMOTE_NAME);
3468     }
3469     else if (strcasecmp(var, "REMOTE_USER") == 0) {
3470         result = r->user;
3471     }
3472     else if (strcasecmp(var, "REMOTE_IDENT") == 0) {
3473         result = (char *)ap_get_remote_logname(r);
3474     }
3475
3476     /* request stuff */
3477     else if (strcasecmp(var, "THE_REQUEST") == 0) { /* non-standard */
3478         result = r->the_request;
3479     }
3480     else if (strcasecmp(var, "REQUEST_METHOD") == 0) {
3481         result = r->method;
3482     }
3483     else if (strcasecmp(var, "REQUEST_URI") == 0) { /* non-standard */
3484         result = r->uri;
3485     }
3486     else if (strcasecmp(var, "SCRIPT_FILENAME") == 0 ||
3487              strcasecmp(var, "REQUEST_FILENAME") == 0  ) {
3488         result = r->filename;
3489     }
3490     else if (strcasecmp(var, "PATH_INFO") == 0) {
3491         result = r->path_info;
3492     }
3493     else if (strcasecmp(var, "QUERY_STRING") == 0) {
3494         result = r->args;
3495     }
3496     else if (strcasecmp(var, "AUTH_TYPE") == 0) {
3497         result = r->ap_auth_type;
3498     }
3499     else if (strcasecmp(var, "IS_SUBREQ") == 0) { /* non-standard */
3500         result = (r->main != NULL ? "true" : "false");
3501     }
3502
3503     /* internal server stuff */
3504     else if (strcasecmp(var, "DOCUMENT_ROOT") == 0) {
3505         result = ap_document_root(r);
3506     }
3507     else if (strcasecmp(var, "SERVER_ADMIN") == 0) {
3508         result = r->server->server_admin;
3509     }
3510     else if (strcasecmp(var, "SERVER_NAME") == 0) {
3511         result = ap_get_server_name(r);
3512     }
3513     else if (strcasecmp(var, "SERVER_ADDR") == 0) { /* non-standard */
3514         result = r->connection->local_ip;
3515     }
3516     else if (strcasecmp(var, "SERVER_PORT") == 0) {
3517         apr_snprintf(resultbuf, sizeof(resultbuf), "%u", ap_get_server_port(r));
3518         result = resultbuf;
3519     }
3520     else if (strcasecmp(var, "SERVER_PROTOCOL") == 0) {
3521         result = r->protocol;
3522     }
3523     else if (strcasecmp(var, "SERVER_SOFTWARE") == 0) {
3524         result = ap_get_server_version();
3525     }
3526     else if (strcasecmp(var, "API_VERSION") == 0) { /* non-standard */
3527         apr_snprintf(resultbuf, sizeof(resultbuf), "%d:%d",
3528                     MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
3529         result = resultbuf;
3530     }
3531
3532 /* XXX: wow this has gotta be slow if you actually use it for a lot, recalculates exploded time for each variable */
3533     /* underlaying Unix system stuff */
3534     else if (strcasecmp(var, "TIME_YEAR") == 0) {
3535         apr_explode_localtime(&tm, apr_now());
3536         apr_snprintf(resultbuf, sizeof(resultbuf), "%04d", tm.tm_year + 1900);
3537         result = resultbuf;
3538     }
3539 #define MKTIMESTR(format, tmfield) \
3540     apr_explode_localtime(&tm, apr_now()); \
3541     apr_snprintf(resultbuf, sizeof(resultbuf), format, tm.tmfield); \
3542     result = resultbuf;
3543     else if (strcasecmp(var, "TIME_MON") == 0) {
3544         MKTIMESTR("%02d", tm_mon+1)
3545     }
3546     else if (strcasecmp(var, "TIME_DAY") == 0) {
3547         MKTIMESTR("%02d", tm_mday)
3548     }
3549     else if (strcasecmp(var, "TIME_HOUR") == 0) {
3550         MKTIMESTR("%02d", tm_hour)
3551     }
3552     else if (strcasecmp(var, "TIME_MIN") == 0) {
3553         MKTIMESTR("%02d", tm_min)
3554     }
3555     else if (strcasecmp(var, "TIME_SEC") == 0) {
3556         MKTIMESTR("%02d", tm_sec)
3557     }
3558     else if (strcasecmp(var, "TIME_WDAY") == 0) {
3559         MKTIMESTR("%d", tm_wday)
3560     }
3561     else if (strcasecmp(var, "TIME") == 0) {
3562         apr_explode_localtime(&tm, apr_now());
3563         apr_snprintf(resultbuf, sizeof(resultbuf),
3564                     "%04d%02d%02d%02d%02d%02d", tm.tm_year + 1900,
3565                     tm.tm_mon+1, tm.tm_mday,
3566                     tm.tm_hour, tm.tm_min, tm.tm_sec);
3567         result = resultbuf;
3568         rewritelog(r, 1, "RESULT='%s'", result);
3569     }
3570
3571     /* all other env-variables from the parent Apache process */
3572     else if (strlen(var) > 4 && strncasecmp(var, "ENV:", 4) == 0) {
3573         /* first try the internal Apache notes structure */
3574         result = apr_table_get(r->notes, var+4);
3575         /* second try the internal Apache env structure  */
3576         if (result == NULL) {
3577             result = apr_table_get(r->subprocess_env, var+4);
3578         }
3579         /* third try the external OS env */
3580         if (result == NULL) {
3581             result = getenv(var+4);
3582         }
3583     }
3584
3585 #define LOOKAHEAD(subrecfunc) \
3586         if ( \
3587           /* filename is safe to use */ \
3588           r->filename != NULL \
3589               /* - and we're either not in a subrequest */ \
3590               && ( r->main == NULL \
3591                   /* - or in a subrequest where paths are non-NULL... */ \
3592                     || ( r->main->uri != NULL && r->uri != NULL \
3593                         /*   ...and sub and main paths differ */ \
3594                         && strcmp(r->main->uri, r->uri) != 0))) { \
3595             /* process a file-based subrequest */ \
3596             rsub = subrecfunc(r->filename, r); \
3597             /* now recursively lookup the variable in the sub_req */ \
3598             result = lookup_variable(rsub, var+5); \
3599             /* copy it up to our scope before we destroy sub_req's apr_pool_t */ \
3600             result = apr_pstrdup(r->pool, result); \
3601             /* cleanup by destroying the subrequest */ \
3602             ap_destroy_sub_req(rsub); \
3603             /* log it */ \
3604             rewritelog(r, 5, "lookahead: path=%s var=%s -> val=%s", \
3605                        r->filename, var+5, result); \
3606             /* return ourself to prevent re-pstrdup */ \
3607             return (char *)result; \
3608         }
3609
3610     /* look-ahead for parameter through URI-based sub-request */
3611     else if (strlen(var) > 5 && strncasecmp(var, "LA-U:", 5) == 0) {
3612         LOOKAHEAD(ap_sub_req_lookup_uri)
3613     }
3614     /* look-ahead for parameter through file-based sub-request */
3615     else if (strlen(var) > 5 && strncasecmp(var, "LA-F:", 5) == 0) {
3616         LOOKAHEAD(ap_sub_req_lookup_file)
3617     }
3618
3619 #if !defined(WIN32) && !defined(NETWARE)
3620     /* Win32 has a rather different view of file ownerships.
3621        For now, just forget it */
3622
3623     /* file stuff */
3624     else if (strcasecmp(var, "SCRIPT_USER") == 0) {
3625         result = "<unknown>";
3626         if (r->finfo.protection != 0) {
3627             if ((pw = getpwuid(r->finfo.user)) != NULL) {
3628                 result = pw->pw_name;
3629             }
3630         }
3631         else {
3632             if (apr_stat(&finfo, r->filename, r->pool) == APR_SUCCESS) {
3633                 if ((pw = getpwuid(finfo.user)) != NULL) {
3634                     result = pw->pw_name;
3635                 }
3636             }
3637         }
3638     }
3639     else if (strcasecmp(var, "SCRIPT_GROUP") == 0) {
3640         result = "<unknown>";
3641         if (r->finfo.protection != 0) {
3642             if ((gr = getgrgid(r->finfo.group)) != NULL) {
3643                 result = gr->gr_name;
3644             }
3645         }
3646         else {
3647             if (apr_stat(&finfo, r->filename, r->pool) == 0) {
3648                 if ((gr = getgrgid(finfo.group)) != NULL) {
3649                     result = gr->gr_name;
3650                 }
3651             }
3652         }
3653     }
3654 #endif /* ndef WIN32 && NETWARE*/
3655
3656     if (result == NULL) {
3657         return apr_pstrdup(r->pool, "");
3658     }
3659     else {
3660         return apr_pstrdup(r->pool, result);
3661     }
3662 }
3663
3664 static char *lookup_header(request_rec *r, const char *name)
3665 {
3666     apr_array_header_t *hdrs_arr;
3667     apr_table_entry_t *hdrs;
3668     int i;
3669
3670     hdrs_arr = apr_table_elts(r->headers_in);
3671     hdrs = (apr_table_entry_t *)hdrs_arr->elts;
3672     for (i = 0; i < hdrs_arr->nelts; ++i) {
3673         if (hdrs[i].key == NULL) {
3674             continue;
3675         }
3676         if (strcasecmp(hdrs[i].key, name) == 0) {
3677             apr_table_merge(r->notes, VARY_KEY_THIS, name);
3678             return hdrs[i].val;
3679         }
3680     }
3681     return NULL;
3682 }
3683
3684
3685
3686
3687 /*
3688 ** +-------------------------------------------------------+
3689 ** |                                                       |
3690 ** |                    caching support
3691 ** |                                                       |
3692 ** +-------------------------------------------------------+
3693 */
3694
3695
3696 static cache *init_cache(apr_pool_t *p)
3697 {
3698     cache *c;
3699
3700     c = (cache *)apr_palloc(p, sizeof(cache));
3701     if (apr_create_pool(&c->pool, p) != APR_SUCCESS)
3702                 return NULL;
3703     c->lists = apr_make_array(c->pool, 2, sizeof(cachelist));
3704     return c;
3705 }
3706
3707 static void set_cache_string(cache *c, const char *res, int mode, time_t t,
3708                              char *key, char *value)
3709 {
3710     cacheentry ce;
3711
3712     ce.time  = t;
3713     ce.key   = key;
3714     ce.value = value;
3715     store_cache_string(c, res, &ce);
3716     return;
3717 }
3718
3719 static char *get_cache_string(cache *c, const char *res, int mode,
3720                               time_t t, char *key)
3721 {
3722     cacheentry *ce;
3723
3724     ce = retrieve_cache_string(c, res, key);
3725     if (ce == NULL) {
3726         return NULL;
3727     }
3728     if (mode & CACHEMODE_TS) {
3729         if (t != ce->time) {
3730             return NULL;
3731         }
3732     }
3733     else if (mode & CACHEMODE_TTL) {
3734         if (t > ce->time) {
3735             return NULL;
3736         }
3737     }
3738     return apr_pstrdup(c->pool, ce->value);
3739 }
3740
3741 static int cache_tlb_hash(char *key)
3742 {
3743     unsigned long n;
3744     char *p;
3745
3746     n = 0;
3747     for (p = key; *p != '\0'; p++) {
3748         n = ((n << 5) + n) ^ (unsigned long)(*p++);
3749     }
3750
3751     return n % CACHE_TLB_ROWS;
3752 }
3753
3754 static cacheentry *cache_tlb_lookup(cachetlbentry *tlb, cacheentry *elt,
3755                                     char *key)
3756 {
3757     int ix = cache_tlb_hash(key);
3758     int i;
3759     int j;
3760
3761     for (i=0; i < CACHE_TLB_COLS; ++i) {
3762         j = tlb[ix].t[i];
3763         if (j < 0)
3764             return NULL;
3765         if (strcmp(elt[j].key, key) == 0)
3766             return &elt[j];
3767     }
3768     return NULL;
3769 }
3770
3771 static void cache_tlb_replace(cachetlbentry *tlb, cacheentry *elt,
3772                               cacheentry *e)
3773 {
3774     int ix = cache_tlb_hash(e->key);
3775     int i;
3776
3777     tlb = &tlb[ix];
3778
3779     for (i=1; i < CACHE_TLB_COLS; ++i)
3780         tlb->t[i] = tlb->t[i-1];
3781
3782     tlb->t[0] = e - elt;
3783 }
3784
3785 static void store_cache_string(cache *c, const char *res, cacheentry *ce)
3786 {
3787     int i;
3788     int j;
3789     cachelist *l;
3790     cacheentry *e;
3791     cachetlbentry *t;
3792     int found_list;
3793
3794     found_list = 0;
3795     /* first try to edit an existing entry */
3796     for (i = 0; i < c->lists->nelts; i++) {
3797         l = &(((cachelist *)c->lists->elts)[i]);
3798         if (strcmp(l->resource, res) == 0) {
3799             found_list = 1;
3800
3801             e = cache_tlb_lookup((cachetlbentry *)l->tlb->elts,
3802                                  (cacheentry *)l->entries->elts, ce->key);
3803             if (e != NULL) {
3804                 e->time  = ce->time;
3805                 e->value = apr_pstrdup(c->pool, ce->value);
3806                 return;
3807             }
3808
3809             for (j = 0; j < l->entries->nelts; j++) {
3810                 e = &(((cacheentry *)l->entries->elts)[j]);
3811                 if (strcmp(e->key, ce->key) == 0) {
3812                     e->time  = ce->time;
3813                     e->value = apr_pstrdup(c->pool, ce->value);
3814                   cache_tlb_replace((cachetlbentry *)l->tlb->elts,
3815                                     (cacheentry *)l->entries->elts, e);
3816                     return;
3817                 }
3818             }
3819         }
3820     }
3821
3822     /* create a needed new list */
3823     if (!found_list) {
3824         l = apr_push_array(c->lists);
3825         l->resource = apr_pstrdup(c->pool, res);
3826         l->entries  = apr_make_array(c->pool, 2, sizeof(cacheentry));
3827         l->tlb      = apr_make_array(c->pool, CACHE_TLB_ROWS,
3828                                     sizeof(cachetlbentry));
3829         for (i=0; i<CACHE_TLB_ROWS; ++i) {
3830             t = &((cachetlbentry *)l->tlb->elts)[i];
3831                 for (j=0; j<CACHE_TLB_COLS; ++j)
3832                     t->t[j] = -1;
3833         }
3834     }
3835
3836     /* create the new entry */
3837     for (i = 0; i < c->lists->nelts; i++) {
3838         l = &(((cachelist *)c->lists->elts)[i]);
3839         if (strcmp(l->resource, res) == 0) {
3840             e = apr_push_array(l->entries);
3841             e->time  = ce->time;
3842             e->key   = apr_pstrdup(c->pool, ce->key);
3843             e->value = apr_pstrdup(c->pool, ce->value);
3844             cache_tlb_replace((cachetlbentry *)l->tlb->elts,
3845                               (cacheentry *)l->entries->elts, e);
3846             return;
3847         }
3848     }
3849
3850     /* not reached, but when it is no problem... */
3851     return;
3852 }
3853
3854 static cacheentry *retrieve_cache_string(cache *c, const char *res, char *key)
3855 {
3856     int i;
3857     int j;
3858     cachelist *l;
3859     cacheentry *e;
3860
3861     for (i = 0; i < c->lists->nelts; i++) {
3862         l = &(((cachelist *)c->lists->elts)[i]);
3863         if (strcmp(l->resource, res) == 0) {
3864
3865             e = cache_tlb_lookup((cachetlbentry *)l->tlb->elts,
3866                                  (cacheentry *)l->entries->elts, key);
3867             if (e != NULL)
3868                 return e;
3869
3870             for (j = 0; j < l->entries->nelts; j++) {
3871                 e = &(((cacheentry *)l->entries->elts)[j]);
3872                 if (strcmp(e->key, key) == 0) {
3873                     return e;
3874                 }
3875             }
3876         }
3877     }
3878     return NULL;
3879 }
3880
3881
3882
3883
3884 /*
3885 ** +-------------------------------------------------------+
3886 ** |                                                       |
3887 ** |                    misc functions
3888 ** |                                                       |
3889 ** +-------------------------------------------------------+
3890 */
3891
3892 static char *subst_prefix_path(request_rec *r, char *input, char *match,
3893                                const char *subst)
3894 {
3895     char matchbuf[LONG_STRING_LEN];
3896     char substbuf[LONG_STRING_LEN];
3897     char *output;
3898     int l;
3899
3900     output = input;
3901
3902     /* first create a match string which always has a trailing slash */
3903     l = apr_cpystrn(matchbuf, match, sizeof(matchbuf)) - matchbuf;
3904     if (matchbuf[l-1] != '/') {
3905        matchbuf[l] = '/';
3906        matchbuf[l+1] = '\0';
3907        l++;
3908     }
3909     /* now compare the prefix */
3910     if (strncmp(input, matchbuf, l) == 0) {
3911         rewritelog(r, 5, "strip matching prefix: %s -> %s", output, output+l);
3912         output = apr_pstrdup(r->pool, output+l);
3913
3914         /* and now add the base-URL as replacement prefix */
3915         l = apr_cpystrn(substbuf, subst, sizeof(substbuf)) - substbuf;
3916         if (substbuf[l-1] != '/') {
3917            substbuf[l] = '/';
3918            substbuf[l+1] = '\0';
3919            l++;
3920         }
3921         if (output[0] == '/') {
3922             rewritelog(r, 4, "add subst prefix: %s -> %s%s",
3923                        output, substbuf, output+1);
3924             output = apr_pstrcat(r->pool, substbuf, output+1, NULL);
3925         }
3926         else {
3927             rewritelog(r, 4, "add subst prefix: %s -> %s%s",
3928                        output, substbuf, output);
3929             output = apr_pstrcat(r->pool, substbuf, output, NULL);
3930         }
3931     }
3932     return output;
3933 }
3934
3935
3936 /*
3937 **
3938 **  own command line parser which don't have the '\\' problem
3939 **
3940 */
3941
3942 static int parseargline(char *str, char **a1, char **a2, char **a3)
3943 {
3944     char *cp;
3945     int isquoted;
3946
3947 #define SKIP_WHITESPACE(cp) \
3948     for ( ; *cp == ' ' || *cp == '\t'; ) { \
3949         cp++; \
3950     };
3951
3952 #define CHECK_QUOTATION(cp,isquoted) \
3953     isquoted = 0; \
3954     if (*cp == '"') { \
3955         isquoted = 1; \
3956         cp++; \
3957     }
3958
3959 #define DETERMINE_NEXTSTRING(cp,isquoted) \
3960     for ( ; *cp != '\0'; cp++) { \
3961         if (   (isquoted    && (*cp     == ' ' || *cp     == '\t')) \
3962             || (*cp == '\\' && (*(cp+1) == ' ' || *(cp+1) == '\t'))) { \
3963             cp++; \
3964             continue; \
3965         } \
3966         if (   (!isquoted && (*cp == ' ' || *cp == '\t')) \
3967             || (isquoted  && *cp == '"')                  ) { \
3968             break; \
3969         } \
3970     }
3971
3972     cp = str;
3973     SKIP_WHITESPACE(cp);
3974
3975     /*  determine first argument */
3976     CHECK_QUOTATION(cp, isquoted);
3977     *a1 = cp;
3978     DETERMINE_NEXTSTRING(cp, isquoted);
3979     if (*cp == '\0') {
3980         return 1;
3981     }
3982     *cp++ = '\0';
3983
3984     SKIP_WHITESPACE(cp);
3985
3986     /*  determine second argument */
3987     CHECK_QUOTATION(cp, isquoted);
3988     *a2 = cp;
3989     DETERMINE_NEXTSTRING(cp, isquoted);
3990     if (*cp == '\0') {
3991         *cp++ = '\0';
3992         *a3 = NULL;
3993         return 0;
3994     }
3995     *cp++ = '\0';
3996
3997     SKIP_WHITESPACE(cp);
3998
3999     /* again check if there are only two arguments */
4000     if (*cp == '\0') {
4001         *cp++ = '\0';
4002         *a3 = NULL;
4003         return 0;
4004     }
4005
4006     /*  determine second argument */
4007     CHECK_QUOTATION(cp, isquoted);
4008     *a3 = cp;
4009     DETERMINE_NEXTSTRING(cp, isquoted);
4010     *cp++ = '\0';
4011
4012     return 0;
4013 }
4014
4015
4016 static void add_env_variable(request_rec *r, char *s)
4017 {
4018     char var[MAX_STRING_LEN];
4019     char val[MAX_STRING_LEN];
4020     char *cp;
4021     int n;
4022
4023     if ((cp = strchr(s, ':')) != NULL) {
4024         n = ((cp-s) > MAX_STRING_LEN-1 ? MAX_STRING_LEN-1 : (cp-s));
4025         memcpy(var, s, n);
4026         var[n] = '\0';
4027         apr_cpystrn(val, cp+1, sizeof(val));
4028         apr_table_set(r->subprocess_env, var, val);
4029         rewritelog(r, 5, "setting env variable '%s' to '%s'", var, val);
4030     }
4031 }
4032
4033
4034 /*
4035 **
4036 **  check that a subrequest won't cause infinite recursion
4037 **
4038 */
4039
4040 static int subreq_ok(request_rec *r)
4041 {
4042     /*
4043      * either not in a subrequest, or in a subrequest
4044      * and URIs aren't NULL and sub/main URIs differ
4045      */
4046     return (r->main == NULL ||
4047             (r->main->uri != NULL && r->uri != NULL &&
4048              strcmp(r->main->uri, r->uri) != 0));
4049 }
4050
4051
4052 /*
4053 **
4054 **  stat() for only the prefix of a path
4055 **
4056 */
4057
4058 static int prefix_stat(const char *path, apr_finfo_t *sb)
4059 {
4060     char curpath[LONG_STRING_LEN];
4061     char *cp;
4062
4063     apr_cpystrn(curpath, path, sizeof(curpath));
4064     if (curpath[0] != '/') {
4065         return 0;
4066     }
4067     if ((cp = strchr(curpath+1, '/')) != NULL) {
4068         *cp = '\0';
4069     }
4070     if (apr_stat(sb, curpath, NULL) == 0) {
4071         return 1;
4072     }
4073     else {
4074         return 0;
4075     }
4076 }
4077
4078
4079 /*
4080 **
4081 **  Lexicographic Compare
4082 **
4083 */
4084
4085 static int compare_lexicography(char *cpNum1, char *cpNum2)
4086 {
4087     int i;
4088     int n1, n2;
4089
4090     n1 = strlen(cpNum1);
4091     n2 = strlen(cpNum2);
4092     if (n1 > n2) {
4093         return 1;
4094     }
4095     if (n1 < n2) {
4096         return -1;
4097     }
4098     for (i = 0; i < n1; i++) {
4099         if (cpNum1[i] > cpNum2[i]) {
4100             return 1;
4101         }
4102         if (cpNum1[i] < cpNum2[i]) {
4103             return -1;
4104         }
4105     }
4106     return 0;
4107 }
4108
4109 /*
4110 **
4111 **  Find end of bracketed expression
4112 **  s points after the opening bracket
4113 **
4114 */
4115
4116 static char *find_closing_bracket(char *s, int left, int right)
4117 {
4118     int depth;
4119
4120     for (depth = 1; *s; ++s) {
4121         if (*s == right && --depth == 0) {
4122             return s;
4123         }
4124         else if (*s == left) {
4125             ++depth;
4126         }
4127     }
4128     return NULL;
4129 }
4130
4131 #ifdef NETWARE
4132 int main(int argc, char *argv[]) 
4133 {
4134     ExitThread(TSR_THREAD, 0);
4135 }
4136 #endif
4137  
4138 /*EOF*/