[core] avoid spurious trace and error abort
[lighttpd.git] / src / configfile.c
blobfbcbcf32fc130b04bebfebff97c1b232414a5a23
1 #include "first.h"
3 #include "server.h"
4 #include "log.h"
5 #include "stream.h"
6 #include "plugin.h"
8 #include "configparser.h"
9 #include "configfile.h"
10 #include "proc_open.h"
11 #include "request.h"
12 #include "version.h"
14 #include <sys/stat.h>
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <limits.h>
24 #include <assert.h>
25 #include <glob.h>
28 static int config_insert(server *srv) {
29 size_t i;
30 int ret = 0;
31 buffer *stat_cache_string;
33 config_values_t cv[] = {
34 { "server.bind", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 0 */
35 { "server.errorlog", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 1 */
36 { "server.errorfile-prefix", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
37 { "server.chroot", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 3 */
38 { "server.username", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 4 */
39 { "server.groupname", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 5 */
40 { "server.port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 6 */
41 { "server.tag", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
42 { "server.use-ipv6", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
43 { "server.modules", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_SERVER }, /* 9 */
45 { "server.event-handler", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 10 */
46 { "server.pid-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 11 */
47 { "server.max-request-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_SERVER }, /* 12 */
48 { "server.max-worker", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 13 */
49 { "server.document-root", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
50 { "server.force-lowercase-filenames", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 15 */
51 { "debug.log-condition-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 16 */
52 { "server.max-keep-alive-requests", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 17 */
53 { "server.name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 18 */
54 { "server.max-keep-alive-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 19 */
56 { "server.max-read-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 20 */
57 { "server.max-write-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 21 */
58 { "server.error-handler", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 22 */
59 { "server.max-fds", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 23 */
60 #ifdef HAVE_LSTAT
61 { "server.follow-symlink", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 24 */
62 #else
63 { "server.follow-symlink",
64 "Your system lacks lstat(). We can not differ symlinks from files."
65 "Please remove server.follow-symlinks from your config.",
66 T_CONFIG_UNSUPPORTED, T_CONFIG_SCOPE_UNSET },
67 #endif
68 { "server.kbytes-per-second", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 25 */
69 { "connection.kbytes-per-second", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 26 */
70 { "mimetype.use-xattr", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 27 */
71 { "mimetype.assign", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 28 */
72 { "ssl.pemfile", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 29 */
74 { "ssl.engine", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 30 */
75 { "debug.log-file-not-found", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 31 */
76 { "debug.log-request-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 32 */
77 { "debug.log-response-header", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 33 */
78 { "debug.log-request-header", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 34 */
79 { "debug.log-ssl-noise", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 35 */
80 { "server.protocol-http11", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 36 */
81 { "debug.log-request-header-on-error", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 37 */
82 { "debug.log-state-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 38 */
83 { "ssl.ca-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 39 */
85 { "server.errorlog-use-syslog", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 40 */
86 { "server.range-requests", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 41 */
87 { "server.stat-cache-engine", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 42 */
88 { "server.max-connections", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 43 */
89 { "server.network-backend", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 44 */
90 { "server.upload-dirs", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_SERVER }, /* 45 */
91 { "server.core-files", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 46 */
92 { "ssl.cipher-list", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 47 */
93 { "ssl.use-sslv2", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 48 */
94 { "etag.use-inode", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 49 */
96 { "etag.use-mtime", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 50 */
97 { "etag.use-size", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 51 */
98 { "server.reject-expect-100-with-417", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 52 */
99 { "debug.log-timeouts", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 53 */
100 { "server.defer-accept", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 54 */
101 { "server.breakagelog", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 55 */
102 { "ssl.verifyclient.activate", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 56 */
103 { "ssl.verifyclient.enforce", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 57 */
104 { "ssl.verifyclient.depth", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 58 */
105 { "ssl.verifyclient.username", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 59 */
107 { "ssl.verifyclient.exportcert", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 60 */
108 { "server.set-v6only", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 61 */
109 { "ssl.use-sslv3", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 62 */
110 { "ssl.dh-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 63 */
111 { "ssl.ec-curve", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 64 */
112 { "ssl.disable-client-renegotiation", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 65 */
113 { "ssl.honor-cipher-order", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 66 */
114 { "ssl.empty-fragments", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 67 */
115 { "server.upload-temp-file-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_SERVER }, /* 68 */
116 { "mimetype.xattr-name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 69 */
117 { "server.listen-backlog", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 70 */
118 { "server.error-handler-404", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 71 */
119 { "server.http-parseopt-header-strict",NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 72 */
120 { "server.http-parseopt-host-strict", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 73 */
121 { "server.http-parseopt-host-normalize",NULL,T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 74 */
122 { "server.bsd-accept-filter", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 75 */
123 { "server.stream-request-body", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 76 */
124 { "server.stream-response-body", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 77 */
126 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
129 /* all T_CONFIG_SCOPE_SERVER options */
130 cv[0].destination = srv->srvconf.bindhost;
131 cv[1].destination = srv->srvconf.errorlog_file;
132 cv[3].destination = srv->srvconf.changeroot;
133 cv[4].destination = srv->srvconf.username;
134 cv[5].destination = srv->srvconf.groupname;
135 cv[6].destination = &(srv->srvconf.port);
136 cv[9].destination = srv->srvconf.modules;
138 cv[10].destination = srv->srvconf.event_handler;
139 cv[11].destination = srv->srvconf.pid_file;
140 cv[13].destination = &(srv->srvconf.max_worker);
142 cv[23].destination = &(srv->srvconf.max_fds);
144 cv[37].destination = &(srv->srvconf.log_request_header_on_error);
145 cv[38].destination = &(srv->srvconf.log_state_handling);
147 cv[40].destination = &(srv->srvconf.errorlog_use_syslog);
148 stat_cache_string = buffer_init();
149 cv[42].destination = stat_cache_string;
150 cv[43].destination = &(srv->srvconf.max_conns);
151 cv[44].destination = srv->srvconf.network_backend;
152 cv[45].destination = srv->srvconf.upload_tempdirs;
153 cv[46].destination = &(srv->srvconf.enable_cores);
155 cv[52].destination = &(srv->srvconf.reject_expect_100_with_417);
156 cv[55].destination = srv->srvconf.breakagelog_file;
158 cv[68].destination = &(srv->srvconf.upload_temp_file_size);
159 cv[69].destination = srv->srvconf.xattr_name;
160 cv[72].destination = &(srv->srvconf.http_header_strict);
161 cv[73].destination = &(srv->srvconf.http_host_strict);
162 cv[74].destination = &(srv->srvconf.http_host_normalize);
164 srv->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
166 force_assert(srv->config_storage);
168 for (i = 0; i < srv->config_context->used; i++) {
169 data_config const* config = (data_config const*)srv->config_context->data[i];
170 specific_config *s;
172 s = calloc(1, sizeof(specific_config));
173 force_assert(s);
174 s->document_root = buffer_init();
175 s->mimetypes = array_init();
176 s->server_name = buffer_init();
177 s->ssl_pemfile = buffer_init();
178 s->ssl_ca_file = buffer_init();
179 s->error_handler = buffer_init();
180 s->error_handler_404 = buffer_init();
181 s->server_tag = buffer_init_string(PACKAGE_DESC);
182 s->ssl_cipher_list = buffer_init();
183 s->ssl_dh_file = buffer_init();
184 s->ssl_ec_curve = buffer_init();
185 s->errorfile_prefix = buffer_init();
186 #if defined(__FreeBSD__) || defined(__NetBSD__) \
187 || defined(__OpenBSD__) || defined(__DragonflyBSD__)
188 s->bsd_accept_filter = (i == 0)
189 ? buffer_init()
190 : buffer_init_buffer(srv->config_storage[0]->bsd_accept_filter);
191 #endif
192 s->max_keep_alive_requests = 16;
193 s->max_keep_alive_idle = 5;
194 s->max_read_idle = 60;
195 s->max_write_idle = 360;
196 s->max_request_size = 0;
197 s->use_xattr = 0;
198 s->ssl_enabled = 0;
199 s->ssl_honor_cipher_order = 1;
200 s->ssl_empty_fragments = 0;
201 s->ssl_use_sslv2 = 0;
202 s->ssl_use_sslv3 = 0;
203 s->use_ipv6 = (i == 0) ? 0 : srv->config_storage[0]->use_ipv6;
204 s->set_v6only = (i == 0) ? 1 : srv->config_storage[0]->set_v6only;
205 s->defer_accept = (i == 0) ? 0 : srv->config_storage[0]->defer_accept;
206 #ifdef HAVE_LSTAT
207 s->follow_symlink = 1;
208 #endif
209 s->kbytes_per_second = 0;
210 s->allow_http11 = 1;
211 s->etag_use_inode = 1;
212 s->etag_use_mtime = 1;
213 s->etag_use_size = 1;
214 s->range_requests = 1;
215 s->force_lowercase_filenames = (i == 0) ? 2 : 0; /* we wan't to detect later if user changed this for global section */
216 s->global_kbytes_per_second = 0;
217 s->global_bytes_per_second_cnt = 0;
218 s->global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
219 s->ssl_verifyclient = 0;
220 s->ssl_verifyclient_enforce = 1;
221 s->ssl_verifyclient_username = buffer_init();
222 s->ssl_verifyclient_depth = 9;
223 s->ssl_verifyclient_export_cert = 0;
224 s->ssl_disable_client_renegotiation = 1;
225 s->listen_backlog = (0 == i ? 1024 : srv->config_storage[0]->listen_backlog);
226 s->stream_request_body = 0;
227 s->stream_response_body = 0;
229 /* all T_CONFIG_SCOPE_CONNECTION options */
230 cv[2].destination = s->errorfile_prefix;
231 cv[7].destination = s->server_tag;
232 cv[8].destination = &(s->use_ipv6);
234 cv[12].destination = &(s->max_request_size);
235 cv[14].destination = s->document_root;
236 cv[15].destination = &(s->force_lowercase_filenames);
237 cv[16].destination = &(s->log_condition_handling);
238 cv[17].destination = &(s->max_keep_alive_requests);
239 cv[18].destination = s->server_name;
240 cv[19].destination = &(s->max_keep_alive_idle);
242 cv[20].destination = &(s->max_read_idle);
243 cv[21].destination = &(s->max_write_idle);
244 cv[22].destination = s->error_handler;
245 #ifdef HAVE_LSTAT
246 cv[24].destination = &(s->follow_symlink);
247 #endif
248 cv[25].destination = &(s->global_kbytes_per_second);
249 cv[26].destination = &(s->kbytes_per_second);
250 cv[27].destination = &(s->use_xattr);
251 cv[28].destination = s->mimetypes;
252 cv[29].destination = s->ssl_pemfile;
254 cv[30].destination = &(s->ssl_enabled);
255 cv[31].destination = &(s->log_file_not_found);
256 cv[32].destination = &(s->log_request_handling);
257 cv[33].destination = &(s->log_response_header);
258 cv[34].destination = &(s->log_request_header);
259 cv[35].destination = &(s->log_ssl_noise);
260 cv[36].destination = &(s->allow_http11);
261 cv[39].destination = s->ssl_ca_file;
263 cv[41].destination = &(s->range_requests);
264 cv[47].destination = s->ssl_cipher_list;
265 cv[48].destination = &(s->ssl_use_sslv2);
266 cv[49].destination = &(s->etag_use_inode);
268 cv[50].destination = &(s->etag_use_mtime);
269 cv[51].destination = &(s->etag_use_size);
270 cv[53].destination = &(s->log_timeouts);
271 cv[54].destination = &(s->defer_accept);
272 cv[56].destination = &(s->ssl_verifyclient);
273 cv[57].destination = &(s->ssl_verifyclient_enforce);
274 cv[58].destination = &(s->ssl_verifyclient_depth);
275 cv[59].destination = s->ssl_verifyclient_username;
277 cv[60].destination = &(s->ssl_verifyclient_export_cert);
278 cv[61].destination = &(s->set_v6only);
279 cv[62].destination = &(s->ssl_use_sslv3);
280 cv[63].destination = s->ssl_dh_file;
281 cv[64].destination = s->ssl_ec_curve;
282 cv[65].destination = &(s->ssl_disable_client_renegotiation);
283 cv[66].destination = &(s->ssl_honor_cipher_order);
284 cv[67].destination = &(s->ssl_empty_fragments);
285 cv[70].destination = &(s->listen_backlog);
286 cv[71].destination = s->error_handler_404;
287 #if defined(__FreeBSD__) || defined(__NetBSD__) \
288 || defined(__OpenBSD__) || defined(__DragonflyBSD__)
289 cv[75].destination = s->bsd_accept_filter;
290 #endif
291 cv[76].destination = &(s->stream_request_body);
292 cv[77].destination = &(s->stream_response_body);
294 srv->config_storage[i] = s;
296 if (0 != (ret = config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION))) {
297 break;
300 if (s->stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN) {
301 s->stream_request_body |= FDEVENT_STREAM_REQUEST;
303 if (s->stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) {
304 s->stream_response_body |= FDEVENT_STREAM_RESPONSE;
309 specific_config *s = srv->config_storage[0];
310 s->http_parseopts= /*(global, but stored in con->conf.http_parseopts)*/
311 (srv->srvconf.http_header_strict ?(HTTP_PARSEOPT_HEADER_STRICT) :0)
312 |(srv->srvconf.http_host_strict ?(HTTP_PARSEOPT_HOST_STRICT
313 |HTTP_PARSEOPT_HOST_NORMALIZE):0)
314 |(srv->srvconf.http_host_normalize ?(HTTP_PARSEOPT_HOST_NORMALIZE):0);
317 if (buffer_string_is_empty(stat_cache_string)) {
318 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
319 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("simple"))) {
320 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
321 #ifdef HAVE_FAM_H
322 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("fam"))) {
323 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_FAM;
324 #endif
325 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("disable"))) {
326 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_NONE;
327 } else {
328 log_error_write(srv, __FILE__, __LINE__, "sb",
329 "server.stat-cache-engine can be one of \"disable\", \"simple\","
330 #ifdef HAVE_FAM_H
331 " \"fam\","
332 #endif
333 " but not:", stat_cache_string);
334 ret = HANDLER_ERROR;
337 buffer_free(stat_cache_string);
340 data_string *ds;
341 int prepend_mod_indexfile = 1;
342 int append_mod_dirlisting = 1;
343 int append_mod_staticfile = 1;
345 /* prepend default modules */
346 for (i = 0; i < srv->srvconf.modules->used; i++) {
347 ds = (data_string *)srv->srvconf.modules->data[i];
349 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_indexfile"))) {
350 prepend_mod_indexfile = 0;
353 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_staticfile"))) {
354 append_mod_staticfile = 0;
357 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_dirlisting"))) {
358 append_mod_dirlisting = 0;
361 if (0 == prepend_mod_indexfile &&
362 0 == append_mod_dirlisting &&
363 0 == append_mod_staticfile) {
364 break;
368 if (prepend_mod_indexfile) {
369 /* mod_indexfile has to be loaded before mod_fastcgi and friends */
370 array *modules = array_init();
372 ds = data_string_init();
373 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_indexfile"));
374 array_insert_unique(modules, (data_unset *)ds);
376 for (i = 0; i < srv->srvconf.modules->used; i++) {
377 data_unset *du = srv->srvconf.modules->data[i];
378 array_insert_unique(modules, du->copy(du));
381 array_free(srv->srvconf.modules);
382 srv->srvconf.modules = modules;
385 /* append default modules */
386 if (append_mod_dirlisting) {
387 ds = data_string_init();
388 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_dirlisting"));
389 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
392 if (append_mod_staticfile) {
393 ds = data_string_init();
394 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_staticfile"));
395 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
399 return ret;
404 #define PATCH(x) con->conf.x = s->x
405 int config_setup_connection(server *srv, connection *con) {
406 specific_config *s = srv->config_storage[0];
408 PATCH(http_parseopts);
410 PATCH(allow_http11);
411 PATCH(mimetypes);
412 PATCH(document_root);
413 PATCH(high_precision_timestamps);
414 PATCH(max_keep_alive_requests);
415 PATCH(max_keep_alive_idle);
416 PATCH(max_read_idle);
417 PATCH(max_write_idle);
418 PATCH(max_request_size);
419 PATCH(use_xattr);
420 PATCH(error_handler);
421 PATCH(error_handler_404);
422 PATCH(errorfile_prefix);
423 #ifdef HAVE_LSTAT
424 PATCH(follow_symlink);
425 #endif
426 PATCH(server_tag);
427 PATCH(kbytes_per_second);
428 PATCH(global_kbytes_per_second);
429 PATCH(global_bytes_per_second_cnt);
431 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
432 buffer_copy_buffer(con->server_name, s->server_name);
434 PATCH(log_request_header);
435 PATCH(log_response_header);
436 PATCH(log_request_handling);
437 PATCH(log_condition_handling);
438 PATCH(log_file_not_found);
439 PATCH(log_ssl_noise);
440 PATCH(log_timeouts);
442 PATCH(range_requests);
443 PATCH(force_lowercase_filenames);
444 /*PATCH(listen_backlog);*//*(not necessary; used only at startup)*/
445 PATCH(stream_request_body);
446 PATCH(stream_response_body);
448 PATCH(ssl_enabled);
450 PATCH(ssl_pemfile);
451 #ifdef USE_OPENSSL
452 PATCH(ssl_pemfile_x509);
453 PATCH(ssl_pemfile_pkey);
454 #endif
455 PATCH(ssl_ca_file);
456 #ifdef USE_OPENSSL
457 PATCH(ssl_ca_file_cert_names);
458 #endif
459 PATCH(ssl_cipher_list);
460 PATCH(ssl_dh_file);
461 PATCH(ssl_ec_curve);
462 PATCH(ssl_honor_cipher_order);
463 PATCH(ssl_empty_fragments);
464 PATCH(ssl_use_sslv2);
465 PATCH(ssl_use_sslv3);
466 PATCH(etag_use_inode);
467 PATCH(etag_use_mtime);
468 PATCH(etag_use_size);
470 PATCH(ssl_verifyclient);
471 PATCH(ssl_verifyclient_enforce);
472 PATCH(ssl_verifyclient_depth);
473 PATCH(ssl_verifyclient_username);
474 PATCH(ssl_verifyclient_export_cert);
475 PATCH(ssl_disable_client_renegotiation);
477 return 0;
480 int config_patch_connection(server *srv, connection *con) {
481 size_t i, j;
483 /* skip the first, the global context */
484 for (i = 1; i < srv->config_context->used; i++) {
485 data_config *dc = (data_config *)srv->config_context->data[i];
486 specific_config *s = srv->config_storage[i];
488 /* condition didn't match */
489 if (!config_check_cond(srv, con, dc)) continue;
491 /* merge config */
492 for (j = 0; j < dc->value->used; j++) {
493 data_unset *du = dc->value->data[j];
495 if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.document-root"))) {
496 PATCH(document_root);
497 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.range-requests"))) {
498 PATCH(range_requests);
499 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler"))) {
500 PATCH(error_handler);
501 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler-404"))) {
502 PATCH(error_handler_404);
503 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.errorfile-prefix"))) {
504 PATCH(errorfile_prefix);
505 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.assign"))) {
506 PATCH(mimetypes);
507 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-requests"))) {
508 PATCH(max_keep_alive_requests);
509 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-idle"))) {
510 PATCH(max_keep_alive_idle);
511 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-write-idle"))) {
512 PATCH(max_write_idle);
513 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-read-idle"))) {
514 PATCH(max_read_idle);
515 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-request-size"))) {
516 PATCH(max_request_size);
517 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.use-xattr"))) {
518 PATCH(use_xattr);
519 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-inode"))) {
520 PATCH(etag_use_inode);
521 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-mtime"))) {
522 PATCH(etag_use_mtime);
523 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-size"))) {
524 PATCH(etag_use_size);
525 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.pemfile"))) {
526 PATCH(ssl_pemfile);
527 #ifdef USE_OPENSSL
528 PATCH(ssl_pemfile_x509);
529 PATCH(ssl_pemfile_pkey);
530 #endif
531 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ca-file"))) {
532 PATCH(ssl_ca_file);
533 #ifdef USE_OPENSSL
534 PATCH(ssl_ca_file_cert_names);
535 #endif
536 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.honor-cipher-order"))) {
537 PATCH(ssl_honor_cipher_order);
538 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.empty-fragments"))) {
539 PATCH(ssl_empty_fragments);
540 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv2"))) {
541 PATCH(ssl_use_sslv2);
542 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv3"))) {
543 PATCH(ssl_use_sslv3);
544 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.cipher-list"))) {
545 PATCH(ssl_cipher_list);
546 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.engine"))) {
547 PATCH(ssl_enabled);
548 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.dh-file"))) {
549 PATCH(ssl_dh_file);
550 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ec-curve"))) {
551 PATCH(ssl_ec_curve);
552 #ifdef HAVE_LSTAT
553 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.follow-symlink"))) {
554 PATCH(follow_symlink);
555 #endif
556 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.name"))) {
557 buffer_copy_buffer(con->server_name, s->server_name);
558 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.tag"))) {
559 PATCH(server_tag);
560 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-request-body"))) {
561 PATCH(stream_request_body);
562 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-response-body"))) {
563 PATCH(stream_response_body);
564 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("connection.kbytes-per-second"))) {
565 PATCH(kbytes_per_second);
566 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-handling"))) {
567 PATCH(log_request_handling);
568 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-header"))) {
569 PATCH(log_request_header);
570 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-response-header"))) {
571 PATCH(log_response_header);
572 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-condition-handling"))) {
573 PATCH(log_condition_handling);
574 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-file-not-found"))) {
575 PATCH(log_file_not_found);
576 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-ssl-noise"))) {
577 PATCH(log_ssl_noise);
578 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-timeouts"))) {
579 PATCH(log_timeouts);
580 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.protocol-http11"))) {
581 PATCH(allow_http11);
582 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.force-lowercase-filenames"))) {
583 PATCH(force_lowercase_filenames);
584 #if 0 /*(not necessary; used only at startup)*/
585 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.listen-backlog"))) {
586 PATCH(listen_backlog);
587 #endif
588 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.kbytes-per-second"))) {
589 PATCH(global_kbytes_per_second);
590 PATCH(global_bytes_per_second_cnt);
591 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
592 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.activate"))) {
593 PATCH(ssl_verifyclient);
594 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.enforce"))) {
595 PATCH(ssl_verifyclient_enforce);
596 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.depth"))) {
597 PATCH(ssl_verifyclient_depth);
598 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.username"))) {
599 PATCH(ssl_verifyclient_username);
600 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.exportcert"))) {
601 PATCH(ssl_verifyclient_export_cert);
602 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.disable-client-renegotiation"))) {
603 PATCH(ssl_disable_client_renegotiation);
608 con->etag_flags = (con->conf.etag_use_mtime ? ETAG_USE_MTIME : 0) |
609 (con->conf.etag_use_inode ? ETAG_USE_INODE : 0) |
610 (con->conf.etag_use_size ? ETAG_USE_SIZE : 0);
612 return 0;
614 #undef PATCH
616 typedef struct {
617 int foo;
618 int bar;
620 const buffer *source;
621 const char *input;
622 size_t offset;
623 size_t size;
625 int line_pos;
626 int line;
628 int in_key;
629 int in_brace;
630 int in_cond;
631 } tokenizer_t;
633 #if 0
634 static int tokenizer_open(server *srv, tokenizer_t *t, buffer *basedir, const char *fn) {
635 if (buffer_string_is_empty(basedir) ||
636 (fn[0] == '/' || fn[0] == '\\') ||
637 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\'))) {
638 t->file = buffer_init_string(fn);
639 } else {
640 t->file = buffer_init_buffer(basedir);
641 buffer_append_string(t->file, fn);
644 if (0 != stream_open(&(t->s), t->file)) {
645 log_error_write(srv, __FILE__, __LINE__, "sbss",
646 "opening configfile ", t->file, "failed:", strerror(errno));
647 buffer_free(t->file);
648 return -1;
651 t->input = t->s.start;
652 t->offset = 0;
653 t->size = t->s.size;
654 t->line = 1;
655 t->line_pos = 1;
657 t->in_key = 1;
658 t->in_brace = 0;
659 t->in_cond = 0;
660 return 0;
663 static int tokenizer_close(server *srv, tokenizer_t *t) {
664 UNUSED(srv);
666 buffer_free(t->file);
667 return stream_close(&(t->s));
669 #endif
670 static int config_skip_newline(tokenizer_t *t) {
671 int skipped = 1;
672 force_assert(t->input[t->offset] == '\r' || t->input[t->offset] == '\n');
673 if (t->input[t->offset] == '\r' && t->input[t->offset + 1] == '\n') {
674 skipped ++;
675 t->offset ++;
677 t->offset ++;
678 return skipped;
681 static int config_skip_comment(tokenizer_t *t) {
682 int i;
683 force_assert(t->input[t->offset] == '#');
684 for (i = 1; t->input[t->offset + i] &&
685 (t->input[t->offset + i] != '\n' && t->input[t->offset + i] != '\r');
686 i++);
687 t->offset += i;
688 return i;
691 static int config_tokenizer(server *srv, tokenizer_t *t, int *token_id, buffer *token) {
692 int tid = 0;
693 size_t i;
695 for (tid = 0; tid == 0 && t->offset < t->size && t->input[t->offset] ; ) {
696 char c = t->input[t->offset];
697 const char *start = NULL;
699 switch (c) {
700 case '=':
701 if (t->in_brace) {
702 if (t->input[t->offset + 1] == '>') {
703 t->offset += 2;
705 buffer_copy_string_len(token, CONST_STR_LEN("=>"));
707 tid = TK_ARRAY_ASSIGN;
708 } else {
709 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
710 "source:", t->source,
711 "line:", t->line, "pos:", t->line_pos,
712 "use => for assignments in arrays");
713 return -1;
715 } else if (t->in_cond) {
716 if (t->input[t->offset + 1] == '=') {
717 t->offset += 2;
719 buffer_copy_string_len(token, CONST_STR_LEN("=="));
721 tid = TK_EQ;
722 } else if (t->input[t->offset + 1] == '~') {
723 t->offset += 2;
725 buffer_copy_string_len(token, CONST_STR_LEN("=~"));
727 tid = TK_MATCH;
728 } else {
729 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
730 "source:", t->source,
731 "line:", t->line, "pos:", t->line_pos,
732 "only =~ and == are allowed in the condition");
733 return -1;
735 t->in_key = 1;
736 t->in_cond = 0;
737 } else if (t->in_key) {
738 tid = TK_ASSIGN;
740 buffer_copy_string_len(token, t->input + t->offset, 1);
742 t->offset++;
743 t->line_pos++;
744 } else {
745 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
746 "source:", t->source,
747 "line:", t->line, "pos:", t->line_pos,
748 "unexpected equal-sign: =");
749 return -1;
752 break;
753 case '!':
754 if (t->in_cond) {
755 if (t->input[t->offset + 1] == '=') {
756 t->offset += 2;
758 buffer_copy_string_len(token, CONST_STR_LEN("!="));
760 tid = TK_NE;
761 } else if (t->input[t->offset + 1] == '~') {
762 t->offset += 2;
764 buffer_copy_string_len(token, CONST_STR_LEN("!~"));
766 tid = TK_NOMATCH;
767 } else {
768 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
769 "source:", t->source,
770 "line:", t->line, "pos:", t->line_pos,
771 "only !~ and != are allowed in the condition");
772 return -1;
774 t->in_key = 1;
775 t->in_cond = 0;
776 } else {
777 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
778 "source:", t->source,
779 "line:", t->line, "pos:", t->line_pos,
780 "unexpected exclamation-marks: !");
781 return -1;
784 break;
785 case '\t':
786 case ' ':
787 t->offset++;
788 t->line_pos++;
789 break;
790 case '\n':
791 case '\r':
792 if (t->in_brace == 0) {
793 int done = 0;
794 while (!done && t->offset < t->size) {
795 switch (t->input[t->offset]) {
796 case '\r':
797 case '\n':
798 config_skip_newline(t);
799 t->line_pos = 1;
800 t->line++;
801 break;
803 case '#':
804 t->line_pos += config_skip_comment(t);
805 break;
807 case '\t':
808 case ' ':
809 t->offset++;
810 t->line_pos++;
811 break;
813 default:
814 done = 1;
817 t->in_key = 1;
818 tid = TK_EOL;
819 buffer_copy_string_len(token, CONST_STR_LEN("(EOL)"));
820 } else {
821 config_skip_newline(t);
822 t->line_pos = 1;
823 t->line++;
825 break;
826 case ',':
827 if (t->in_brace > 0) {
828 tid = TK_COMMA;
830 buffer_copy_string_len(token, CONST_STR_LEN("(COMMA)"));
833 t->offset++;
834 t->line_pos++;
835 break;
836 case '"':
837 /* search for the terminating " */
838 start = t->input + t->offset + 1;
839 buffer_copy_string_len(token, CONST_STR_LEN(""));
841 for (i = 1; t->input[t->offset + i]; i++) {
842 if (t->input[t->offset + i] == '\\' &&
843 t->input[t->offset + i + 1] == '"') {
845 buffer_append_string_len(token, start, t->input + t->offset + i - start);
847 start = t->input + t->offset + i + 1;
849 /* skip the " */
850 i++;
851 continue;
855 if (t->input[t->offset + i] == '"') {
856 tid = TK_STRING;
858 buffer_append_string_len(token, start, t->input + t->offset + i - start);
860 break;
864 if (t->input[t->offset + i] == '\0') {
865 /* ERROR */
867 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
868 "source:", t->source,
869 "line:", t->line, "pos:", t->line_pos,
870 "missing closing quote");
872 return -1;
875 t->offset += i + 1;
876 t->line_pos += i + 1;
878 break;
879 case '(':
880 t->offset++;
881 t->in_brace++;
883 tid = TK_LPARAN;
885 buffer_copy_string_len(token, CONST_STR_LEN("("));
886 break;
887 case ')':
888 t->offset++;
889 t->in_brace--;
891 tid = TK_RPARAN;
893 buffer_copy_string_len(token, CONST_STR_LEN(")"));
894 break;
895 case '$':
896 t->offset++;
898 tid = TK_DOLLAR;
899 t->in_cond = 1;
900 t->in_key = 0;
902 buffer_copy_string_len(token, CONST_STR_LEN("$"));
904 break;
906 case '+':
907 if (t->input[t->offset + 1] == '=') {
908 t->offset += 2;
909 buffer_copy_string_len(token, CONST_STR_LEN("+="));
910 tid = TK_APPEND;
911 } else {
912 t->offset++;
913 tid = TK_PLUS;
914 buffer_copy_string_len(token, CONST_STR_LEN("+"));
916 break;
918 case '{':
919 t->offset++;
921 tid = TK_LCURLY;
923 buffer_copy_string_len(token, CONST_STR_LEN("{"));
925 break;
927 case '}':
928 t->offset++;
930 tid = TK_RCURLY;
932 buffer_copy_string_len(token, CONST_STR_LEN("}"));
934 break;
936 case '[':
937 t->offset++;
939 tid = TK_LBRACKET;
941 buffer_copy_string_len(token, CONST_STR_LEN("["));
943 break;
945 case ']':
946 t->offset++;
948 tid = TK_RBRACKET;
950 buffer_copy_string_len(token, CONST_STR_LEN("]"));
952 break;
953 case '#':
954 t->line_pos += config_skip_comment(t);
956 break;
957 default:
958 if (t->in_cond) {
959 for (i = 0; t->input[t->offset + i] &&
960 (isalpha((unsigned char)t->input[t->offset + i])
961 ); i++);
963 if (i && t->input[t->offset + i]) {
964 tid = TK_SRVVARNAME;
965 buffer_copy_string_len(token, t->input + t->offset, i);
967 t->offset += i;
968 t->line_pos += i;
969 } else {
970 /* ERROR */
971 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
972 "source:", t->source,
973 "line:", t->line, "pos:", t->line_pos,
974 "invalid character in condition");
975 return -1;
977 } else if (isdigit((unsigned char)c)) {
978 /* take all digits */
979 for (i = 0; t->input[t->offset + i] && isdigit((unsigned char)t->input[t->offset + i]); i++);
981 /* was there it least a digit ? */
982 if (i) {
983 tid = TK_INTEGER;
985 buffer_copy_string_len(token, t->input + t->offset, i);
987 t->offset += i;
988 t->line_pos += i;
990 } else {
991 /* the key might consist of [-.0-9a-z] */
992 for (i = 0; t->input[t->offset + i] &&
993 (isalnum((unsigned char)t->input[t->offset + i]) ||
994 t->input[t->offset + i] == '.' ||
995 t->input[t->offset + i] == '_' || /* for env.* */
996 t->input[t->offset + i] == '-'
997 ); i++);
999 if (i && t->input[t->offset + i]) {
1000 buffer_copy_string_len(token, t->input + t->offset, i);
1002 if (strcmp(token->ptr, "include") == 0) {
1003 tid = TK_INCLUDE;
1004 } else if (strcmp(token->ptr, "include_shell") == 0) {
1005 tid = TK_INCLUDE_SHELL;
1006 } else if (strcmp(token->ptr, "global") == 0) {
1007 tid = TK_GLOBAL;
1008 } else if (strcmp(token->ptr, "else") == 0) {
1009 tid = TK_ELSE;
1010 } else {
1011 tid = TK_LKEY;
1014 t->offset += i;
1015 t->line_pos += i;
1016 } else {
1017 /* ERROR */
1018 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
1019 "source:", t->source,
1020 "line:", t->line, "pos:", t->line_pos,
1021 "invalid character in variable name");
1022 return -1;
1025 break;
1029 if (tid) {
1030 *token_id = tid;
1031 #if 0
1032 log_error_write(srv, __FILE__, __LINE__, "sbsdsdbdd",
1033 "source:", t->source,
1034 "line:", t->line, "pos:", t->line_pos,
1035 token, token->used - 1, tid);
1036 #endif
1038 return 1;
1039 } else if (t->offset < t->size) {
1040 fprintf(stderr, "%s.%d: %d, %s\n",
1041 __FILE__, __LINE__,
1042 tid, token->ptr);
1044 return 0;
1047 static int config_parse(server *srv, config_t *context, tokenizer_t *t) {
1048 void *pParser;
1049 int token_id;
1050 buffer *token, *lasttoken;
1051 int ret;
1053 pParser = configparserAlloc( malloc );
1054 force_assert(pParser);
1055 lasttoken = buffer_init();
1056 token = buffer_init();
1057 while((1 == (ret = config_tokenizer(srv, t, &token_id, token))) && context->ok) {
1058 buffer_copy_buffer(lasttoken, token);
1059 configparser(pParser, token_id, token, context);
1061 token = buffer_init();
1063 buffer_free(token);
1065 if (ret != -1 && context->ok) {
1066 /* add an EOL at EOF, better than say sorry */
1067 configparser(pParser, TK_EOL, buffer_init_string("(EOL)"), context);
1068 if (context->ok) {
1069 configparser(pParser, 0, NULL, context);
1072 configparserFree(pParser, free);
1074 if (ret == -1) {
1075 log_error_write(srv, __FILE__, __LINE__, "sb",
1076 "configfile parser failed at:", lasttoken);
1077 } else if (context->ok == 0) {
1078 log_error_write(srv, __FILE__, __LINE__, "sbsdsdsb",
1079 "source:", t->source,
1080 "line:", t->line, "pos:", t->line_pos,
1081 "parser failed somehow near here:", lasttoken);
1082 ret = -1;
1084 buffer_free(lasttoken);
1086 return ret == -1 ? -1 : 0;
1089 static int tokenizer_init(tokenizer_t *t, const buffer *source, const char *input, size_t size) {
1091 t->source = source;
1092 t->input = input;
1093 t->size = size;
1094 t->offset = 0;
1095 t->line = 1;
1096 t->line_pos = 1;
1098 t->in_key = 1;
1099 t->in_brace = 0;
1100 t->in_cond = 0;
1101 return 0;
1104 static int config_parse_file_stream(server *srv, config_t *context, const buffer *filename) {
1105 tokenizer_t t;
1106 stream s;
1107 int ret;
1109 if (0 != stream_open(&s, filename)) {
1110 log_error_write(srv, __FILE__, __LINE__, "sbss",
1111 "opening configfile ", filename, "failed:", strerror(errno));
1112 return -1;
1113 } else {
1114 tokenizer_init(&t, filename, s.start, s.size);
1115 ret = config_parse(srv, context, &t);
1118 stream_close(&s);
1119 return ret;
1122 int config_parse_file(server *srv, config_t *context, const char *fn) {
1123 buffer *filename;
1124 size_t i;
1125 int ret = -1;
1126 #ifdef GLOB_BRACE
1127 int flags = GLOB_BRACE;
1128 #else
1129 int flags = 0;
1130 #endif
1131 glob_t gl;
1133 if ((fn[0] == '/' || fn[0] == '\\') ||
1134 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\')) ||
1135 (fn[0] == '.' && fn[1] == '.' && (fn[2] == '/' || fn[2] == '\\'))) {
1136 filename = buffer_init_string(fn);
1137 } else {
1138 filename = buffer_init_buffer(context->basedir);
1139 buffer_append_string(filename, fn);
1142 switch (glob(filename->ptr, flags, NULL, &gl)) {
1143 case 0:
1144 for (i = 0; i < gl.gl_pathc; ++i) {
1145 buffer_copy_string(filename, gl.gl_pathv[i]);
1146 ret = config_parse_file_stream(srv, context, filename);
1147 if (0 != ret) break;
1149 globfree(&gl);
1150 break;
1151 case GLOB_NOMATCH:
1152 if (filename->ptr[strcspn(filename->ptr, "*?[]{}")] != '\0') { /*(contains glob metachars)*/
1153 ret = 0; /* not an error if no files match glob pattern */
1155 else {
1156 log_error_write(srv, __FILE__, __LINE__, "sb", "include file not found: ", filename);
1158 break;
1159 case GLOB_ABORTED:
1160 case GLOB_NOSPACE:
1161 log_error_write(srv, __FILE__, __LINE__, "sbss", "glob()", filename, "failed:", strerror(errno));
1162 break;
1165 buffer_free(filename);
1166 return ret;
1169 static char* getCWD(void) {
1170 char *s, *s1;
1171 size_t len;
1172 #ifdef PATH_MAX
1173 len = PATH_MAX;
1174 #else
1175 len = 4096;
1176 #endif
1178 s = malloc(len);
1179 if (!s) return NULL;
1180 while (NULL == getcwd(s, len)) {
1181 if (errno != ERANGE || SSIZE_MAX - len < len) {
1182 free(s);
1183 return NULL;
1185 len *= 2;
1186 s1 = realloc(s, len);
1187 if (!s1) {
1188 free(s);
1189 return NULL;
1191 s = s1;
1193 return s;
1196 int config_parse_cmd(server *srv, config_t *context, const char *cmd) {
1197 tokenizer_t t;
1198 int ret;
1199 buffer *source;
1200 buffer *out;
1201 char *oldpwd;
1203 if (NULL == (oldpwd = getCWD())) {
1204 log_error_write(srv, __FILE__, __LINE__, "s",
1205 "cannot get cwd", strerror(errno));
1206 return -1;
1209 if (!buffer_string_is_empty(context->basedir)) {
1210 if (0 != chdir(context->basedir->ptr)) {
1211 log_error_write(srv, __FILE__, __LINE__, "sbs",
1212 "cannot change directory to", context->basedir, strerror(errno));
1213 free(oldpwd);
1214 return -1;
1218 source = buffer_init_string(cmd);
1219 out = buffer_init();
1221 if (0 != proc_open_buffer(cmd, NULL, out, NULL)) {
1222 log_error_write(srv, __FILE__, __LINE__, "sbss",
1223 "opening", source, "failed:", strerror(errno));
1224 ret = -1;
1225 } else {
1226 tokenizer_init(&t, source, CONST_BUF_LEN(out));
1227 ret = config_parse(srv, context, &t);
1230 buffer_free(source);
1231 buffer_free(out);
1232 if (0 != chdir(oldpwd)) {
1233 log_error_write(srv, __FILE__, __LINE__, "sss",
1234 "cannot change directory to", oldpwd, strerror(errno));
1235 free(oldpwd);
1236 return -1;
1238 free(oldpwd);
1239 return ret;
1242 static void context_init(server *srv, config_t *context) {
1243 context->srv = srv;
1244 context->ok = 1;
1245 vector_config_weak_init(&context->configs_stack);
1246 context->basedir = buffer_init();
1249 static void context_free(config_t *context) {
1250 vector_config_weak_clear(&context->configs_stack);
1251 buffer_free(context->basedir);
1254 int config_read(server *srv, const char *fn) {
1255 config_t context;
1256 data_config *dc;
1257 data_integer *dpid;
1258 data_string *dcwd;
1259 int ret;
1260 char *pos;
1261 buffer *filename;
1263 context_init(srv, &context);
1264 context.all_configs = srv->config_context;
1266 #ifdef __WIN32
1267 pos = strrchr(fn, '\\');
1268 #else
1269 pos = strrchr(fn, '/');
1270 #endif
1271 if (pos) {
1272 buffer_copy_string_len(context.basedir, fn, pos - fn + 1);
1275 dc = data_config_init();
1276 buffer_copy_string_len(dc->key, CONST_STR_LEN("global"));
1278 force_assert(context.all_configs->used == 0);
1279 dc->context_ndx = context.all_configs->used;
1280 array_insert_unique(context.all_configs, (data_unset *)dc);
1281 context.current = dc;
1283 /* default context */
1284 dpid = data_integer_init();
1285 dpid->value = getpid();
1286 buffer_copy_string_len(dpid->key, CONST_STR_LEN("var.PID"));
1287 array_insert_unique(dc->value, (data_unset *)dpid);
1289 dcwd = data_string_init();
1290 buffer_string_prepare_copy(dcwd->value, 1023);
1291 if (NULL != getcwd(dcwd->value->ptr, dcwd->value->size - 1)) {
1292 buffer_commit(dcwd->value, strlen(dcwd->value->ptr));
1293 buffer_copy_string_len(dcwd->key, CONST_STR_LEN("var.CWD"));
1294 array_insert_unique(dc->value, (data_unset *)dcwd);
1295 } else {
1296 dcwd->free((data_unset*) dcwd);
1299 filename = buffer_init_string(fn);
1300 ret = config_parse_file_stream(srv, &context, filename);
1301 buffer_free(filename);
1303 /* remains nothing if parser is ok */
1304 force_assert(!(0 == ret && context.ok && 0 != context.configs_stack.used));
1305 context_free(&context);
1307 if (0 != ret) {
1308 return ret;
1311 if (0 != config_insert(srv)) {
1312 return -1;
1315 return 0;
1318 int config_set_defaults(server *srv) {
1319 size_t i;
1320 specific_config *s = srv->config_storage[0];
1321 struct stat st1, st2;
1323 struct ev_map { fdevent_handler_t et; const char *name; } event_handlers[] =
1325 /* - epoll is most reliable
1326 * - select works everywhere
1328 #ifdef USE_LINUX_EPOLL
1329 { FDEVENT_HANDLER_LINUX_SYSEPOLL, "linux-sysepoll" },
1330 #endif
1331 #ifdef USE_POLL
1332 { FDEVENT_HANDLER_POLL, "poll" },
1333 #endif
1334 #ifdef USE_SELECT
1335 { FDEVENT_HANDLER_SELECT, "select" },
1336 #endif
1337 #ifdef USE_LIBEV
1338 { FDEVENT_HANDLER_LIBEV, "libev" },
1339 #endif
1340 #ifdef USE_SOLARIS_DEVPOLL
1341 { FDEVENT_HANDLER_SOLARIS_DEVPOLL,"solaris-devpoll" },
1342 #endif
1343 #ifdef USE_SOLARIS_PORT
1344 { FDEVENT_HANDLER_SOLARIS_PORT, "solaris-eventports" },
1345 #endif
1346 #ifdef USE_FREEBSD_KQUEUE
1347 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "freebsd-kqueue" },
1348 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "kqueue" },
1349 #endif
1350 { FDEVENT_HANDLER_UNSET, NULL }
1353 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1354 if (-1 == stat(srv->srvconf.changeroot->ptr, &st1)) {
1355 log_error_write(srv, __FILE__, __LINE__, "sb",
1356 "server.chroot doesn't exist:", srv->srvconf.changeroot);
1357 return -1;
1359 if (!S_ISDIR(st1.st_mode)) {
1360 log_error_write(srv, __FILE__, __LINE__, "sb",
1361 "server.chroot isn't a directory:", srv->srvconf.changeroot);
1362 return -1;
1366 if (srv->srvconf.upload_tempdirs->used) {
1367 buffer * const b = srv->tmp_buf;
1368 size_t len;
1369 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1370 buffer_copy_buffer(b, srv->srvconf.changeroot);
1371 buffer_append_slash(b);
1372 } else {
1373 buffer_reset(b);
1375 len = buffer_string_length(b);
1377 for (i = 0; i < srv->srvconf.upload_tempdirs->used; ++i) {
1378 const data_string * const ds = (data_string *)srv->srvconf.upload_tempdirs->data[i];
1379 buffer_string_set_length(b, len); /*(truncate)*/
1380 buffer_append_string_buffer(b, ds->value);
1381 if (-1 == stat(b->ptr, &st1)) {
1382 log_error_write(srv, __FILE__, __LINE__, "sb",
1383 "server.upload-dirs doesn't exist:", b);
1384 } else if (!S_ISDIR(st1.st_mode)) {
1385 log_error_write(srv, __FILE__, __LINE__, "sb",
1386 "server.upload-dirs isn't a directory:", b);
1391 if (buffer_string_is_empty(s->document_root)) {
1392 log_error_write(srv, __FILE__, __LINE__, "s",
1393 "a default document-root has to be set");
1395 return -1;
1398 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1400 buffer_to_lower(srv->tmp_buf);
1402 if (2 == s->force_lowercase_filenames) { /* user didn't configure it in global section? */
1403 s->force_lowercase_filenames = 0; /* default to 0 */
1405 if (0 == stat(srv->tmp_buf->ptr, &st1)) {
1406 int is_lower = 0;
1408 is_lower = buffer_is_equal(srv->tmp_buf, s->document_root);
1410 /* lower-case existed, check upper-case */
1411 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1413 buffer_to_upper(srv->tmp_buf);
1415 /* we have to handle the special case that upper and lower-casing results in the same filename
1416 * as in server.document-root = "/" or "/12345/" */
1418 if (is_lower && buffer_is_equal(srv->tmp_buf, s->document_root)) {
1419 /* lower-casing and upper-casing didn't result in
1420 * an other filename, no need to stat(),
1421 * just assume it is case-sensitive. */
1423 s->force_lowercase_filenames = 0;
1424 } else if (0 == stat(srv->tmp_buf->ptr, &st2)) {
1426 /* upper case exists too, doesn't the FS handle this ? */
1428 /* upper and lower have the same inode -> case-insensitve FS */
1430 if (st1.st_ino == st2.st_ino) {
1431 /* upper and lower have the same inode -> case-insensitve FS */
1433 s->force_lowercase_filenames = 1;
1439 if (srv->srvconf.port == 0) {
1440 srv->srvconf.port = s->ssl_enabled ? 443 : 80;
1443 if (buffer_string_is_empty(srv->srvconf.event_handler)) {
1444 /* choose a good default
1446 * the event_handler list is sorted by 'goodness'
1447 * taking the first available should be the best solution
1449 srv->event_handler = event_handlers[0].et;
1451 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1452 log_error_write(srv, __FILE__, __LINE__, "s",
1453 "sorry, there is no event handler for this system");
1455 return -1;
1457 } else {
1459 * User override
1462 for (i = 0; event_handlers[i].name; i++) {
1463 if (0 == strcmp(event_handlers[i].name, srv->srvconf.event_handler->ptr)) {
1464 srv->event_handler = event_handlers[i].et;
1465 break;
1469 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1470 log_error_write(srv, __FILE__, __LINE__, "sb",
1471 "the selected event-handler in unknown or not supported:",
1472 srv->srvconf.event_handler );
1474 return -1;
1478 if (s->ssl_enabled) {
1479 if (buffer_string_is_empty(s->ssl_pemfile)) {
1480 /* PEM file is require */
1482 log_error_write(srv, __FILE__, __LINE__, "s",
1483 "ssl.pemfile has to be set");
1484 return -1;
1487 #ifndef USE_OPENSSL
1488 log_error_write(srv, __FILE__, __LINE__, "s",
1489 "ssl support is missing, recompile with --with-openssl");
1491 return -1;
1492 #endif
1495 return 0;