vhost.c revision 9c518951a46c7a12e20876827bb2e84ef87d3c11
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* ====================================================================
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * The Apache Software License, Version 1.1
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding *
bc8fd1b0b1afdf89b8d28eefa8cd74e26ba97986fielding * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * reserved.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Redistribution and use in source and binary forms, with or without
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * modification, are permitted provided that the following conditions
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * are met:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * 1. Redistributions of source code must retain the above copyright
7e79e8fd53348f9fc6e8009a4a2522425ab6f08ffielding * notice, this list of conditions and the following disclaimer.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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 *
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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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 *
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 * <http://www.apache.org/>.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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 */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * http_vhost.c: functions pertaining to virtual host addresses
785be1b6298010956622771c870ab3cd8ca57a2faaron * (configuration and run-time)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "apr.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "apr_strings.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "apr_lib.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#define APR_WANT_STRFUNC
1b21d7b3d97def358b2e923655edeb16613a1c31gstein#include "apr_want.h"
1b21d7b3d97def358b2e923655edeb16613a1c31gstein
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#define CORE_PRIVATE
2d71630471d1c23f0137309e3c3957c633ecbfd6rbb#include "ap_config.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "httpd.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "http_config.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "http_log.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "http_vhost.h"
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include "http_protocol.h"
51af95bb51b5084e883bad250b2afa2838e9ceebfielding#include "http_core.h"
d4f1d9c1ff112a8ab9bee31f196973761329b236rbb
7fae9cc4639013f3c04c085547256c68814aee8ftrawick#if APR_HAVE_ARPA_INET_H
7fae9cc4639013f3c04c085547256c68814aee8ftrawick#include <arpa/inet.h>
7fae9cc4639013f3c04c085547256c68814aee8ftrawick#endif
7fae9cc4639013f3c04c085547256c68814aee8ftrawick
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * After all the definitions there's an explanation of how it's all put
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * together.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* meta-list of name-vhosts. Each server_rec can be in possibly multiple
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * lists of name-vhosts.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingtypedef struct name_chain name_chain;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstruct name_chain {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding name_chain *next;
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};
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* meta-list of ip addresses. Each server_rec can be in possibly multiple
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * hash chains since it can have multiple ips.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingtypedef struct ipaddr_chain ipaddr_chain;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstruct ipaddr_chain {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ipaddr_chain *next;
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 */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding};
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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 */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#ifndef IPHASH_TABLE_SIZE
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#define IPHASH_TABLE_SIZE 256
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#endif
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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 *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Note that after config time this is constant, so it is thread-safe.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic ipaddr_chain *iphash_table[IPHASH_TABLE_SIZE];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* dump out statistics about the hash function */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* #define IPHASH_STATISTICS */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* list of the _default_ servers */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic ipaddr_chain *default_list;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* list of the NameVirtualHost addresses */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic server_addr_rec *name_vhost_list;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic server_addr_rec **name_vhost_list_tail;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * How it's used:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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 *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * If that ipaddr_chain has names == NULL then you're done, it's an ip-vhost.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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 *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * If there was no ip address match in the iphash_table then do a lookup
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * in the default_list.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
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 */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* called at the beginning of the config */
45acd673a68181802b112e97e84fa3813ddd3ec1stoddardAP_DECLARE(void) ap_init_vhost_config(apr_pool_t *p)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding memset(iphash_table, 0, sizeof(iphash_table));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding default_list = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding name_vhost_list = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding name_vhost_list_tail = &name_vhost_list;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*
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
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic const char *get_addresses(apr_pool_t *p, const char *w_,
785be1b6298010956622771c870ab3cd8ca57a2faaron server_addr_rec ***paddr,
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick apr_port_t default_port)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick apr_sockaddr_t *my_addr;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_addr_rec *sar;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick char *w, *host, *scope_id;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick int wild_port;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick size_t wlen;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick apr_port_t port;
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick apr_status_t rv;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick if (*w_ == '\0')
785be1b6298010956622771c870ab3cd8ca57a2faaron return NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick w = apr_pstrdup(p, w_);
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick /* apr_parse_addr_port() doesn't understand ":*" so handle that first. */
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames wlen = strlen(w); /* wlen must be > 0 at this point */
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames wild_port = 0;
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames if (w[wlen - 1] == '*') {
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames if (wlen < 2) {
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames wild_port = 1;
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames }
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames else if (w[wlen - 2] == ':') {
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames w[wlen - 2] = '\0';
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames wild_port = 1;
e160b861b50a3a8dcc013b8cd3ef849fe777e52fgregames }
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick }
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.
560f6ac786d611b858b2bad932713d9e971f0716trawick */
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick if (rv != APR_SUCCESS) {
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick return "The address or port is invalid";
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick }
7bf77d70b6830636bc36e6b76a228c301be23ff7brianp if (!host) {
7bf77d70b6830636bc36e6b76a228c301be23ff7brianp return "Missing address for VirtualHost";
7bf77d70b6830636bc36e6b76a228c301be23ff7brianp }
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick if (scope_id) {
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick return "Scope ids are not supported";
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick }
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick if (!port && !wild_port) {
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick port = default_port;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
eae32ab3fb398ca408bc2d45b22adf1b67a75471rbb if (strcmp(host, "*") == 0) {
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 */
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron else if (strcasecmp(host, "_default_") == 0
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick || strcmp(host, "255.255.255.255") == 0) {
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 */
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron else {
066877f1a045103acfdd376d48cdd473c33f409bdougm rv = apr_sockaddr_info_get(&my_addr, host, APR_UNSPEC, port, 0, p);
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick if (rv != APR_SUCCESS) {
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick "Cannot resolve host name %s --- ignoring!", host);
74b39333600dee3260355ad3a06e36ef6c61c8f1dreid return NULL;
74b39333600dee3260355ad3a06e36ef6c61c8f1dreid }
74b39333600dee3260355ad3a06e36ef6c61c8f1dreid }
74b39333600dee3260355ad3a06e36ef6c61c8f1dreid
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick /* XXX Gotta go through *all* addresses for the host name!
066877f1a045103acfdd376d48cdd473c33f409bdougm * Fix apr_sockaddr_info_get() to save them! */
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick
d17890657bc529b3f9db20e5546511182b829565dreid sar = apr_pcalloc(p, sizeof(server_addr_rec));
d17890657bc529b3f9db20e5546511182b829565dreid **paddr = sar;
d17890657bc529b3f9db20e5546511182b829565dreid *paddr = &sar->next;
d17890657bc529b3f9db20e5546511182b829565dreid sar->host_addr = my_addr;
d17890657bc529b3f9db20e5546511182b829565dreid sar->host_port = port;
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick sar->virthost = host;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* parse the <VirtualHost> addresses */
785be1b6298010956622771c870ab3cd8ca57a2faaronconst char *ap_parse_vhost_addrs(apr_pool_t *p,
785be1b6298010956622771c870ab3cd8ca57a2faaron const char *hostname,
785be1b6298010956622771c870ab3cd8ca57a2faaron server_rec *s)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_addr_rec **addrs;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *err;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* start the list of addreses */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding addrs = &s->addrs;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding while (hostname[0]) {
785be1b6298010956622771c870ab3cd8ca57a2faaron err = get_addresses(p, ap_getword_conf(p, &hostname), &addrs, s->port);
785be1b6298010956622771c870ab3cd8ca57a2faaron if (err) {
785be1b6298010956622771c870ab3cd8ca57a2faaron *addrs = NULL;
785be1b6298010956622771c870ab3cd8ca57a2faaron return err;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* terminate the list */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *addrs = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (s->addrs) {
785be1b6298010956622771c870ab3cd8ca57a2faaron if (s->addrs->host_port) {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* override the default port which is inherited from main_server */
785be1b6298010956622771c870ab3cd8ca57a2faaron s->port = s->addrs->host_port;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
785be1b6298010956622771c870ab3cd8ca57a2faaronconst char *ap_set_name_virtual_host(cmd_parms *cmd, void *dummy,
785be1b6298010956622771c870ab3cd8ca57a2faaron const char *arg)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* use whatever port the main server has at this point */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return get_addresses(cmd->pool, arg, &name_vhost_list_tail,
785be1b6298010956622771c870ab3cd8ca57a2faaron cmd->server->port);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#ifdef IPHASH_STATISTICS
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int iphash_compare(const void *a, const void *b)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return (*(const int *) b - *(const int *) a);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic void dump_iphash_statistics(server_rec *main_s)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding unsigned count[IPHASH_TABLE_SIZE];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int i;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ipaddr_chain *src;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding unsigned total;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char buf[HUGE_STRING_LEN];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char *p;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding total = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
785be1b6298010956622771c870ab3cd8ca57a2faaron count[i] = 0;
785be1b6298010956622771c870ab3cd8ca57a2faaron for (src = iphash_table[i]; src; src = src->next) {
785be1b6298010956622771c870ab3cd8ca57a2faaron ++count[i];
785be1b6298010956622771c870ab3cd8ca57a2faaron if (i < IPHASH_TABLE_SIZE) {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* don't count the slop buckets in the total */
785be1b6298010956622771c870ab3cd8ca57a2faaron ++total;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding qsort(count, IPHASH_TABLE_SIZE, sizeof(count[0]), iphash_compare);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm p = buf + apr_snprintf(buf, sizeof(buf),
785be1b6298010956622771c870ab3cd8ca57a2faaron "iphash: total hashed = %u, avg chain = %u, "
785be1b6298010956622771c870ab3cd8ca57a2faaron "chain lengths (count x len):",
785be1b6298010956622771c870ab3cd8ca57a2faaron total, total / IPHASH_TABLE_SIZE);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding total = 1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 1; i < IPHASH_TABLE_SIZE; ++i) {
785be1b6298010956622771c870ab3cd8ca57a2faaron if (count[i - 1] != count[i]) {
785be1b6298010956622771c870ab3cd8ca57a2faaron p += apr_snprintf(p, sizeof(buf) - (p - buf), " %ux%u",
785be1b6298010956622771c870ab3cd8ca57a2faaron total, count[i - 1]);
785be1b6298010956622771c870ab3cd8ca57a2faaron total = 1;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron else {
785be1b6298010956622771c870ab3cd8ca57a2faaron ++total;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm p += apr_snprintf(p, sizeof(buf) - (p - buf), " %ux%u",
785be1b6298010956622771c870ab3cd8ca57a2faaron total, count[IPHASH_TABLE_SIZE - 1]);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, main_s, buf);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#endif
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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 *
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 *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Hash function provided by David Hankins.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
a7ed9c525f9460187f327cea953bf90ecf1bdc51gsteinstatic APR_INLINE unsigned hash_inaddr(unsigned key)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding key ^= (key >> 16);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return ((key >> 8) ^ key) % IPHASH_TABLE_SIZE;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
a7ed9c525f9460187f327cea953bf90ecf1bdc51gsteinstatic APR_INLINE unsigned hash_addr(struct apr_sockaddr_t *sa)
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick{
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick unsigned key;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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 */
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick key = *(unsigned *)((char *)sa->ipaddr_ptr + sa->ipaddr_len - 4);
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick return hash_inaddr(key);
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic ipaddr_chain *new_ipaddr_chain(apr_pool_t *p,
785be1b6298010956622771c870ab3cd8ca57a2faaron server_rec *s, server_addr_rec *sar)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ipaddr_chain *new;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm new = apr_palloc(p, sizeof(*new));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding new->names = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding new->server = s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding new->sar = sar;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding new->next = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return new;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
785be1b6298010956622771c870ab3cd8ca57a2faaronstatic name_chain *new_name_chain(apr_pool_t *p,
785be1b6298010956622771c870ab3cd8ca57a2faaron server_rec *s, server_addr_rec *sar)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding name_chain *new;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm new = apr_palloc(p, sizeof(*new));
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding new->server = s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding new->sar = sar;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding new->next = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return new;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
a7ed9c525f9460187f327cea953bf90ecf1bdc51gsteinstatic APR_INLINE ipaddr_chain *find_ipaddr(apr_sockaddr_t *sa)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding unsigned bucket;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ipaddr_chain *trav;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* scan the hash apr_table_t for an exact match first */
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick bucket = hash_addr(sa);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (trav = iphash_table[bucket]; trav; trav = trav->next) {
785be1b6298010956622771c870ab3cd8ca57a2faaron server_addr_rec *sar = trav->sar;
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick apr_sockaddr_t *cur = sar->host_addr;
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick
39b76a07959a0a332366c735a23894d9e8ed6872trawick if (cur->port == 0 || sa->port == 0 || cur->port == sa->port) {
8fd7c5046d164fb0959222497e5925dfc6a52ff3trawick if (apr_sockaddr_equal(cur, sa)) {
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick return trav;
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick }
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
8f8ec0957334f50b7ac11359f90490ee467258eedreidstatic ipaddr_chain *find_default_server(apr_port_t port)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_addr_rec *sar;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ipaddr_chain *trav;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (trav = default_list; trav; trav = trav->next) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding sar = trav->sar;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (sar->host_port == 0 || sar->host_port == port) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* match! */
785be1b6298010956622771c870ab3cd8ca57a2faaron return trav;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanfstatic void dump_a_vhost(apr_file_t *f, ipaddr_chain *ic)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding name_chain *nc;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf int len;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char buf[MAX_STRING_LEN];
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick apr_sockaddr_t *ha = ic->sar->host_addr;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
39b76a07959a0a332366c735a23894d9e8ed6872trawick if (ha->family == APR_INET &&
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick ha->sa.sin.sin_addr.s_addr == DEFAULT_VHOST_ADDR) {
785be1b6298010956622771c870ab3cd8ca57a2faaron len = apr_snprintf(buf, sizeof(buf), "_default_:%u",
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->sar->host_port);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
39b76a07959a0a332366c735a23894d9e8ed6872trawick else if (ha->family == APR_INET &&
6b38fca3ec543a0f72efd5683e91a0b30fc752d1trawick ha->sa.sin.sin_addr.s_addr == INADDR_ANY) {
785be1b6298010956622771c870ab3cd8ca57a2faaron len = apr_snprintf(buf, sizeof(buf), "*:%u",
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->sar->host_port);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf else {
785be1b6298010956622771c870ab3cd8ca57a2faaron len = apr_snprintf(buf, sizeof(buf), "%pI", ha);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (ic->sar->host_port == 0) {
785be1b6298010956622771c870ab3cd8ca57a2faaron buf[len-1] = '*';
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (ic->names == NULL) {
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_file_printf(f, "%-22s %s (%s:%u)\n", buf,
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->server->server_hostname,
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->server->defn_name, ic->server->defn_line_number);
785be1b6298010956622771c870ab3cd8ca57a2faaron return;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_file_printf(f, "%-22s is a NameVirtualHost\n"
785be1b6298010956622771c870ab3cd8ca57a2faaron "%8s default server %s (%s:%u)\n",
785be1b6298010956622771c870ab3cd8ca57a2faaron buf, "", ic->server->server_hostname,
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->server->defn_name, ic->server->defn_line_number);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf for (nc = ic->names; nc; nc = nc->next) {
785be1b6298010956622771c870ab3cd8ca57a2faaron if (nc->sar->host_port) {
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_file_printf(f, "%8s port %u ", "", nc->sar->host_port);
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron else {
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_file_printf(f, "%8s port * ", "");
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_file_printf(f, "namevhost %s (%s:%u)\n",
785be1b6298010956622771c870ab3cd8ca57a2faaron nc->server->server_hostname,
785be1b6298010956622771c870ab3cd8ca57a2faaron nc->server->defn_name, nc->server->defn_line_number);
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf}
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanfstatic void dump_vhost_config(apr_file_t *f)
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf{
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf ipaddr_chain *ic;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf int i;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_file_printf(f, "VirtualHost configuration:\n");
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
785be1b6298010956622771c870ab3cd8ca57a2faaron for (ic = iphash_table[i]; ic; ic = ic->next) {
785be1b6298010956622771c870ab3cd8ca57a2faaron dump_a_vhost(f, ic);
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (default_list) {
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_file_printf(f, "wildcard NameVirtualHosts and _default_ servers:\n");
785be1b6298010956622771c870ab3cd8ca57a2faaron for (ic = default_list; ic; ic = ic->next) {
785be1b6298010956622771c870ab3cd8ca57a2faaron dump_a_vhost(f, ic);
785be1b6298010956622771c870ab3cd8ca57a2faaron }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf}
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf/*
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * Two helper functions for ap_fini_vhost_config()
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf */
785be1b6298010956622771c870ab3cd8ca57a2faaronstatic int add_name_vhost_config(apr_pool_t *p, server_rec *main_s,
785be1b6298010956622771c870ab3cd8ca57a2faaron server_rec *s, server_addr_rec *sar,
785be1b6298010956622771c870ab3cd8ca57a2faaron ipaddr_chain *ic)
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf{
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.
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf if (ic->names || ic->server == NULL) {
785be1b6298010956622771c870ab3cd8ca57a2faaron name_chain *nc = new_name_chain(p, s, sar);
785be1b6298010956622771c870ab3cd8ca57a2faaron nc->next = ic->names;
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->names = nc;
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->server = s;
785be1b6298010956622771c870ab3cd8ca57a2faaron if (sar->host_port != ic->sar->host_port) {
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 sar->virthost, sar->host_port);
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron return 1;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf else {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* IP-based vhosts are handled by the caller */
785be1b6298010956622771c870ab3cd8ca57a2faaron return 0;
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf}
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanfstatic void remove_unused_name_vhosts(server_rec *main_s, ipaddr_chain **pic)
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf{
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf while (*pic) {
785be1b6298010956622771c870ab3cd8ca57a2faaron ipaddr_chain *ic = *pic;
785be1b6298010956622771c870ab3cd8ca57a2faaron
785be1b6298010956622771c870ab3cd8ca57a2faaron if (ic->server == NULL) {
785be1b6298010956622771c870ab3cd8ca57a2faaron ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, main_s,
785be1b6298010956622771c870ab3cd8ca57a2faaron "NameVirtualHost %s:%u has no VirtualHosts",
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->sar->virthost, ic->sar->host_port);
785be1b6298010956622771c870ab3cd8ca57a2faaron *pic = ic->next;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron else if (ic->names == NULL) {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* if server != NULL and names == NULL then we're done
785be1b6298010956622771c870ab3cd8ca57a2faaron * looking at NameVirtualHosts
785be1b6298010956622771c870ab3cd8ca57a2faaron */
785be1b6298010956622771c870ab3cd8ca57a2faaron break;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron else {
785be1b6298010956622771c870ab3cd8ca57a2faaron pic = &ic->next;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_addr_rec *sar;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int has_default_vhost_addr;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_rec *s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int i;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ipaddr_chain **iphash_table_tail[IPHASH_TABLE_SIZE];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* terminate the name_vhost list */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *name_vhost_list_tail = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* Main host first */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding s = main_s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!s->server_hostname) {
785be1b6298010956622771c870ab3cd8ca57a2faaron s->server_hostname = ap_get_local_host(p);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* initialize the tails */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
785be1b6298010956622771c870ab3cd8ca57a2faaron iphash_table_tail[i] = &iphash_table[i];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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 */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (sar = name_vhost_list; sar; sar = sar->next) {
5f08a022a210f4e511561e89f500621a15e6177dtrawick char inaddr_any[16] = {0}; /* big enough to handle IPv4 or IPv6 */
785be1b6298010956622771c870ab3cd8ca57a2faaron unsigned bucket = hash_addr(sar->host_addr);
785be1b6298010956622771c870ab3cd8ca57a2faaron ipaddr_chain *ic = new_ipaddr_chain(p, NULL, sar);
785be1b6298010956622771c870ab3cd8ca57a2faaron
5f08a022a210f4e511561e89f500621a15e6177dtrawick if (memcmp(sar->host_addr->ipaddr_ptr, inaddr_any,
5f08a022a210f4e511561e89f500621a15e6177dtrawick sar->host_addr->ipaddr_len)) { /* not IN[6]ADDR_ANY */
785be1b6298010956622771c870ab3cd8ca57a2faaron *iphash_table_tail[bucket] = ic;
785be1b6298010956622771c870ab3cd8ca57a2faaron iphash_table_tail[bucket] = &ic->next;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron else {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* A wildcard NameVirtualHost goes on the default_list so
785be1b6298010956622771c870ab3cd8ca57a2faaron * that it can catch incoming requests on any address.
785be1b6298010956622771c870ab3cd8ca57a2faaron */
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->next = default_list;
785be1b6298010956622771c870ab3cd8ca57a2faaron default_list = ic;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
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().
785be1b6298010956622771c870ab3cd8ca57a2faaron */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (s = main_s->next; s; s = s->next) {
785be1b6298010956622771c870ab3cd8ca57a2faaron has_default_vhost_addr = 0;
785be1b6298010956622771c870ab3cd8ca57a2faaron for (sar = s->addrs; sar; sar = sar->next) {
785be1b6298010956622771c870ab3cd8ca57a2faaron ipaddr_chain *ic;
900127764fb985c340ee4979cac97146a330c694trawick char inaddr_any[16] = {0}; /* big enough to handle IPv4 or IPv6 */
785be1b6298010956622771c870ab3cd8ca57a2faaron
900127764fb985c340ee4979cac97146a330c694trawick if ((sar->host_addr->family == AF_INET &&
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 ic = find_default_server(sar->host_port);
785be1b6298010956622771c870ab3cd8ca57a2faaron if (!ic || !add_name_vhost_config(p, main_s, s, sar, ic)) {
785be1b6298010956622771c870ab3cd8ca57a2faaron if (ic && ic->sar->host_port != 0) {
785be1b6298010956622771c870ab3cd8ca57a2faaron ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
785be1b6298010956622771c870ab3cd8ca57a2faaron 0, main_s, "_default_ VirtualHost "
785be1b6298010956622771c870ab3cd8ca57a2faaron "overlap on port %u, the first has "
785be1b6298010956622771c870ab3cd8ca57a2faaron "precedence", sar->host_port);
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron ic = new_ipaddr_chain(p, s, sar);
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->next = default_list;
785be1b6298010956622771c870ab3cd8ca57a2faaron default_list = ic;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron has_default_vhost_addr = 1;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron else {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* see if it matches something we've already got */
785be1b6298010956622771c870ab3cd8ca57a2faaron ic = find_ipaddr(sar->host_addr);
785be1b6298010956622771c870ab3cd8ca57a2faaron
785be1b6298010956622771c870ab3cd8ca57a2faaron if (!ic) {
785be1b6298010956622771c870ab3cd8ca57a2faaron unsigned bucket = hash_addr(sar->host_addr);
785be1b6298010956622771c870ab3cd8ca57a2faaron
785be1b6298010956622771c870ab3cd8ca57a2faaron ic = new_ipaddr_chain(p, s, sar);
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->next = *iphash_table_tail[bucket];
785be1b6298010956622771c870ab3cd8ca57a2faaron *iphash_table_tail[bucket] = ic;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron else if (!add_name_vhost_config(p, main_s, s, sar, ic)) {
785be1b6298010956622771c870ab3cd8ca57a2faaron ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING,
785be1b6298010956622771c870ab3cd8ca57a2faaron 0, main_s, "VirtualHost %s:%u overlaps "
785be1b6298010956622771c870ab3cd8ca57a2faaron "with VirtualHost %s:%u, the first has "
785be1b6298010956622771c870ab3cd8ca57a2faaron "precedence, perhaps you need a "
785be1b6298010956622771c870ab3cd8ca57a2faaron "NameVirtualHost directive",
785be1b6298010956622771c870ab3cd8ca57a2faaron sar->virthost, sar->host_port,
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->sar->virthost, ic->sar->host_port);
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->sar = sar;
785be1b6298010956622771c870ab3cd8ca57a2faaron ic->server = s;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron
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 */
785be1b6298010956622771c870ab3cd8ca57a2faaron if (!s->server_hostname) {
785be1b6298010956622771c870ab3cd8ca57a2faaron if (has_default_vhost_addr) {
785be1b6298010956622771c870ab3cd8ca57a2faaron s->server_hostname = main_s->server_hostname;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
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 */
785be1b6298010956622771c870ab3cd8ca57a2faaron s->server_hostname =
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_pstrdup(p, "bogus_host_without_forward_dns");
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron else {
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_status_t rv;
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick char *hostname;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick rv = apr_getnameinfo(&hostname, s->addrs->host_addr, 0);
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick if (rv == APR_SUCCESS) {
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick s->server_hostname = apr_pstrdup(p, hostname);
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron else {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* again, what can we do? They didn't specify a
785be1b6298010956622771c870ab3cd8ca57a2faaron ServerName, and their DNS isn't working. -djg */
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick char *ipaddr_str;
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_sockaddr_ip_get(&ipaddr_str, s->addrs->host_addr);
785be1b6298010956622771c870ab3cd8ca57a2faaron ap_log_error(APLOG_MARK, APLOG_ERR, rv, main_s,
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick "Failed to resolve server name "
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick "for %s (check DNS) -- or specify an explicit "
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick "ServerName",
8a261a9f7d18d1e862d63f68e93f288d3e1f0d94trawick ipaddr_str);
785be1b6298010956622771c870ab3cd8ca57a2faaron s->server_hostname =
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_pstrdup(p, "bogus_host_without_reverse_dns");
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* now go through and delete any NameVirtualHosts that didn't have any
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * hosts associated with them. Lamers.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
785be1b6298010956622771c870ab3cd8ca57a2faaron remove_unused_name_vhosts(main_s, &iphash_table[i]);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf remove_unused_name_vhosts(main_s, &default_list);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#ifdef IPHASH_STATISTICS
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding dump_iphash_statistics(main_s);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#endif
bbbf8f0e622ad5a37ccf70f35660fc755575278arbb if (ap_exists_config_define("DUMP_VHOSTS")) {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm apr_file_t *thefile = NULL;
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_file_open_stderr(&thefile, p);
785be1b6298010956622771c870ab3cd8ca57a2faaron dump_vhost_config(thefile);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*****************************************************************************
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * run-time vhost matching functions
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf/* Lowercase and remove any trailing dot and/or :port from the hostname,
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf * and check that it is sane.
91583d2e9c0550f539ea6f4dedf051979ad1ad88fanf *
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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic void fix_hostname(request_rec *r)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick char *host, *scope_id;
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf char *dst;
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick apr_port_t port;
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick apr_status_t rv;
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf
9c518951a46c7a12e20876827bb2e84ef87d3c11jerenkrantz /* According to RFC 2616, Host header field CAN be blank. */
9c518951a46c7a12e20876827bb2e84ef87d3c11jerenkrantz if (!*r->hostname) {
9c518951a46c7a12e20876827bb2e84ef87d3c11jerenkrantz return;
9c518951a46c7a12e20876827bb2e84ef87d3c11jerenkrantz }
9c518951a46c7a12e20876827bb2e84ef87d3c11jerenkrantz
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick rv = apr_parse_addr_port(&host, &scope_id, &port, r->hostname, r->pool);
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick if (rv != APR_SUCCESS || scope_id) {
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick goto bad;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
0db1b9810f06c0e3c537e0e0dfbc30160c308526trawick if (!host && port) {
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
0db1b9810f06c0e3c537e0e0dfbc30160c308526trawick */
0db1b9810f06c0e3c537e0e0dfbc30160c308526trawick host = apr_psprintf(r->pool, "%d", (int)port);
0db1b9810f06c0e3c537e0e0dfbc30160c308526trawick }
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 */
644be6f54749d2d9950d2c4d2ac448f7af016d26martin r->parsed_uri.port = port;
644be6f54749d2d9950d2c4d2ac448f7af016d26martin r->parsed_uri.port_str = apr_psprintf(r->pool, "%d", (int)port);
644be6f54749d2d9950d2c4d2ac448f7af016d26martin }
0db1b9810f06c0e3c537e0e0dfbc30160c308526trawick
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick /* if the hostname is an IPv6 numeric address string, it was validated
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick * already; otherwise, further validation is needed
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick */
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick if (r->hostname[0] != '[') {
71715c646d5231de578431f8961e711764b899d3fanf for (dst = host; *dst; dst++) {
785be1b6298010956622771c870ab3cd8ca57a2faaron if (*dst == '.') {
ccad4fdc6c75a352157a413bd3bbaf4d0c8a6f72brianp if (*(dst + 1) == '.') {
785be1b6298010956622771c870ab3cd8ca57a2faaron goto bad;
ccad4fdc6c75a352157a413bd3bbaf4d0c8a6f72brianp }
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron else if (*dst == '/' || *dst == '\\') {
785be1b6298010956622771c870ab3cd8ca57a2faaron goto bad;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick }
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick /* strip trailing gubbins */
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick if (dst > host && dst[-1] == '.') {
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick dst[-1] = '\0';
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick }
3c48210f662a2ab8ed90708989e04c09aae33cb2trawick }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding r->hostname = host;
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf return;
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanfbad:
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf r->status = HTTP_BAD_REQUEST;
5e4791fd54c10fdd77ca81bee8ac8398f16d6d6astoddard ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
785be1b6298010956622771c870ab3cd8ca57a2faaron "Client sent malformed Host header");
3926b3b7716683a1241c1ff6f8dd2f9c5073665afanf return;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/* return 1 if host matches ServerName or ServerAliases */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int matches_aliases(server_rec *s, const char *host)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int i;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm apr_array_header_t *names;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* match ServerName */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (!strcasecmp(host, s->server_hostname)) {
785be1b6298010956622771c870ab3cd8ca57a2faaron return 1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* search all the aliases from ServerAlias directive */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding names = s->names;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (names) {
785be1b6298010956622771c870ab3cd8ca57a2faaron char **name = (char **) names->elts;
785be1b6298010956622771c870ab3cd8ca57a2faaron for (i = 0; i < names->nelts; ++i) {
785be1b6298010956622771c870ab3cd8ca57a2faaron if(!name[i]) continue;
785be1b6298010956622771c870ab3cd8ca57a2faaron if (!strcasecmp(host, name[i]))
785be1b6298010956622771c870ab3cd8ca57a2faaron return 1;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding names = s->wild_names;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (names) {
785be1b6298010956622771c870ab3cd8ca57a2faaron char **name = (char **) names->elts;
785be1b6298010956622771c870ab3cd8ca57a2faaron for (i = 0; i < names->nelts; ++i) {
785be1b6298010956622771c870ab3cd8ca57a2faaron if(!name[i]) continue;
785be1b6298010956622771c870ab3cd8ca57a2faaron if (!ap_strcasecmp_match(host, name[i]))
785be1b6298010956622771c870ab3cd8ca57a2faaron return 1;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
3d96ee83babeec32482c9082c9426340cee8c44dwroweAP_DECLARE(int) ap_matches_request_vhost(request_rec *r, const char *host,
785be1b6298010956622771c870ab3cd8ca57a2faaron apr_port_t port)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_rec *s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_addr_rec *sar;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding s = r->server;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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 *
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 *
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 */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (sar = s->addrs; sar; sar = sar->next) {
785be1b6298010956622771c870ab3cd8ca57a2faaron if ((sar->host_port == 0 || port == sar->host_port)
785be1b6298010956622771c870ab3cd8ca57a2faaron && !strcasecmp(host, sar->virthost)) {
785be1b6298010956622771c870ab3cd8ca57a2faaron return 1;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* the Port has to match now, because the rest don't have ports associated
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * with them. */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (port != s->port) {
785be1b6298010956622771c870ab3cd8ca57a2faaron return 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return matches_aliases(s, host);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic void check_hostalias(request_rec *r)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
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 *
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 */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding const char *host = r->hostname;
dad234382d8424e1c5a30af2838e172aec9d6d1bdreid apr_port_t port;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_rec *s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_rec *last_s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding name_chain *src;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding last_s = NULL;
e6cc28a5eb3371ba0c38e941855e71ff0054f50erbb
e6cc28a5eb3371ba0c38e941855e71ff0054f50erbb apr_sockaddr_port_get(&port, r->connection->local_addr);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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 */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding for (src = r->connection->vhost_lookup_data; src; src = src->next) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_addr_rec *sar;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
785be1b6298010956622771c870ab3cd8ca57a2faaron /* We only consider addresses on the name_chain which have a matching
785be1b6298010956622771c870ab3cd8ca57a2faaron * port
785be1b6298010956622771c870ab3cd8ca57a2faaron */
785be1b6298010956622771c870ab3cd8ca57a2faaron sar = src->sar;
785be1b6298010956622771c870ab3cd8ca57a2faaron if (sar->host_port != 0 && port != sar->host_port) {
785be1b6298010956622771c870ab3cd8ca57a2faaron continue;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding s = src->server;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
785be1b6298010956622771c870ab3cd8ca57a2faaron /* does it match the virthost from the sar? */
785be1b6298010956622771c870ab3cd8ca57a2faaron if (!strcasecmp(host, sar->virthost)) {
785be1b6298010956622771c870ab3cd8ca57a2faaron goto found;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron
785be1b6298010956622771c870ab3cd8ca57a2faaron if (s == last_s) {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* we've already done ServerName and ServerAlias checks for this
785be1b6298010956622771c870ab3cd8ca57a2faaron * vhost
785be1b6298010956622771c870ab3cd8ca57a2faaron */
785be1b6298010956622771c870ab3cd8ca57a2faaron continue;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron last_s = s;
785be1b6298010956622771c870ab3cd8ca57a2faaron
785be1b6298010956622771c870ab3cd8ca57a2faaron if (matches_aliases(s, host)) {
785be1b6298010956622771c870ab3cd8ca57a2faaron goto found;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingfound:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* s is the first matching server, we're done */
51af95bb51b5084e883bad250b2afa2838e9ceebfielding r->server = s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic void check_serverpath(request_rec *r)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_rec *s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding server_rec *last_s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding name_chain *src;
dad234382d8424e1c5a30af2838e172aec9d6d1bdreid apr_port_t port;
382fa07a63096c4a1aabfed36433ea5ac9c40ad0trawick
e6cc28a5eb3371ba0c38e941855e71ff0054f50erbb apr_sockaddr_port_get(&port, r->connection->local_addr);
e6cc28a5eb3371ba0c38e941855e71ff0054f50erbb
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /*
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 *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * See the comment in check_hostalias about how each vhost can be
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * listed multiple times.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding last_s = NULL;
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 * port
785be1b6298010956622771c870ab3cd8ca57a2faaron */
785be1b6298010956622771c870ab3cd8ca57a2faaron if (src->sar->host_port != 0 && port != src->sar->host_port) {
785be1b6298010956622771c870ab3cd8ca57a2faaron continue;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding s = src->server;
785be1b6298010956622771c870ab3cd8ca57a2faaron if (s == last_s) {
785be1b6298010956622771c870ab3cd8ca57a2faaron continue;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
785be1b6298010956622771c870ab3cd8ca57a2faaron last_s = s;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (s->path && !strncmp(r->uri, s->path, s->pathlen) &&
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding (s->path[s->pathlen - 1] == '/' ||
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding r->uri[s->pathlen] == '/' ||
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding r->uri[s->pathlen] == '\0')) {
51af95bb51b5084e883bad250b2afa2838e9ceebfielding r->server = s;
785be1b6298010956622771c870ab3cd8ca57a2faaron return;
785be1b6298010956622771c870ab3cd8ca57a2faaron }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
45acd673a68181802b112e97e84fa3813ddd3ec1stoddardAP_DECLARE(void) ap_update_vhost_from_headers(request_rec *r)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* must set this for HTTP/1.1 support */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm if (r->hostname || (r->hostname = apr_table_get(r->headers_in, "Host"))) {
785be1b6298010956622771c870ab3cd8ca57a2faaron fix_hostname(r);
785be1b6298010956622771c870ab3cd8ca57a2faaron if (r->status != HTTP_OK)
785be1b6298010956622771c870ab3cd8ca57a2faaron return;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding /* check if we tucked away a name_chain */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (r->connection->vhost_lookup_data) {
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (r->hostname)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding check_hostalias(r);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding else
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding check_serverpath(r);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
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.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
45acd673a68181802b112e97e84fa3813ddd3ec1stoddardAP_DECLARE(void) ap_update_vhost_given_ip(conn_rec *conn)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding ipaddr_chain *trav;
8f8ec0957334f50b7ac11359f90490ee467258eedreid apr_port_t port;
382fa07a63096c4a1aabfed36433ea5ac9c40ad0trawick
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* scan the hash apr_table_t for an exact match first */
17f3ba69f65182426ad4e568bb2d6f192ccd2ed5trawick trav = find_ipaddr(conn->local_addr);
e6cc28a5eb3371ba0c38e941855e71ff0054f50erbb
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (trav) {
785be1b6298010956622771c870ab3cd8ca57a2faaron /* save the name_chain for later in case this is a name-vhost */
785be1b6298010956622771c870ab3cd8ca57a2faaron conn->vhost_lookup_data = trav->names;
785be1b6298010956622771c870ab3cd8ca57a2faaron conn->base_server = trav->server;
785be1b6298010956622771c870ab3cd8ca57a2faaron return;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /* maybe there's a default server or wildcard name-based vhost
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * matching this port
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_sockaddr_port_get(&port, conn->local_addr);
e6cc28a5eb3371ba0c38e941855e71ff0054f50erbb
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding trav = find_default_server(port);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding if (trav) {
785be1b6298010956622771c870ab3cd8ca57a2faaron conn->vhost_lookup_data = trav->names;
785be1b6298010956622771c870ab3cd8ca57a2faaron conn->base_server = trav->server;
785be1b6298010956622771c870ab3cd8ca57a2faaron return;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf /* otherwise we're stuck with just the main server
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf * and no name-based vhosts
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf */
b88f887ed5554d9050d97f9a56a89ae62bdbd906fanf conn->vhost_lookup_data = NULL;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}