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