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