vhost.c revision 9c518951a46c7a12e20876827bb2e84ef87d3c11
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* ====================================================================
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * The Apache Software License, Version 1.1
bc8fd1b0b1afdf89b8d28eefa8cd74e26ba97986fielding * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * reserved.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Redistribution and use in source and binary forms, with or without
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * modification, are permitted provided that the following conditions
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 1. Redistributions of source code must retain the above copyright
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * notice, this list of conditions and the following disclaimer.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 2. Redistributions in binary form must reproduce the above copyright
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * notice, this list of conditions and the following disclaimer in
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * the documentation and/or other materials provided with the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * distribution.
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * 3. The end-user documentation included with the redistribution,
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * if any, must include the following acknowledgment:
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * "This product includes software developed by the
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * Apache Software Foundation (http://www.apache.org/)."
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * Alternately, this acknowledgment may appear in the software itself,
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * if and wherever such third-party acknowledgments normally appear.
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * 4. The names "Apache" and "Apache Software Foundation" must
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * not be used to endorse or promote products derived from this
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * software without prior written permission. For written
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * permission, please contact apache@apache.org.
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * 5. Products derived from this software may not be called "Apache",
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * nor may "Apache" appear in their name, without prior written
64185f9824e42f21ca7b9ae6c004484215c031a7rbb * permission of the Apache Software Foundation.
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * SUCH DAMAGE.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ====================================================================
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * This software consists of voluntary contributions made by many
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * individuals on behalf of the Apache Software Foundation. For more
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * information on the Apache Software Foundation, please see
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * Portions of this software are based upon public domain software
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * originally written at the National Center for Supercomputing Applications,
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * University of Illinois, Urbana-Champaign.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * http_vhost.c: functions pertaining to virtual host addresses
785be1b6298010956622771c870ab3cd8ca57a2faaron * (configuration and run-time)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * After all the definitions there's an explanation of how it's all put
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * together.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* meta-list of name-vhosts. Each server_rec can be in possibly multiple
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * lists of name-vhosts.
785be1b6298010956622771c870ab3cd8ca57a2faaron server_addr_rec *sar; /* the record causing it to be in
785be1b6298010956622771c870ab3cd8ca57a2faaron * this chain (needed for port comparisons) */
785be1b6298010956622771c870ab3cd8ca57a2faaron server_rec *server; /* the server to use on a match */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* meta-list of ip addresses. Each server_rec can be in possibly multiple
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * hash chains since it can have multiple ips.
785be1b6298010956622771c870ab3cd8ca57a2faaron server_addr_rec *sar; /* the record causing it to be in
785be1b6298010956622771c870ab3cd8ca57a2faaron * this chain (need for both ip addr and port
785be1b6298010956622771c870ab3cd8ca57a2faaron * comparisons) */
785be1b6298010956622771c870ab3cd8ca57a2faaron server_rec *server; /* the server to use if this matches */
785be1b6298010956622771c870ab3cd8ca57a2faaron name_chain *names; /* if non-NULL then a list of name-vhosts
785be1b6298010956622771c870ab3cd8ca57a2faaron * sharing this address */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm/* This defines the size of the hash apr_table_t used for hashing ip addresses
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * of virtual hosts. It must be a power of two.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* A (n) bucket hash table, each entry has a pointer to a server rec and
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * a pointer to the other entries in that bucket. Each individual address,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * even for virtualhosts with multiple addresses, has an entry in this hash
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * table. There are extra buckets for _default_, and name-vhost entries.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Note that after config time this is constant, so it is thread-safe.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic ipaddr_chain *iphash_table[IPHASH_TABLE_SIZE];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* dump out statistics about the hash function */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* #define IPHASH_STATISTICS */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* list of the _default_ servers */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* list of the NameVirtualHost addresses */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * How it's used:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * The ip address determines which chain in iphash_table is interesting, then
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * a comparison is done down that chain to find the first ipaddr_chain whose
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * sar matches the address:port pair.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * If that ipaddr_chain has names == NULL then you're done, it's an ip-vhost.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Otherwise it's a name-vhost list, and the default is the server in the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ipaddr_chain record. We tuck away the ipaddr_chain record in the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * conn_rec field vhost_lookup_data. Later on after the headers we get a
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * second chance, and we use the name_chain to figure out what name-vhost
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * matches the headers.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * If there was no ip address match in the iphash_table then do a lookup
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * in the default_list.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * How it's put together ... well you should be able to figure that out
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * from how it's used. Or something like that.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* called at the beginning of the config */
45acd673a68181802b112e97e84fa3813ddd3ec1stoddardAP_DECLARE(void) ap_init_vhost_config(apr_pool_t *p)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Parses a host of the form <address>[:port]
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * paddr is used to create a list in the order of input
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * **paddr is the ->next pointer of the last entry (or s->addrs)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * *paddr is the variable used to keep track of **paddr between calls
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * port is the default port to assume
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic const char *get_addresses(apr_pool_t *p, const char *w_,
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick /* apr_parse_addr_port() doesn't understand ":*" so handle that first. */
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames wlen = strlen(w); /* wlen must be > 0 at this point */
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick rv = apr_parse_addr_port(&host, &scope_id, &port, w, p);
560f6ac786d611b858b2bad932713d9e971f0716trawick /* If the string is "80", apr_parse_addr_port() will be happy and set
560f6ac786d611b858b2bad932713d9e971f0716trawick * host to NULL and port to 80, so watch out for that.
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick return "The address or port is invalid";
7bf77d70b6830636bc36e6b76a228c301be23ff7brianp return "Missing address for VirtualHost";
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick return "Scope ids are not supported";
39b76a07959a0a332366c735a23894d9e8ed6872trawick rv = apr_sockaddr_info_get(&my_addr, "0.0.0.0", APR_INET, port, 0, p);
39b76a07959a0a332366c735a23894d9e8ed6872trawick ap_assert(rv == APR_SUCCESS); /* must be bug or out of storage */
39b76a07959a0a332366c735a23894d9e8ed6872trawick rv = apr_sockaddr_info_get(&my_addr, "255.255.255.255", APR_INET, port, 0, p);
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick ap_assert(rv == APR_SUCCESS); /* must be bug or out of storage */
066877f1a045103acfdd376d48cdd473c33f409bdougm rv = apr_sockaddr_info_get(&my_addr, host, APR_UNSPEC, port, 0, p);
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick /* XXX Gotta go through *all* addresses for the host name!
066877f1a045103acfdd376d48cdd473c33f409bdougm * Fix apr_sockaddr_info_get() to save them! */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* parse the <VirtualHost> addresses */
785be1b6298010956622771c870ab3cd8ca57a2faaron const char *hostname,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *err;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* start the list of addreses */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (hostname[0]) {
785be1b6298010956622771c870ab3cd8ca57a2faaron err = get_addresses(p, ap_getword_conf(p, &hostname), &addrs, s->port);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* terminate the list */
785be1b6298010956622771c870ab3cd8ca57a2faaron /* override the default port which is inherited from main_server */
785be1b6298010956622771c870ab3cd8ca57a2faaronconst char *ap_set_name_virtual_host(cmd_parms *cmd, void *dummy,
785be1b6298010956622771c870ab3cd8ca57a2faaron const char *arg)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* use whatever port the main server has at this point */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return get_addresses(cmd->pool, arg, &name_vhost_list_tail,
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm/* hash apr_table_t statistics, keep this in here for the beta period so
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * we can find out if the hash function is ok
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int iphash_compare(const void *a, const void *b)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return (*(const int *) b - *(const int *) a);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic void dump_iphash_statistics(server_rec *main_s)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* don't count the slop buckets in the total */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding qsort(count, IPHASH_TABLE_SIZE, sizeof(count[0]), iphash_compare);
785be1b6298010956622771c870ab3cd8ca57a2faaron "iphash: total hashed = %u, avg chain = %u, "
785be1b6298010956622771c870ab3cd8ca57a2faaron "chain lengths (count x len):",
785be1b6298010956622771c870ab3cd8ca57a2faaron p += apr_snprintf(p, sizeof(buf) - (p - buf), " %ux%u",
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm p += apr_snprintf(p, sizeof(buf) - (p - buf), " %ux%u",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, main_s, buf);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* This hashing function is designed to get good distribution in the cases
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * where the server is handling entire "networks" of servers. i.e. a
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * whack of /24s. This is probably the most common configuration for
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ISPs with large virtual servers.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * NOTE: This function is symmetric (i.e. collapses all 4 octets
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * into one), so machine byte order (big/little endianness) does not matter.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Hash function provided by David Hankins.
a7ed9c525f9460187f327cea953bf90ecf1bdc51gsteinstatic APR_INLINE unsigned hash_addr(struct apr_sockaddr_t *sa)
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick /* The key is the last four bytes of the IP address.
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick * For IPv4, this is the entire address, as always.
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick * For IPv6, this is usually part of the MAC address.
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick key = *(unsigned *)((char *)sa->ipaddr_ptr + sa->ipaddr_len - 4);
a7ed9c525f9460187f327cea953bf90ecf1bdc51gsteinstatic APR_INLINE ipaddr_chain *find_ipaddr(apr_sockaddr_t *sa)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* scan the hash apr_table_t for an exact match first */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (trav = iphash_table[bucket]; trav; trav = trav->next) {
39b76a07959a0a332366c735a23894d9e8ed6872trawick if (cur->port == 0 || sa->port == 0 || cur->port == sa->port) {
8f8ec0957334f50b7ac11359f90490ee467258eedreidstatic ipaddr_chain *find_default_server(apr_port_t port)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (trav = default_list; trav; trav = trav->next) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (sar->host_port == 0 || sar->host_port == port) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* match! */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanfstatic void dump_a_vhost(apr_file_t *f, ipaddr_chain *ic)
785be1b6298010956622771c870ab3cd8ca57a2faaron "%8s default server %s (%s:%u)\n",
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_file_printf(f, "%8s port %u ", "", nc->sar->host_port);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_file_printf(f, "wildcard NameVirtualHosts and _default_ servers:\n");
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * Two helper functions for ap_fini_vhost_config()
785be1b6298010956622771c870ab3cd8ca57a2faaronstatic int add_name_vhost_config(apr_pool_t *p, server_rec *main_s,
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /* the first time we encounter a NameVirtualHost address
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * ic->server will be NULL, on subsequent encounters
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * ic->names will be non-NULL.
785be1b6298010956622771c870ab3cd8ca57a2faaron /* one of the two is a * port, the other isn't */
785be1b6298010956622771c870ab3cd8ca57a2faaron ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, main_s,
785be1b6298010956622771c870ab3cd8ca57a2faaron "VirtualHost %s:%u -- mixing * "
785be1b6298010956622771c870ab3cd8ca57a2faaron "ports and non-* ports with "
785be1b6298010956622771c870ab3cd8ca57a2faaron "a NameVirtualHost address is not supported,"
785be1b6298010956622771c870ab3cd8ca57a2faaron " proceeding with undefined results",
785be1b6298010956622771c870ab3cd8ca57a2faaron /* IP-based vhosts are handled by the caller */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanfstatic void remove_unused_name_vhosts(server_rec *main_s, ipaddr_chain **pic)
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf while (*pic) {
785be1b6298010956622771c870ab3cd8ca57a2faaron ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, main_s,
785be1b6298010956622771c870ab3cd8ca57a2faaron "NameVirtualHost %s:%u has no VirtualHosts",
785be1b6298010956622771c870ab3cd8ca57a2faaron /* if server != NULL and names == NULL then we're done
785be1b6298010956622771c870ab3cd8ca57a2faaron * looking at NameVirtualHosts
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* compile the tables and such we need to do the run-time vhost lookups */
ebc18d48bea83ee5ed7a1b4e30007e5192539829wroweAP_DECLARE(void) ap_fini_vhost_config(apr_pool_t *p, server_rec *main_s)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ipaddr_chain **iphash_table_tail[IPHASH_TABLE_SIZE];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* terminate the name_vhost list */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Main host first */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* initialize the tails */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* The first things to go into the hash apr_table_t are the NameVirtualHosts
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Since name_vhost_list is in the same order that the directives
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * occured in the config file, we'll copy it in that order.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (sar = name_vhost_list; sar; sar = sar->next) {
5f08a022a210f4e511561e89f500621a15e6177dtrawick char inaddr_any[16] = {0}; /* big enough to handle IPv4 or IPv6 */
5f08a022a210f4e511561e89f500621a15e6177dtrawick sar->host_addr->ipaddr_len)) { /* not IN[6]ADDR_ANY */
785be1b6298010956622771c870ab3cd8ca57a2faaron /* A wildcard NameVirtualHost goes on the default_list so
785be1b6298010956622771c870ab3cd8ca57a2faaron * that it can catch incoming requests on any address.
785be1b6298010956622771c870ab3cd8ca57a2faaron /* Notice that what we've done is insert an ipaddr_chain with
785be1b6298010956622771c870ab3cd8ca57a2faaron * both server and names NULL. This fact is used to spot name-
785be1b6298010956622771c870ab3cd8ca57a2faaron * based vhosts in add_name_vhost_config().
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* The next things to go into the hash apr_table_t are the virtual hosts
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * themselves. They're listed off of main_s->next in the reverse
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * order they occured in the config file, so we insert them at
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * the iphash_table_tail but don't advance the tail.
900127764fb985c340ee4979cac97146a330c694trawick char inaddr_any[16] = {0}; /* big enough to handle IPv4 or IPv6 */
900127764fb985c340ee4979cac97146a330c694trawick sar->host_addr->sa.sin.sin_addr.s_addr == DEFAULT_VHOST_ADDR)
900127764fb985c340ee4979cac97146a330c694trawick || !memcmp(sar->host_addr->ipaddr_ptr, inaddr_any, sar->host_addr->ipaddr_len)) {
785be1b6298010956622771c870ab3cd8ca57a2faaron if (!ic || !add_name_vhost_config(p, main_s, s, sar, ic)) {
785be1b6298010956622771c870ab3cd8ca57a2faaron "overlap on port %u, the first has "
785be1b6298010956622771c870ab3cd8ca57a2faaron /* see if it matches something we've already got */
785be1b6298010956622771c870ab3cd8ca57a2faaron else if (!add_name_vhost_config(p, main_s, s, sar, ic)) {
785be1b6298010956622771c870ab3cd8ca57a2faaron "with VirtualHost %s:%u, the first has "
785be1b6298010956622771c870ab3cd8ca57a2faaron "precedence, perhaps you need a "
785be1b6298010956622771c870ab3cd8ca57a2faaron "NameVirtualHost directive",
785be1b6298010956622771c870ab3cd8ca57a2faaron /* Ok now we want to set up a server_hostname if the user was
785be1b6298010956622771c870ab3cd8ca57a2faaron * silly enough to forget one.
785be1b6298010956622771c870ab3cd8ca57a2faaron * XXX: This is silly we should just crash and burn.
785be1b6298010956622771c870ab3cd8ca57a2faaron else if (!s->addrs) {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* what else can we do? at this point this vhost has
785be1b6298010956622771c870ab3cd8ca57a2faaron no configured name, probably because they used
785be1b6298010956622771c870ab3cd8ca57a2faaron DNS in the VirtualHost statement. It's disabled
785be1b6298010956622771c870ab3cd8ca57a2faaron anyhow by the host matching code. -djg */
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick rv = apr_getnameinfo(&hostname, s->addrs->host_addr, 0);
785be1b6298010956622771c870ab3cd8ca57a2faaron /* again, what can we do? They didn't specify a
785be1b6298010956622771c870ab3cd8ca57a2faaron ServerName, and their DNS isn't working. -djg */
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_sockaddr_ip_get(&ipaddr_str, s->addrs->host_addr);
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick "Failed to resolve server name "
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick "for %s (check DNS) -- or specify an explicit "
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick "ServerName",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* now go through and delete any NameVirtualHosts that didn't have any
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * hosts associated with them. Lamers.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*****************************************************************************
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * run-time vhost matching functions
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf/* Lowercase and remove any trailing dot and/or :port from the hostname,
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf * and check that it is sane.
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf * In most configurations the exact syntax of the hostname isn't
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf * important so strict sanity checking isn't necessary. However, in
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf * mass hosting setups (using mod_vhost_alias or mod_rewrite) where
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf * the hostname is interpolated into the filename, we need to be sure
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf * that the interpolation doesn't expose parts of the filesystem.
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf * We don't do strict RFC 952 / RFC 1123 syntax checking in order
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf * to support iDNS and people who erroneously use underscores.
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf * Instead we just check for filesystem metacharacters: directory
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf * separators / and \ and sequences of more than one dot.
9c518951a46c7a12e20876827bb2e84ef87d3c11jerenkrantz /* According to RFC 2616, Host header field CAN be blank. */
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick rv = apr_parse_addr_port(&host, &scope_id, &port, r->hostname, r->pool);
0db1b9810f06c0e3c537e0e0dfbc30160c308526trawick /* silly looking host ("Host: 123") but that isn't our job
0db1b9810f06c0e3c537e0e0dfbc30160c308526trawick * here to judge; apr_parse_addr_port() would think we had a port
0db1b9810f06c0e3c537e0e0dfbc30160c308526trawick * but no address
644be6f54749d2d9950d2c4d2ac448f7af016d26martin else if (port) {
644be6f54749d2d9950d2c4d2ac448f7af016d26martin /* Don't throw the Host: header's port number away:
644be6f54749d2d9950d2c4d2ac448f7af016d26martin save it in parsed_uri -- ap_get_server_port() needs it! */
644be6f54749d2d9950d2c4d2ac448f7af016d26martin /* @@@ XXX there should be a better way to pass the port.
644be6f54749d2d9950d2c4d2ac448f7af016d26martin * Like r->hostname, there should be a r->portno
644be6f54749d2d9950d2c4d2ac448f7af016d26martin r->parsed_uri.port_str = apr_psprintf(r->pool, "%d", (int)port);
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick /* if the hostname is an IPv6 numeric address string, it was validated
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick * already; otherwise, further validation is needed
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick /* strip trailing gubbins */
5e4791fd54c10fdd77ca81bee8ac8398f16d6d6astoddard ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
785be1b6298010956622771c870ab3cd8ca57a2faaron "Client sent malformed Host header");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* return 1 if host matches ServerName or ServerAliases */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int matches_aliases(server_rec *s, const char *host)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* match ServerName */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* search all the aliases from ServerAlias directive */
785be1b6298010956622771c870ab3cd8ca57a2faaron if(!name[i]) continue;
785be1b6298010956622771c870ab3cd8ca57a2faaron if(!name[i]) continue;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* Suppose a request came in on the same socket as this r, and included
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * a header "Host: host:port", would it map to r->server? It's more
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * than just that though. When we do the normal matches for each request
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * we don't even bother considering Host: etc on non-namevirtualhosts,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * we just call it a match. But here we require the host:port to match
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * the ServerName and/or ServerAliases.
3d96ee83babeec32482c9082c9426340cee8c44dwroweAP_DECLARE(int) ap_matches_request_vhost(request_rec *r, const char *host,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* search all the <VirtualHost> values */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* XXX: If this is a NameVirtualHost then we may not be doing the Right Thing
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * consider:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * NameVirtualHost 10.1.1.1
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * <VirtualHost 10.1.1.1>
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ServerName v1
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * </VirtualHost>
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * <VirtualHost 10.1.1.1>
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ServerName v2
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * </VirtualHost>
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Suppose r->server is v2, and we're asked to match "10.1.1.1". We'll say
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * "yup it's v2", when really it isn't... if a request came in for 10.1.1.1
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * it would really go to v1.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* the Port has to match now, because the rest don't have ports associated
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * with them. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Even if the request has a Host: header containing a port we ignore
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * that port. We always use the physical port of the socket. There
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * are a few reasons for this:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * - the default of 80 or 443 for SSL is easier to handle this way
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * - there is less of a possibility of a security problem
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * - it simplifies the data structure
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * - the client may have no idea that a proxy somewhere along the way
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * translated the request to another ip:port
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * - except for the addresses from the VirtualHost line, none of the other
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * names we'll match have ports associated with them
e6cc28a5eb3371ba0c38e941855e71ff0054f50erbb apr_sockaddr_port_get(&port, r->connection->local_addr);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Recall that the name_chain is a list of server_addr_recs, some of
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * whose ports may not match. Also each server may appear more than
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * once in the chain -- specifically, it will appear once for each
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * address from its VirtualHost line which matched. We only want to
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * do the full ServerName/ServerAlias comparisons once for each
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * server, fortunately we know that all the VirtualHost addresses for
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * a single server are adjacent to each other.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (src = r->connection->vhost_lookup_data; src; src = src->next) {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* We only consider addresses on the name_chain which have a matching
785be1b6298010956622771c870ab3cd8ca57a2faaron /* does it match the virthost from the sar? */
785be1b6298010956622771c870ab3cd8ca57a2faaron if (s == last_s) {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* we've already done ServerName and ServerAlias checks for this
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* s is the first matching server, we're done */
e6cc28a5eb3371ba0c38e941855e71ff0054f50erbb apr_sockaddr_port_get(&port, r->connection->local_addr);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * This is in conjunction with the ServerPath code in http_core, so we
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * get the right host attached to a non- Host-sending request.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * See the comment in check_hostalias about how each vhost can be
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * listed multiple times.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (src = r->connection->vhost_lookup_data; src; src = src->next) {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* We only consider addresses on the name_chain which have a matching
785be1b6298010956622771c870ab3cd8ca57a2faaron if (src->sar->host_port != 0 && port != src->sar->host_port) {
785be1b6298010956622771c870ab3cd8ca57a2faaron if (s == last_s) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (s->path && !strncmp(r->uri, s->path, s->pathlen) &&
45acd673a68181802b112e97e84fa3813ddd3ec1stoddardAP_DECLARE(void) ap_update_vhost_from_headers(request_rec *r)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* must set this for HTTP/1.1 support */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if (r->hostname || (r->hostname = apr_table_get(r->headers_in, "Host"))) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* check if we tucked away a name_chain */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* Called for a new connection which has a known local_addr. Note that the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * new connection is assumed to have conn->server == main server.
45acd673a68181802b112e97e84fa3813ddd3ec1stoddardAP_DECLARE(void) ap_update_vhost_given_ip(conn_rec *conn)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* scan the hash apr_table_t for an exact match first */
785be1b6298010956622771c870ab3cd8ca57a2faaron /* save the name_chain for later in case this is a name-vhost */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /* maybe there's a default server or wildcard name-based vhost
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * matching this port
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /* otherwise we're stuck with just the main server
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * and no name-based vhosts