1 |
/* |
2 |
* Boa, an http server |
3 |
* Copyright (C) 1999 Larry Doolittle <ldoolitt@boa.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: config.c,v 1.5 2002/09/25 07:33:35 nmav Exp $*/ |
22 |
|
23 |
#include "boa.h" |
24 |
#include "y.tab.h" |
25 |
#include "parse.h" |
26 |
|
27 |
int yyparse(void); /* Better match the output of lex */ |
28 |
|
29 |
#ifdef DEBUG |
30 |
#define DBG(x) x |
31 |
#else |
32 |
#define DBG(x) |
33 |
#endif |
34 |
|
35 |
int server_port; |
36 |
uid_t server_uid; |
37 |
gid_t server_gid; |
38 |
char *server_root; |
39 |
char *server_name; |
40 |
char *server_admin; |
41 |
char *server_ip; |
42 |
long int max_connections; |
43 |
|
44 |
int max_files_cache = 256; |
45 |
int max_file_size_cache = 100 * 1024; |
46 |
|
47 |
int server_max_threads = 1; |
48 |
|
49 |
char *server_cert; |
50 |
char *server_key; |
51 |
int ssl_session_cache; |
52 |
int boa_ssl = 0; |
53 |
int ssl_port = 443; |
54 |
int ssl_dh_bits = 1024; /* default value */ |
55 |
int ssl_session_timeout = 3600; |
56 |
int ssl_params_refresh = 432000; /* every 5 days */ |
57 |
|
58 |
char *ssl_ciphers = NULL; |
59 |
char *ssl_mac = NULL; |
60 |
char *ssl_kx = NULL; |
61 |
char *ssl_comp = NULL; |
62 |
char *ssl_protocol = NULL; |
63 |
|
64 |
char *default_document_root; |
65 |
int default_document_root_size; |
66 |
|
67 |
char *directory_index; |
68 |
char *default_type; |
69 |
char *dirmaker; |
70 |
char *cachedir; |
71 |
|
72 |
char *tempdir; |
73 |
|
74 |
char *cgi_path = NULL; |
75 |
int single_post_limit = SINGLE_POST_LIMIT_DEFAULT; |
76 |
|
77 |
int ka_timeout; |
78 |
int ka_max; |
79 |
|
80 |
/* These came from log.c */ |
81 |
char *error_log_name; |
82 |
char *access_log_name; |
83 |
char *cgi_log_name; |
84 |
|
85 |
int use_localtime; |
86 |
|
87 |
/* These are new */ |
88 |
static void c_set_user(char *v1, char* v2, char* v3, char* v4, void *t); |
89 |
static void c_set_group(char *v1, char* v2, char* v3, char* v4, void *t); |
90 |
static void c_set_string(char *v1, char* v2, char* v3, char* v4, void *t); |
91 |
static void c_set_int(char *v1, char* v2, char* v3, char* v4, void *t); |
92 |
static void c_set_unity(char *v1, char* v2, char* v3, char* v4, void *t); |
93 |
static void c_add_type(char *v1, char* v2, char* v3, char* v4, void *t); |
94 |
static void c_add_vhost(char *v1, char* v2, char* v3, char*v4, void *t); |
95 |
static void c_add_alias(char *v1, char* v2, char* v3, char* v4, void *t); |
96 |
|
97 |
/* Fakery to keep the value passed to action() a void *, |
98 |
see usage in table and c_add_alias() below */ |
99 |
static int script_number = SCRIPTALIAS; |
100 |
static int redirect_number = REDIRECT; |
101 |
static int alias_number = ALIAS; |
102 |
static uid_t current_uid=0; |
103 |
|
104 |
/* Help keep the table below compact */ |
105 |
#define S0A STMT_NO_ARGS |
106 |
#define S1A STMT_ONE_ARG |
107 |
#define S2A STMT_TWO_ARGS |
108 |
#define S3A STMT_THREE_ARGS |
109 |
#define S4A STMT_FOUR_ARGS |
110 |
|
111 |
struct ccommand clist[] = { |
112 |
{"SSLCiphers", S1A, c_set_string, &ssl_ciphers}, |
113 |
{"SSLKeyExchangeAlgorithms", S1A, c_set_string, &ssl_kx}, |
114 |
{"SSLMACAlgorithms", S1A, c_set_string, &ssl_mac}, |
115 |
{"SSLProtocols", S1A, c_set_string, &ssl_protocol}, |
116 |
{"SSLCompressionMethods", S1A, c_set_string, &ssl_comp}, |
117 |
{"SSLCertificate", S1A, c_set_string, &server_cert}, |
118 |
{"SSLKey", S1A, c_set_string, &server_key}, |
119 |
{"SSLSessionCache", S1A, c_set_int, &ssl_session_cache}, |
120 |
{"SSL", S1A, c_set_int, &boa_ssl}, |
121 |
{"SSLPort", S1A, c_set_int, &ssl_port}, |
122 |
{"SSLDHBits", S1A, c_set_int, &ssl_dh_bits}, |
123 |
{"SSLSessionTimeout", S1A, c_set_int, &ssl_session_timeout}, |
124 |
{"SSLParamsRefresh", S1A, c_set_int, &ssl_params_refresh}, |
125 |
{"Threads", S1A, c_set_int, &server_max_threads}, |
126 |
{"Port", S1A, c_set_int, &server_port}, |
127 |
{"Listen", S1A, c_set_string, &server_ip}, |
128 |
{"BackLog", S1A, c_set_int, &backlog}, |
129 |
{"User", S1A, c_set_user, NULL}, |
130 |
{"Group", S1A, c_set_group, NULL}, |
131 |
{"ServerAdmin", S1A, c_set_string, &server_admin}, |
132 |
{"ServerRoot", S1A, c_set_string, &server_root}, |
133 |
{"ErrorLog", S1A, c_set_string, &error_log_name}, |
134 |
{"AccessLog", S1A, c_set_string, &access_log_name}, |
135 |
{"UseLocaltime", S0A, c_set_unity, &use_localtime}, |
136 |
{"CgiLog", S1A, c_set_string, &cgi_log_name}, |
137 |
{"VerboseCGILogs", S0A, c_set_unity, &verbose_cgi_logs}, |
138 |
{"ServerName", S1A, c_set_string, &server_name}, |
139 |
{"DocumentRoot", S1A, c_set_string, &default_document_root}, |
140 |
{"DirectoryIndex", S1A, c_set_string, &directory_index}, |
141 |
{"DirectoryMaker", S1A, c_set_string, &dirmaker}, |
142 |
{"DirectoryCache", S1A, c_set_string, &cachedir}, |
143 |
{"KeepAliveMax", S1A, c_set_int, &ka_max}, |
144 |
{"KeepAliveTimeout", S1A, c_set_int, &ka_timeout}, |
145 |
{"MimeTypes", S1A, c_set_string, &mime_types}, |
146 |
{"DefaultType", S1A, c_set_string, &default_type}, |
147 |
{"AddType", S2A, c_add_type, NULL}, |
148 |
{"ScriptAlias", S3A, c_add_alias, &script_number}, |
149 |
{"Redirect", S3A, c_add_alias, &redirect_number}, |
150 |
{"Alias", S3A, c_add_alias, &alias_number}, |
151 |
/* HOST - IP - DOCUMENT_ROOT - USER_DIR */ |
152 |
{"VirtualHost", S4A, c_add_vhost, NULL}, |
153 |
{"SinglePostLimit", S1A, c_set_int, &single_post_limit}, |
154 |
{"CGIPath", S1A, c_set_string, &cgi_path}, |
155 |
{"MaxConnections", S1A, c_set_int, &max_connections}, |
156 |
{"MaxFilesCache", S1A, c_set_int, &max_files_cache}, |
157 |
{"MaxFileSizeCache", S1A, c_set_int, &max_file_size_cache}, |
158 |
}; |
159 |
|
160 |
static void c_set_user(char *v1, char* v2, char* v3, char* v4, void *t) |
161 |
{ |
162 |
struct passwd *passwdbuf; |
163 |
char *endptr; |
164 |
int i; |
165 |
|
166 |
DBG(printf("User %s = ", v1); |
167 |
) |
168 |
i = strtol(v1, &endptr, 0); |
169 |
if (*v1 != '\0' && *endptr == '\0') { |
170 |
server_uid = i; |
171 |
} else { |
172 |
passwdbuf = getpwnam(v1); |
173 |
if (!passwdbuf) { |
174 |
if (current_uid) |
175 |
return; |
176 |
fprintf(stderr, "No such user: %s\n", v1); |
177 |
exit(1); |
178 |
} |
179 |
server_uid = passwdbuf->pw_uid; |
180 |
} |
181 |
DBG(printf("%d\n", server_uid); |
182 |
) |
183 |
} |
184 |
|
185 |
static void c_set_group(char *v1, char* v2, char* v3, char* v4, void *t) |
186 |
{ |
187 |
struct group *groupbuf; |
188 |
char *endptr; |
189 |
int i; |
190 |
DBG(printf("Group %s = ", v1); |
191 |
) |
192 |
i = strtol(v1, &endptr, 0); |
193 |
if (*v1 != '\0' && *endptr == '\0') { |
194 |
server_gid = i; |
195 |
} else { |
196 |
groupbuf = getgrnam(v1); |
197 |
if (!groupbuf) { |
198 |
if (current_uid) |
199 |
return; |
200 |
fprintf(stderr, "No such group: %s\n", v1); |
201 |
exit(1); |
202 |
} |
203 |
server_gid = groupbuf->gr_gid; |
204 |
} |
205 |
DBG(printf("%d\n", server_gid); |
206 |
) |
207 |
} |
208 |
|
209 |
static void c_set_string(char *v1, char* v2, char* v3, char* v4, void *t) |
210 |
{ |
211 |
char *s; |
212 |
DBG(printf("Setting pointer %p to string %s ..", t, v1); |
213 |
) |
214 |
if (t) { |
215 |
s = *(char **) t; |
216 |
if (s) |
217 |
free(s); |
218 |
*(char **) t = strdup(v1); |
219 |
if (!*(char **) t) { |
220 |
DIE("Unable to strdup in c_set_string"); |
221 |
} |
222 |
DBG(printf("done.\n"); |
223 |
) |
224 |
} else { |
225 |
DBG(printf("skipped.\n"); |
226 |
) |
227 |
} |
228 |
} |
229 |
|
230 |
static void c_set_int(char *v1, char* v2, char* v3, char* v4, void *t) |
231 |
{ |
232 |
char *endptr; |
233 |
int i; |
234 |
DBG(printf("Setting pointer %p to integer string %s ..", t, v1); |
235 |
) |
236 |
if (t) { |
237 |
i = strtol(v1, &endptr, 0); /* Automatic base 10/16/8 switching */ |
238 |
if (*v1 != '\0' && *endptr == '\0') { |
239 |
*(int *) t = i; |
240 |
DBG(printf(" Integer converted as %d, done\n", i); |
241 |
) |
242 |
} else { |
243 |
/* XXX should tell line number to user */ |
244 |
fprintf(stderr, "Error: %s found where integer expected\n", |
245 |
v1); |
246 |
} |
247 |
} else { |
248 |
DBG(printf("skipped.\n"); |
249 |
) |
250 |
} |
251 |
} |
252 |
|
253 |
static void c_set_unity(char *v1, char* v2, char* v3, char* v4, void *t) |
254 |
{ |
255 |
DBG(printf("Setting pointer %p to unity\n", t); |
256 |
) |
257 |
if (t) |
258 |
*(int *) t = 1; |
259 |
} |
260 |
|
261 |
static void c_add_type(char *v1, char* v2, char* v3, char* v4, void *t) |
262 |
{ |
263 |
add_mime_type(v1, v2); |
264 |
} |
265 |
|
266 |
static void c_add_vhost(char *v1, char *v2, char* v3, char* v4, void *t) |
267 |
{ |
268 |
add_virthost(v1, v2, v3, v4); |
269 |
} |
270 |
|
271 |
|
272 |
static void c_add_alias(char *v1, char *v2, char* v3, char* v4, void *t) |
273 |
{ |
274 |
add_alias(v1, v3, v2, *(int *) t); |
275 |
} |
276 |
|
277 |
struct ccommand *lookup_keyword(char *c) |
278 |
{ |
279 |
struct ccommand *p; |
280 |
DBG(printf("Checking string '%s' against keyword list\n", c); |
281 |
) |
282 |
for (p = clist; |
283 |
p < clist + (sizeof (clist) / sizeof (struct ccommand)); p++) { |
284 |
if (strcmp(c, p->name) == 0) |
285 |
return p; |
286 |
} |
287 |
return NULL; |
288 |
} |
289 |
|
290 |
/* |
291 |
* Name: read_config_files |
292 |
* |
293 |
* Description: Reads config files via yyparse, then makes sure that |
294 |
* all required variables were set properly. |
295 |
*/ |
296 |
void read_config_files(void) |
297 |
{ |
298 |
char *temp; |
299 |
current_uid = getuid(); |
300 |
yyin = fopen("hydra.conf", "r"); |
301 |
|
302 |
if (!yyin) { |
303 |
fputs("Could not open hydra.conf for reading.\n", stderr); |
304 |
exit(1); |
305 |
} |
306 |
if (yyparse()) { |
307 |
fputs("Error parsing config files, exiting\n", stderr); |
308 |
exit(1); |
309 |
} |
310 |
|
311 |
if (!server_name) { |
312 |
struct hostent *he; |
313 |
char temp_name[100]; |
314 |
|
315 |
if (gethostname(temp_name, 100) == -1) { |
316 |
perror("gethostname:"); |
317 |
exit(1); |
318 |
} |
319 |
|
320 |
he = gethostbyname(temp_name); |
321 |
if (he == NULL) { |
322 |
perror("gethostbyname:"); |
323 |
exit(1); |
324 |
} |
325 |
|
326 |
server_name = strdup(he->h_name); |
327 |
if (server_name == NULL) { |
328 |
perror("strdup:"); |
329 |
exit(1); |
330 |
} |
331 |
} |
332 |
tempdir = getenv("TMP"); |
333 |
if (tempdir == NULL) |
334 |
tempdir = "/tmp"; |
335 |
|
336 |
if (single_post_limit < 0) { |
337 |
fprintf(stderr, "Invalid value for single_post_limit: %d\n", |
338 |
single_post_limit); |
339 |
exit(1); |
340 |
} |
341 |
|
342 |
if (default_document_root) { |
343 |
temp = normalize_path(default_document_root); |
344 |
free(default_document_root); |
345 |
default_document_root = temp; |
346 |
default_document_root_size = strlen( default_document_root); |
347 |
if ( default_document_root_size > MAX_PATH_LENGTH) { |
348 |
log_error_time(); |
349 |
fprintf(stderr, "DocumentRoot size is too long\n"); |
350 |
exit(1); |
351 |
} |
352 |
} else default_document_root_size = 0; |
353 |
|
354 |
if (error_log_name) { |
355 |
temp = normalize_path(error_log_name); |
356 |
free(error_log_name); |
357 |
error_log_name = temp; |
358 |
} |
359 |
|
360 |
if (access_log_name) { |
361 |
temp = normalize_path(access_log_name); |
362 |
free(access_log_name); |
363 |
access_log_name = temp; |
364 |
} |
365 |
|
366 |
if (cgi_log_name) { |
367 |
temp = normalize_path(cgi_log_name); |
368 |
free(cgi_log_name); |
369 |
cgi_log_name = temp; |
370 |
} |
371 |
|
372 |
if (dirmaker) { |
373 |
temp = normalize_path(dirmaker); |
374 |
free(dirmaker); |
375 |
dirmaker = temp; |
376 |
} |
377 |
|
378 |
#if 0 |
379 |
if (mime_types) { |
380 |
temp = normalize_path(mime_types); |
381 |
free(mime_types); |
382 |
mime_types = temp; |
383 |
} |
384 |
#endif |
385 |
} |