96d955fb973fbae4bed231cb1bfb94b154293ec8
[m6w6/mod-domaintree] / mod_domaintree.c
1 /*
2 * mod_domaintree.c - Apache2
3 *
4 * $Id$
5 *
6 * Copyright 2005 Michael Wallner <mike@iworks.at>
7 *
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
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
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.
19 *
20 *
21 * <pre>
22 * DomainTreeEnabled On
23 * DomainTreeMaxdepth 25
24 * DomainTreeStripWWW On
25 * DomainTreePrefix /sites
26 * DomainTreeSuffix /html
27 * DomainTreeCache 5
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.*
34 * <Directory "/sites/.../home">
35 * DomainTreeSuexec
36 * </Directory>
37 *
38 * /sites
39 * +- /at
40 * | +- /co
41 * | | +- /company
42 * | | +- /html
43 * | | +- /sub1
44 * | | | +- /html
45 * | | +- /sub2
46 * | | +- /html
47 * | +- /or
48 * | +- /organisation
49 * | +- /html
50 * +- /com
51 * +- /example
52 * +- /html
53 * </pre>
54 */
55
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
59
60 #define MODULE "mod_domaintree"
61 #define AUTHOR "<mike@iworks.at>"
62 #define VERSION "1.5"
63
64 /* {{{ Includes */
65
66 #include "apr.h"
67 #include "apr_lib.h"
68 #include "apr_hash.h"
69 #include "apr_tables.h"
70 #include "apr_strings.h"
71
72 #if APR_HAVE_UNISTD_H
73 #include <unistd.h>
74 #endif
75
76 #define APR_WANT_MEMFUNC
77 #define APR_WANT_STRFUNC
78 #include "apr_want.h"
79
80 #include "httpd.h"
81 #include "http_config.h"
82 #include "http_core.h"
83 #include "http_log.h"
84 #include "http_protocol.h"
85 #include "http_request.h"
86
87 /* }}} */
88 /* {{{ domaintree_module */
89
90 module AP_MODULE_DECLARE_DATA domaintree_module;
91
92 /* }}} */
93 /* {{{ Macros & Types */
94
95 #ifndef HAVE_UNIX_SUEXEC
96 # ifdef SUEXEC_BIN
97 # define HAVE_UNIX_SUEXEC
98 # endif
99 #endif
100 #ifdef HAVE_UNIX_SUEXEC
101 # include "unixd.h"
102 #endif
103
104 #define DBG 0
105
106 #define MDT_CNF domaintree_conf
107 #define MDT_PTR (&domaintree_module)
108
109 #define GET_MDT_CNF(srv) ((MDT_CNF *) ap_get_module_config((srv)->module_config, MDT_PTR))
110
111 #define IF_SET_ELSE(a, b) (a != -1) ? (a) : (b)
112
113 #define NUL '\0'
114 #define EMPTY(str) ((str == NULL) || (*(str) == NUL))
115 #if DBG
116 # define local static
117 #else
118 # define local static APR_INLINE
119 #endif
120
121 #define DT_LOG_ERR APLOG_MARK, APLOG_ERR, APR_SUCCESS, DT->server, "DomainTree: "
122 #define DT_LOG_WRN APLOG_MARK, APLOG_WARNING, APR_SUCCESS, DT->server, "DomainTree: "
123 #define DT_LOG_DBG APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server, "DomainTree: "
124
125 typedef int STATUS;
126
127 typedef struct {
128 long recursion;
129 apr_table_t *faketable;
130 } aliases_t;
131
132 typedef struct {
133 char *host;
134 char *path;
135 apr_time_t lacc;
136 apr_pool_t *pool;
137 } dircache_entry_t;
138
139 typedef struct {
140 long clim;
141 apr_hash_t *hmap;
142 apr_pool_t *pool;
143 apr_global_mutex_t *lock;
144 } dircache_t;
145
146 typedef apr_array_header_t *hostlist_t;
147
148 typedef struct {
149 server_rec *server;
150 int enabled;
151 int stripwww;
152 int statroot;
153 long maxdepth;
154 char *prefix;
155 char *suffix;
156 aliases_t aliases;
157 dircache_t dircache;
158 hostlist_t ignore;
159 hostlist_t forbid;
160 #ifdef HAVE_UNIX_SUEXEC
161 hostlist_t suexec;
162 #endif
163 } domaintree_conf;
164
165 /* }}} */
166 /* {{{ dircache */
167
168 local char *domaintree_cache_get(MDT_CNF *DT, apr_time_t atime, const char *host)
169 {
170 char *path = NULL;
171 dircache_entry_t *cache_entry;
172
173 apr_global_mutex_lock(DT->dircache.lock);
174
175 if ((cache_entry = apr_hash_get(DT->dircache.hmap, host, APR_HASH_KEY_STRING))) {
176 cache_entry->lacc = atime;
177 path = cache_entry->path;
178 }
179
180 apr_global_mutex_unlock(DT->dircache.lock);
181
182 if (path) {
183 ap_log_error(DT_LOG_DBG "cache hit = %s for %s", path, host);
184 }
185
186 return path;
187 }
188
189 local void domaintree_cache_set(MDT_CNF *DT, apr_time_t atime, const char *host, const char *path)
190 {
191 apr_pool_t *pool;
192 dircache_entry_t *cache_entry;
193
194 apr_pool_create(&pool, DT->dircache.pool);
195 cache_entry = apr_palloc(pool, sizeof(dircache_entry_t));
196
197 cache_entry->pool = pool;
198 cache_entry->lacc = atime;
199 cache_entry->host = apr_pstrdup(pool, host);
200 cache_entry->path = apr_pstrdup(pool, path);
201
202 apr_global_mutex_lock(DT->dircache.lock);
203
204 if (apr_hash_count(DT->dircache.hmap) >= DT->dircache.clim) {
205 apr_hash_index_t *idx;
206 dircache_entry_t *purge_this = NULL;
207
208 ap_log_error(DT_LOG_WRN "reached cache limit (%ld)", DT->dircache.clim);
209
210 for (idx = apr_hash_first(DT->dircache.pool, DT->dircache.hmap); idx; idx = apr_hash_next(idx)) {
211 dircache_entry_t *current;
212
213 apr_hash_this(idx, NULL, NULL, (void *) &current);
214 if ((!purge_this) || (purge_this->lacc > current->lacc)) {
215 purge_this = current;
216 }
217 }
218
219 if (purge_this) {
220 ap_log_error(DT_LOG_DBG "cache del = %s", purge_this->host);
221 apr_hash_set(DT->dircache.hmap, purge_this->host, APR_HASH_KEY_STRING, NULL);
222 apr_pool_destroy(purge_this->pool);
223 }
224 }
225 apr_hash_set(DT->dircache.hmap, cache_entry->host, APR_HASH_KEY_STRING, cache_entry);
226
227 apr_global_mutex_unlock(DT->dircache.lock);
228
229 ap_log_error(DT_LOG_DBG "cache set = %s for %s", path, host);
230 }
231
232 /* }}} */
233 /* {{{ Helpers */
234
235 local char *strtr(char *string, char from, char to)
236 {
237 char *ptr = string;
238
239 if (from != to) {
240 while ((ptr = strchr(ptr, from))) {
241 *ptr = to;
242 }
243 }
244 return string;
245 }
246
247 #define TRIM_LEFT 1
248 #define TRIM_RIGHT 2
249 #define TRIM_BOTH (TRIM_LEFT|TRIM_RIGHT)
250 local char *trim(char *string, size_t length, char what, int where)
251 {
252 if (where & TRIM_RIGHT) {
253 while (length-- && (string[length] == what)) {
254 string[length] = NUL;
255 }
256 }
257 if (where & TRIM_LEFT) {
258 while (*string == what) {
259 ++string;
260 }
261 }
262 return string;
263 }
264
265 local int strmatch(const char *match, const char *string, const char **begin, const char **end)
266 {
267 *begin = *end = NULL;
268
269 while (*match && *string)
270 {
271 switch (*match)
272 {
273 case '*':
274 while (*match == '*' || *match == '?') {
275 ++match;
276 }
277
278 if (!*begin) {
279 *begin = string;
280 }
281
282 if (!*match) {
283 *end = string + strlen(string);
284 return 1;
285 }
286
287 if (!(string = strchr(string, *match))) {
288 *end = string;
289 return 0;
290 }
291 break;
292
293 case '?':
294 if (!*begin) {
295 *begin = string;
296 }
297 ++string;
298 ++match;
299 break;
300
301 default:
302 if (*match == *string) {
303 if (!*begin) {
304 *begin = string;
305 }
306 ++match;
307 } else {
308 if (*begin) {
309 *end = string - 1;
310 return 0;
311 }
312 }
313 ++string;
314 break;
315 }
316 }
317
318 *end = string;
319 return 1;
320 }
321
322 local char *struniqchr(char *string, char uniq)
323 {
324 char *ptr = string;
325
326 while (ptr[0]) {
327 if (ptr[0] == uniq && ptr[1] == uniq) {
328 char *pos = &ptr[1];
329
330 for (; pos[1] == uniq; ++pos);
331 for (; pos[0]; ++pos) {
332 pos[0] = pos[1];
333 }
334 }
335 ++ptr;
336 }
337
338 return string;
339 }
340
341 local char *domaintree_host(apr_pool_t *pool, MDT_CNF *DT, const char *host_name)
342 {
343 size_t len;
344 char *port, *host;
345
346 if (EMPTY(host_name)) {
347 ap_log_error(DT_LOG_WRN "no host/server name");
348 return NULL;
349 }
350
351 /* copy hostname */
352 host = apr_pstrdup(pool, host_name);
353
354 /* check for :NN port */
355 if ((port = strchr(host, ':'))) {
356 len = port - host;
357 *port = NUL;
358 } else {
359 len = strlen(host);
360 }
361
362 /* strip leading & trailing dots */
363 host = trim(host, len, '.', TRIM_BOTH);
364
365 ap_log_error(DT_LOG_DBG "host name = %s for %s", host, host_name);
366
367 return host;
368 }
369
370 local char *domaintree_path(apr_pool_t *pool, MDT_CNF *DT, const char *host_name)
371 {
372 long depth = 0, maxdepth = IF_SET_ELSE(DT->maxdepth, 20);
373 const char *host = host_name;
374 char *path = NULL, *host_ptr;
375
376 while ((host_ptr = strchr(host, '.'))) {
377
378 /* check max depth */
379 if (++depth > maxdepth) {
380 ap_log_error(DT_LOG_ERR "maxdepth exceeded (%ld)", maxdepth);
381 return NULL;
382 }
383
384 /* append part */
385 if (host_ptr - host) {
386
387 /* strip WWW */
388 if ((DT->stripwww > 0) && (depth == 1) && (!strncmp(host, "www.", sizeof("www")))) {
389 ap_log_error(DT_LOG_DBG "strip www");
390 } else {
391 path = apr_pstrcat(pool, apr_pstrndup(pool, host, host_ptr - host), "/", path, NULL);
392 }
393 }
394
395 host = host_ptr + 1;
396 }
397
398 /* append last part if any and duplicate full path */
399 if (*host) {
400 path = apr_pstrcat(pool, host, "/", path, NULL);
401 }
402
403 ap_log_error(DT_LOG_DBG "path name = %s for %s", path, host_name);
404
405 return path;
406 }
407
408 local void domaintree_fake(apr_pool_t *pool, MDT_CNF *DT, char **path)
409 {
410 int i, more;
411 long recurlevel = 0;
412 apr_pool_t *local_pool;
413 const apr_array_header_t *header = apr_table_elts(DT->aliases.faketable);
414 apr_table_entry_t *array = (apr_table_entry_t *) header->elts;
415
416 if (APR_SUCCESS != apr_pool_create(&local_pool, pool)) {
417 return;
418 }
419
420 do {
421 more = 0;
422
423 if (recurlevel++ > DT->aliases.recursion) {
424 ap_log_error(DT_LOG_ERR "maximum alias recursion level (%ld) exceeded! Check if you have recursive definitions of DomainTreeAlias directives.", DT->aliases.recursion);
425 break;
426 }
427
428 for (i = 0; i < header->nelts; ++i) {
429 const char *begin, *end;
430
431 ap_log_error(DT_LOG_DBG "fake test = %s on %s", array[i].key, *path);
432 if (strmatch(array[i].key, *path, &begin, &end)) {
433 ap_log_error(DT_LOG_DBG "fake done = %s (%s <> %s)", *path, array[i].key, array[i].val);
434 *path = apr_pstrcat(local_pool, "/", apr_pstrndup(local_pool, *path, begin - *path), "/", array[i].val, "/", end, NULL);
435 more = 1;
436 }
437 }
438 } while (more && (DT->aliases.recursion > 0));
439
440 *path = apr_pstrdup(pool, struniqchr(*path, '/'));
441
442 apr_pool_destroy(local_pool);
443 }
444
445 #define TEST_IS_BOS 1
446 #define TEST_IS_EOS 2
447 #define TEST_IS_AOS 3
448 local int domaintree_test(MDT_CNF *DT, const char *host, int argc, const char **argv, int flags, const char **bos, const char **eos)
449 {
450 if (argc) {
451 int i;
452 const char *begin, *end, *host_end = host + strlen(host);
453
454 for (i = 0; i < argc; ++i) {
455 ap_log_error(DT_LOG_DBG "host test = %s <> %s", argv[i], host);
456 if (strmatch(argv[i], host, &begin, &end)) {
457 if ((flags & TEST_IS_BOS) && begin != host) {
458 continue;
459 }
460 if ((flags & TEST_IS_EOS) && end != host_end) {
461 continue;
462 }
463 if (bos) {
464 *bos = begin;
465 }
466 if (eos) {
467 *eos = end;
468 }
469 ap_log_error(DT_LOG_DBG "test done = %s by %s", host, argv[i]);
470 return i+1;
471 }
472 }
473 }
474 return 0;
475 }
476
477 /* }}} */
478 /* {{{ Hooks */
479
480 static STATUS domaintree_hook_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
481 {
482 ap_add_version_component(pconf, "MDT/" VERSION);
483 return OK;
484 }
485
486 static STATUS domaintree_hook_translate_name(request_rec *r)
487 {
488 MDT_CNF *DT;
489 char *host, *path, *docroot;
490
491 if ((!(DT = GET_MDT_CNF(r->server))) || (DT->enabled < 1)) {
492 return DECLINED;
493 }
494
495 #if DBG
496 ap_log_error(DT_LOG_DBG "processid = %d", (int) getpid());
497 #endif
498
499 /* get a usable host name */
500 if (!(host = domaintree_host(r->pool, DT, ap_get_server_name(r)))) {
501 return DECLINED;
502 }
503
504 /* ignore? */
505 if (domaintree_test(DT, host, DT->ignore->nelts, (const char **) DT->ignore->elts, TEST_IS_AOS, NULL, NULL)) {
506 return DECLINED;
507 }
508
509 /* forbid? */
510 if (domaintree_test(DT, host, DT->forbid->nelts, (const char **) DT->forbid->elts, TEST_IS_AOS, NULL, NULL)) {
511 return HTTP_FORBIDDEN;
512 }
513
514 /* check cache */
515 if ((DT->dircache.clim < 1) || (!(path = domaintree_cache_get(DT, r->request_time, host)))) {
516 /* build path */
517 if (!(path = domaintree_path(r->pool, DT, host))) {
518 return DECLINED;
519 }
520
521 /* apply any aliases */
522 if (apr_table_elts(DT->aliases.faketable)->nelts) {
523 domaintree_fake(r->pool, DT, &path);
524 }
525
526 /* add to cache */
527 if (DT->dircache.clim > 0) {
528 domaintree_cache_set(DT, r->request_time, host, path);
529 }
530 }
531
532 /* compose virtual docroot */
533 docroot = struniqchr(apr_pstrcat(r->pool, DT->prefix, "/", path, "/", DT->suffix, "/", NULL), '/');
534
535 /* stat docroot */
536 if (DT->statroot > 0) {
537 apr_finfo_t sb;
538
539 switch (apr_stat(&sb, docroot, APR_FINFO_MIN, r->pool)) {
540 case APR_SUCCESS:
541 case APR_INCOMPLETE:
542 ap_log_error(DT_LOG_DBG "stat path = %s (success)", docroot);
543 break;
544
545 default:
546 ap_log_error(DT_LOG_DBG "stat path = %s (failure)", docroot);
547 return DECLINED;
548 }
549 }
550
551 /* set virtual docroot */
552 apr_table_set(r->subprocess_env, "VIRTUAL_DOCUMENT_ROOT", docroot);
553
554 #ifdef HAVE_UNIX_SUEXEC
555 /* set suexec note */
556 {
557 const char *username, *separator;
558
559 if (domaintree_test(DT, docroot, DT->suexec->nelts, DT->suexec->elts, TEST_IS_BOS, NULL, &username)) {
560 if ((separator = strchr(username, '/'))) {
561 username = apr_pstrndup(r->pool, username, separator-username);
562 } else {
563 username = apr_pstrdup(r->pool, username);
564 }
565 apr_table_setn(r->notes, "mod_domaintree.suexec", username);
566 }
567 }
568 #endif
569
570 /* done */
571 r->canonical_filename = "";
572 r->filename = apr_pstrcat(r->pool, docroot, EMPTY(r->uri) ? NULL : ('/' == *r->uri ? r->uri + 1 : r->uri), NULL);
573
574 ap_log_error(DT_LOG_DBG "path done = %s", r->filename);
575
576 return OK;
577 }
578
579 #ifdef HAVE_UNIX_SUEXEC
580 static STATUS domaintree_hook_get_suexec_identity(const request_rec *r)
581 {
582 ap_unix_identity_t *ugid = NULL;
583 #if APR_HAS_USER
584 const char *username;
585
586 if ((username = apr_table_get(r->notes, "mod_domaintree.suexec"))) {
587 if ((ugid = apr_palloc(r->pool, sizeof(*ugid)))) {
588 if (APR_SUCCESS == apr_uid_get(&ugid->uid, &ugid->gid, username, r->pool)) {
589 ugid->userdir = 1;
590 }
591 }
592 }
593 #endif
594 return ugid;
595 }
596 #endif
597
598 static void domaintree_hooks(apr_pool_t *pool)
599 {
600 static const char * const pre[] = {"mod_alias.c", "mod_userdir.c", NULL};
601
602 ap_hook_post_config(domaintree_hook_post_config, NULL, NULL, APR_HOOK_MIDDLE);
603 ap_hook_translate_name(domaintree_hook_translate_name, pre, NULL, APR_HOOK_FIRST);
604 #ifdef HAVE_UNIX_SUEXEC
605 ap_hook_get_suexec_identity(domaintree_hook_get_suexec_identity, NULL, NULL, APR_HOOK_FIRST);
606 #endif
607 }
608
609 /* }}} */
610 /* {{{ Configuration */
611
612 static void *domaintree_create_srv(apr_pool_t *p, server_rec *s)
613 {
614 MDT_CNF *DT= (MDT_CNF *) apr_palloc(p, sizeof(MDT_CNF));
615
616 DT->server = s;
617 DT->enabled = -1;
618 DT->stripwww = -1;
619 DT->statroot = -1;
620 DT->maxdepth = -1;
621
622 DT->prefix = "/var/www";
623 DT->suffix = "public_html";
624
625 DT->ignore = apr_array_make(p, 0, sizeof(char *));
626 DT->forbid = apr_array_make(p, 0, sizeof(char *));
627 #ifdef HAVE_UNIX_SUEXEC
628 DT->suexec = apr_array_make(p, 0, sizeof(char *));
629 #endif
630
631 DT->aliases.recursion = -1;
632 DT->aliases.faketable = apr_table_make(p, 0);
633
634 DT->dircache.clim = -1;
635 DT->dircache.hmap = apr_hash_make(p);
636 apr_pool_create(&DT->dircache.pool, p);
637 apr_global_mutex_create(&DT->dircache.lock, __FILE__, APR_LOCK_DEFAULT, p);
638 #if DBG
639 fprintf(stderr, "MDT: cfg create %p\n", DT);
640 #endif
641 return DT;
642 }
643
644 static void *domaintree_merge_srv(apr_pool_t *p, void *old_cfg_ptr, void *new_cfg_ptr)
645 {
646 MDT_CNF *old_cfg = (MDT_CNF *) old_cfg_ptr, *new_cfg = (MDT_CNF *) new_cfg_ptr;
647 MDT_CNF *DT = (MDT_CNF *) apr_palloc(p, sizeof(MDT_CNF));
648
649 DT->server = new_cfg->server;
650 DT->enabled = IF_SET_ELSE(new_cfg->enabled, old_cfg->enabled);
651 DT->stripwww = IF_SET_ELSE(new_cfg->stripwww, old_cfg->stripwww);
652 DT->statroot = IF_SET_ELSE(new_cfg->statroot, old_cfg->statroot);
653 DT->maxdepth = IF_SET_ELSE(new_cfg->maxdepth, old_cfg->maxdepth);
654
655 DT->prefix = EMPTY(new_cfg->prefix) ? EMPTY(old_cfg->prefix) ? "/var/www" : old_cfg->prefix : new_cfg->prefix;
656 DT->suffix = EMPTY(new_cfg->suffix) ? EMPTY(old_cfg->suffix) ? "public_html" : old_cfg->suffix : new_cfg->suffix;
657
658 DT->ignore = apr_array_append(p, new_cfg->ignore, old_cfg->ignore);
659 DT->forbid = apr_array_append(p, new_cfg->forbid, old_cfg->forbid);
660 #ifdef HAVE_UNIX_SUEXEC
661 DT->suexec = apr_array_append(p, new_cfg->suexec, old_cfg->suexec);
662 #endif
663
664 DT->aliases.recursion = IF_SET_ELSE(new_cfg->aliases.recursion, old_cfg->aliases.recursion);
665 DT->aliases.faketable = apr_table_overlay(p, new_cfg->aliases.faketable, old_cfg->aliases.faketable);
666
667 DT->dircache.clim = IF_SET_ELSE(new_cfg->dircache.clim, old_cfg->dircache.clim);
668 DT->dircache.hmap = apr_hash_overlay(p, new_cfg->dircache.hmap, old_cfg->dircache.hmap);
669 apr_global_mutex_create(&new_cfg->dircache.lock, __FILE__, APR_LOCK_DEFAULT, p);
670 #if DBG
671 fprintf(stderr, "MDT: cfg merge %p + %p = %p\n", old_cfg, new_cfg, DT);
672 #endif
673 return DT;
674 }
675
676 static const char *domaintree_init_enable(cmd_parms *cmd, void *conf, int flag)
677 {
678 GET_MDT_CNF(cmd->server)->enabled = flag;
679 return NULL;
680 }
681
682 static const char *domaintree_init_stripwww(cmd_parms *cmd, void *conf, int flag)
683 {
684 GET_MDT_CNF(cmd->server)->stripwww = flag;
685 return NULL;
686 }
687
688 static const char *domaintree_init_statroot(cmd_parms *cmd, void *conf, int flag)
689 {
690 GET_MDT_CNF(cmd->server)->statroot = flag;
691 return NULL;
692 }
693
694 static const char *domaintree_init_prefix(cmd_parms *cmd, void *conf, const char *prefix)
695 {
696 GET_MDT_CNF(cmd->server)->prefix = EMPTY(prefix) ? "/" : trim(apr_pstrdup(cmd->pool, prefix), strlen(prefix), '/', TRIM_RIGHT);
697 return NULL;
698 }
699
700 static const char *domaintree_init_suffix(cmd_parms *cmd, void *conf, const char *suffix)
701 {
702 GET_MDT_CNF(cmd->server)->suffix = EMPTY(suffix) ? "" : trim(apr_pstrdup(cmd->pool, suffix), strlen(suffix), '/', TRIM_BOTH);
703 return NULL;
704 }
705
706 static const char *domaintree_init_maxdepth(cmd_parms *cmd, void *conf, const char *max_depth)
707 {
708 long depth;
709
710 if ((depth = atol(max_depth))) {
711 if (depth >= 0L) {
712 GET_MDT_CNF(cmd->server)->maxdepth = depth;
713 } else {
714 return "Maximum DomainTree depth cannot be negative.";
715 }
716 }
717
718 return NULL;
719 }
720
721 static const char *domaintree_init_aliasrecursion(cmd_parms *cmd, void *conf, const char *alias_recursion)
722 {
723 long recursion;
724
725 if ((recursion = atol(alias_recursion))) {
726 if (recursion >= 0L) {
727 GET_MDT_CNF(cmd->server)->aliases.recursion = recursion;
728 } else {
729 return "DomainTree alias recursion cannot be negative.";
730 }
731 }
732
733 return NULL;
734 }
735
736 static const char *domaintree_init_alias(cmd_parms *cmd, void *conf, const char *fake, const char *real)
737 {
738 char *f = strtr(apr_pstrdup(cmd->pool, fake), '.', '/'), *r = strtr(apr_pstrdup(cmd->pool, real), '.', '/');
739
740 apr_table_set(GET_MDT_CNF(cmd->server)->aliases.faketable, trim(f, strlen(f), '/', TRIM_BOTH), trim(r, strlen(r), '/', TRIM_BOTH));
741
742 return NULL;
743 }
744
745 static const char *domaintree_init_cache(cmd_parms *cmd, void *conf, const char *cache)
746 {
747 long limit;
748
749 if ((limit = atol(cache))) {
750 if (limit >= 0L) {
751 GET_MDT_CNF(cmd->server)->dircache.clim = limit;
752 } else {
753 return "DomainTree cache limit cannot be negative.";
754 }
755 }
756
757 return NULL;
758 }
759
760 static const char *domaintree_init_ignore(cmd_parms *cmd, void *conf, const char *ignore)
761 {
762 *((char **) apr_array_push(GET_MDT_CNF(cmd->server)->ignore)) = trim(apr_pstrdup(cmd->pool, ignore), strlen(ignore), '.', TRIM_BOTH);
763 return NULL;
764 }
765
766 static const char *domaintree_init_forbid(cmd_parms *cmd, void *conf, const char *forbid)
767 {
768 *((char **) apr_array_push(GET_MDT_CNF(cmd->server)->forbid)) = trim(apr_pstrdup(cmd->pool, forbid), strlen(forbid), '.', TRIM_BOTH);
769 return NULL;
770 }
771
772 static const char *domaintree_init_suexec(cmd_parms *cmd, void *conf)
773 {
774 #ifdef HAVE_UNIX_SUEXEC
775 apr_finfo_t sb;
776
777 if (!cmd->path) {
778 return "DomainTreeSuexec is a per directory configuration directive";
779 }
780
781 switch (apr_stat(&sb, cmd->path, APR_FINFO_MIN, cmd->pool)) {
782 case APR_SUCCESS:
783 case APR_INCOMPLETE:
784 break;
785 default:
786 return "DomainTreeSuexec must be defined for an existing path";
787 }
788
789 *((char **) apr_array_push(GET_MDT_CNF(cmd->server)->suexec)) = trim(apr_pstrdup(cmd->pool, cmd->path), strlen(cmd->path), '.', TRIM_BOTH);
790
791 return NULL;
792 #else
793 return "HAVE_UNIX_SUEXEC was undefined at compile time";
794 #endif
795 }
796
797 /* }}} */
798 /* {{{ Commands */
799
800 static command_rec domaintree_commands[] = {
801 AP_INIT_FLAG(
802 "DomainTreeEnabled", domaintree_init_enable, NULL, RSRC_CONF,
803 "Turn the module on or off."
804 ),
805
806 AP_INIT_FLAG(
807 "DomainTreeStripWWW", domaintree_init_stripwww, NULL, RSRC_CONF,
808 "Strip leading www from host. (default On)"
809 ),
810
811 AP_INIT_FLAG(
812 "DomainTreeStatRoot", domaintree_init_statroot, NULL, RSRC_CONF,
813 "Wheter to check for the evaluated virtual document root with a stat call. (default Off)"
814 ),
815
816 AP_INIT_TAKE1(
817 "DomainTreePrefix", domaintree_init_prefix, NULL, RSRC_CONF,
818 "DomainTree path prefix. (default /var/www) Do not forget the leading slash!"
819 ),
820
821 AP_INIT_TAKE1(
822 "DomainTreeSuffix", domaintree_init_suffix, NULL, RSRC_CONF,
823 "DomainTree path suffix. (default public_html)"
824 ),
825
826 AP_INIT_TAKE1(
827 "DomainTreeMaxdepth", domaintree_init_maxdepth, NULL, RSRC_CONF,
828 "DomainTree max path depth. (default 20)"
829 ),
830
831 AP_INIT_TAKE1(
832 "DomainTreeAliasRecursion", domaintree_init_aliasrecursion, NULL, RSRC_CONF,
833 "Whether (and how often at the maximum) DomainTree should walk recursively "
834 "through the aliases list as long as matching aliases are found. (default: 0 = turned off)"
835 ),
836
837 AP_INIT_TAKE2(
838 "DomainTreeAlias", domaintree_init_alias, NULL, RSRC_CONF,
839 "DomainTree aliases; e.g. DomainTreeAlias com/example/tickets com/example/support (dots or slashes equal)"
840 ),
841
842 AP_INIT_TAKE1(
843 "DomainTreeCache", domaintree_init_cache, NULL, RSRC_CONF,
844 "DomainTree server-wide host to directory cache; specify how many cache entries to allow (default: 0 = turned off)"
845 ),
846
847 AP_INIT_ITERATE(
848 "DomainTreeIgnore", domaintree_init_ignore, NULL, RSRC_CONF,
849 "DomainTree ignored hosts; uses the same matching alogrithm like DomainTreeAlias"
850 ),
851
852 AP_INIT_ITERATE(
853 "DomainTreeForbid", domaintree_init_forbid, NULL, RSRC_CONF,
854 "DomanTree forbidden hosts; uses the same matching algorithm like DomainTreeAlias"
855 ),
856
857 AP_INIT_NO_ARGS(
858 "DomainTreeSuexec", domaintree_init_suexec, NULL, ACCESS_CONF,
859 "DomainTree user home directory; enable suexec hook for domain based user-dir hosting in this directory"
860 ),
861
862 { NULL }
863 };
864
865 /* }}} */
866 /* {{{ Module Administrativa */
867
868 module AP_MODULE_DECLARE_DATA domaintree_module = {
869 STANDARD20_MODULE_STUFF,
870 NULL, /* create per-dir */
871 NULL, /* merge per-dir */
872 domaintree_create_srv, /* create per-server */
873 domaintree_merge_srv, /* merge per-server */
874 domaintree_commands, /* config commands */
875 domaintree_hooks /* hooks */
876 };
877
878 /* }}} */
879
880 /*
881 * Local variables:
882 * tab-width: 4
883 * c-basic-offset: 4
884 * End:
885 * vim600: noet sw=4 ts=4 fdm=marker
886 * vim<600: noet sw=4 ts=4
887 */