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