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