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