2 * mod_domaintree.c - Apache2
6 * Copyright 2005 Michael Wallner <mike@iworks.at>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 * DomainTreeEnabled On
23 * DomainTreeMaxdepth 25
24 * DomainTreeStripWWW On
25 * DomainTreePrefix /sites
26 * DomainTreeSuffix /html
28 * DomainTreeAliasRecursion Off
29 * DomainTreeAlias /??/exmaple /com/exmaple
30 * DomainTreeAlias /???/example /com/example
31 * DomainTreeAlias /*one/ /anyone/
32 * DomainTreeIgnore *.foo.com *.foo.co.uk
33 * DomainTreeForbid html.*
57 #define MODULE "mod_domaintree"
58 #define AUTHOR "<mike@iworks.at>"
66 #include "apr_tables.h"
67 #include "apr_strings.h"
73 #define APR_WANT_MEMFUNC
74 #define APR_WANT_STRFUNC
78 #include "http_config.h"
79 #include "http_core.h"
81 #include "http_protocol.h"
82 #include "http_request.h"
85 /* {{{ domaintree_module */
87 module AP_MODULE_DECLARE_DATA domaintree_module
;
90 /* {{{ Macros & Types */
94 #define MDT_CNF domaintree_conf
95 #define MDT_PTR (&domaintree_module)
97 #define GET_MDT_CNF(srv) ((MDT_CNF *) ap_get_module_config((srv)->module_config, MDT_PTR))
99 #define IF_SET_ELSE(a, b) (a != -1) ? (a) : (b)
102 #define EMPTY(str) ((str == NULL) || (*(str) == NUL))
104 # define local static
106 # define local static APR_INLINE
109 #define DT_LOG_ERR APLOG_MARK, APLOG_ERR, APR_SUCCESS, DT->server, "DomainTree: "
110 #define DT_LOG_WRN APLOG_MARK, APLOG_WARNING, APR_SUCCESS, DT->server, "DomainTree: "
111 #define DT_LOG_DBG APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: "
117 apr_table_t
*faketable
;
131 apr_global_mutex_t
*lock
;
134 typedef apr_array_header_t
*hostlist_t
;
153 local
char *domaintree_cache_get(MDT_CNF
*DT
, apr_time_t atime
, const char *host
)
156 dircache_entry_t
*cache_entry
;
158 apr_global_mutex_lock(DT
->dircache
.lock
);
160 if ((cache_entry
= apr_hash_get(DT
->dircache
.hmap
, host
, APR_HASH_KEY_STRING
))) {
161 cache_entry
->lacc
= atime
;
162 path
= cache_entry
->path
;
165 apr_global_mutex_unlock(DT
->dircache
.lock
);
168 ap_log_error(DT_LOG_DBG
"cache hit = %s for %s", path
, host
);
174 local
void domaintree_cache_set(MDT_CNF
*DT
, apr_time_t atime
, const char *host
, const char *path
)
177 dircache_entry_t
*cache_entry
;
179 apr_pool_create(&pool
, DT
->dircache
.pool
);
180 cache_entry
= apr_palloc(pool
, sizeof(dircache_entry_t
));
182 cache_entry
->pool
= pool
;
183 cache_entry
->lacc
= atime
;
184 cache_entry
->host
= apr_pstrdup(pool
, host
);
185 cache_entry
->path
= apr_pstrdup(pool
, path
);
187 apr_global_mutex_lock(DT
->dircache
.lock
);
189 if (apr_hash_count(DT
->dircache
.hmap
) >= DT
->dircache
.clim
) {
190 apr_hash_index_t
*idx
;
191 dircache_entry_t
*purge_this
= NULL
;
193 ap_log_error(DT_LOG_WRN
"reached cache limit (%ld)", DT
->dircache
.clim
);
195 for (idx
= apr_hash_first(DT
->dircache
.pool
, DT
->dircache
.hmap
); idx
; idx
= apr_hash_next(idx
)) {
196 dircache_entry_t
*current
;
198 apr_hash_this(idx
, NULL
, NULL
, (void *) ¤t
);
199 if ((!purge_this
) || (purge_this
->lacc
> current
->lacc
)) {
200 purge_this
= current
;
205 ap_log_error(DT_LOG_DBG
"cache del = %s", purge_this
->host
);
206 apr_hash_set(DT
->dircache
.hmap
, purge_this
->host
, APR_HASH_KEY_STRING
, NULL
);
207 apr_pool_destroy(purge_this
->pool
);
210 apr_hash_set(DT
->dircache
.hmap
, cache_entry
->host
, APR_HASH_KEY_STRING
, cache_entry
);
212 apr_global_mutex_unlock(DT
->dircache
.lock
);
214 ap_log_error(DT_LOG_DBG
"cache set = %s for %s", path
, host
);
220 local
char *strtr(char *string
, char from
, char to
)
225 while ((ptr
= strchr(ptr
, from
))) {
234 #define TRIM_BOTH (TRIM_LEFT|TRIM_RIGHT)
235 local
char *trim(char *string
, size_t length
, char what
, int where
)
237 if (where
& TRIM_RIGHT
) {
238 while (length
-- && (string
[length
] == what
)) {
239 string
[length
] = NUL
;
242 if (where
& TRIM_LEFT
) {
243 while (*string
== what
) {
250 local
int strmatch(const char *match
, const char *string
, const char **begin
, const char **end
)
252 *begin
= *end
= NULL
;
254 while (*match
&& *string
)
259 while (*match
== '*' || *match
== '?') {
268 *end
= string
+ strlen(string
);
272 if (!(string
= strchr(string
, *match
))) {
287 if (*match
== *string
) {
307 local
char *struniqchr(char *string
, char uniq
)
312 if (ptr
[0] == uniq
&& ptr
[1] == uniq
) {
315 for (; pos
[1] == uniq
; ++pos
);
316 for (; pos
[0]; ++pos
) {
326 local
char *domaintree_host(apr_pool_t
*pool
, MDT_CNF
*DT
, const char *host_name
)
331 if (EMPTY(host_name
)) {
332 ap_log_error(DT_LOG_WRN
"no host/server name");
337 host
= apr_pstrdup(pool
, host_name
);
339 /* check for :NN port */
340 if ((port
= strchr(host
, ':'))) {
347 /* strip leading & trailing dots */
348 host
= trim(host
, len
, '.', TRIM_BOTH
);
350 ap_log_error(DT_LOG_DBG
"host name = %s for %s", host
, host_name
);
355 local
char *domaintree_path(apr_pool_t
*pool
, MDT_CNF
*DT
, const char *host_name
)
357 long depth
= 0, maxdepth
= IF_SET_ELSE(DT
->maxdepth
, 20);
358 const char *host
= host_name
;
359 char *path
= NULL
, *host_ptr
;
361 while ((host_ptr
= strchr(host
, '.'))) {
363 /* check max depth */
364 if (++depth
> maxdepth
) {
365 ap_log_error(DT_LOG_ERR
"maxdepth exceeded (%ld)", maxdepth
);
370 if (host_ptr
- host
) {
373 if ((DT
->stripwww
> 0) && (depth
== 1) && (!strncmp(host
, "www.", sizeof("www")))) {
374 ap_log_error(DT_LOG_DBG
"strip www");
376 path
= apr_pstrcat(pool
, apr_pstrndup(pool
, host
, host_ptr
- host
), "/", path
, NULL
);
383 /* append last part if any and duplicate full path */
385 path
= apr_pstrcat(pool
, host
, "/", path
, NULL
);
388 ap_log_error(DT_LOG_DBG
"path name = %s for %s", path
, host_name
);
393 local
void domaintree_fake(apr_pool_t
*pool
, MDT_CNF
*DT
, char **path
)
397 apr_pool_t
*local_pool
;
398 const apr_array_header_t
*header
= apr_table_elts(DT
->aliases
.faketable
);
399 apr_table_entry_t
*array
= (apr_table_entry_t
*) header
->elts
;
401 if (APR_SUCCESS
!= apr_pool_create(&local_pool
, pool
)) {
408 if (recurlevel
++ > DT
->aliases
.recursion
) {
409 ap_log_error(DT_LOG_ERR
"maximum alias recursion level (%ld) exceeded! Check if you have recursive definitions of DomainTreeAlias directives.", DT
->aliases
.recursion
);
413 for (i
= 0; i
< header
->nelts
; ++i
) {
414 const char *begin
, *end
;
416 ap_log_error(DT_LOG_DBG
"fake test = %s on %s", array
[i
].key
, *path
);
417 if (strmatch(array
[i
].key
, *path
, &begin
, &end
)) {
418 ap_log_error(DT_LOG_DBG
"fake done = %s (%s <> %s)", *path
, array
[i
].key
, array
[i
].val
);
419 *path
= apr_pstrcat(local_pool
, "/", apr_pstrndup(local_pool
, *path
, begin
- *path
), "/", array
[i
].val
, "/", end
, NULL
);
423 } while (more
&& (DT
->aliases
.recursion
> 0));
425 *path
= apr_pstrdup(pool
, struniqchr(*path
, '/'));
427 apr_pool_destroy(local_pool
);
430 local
int domaintree_test(MDT_CNF
*DT
, const char *host
, int argc
, const char **argv
)
434 const char *begin
, *end
, *host_end
= host
+ strlen(host
);
436 for (i
= 0; i
< argc
; ++i
) {
437 ap_log_error(DT_LOG_DBG
"host test = %s <> %s", argv
[i
], host
);
438 if (strmatch(argv
[i
], host
, &begin
, &end
) && begin
== host
&& end
== host_end
) {
439 ap_log_error(DT_LOG_DBG
"test done = %s by %s", host
, argv
[i
]);
450 static STATUS
domaintree_hook_post_config(apr_pool_t
*pconf
, apr_pool_t
*plog
, apr_pool_t
*ptemp
, server_rec
*s
)
452 ap_add_version_component(pconf
, "MDT/" VERSION
);
456 static STATUS
domaintree_hook_translate_name(request_rec
*r
)
459 char *host
, *path
, *docroot
;
461 if ((!(DT
= GET_MDT_CNF(r
->server
))) || (DT
->enabled
< 1)) {
466 ap_log_error(DT_LOG_DBG
"processid = %d", (int) getpid());
469 /* get a usable host name */
470 if (!(host
= domaintree_host(r
->pool
, DT
, ap_get_server_name(r
)))) {
475 if (domaintree_test(DT
, host
, DT
->ignore
->nelts
, (const char **) DT
->ignore
->elts
)) {
480 if (domaintree_test(DT
, host
, DT
->forbid
->nelts
, (const char **) DT
->forbid
->elts
)) {
481 return HTTP_FORBIDDEN
;
485 if ((DT
->dircache
.clim
< 1) || (!(path
= domaintree_cache_get(DT
, r
->request_time
, host
)))) {
487 if (!(path
= domaintree_path(r
->pool
, DT
, host
))) {
491 /* apply any aliases */
492 if (apr_table_elts(DT
->aliases
.faketable
)->nelts
) {
493 domaintree_fake(r
->pool
, DT
, &path
);
497 if (DT
->dircache
.clim
> 0) {
498 domaintree_cache_set(DT
, r
->request_time
, host
, path
);
502 /* compose virtual docroot */
503 docroot
= struniqchr(apr_pstrcat(r
->pool
, DT
->prefix
, "/", path
, "/", DT
->suffix
, "/", NULL
), '/');
506 if (DT
->statroot
> 0) {
509 switch (apr_stat(&sb
, docroot
, APR_FINFO_MIN
, r
->pool
)) {
512 ap_log_error(DT_LOG_DBG
"stat path = %s (success)", docroot
);
516 ap_log_error(DT_LOG_DBG
"stat path = %s (failure)", docroot
);
521 /* set virtual docroot */
522 apr_table_set(r
->subprocess_env
, "VIRTUAL_DOCUMENT_ROOT", docroot
);
525 r
->canonical_filename
= "";
526 r
->filename
= apr_pstrcat(r
->pool
, docroot
, EMPTY(r
->uri
) ? NULL
: ('/' == *r
->uri
? r
->uri
+ 1 : r
->uri
), NULL
);
528 ap_log_error(DT_LOG_DBG
"path done = %s", r
->filename
);
533 static void domaintree_hooks(apr_pool_t
*pool
)
535 static const char * const pre
[] = {"mod_alias.c", "mod_userdir.c", NULL
};
537 ap_hook_post_config(domaintree_hook_post_config
, NULL
, NULL
, APR_HOOK_MIDDLE
);
538 ap_hook_translate_name(domaintree_hook_translate_name
, pre
, NULL
, APR_HOOK_FIRST
);
542 /* {{{ Configuration */
544 static void *domaintree_create_srv(apr_pool_t
*p
, server_rec
*s
)
546 MDT_CNF
*DT
= (MDT_CNF
*) apr_palloc(p
, sizeof(MDT_CNF
));
554 DT
->prefix
= "/var/www";
555 DT
->suffix
= "public_html";
557 DT
->ignore
= apr_array_make(p
, 0, sizeof(char *));
558 DT
->forbid
= apr_array_make(p
, 0, sizeof(char *));
560 DT
->aliases
.recursion
= -1;
561 DT
->aliases
.faketable
= apr_table_make(p
, 0);
563 DT
->dircache
.clim
= -1;
564 DT
->dircache
.hmap
= apr_hash_make(p
);
565 apr_pool_create(&DT
->dircache
.pool
, p
);
566 apr_global_mutex_create(&DT
->dircache
.lock
, __FILE__
, APR_LOCK_DEFAULT
, p
);
568 fprintf(stderr
, "MDT: cfg create %p\n", DT
);
573 static void *domaintree_merge_srv(apr_pool_t
*p
, void *old_cfg_ptr
, void *new_cfg_ptr
)
575 MDT_CNF
*old_cfg
= (MDT_CNF
*) old_cfg_ptr
, *new_cfg
= (MDT_CNF
*) new_cfg_ptr
;
576 MDT_CNF
*DT
= (MDT_CNF
*) apr_palloc(p
, sizeof(MDT_CNF
));
578 DT
->server
= new_cfg
->server
;
579 DT
->enabled
= IF_SET_ELSE(new_cfg
->enabled
, old_cfg
->enabled
);
580 DT
->stripwww
= IF_SET_ELSE(new_cfg
->stripwww
, old_cfg
->stripwww
);
581 DT
->statroot
= IF_SET_ELSE(new_cfg
->statroot
, old_cfg
->statroot
);
582 DT
->maxdepth
= IF_SET_ELSE(new_cfg
->maxdepth
, old_cfg
->maxdepth
);
584 DT
->prefix
= EMPTY(new_cfg
->prefix
) ? EMPTY(old_cfg
->prefix
) ? "/var/www" : old_cfg
->prefix
: new_cfg
->prefix
;
585 DT
->suffix
= EMPTY(new_cfg
->suffix
) ? EMPTY(old_cfg
->suffix
) ? "public_html" : old_cfg
->suffix
: new_cfg
->suffix
;
587 DT
->ignore
= apr_array_append(p
, new_cfg
->ignore
, old_cfg
->ignore
);
588 DT
->forbid
= apr_array_append(p
, new_cfg
->forbid
, old_cfg
->forbid
);
590 DT
->aliases
.recursion
= IF_SET_ELSE(new_cfg
->aliases
.recursion
, old_cfg
->aliases
.recursion
);
591 DT
->aliases
.faketable
= apr_table_overlay(p
, new_cfg
->aliases
.faketable
, old_cfg
->aliases
.faketable
);
593 DT
->dircache
.clim
= IF_SET_ELSE(new_cfg
->dircache
.clim
, old_cfg
->dircache
.clim
);
594 DT
->dircache
.hmap
= apr_hash_overlay(p
, new_cfg
->dircache
.hmap
, old_cfg
->dircache
.hmap
);
595 apr_global_mutex_create(&new_cfg
->dircache
.lock
, __FILE__
, APR_LOCK_DEFAULT
, p
);
597 fprintf(stderr
, "MDT: cfg merge %p + %p = %p\n", old_cfg
, new_cfg
, DT
);
602 static const char *domaintree_init_enable(cmd_parms
*cmd
, void *conf
, int flag
)
604 GET_MDT_CNF(cmd
->server
)->enabled
= flag
;
608 static const char *domaintree_init_stripwww(cmd_parms
*cmd
, void *conf
, int flag
)
610 GET_MDT_CNF(cmd
->server
)->stripwww
= flag
;
614 static const char *domaintree_init_statroot(cmd_parms
*cmd
, void *conf
, int flag
)
616 GET_MDT_CNF(cmd
->server
)->statroot
= flag
;
620 static const char *domaintree_init_prefix(cmd_parms
*cmd
, void *conf
, const char *prefix
)
622 GET_MDT_CNF(cmd
->server
)->prefix
= EMPTY(prefix
) ? "/" : trim(apr_pstrdup(cmd
->pool
, prefix
), strlen(prefix
), '/', TRIM_RIGHT
);
626 static const char *domaintree_init_suffix(cmd_parms
*cmd
, void *conf
, const char *suffix
)
628 GET_MDT_CNF(cmd
->server
)->suffix
= EMPTY(suffix
) ? "" : trim(apr_pstrdup(cmd
->pool
, suffix
), strlen(suffix
), '/', TRIM_BOTH
);
632 static const char *domaintree_init_maxdepth(cmd_parms
*cmd
, void *conf
, const char *max_depth
)
636 if ((depth
= atol(max_depth
))) {
638 GET_MDT_CNF(cmd
->server
)->maxdepth
= depth
;
640 return "Maximum DomainTree depth cannot be negative.";
647 static const char *domaintree_init_aliasrecursion(cmd_parms
*cmd
, void *conf
, const char *alias_recursion
)
651 if ((recursion
= atol(alias_recursion
))) {
652 if (recursion
>= 0L) {
653 GET_MDT_CNF(cmd
->server
)->aliases
.recursion
= recursion
;
655 return "DomainTree alias recursion cannot be negative.";
662 static const char *domaintree_init_alias(cmd_parms
*cmd
, void *conf
, const char *fake
, const char *real
)
664 char *f
= strtr(apr_pstrdup(cmd
->pool
, fake
), '.', '/'), *r
= strtr(apr_pstrdup(cmd
->pool
, real
), '.', '/');
666 apr_table_set(GET_MDT_CNF(cmd
->server
)->aliases
.faketable
, trim(f
, strlen(f
), '/', TRIM_BOTH
), trim(r
, strlen(r
), '/', TRIM_BOTH
));
671 static const char *domaintree_init_cache(cmd_parms
*cmd
, void *conf
, const char *cache
)
675 if ((limit
= atol(cache
))) {
677 GET_MDT_CNF(cmd
->server
)->dircache
.clim
= limit
;
679 return "DomainTree cache limit cannot be negative.";
686 static const char *domaintree_init_ignore(cmd_parms
*cmd
, void *conf
, const char *ignore
)
688 *((char **) apr_array_push(GET_MDT_CNF(cmd
->server
)->ignore
)) = trim(apr_pstrdup(cmd
->pool
, ignore
), strlen(ignore
), '.', TRIM_BOTH
);
692 static const char *domaintree_init_forbid(cmd_parms
*cmd
, void *conf
, const char *forbid
)
694 *((char **) apr_array_push(GET_MDT_CNF(cmd
->server
)->forbid
)) = trim(apr_pstrdup(cmd
->pool
, forbid
), strlen(forbid
), '.', TRIM_BOTH
);
701 static command_rec domaintree_commands
[] = {
703 "DomainTreeEnabled", domaintree_init_enable
, NULL
, RSRC_CONF
,
704 "Turn the module on or off."
708 "DomainTreeStripWWW", domaintree_init_stripwww
, NULL
, RSRC_CONF
,
709 "Strip leading www from host. (default On)"
713 "DomainTreeStatRoot", domaintree_init_statroot
, NULL
, RSRC_CONF
,
714 "Wheter to check for the evaluated virtual document root with a stat call. (default Off)"
718 "DomainTreePrefix", domaintree_init_prefix
, NULL
, RSRC_CONF
,
719 "DomainTree path prefix. (default /var/www) Do not forget the leading slash!"
723 "DomainTreeSuffix", domaintree_init_suffix
, NULL
, RSRC_CONF
,
724 "DomainTree path suffix. (default public_html)"
728 "DomainTreeMaxdepth", domaintree_init_maxdepth
, NULL
, RSRC_CONF
,
729 "DomainTree max path depth. (default 20)"
733 "DomainTreeAliasRecursion", domaintree_init_aliasrecursion
, NULL
, RSRC_CONF
,
734 "Whether (and how often at the maximum) DomainTree should walk recursively "
735 "through the aliases list as long as matching aliases are found. (default: 0 = turned off)"
739 "DomainTreeAlias", domaintree_init_alias
, NULL
, RSRC_CONF
,
740 "DomainTree aliases; e.g. DomainTreeAlias com/example/tickets com/example/support (dots or slashes equal)"
744 "DomainTreeCache", domaintree_init_cache
, NULL
, RSRC_CONF
,
745 "DomainTree server-wide host to directory cache; specify how many cache entries to allow (default: 0 = turned off)"
749 "DomainTreeIgnore", domaintree_init_ignore
, NULL
, RSRC_CONF
,
750 "DomainTree ignored hosts; uses the same matching alogrithm like DomainTreeAlias"
754 "DomainTreeForbid", domaintree_init_forbid
, NULL
, RSRC_CONF
,
755 "DomanTree forbidden hosts; uses the same matching algorithm like DomainTreeAlias"
762 /* {{{ Module Administrativa */
764 module AP_MODULE_DECLARE_DATA domaintree_module
= {
765 STANDARD20_MODULE_STUFF
,
766 NULL
, /* create per-dir */
767 NULL
, /* merge per-dir */
768 domaintree_create_srv
, /* create per-server */
769 domaintree_merge_srv
, /* merge per-server */
770 domaintree_commands
, /* config commands */
771 domaintree_hooks
/* hooks */
781 * vim600: noet sw=4 ts=4 fdm=marker
782 * vim<600: noet sw=4 ts=4