graceful shutdown without unnecessary 1 sec delay
[lighttpd.git] / src / configfile.c
blob6489b09c505e512a80e8073ec8915f8abb92e4b8
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 { "server.host",
127 "use server.bind instead",
128 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
129 { "server.docroot",
130 "use server.document-root instead",
131 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
132 { "server.virtual-root",
133 "load mod_simple_vhost and use simple-vhost.server-root instead",
134 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
135 { "server.virtual-default-host",
136 "load mod_simple_vhost and use simple-vhost.default-host instead",
137 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
138 { "server.virtual-docroot",
139 "load mod_simple_vhost and use simple-vhost.document-root instead",
140 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
141 { "server.userid",
142 "use server.username instead",
143 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
144 { "server.groupid",
145 "use server.groupname instead",
146 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
147 { "server.use-keep-alive",
148 "use server.max-keep-alive-requests = 0 instead",
149 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
150 { "server.force-lower-case-files",
151 "use server.force-lowercase-filenames instead",
152 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
154 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
157 /* all T_CONFIG_SCOPE_SERVER options */
158 cv[0].destination = srv->srvconf.bindhost;
159 cv[1].destination = srv->srvconf.errorlog_file;
160 cv[3].destination = srv->srvconf.changeroot;
161 cv[4].destination = srv->srvconf.username;
162 cv[5].destination = srv->srvconf.groupname;
163 cv[6].destination = &(srv->srvconf.port);
164 cv[9].destination = srv->srvconf.modules;
166 cv[10].destination = srv->srvconf.event_handler;
167 cv[11].destination = srv->srvconf.pid_file;
168 cv[12].destination = &(srv->srvconf.max_request_size);
169 cv[13].destination = &(srv->srvconf.max_worker);
171 cv[23].destination = &(srv->srvconf.max_fds);
173 cv[37].destination = &(srv->srvconf.log_request_header_on_error);
174 cv[38].destination = &(srv->srvconf.log_state_handling);
176 cv[40].destination = &(srv->srvconf.errorlog_use_syslog);
177 stat_cache_string = buffer_init();
178 cv[42].destination = stat_cache_string;
179 cv[43].destination = &(srv->srvconf.max_conns);
180 cv[44].destination = srv->srvconf.network_backend;
181 cv[45].destination = srv->srvconf.upload_tempdirs;
182 cv[46].destination = &(srv->srvconf.enable_cores);
184 cv[52].destination = &(srv->srvconf.reject_expect_100_with_417);
185 cv[55].destination = srv->srvconf.breakagelog_file;
187 cv[68].destination = &(srv->srvconf.upload_temp_file_size);
188 cv[69].destination = srv->srvconf.xattr_name;
189 cv[72].destination = &(srv->srvconf.http_header_strict);
190 cv[73].destination = &(srv->srvconf.http_host_strict);
191 cv[74].destination = &(srv->srvconf.http_host_normalize);
193 srv->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
195 force_assert(srv->config_storage);
197 for (i = 0; i < srv->config_context->used; i++) {
198 data_config const* config = (data_config const*)srv->config_context->data[i];
199 specific_config *s;
201 s = calloc(1, sizeof(specific_config));
202 force_assert(s);
203 s->document_root = buffer_init();
204 s->mimetypes = array_init();
205 s->server_name = buffer_init();
206 s->ssl_pemfile = buffer_init();
207 s->ssl_ca_file = buffer_init();
208 s->error_handler = buffer_init();
209 s->error_handler_404 = buffer_init();
210 s->server_tag = buffer_init_string(PACKAGE_DESC);
211 s->ssl_cipher_list = buffer_init();
212 s->ssl_dh_file = buffer_init();
213 s->ssl_ec_curve = buffer_init();
214 s->errorfile_prefix = buffer_init();
215 #if defined(__FreeBSD__) || defined(__NetBSD__) \
216 || defined(__OpenBSD__) || defined(__DragonflyBSD__)
217 s->bsd_accept_filter = (i == 0)
218 ? buffer_init()
219 : buffer_init_buffer(srv->config_storage[0]->bsd_accept_filter);
220 #endif
221 s->max_keep_alive_requests = 16;
222 s->max_keep_alive_idle = 5;
223 s->max_read_idle = 60;
224 s->max_write_idle = 360;
225 s->use_xattr = 0;
226 s->ssl_enabled = 0;
227 s->ssl_honor_cipher_order = 1;
228 s->ssl_empty_fragments = 0;
229 s->ssl_use_sslv2 = 0;
230 s->ssl_use_sslv3 = 0;
231 s->use_ipv6 = 0;
232 s->set_v6only = 1;
233 s->defer_accept = (i == 0) ? 0 : srv->config_storage[0]->defer_accept;
234 #ifdef HAVE_LSTAT
235 s->follow_symlink = 1;
236 #endif
237 s->kbytes_per_second = 0;
238 s->allow_http11 = 1;
239 s->etag_use_inode = 1;
240 s->etag_use_mtime = 1;
241 s->etag_use_size = 1;
242 s->range_requests = 1;
243 s->force_lowercase_filenames = (i == 0) ? 2 : 0; /* we wan't to detect later if user changed this for global section */
244 s->global_kbytes_per_second = 0;
245 s->global_bytes_per_second_cnt = 0;
246 s->global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
247 s->ssl_verifyclient = 0;
248 s->ssl_verifyclient_enforce = 1;
249 s->ssl_verifyclient_username = buffer_init();
250 s->ssl_verifyclient_depth = 9;
251 s->ssl_verifyclient_export_cert = 0;
252 s->ssl_disable_client_renegotiation = 1;
253 s->listen_backlog = (0 == i ? 1024 : srv->config_storage[0]->listen_backlog);
254 s->stream_request_body = 0;
255 s->stream_response_body = 0;
257 /* all T_CONFIG_SCOPE_CONNECTION options */
258 cv[2].destination = s->errorfile_prefix;
259 cv[7].destination = s->server_tag;
260 cv[8].destination = &(s->use_ipv6);
262 cv[14].destination = s->document_root;
263 cv[15].destination = &(s->force_lowercase_filenames);
264 cv[16].destination = &(s->log_condition_handling);
265 cv[17].destination = &(s->max_keep_alive_requests);
266 cv[18].destination = s->server_name;
267 cv[19].destination = &(s->max_keep_alive_idle);
269 cv[20].destination = &(s->max_read_idle);
270 cv[21].destination = &(s->max_write_idle);
271 cv[22].destination = s->error_handler;
272 #ifdef HAVE_LSTAT
273 cv[24].destination = &(s->follow_symlink);
274 #endif
275 cv[25].destination = &(s->global_kbytes_per_second);
276 cv[26].destination = &(s->kbytes_per_second);
277 cv[27].destination = &(s->use_xattr);
278 cv[28].destination = s->mimetypes;
279 cv[29].destination = s->ssl_pemfile;
281 cv[30].destination = &(s->ssl_enabled);
282 cv[31].destination = &(s->log_file_not_found);
283 cv[32].destination = &(s->log_request_handling);
284 cv[33].destination = &(s->log_response_header);
285 cv[34].destination = &(s->log_request_header);
286 cv[35].destination = &(s->log_ssl_noise);
287 cv[36].destination = &(s->allow_http11);
288 cv[39].destination = s->ssl_ca_file;
290 cv[41].destination = &(s->range_requests);
291 cv[47].destination = s->ssl_cipher_list;
292 cv[48].destination = &(s->ssl_use_sslv2);
293 cv[49].destination = &(s->etag_use_inode);
295 cv[50].destination = &(s->etag_use_mtime);
296 cv[51].destination = &(s->etag_use_size);
297 cv[53].destination = &(s->log_timeouts);
298 cv[54].destination = &(s->defer_accept);
299 cv[56].destination = &(s->ssl_verifyclient);
300 cv[57].destination = &(s->ssl_verifyclient_enforce);
301 cv[58].destination = &(s->ssl_verifyclient_depth);
302 cv[59].destination = s->ssl_verifyclient_username;
304 cv[60].destination = &(s->ssl_verifyclient_export_cert);
305 cv[61].destination = &(s->set_v6only);
306 cv[62].destination = &(s->ssl_use_sslv3);
307 cv[63].destination = s->ssl_dh_file;
308 cv[64].destination = s->ssl_ec_curve;
309 cv[65].destination = &(s->ssl_disable_client_renegotiation);
310 cv[66].destination = &(s->ssl_honor_cipher_order);
311 cv[67].destination = &(s->ssl_empty_fragments);
312 cv[70].destination = &(s->listen_backlog);
313 cv[71].destination = s->error_handler_404;
314 #if defined(__FreeBSD__) || defined(__NetBSD__) \
315 || defined(__OpenBSD__) || defined(__DragonflyBSD__)
316 cv[75].destination = s->bsd_accept_filter;
317 #endif
318 cv[76].destination = &(s->stream_request_body);
319 cv[77].destination = &(s->stream_response_body);
321 srv->config_storage[i] = s;
323 if (0 != (ret = config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION))) {
324 break;
327 if (s->stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN) {
328 s->stream_request_body |= FDEVENT_STREAM_REQUEST;
330 if (s->stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) {
331 s->stream_response_body |= FDEVENT_STREAM_RESPONSE;
336 specific_config *s = srv->config_storage[0];
337 s->http_parseopts= /*(global, but stored in con->conf.http_parseopts)*/
338 (srv->srvconf.http_header_strict ?(HTTP_PARSEOPT_HEADER_STRICT) :0)
339 |(srv->srvconf.http_host_strict ?(HTTP_PARSEOPT_HOST_STRICT
340 |HTTP_PARSEOPT_HOST_NORMALIZE):0)
341 |(srv->srvconf.http_host_normalize ?(HTTP_PARSEOPT_HOST_NORMALIZE):0);
344 if (buffer_string_is_empty(stat_cache_string)) {
345 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
346 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("simple"))) {
347 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
348 #ifdef HAVE_FAM_H
349 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("fam"))) {
350 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_FAM;
351 #endif
352 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("disable"))) {
353 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_NONE;
354 } else {
355 log_error_write(srv, __FILE__, __LINE__, "sb",
356 "server.stat-cache-engine can be one of \"disable\", \"simple\","
357 #ifdef HAVE_FAM_H
358 " \"fam\","
359 #endif
360 " but not:", stat_cache_string);
361 ret = HANDLER_ERROR;
364 buffer_free(stat_cache_string);
367 data_string *ds;
368 int prepend_mod_indexfile = 1;
369 int append_mod_dirlisting = 1;
370 int append_mod_staticfile = 1;
372 /* prepend default modules */
373 for (i = 0; i < srv->srvconf.modules->used; i++) {
374 ds = (data_string *)srv->srvconf.modules->data[i];
376 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_indexfile"))) {
377 prepend_mod_indexfile = 0;
380 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_staticfile"))) {
381 append_mod_staticfile = 0;
384 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_dirlisting"))) {
385 append_mod_dirlisting = 0;
388 if (0 == prepend_mod_indexfile &&
389 0 == append_mod_dirlisting &&
390 0 == append_mod_staticfile) {
391 break;
395 if (prepend_mod_indexfile) {
396 /* mod_indexfile has to be loaded before mod_fastcgi and friends */
397 array *modules = array_init();
399 ds = data_string_init();
400 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_indexfile"));
401 array_insert_unique(modules, (data_unset *)ds);
403 for (i = 0; i < srv->srvconf.modules->used; i++) {
404 data_unset *du = srv->srvconf.modules->data[i];
405 array_insert_unique(modules, du->copy(du));
408 array_free(srv->srvconf.modules);
409 srv->srvconf.modules = modules;
412 /* append default modules */
413 if (append_mod_dirlisting) {
414 ds = data_string_init();
415 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_dirlisting"));
416 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
419 if (append_mod_staticfile) {
420 ds = data_string_init();
421 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_staticfile"));
422 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
426 return ret;
431 #define PATCH(x) con->conf.x = s->x
432 int config_setup_connection(server *srv, connection *con) {
433 specific_config *s = srv->config_storage[0];
435 PATCH(http_parseopts);
437 PATCH(allow_http11);
438 PATCH(mimetypes);
439 PATCH(document_root);
440 PATCH(max_keep_alive_requests);
441 PATCH(max_keep_alive_idle);
442 PATCH(max_read_idle);
443 PATCH(max_write_idle);
444 PATCH(use_xattr);
445 PATCH(error_handler);
446 PATCH(error_handler_404);
447 PATCH(errorfile_prefix);
448 #ifdef HAVE_LSTAT
449 PATCH(follow_symlink);
450 #endif
451 PATCH(server_tag);
452 PATCH(kbytes_per_second);
453 PATCH(global_kbytes_per_second);
454 PATCH(global_bytes_per_second_cnt);
456 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
457 buffer_copy_buffer(con->server_name, s->server_name);
459 PATCH(log_request_header);
460 PATCH(log_response_header);
461 PATCH(log_request_handling);
462 PATCH(log_condition_handling);
463 PATCH(log_file_not_found);
464 PATCH(log_ssl_noise);
465 PATCH(log_timeouts);
467 PATCH(range_requests);
468 PATCH(force_lowercase_filenames);
469 /*PATCH(listen_backlog);*//*(not necessary; used only at startup)*/
470 PATCH(stream_request_body);
471 PATCH(stream_response_body);
473 PATCH(ssl_enabled);
475 PATCH(ssl_pemfile);
476 #ifdef USE_OPENSSL
477 PATCH(ssl_pemfile_x509);
478 PATCH(ssl_pemfile_pkey);
479 #endif
480 PATCH(ssl_ca_file);
481 #ifdef USE_OPENSSL
482 PATCH(ssl_ca_file_cert_names);
483 #endif
484 PATCH(ssl_cipher_list);
485 PATCH(ssl_dh_file);
486 PATCH(ssl_ec_curve);
487 PATCH(ssl_honor_cipher_order);
488 PATCH(ssl_empty_fragments);
489 PATCH(ssl_use_sslv2);
490 PATCH(ssl_use_sslv3);
491 PATCH(etag_use_inode);
492 PATCH(etag_use_mtime);
493 PATCH(etag_use_size);
495 PATCH(ssl_verifyclient);
496 PATCH(ssl_verifyclient_enforce);
497 PATCH(ssl_verifyclient_depth);
498 PATCH(ssl_verifyclient_username);
499 PATCH(ssl_verifyclient_export_cert);
500 PATCH(ssl_disable_client_renegotiation);
502 return 0;
505 int config_patch_connection(server *srv, connection *con) {
506 size_t i, j;
508 /* skip the first, the global context */
509 for (i = 1; i < srv->config_context->used; i++) {
510 data_config *dc = (data_config *)srv->config_context->data[i];
511 specific_config *s = srv->config_storage[i];
513 /* condition didn't match */
514 if (!config_check_cond(srv, con, dc)) continue;
516 /* merge config */
517 for (j = 0; j < dc->value->used; j++) {
518 data_unset *du = dc->value->data[j];
520 if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.document-root"))) {
521 PATCH(document_root);
522 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.range-requests"))) {
523 PATCH(range_requests);
524 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler"))) {
525 PATCH(error_handler);
526 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler-404"))) {
527 PATCH(error_handler_404);
528 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.errorfile-prefix"))) {
529 PATCH(errorfile_prefix);
530 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.assign"))) {
531 PATCH(mimetypes);
532 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-requests"))) {
533 PATCH(max_keep_alive_requests);
534 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-idle"))) {
535 PATCH(max_keep_alive_idle);
536 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-write-idle"))) {
537 PATCH(max_write_idle);
538 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-read-idle"))) {
539 PATCH(max_read_idle);
540 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.use-xattr"))) {
541 PATCH(use_xattr);
542 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-inode"))) {
543 PATCH(etag_use_inode);
544 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-mtime"))) {
545 PATCH(etag_use_mtime);
546 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-size"))) {
547 PATCH(etag_use_size);
548 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.pemfile"))) {
549 PATCH(ssl_pemfile);
550 #ifdef USE_OPENSSL
551 PATCH(ssl_pemfile_x509);
552 PATCH(ssl_pemfile_pkey);
553 #endif
554 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ca-file"))) {
555 PATCH(ssl_ca_file);
556 #ifdef USE_OPENSSL
557 PATCH(ssl_ca_file_cert_names);
558 #endif
559 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.honor-cipher-order"))) {
560 PATCH(ssl_honor_cipher_order);
561 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.empty-fragments"))) {
562 PATCH(ssl_empty_fragments);
563 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv2"))) {
564 PATCH(ssl_use_sslv2);
565 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv3"))) {
566 PATCH(ssl_use_sslv3);
567 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.cipher-list"))) {
568 PATCH(ssl_cipher_list);
569 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.engine"))) {
570 PATCH(ssl_enabled);
571 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.dh-file"))) {
572 PATCH(ssl_dh_file);
573 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ec-curve"))) {
574 PATCH(ssl_ec_curve);
575 #ifdef HAVE_LSTAT
576 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.follow-symlink"))) {
577 PATCH(follow_symlink);
578 #endif
579 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.name"))) {
580 buffer_copy_buffer(con->server_name, s->server_name);
581 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.tag"))) {
582 PATCH(server_tag);
583 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-request-body"))) {
584 PATCH(stream_request_body);
585 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-response-body"))) {
586 PATCH(stream_response_body);
587 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("connection.kbytes-per-second"))) {
588 PATCH(kbytes_per_second);
589 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-handling"))) {
590 PATCH(log_request_handling);
591 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-header"))) {
592 PATCH(log_request_header);
593 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-response-header"))) {
594 PATCH(log_response_header);
595 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-condition-handling"))) {
596 PATCH(log_condition_handling);
597 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-file-not-found"))) {
598 PATCH(log_file_not_found);
599 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-ssl-noise"))) {
600 PATCH(log_ssl_noise);
601 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-timeouts"))) {
602 PATCH(log_timeouts);
603 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.protocol-http11"))) {
604 PATCH(allow_http11);
605 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.force-lowercase-filenames"))) {
606 PATCH(force_lowercase_filenames);
607 #if 0 /*(not necessary; used only at startup)*/
608 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.listen-backlog"))) {
609 PATCH(listen_backlog);
610 #endif
611 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.kbytes-per-second"))) {
612 PATCH(global_kbytes_per_second);
613 PATCH(global_bytes_per_second_cnt);
614 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
615 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.activate"))) {
616 PATCH(ssl_verifyclient);
617 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.enforce"))) {
618 PATCH(ssl_verifyclient_enforce);
619 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.depth"))) {
620 PATCH(ssl_verifyclient_depth);
621 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.username"))) {
622 PATCH(ssl_verifyclient_username);
623 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.exportcert"))) {
624 PATCH(ssl_verifyclient_export_cert);
625 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.disable-client-renegotiation"))) {
626 PATCH(ssl_disable_client_renegotiation);
631 con->etag_flags = (con->conf.etag_use_mtime ? ETAG_USE_MTIME : 0) |
632 (con->conf.etag_use_inode ? ETAG_USE_INODE : 0) |
633 (con->conf.etag_use_size ? ETAG_USE_SIZE : 0);
635 return 0;
637 #undef PATCH
639 typedef struct {
640 int foo;
641 int bar;
643 const buffer *source;
644 const char *input;
645 size_t offset;
646 size_t size;
648 int line_pos;
649 int line;
651 int in_key;
652 int in_brace;
653 int in_cond;
654 } tokenizer_t;
656 #if 0
657 static int tokenizer_open(server *srv, tokenizer_t *t, buffer *basedir, const char *fn) {
658 if (buffer_string_is_empty(basedir) ||
659 (fn[0] == '/' || fn[0] == '\\') ||
660 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\'))) {
661 t->file = buffer_init_string(fn);
662 } else {
663 t->file = buffer_init_buffer(basedir);
664 buffer_append_string(t->file, fn);
667 if (0 != stream_open(&(t->s), t->file)) {
668 log_error_write(srv, __FILE__, __LINE__, "sbss",
669 "opening configfile ", t->file, "failed:", strerror(errno));
670 buffer_free(t->file);
671 return -1;
674 t->input = t->s.start;
675 t->offset = 0;
676 t->size = t->s.size;
677 t->line = 1;
678 t->line_pos = 1;
680 t->in_key = 1;
681 t->in_brace = 0;
682 t->in_cond = 0;
683 return 0;
686 static int tokenizer_close(server *srv, tokenizer_t *t) {
687 UNUSED(srv);
689 buffer_free(t->file);
690 return stream_close(&(t->s));
692 #endif
693 static int config_skip_newline(tokenizer_t *t) {
694 int skipped = 1;
695 force_assert(t->input[t->offset] == '\r' || t->input[t->offset] == '\n');
696 if (t->input[t->offset] == '\r' && t->input[t->offset + 1] == '\n') {
697 skipped ++;
698 t->offset ++;
700 t->offset ++;
701 return skipped;
704 static int config_skip_comment(tokenizer_t *t) {
705 int i;
706 force_assert(t->input[t->offset] == '#');
707 for (i = 1; t->input[t->offset + i] &&
708 (t->input[t->offset + i] != '\n' && t->input[t->offset + i] != '\r');
709 i++);
710 t->offset += i;
711 return i;
714 static int config_tokenizer(server *srv, tokenizer_t *t, int *token_id, buffer *token) {
715 int tid = 0;
716 size_t i;
718 for (tid = 0; tid == 0 && t->offset < t->size && t->input[t->offset] ; ) {
719 char c = t->input[t->offset];
720 const char *start = NULL;
722 switch (c) {
723 case '=':
724 if (t->in_brace) {
725 if (t->input[t->offset + 1] == '>') {
726 t->offset += 2;
728 buffer_copy_string_len(token, CONST_STR_LEN("=>"));
730 tid = TK_ARRAY_ASSIGN;
731 } else {
732 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
733 "source:", t->source,
734 "line:", t->line, "pos:", t->line_pos,
735 "use => for assignments in arrays");
736 return -1;
738 } else if (t->in_cond) {
739 if (t->input[t->offset + 1] == '=') {
740 t->offset += 2;
742 buffer_copy_string_len(token, CONST_STR_LEN("=="));
744 tid = TK_EQ;
745 } else if (t->input[t->offset + 1] == '~') {
746 t->offset += 2;
748 buffer_copy_string_len(token, CONST_STR_LEN("=~"));
750 tid = TK_MATCH;
751 } else {
752 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
753 "source:", t->source,
754 "line:", t->line, "pos:", t->line_pos,
755 "only =~ and == are allowed in the condition");
756 return -1;
758 t->in_key = 1;
759 t->in_cond = 0;
760 } else if (t->in_key) {
761 tid = TK_ASSIGN;
763 buffer_copy_string_len(token, t->input + t->offset, 1);
765 t->offset++;
766 t->line_pos++;
767 } else {
768 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
769 "source:", t->source,
770 "line:", t->line, "pos:", t->line_pos,
771 "unexpected equal-sign: =");
772 return -1;
775 break;
776 case '!':
777 if (t->in_cond) {
778 if (t->input[t->offset + 1] == '=') {
779 t->offset += 2;
781 buffer_copy_string_len(token, CONST_STR_LEN("!="));
783 tid = TK_NE;
784 } else if (t->input[t->offset + 1] == '~') {
785 t->offset += 2;
787 buffer_copy_string_len(token, CONST_STR_LEN("!~"));
789 tid = TK_NOMATCH;
790 } else {
791 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
792 "source:", t->source,
793 "line:", t->line, "pos:", t->line_pos,
794 "only !~ and != are allowed in the condition");
795 return -1;
797 t->in_key = 1;
798 t->in_cond = 0;
799 } else {
800 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
801 "source:", t->source,
802 "line:", t->line, "pos:", t->line_pos,
803 "unexpected exclamation-marks: !");
804 return -1;
807 break;
808 case '\t':
809 case ' ':
810 t->offset++;
811 t->line_pos++;
812 break;
813 case '\n':
814 case '\r':
815 if (t->in_brace == 0) {
816 int done = 0;
817 while (!done && t->offset < t->size) {
818 switch (t->input[t->offset]) {
819 case '\r':
820 case '\n':
821 config_skip_newline(t);
822 t->line_pos = 1;
823 t->line++;
824 break;
826 case '#':
827 t->line_pos += config_skip_comment(t);
828 break;
830 case '\t':
831 case ' ':
832 t->offset++;
833 t->line_pos++;
834 break;
836 default:
837 done = 1;
840 t->in_key = 1;
841 tid = TK_EOL;
842 buffer_copy_string_len(token, CONST_STR_LEN("(EOL)"));
843 } else {
844 config_skip_newline(t);
845 t->line_pos = 1;
846 t->line++;
848 break;
849 case ',':
850 if (t->in_brace > 0) {
851 tid = TK_COMMA;
853 buffer_copy_string_len(token, CONST_STR_LEN("(COMMA)"));
856 t->offset++;
857 t->line_pos++;
858 break;
859 case '"':
860 /* search for the terminating " */
861 start = t->input + t->offset + 1;
862 buffer_copy_string_len(token, CONST_STR_LEN(""));
864 for (i = 1; t->input[t->offset + i]; i++) {
865 if (t->input[t->offset + i] == '\\' &&
866 t->input[t->offset + i + 1] == '"') {
868 buffer_append_string_len(token, start, t->input + t->offset + i - start);
870 start = t->input + t->offset + i + 1;
872 /* skip the " */
873 i++;
874 continue;
878 if (t->input[t->offset + i] == '"') {
879 tid = TK_STRING;
881 buffer_append_string_len(token, start, t->input + t->offset + i - start);
883 break;
887 if (t->input[t->offset + i] == '\0') {
888 /* ERROR */
890 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
891 "source:", t->source,
892 "line:", t->line, "pos:", t->line_pos,
893 "missing closing quote");
895 return -1;
898 t->offset += i + 1;
899 t->line_pos += i + 1;
901 break;
902 case '(':
903 t->offset++;
904 t->in_brace++;
906 tid = TK_LPARAN;
908 buffer_copy_string_len(token, CONST_STR_LEN("("));
909 break;
910 case ')':
911 t->offset++;
912 t->in_brace--;
914 tid = TK_RPARAN;
916 buffer_copy_string_len(token, CONST_STR_LEN(")"));
917 break;
918 case '$':
919 t->offset++;
921 tid = TK_DOLLAR;
922 t->in_cond = 1;
923 t->in_key = 0;
925 buffer_copy_string_len(token, CONST_STR_LEN("$"));
927 break;
929 case '+':
930 if (t->input[t->offset + 1] == '=') {
931 t->offset += 2;
932 buffer_copy_string_len(token, CONST_STR_LEN("+="));
933 tid = TK_APPEND;
934 } else {
935 t->offset++;
936 tid = TK_PLUS;
937 buffer_copy_string_len(token, CONST_STR_LEN("+"));
939 break;
941 case '{':
942 t->offset++;
944 tid = TK_LCURLY;
946 buffer_copy_string_len(token, CONST_STR_LEN("{"));
948 break;
950 case '}':
951 t->offset++;
953 tid = TK_RCURLY;
955 buffer_copy_string_len(token, CONST_STR_LEN("}"));
957 break;
959 case '[':
960 t->offset++;
962 tid = TK_LBRACKET;
964 buffer_copy_string_len(token, CONST_STR_LEN("["));
966 break;
968 case ']':
969 t->offset++;
971 tid = TK_RBRACKET;
973 buffer_copy_string_len(token, CONST_STR_LEN("]"));
975 break;
976 case '#':
977 t->line_pos += config_skip_comment(t);
979 break;
980 default:
981 if (t->in_cond) {
982 for (i = 0; t->input[t->offset + i] &&
983 (isalpha((unsigned char)t->input[t->offset + i])
984 ); i++);
986 if (i && t->input[t->offset + i]) {
987 tid = TK_SRVVARNAME;
988 buffer_copy_string_len(token, t->input + t->offset, i);
990 t->offset += i;
991 t->line_pos += i;
992 } else {
993 /* ERROR */
994 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
995 "source:", t->source,
996 "line:", t->line, "pos:", t->line_pos,
997 "invalid character in condition");
998 return -1;
1000 } else if (isdigit((unsigned char)c)) {
1001 /* take all digits */
1002 for (i = 0; t->input[t->offset + i] && isdigit((unsigned char)t->input[t->offset + i]); i++);
1004 /* was there it least a digit ? */
1005 if (i) {
1006 tid = TK_INTEGER;
1008 buffer_copy_string_len(token, t->input + t->offset, i);
1010 t->offset += i;
1011 t->line_pos += i;
1013 } else {
1014 /* the key might consist of [-.0-9a-z] */
1015 for (i = 0; t->input[t->offset + i] &&
1016 (isalnum((unsigned char)t->input[t->offset + i]) ||
1017 t->input[t->offset + i] == '.' ||
1018 t->input[t->offset + i] == '_' || /* for env.* */
1019 t->input[t->offset + i] == '-'
1020 ); i++);
1022 if (i && t->input[t->offset + i]) {
1023 buffer_copy_string_len(token, t->input + t->offset, i);
1025 if (strcmp(token->ptr, "include") == 0) {
1026 tid = TK_INCLUDE;
1027 } else if (strcmp(token->ptr, "include_shell") == 0) {
1028 tid = TK_INCLUDE_SHELL;
1029 } else if (strcmp(token->ptr, "global") == 0) {
1030 tid = TK_GLOBAL;
1031 } else if (strcmp(token->ptr, "else") == 0) {
1032 tid = TK_ELSE;
1033 } else {
1034 tid = TK_LKEY;
1037 t->offset += i;
1038 t->line_pos += i;
1039 } else {
1040 /* ERROR */
1041 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
1042 "source:", t->source,
1043 "line:", t->line, "pos:", t->line_pos,
1044 "invalid character in variable name");
1045 return -1;
1048 break;
1052 if (tid) {
1053 *token_id = tid;
1054 #if 0
1055 log_error_write(srv, __FILE__, __LINE__, "sbsdsdbdd",
1056 "source:", t->source,
1057 "line:", t->line, "pos:", t->line_pos,
1058 token, token->used - 1, tid);
1059 #endif
1061 return 1;
1062 } else if (t->offset < t->size) {
1063 fprintf(stderr, "%s.%d: %d, %s\n",
1064 __FILE__, __LINE__,
1065 tid, token->ptr);
1067 return 0;
1070 static int config_parse(server *srv, config_t *context, tokenizer_t *t) {
1071 void *pParser;
1072 int token_id;
1073 buffer *token, *lasttoken;
1074 int ret;
1076 pParser = configparserAlloc( malloc );
1077 force_assert(pParser);
1078 lasttoken = buffer_init();
1079 token = buffer_init();
1080 while((1 == (ret = config_tokenizer(srv, t, &token_id, token))) && context->ok) {
1081 buffer_copy_buffer(lasttoken, token);
1082 configparser(pParser, token_id, token, context);
1084 token = buffer_init();
1086 buffer_free(token);
1088 if (ret != -1 && context->ok) {
1089 /* add an EOL at EOF, better than say sorry */
1090 configparser(pParser, TK_EOL, buffer_init_string("(EOL)"), context);
1091 if (context->ok) {
1092 configparser(pParser, 0, NULL, context);
1095 configparserFree(pParser, free);
1097 if (ret == -1) {
1098 log_error_write(srv, __FILE__, __LINE__, "sb",
1099 "configfile parser failed at:", lasttoken);
1100 } else if (context->ok == 0) {
1101 log_error_write(srv, __FILE__, __LINE__, "sbsdsdsb",
1102 "source:", t->source,
1103 "line:", t->line, "pos:", t->line_pos,
1104 "parser failed somehow near here:", lasttoken);
1105 ret = -1;
1107 buffer_free(lasttoken);
1109 return ret == -1 ? -1 : 0;
1112 static int tokenizer_init(tokenizer_t *t, const buffer *source, const char *input, size_t size) {
1114 t->source = source;
1115 t->input = input;
1116 t->size = size;
1117 t->offset = 0;
1118 t->line = 1;
1119 t->line_pos = 1;
1121 t->in_key = 1;
1122 t->in_brace = 0;
1123 t->in_cond = 0;
1124 return 0;
1127 static int config_parse_file_stream(server *srv, config_t *context, const buffer *filename) {
1128 tokenizer_t t;
1129 stream s;
1130 int ret;
1132 if (0 != stream_open(&s, filename)) {
1133 log_error_write(srv, __FILE__, __LINE__, "sbss",
1134 "opening configfile ", filename, "failed:", strerror(errno));
1135 return -1;
1136 } else {
1137 tokenizer_init(&t, filename, s.start, s.size);
1138 ret = config_parse(srv, context, &t);
1141 stream_close(&s);
1142 return ret;
1145 int config_parse_file(server *srv, config_t *context, const char *fn) {
1146 buffer *filename;
1147 size_t i;
1148 int ret = -1;
1149 #ifdef GLOB_BRACE
1150 int flags = GLOB_BRACE;
1151 #else
1152 int flags = 0;
1153 #endif
1154 glob_t gl;
1156 if ((fn[0] == '/' || fn[0] == '\\') ||
1157 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\')) ||
1158 (fn[0] == '.' && fn[1] == '.' && (fn[2] == '/' || fn[2] == '\\'))) {
1159 filename = buffer_init_string(fn);
1160 } else {
1161 filename = buffer_init_buffer(context->basedir);
1162 buffer_append_string(filename, fn);
1165 switch (glob(filename->ptr, flags, NULL, &gl)) {
1166 case 0:
1167 for (i = 0; i < gl.gl_pathc; ++i) {
1168 buffer_copy_string(filename, gl.gl_pathv[i]);
1169 ret = config_parse_file_stream(srv, context, filename);
1170 if (0 != ret) break;
1172 globfree(&gl);
1173 break;
1174 case GLOB_NOMATCH:
1175 if (filename->ptr[strcspn(filename->ptr, "*?[]{}")] != '\0') { /*(contains glob metachars)*/
1176 ret = 0; /* not an error if no files match glob pattern */
1178 else {
1179 log_error_write(srv, __FILE__, __LINE__, "sb", "include file not found: ", filename);
1181 break;
1182 case GLOB_ABORTED:
1183 case GLOB_NOSPACE:
1184 log_error_write(srv, __FILE__, __LINE__, "sbss", "glob()", filename, "failed:", strerror(errno));
1185 break;
1188 buffer_free(filename);
1189 return ret;
1192 static char* getCWD(void) {
1193 char *s, *s1;
1194 size_t len;
1195 #ifdef PATH_MAX
1196 len = PATH_MAX;
1197 #else
1198 len = 4096;
1199 #endif
1201 s = malloc(len);
1202 if (!s) return NULL;
1203 while (NULL == getcwd(s, len)) {
1204 if (errno != ERANGE || SSIZE_MAX - len < len) {
1205 free(s);
1206 return NULL;
1208 len *= 2;
1209 s1 = realloc(s, len);
1210 if (!s1) {
1211 free(s);
1212 return NULL;
1214 s = s1;
1216 return s;
1219 int config_parse_cmd(server *srv, config_t *context, const char *cmd) {
1220 tokenizer_t t;
1221 int ret;
1222 buffer *source;
1223 buffer *out;
1224 char *oldpwd;
1226 if (NULL == (oldpwd = getCWD())) {
1227 log_error_write(srv, __FILE__, __LINE__, "s",
1228 "cannot get cwd", strerror(errno));
1229 return -1;
1232 if (!buffer_string_is_empty(context->basedir)) {
1233 if (0 != chdir(context->basedir->ptr)) {
1234 log_error_write(srv, __FILE__, __LINE__, "sbs",
1235 "cannot change directory to", context->basedir, strerror(errno));
1236 free(oldpwd);
1237 return -1;
1241 source = buffer_init_string(cmd);
1242 out = buffer_init();
1244 if (0 != proc_open_buffer(cmd, NULL, out, NULL)) {
1245 log_error_write(srv, __FILE__, __LINE__, "sbss",
1246 "opening", source, "failed:", strerror(errno));
1247 ret = -1;
1248 } else {
1249 tokenizer_init(&t, source, CONST_BUF_LEN(out));
1250 ret = config_parse(srv, context, &t);
1253 buffer_free(source);
1254 buffer_free(out);
1255 if (0 != chdir(oldpwd)) {
1256 log_error_write(srv, __FILE__, __LINE__, "sss",
1257 "cannot change directory to", oldpwd, strerror(errno));
1258 free(oldpwd);
1259 return -1;
1261 free(oldpwd);
1262 return ret;
1265 static void context_init(server *srv, config_t *context) {
1266 context->srv = srv;
1267 context->ok = 1;
1268 vector_config_weak_init(&context->configs_stack);
1269 context->basedir = buffer_init();
1272 static void context_free(config_t *context) {
1273 vector_config_weak_clear(&context->configs_stack);
1274 buffer_free(context->basedir);
1277 int config_read(server *srv, const char *fn) {
1278 config_t context;
1279 data_config *dc;
1280 data_integer *dpid;
1281 data_string *dcwd;
1282 int ret;
1283 char *pos;
1284 buffer *filename;
1286 context_init(srv, &context);
1287 context.all_configs = srv->config_context;
1289 #ifdef __WIN32
1290 pos = strrchr(fn, '\\');
1291 #else
1292 pos = strrchr(fn, '/');
1293 #endif
1294 if (pos) {
1295 buffer_copy_string_len(context.basedir, fn, pos - fn + 1);
1298 dc = data_config_init();
1299 buffer_copy_string_len(dc->key, CONST_STR_LEN("global"));
1301 force_assert(context.all_configs->used == 0);
1302 dc->context_ndx = context.all_configs->used;
1303 array_insert_unique(context.all_configs, (data_unset *)dc);
1304 context.current = dc;
1306 /* default context */
1307 dpid = data_integer_init();
1308 dpid->value = getpid();
1309 buffer_copy_string_len(dpid->key, CONST_STR_LEN("var.PID"));
1310 array_insert_unique(dc->value, (data_unset *)dpid);
1312 dcwd = data_string_init();
1313 buffer_string_prepare_copy(dcwd->value, 1023);
1314 if (NULL != getcwd(dcwd->value->ptr, dcwd->value->size - 1)) {
1315 buffer_commit(dcwd->value, strlen(dcwd->value->ptr));
1316 buffer_copy_string_len(dcwd->key, CONST_STR_LEN("var.CWD"));
1317 array_insert_unique(dc->value, (data_unset *)dcwd);
1318 } else {
1319 dcwd->free((data_unset*) dcwd);
1322 filename = buffer_init_string(fn);
1323 ret = config_parse_file_stream(srv, &context, filename);
1324 buffer_free(filename);
1326 /* remains nothing if parser is ok */
1327 force_assert(!(0 == ret && context.ok && 0 != context.configs_stack.used));
1328 context_free(&context);
1330 if (0 != ret) {
1331 return ret;
1334 if (0 != config_insert(srv)) {
1335 return -1;
1338 return 0;
1341 int config_set_defaults(server *srv) {
1342 size_t i;
1343 specific_config *s = srv->config_storage[0];
1344 struct stat st1, st2;
1346 struct ev_map { fdevent_handler_t et; const char *name; } event_handlers[] =
1348 /* - epoll is most reliable
1349 * - select works everywhere
1351 #ifdef USE_LINUX_EPOLL
1352 { FDEVENT_HANDLER_LINUX_SYSEPOLL, "linux-sysepoll" },
1353 #endif
1354 #ifdef USE_POLL
1355 { FDEVENT_HANDLER_POLL, "poll" },
1356 #endif
1357 #ifdef USE_SELECT
1358 { FDEVENT_HANDLER_SELECT, "select" },
1359 #endif
1360 #ifdef USE_LIBEV
1361 { FDEVENT_HANDLER_LIBEV, "libev" },
1362 #endif
1363 #ifdef USE_SOLARIS_DEVPOLL
1364 { FDEVENT_HANDLER_SOLARIS_DEVPOLL,"solaris-devpoll" },
1365 #endif
1366 #ifdef USE_SOLARIS_PORT
1367 { FDEVENT_HANDLER_SOLARIS_PORT, "solaris-eventports" },
1368 #endif
1369 #ifdef USE_FREEBSD_KQUEUE
1370 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "freebsd-kqueue" },
1371 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "kqueue" },
1372 #endif
1373 { FDEVENT_HANDLER_UNSET, NULL }
1376 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1377 if (-1 == stat(srv->srvconf.changeroot->ptr, &st1)) {
1378 log_error_write(srv, __FILE__, __LINE__, "sb",
1379 "server.chroot doesn't exist:", srv->srvconf.changeroot);
1380 return -1;
1382 if (!S_ISDIR(st1.st_mode)) {
1383 log_error_write(srv, __FILE__, __LINE__, "sb",
1384 "server.chroot isn't a directory:", srv->srvconf.changeroot);
1385 return -1;
1389 if (srv->srvconf.upload_tempdirs->used) {
1390 buffer * const b = srv->tmp_buf;
1391 size_t len;
1392 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1393 buffer_copy_buffer(b, srv->srvconf.changeroot);
1394 buffer_append_slash(b);
1395 } else {
1396 buffer_reset(b);
1398 len = buffer_string_length(b);
1400 for (i = 0; i < srv->srvconf.upload_tempdirs->used; ++i) {
1401 const data_string * const ds = (data_string *)srv->srvconf.upload_tempdirs->data[i];
1402 buffer_string_set_length(b, len); /*(truncate)*/
1403 buffer_append_string_buffer(b, ds->value);
1404 if (-1 == stat(b->ptr, &st1)) {
1405 log_error_write(srv, __FILE__, __LINE__, "sb",
1406 "server.upload-dirs doesn't exist:", b);
1407 } else if (!S_ISDIR(st1.st_mode)) {
1408 log_error_write(srv, __FILE__, __LINE__, "sb",
1409 "server.upload-dirs isn't a directory:", b);
1414 if (buffer_string_is_empty(s->document_root)) {
1415 log_error_write(srv, __FILE__, __LINE__, "s",
1416 "a default document-root has to be set");
1418 return -1;
1421 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1423 buffer_to_lower(srv->tmp_buf);
1425 if (2 == s->force_lowercase_filenames) { /* user didn't configure it in global section? */
1426 s->force_lowercase_filenames = 0; /* default to 0 */
1428 if (0 == stat(srv->tmp_buf->ptr, &st1)) {
1429 int is_lower = 0;
1431 is_lower = buffer_is_equal(srv->tmp_buf, s->document_root);
1433 /* lower-case existed, check upper-case */
1434 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1436 buffer_to_upper(srv->tmp_buf);
1438 /* we have to handle the special case that upper and lower-casing results in the same filename
1439 * as in server.document-root = "/" or "/12345/" */
1441 if (is_lower && buffer_is_equal(srv->tmp_buf, s->document_root)) {
1442 /* lower-casing and upper-casing didn't result in
1443 * an other filename, no need to stat(),
1444 * just assume it is case-sensitive. */
1446 s->force_lowercase_filenames = 0;
1447 } else if (0 == stat(srv->tmp_buf->ptr, &st2)) {
1449 /* upper case exists too, doesn't the FS handle this ? */
1451 /* upper and lower have the same inode -> case-insensitve FS */
1453 if (st1.st_ino == st2.st_ino) {
1454 /* upper and lower have the same inode -> case-insensitve FS */
1456 s->force_lowercase_filenames = 1;
1462 if (srv->srvconf.port == 0) {
1463 srv->srvconf.port = s->ssl_enabled ? 443 : 80;
1466 if (buffer_string_is_empty(srv->srvconf.event_handler)) {
1467 /* choose a good default
1469 * the event_handler list is sorted by 'goodness'
1470 * taking the first available should be the best solution
1472 srv->event_handler = event_handlers[0].et;
1474 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1475 log_error_write(srv, __FILE__, __LINE__, "s",
1476 "sorry, there is no event handler for this system");
1478 return -1;
1480 } else {
1482 * User override
1485 for (i = 0; event_handlers[i].name; i++) {
1486 if (0 == strcmp(event_handlers[i].name, srv->srvconf.event_handler->ptr)) {
1487 srv->event_handler = event_handlers[i].et;
1488 break;
1492 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1493 log_error_write(srv, __FILE__, __LINE__, "sb",
1494 "the selected event-handler in unknown or not supported:",
1495 srv->srvconf.event_handler );
1497 return -1;
1501 if (s->ssl_enabled) {
1502 if (buffer_string_is_empty(s->ssl_pemfile)) {
1503 /* PEM file is require */
1505 log_error_write(srv, __FILE__, __LINE__, "s",
1506 "ssl.pemfile has to be set");
1507 return -1;
1510 #ifndef USE_OPENSSL
1511 log_error_write(srv, __FILE__, __LINE__, "s",
1512 "ssl support is missing, recompile with --with-openssl");
1514 return -1;
1515 #endif
1518 return 0;