]> granicus.if.org Git - apache/blob - server/config.c
non-static directive parsers should be AP_DECLARE_NONSTD()
[apache] / server / config.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * http_config.c: once was auxillary functions for reading httpd's config
19  * file and converting filenames into a namespace
20  *
21  * Rob McCool
22  *
23  * Wall-to-wall rewrite for Apache... commands which are part of the
24  * server core can now be found next door in "http_core.c".  Now contains
25  * general command loop, and functions which do bookkeeping for the new
26  * Apache config stuff (modules and configuration vectors).
27  *
28  * rst
29  *
30  */
31
32 #include "apr.h"
33 #include "apr_strings.h"
34 #include "apr_portable.h"
35 #include "apr_file_io.h"
36 #include "apr_fnmatch.h"
37
38 #define APR_WANT_STDIO
39 #define APR_WANT_STRFUNC
40 #include "apr_want.h"
41
42 #include "ap_config.h"
43 #include "httpd.h"
44 #include "http_config.h"
45 #include "http_protocol.h"
46 #include "http_core.h"
47 #include "http_log.h"      /* for errors in parse_htaccess */
48 #include "http_request.h"  /* for default_handler (see invoke_handler) */
49 #include "http_main.h"
50 #include "http_vhost.h"
51 #include "util_cfgtree.h"
52
53 AP_DECLARE_DATA const char *ap_server_argv0 = NULL;
54 AP_DECLARE_DATA const char *ap_server_root = NULL;
55 AP_DECLARE_DATA server_rec *ap_server_conf = NULL;
56 AP_DECLARE_DATA apr_pool_t *ap_pglobal = NULL;
57
58 AP_DECLARE_DATA apr_array_header_t *ap_server_pre_read_config = NULL;
59 AP_DECLARE_DATA apr_array_header_t *ap_server_post_read_config = NULL;
60 AP_DECLARE_DATA apr_array_header_t *ap_server_config_defines = NULL;
61
62 AP_DECLARE_DATA ap_directive_t *ap_conftree = NULL;
63
64 APR_HOOK_STRUCT(
65            APR_HOOK_LINK(header_parser)
66            APR_HOOK_LINK(pre_config)
67            APR_HOOK_LINK(check_config)
68            APR_HOOK_LINK(post_config)
69            APR_HOOK_LINK(open_logs)
70            APR_HOOK_LINK(child_init)
71            APR_HOOK_LINK(handler)
72            APR_HOOK_LINK(quick_handler)
73            APR_HOOK_LINK(optional_fn_retrieve)
74            APR_HOOK_LINK(test_config)
75 )
76
77 AP_IMPLEMENT_HOOK_RUN_ALL(int, header_parser,
78                           (request_rec *r), (r), OK, DECLINED)
79
80 AP_IMPLEMENT_HOOK_RUN_ALL(int, pre_config,
81                           (apr_pool_t *pconf, apr_pool_t *plog,
82                            apr_pool_t *ptemp),
83                           (pconf, plog, ptemp), OK, DECLINED)
84
85 AP_IMPLEMENT_HOOK_RUN_ALL(int, check_config,
86                           (apr_pool_t *pconf, apr_pool_t *plog,
87                            apr_pool_t *ptemp, server_rec *s),
88                           (pconf, plog, ptemp, s), OK, DECLINED)
89
90 AP_IMPLEMENT_HOOK_VOID(test_config,
91                        (apr_pool_t *pconf, server_rec *s),
92                        (pconf, s))
93
94 AP_IMPLEMENT_HOOK_RUN_ALL(int, post_config,
95                           (apr_pool_t *pconf, apr_pool_t *plog,
96                            apr_pool_t *ptemp, server_rec *s),
97                           (pconf, plog, ptemp, s), OK, DECLINED)
98
99 /* During the course of debugging I expanded this macro out, so
100  * rather than remove all the useful information there is in the
101  * following lines, I'm going to leave it here in case anyone
102  * else finds it useful.
103  *
104  * Ben has looked at it and thinks it correct :)
105  *
106 AP_DECLARE(int) ap_hook_post_config(ap_HOOK_post_config_t *pf,
107                                     const char * const *aszPre,
108                                     const char * const *aszSucc,
109                                     int nOrder)
110 {
111     ap_LINK_post_config_t *pHook;
112
113     if (!_hooks.link_post_config) {
114         _hooks.link_post_config = apr_array_make(apr_hook_global_pool, 1,
115                                                  sizeof(ap_LINK_post_config_t));
116         apr_hook_sort_register("post_config", &_hooks.link_post_config);
117     }
118
119     pHook = apr_array_push(_hooks.link_post_config);
120     pHook->pFunc = pf;
121     pHook->aszPredecessors = aszPre;
122     pHook->aszSuccessors = aszSucc;
123     pHook->nOrder = nOrder;
124     pHook->szName = apr_hook_debug_current;
125
126     if (apr_hook_debug_enabled)
127         apr_hook_debug_show("post_config", aszPre, aszSucc);
128 }
129
130 AP_DECLARE(apr_array_header_t *) ap_hook_get_post_config(void) {
131     return _hooks.link_post_config;
132 }
133
134 AP_DECLARE(int) ap_run_post_config(apr_pool_t *pconf,
135                                    apr_pool_t *plog,
136                                    apr_pool_t *ptemp,
137                                    server_rec *s)
138 {
139     ap_LINK_post_config_t *pHook;
140     int n;
141
142     if(!_hooks.link_post_config)
143         return;
144
145     pHook = (ap_LINK_post_config_t *)_hooks.link_post_config->elts;
146     for (n = 0; n < _hooks.link_post_config->nelts; ++n)
147         pHook[n].pFunc (pconf, plog, ptemp, s);
148 }
149  */
150
151 AP_IMPLEMENT_HOOK_RUN_ALL(int, open_logs,
152                           (apr_pool_t *pconf, apr_pool_t *plog,
153                            apr_pool_t *ptemp, server_rec *s),
154                           (pconf, plog, ptemp, s), OK, DECLINED)
155
156 AP_IMPLEMENT_HOOK_VOID(child_init,
157                        (apr_pool_t *pchild, server_rec *s),
158                        (pchild, s))
159
160 AP_IMPLEMENT_HOOK_RUN_FIRST(int, handler, (request_rec *r),
161                             (r), DECLINED)
162
163 AP_IMPLEMENT_HOOK_RUN_FIRST(int, quick_handler, (request_rec *r, int lookup),
164                             (r, lookup), DECLINED)
165
166 AP_IMPLEMENT_HOOK_VOID(optional_fn_retrieve, (void), ())
167
168 /****************************************************************
169  *
170  * We begin with the functions which deal with the linked list
171  * of modules which control just about all of the server operation.
172  */
173
174 /* total_modules is the number of modules that have been linked
175  * into the server.
176  */
177 static int total_modules = 0;
178
179 /* dynamic_modules is the number of modules that have been added
180  * after the pre-loaded ones have been set up. It shouldn't be larger
181  * than DYNAMIC_MODULE_LIMIT.
182  */
183 static int dynamic_modules = 0;
184
185 AP_DECLARE_DATA module *ap_top_module = NULL;
186 AP_DECLARE_DATA module **ap_loaded_modules=NULL;
187
188 static apr_hash_t *ap_config_hash = NULL;
189
190 typedef int (*handler_func)(request_rec *);
191 typedef void *(*dir_maker_func)(apr_pool_t *, char *);
192 typedef void *(*merger_func)(apr_pool_t *, void *, void *);
193
194 /* maximum nesting level for config directories */
195 #ifndef AP_MAX_INCLUDE_DIR_DEPTH
196 #define AP_MAX_INCLUDE_DIR_DEPTH (128)
197 #endif
198
199 /* Dealing with config vectors.  These are associated with per-directory,
200  * per-server, and per-request configuration, and have a void* pointer for
201  * each modules.  The nature of the structure pointed to is private to the
202  * module in question... the core doesn't (and can't) know.  However, there
203  * are defined interfaces which allow it to create instances of its private
204  * per-directory and per-server structures, and to merge the per-directory
205  * structures of a directory and its subdirectory (producing a new one in
206  * which the defaults applying to the base directory have been properly
207  * overridden).
208  */
209
210 static ap_conf_vector_t *create_empty_config(apr_pool_t *p)
211 {
212     void *conf_vector = apr_pcalloc(p, sizeof(void *) *
213                                     (total_modules + DYNAMIC_MODULE_LIMIT));
214     return conf_vector;
215 }
216
217 static ap_conf_vector_t *create_default_per_dir_config(apr_pool_t *p)
218 {
219     void **conf_vector = apr_pcalloc(p, sizeof(void *) *
220                                      (total_modules + DYNAMIC_MODULE_LIMIT));
221     module *modp;
222
223     for (modp = ap_top_module; modp; modp = modp->next) {
224         dir_maker_func df = modp->create_dir_config;
225
226         if (df)
227             conf_vector[modp->module_index] = (*df)(p, NULL);
228     }
229
230     return (ap_conf_vector_t *)conf_vector;
231 }
232
233 AP_CORE_DECLARE(ap_conf_vector_t *) ap_merge_per_dir_configs(apr_pool_t *p,
234                                            ap_conf_vector_t *base,
235                                            ap_conf_vector_t *new_conf)
236 {
237     void **conf_vector = apr_palloc(p, sizeof(void *) * total_modules);
238     void **base_vector = (void **)base;
239     void **new_vector = (void **)new_conf;
240     module *modp;
241
242     for (modp = ap_top_module; modp; modp = modp->next) {
243         int i = modp->module_index;
244
245         if (!new_vector[i]) {
246             conf_vector[i] = base_vector[i];
247         }
248         else {
249             merger_func df = modp->merge_dir_config;
250             if (df && base_vector[i]) {
251                 conf_vector[i] = (*df)(p, base_vector[i], new_vector[i]);
252             }
253             else
254                 conf_vector[i] = new_vector[i];
255         }
256     }
257
258     return (ap_conf_vector_t *)conf_vector;
259 }
260
261 static ap_conf_vector_t *create_server_config(apr_pool_t *p, server_rec *s)
262 {
263     void **conf_vector = apr_pcalloc(p, sizeof(void *) *
264                                      (total_modules + DYNAMIC_MODULE_LIMIT));
265     module *modp;
266
267     for (modp = ap_top_module; modp; modp = modp->next) {
268         if (modp->create_server_config)
269             conf_vector[modp->module_index] = (*modp->create_server_config)(p, s);
270     }
271
272     return (ap_conf_vector_t *)conf_vector;
273 }
274
275 static void merge_server_configs(apr_pool_t *p, ap_conf_vector_t *base,
276                                  ap_conf_vector_t *virt)
277 {
278     /* Can reuse the 'virt' vector for the spine of it, since we don't
279      * have to deal with the moral equivalent of .htaccess files here...
280      */
281
282     void **base_vector = (void **)base;
283     void **virt_vector = (void **)virt;
284     module *modp;
285
286     for (modp = ap_top_module; modp; modp = modp->next) {
287         merger_func df = modp->merge_server_config;
288         int i = modp->module_index;
289
290         if (!virt_vector[i])
291             virt_vector[i] = base_vector[i];
292         else if (df)
293             virt_vector[i] = (*df)(p, base_vector[i], virt_vector[i]);
294     }
295 }
296
297 AP_CORE_DECLARE(ap_conf_vector_t *) ap_create_request_config(apr_pool_t *p)
298 {
299     return create_empty_config(p);
300 }
301
302 AP_CORE_DECLARE(ap_conf_vector_t *) ap_create_conn_config(apr_pool_t *p)
303 {
304     return create_empty_config(p);
305 }
306
307 AP_CORE_DECLARE(ap_conf_vector_t *) ap_create_per_dir_config(apr_pool_t *p)
308 {
309     return create_empty_config(p);
310 }
311
312 static int ap_invoke_filter_init(ap_filter_t *filters)
313 {
314     while (filters) {
315         if (filters->frec->filter_init_func) {
316             int result = filters->frec->filter_init_func(filters);
317             if (result != OK) {
318                 return result;
319             }
320         }
321         filters = filters->next;
322     }
323     return OK;
324 }
325
326 /*
327  * TODO: Move this to an appropriate include file and possibly prefix it
328  * with AP_.
329  */
330 #define DEFAULT_HANDLER_NAME ""
331
332 AP_CORE_DECLARE(int) ap_invoke_handler(request_rec *r)
333 {
334     const char *handler;
335     const char *p;
336     int result;
337     const char *old_handler = r->handler;
338     const char *ignore;
339
340     /*
341      * The new insert_filter stage makes the most sense here.  We only use
342      * it when we are going to run the request, so we must insert filters
343      * if any are available.  Since the goal of this phase is to allow all
344      * modules to insert a filter if they want to, this filter returns
345      * void.  I just can't see any way that this filter can reasonably
346      * fail, either your modules inserts something or it doesn't.  rbb
347      */
348     ap_run_insert_filter(r);
349
350     /* Before continuing, allow each filter that is in the two chains to
351      * run their init function to let them do any magic before we could
352      * start generating data.
353      */
354     result = ap_invoke_filter_init(r->input_filters);
355     if (result != OK) {
356         return result;
357     }
358     result = ap_invoke_filter_init(r->output_filters);
359     if (result != OK) {
360         return result;
361     }
362
363     if (!r->handler) {
364         if (r->content_type) {
365             handler = r->content_type;
366             if ((p=ap_strchr_c(handler, ';')) != NULL) {
367                 char *new_handler = (char *)apr_pmemdup(r->pool, handler,
368                                                         p - handler + 1);
369                 char *p2 = new_handler + (p - handler);
370                 handler = new_handler;
371
372                 /* exclude media type arguments */
373                 while (p2 > handler && p2[-1] == ' ')
374                     --p2; /* strip trailing spaces */
375
376                 *p2='\0';
377             }
378         }
379         else {
380             handler = DEFAULT_HANDLER_NAME;
381         }
382
383         r->handler = handler;
384     }
385
386     result = ap_run_handler(r);
387
388     r->handler = old_handler;
389
390     if (result == DECLINED && r->handler && r->filename) {
391         ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
392             "handler \"%s\" not found for: %s", r->handler, r->filename);
393     }
394     if ((result != OK) && (result != DONE) && (result != DECLINED) && (result != SUSPENDED)
395         && (result != AP_FILTER_ERROR) /* ap_die() knows about this specifically */
396         && !ap_is_HTTP_VALID_RESPONSE(result)) {
397         /* If a module is deliberately returning something else
398          * (request_rec in non-HTTP or proprietary extension?)
399          * let it set a note to allow it explicitly.
400          * Otherwise, a return code that is neither reserved nor HTTP
401          * is a bug, as in PR#31759.
402          */
403         ignore = apr_table_get(r->notes, "HTTP_IGNORE_RANGE");
404         if (!ignore) {
405             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
406                           "Handler for %s returned invalid result code %d",
407                           r->handler, result);
408             result = HTTP_INTERNAL_SERVER_ERROR;
409         }
410     }
411
412     return result == DECLINED ? HTTP_INTERNAL_SERVER_ERROR : result;
413 }
414
415 AP_DECLARE(int) ap_method_is_limited(cmd_parms *cmd, const char *method)
416 {
417     int methnum;
418
419     methnum = ap_method_number_of(method);
420
421     /*
422      * A method number either hardcoded into apache or
423      * added by a module and registered.
424      */
425     if (methnum != M_INVALID) {
426         return (cmd->limited & (AP_METHOD_BIT << methnum)) ? 1 : 0;
427     }
428
429     return 0; /* not found */
430 }
431
432 AP_DECLARE(void) ap_register_hooks(module *m, apr_pool_t *p)
433 {
434     if (m->register_hooks) {
435         if (getenv("SHOW_HOOKS")) {
436             printf("Registering hooks for %s\n", m->name);
437             apr_hook_debug_enabled = 1;
438         }
439
440         apr_hook_debug_current = m->name;
441         m->register_hooks(p);
442     }
443 }
444
445 static void ap_add_module_commands(module *m, apr_pool_t *p);
446
447 typedef struct ap_mod_list_struct ap_mod_list;
448 struct ap_mod_list_struct {
449     struct ap_mod_list_struct *next;
450     module *m;
451     const command_rec *cmd;
452 };
453
454 static apr_status_t reload_conf_hash(void *baton)
455 {
456     ap_config_hash = NULL;
457     return APR_SUCCESS;
458 }
459
460 static void rebuild_conf_hash(apr_pool_t *p, int add_prelinked)
461 {
462     module **m;
463
464     ap_config_hash = apr_hash_make(p);
465
466     apr_pool_cleanup_register(p, NULL, reload_conf_hash,
467                               apr_pool_cleanup_null);
468     if (add_prelinked) {
469         for (m = ap_prelinked_modules; *m != NULL; m++) {
470             ap_add_module_commands(*m, p);
471         }
472     }
473 }
474
475 static void ap_add_module_commands(module *m, apr_pool_t *p)
476 {
477     apr_pool_t *tpool;
478     ap_mod_list *mln;
479     const command_rec *cmd;
480     char *dir;
481
482     cmd = m->cmds;
483
484     if (ap_config_hash == NULL) {
485         rebuild_conf_hash(p, 0);
486     }
487
488     tpool = apr_hash_pool_get(ap_config_hash);
489
490     while (cmd && cmd->name) {
491         mln = apr_palloc(tpool, sizeof(ap_mod_list));
492         mln->cmd = cmd;
493         mln->m = m;
494         dir = apr_pstrdup(tpool, cmd->name);
495
496         ap_str_tolower(dir);
497
498         mln->next = apr_hash_get(ap_config_hash, dir, APR_HASH_KEY_STRING);
499         apr_hash_set(ap_config_hash, dir, APR_HASH_KEY_STRING, mln);
500         ++cmd;
501     }
502 }
503
504
505 /* One-time setup for precompiled modules --- NOT to be done on restart */
506
507 AP_DECLARE(const char *) ap_add_module(module *m, apr_pool_t *p)
508 {
509     /* This could be called from a LoadModule httpd.conf command,
510      * after the file has been linked and the module structure within it
511      * teased out...
512      */
513
514     if (m->version != MODULE_MAGIC_NUMBER_MAJOR) {
515         return apr_psprintf(p, "Module \"%s\" is not compatible with this "
516                             "version of Apache (found %d, need %d). Please "
517                             "contact the vendor for the correct version.",
518                             m->name, m->version, MODULE_MAGIC_NUMBER_MAJOR);
519     }
520
521     if (m->next == NULL) {
522         m->next = ap_top_module;
523         ap_top_module = m;
524     }
525
526     if (m->module_index == -1) {
527         m->module_index = total_modules++;
528         dynamic_modules++;
529
530         if (dynamic_modules > DYNAMIC_MODULE_LIMIT) {
531             return apr_psprintf(p, "Module \"%s\" could not be loaded, "
532                                 "because the dynamic module limit was "
533                                 "reached. Please increase "
534                                 "DYNAMIC_MODULE_LIMIT and recompile.", m->name);
535         }
536     }
537
538     /* Some C compilers put a complete path into __FILE__, but we want
539      * only the filename (e.g. mod_includes.c). So check for path
540      * components (Unix and DOS), and remove them.
541      */
542
543     if (ap_strrchr_c(m->name, '/'))
544         m->name = 1 + ap_strrchr_c(m->name, '/');
545
546     if (ap_strrchr_c(m->name, '\\'))
547         m->name = 1 + ap_strrchr_c(m->name, '\\');
548
549 #ifdef _OSD_POSIX
550     /* __FILE__ =
551      * "*POSIX(/home/martin/apache/src/modules/standard/mod_info.c)"
552      */
553
554     /* We cannot fix the string in-place, because it's const */
555     if (m->name[strlen(m->name)-1] == ')') {
556         char *tmp = strdup(m->name); /* FIXME: memory leak, albeit a small one */
557         tmp[strlen(tmp)-1] = '\0';
558         m->name = tmp;
559     }
560 #endif /*_OSD_POSIX*/
561
562     ap_add_module_commands(m, p);
563     /*  FIXME: is this the right place to call this?
564      *  It doesn't appear to be
565      */
566     ap_register_hooks(m, p);
567
568     return NULL;
569 }
570
571 /*
572  * remove_module undoes what add_module did. There are some caveats:
573  * when the module is removed, its slot is lost so all the current
574  * per-dir and per-server configurations are invalid. So we should
575  * only ever call this function when you are invalidating almost
576  * all our current data. I.e. when doing a restart.
577  */
578
579 AP_DECLARE(void) ap_remove_module(module *m)
580 {
581     module *modp;
582
583     modp = ap_top_module;
584     if (modp == m) {
585         /* We are the top module, special case */
586         ap_top_module = modp->next;
587         m->next = NULL;
588     }
589     else {
590         /* Not the top module, find use. When found modp will
591          * point to the module _before_ us in the list
592          */
593
594         while (modp && modp->next != m) {
595             modp = modp->next;
596         }
597
598         if (!modp) {
599             /* Uh-oh, this module doesn't exist */
600             ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
601                          "Cannot remove module %s: not found in module list",
602                          m->name);
603             return;
604         }
605
606         /* Eliminate us from the module list */
607         modp->next = modp->next->next;
608     }
609
610     m->module_index = -1; /* simulate being unloaded, should
611                            * be unnecessary */
612     dynamic_modules--;
613     total_modules--;
614 }
615
616 AP_DECLARE(const char *) ap_add_loaded_module(module *mod, apr_pool_t *p)
617 {
618     module **m;
619     const char *error;
620
621     /*
622      *  Add module pointer to top of chained module list
623      */
624     error = ap_add_module(mod, p);
625     if (error) {
626         return error;
627     }
628
629     /*
630      *  And module pointer to list of loaded modules
631      *
632      *  Notes: 1. ap_add_module() would already complain if no more space
633      *            exists for adding a dynamically loaded module
634      *         2. ap_add_module() accepts double inclusion, so we have
635      *            to accept this, too.
636      */
637     for (m = ap_loaded_modules; *m != NULL; m++)
638         ;
639     *m++ = mod;
640     *m = NULL;
641
642     return NULL;
643 }
644
645 AP_DECLARE(void) ap_remove_loaded_module(module *mod)
646 {
647     module **m;
648     module **m2;
649     int done;
650
651     /*
652      *  Remove module pointer from chained module list
653      */
654     ap_remove_module(mod);
655
656     /*
657      *  Remove module pointer from list of loaded modules
658      *
659      *  Note: 1. We cannot determine if the module was successfully
660      *           removed by ap_remove_module().
661      *        2. We have not to complain explicity when the module
662      *           is not found because ap_remove_module() did it
663      *           for us already.
664      */
665     for (m = m2 = ap_loaded_modules, done = 0; *m2 != NULL; m2++) {
666         if (*m2 == mod && done == 0)
667             done = 1;
668         else
669             *m++ = *m2;
670     }
671
672     *m = NULL;
673 }
674
675 AP_DECLARE(const char *) ap_setup_prelinked_modules(process_rec *process)
676 {
677     module **m;
678     module **m2;
679     const char *error;
680
681     apr_hook_global_pool=process->pconf;
682
683     rebuild_conf_hash(process->pconf, 0);
684
685     /*
686      *  Initialise total_modules variable and module indices
687      */
688     total_modules = 0;
689     for (m = ap_preloaded_modules; *m != NULL; m++)
690         (*m)->module_index = total_modules++;
691
692     /*
693      *  Initialise list of loaded modules
694      */
695     ap_loaded_modules = (module **)apr_palloc(process->pool,
696         sizeof(module *) * (total_modules + DYNAMIC_MODULE_LIMIT + 1));
697
698     if (ap_loaded_modules == NULL) {
699         return "Ouch! Out of memory in ap_setup_prelinked_modules()!";
700     }
701
702     for (m = ap_preloaded_modules, m2 = ap_loaded_modules; *m != NULL; )
703         *m2++ = *m++;
704
705     *m2 = NULL;
706
707     /*
708      *   Initialize chain of linked (=activate) modules
709      */
710     for (m = ap_prelinked_modules; *m != NULL; m++) {
711         error = ap_add_module(*m, process->pconf);
712         if (error) {
713             return error;
714         }
715     }
716
717     apr_hook_sort_all();
718
719     return NULL;
720 }
721
722 AP_DECLARE(const char *) ap_find_module_name(module *m)
723 {
724     return m->name;
725 }
726
727 AP_DECLARE(module *) ap_find_linked_module(const char *name)
728 {
729     module *modp;
730
731     for (modp = ap_top_module; modp; modp = modp->next) {
732         if (strcmp(modp->name, name) == 0)
733             return modp;
734     }
735
736     return NULL;
737 }
738
739 /*****************************************************************
740  *
741  * Resource, access, and .htaccess config files now parsed by a common
742  * command loop.
743  *
744  * Let's begin with the basics; parsing the line and
745  * invoking the function...
746  */
747
748 #define AP_MAX_ARGC 64
749
750 static const char *invoke_cmd(const command_rec *cmd, cmd_parms *parms,
751                               void *mconfig, const char *args)
752 {
753     char *w, *w2, *w3;
754     const char *errmsg = NULL;
755
756     if ((parms->override & cmd->req_override) == 0)
757         return apr_pstrcat(parms->pool, cmd->name, " not allowed here", NULL);
758
759     parms->info = cmd->cmd_data;
760     parms->cmd = cmd;
761
762     switch (cmd->args_how) {
763     case RAW_ARGS:
764 #ifdef RESOLVE_ENV_PER_TOKEN
765         args = ap_resolve_env(parms->pool,args);
766 #endif
767         return cmd->AP_RAW_ARGS(parms, mconfig, args);
768
769     case TAKE_ARGV:
770         {
771             char *argv[AP_MAX_ARGC];
772             int argc = 0;
773
774             do {
775                 w = ap_getword_conf(parms->pool, &args);
776                 if (*w == '\0' && *args == '\0') {
777                     break;
778                 }
779                 argv[argc] = w;
780                 argc++;
781             } while (argc < AP_MAX_ARGC && *args != '\0');
782
783             return cmd->AP_TAKE_ARGV(parms, mconfig, argc, argv);
784         }
785
786     case NO_ARGS:
787         if (*args != 0)
788             return apr_pstrcat(parms->pool, cmd->name, " takes no arguments",
789                                NULL);
790
791         return cmd->AP_NO_ARGS(parms, mconfig);
792
793     case TAKE1:
794         w = ap_getword_conf(parms->pool, &args);
795
796         if (*w == '\0' || *args != 0)
797             return apr_pstrcat(parms->pool, cmd->name, " takes one argument",
798                                cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
799
800         return cmd->AP_TAKE1(parms, mconfig, w);
801
802     case TAKE2:
803         w = ap_getword_conf(parms->pool, &args);
804         w2 = ap_getword_conf(parms->pool, &args);
805
806         if (*w == '\0' || *w2 == '\0' || *args != 0)
807             return apr_pstrcat(parms->pool, cmd->name, " takes two arguments",
808                                cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
809
810         return cmd->AP_TAKE2(parms, mconfig, w, w2);
811
812     case TAKE12:
813         w = ap_getword_conf(parms->pool, &args);
814         w2 = ap_getword_conf(parms->pool, &args);
815
816         if (*w == '\0' || *args != 0)
817             return apr_pstrcat(parms->pool, cmd->name, " takes 1-2 arguments",
818                                cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
819
820         return cmd->AP_TAKE2(parms, mconfig, w, *w2 ? w2 : NULL);
821
822     case TAKE3:
823         w = ap_getword_conf(parms->pool, &args);
824         w2 = ap_getword_conf(parms->pool, &args);
825         w3 = ap_getword_conf(parms->pool, &args);
826
827         if (*w == '\0' || *w2 == '\0' || *w3 == '\0' || *args != 0)
828             return apr_pstrcat(parms->pool, cmd->name, " takes three arguments",
829                                cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
830
831         return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
832
833     case TAKE23:
834         w = ap_getword_conf(parms->pool, &args);
835         w2 = ap_getword_conf(parms->pool, &args);
836         w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
837
838         if (*w == '\0' || *w2 == '\0' || *args != 0)
839             return apr_pstrcat(parms->pool, cmd->name,
840                                " takes two or three arguments",
841                                cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
842
843         return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
844
845     case TAKE123:
846         w = ap_getword_conf(parms->pool, &args);
847         w2 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
848         w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
849
850         if (*w == '\0' || *args != 0)
851             return apr_pstrcat(parms->pool, cmd->name,
852                                " takes one, two or three arguments",
853                                cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
854
855         return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
856
857     case TAKE13:
858         w = ap_getword_conf(parms->pool, &args);
859         w2 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
860         w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
861
862         if (*w == '\0' || (w2 && *w2 && !w3) || *args != 0)
863             return apr_pstrcat(parms->pool, cmd->name,
864                                " takes one or three arguments",
865                                cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
866
867         return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
868
869     case ITERATE:
870         while (*(w = ap_getword_conf(parms->pool, &args)) != '\0') {
871
872             errmsg = cmd->AP_TAKE1(parms, mconfig, w);
873
874             if (errmsg && strcmp(errmsg, DECLINE_CMD) != 0)
875                 return errmsg;
876         }
877
878         return errmsg;
879
880     case ITERATE2:
881         w = ap_getword_conf(parms->pool, &args);
882
883         if (*w == '\0' || *args == 0)
884             return apr_pstrcat(parms->pool, cmd->name,
885                                " requires at least two arguments",
886                                cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
887
888         while (*(w2 = ap_getword_conf(parms->pool, &args)) != '\0') {
889
890             errmsg = cmd->AP_TAKE2(parms, mconfig, w, w2);
891
892             if (errmsg && strcmp(errmsg, DECLINE_CMD) != 0)
893                 return errmsg;
894         }
895
896         return errmsg;
897
898     case FLAG:
899         w = ap_getword_conf(parms->pool, &args);
900
901         if (*w == '\0' || (strcasecmp(w, "on") && strcasecmp(w, "off")))
902             return apr_pstrcat(parms->pool, cmd->name, " must be On or Off",
903                                NULL);
904
905         return cmd->AP_FLAG(parms, mconfig, strcasecmp(w, "off") != 0);
906
907     default:
908         return apr_pstrcat(parms->pool, cmd->name,
909                            " is improperly configured internally (server bug)",
910                            NULL);
911     }
912 }
913
914 AP_CORE_DECLARE(const command_rec *) ap_find_command(const char *name,
915                                                      const command_rec *cmds)
916 {
917     while (cmds->name) {
918         if (!strcasecmp(name, cmds->name))
919             return cmds;
920
921         ++cmds;
922     }
923
924     return NULL;
925 }
926
927 AP_CORE_DECLARE(const command_rec *) ap_find_command_in_modules(
928                                           const char *cmd_name, module **mod)
929 {
930     const command_rec *cmdp;
931     module *modp;
932
933     for (modp = *mod; modp; modp = modp->next) {
934         if (modp->cmds && (cmdp = ap_find_command(cmd_name, modp->cmds))) {
935             *mod = modp;
936             return cmdp;
937         }
938     }
939
940     return NULL;
941 }
942
943 AP_CORE_DECLARE(void *) ap_set_config_vectors(server_rec *server,
944                                               ap_conf_vector_t *section_vector,
945                                               const char *section,
946                                               module *mod, apr_pool_t *pconf)
947 {
948     void *section_config = ap_get_module_config(section_vector, mod);
949     void *server_config = ap_get_module_config(server->module_config, mod);
950
951     if (!section_config && mod->create_dir_config) {
952         /* ### need to fix the create_dir_config functions' prototype... */
953         section_config = (*mod->create_dir_config)(pconf, (char *)section);
954         ap_set_module_config(section_vector, mod, section_config);
955     }
956
957     if (!server_config && mod->create_server_config) {
958         server_config = (*mod->create_server_config)(pconf, server);
959         ap_set_module_config(server->module_config, mod, server_config);
960     }
961
962     return section_config;
963 }
964
965 static const char *execute_now(char *cmd_line, const char *args,
966                                cmd_parms *parms,
967                                apr_pool_t *p, apr_pool_t *ptemp,
968                                ap_directive_t **sub_tree,
969                                ap_directive_t *parent);
970
971 static const char *ap_build_config_sub(apr_pool_t *p, apr_pool_t *temp_pool,
972                                        const char *l, cmd_parms *parms,
973                                        ap_directive_t **current,
974                                        ap_directive_t **curr_parent,
975                                        ap_directive_t **conftree)
976 {
977     const char *retval = NULL;
978     const char *args;
979     char *cmd_name;
980     ap_directive_t *newdir;
981     module *mod = ap_top_module;
982     const command_rec *cmd;
983
984     if (*l == '#' || *l == '\0')
985         return NULL;
986
987 #if RESOLVE_ENV_PER_TOKEN
988     args = l;
989 #else
990     args = ap_resolve_env(temp_pool, l);
991 #endif
992
993     cmd_name = ap_getword_conf(p, &args);
994     if (*cmd_name == '\0') {
995         /* Note: this branch should not occur. An empty line should have
996          * triggered the exit further above.
997          */
998         return NULL;
999     }
1000
1001     if (cmd_name[1] != '/') {
1002         char *lastc = cmd_name + strlen(cmd_name) - 1;
1003         if (*lastc == '>') {
1004             *lastc = '\0' ;
1005         }
1006         if (cmd_name[0] == '<' && *args == '\0') {
1007             args = ">";
1008         }
1009     }
1010
1011     newdir = apr_pcalloc(p, sizeof(ap_directive_t));
1012     newdir->filename = parms->config_file->name;
1013     newdir->line_num = parms->config_file->line_number;
1014     newdir->directive = cmd_name;
1015     newdir->args = apr_pstrdup(p, args);
1016
1017     if ((cmd = ap_find_command_in_modules(cmd_name, &mod)) != NULL) {
1018         if (cmd->req_override & EXEC_ON_READ) {
1019             ap_directive_t *sub_tree = NULL;
1020
1021             parms->err_directive = newdir;
1022             retval = execute_now(cmd_name, args, parms, p, temp_pool,
1023                                  &sub_tree, *curr_parent);
1024             if (*current) {
1025                 (*current)->next = sub_tree;
1026             }
1027             else {
1028                 *current = sub_tree;
1029                 if (*curr_parent) {
1030                     (*curr_parent)->first_child = (*current);
1031                 }
1032                 if (*current) {
1033                     (*current)->parent = (*curr_parent);
1034                 }
1035             }
1036             if (*current) {
1037                 if (!*conftree) {
1038                     /* Before walking *current to the end of the list,
1039                      * set the head to *current.
1040                      */
1041                     *conftree = *current;
1042                 }
1043                 while ((*current)->next != NULL) {
1044                     (*current) = (*current)->next;
1045                     (*current)->parent = (*curr_parent);
1046                 }
1047             }
1048             return retval;
1049         }
1050     }
1051
1052     if (cmd_name[0] == '<') {
1053         if (cmd_name[1] != '/') {
1054             (*current) = ap_add_node(curr_parent, *current, newdir, 1);
1055         }
1056         else if (*curr_parent == NULL) {
1057             parms->err_directive = newdir;
1058             return apr_pstrcat(p, cmd_name,
1059                                " without matching <", cmd_name + 2,
1060                                " section", NULL);
1061         }
1062         else {
1063             char *bracket = cmd_name + strlen(cmd_name) - 1;
1064
1065             if (*bracket != '>') {
1066                 parms->err_directive = newdir;
1067                 return apr_pstrcat(p, cmd_name,
1068                                    "> directive missing closing '>'", NULL);
1069             }
1070
1071             *bracket = '\0';
1072
1073             if (strcasecmp(cmd_name + 2,
1074                            (*curr_parent)->directive + 1) != 0) {
1075                 parms->err_directive = newdir;
1076                 return apr_pstrcat(p, "Expected </",
1077                                    (*curr_parent)->directive + 1, "> but saw ",
1078                                    cmd_name, ">", NULL);
1079             }
1080
1081             *bracket = '>';
1082
1083             /* done with this section; move up a level */
1084             *current = *curr_parent;
1085             *curr_parent = (*current)->parent;
1086         }
1087     }
1088     else {
1089         *current = ap_add_node(curr_parent, *current, newdir, 0);
1090     }
1091
1092     return retval;
1093 }
1094
1095 AP_DECLARE(const char *) ap_build_cont_config(apr_pool_t *p,
1096                                               apr_pool_t *temp_pool,
1097                                               cmd_parms *parms,
1098                                               ap_directive_t **current,
1099                                               ap_directive_t **curr_parent,
1100                                               char *orig_directive)
1101 {
1102     char *l;
1103     char *bracket;
1104     const char *retval;
1105     ap_directive_t *sub_tree = NULL;
1106
1107     /* Since this function can be called recursively, allocate
1108      * the temporary 8k string buffer from the temp_pool rather
1109      * than the stack to avoid over-running a fixed length stack.
1110      */
1111     l = apr_palloc(temp_pool, MAX_STRING_LEN);
1112
1113     bracket = apr_pstrcat(p, orig_directive + 1, ">", NULL);
1114     while (!(ap_cfg_getline(l, MAX_STRING_LEN, parms->config_file))) {
1115         if (!memcmp(l, "</", 2)
1116             && (strcasecmp(l + 2, bracket) == 0)
1117             && (*curr_parent == NULL)) {
1118             break;
1119         }
1120         retval = ap_build_config_sub(p, temp_pool, l, parms, current,
1121                                      curr_parent, &sub_tree);
1122         if (retval != NULL)
1123             return retval;
1124
1125         if (sub_tree == NULL && curr_parent != NULL) {
1126             sub_tree = *curr_parent;
1127         }
1128
1129         if (sub_tree == NULL && current != NULL) {
1130             sub_tree = *current;
1131         }
1132     }
1133
1134     *current = sub_tree;
1135     return NULL;
1136 }
1137
1138 static const char *ap_walk_config_sub(const ap_directive_t *current,
1139                                       cmd_parms *parms,
1140                                       ap_conf_vector_t *section_vector)
1141 {
1142     const command_rec *cmd;
1143     ap_mod_list *ml;
1144     char *dir = apr_pstrdup(parms->pool, current->directive);
1145
1146     ap_str_tolower(dir);
1147
1148     ml = apr_hash_get(ap_config_hash, dir, APR_HASH_KEY_STRING);
1149
1150     if (ml == NULL) {
1151         parms->err_directive = current;
1152         return apr_pstrcat(parms->pool, "Invalid command '",
1153                            current->directive,
1154                            "', perhaps misspelled or defined by a module "
1155                            "not included in the server configuration",
1156                            NULL);
1157     }
1158
1159     for ( ; ml != NULL; ml = ml->next) {
1160         void *dir_config = ap_set_config_vectors(parms->server,
1161                                                  section_vector,
1162                                                  parms->path,
1163                                                  ml->m,
1164                                                  parms->pool);
1165         const char *retval;
1166         cmd = ml->cmd;
1167
1168         /* Once was enough? */
1169         if (cmd->req_override & EXEC_ON_READ) {
1170             continue;
1171         }
1172
1173         retval = invoke_cmd(cmd, parms, dir_config, current->args);
1174
1175         if (retval != NULL && strcmp(retval, DECLINE_CMD) != 0) {
1176             /* If the directive in error has already been set, don't
1177              * replace it.  Otherwise, an error inside a container
1178              * will be reported as occuring on the first line of the
1179              * container.
1180              */
1181             if (!parms->err_directive) {
1182                 parms->err_directive = current;
1183             }
1184             return retval;
1185         }
1186     }
1187
1188     return NULL;
1189 }
1190
1191 AP_DECLARE(const char *) ap_walk_config(ap_directive_t *current,
1192                                         cmd_parms *parms,
1193                                         ap_conf_vector_t *section_vector)
1194 {
1195     ap_conf_vector_t *oldconfig = parms->context;
1196
1197     parms->context = section_vector;
1198
1199     /* scan through all directives, executing each one */
1200     for (; current != NULL; current = current->next) {
1201         const char *errmsg;
1202
1203         parms->directive = current;
1204
1205         /* actually parse the command and execute the correct function */
1206         errmsg = ap_walk_config_sub(current, parms, section_vector);
1207         if (errmsg != NULL) {
1208             /* restore the context (just in case) */
1209             parms->context = oldconfig;
1210             return errmsg;
1211         }
1212     }
1213
1214     parms->context = oldconfig;
1215     return NULL;
1216 }
1217
1218 AP_DECLARE(const char *) ap_build_config(cmd_parms *parms,
1219                                          apr_pool_t *p, apr_pool_t *temp_pool,
1220                                          ap_directive_t **conftree)
1221 {
1222     ap_directive_t *current = *conftree;
1223     ap_directive_t *curr_parent = NULL;
1224     char *l = apr_palloc (temp_pool, MAX_STRING_LEN);
1225     const char *errmsg;
1226
1227     if (current != NULL) {
1228         while (current->next) {
1229             current = current->next;
1230         }
1231     }
1232
1233     while (!(ap_cfg_getline(l, MAX_STRING_LEN, parms->config_file))) {
1234         errmsg = ap_build_config_sub(p, temp_pool, l, parms,
1235                                      &current, &curr_parent, conftree);
1236         if (errmsg != NULL)
1237             return errmsg;
1238
1239         if (*conftree == NULL && curr_parent != NULL) {
1240             *conftree = curr_parent;
1241         }
1242
1243         if (*conftree == NULL && current != NULL) {
1244             *conftree = current;
1245         }
1246     }
1247
1248     if (curr_parent != NULL) {
1249         errmsg = "";
1250
1251         while (curr_parent != NULL) {
1252             errmsg = apr_psprintf(p, "%s%s%s:%u: %s> was not closed.",
1253                                   errmsg,
1254                                   *errmsg == '\0' ? "" : APR_EOL_STR,
1255                                   curr_parent->filename,
1256                                   curr_parent->line_num,
1257                                   curr_parent->directive);
1258
1259             parms->err_directive = curr_parent;
1260             curr_parent = curr_parent->parent;
1261         }
1262
1263         return errmsg;
1264     }
1265
1266     return NULL;
1267 }
1268
1269 /*
1270  * Generic command functions...
1271  */
1272
1273 AP_DECLARE_NONSTD(const char *) ap_set_string_slot(cmd_parms *cmd,
1274                                                    void *struct_ptr,
1275                                                    const char *arg)
1276 {
1277     int offset = (int)(long)cmd->info;
1278
1279     *(const char **)((char *)struct_ptr + offset) = arg;
1280
1281     return NULL;
1282 }
1283
1284 AP_DECLARE_NONSTD(const char *) ap_set_int_slot(cmd_parms *cmd,
1285                                                 void *struct_ptr,
1286                                                 const char *arg)
1287 {
1288     char *endptr;
1289     char *error_str = NULL;
1290     int offset = (int)(long)cmd->info;
1291
1292     *(int *)((char*)struct_ptr + offset) = strtol(arg, &endptr, 10);
1293
1294     if ((*arg == '\0') || (*endptr != '\0')) {
1295         error_str = apr_psprintf(cmd->pool,
1296                      "Invalid value for directive %s, expected integer",
1297                      cmd->directive->directive);
1298     }
1299
1300     return error_str;
1301 }
1302
1303 AP_DECLARE_NONSTD(const char *) ap_set_string_slot_lower(cmd_parms *cmd,
1304                                                          void *struct_ptr,
1305                                                          const char *arg_)
1306 {
1307     char *arg = apr_pstrdup(cmd->pool,arg_);
1308     int offset = (int)(long)cmd->info;
1309
1310     ap_str_tolower(arg);
1311     *(char **)((char *)struct_ptr + offset) = arg;
1312
1313     return NULL;
1314 }
1315
1316 AP_DECLARE_NONSTD(const char *) ap_set_flag_slot(cmd_parms *cmd,
1317                                                  void *struct_ptr_v, int arg)
1318 {
1319     int offset = (int)(long)cmd->info;
1320     char *struct_ptr = (char *)struct_ptr_v;
1321
1322     *(int *)(struct_ptr + offset) = arg ? 1 : 0;
1323
1324     return NULL;
1325 }
1326
1327 AP_DECLARE_NONSTD(const char *) ap_set_file_slot(cmd_parms *cmd, void *struct_ptr,
1328                                                  const char *arg)
1329 {
1330     /* Prepend server_root to relative arg.
1331      * This allows most args to be independent of server_root,
1332      * so the server can be moved or mirrored with less pain.
1333      */
1334     const char *path;
1335     int offset = (int)(long)cmd->info;
1336
1337     path = ap_server_root_relative(cmd->pool, arg);
1338
1339     if (!path) {
1340         return apr_pstrcat(cmd->pool, "Invalid file path ",
1341                            arg, NULL);
1342     }
1343
1344     *(const char **) ((char*)struct_ptr + offset) = path;
1345
1346     return NULL;
1347 }
1348
1349 AP_DECLARE_NONSTD(const char *) ap_set_deprecated(cmd_parms *cmd,
1350                                                   void *struct_ptr,
1351                                                   const char *arg)
1352 {
1353     return cmd->cmd->errmsg;
1354 }
1355
1356 /*****************************************************************
1357  *
1358  * Reading whole config files...
1359  */
1360
1361 static cmd_parms default_parms =
1362 {NULL, 0, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
1363
1364 AP_DECLARE(char *) ap_server_root_relative(apr_pool_t *p, const char *file)
1365 {
1366     char *newpath = NULL;
1367     apr_status_t rv;
1368     rv = apr_filepath_merge(&newpath, ap_server_root, file,
1369                             APR_FILEPATH_TRUENAME, p);
1370     if (newpath && (rv == APR_SUCCESS || APR_STATUS_IS_EPATHWILD(rv)
1371                                       || APR_STATUS_IS_ENOENT(rv)
1372                                       || APR_STATUS_IS_ENOTDIR(rv))) {
1373         return newpath;
1374     }
1375     else {
1376         return NULL;
1377     }
1378 }
1379
1380 AP_DECLARE(const char *) ap_soak_end_container(cmd_parms *cmd, char *directive)
1381 {
1382     char l[MAX_STRING_LEN];
1383     const char *args;
1384     char *cmd_name;
1385
1386     while(!(ap_cfg_getline(l, MAX_STRING_LEN, cmd->config_file))) {
1387 #if RESOLVE_ENV_PER_TOKEN
1388         args = l;
1389 #else
1390         args = ap_resolve_env(cmd->temp_pool, l);
1391 #endif
1392
1393         cmd_name = ap_getword_conf(cmd->pool, &args);
1394         if (cmd_name[0] == '<') {
1395             if (cmd_name[1] == '/') {
1396                 cmd_name[strlen(cmd_name) - 1] = '\0';
1397
1398                 if (strcasecmp(cmd_name + 2, directive + 1) != 0) {
1399                     return apr_pstrcat(cmd->pool, "Expected </",
1400                                        directive + 1, "> but saw ",
1401                                        cmd_name, ">", NULL);
1402                 }
1403
1404                 return NULL; /* found end of container */
1405             }
1406             else {
1407                 const char *msg;
1408
1409                 if (*args == '\0' && cmd_name[strlen(cmd_name) - 1] == '>') {
1410                     cmd_name[strlen(cmd_name) - 1] = '\0';
1411                 }
1412
1413                 if ((msg = ap_soak_end_container(cmd, cmd_name)) != NULL) {
1414                     return msg;
1415                 }
1416             }
1417         }
1418     }
1419
1420     return apr_pstrcat(cmd->pool, "Expected </",
1421                        directive + 1, "> before end of configuration",
1422                        NULL);
1423 }
1424
1425 static const char *execute_now(char *cmd_line, const char *args,
1426                                cmd_parms *parms,
1427                                apr_pool_t *p, apr_pool_t *ptemp,
1428                                ap_directive_t **sub_tree,
1429                                ap_directive_t *parent)
1430 {
1431     const command_rec *cmd;
1432     ap_mod_list *ml;
1433     char *dir = apr_pstrdup(parms->pool, cmd_line);
1434
1435     ap_str_tolower(dir);
1436
1437     ml = apr_hash_get(ap_config_hash, dir, APR_HASH_KEY_STRING);
1438
1439     if (ml == NULL) {
1440         return apr_pstrcat(parms->pool, "Invalid command '",
1441                            cmd_line,
1442                            "', perhaps misspelled or defined by a module "
1443                            "not included in the server configuration",
1444                            NULL);
1445     }
1446
1447     for ( ; ml != NULL; ml = ml->next) {
1448         const char *retval;
1449         cmd = ml->cmd;
1450
1451         retval = invoke_cmd(cmd, parms, sub_tree, args);
1452
1453         if (retval != NULL) {
1454             return retval;
1455         }
1456     }
1457
1458     return NULL;
1459 }
1460
1461 /* This structure and the following functions are needed for the
1462  * table-based config file reading. They are passed to the
1463  * cfg_open_custom() routine.
1464  */
1465
1466 /* Structure to be passed to cfg_open_custom(): it contains an
1467  * index which is incremented from 0 to nelts on each call to
1468  * cfg_getline() (which in turn calls arr_elts_getstr())
1469  * and an apr_array_header_t pointer for the string array.
1470  */
1471 typedef struct {
1472     apr_array_header_t *array;
1473     int curr_idx;
1474 } arr_elts_param_t;
1475
1476
1477 /* arr_elts_getstr() returns the next line from the string array. */
1478 static void *arr_elts_getstr(void *buf, size_t bufsiz, void *param)
1479 {
1480     arr_elts_param_t *arr_param = (arr_elts_param_t *)param;
1481
1482     /* End of array reached? */
1483     if (++arr_param->curr_idx > arr_param->array->nelts)
1484         return NULL;
1485
1486     /* return the line */
1487     apr_cpystrn(buf,
1488                 ((char **)arr_param->array->elts)[arr_param->curr_idx - 1],
1489                 bufsiz);
1490
1491     return buf;
1492 }
1493
1494
1495 /* arr_elts_close(): dummy close routine (makes sure no more lines can be read) */
1496 static int arr_elts_close(void *param)
1497 {
1498     arr_elts_param_t *arr_param = (arr_elts_param_t *)param;
1499
1500     arr_param->curr_idx = arr_param->array->nelts;
1501
1502     return 0;
1503 }
1504
1505 static const char *process_command_config(server_rec *s,
1506                                           apr_array_header_t *arr,
1507                                           ap_directive_t **conftree,
1508                                           apr_pool_t *p,
1509                                           apr_pool_t *ptemp)
1510 {
1511     const char *errmsg;
1512     cmd_parms parms;
1513     arr_elts_param_t arr_parms;
1514
1515     arr_parms.curr_idx = 0;
1516     arr_parms.array = arr;
1517
1518     if (ap_config_hash == NULL) {
1519         rebuild_conf_hash(s->process->pconf, 1);
1520     }
1521
1522     parms = default_parms;
1523     parms.pool = p;
1524     parms.temp_pool = ptemp;
1525     parms.server = s;
1526     parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1527     parms.override_opts = OPT_ALL | OPT_SYM_OWNER | OPT_MULTI;
1528
1529     parms.config_file = ap_pcfg_open_custom(p, "-c/-C directives",
1530                                             &arr_parms, NULL,
1531                                             arr_elts_getstr, arr_elts_close);
1532
1533     errmsg = ap_build_config(&parms, p, ptemp, conftree);
1534     ap_cfg_closefile(parms.config_file);
1535
1536     if (errmsg) {
1537         return apr_pstrcat(p, "Syntax error in -C/-c directive: ", errmsg,
1538                            NULL);
1539     }
1540
1541     return NULL;
1542 }
1543
1544 typedef struct {
1545     char *fname;
1546 } fnames;
1547
1548 static int fname_alphasort(const void *fn1, const void *fn2)
1549 {
1550     const fnames *f1 = fn1;
1551     const fnames *f2 = fn2;
1552
1553     return strcmp(f1->fname,f2->fname);
1554 }
1555
1556 static const char *process_resource_config_nofnmatch(server_rec *s,
1557                                                      const char *fname,
1558                                                      ap_directive_t **conftree,
1559                                                      apr_pool_t *p,
1560                                                      apr_pool_t *ptemp,
1561                                                      unsigned depth)
1562 {
1563     cmd_parms parms;
1564     ap_configfile_t *cfp;
1565     const char *error;
1566     apr_status_t rv;
1567
1568     if (ap_is_directory(p, fname)) {
1569         apr_dir_t *dirp;
1570         apr_finfo_t dirent;
1571         int current;
1572         apr_array_header_t *candidates = NULL;
1573         fnames *fnew;
1574         char *path = apr_pstrdup(p, fname);
1575
1576         if (++depth > AP_MAX_INCLUDE_DIR_DEPTH) {
1577             return apr_psprintf(p, "Directory %s exceeds the maximum include "
1578                                 "directory nesting level of %u. You have "
1579                                 "probably a recursion somewhere.", path,
1580                                 AP_MAX_INCLUDE_DIR_DEPTH);
1581         }
1582
1583         /*
1584          * first course of business is to grok all the directory
1585          * entries here and store 'em away. Recall we need full pathnames
1586          * for this.
1587          */
1588         rv = apr_dir_open(&dirp, path, p);
1589         if (rv != APR_SUCCESS) {
1590             char errmsg[120];
1591             return apr_psprintf(p, "Could not open config directory %s: %s",
1592                                 path, apr_strerror(rv, errmsg, sizeof errmsg));
1593         }
1594
1595         candidates = apr_array_make(p, 1, sizeof(fnames));
1596         while (apr_dir_read(&dirent, APR_FINFO_DIRENT, dirp) == APR_SUCCESS) {
1597             /* strip out '.' and '..' */
1598             if (strcmp(dirent.name, ".")
1599                 && strcmp(dirent.name, "..")) {
1600                 fnew = (fnames *) apr_array_push(candidates);
1601                 fnew->fname = ap_make_full_path(p, path, dirent.name);
1602             }
1603         }
1604
1605         apr_dir_close(dirp);
1606         if (candidates->nelts != 0) {
1607             qsort((void *) candidates->elts, candidates->nelts,
1608                   sizeof(fnames), fname_alphasort);
1609
1610             /*
1611              * Now recurse these... we handle errors and subdirectories
1612              * via the recursion, which is nice
1613              */
1614             for (current = 0; current < candidates->nelts; ++current) {
1615                 fnew = &((fnames *) candidates->elts)[current];
1616                 error = process_resource_config_nofnmatch(s, fnew->fname,
1617                                                           conftree, p, ptemp,
1618                                                           depth);
1619                 if (error) {
1620                     return error;
1621                 }
1622             }
1623         }
1624
1625         return NULL;
1626     }
1627
1628     /* GCC's initialization extensions are soooo nice here... */
1629     parms = default_parms;
1630     parms.pool = p;
1631     parms.temp_pool = ptemp;
1632     parms.server = s;
1633     parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1634     parms.override_opts = OPT_ALL | OPT_SYM_OWNER | OPT_MULTI;
1635
1636     rv = ap_pcfg_openfile(&cfp, p, fname);
1637     if (rv != APR_SUCCESS) {
1638         char errmsg[120];
1639         return apr_psprintf(p, "Could not open configuration file %s: %s",
1640                             fname, apr_strerror(rv, errmsg, sizeof errmsg));
1641     }
1642
1643     parms.config_file = cfp;
1644     error = ap_build_config(&parms, p, ptemp, conftree);
1645     ap_cfg_closefile(cfp);
1646
1647     if (error) {
1648         return apr_psprintf(p, "Syntax error on line %d of %s: %s",
1649                             parms.err_directive->line_num,
1650                             parms.err_directive->filename, error);
1651     }
1652
1653     return NULL;
1654 }
1655
1656 AP_DECLARE(const char *) ap_process_resource_config(server_rec *s,
1657                                                     const char *fname,
1658                                                     ap_directive_t **conftree,
1659                                                     apr_pool_t *p,
1660                                                     apr_pool_t *ptemp)
1661 {
1662     /* XXX: lstat() won't work on the wildcard pattern...
1663      */
1664
1665     /* don't require conf/httpd.conf if we have a -C or -c switch */
1666     if ((ap_server_pre_read_config->nelts
1667         || ap_server_post_read_config->nelts)
1668         && !(strcmp(fname, ap_server_root_relative(p, SERVER_CONFIG_FILE)))) {
1669         apr_finfo_t finfo;
1670
1671         if (apr_stat(&finfo, fname, APR_FINFO_LINK | APR_FINFO_TYPE, p) != APR_SUCCESS)
1672             return NULL;
1673     }
1674
1675     if (!apr_fnmatch_test(fname)) {
1676         return process_resource_config_nofnmatch(s, fname, conftree, p, ptemp,
1677                                                  0);
1678     }
1679     else {
1680         apr_dir_t *dirp;
1681         apr_finfo_t dirent;
1682         int current;
1683         apr_array_header_t *candidates = NULL;
1684         fnames *fnew;
1685         apr_status_t rv;
1686         char *path = apr_pstrdup(p, fname), *pattern = NULL;
1687
1688         pattern = ap_strrchr(path, '/');
1689
1690         AP_DEBUG_ASSERT(pattern != NULL); /* path must be absolute. */
1691
1692         *pattern++ = '\0';
1693
1694         if (apr_fnmatch_test(path)) {
1695             return apr_pstrcat(p, "Wildcard patterns not allowed in Include ",
1696                                fname, NULL);
1697         }
1698
1699         if (!ap_is_directory(p, path)){
1700             return apr_pstrcat(p, "Include directory '", path, "' not found",
1701                                NULL);
1702         }
1703
1704         if (!apr_fnmatch_test(pattern)) {
1705             return apr_pstrcat(p, "Must include a wildcard pattern for "
1706                                "Include ", fname, NULL);
1707         }
1708
1709         /*
1710          * first course of business is to grok all the directory
1711          * entries here and store 'em away. Recall we need full pathnames
1712          * for this.
1713          */
1714         rv = apr_dir_open(&dirp, path, p);
1715         if (rv != APR_SUCCESS) {
1716             char errmsg[120];
1717             return apr_psprintf(p, "Could not open config directory %s: %s",
1718                                 path, apr_strerror(rv, errmsg, sizeof errmsg));
1719         }
1720
1721         candidates = apr_array_make(p, 1, sizeof(fnames));
1722         while (apr_dir_read(&dirent, APR_FINFO_DIRENT, dirp) == APR_SUCCESS) {
1723             /* strip out '.' and '..' */
1724             if (strcmp(dirent.name, ".")
1725                 && strcmp(dirent.name, "..")
1726                 && (apr_fnmatch(pattern, dirent.name,
1727                                 APR_FNM_PERIOD) == APR_SUCCESS)) {
1728                 fnew = (fnames *) apr_array_push(candidates);
1729                 fnew->fname = ap_make_full_path(p, path, dirent.name);
1730             }
1731         }
1732
1733         apr_dir_close(dirp);
1734         if (candidates->nelts != 0) {
1735             const char *error;
1736
1737             qsort((void *) candidates->elts, candidates->nelts,
1738                   sizeof(fnames), fname_alphasort);
1739
1740             /*
1741              * Now recurse these... we handle errors and subdirectories
1742              * via the recursion, which is nice
1743              */
1744             for (current = 0; current < candidates->nelts; ++current) {
1745                 fnew = &((fnames *) candidates->elts)[current];
1746                 error = process_resource_config_nofnmatch(s, fnew->fname,
1747                                                           conftree, p,
1748                                                           ptemp, 0);
1749                 if (error) {
1750                     return error;
1751                 }
1752             }
1753         }
1754     }
1755
1756     return NULL;
1757 }
1758
1759 AP_DECLARE(int) ap_process_config_tree(server_rec *s,
1760                                        ap_directive_t *conftree,
1761                                        apr_pool_t *p,
1762                                        apr_pool_t *ptemp)
1763 {
1764     const char *errmsg;
1765     cmd_parms parms;
1766
1767     parms = default_parms;
1768     parms.pool = p;
1769     parms.temp_pool = ptemp;
1770     parms.server = s;
1771     parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1772     parms.override_opts = OPT_ALL | OPT_SYM_OWNER | OPT_MULTI;
1773     parms.limited = -1;
1774
1775     errmsg = ap_walk_config(conftree, &parms, s->lookup_defaults);
1776     if (errmsg) {
1777         ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0, p,
1778                      "Syntax error on line %d of %s:",
1779                      parms.err_directive->line_num,
1780                      parms.err_directive->filename);
1781         ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0, p,
1782                      "%s", errmsg);
1783         return HTTP_INTERNAL_SERVER_ERROR;
1784     }
1785
1786     return OK;
1787 }
1788
1789 AP_CORE_DECLARE(int) ap_parse_htaccess(ap_conf_vector_t **result,
1790                                        request_rec *r, int override,
1791                                        int override_opts,
1792                                        const char *d, const char *access_name)
1793 {
1794     ap_configfile_t *f = NULL;
1795     cmd_parms parms;
1796     char *filename = NULL;
1797     const struct htaccess_result *cache;
1798     struct htaccess_result *new;
1799     ap_conf_vector_t *dc = NULL;
1800     apr_status_t status;
1801
1802     /* firstly, search cache */
1803     for (cache = r->htaccess; cache != NULL; cache = cache->next) {
1804         if (cache->override == override && strcmp(cache->dir, d) == 0) {
1805             *result = cache->htaccess;
1806             return OK;
1807         }
1808     }
1809
1810     parms = default_parms;
1811     parms.override = override;
1812     parms.override_opts = override_opts;
1813     parms.pool = r->pool;
1814     parms.temp_pool = r->pool;
1815     parms.server = r->server;
1816     parms.path = apr_pstrdup(r->pool, d);
1817
1818     /* loop through the access names and find the first one */
1819     while (access_name[0]) {
1820         /* AFAICT; there is no use of the actual 'filename' against
1821          * any canonicalization, so we will simply take the given
1822          * name, ignoring case sensitivity and aliases
1823          */
1824         filename = ap_make_full_path(r->pool, d,
1825                                      ap_getword_conf(r->pool, &access_name));
1826         status = ap_pcfg_openfile(&f, r->pool, filename);
1827
1828         if (status == APR_SUCCESS) {
1829             const char *errmsg;
1830             ap_directive_t *temptree = NULL;
1831
1832             dc = ap_create_per_dir_config(r->pool);
1833
1834             parms.config_file = f;
1835             errmsg = ap_build_config(&parms, r->pool, r->pool, &temptree);
1836             if (errmsg == NULL)
1837                 errmsg = ap_walk_config(temptree, &parms, dc);
1838
1839             ap_cfg_closefile(f);
1840
1841             if (errmsg) {
1842                 ap_log_rerror(APLOG_MARK, APLOG_ALERT, 0, r,
1843                               "%s: %s", filename, errmsg);
1844                 return HTTP_INTERNAL_SERVER_ERROR;
1845             }
1846
1847             *result = dc;
1848             break;
1849         }
1850         else {
1851             if (!APR_STATUS_IS_ENOENT(status)
1852                 && !APR_STATUS_IS_ENOTDIR(status)) {
1853                 ap_log_rerror(APLOG_MARK, APLOG_CRIT, status, r,
1854                               "%s pcfg_openfile: unable to check htaccess file, "
1855                               "ensure it is readable",
1856                               filename);
1857                 apr_table_setn(r->notes, "error-notes",
1858                                "Server unable to read htaccess file, denying "
1859                                "access to be safe");
1860                 return HTTP_FORBIDDEN;
1861             }
1862         }
1863     }
1864
1865     /* cache it */
1866     new = apr_palloc(r->pool, sizeof(struct htaccess_result));
1867     new->dir = parms.path;
1868     new->override = override;
1869     new->override_opts = override_opts;
1870     new->htaccess = dc;
1871
1872     /* add to head of list */
1873     new->next = r->htaccess;
1874     r->htaccess = new;
1875
1876     return OK;
1877 }
1878
1879 AP_CORE_DECLARE(const char *) ap_init_virtual_host(apr_pool_t *p,
1880                                                    const char *hostname,
1881                                                    server_rec *main_server,
1882                                                    server_rec **ps)
1883 {
1884     server_rec *s = (server_rec *) apr_pcalloc(p, sizeof(server_rec));
1885
1886     /* TODO: this crap belongs in http_core */
1887     s->process = main_server->process;
1888     s->server_admin = NULL;
1889     s->server_hostname = NULL;
1890     s->server_scheme = NULL;
1891     s->error_fname = NULL;
1892     s->timeout = 0;
1893     s->keep_alive_timeout = 0;
1894     s->keep_alive = -1;
1895     s->keep_alive_max = -1;
1896     s->error_log = main_server->error_log;
1897     s->loglevel = main_server->loglevel;
1898     /* useful default, otherwise we get a port of 0 on redirects */
1899     s->port = main_server->port;
1900     s->next = NULL;
1901
1902     s->is_virtual = 1;
1903     s->names = apr_array_make(p, 4, sizeof(char **));
1904     s->wild_names = apr_array_make(p, 4, sizeof(char **));
1905
1906     s->module_config = create_empty_config(p);
1907     s->lookup_defaults = ap_create_per_dir_config(p);
1908
1909     s->limit_req_line = main_server->limit_req_line;
1910     s->limit_req_fieldsize = main_server->limit_req_fieldsize;
1911     s->limit_req_fields = main_server->limit_req_fields;
1912
1913     *ps = s;
1914
1915     return ap_parse_vhost_addrs(p, hostname, s);
1916 }
1917
1918
1919 AP_DECLARE(void) ap_fixup_virtual_hosts(apr_pool_t *p, server_rec *main_server)
1920 {
1921     server_rec *virt;
1922
1923     for (virt = main_server->next; virt; virt = virt->next) {
1924         merge_server_configs(p, main_server->module_config,
1925                              virt->module_config);
1926
1927         virt->lookup_defaults =
1928             ap_merge_per_dir_configs(p, main_server->lookup_defaults,
1929                                      virt->lookup_defaults);
1930
1931         if (virt->server_admin == NULL)
1932             virt->server_admin = main_server->server_admin;
1933
1934         if (virt->timeout == 0)
1935             virt->timeout = main_server->timeout;
1936
1937         if (virt->keep_alive_timeout == 0)
1938             virt->keep_alive_timeout = main_server->keep_alive_timeout;
1939
1940         if (virt->keep_alive == -1)
1941             virt->keep_alive = main_server->keep_alive;
1942
1943         if (virt->keep_alive_max == -1)
1944             virt->keep_alive_max = main_server->keep_alive_max;
1945
1946         /* XXX: this is really something that should be dealt with by a
1947          * post-config api phase
1948          */
1949         ap_core_reorder_directories(p, virt);
1950     }
1951
1952     ap_core_reorder_directories(p, main_server);
1953 }
1954
1955 /*****************************************************************
1956  *
1957  * Getting *everything* configured...
1958  */
1959
1960 static void init_config_globals(apr_pool_t *p)
1961 {
1962     /* Global virtual host hash bucket pointers.  Init to null. */
1963     ap_init_vhost_config(p);
1964 }
1965
1966 static server_rec *init_server_config(process_rec *process, apr_pool_t *p)
1967 {
1968     apr_status_t rv;
1969     server_rec *s = (server_rec *) apr_pcalloc(p, sizeof(server_rec));
1970
1971     apr_file_open_stderr(&s->error_log, p);
1972     s->process = process;
1973     s->port = 0;
1974     s->server_admin = DEFAULT_ADMIN;
1975     s->server_hostname = NULL;
1976     s->server_scheme = NULL;
1977     s->error_fname = DEFAULT_ERRORLOG;
1978     s->loglevel = DEFAULT_LOGLEVEL;
1979     s->limit_req_line = DEFAULT_LIMIT_REQUEST_LINE;
1980     s->limit_req_fieldsize = DEFAULT_LIMIT_REQUEST_FIELDSIZE;
1981     s->limit_req_fields = DEFAULT_LIMIT_REQUEST_FIELDS;
1982     s->timeout = apr_time_from_sec(DEFAULT_TIMEOUT);
1983     s->keep_alive_timeout = apr_time_from_sec(DEFAULT_KEEPALIVE_TIMEOUT);
1984     s->keep_alive_max = DEFAULT_KEEPALIVE;
1985     s->keep_alive = 1;
1986     s->next = NULL;
1987     s->addrs = apr_pcalloc(p, sizeof(server_addr_rec));
1988
1989     /* NOT virtual host; don't match any real network interface */
1990     rv = apr_sockaddr_info_get(&s->addrs->host_addr,
1991                                NULL, APR_INET, 0, 0, p);
1992     ap_assert(rv == APR_SUCCESS); /* otherwise: bug or no storage */
1993
1994     s->addrs->host_port = 0; /* matches any port */
1995     s->addrs->virthost = ""; /* must be non-NULL */
1996     s->names = s->wild_names = NULL;
1997
1998     s->module_config = create_server_config(p, s);
1999     s->lookup_defaults = create_default_per_dir_config(p);
2000
2001     return s;
2002 }
2003
2004
2005 AP_DECLARE(server_rec*) ap_read_config(process_rec *process, apr_pool_t *ptemp,
2006                                        const char *filename,
2007                                        ap_directive_t **conftree)
2008 {
2009     const char *confname, *error;
2010     apr_pool_t *p = process->pconf;
2011     server_rec *s = init_server_config(process, p);
2012
2013     init_config_globals(p);
2014
2015     /* All server-wide config files now have the SAME syntax... */
2016     error = process_command_config(s, ap_server_pre_read_config, conftree,
2017                                    p, ptemp);
2018     if (error) {
2019         ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL, "%s: %s",
2020                      ap_server_argv0, error);
2021         return NULL;
2022     }
2023
2024     /* process_command_config may change the ServerRoot so
2025      * compute this config file name afterwards.
2026      */
2027     confname = ap_server_root_relative(p, filename);
2028
2029     if (!confname) {
2030         ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT,
2031                      APR_EBADPATH, NULL, "Invalid config file path %s",
2032                      filename);
2033         return NULL;
2034     }
2035
2036     error = ap_process_resource_config(s, confname, conftree, p, ptemp);
2037     if (error) {
2038         ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL,
2039                      "%s: %s", ap_server_argv0, error);
2040         return NULL;
2041     }
2042
2043     error = process_command_config(s, ap_server_post_read_config, conftree,
2044                                    p, ptemp);
2045
2046     if (error) {
2047         ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL, "%s: %s",
2048                      ap_server_argv0, error);
2049         return NULL;
2050     }
2051
2052     return s;
2053 }
2054
2055 AP_DECLARE(void) ap_single_module_configure(apr_pool_t *p, server_rec *s,
2056                                             module *m)
2057 {
2058     if (m->create_server_config)
2059         ap_set_module_config(s->module_config, m,
2060                              (*m->create_server_config)(p, s));
2061
2062     if (m->create_dir_config)
2063         ap_set_module_config(s->lookup_defaults, m,
2064                              (*m->create_dir_config)(p, NULL));
2065 }
2066
2067 AP_DECLARE(void) ap_run_rewrite_args(process_rec *process)
2068 {
2069     module *m;
2070
2071     for (m = ap_top_module; m; m = m->next) {
2072         if (m->rewrite_args) {
2073             (*m->rewrite_args)(process);
2074         }
2075     }
2076 }
2077
2078 /********************************************************************
2079  * Configuration directives are restricted in terms of where they may
2080  * appear in the main configuration files and/or .htaccess files according
2081  * to the bitmask req_override in the command_rec structure.
2082  * If any of the overrides set in req_override are also allowed in the
2083  * context in which the command is read, then the command is allowed.
2084  * The context is determined as follows:
2085  *
2086  *    inside *.conf --> override = (RSRC_CONF|OR_ALL)&~(OR_AUTHCFG|OR_LIMIT);
2087  *    within <Directory> or <Location> --> override = OR_ALL|ACCESS_CONF;
2088  *    within .htaccess --> override = AllowOverride for current directory;
2089  *
2090  * the result is, well, a rather confusing set of possibilities for when
2091  * a particular directive is allowed to be used.  This procedure prints
2092  * in English where the given (pc) directive can be used.
2093  */
2094 static void show_overrides(const command_rec *pc, module *pm)
2095 {
2096     int n = 0;
2097
2098     printf("\tAllowed in *.conf ");
2099     if ((pc->req_override & (OR_OPTIONS | OR_FILEINFO | OR_INDEXES))
2100         || ((pc->req_override & RSRC_CONF)
2101         && ((pc->req_override & (ACCESS_CONF | OR_AUTHCFG | OR_LIMIT))))) {
2102         printf("anywhere");
2103     }
2104     else if (pc->req_override & RSRC_CONF) {
2105         printf("only outside <Directory>, <Files> or <Location>");
2106     }
2107     else {
2108         printf("only inside <Directory>, <Files> or <Location>");
2109     }
2110
2111     /* Warn if the directive is allowed inside <Directory> or .htaccess
2112      * but module doesn't support per-dir configuration
2113      */
2114     if ((pc->req_override & (OR_ALL | ACCESS_CONF)) && !pm->create_dir_config)
2115         printf(" [no per-dir config]");
2116
2117     if (pc->req_override & OR_ALL) {
2118         printf(" and in .htaccess\n\twhen AllowOverride");
2119
2120         if ((pc->req_override & OR_ALL) == OR_ALL) {
2121             printf(" isn't None");
2122         }
2123         else {
2124             printf(" includes ");
2125
2126             if (pc->req_override & OR_AUTHCFG) {
2127                 if (n++)
2128                     printf(" or ");
2129
2130                 printf("AuthConfig");
2131             }
2132
2133             if (pc->req_override & OR_LIMIT) {
2134                 if (n++)
2135                     printf(" or ");
2136
2137                 printf("Limit");
2138             }
2139
2140             if (pc->req_override & OR_OPTIONS) {
2141                 if (n++)
2142                     printf(" or ");
2143
2144                 printf("Options");
2145             }
2146
2147             if (pc->req_override & OR_FILEINFO) {
2148                 if (n++)
2149                     printf(" or ");
2150
2151                 printf("FileInfo");
2152             }
2153
2154             if (pc->req_override & OR_INDEXES) {
2155                 if (n++)
2156                     printf(" or ");
2157
2158                 printf("Indexes");
2159             }
2160         }
2161     }
2162
2163     printf("\n");
2164 }
2165
2166 /* Show the preloaded configuration directives, the help string explaining
2167  * the directive arguments, in what module they are handled, and in
2168  * what parts of the configuration they are allowed.  Used for httpd -L.
2169  */
2170 AP_DECLARE(void) ap_show_directives(void)
2171 {
2172     const command_rec *pc;
2173     int n;
2174
2175     for (n = 0; ap_loaded_modules[n]; ++n) {
2176         for (pc = ap_loaded_modules[n]->cmds; pc && pc->name; ++pc) {
2177             printf("%s (%s)\n", pc->name, ap_loaded_modules[n]->name);
2178
2179             if (pc->errmsg)
2180                 printf("\t%s\n", pc->errmsg);
2181
2182             show_overrides(pc, ap_loaded_modules[n]);
2183         }
2184     }
2185 }
2186
2187 /* Show the preloaded module names.  Used for httpd -l. */
2188 AP_DECLARE(void) ap_show_modules(void)
2189 {
2190     int n;
2191
2192     printf("Compiled in modules:\n");
2193     for (n = 0; ap_loaded_modules[n]; ++n)
2194         printf("  %s\n", ap_loaded_modules[n]->name);
2195 }
2196
2197 AP_DECLARE(void *) ap_retained_data_get(const char *key)
2198 {
2199     void *retained;
2200
2201     apr_pool_userdata_get((void *)&retained, key, ap_pglobal);
2202     return retained;
2203 }
2204
2205 AP_DECLARE(void *) ap_retained_data_create(const char *key, apr_size_t size)
2206 {
2207     void *retained;
2208
2209     retained = apr_pcalloc(ap_pglobal, size);
2210     apr_pool_userdata_set((const void *)retained, key, apr_pool_cleanup_null, ap_pglobal);
2211     return retained;
2212 }