-/* 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
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* limitations under the License.
*/
-/*
- * http_vhost.c: functions pertaining to virtual host addresses
- * (configuration and run-time)
+/**
+ * @file vhost.c
+ * @brief functions pertaining to virtual host addresses
+ * (configuration and run-time)
*/
#include "apr.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
-#define CORE_PRIVATE
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include <arpa/inet.h>
#endif
+/* we know core's module_index is 0 */
+#undef APLOG_MODULE_INDEX
+#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
+
/*
* After all the definitions there's an explanation of how it's all put
* together.
server_rec *server; /* the server to use if this matches */
name_chain *names; /* if non-NULL then a list of name-vhosts
* sharing this address */
+ name_chain *initialnames; /* no runtime use, temporary storage of first
+ * NVH'es names */
};
/* This defines the size of the hash table used for hashing ip addresses
/* 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;
+/* whether a config error was seen */
+static int config_error = 0;
+
+/* config check function */
+static int vhost_check_config(apr_pool_t *p, apr_pool_t *plog,
+ apr_pool_t *ptemp, server_rec *s);
/*
* How it's used:
{
memset(iphash_table, 0, sizeof(iphash_table));
default_list = NULL;
- name_vhost_list = NULL;
- name_vhost_list_tail = &name_vhost_list;
+ ap_hook_check_config(vhost_check_config, NULL, NULL, APR_HOOK_MIDDLE);
}
* port is the default port to assume
*/
static const char *get_addresses(apr_pool_t *p, const char *w_,
- server_addr_rec ***paddr,
+ server_addr_rec ***paddr,
apr_port_t default_port)
{
apr_sockaddr_t *my_addr;
if (*w_ == '\0')
return NULL;
- w = apr_pstrdup(p, w_);
+ wlen = strlen(w_); /* wlen must be > 0 at this point */
+ w = apr_pstrmemdup(p, w_, wlen);
/* apr_parse_addr_port() doesn't understand ":*" so handle that first. */
- wlen = strlen(w); /* wlen must be > 0 at this point */
wild_port = 0;
if (w[wlen - 1] == '*') {
if (wlen < 2) {
port = default_port;
}
- if (strcmp(host, "*") == 0) {
- rv = apr_sockaddr_info_get(&my_addr, "0.0.0.0", APR_INET, port, 0, p);
- ap_assert(rv == APR_SUCCESS); /* must be bug or out of storage */
- }
- else if (strcasecmp(host, "_default_") == 0
- || strcmp(host, "255.255.255.255") == 0) {
- rv = apr_sockaddr_info_get(&my_addr, "255.255.255.255", APR_INET, port, 0, p);
- ap_assert(rv == APR_SUCCESS); /* must be bug or out of storage */
+ if (strcmp(host, "*") == 0 || strcasecmp(host, "_default_") == 0) {
+ rv = apr_sockaddr_info_get(&my_addr, NULL, APR_UNSPEC, port, 0, p);
+ if (rv) {
+ return "Could not determine a wildcard address ('0.0.0.0') -- "
+ "check resolver configuration.";
+ }
}
else {
rv = apr_sockaddr_info_get(&my_addr, host, APR_UNSPEC, port, 0, p);
if (rv != APR_SUCCESS) {
- ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
- "Cannot resolve host name %s --- ignoring!", host);
+ ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL, APLOGNO(00547)
+ "Could not 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! */
+ /* Remember all addresses for the host */
+
+ do {
+ sar = apr_pcalloc(p, sizeof(server_addr_rec));
+ **paddr = sar;
+ *paddr = &sar->next;
+ sar->host_addr = my_addr;
+ sar->host_port = port;
+ sar->virthost = host;
+ my_addr = my_addr->next;
+ } while (my_addr);
- sar = apr_pcalloc(p, sizeof(server_addr_rec));
- **paddr = sar;
- *paddr = &sar->next;
- sar->host_addr = my_addr;
- sar->host_port = port;
- sar->virthost = host;
return NULL;
}
}
-const char *ap_set_name_virtual_host(cmd_parms *cmd, void *dummy,
- const char *arg)
+AP_DECLARE_NONSTD(const char *)ap_set_name_virtual_host(cmd_parms *cmd,
+ void *dummy,
+ const char *arg)
{
- /* use whatever port the main server has at this point */
- return get_addresses(cmd->pool, arg, &name_vhost_list_tail,
- cmd->server->port);
+ static int warnonce = 0;
+ if (++warnonce == 1) {
+ ap_log_error(APLOG_MARK, APLOG_NOTICE|APLOG_STARTUP, APR_SUCCESS, NULL, APLOGNO(00548)
+ "NameVirtualHost has no effect and will be removed in the "
+ "next release %s:%d",
+ cmd->directive->filename,
+ cmd->directive->line_num);
+ }
+
+ return NULL;
}
}
qsort(count, IPHASH_TABLE_SIZE, sizeof(count[0]), iphash_compare);
p = buf + apr_snprintf(buf, sizeof(buf),
- "iphash: total hashed = %u, avg chain = %u, "
+ APLOGNO(03235) "iphash: total hashed = %u, avg chain = %u, "
"chain lengths (count x len):",
total, total / IPHASH_TABLE_SIZE);
total = 1;
}
p += apr_snprintf(p, sizeof(buf) - (p - buf), " %ux%u",
total, count[IPHASH_TABLE_SIZE - 1]);
+ /* Intentional no APLOGNO */
+ /* buf provides APLOGNO */
ap_log_error(APLOG_MARK, APLOG_DEBUG, main_s, buf);
}
#endif
new = apr_palloc(p, sizeof(*new));
new->names = NULL;
+ new->initialnames = NULL;
new->server = s;
new->sar = sar;
new->next = NULL;
static APR_INLINE ipaddr_chain *find_ipaddr(apr_sockaddr_t *sa)
{
unsigned bucket;
- ipaddr_chain *trav;
+ ipaddr_chain *trav = NULL;
+ ipaddr_chain *wild_match = NULL;
/* scan the hash table for an exact match first */
bucket = hash_addr(sa);
server_addr_rec *sar = trav->sar;
apr_sockaddr_t *cur = sar->host_addr;
- if (cur->port == 0 || sa->port == 0 || cur->port == sa->port) {
+ if (cur->port == sa->port) {
if (apr_sockaddr_equal(cur, sa)) {
return trav;
}
- }
+ }
+ if (wild_match == NULL && (cur->port == 0 || sa->port == 0)) {
+ if (apr_sockaddr_equal(cur, sa)) {
+ /* don't break, continue looking for an exact match */
+ wild_match = trav;
+ }
+ }
}
- return NULL;
+ return wild_match;
}
static ipaddr_chain *find_default_server(apr_port_t port)
{
server_addr_rec *sar;
- ipaddr_chain *trav;
+ ipaddr_chain *trav = NULL;
+ ipaddr_chain *wild_match = NULL;
for (trav = default_list; trav; trav = trav->next) {
sar = trav->sar;
- if (sar->host_port == 0 || sar->host_port == port) {
+ if (sar->host_port == port) {
/* match! */
return trav;
}
+ if (wild_match == NULL && sar->host_port == 0) {
+ /* don't break, continue looking for an exact match */
+ wild_match = trav;
+ }
}
- return NULL;
+ return wild_match;
}
+#if APR_HAVE_IPV6
+#define IS_IN6_ANYADDR(ad) ((ad)->family == APR_INET6 \
+ && IN6_IS_ADDR_UNSPECIFIED(&(ad)->sa.sin6.sin6_addr))
+#else
+#define IS_IN6_ANYADDR(ad) (0)
+#endif
+
static void dump_a_vhost(apr_file_t *f, ipaddr_chain *ic)
{
name_chain *nc;
char buf[MAX_STRING_LEN];
apr_sockaddr_t *ha = ic->sar->host_addr;
- if (ha->family == APR_INET &&
- ha->sa.sin.sin_addr.s_addr == DEFAULT_VHOST_ADDR) {
- len = apr_snprintf(buf, sizeof(buf), "_default_:%u",
- ic->sar->host_port);
- }
- else if (ha->family == APR_INET &&
- ha->sa.sin.sin_addr.s_addr == INADDR_ANY) {
+ if ((ha->family == APR_INET && ha->sa.sin.sin_addr.s_addr == INADDR_ANY)
+ || IS_IN6_ANYADDR(ha)) {
len = apr_snprintf(buf, sizeof(buf), "*:%u",
ic->sar->host_port);
}
apr_file_printf(f, "namevhost %s (%s:%u)\n",
nc->server->server_hostname,
nc->server->defn_name, nc->server->defn_line_number);
+ if (nc->server->names) {
+ apr_array_header_t *names = nc->server->names;
+ char **name = (char **)names->elts;
+ int i;
+ for (i = 0; i < names->nelts; ++i) {
+ if (name[i]) {
+ apr_file_printf(f, "%16s alias %s\n", "", name[i]);
+ }
+ }
+ }
+ if (nc->server->wild_names) {
+ apr_array_header_t *names = nc->server->wild_names;
+ char **name = (char **)names->elts;
+ int i;
+ for (i = 0; i < names->nelts; ++i) {
+ if (name[i]) {
+ apr_file_printf(f, "%16s wild alias %s\n", "", name[i]);
+ }
+ }
+ }
}
}
int i;
apr_file_printf(f, "VirtualHost configuration:\n");
+
+ /* non-wildcard servers */
for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
for (ic = iphash_table[i]; ic; ic = ic->next) {
dump_a_vhost(f, ic);
}
}
- if (default_list) {
- apr_file_printf(f, "wildcard NameVirtualHosts and _default_ servers:\n");
- for (ic = default_list; ic; ic = ic->next) {
- dump_a_vhost(f, ic);
- }
+
+ /* wildcard servers */
+ for (ic = default_list; ic; ic = ic->next) {
+ dump_a_vhost(f, ic);
}
}
+
/*
- * Two helper functions for ap_fini_vhost_config()
+ * When a second or later virtual host maps to the same IP chain,
+ * add the relevant server names to the chain. Special care is taken
+ * to avoid adding ic->names until we're sure there are multiple VH'es.
*/
-static int add_name_vhost_config(apr_pool_t *p, server_rec *main_s,
+static void add_name_vhost_config(apr_pool_t *p, server_rec *main_s,
server_rec *s, server_addr_rec *sar,
ipaddr_chain *ic)
{
- /* the first time we encounter a NameVirtualHost address
- * ic->server will be NULL, on subsequent encounters
- * ic->names will be non-NULL.
- */
- if (ic->names || ic->server == NULL) {
- name_chain *nc = new_name_chain(p, s, sar);
- nc->next = ic->names;
- ic->names = nc;
- ic->server = s;
- if (sar->host_port != ic->sar->host_port) {
- /* one of the two is a * port, the other isn't */
- ap_log_error(APLOG_MARK, APLOG_ERR, 0, main_s,
- "VirtualHost %s:%u -- mixing * "
- "ports and non-* ports with "
- "a NameVirtualHost address is not supported,"
- " proceeding with undefined results",
- sar->virthost, sar->host_port);
- }
- return 1;
- }
- else {
- /* IP-based vhosts are handled by the caller */
- return 0;
- }
-}
-static void remove_unused_name_vhosts(server_rec *main_s, ipaddr_chain **pic)
-{
- while (*pic) {
- ipaddr_chain *ic = *pic;
-
- if (ic->server == NULL) {
- ap_log_error(APLOG_MARK, APLOG_WARNING, 0, main_s,
- "NameVirtualHost %s:%u has no VirtualHosts",
- ic->sar->virthost, ic->sar->host_port);
- *pic = ic->next;
- }
- else {
- pic = &ic->next;
- }
- }
+ name_chain *nc = new_name_chain(p, s, sar);
+ nc->next = ic->names;
+
+ /* iterating backwards, so each one we see becomes the current default server */
+ ic->server = s;
+
+ if (ic->names == NULL) {
+ if (ic->initialnames == NULL) {
+ /* first pass, set these names aside in case we see another VH.
+ * Until then, this looks like an IP-based VH to runtime.
+ */
+ ic->initialnames = nc;
+ }
+ else {
+ /* second pass through this chain -- this really is an NVH, and we
+ * have two sets of names to link in.
+ */
+ nc->next = ic->initialnames;
+ ic->names = nc;
+ ic->initialnames = NULL;
+ }
+ }
+ else {
+ /* 3rd or more -- just keep stacking the names */
+ ic->names = nc;
+ }
}
/* compile the tables and such we need to do the run-time vhost lookups */
int i;
ipaddr_chain **iphash_table_tail[IPHASH_TABLE_SIZE];
- /* terminate the name_vhost list */
- *name_vhost_list_tail = NULL;
-
/* Main host first */
s = main_s;
iphash_table_tail[i] = &iphash_table[i];
}
- /* The first things to go into the hash table 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.
- */
- for (sar = name_vhost_list; sar; sar = sar->next) {
- char inaddr_any[16] = {0}; /* big enough to handle IPv4 or IPv6 */
- unsigned bucket = hash_addr(sar->host_addr);
- ipaddr_chain *ic = new_ipaddr_chain(p, NULL, sar);
-
- if (memcmp(sar->host_addr->ipaddr_ptr, inaddr_any,
- sar->host_addr->ipaddr_len)) { /* not IN[6]ADDR_ANY */
- *iphash_table_tail[bucket] = ic;
- iphash_table_tail[bucket] = &ic->next;
- }
- else {
- /* A wildcard NameVirtualHost goes on the default_list so
- * that it can catch incoming requests on any address.
- */
- ic->next = default_list;
- 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 table 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
*/
for (s = main_s->next; s; s = s->next) {
+ server_addr_rec *sar_prev = NULL;
has_default_vhost_addr = 0;
for (sar = s->addrs; sar; sar = sar->next) {
ipaddr_chain *ic;
char inaddr_any[16] = {0}; /* big enough to handle IPv4 or IPv6 */
-
- if ((sar->host_addr->family == AF_INET &&
- sar->host_addr->sa.sin.sin_addr.s_addr == DEFAULT_VHOST_ADDR)
- || !memcmp(sar->host_addr->ipaddr_ptr, inaddr_any, sar->host_addr->ipaddr_len)) {
+ /* XXX: this treats 0.0.0.0 as a "default" server which matches no-exact-match for IPv6 */
+ if (!memcmp(sar->host_addr->ipaddr_ptr, inaddr_any, sar->host_addr->ipaddr_len)) {
ic = find_default_server(sar->host_port);
- if (!ic || !add_name_vhost_config(p, main_s, s, sar, ic)) {
- if (ic && ic->sar->host_port != 0) {
- ap_log_error(APLOG_MARK, APLOG_WARNING,
- 0, main_s, "_default_ VirtualHost "
- "overlap on port %u, the first has "
- "precedence", sar->host_port);
+
+ if (ic && sar->host_port == ic->sar->host_port) { /* we're a match for an existing "default server" */
+ if (!sar_prev || memcmp(sar_prev->host_addr->ipaddr_ptr, inaddr_any, sar_prev->host_addr->ipaddr_len)
+ || sar_prev->host_port != sar->host_port) {
+ add_name_vhost_config(p, main_s, s, sar, ic);
}
+ }
+ else {
+ /* No default server, or we found a default server but
+ ** exactly one of us is a wildcard port, which means we want
+ ** two ip-based vhosts not an NVH with two names
+ */
ic = new_ipaddr_chain(p, s, sar);
ic->next = default_list;
default_list = ic;
+ add_name_vhost_config(p, main_s, s, sar, ic);
}
has_default_vhost_addr = 1;
}
/* see if it matches something we've already got */
ic = find_ipaddr(sar->host_addr);
- if (!ic) {
+ if (!ic || sar->host_port != ic->sar->host_port) {
+ /* No matching server, or we found a matching server but
+ ** exactly one of us is a wildcard port, which means we want
+ ** two ip-based vhosts not an NVH with two names
+ */
unsigned bucket = hash_addr(sar->host_addr);
-
ic = new_ipaddr_chain(p, s, sar);
ic->next = *iphash_table_tail[bucket];
*iphash_table_tail[bucket] = ic;
}
- else if (!add_name_vhost_config(p, main_s, s, sar, ic)) {
- ap_log_error(APLOG_MARK, APLOG_WARNING,
- 0, main_s, "VirtualHost %s:%u overlaps "
- "with VirtualHost %s:%u, the first has "
- "precedence, perhaps you need a "
- "NameVirtualHost directive",
- sar->virthost, sar->host_port,
- ic->sar->virthost, ic->sar->host_port);
- ic->sar = sar;
- ic->server = s;
- }
+ add_name_vhost_config(p, main_s, s, sar, ic);
}
+ sar_prev = sar;
}
/* Ok now we want to set up a server_hostname if the user was
char *ipaddr_str;
apr_sockaddr_ip_get(&ipaddr_str, s->addrs->host_addr);
- ap_log_error(APLOG_MARK, APLOG_ERR, rv, main_s,
+ ap_log_error(APLOG_MARK, APLOG_ERR, rv, main_s, APLOGNO(00549)
"Failed to resolve server name "
"for %s (check DNS) -- or specify an explicit "
"ServerName",
}
}
- /* 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) {
- remove_unused_name_vhosts(main_s, &iphash_table[i]);
- }
- remove_unused_name_vhosts(main_s, &default_list);
-
#ifdef IPHASH_STATISTICS
dump_iphash_statistics(main_s);
#endif
if (ap_exists_config_define("DUMP_VHOSTS")) {
apr_file_t *thefile = NULL;
- apr_file_open_stderr(&thefile, p);
+ apr_file_open_stdout(&thefile, p);
dump_vhost_config(thefile);
}
}
+static int vhost_check_config(apr_pool_t *p, apr_pool_t *plog,
+ apr_pool_t *ptemp, server_rec *s)
+{
+ return config_error ? !OK : OK;
+}
/*****************************************************************************
* run-time vhost matching functions
char *dst;
apr_port_t port;
apr_status_t rv;
+ const char *c;
/* According to RFC 2616, Host header field CAN be blank. */
if (!*r->hostname) {
return;
}
+ /* apr_parse_addr_port will interpret a bare integer as a port
+ * which is incorrect in this context. So treat it separately.
+ */
+ for (c = r->hostname; apr_isdigit(*c); ++c);
+ if (!*c) { /* pure integer */
+ return;
+ }
+
rv = apr_parse_addr_port(&host, &scope_id, &port, r->hostname, r->pool);
if (rv != APR_SUCCESS || scope_id) {
goto bad;
}
- if (!host && port) {
- /* 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
- */
- host = apr_itoa(r->pool, (int)port);
- }
- else if (port) {
+ if (port) {
/* Don't throw the Host: header's port number away:
- save it in parsed_uri -- ap_get_server_port() needs it! */
+ 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
- */
+ * Like r->hostname, there should be a r->portno
+ */
r->parsed_uri.port = port;
- r->parsed_uri.port_str = apr_itoa(r->pool, (int)port);
+ r->parsed_uri.port_str = apr_itoa(r->pool, (int)port);
}
- /* if the hostname is an IPv6 numeric address string, it was validated
- * already; otherwise, further validation is needed
+ /* if the hostname is an IPv6 numeric address string, it was validated
+ * already; otherwise, further validation is needed
*/
if (r->hostname[0] != '[') {
for (dst = host; *dst; dst++) {
bad:
r->status = HTTP_BAD_REQUEST;
- ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
- "Client sent malformed Host header");
+ ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00550)
+ "Client sent malformed Host header: %s",
+ r->hostname);
return;
}
/* search all the <VirtualHost> values */
/* XXX: If this is a NameVirtualHost then we may not be doing the Right Thing
- * consider:
+ * consider:
*
* NameVirtualHost 10.1.1.1
* <VirtualHost 10.1.1.1>
const char *host = r->hostname;
apr_port_t port;
server_rec *s;
+ server_rec *virthost_s;
server_rec *last_s;
name_chain *src;
+ virthost_s = NULL;
last_s = NULL;
port = r->connection->local_addr->port;
s = src->server;
- /* does it match the virthost from the sar? */
- if (!strcasecmp(host, sar->virthost)) {
- goto found;
- }
-
- if (s == last_s) {
- /* we've already done ServerName and ServerAlias checks for this
- * vhost
- */
- continue;
+ /* If we still need to do ServerName and ServerAlias checks for this
+ * server, do them now.
+ */
+ if (s != last_s) {
+ /* does it match any ServerName or ServerAlias directive? */
+ if (matches_aliases(s, host)) {
+ goto found;
+ }
}
last_s = s;
- if (matches_aliases(s, host)) {
- goto found;
+ /* Fallback: does it match the virthost from the sar? */
+ if (!strcasecmp(host, sar->virthost)) {
+ /* only the first match is used */
+ if (virthost_s == NULL) {
+ virthost_s = s;
+ }
}
}
+
+ /* If ServerName and ServerAlias check failed, we end up here. If it
+ * matches a VirtualHost, virthost_s is set. Use that as fallback
+ */
+ if (virthost_s) {
+ s = virthost_s;
+ goto found;
+ }
+
return;
found:
}
}
+/**
+ * For every virtual host on this connection, call func_cb.
+ */
+AP_DECLARE(int) ap_vhost_iterate_given_conn(conn_rec *conn,
+ ap_vhost_iterate_conn_cb func_cb,
+ void* baton)
+{
+ server_rec *s;
+ server_rec *last_s;
+ name_chain *src;
+ apr_port_t port;
+ int rv = 0;
+
+ if (conn->vhost_lookup_data) {
+ last_s = NULL;
+ port = conn->local_addr->port;
+
+ for (src = conn->vhost_lookup_data; src; src = src->next) {
+ server_addr_rec *sar;
+
+ /* We only consider addresses on the name_chain which have a
+ * matching port.
+ */
+ sar = src->sar;
+ if (sar->host_port != 0 && port != sar->host_port) {
+ continue;
+ }
+
+ s = src->server;
+
+ if (s == last_s) {
+ /* we've already done a callback for this vhost. */
+ continue;
+ }
+
+ last_s = s;
+
+ rv = func_cb(baton, conn, s);
+
+ if (rv != 0) {
+ break;
+ }
+ }
+ }
+ else {
+ rv = func_cb(baton, conn, conn->base_server);
+ }
+
+ return rv;
+}
/* Called for a new connection which has a known local_addr. Note that the
* new connection is assumed to have conn->server == main server.