- add build env
[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 *
33 * /sites
34 * +- /at
35 * | +- /co
36 * | | +- /company
37 * | | +- /html
38 * | | +- /sub1
39 * | | | +- /html
40 * | | +- /sub2
41 * | | +- /html
42 * | +- /or
43 * | +- /organisation
44 * | +- /html
45 * +- /com
46 * +- /example
47 * +- /html
48 * </pre>
49 */
50
51 #ifdef HAVE_CONFIG_H
52 #include "config.h"
53 #endif
54
55 #define MODULE "mod_domaintree"
56 #define AUTHOR "<mike@iworks.at"
57 #define VERSION "1.3"
58
59 /* {{{ Includes */
60
61 #include "apr.h"
62 #include "apr_lib.h"
63 #include "apr_hash.h"
64 #include "apr_tables.h"
65 #include "apr_strings.h"
66
67 #if APR_HAVE_UNISTD_H
68 #include <unistd.h>
69 #endif
70
71 #define APR_WANT_MEMFUNC
72 #define APR_WANT_STRFUNC
73 #include "apr_want.h"
74
75 #include "httpd.h"
76 #include "http_config.h"
77 #include "http_core.h"
78 #include "http_log.h"
79 #include "http_protocol.h"
80 #include "http_request.h"
81
82 /* }}} */
83 /* {{{ domaintree_module */
84
85 module AP_MODULE_DECLARE_DATA domaintree_module;
86
87 /* }}} */
88 /* {{{ Macros & Types */
89
90 #define MOD_DT_CNF domaintree_conf
91 #define MOD_DT_PTR (&domaintree_module)
92
93 #define GET_MOD_DT_CNF(srv) ((MOD_DT_CNF *) ap_get_module_config((srv)->module_config, MOD_DT_PTR))
94
95 #define NUL '\0'
96 #define EMPTY(str) ((str == NULL) || (*(str) == NUL))
97 #define local static APR_INLINE
98
99 #define DT_LOG_ERR ap_log_error(APLOG_MARK, APLOG_ERR, APR_SUCCESS, DT->server,
100 #define DT_LOG_WRN ap_log_error(APLOG_MARK, APLOG_WARNING, APR_SUCCESS, DT->server,
101 #define DT_LOG_DBG ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, DT->server,
102 #define DT_LOG_END );
103
104 typedef int STATUS;
105
106 typedef struct {
107 long recursion;
108 apr_table_t *faketable;
109 } aliases_t;
110
111 struct dircache_entry {
112 char *host;
113 char *path;
114 apr_time_t lacc;
115 apr_pool_t *pool;
116 };
117
118 typedef struct {
119 long clim;
120 apr_hash_t *hmap;
121 apr_pool_t *pool;
122 apr_global_mutex_t *lock;
123 } dircache_t;
124
125 typedef struct {
126 server_rec *server;
127 int enabled;
128 int stripwww;
129 int statroot;
130 long maxdepth;
131 char *prefix;
132 char *suffix;
133 aliases_t aliases;
134 dircache_t dircache;
135 } domaintree_conf;
136
137 /* }}} */
138 /* {{{ Helpers */
139
140 local char *strtr(char *string, char from, char to)
141 {
142 char *ptr = string;
143
144 if (from != to) {
145 while ((ptr = strchr(ptr, from))) {
146 *ptr = to;
147 }
148 }
149 return string;
150 }
151
152 #define TRIM_LEFT 1
153 #define TRIM_RIGHT 2
154 #define TRIM_BOTH (TRIM_LEFT|TRIM_RIGHT)
155 local char *trim(char *string, size_t length, char what, int where)
156 {
157 if (where & TRIM_RIGHT) {
158 while (length-- && (string[length] == what)) {
159 string[length] = NUL;
160 }
161 }
162 if (where & TRIM_LEFT) {
163 while (*string == what) {
164 ++string;
165 }
166 }
167 return string;
168 }
169
170 local int strmatch(const char *match, const char *string, const char **begin, const char **end)
171 {
172 *begin = *end = NULL;
173
174 while (*match)
175 {
176 switch (*match)
177 {
178 case '*':
179 while (*match == '*' || *match == '?') {
180 ++match;
181 }
182
183 if (!*begin) {
184 *begin = string;
185 }
186
187 if (!*match) {
188 *end = string + strlen(string);
189 return 1;
190 }
191
192 if (!(string = strchr(string, *match))) {
193 *end = string;
194 return 0;
195 }
196 break;
197
198 case '?':
199 if (!*begin) {
200 *begin = string;
201 }
202 ++string;
203 ++match;
204 break;
205
206 default:
207 if (*match == *string) {
208 if (!*begin) {
209 *begin = string;
210 }
211 ++match;
212 } else {
213 if (*begin) {
214 *end = string - 1;
215 return 0;
216 }
217 }
218 ++string;
219 break;
220 }
221 }
222
223 *end = string;
224 return 1;
225 }
226
227 local char *struniqchr(char *string, char uniq)
228 {
229 char *ptr = string;
230
231 while (*ptr) {
232 if (*ptr == uniq && *(ptr + 1) == uniq) {
233 char *pos = ptr + 1;
234
235 while (*(pos + 1) == uniq) {
236 ++pos;
237 }
238
239 memmove(ptr, pos, strlen(pos) + 1);
240 }
241 ++ptr;
242 }
243
244 return string;
245 }
246
247 local char *domaintree_host(apr_pool_t *pool, MOD_DT_CNF *DT, const char *host_name)
248 {
249 size_t len;
250 char *port, *host;
251
252 if (EMPTY(host_name)) {
253 DT_LOG_WRN
254 "DomainTree: no host/server name"
255 DT_LOG_END
256 return NULL;
257 }
258
259 /* copy hostname */
260 host = apr_pstrdup(pool, host_name);
261
262 /* check for :NN port */
263 if ((port = strchr(host, ':'))) {
264 len = port - host;
265 *port = NUL;
266 } else {
267 len = strlen(host);
268 }
269
270 /* strip leading & trailing dots */
271 host = trim(host, len, '.', TRIM_BOTH);
272
273 DT_LOG_DBG
274 "DomainTree: host name = %s for %s", host, host_name
275 DT_LOG_END
276
277 return host;
278 }
279
280 local char *domaintree_path(apr_pool_t *pool, MOD_DT_CNF *DT, const char *host_name)
281 {
282 long depth = 0;
283 const char *host = host_name;
284 char *path = NULL, *host_ptr;
285
286 while ((host_ptr = strchr(host, '.'))) {
287
288 /* check max depth */
289 if (++depth > DT->maxdepth) {
290 DT_LOG_ERR
291 "DomainTree: maxdepth exceeded (%ld)", DT->maxdepth
292 DT_LOG_END
293 return NULL;
294 }
295
296 /* append part */
297 if (host_ptr - host) {
298
299 /* strip WWW */
300 if (DT->stripwww && (depth == 1) && (!strncmp(host, "www.", sizeof("www")))) {
301 DT_LOG_DBG
302 "DomainTree: strip www"
303 DT_LOG_END
304 } else {
305 path = apr_pstrcat(pool, apr_pstrndup(pool, host, host_ptr - host), "/", path, NULL);
306 }
307 }
308
309 host = host_ptr + 1;
310 }
311
312 /* append last part if any and duplicate full path */
313 if (*host) {
314 path = apr_pstrcat(pool, host, "/", path, NULL);
315 }
316
317 DT_LOG_DBG
318 "DomainTree: path name = %s for %s", path, host_name
319 DT_LOG_END
320
321 return path;
322 }
323
324 local void domaintree_fake(apr_pool_t *pool, MOD_DT_CNF *DT, char **path)
325 {
326 int i, more;
327 long recurlevel = 0;
328 apr_pool_t *local_pool;
329 const apr_array_header_t *header = apr_table_elts(DT->aliases.faketable);
330 apr_table_entry_t *array = (apr_table_entry_t *) header->elts;
331
332 if (APR_SUCCESS != apr_pool_create(&local_pool, pool)) {
333 return;
334 }
335
336 do {
337 more = 0;
338
339 if (recurlevel++ > DT->aliases.recursion) {
340 DT_LOG_ERR
341 "DomainTree: maximum alias recursion level (%ld) exceeded! "
342 "Check if you have recursive definitions of DomainTreeAlias directives.",
343 DT->aliases.recursion
344 DT_LOG_END
345 break;
346 }
347
348 for (i = 0; i < header->nelts; ++i) {
349 const char *begin, *end;
350
351 DT_LOG_DBG
352 "DomainTree: fake test = %s on %s", array[i].key, *path
353 DT_LOG_END
354
355 if (strmatch(array[i].key, *path, &begin, &end)) {
356 more = 1;
357 *path = apr_pstrcat(local_pool, "/", apr_pstrndup(local_pool, *path, begin - *path), "/", array[i].val, "/", end, NULL);
358
359 DT_LOG_DBG
360 "DomainTree: fake done = %s (%s <> %s)", *path, array[i].key, array[i].val
361 DT_LOG_END
362 }
363 }
364 } while (more && DT->aliases.recursion);
365
366 *path = apr_pstrdup(pool, struniqchr(*path, '/'));
367
368 apr_pool_destroy(local_pool);
369 }
370
371 local char *domaintree_cache_get(MOD_DT_CNF *DT, apr_time_t atime, const char *host)
372 {
373 char *path = NULL;
374 struct dircache_entry *cache_entry;
375
376 apr_global_mutex_lock(DT->dircache.lock);
377
378 if ((cache_entry = apr_hash_get(DT->dircache.hmap, host, APR_HASH_KEY_STRING))) {
379 cache_entry->lacc = atime;
380 path = cache_entry->path;
381 }
382
383 apr_global_mutex_unlock(DT->dircache.lock);
384
385 if (path) {
386 DT_LOG_DBG
387 "DomainTree: cache hit = %s for %s", path, host
388 DT_LOG_END
389 }
390
391 return path;
392 }
393
394 local void domaintree_cache_set(MOD_DT_CNF *DT, apr_time_t atime, const char *host, const char *path)
395 {
396 apr_pool_t *pool;
397 struct dircache_entry *cache_entry;
398
399 apr_pool_create(&pool, DT->dircache.pool);
400 cache_entry = apr_palloc(pool, sizeof(struct dircache_entry));
401
402 cache_entry->pool = pool;
403 cache_entry->lacc = atime;
404 cache_entry->host = apr_pstrdup(pool, host);
405 cache_entry->path = apr_pstrdup(pool, path);
406
407 apr_global_mutex_lock(DT->dircache.lock);
408
409 if (apr_hash_count(DT->dircache.hmap) >= DT->dircache.clim) {
410 apr_hash_index_t *idx;
411 struct dircache_entry *purge_this = NULL;
412
413 DT_LOG_WRN
414 "DomainTree: reached cache limit (%ld)", DT->dircache.clim
415 DT_LOG_END
416
417 for (idx = apr_hash_first(DT->dircache.pool, DT->dircache.hmap); idx; idx = apr_hash_next(idx)) {
418 struct dircache_entry *current;
419
420 apr_hash_this(idx, NULL, NULL, (void **) &current);
421 if ((!purge_this) || (purge_this->lacc > current->lacc)) {
422 purge_this = current;
423 }
424 }
425
426 if (purge_this) {
427 DT_LOG_DBG
428 "DomainTree: cache del = %s", purge_this->host
429 DT_LOG_END
430 apr_hash_set(DT->dircache.hmap, purge_this->host, APR_HASH_KEY_STRING, NULL);
431 apr_pool_destroy(purge_this->pool);
432 }
433 }
434 apr_hash_set(DT->dircache.hmap, cache_entry->host, APR_HASH_KEY_STRING, cache_entry);
435
436 apr_global_mutex_unlock(DT->dircache.lock);
437
438 DT_LOG_DBG
439 "DomainTree: cache set = %s for %s", path, host
440 DT_LOG_END
441 }
442
443 /* }}} */
444 /* {{{ Hooks */
445
446 static STATUS domaintree_hook_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
447 {
448 ap_add_version_component(pconf, "MDT/" VERSION);
449 return OK;
450 }
451
452 static STATUS domaintree_hook_translate_name(request_rec *r)
453 {
454 MOD_DT_CNF *DT = NULL;
455 char *host, *path, *docroot;
456
457 if ((!(DT = GET_MOD_DT_CNF(r->server))) || (!DT->enabled)) {
458 return DECLINED;
459 }
460
461 DT_LOG_DBG
462 "DomainTree: processid = %d", (int) getpid()
463 DT_LOG_END
464
465 /* get a usable host name */
466 if (!(host = domaintree_host(r->pool, DT, ap_get_server_name(r)))) {
467 return DECLINED;
468 }
469
470 /* check cache */
471 if ((!DT->dircache.clim) || (!(path = domaintree_cache_get(DT, r->request_time, host)))) {
472 /* build path */
473 if (!(path = domaintree_path(r->pool, DT, host))) {
474 return DECLINED;
475 }
476
477 /* apply any aliases */
478 if (apr_table_elts(DT->aliases.faketable)->nelts) {
479 domaintree_fake(r->pool, DT, &path);
480 }
481
482 /* add to cache */
483 if (DT->dircache.clim) {
484 domaintree_cache_set(DT, r->request_time, host, path);
485 }
486 }
487
488 /* compose virtual docroot */
489 docroot = struniqchr(apr_pstrcat(r->pool, DT->prefix, "/", path, "/", DT->suffix, "/", NULL), '/');
490
491 /* stat docroot */
492 if (DT->statroot) {
493 apr_finfo_t sb;
494
495 switch (apr_stat(&sb, docroot, APR_FINFO_MIN, r->pool))
496 {
497 case APR_SUCCESS:
498 case APR_INCOMPLETE:
499 DT_LOG_DBG
500 "DomainTree: stat path = %s (success)", docroot
501 DT_LOG_END
502 break;
503
504 default:
505 DT_LOG_DBG
506 "DomainTree: stat path = %s (failure)", docroot
507 DT_LOG_END
508 return DECLINED;
509 break;
510 }
511 }
512
513 /* set virtual docroot */
514 apr_table_set(r->subprocess_env, "VIRTUAL_DOCUMENT_ROOT", docroot);
515
516 /* done */
517 r->canonical_filename = "";
518 r->filename = apr_pstrcat(r->pool, docroot, EMPTY(r->uri) ? NULL : ('/' == *r->uri ? r->uri + 1 : r->uri), NULL);
519
520 DT_LOG_DBG
521 "DomainTree: path done = %s", r->filename
522 DT_LOG_END
523
524 return OK;
525 }
526
527 static void domaintree_hooks(apr_pool_t *pool)
528 {
529 static const char * const pre[] = {"mod_alias.c", "mod_userdir.c", NULL};
530
531 ap_hook_post_config(domaintree_hook_post_config, NULL, NULL, APR_HOOK_MIDDLE);
532 ap_hook_translate_name(domaintree_hook_translate_name, pre, NULL, APR_HOOK_FIRST);
533 }
534
535 /* }}} */
536 /* {{{ Configuration */
537
538 static void *domaintree_create_srv(apr_pool_t *p, server_rec *s)
539 {
540 MOD_DT_CNF *DT;
541
542 DT = (MOD_DT_CNF *) apr_palloc(p, sizeof(MOD_DT_CNF));
543
544 DT->server = s;
545 DT->enabled = 0;
546 DT->stripwww = 1;
547 DT->statroot = 0;
548 DT->maxdepth = 20;
549
550 DT->prefix = "/var/www";
551 DT->suffix = "public_html";
552
553 DT->aliases.recursion = 0;
554 DT->aliases.faketable = apr_table_make(p, 0);
555
556 DT->dircache.clim = 0;
557 DT->dircache.hmap = apr_hash_make(p);
558 apr_pool_create(&DT->dircache.pool, p);
559 apr_global_mutex_create(&DT->dircache.lock, __FILE__, APR_LOCK_DEFAULT, p);
560
561 return DT;
562 }
563
564 static const char *domaintree_enable(cmd_parms *cmd, void *conf, int flag)
565 {
566 GET_MOD_DT_CNF(cmd->server)->enabled = flag;
567 return NULL;
568 }
569
570 static const char *domaintree_stripwww(cmd_parms *cmd, void *conf, int flag)
571 {
572 GET_MOD_DT_CNF(cmd->server)->stripwww = flag;
573 return NULL;
574 }
575
576 static const char *domaintree_statroot(cmd_parms *cmd, void *conf, int flag)
577 {
578 GET_MOD_DT_CNF(cmd->server)->statroot = flag;
579 return NULL;
580 }
581
582 static const char *domaintree_prefix(cmd_parms *cmd, void *conf, const char *prefix)
583 {
584 GET_MOD_DT_CNF(cmd->server)->prefix = EMPTY(prefix) ? "/" : trim(apr_pstrdup(cmd->pool, prefix), strlen(prefix), '/', TRIM_RIGHT);
585 return NULL;
586 }
587
588 static const char *domaintree_suffix(cmd_parms *cmd, void *conf, const char *suffix)
589 {
590 GET_MOD_DT_CNF(cmd->server)->suffix = EMPTY(suffix) ? "" : trim(apr_pstrdup(cmd->pool, suffix), strlen(suffix), '/', TRIM_BOTH);
591 return NULL;
592 }
593
594 static const char *domaintree_maxdepth(cmd_parms *cmd, void *conf, const char *max_depth)
595 {
596 long depth;
597
598 if ((depth = atol(max_depth))) {
599 if (depth > 0L) {
600 GET_MOD_DT_CNF(cmd->server)->maxdepth = depth;
601 } else {
602 return "Maximum DomainTree depth cannot be negative.";
603 }
604 }
605
606 return NULL;
607 }
608
609 static const char *domaintree_aliasrecursion(cmd_parms *cmd, void *conf, const char *alias_recursion)
610 {
611 long recursion;
612
613 if ((recursion = atol(alias_recursion))) {
614 if (recursion > 0L) {
615 GET_MOD_DT_CNF(cmd->server)->aliases.recursion = recursion;
616 } else {
617 return "DomainTree alias recursion cannot be negative.";
618 }
619 }
620
621 return NULL;
622 }
623
624 static const char *domaintree_alias(cmd_parms *cmd, void *conf, const char *fake, const char *real)
625 {
626 char *f = strtr(apr_pstrdup(cmd->pool, fake), '.', '/'), *r = strtr(apr_pstrdup(cmd->pool, real), '.', '/');
627
628 apr_table_set(GET_MOD_DT_CNF(cmd->server)->aliases.faketable, trim(f, strlen(f), '/', TRIM_BOTH), trim(r, strlen(r), '/', TRIM_BOTH));
629
630 return NULL;
631 }
632
633 static const char *domaintree_cache(cmd_parms *cmd, void *conf, const char *cache)
634 {
635 long limit;
636
637 if ((limit = atol(cache))) {
638 if (limit > 0L) {
639 GET_MOD_DT_CNF(cmd->server)->dircache.clim = limit;
640 } else {
641 return "DomainTree cache limit cannot be negative.";
642 }
643 }
644
645 return NULL;
646 }
647
648 /* }}} */
649 /* {{{ Commands */
650
651 static command_rec domaintree_commands[] = {
652 AP_INIT_FLAG(
653 "DomainTreeEnabled", domaintree_enable, NULL, RSRC_CONF,
654 "Turn the module on or off."
655 ),
656
657 AP_INIT_FLAG(
658 "DomainTreeStripWWW", domaintree_stripwww, NULL, RSRC_CONF,
659 "Strip leading www from host. (default On)"
660 ),
661
662 AP_INIT_FLAG(
663 "DomainTreeStatRoot", domaintree_statroot, NULL, RSRC_CONF,
664 "Wheter to check for the evaluated virtual document root with a stat call. (default Off)"
665 ),
666
667 AP_INIT_TAKE1(
668 "DomainTreePrefix", domaintree_prefix, NULL, RSRC_CONF,
669 "DomainTree path prefix. (default /var/www) Do not forget the leading slash!"
670 ),
671
672 AP_INIT_TAKE1(
673 "DomainTreeSuffix", domaintree_suffix, NULL, RSRC_CONF,
674 "DomainTree path suffix. (default public_html)"
675 ),
676
677 AP_INIT_TAKE1(
678 "DomainTreeMaxdepth", domaintree_maxdepth, NULL, RSRC_CONF,
679 "DomainTree max path depth. (default 20)"
680 ),
681
682 AP_INIT_TAKE1(
683 "DomainTreeAliasRecursion", domaintree_aliasrecursion, NULL, RSRC_CONF,
684 "Whether (and how often at the maximum) DomainTree should walk recursively "
685 "through the aliases list as long as matching aliases are found. (default: 0 = turned off)"
686 ),
687
688 AP_INIT_TAKE2(
689 "DomainTreeAlias", domaintree_alias, NULL, RSRC_CONF,
690 "DomainTree aliases; e.g. DomainTreeAlias com/example/tickets com/example/support (dots or slashes equal)"
691 ),
692
693 AP_INIT_TAKE1(
694 "DomainTreeCache", domaintree_cache, NULL, RSRC_CONF,
695 "DomainTree server-wide host to directory cache; specify how many cache entries to allow (default: 0 = turned off)"
696 ),
697
698 { NULL }
699 };
700
701 /* }}} */
702 /* {{{ Module Administrativa */
703
704 module AP_MODULE_DECLARE_DATA domaintree_module = {
705 STANDARD20_MODULE_STUFF,
706 NULL, /* create per-dir */
707 NULL, /* merge per-dir */
708 domaintree_create_srv, /* create per-server */
709 NULL, /* merge per-server */
710 domaintree_commands, /* config commands */
711 domaintree_hooks /* hooks */
712 };
713
714 /* }}} */
715
716 /*
717 * Local variables:
718 * tab-width: 4
719 * c-basic-offset: 4
720 * End:
721 * vim600: noet sw=4 ts=4 fdm=marker
722 * vim<600: noet sw=4 ts=4
723 */