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