]> granicus.if.org Git - apache/blob - server/vhost.c
Add ap_vhost_iterate_given_conn() as I had previously mentioned on the mailing list.
[apache] / server / vhost.c
1 /* Copyright 1999-2005 The Apache Software Foundation or its licensors, as
2  * applicable.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * http_vhost.c: functions pertaining to virtual host addresses
19  *      (configuration and run-time)
20  */
21
22 #include "apr.h"
23 #include "apr_strings.h"
24 #include "apr_lib.h"
25
26 #define APR_WANT_STRFUNC
27 #include "apr_want.h"
28
29 #define CORE_PRIVATE
30 #include "ap_config.h"
31 #include "httpd.h"
32 #include "http_config.h"
33 #include "http_log.h"
34 #include "http_vhost.h"
35 #include "http_protocol.h"
36 #include "http_core.h"
37
38 #if APR_HAVE_ARPA_INET_H
39 #include <arpa/inet.h>
40 #endif
41
42 /*
43  * After all the definitions there's an explanation of how it's all put
44  * together.
45  */
46
47 /* meta-list of name-vhosts.  Each server_rec can be in possibly multiple
48  * lists of name-vhosts.
49  */
50 typedef struct name_chain name_chain;
51 struct name_chain {
52     name_chain *next;
53     server_addr_rec *sar;       /* the record causing it to be in
54                                  * this chain (needed for port comparisons) */
55     server_rec *server;         /* the server to use on a match */
56 };
57
58 /* meta-list of ip addresses.  Each server_rec can be in possibly multiple
59  * hash chains since it can have multiple ips.
60  */
61 typedef struct ipaddr_chain ipaddr_chain;
62 struct ipaddr_chain {
63     ipaddr_chain *next;
64     server_addr_rec *sar;       /* the record causing it to be in
65                                  * this chain (need for both ip addr and port
66                                  * comparisons) */
67     server_rec *server;         /* the server to use if this matches */
68     name_chain *names;          /* if non-NULL then a list of name-vhosts
69                                  * sharing this address */
70 };
71
72 /* This defines the size of the hash table used for hashing ip addresses
73  * of virtual hosts.  It must be a power of two.
74  */
75 #ifndef IPHASH_TABLE_SIZE
76 #define IPHASH_TABLE_SIZE 256
77 #endif
78
79 /* A (n) bucket hash table, each entry has a pointer to a server rec and
80  * a pointer to the other entries in that bucket.  Each individual address,
81  * even for virtualhosts with multiple addresses, has an entry in this hash
82  * table.  There are extra buckets for _default_, and name-vhost entries.
83  *
84  * Note that after config time this is constant, so it is thread-safe.
85  */
86 static ipaddr_chain *iphash_table[IPHASH_TABLE_SIZE];
87
88 /* dump out statistics about the hash function */
89 /* #define IPHASH_STATISTICS */
90
91 /* list of the _default_ servers */
92 static ipaddr_chain *default_list;
93
94 /* list of the NameVirtualHost addresses */
95 static server_addr_rec *name_vhost_list;
96 static server_addr_rec **name_vhost_list_tail;
97
98 /*
99  * How it's used:
100  *
101  * The ip address determines which chain in iphash_table is interesting, then
102  * a comparison is done down that chain to find the first ipaddr_chain whose
103  * sar matches the address:port pair.
104  *
105  * If that ipaddr_chain has names == NULL then you're done, it's an ip-vhost.
106  *
107  * Otherwise it's a name-vhost list, and the default is the server in the
108  * ipaddr_chain record.  We tuck away the ipaddr_chain record in the
109  * conn_rec field vhost_lookup_data.  Later on after the headers we get a
110  * second chance, and we use the name_chain to figure out what name-vhost
111  * matches the headers.
112  *
113  * If there was no ip address match in the iphash_table then do a lookup
114  * in the default_list.
115  *
116  * How it's put together ... well you should be able to figure that out
117  * from how it's used.  Or something like that.
118  */
119
120
121 /* called at the beginning of the config */
122 AP_DECLARE(void) ap_init_vhost_config(apr_pool_t *p)
123 {
124     memset(iphash_table, 0, sizeof(iphash_table));
125     default_list = NULL;
126     name_vhost_list = NULL;
127     name_vhost_list_tail = &name_vhost_list;
128 }
129
130
131 /*
132  * Parses a host of the form <address>[:port]
133  * paddr is used to create a list in the order of input
134  * **paddr is the ->next pointer of the last entry (or s->addrs)
135  * *paddr is the variable used to keep track of **paddr between calls
136  * port is the default port to assume
137  */
138 static const char *get_addresses(apr_pool_t *p, const char *w_,
139                                  server_addr_rec ***paddr, 
140                                  apr_port_t default_port)
141 {
142     apr_sockaddr_t *my_addr;
143     server_addr_rec *sar;
144     char *w, *host, *scope_id;
145     int wild_port;
146     apr_size_t wlen;
147     apr_port_t port;
148     apr_status_t rv;
149
150     if (*w_ == '\0')
151         return NULL;
152
153     w = apr_pstrdup(p, w_);
154     /* apr_parse_addr_port() doesn't understand ":*" so handle that first. */
155     wlen = strlen(w);                    /* wlen must be > 0 at this point */
156     wild_port = 0;
157     if (w[wlen - 1] == '*') {
158         if (wlen < 2) {
159             wild_port = 1;
160         }
161         else if (w[wlen - 2] == ':') {
162             w[wlen - 2] = '\0';
163             wild_port = 1;
164         }
165     }
166     rv = apr_parse_addr_port(&host, &scope_id, &port, w, p);
167     /* If the string is "80", apr_parse_addr_port() will be happy and set
168      * host to NULL and port to 80, so watch out for that.
169      */
170     if (rv != APR_SUCCESS) {
171         return "The address or port is invalid";
172     }
173     if (!host) {
174         return "Missing address for VirtualHost";
175     }
176     if (scope_id) {
177         return "Scope ids are not supported";
178     }
179     if (!port && !wild_port) {
180         port = default_port;
181     }
182
183     if (strcmp(host, "*") == 0) {
184         rv = apr_sockaddr_info_get(&my_addr, "0.0.0.0", APR_INET, port, 0, p);
185         ap_assert(rv == APR_SUCCESS); /* must be bug or out of storage */
186     }
187     else if (strcasecmp(host, "_default_") == 0
188         || strcmp(host, "255.255.255.255") == 0) {
189         rv = apr_sockaddr_info_get(&my_addr, "255.255.255.255", APR_INET, port, 0, p);
190         ap_assert(rv == APR_SUCCESS); /* must be bug or out of storage */
191     }
192     else {
193         rv = apr_sockaddr_info_get(&my_addr, host, APR_UNSPEC, port, 0, p);
194         if (rv != APR_SUCCESS) {
195             ap_log_error(APLOG_MARK, APLOG_ERR, rv, NULL,
196                 "Cannot resolve host name %s --- ignoring!", host);
197             return NULL;
198         }
199     }
200
201     /* Remember all addresses for the host */
202
203     do {
204         sar = apr_pcalloc(p, sizeof(server_addr_rec));
205         **paddr = sar;
206         *paddr = &sar->next;
207         sar->host_addr = my_addr;
208         sar->host_port = port;
209         sar->virthost = host;
210         my_addr = my_addr->next;
211     } while (my_addr);
212
213     return NULL;
214 }
215
216
217 /* parse the <VirtualHost> addresses */
218 const char *ap_parse_vhost_addrs(apr_pool_t *p,
219                                  const char *hostname,
220                                  server_rec *s)
221 {
222     server_addr_rec **addrs;
223     const char *err;
224
225     /* start the list of addreses */
226     addrs = &s->addrs;
227     while (hostname[0]) {
228         err = get_addresses(p, ap_getword_conf(p, &hostname), &addrs, s->port);
229         if (err) {
230             *addrs = NULL;
231             return err;
232         }
233     }
234     /* terminate the list */
235     *addrs = NULL;
236     if (s->addrs) {
237         if (s->addrs->host_port) {
238             /* override the default port which is inherited from main_server */
239             s->port = s->addrs->host_port;
240         }
241     }
242     return NULL;
243 }
244
245
246 const char *ap_set_name_virtual_host(cmd_parms *cmd, void *dummy,
247                                      const char *arg)
248 {
249     /* use whatever port the main server has at this point */
250     return get_addresses(cmd->pool, arg, &name_vhost_list_tail,
251                          cmd->server->port);
252 }
253
254
255 /* hash table statistics, keep this in here for the beta period so
256  * we can find out if the hash function is ok
257  */
258 #ifdef IPHASH_STATISTICS
259 static int iphash_compare(const void *a, const void *b)
260 {
261     return (*(const int *) b - *(const int *) a);
262 }
263
264
265 static void dump_iphash_statistics(server_rec *main_s)
266 {
267     unsigned count[IPHASH_TABLE_SIZE];
268     int i;
269     ipaddr_chain *src;
270     unsigned total;
271     char buf[HUGE_STRING_LEN];
272     char *p;
273
274     total = 0;
275     for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
276         count[i] = 0;
277         for (src = iphash_table[i]; src; src = src->next) {
278             ++count[i];
279             if (i < IPHASH_TABLE_SIZE) {
280                 /* don't count the slop buckets in the total */
281                 ++total;
282             }
283         }
284     }
285     qsort(count, IPHASH_TABLE_SIZE, sizeof(count[0]), iphash_compare);
286     p = buf + apr_snprintf(buf, sizeof(buf),
287                            "iphash: total hashed = %u, avg chain = %u, "
288                            "chain lengths (count x len):",
289                            total, total / IPHASH_TABLE_SIZE);
290     total = 1;
291     for (i = 1; i < IPHASH_TABLE_SIZE; ++i) {
292         if (count[i - 1] != count[i]) {
293             p += apr_snprintf(p, sizeof(buf) - (p - buf), " %ux%u",
294                               total, count[i - 1]);
295             total = 1;
296         }
297         else {
298             ++total;
299         }
300     }
301     p += apr_snprintf(p, sizeof(buf) - (p - buf), " %ux%u",
302                       total, count[IPHASH_TABLE_SIZE - 1]);
303     ap_log_error(APLOG_MARK, APLOG_DEBUG, main_s, buf);
304 }
305 #endif
306
307
308 /* This hashing function is designed to get good distribution in the cases
309  * where the server is handling entire "networks" of servers.  i.e. a
310  * whack of /24s.  This is probably the most common configuration for
311  * ISPs with large virtual servers.
312  *
313  * NOTE: This function is symmetric (i.e. collapses all 4 octets
314  * into one), so machine byte order (big/little endianness) does not matter.
315  *
316  * Hash function provided by David Hankins.
317  */
318 static APR_INLINE unsigned hash_inaddr(unsigned key)
319 {
320     key ^= (key >> 16);
321     return ((key >> 8) ^ key) % IPHASH_TABLE_SIZE;
322 }
323
324 static APR_INLINE unsigned hash_addr(struct apr_sockaddr_t *sa)
325 {
326     unsigned key;
327
328     /* The key is the last four bytes of the IP address.
329      * For IPv4, this is the entire address, as always.
330      * For IPv6, this is usually part of the MAC address.
331      */
332     key = *(unsigned *)((char *)sa->ipaddr_ptr + sa->ipaddr_len - 4);
333     return hash_inaddr(key);
334 }
335
336 static ipaddr_chain *new_ipaddr_chain(apr_pool_t *p,
337                                       server_rec *s, server_addr_rec *sar)
338 {
339     ipaddr_chain *new;
340
341     new = apr_palloc(p, sizeof(*new));
342     new->names = NULL;
343     new->server = s;
344     new->sar = sar;
345     new->next = NULL;
346     return new;
347 }
348
349
350 static name_chain *new_name_chain(apr_pool_t *p,
351                                   server_rec *s, server_addr_rec *sar)
352 {
353     name_chain *new;
354
355     new = apr_palloc(p, sizeof(*new));
356     new->server = s;
357     new->sar = sar;
358     new->next = NULL;
359     return new;
360 }
361
362
363 static APR_INLINE ipaddr_chain *find_ipaddr(apr_sockaddr_t *sa)
364 {
365     unsigned bucket;
366     ipaddr_chain *trav;
367
368     /* scan the hash table for an exact match first */
369     bucket = hash_addr(sa);
370     for (trav = iphash_table[bucket]; trav; trav = trav->next) {
371         server_addr_rec *sar = trav->sar;
372         apr_sockaddr_t *cur = sar->host_addr;
373
374         if (cur->port == 0 || sa->port == 0 || cur->port == sa->port) {
375             if (apr_sockaddr_equal(cur, sa)) {
376                 return trav;
377             }
378         }    
379     }
380     return NULL;
381 }
382
383 static ipaddr_chain *find_default_server(apr_port_t port)
384 {
385     server_addr_rec *sar;
386     ipaddr_chain *trav;
387
388     for (trav = default_list; trav; trav = trav->next) {
389         sar = trav->sar;
390         if (sar->host_port == 0 || sar->host_port == port) {
391             /* match! */
392             return trav;
393         }
394     }
395     return NULL;
396 }
397
398 static void dump_a_vhost(apr_file_t *f, ipaddr_chain *ic)
399 {
400     name_chain *nc;
401     int len;
402     char buf[MAX_STRING_LEN];
403     apr_sockaddr_t *ha = ic->sar->host_addr;
404
405     if (ha->family == APR_INET &&
406         ha->sa.sin.sin_addr.s_addr == DEFAULT_VHOST_ADDR) {
407         len = apr_snprintf(buf, sizeof(buf), "_default_:%u",
408                            ic->sar->host_port);
409     }
410     else if (ha->family == APR_INET &&
411              ha->sa.sin.sin_addr.s_addr == INADDR_ANY) {
412         len = apr_snprintf(buf, sizeof(buf), "*:%u",
413                            ic->sar->host_port);
414     }
415     else {
416         len = apr_snprintf(buf, sizeof(buf), "%pI", ha);
417     }
418     if (ic->sar->host_port == 0) {
419         buf[len-1] = '*';
420     }
421     if (ic->names == NULL) {
422         apr_file_printf(f, "%-22s %s (%s:%u)\n", buf,
423                         ic->server->server_hostname,
424                         ic->server->defn_name, ic->server->defn_line_number);
425         return;
426     }
427     apr_file_printf(f, "%-22s is a NameVirtualHost\n"
428                     "%8s default server %s (%s:%u)\n",
429                     buf, "", ic->server->server_hostname,
430                     ic->server->defn_name, ic->server->defn_line_number);
431     for (nc = ic->names; nc; nc = nc->next) {
432         if (nc->sar->host_port) {
433             apr_file_printf(f, "%8s port %u ", "", nc->sar->host_port);
434         }
435         else {
436             apr_file_printf(f, "%8s port * ", "");
437         }
438         apr_file_printf(f, "namevhost %s (%s:%u)\n",
439                         nc->server->server_hostname,
440                         nc->server->defn_name, nc->server->defn_line_number);
441     }
442 }
443
444 static void dump_vhost_config(apr_file_t *f)
445 {
446     ipaddr_chain *ic;
447     int i;
448
449     apr_file_printf(f, "VirtualHost configuration:\n");
450     for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
451         for (ic = iphash_table[i]; ic; ic = ic->next) {
452             dump_a_vhost(f, ic);
453         }
454     }
455     if (default_list) {
456         apr_file_printf(f, "wildcard NameVirtualHosts and _default_ servers:\n");
457         for (ic = default_list; ic; ic = ic->next) {
458             dump_a_vhost(f, ic);
459         }
460     }
461 }
462
463 /*
464  * Two helper functions for ap_fini_vhost_config()
465  */
466 static int add_name_vhost_config(apr_pool_t *p, server_rec *main_s,
467                                  server_rec *s, server_addr_rec *sar,
468                                  ipaddr_chain *ic)
469 {
470     /* the first time we encounter a NameVirtualHost address
471      * ic->server will be NULL, on subsequent encounters
472      * ic->names will be non-NULL.
473      */
474     if (ic->names || ic->server == NULL) {
475         name_chain *nc = new_name_chain(p, s, sar);
476         nc->next = ic->names;
477         ic->names = nc;
478         ic->server = s;
479         if (sar->host_port != ic->sar->host_port) {
480             /* one of the two is a * port, the other isn't */
481             ap_log_error(APLOG_MARK, APLOG_ERR, 0, main_s,
482                          "VirtualHost %s:%u -- mixing * "
483                          "ports and non-* ports with "
484                          "a NameVirtualHost address is not supported,"
485                          " proceeding with undefined results",
486                          sar->virthost, sar->host_port);
487         }
488         return 1;
489     }
490     else {
491         /* IP-based vhosts are handled by the caller */
492         return 0;
493     }
494 }
495
496 static void remove_unused_name_vhosts(server_rec *main_s, ipaddr_chain **pic)
497 {
498     while (*pic) {
499         ipaddr_chain *ic = *pic;
500         
501         if (ic->server == NULL) {
502             ap_log_error(APLOG_MARK, APLOG_WARNING, 0, main_s,
503                          "NameVirtualHost %s:%u has no VirtualHosts",
504                          ic->sar->virthost, ic->sar->host_port);
505             *pic = ic->next;
506         }
507         else {
508             pic = &ic->next;
509         }
510     }
511 }
512
513 /* compile the tables and such we need to do the run-time vhost lookups */
514 AP_DECLARE(void) ap_fini_vhost_config(apr_pool_t *p, server_rec *main_s)
515 {
516     server_addr_rec *sar;
517     int has_default_vhost_addr;
518     server_rec *s;
519     int i;
520     ipaddr_chain **iphash_table_tail[IPHASH_TABLE_SIZE];
521
522     /* terminate the name_vhost list */
523     *name_vhost_list_tail = NULL;
524
525     /* Main host first */
526     s = main_s;
527
528     if (!s->server_hostname) {
529         s->server_hostname = ap_get_local_host(p);
530     }
531
532     /* initialize the tails */
533     for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
534         iphash_table_tail[i] = &iphash_table[i];
535     }
536
537     /* The first things to go into the hash table are the NameVirtualHosts
538      * Since name_vhost_list is in the same order that the directives
539      * occured in the config file, we'll copy it in that order.
540      */
541     for (sar = name_vhost_list; sar; sar = sar->next) {
542         char inaddr_any[16] = {0}; /* big enough to handle IPv4 or IPv6 */
543         unsigned bucket = hash_addr(sar->host_addr);
544         ipaddr_chain *ic = new_ipaddr_chain(p, NULL, sar);
545
546         if (memcmp(sar->host_addr->ipaddr_ptr, inaddr_any, 
547                    sar->host_addr->ipaddr_len)) { /* not IN[6]ADDR_ANY */
548             *iphash_table_tail[bucket] = ic;
549             iphash_table_tail[bucket] = &ic->next;
550         }
551         else {
552             /* A wildcard NameVirtualHost goes on the default_list so
553              * that it can catch incoming requests on any address.
554              */
555             ic->next = default_list;
556             default_list = ic;
557         }
558         /* Notice that what we've done is insert an ipaddr_chain with
559          * both server and names NULL. This fact is used to spot name-
560          * based vhosts in add_name_vhost_config().
561          */
562     }
563
564     /* The next things to go into the hash table are the virtual hosts
565      * themselves.  They're listed off of main_s->next in the reverse
566      * order they occured in the config file, so we insert them at
567      * the iphash_table_tail but don't advance the tail.
568      */
569
570     for (s = main_s->next; s; s = s->next) {
571         has_default_vhost_addr = 0;
572         for (sar = s->addrs; sar; sar = sar->next) {
573             ipaddr_chain *ic;
574             char inaddr_any[16] = {0}; /* big enough to handle IPv4 or IPv6 */
575
576             if ((sar->host_addr->family == AF_INET &&
577                  sar->host_addr->sa.sin.sin_addr.s_addr == DEFAULT_VHOST_ADDR)
578                 || !memcmp(sar->host_addr->ipaddr_ptr, inaddr_any, sar->host_addr->ipaddr_len)) {
579                 ic = find_default_server(sar->host_port);
580                 if (!ic || !add_name_vhost_config(p, main_s, s, sar, ic)) {
581                     if (ic && ic->sar->host_port != 0) {
582                         ap_log_error(APLOG_MARK, APLOG_WARNING,
583                                      0, main_s, "_default_ VirtualHost "
584                                      "overlap on port %u, the first has "
585                                      "precedence", sar->host_port);
586                     }
587                     ic = new_ipaddr_chain(p, s, sar);
588                     ic->next = default_list;
589                     default_list = ic;
590                 }
591                 has_default_vhost_addr = 1;
592             }
593             else {
594                 /* see if it matches something we've already got */
595                 ic = find_ipaddr(sar->host_addr);
596
597                 if (!ic) {
598                     unsigned bucket = hash_addr(sar->host_addr);
599
600                     ic = new_ipaddr_chain(p, s, sar);
601                     ic->next = *iphash_table_tail[bucket];
602                     *iphash_table_tail[bucket] = ic;
603                 }
604                 else if (!add_name_vhost_config(p, main_s, s, sar, ic)) {
605                     ap_log_error(APLOG_MARK, APLOG_WARNING,
606                                  0, main_s, "VirtualHost %s:%u overlaps "
607                                  "with VirtualHost %s:%u, the first has "
608                                  "precedence, perhaps you need a "
609                                  "NameVirtualHost directive",
610                                  sar->virthost, sar->host_port,
611                                  ic->sar->virthost, ic->sar->host_port);
612                     ic->sar = sar;
613                     ic->server = s;
614                 }
615             }
616         }
617
618         /* Ok now we want to set up a server_hostname if the user was
619          * silly enough to forget one.
620          * XXX: This is silly we should just crash and burn.
621          */
622         if (!s->server_hostname) {
623             if (has_default_vhost_addr) {
624                 s->server_hostname = main_s->server_hostname;
625             }
626             else if (!s->addrs) {
627                 /* what else can we do?  at this point this vhost has
628                     no configured name, probably because they used
629                     DNS in the VirtualHost statement.  It's disabled
630                     anyhow by the host matching code.  -djg */
631                 s->server_hostname =
632                     apr_pstrdup(p, "bogus_host_without_forward_dns");
633             }
634             else {
635                 apr_status_t rv;
636                 char *hostname;
637
638                 rv = apr_getnameinfo(&hostname, s->addrs->host_addr, 0);
639                 if (rv == APR_SUCCESS) {
640                     s->server_hostname = apr_pstrdup(p, hostname);
641                 }
642                 else {
643                     /* again, what can we do?  They didn't specify a
644                        ServerName, and their DNS isn't working. -djg */
645                     char *ipaddr_str;
646
647                     apr_sockaddr_ip_get(&ipaddr_str, s->addrs->host_addr);
648                     ap_log_error(APLOG_MARK, APLOG_ERR, rv, main_s,
649                                  "Failed to resolve server name "
650                                  "for %s (check DNS) -- or specify an explicit "
651                                  "ServerName",
652                                  ipaddr_str);
653                     s->server_hostname =
654                         apr_pstrdup(p, "bogus_host_without_reverse_dns");
655                 }
656             }
657         }
658     }
659
660     /* now go through and delete any NameVirtualHosts that didn't have any
661      * hosts associated with them.  Lamers.
662      */
663     for (i = 0; i < IPHASH_TABLE_SIZE; ++i) {
664         remove_unused_name_vhosts(main_s, &iphash_table[i]);
665     }
666     remove_unused_name_vhosts(main_s, &default_list);
667
668 #ifdef IPHASH_STATISTICS
669     dump_iphash_statistics(main_s);
670 #endif
671     if (ap_exists_config_define("DUMP_VHOSTS")) {
672         apr_file_t *thefile = NULL;
673         apr_file_open_stderr(&thefile, p);
674         dump_vhost_config(thefile);
675     }
676 }
677
678
679 /*****************************************************************************
680  * run-time vhost matching functions
681  */
682
683 /* Lowercase and remove any trailing dot and/or :port from the hostname,
684  * and check that it is sane.
685  *
686  * In most configurations the exact syntax of the hostname isn't
687  * important so strict sanity checking isn't necessary. However, in
688  * mass hosting setups (using mod_vhost_alias or mod_rewrite) where
689  * the hostname is interpolated into the filename, we need to be sure
690  * that the interpolation doesn't expose parts of the filesystem.
691  * We don't do strict RFC 952 / RFC 1123 syntax checking in order
692  * to support iDNS and people who erroneously use underscores.
693  * Instead we just check for filesystem metacharacters: directory
694  * separators / and \ and sequences of more than one dot.
695  */
696 static void fix_hostname(request_rec *r)
697 {
698     char *host, *scope_id;
699     char *dst;
700     apr_port_t port;
701     apr_status_t rv;
702
703     /* According to RFC 2616, Host header field CAN be blank. */
704     if (!*r->hostname) {
705         return;
706     }
707
708     rv = apr_parse_addr_port(&host, &scope_id, &port, r->hostname, r->pool);
709     if (rv != APR_SUCCESS || scope_id) {
710         goto bad;
711     }
712
713     if (!host && port) {
714         /* silly looking host ("Host: 123") but that isn't our job
715          * here to judge; apr_parse_addr_port() would think we had a port
716          * but no address
717          */
718         host = apr_itoa(r->pool, (int)port);
719     }
720     else if (port) {
721         /* Don't throw the Host: header's port number away:
722            save it in parsed_uri -- ap_get_server_port() needs it! */
723         /* @@@ XXX there should be a better way to pass the port.
724          *         Like r->hostname, there should be a r->portno
725          */
726         r->parsed_uri.port = port;
727         r->parsed_uri.port_str = apr_itoa(r->pool, (int)port);
728     }
729
730     /* if the hostname is an IPv6 numeric address string, it was validated 
731      * already; otherwise, further validation is needed 
732      */
733     if (r->hostname[0] != '[') {
734         for (dst = host; *dst; dst++) {
735             if (apr_islower(*dst)) {
736                 /* leave char unchanged */
737             }
738             else if (*dst == '.') {
739                 if (*(dst + 1) == '.') {
740                     goto bad;
741                 }
742             }
743             else if (apr_isupper(*dst)) {
744                 *dst = apr_tolower(*dst);
745             }
746             else if (*dst == '/' || *dst == '\\') {
747                 goto bad;
748             }
749         }
750         /* strip trailing gubbins */
751         if (dst > host && dst[-1] == '.') {
752             dst[-1] = '\0';
753         }
754     }
755     r->hostname = host;
756     return;
757
758 bad:
759     r->status = HTTP_BAD_REQUEST;
760     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
761                   "Client sent malformed Host header");
762     return;
763 }
764
765
766 /* return 1 if host matches ServerName or ServerAliases */
767 static int matches_aliases(server_rec *s, const char *host)
768 {
769     int i;
770     apr_array_header_t *names;
771
772     /* match ServerName */
773     if (!strcasecmp(host, s->server_hostname)) {
774         return 1;
775     }
776
777     /* search all the aliases from ServerAlias directive */
778     names = s->names;
779     if (names) {
780         char **name = (char **) names->elts;
781         for (i = 0; i < names->nelts; ++i) {
782             if(!name[i]) continue;
783             if (!strcasecmp(host, name[i]))
784                 return 1;
785         }
786     }
787     names = s->wild_names;
788     if (names) {
789         char **name = (char **) names->elts;
790         for (i = 0; i < names->nelts; ++i) {
791             if(!name[i]) continue;
792             if (!ap_strcasecmp_match(host, name[i]))
793                 return 1;
794         }
795     }
796     return 0;
797 }
798
799
800 /* Suppose a request came in on the same socket as this r, and included
801  * a header "Host: host:port", would it map to r->server?  It's more
802  * than just that though.  When we do the normal matches for each request
803  * we don't even bother considering Host: etc on non-namevirtualhosts,
804  * we just call it a match.  But here we require the host:port to match
805  * the ServerName and/or ServerAliases.
806  */
807 AP_DECLARE(int) ap_matches_request_vhost(request_rec *r, const char *host,
808                                          apr_port_t port)
809 {
810     server_rec *s;
811     server_addr_rec *sar;
812
813     s = r->server;
814
815     /* search all the <VirtualHost> values */
816     /* XXX: If this is a NameVirtualHost then we may not be doing the Right Thing
817      * consider: 
818      *
819      *     NameVirtualHost 10.1.1.1
820      *     <VirtualHost 10.1.1.1>
821      *     ServerName v1
822      *     </VirtualHost>
823      *     <VirtualHost 10.1.1.1>
824      *     ServerName v2
825      *     </VirtualHost>
826      *
827      * Suppose r->server is v2, and we're asked to match "10.1.1.1".  We'll say
828      * "yup it's v2", when really it isn't... if a request came in for 10.1.1.1
829      * it would really go to v1.
830      */
831     for (sar = s->addrs; sar; sar = sar->next) {
832         if ((sar->host_port == 0 || port == sar->host_port)
833             && !strcasecmp(host, sar->virthost)) {
834             return 1;
835         }
836     }
837
838     /* the Port has to match now, because the rest don't have ports associated
839      * with them. */
840     if (port != s->port) {
841         return 0;
842     }
843
844     return matches_aliases(s, host);
845 }
846
847
848 static void check_hostalias(request_rec *r)
849 {
850     /*
851      * Even if the request has a Host: header containing a port we ignore
852      * that port.  We always use the physical port of the socket.  There
853      * are a few reasons for this:
854      *
855      * - the default of 80 or 443 for SSL is easier to handle this way
856      * - there is less of a possibility of a security problem
857      * - it simplifies the data structure
858      * - the client may have no idea that a proxy somewhere along the way
859      *   translated the request to another ip:port
860      * - except for the addresses from the VirtualHost line, none of the other
861      *   names we'll match have ports associated with them
862      */
863     const char *host = r->hostname;
864     apr_port_t port;
865     server_rec *s;
866     server_rec *last_s;
867     name_chain *src;
868
869     last_s = NULL;
870
871     port = r->connection->local_addr->port;
872
873     /* Recall that the name_chain is a list of server_addr_recs, some of
874      * whose ports may not match.  Also each server may appear more than
875      * once in the chain -- specifically, it will appear once for each
876      * address from its VirtualHost line which matched.  We only want to
877      * do the full ServerName/ServerAlias comparisons once for each
878      * server, fortunately we know that all the VirtualHost addresses for
879      * a single server are adjacent to each other.
880      */
881
882     for (src = r->connection->vhost_lookup_data; src; src = src->next) {
883         server_addr_rec *sar;
884
885         /* We only consider addresses on the name_chain which have a matching
886          * port
887          */
888         sar = src->sar;
889         if (sar->host_port != 0 && port != sar->host_port) {
890             continue;
891         }
892
893         s = src->server;
894
895         /* does it match the virthost from the sar? */
896         if (!strcasecmp(host, sar->virthost)) {
897             goto found;
898         }
899
900         if (s == last_s) {
901             /* we've already done ServerName and ServerAlias checks for this
902              * vhost
903              */
904             continue;
905         }
906         last_s = s;
907
908         if (matches_aliases(s, host)) {
909             goto found;
910         }
911     }
912     return;
913
914 found:
915     /* s is the first matching server, we're done */
916     r->server = s;
917 }
918
919
920 static void check_serverpath(request_rec *r)
921 {
922     server_rec *s;
923     server_rec *last_s;
924     name_chain *src;
925     apr_port_t port;
926
927     port = r->connection->local_addr->port;
928
929     /*
930      * This is in conjunction with the ServerPath code in http_core, so we
931      * get the right host attached to a non- Host-sending request.
932      *
933      * See the comment in check_hostalias about how each vhost can be
934      * listed multiple times.
935      */
936
937     last_s = NULL;
938     for (src = r->connection->vhost_lookup_data; src; src = src->next) {
939         /* We only consider addresses on the name_chain which have a matching
940          * port
941          */
942         if (src->sar->host_port != 0 && port != src->sar->host_port) {
943             continue;
944         }
945
946         s = src->server;
947         if (s == last_s) {
948             continue;
949         }
950         last_s = s;
951
952         if (s->path && !strncmp(r->uri, s->path, s->pathlen) &&
953             (s->path[s->pathlen - 1] == '/' ||
954              r->uri[s->pathlen] == '/' ||
955              r->uri[s->pathlen] == '\0')) {
956             r->server = s;
957             return;
958         }
959     }
960 }
961
962
963 AP_DECLARE(void) ap_update_vhost_from_headers(request_rec *r)
964 {
965     /* must set this for HTTP/1.1 support */
966     if (r->hostname || (r->hostname = apr_table_get(r->headers_in, "Host"))) {
967         fix_hostname(r);
968         if (r->status != HTTP_OK)
969             return;
970     }
971     /* check if we tucked away a name_chain */
972     if (r->connection->vhost_lookup_data) {
973         if (r->hostname)
974             check_hostalias(r);
975         else
976             check_serverpath(r);
977     }
978 }
979
980 /**
981  * For every virtual host on this connection, call func_cb.
982  */
983 AP_DECLARE(int) ap_vhost_iterate_given_conn(conn_rec *conn, 
984                                             ap_vhost_iterate_conn_cb func_cb,
985                                             void* baton)
986 {
987     server_rec *s;
988     server_rec *last_s;
989     name_chain *src;
990     apr_port_t port;
991     int rv = 0;
992
993     if (conn->vhost_lookup_data) {
994         last_s = NULL;
995         port = conn->local_addr->port;
996
997         for (src = conn->vhost_lookup_data; src; src = src->next) {
998             server_addr_rec *sar;
999
1000             /* We only consider addresses on the name_chain which have a 
1001              * matching port.
1002              */
1003             sar = src->sar;
1004             if (sar->host_port != 0 && port != sar->host_port) {
1005                 continue;
1006             }
1007
1008             s = src->server;
1009
1010             if (s == last_s) {
1011                 /* we've already done a callback for this vhost. */
1012                 continue;
1013             }
1014
1015             last_s = s;
1016
1017             rv = func_cb(baton, conn, s);
1018
1019             if (rv != 0) {
1020                 break;
1021             }
1022         }
1023     }
1024     else {
1025         rv = func_cb(baton, conn, conn->base_server);
1026     }
1027
1028     return rv;
1029 }
1030
1031 /* Called for a new connection which has a known local_addr.  Note that the
1032  * new connection is assumed to have conn->server == main server.
1033  */
1034 AP_DECLARE(void) ap_update_vhost_given_ip(conn_rec *conn)
1035 {
1036     ipaddr_chain *trav;
1037     apr_port_t port;
1038
1039     /* scan the hash table for an exact match first */
1040     trav = find_ipaddr(conn->local_addr);
1041
1042     if (trav) {
1043         /* save the name_chain for later in case this is a name-vhost */
1044         conn->vhost_lookup_data = trav->names;
1045         conn->base_server = trav->server;
1046         return;
1047     }
1048
1049     /* maybe there's a default server or wildcard name-based vhost
1050      * matching this port
1051      */
1052     port = conn->local_addr->port;
1053
1054     trav = find_default_server(port);
1055     if (trav) {
1056         conn->vhost_lookup_data = trav->names;
1057         conn->base_server = trav->server;
1058         return;
1059     }
1060
1061     /* otherwise we're stuck with just the main server
1062      * and no name-based vhosts
1063      */
1064     conn->vhost_lookup_data = NULL;
1065 }