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