home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / http_cor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-12  |  30.0 KB  |  951 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54.  
  55. #define CORE_PRIVATE
  56. #include "httpd.h"
  57. #include "http_config.h"
  58. #include "http_core.h"
  59. #include "http_protocol.h"    /* For index_of_response().  Grump. */
  60. #include "http_conf_globals.h"
  61.  
  62. #include "http_main.h"        /* For the default_handler below... */
  63. #include "http_log.h"
  64. #include "rfc1413.h"
  65. #include "util_md5.h"
  66. #include "scoreboard.h"
  67.  
  68. /* Server core module... This module provides support for really basic
  69.  * server operations, including options and commands which control the
  70.  * operation of other modules.  Consider this the bureaucracy module.
  71.  *
  72.  * The core module also defines handlers, etc., do handle just enough
  73.  * to allow a server with the core module ONLY to actually serve documents
  74.  * (though it slaps DefaultType on all of 'em); this was useful in testing,
  75.  * but may not be worth preserving.
  76.  *
  77.  * This file could almost be mod_core.c, except for the stuff which affects
  78.  * the http_conf_globals.
  79.  */
  80.  
  81. void *create_core_dir_config (pool *a, char *dir)
  82. {
  83.     core_dir_config *conf =
  84.       (core_dir_config *)pcalloc(a, sizeof(core_dir_config));
  85.   
  86.     if (!dir || dir[strlen(dir) - 1] == '/') conf->d = dir;
  87.     else conf->d = pstrcat (a, dir, "/", NULL);
  88.  
  89.     conf->opts = dir ? OPT_UNSET : OPT_ALL;
  90.     conf->override = dir ? OR_UNSET : OR_ALL;
  91.  
  92.     conf->content_md5 = 2;
  93.  
  94.     conf->hostname_lookups = 2;/* binary, but will use 2 as an "unset = on" */
  95.     conf->do_rfc1413 = DEFAULT_RFC1413 | 2;  /* set bit 1 to indicate default */
  96.     return (void *)conf;
  97. }
  98.  
  99. void *merge_core_dir_configs (pool *a, void *basev, void *newv)
  100. {
  101.     core_dir_config *base = (core_dir_config *)basev;
  102.     core_dir_config *new = (core_dir_config *)newv;
  103.     core_dir_config *conf =
  104.       (core_dir_config *)pcalloc (a, sizeof(core_dir_config));
  105.     int i;
  106.   
  107.     memcpy ((char *)conf, (const char *)base, sizeof(core_dir_config));
  108.     
  109.     conf->d = new->d;
  110.     
  111.     if (new->opts != OPT_UNSET) conf->opts = new->opts;
  112.     if (new->override != OR_UNSET) conf->override = new->override;
  113.     if (new->default_type) conf->default_type = new->default_type;
  114.     
  115.     if (new->auth_type) conf->auth_type = new->auth_type;
  116.     if (new->auth_name) conf->auth_name = new->auth_name;
  117.     if (new->requires) conf->requires = new->requires;
  118.  
  119.     for (i = 0; i <= RESPONSE_CODES; ++i)
  120.         if (new->response_code_strings[i] != NULL)
  121.        conf->response_code_strings[i] = new->response_code_strings[i];
  122.     if (new->hostname_lookups != 2)
  123.     conf->hostname_lookups = new->hostname_lookups;
  124.     if ((new->do_rfc1413 & 2) == 0) conf->do_rfc1413 = new->do_rfc1413;
  125.     if ((new->content_md5 & 2) == 0) conf->content_md5 = new->content_md5;
  126.  
  127.     return (void*)conf;
  128. }
  129.  
  130. void *create_core_server_config (pool *a, server_rec *s)
  131. {
  132.     core_server_config *conf =
  133.       (core_server_config *)pcalloc(a, sizeof(core_server_config));
  134.     int is_virtual = s->is_virtual;
  135.   
  136.     conf->access_name = is_virtual ? NULL : DEFAULT_ACCESS_FNAME;
  137.     conf->document_root = is_virtual ? NULL : DOCUMENT_LOCATION;
  138.     conf->sec = make_array (a, 40, sizeof(void *));
  139.     conf->sec_url = make_array (a, 40, sizeof(void *));
  140.     
  141.     return (void *)conf;
  142. }
  143.  
  144. void *merge_core_server_configs (pool *p, void *basev, void *virtv)
  145. {
  146.     core_server_config *base = (core_server_config *)basev;
  147.     core_server_config *virt = (core_server_config *)virtv;
  148.     core_server_config *conf = 
  149.     (core_server_config *)pcalloc(p, sizeof(core_server_config));
  150.  
  151.     *conf = *virt;
  152.     if (!conf->access_name) conf->access_name = base->access_name;
  153.     if (!conf->document_root) conf->document_root = base->document_root;
  154.     conf->sec = append_arrays (p, virt->sec, base->sec);
  155.     conf->sec_url = append_arrays (p, virt->sec_url, base->sec_url);
  156.  
  157.     return conf;
  158. }
  159.  
  160. /* Add per-directory configuration entry (for <directory> section);
  161.  * these are part of the core server config.
  162.  */
  163.  
  164. void add_per_dir_conf (server_rec *s, void *dir_config)
  165. {
  166.     core_server_config *sconf = get_module_config (s->module_config,
  167.                            &core_module);
  168.     void **new_space = (void **) push_array (sconf->sec);
  169.     
  170.     *new_space = dir_config;
  171. }
  172.  
  173. void add_per_url_conf (server_rec *s, void *url_config)
  174. {
  175.     core_server_config *sconf = get_module_config (s->module_config,
  176.                            &core_module);
  177.     void **new_space = (void **) push_array (sconf->sec_url);
  178.     
  179.     *new_space = url_config;
  180. }
  181.  
  182. /*****************************************************************
  183.  *
  184.  * There are some elements of the core config structures in which
  185.  * other modules have a legitimate interest (this is ugly, but necessary
  186.  * to preserve NCSA back-compatibility).  So, we have a bunch of accessors
  187.  * here...
  188.  */
  189.  
  190. int allow_options (request_rec *r)
  191. {
  192.     core_dir_config *conf = 
  193.       (core_dir_config *)get_module_config(r->per_dir_config, &core_module); 
  194.  
  195.     return conf->opts; 
  196.  
  197. int allow_overrides (request_rec *r) 
  198.     core_dir_config *conf = 
  199.       (core_dir_config *)get_module_config(r->per_dir_config, &core_module); 
  200.  
  201.     return conf->override; 
  202.  
  203. char *auth_type (request_rec *r)
  204. {
  205.     core_dir_config *conf = 
  206.       (core_dir_config *)get_module_config(r->per_dir_config, &core_module); 
  207.  
  208.     return conf->auth_type;
  209. }
  210.  
  211. char *auth_name (request_rec *r)
  212. {
  213.     core_dir_config *conf = 
  214.       (core_dir_config *)get_module_config(r->per_dir_config, &core_module); 
  215.  
  216.     return conf->auth_name;
  217. }
  218.  
  219. char *default_type (request_rec *r)
  220. {
  221.     core_dir_config *conf = 
  222.       (core_dir_config *)get_module_config(r->per_dir_config, &core_module); 
  223.  
  224.     return conf->default_type ? conf->default_type : DEFAULT_TYPE;
  225. }
  226.  
  227. char *document_root (request_rec *r) /* Don't use this!!! */
  228. {
  229.     core_server_config *conf = 
  230.       (core_server_config *)get_module_config(r->server->module_config,
  231.                           &core_module); 
  232.  
  233.     return conf->document_root;
  234. }
  235.  
  236. array_header *requires (request_rec *r)
  237. {
  238.     core_dir_config *conf = 
  239.       (core_dir_config *)get_module_config(r->per_dir_config, &core_module); 
  240.  
  241.     return conf->requires;
  242. }
  243.  
  244.  
  245. /* Should probably just get rid of this... the only code that cares is
  246.  * part of the core anyway (and in fact, it isn't publicised to other
  247.  * modules).
  248.  */
  249.  
  250. char *response_code_string (request_rec *r, int error_index)
  251. {
  252.     core_dir_config *conf = 
  253.       (core_dir_config *)get_module_config(r->per_dir_config, &core_module); 
  254.  
  255.     return conf->response_code_strings[error_index];
  256. }
  257.  
  258. const char *
  259. get_remote_host(conn_rec *conn, void *dir_config, int type)
  260. {
  261.     struct in_addr *iaddr;
  262.     struct hostent *hptr;
  263. #ifdef MAXIMUM_DNS
  264.     char **haddr;
  265. #endif
  266.     core_dir_config *dir_conf;
  267.  
  268. /* If we haven't checked the host name, and we want to */
  269.     dir_conf = (core_dir_config *)get_module_config(dir_config, &core_module);
  270.  
  271.     if (conn->remote_host == NULL && dir_conf->hostname_lookups)
  272.     {
  273. #ifdef STATUS
  274.     int old_stat = update_child_status(conn->child_num,
  275.                         SERVER_BUSY_DNS,
  276.                         (request_rec*)NULL);
  277. #endif /* STATUS */
  278.     iaddr = &(conn->remote_addr.sin_addr);
  279.     hptr = gethostbyaddr((char *)iaddr, sizeof(struct in_addr), AF_INET);
  280.     if (hptr != NULL)
  281.     {
  282.         conn->remote_host = pstrdup(conn->pool, (void *)hptr->h_name);
  283.         str_tolower (conn->remote_host);
  284.        
  285. #ifdef MAXIMUM_DNS
  286.     /* Grrr. Check THAT name to make sure it's really the name of the addr. */
  287.     /* Code from Harald Hanche-Olsen <hanche@imf.unit.no> */
  288.  
  289.         hptr = gethostbyname(conn->remote_host);
  290.         if (hptr)
  291.         {
  292.         for(haddr=hptr->h_addr_list; *haddr; haddr++)
  293.             if(((struct in_addr *)(*haddr))->s_addr == iaddr->s_addr)
  294.             break;
  295.         }
  296.         if((!hptr) || (!(*haddr)))
  297.         conn->remote_host = NULL;
  298. #endif
  299.     }
  300. /* if failed, set it to the NULL string to indicate error */
  301.     if (conn->remote_host == NULL) conn->remote_host = "";
  302. #ifdef STATUS
  303.     (void)update_child_status(conn->child_num,old_stat,(request_rec*)NULL);
  304. #endif /* STATUS */
  305.     }
  306.  
  307. /*
  308.  * Return the desired information; either the remote DNS name, if found,
  309.  * or either NULL (if the hostname was requested) or the IP address
  310.  * (if any identifier was requested).
  311.  */
  312.     if (conn->remote_host != NULL && conn->remote_host[0] != '\0')
  313.     return conn->remote_host;
  314.     else
  315.     {
  316.     if (type == REMOTE_HOST) return NULL;
  317.     else return conn->remote_ip;
  318.     }
  319. }
  320.  
  321. const char *
  322. get_remote_logname(request_rec *r)
  323. {
  324.     core_dir_config *dir_conf;
  325.  
  326.     if (r->connection->remote_logname != NULL)
  327.     return r->connection->remote_logname;
  328.  
  329. /* If we haven't checked the identity, and we want to */
  330.     dir_conf = (core_dir_config *)
  331.     get_module_config(r->per_dir_config, &core_module);
  332.  
  333.     if (dir_conf->do_rfc1413 & 1)
  334.     return rfc1413(r->connection, r->server);
  335.     else
  336.     return NULL;
  337. }
  338.  
  339. /*****************************************************************
  340.  *
  341.  * Commands... this module handles almost all of the NCSA httpd.conf
  342.  * commands, but most of the old srm.conf is in the the modules.
  343.  */
  344.  
  345. char *set_access_name (cmd_parms *cmd, void *dummy, char *arg)
  346. {
  347.     void *sconf = cmd->server->module_config;
  348.     core_server_config *conf = get_module_config (sconf, &core_module);
  349.   
  350.     conf->access_name = arg;
  351.     return NULL;
  352. }
  353.  
  354. char *set_document_root (cmd_parms *cmd, void *dummy, char *arg)
  355. {
  356.     void *sconf = cmd->server->module_config;
  357.     core_server_config *conf = get_module_config (sconf, &core_module);
  358.   
  359.     if (!is_directory (arg))
  360.     if (cmd->server->is_virtual)
  361.         fprintf (stderr, "Warning: DocumentRoot [%s] does not exist\n", arg);
  362.     else
  363.         return "DocumentRoot must be a directory";
  364.     
  365.     conf->document_root = arg;
  366.     return NULL;
  367. }
  368.  
  369. char *set_error_document (cmd_parms *cmd, core_dir_config *conf, char *line)
  370. {
  371.     int error_number, index_number;
  372.     char *w;
  373.                 
  374.     /* 1st parameter should be a 3 digit number, which we recognize;
  375.      * convert it into an array index
  376.      */
  377.   
  378.     w = getword_conf (cmd->pool, &line);
  379.     error_number = atoi(w);
  380.     index_number = index_of_response(error_number);
  381.   
  382.     if (index_number < 0)
  383.         return pstrcat (cmd->pool, "Illegal HTTP response code ", w, NULL);
  384.                 
  385.     /* Nuke trailing '"', if present */
  386.     
  387.     if (line[strlen(line) - 1] == '"') line[strlen(line) - 1] = '\0';
  388.   
  389.     /* Store it... */
  390.  
  391.     conf->response_code_strings[index_number] = pstrdup (cmd->pool, line);
  392.  
  393.     return NULL;
  394. }
  395.  
  396. /* access.conf commands...
  397.  *
  398.  * The *only* thing that can appear in access.conf at top level is a
  399.  * <Directory> section.  NB we need to have a way to cut the srm_command_loop
  400.  * invoked by dirsection (i.e., <Directory>) short when </Directory> is seen.
  401.  * We do that by returning an error, which dirsection itself recognizes and
  402.  * discards as harmless.  Cheesy, but it works.
  403.  */
  404.  
  405. char *set_override (cmd_parms *cmd, core_dir_config *d, char *l)
  406. {
  407.     char *w;
  408.   
  409.     d->override = OR_NONE;
  410.     while(l[0]) {
  411.         w = getword_conf (cmd->pool, &l);
  412.     if(!strcasecmp(w,"Limit"))
  413.         d->override |= OR_LIMIT;
  414.     else if(!strcasecmp(w,"Options"))
  415.         d->override |= OR_OPTIONS;
  416.     else if(!strcasecmp(w,"FileInfo"))
  417.             d->override |= OR_FILEINFO;
  418.     else if(!strcasecmp(w,"AuthConfig"))
  419.         d->override |= OR_AUTHCFG;
  420.     else if(!strcasecmp(w,"Indexes"))
  421.             d->override |= OR_INDEXES;
  422.     else if(!strcasecmp(w,"None"))
  423.         d->override = OR_NONE;
  424.     else if(!strcasecmp(w,"All")) 
  425.         d->override = OR_ALL;
  426.     else 
  427.         return pstrcat (cmd->pool, "Illegal override option ", w, NULL);
  428.     }
  429.  
  430.     return NULL;
  431. }
  432.  
  433. char *set_options (cmd_parms *cmd, core_dir_config *d, char *l)
  434. {
  435.     d->opts = OPT_NONE;
  436.     while(l[0]) {
  437.         char *w = getword_conf(cmd->pool, &l);
  438.     if(!strcasecmp(w,"Indexes"))
  439.         d->opts |= OPT_INDEXES;
  440.     else if(!strcasecmp(w,"Includes"))
  441.         d->opts |= OPT_INCLUDES;
  442.     else if(!strcasecmp(w,"IncludesNOEXEC"))
  443.         d->opts |= (OPT_INCLUDES | OPT_INCNOEXEC);
  444.     else if(!strcasecmp(w,"FollowSymLinks"))
  445.         d->opts |= OPT_SYM_LINKS;
  446.     else if(!strcasecmp(w,"SymLinksIfOwnerMatch"))
  447.         d->opts |= OPT_SYM_OWNER;
  448.     else if(!strcasecmp(w,"execCGI"))
  449.         d->opts |= OPT_EXECCGI;
  450.     else if (!strcasecmp(w,"MultiViews"))
  451.         d->opts |= OPT_MULTI;
  452.     else if (!strcasecmp(w,"RunScripts")) /* AI backcompat. Yuck */
  453.         d->opts |= OPT_MULTI|OPT_EXECCGI;
  454.     else if(!strcasecmp(w,"None")) 
  455.         d->opts = OPT_NONE;
  456.     else if(!strcasecmp(w,"All")) 
  457.         d->opts = OPT_ALL;
  458.     else 
  459.         return pstrcat (cmd->pool, "Illegal option ", w, NULL);
  460.     }
  461.  
  462.     return NULL;
  463. }
  464.  
  465. char *require (cmd_parms *cmd, core_dir_config *c, char *arg)
  466. {
  467.     require_line *r;
  468.   
  469.     if (!c->requires)
  470.         c->requires = make_array (cmd->pool, 2, sizeof(require_line));
  471.     
  472.     r = (require_line *)push_array (c->requires);
  473.     r->requirement = pstrdup (cmd->pool, arg);
  474.     r->method_mask = cmd->limited;
  475.     return NULL;
  476. }
  477.  
  478. char *limit (cmd_parms *cmd, void *dummy, char *arg)
  479. {
  480.     char *limited_methods = getword(cmd->pool,&arg,'>');
  481.     int limited = 0;
  482.   
  483.     if (cmd->limited > 0) return "Can't nest <Limit> sections";
  484.     
  485.     while(limited_methods[0]) {
  486.         char *method = getword_conf (cmd->pool, &limited_methods);
  487.     if(!strcasecmp(method,"GET")) limited |= (1 << M_GET);
  488.     else if(!strcasecmp(method,"PUT")) limited |= (1 << M_PUT);
  489.     else if(!strcasecmp(method,"POST")) limited |= (1 << M_POST);
  490.     else if(!strcasecmp(method,"DELETE")) limited |= (1 << M_DELETE);
  491.         else if(!strcasecmp(method,"CONNECT")) limited |= (1 << M_CONNECT);
  492.     else return "unknown method in <Limit>";
  493.     }
  494.  
  495.     cmd->limited = limited;
  496.     return NULL;
  497. }
  498.  
  499. char *endlimit (cmd_parms *cmd, void *dummy, void *dummy2)
  500. {
  501.     if (cmd->limited == -1) return "</Limit> unexpected";
  502.     
  503.     cmd->limited = -1;
  504.     return NULL;
  505. }
  506.  
  507. static char *end_dir_magic = "</Directory> outside of any <Directory> section";
  508.  
  509. char *end_dirsection (cmd_parms *cmd, void *dummy) {
  510.     return end_dir_magic;
  511. }
  512.  
  513. char *dirsection (cmd_parms *cmd, void *dummy, char *arg)
  514. {
  515.     char *errmsg, *endp = strrchr (arg, '>');
  516.     int old_overrides = cmd->override;
  517.     char *old_path = cmd->path;
  518.     void *new_dir_conf = create_per_dir_config (cmd->pool);
  519.  
  520.     if (endp) *endp = '\0';
  521.  
  522.     if (cmd->path) return "<Directory> sections don't nest";
  523.     if (cmd->limited != -1) return "Can't have <Directory> within <Limit>";
  524.     
  525.     cmd->path = getword_conf (cmd->pool, &arg);
  526.     cmd->override = OR_ALL|ACCESS_CONF;
  527.  
  528.     errmsg = srm_command_loop (cmd, new_dir_conf);
  529.     add_per_dir_conf (cmd->server, new_dir_conf);
  530.     
  531.     cmd->path = old_path;
  532.     cmd->override = old_overrides;
  533.  
  534.     if (errmsg == end_dir_magic) return NULL;
  535.     return errmsg;
  536. }
  537.  
  538. static char *end_url_magic = "</Location> outside of any <Location> section";
  539.  
  540. char *end_urlsection (cmd_parms *cmd, void *dummy) {
  541.     return end_url_magic;
  542. }
  543.  
  544. char *urlsection (cmd_parms *cmd, void *dummy, char *arg)
  545. {
  546.     char *errmsg, *endp = strrchr (arg, '>');
  547.     int old_overrides = cmd->override;
  548.     char *old_path = cmd->path;
  549.     core_dir_config *conf;
  550.  
  551.     void *new_url_conf = create_per_dir_config (cmd->pool);
  552.  
  553.     if (endp) *endp = '\0';
  554.  
  555.     if (cmd->path) return "<Location> sections don't nest";
  556.     if (cmd->limited != -1) return "Can't have <Location> within <Limit>";
  557.     
  558.     cmd->path = getword_conf (cmd->pool, &arg);
  559.     cmd->override = OR_ALL|ACCESS_CONF;
  560.  
  561.     errmsg = srm_command_loop (cmd, new_url_conf);
  562.     if (errmsg != end_url_magic) return errmsg;
  563.  
  564.     conf = (core_dir_config *)get_module_config(new_url_conf, &core_module);
  565.     conf->d = pstrdup(cmd->pool, cmd->path);    /* No mangling, please */
  566.  
  567.     add_per_url_conf (cmd->server, new_url_conf);
  568.     
  569.     cmd->path = old_path;
  570.     cmd->override = old_overrides;
  571.  
  572.     return NULL;
  573. }
  574.  
  575. /* httpd.conf commands... beginning with the <VirtualHost> business */
  576.  
  577. char *end_virthost_magic = "</Virtualhost> out of place";
  578.  
  579. char *end_virtualhost_section (cmd_parms *cmd, void *dummy) {
  580.     return end_virthost_magic;
  581. }
  582.  
  583. char *virtualhost_section (cmd_parms *cmd, void *dummy, char *arg)
  584. {
  585.     server_rec *main_server = cmd->server, *s;
  586.     char *errmsg, *endp = strrchr (arg, '>');
  587.     pool *p = cmd->pool, *ptemp = cmd->temp_pool;
  588.  
  589.     if (endp) *endp = '\0';
  590.     
  591.     if (main_server->is_virtual)
  592.     return "<VirtualHost> doesn't nest!";
  593.     
  594.     s = init_virtual_host (p, arg);
  595.     s->next = main_server->next;
  596.     main_server->next = s;
  597.     
  598.     cmd->server = s;
  599.     errmsg = srm_command_loop (cmd, s->lookup_defaults);
  600.     cmd->server = main_server;
  601.  
  602.     if (s->srm_confname)
  603.     process_resource_config (s, s->srm_confname, p, ptemp);
  604.  
  605.     if (s->access_confname)
  606.     process_resource_config (s, s->access_confname, p, ptemp);
  607.     
  608.     if (errmsg == end_virthost_magic) return NULL;
  609.     return errmsg;
  610. }
  611.  
  612. char *set_server_string_slot (cmd_parms *cmd, void *dummy, char *arg)
  613. {
  614.     /* This one's pretty generic... */
  615.   
  616.     int offset = (int)cmd->info;
  617.     char *struct_ptr = (char *)cmd->server;
  618.     
  619.     *(char **)(struct_ptr + offset) = pstrdup (cmd->pool, arg);
  620.     return NULL;
  621. }
  622.  
  623. char *server_type (cmd_parms *cmd, void *dummy, char *arg)
  624. {
  625.     if (!strcasecmp (arg, "inetd")) standalone = 0;
  626.     else if (!strcasecmp (arg, "standalone")) standalone = 1;
  627.     else return "ServerType must be either 'inetd' or 'standalone'";
  628.  
  629.     return NULL;
  630. }
  631.  
  632. char *server_port (cmd_parms *cmd, void *dummy, char *arg) {
  633.     cmd->server->port = atoi (arg);
  634.     return NULL;
  635. }
  636.  
  637. char *set_user (cmd_parms *cmd, void *dummy, char *arg) {
  638.     user_name = pstrdup (cmd->pool, arg);
  639.     user_id = uname2id (user_name);
  640.     return NULL;
  641. }
  642.  
  643. char *set_group (cmd_parms *cmd, void *dummy, char *arg) {
  644.     group_id = gname2id(arg);
  645.     return NULL;
  646. }
  647.  
  648. char *set_server_root (cmd_parms *cmd, void *dummy, char *arg) {
  649.     if (!is_directory (arg)) return "ServerRoot must be a valid directory";
  650.     strcpy (server_root, arg);
  651.     return NULL;
  652. }
  653.  
  654. char *set_timeout (cmd_parms *cmd, void *dummy, char *arg) {
  655.     cmd->server->timeout = atoi (arg);
  656.     return NULL;
  657. }
  658.  
  659. char *set_keep_alive_timeout (cmd_parms *cmd, void *dummy, char *arg) {
  660.     cmd->server->keep_alive_timeout = atoi (arg);
  661.     return NULL;
  662. }
  663.  
  664. char *set_keep_alive (cmd_parms *cmd, void *dummy, char *arg) {
  665.     cmd->server->keep_alive = atoi (arg);
  666.     return NULL;
  667. }
  668.  
  669. char *set_pidfile (cmd_parms *cmd, void *dummy, char *arg) {
  670.     pid_fname = pstrdup (cmd->pool, arg);
  671.     return NULL;
  672. }
  673.  
  674. char *set_scoreboard (cmd_parms *cmd, void *dummy, char *arg) {
  675.     scoreboard_fname = pstrdup (cmd->pool, arg);
  676.     return NULL;
  677. }
  678.  
  679. char *set_idcheck (cmd_parms *cmd, core_dir_config *d, int arg) {
  680.     d->do_rfc1413 = arg;
  681.     return NULL;
  682. }
  683.  
  684. char *set_hostname_lookups (cmd_parms *cmd, core_dir_config *d, int arg) {
  685.     d->hostname_lookups = arg;
  686.     return NULL;
  687. }
  688.  
  689. char *set_serverpath (cmd_parms *cmd, void *dummy, char *arg) {
  690.     cmd->server->path = pstrdup (cmd->pool, arg);
  691.     cmd->server->pathlen = strlen (arg);
  692.     return NULL;
  693. }
  694.  
  695. char *set_content_md5 (cmd_parms *cmd, core_dir_config *d, int arg) {
  696.     d->content_md5 = arg;
  697.     return NULL;
  698. }
  699.  
  700. char *set_daemons_to_start (cmd_parms *cmd, void *dummy, char *arg) {
  701.     daemons_to_start = atoi (arg);
  702.     return NULL;
  703. }
  704.  
  705. char *set_min_free_servers (cmd_parms *cmd, void *dummy, char *arg) {
  706.     daemons_min_free = atoi (arg);
  707.     if (daemons_min_free <= 0) {
  708.        fprintf(stderr, "WARNING: detected MinSpareServers set to non-positive.\n");
  709.        fprintf(stderr, "Resetting to 1 to avoid almost certain Apache failure.\n");
  710.        fprintf(stderr, "Please read the documentation.\n");
  711.        daemons_min_free = 1;
  712.     }
  713.        
  714.     return NULL;
  715. }
  716.  
  717. char *set_max_free_servers (cmd_parms *cmd, void *dummy, char *arg) {
  718.     daemons_max_free = atoi (arg);
  719.     return NULL;
  720. }
  721.  
  722. char *set_server_limit (cmd_parms *cmd, void *dummy, char *arg) {
  723.     daemons_limit = atoi (arg);
  724.     if (daemons_limit > HARD_SERVER_LIMIT)
  725.         daemons_limit = HARD_SERVER_LIMIT;
  726.     return NULL;
  727. }
  728.  
  729. char *set_max_requests (cmd_parms *cmd, void *dummy, char *arg) {
  730.     max_requests_per_child = atoi (arg);
  731.     return NULL;
  732. }
  733.  
  734. char *set_bind_address (cmd_parms *cmd, void *dummy, char *arg) {
  735.     bind_address.s_addr = get_virthost_addr (arg, NULL);
  736.     return NULL;
  737. }
  738.  
  739. char *set_listener(cmd_parms *cmd, void *dummy, char *ips)
  740. {
  741.     listen_rec *new;
  742.     char *ports;
  743.  
  744.     if (cmd->server->is_virtual) return "Listen not allowed in <VirtualHost>";
  745.     ports=strchr(ips, ':');
  746.     if (ports != NULL)
  747.     {
  748.     if (ports == ips) return "Missing IP address";
  749.     else if (ports[0] == '\0')
  750.         return "Address must end in :<port-number>";
  751.     *(ports++) = '\0';
  752.     } else
  753.     ports = ips;
  754.  
  755.     new=palloc(cmd->pool, sizeof(listen_rec));
  756.     new->local_addr.sin_family = AF_INET;
  757.     if (ports == ips) /* no address */
  758.     new->local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  759.     else
  760.     new->local_addr.sin_addr.s_addr = get_virthost_addr(ips, NULL);
  761.     new->local_addr.sin_port = htons(atoi(ports));
  762.     new->next = listeners;
  763.     listeners = new;
  764.     return NULL;
  765. }
  766.  
  767. /* Note --- ErrorDocument will now work from .htaccess files.  
  768.  * The AllowOverride of Fileinfo allows webmasters to turn it off
  769.  */
  770.  
  771. command_rec core_cmds[] = {
  772.  
  773. /* Old access config file commands */
  774.  
  775. { "<Directory", dirsection, NULL, RSRC_CONF, RAW_ARGS, NULL },
  776. { "</Directory>", end_dirsection, NULL, ACCESS_CONF, NO_ARGS, NULL },
  777. { "<Location", urlsection, NULL, RSRC_CONF, RAW_ARGS, NULL },
  778. { "</Location>", end_urlsection, NULL, ACCESS_CONF, NO_ARGS, NULL },
  779. { "<Limit", limit, NULL, OR_ALL, RAW_ARGS, NULL },
  780. { "</Limit>", endlimit, NULL, OR_ALL, RAW_ARGS, NULL },
  781. { "AuthType", set_string_slot, (void*)XtOffsetOf(core_dir_config, auth_type),
  782.     OR_AUTHCFG, TAKE1, "an HTTP authorization type (e.g., \"Basic\")" },
  783. { "AuthName", set_string_slot, (void*)XtOffsetOf(core_dir_config, auth_name),
  784.     OR_AUTHCFG, RAW_ARGS, NULL },
  785. { "Require", require, NULL, OR_AUTHCFG, RAW_ARGS, NULL },
  786.     
  787. /* Old resource config file commands */
  788.   
  789. { "AccessFileName", set_access_name, NULL, RSRC_CONF, TAKE1, NULL },
  790. { "DocumentRoot", set_document_root, NULL, RSRC_CONF, TAKE1, NULL },
  791. { "ErrorDocument", set_error_document, NULL, OR_FILEINFO, RAW_ARGS, NULL },
  792. { "AllowOverride", set_override, NULL, ACCESS_CONF, RAW_ARGS, NULL },
  793. { "Options", set_options, NULL, OR_OPTIONS, RAW_ARGS, NULL },
  794. { "DefaultType", set_string_slot,
  795.     (void*)XtOffsetOf (core_dir_config, default_type),
  796.     OR_FILEINFO, TAKE1, "the default MIME type for untypable files" },
  797.  
  798. /* Old server config file commands */
  799.  
  800. { "ServerType", server_type, NULL, RSRC_CONF, TAKE1,"'inetd' or 'standalone'"},
  801. { "Port", server_port, NULL, RSRC_CONF, TAKE1, "a TCP port number"},
  802. { "HostnameLookups", set_hostname_lookups, NULL, ACCESS_CONF|RSRC_CONF, FLAG, NULL },
  803. { "User", set_user, NULL, RSRC_CONF, TAKE1, "a username"},
  804. { "Group", set_group, NULL, RSRC_CONF, TAKE1, "a group name"},
  805. { "ServerAdmin", set_server_string_slot,
  806.   (void *)XtOffsetOf (server_rec, server_admin), RSRC_CONF, TAKE1,
  807.   "The email address of the server administrator" },
  808. { "ServerName", set_server_string_slot,
  809.   (void *)XtOffsetOf (server_rec, server_hostname), RSRC_CONF, TAKE1,
  810.   "The hostname of the server" },
  811. { "ServerRoot", set_server_root, NULL, RSRC_CONF, TAKE1, "a directory"},
  812. { "ErrorLog", set_server_string_slot,
  813.   (void *)XtOffsetOf (server_rec, error_fname), RSRC_CONF, TAKE1,
  814.   "the filename of the error log" },
  815. { "PidFile", set_pidfile, NULL, RSRC_CONF, TAKE1,
  816.     "a file for logging the server process ID"},
  817. { "ScoreBoardFile", set_scoreboard, NULL, RSRC_CONF, TAKE1,
  818.     "a file for apache to maintain runtime process management information"},
  819. { "AccessConfig", set_server_string_slot,
  820.   (void *)XtOffsetOf (server_rec, access_confname), RSRC_CONF, TAKE1,
  821.   "the filename of the access config file" },
  822. { "ResourceConfig", set_server_string_slot,
  823.   (void *)XtOffsetOf (server_rec, srm_confname), RSRC_CONF, TAKE1,
  824.   "the filename of the resource config file" },
  825. { "ServerAlias", set_server_string_slot,
  826.    (void *)XtOffsetOf (server_rec, names), RSRC_CONF, RAW_ARGS,
  827.    "a name or names alternately used to access the server" },
  828. { "ServerPath", set_serverpath, NULL, RSRC_CONF, TAKE1,
  829.   "The pathname the server can be reached at" },
  830. { "Timeout", set_timeout, NULL, RSRC_CONF, TAKE1, "timeout duration (sec)"},
  831. { "KeepAliveTimeout", set_keep_alive_timeout, NULL, RSRC_CONF, TAKE1, "Keep-Alive timeout duration (sec)"},
  832. { "KeepAlive", set_keep_alive, NULL, RSRC_CONF, TAKE1, "Maximum Keep-Alive requests per connection (0 to disable)" },
  833. { "IdentityCheck", set_idcheck, NULL, RSRC_CONF|ACCESS_CONF, FLAG, NULL },
  834. { "ContentDigest", set_content_md5, NULL, RSRC_CONF|ACCESS_CONF|OR_AUTHCFG, FLAG, "whether or not to send a Content-MD5 header with each request" },
  835. { "CacheNegotiatedDocs", },
  836. { "StartServers", set_daemons_to_start, NULL, RSRC_CONF, TAKE1, NULL },
  837. { "MinSpareServers", set_min_free_servers, NULL, RSRC_CONF, TAKE1, NULL },
  838. { "MaxSpareServers", set_max_free_servers, NULL, RSRC_CONF, TAKE1, NULL },
  839. { "MaxServers", set_max_free_servers, NULL, RSRC_CONF, TAKE1, NULL },
  840. { "ServersSafetyLimit", set_server_limit, NULL, RSRC_CONF, TAKE1, NULL },
  841. { "MaxClients", set_server_limit, NULL, RSRC_CONF, TAKE1, NULL },
  842. { "MaxRequestsPerChild", set_max_requests, NULL, RSRC_CONF, TAKE1, NULL },
  843. { "BindAddress", set_bind_address, NULL, RSRC_CONF, TAKE1,
  844.   "'*', a numeric IP address, or the name of a host with a unique IP address"},
  845. { "Listen", set_listener, NULL, RSRC_CONF, TAKE1,
  846.       "a port number or a numeric IP address and a port number"},
  847. { "<VirtualHost", virtualhost_section, NULL, RSRC_CONF, RAW_ARGS, NULL },
  848. { "</VirtualHost>", end_virtualhost_section, NULL, RSRC_CONF, NO_ARGS, NULL },
  849. { NULL },
  850. };
  851.  
  852. /*****************************************************************
  853.  *
  854.  * Core handlers for various phases of server operation...
  855.  */
  856.  
  857. int core_translate (request_rec *r)
  858. {
  859.     void *sconf = r->server->module_config;
  860.     core_server_config *conf = get_module_config (sconf, &core_module);
  861.   
  862.     if (r->proxyreq) return NOT_IMPLEMENTED;
  863.     if (r->uri[0] != '/') return BAD_REQUEST;
  864.     
  865.     if (r->server->path &&
  866.     !strncmp(r->uri, r->server->path, r->server->pathlen))
  867.       r->filename = pstrcat (r->pool, conf->document_root,
  868.                  (r->uri + r->server->pathlen), NULL);
  869.     else
  870.       r->filename = pstrcat (r->pool, conf->document_root, r->uri, NULL);
  871.  
  872.     return OK;
  873. }
  874.  
  875. int do_nothing (request_rec *r) { return OK; }
  876.  
  877. /*
  878.  * Default handler for MIME types without other handlers.  Only GET
  879.  * at this point... anyone who wants to write a generic handler for
  880.  * PUT or POST is free to do so, but it seems unwise to provide any
  881.  * defaults yet...
  882.  */
  883.  
  884. int default_handler (request_rec *r)
  885. {
  886.     core_dir_config *d =
  887.       (core_dir_config *)get_module_config(r->per_dir_config, &core_module);
  888.     int errstatus;
  889.     FILE *f;
  890.     
  891.     if (r->method_number != M_GET) return DECLINED;
  892.  
  893.     if (r->finfo.st_mode == 0 || (r->path_info && *r->path_info)) {
  894.     log_reason("File does not exist", r->filename, r);
  895.     return NOT_FOUND;
  896.     }
  897.     
  898.     if ((errstatus = set_content_length (r, r->finfo.st_size))
  899.     || (errstatus = set_last_modified (r, r->finfo.st_mtime)))
  900.         return errstatus;
  901.     
  902. #ifdef __EMX__
  903.     /* Need binary mode for OS/2 */
  904.     f = fopen (r->filename, "rb");
  905. #else
  906.     f = fopen (r->filename, "r");
  907. #endif
  908.  
  909.     if (f == NULL) {
  910.         log_reason("file permissions deny server access", r->filename, r);
  911.         return FORBIDDEN;
  912.     }
  913.  
  914.     if (d->content_md5 & 1) {
  915.       table_set (r->headers_out, "Content-MD5", md5digest(r->pool, f));
  916.     }
  917.  
  918.     soft_timeout ("send", r);
  919.     
  920.     send_http_header (r);
  921.     if (!r->header_only) send_fd (f, r);
  922.     fclose (f);
  923.     return OK;
  924. }
  925.  
  926. handler_rec core_handlers[] = {
  927. { "*/*", default_handler },
  928. { NULL }
  929. };
  930.  
  931. module core_module = {
  932.    STANDARD_MODULE_STUFF,
  933.    NULL,            /* initializer */
  934.    create_core_dir_config,    /* create per-directory config structure */
  935.    merge_core_dir_configs,    /* merge per-directory config structures */
  936.    create_core_server_config,    /* create per-server config structure */
  937.    merge_core_server_configs,    /* merge per-server config structures */
  938.    core_cmds,            /* command table */
  939.    core_handlers,        /* handlers */
  940.    core_translate,        /* translate_handler */
  941.    NULL,            /* check_user_id */
  942.    NULL,            /* check auth */
  943.    do_nothing,            /* check access */
  944.    do_nothing,            /* type_checker */
  945.    NULL,            /* pre-run fixups */
  946.    NULL                /* logger */
  947. };
  948.