]> granicus.if.org Git - apache/blob - server/config.c
When we are starting the server, we have a pool that can be used to open
[apache] / server / config.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon public domain software
55  * originally written at the National Center for Supercomputing Applications,
56  * University of Illinois, Urbana-Champaign.
57  */
58
59 /*
60  * http_config.c: once was auxillary functions for reading httpd's config
61  * file and converting filenames into a namespace
62  *
63  * Rob McCool 
64  * 
65  * Wall-to-wall rewrite for Apache... commands which are part of the
66  * server core can now be found next door in "http_core.c".  Now contains
67  * general command loop, and functions which do bookkeeping for the new
68  * Apache config stuff (modules and configuration vectors).
69  *
70  * rst
71  *
72  */
73
74 #include "apr.h"
75 #include "apr_strings.h"
76 #include "apr_portable.h"
77 #include "apr_file_io.h"
78
79 #if APR_HAVE_STDIO_H
80 #include <stdio.h>
81 #endif
82
83 #define CORE_PRIVATE
84
85 #include "ap_config.h"
86 #include "httpd.h"
87 #include "http_config.h"
88 #include "http_protocol.h"
89 #include "http_core.h"
90 #include "http_log.h"           /* for errors in parse_htaccess */
91 #include "http_request.h"       /* for default_handler (see invoke_handler) */
92 #include "http_main.h"
93 #include "http_vhost.h"
94 #include "util_cfgtree.h"
95 #ifdef HAVE_ARPA_INET_H
96 #include <arpa/inet.h>
97 #endif
98 #ifdef HAVE_STRINGS_H
99 #include <strings.h>
100 #endif
101
102 AP_DECLARE_DATA const char *ap_server_argv0;
103
104 AP_DECLARE_DATA const char *ap_server_root;
105
106 AP_DECLARE_DATA apr_array_header_t *ap_server_pre_read_config;
107 AP_DECLARE_DATA apr_array_header_t *ap_server_post_read_config;
108 AP_DECLARE_DATA apr_array_header_t *ap_server_config_defines;
109
110 AP_DECLARE_DATA ap_directive_t *ap_conftree;
111
112 AP_HOOK_STRUCT(
113             AP_HOOK_LINK(header_parser)
114             AP_HOOK_LINK(pre_config)
115             AP_HOOK_LINK(post_config)
116             AP_HOOK_LINK(open_logs)
117             AP_HOOK_LINK(child_init)
118 )
119
120 AP_IMPLEMENT_HOOK_RUN_ALL(int,header_parser,
121                           (request_rec *r),(r),OK,DECLINED)
122 AP_IMPLEMENT_HOOK_VOID(pre_config,
123                        (apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp),
124                        (pconf,plog,ptemp))
125 AP_IMPLEMENT_HOOK_VOID(post_config,
126                        (apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp,
127                         server_rec *s),(pconf,plog,ptemp,s))
128 AP_IMPLEMENT_HOOK_VOID(open_logs,
129                        (apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, 
130                         server_rec *s),(pconf,plog,ptemp,s))
131 AP_IMPLEMENT_HOOK_VOID(child_init,
132                        (apr_pool_t *pchild, server_rec *s),(pchild,s))
133
134 /****************************************************************
135  *
136  * We begin with the functions which deal with the linked list
137  * of modules which control just about all of the server operation.
138  */
139
140 /* total_modules is the number of modules that have been linked
141  * into the server.
142  */
143 static int total_modules = 0;
144 /* dynamic_modules is the number of modules that have been added
145  * after the pre-loaded ones have been set up. It shouldn't be larger
146  * than DYNAMIC_MODULE_LIMIT.
147  */
148 static int dynamic_modules = 0;
149 AP_DECLARE_DATA module *top_module = NULL;
150 AP_DECLARE_DATA module **ap_loaded_modules=NULL;
151
152 typedef int (*handler_func) (request_rec *);
153 typedef void *(*dir_maker_func) (apr_pool_t *, char *);
154 typedef void *(*merger_func) (apr_pool_t *, void *, void *);
155
156 /* Dealing with config vectors.  These are associated with per-directory,
157  * per-server, and per-request configuration, and have a void* pointer for
158  * each modules.  The nature of the structure pointed to is private to the
159  * module in question... the core doesn't (and can't) know.  However, there
160  * are defined interfaces which allow it to create instances of its private
161  * per-directory and per-server structures, and to merge the per-directory
162  * structures of a directory and its subdirectory (producing a new one in
163  * which the defaults applying to the base directory have been properly
164  * overridden).
165  */
166
167 #ifndef ap_get_module_config
168 AP_DECLARE(void *) ap_get_module_config(void *conf_vector, module *m)
169 {
170     void **confv = (void **) conf_vector;
171     return confv[m->module_index];
172 }
173 #endif
174
175 #ifndef ap_set_module_config
176 AP_DECLARE(void) ap_set_module_config(void *conf_vector, module *m, void *val)
177 {
178     void **confv = (void **) conf_vector;
179     confv[m->module_index] = val;
180 }
181 #endif
182
183 static void *create_empty_config(apr_pool_t *p)
184 {
185     void **conf_vector = (void **) apr_pcalloc(p, sizeof(void *) *
186                                     (total_modules + DYNAMIC_MODULE_LIMIT));
187     return (void *) conf_vector;
188 }
189
190 static void *create_default_per_dir_config(apr_pool_t *p)
191 {
192     void **conf_vector = (void **) apr_pcalloc(p, sizeof(void *) * (total_modules + DYNAMIC_MODULE_LIMIT));
193     module *modp;
194
195     for (modp = top_module; modp; modp = modp->next) {
196         dir_maker_func df = modp->create_dir_config;
197
198         if (df)
199             conf_vector[modp->module_index] = (*df) (p, NULL);
200     }
201
202     return (void *) conf_vector;
203 }
204
205 void *
206      ap_merge_per_dir_configs(apr_pool_t *p, void *base, void *new)
207 {
208     void **conf_vector = (void **) apr_palloc(p, sizeof(void *) * total_modules);
209     void **base_vector = (void **) base;
210     void **new_vector = (void **) new;
211     module *modp;
212
213     for (modp = top_module; modp; modp = modp->next) {
214         merger_func df = modp->merge_dir_config;
215         int i = modp->module_index;
216
217         if (df && new_vector[i])
218             conf_vector[i] = (*df) (p, base_vector[i], new_vector[i]);
219         else
220             conf_vector[i] = new_vector[i] ? new_vector[i] : base_vector[i];
221     }
222
223     return (void *) conf_vector;
224 }
225
226 static void *create_server_config(apr_pool_t *p, server_rec *s)
227 {
228     void **conf_vector = (void **) apr_pcalloc(p, sizeof(void *) * (total_modules + DYNAMIC_MODULE_LIMIT));
229     module *modp;
230
231     for (modp = top_module; modp; modp = modp->next) {
232         if (modp->create_server_config)
233             conf_vector[modp->module_index] = (*modp->create_server_config) (p, s);
234     }
235
236     return (void *) conf_vector;
237 }
238
239 static void merge_server_configs(apr_pool_t *p, void *base, void *virt)
240 {
241     /* Can reuse the 'virt' vector for the spine of it, since we don't
242      * have to deal with the moral equivalent of .htaccess files here...
243      */
244
245     void **base_vector = (void **) base;
246     void **virt_vector = (void **) virt;
247     module *modp;
248
249     for (modp = top_module; modp; modp = modp->next) {
250         merger_func df = modp->merge_server_config;
251         int i = modp->module_index;
252
253         if (!virt_vector[i])
254             virt_vector[i] = base_vector[i];
255         else if (df)
256             virt_vector[i] = (*df) (p, base_vector[i], virt_vector[i]);
257     }
258 }
259
260 void *ap_create_request_config(apr_pool_t *p)
261 {
262     return create_empty_config(p);
263 }
264
265 void *ap_create_conn_config(apr_pool_t *p)
266 {
267     return create_empty_config(p);
268 }
269
270 AP_CORE_DECLARE(void *) ap_create_per_dir_config(apr_pool_t *p)
271 {
272     return create_empty_config(p);
273 }
274
275 /*
276  * For speed/efficiency we generate a compact list of all the handlers
277  * and wildcard handlers.  This means we won't have to scan the entire
278  * module list looking for handlers... where we'll find a whole whack
279  * of NULLs.
280  */
281 typedef struct {
282     handler_rec hr;
283     size_t len;
284 } fast_handler_rec;
285
286 static fast_handler_rec *handlers;
287 static fast_handler_rec *wildhandlers;
288
289 static void init_handlers(apr_pool_t *p)
290 {
291     module *modp;
292     int nhandlers = 0;
293     int nwildhandlers = 0;
294     const handler_rec *handp;
295     fast_handler_rec *ph, *pw;
296     const char *starp;
297
298     for (modp = top_module; modp; modp = modp->next) {
299         if (!modp->handlers)
300             continue;
301         for (handp = modp->handlers; handp->content_type; ++handp) {
302             if (ap_strchr_c(handp->content_type, '*')) {
303                 nwildhandlers ++;
304             } else {
305                 nhandlers ++;
306             }
307         }
308     }
309     ph = handlers = apr_palloc(p, sizeof(*ph)*(nhandlers + 1));
310     pw = wildhandlers = apr_palloc(p, sizeof(*pw)*(nwildhandlers + 1));
311     for (modp = top_module; modp; modp = modp->next) {
312         if (!modp->handlers)
313             continue;
314         for (handp = modp->handlers; handp->content_type; ++handp) {
315             if ((starp = ap_strchr_c(handp->content_type, '*'))) {
316                 pw->hr.content_type = handp->content_type;
317                 pw->hr.handler = handp->handler;
318                 pw->len = starp - handp->content_type;
319                 pw ++;
320             } else {
321                 ph->hr.content_type = handp->content_type;
322                 ph->hr.handler = handp->handler;
323                 ph->len = strlen(handp->content_type);
324                 ph ++;
325             }
326         }
327     }
328     pw->hr.content_type = NULL;
329     pw->hr.handler = NULL;
330     ph->hr.content_type = NULL;
331     ph->hr.handler = NULL;
332 }
333
334 int ap_invoke_handler(request_rec *r)
335 {
336     fast_handler_rec *handp;
337     const char *handler;
338     const char *p;
339     size_t handler_len;
340     int result = HTTP_INTERNAL_SERVER_ERROR;
341
342     if (r->handler) {
343         handler = r->handler;
344         handler_len = strlen(handler);
345     }
346     else {
347         handler = r->content_type ? r->content_type : ap_default_type(r);
348         if ((p = ap_strchr_c(handler, ';')) != NULL) {
349             /* MIME type arguments */
350             while (p > handler && p[-1] == ' ')
351                 --p;            /* strip trailing spaces */
352             handler_len = p - handler;
353         }
354         else {
355             handler_len = strlen(handler);
356         }
357     }
358
359     /* Pass one --- direct matches */
360
361     for (handp = handlers; handp->hr.content_type; ++handp) {
362         if (handler_len == handp->len
363             && !strncmp(handler, handp->hr.content_type, handler_len)) {
364             result = (*handp->hr.handler) (r);
365
366             if (result != DECLINED)
367                 return result;
368         }
369     }
370
371     /* Pass two --- wildcard matches */
372
373     for (handp = wildhandlers; handp->hr.content_type; ++handp) {
374         if (handler_len >= handp->len
375             && !strncmp(handler, handp->hr.content_type, handp->len)) {
376             result = (*handp->hr.handler) (r);
377
378             if (result != DECLINED)
379                 return result;
380          }
381     }
382
383     if (result == HTTP_INTERNAL_SERVER_ERROR && r->handler && r->filename) {
384         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, r,
385             "handler \"%s\" not found for: %s", r->handler, r->filename);
386     }
387     return HTTP_INTERNAL_SERVER_ERROR;
388 }
389
390 AP_DECLARE(int) ap_method_is_limited(cmd_parms *cmd, const char *method) {
391     int methnum;
392     int i;
393     char **xmethod;
394
395     methnum = ap_method_number_of(method);
396     /*
397      * The simple case: a method hard-coded into Apache.
398      */
399     if (methnum != M_INVALID) {
400         return (methnum & cmd->limited);
401     }
402     /*
403      * Some extension method we don't know implicitly.
404      */
405     if ((cmd->limited_xmethods == NULL)
406         || (cmd->limited_xmethods->nelts == 0)) {
407         return 0;
408     }
409     xmethod = (char **) cmd->limited_xmethods->elts;
410     for (i = 0; i < cmd->limited_xmethods->nelts; ++i) {
411         if (strcmp(method, xmethod[i]) == 0) {
412             return 1;
413         }
414     }
415     return 0;
416 }
417
418 AP_DECLARE(void) ap_register_hooks(module *m)
419     {
420     if(m->register_hooks)
421         {
422         if(getenv("SHOW_HOOKS"))
423             {
424             printf("Registering hooks for %s\n",m->name);
425             ap_debug_module_hooks=1;
426             }
427         ap_debug_module_name=m->name;
428         m->register_hooks();
429         }
430     }
431
432 /* One-time setup for precompiled modules --- NOT to be done on restart */
433
434 AP_DECLARE(void) ap_add_module(module *m)
435 {
436     /* This could be called from an AddModule httpd.conf command,
437      * after the file has been linked and the module structure within it
438      * teased out...
439      */
440
441     if (m->version != MODULE_MAGIC_NUMBER_MAJOR) {
442         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
443                      "%s: module \"%s\" is not compatible with this "
444                      "version of Apache.", ap_server_argv0, m->name);
445         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Please contact the vendor for the correct version.");
446         exit(1);
447     }
448
449     if (m->next == NULL) {
450         m->next = top_module;
451         top_module = m;
452     }
453     if (m->module_index == -1) {
454         m->module_index = total_modules++;
455         dynamic_modules++;
456
457         if (dynamic_modules > DYNAMIC_MODULE_LIMIT) {
458             ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, 
459                          "%s: module \"%s\" could not be loaded, because"
460                          " the dynamic", ap_server_argv0, m->name);
461             ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
462                          "module limit was reached. Please increase "
463                          "DYNAMIC_MODULE_LIMIT and recompile.");
464             exit(1);
465         }
466     }
467
468     /* Some C compilers put a complete path into __FILE__, but we want
469      * only the filename (e.g. mod_includes.c). So check for path
470      * components (Unix and DOS), and remove them.
471      */
472
473     if (ap_strrchr_c(m->name, '/'))
474         m->name = 1 + ap_strrchr_c(m->name, '/');
475     if (ap_strrchr_c(m->name, '\\'))
476         m->name = 1 + ap_strrchr_c(m->name, '\\');
477
478 #ifdef _OSD_POSIX /* __FILE__="*POSIX(/home/martin/apache/src/modules/standard/mod_info.c)" */
479     /* We cannot fix the string in-place, because it's const */
480     if (m->name[strlen(m->name)-1]==')') {
481         char *tmp = strdup(m->name);    /* FIXME:memory leak, albeit a small one */
482         tmp[strlen(tmp)-1] = '\0';
483         m->name = tmp;
484     }
485 #endif /*_OSD_POSIX*/
486
487     /* FIXME: is this the right place to call this? */
488     ap_register_hooks(m);
489 }
490
491 /* 
492  * remove_module undoes what add_module did. There are some caveats:
493  * when the module is removed, its slot is lost so all the current
494  * per-dir and per-server configurations are invalid. So we should
495  * only ever call this function when you are invalidating almost
496  * all our current data. I.e. when doing a restart.
497  */
498
499 AP_DECLARE(void) ap_remove_module(module *m)
500 {
501     module *modp;
502
503     modp = top_module;
504     if (modp == m) {
505         /* We are the top module, special case */
506         top_module = modp->next;
507         m->next = NULL;
508     }
509     else {
510         /* Not the top module, find use. When found modp will
511          * point to the module _before_ us in the list
512          */
513
514         while (modp && modp->next != m) {
515             modp = modp->next;
516         }
517         if (!modp) {
518             /* Uh-oh, this module doesn't exist */
519             ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, NULL,
520                 "Cannot remove module %s: not found in module list",
521                 m->name);
522             return;
523         }
524         /* Eliminate us from the module list */
525         modp->next = modp->next->next;
526     }
527
528     m->module_index = -1;       /* simulate being unloaded, should
529                                  * be unnecessary */
530     dynamic_modules--;
531     total_modules--;
532 }
533
534 AP_DECLARE(void) ap_add_loaded_module(module *mod)
535 {
536     module **m;
537
538     /* 
539      *  Add module pointer to top of chained module list 
540      */
541     ap_add_module(mod);
542
543     /* 
544      *  And module pointer to list of loaded modules 
545      *
546      *  Notes: 1. ap_add_module() would already complain if no more space
547      *            exists for adding a dynamically loaded module
548      *         2. ap_add_module() accepts double inclusion, so we have
549      *            to accept this, too.
550      */
551     for (m = ap_loaded_modules; *m != NULL; m++)
552         ;
553     *m++ = mod;
554     *m = NULL;
555 }
556
557 AP_DECLARE(void) ap_remove_loaded_module(module *mod)
558 {
559     module **m;
560     module **m2;
561     int done;
562
563     /* 
564      *  Remove module pointer from chained module list 
565      */
566     ap_remove_module(mod);
567
568     /* 
569      *  Remove module pointer from list of loaded modules
570      *
571      *  Note: 1. We cannot determine if the module was successfully
572      *           removed by ap_remove_module().
573      *        2. We have not to complain explicity when the module
574      *           is not found because ap_remove_module() did it
575      *           for us already.
576      */
577     for (m = m2 = ap_loaded_modules, done = 0; *m2 != NULL; m2++) {
578         if (*m2 == mod && done == 0)
579             done = 1;
580         else
581             *m++ = *m2;
582     }
583     *m = NULL;
584 }
585
586 AP_DECLARE(void) ap_setup_prelinked_modules(process_rec *process)
587 {
588     module **m;
589     module **m2;
590
591     ap_global_hook_pool=process->pconf;
592
593     /*
594      *  Initialise total_modules variable and module indices
595      */
596     total_modules = 0;
597     for (m = ap_preloaded_modules; *m != NULL; m++)
598         (*m)->module_index = total_modules++;
599
600     /* 
601      *  Initialise list of loaded modules
602      */
603     ap_loaded_modules = (module **)apr_palloc(process->pool,
604         sizeof(module *)*(total_modules+DYNAMIC_MODULE_LIMIT+1));
605     if (ap_loaded_modules == NULL) {
606         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
607                      "Ouch!  Out of memory in ap_setup_prelinked_modules()!");
608     }
609     for (m = ap_preloaded_modules, m2 = ap_loaded_modules; *m != NULL; )
610         *m2++ = *m++;
611     *m2 = NULL;
612
613     /*
614      *   Initialize chain of linked (=activate) modules
615      */
616     for (m = ap_prelinked_modules; *m != NULL; m++)
617         ap_add_module(*m);
618
619     ap_sort_hooks();
620 }
621
622 AP_DECLARE(const char *) ap_find_module_name(module *m)
623 {
624     return m->name;
625 }
626
627 AP_DECLARE(module *) ap_find_linked_module(const char *name)
628 {
629     module *modp;
630
631     for (modp = top_module; modp; modp = modp->next) {
632         if (strcmp(modp->name, name) == 0)
633             return modp;
634     }
635     return NULL;
636 }
637
638 /* Add a named module.  Returns 1 if module found, 0 otherwise.  */
639 AP_DECLARE(int) ap_add_named_module(const char *name)
640 {
641     module *modp;
642     int i = 0;
643
644     for (modp = ap_loaded_modules[i]; modp; modp = ap_loaded_modules[++i]) {
645         if (strcmp(modp->name, name) == 0) {
646             /* Only add modules that are not already enabled.  */
647             if (modp->next == NULL) {
648                 ap_add_module(modp);
649             }
650             return 1;
651         }
652     }
653
654     return 0;
655 }
656
657 /* Clear the internal list of modules, in preparation for starting over. */
658 AP_DECLARE(void) ap_clear_module_list()
659 {
660     module **m = &top_module;
661     module **next_m;
662
663     while (*m) {
664         next_m = &((*m)->next);
665         *m = NULL;
666         m = next_m;
667     }
668
669     /* This is required; so we add it always.  */
670     ap_add_named_module("http_core.c");
671 }
672
673 /*****************************************************************
674  *
675  * Resource, access, and .htaccess config files now parsed by a common
676  * command loop.
677  *
678  * Let's begin with the basics; parsing the line and
679  * invoking the function...
680  */
681
682 static const char *invoke_cmd(const command_rec *cmd, cmd_parms *parms,
683                               void *mconfig, const char *args)
684 {
685     char *w, *w2, *w3;
686     const char *errmsg;
687
688     if ((parms->override & cmd->req_override) == 0)
689         return apr_pstrcat(parms->pool, cmd->name, " not allowed here", NULL);
690
691     parms->info = cmd->cmd_data;
692     parms->cmd = cmd;
693
694     switch (cmd->args_how) {
695     case RAW_ARGS:
696 #ifdef RESOLVE_ENV_PER_TOKEN
697         args = ap_resolve_env(parms->pool,args);
698 #endif
699         return cmd->AP_RAW_ARGS(parms, mconfig, args);
700
701     case NO_ARGS:
702         if (*args != 0)
703             return apr_pstrcat(parms->pool, cmd->name, " takes no arguments",
704                            NULL);
705
706         return cmd->AP_NO_ARGS(parms, mconfig);
707
708     case TAKE1:
709         w = ap_getword_conf(parms->pool, &args);
710
711         if (*w == '\0' || *args != 0)
712             return apr_pstrcat(parms->pool, cmd->name, " takes one argument",
713                             cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
714
715         return cmd->AP_TAKE1(parms, mconfig, w);
716
717     case TAKE2:
718         w = ap_getword_conf(parms->pool, &args);
719         w2 = ap_getword_conf(parms->pool, &args);
720
721         if (*w == '\0' || *w2 == '\0' || *args != 0)
722             return apr_pstrcat(parms->pool, cmd->name, " takes two arguments",
723                             cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
724
725         return cmd->AP_TAKE2(parms, mconfig, w, w2);
726
727     case TAKE12:
728
729         w = ap_getword_conf(parms->pool, &args);
730         w2 = ap_getword_conf(parms->pool, &args);
731
732         if (*w == '\0' || *args != 0)
733             return apr_pstrcat(parms->pool, cmd->name, " takes 1-2 arguments",
734                             cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
735
736         return cmd->AP_TAKE2(parms, mconfig, w, *w2 ? w2 : NULL);
737
738     case TAKE3:
739
740         w = ap_getword_conf(parms->pool, &args);
741         w2 = ap_getword_conf(parms->pool, &args);
742         w3 = ap_getword_conf(parms->pool, &args);
743
744         if (*w == '\0' || *w2 == '\0' || *w3 == '\0' || *args != 0)
745             return apr_pstrcat(parms->pool, cmd->name, " takes three arguments",
746                             cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
747
748         return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
749
750     case TAKE23:
751
752         w = ap_getword_conf(parms->pool, &args);
753         w2 = ap_getword_conf(parms->pool, &args);
754         w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
755
756         if (*w == '\0' || *w2 == '\0' || *args != 0)
757             return apr_pstrcat(parms->pool, cmd->name,
758                             " takes two or three arguments",
759                             cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
760
761         return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
762
763     case TAKE123:
764
765         w = ap_getword_conf(parms->pool, &args);
766         w2 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
767         w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
768
769         if (*w == '\0' || *args != 0)
770             return apr_pstrcat(parms->pool, cmd->name,
771                             " takes one, two or three arguments",
772                             cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
773
774         return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
775
776     case TAKE13:
777
778         w = ap_getword_conf(parms->pool, &args);
779         w2 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
780         w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
781
782         if (*w == '\0' || (w2 && *w2 && !w3) || *args != 0)
783             return apr_pstrcat(parms->pool, cmd->name,
784                             " takes one or three arguments",
785                             cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
786
787         return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
788
789     case ITERATE:
790
791         while (*(w = ap_getword_conf(parms->pool, &args)) != '\0')
792             {
793             if ((errmsg = cmd->AP_TAKE1(parms, mconfig, w)))
794                 return errmsg;
795             }
796
797         return NULL;
798
799     case ITERATE2:
800
801         w = ap_getword_conf(parms->pool, &args);
802
803         if (*w == '\0' || *args == 0)
804             return apr_pstrcat(parms->pool, cmd->name,
805                             " requires at least two arguments",
806                             cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
807
808         while (*(w2 = ap_getword_conf(parms->pool, &args)) != '\0')
809             {
810             if ((errmsg = cmd->AP_TAKE2(parms, mconfig, w, w2)))
811                 return errmsg;
812             }
813
814         return NULL;
815
816     case FLAG:
817
818         w = ap_getword_conf(parms->pool, &args);
819
820         if (*w == '\0' || (strcasecmp(w, "on") && strcasecmp(w, "off")))
821             return apr_pstrcat(parms->pool, cmd->name, " must be On or Off",
822                             NULL);
823
824         return cmd->AP_FLAG(parms, mconfig, strcasecmp(w, "off") != 0);
825
826     default:
827
828         return apr_pstrcat(parms->pool, cmd->name,
829                     " is improperly configured internally (server bug)",
830                         NULL);
831     }
832 }
833
834 AP_CORE_DECLARE(const command_rec *) ap_find_command(const char *name, const command_rec *cmds)
835 {
836     while (cmds->name)
837         if (!strcasecmp(name, cmds->name))
838             return cmds;
839         else
840             ++cmds;
841
842     return NULL;
843 }
844
845 AP_CORE_DECLARE(const command_rec *) ap_find_command_in_modules(const char *cmd_name, module **mod)
846 {
847     const command_rec *cmdp;
848     module *modp;
849
850     for (modp = *mod; modp; modp = modp->next)
851         if (modp->cmds && (cmdp = ap_find_command(cmd_name, modp->cmds))) {
852             *mod = modp;
853             return cmdp;
854         }
855
856     return NULL;
857 }
858
859 AP_CORE_DECLARE(void *) ap_set_config_vectors(cmd_parms *parms, void *config, module *mod)
860 {
861     void *mconfig = ap_get_module_config(config, mod);
862     void *sconfig = ap_get_module_config(parms->server->module_config, mod);
863
864     if (!mconfig && mod->create_dir_config) {
865         mconfig = (*mod->create_dir_config) (parms->pool, parms->path);
866         ap_set_module_config(config, mod, mconfig);
867     }
868
869     if (!sconfig && mod->create_server_config) {
870         sconfig = (*mod->create_server_config) (parms->pool, parms->server);
871         ap_set_module_config(parms->server->module_config, mod, sconfig);
872     }
873     return mconfig;
874 }
875
876 static const char *execute_now(char *cmd_line, const char *args, cmd_parms *parms, 
877                          apr_pool_t *p, apr_pool_t *ptemp,
878                          ap_directive_t **sub_tree, ap_directive_t *parent);
879
880 static const char * ap_build_config_sub(apr_pool_t *p, apr_pool_t *temp_pool,
881                                         const char *l, cmd_parms *parms,
882                                         ap_directive_t **current,
883                                         ap_directive_t **curr_parent,
884                                         ap_directive_t **conftree)
885 {
886     const char *args;
887     char *cmd_name;
888     ap_directive_t *newdir;
889     module *mod = top_module;
890     const command_rec *cmd;
891
892     if (*l == '#' || *l == '\0')
893         return NULL;
894
895 #if RESOLVE_ENV_PER_TOKEN
896     args = l;
897 #else
898     args = ap_resolve_env(temp_pool, l); 
899 #endif
900     cmd_name = ap_getword_conf(p, &args);
901     if (*cmd_name == '\0') {
902         /* Note: this branch should not occur. An empty line should have
903          * triggered the exit further above.
904          */
905         return NULL;
906     }
907
908     newdir = apr_pcalloc(p, sizeof(ap_directive_t));
909     newdir->filename = parms->config_file->name;
910     newdir->line_num = parms->config_file->line_number;
911     newdir->directive = cmd_name;
912     newdir->args = apr_pstrdup(p, args);
913
914     if ((cmd = ap_find_command_in_modules(cmd_name, &mod)) != NULL) {
915         if (cmd->req_override & EXEC_ON_READ) {
916             const char *retval;
917             ap_directive_t *sub_tree = NULL;
918
919             parms->err_directive = newdir;
920             retval = execute_now(cmd_name, args, parms, p, temp_pool, 
921                                  &sub_tree, *curr_parent);
922             if (*current) {
923                 (*current)->next = sub_tree;
924             }
925             else {
926                 (*current) = sub_tree;
927                 if (*curr_parent) {
928                     (*curr_parent)->first_child = (*current);
929                 }
930                 if (*current) {
931                     (*current)->parent = (*curr_parent);
932                 }
933             }
934             if (*current) {
935                 if (!*conftree) {
936                     /* Before walking *current to the end of the list,
937                      * set the head to *current.
938                      */
939                     *conftree = *current;
940                 }
941                 while ((*current)->next != NULL) {
942                     (*current) = (*current)->next;
943                     (*current)->parent = (*curr_parent);
944                 }
945             }
946             return retval;
947         }
948     }
949
950     if (cmd_name[0] == '<') {
951         if (cmd_name[1] != '/') {
952             (*current) = ap_add_node(curr_parent, *current, newdir, 1);
953         }
954         else if (*curr_parent == NULL) {
955             parms->err_directive = newdir;
956             return apr_pstrcat(p, cmd_name,
957                               " without matching <", cmd_name + 2,
958                               " section", NULL);
959         }
960         else {
961             char *bracket = cmd_name + strlen(cmd_name) - 1;
962
963             if (*bracket != '>') {
964                 return apr_pstrcat(p, cmd_name,
965                                   "> directive missing closing '>'", NULL);
966             }
967             *bracket = '\0';
968             if (strcasecmp(cmd_name + 2,
969                             (*curr_parent)->directive + 1) != 0) {
970                 return apr_pstrcat(p, "Expected </",
971                                   (*curr_parent)->directive + 1, "> but saw ",
972                                   cmd_name, ">", NULL);
973             }
974             *bracket = '>';
975
976             /* done with this section; move up a level */
977             *current = *curr_parent;
978             *curr_parent = (*current)->parent;
979         }
980     }
981     else {
982         *current = ap_add_node(curr_parent, *current, newdir, 0);
983     }
984
985     return NULL;
986 }
987
988 const char *ap_build_cont_config(apr_pool_t *p, apr_pool_t *temp_pool,
989                                  cmd_parms *parms,
990                                  ap_directive_t **current,
991                                  ap_directive_t **curr_parent,
992                                  char *orig_directive)
993 {
994     char l[MAX_STRING_LEN];
995     char *bracket;
996     const char *retval;
997     ap_directive_t *sub_tree = NULL;
998
999     bracket = apr_pstrcat(p, orig_directive + 1, ">", NULL);
1000     while (!(ap_cfg_getline(l, MAX_STRING_LEN, parms->config_file))) {
1001         if (!memcmp(l, "</", 2) &&
1002             (strcasecmp(l + 2, bracket) == 0) &&
1003             (*curr_parent == NULL)) {
1004             break;
1005         } 
1006         retval = ap_build_config_sub(p, temp_pool, l, parms, current, 
1007                                      curr_parent, &sub_tree);
1008         if (retval != NULL)
1009             return retval;
1010         if (sub_tree == NULL && curr_parent != NULL) { 
1011             sub_tree = *curr_parent;
1012         }
1013         if (sub_tree == NULL && current != NULL) {
1014             sub_tree = *current;
1015         }
1016     }
1017     *current = sub_tree;
1018     return NULL;
1019 }
1020
1021 static const char *ap_walk_config_sub(const ap_directive_t *current,
1022                                       cmd_parms *parms, void *config)
1023 {
1024     module *mod = top_module;
1025
1026     while (1) {
1027         const command_rec *cmd;
1028
1029         if (!(cmd = ap_find_command_in_modules(current->directive, &mod))) {
1030             parms->err_directive = current;
1031             return apr_pstrcat(parms->pool, "Invalid command '", 
1032                               current->directive,
1033                               "', perhaps mis-spelled or defined by a module "
1034                               "not included in the server configuration",
1035                               NULL);
1036         }
1037         else {
1038             void *mconfig = ap_set_config_vectors(parms,config, mod);
1039             const char *retval;
1040
1041             retval = invoke_cmd(cmd, parms, mconfig, current->args);
1042             if (retval == NULL) {
1043                 return NULL;
1044             }
1045             if (strcmp(retval, DECLINE_CMD) != 0) {
1046                 /* If the directive in error has already been set, don't
1047                  * replace it.  Otherwise, an error inside a container 
1048                  * will be reported as occuring on the first line of the
1049                  * container.
1050                  */
1051                 if (!parms->err_directive) {
1052                     parms->err_directive = current;
1053                 }
1054                 return retval;
1055             }
1056
1057             mod = mod->next;    /* Next time around, skip this one */
1058         }
1059     }
1060     /* NOTREACHED */
1061 }
1062
1063 AP_DECLARE(const char *) ap_walk_config(ap_directive_t *current,
1064                                         cmd_parms *parms, void *config)
1065 {
1066     void *oldconfig = parms->context;
1067
1068     parms->context = config;
1069
1070     /* scan through all directives, executing each one */
1071     for (; current != NULL; current = current->next) {
1072         const char *errmsg;
1073
1074         parms->directive = current;
1075
1076         /* actually parse the command and execute the correct function */
1077         errmsg = ap_walk_config_sub(current, parms, config);
1078         if (errmsg != NULL) {
1079             /* restore the context (just in case) */
1080             parms->context = oldconfig;
1081             return errmsg;
1082         }
1083     }
1084
1085     parms->context = oldconfig;
1086     return NULL;
1087 }
1088
1089
1090 AP_DECLARE(const char *) ap_build_config(cmd_parms *parms,
1091                                          apr_pool_t *p, apr_pool_t *temp_pool,
1092                                          ap_directive_t **conftree)
1093 {
1094     ap_directive_t *current = *conftree;
1095     ap_directive_t *curr_parent = NULL;
1096     char l[MAX_STRING_LEN];
1097     const char *errmsg;
1098
1099     if (current != NULL) {
1100         while (current->next) {
1101             current = current->next;
1102         }
1103     }
1104
1105     while (!(ap_cfg_getline(l, MAX_STRING_LEN, parms->config_file))) {
1106
1107         errmsg = ap_build_config_sub(p, temp_pool, l, parms,
1108                                      &current, &curr_parent, conftree);
1109         if (errmsg != NULL)
1110             return errmsg;
1111
1112         if (*conftree == NULL && curr_parent != NULL) { 
1113             *conftree = curr_parent;
1114         }
1115         if (*conftree == NULL && current != NULL) {
1116             *conftree = current;
1117         }
1118     }
1119
1120     if (curr_parent != NULL) {
1121         errmsg = "";
1122         while (curr_parent != NULL) {
1123             errmsg = apr_psprintf(p, "%s%s%s:%u: %s> was not closed.",
1124                                  errmsg,
1125                                  *errmsg == '\0' ? "" : APR_EOL_STR,
1126                                  curr_parent->filename,
1127                                  curr_parent->line_num,
1128                                  curr_parent->directive);
1129             curr_parent = curr_parent->parent;
1130         }
1131         return errmsg;
1132     }
1133
1134     return NULL;
1135 }
1136
1137 /*
1138  * Generic command functions...
1139  */
1140
1141 AP_DECLARE_NONSTD(const char *) ap_set_string_slot(cmd_parms *cmd,
1142                                                    void *struct_ptr,
1143                                                    const char *arg)
1144 {
1145     /* This one's pretty generic... */
1146
1147     int offset = (int) (long) cmd->info;
1148     *(const char **) ((char *)struct_ptr + offset) = arg;
1149     return NULL;
1150 }
1151
1152 AP_DECLARE_NONSTD(const char *) ap_set_string_slot_lower(cmd_parms *cmd,
1153                                                          void *struct_ptr,
1154                                                          const char *arg_)
1155 {
1156     /* This one's pretty generic... */
1157     char *arg=apr_pstrdup(cmd->pool,arg_);
1158
1159     int offset = (int) (long) cmd->info;
1160     ap_str_tolower(arg);
1161     *(char **) ((char *)struct_ptr + offset) = arg;
1162     return NULL;
1163 }
1164
1165 AP_DECLARE_NONSTD(const char *) ap_set_flag_slot(cmd_parms *cmd,
1166                                                  void *struct_ptr_v, int arg)
1167 {
1168     /* This one's pretty generic too... */
1169
1170     int offset = (int) (long) cmd->info;
1171     char *struct_ptr = (char *)struct_ptr_v;
1172     *(int *) (struct_ptr + offset) = arg ? 1 : 0;
1173     return NULL;
1174 }
1175
1176 AP_DECLARE_NONSTD(const char *) ap_set_file_slot(cmd_parms *cmd, void *struct_ptr, 
1177                                                  const char *arg)
1178 {
1179     /* Prepend server_root to relative arg.
1180        This allows .htaccess to be independent of server_root,
1181        so the server can be moved or mirrored with less pain.  */
1182     char *p;
1183     int offset = (int) (long) cmd->info;
1184 #ifndef OS2
1185     arg = ap_os_canonical_filename(cmd->pool, arg);
1186 #endif
1187     if (ap_os_is_path_absolute(arg))
1188         p = apr_pstrdup(cmd->pool, arg);
1189     else
1190         p = ap_make_full_path(cmd->pool, ap_server_root, arg);
1191     *(char **) ((char*)struct_ptr + offset) = p;
1192     return NULL;
1193 }
1194
1195 /*****************************************************************
1196  *
1197  * Reading whole config files...
1198  */
1199
1200 static cmd_parms default_parms =
1201 {NULL, 0, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
1202
1203 AP_DECLARE(const char *) ap_server_root_relative(apr_pool_t *p, const char *file)
1204 {
1205 #ifndef OS2
1206     file = ap_os_canonical_filename(p, file);
1207 #endif
1208     if(ap_os_is_path_absolute(file))
1209         return file;
1210     return ap_make_full_path(p, ap_server_root, file);
1211 }
1212
1213 AP_DECLARE(const char *) ap_soak_end_container(cmd_parms *cmd, char *directive)
1214 {
1215     char l[MAX_STRING_LEN];
1216     const char *args;
1217     char *cmd_name;
1218
1219     while(!(ap_cfg_getline(l, MAX_STRING_LEN, cmd->config_file))) {
1220 #if RESOLVE_ENV_PER_TOKEN
1221         args = l;
1222 #else
1223         args = ap_resolve_env(cmd->temp_pool, l);
1224 #endif
1225         cmd_name = ap_getword_conf(cmd->pool, &args);
1226         if (cmd_name[0] == '<') {
1227             if (cmd_name[1] == '/') {
1228                 cmd_name[strlen(cmd_name) - 1] = '\0';
1229                 if (strcasecmp(cmd_name + 2, directive + 1) != 0) {
1230                     return apr_pstrcat(cmd->pool, "Expected </",
1231                                       directive + 1, "> but saw ",
1232                                       cmd_name, ">", NULL);
1233                 }
1234                 break;
1235             }
1236             else {
1237                 ap_soak_end_container(cmd, cmd_name);
1238             }
1239         }
1240     }
1241     return NULL;
1242 }
1243
1244 static const char *execute_now(char *cmd_line, const char *args, cmd_parms *parms, 
1245                          apr_pool_t *p, apr_pool_t *ptemp, 
1246                          ap_directive_t **sub_tree, ap_directive_t *parent)
1247 {
1248     module *mod = top_module;
1249     const command_rec *cmd;
1250
1251     if (!(cmd = ap_find_command_in_modules(cmd_line, &mod))) {
1252         return apr_pstrcat(parms->pool, "Invalid command '", 
1253                           cmd_line,
1254                           "', perhaps mis-spelled or defined by a module "
1255                           "not included in the server configuration",
1256                           NULL);
1257     }
1258     else {
1259         return invoke_cmd(cmd, parms, sub_tree, args);
1260     }
1261 }
1262
1263 /* This structure and the following functions are needed for the
1264  * table-based config file reading. They are passed to the
1265  * cfg_open_custom() routine.
1266  */
1267
1268 /* Structure to be passed to cfg_open_custom(): it contains an
1269  * index which is incremented from 0 to nelts on each call to
1270  * cfg_getline() (which in turn calls arr_elts_getstr())
1271  * and an apr_array_header_t pointer for the string array.
1272  */
1273 typedef struct {
1274     apr_array_header_t *array;
1275     int curr_idx;
1276 } arr_elts_param_t;
1277
1278
1279 /* arr_elts_getstr() returns the next line from the string array. */
1280 static void *arr_elts_getstr(void *buf, size_t bufsiz, void *param)
1281 {
1282     arr_elts_param_t *arr_param = (arr_elts_param_t *) param;
1283
1284     /* End of array reached? */
1285     if (++arr_param->curr_idx > arr_param->array->nelts)
1286         return NULL;
1287
1288     /* return the line */
1289     apr_cpystrn(buf, ((char **) arr_param->array->elts)[arr_param->curr_idx - 1], bufsiz);
1290
1291     return buf;
1292 }
1293
1294
1295 /* arr_elts_close(): dummy close routine (makes sure no more lines can be read) */
1296 static int arr_elts_close(void *param)
1297 {
1298     arr_elts_param_t *arr_param = (arr_elts_param_t *) param;
1299     arr_param->curr_idx = arr_param->array->nelts;
1300     return 0;
1301 }
1302
1303 static void process_command_config(server_rec *s, apr_array_header_t *arr, 
1304                               ap_directive_t **conftree, apr_pool_t *p,
1305                               apr_pool_t *ptemp)
1306 {
1307     const char *errmsg;
1308     cmd_parms parms;
1309     arr_elts_param_t arr_parms;
1310
1311     arr_parms.curr_idx = 0;
1312     arr_parms.array = arr;
1313
1314     parms = default_parms;
1315     parms.pool = p;
1316     parms.temp_pool = ptemp;
1317     parms.server = s;
1318     parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1319
1320     parms.config_file = ap_pcfg_open_custom(p, "-c/-C directives",
1321                               &arr_parms, NULL,
1322                               arr_elts_getstr, arr_elts_close);
1323
1324     errmsg = ap_build_config(&parms, p, ptemp, conftree);
1325     if (errmsg) {
1326         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1327                      "Syntax error in -C/-c directive:" APR_EOL_STR "%s", 
1328                      errmsg);
1329         exit(1);
1330     }
1331
1332     ap_cfg_closefile(parms.config_file);
1333 }
1334
1335 typedef struct {
1336     char *fname;
1337 } fnames;
1338
1339 static int fname_alphasort(const void *fn1, const void *fn2)
1340 {
1341     const fnames *f1 = fn1;
1342     const fnames *f2 = fn2;
1343
1344     return strcmp(f1->fname,f2->fname);
1345 }
1346
1347 void ap_process_resource_config(server_rec *s, const char *fname, 
1348                                 ap_directive_t **conftree, apr_pool_t *p, 
1349                                 apr_pool_t *ptemp)
1350 {
1351     cmd_parms parms;
1352     apr_finfo_t finfo;
1353     const char *errmsg;
1354     configfile_t *cfp;
1355
1356     fname = ap_server_root_relative(p, fname);
1357
1358     /* don't require conf/httpd.conf if we have a -C or -c switch */
1359     if ((ap_server_pre_read_config->nelts
1360          || ap_server_post_read_config->nelts)
1361         && !(strcmp(fname, ap_server_root_relative(p, SERVER_CONFIG_FILE)))) {
1362         if (apr_stat(&finfo, fname, p) != APR_SUCCESS)     
1363             return;
1364     }
1365
1366     /* 
1367      * here we want to check if the candidate file is really a
1368      * directory, and most definitely NOT a symlink (to prevent
1369      * horrible loops).  If so, let's recurse and toss it back into
1370      * the function.
1371      */
1372     if (ap_is_rdirectory(fname)) {
1373         apr_dir_t *dirp;
1374         int current;
1375         apr_array_header_t *candidates = NULL;
1376         fnames *fnew;
1377
1378         /*
1379          * first course of business is to grok all the directory
1380          * entries here and store 'em away. Recall we need full pathnames
1381          * for this.
1382          */
1383         fprintf(stderr, "Processing config directory: %s\n", fname);
1384         if (apr_dir_open(&dirp, fname, p) != APR_SUCCESS) {
1385             perror("fopen");
1386             fprintf(stderr, "%s: could not open config directory %s\n",
1387                 ap_server_argv0, fname);
1388             exit(1);
1389         }
1390         candidates = apr_make_array(p, 1, sizeof(fnames));
1391         while (apr_readdir(dirp) == APR_SUCCESS) {
1392             char *d_name;
1393             apr_get_dir_filename(&d_name, dirp);
1394             /* strip out '.' and '..' */
1395             if (strcmp(d_name, ".") &&
1396                 strcmp(d_name, "..")) {
1397                 fnew = (fnames *) apr_push_array(candidates);
1398                 fnew->fname = ap_make_full_path(p, fname, d_name);
1399             }
1400         }
1401         apr_closedir(dirp);
1402         if (candidates->nelts != 0) {
1403             qsort((void *) candidates->elts, candidates->nelts,
1404               sizeof(fnames), fname_alphasort);
1405             /*
1406              * Now recurse these... we handle errors and subdirectories
1407              * via the recursion, which is nice
1408              */
1409             for (current = 0; current < candidates->nelts; ++current) {
1410                 fnew = &((fnames *) candidates->elts)[current];
1411                 fprintf(stderr, " Processing config file: %s\n", fnew->fname);
1412                 ap_process_resource_config(s, fnew->fname, conftree, p, ptemp);
1413             }
1414         }
1415         return;
1416     }
1417     
1418     /* GCC's initialization extensions are soooo nice here... */
1419
1420     parms = default_parms;
1421     parms.pool = p;
1422     parms.temp_pool = ptemp;
1423     parms.server = s;
1424     parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1425
1426     if (ap_pcfg_openfile(&cfp, p, fname) != APR_SUCCESS) {
1427         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1428                      "%s: could not open document config file %s",
1429                      ap_server_argv0, fname);
1430         exit(1);
1431     }
1432
1433     parms.config_file = cfp;
1434
1435     errmsg = ap_build_config(&parms, p, ptemp, conftree);
1436
1437     if (errmsg != NULL) {
1438         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1439                      "Syntax error on line %d of %s:",
1440                      parms.err_directive->line_num, 
1441                      parms.err_directive->filename);
1442         ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, 
1443                      "%s", errmsg);
1444         exit(1);
1445     }
1446
1447     ap_cfg_closefile(cfp);
1448 }
1449
1450 AP_DECLARE(void)ap_process_config_tree(server_rec *s, ap_directive_t *conftree,
1451                                        apr_pool_t *p, apr_pool_t *ptemp)
1452 {
1453     const char *errmsg;
1454     cmd_parms parms;
1455
1456     parms = default_parms;
1457     parms.pool = p;
1458     parms.temp_pool = ptemp;
1459     parms.server = s;
1460     parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1461     parms.limited = -1;
1462
1463     errmsg = ap_walk_config(conftree, &parms, s->lookup_defaults);
1464     if (errmsg) {
1465         ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, p,
1466                      "Syntax error on line %d of %s:",
1467                      parms.err_directive->line_num,
1468                      parms.err_directive->filename);
1469         ap_log_perror(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, p, 
1470                      "%s", errmsg);
1471         exit(1);
1472     }
1473 }
1474
1475 int ap_parse_htaccess(void **result, request_rec *r, int override,
1476                       const char *d, const char *access_name) {
1477     configfile_t *f = NULL;
1478     cmd_parms parms;
1479     char *filename = NULL;
1480     const struct htaccess_result *cache;
1481     struct htaccess_result *new;
1482     void *dc = NULL;
1483     apr_status_t status;
1484
1485 /* firstly, search cache */
1486     for (cache = r->htaccess; cache != NULL; cache = cache->next)
1487         if (cache->override == override && strcmp(cache->dir, d) == 0) {
1488             if (cache->htaccess != NULL)
1489                 *result = cache->htaccess;
1490             return OK;
1491         }
1492
1493     parms = default_parms;
1494     parms.override = override;
1495     parms.pool = r->pool;
1496     parms.temp_pool = r->pool;
1497     parms.server = r->server;
1498     parms.path = apr_pstrdup(r->pool, d);
1499
1500     /* loop through the access names and find the first one */
1501
1502     while (access_name[0]) {
1503         filename = ap_make_full_path(r->pool, d,
1504                                      ap_getword_conf(r->pool, &access_name));
1505         status = ap_pcfg_openfile(&f, r->pool, filename);
1506
1507         if (status == APR_SUCCESS) {
1508             const char *errmsg;
1509             ap_directive_t *temptree = NULL;
1510
1511             dc = ap_create_per_dir_config(r->pool);
1512
1513             parms.config_file = f;
1514             errmsg = ap_build_config(&parms, r->pool, r->pool, &temptree);
1515             if (errmsg == NULL)
1516                 errmsg = ap_walk_config(temptree, &parms, dc);
1517
1518             ap_cfg_closefile(f);
1519
1520             if (errmsg) {
1521                 ap_log_rerror(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO, 0, r,
1522                               "%s: %s", filename, errmsg);
1523                 return HTTP_INTERNAL_SERVER_ERROR;
1524             }
1525             *result = dc;
1526             break;
1527         } else {
1528             if (!APR_STATUS_IS_ENOENT(status) && !APR_STATUS_IS_ENOTDIR(status)) {
1529                 ap_log_rerror(APLOG_MARK, APLOG_CRIT, status, r,
1530                               "%s pcfg_openfile: unable to check htaccess file, "
1531                               "ensure it is readable",
1532                               filename);
1533                 apr_table_setn(r->notes, "error-notes",
1534                               "Server unable to read htaccess file, denying "
1535                               "access to be safe");
1536                 return HTTP_FORBIDDEN;
1537             }
1538         }
1539     }
1540
1541 /* cache it */
1542     new = apr_palloc(r->pool, sizeof(struct htaccess_result));
1543     new->dir = parms.path;
1544     new->override = override;
1545     new->htaccess = dc;
1546 /* add to head of list */
1547     new->next = r->htaccess;
1548     r->htaccess = new;
1549
1550     return OK;
1551 }
1552
1553 AP_CORE_DECLARE(const char *) ap_init_virtual_host(apr_pool_t *p, const char *hostname,
1554                               server_rec *main_server, server_rec **ps)
1555 {
1556     server_rec *s = (server_rec *) apr_pcalloc(p, sizeof(server_rec));
1557
1558     /* TODO: this crap belongs in http_core */
1559     s->process = main_server->process;
1560     s->server_admin = NULL;
1561     s->server_hostname = NULL;
1562     s->error_fname = NULL;
1563     s->timeout = 0;
1564     s->keep_alive_timeout = 0;
1565     s->keep_alive = -1;
1566     s->keep_alive_max = -1;
1567     s->error_log = main_server->error_log;
1568     s->loglevel = main_server->loglevel;
1569     /* useful default, otherwise we get a port of 0 on redirects */
1570     s->port = main_server->port;
1571     s->next = NULL;
1572
1573     s->is_virtual = 1;
1574     s->names = apr_make_array(p, 4, sizeof(char **));
1575     s->wild_names = apr_make_array(p, 4, sizeof(char **));
1576
1577     s->module_config = create_empty_config(p);
1578     s->lookup_defaults = ap_create_per_dir_config(p);
1579
1580     s->limit_req_line = main_server->limit_req_line;
1581     s->limit_req_fieldsize = main_server->limit_req_fieldsize;
1582     s->limit_req_fields = main_server->limit_req_fields;
1583
1584     *ps = s;
1585
1586     return ap_parse_vhost_addrs(p, hostname, s);
1587 }
1588
1589
1590 AP_DECLARE(void) ap_fixup_virtual_hosts(apr_pool_t *p, server_rec *main_server)
1591 {
1592     server_rec *virt;
1593
1594     for (virt = main_server->next; virt; virt = virt->next) {
1595         merge_server_configs(p, main_server->module_config,
1596                              virt->module_config);
1597
1598         virt->lookup_defaults =
1599             ap_merge_per_dir_configs(p, main_server->lookup_defaults,
1600                                   virt->lookup_defaults);
1601
1602         if (virt->server_admin == NULL)
1603             virt->server_admin = main_server->server_admin;
1604
1605         if (virt->timeout == 0)
1606             virt->timeout = main_server->timeout;
1607
1608         if (virt->keep_alive_timeout == 0)
1609             virt->keep_alive_timeout = main_server->keep_alive_timeout;
1610
1611         if (virt->keep_alive == -1)
1612             virt->keep_alive = main_server->keep_alive;
1613
1614         if (virt->keep_alive_max == -1)
1615             virt->keep_alive_max = main_server->keep_alive_max;
1616
1617         /* XXX: this is really something that should be dealt with by a
1618          * post-config api phase */
1619         ap_core_reorder_directories(p, virt);
1620     }
1621     ap_core_reorder_directories(p, main_server);
1622 }
1623
1624 /*****************************************************************
1625  *
1626  * Getting *everything* configured... 
1627  */
1628
1629 static void init_config_globals(apr_pool_t *p)
1630 {
1631     /* Global virtual host hash bucket pointers.  Init to null. */
1632     ap_init_vhost_config(p);
1633 }
1634
1635 static server_rec *init_server_config(process_rec *process, apr_pool_t *p)
1636 {
1637     apr_status_t rv;
1638     server_rec *s = (server_rec *) apr_pcalloc(p, sizeof(server_rec));
1639
1640     apr_open_stderr(&s->error_log, p);
1641     s->process = process;
1642     s->port = 0;
1643     s->server_admin = DEFAULT_ADMIN;
1644     s->server_hostname = NULL;
1645     s->error_fname = DEFAULT_ERRORLOG;
1646     s->loglevel = DEFAULT_LOGLEVEL;
1647     s->limit_req_line = DEFAULT_LIMIT_REQUEST_LINE;
1648     s->limit_req_fieldsize = DEFAULT_LIMIT_REQUEST_FIELDSIZE;
1649     s->limit_req_fields = DEFAULT_LIMIT_REQUEST_FIELDS;
1650     s->timeout = DEFAULT_TIMEOUT;     
1651     s->keep_alive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
1652     s->keep_alive_max = DEFAULT_KEEPALIVE;
1653     s->keep_alive = 1;
1654     s->next = NULL;
1655     s->addrs = apr_pcalloc(p, sizeof(server_addr_rec));
1656     /* NOT virtual host; don't match any real network interface */
1657     rv = apr_getaddrinfo(&s->addrs->host_addr,
1658                          NULL, APR_INET, 0, 0, p);
1659     ap_assert(rv == APR_SUCCESS); /* otherwise: bug or no storage */
1660     s->addrs->host_port = 0;    /* matches any port */
1661     s->addrs->virthost = "";    /* must be non-NULL */
1662     s->names = s->wild_names = NULL;
1663
1664     s->module_config = create_server_config(p, s);
1665     s->lookup_defaults = create_default_per_dir_config(p);
1666
1667     return s;
1668 }
1669
1670
1671 AP_DECLARE(server_rec*) ap_read_config(process_rec *process, apr_pool_t *ptemp,
1672                                        const char *confname, 
1673                                        ap_directive_t **conftree)
1674 {
1675     apr_pool_t *p = process->pconf;
1676     server_rec *s = init_server_config(process, p);
1677
1678     init_config_globals(p);
1679
1680     /* All server-wide config files now have the SAME syntax... */
1681
1682     process_command_config(s, ap_server_pre_read_config, conftree,
1683                                       p, ptemp);
1684
1685     ap_process_resource_config(s, confname, conftree, p, ptemp);
1686
1687     process_command_config(s, ap_server_post_read_config, conftree,
1688                                       p, ptemp);
1689
1690     return s;
1691 }
1692
1693 void ap_single_module_configure(apr_pool_t *p, server_rec *s, module *m)
1694 {
1695     if (m->create_server_config)
1696         ap_set_module_config(s->module_config, m,
1697                              (*m->create_server_config)(p, s));
1698     if (m->create_dir_config)
1699         ap_set_module_config(s->lookup_defaults, m,
1700                              (*m->create_dir_config)(p, NULL));
1701 }
1702
1703 AP_DECLARE(void) ap_run_rewrite_args(process_rec *process)
1704 {
1705     module *m;
1706
1707     for (m = top_module; m; m = m->next)
1708         if (m->rewrite_args)
1709             (*m->rewrite_args) (process);
1710 }
1711
1712 AP_DECLARE(void) ap_post_config_hook(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
1713 {
1714     ap_run_post_config(pconf,plog,ptemp,s); 
1715     init_handlers(pconf);
1716 }
1717
1718 void ap_child_init_hook(apr_pool_t *pchild, server_rec *s)
1719 {
1720     /* TODO: uh this seems ugly, is there a better way? */
1721     /*ap_child_init_alloc();    PUT THIS BACK IN XXXXX */
1722
1723     ap_run_child_init(pchild,s);
1724 }
1725
1726 /********************************************************************
1727  * Configuration directives are restricted in terms of where they may
1728  * appear in the main configuration files and/or .htaccess files according
1729  * to the bitmask req_override in the command_rec structure.
1730  * If any of the overrides set in req_override are also allowed in the
1731  * context in which the command is read, then the command is allowed.
1732  * The context is determined as follows:
1733  *
1734  *    inside *.conf --> override = (RSRC_CONF|OR_ALL)&~(OR_AUTHCFG|OR_LIMIT);
1735  *    within <Directory> or <Location> --> override = OR_ALL|ACCESS_CONF;
1736  *    within .htaccess --> override = AllowOverride for current directory;
1737  *
1738  * the result is, well, a rather confusing set of possibilities for when
1739  * a particular directive is allowed to be used.  This procedure prints
1740  * in English where the given (pc) directive can be used.
1741  */
1742 static void show_overrides(const command_rec *pc, module *pm)
1743 {
1744     int n = 0;
1745
1746     printf("\tAllowed in *.conf ");
1747     if ((pc->req_override & (OR_OPTIONS | OR_FILEINFO | OR_INDEXES)) ||
1748         ((pc->req_override & RSRC_CONF) &&
1749          ((pc->req_override & (ACCESS_CONF | OR_AUTHCFG | OR_LIMIT)))))
1750         printf("anywhere");
1751     else if (pc->req_override & RSRC_CONF)
1752         printf("only outside <Directory>, <Files> or <Location>");
1753     else
1754         printf("only inside <Directory>, <Files> or <Location>");
1755
1756     /* Warn if the directive is allowed inside <Directory> or .htaccess
1757      * but module doesn't support per-dir configuration */
1758
1759     if ((pc->req_override & (OR_ALL | ACCESS_CONF)) && !pm->create_dir_config)
1760         printf(" [no per-dir config]");
1761
1762     if (pc->req_override & OR_ALL) {
1763         printf(" and in .htaccess\n\twhen AllowOverride");
1764
1765         if ((pc->req_override & OR_ALL) == OR_ALL)
1766             printf(" isn't None");
1767         else {
1768             printf(" includes ");
1769
1770             if (pc->req_override & OR_AUTHCFG) {
1771                 if (n++)
1772                     printf(" or ");
1773                 printf("AuthConfig");
1774             }
1775             if (pc->req_override & OR_LIMIT) {
1776                 if (n++)
1777                     printf(" or ");
1778                 printf("Limit");
1779             }
1780             if (pc->req_override & OR_OPTIONS) {
1781                 if (n++)
1782                     printf(" or ");
1783                 printf("Options");
1784             }
1785             if (pc->req_override & OR_FILEINFO) {
1786                 if (n++)
1787                     printf(" or ");
1788                 printf("FileInfo");
1789             }
1790             if (pc->req_override & OR_INDEXES) {
1791                 if (n++)
1792                     printf(" or ");
1793                 printf("Indexes");
1794             }
1795         }
1796     }
1797     printf("\n");
1798 }
1799
1800 /* Show the preloaded configuration directives, the help string explaining
1801  * the directive arguments, in what module they are handled, and in
1802  * what parts of the configuration they are allowed.  Used for httpd -h.
1803  */
1804 AP_DECLARE(void) ap_show_directives()
1805 {
1806     const command_rec *pc;
1807     int n;
1808
1809     for (n = 0; ap_loaded_modules[n]; ++n)
1810         for (pc = ap_loaded_modules[n]->cmds; pc && pc->name; ++pc) {
1811             printf("%s (%s)\n", pc->name, ap_loaded_modules[n]->name);
1812             if (pc->errmsg)
1813                 printf("\t%s\n", pc->errmsg);
1814             show_overrides(pc, ap_loaded_modules[n]);
1815         }
1816 }
1817
1818 /* Show the preloaded module names.  Used for httpd -l. */
1819 AP_DECLARE(void) ap_show_modules()
1820 {
1821     int n;
1822
1823     printf("Compiled in modules:\n");
1824     for (n = 0; ap_loaded_modules[n]; ++n)
1825         printf("  %s\n", ap_loaded_modules[n]->name);
1826 }
1827