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