vhost.c revision 2d71630471d1c23f0137309e3c3957c633ecbfd6
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* ====================================================================
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Copyright (c) 1995-1999 The Apache Group. All rights 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
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 3. All advertising materials mentioning features or use of this
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * software must display the following acknowledgment:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * "This product includes software developed by the Apache Group
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * for use in the Apache HTTP server project (http://www.apache.org/)."
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 4. The names "Apache Server" and "Apache Group" must not be used to
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * endorse or promote products derived from this software without
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * prior written permission. For written permission, please contact
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * apache@apache.org.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 5. Products derived from this software may not be called "Apache"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * nor may "Apache" appear in their names without prior written
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * permission of the Apache Group.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 6. Redistributions of any form whatsoever must retain the following
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * acknowledgment:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * "This product includes software developed by the Apache Group
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * for use in the Apache HTTP server project (http://www.apache.org/)."
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * OF THE POSSIBILITY OF SUCH DAMAGE.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ====================================================================
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * This software consists of voluntary contributions made by many
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * individuals on behalf of the Apache Group and was originally based
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * on public domain software written at the National Center for
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Supercomputing Applications, University of Illinois, Urbana-Champaign.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * For more information on the Apache Group and the Apache HTTP server
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * project, please see <http://www.apache.org/>.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * http_vhost.c: functions pertaining to virtual host addresses
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * (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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_addr_rec *sar; /* the record causing it to be in
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * this chain (needed for port comparisons) */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding 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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_addr_rec *sar; /* the record causing it to be in
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * this chain (need for both ip addr and port
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * comparisons) */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_rec *server; /* the server to use if this matches */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding name_chain *names; /* if non-NULL then a list of name-vhosts
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * sharing this address */
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbb/* This defines the size of the hash ap_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 */
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
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbbstatic const char *get_addresses(ap_context_t *p, char *w, server_addr_rec ***paddr,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding unsigned long my_addr;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (*w == 0)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return ":port must be numeric";
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if ((my_addr = ap_inet_addr(w)) != INADDR_NONE) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if ((!hep) || (hep->h_addrtype != AF_INET || !hep->h_addr_list[0])) {
0942697a9b5de44865676345a3828741c827efe6rbb ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, NULL,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "Cannot resolve host name %s --- ignoring!", w);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding sar->host_addr = *(struct in_addr *) hep->h_addr_list[i];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* parse the <VirtualHost> addresses */
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbbconst char *ap_parse_vhost_addrs(ap_context_t *p, const char *hostname, server_rec *s)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *err;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* start the list of addreses */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (hostname[0]) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding err = get_addresses(p, ap_getword_conf(p, &hostname), &addrs, s->port);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* terminate the list */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* override the default port which is inherited from main_server */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingconst char *ap_set_name_virtual_host (cmd_parms *cmd, void *dummy, char *arg)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* use whatever port the main server has at this point */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return get_addresses(cmd->pool, arg, &name_vhost_list_tail,
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbb/* hash ap_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) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (src = iphash_table[i]; src; src = src->next) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* don't count the slop buckets in the total */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding qsort(count, IPHASH_TABLE_SIZE, sizeof(count[0]), iphash_compare);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "iphash: total hashed = %u, avg chain = %u, "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "chain lengths (count x len):",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding p += ap_snprintf(p, sizeof(buf) - (p - buf), " %ux%u",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding p += ap_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.
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbbstatic name_chain *new_name_chain(ap_context_t *p, server_rec *s, server_addr_rec *sar)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic ap_inline ipaddr_chain *find_ipaddr(struct in_addr *server_ip,
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbb /* scan the hash ap_table_t for an exact match first */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (trav = iphash_table[bucket]; trav; trav = trav->next) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding || port == 0)) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic ipaddr_chain *find_default_server(unsigned port)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (trav = default_list; trav; trav = trav->next) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (sar->host_port == 0 || sar->host_port == port) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* match! */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_snprintf(buf, sizeof(buf), "%pA:*", &ic->sar->host_addr);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_snprintf(buf, sizeof(buf), "%pA:%u", &ic->sar->host_addr,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ic->server->server_hostname, ic->server->defn_name,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "%22s default server %s (%s:%u)\n",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ic->server->defn_name, ic->server->defn_line_number);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ic->server->server_hostname, ic->server->defn_name,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* compile the tables and such we need to do the run-time vhost lookups */
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbbvoid ap_fini_vhost_config(ap_context_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) {
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbb /* The first things to go into the hash ap_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) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding unsigned bucket = hash_inaddr(sar->host_addr.s_addr);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ipaddr_chain *new = new_ipaddr_chain(p, NULL, sar);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Notice that what we've done is insert an ipaddr_chain with
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * both server and names NULL. Remember that.
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbb /* The next things to go into the hash ap_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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* add it to default bucket for each appropriate sar
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * since we need to do a port test
0942697a9b5de44865676345a3828741c827efe6rbb ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, main_s,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "_default_ VirtualHost overlap on port %u,"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* see if it matches something we've already got */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* the first time we encounter a NameVirtualHost address
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ic->server will be NULL, on subsequent encounters
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * ic->names will be non-NULL.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* one of the two is a * port, the other isn't */
0942697a9b5de44865676345a3828741c827efe6rbb ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, main_s,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "VirtualHost %s:%u -- mixing * "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "ports and non-* ports with "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "a NameVirtualHost address is not supported,"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding " proceeding with undefined results",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if (ic) {
0942697a9b5de44865676345a3828741c827efe6rbb ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, main_s,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "VirtualHost %s:%u overlaps with "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "VirtualHost %s:%u, the first has precedence, "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "perhaps you need a NameVirtualHost directive",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding unsigned bucket = hash_inaddr(sar->host_addr.s_addr);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Ok now we want to set up a server_hostname if the user was
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * silly enough to forget one.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * XXX: This is silly we should just crash and burn.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else if (!s->addrs) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* what else can we do? at this point this vhost has
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding no configured name, probably because they used
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding DNS in the VirtualHost statement. It's disabled
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding anyhow by the host matching code. -djg */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if ((h = gethostbyaddr((char *) &(s->addrs->host_addr),
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding s->server_hostname = ap_pstrdup(p, (char *) h->h_name);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* again, what can we do? They didn't specify a
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ServerName, and their DNS isn't working. -djg */
0942697a9b5de44865676345a3828741c827efe6rbb ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, main_s,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "Failed to resolve server name "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "for %s (check DNS) -- or specify an explicit "
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "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) {
0942697a9b5de44865676345a3828741c827efe6rbb ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, main_s,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding "NameVirtualHost %s:%u has no VirtualHosts",
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* if server != NULL and names == NULL then we're done
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * looking at NameVirtualHosts
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.
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf char *host = ap_palloc(r->pool, strlen(r->hostname) + 1);
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf const char *src;
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf /* check and copy the host part */
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf while (*src) {
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf /* check the port part */
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf while (*src) {
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf /* strip trailing gubbins */
5e4791fd54c10fdd77ca81bee8ac8398f16d6d6astoddard ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf "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 */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if(!name[i]) continue;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding 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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingAPI_EXPORT(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 if ((sar->host_port == 0 || port == sar->host_port)
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
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding unsigned port = ntohs(r->connection->local_addr.sin_port);
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) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* We only consider addresses on the name_chain which have a matching
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (sar->host_port != 0 && port != sar->host_port) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* does it match the virthost from the sar? */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* we've already done ServerName and ServerAlias checks for this
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* s is the first matching server, we're done */
061c3874622b67beff724876b9bd1b1f291f2eaafielding /* use AP func here. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding unsigned port = ntohs(r->connection->local_addr.sin_port);
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) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* We only consider addresses on the name_chain which have a matching
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (src->sar->host_port != 0 && port != src->sar->host_port) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (s->path && !strncmp(r->uri, s->path, s->pathlen) &&
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* must set this for HTTP/1.1 support */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (r->hostname || (r->hostname = ap_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.
b4c8a80f7dbfc9b56dbe03bdc28f0b5eb5f23697rbb /* scan the hash ap_table_t for an exact match first */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding trav = find_ipaddr(&conn->local_addr.sin_addr, port);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* save the name_chain for later in case this is a name-vhost */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* There's certainly no name-vhosts with this address, they would have
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * been matched above.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* maybe there's a default server matching this port */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* otherwise we're stuck with just the main server */