1 |
/* |
2 |
* Hydra, 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.12 2002/10/27 08:15:25 nmav Exp $ */ |
22 |
|
23 |
#include "boa.h" |
24 |
|
25 |
static virthost *virthost_hashtable[VIRTHOST_HASHTABLE_SIZE]; |
26 |
|
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 || document_root_len == 0) { |
55 |
DIE("empty values sent to add_virthost"); |
56 |
} |
57 |
|
58 |
if ( document_root_len > MAX_PATH_LENGTH || user_dir_len > MAX_USER_DIR_LENGTH) { |
59 |
DIE("DocumentRoot or UserDir length is too long."); |
60 |
} |
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 |
new = (virthost *) calloc(1, sizeof (virthost)); |
75 |
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 |
new->document_root = strdup(document_root); |
113 |
if (!new->document_root) { |
114 |
DIE("strdup of document_root failed"); |
115 |
} |
116 |
new->document_root_len = document_root_len; |
117 |
|
118 |
new->next = NULL; |
119 |
} |
120 |
|
121 |
/* |
122 |
* Name: find_virthost |
123 |
* |
124 |
* Description: Locates host in the virthost hashtable if it exists. |
125 |
* |
126 |
* Returns: |
127 |
* |
128 |
* virthost structure or NULL if not found |
129 |
*/ |
130 |
|
131 |
virthost *find_virthost(const char *_host, int hostlen) |
132 |
{ |
133 |
virthost *current; |
134 |
int hash; |
135 |
char host[MAX_SITENAME_LENGTH], *p; |
136 |
|
137 |
/* Find Hostname, IP, document root */ |
138 |
if (_host==NULL) return NULL; |
139 |
|
140 |
if (hostlen == 0) |
141 |
hostlen = strlen(_host); |
142 |
|
143 |
if (hostlen >= MAX_SITENAME_LENGTH) |
144 |
return NULL; |
145 |
|
146 |
/* Remove port number.. Ie www.site.gr:8080 |
147 |
*/ |
148 |
strcpy( host, _host); |
149 |
p = strrchr( host, ':'); |
150 |
if (p) { |
151 |
*p = 0; |
152 |
hostlen = strlen( host); |
153 |
} |
154 |
|
155 |
hash = get_host_hash_value( host); |
156 |
|
157 |
current = virthost_hashtable[hash]; |
158 |
while (current) { |
159 |
#ifdef FASCIST_LOGGING |
160 |
fprintf(stderr, |
161 |
"%s:%d - comparing \"%s\" (request) to \"%s\" (virthost): ", |
162 |
__FILE__, __LINE__, host, current->host); |
163 |
#endif |
164 |
/* current->host_len must always be: |
165 |
* equal to the host |
166 |
*/ |
167 |
if (current->host_len == hostlen && |
168 |
!memcmp(host, current->host, current->host_len)) { |
169 |
#ifdef FASCIST_LOGGING |
170 |
fprintf(stderr, "Got it!\n"); |
171 |
#endif |
172 |
return current; |
173 |
} |
174 |
#ifdef FASCIST_LOGGING |
175 |
else |
176 |
fprintf(stderr, "Don't Got it!\n"); |
177 |
#endif |
178 |
current = current->next; |
179 |
} |
180 |
return current; |
181 |
} |
182 |
|
183 |
|
184 |
|
185 |
/* |
186 |
* Empties the virthost hashtable, deallocating any allocated memory. |
187 |
*/ |
188 |
|
189 |
void dump_virthost(void) |
190 |
{ |
191 |
int i; |
192 |
virthost *temp; |
193 |
|
194 |
for (i = 0; i < VIRTHOST_HASHTABLE_SIZE; ++i) { /* these limits OK? */ |
195 |
if (virthost_hashtable[i]) { |
196 |
temp = virthost_hashtable[i]; |
197 |
while (temp) { |
198 |
virthost *temp_next; |
199 |
|
200 |
if (temp->host) |
201 |
free(temp->host); |
202 |
free(temp->access_nodes); |
203 |
if (temp->ip) |
204 |
free(temp->ip); |
205 |
if (temp->document_root) |
206 |
free(temp->document_root); |
207 |
if (temp->user_dir) |
208 |
free(temp->user_dir); |
209 |
dump_alias( temp); /* clear all aliases */ |
210 |
|
211 |
temp_next = temp->next; |
212 |
free(temp); |
213 |
temp = temp_next; |
214 |
} |
215 |
virthost_hashtable[i] = NULL; |
216 |
} |
217 |
} |
218 |
} |