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