1 |
/* |
2 |
* Boa, an http server |
3 |
* Copyright (C) 2002 Nikos Mavroyanopoulos <nmav@gnutls.org> |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License as published by |
7 |
* the Free Software Foundation; either version 1, or (at your option) |
8 |
* any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 |
* |
19 |
*/ |
20 |
|
21 |
/* $Id: virthost.c,v 1.5 2002/09/25 06:42:34 nmav Exp $ */ |
22 |
|
23 |
#include "boa.h" |
24 |
|
25 |
static virthost *virthost_hashtable[VIRTHOST_HASHTABLE_SIZE]; |
26 |
|
27 |
/* |
28 |
* Name: get_host_hash_value |
29 |
* |
30 |
* Here we simply use the algorithm used in get_alias_hash_value(); |
31 |
*/ |
32 |
|
33 |
int get_host_hash_value(const char *file) |
34 |
{ |
35 |
unsigned int hash = 0; |
36 |
unsigned int index = 0; |
37 |
unsigned char c; |
38 |
|
39 |
hash = file[index++]; |
40 |
while ((c = file[index++]) && c != '/') |
41 |
hash += (unsigned int) c; |
42 |
|
43 |
return hash % VIRTHOST_HASHTABLE_SIZE; |
44 |
} |
45 |
|
46 |
/* |
47 |
* Name: add_virthost |
48 |
* |
49 |
* Description: add a virtual host to the virthost hash table. |
50 |
*/ |
51 |
|
52 |
void add_virthost(const char *host, const char *ip, const char* document_root, |
53 |
const char* user_dir) |
54 |
{ |
55 |
int hash; |
56 |
virthost *old, *new; |
57 |
int hostlen, iplen, document_root_len, user_dir_len = 0; |
58 |
|
59 |
/* sanity checking */ |
60 |
if (host == NULL || ip == NULL || document_root == NULL) { |
61 |
DIE("NULL values sent to add_virthost"); |
62 |
} |
63 |
|
64 |
iplen = strlen(ip); |
65 |
|
66 |
if (iplen > NI_MAXHOST) { |
67 |
DIE("IP in virthost is tooooo long"); |
68 |
} |
69 |
hostlen = strlen(host); |
70 |
document_root_len = strlen( document_root); |
71 |
if (user_dir) user_dir_len = strlen( user_dir); |
72 |
|
73 |
if (iplen == 0 || hostlen == 0 || document_root_len == 0) { |
74 |
DIE("empty values sent to add_virthost"); |
75 |
} |
76 |
|
77 |
if ( document_root_len > MAX_PATH_LENGTH || user_dir_len > MAX_USER_DIR_LENGTH) { |
78 |
DIE("DocumentRoot or UserDir length is too long."); |
79 |
} |
80 |
|
81 |
hash = get_host_hash_value( host); |
82 |
|
83 |
old = virthost_hashtable[hash]; |
84 |
|
85 |
if (old) { |
86 |
while (old->next) { |
87 |
if (!strcmp( host, old->host)) /* don't add twice */ |
88 |
return; |
89 |
old = old->next; |
90 |
} |
91 |
} |
92 |
|
93 |
new = (virthost *) calloc(1, sizeof (virthost)); |
94 |
if (!new) { |
95 |
DIE("out of memory adding virthost to hash"); |
96 |
} |
97 |
|
98 |
if (old) |
99 |
old->next = new; |
100 |
else |
101 |
virthost_hashtable[hash] = new; |
102 |
|
103 |
new->host = strdup( host); |
104 |
if (!new->host) { |
105 |
DIE("failed strdup"); |
106 |
} |
107 |
new->host_len = hostlen; |
108 |
|
109 |
if (user_dir && user_dir_len > 0) { |
110 |
new->user_dir = strdup(user_dir); |
111 |
new->user_dir_len = user_dir_len; |
112 |
} else { |
113 |
new->user_dir = NULL; |
114 |
new->user_dir_len = 0; |
115 |
} |
116 |
|
117 |
if (iplen == 0 || !strchr( ip, '*')) { /* if the IP part is '*' then |
118 |
* we don't bind this virthost to a |
119 |
* specific ip */ |
120 |
new->ip = strdup( ip); |
121 |
if (!new->ip) { |
122 |
DIE("failed strdup"); |
123 |
} |
124 |
new->ip_len = iplen; |
125 |
} else { |
126 |
new->ip = NULL; |
127 |
new->ip_len = 0; |
128 |
} |
129 |
|
130 |
/* check for "here" */ |
131 |
if (document_root[0] == '.' && document_root[1] == '/') { |
132 |
new->document_root = normalize_path( (char*)(document_root+2)); |
133 |
if (!new->document_root) { |
134 |
/* superfluous - normalize_path checks for NULL return values. */ |
135 |
DIE("normalize_path returned NULL"); |
136 |
} |
137 |
document_root_len = strlen(new->document_root); |
138 |
} else { |
139 |
new->document_root = strdup(document_root); |
140 |
if (!new->document_root) { |
141 |
DIE("strdup of document_root failed"); |
142 |
} |
143 |
} |
144 |
new->document_root_len = document_root_len; |
145 |
|
146 |
new->next = NULL; |
147 |
} |
148 |
|
149 |
/* |
150 |
* Name: find_virthost |
151 |
* |
152 |
* Description: Locates host in the virthost hashtable if it exists. |
153 |
* |
154 |
* Returns: |
155 |
* |
156 |
* virthost structure or NULL if not found |
157 |
*/ |
158 |
|
159 |
virthost *find_virthost(const char *host, int hostlen) |
160 |
{ |
161 |
virthost *current; |
162 |
int hash; |
163 |
|
164 |
/* Find Hostname, IP, document root */ |
165 |
|
166 |
if (hostlen == 0) |
167 |
hostlen = strlen(host); |
168 |
|
169 |
hash = get_host_hash_value( host); |
170 |
|
171 |
current = virthost_hashtable[hash]; |
172 |
while (current) { |
173 |
#ifdef FASCIST_LOGGING |
174 |
fprintf(stderr, |
175 |
"%s:%d - comparing \"%s\" (request) to \"%s\" (virthost): ", |
176 |
__FILE__, __LINE__, host, current->host); |
177 |
#endif |
178 |
/* current->host_len must always be: |
179 |
* equal to the host |
180 |
*/ |
181 |
if (current->host_len == hostlen && |
182 |
!memcmp(host, current->host, current->host_len)) { |
183 |
#ifdef FASCIST_LOGGING |
184 |
fprintf(stderr, "Got it!\n"); |
185 |
#endif |
186 |
return current; |
187 |
} |
188 |
#ifdef FASCIST_LOGGING |
189 |
else |
190 |
fprintf(stderr, "Don't Got it!\n"); |
191 |
#endif |
192 |
current = current->next; |
193 |
} |
194 |
return current; |
195 |
} |
196 |
|
197 |
|
198 |
|
199 |
/* |
200 |
* Empties the virthost hashtable, deallocating any allocated memory. |
201 |
*/ |
202 |
|
203 |
void dump_virthost(void) |
204 |
{ |
205 |
int i; |
206 |
virthost *temp; |
207 |
|
208 |
for (i = 0; i < VIRTHOST_HASHTABLE_SIZE; ++i) { /* these limits OK? */ |
209 |
if (virthost_hashtable[i]) { |
210 |
temp = virthost_hashtable[i]; |
211 |
while (temp) { |
212 |
virthost *temp_next; |
213 |
|
214 |
if (temp->host) |
215 |
free(temp->host); |
216 |
if (temp->ip) |
217 |
free(temp->ip); |
218 |
if (temp->document_root) |
219 |
free(temp->document_root); |
220 |
if (temp->user_dir) |
221 |
free(temp->user_dir); |
222 |
dump_alias( temp); /* clear all aliases */ |
223 |
|
224 |
temp_next = temp->next; |
225 |
free(temp); |
226 |
temp = temp_next; |
227 |
} |
228 |
virthost_hashtable[i] = NULL; |
229 |
} |
230 |
} |
231 |
} |