1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
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.
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.
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.
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
47 * ====================================================================
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/>.
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.
60 * _ __ ___ ___ __| | _ __ _____ ___ __(_) |_ ___
61 * | '_ ` _ \ / _ \ / _` | | '__/ _ \ \ /\ / / '__| | __/ _ \
62 * | | | | | | (_) | (_| | | | | __/\ V V /| | | | || __/
63 * |_| |_| |_|\___/ \__,_|___|_| \___| \_/\_/ |_| |_|\__\___|
66 * URL Rewriting Module
68 * This module uses a rule-based rewriting engine (based on a
69 * regular-expression parser) to rewrite requested URLs on the fly.
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
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.
83 * This module was originally written in April 1996 and
84 * gifted exclusively to the The Apache Software Foundation in July 1997 by
92 #include "apr_strings.h"
96 #include "apr_signal.h"
97 #include "apr_global_mutex.h"
101 #include "apr_thread_mutex.h"
104 #define APR_WANT_MEMFUNC
105 #define APR_WANT_STRFUNC
106 #define APR_WANT_IOVEC
107 #include "apr_want.h"
109 /* XXX: Do we really need these headers? */
110 #if APR_HAVE_UNISTD_H
113 #if APR_HAVE_SYS_TYPES_H
114 #include <sys/types.h>
116 #if APR_HAVE_STDARG_H
119 #if APR_HAVE_STDLIB_H
126 #include "ap_config.h"
128 #include "http_config.h"
129 #include "http_request.h"
130 #include "http_core.h"
131 #include "http_log.h"
132 #include "http_protocol.h"
133 #include "http_vhost.h"
135 #include "mod_rewrite.h"
137 #if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
139 #define MOD_REWRITE_SET_MUTEX_PERMS /* XXX Apache should define something */
143 * The key in the r->notes apr_table_t wherein we store our accumulated
144 * Vary values, and the one used for per-condition checks in a chain.
146 #define VARY_KEY "rewrite-Vary"
147 #define VARY_KEY_THIS "rewrite-Vary-this"
149 /* remembered mime-type for [T=...] */
150 #define REWRITE_FORCED_MIMETYPE_NOTEVAR "rewrite-forced-mimetype"
152 #define ENVVAR_SCRIPT_URL "SCRIPT_URL"
153 #define REDIRECT_ENVVAR_SCRIPT_URL "REDIRECT_" ENVVAR_SCRIPT_URL
154 #define ENVVAR_SCRIPT_URI "SCRIPT_URI"
156 #define CONDFLAG_NONE 1<<0
157 #define CONDFLAG_NOCASE 1<<1
158 #define CONDFLAG_NOTMATCH 1<<2
159 #define CONDFLAG_ORNEXT 1<<3
161 #define RULEFLAG_NONE 1<<0
162 #define RULEFLAG_FORCEREDIRECT 1<<1
163 #define RULEFLAG_LASTRULE 1<<2
164 #define RULEFLAG_NEWROUND 1<<3
165 #define RULEFLAG_CHAIN 1<<4
166 #define RULEFLAG_IGNOREONSUBREQ 1<<5
167 #define RULEFLAG_NOTMATCH 1<<6
168 #define RULEFLAG_PROXY 1<<7
169 #define RULEFLAG_PASSTHROUGH 1<<8
170 #define RULEFLAG_FORBIDDEN 1<<9
171 #define RULEFLAG_GONE 1<<10
172 #define RULEFLAG_QSAPPEND 1<<11
173 #define RULEFLAG_NOCASE 1<<12
174 #define RULEFLAG_NOESCAPE 1<<13
176 /* return code of the rewrite rule
177 * the result may be escaped - or not
179 #define ACTION_NORMAL 1<<0
180 #define ACTION_NOESCAPE 1<<1
183 #define MAPTYPE_TXT 1<<0
184 #define MAPTYPE_DBM 1<<1
185 #define MAPTYPE_PRG 1<<2
186 #define MAPTYPE_INT 1<<3
187 #define MAPTYPE_RND 1<<4
189 #define ENGINE_DISABLED 1<<0
190 #define ENGINE_ENABLED 1<<1
192 #define OPTION_NONE 1<<0
193 #define OPTION_INHERIT 1<<1
196 #define RAND_MAX 32767
199 /* max cookie size in rfc 2109 */
200 /* XXX: not used at all. We should do a check somewhere and/or cut the cookie */
201 #define MAX_COOKIE_LEN 4096
203 /* max number of regex captures */
204 #define MAX_NMATCH 10
206 /* default maximum number of internal redirects */
207 #define REWRITE_REDIRECT_LIMIT 10
209 /* for rewrite log file */
210 #define REWRITELOG_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
211 #define REWRITELOG_FLAGS ( APR_WRITE | APR_APPEND | APR_CREATE )
213 /* max line length (incl.\n) in text rewrite maps */
214 #ifndef REWRITE_MAX_TXT_MAP_LINE
215 #define REWRITE_MAX_TXT_MAP_LINE 1024
218 /* max response length (incl.\n) in prg rewrite maps */
219 #ifndef REWRITE_MAX_PRG_MAP_LINE
220 #define REWRITE_MAX_PRG_MAP_LINE 2048
223 /* for better readbility */
224 #define LEFT_CURLY '{'
225 #define RIGHT_CURLY '}'
228 * check that a subrequest won't cause infinite recursion
230 * either not in a subrequest, or in a subrequest
231 * and URIs aren't NULL and sub/main URIs differ
233 #define subreq_ok(r) (!r->main || \
234 (r->main->uri && r->uri && strcmp(r->main->uri, r->uri)))
238 * +-------------------------------------------------------+
240 * | Types and Structures
242 * +-------------------------------------------------------+
246 const char *datafile; /* filename for map data files */
247 const char *dbmtype; /* dbm type for dbm map data files */
248 const char *checkfile; /* filename to check for map existence */
249 int type; /* the type of the map */
250 apr_file_t *fpin; /* in file pointer for program maps */
251 apr_file_t *fpout; /* out file pointer for program maps */
252 apr_file_t *fperr; /* err file pointer for program maps */
253 char *(*func)(request_rec *, /* function pointer for internal maps */
255 char **argv; /* argv of the external rewrite map */
258 /* special pattern types for RewriteCond */
273 char *input; /* Input string of RewriteCond */
274 char *pattern; /* the RegExp pattern string */
275 regex_t *regexp; /* the precompiled regexp */
276 int flags; /* Flags which control the match */
277 pattern_type ptype; /* pattern type */
280 /* single linked list for env vars and cookies */
281 typedef struct data_item {
282 struct data_item *next;
287 apr_array_header_t *rewriteconds;/* the corresponding RewriteCond entries */
288 char *pattern; /* the RegExp pattern string */
289 regex_t *regexp; /* the RegExp pattern compilation */
290 char *output; /* the Substitution string */
291 int flags; /* Flags which control the substitution */
292 char *forced_mimetype; /* forced MIME type of substitution */
293 int forced_responsecode; /* forced HTTP redirect response status */
294 data_item *env; /* added environment variables */
295 data_item *cookie; /* added cookies */
296 int skip; /* number of next rules to skip */
300 int state; /* the RewriteEngine state */
301 int options; /* the RewriteOption state */
302 const char *rewritelogfile; /* the RewriteLog filename */
303 apr_file_t *rewritelogfp; /* the RewriteLog open filepointer */
304 int rewriteloglevel; /* the RewriteLog level of verbosity */
305 apr_hash_t *rewritemaps; /* the RewriteMap entries */
306 apr_array_header_t *rewriteconds; /* the RewriteCond entries (temp.) */
307 apr_array_header_t *rewriterules; /* the RewriteRule entries */
308 server_rec *server; /* the corresponding server indicator */
309 int redirect_limit; /* max number of internal redirects */
310 } rewrite_server_conf;
313 int state; /* the RewriteEngine state */
314 int options; /* the RewriteOption state */
315 apr_array_header_t *rewriteconds; /* the RewriteCond entries (temp.) */
316 apr_array_header_t *rewriterules; /* the RewriteRule entries */
317 char *directory; /* the directory where it applies */
318 const char *baseurl; /* the base-URL where it applies */
319 int redirect_limit; /* max. number of internal redirects */
320 } rewrite_perdir_conf;
323 int redirects; /* current number of redirects */
324 int redirect_limit; /* maximum number of redirects */
325 } rewrite_request_conf;
328 /* the (per-child) cache structures.
330 typedef struct cache {
334 apr_thread_mutex_t *lock;
338 /* cached maps contain an mtime for the whole map and live in a subpool
339 * of the cachep->pool. That makes it easy to forget them if necessary.
347 /* the regex structure for the
348 * substitution of backreferences
350 typedef struct backrefinfo {
353 regmatch_t regmatch[10];
356 /* single linked list used for
359 typedef struct result_list {
360 struct result_list *next;
367 * +-------------------------------------------------------+
369 * | static module data
371 * +-------------------------------------------------------+
374 /* the global module structure */
375 module AP_MODULE_DECLARE_DATA rewrite_module;
377 /* rewritemap int: handler function registry */
378 static apr_hash_t *mapfunc_hash;
381 static cache *cachep;
383 /* whether proxy module is available or not */
384 static int proxy_available;
386 /* whether random seed can be reaped */
387 static int rewrite_rand_init_done = 0;
390 static const char *lockname;
391 static apr_global_mutex_t *rewrite_mapr_lock_acquire = NULL;
392 static apr_global_mutex_t *rewrite_log_lock = NULL;
396 * +-------------------------------------------------------+
398 * | rewriting logfile support
400 * +-------------------------------------------------------+
403 static char *current_logtime(request_rec *r)
409 apr_time_exp_lt(&t, apr_time_now());
411 apr_strftime(tstr, &len, sizeof(tstr), "[%d/%b/%Y:%H:%M:%S ", &t);
412 apr_snprintf(tstr+len, sizeof(tstr)-len, "%c%.2d%.2d]",
413 t.tm_gmtoff < 0 ? '-' : '+',
414 t.tm_gmtoff / (60*60), t.tm_gmtoff % (60*60));
416 return apr_pstrdup(r->pool, tstr);
419 static int open_rewritelog(server_rec *s, apr_pool_t *p)
421 rewrite_server_conf *conf;
424 conf = ap_get_module_config(s->module_config, &rewrite_module);
426 /* - no logfile configured
427 * - logfilename empty
428 * - virtual log shared w/ main server
430 if (!conf->rewritelogfile || !*conf->rewritelogfile || conf->rewritelogfp) {
434 if (*conf->rewritelogfile == '|') {
437 fname = ap_server_root_relative(p, conf->rewritelogfile+1);
439 ap_log_error(APLOG_MARK, APLOG_ERR, APR_EBADPATH, s,
440 "mod_rewrite: Invalid RewriteLog "
441 "path %s", conf->rewritelogfile+1);
445 if ((pl = ap_open_piped_log(p, fname)) == NULL) {
446 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
447 "mod_rewrite: could not open reliable pipe "
448 "to RewriteLog filter %s", fname);
451 conf->rewritelogfp = ap_piped_log_write_fd(pl);
456 fname = ap_server_root_relative(p, conf->rewritelogfile);
458 ap_log_error(APLOG_MARK, APLOG_ERR, APR_EBADPATH, s,
459 "mod_rewrite: Invalid RewriteLog "
460 "path %s", conf->rewritelogfile);
464 if ((rc = apr_file_open(&conf->rewritelogfp, fname,
465 REWRITELOG_FLAGS, REWRITELOG_MODE, p))
467 ap_log_error(APLOG_MARK, APLOG_ERR, rc, s,
468 "mod_rewrite: could not open RewriteLog "
477 static void rewritelog(request_rec *r, int level, const char *fmt, ...)
479 rewrite_server_conf *conf;
480 char *logline, *text;
481 const char *rhost, *rname;
488 conf = ap_get_module_config(r->server->module_config, &rewrite_module);
490 if (!conf->rewritelogfp || level > conf->rewriteloglevel) {
494 rhost = ap_get_remote_host(r->connection, r->per_dir_config,
495 REMOTE_NOLOOKUP, NULL);
496 rname = ap_get_remote_logname(r);
498 for (redir=0, req=r; req->prev; req = req->prev) {
503 text = apr_pvsprintf(r->pool, fmt, ap);
506 logline = apr_psprintf(r->pool, "%s %s %s %s [%s/sid#%pp][rid#%pp/%s%s%s] "
507 "(%d) %s" APR_EOL_STR,
508 rhost ? rhost : "UNKNOWN-HOST",
510 r->user ? (*r->user ? r->user : "\"\"") : "-",
512 ap_get_server_name(r),
515 r->main ? "subreq" : "initial",
516 redir ? "/redir#" : "",
517 redir ? apr_itoa(r->pool, redir) : "",
520 rv = apr_global_mutex_lock(rewrite_log_lock);
521 if (rv != APR_SUCCESS) {
522 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
523 "apr_global_mutex_lock(rewrite_log_lock) failed");
524 /* XXX: Maybe this should be fatal? */
527 nbytes = strlen(logline);
528 apr_file_write(conf->rewritelogfp, logline, &nbytes);
530 rv = apr_global_mutex_unlock(rewrite_log_lock);
531 if (rv != APR_SUCCESS) {
532 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
533 "apr_global_mutex_unlock(rewrite_log_lock) failed");
534 /* XXX: Maybe this should be fatal? */
542 * +-------------------------------------------------------+
544 * | URI and path functions
546 * +-------------------------------------------------------+
549 /* return number of chars of the scheme (incl. '://')
550 * if the URI is absolute (includes a scheme etc.)
553 * NOTE: If you add new schemes here, please have a
554 * look at escape_absolute_uri and splitout_queryargs.
555 * Not every scheme takes query strings and some schemes
556 * may be handled in a special way.
558 * XXX: we may consider a scheme registry, perhaps with
559 * appropriate escape callbacks to allow other modules
560 * to extend mod_rewrite at runtime.
562 static unsigned is_absolute_uri(char *uri)
565 if (*uri == '/' || strlen(uri) <= 5) {
572 if (!strncasecmp(uri, "tp://", 5)) { /* ftp:// */
579 if (!strncasecmp(uri, "opher://", 8)) { /* gopher:// */
586 if (!strncasecmp(uri, "ttp://", 6)) { /* http:// */
589 else if (!strncasecmp(uri, "ttps://", 7)) { /* https:// */
596 if (!strncasecmp(uri, "dap://", 6)) { /* ldap:// */
603 if (!strncasecmp(uri, "ailto:", 6)) { /* mailto: */
610 if (!strncasecmp(uri, "ews:", 4)) { /* news: */
613 else if (!strncasecmp(uri, "ntp://", 6)) { /* nntp:// */
623 * escape absolute uri, which may or may not be path oriented.
624 * So let's handle them differently.
626 static char *escape_absolute_uri(apr_pool_t *p, char *uri, unsigned scheme)
631 * NULL should indicate elsewhere, that something's wrong
633 if (!scheme || strlen(uri) < scheme) {
639 /* scheme with authority part? */
642 while (*cp && *cp != '/') {
646 /* nothing after the hostpart. ready! */
647 if (!*cp || !*++cp) {
648 return apr_pstrdup(p, uri);
651 /* remember the hostname stuff */
654 /* special thing for ldap.
655 * The parts are separated by question marks. From RFC 2255:
656 * ldapurl = scheme "://" [hostport] ["/"
657 * [dn ["?" [attributes] ["?" [scope]
658 * ["?" [filter] ["?" extensions]]]]]]
660 if (!strncasecmp(uri, "ldap", 4)) {
664 token[0] = cp = apr_pstrdup(p, cp);
665 while (*cp && c < 5) {
673 return apr_pstrcat(p, apr_pstrndup(p, uri, scheme),
674 ap_escape_uri(p, token[0]),
675 (c >= 1) ? "?" : NULL,
676 (c >= 1) ? ap_escape_uri(p, token[1]) : NULL,
677 (c >= 2) ? "?" : NULL,
678 (c >= 2) ? ap_escape_uri(p, token[2]) : NULL,
679 (c >= 3) ? "?" : NULL,
680 (c >= 3) ? ap_escape_uri(p, token[3]) : NULL,
681 (c >= 4) ? "?" : NULL,
682 (c >= 4) ? ap_escape_uri(p, token[4]) : NULL,
687 /* Nothing special here. Apply normal escaping. */
688 return apr_pstrcat(p, apr_pstrndup(p, uri, scheme),
689 ap_escape_uri(p, cp), NULL);
693 * split out a QUERY_STRING part from
694 * the current URI string
696 static void splitout_queryargs(request_rec *r, int qsappend)
700 /* don't touch, unless it's an http or mailto URL.
701 * See RFC 1738 and RFC 2368.
703 if ( is_absolute_uri(r->filename)
704 && strncasecmp(r->filename, "http", 4)
705 && strncasecmp(r->filename, "mailto", 6)) {
706 r->args = NULL; /* forget the query that's still flying around */
710 q = ap_strchr(r->filename, '?');
715 olduri = apr_pstrdup(r->pool, r->filename);
718 r->args = apr_pstrcat(r->pool, q, "&", r->args, NULL);
721 r->args = apr_pstrdup(r->pool, q);
724 len = strlen(r->args);
728 else if (r->args[len-1] == '&') {
729 r->args[len-1] = '\0';
732 rewritelog(r, 3, "split uri=%s -> uri=%s, args=%s", olduri,
733 r->filename, r->args ? r->args : "<none>");
740 * strip 'http[s]://ourhost/' from URI
742 static void reduce_uri(request_rec *r)
747 cp = (char *)ap_http_method(r);
749 if ( strlen(r->filename) > l+3
750 && strncasecmp(r->filename, cp, l) == 0
751 && r->filename[l] == ':'
752 && r->filename[l+1] == '/'
753 && r->filename[l+2] == '/' ) {
756 char *portp, *host, *url, *scratch;
758 scratch = apr_pstrdup(r->pool, r->filename); /* our scratchpad */
760 /* cut the hostname and port out of the URI */
761 cp = host = scratch + l + 3; /* 3 == strlen("://") */
762 while (*cp && *cp != '/' && *cp != ':') {
766 if (*cp == ':') { /* additional port given */
769 while (*cp && *cp != '/') {
775 url = r->filename + (cp - scratch);
780 else if (*cp == '/') { /* default port */
783 port = ap_default_port(r);
784 url = r->filename + (cp - scratch);
787 port = ap_default_port(r);
791 /* now check whether we could reduce it to a local path... */
792 if (ap_matches_request_vhost(r, host, port)) {
793 rewritelog(r, 3, "reduce %s -> %s", r->filename, url);
794 r->filename = apr_pstrdup(r->pool, url);
802 * add 'http[s]://ourhost[:ourport]/' to URI
803 * if URI is still not fully qualified
805 static void fully_qualify_uri(request_rec *r)
807 if (!is_absolute_uri(r->filename)) {
808 const char *thisserver;
812 thisserver = ap_get_server_name(r);
813 port = ap_get_server_port(r);
814 thisport = ap_is_default_port(port, r)
816 : apr_psprintf(r->pool, ":%u", port);
818 r->filename = apr_psprintf(r->pool, "%s://%s%s%s%s",
819 ap_http_method(r), thisserver, thisport,
820 (*r->filename == '/') ? "" : "/",
828 * stat() only the first segment of a path
830 static int prefix_stat(const char *path, apr_pool_t *pool)
832 const char *curpath = path;
838 rv = apr_filepath_root(&root, &curpath, APR_FILEPATH_TRUENAME, pool);
840 if (rv != APR_SUCCESS) {
844 /* let's recognize slashes only, the mod_rewrite semantics are opaque
847 if ((slash = ap_strchr_c(curpath, '/')) != NULL) {
848 rv = apr_filepath_merge(&statpath, root,
849 apr_pstrndup(pool, curpath,
850 (apr_size_t)(slash - curpath)),
851 APR_FILEPATH_NOTABOVEROOT |
852 APR_FILEPATH_NOTRELATIVE, pool);
855 rv = apr_filepath_merge(&statpath, root, curpath,
856 APR_FILEPATH_NOTABOVEROOT |
857 APR_FILEPATH_NOTRELATIVE, pool);
860 if (rv == APR_SUCCESS) {
863 if (apr_stat(&sb, statpath, APR_FINFO_MIN, pool) == APR_SUCCESS) {
872 * substitute the prefix path 'match' in 'input' with 'subst' (RewriteBase)
874 static char *subst_prefix_path(request_rec *r, char *input, char *match,
877 apr_size_t len = strlen(match);
879 if (len && match[len - 1] == '/') {
883 if (!strncmp(input, match, len) && input[len++] == '/') {
884 apr_size_t slen, outlen;
887 rewritelog(r, 5, "strip matching prefix: %s -> %s", input, input+len);
889 slen = strlen(subst);
890 if (slen && subst[slen - 1] != '/') {
894 outlen = strlen(input) + slen - len;
895 output = apr_palloc(r->pool, outlen + 1); /* don't forget the \0 */
897 memcpy(output, subst, slen);
898 if (slen && !output[slen-1]) {
899 output[slen-1] = '/';
901 memcpy(output+slen, input+len, outlen - slen);
902 output[outlen] = '\0';
904 rewritelog(r, 4, "add subst prefix: %s -> %s", input+len, output);
909 /* prefix didn't match */
915 * +-------------------------------------------------------+
919 * +-------------------------------------------------------+
922 static void set_cache_value(const char *name, apr_time_t t, char *key,
929 apr_thread_mutex_lock(cachep->lock);
931 map = apr_hash_get(cachep->maps, name, APR_HASH_KEY_STRING);
936 if (apr_pool_create(&p, cachep->pool) != APR_SUCCESS) {
938 apr_thread_mutex_unlock(cachep->lock);
943 map = apr_palloc(cachep->pool, sizeof(cachedmap));
945 map->entries = apr_hash_make(map->pool);
948 apr_hash_set(cachep->maps, name, APR_HASH_KEY_STRING, map);
950 else if (map->mtime != t) {
951 apr_pool_clear(map->pool);
952 map->entries = apr_hash_make(map->pool);
956 /* Now we should have a valid map->entries hash, where we
957 * can store our value.
959 * We need to copy the key and the value into OUR pool,
960 * so that we don't leave it during the r->pool cleanup.
962 apr_hash_set(map->entries,
963 apr_pstrdup(map->pool, key), APR_HASH_KEY_STRING,
964 apr_pstrdup(map->pool, val));
967 apr_thread_mutex_unlock(cachep->lock);
974 static char *get_cache_value(const char *name, apr_time_t t, char *key,
982 apr_thread_mutex_lock(cachep->lock);
984 map = apr_hash_get(cachep->maps, name, APR_HASH_KEY_STRING);
987 /* if this map is outdated, forget it. */
988 if (map->mtime != t) {
989 apr_pool_clear(map->pool);
990 map->entries = apr_hash_make(map->pool);
994 val = apr_hash_get(map->entries, key, APR_HASH_KEY_STRING);
996 /* copy the cached value into the supplied pool,
997 * where it belongs (r->pool usually)
999 val = apr_pstrdup(p, val);
1005 apr_thread_mutex_unlock(cachep->lock);
1012 static int init_cache(apr_pool_t *p)
1014 cachep = apr_palloc(p, sizeof(cache));
1015 if (apr_pool_create(&cachep->pool, p) != APR_SUCCESS) {
1016 cachep = NULL; /* turns off cache */
1020 cachep->maps = apr_hash_make(cachep->pool);
1022 (void)apr_thread_mutex_create(&(cachep->lock), APR_THREAD_MUTEX_DEFAULT, p);
1030 * +-------------------------------------------------------+
1034 * +-------------------------------------------------------+
1038 * General Note: key is already a fresh string, created (expanded) just
1039 * for the purpose to be passed in here. So one can modify key itself.
1042 static char *rewrite_mapfunc_toupper(request_rec *r, char *key)
1046 for (p = key; *p; ++p) {
1047 *p = apr_toupper(*p);
1053 static char *rewrite_mapfunc_tolower(request_rec *r, char *key)
1057 for (p = key; *p; ++p) {
1058 *p = apr_tolower(*p);
1064 static char *rewrite_mapfunc_escape(request_rec *r, char *key)
1066 return ap_escape_uri(r->pool, key);
1069 static char *rewrite_mapfunc_unescape(request_rec *r, char *key)
1071 ap_unescape_url(key);
1076 static char *select_random_value_part(request_rec *r, char *value)
1081 /* count number of distinct values */
1082 while ((p = ap_strchr(p, '|')) != NULL) {
1088 /* initialize random generator
1090 * XXX: Probably this should be wrapped into a thread mutex,
1091 * shouldn't it? Is it worth the effort?
1093 if (!rewrite_rand_init_done) {
1094 srand((unsigned)(getpid()));
1095 rewrite_rand_init_done = 1;
1098 /* select a random subvalue */
1099 n = (int)(((double)(rand() % RAND_MAX) / RAND_MAX) * n + 1);
1101 /* extract it from the whole string */
1102 while (--n && (value = ap_strchr(value, '|')) != NULL) {
1106 if (value) { /* should not be NULL, but ... */
1107 p = ap_strchr(value, '|');
1117 /* child process code */
1118 static void rewrite_child_errfn(apr_pool_t *p, apr_status_t err,
1121 ap_log_error(APLOG_MARK, APLOG_ERR, err, NULL, "%s", desc);
1124 static apr_status_t rewritemap_program_child(apr_pool_t *p,
1125 const char *progname, char **argv,
1130 apr_procattr_t *procattr;
1131 apr_proc_t *procnew;
1133 if ( APR_SUCCESS == (rc=apr_procattr_create(&procattr, p))
1134 && APR_SUCCESS == (rc=apr_procattr_io_set(procattr, APR_FULL_BLOCK,
1135 APR_FULL_BLOCK, APR_NO_PIPE))
1136 && APR_SUCCESS == (rc=apr_procattr_dir_set(procattr,
1137 ap_make_dirstr_parent(p, argv[0])))
1138 && APR_SUCCESS == (rc=apr_procattr_cmdtype_set(procattr, APR_PROGRAM))
1139 && APR_SUCCESS == (rc=apr_procattr_child_errfn_set(procattr,
1140 rewrite_child_errfn))
1141 && APR_SUCCESS == (rc=apr_procattr_error_check_set(procattr, 1))) {
1143 procnew = apr_pcalloc(p, sizeof(*procnew));
1144 rc = apr_proc_create(procnew, argv[0], (const char **)argv, NULL,
1147 if (rc == APR_SUCCESS) {
1148 apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
1151 (*fpin) = procnew->in;
1155 (*fpout) = procnew->out;
1163 static apr_status_t run_rewritemap_programs(server_rec *s, apr_pool_t *p)
1165 rewrite_server_conf *conf;
1166 apr_hash_index_t *hi;
1169 conf = ap_get_module_config(s->module_config, &rewrite_module);
1171 /* If the engine isn't turned on,
1172 * don't even try to do anything.
1174 if (conf->state == ENGINE_DISABLED) {
1178 for (hi = apr_hash_first(p, conf->rewritemaps); hi; hi = apr_hash_next(hi)){
1179 apr_file_t *fpin = NULL;
1180 apr_file_t *fpout = NULL;
1181 rewritemap_entry *map;
1184 apr_hash_this(hi, NULL, NULL, &val);
1187 if (map->type != MAPTYPE_PRG) {
1190 if (!(map->argv[0]) || !*(map->argv[0]) || map->fpin || map->fpout) {
1194 rc = rewritemap_program_child(p, map->argv[0], map->argv,
1196 if (rc != APR_SUCCESS || fpin == NULL || fpout == NULL) {
1197 ap_log_error(APLOG_MARK, APLOG_ERR, rc, s,
1198 "mod_rewrite: could not start RewriteMap "
1199 "program %s", map->checkfile);
1211 * +-------------------------------------------------------+
1213 * | Lookup functions
1215 * +-------------------------------------------------------+
1218 static char *lookup_map_txtfile(request_rec *r, const char *file, char *key)
1220 apr_file_t *fp = NULL;
1221 char line[REWRITE_MAX_TXT_MAP_LINE + 1]; /* +1 for \0 */
1222 char *value, *keylast;
1224 if (apr_file_open(&fp, file, APR_READ, APR_OS_DEFAULT,
1225 r->pool) != APR_SUCCESS) {
1229 keylast = key + strlen(key);
1231 while (apr_file_gets(line, sizeof(line), fp) == APR_SUCCESS) {
1234 /* ignore comments and lines starting with whitespaces */
1235 if (*line == '#' || apr_isspace(*line)) {
1241 while (c < keylast && *p == *c && !apr_isspace(*p)) {
1246 /* key doesn't match - ignore. */
1247 if (c != keylast || !apr_isspace(*p)) {
1251 /* jump to the value */
1252 while (*p && apr_isspace(*p)) {
1256 /* no value? ignore */
1261 /* extract the value and return. */
1263 while (*p && !apr_isspace(*p)) {
1266 value = apr_pstrmemdup(r->pool, c, p - c);
1274 static char *lookup_map_dbmfile(request_rec *r, const char *file,
1275 const char *dbmtype, char *key)
1277 apr_dbm_t *dbmfp = NULL;
1282 if (apr_dbm_open_ex(&dbmfp, dbmtype, file, APR_DBM_READONLY, APR_OS_DEFAULT,
1283 r->pool) != APR_SUCCESS) {
1288 dbmkey.dsize = strlen(key);
1290 if (apr_dbm_fetch(dbmfp, dbmkey, &dbmval) == APR_SUCCESS && dbmval.dptr) {
1291 value = apr_pstrmemdup(r->pool, dbmval.dptr, dbmval.dsize);
1297 apr_dbm_close(dbmfp);
1302 static char *lookup_map_program(request_rec *r, apr_file_t *fpin,
1303 apr_file_t *fpout, char *key)
1305 char buf[REWRITE_MAX_PRG_MAP_LINE];
1312 struct iovec iova[2];
1316 /* when `RewriteEngine off' was used in the per-server
1317 * context then the rewritemap-programs were not spawned.
1318 * In this case using such a map (usually in per-dir context)
1319 * is useless because it is not available.
1321 * newlines in the key leave bytes in the pipe and cause
1322 * bad things to happen (next map lookup will use the chars
1323 * after the \n instead of the new key etc etc - in other words,
1324 * the Rewritemap falls out of sync with the requests).
1326 if (fpin == NULL || fpout == NULL || ap_strchr(key, '\n')) {
1331 if (rewrite_mapr_lock_acquire) {
1332 rv = apr_global_mutex_lock(rewrite_mapr_lock_acquire);
1333 if (rv != APR_SUCCESS) {
1334 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1335 "apr_global_mutex_lock(rewrite_mapr_lock_acquire) "
1337 return NULL; /* Maybe this should be fatal? */
1341 /* write out the request key */
1343 nbytes = strlen(key);
1344 apr_file_write(fpin, key, &nbytes);
1346 apr_file_write(fpin, "\n", &nbytes);
1348 iova[0].iov_base = key;
1349 iova[0].iov_len = strlen(key);
1350 iova[1].iov_base = "\n";
1351 iova[1].iov_len = 1;
1354 apr_file_writev(fpin, iova, niov, &nbytes);
1357 /* read in the response value */
1360 apr_file_read(fpout, &c, &nbytes);
1361 while (nbytes == 1 && (i < REWRITE_MAX_PRG_MAP_LINE)) {
1367 apr_file_read(fpout, &c, &nbytes);
1370 /* give the lock back */
1371 if (rewrite_mapr_lock_acquire) {
1372 rv = apr_global_mutex_unlock(rewrite_mapr_lock_acquire);
1373 if (rv != APR_SUCCESS) {
1374 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1375 "apr_global_mutex_unlock(rewrite_mapr_lock_acquire) "
1377 return NULL; /* Maybe this should be fatal? */
1381 if (i == 4 && strncasecmp(buf, "NULL", 4) == 0) {
1385 return apr_pstrmemdup(r->pool, buf, i);
1389 * generic map lookup
1391 static char *lookup_map(request_rec *r, char *name, char *key)
1393 rewrite_server_conf *conf;
1394 rewritemap_entry *s;
1399 /* get map configuration */
1400 conf = ap_get_module_config(r->server->module_config, &rewrite_module);
1401 s = apr_hash_get(conf->rewritemaps, name, APR_HASH_KEY_STRING);
1403 /* map doesn't exist */
1410 * Text file map (perhaps random)
1414 rv = apr_stat(&st, s->checkfile, APR_FINFO_MIN, r->pool);
1415 if (rv != APR_SUCCESS) {
1416 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1417 "mod_rewrite: can't access text RewriteMap file %s",
1419 rewritelog(r, 1, "can't open RewriteMap file, see error log");
1423 value = get_cache_value(name, st.mtime, key, r->pool);
1425 rewritelog(r, 6, "cache lookup FAILED, forcing new map lookup");
1427 value = lookup_map_txtfile(r, s->datafile, key);
1429 rewritelog(r, 5, "map lookup FAILED: map=%s[txt] key=%s",
1431 set_cache_value(name, st.mtime, key, "");
1435 rewritelog(r, 5, "map lookup OK: map=%s[txt] key=%s -> val=%s",
1437 set_cache_value(name, st.mtime, key, value);
1440 rewritelog(r, 5, "cache lookup OK: map=%s[txt] key=%s -> val=%s",
1444 if (s->type == MAPTYPE_RND && *value) {
1445 value = select_random_value_part(r, value);
1446 rewritelog(r, 5, "randomly chosen the subvalue `%s'", value);
1449 return *value ? value : NULL;
1455 rv = apr_stat(&st, s->checkfile, APR_FINFO_MIN, r->pool);
1456 if (rv != APR_SUCCESS) {
1457 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1458 "mod_rewrite: can't access DBM RewriteMap file %s",
1460 rewritelog(r, 1, "can't open DBM RewriteMap file, see error log");
1464 value = get_cache_value(name, st.mtime, key, r->pool);
1466 rewritelog(r, 6, "cache lookup FAILED, forcing new map lookup");
1468 value = lookup_map_dbmfile(r, s->datafile, s->dbmtype, key);
1470 rewritelog(r, 5, "map lookup FAILED: map=%s[dbm] key=%s",
1472 set_cache_value(name, st.mtime, key, "");
1476 rewritelog(r, 5, "map lookup OK: map=%s[dbm] key=%s -> val=%s",
1478 set_cache_value(name, st.mtime, key, value);
1482 rewritelog(r, 5, "cache lookup OK: map=%s[dbm] key=%s -> val=%s",
1484 return *value ? value : NULL;
1490 value = lookup_map_program(r, s->fpin, s->fpout, key);
1492 rewritelog(r, 5, "map lookup FAILED: map=%s key=%s", name, key);
1496 rewritelog(r, 5, "map lookup OK: map=%s key=%s -> val=%s",
1504 value = s->func(r, key);
1506 rewritelog(r, 5, "map lookup FAILED: map=%s key=%s", name, key);
1510 rewritelog(r, 5, "map lookup OK: map=%s key=%s -> val=%s",
1519 * lookup a HTTP header and set VARY note
1521 static const char *lookup_header(request_rec *r, const char *name)
1523 const char *val = apr_table_get(r->headers_in, name);
1526 apr_table_merge(r->notes, VARY_KEY_THIS, name);
1533 * generic variable lookup
1535 static char *lookup_variable(request_rec *r, char *var)
1538 apr_size_t varlen = strlen(var);
1542 return apr_pstrdup(r->pool, "");
1547 /* fast tests for variable length variables (sic) first */
1548 if (var[3] == ':') {
1549 if (var[4] && !strncasecmp(var, "ENV", 3)) {
1551 result = apr_table_get(r->notes, var);
1554 result = apr_table_get(r->subprocess_env, var);
1557 result = getenv(var);
1561 else if (var[4] == ':') {
1565 if (!strncasecmp(var, "HTTP", 4)) {
1566 result = lookup_header(r, var+5);
1568 else if (!strncasecmp(var, "LA-U", 4)) {
1569 if (r->filename && subreq_ok(r)) {
1570 rr = ap_sub_req_lookup_uri(r->filename, r, NULL);
1571 result = apr_pstrdup(r->pool, lookup_variable(rr, var+5));
1572 ap_destroy_sub_req(rr);
1574 rewritelog(r, 5, "lookahead: path=%s var=%s -> val=%s",
1575 r->filename, var+5, result);
1577 return (char *)result;
1580 else if (!strncasecmp(var, "LA-F", 4)) {
1581 if (r->filename && subreq_ok(r)) {
1582 rr = ap_sub_req_lookup_file(r->filename, r, NULL);
1583 result = apr_pstrdup(r->pool, lookup_variable(rr, var+5));
1584 ap_destroy_sub_req(rr);
1586 rewritelog(r, 5, "lookahead: path=%s var=%s -> val=%s",
1587 r->filename, var+5, result);
1589 return (char *)result;
1595 /* well, do it the hard way */
1600 /* can't do this above, because of the getenv call */
1601 for (p = var; *p; ++p) {
1602 *p = apr_toupper(*p);
1607 if (!strcmp(var, "TIME")) {
1608 apr_time_exp_lt(&tm, apr_time_now());
1609 result = apr_psprintf(r->pool, "%04d%02d%02d%02d%02d%02d",
1610 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
1611 tm.tm_hour, tm.tm_min, tm.tm_sec);
1612 rewritelog(r, 1, "RESULT='%s'", result);
1613 return (char *)result;
1620 if (!strcmp(var, "TIME_DAY")) {
1621 apr_time_exp_lt(&tm, apr_time_now());
1622 return apr_psprintf(r->pool, "%02d", tm.tm_mday);
1627 if (!strcmp(var, "TIME_SEC")) {
1628 apr_time_exp_lt(&tm, apr_time_now());
1629 return apr_psprintf(r->pool, "%02d", tm.tm_sec);
1634 if (!strcmp(var, "TIME_MIN")) {
1635 apr_time_exp_lt(&tm, apr_time_now());
1636 return apr_psprintf(r->pool, "%02d", tm.tm_min);
1641 if (!strcmp(var, "TIME_MON")) {
1642 apr_time_exp_lt(&tm, apr_time_now());
1643 return apr_psprintf(r->pool, "%02d", tm.tm_mon+1);
1652 if (var[8] == 'Y' && !strcmp(var, "TIME_WDAY")) {
1653 apr_time_exp_lt(&tm, apr_time_now());
1654 return apr_psprintf(r->pool, "%d", tm.tm_wday);
1656 else if (!strcmp(var, "TIME_YEAR")) {
1657 apr_time_exp_lt(&tm, apr_time_now());
1658 return apr_psprintf(r->pool, "%04d", tm.tm_year+1900);
1663 if (!strcmp(var, "IS_SUBREQ")) {
1664 result = (r->main ? "true" : "false");
1669 if (!strcmp(var, "PATH_INFO")) {
1670 result = r->path_info;
1675 if (!strcmp(var, "AUTH_TYPE")) {
1676 result = r->ap_auth_type;
1681 if (!strcmp(var, "HTTP_HOST")) {
1682 result = lookup_header(r, "Host");
1687 if (!strcmp(var, "TIME_HOUR")) {
1688 apr_time_exp_lt(&tm, apr_time_now());
1689 return apr_psprintf(r->pool, "%02d", tm.tm_hour);
1698 if (!strcmp(var, "SERVER_NAME")) {
1699 result = ap_get_server_name(r);
1704 if (*var == 'R' && !strcmp(var, "REMOTE_ADDR")) {
1705 result = r->connection->remote_ip;
1707 else if (!strcmp(var, "SERVER_ADDR")) {
1708 result = r->connection->local_ip;
1713 if (*var == 'H' && !strcmp(var, "HTTP_ACCEPT")) {
1714 result = lookup_header(r, "Accept");
1716 else if (!strcmp(var, "THE_REQUEST")) {
1717 result = r->the_request;
1722 if (!strcmp(var, "API_VERSION")) {
1723 return apr_psprintf(r->pool, "%d:%d",
1724 MODULE_MAGIC_NUMBER_MAJOR,
1725 MODULE_MAGIC_NUMBER_MINOR);
1730 if (!strcmp(var, "HTTP_COOKIE")) {
1731 result = lookup_header(r, "Cookie");
1736 if (*var == 'R' && !strcmp(var, "REMOTE_HOST")) {
1737 result = ap_get_remote_host(r->connection,r->per_dir_config,
1740 else if (!strcmp(var, "SERVER_PORT")) {
1741 return apr_psprintf(r->pool, "%u", ap_get_server_port(r));
1746 if (*var == 'R' && !strcmp(var, "REMOTE_USER")) {
1749 else if (!strcmp(var, "SCRIPT_USER")) {
1750 result = "<unknown>";
1751 if (r->finfo.valid & APR_FINFO_USER) {
1752 apr_uid_name_get((char **)&result, r->finfo.user,
1759 if (!strcmp(var, "REQUEST_URI")) {
1769 if (!strcmp(var, "SCRIPT_GROUP")) {
1770 result = "<unknown>";
1771 if (r->finfo.valid & APR_FINFO_GROUP) {
1772 apr_gid_name_get((char **)&result, r->finfo.group,
1779 if (!strcmp(var, "REMOTE_IDENT")) {
1780 result = ap_get_remote_logname(r);
1785 if (!strcmp(var, "HTTP_REFERER")) {
1786 result = lookup_header(r, "Referer");
1791 if (!strcmp(var, "QUERY_STRING")) {
1797 if (!strcmp(var, "SERVER_ADMIN")) {
1798 result = r->server->server_admin;
1805 if (!strcmp(var, "DOCUMENT_ROOT")) {
1806 result = ap_document_root(r);
1811 if (*var == 'H' && !strcmp(var, "HTTP_FORWARDED")) {
1812 result = lookup_header(r, "Forwarded");
1814 else if (!strcmp(var, "REQUEST_METHOD")) {
1822 if (!strcmp(var, "HTTP_USER_AGENT")) {
1823 result = lookup_header(r, "User-Agent");
1828 if (!strcmp(var, "SCRIPT_FILENAME")) {
1829 result = r->filename; /* same as request_filename (16) */
1834 if (!strcmp(var, "SERVER_PROTOCOL")) {
1835 result = r->protocol;
1840 if (!strcmp(var, "SERVER_SOFTWARE")) {
1841 result = ap_get_server_version();
1848 if (!strcmp(var, "REQUEST_FILENAME")) {
1849 result = r->filename; /* same as script_filename (15) */
1854 if (!strcmp(var, "HTTP_PROXY_CONNECTION")) {
1855 result = lookup_header(r, "Proxy-Connection");
1861 return apr_pstrdup(r->pool, result ? result : "");
1866 * +-------------------------------------------------------+
1868 * | Expansion functions
1870 * +-------------------------------------------------------+
1874 * Bracketed expression handling
1875 * s points after the opening bracket
1877 static APR_INLINE char *find_closing_curly(char *s)
1881 for (depth = 1; *s; ++s) {
1882 if (*s == RIGHT_CURLY && --depth == 0) {
1885 else if (*s == LEFT_CURLY) {
1893 static APR_INLINE char *find_char_in_curlies(char *s, int c)
1897 for (depth = 1; *s; ++s) {
1898 if (*s == c && depth == 1) {
1901 else if (*s == RIGHT_CURLY && --depth == 0) {
1904 else if (*s == LEFT_CURLY) {
1912 /* perform all the expansions on the input string
1913 * putting the result into a new string
1915 * for security reasons this expansion must be performed in a
1916 * single pass, otherwise an attacker can arrange for the result
1917 * of an earlier expansion to include expansion specifiers that
1918 * are interpreted by a later expansion, producing results that
1919 * were not intended by the administrator.
1921 static char *do_expand(request_rec *r, char *input,
1922 backrefinfo *briRR, backrefinfo *briRC)
1924 result_list *result, *current;
1925 apr_size_t span, inputlen, outlen;
1928 span = strcspn(input, "\\$%");
1929 inputlen = strlen(input);
1932 if (inputlen == span) {
1933 return apr_pstrdup(r->pool, input);
1936 /* well, actually something to do */
1937 result = current = apr_palloc(r->pool, sizeof(result_list));
1940 current->next = NULL;
1941 current->string = input;
1942 current->len = span;
1945 /* loop for specials */
1947 /* prepare next entry */
1949 current->next = apr_palloc(r->pool, sizeof(result_list));
1950 current = current->next;
1951 current->next = NULL;
1955 /* escaped character */
1960 current->string = p;
1964 current->string = ++p;
1969 /* variable or map lookup */
1970 else if (p[1] == '{') {
1973 endp = find_closing_curly(p+2);
1976 current->string = p;
1981 /* variable lookup */
1982 else if (*p == '%') {
1983 p = lookup_variable(r, apr_pstrmemdup(r->pool, p+2, endp-p-2));
1986 current->len = span;
1987 current->string = p;
1993 else { /* *p == '$' */
1997 * To make rewrite maps useful, the lookup key and
1998 * default values must be expanded, so we make
1999 * recursive calls to do the work. For security
2000 * reasons we must never expand a string that includes
2001 * verbatim data from the network. The recursion here
2002 * isn't a problem because the result of expansion is
2003 * only passed to lookup_map() so it cannot be
2004 * re-expanded, only re-looked-up. Another way of
2005 * looking at it is that the recursion is entirely
2006 * driven by the syntax of the nested curly brackets.
2009 key = find_char_in_curlies(p+2, ':');
2012 current->string = p;
2019 map = apr_pstrmemdup(r->pool, p+2, endp-p-2);
2020 key = map + (key-p-2);
2022 dflt = find_char_in_curlies(key, '|');
2030 /* reuse of key variable as result */
2031 key = lookup_map(r, map, do_expand(r, key, briRR, briRC));
2033 if (!key && *dflt) {
2034 key = do_expand(r, dflt, briRR, briRC);
2039 current->len = span;
2040 current->string = key;
2050 else if (apr_isdigit(p[1])) {
2052 backrefinfo *bri = (*p == '$') ? briRR : briRC;
2054 /* see ap_pregsub() in server/util.c */
2055 if (bri && n <= bri->nsub
2056 && bri->regmatch[n].rm_eo > bri->regmatch[n].rm_so) {
2057 span = bri->regmatch[n].rm_eo - bri->regmatch[n].rm_so;
2059 current->len = span;
2060 current->string = bri->source + bri->regmatch[n].rm_so;
2067 /* not for us, just copy it */
2070 current->string = p++;
2074 /* check the remainder */
2075 if (*p && (span = strcspn(p, "\\$%")) > 0) {
2077 current->next = apr_palloc(r->pool, sizeof(result_list));
2078 current = current->next;
2079 current->next = NULL;
2082 current->len = span;
2083 current->string = p;
2088 } while (p < input+inputlen);
2090 /* assemble result */
2091 c = p = apr_palloc(r->pool, outlen + 1); /* don't forget the \0 */
2094 ap_assert(c+result->len <= p+outlen); /* XXX: can be removed after
2095 * extensive testing and
2098 memcpy(c, result->string, result->len);
2101 result = result->next;
2110 * perform all the expansions on the environment variables
2112 static void do_expand_env(request_rec *r, data_item *env,
2113 backrefinfo *briRR, backrefinfo *briRC)
2118 name = do_expand(r, env->data, briRR, briRC);
2119 if ((val = ap_strchr(name, ':')) != NULL) {
2122 apr_table_set(r->subprocess_env, name, val);
2123 rewritelog(r, 5, "setting env variable '%s' to '%s'", name, val);
2133 * perform all the expansions on the cookies
2135 * TODO: use cached time similar to how logging does it
2137 static void add_cookie(request_rec *r, char *s)
2148 var = apr_strtok(s, ":", &tok_cntx);
2149 val = apr_strtok(NULL, ":", &tok_cntx);
2150 domain = apr_strtok(NULL, ":", &tok_cntx);
2152 if (var && val && domain) {
2153 request_rec *rmain = r;
2157 while (rmain->main) {
2158 rmain = rmain->main;
2161 notename = apr_pstrcat(rmain->pool, var, "_rewrite", NULL);
2162 apr_pool_userdata_get(&data, notename, rmain->pool);
2164 expires = apr_strtok(NULL, ":", &tok_cntx);
2165 path = expires ? apr_strtok(NULL, ":", &tok_cntx) : NULL;
2167 cookie = apr_pstrcat(rmain->pool,
2169 "; path=", (path)? path : "/",
2170 "; domain=", domain,
2171 (expires)? "; expires=" : NULL,
2175 apr_time_from_sec((60 *
2177 "%a, %d-%b-%Y %T GMT", 1)
2181 apr_table_add(rmain->err_headers_out, "Set-Cookie", cookie);
2182 apr_pool_userdata_set("set", notename, NULL, rmain->pool);
2183 rewritelog(rmain, 5, "setting cookie '%s'", cookie);
2186 rewritelog(rmain, 5, "skipping already set cookie '%s'", var);
2193 static void do_expand_cookie(request_rec *r, data_item *cookie,
2194 backrefinfo *briRR, backrefinfo *briRC)
2197 add_cookie(r, do_expand(r, cookie->data, briRR, briRC));
2198 cookie = cookie->next;
2206 * Expand tilde-paths (/~user) through Unix /etc/passwd
2207 * database information (or other OS-specific database)
2209 static char *expand_tildepaths(request_rec *r, char *uri)
2211 if (uri && *uri == '/' && uri[1] == '~') {
2215 while (*p && *p != '/') {
2222 user = apr_pstrmemdup(r->pool, user, p-user);
2223 if (apr_uid_homepath_get(&homedir, user, r->pool) == APR_SUCCESS) {
2225 /* reuse of user variable */
2226 user = homedir + strlen(homedir) - 1;
2227 if (user >= homedir && *user == '/') {
2231 return apr_pstrcat(r->pool, homedir, p, NULL);
2242 #endif /* if APR_HAS_USER */
2246 * +-------------------------------------------------------+
2248 * | rewriting lockfile support
2250 * +-------------------------------------------------------+
2253 static apr_status_t rewritelock_create(server_rec *s, apr_pool_t *p)
2257 /* only operate if a lockfile is used */
2258 if (lockname == NULL || *(lockname) == '\0') {
2262 /* create the lockfile */
2263 rc = apr_global_mutex_create(&rewrite_mapr_lock_acquire, lockname,
2264 APR_LOCK_DEFAULT, p);
2265 if (rc != APR_SUCCESS) {
2266 ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s,
2267 "mod_rewrite: Parent could not create RewriteLock "
2268 "file %s", lockname);
2272 #ifdef MOD_REWRITE_SET_MUTEX_PERMS
2273 rc = unixd_set_global_mutex_perms(rewrite_mapr_lock_acquire);
2274 if (rc != APR_SUCCESS) {
2275 ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s,
2276 "mod_rewrite: Parent could not set permissions "
2277 "on RewriteLock; check User and Group directives");
2285 static apr_status_t rewritelock_remove(void *data)
2287 /* only operate if a lockfile is used */
2288 if (lockname == NULL || *(lockname) == '\0') {
2292 /* destroy the rewritelock */
2293 apr_global_mutex_destroy (rewrite_mapr_lock_acquire);
2294 rewrite_mapr_lock_acquire = NULL;
2301 * +-------------------------------------------------------+
2303 * | configuration directive handling
2305 * +-------------------------------------------------------+
2309 * own command line parser for RewriteRule and RewriteCond,
2310 * which doesn't have the '\\' problem.
2311 * (returns true on error)
2313 * XXX: what an inclined parser. Seems we have to leave it so
2314 * for backwards compat. *sigh*
2316 static int parseargline(char *str, char **a1, char **a2, char **a3)
2320 while (apr_isspace(*str)) {
2325 * determine first argument
2327 quote = (*str == '"' || *str == '\'') ? *str++ : '\0';
2330 for (; *str; ++str) {
2331 if ((apr_isspace(*str) && !quote) || (*str == quote)) {
2334 if (*str == '\\' && apr_isspace(str[1])) {
2345 while (apr_isspace(*str)) {
2350 * determine second argument
2352 quote = (*str == '"' || *str == '\'') ? *str++ : '\0';
2355 for (; *str; ++str) {
2356 if ((apr_isspace(*str) && !quote) || (*str == quote)) {
2359 if (*str == '\\' && apr_isspace(str[1])) {
2366 *a3 = NULL; /* 3rd argument is optional */
2371 while (apr_isspace(*str)) {
2376 *a3 = NULL; /* 3rd argument is still optional */
2381 * determine third argument
2383 quote = (*str == '"' || *str == '\'') ? *str++ : '\0';
2385 for (; *str; ++str) {
2386 if ((apr_isspace(*str) && !quote) || (*str == quote)) {
2389 if (*str == '\\' && apr_isspace(str[1])) {
2399 static void *config_server_create(apr_pool_t *p, server_rec *s)
2401 rewrite_server_conf *a;
2403 a = (rewrite_server_conf *)apr_pcalloc(p, sizeof(rewrite_server_conf));
2405 a->state = ENGINE_DISABLED;
2406 a->options = OPTION_NONE;
2407 a->rewritelogfile = NULL;
2408 a->rewritelogfp = NULL;
2409 a->rewriteloglevel = 0;
2410 a->rewritemaps = apr_hash_make(p);
2411 a->rewriteconds = apr_array_make(p, 2, sizeof(rewritecond_entry));
2412 a->rewriterules = apr_array_make(p, 2, sizeof(rewriterule_entry));
2414 a->redirect_limit = 0; /* unset (use default) */
2419 static void *config_server_merge(apr_pool_t *p, void *basev, void *overridesv)
2421 rewrite_server_conf *a, *base, *overrides;
2423 a = (rewrite_server_conf *)apr_pcalloc(p,
2424 sizeof(rewrite_server_conf));
2425 base = (rewrite_server_conf *)basev;
2426 overrides = (rewrite_server_conf *)overridesv;
2428 a->state = overrides->state;
2429 a->options = overrides->options;
2430 a->server = overrides->server;
2431 a->redirect_limit = overrides->redirect_limit
2432 ? overrides->redirect_limit
2433 : base->redirect_limit;
2435 if (a->options & OPTION_INHERIT) {
2437 * local directives override
2438 * and anything else is inherited
2440 a->rewriteloglevel = overrides->rewriteloglevel != 0
2441 ? overrides->rewriteloglevel
2442 : base->rewriteloglevel;
2443 a->rewritelogfile = overrides->rewritelogfile != NULL
2444 ? overrides->rewritelogfile
2445 : base->rewritelogfile;
2446 a->rewritelogfp = overrides->rewritelogfp != NULL
2447 ? overrides->rewritelogfp
2448 : base->rewritelogfp;
2449 a->rewritemaps = apr_hash_overlay(p, overrides->rewritemaps,
2451 a->rewriteconds = apr_array_append(p, overrides->rewriteconds,
2452 base->rewriteconds);
2453 a->rewriterules = apr_array_append(p, overrides->rewriterules,
2454 base->rewriterules);
2458 * local directives override
2459 * and anything else gets defaults
2461 a->rewriteloglevel = overrides->rewriteloglevel;
2462 a->rewritelogfile = overrides->rewritelogfile;
2463 a->rewritelogfp = overrides->rewritelogfp;
2464 a->rewritemaps = overrides->rewritemaps;
2465 a->rewriteconds = overrides->rewriteconds;
2466 a->rewriterules = overrides->rewriterules;
2472 static void *config_perdir_create(apr_pool_t *p, char *path)
2474 rewrite_perdir_conf *a;
2476 a = (rewrite_perdir_conf *)apr_pcalloc(p, sizeof(rewrite_perdir_conf));
2478 a->state = ENGINE_DISABLED;
2479 a->options = OPTION_NONE;
2481 a->rewriteconds = apr_array_make(p, 2, sizeof(rewritecond_entry));
2482 a->rewriterules = apr_array_make(p, 2, sizeof(rewriterule_entry));
2483 a->redirect_limit = 0; /* unset (use server config) */
2486 a->directory = NULL;
2489 /* make sure it has a trailing slash */
2490 if (path[strlen(path)-1] == '/') {
2491 a->directory = apr_pstrdup(p, path);
2494 a->directory = apr_pstrcat(p, path, "/", NULL);
2501 static void *config_perdir_merge(apr_pool_t *p, void *basev, void *overridesv)
2503 rewrite_perdir_conf *a, *base, *overrides;
2505 a = (rewrite_perdir_conf *)apr_pcalloc(p,
2506 sizeof(rewrite_perdir_conf));
2507 base = (rewrite_perdir_conf *)basev;
2508 overrides = (rewrite_perdir_conf *)overridesv;
2510 a->state = overrides->state;
2511 a->options = overrides->options;
2512 a->directory = overrides->directory;
2513 a->baseurl = overrides->baseurl;
2514 a->redirect_limit = overrides->redirect_limit
2515 ? overrides->redirect_limit
2516 : base->redirect_limit;
2518 if (a->options & OPTION_INHERIT) {
2519 a->rewriteconds = apr_array_append(p, overrides->rewriteconds,
2520 base->rewriteconds);
2521 a->rewriterules = apr_array_append(p, overrides->rewriterules,
2522 base->rewriterules);
2525 a->rewriteconds = overrides->rewriteconds;
2526 a->rewriterules = overrides->rewriterules;
2532 static const char *cmd_rewriteengine(cmd_parms *cmd,
2533 void *in_dconf, int flag)
2535 rewrite_perdir_conf *dconf = in_dconf;
2536 rewrite_server_conf *sconf;
2538 sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
2540 if (cmd->path == NULL) { /* is server command */
2541 sconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
2543 else /* is per-directory command */ {
2544 dconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
2550 static const char *cmd_rewriteoptions(cmd_parms *cmd,
2551 void *in_dconf, const char *option)
2553 int options = 0, limit = 0;
2557 w = ap_getword_conf(cmd->pool, &option);
2559 if (!strcasecmp(w, "inherit")) {
2560 options |= OPTION_INHERIT;
2562 else if (!strncasecmp(w, "MaxRedirects=", 13)) {
2563 limit = atoi(&w[13]);
2565 return "RewriteOptions: MaxRedirects takes a number greater "
2569 else if (!strcasecmp(w, "MaxRedirects")) { /* be nice */
2570 return "RewriteOptions: MaxRedirects has the format MaxRedirects"
2574 return apr_pstrcat(cmd->pool, "RewriteOptions: unknown option '",
2579 /* put it into the appropriate config */
2580 if (cmd->path == NULL) { /* is server command */
2581 rewrite_server_conf *conf =
2582 ap_get_module_config(cmd->server->module_config,
2585 conf->options |= options;
2586 conf->redirect_limit = limit;
2588 else { /* is per-directory command */
2589 rewrite_perdir_conf *conf = in_dconf;
2591 conf->options |= options;
2592 conf->redirect_limit = limit;
2598 static const char *cmd_rewritelog(cmd_parms *cmd, void *dconf, const char *a1)
2600 rewrite_server_conf *sconf;
2602 sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
2603 sconf->rewritelogfile = a1;
2608 static const char *cmd_rewriteloglevel(cmd_parms *cmd, void *dconf,
2611 rewrite_server_conf *sconf;
2613 sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
2614 sconf->rewriteloglevel = atoi(a1);
2619 static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
2622 rewrite_server_conf *sconf;
2623 rewritemap_entry *newmap;
2627 sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
2629 newmap = apr_palloc(cmd->pool, sizeof(rewritemap_entry));
2630 newmap->func = NULL;
2632 if (strncasecmp(a2, "txt:", 4) == 0) {
2633 if ((fname = ap_server_root_relative(cmd->pool, a2+4)) == NULL) {
2634 return apr_pstrcat(cmd->pool, "RewriteMap: bad path to txt map: ",
2638 newmap->type = MAPTYPE_TXT;
2639 newmap->datafile = fname;
2640 newmap->checkfile = fname;
2642 else if (strncasecmp(a2, "rnd:", 4) == 0) {
2643 if ((fname = ap_server_root_relative(cmd->pool, a2+4)) == NULL) {
2644 return apr_pstrcat(cmd->pool, "RewriteMap: bad path to rnd map: ",
2648 newmap->type = MAPTYPE_RND;
2649 newmap->datafile = fname;
2650 newmap->checkfile = fname;
2652 else if (strncasecmp(a2, "dbm", 3) == 0) {
2653 const char *ignored_fname;
2656 newmap->type = MAPTYPE_DBM;
2660 newmap->dbmtype = "default";
2663 else if (a2[3] == '=') {
2664 const char *colon = ap_strchr_c(a2 + 4, ':');
2667 newmap->dbmtype = apr_pstrndup(cmd->pool, a2 + 4,
2668 colon - (a2 + 3) - 1);
2674 return apr_pstrcat(cmd->pool, "RewriteMap: bad map:",
2678 if ((newmap->datafile = ap_server_root_relative(cmd->pool,
2680 return apr_pstrcat(cmd->pool, "RewriteMap: bad path to dbm map: ",
2684 rv = apr_dbm_get_usednames_ex(cmd->pool, newmap->dbmtype,
2685 newmap->datafile, &newmap->checkfile,
2687 if (rv != APR_SUCCESS) {
2688 return apr_pstrcat(cmd->pool, "RewriteMap: dbm type ",
2689 newmap->dbmtype, " is invalid", NULL);
2692 else if (strncasecmp(a2, "prg:", 4) == 0) {
2693 apr_tokenize_to_argv(a2 + 4, &newmap->argv, cmd->pool);
2695 fname = newmap->argv[0];
2696 if ((newmap->argv[0] = ap_server_root_relative(cmd->pool,
2698 return apr_pstrcat(cmd->pool, "RewriteMap: bad path to prg map: ",
2702 newmap->type = MAPTYPE_PRG;
2703 newmap->datafile = NULL;
2704 newmap->checkfile = newmap->argv[0];
2706 else if (strncasecmp(a2, "int:", 4) == 0) {
2707 newmap->type = MAPTYPE_INT;
2708 newmap->datafile = NULL;
2709 newmap->checkfile = NULL;
2710 newmap->func = (char *(*)(request_rec *,char *))
2711 apr_hash_get(mapfunc_hash, a2+4, strlen(a2+4));
2712 if ((sconf->state == ENGINE_ENABLED) && (newmap->func == NULL)) {
2713 return apr_pstrcat(cmd->pool, "RewriteMap: internal map not found:",
2718 if ((fname = ap_server_root_relative(cmd->pool, a2)) == NULL) {
2719 return apr_pstrcat(cmd->pool, "RewriteMap: bad path to txt map: ",
2723 newmap->type = MAPTYPE_TXT;
2724 newmap->datafile = fname;
2725 newmap->checkfile = fname;
2727 newmap->fpin = NULL;
2728 newmap->fpout = NULL;
2730 if (newmap->checkfile && (sconf->state == ENGINE_ENABLED)
2731 && (apr_stat(&st, newmap->checkfile, APR_FINFO_MIN,
2732 cmd->pool) != APR_SUCCESS)) {
2733 return apr_pstrcat(cmd->pool,
2734 "RewriteMap: file for map ", a1,
2735 " not found:", newmap->checkfile, NULL);
2738 apr_hash_set(sconf->rewritemaps, a1, APR_HASH_KEY_STRING, newmap);
2743 static const char *cmd_rewritelock(cmd_parms *cmd, void *dconf, const char *a1)
2747 if ((error = ap_check_cmd_context(cmd, GLOBAL_ONLY)) != NULL)
2750 /* fixup the path, especially for rewritelock_remove() */
2751 lockname = ap_server_root_relative(cmd->pool, a1);
2754 return apr_pstrcat(cmd->pool, "Invalid RewriteLock path ", a1);
2760 static const char *cmd_rewritebase(cmd_parms *cmd, void *in_dconf,
2763 rewrite_perdir_conf *dconf = in_dconf;
2765 if (cmd->path == NULL || dconf == NULL) {
2766 return "RewriteBase: only valid in per-directory config files";
2768 if (a1[0] == '\0') {
2769 return "RewriteBase: empty URL not allowed";
2772 return "RewriteBase: argument is not a valid URL";
2775 dconf->baseurl = a1;
2781 * generic lexer for RewriteRule and RewriteCond flags.
2782 * The parser will be passed in as a function pointer
2783 * and called if a flag was found
2785 static const char *cmd_parseflagfield(apr_pool_t *p, void *cfg, char *key,
2786 const char *(*parse)(apr_pool_t *,
2790 char *val, *nextp, *endp;
2793 endp = key + strlen(key) - 1;
2794 if (*key != '[' || *endp != ']') {
2795 return "RewriteCond: bad flag delimiters";
2798 *endp = ','; /* for simpler parsing */
2802 /* skip leading spaces */
2803 while (apr_isspace(*key)) {
2807 if (!*key || (nextp = ap_strchr(key, ',')) == NULL) { /* NULL should not
2813 /* strip trailing spaces */
2815 while (apr_isspace(*endp)) {
2820 /* split key and val */
2821 val = ap_strchr(key, '=');
2829 err = parse(p, cfg, key, val);
2840 static const char *cmd_rewritecond_setflag(apr_pool_t *p, void *_cfg,
2841 char *key, char *val)
2843 rewritecond_entry *cfg = _cfg;
2845 if ( strcasecmp(key, "nocase") == 0
2846 || strcasecmp(key, "NC") == 0 ) {
2847 cfg->flags |= CONDFLAG_NOCASE;
2849 else if ( strcasecmp(key, "ornext") == 0
2850 || strcasecmp(key, "OR") == 0 ) {
2851 cfg->flags |= CONDFLAG_ORNEXT;
2854 return apr_pstrcat(p, "RewriteCond: unknown flag '", key, "'", NULL);
2859 static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
2862 rewrite_perdir_conf *dconf = in_dconf;
2863 char *str = apr_pstrdup(cmd->pool, in_str);
2864 rewrite_server_conf *sconf;
2865 rewritecond_entry *newcond;
2872 sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
2874 /* make a new entry in the internal temporary rewrite rule list */
2875 if (cmd->path == NULL) { /* is server command */
2876 newcond = apr_array_push(sconf->rewriteconds);
2878 else { /* is per-directory command */
2879 newcond = apr_array_push(dconf->rewriteconds);
2882 /* parse the argument line ourself
2883 * a1 .. a3 are substrings of str, which is a fresh copy
2884 * of the argument line. So we can use a1 .. a3 without
2885 * copying them again.
2887 if (parseargline(str, &a1, &a2, &a3)) {
2888 return apr_pstrcat(cmd->pool, "RewriteCond: bad argument line '", str,
2892 /* arg1: the input string */
2893 newcond->input = a1;
2895 /* arg3: optional flags field
2896 * (this has to be parsed first, because we need to
2897 * know if the regex should be compiled with ICASE!)
2899 newcond->flags = CONDFLAG_NONE;
2901 if ((err = cmd_parseflagfield(cmd->pool, newcond, a3,
2902 cmd_rewritecond_setflag)) != NULL) {
2907 /* arg2: the pattern */
2909 newcond->flags |= CONDFLAG_NOTMATCH;
2913 /* determine the pattern type */
2916 if (!a2[2] && *a2 == '-') {
2918 case 'f': newcond->ptype = CONDPAT_FILE_EXISTS; break;
2919 case 's': newcond->ptype = CONDPAT_FILE_SIZE; break;
2920 case 'l': newcond->ptype = CONDPAT_FILE_LINK; break;
2921 case 'd': newcond->ptype = CONDPAT_FILE_DIR; break;
2922 case 'U': newcond->ptype = CONDPAT_LU_URL; break;
2923 case 'F': newcond->ptype = CONDPAT_LU_FILE; break;
2928 case '>': newcond->ptype = CONDPAT_STR_GT; break;
2929 case '<': newcond->ptype = CONDPAT_STR_LT; break;
2930 case '=': newcond->ptype = CONDPAT_STR_EQ;
2931 /* "" represents an empty string */
2932 if (*++a2 == '"' && a2[1] == '"' && !a2[2]) {
2940 if (newcond->ptype && newcond->ptype != CONDPAT_STR_EQ &&
2941 (newcond->flags & CONDFLAG_NOCASE)) {
2942 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2943 "RewriteCond: NoCase option for non-regex pattern '%s' "
2944 "is not supported and will be ignored.", a2);
2945 newcond->flags &= ~CONDFLAG_NOCASE;
2948 newcond->pattern = a2;
2950 if (!newcond->ptype) {
2951 regexp = ap_pregcomp(cmd->pool, a2,
2952 REG_EXTENDED | ((newcond->flags & CONDFLAG_NOCASE)
2955 return apr_pstrcat(cmd->pool, "RewriteCond: cannot compile regular "
2956 "expression '", a2, "'", NULL);
2959 newcond->regexp = regexp;
2965 static const char *cmd_rewriterule_setflag(apr_pool_t *p, void *_cfg,
2966 char *key, char *val)
2968 rewriterule_entry *cfg = _cfg;
2974 if (!*key || !strcasecmp(key, "hain")) { /* chain */
2975 cfg->flags |= RULEFLAG_CHAIN;
2977 else if (((*key == 'O' || *key == 'o') && !key[1])
2978 || !strcasecmp(key, "ookie")) { /* cookie */
2979 data_item *cp = cfg->cookie;
2982 cp = cfg->cookie = apr_palloc(p, sizeof(*cp));
2988 cp->next = apr_palloc(p, sizeof(*cp));
2999 if (!*key || !strcasecmp(key, "nv")) { /* env */
3000 data_item *cp = cfg->env;
3003 cp = cfg->env = apr_palloc(p, sizeof(*cp));
3009 cp->next = apr_palloc(p, sizeof(*cp));
3020 if (!*key || !strcasecmp(key, "orbidden")) { /* forbidden */
3021 cfg->flags |= RULEFLAG_FORBIDDEN;
3027 if (!*key || !strcasecmp(key, "one")) { /* gone */
3028 cfg->flags |= RULEFLAG_GONE;
3034 if (!*key || !strcasecmp(key, "ast")) { /* last */
3035 cfg->flags |= RULEFLAG_LASTRULE;
3041 if (((*key == 'E' || *key == 'e') && !key[1])
3042 || !strcasecmp(key, "oescape")) { /* noescape */
3043 cfg->flags |= RULEFLAG_NOESCAPE;
3045 else if (!*key || !strcasecmp(key, "ext")) { /* next */
3046 cfg->flags |= RULEFLAG_NEWROUND;
3048 else if (((*key == 'S' || *key == 's') && !key[1])
3049 || !strcasecmp(key, "osubreq")) { /* nosubreq */
3050 cfg->flags |= RULEFLAG_IGNOREONSUBREQ;
3052 else if (((*key == 'C' || *key == 'c') && !key[1])
3053 || !strcasecmp(key, "ocase")) { /* nocase */
3054 cfg->flags |= RULEFLAG_NOCASE;
3060 if (!*key || !strcasecmp(key, "roxy")) { /* proxy */
3061 cfg->flags |= RULEFLAG_PROXY;
3063 else if (((*key == 'T' || *key == 't') && !key[1])
3064 || !strcasecmp(key, "assthrough")) { /* passthrough */
3065 cfg->flags |= RULEFLAG_PASSTHROUGH;
3071 if ( !strcasecmp(key, "QSA")
3072 || !strcasecmp(key, "qsappend")) { /* qsappend */
3073 cfg->flags |= RULEFLAG_QSAPPEND;
3079 if (!*key || !strcasecmp(key, "edirect")) { /* redirect */
3080 cfg->flags |= RULEFLAG_FORCEREDIRECT;
3081 if (strlen(val) > 0) {
3082 if (strcasecmp(val, "permanent") == 0) {
3083 status = HTTP_MOVED_PERMANENTLY;
3085 else if (strcasecmp(val, "temp") == 0) {
3086 status = HTTP_MOVED_TEMPORARILY;
3088 else if (strcasecmp(val, "seeother") == 0) {
3089 status = HTTP_SEE_OTHER;
3091 else if (apr_isdigit(*val)) {
3093 if (!ap_is_HTTP_REDIRECT(status)) {
3094 return "RewriteRule: invalid HTTP response code "
3098 cfg->forced_responsecode = status;
3105 if (!*key || !strcasecmp(key, "kip")) { /* skip */
3106 cfg->skip = atoi(val);
3112 if (!*key || !strcasecmp(key, "ype")) { /* type */
3113 cfg->forced_mimetype = val;
3114 ap_str_tolower(cfg->forced_mimetype);
3119 return apr_pstrcat(p, "RewriteRule: unknown flag '", key, "'", NULL);
3125 static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
3128 rewrite_perdir_conf *dconf = in_dconf;
3129 char *str = apr_pstrdup(cmd->pool, in_str);
3130 rewrite_server_conf *sconf;
3131 rewriterule_entry *newrule;
3138 sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
3140 /* make a new entry in the internal rewrite rule list */
3141 if (cmd->path == NULL) { /* is server command */
3142 newrule = apr_array_push(sconf->rewriterules);
3144 else { /* is per-directory command */
3145 newrule = apr_array_push(dconf->rewriterules);
3148 /* parse the argument line ourself */
3149 if (parseargline(str, &a1, &a2, &a3)) {
3150 return apr_pstrcat(cmd->pool, "RewriteRule: bad argument line '", str,
3154 /* arg3: optional flags field */
3155 newrule->forced_mimetype = NULL;
3156 newrule->forced_responsecode = HTTP_MOVED_TEMPORARILY;
3157 newrule->flags = RULEFLAG_NONE;
3158 newrule->env = NULL;
3159 newrule->cookie = NULL;
3162 if ((err = cmd_parseflagfield(cmd->pool, newrule, a3,
3163 cmd_rewriterule_setflag)) != NULL) {
3168 /* arg1: the pattern
3169 * try to compile the regexp to test if is ok
3172 newrule->flags |= RULEFLAG_NOTMATCH;
3176 regexp = ap_pregcomp(cmd->pool, a1, REG_EXTENDED |
3177 ((newrule->flags & RULEFLAG_NOCASE)
3180 return apr_pstrcat(cmd->pool,
3181 "RewriteRule: cannot compile regular expression '",
3185 newrule->pattern = a1;
3186 newrule->regexp = regexp;
3188 /* arg2: the output string */
3189 newrule->output = a2;
3191 /* now, if the server or per-dir config holds an
3192 * array of RewriteCond entries, we take it for us
3193 * and clear the array
3195 if (cmd->path == NULL) { /* is server command */
3196 newrule->rewriteconds = sconf->rewriteconds;
3197 sconf->rewriteconds = apr_array_make(cmd->pool, 2,
3198 sizeof(rewritecond_entry));
3200 else { /* is per-directory command */
3201 newrule->rewriteconds = dconf->rewriteconds;
3202 dconf->rewriteconds = apr_array_make(cmd->pool, 2,
3203 sizeof(rewritecond_entry));
3211 * +-------------------------------------------------------+
3213 * | the rewriting engine
3215 * +-------------------------------------------------------+
3218 /* Lexicographic Compare */
3219 static APR_INLINE int compare_lexicography(char *cpNum1, char *cpNum2)
3224 n1 = strlen(cpNum1);
3225 n2 = strlen(cpNum2);
3232 for (i = 0; i < n1; i++) {
3233 if (cpNum1[i] > cpNum2[i]) {
3236 if (cpNum1[i] < cpNum2[i]) {
3244 * Apply a single rewriteCond
3246 static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p,
3247 char *perdir, backrefinfo *briRR,
3250 char *input = do_expand(r, p->input, briRR, briRC);
3253 regmatch_t regmatch[MAX_NMATCH];
3257 case CONDPAT_FILE_EXISTS:
3258 if ( apr_stat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS
3259 && sb.filetype == APR_REG) {
3264 case CONDPAT_FILE_SIZE:
3265 if ( apr_stat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS
3266 && sb.filetype == APR_REG && sb.size > 0) {
3271 case CONDPAT_FILE_LINK:
3273 if ( apr_lstat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS
3274 && sb.filetype == APR_LNK) {
3280 case CONDPAT_FILE_DIR:
3281 if ( apr_stat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS
3282 && sb.filetype == APR_DIR) {
3287 case CONDPAT_LU_URL:
3288 if (*input && subreq_ok(r)) {
3289 rsub = ap_sub_req_lookup_uri(input, r, NULL);
3290 if (rsub->status < 400) {
3293 rewritelog(r, 5, "RewriteCond URI (-U) check: "
3294 "path=%s -> status=%d", input, rsub->status);
3295 ap_destroy_sub_req(rsub);
3299 case CONDPAT_LU_FILE:
3300 if (*input && subreq_ok(r)) {
3301 rsub = ap_sub_req_lookup_file(input, r, NULL);
3302 if (rsub->status < 300 &&
3303 /* double-check that file exists since default result is 200 */
3304 apr_stat(&sb, rsub->filename, APR_FINFO_MIN,
3305 r->pool) == APR_SUCCESS) {
3308 rewritelog(r, 5, "RewriteCond file (-F) check: path=%s "
3309 "-> file=%s status=%d", input, rsub->filename,
3311 ap_destroy_sub_req(rsub);
3315 case CONDPAT_STR_GT:
3316 rc = (compare_lexicography(input, p->pattern+1) == 1) ? 1 : 0;
3319 case CONDPAT_STR_LT:
3320 rc = (compare_lexicography(input, p->pattern+1) == -1) ? 1 : 0;
3323 case CONDPAT_STR_EQ:
3324 if (p->flags & CONDFLAG_NOCASE) {
3325 rc = !strcasecmp(input, p->pattern);
3328 rc = !strcmp(input, p->pattern);
3333 /* it is really a regexp pattern, so apply it */
3334 rc = !ap_regexec(p->regexp, input, p->regexp->re_nsub+1, regmatch, 0);
3336 /* update briRC backref info */
3337 if (rc && !(p->flags & CONDFLAG_NOTMATCH)) {
3338 briRC->source = input;
3339 briRC->nsub = p->regexp->re_nsub;
3340 memcpy(briRC->regmatch, regmatch, sizeof(regmatch));
3345 if (p->flags & CONDFLAG_NOTMATCH) {
3349 rewritelog(r, 4, "RewriteCond: input='%s' pattern='%s%s%s'%s => %s",
3350 input, (p->flags & CONDFLAG_NOTMATCH) ? "!" : "",
3351 (p->ptype == CONDPAT_STR_EQ) ? "=" : "", p->pattern,
3352 (p->flags & CONDFLAG_NOCASE) ? " [NC]" : "",
3353 rc ? "matched" : "not-matched");
3359 * Apply a single RewriteRule
3361 static int apply_rewrite_rule(request_rec *r, rewriterule_entry *p,
3369 regmatch_t regmatch[MAX_NMATCH];
3370 backrefinfo *briRR = NULL;
3371 backrefinfo *briRC = NULL;
3374 apr_array_header_t *rewriteconds;
3375 rewritecond_entry *conds;
3376 rewritecond_entry *c;
3388 * Add (perhaps splitted away) PATH_INFO postfix to URL to
3389 * make sure we really match against the complete URL.
3391 if (perdir != NULL && r->path_info != NULL && r->path_info[0] != '\0') {
3392 rewritelog(r, 3, "[per-dir %s] add path info postfix: %s -> %s%s",
3393 perdir, uri, uri, r->path_info);
3394 uri = apr_pstrcat(r->pool, uri, r->path_info, NULL);
3398 * On per-directory context (.htaccess) strip the location
3399 * prefix from the URL to make sure patterns apply only to
3400 * the local part. Additionally indicate this special
3401 * threatment in the logfile.
3404 if (perdir != NULL) {
3405 if ( strlen(uri) >= strlen(perdir)
3406 && strncmp(uri, perdir, strlen(perdir)) == 0) {
3407 rewritelog(r, 3, "[per-dir %s] strip per-dir prefix: %s -> %s",
3408 perdir, uri, uri+strlen(perdir));
3409 uri = uri+strlen(perdir);
3415 * Try to match the URI against the RewriteRule pattern
3416 * and exit immeddiately if it didn't apply.
3418 if (perdir == NULL) {
3419 rewritelog(r, 3, "applying pattern '%s' to uri '%s'",
3423 rewritelog(r, 3, "[per-dir %s] applying pattern '%s' to uri '%s'",
3424 perdir, p->pattern, uri);
3426 rc = (ap_regexec(regexp, uri, regexp->re_nsub+1, regmatch, 0) == 0);
3427 if (! (( rc && !(p->flags & RULEFLAG_NOTMATCH)) ||
3428 (!rc && (p->flags & RULEFLAG_NOTMATCH)) ) ) {
3433 * Else create the RewriteRule `regsubinfo' structure which
3434 * holds the substitution information.
3436 briRR = (backrefinfo *)apr_palloc(r->pool, sizeof(backrefinfo));
3437 if (!rc && (p->flags & RULEFLAG_NOTMATCH)) {
3438 /* empty info on negative patterns */
3443 briRR->source = apr_pstrdup(r->pool, uri);
3444 briRR->nsub = regexp->re_nsub;
3445 memcpy((void *)(briRR->regmatch), (void *)(regmatch),
3450 * Initiallally create the RewriteCond backrefinfo with
3451 * empty backrefinfo, i.e. not subst parts
3452 * (this one is adjusted inside apply_rewrite_cond() later!!)
3454 briRC = (backrefinfo *)apr_pcalloc(r->pool, sizeof(backrefinfo));
3459 * Ok, we already know the pattern has matched, but we now
3460 * additionally have to check for all existing preconditions
3461 * (RewriteCond) which have to be also true. We do this at
3462 * this very late stage to avoid unnessesary checks which
3463 * would slow down the rewriting engine!!
3465 rewriteconds = p->rewriteconds;
3466 conds = (rewritecond_entry *)rewriteconds->elts;
3468 for (i = 0; i < rewriteconds->nelts; i++) {
3470 rc = apply_rewrite_cond(r, c, perdir, briRR, briRC);
3471 if (c->flags & CONDFLAG_ORNEXT) {
3476 /* One condition is false, but another can be
3477 * still true, so we have to continue...
3479 apr_table_unset(r->notes, VARY_KEY_THIS);
3483 /* One true condition is enough in "or" case, so
3484 * skip the other conditions which are "ornext"
3487 while ( i < rewriteconds->nelts
3488 && c->flags & CONDFLAG_ORNEXT) {
3497 * The "AND" case, i.e. no "or" flag,
3498 * so a single failure means total failure.
3505 vary = apr_table_get(r->notes, VARY_KEY_THIS);
3507 apr_table_merge(r->notes, VARY_KEY, vary);
3508 apr_table_unset(r->notes, VARY_KEY_THIS);
3511 /* if any condition fails the complete rule fails */
3513 apr_table_unset(r->notes, VARY_KEY);
3514 apr_table_unset(r->notes, VARY_KEY_THIS);
3519 * Regardless of what we do next, we've found a match. Check to see
3520 * if any of the request header fields were involved, and add them
3521 * to the Vary field of the response.
3523 if ((vary = apr_table_get(r->notes, VARY_KEY)) != NULL) {
3524 apr_table_merge(r->headers_out, "Vary", vary);
3525 apr_table_unset(r->notes, VARY_KEY);
3529 * If this is a pure matching rule (`RewriteRule <pat> -')
3530 * we stop processing and return immediately. The only thing
3531 * we have not to forget are the environment variables and
3533 * (`RewriteRule <pat> - [E=...,CO=...]')
3535 if (output[0] == '-' && !output[1]) {
3536 do_expand_env(r, p->env, briRR, briRC);
3537 do_expand_cookie(r, p->cookie, briRR, briRC);
3538 if (p->forced_mimetype != NULL) {
3539 if (perdir == NULL) {
3540 /* In the per-server context we can force the MIME-type
3541 * the correct way by notifying our MIME-type hook handler
3542 * to do the job when the MIME-type API stage is reached.
3544 rewritelog(r, 2, "remember %s to have MIME-type '%s'",
3545 r->filename, p->forced_mimetype);
3546 apr_table_setn(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
3547 p->forced_mimetype);
3550 /* In per-directory context we operate in the Fixup API hook
3551 * which is after the MIME-type hook, so our MIME-type handler
3552 * has no chance to set r->content_type. And because we are
3553 * in the situation where no substitution takes place no
3554 * sub-request will happen (which could solve the
3555 * restriction). As a workaround we do it ourself now
3556 * immediately although this is not strictly API-conforming.
3557 * But it's the only chance we have...
3559 rewritelog(r, 1, "[per-dir %s] force %s to have MIME-type "
3560 "'%s'", perdir, r->filename, p->forced_mimetype);
3561 ap_set_content_type(r, p->forced_mimetype);
3568 * Ok, now we finally know all patterns have matched and
3569 * that there is something to replace, so we create the
3570 * substitution URL string in `newuri'.
3572 newuri = do_expand(r, output, briRR, briRC);
3573 if (perdir == NULL) {
3574 rewritelog(r, 2, "rewrite %s -> %s", uri, newuri);
3577 rewritelog(r, 2, "[per-dir %s] rewrite %s -> %s", perdir, uri, newuri);
3581 * Additionally do expansion for the environment variable
3582 * strings (`RewriteRule .. .. [E=<string>]').
3584 do_expand_env(r, p->env, briRR, briRC);
3587 * Also set cookies for any cookie strings
3588 * (`RewriteRule .. .. [CO=<string>]').
3590 do_expand_cookie(r, p->cookie, briRR, briRC);
3593 * Now replace API's knowledge of the current URI:
3594 * Replace r->filename with the new URI string and split out
3595 * an on-the-fly generated QUERY_STRING part into r->args
3597 r->filename = apr_pstrdup(r->pool, newuri);
3598 splitout_queryargs(r, p->flags & RULEFLAG_QSAPPEND);
3601 * Add the previously stripped per-directory location
3602 * prefix if the new URI is not a new one for this
3603 * location, i.e. if it's not an absolute URL (!) path nor
3604 * a fully qualified URL scheme.
3606 if (prefixstrip && *r->filename != '/'
3607 && !is_absolute_uri(r->filename)) {
3608 rewritelog(r, 3, "[per-dir %s] add per-dir prefix: %s -> %s%s",
3609 perdir, r->filename, perdir, r->filename);
3610 r->filename = apr_pstrcat(r->pool, perdir, r->filename, NULL);
3614 * If this rule is forced for proxy throughput
3615 * (`RewriteRule ... ... [P]') then emulate mod_proxy's
3616 * URL-to-filename handler to be sure mod_proxy is triggered
3617 * for this URL later in the Apache API. But make sure it is
3618 * a fully-qualified URL. (If not it is qualified with
3621 if (p->flags & RULEFLAG_PROXY) {
3622 fully_qualify_uri(r);
3623 if (perdir == NULL) {
3624 rewritelog(r, 2, "forcing proxy-throughput with %s", r->filename);
3627 rewritelog(r, 2, "[per-dir %s] forcing proxy-throughput with %s",
3628 perdir, r->filename);
3630 r->filename = apr_pstrcat(r->pool, "proxy:", r->filename, NULL);
3635 * If this rule is explicitly forced for HTTP redirection
3636 * (`RewriteRule .. .. [R]') then force an external HTTP
3637 * redirect. But make sure it is a fully-qualified URL. (If
3638 * not it is qualified with ourself).
3640 if (p->flags & RULEFLAG_FORCEREDIRECT) {
3641 fully_qualify_uri(r);
3642 if (perdir == NULL) {
3644 "explicitly forcing redirect with %s", r->filename);
3648 "[per-dir %s] explicitly forcing redirect with %s",
3649 perdir, r->filename);
3651 r->status = p->forced_responsecode;
3656 * Special Rewriting Feature: Self-Reduction
3657 * We reduce the URL by stripping a possible
3658 * http[s]://<ourhost>[:<port>] prefix, i.e. a prefix which
3659 * corresponds to ourself. This is to simplify rewrite maps
3660 * and to avoid recursion, etc. When this prefix is not a
3661 * coincidence then the user has to use [R] explicitly (see
3667 * If this rule is still implicitly forced for HTTP
3668 * redirection (`RewriteRule .. <scheme>://...') then
3669 * directly force an external HTTP redirect.
3671 if (is_absolute_uri(r->filename)) {
3672 if (perdir == NULL) {
3674 "implicitly forcing redirect (rc=%d) with %s",
3675 p->forced_responsecode, r->filename);
3678 rewritelog(r, 2, "[per-dir %s] implicitly forcing redirect "
3679 "(rc=%d) with %s", perdir, p->forced_responsecode,
3682 r->status = p->forced_responsecode;
3687 * Finally we had to remember if a MIME-type should be
3688 * forced for this URL (`RewriteRule .. .. [T=<type>]')
3689 * Later in the API processing phase this is forced by our
3690 * MIME API-hook function. This time it's no problem even for
3691 * the per-directory context (where the MIME-type hook was
3692 * already processed) because a sub-request happens ;-)
3694 if (p->forced_mimetype != NULL) {
3695 apr_table_setn(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
3696 p->forced_mimetype);
3697 if (perdir == NULL) {
3698 rewritelog(r, 2, "remember %s to have MIME-type '%s'",
3699 r->filename, p->forced_mimetype);
3703 "[per-dir %s] remember %s to have MIME-type '%s'",
3704 perdir, r->filename, p->forced_mimetype);
3709 * Puuhhhhhhhh... WHAT COMPLICATED STUFF ;_)
3710 * But now we're done for this particular rule.
3716 * Apply a complete rule set,
3717 * i.e. a list of rewrite rules
3719 static int apply_rewrite_list(request_rec *r, apr_array_header_t *rewriterules,
3722 rewriterule_entry *entries;
3723 rewriterule_entry *p;
3730 * Iterate over all existing rules
3732 entries = (rewriterule_entry *)rewriterules->elts;
3735 for (i = 0; i < rewriterules->nelts; i++) {
3739 * Ignore this rule on subrequests if we are explicitly
3740 * asked to do so or this is a proxy-throughput or a
3741 * forced redirect rule.
3743 if (r->main != NULL &&
3744 (p->flags & RULEFLAG_IGNOREONSUBREQ ||
3745 p->flags & RULEFLAG_PROXY ||
3746 p->flags & RULEFLAG_FORCEREDIRECT )) {
3751 * Apply the current rule.
3753 rc = apply_rewrite_rule(r, p, perdir);
3756 * Indicate a change if this was not a match-only rule.
3759 changed = ((p->flags & RULEFLAG_NOESCAPE)
3760 ? ACTION_NOESCAPE : ACTION_NORMAL);
3764 * Pass-Through Feature (`RewriteRule .. .. [PT]'):
3765 * Because the Apache 1.x API is very limited we
3766 * need this hack to pass the rewritten URL to other
3767 * modules like mod_alias, mod_userdir, etc.
3769 if (p->flags & RULEFLAG_PASSTHROUGH) {
3770 rewritelog(r, 2, "forcing '%s' to get passed through "
3771 "to next API URI-to-filename handler", r->filename);
3772 r->filename = apr_pstrcat(r->pool, "passthrough:",
3774 changed = ACTION_NORMAL;
3779 * Rule has the "forbidden" flag set which means that
3780 * we stop processing and indicate this to the caller.
3782 if (p->flags & RULEFLAG_FORBIDDEN) {
3783 rewritelog(r, 2, "forcing '%s' to be forbidden", r->filename);
3784 r->filename = apr_pstrcat(r->pool, "forbidden:",
3786 changed = ACTION_NORMAL;
3791 * Rule has the "gone" flag set which means that
3792 * we stop processing and indicate this to the caller.
3794 if (p->flags & RULEFLAG_GONE) {
3795 rewritelog(r, 2, "forcing '%s' to be gone", r->filename);
3796 r->filename = apr_pstrcat(r->pool, "gone:", r->filename, NULL);
3797 changed = ACTION_NORMAL;
3802 * Stop processing also on proxy pass-through and
3803 * last-rule and new-round flags.
3805 if (p->flags & RULEFLAG_PROXY) {
3808 if (p->flags & RULEFLAG_LASTRULE) {
3813 * On "new-round" flag we just start from the top of
3814 * the rewriting ruleset again.
3816 if (p->flags & RULEFLAG_NEWROUND) {
3821 * If we are forced to skip N next rules, do it now.
3825 while ( i < rewriterules->nelts
3835 * If current rule is chained with next rule(s),
3836 * skip all this next rule(s)
3838 while ( i < rewriterules->nelts
3839 && p->flags & RULEFLAG_CHAIN) {
3850 * +-------------------------------------------------------+
3852 * | Module Initialization Hooks
3854 * +-------------------------------------------------------+
3857 static int pre_config(apr_pool_t *pconf,
3861 APR_OPTIONAL_FN_TYPE(ap_register_rewrite_mapfunc) *map_pfn_register;
3863 /* register int: rewritemap handlers */
3864 mapfunc_hash = apr_hash_make(pconf);
3865 map_pfn_register = APR_RETRIEVE_OPTIONAL_FN(ap_register_rewrite_mapfunc);
3866 if (map_pfn_register) {
3867 map_pfn_register("tolower", rewrite_mapfunc_tolower);
3868 map_pfn_register("toupper", rewrite_mapfunc_toupper);
3869 map_pfn_register("escape", rewrite_mapfunc_escape);
3870 map_pfn_register("unescape", rewrite_mapfunc_unescape);
3875 static int post_config(apr_pool_t *p,
3883 const char *userdata_key = "rewrite_init_module";
3885 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
3888 apr_pool_userdata_set((const void *)1, userdata_key,
3889 apr_pool_cleanup_null, s->process->pool);
3892 /* check if proxy module is available */
3893 proxy_available = (ap_find_linked_module("mod_proxy.c") != NULL);
3895 /* create the rewriting lockfiles in the parent */
3896 if ((rv = apr_global_mutex_create(&rewrite_log_lock, NULL,
3897 APR_LOCK_DEFAULT, p)) != APR_SUCCESS) {
3898 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
3899 "mod_rewrite: could not create rewrite_log_lock");
3900 return HTTP_INTERNAL_SERVER_ERROR;
3903 #ifdef MOD_REWRITE_SET_MUTEX_PERMS
3904 rv = unixd_set_global_mutex_perms(rewrite_log_lock);
3905 if (rv != APR_SUCCESS) {
3906 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
3907 "mod_rewrite: Could not set permissions on "
3908 "rewrite_log_lock; check User and Group directives");
3909 return HTTP_INTERNAL_SERVER_ERROR;
3913 rv = rewritelock_create(s, p);
3914 if (rv != APR_SUCCESS) {
3915 return HTTP_INTERNAL_SERVER_ERROR;
3918 apr_pool_cleanup_register(p, (void *)s, rewritelock_remove,
3919 apr_pool_cleanup_null);
3921 /* step through the servers and
3922 * - open each rewriting logfile
3923 * - open the RewriteMap prg:xxx programs
3925 for (; s; s = s->next) {
3926 if (!open_rewritelog(s, p)) {
3927 return HTTP_INTERNAL_SERVER_ERROR;
3931 if (run_rewritemap_programs(s, p) != APR_SUCCESS) {
3932 return HTTP_INTERNAL_SERVER_ERROR;
3940 static void init_child(apr_pool_t *p, server_rec *s)
3944 if (lockname != NULL && *(lockname) != '\0') {
3945 rv = apr_global_mutex_child_init(&rewrite_mapr_lock_acquire,
3947 if (rv != APR_SUCCESS) {
3948 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
3949 "mod_rewrite: could not init rewrite_mapr_lock_acquire"
3954 rv = apr_global_mutex_child_init(&rewrite_log_lock, NULL, p);
3955 if (rv != APR_SUCCESS) {
3956 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
3957 "mod_rewrite: could not init rewrite log lock in child");
3960 /* create the lookup cache */
3961 if (!init_cache(p)) {
3962 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
3963 "mod_rewrite: could not init map cache in child");
3969 * +-------------------------------------------------------+
3973 * +-------------------------------------------------------+
3977 * URI-to-filename hook
3978 * [deals with RewriteRules in server context]
3980 static int hook_uri2file(request_rec *r)
3982 rewrite_server_conf *conf;
3983 const char *saved_rulestatus;
3985 const char *thisserver;
3987 const char *thisurl;
3992 * retrieve the config structures
3994 conf = ap_get_module_config(r->server->module_config, &rewrite_module);
3997 * only do something under runtime if the engine is really enabled,
3998 * else return immediately!
4000 if (conf->state == ENGINE_DISABLED) {
4005 * check for the ugly API case of a virtual host section where no
4006 * mod_rewrite directives exists. In this situation we became no chance
4007 * by the API to setup our default per-server config so we have to
4008 * on-the-fly assume we have the default config. But because the default
4009 * config has a disabled rewriting engine we are lucky because can
4010 * just stop operating now.
4012 if (conf->server != r->server) {
4017 * add the SCRIPT_URL variable to the env. this is a bit complicated
4018 * due to the fact that apache uses subrequests and internal redirects
4021 if (r->main == NULL) {
4022 var = apr_table_get(r->subprocess_env, REDIRECT_ENVVAR_SCRIPT_URL);
4024 apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, r->uri);
4027 apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var);
4031 var = apr_table_get(r->main->subprocess_env, ENVVAR_SCRIPT_URL);
4032 apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var);
4036 * create the SCRIPT_URI variable for the env
4039 /* add the canonical URI of this URL */
4040 thisserver = ap_get_server_name(r);
4041 port = ap_get_server_port(r);
4042 if (ap_is_default_port(port, r)) {
4046 thisport = apr_psprintf(r->pool, ":%u", port);
4048 thisurl = apr_table_get(r->subprocess_env, ENVVAR_SCRIPT_URL);
4050 /* set the variable */
4051 var = apr_pstrcat(r->pool, ap_http_method(r), "://", thisserver, thisport,
4053 apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URI, var);
4055 if (!(saved_rulestatus = apr_table_get(r->notes,"mod_rewrite_rewritten"))) {
4056 /* if filename was not initially set,
4057 * we start with the requested URI
4059 if (r->filename == NULL) {
4060 r->filename = apr_pstrdup(r->pool, r->uri);
4061 rewritelog(r, 2, "init rewrite engine with requested uri %s",
4065 rewritelog(r, 2, "init rewrite engine with passed filename %s."
4066 " Original uri = %s", r->filename, r->uri);
4070 * now apply the rules ...
4072 rulestatus = apply_rewrite_list(r, conf->rewriterules, NULL);
4073 apr_table_set(r->notes,"mod_rewrite_rewritten",
4074 apr_psprintf(r->pool,"%d",rulestatus));
4078 "uri already rewritten. Status %s, Uri %s, r->filename %s",
4079 saved_rulestatus, r->uri, r->filename);
4080 rulestatus = atoi(saved_rulestatus);
4085 apr_size_t flen = strlen(r->filename);
4087 if (flen > 6 && strncmp(r->filename, "proxy:", 6) == 0) {
4088 /* it should be go on as an internal proxy request */
4090 /* check if the proxy module is enabled, so
4091 * we can actually use it!
4093 if (!proxy_available) {
4094 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
4095 "attempt to make remote request from mod_rewrite "
4096 "without proxy enabled: %s", r->filename);
4097 return HTTP_FORBIDDEN;
4100 /* make sure the QUERY_STRING and
4101 * PATH_INFO parts get incorporated
4103 if (r->path_info != NULL) {
4104 r->filename = apr_pstrcat(r->pool, r->filename,
4105 r->path_info, NULL);
4107 if (r->args != NULL &&
4108 r->uri == r->unparsed_uri) {
4109 /* see proxy_http:proxy_http_canon() */
4110 r->filename = apr_pstrcat(r->pool, r->filename,
4111 "?", r->args, NULL);
4114 /* now make sure the request gets handled by the proxy handler */
4115 r->proxyreq = PROXYREQ_REVERSE;
4116 r->handler = "proxy-server";
4118 rewritelog(r, 1, "go-ahead with proxy request %s [OK]",
4122 else if ((skip = is_absolute_uri(r->filename)) > 0) {
4125 /* it was finally rewritten to a remote URL */
4127 if (rulestatus != ACTION_NOESCAPE) {
4128 rewritelog(r, 1, "escaping %s for redirect", r->filename);
4129 r->filename = escape_absolute_uri(r->pool, r->filename, skip);
4132 /* append the QUERY_STRING part */
4134 r->filename = apr_pstrcat(r->pool, r->filename, "?",
4135 (rulestatus == ACTION_NOESCAPE)
4137 : ap_escape_uri(r->pool, r->args),
4141 /* determine HTTP redirect response code */
4142 if (ap_is_HTTP_REDIRECT(r->status)) {
4144 r->status = HTTP_OK; /* make Apache kernel happy */
4147 n = HTTP_MOVED_TEMPORARILY;
4150 /* now do the redirection */
4151 apr_table_setn(r->headers_out, "Location", r->filename);
4152 rewritelog(r, 1, "redirect to %s [REDIRECT/%d]", r->filename, n);
4155 else if (flen > 10 && strncmp(r->filename, "forbidden:", 10) == 0) {
4156 /* This URLs is forced to be forbidden for the requester */
4157 return HTTP_FORBIDDEN;
4159 else if (flen > 5 && strncmp(r->filename, "gone:", 5) == 0) {
4160 /* This URLs is forced to be gone */
4163 else if (flen > 12 && strncmp(r->filename, "passthrough:", 12) == 0) {
4165 * Hack because of underpowered API: passing the current
4166 * rewritten filename through to other URL-to-filename handlers
4167 * just as it were the requested URL. This is to enable
4168 * post-processing by mod_alias, etc. which always act on
4169 * r->uri! The difference here is: We do not try to
4170 * add the document root
4172 r->uri = apr_pstrdup(r->pool, r->filename+12);
4176 /* it was finally rewritten to a local path */
4178 /* expand "/~user" prefix */
4180 r->filename = expand_tildepaths(r, r->filename);
4182 rewritelog(r, 2, "local path result: %s", r->filename);
4184 /* the filename must be either an absolute local path or an
4185 * absolute local URL.
4187 if ( *r->filename != '/'
4188 && !ap_os_is_path_absolute(r->pool, r->filename)) {
4189 return HTTP_BAD_REQUEST;
4192 /* if there is no valid prefix, we call
4193 * the translator from the core and
4194 * prefix the filename with document_root
4197 * We cannot leave out the prefix_stat because
4198 * - when we always prefix with document_root
4199 * then no absolute path can be created, e.g. via
4200 * emulating a ScriptAlias directive, etc.
4201 * - when we always NOT prefix with document_root
4202 * then the files under document_root have to
4203 * be references directly and document_root
4204 * gets never used and will be a dummy parameter -
4208 * Under real Unix systems this is no problem,
4209 * because we only do stat() on the first directory
4210 * and this gets cached by the kernel for along time!
4212 if (!prefix_stat(r->filename, r->pool)) {
4216 r->uri = r->filename;
4217 res = ap_core_translate(r);
4221 rewritelog(r, 1, "prefixing with document_root of %s "
4222 "FAILED", r->filename);
4227 rewritelog(r, 2, "prefixed with document_root to %s",
4231 rewritelog(r, 1, "go-ahead with %s [OK]", r->filename);
4236 rewritelog(r, 1, "pass through %s", r->filename);
4243 * [RewriteRules in directory context]
4245 static int hook_fixup(request_rec *r)
4247 rewrite_perdir_conf *dconf;
4256 dconf = (rewrite_perdir_conf *)ap_get_module_config(r->per_dir_config,
4259 /* if there is no per-dir config we return immediately */
4260 if (dconf == NULL) {
4264 /* we shouldn't do anything in subrequests */
4265 if (r->main != NULL) {
4269 /* if there are no real (i.e. no RewriteRule directives!)
4270 per-dir config of us, we return also immediately */
4271 if (dconf->directory == NULL) {
4276 * .htaccess file is called before really entering the directory, i.e.:
4277 * URL: http://localhost/foo and .htaccess is located in foo directory
4278 * Ignore such attempts, since they may lead to undefined behaviour.
4280 l = strlen(dconf->directory) - 1;
4281 if (r->filename && strlen(r->filename) == l &&
4282 (dconf->directory)[l] == '/' &&
4283 !strncmp(r->filename, dconf->directory, l)) {
4288 * only do something under runtime if the engine is really enabled,
4289 * for this directory, else return immediately!
4291 if (dconf->state == ENGINE_DISABLED) {
4296 * Do the Options check after engine check, so
4297 * the user is able to explicitely turn RewriteEngine Off.
4299 if (!(ap_allow_options(r) & (OPT_SYM_LINKS | OPT_SYM_OWNER))) {
4300 /* FollowSymLinks is mandatory! */
4301 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
4302 "Options FollowSymLinks or SymLinksIfOwnerMatch is off "
4303 "which implies that RewriteRule directive is forbidden: "
4305 return HTTP_FORBIDDEN;
4309 * remember the current filename before rewriting for later check
4310 * to prevent deadlooping because of internal redirects
4311 * on final URL/filename which can be equal to the inital one.
4313 ofilename = r->filename;
4316 * now apply the rules ...
4318 rulestatus = apply_rewrite_list(r, dconf->rewriterules, dconf->directory);
4321 l = strlen(r->filename);
4323 if (l > 6 && strncmp(r->filename, "proxy:", 6) == 0) {
4324 /* it should go on as an internal proxy request */
4326 /* make sure the QUERY_STRING and
4327 * PATH_INFO parts get incorporated
4328 * (r->path_info was already appended by the
4329 * rewriting engine because of the per-dir context!)
4331 if (r->args != NULL) {
4332 r->filename = apr_pstrcat(r->pool, r->filename,
4333 "?", r->args, NULL);
4336 /* now make sure the request gets handled by the proxy handler */
4337 r->proxyreq = PROXYREQ_REVERSE;
4338 r->handler = "proxy-server";
4340 rewritelog(r, 1, "[per-dir %s] go-ahead with proxy request "
4341 "%s [OK]", dconf->directory, r->filename);
4344 else if ((skip = is_absolute_uri(r->filename)) > 0) {
4345 /* it was finally rewritten to a remote URL */
4347 /* because we are in a per-dir context
4348 * first try to replace the directory with its base-URL
4349 * if there is a base-URL available
4351 if (dconf->baseurl != NULL) {
4352 /* skip 'scheme://' */
4353 cp = r->filename + skip;
4355 if ((cp = ap_strchr(cp, '/')) != NULL && *(++cp)) {
4357 "[per-dir %s] trying to replace "
4358 "prefix %s with %s",
4359 dconf->directory, dconf->directory,
4362 /* I think, that hack needs an explanation:
4364 * mod_rewrite was written for unix systems, were
4365 * absolute file-system paths start with a slash.
4366 * URL-paths _also_ start with slashes, so they
4367 * can be easily compared with system paths.
4369 * the following assumes, that the actual url-path
4370 * may be prefixed by the current directory path and
4371 * tries to replace the system path with the RewriteBase
4373 * That assumption is true if we use a RewriteRule like
4375 * RewriteRule ^foo bar [R]
4377 * (see apply_rewrite_rule function)
4378 * However on systems that don't have a / as system
4379 * root this will never match, so we skip the / after the
4380 * hostname and compare/substitute only the stuff after it.
4382 * (note that cp was already increased to the right value)
4384 cp2 = subst_prefix_path(r, cp, (*dconf->directory == '/')
4385 ? dconf->directory + 1
4387 dconf->baseurl + 1);
4388 if (strcmp(cp2, cp) != 0) {
4390 r->filename = apr_pstrcat(r->pool, r->filename,
4396 /* now prepare the redirect... */
4397 if (rulestatus != ACTION_NOESCAPE) {
4398 rewritelog(r, 1, "[per-dir %s] escaping %s for redirect",
4399 dconf->directory, r->filename);
4400 r->filename = escape_absolute_uri(r->pool, r->filename, skip);
4403 /* append the QUERY_STRING part */
4405 r->filename = apr_pstrcat(r->pool, r->filename, "?",
4406 (rulestatus == ACTION_NOESCAPE)
4408 : ap_escape_uri(r->pool, r->args),
4412 /* determine HTTP redirect response code */
4413 if (ap_is_HTTP_REDIRECT(r->status)) {
4415 r->status = HTTP_OK; /* make Apache kernel happy */
4418 n = HTTP_MOVED_TEMPORARILY;
4421 /* now do the redirection */
4422 apr_table_setn(r->headers_out, "Location", r->filename);
4423 rewritelog(r, 1, "[per-dir %s] redirect to %s [REDIRECT/%d]",
4424 dconf->directory, r->filename, n);
4427 else if (l > 10 && strncmp(r->filename, "forbidden:", 10) == 0) {
4428 /* This URL is forced to be forbidden for the requester */
4429 return HTTP_FORBIDDEN;
4431 else if (l > 5 && strncmp(r->filename, "gone:", 5) == 0) {
4432 /* This URL is forced to be gone */
4436 /* it was finally rewritten to a local path */
4438 /* if someone used the PASSTHROUGH flag in per-dir
4439 * context we just ignore it. It is only useful
4440 * in per-server context
4442 if (l > 12 && strncmp(r->filename, "passthrough:", 12) == 0) {
4443 r->filename = apr_pstrdup(r->pool, r->filename+12);
4446 /* the filename must be either an absolute local path or an
4447 * absolute local URL.
4449 if ( *r->filename != '/'
4450 && !ap_os_is_path_absolute(r->pool, r->filename)) {
4451 return HTTP_BAD_REQUEST;
4454 /* Check for deadlooping:
4455 * At this point we KNOW that at least one rewriting
4456 * rule was applied, but when the resulting URL is
4457 * the same as the initial URL, we are not allowed to
4458 * use the following internal redirection stuff because
4459 * this would lead to a deadloop.
4461 if (strcmp(r->filename, ofilename) == 0) {
4462 rewritelog(r, 1, "[per-dir %s] initial URL equal rewritten "
4463 "URL: %s [IGNORING REWRITE]",
4464 dconf->directory, r->filename);
4468 /* if there is a valid base-URL then substitute
4469 * the per-dir prefix with this base-URL if the
4470 * current filename still is inside this per-dir
4471 * context. If not then treat the result as a
4474 if (dconf->baseurl != NULL) {
4476 "[per-dir %s] trying to replace prefix %s with %s",
4477 dconf->directory, dconf->directory, dconf->baseurl);
4478 r->filename = subst_prefix_path(r, r->filename,
4483 /* if no explicit base-URL exists we assume
4484 * that the directory prefix is also a valid URL
4485 * for this webserver and only try to remove the
4486 * document_root if it is prefix
4488 if ((ccp = ap_document_root(r)) != NULL) {
4489 /* strip trailing slash */
4491 if (ccp[l-1] == '/') {
4494 if (!strncmp(r->filename, ccp, l) &&
4495 r->filename[l] == '/') {
4497 "[per-dir %s] strip document_root "
4499 dconf->directory, r->filename,
4501 r->filename = apr_pstrdup(r->pool, r->filename+l);
4506 /* now initiate the internal redirect */
4507 rewritelog(r, 1, "[per-dir %s] internal redirect with %s "
4508 "[INTERNAL REDIRECT]", dconf->directory, r->filename);
4509 r->filename = apr_pstrcat(r->pool, "redirect:", r->filename, NULL);
4510 r->handler = "redirect-handler";
4515 rewritelog(r, 1, "[per-dir %s] pass through %s",
4516 dconf->directory, r->filename);
4523 * [T=...] in server-context
4525 static int hook_mimetype(request_rec *r)
4529 /* now check if we have to force a MIME-type */
4530 t = apr_table_get(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR);
4535 rewritelog(r, 1, "force filename %s to have MIME-type '%s'",
4537 ap_set_content_type(r, t);
4542 /* check whether redirect limit is reached */
4543 static int is_redirect_limit_exceeded(request_rec *r)
4545 request_rec *top = r;
4546 rewrite_request_conf *reqc;
4547 rewrite_perdir_conf *dconf;
4549 /* we store it in the top request */
4557 /* fetch our config */
4558 reqc = (rewrite_request_conf *) ap_get_module_config(top->request_config,
4561 /* no config there? create one. */
4563 rewrite_server_conf *sconf;
4565 reqc = apr_palloc(top->pool, sizeof(rewrite_request_conf));
4566 sconf = ap_get_module_config(r->server->module_config, &rewrite_module);
4568 reqc->redirects = 0;
4569 reqc->redirect_limit = sconf->redirect_limit
4570 ? sconf->redirect_limit
4571 : REWRITE_REDIRECT_LIMIT;
4573 /* associate it with this request */
4574 ap_set_module_config(top->request_config, &rewrite_module, reqc);
4577 /* allow to change the limit during redirects. */
4578 dconf = (rewrite_perdir_conf *)ap_get_module_config(r->per_dir_config,
4581 /* 0 == unset; take server conf ... */
4582 if (dconf->redirect_limit) {
4583 reqc->redirect_limit = dconf->redirect_limit;
4586 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
4587 "mod_rewrite's internal redirect status: %d/%d.",
4588 reqc->redirects, reqc->redirect_limit);
4590 /* and now give the caller a hint */
4591 return (reqc->redirects++ >= reqc->redirect_limit);
4595 * "content" handler for internal redirects
4597 static int handler_redirect(request_rec *r)
4599 if (strcmp(r->handler, "redirect-handler")) {
4603 /* just make sure that we are really meant! */
4604 if (strncmp(r->filename, "redirect:", 9) != 0) {
4608 if (is_redirect_limit_exceeded(r)) {
4609 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
4610 "mod_rewrite: maximum number of internal redirects "
4611 "reached. Assuming configuration error. Use "
4612 "'RewriteOptions MaxRedirects' to increase the limit "
4614 return HTTP_INTERNAL_SERVER_ERROR;
4617 /* now do the internal redirect */
4618 ap_internal_redirect(apr_pstrcat(r->pool, r->filename+9,
4619 r->args ? "?" : NULL, r->args, NULL), r);
4621 /* and return gracefully */
4627 * +-------------------------------------------------------+
4629 * | Module paraphernalia
4631 * +-------------------------------------------------------+
4634 static const command_rec command_table[] = {
4635 AP_INIT_FLAG( "RewriteEngine", cmd_rewriteengine, NULL, OR_FILEINFO,
4636 "On or Off to enable or disable (default) the whole "
4637 "rewriting engine"),
4638 AP_INIT_ITERATE( "RewriteOptions", cmd_rewriteoptions, NULL, OR_FILEINFO,
4639 "List of option strings to set"),
4640 AP_INIT_TAKE1( "RewriteBase", cmd_rewritebase, NULL, OR_FILEINFO,
4641 "the base URL of the per-directory context"),
4642 AP_INIT_RAW_ARGS("RewriteCond", cmd_rewritecond, NULL, OR_FILEINFO,
4643 "an input string and a to be applied regexp-pattern"),
4644 AP_INIT_RAW_ARGS("RewriteRule", cmd_rewriterule, NULL, OR_FILEINFO,
4645 "an URL-applied regexp-pattern and a substitution URL"),
4646 AP_INIT_TAKE2( "RewriteMap", cmd_rewritemap, NULL, RSRC_CONF,
4647 "a mapname and a filename"),
4648 AP_INIT_TAKE1( "RewriteLock", cmd_rewritelock, NULL, RSRC_CONF,
4649 "the filename of a lockfile used for inter-process "
4651 AP_INIT_TAKE1( "RewriteLog", cmd_rewritelog, NULL, RSRC_CONF,
4652 "the filename of the rewriting logfile"),
4653 AP_INIT_TAKE1( "RewriteLogLevel", cmd_rewriteloglevel, NULL, RSRC_CONF,
4654 "the level of the rewriting logfile verbosity "
4655 "(0=none, 1=std, .., 9=max)"),
4659 static void ap_register_rewrite_mapfunc(char *name, rewrite_mapfunc_t *func)
4661 apr_hash_set(mapfunc_hash, name, strlen(name), (const void *)func);
4664 static void register_hooks(apr_pool_t *p)
4666 /* fixup after mod_proxy, so that the proxied url will not
4667 * escaped accidentally by mod_proxy's fixup.
4669 static const char * const aszPre[]={ "mod_proxy.c", NULL };
4671 /* check type before mod_mime, so that [T=foo/bar] will not be
4672 * overridden by AddType definitions.
4674 static const char * const ct_aszSucc[]={ "mod_mime.c", NULL };
4676 APR_REGISTER_OPTIONAL_FN(ap_register_rewrite_mapfunc);
4678 ap_hook_handler(handler_redirect, NULL, NULL, APR_HOOK_MIDDLE);
4679 ap_hook_pre_config(pre_config, NULL, NULL, APR_HOOK_MIDDLE);
4680 ap_hook_post_config(post_config, NULL, NULL, APR_HOOK_MIDDLE);
4681 ap_hook_child_init(init_child, NULL, NULL, APR_HOOK_MIDDLE);
4683 ap_hook_fixups(hook_fixup, aszPre, NULL, APR_HOOK_FIRST);
4684 ap_hook_translate_name(hook_uri2file, NULL, NULL, APR_HOOK_FIRST);
4685 ap_hook_type_checker(hook_mimetype, NULL, ct_aszSucc, APR_HOOK_MIDDLE);
4688 /* the main config structure */
4689 module AP_MODULE_DECLARE_DATA rewrite_module = {
4690 STANDARD20_MODULE_STUFF,
4691 config_perdir_create, /* create per-dir config structures */
4692 config_perdir_merge, /* merge per-dir config structures */
4693 config_server_create, /* create per-server config structures */
4694 config_server_merge, /* merge per-server config structures */
4695 command_table, /* table of config file commands */
4696 register_hooks /* register hooks */