1 |
nmav |
1.1 |
/* |
2 |
|
|
* Boa, an http server |
3 |
nmav |
1.2 |
* Copyright (C) 2002 Nikos Mavroyanopoulos <nmav@gnutls.org> |
4 |
nmav |
1.1 |
* |
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 |
nmav |
1.9 |
/* $Id: virthost.c,v 1.8 2002/09/28 08:50:07 nmav Exp $ */ |
22 |
nmav |
1.1 |
|
23 |
|
|
#include "boa.h" |
24 |
|
|
|
25 |
nmav |
1.6 |
static virthost *virthost_hashtable[VIRTHOST_HASHTABLE_SIZE]; |
26 |
nmav |
1.1 |
|
27 |
|
|
/* |
28 |
|
|
* Name: add_virthost |
29 |
|
|
* |
30 |
|
|
* Description: add a virtual host to the virthost hash table. |
31 |
|
|
*/ |
32 |
|
|
|
33 |
|
|
void add_virthost(const char *host, const char *ip, const char* document_root, |
34 |
|
|
const char* user_dir) |
35 |
|
|
{ |
36 |
|
|
int hash; |
37 |
|
|
virthost *old, *new; |
38 |
|
|
int hostlen, iplen, document_root_len, user_dir_len = 0; |
39 |
|
|
|
40 |
|
|
/* sanity checking */ |
41 |
|
|
if (host == NULL || ip == NULL || document_root == NULL) { |
42 |
|
|
DIE("NULL values sent to add_virthost"); |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
iplen = strlen(ip); |
46 |
|
|
|
47 |
|
|
if (iplen > NI_MAXHOST) { |
48 |
|
|
DIE("IP in virthost is tooooo long"); |
49 |
|
|
} |
50 |
|
|
hostlen = strlen(host); |
51 |
|
|
document_root_len = strlen( document_root); |
52 |
|
|
if (user_dir) user_dir_len = strlen( user_dir); |
53 |
|
|
|
54 |
|
|
if (iplen == 0 || hostlen == 0 || document_root_len == 0) { |
55 |
|
|
DIE("empty values sent to add_virthost"); |
56 |
nmav |
1.4 |
} |
57 |
|
|
|
58 |
nmav |
1.5 |
if ( document_root_len > MAX_PATH_LENGTH || user_dir_len > MAX_USER_DIR_LENGTH) { |
59 |
|
|
DIE("DocumentRoot or UserDir length is too long."); |
60 |
nmav |
1.1 |
} |
61 |
|
|
|
62 |
|
|
hash = get_host_hash_value( host); |
63 |
|
|
|
64 |
|
|
old = virthost_hashtable[hash]; |
65 |
|
|
|
66 |
|
|
if (old) { |
67 |
|
|
while (old->next) { |
68 |
|
|
if (!strcmp( host, old->host)) /* don't add twice */ |
69 |
|
|
return; |
70 |
|
|
old = old->next; |
71 |
|
|
} |
72 |
|
|
} |
73 |
|
|
|
74 |
nmav |
1.5 |
new = (virthost *) calloc(1, sizeof (virthost)); |
75 |
nmav |
1.1 |
if (!new) { |
76 |
|
|
DIE("out of memory adding virthost to hash"); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
if (old) |
80 |
|
|
old->next = new; |
81 |
|
|
else |
82 |
|
|
virthost_hashtable[hash] = new; |
83 |
|
|
|
84 |
|
|
new->host = strdup( host); |
85 |
|
|
if (!new->host) { |
86 |
|
|
DIE("failed strdup"); |
87 |
|
|
} |
88 |
|
|
new->host_len = hostlen; |
89 |
|
|
|
90 |
|
|
if (user_dir && user_dir_len > 0) { |
91 |
|
|
new->user_dir = strdup(user_dir); |
92 |
|
|
new->user_dir_len = user_dir_len; |
93 |
|
|
} else { |
94 |
|
|
new->user_dir = NULL; |
95 |
|
|
new->user_dir_len = 0; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
if (iplen == 0 || !strchr( ip, '*')) { /* if the IP part is '*' then |
99 |
|
|
* we don't bind this virthost to a |
100 |
|
|
* specific ip */ |
101 |
|
|
new->ip = strdup( ip); |
102 |
|
|
if (!new->ip) { |
103 |
|
|
DIE("failed strdup"); |
104 |
|
|
} |
105 |
|
|
new->ip_len = iplen; |
106 |
|
|
} else { |
107 |
|
|
new->ip = NULL; |
108 |
|
|
new->ip_len = 0; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
/* check for "here" */ |
112 |
|
|
if (document_root[0] == '.' && document_root[1] == '/') { |
113 |
|
|
new->document_root = normalize_path( (char*)(document_root+2)); |
114 |
|
|
if (!new->document_root) { |
115 |
|
|
/* superfluous - normalize_path checks for NULL return values. */ |
116 |
|
|
DIE("normalize_path returned NULL"); |
117 |
|
|
} |
118 |
|
|
document_root_len = strlen(new->document_root); |
119 |
|
|
} else { |
120 |
|
|
new->document_root = strdup(document_root); |
121 |
|
|
if (!new->document_root) { |
122 |
|
|
DIE("strdup of document_root failed"); |
123 |
|
|
} |
124 |
|
|
} |
125 |
|
|
new->document_root_len = document_root_len; |
126 |
|
|
|
127 |
|
|
new->next = NULL; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
/* |
131 |
|
|
* Name: find_virthost |
132 |
|
|
* |
133 |
|
|
* Description: Locates host in the virthost hashtable if it exists. |
134 |
|
|
* |
135 |
|
|
* Returns: |
136 |
|
|
* |
137 |
|
|
* virthost structure or NULL if not found |
138 |
|
|
*/ |
139 |
|
|
|
140 |
nmav |
1.9 |
virthost *find_virthost(const char *_host, int hostlen) |
141 |
nmav |
1.1 |
{ |
142 |
|
|
virthost *current; |
143 |
|
|
int hash; |
144 |
nmav |
1.9 |
char host[MAX_SITENAME_LENGTH], *p; |
145 |
nmav |
1.1 |
|
146 |
|
|
/* Find Hostname, IP, document root */ |
147 |
nmav |
1.9 |
if (_host==NULL) return NULL; |
148 |
nmav |
1.1 |
|
149 |
|
|
if (hostlen == 0) |
150 |
nmav |
1.9 |
hostlen = strlen(_host); |
151 |
|
|
|
152 |
|
|
if (hostlen >= MAX_SITENAME_LENGTH) |
153 |
|
|
return NULL; |
154 |
|
|
|
155 |
|
|
/* Remove port number.. Ie www.site.gr:8080 |
156 |
|
|
*/ |
157 |
|
|
strcpy( host, _host); |
158 |
|
|
p = strrchr( host, ':'); |
159 |
|
|
if (p) { |
160 |
|
|
*p = 0; |
161 |
|
|
hostlen = strlen( host); |
162 |
|
|
} |
163 |
nmav |
1.1 |
|
164 |
|
|
hash = get_host_hash_value( host); |
165 |
|
|
|
166 |
|
|
current = virthost_hashtable[hash]; |
167 |
|
|
while (current) { |
168 |
|
|
#ifdef FASCIST_LOGGING |
169 |
|
|
fprintf(stderr, |
170 |
|
|
"%s:%d - comparing \"%s\" (request) to \"%s\" (virthost): ", |
171 |
|
|
__FILE__, __LINE__, host, current->host); |
172 |
|
|
#endif |
173 |
|
|
/* current->host_len must always be: |
174 |
|
|
* equal to the host |
175 |
|
|
*/ |
176 |
|
|
if (current->host_len == hostlen && |
177 |
|
|
!memcmp(host, current->host, current->host_len)) { |
178 |
|
|
#ifdef FASCIST_LOGGING |
179 |
|
|
fprintf(stderr, "Got it!\n"); |
180 |
|
|
#endif |
181 |
|
|
return current; |
182 |
|
|
} |
183 |
|
|
#ifdef FASCIST_LOGGING |
184 |
|
|
else |
185 |
|
|
fprintf(stderr, "Don't Got it!\n"); |
186 |
|
|
#endif |
187 |
|
|
current = current->next; |
188 |
|
|
} |
189 |
|
|
return current; |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
/* |
195 |
|
|
* Empties the virthost hashtable, deallocating any allocated memory. |
196 |
|
|
*/ |
197 |
|
|
|
198 |
|
|
void dump_virthost(void) |
199 |
|
|
{ |
200 |
|
|
int i; |
201 |
|
|
virthost *temp; |
202 |
|
|
|
203 |
|
|
for (i = 0; i < VIRTHOST_HASHTABLE_SIZE; ++i) { /* these limits OK? */ |
204 |
|
|
if (virthost_hashtable[i]) { |
205 |
|
|
temp = virthost_hashtable[i]; |
206 |
|
|
while (temp) { |
207 |
|
|
virthost *temp_next; |
208 |
|
|
|
209 |
|
|
if (temp->host) |
210 |
|
|
free(temp->host); |
211 |
|
|
if (temp->ip) |
212 |
|
|
free(temp->ip); |
213 |
|
|
if (temp->document_root) |
214 |
|
|
free(temp->document_root); |
215 |
nmav |
1.3 |
if (temp->user_dir) |
216 |
|
|
free(temp->user_dir); |
217 |
nmav |
1.5 |
dump_alias( temp); /* clear all aliases */ |
218 |
|
|
|
219 |
nmav |
1.1 |
temp_next = temp->next; |
220 |
|
|
free(temp); |
221 |
|
|
temp = temp_next; |
222 |
|
|
} |
223 |
|
|
virthost_hashtable[i] = NULL; |
224 |
|
|
} |
225 |
|
|
} |
226 |
|
|
} |