vhost.c revision 6de8046f8f7e07cd83895a528df25d977e502c76
/* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* http_vhost.c: functions pertaining to virtual host addresses
* (configuration and run-time)
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_lib.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
#define CORE_PRIVATE
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "http_vhost.h"
#include "http_protocol.h"
#include "http_core.h"
#endif
/*
* After all the definitions there's an explanation of how it's all put
* together.
*/
/* meta-list of name-vhosts. Each server_rec can be in possibly multiple
* lists of name-vhosts.
*/
typedef struct name_chain name_chain;
struct name_chain {
* this chain (needed for port comparisons) */
};
/* meta-list of ip addresses. Each server_rec can be in possibly multiple
* hash chains since it can have multiple ips.
*/
typedef struct ipaddr_chain ipaddr_chain;
struct ipaddr_chain {
* this chain (need for both ip addr and port
* comparisons) */
* sharing this address */
};
/* This defines the size of the hash apr_table_t used for hashing ip addresses
* of virtual hosts. It must be a power of two.
*/
#ifndef IPHASH_TABLE_SIZE
#define IPHASH_TABLE_SIZE 256
#endif
/* A (n) bucket hash table, each entry has a pointer to a server rec and
* a pointer to the other entries in that bucket. Each individual address,
* even for virtualhosts with multiple addresses, has an entry in this hash
* table. There are extra buckets for _default_, and name-vhost entries.
*
* Note that after config time this is constant, so it is thread-safe.
*/
/* dump out statistics about the hash function */
/* #define IPHASH_STATISTICS */
/* list of the _default_ servers */
static ipaddr_chain *default_list;
/* list of the NameVirtualHost addresses */
static server_addr_rec *name_vhost_list;
static server_addr_rec **name_vhost_list_tail;
/*
* How it's used:
*
* The ip address determines which chain in iphash_table is interesting, then
* a comparison is done down that chain to find the first ipaddr_chain whose
* sar matches the address:port pair.
*
* If that ipaddr_chain has names == NULL then you're done, it's an ip-vhost.
*
* Otherwise it's a name-vhost list, and the default is the server in the
* ipaddr_chain record. We tuck away the ipaddr_chain record in the
* conn_rec field vhost_lookup_data. Later on after the headers we get a
* second chance, and we use the name_chain to figure out what name-vhost
* matches the headers.
*
* If there was no ip address match in the iphash_table then do a lookup
* in the default_list.
*
* How it's put together ... well you should be able to figure that out
* from how it's used. Or something like that.
*/
/* called at the beginning of the config */
{
default_list = NULL;
}
/*
* Parses a host of the form <address>[:port]
* paddr is used to create a list in the order of input
* **paddr is the ->next pointer of the last entry (or s->addrs)
* *paddr is the variable used to keep track of **paddr between calls
* port is the default port to assume
*/
server_addr_rec ***paddr,
{
int wild_port;
if (*w_ == '\0')
return NULL;
w = apr_pstrdup(p, w_);
/* apr_parse_addr_port() doesn't understand ":*" so handle that first. */
wild_port = 0;
if (wlen < 2) {
wild_port = 1;
}
wild_port = 1;
}
}
/* If the string is "80", apr_parse_addr_port() will be happy and set
* host to NULL and port to 80, so watch out for that.
*/
if (rv != APR_SUCCESS) {
return "The address or port is invalid";
}
if (!host) {
return "Missing address for VirtualHost";
}
if (scope_id) {
return "Scope ids are not supported";
}
port = default_port;
}
}
}
else {
if (rv != APR_SUCCESS) {
"Cannot resolve host name %s --- ignoring!", host);
return NULL;
}
}
/* XXX Gotta go through *all* addresses for the host name!
* Fix apr_sockaddr_info_get() to save them! */
return NULL;
}
/* parse the <VirtualHost> addresses */
const char *ap_parse_vhost_addrs(apr_pool_t *p,
const char *hostname,
server_rec *s)
{
const char *err;
/* start the list of addreses */
while (hostname[0]) {
if (err) {
return err;
}
}
/* terminate the list */
if (s->addrs) {
/* override the default port which is inherited from main_server */
}
}
return NULL;
}
const char *arg)
{
/* use whatever port the main server has at this point */
}
/* hash apr_table_t statistics, keep this in here for the beta period so
* we can find out if the hash function is ok
*/
#ifdef IPHASH_STATISTICS
static int iphash_compare(const void *a, const void *b)
{
return (*(const int *) b - *(const int *) a);
}
{
unsigned count[IPHASH_TABLE_SIZE];
int i;
unsigned total;
char buf[HUGE_STRING_LEN];
char *p;
total = 0;
for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
count[i] = 0;
++count[i];
if (i < IPHASH_TABLE_SIZE) {
/* don't count the slop buckets in the total */
++total;
}
}
}
"iphash: total hashed = %u, avg chain = %u, "
"chain lengths (count x len):",
total = 1;
for (i = 1; i < IPHASH_TABLE_SIZE; ++i) {
total = 1;
}
else {
++total;
}
}
}
#endif
/* This hashing function is designed to get good distribution in the cases
* where the server is handling entire "networks" of servers. i.e. a
* whack of /24s. This is probably the most common configuration for
* ISPs with large virtual servers.
*
* NOTE: This function is symmetric (i.e. collapses all 4 octets
*
* Hash function provided by David Hankins.
*/
{
}
{
unsigned key;
/* The key is the last four bytes of the IP address.
* For IPv4, this is the entire address, as always.
* For IPv6, this is usually part of the MAC address.
*/
return hash_inaddr(key);
}
{
return new;
}
{
return new;
}
{
unsigned bucket;
/* scan the hash apr_table_t for an exact match first */
return trav;
}
}
}
return NULL;
}
{
/* match! */
return trav;
}
}
return NULL;
}
{
name_chain *nc;
int len;
char buf[MAX_STRING_LEN];
}
}
else {
}
}
return;
}
apr_file_printf(f, "%-22s is a NameVirtualHost\n"
"%8s default server %s (%s:%u)\n",
}
else {
}
apr_file_printf(f, "namevhost %s (%s:%u)\n",
}
}
static void dump_vhost_config(apr_file_t *f)
{
int i;
apr_file_printf(f, "VirtualHost configuration:\n");
for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
dump_a_vhost(f, ic);
}
}
if (default_list) {
apr_file_printf(f, "wildcard NameVirtualHosts and _default_ servers:\n");
dump_a_vhost(f, ic);
}
}
}
/*
* Two helper functions for ap_fini_vhost_config()
*/
{
/* the first time we encounter a NameVirtualHost address
* ic->server will be NULL, on subsequent encounters
* ic->names will be non-NULL.
*/
/* one of the two is a * port, the other isn't */
"VirtualHost %s:%u -- mixing * "
"ports and non-* ports with "
"a NameVirtualHost address is not supported,"
" proceeding with undefined results",
}
return 1;
}
else {
/* IP-based vhosts are handled by the caller */
return 0;
}
}
{
while (*pic) {
"NameVirtualHost %s:%u has no VirtualHosts",
}
/* if server != NULL and names == NULL then we're done
* looking at NameVirtualHosts
*/
break;
}
else {
}
}
}
/* compile the tables and such we need to do the run-time vhost lookups */
{
server_rec *s;
int i;
/* terminate the name_vhost list */
/* Main host first */
s = main_s;
if (!s->server_hostname) {
s->server_hostname = ap_get_local_host(p);
}
/* initialize the tails */
for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
iphash_table_tail[i] = &iphash_table[i];
}
/* The first things to go into the hash apr_table_t are the NameVirtualHosts
* Since name_vhost_list is in the same order that the directives
* occured in the config file, we'll copy it in that order.
*/
}
else {
/* A wildcard NameVirtualHost goes on the default_list so
* that it can catch incoming requests on any address.
*/
default_list = ic;
}
/* Notice that what we've done is insert an ipaddr_chain with
* both server and names NULL. This fact is used to spot name-
* based vhosts in add_name_vhost_config().
*/
}
/* The next things to go into the hash apr_table_t are the virtual hosts
* themselves. They're listed off of main_s->next in the reverse
* order they occured in the config file, so we insert them at
* the iphash_table_tail but don't advance the tail.
*/
0, main_s, "_default_ VirtualHost "
"overlap on port %u, the first has "
}
default_list = ic;
}
}
else {
/* see if it matches something we've already got */
if (!ic) {
}
0, main_s, "VirtualHost %s:%u overlaps "
"with VirtualHost %s:%u, the first has "
"precedence, perhaps you need a "
"NameVirtualHost directive",
}
}
}
/* Ok now we want to set up a server_hostname if the user was
* silly enough to forget one.
* XXX: This is silly we should just crash and burn.
*/
if (!s->server_hostname) {
if (has_default_vhost_addr) {
}
else if (!s->addrs) {
/* what else can we do? at this point this vhost has
no configured name, probably because they used
DNS in the VirtualHost statement. It's disabled
anyhow by the host matching code. -djg */
s->server_hostname =
apr_pstrdup(p, "bogus_host_without_forward_dns");
}
else {
char *hostname;
if (rv == APR_SUCCESS) {
}
else {
/* again, what can we do? They didn't specify a
ServerName, and their DNS isn't working. -djg */
char *ipaddr_str;
"Failed to resolve server name "
"for %s (check DNS) -- or specify an explicit "
"ServerName",
s->server_hostname =
apr_pstrdup(p, "bogus_host_without_reverse_dns");
}
}
}
}
/* now go through and delete any NameVirtualHosts that didn't have any
* hosts associated with them. Lamers.
*/
for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
}
#ifdef IPHASH_STATISTICS
#endif
if (ap_exists_config_define("DUMP_VHOSTS")) {
apr_file_open_stderr(&thefile, p);
}
}
/*****************************************************************************
* run-time vhost matching functions
*/
* and check that it is sane.
*
* In most configurations the exact syntax of the hostname isn't
* important so strict sanity checking isn't necessary. However, in
* mass hosting setups (using mod_vhost_alias or mod_rewrite) where
* the hostname is interpolated into the filename, we need to be sure
* that the interpolation doesn't expose parts of the filesystem.
* We don't do strict RFC 952 / RFC 1123 syntax checking in order
* to support iDNS and people who erroneously use underscores.
* Instead we just check for filesystem metacharacters: directory
* separators / and \ and sequences of more than one dot.
*/
static void fix_hostname(request_rec *r)
{
char *dst;
/* According to RFC 2616, Host header field CAN be blank. */
if (!*r->hostname) {
return;
}
goto bad;
}
/* silly looking host ("Host: 123") but that isn't our job
* here to judge; apr_parse_addr_port() would think we had a port
* but no address
*/
}
else if (port) {
/* Don't throw the Host: header's port number away:
save it in parsed_uri -- ap_get_server_port() needs it! */
/* @@@ XXX there should be a better way to pass the port.
* Like r->hostname, there should be a r->portno
*/
}
/* if the hostname is an IPv6 numeric address string, it was validated
* already; otherwise, further validation is needed
*/
if (r->hostname[0] != '[') {
if (apr_islower(*dst)) {
/* leave char unchanged */
}
else if (*dst == '.') {
goto bad;
}
}
else if (apr_isupper(*dst)) {
}
goto bad;
}
}
/* strip trailing gubbins */
}
}
return;
bad:
r->status = HTTP_BAD_REQUEST;
"Client sent malformed Host header");
return;
}
/* return 1 if host matches ServerName or ServerAliases */
{
int i;
/* match ServerName */
return 1;
}
/* search all the aliases from ServerAlias directive */
if (names) {
if(!name[i]) continue;
return 1;
}
}
names = s->wild_names;
if (names) {
if(!name[i]) continue;
return 1;
}
}
return 0;
}
/* Suppose a request came in on the same socket as this r, and included
* a header "Host: host:port", would it map to r->server? It's more
* than just that though. When we do the normal matches for each request
* we don't even bother considering Host: etc on non-namevirtualhosts,
* we just call it a match. But here we require the host:port to match
*/
{
server_rec *s;
s = r->server;
/* search all the <VirtualHost> values */
/* XXX: If this is a NameVirtualHost then we may not be doing the Right Thing
* consider:
*
* NameVirtualHost 10.1.1.1
* <VirtualHost 10.1.1.1>
* ServerName v1
* </VirtualHost>
* <VirtualHost 10.1.1.1>
* ServerName v2
* </VirtualHost>
*
* Suppose r->server is v2, and we're asked to match "10.1.1.1". We'll say
* "yup it's v2", when really it isn't... if a request came in for 10.1.1.1
* it would really go to v1.
*/
return 1;
}
}
/* the Port has to match now, because the rest don't have ports associated
* with them. */
return 0;
}
return matches_aliases(s, host);
}
static void check_hostalias(request_rec *r)
{
/*
* Even if the request has a Host: header containing a port we ignore
* that port. We always use the physical port of the socket. There
* are a few reasons for this:
*
* - the default of 80 or 443 for SSL is easier to handle this way
* - there is less of a possibility of a security problem
* - it simplifies the data structure
* - the client may have no idea that a proxy somewhere along the way
* translated the request to another ip:port
* - except for the addresses from the VirtualHost line, none of the other
* names we'll match have ports associated with them
*/
server_rec *s;
/* Recall that the name_chain is a list of server_addr_recs, some of
* whose ports may not match. Also each server may appear more than
* once in the chain -- specifically, it will appear once for each
* address from its VirtualHost line which matched. We only want to
* do the full ServerName/ServerAlias comparisons once for each
* server, fortunately we know that all the VirtualHost addresses for
* a single server are adjacent to each other.
*/
/* We only consider addresses on the name_chain which have a matching
* port
*/
continue;
}
/* does it match the virthost from the sar? */
goto found;
}
if (s == last_s) {
/* we've already done ServerName and ServerAlias checks for this
* vhost
*/
continue;
}
last_s = s;
if (matches_aliases(s, host)) {
goto found;
}
}
return;
/* s is the first matching server, we're done */
r->server = s;
}
static void check_serverpath(request_rec *r)
{
server_rec *s;
/*
* This is in conjunction with the ServerPath code in http_core, so we
* get the right host attached to a non- Host-sending request.
*
* See the comment in check_hostalias about how each vhost can be
* listed multiple times.
*/
/* We only consider addresses on the name_chain which have a matching
* port
*/
continue;
}
if (s == last_s) {
continue;
}
last_s = s;
r->server = s;
return;
}
}
}
{
/* must set this for HTTP/1.1 support */
fix_hostname(r);
return;
}
/* check if we tucked away a name_chain */
if (r->connection->vhost_lookup_data) {
if (r->hostname)
check_hostalias(r);
else
check_serverpath(r);
}
}
/* Called for a new connection which has a known local_addr. Note that the
* new connection is assumed to have conn->server == main server.
*/
{
/* scan the hash apr_table_t for an exact match first */
if (trav) {
/* save the name_chain for later in case this is a name-vhost */
return;
}
/* maybe there's a default server or wildcard name-based vhost
* matching this port
*/
if (trav) {
return;
}
/* otherwise we're stuck with just the main server
* and no name-based vhosts
*/
}