[core] make server.max-request-size scopeable (fixes #1901)
[lighttpd.git] / src / configfile.c
blob476c0e930be1ebb1a043ae0488ac219d83ace639
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[13].destination = &(srv->srvconf.max_worker);
170 cv[23].destination = &(srv->srvconf.max_fds);
172 cv[37].destination = &(srv->srvconf.log_request_header_on_error);
173 cv[38].destination = &(srv->srvconf.log_state_handling);
175 cv[40].destination = &(srv->srvconf.errorlog_use_syslog);
176 stat_cache_string = buffer_init();
177 cv[42].destination = stat_cache_string;
178 cv[43].destination = &(srv->srvconf.max_conns);
179 cv[44].destination = srv->srvconf.network_backend;
180 cv[45].destination = srv->srvconf.upload_tempdirs;
181 cv[46].destination = &(srv->srvconf.enable_cores);
183 cv[52].destination = &(srv->srvconf.reject_expect_100_with_417);
184 cv[55].destination = srv->srvconf.breakagelog_file;
186 cv[68].destination = &(srv->srvconf.upload_temp_file_size);
187 cv[69].destination = srv->srvconf.xattr_name;
188 cv[72].destination = &(srv->srvconf.http_header_strict);
189 cv[73].destination = &(srv->srvconf.http_host_strict);
190 cv[74].destination = &(srv->srvconf.http_host_normalize);
192 srv->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
194 force_assert(srv->config_storage);
196 for (i = 0; i < srv->config_context->used; i++) {
197 data_config const* config = (data_config const*)srv->config_context->data[i];
198 specific_config *s;
200 s = calloc(1, sizeof(specific_config));
201 force_assert(s);
202 s->document_root = buffer_init();
203 s->mimetypes = array_init();
204 s->server_name = buffer_init();
205 s->ssl_pemfile = buffer_init();
206 s->ssl_ca_file = buffer_init();
207 s->error_handler = buffer_init();
208 s->error_handler_404 = buffer_init();
209 s->server_tag = buffer_init_string(PACKAGE_DESC);
210 s->ssl_cipher_list = buffer_init();
211 s->ssl_dh_file = buffer_init();
212 s->ssl_ec_curve = buffer_init();
213 s->errorfile_prefix = buffer_init();
214 #if defined(__FreeBSD__) || defined(__NetBSD__) \
215 || defined(__OpenBSD__) || defined(__DragonflyBSD__)
216 s->bsd_accept_filter = (i == 0)
217 ? buffer_init()
218 : buffer_init_buffer(srv->config_storage[0]->bsd_accept_filter);
219 #endif
220 s->max_keep_alive_requests = 16;
221 s->max_keep_alive_idle = 5;
222 s->max_read_idle = 60;
223 s->max_write_idle = 360;
224 s->max_request_size = 0;
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[12].destination = &(s->max_request_size);
263 cv[14].destination = s->document_root;
264 cv[15].destination = &(s->force_lowercase_filenames);
265 cv[16].destination = &(s->log_condition_handling);
266 cv[17].destination = &(s->max_keep_alive_requests);
267 cv[18].destination = s->server_name;
268 cv[19].destination = &(s->max_keep_alive_idle);
270 cv[20].destination = &(s->max_read_idle);
271 cv[21].destination = &(s->max_write_idle);
272 cv[22].destination = s->error_handler;
273 #ifdef HAVE_LSTAT
274 cv[24].destination = &(s->follow_symlink);
275 #endif
276 cv[25].destination = &(s->global_kbytes_per_second);
277 cv[26].destination = &(s->kbytes_per_second);
278 cv[27].destination = &(s->use_xattr);
279 cv[28].destination = s->mimetypes;
280 cv[29].destination = s->ssl_pemfile;
282 cv[30].destination = &(s->ssl_enabled);
283 cv[31].destination = &(s->log_file_not_found);
284 cv[32].destination = &(s->log_request_handling);
285 cv[33].destination = &(s->log_response_header);
286 cv[34].destination = &(s->log_request_header);
287 cv[35].destination = &(s->log_ssl_noise);
288 cv[36].destination = &(s->allow_http11);
289 cv[39].destination = s->ssl_ca_file;
291 cv[41].destination = &(s->range_requests);
292 cv[47].destination = s->ssl_cipher_list;
293 cv[48].destination = &(s->ssl_use_sslv2);
294 cv[49].destination = &(s->etag_use_inode);
296 cv[50].destination = &(s->etag_use_mtime);
297 cv[51].destination = &(s->etag_use_size);
298 cv[53].destination = &(s->log_timeouts);
299 cv[54].destination = &(s->defer_accept);
300 cv[56].destination = &(s->ssl_verifyclient);
301 cv[57].destination = &(s->ssl_verifyclient_enforce);
302 cv[58].destination = &(s->ssl_verifyclient_depth);
303 cv[59].destination = s->ssl_verifyclient_username;
305 cv[60].destination = &(s->ssl_verifyclient_export_cert);
306 cv[61].destination = &(s->set_v6only);
307 cv[62].destination = &(s->ssl_use_sslv3);
308 cv[63].destination = s->ssl_dh_file;
309 cv[64].destination = s->ssl_ec_curve;
310 cv[65].destination = &(s->ssl_disable_client_renegotiation);
311 cv[66].destination = &(s->ssl_honor_cipher_order);
312 cv[67].destination = &(s->ssl_empty_fragments);
313 cv[70].destination = &(s->listen_backlog);
314 cv[71].destination = s->error_handler_404;
315 #if defined(__FreeBSD__) || defined(__NetBSD__) \
316 || defined(__OpenBSD__) || defined(__DragonflyBSD__)
317 cv[75].destination = s->bsd_accept_filter;
318 #endif
319 cv[76].destination = &(s->stream_request_body);
320 cv[77].destination = &(s->stream_response_body);
322 srv->config_storage[i] = s;
324 if (0 != (ret = config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION))) {
325 break;
328 if (s->stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN) {
329 s->stream_request_body |= FDEVENT_STREAM_REQUEST;
331 if (s->stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) {
332 s->stream_response_body |= FDEVENT_STREAM_RESPONSE;
337 specific_config *s = srv->config_storage[0];
338 s->http_parseopts= /*(global, but stored in con->conf.http_parseopts)*/
339 (srv->srvconf.http_header_strict ?(HTTP_PARSEOPT_HEADER_STRICT) :0)
340 |(srv->srvconf.http_host_strict ?(HTTP_PARSEOPT_HOST_STRICT
341 |HTTP_PARSEOPT_HOST_NORMALIZE):0)
342 |(srv->srvconf.http_host_normalize ?(HTTP_PARSEOPT_HOST_NORMALIZE):0);
345 if (buffer_string_is_empty(stat_cache_string)) {
346 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
347 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("simple"))) {
348 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
349 #ifdef HAVE_FAM_H
350 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("fam"))) {
351 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_FAM;
352 #endif
353 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("disable"))) {
354 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_NONE;
355 } else {
356 log_error_write(srv, __FILE__, __LINE__, "sb",
357 "server.stat-cache-engine can be one of \"disable\", \"simple\","
358 #ifdef HAVE_FAM_H
359 " \"fam\","
360 #endif
361 " but not:", stat_cache_string);
362 ret = HANDLER_ERROR;
365 buffer_free(stat_cache_string);
368 data_string *ds;
369 int prepend_mod_indexfile = 1;
370 int append_mod_dirlisting = 1;
371 int append_mod_staticfile = 1;
373 /* prepend default modules */
374 for (i = 0; i < srv->srvconf.modules->used; i++) {
375 ds = (data_string *)srv->srvconf.modules->data[i];
377 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_indexfile"))) {
378 prepend_mod_indexfile = 0;
381 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_staticfile"))) {
382 append_mod_staticfile = 0;
385 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_dirlisting"))) {
386 append_mod_dirlisting = 0;
389 if (0 == prepend_mod_indexfile &&
390 0 == append_mod_dirlisting &&
391 0 == append_mod_staticfile) {
392 break;
396 if (prepend_mod_indexfile) {
397 /* mod_indexfile has to be loaded before mod_fastcgi and friends */
398 array *modules = array_init();
400 ds = data_string_init();
401 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_indexfile"));
402 array_insert_unique(modules, (data_unset *)ds);
404 for (i = 0; i < srv->srvconf.modules->used; i++) {
405 data_unset *du = srv->srvconf.modules->data[i];
406 array_insert_unique(modules, du->copy(du));
409 array_free(srv->srvconf.modules);
410 srv->srvconf.modules = modules;
413 /* append default modules */
414 if (append_mod_dirlisting) {
415 ds = data_string_init();
416 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_dirlisting"));
417 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
420 if (append_mod_staticfile) {
421 ds = data_string_init();
422 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_staticfile"));
423 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
427 return ret;
432 #define PATCH(x) con->conf.x = s->x
433 int config_setup_connection(server *srv, connection *con) {
434 specific_config *s = srv->config_storage[0];
436 PATCH(http_parseopts);
438 PATCH(allow_http11);
439 PATCH(mimetypes);
440 PATCH(document_root);
441 PATCH(max_keep_alive_requests);
442 PATCH(max_keep_alive_idle);
443 PATCH(max_read_idle);
444 PATCH(max_write_idle);
445 PATCH(max_request_size);
446 PATCH(use_xattr);
447 PATCH(error_handler);
448 PATCH(error_handler_404);
449 PATCH(errorfile_prefix);
450 #ifdef HAVE_LSTAT
451 PATCH(follow_symlink);
452 #endif
453 PATCH(server_tag);
454 PATCH(kbytes_per_second);
455 PATCH(global_kbytes_per_second);
456 PATCH(global_bytes_per_second_cnt);
458 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
459 buffer_copy_buffer(con->server_name, s->server_name);
461 PATCH(log_request_header);
462 PATCH(log_response_header);
463 PATCH(log_request_handling);
464 PATCH(log_condition_handling);
465 PATCH(log_file_not_found);
466 PATCH(log_ssl_noise);
467 PATCH(log_timeouts);
469 PATCH(range_requests);
470 PATCH(force_lowercase_filenames);
471 /*PATCH(listen_backlog);*//*(not necessary; used only at startup)*/
472 PATCH(stream_request_body);
473 PATCH(stream_response_body);
475 PATCH(ssl_enabled);
477 PATCH(ssl_pemfile);
478 #ifdef USE_OPENSSL
479 PATCH(ssl_pemfile_x509);
480 PATCH(ssl_pemfile_pkey);
481 #endif
482 PATCH(ssl_ca_file);
483 #ifdef USE_OPENSSL
484 PATCH(ssl_ca_file_cert_names);
485 #endif
486 PATCH(ssl_cipher_list);
487 PATCH(ssl_dh_file);
488 PATCH(ssl_ec_curve);
489 PATCH(ssl_honor_cipher_order);
490 PATCH(ssl_empty_fragments);
491 PATCH(ssl_use_sslv2);
492 PATCH(ssl_use_sslv3);
493 PATCH(etag_use_inode);
494 PATCH(etag_use_mtime);
495 PATCH(etag_use_size);
497 PATCH(ssl_verifyclient);
498 PATCH(ssl_verifyclient_enforce);
499 PATCH(ssl_verifyclient_depth);
500 PATCH(ssl_verifyclient_username);
501 PATCH(ssl_verifyclient_export_cert);
502 PATCH(ssl_disable_client_renegotiation);
504 return 0;
507 int config_patch_connection(server *srv, connection *con) {
508 size_t i, j;
510 /* skip the first, the global context */
511 for (i = 1; i < srv->config_context->used; i++) {
512 data_config *dc = (data_config *)srv->config_context->data[i];
513 specific_config *s = srv->config_storage[i];
515 /* condition didn't match */
516 if (!config_check_cond(srv, con, dc)) continue;
518 /* merge config */
519 for (j = 0; j < dc->value->used; j++) {
520 data_unset *du = dc->value->data[j];
522 if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.document-root"))) {
523 PATCH(document_root);
524 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.range-requests"))) {
525 PATCH(range_requests);
526 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler"))) {
527 PATCH(error_handler);
528 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler-404"))) {
529 PATCH(error_handler_404);
530 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.errorfile-prefix"))) {
531 PATCH(errorfile_prefix);
532 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.assign"))) {
533 PATCH(mimetypes);
534 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-requests"))) {
535 PATCH(max_keep_alive_requests);
536 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-idle"))) {
537 PATCH(max_keep_alive_idle);
538 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-write-idle"))) {
539 PATCH(max_write_idle);
540 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-read-idle"))) {
541 PATCH(max_read_idle);
542 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-request-size"))) {
543 PATCH(max_request_size);
544 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.use-xattr"))) {
545 PATCH(use_xattr);
546 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-inode"))) {
547 PATCH(etag_use_inode);
548 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-mtime"))) {
549 PATCH(etag_use_mtime);
550 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-size"))) {
551 PATCH(etag_use_size);
552 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.pemfile"))) {
553 PATCH(ssl_pemfile);
554 #ifdef USE_OPENSSL
555 PATCH(ssl_pemfile_x509);
556 PATCH(ssl_pemfile_pkey);
557 #endif
558 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ca-file"))) {
559 PATCH(ssl_ca_file);
560 #ifdef USE_OPENSSL
561 PATCH(ssl_ca_file_cert_names);
562 #endif
563 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.honor-cipher-order"))) {
564 PATCH(ssl_honor_cipher_order);
565 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.empty-fragments"))) {
566 PATCH(ssl_empty_fragments);
567 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv2"))) {
568 PATCH(ssl_use_sslv2);
569 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv3"))) {
570 PATCH(ssl_use_sslv3);
571 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.cipher-list"))) {
572 PATCH(ssl_cipher_list);
573 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.engine"))) {
574 PATCH(ssl_enabled);
575 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.dh-file"))) {
576 PATCH(ssl_dh_file);
577 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ec-curve"))) {
578 PATCH(ssl_ec_curve);
579 #ifdef HAVE_LSTAT
580 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.follow-symlink"))) {
581 PATCH(follow_symlink);
582 #endif
583 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.name"))) {
584 buffer_copy_buffer(con->server_name, s->server_name);
585 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.tag"))) {
586 PATCH(server_tag);
587 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-request-body"))) {
588 PATCH(stream_request_body);
589 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-response-body"))) {
590 PATCH(stream_response_body);
591 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("connection.kbytes-per-second"))) {
592 PATCH(kbytes_per_second);
593 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-handling"))) {
594 PATCH(log_request_handling);
595 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-header"))) {
596 PATCH(log_request_header);
597 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-response-header"))) {
598 PATCH(log_response_header);
599 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-condition-handling"))) {
600 PATCH(log_condition_handling);
601 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-file-not-found"))) {
602 PATCH(log_file_not_found);
603 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-ssl-noise"))) {
604 PATCH(log_ssl_noise);
605 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-timeouts"))) {
606 PATCH(log_timeouts);
607 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.protocol-http11"))) {
608 PATCH(allow_http11);
609 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.force-lowercase-filenames"))) {
610 PATCH(force_lowercase_filenames);
611 #if 0 /*(not necessary; used only at startup)*/
612 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.listen-backlog"))) {
613 PATCH(listen_backlog);
614 #endif
615 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.kbytes-per-second"))) {
616 PATCH(global_kbytes_per_second);
617 PATCH(global_bytes_per_second_cnt);
618 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
619 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.activate"))) {
620 PATCH(ssl_verifyclient);
621 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.enforce"))) {
622 PATCH(ssl_verifyclient_enforce);
623 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.depth"))) {
624 PATCH(ssl_verifyclient_depth);
625 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.username"))) {
626 PATCH(ssl_verifyclient_username);
627 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.exportcert"))) {
628 PATCH(ssl_verifyclient_export_cert);
629 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.disable-client-renegotiation"))) {
630 PATCH(ssl_disable_client_renegotiation);
635 con->etag_flags = (con->conf.etag_use_mtime ? ETAG_USE_MTIME : 0) |
636 (con->conf.etag_use_inode ? ETAG_USE_INODE : 0) |
637 (con->conf.etag_use_size ? ETAG_USE_SIZE : 0);
639 return 0;
641 #undef PATCH
643 typedef struct {
644 int foo;
645 int bar;
647 const buffer *source;
648 const char *input;
649 size_t offset;
650 size_t size;
652 int line_pos;
653 int line;
655 int in_key;
656 int in_brace;
657 int in_cond;
658 } tokenizer_t;
660 #if 0
661 static int tokenizer_open(server *srv, tokenizer_t *t, buffer *basedir, const char *fn) {
662 if (buffer_string_is_empty(basedir) ||
663 (fn[0] == '/' || fn[0] == '\\') ||
664 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\'))) {
665 t->file = buffer_init_string(fn);
666 } else {
667 t->file = buffer_init_buffer(basedir);
668 buffer_append_string(t->file, fn);
671 if (0 != stream_open(&(t->s), t->file)) {
672 log_error_write(srv, __FILE__, __LINE__, "sbss",
673 "opening configfile ", t->file, "failed:", strerror(errno));
674 buffer_free(t->file);
675 return -1;
678 t->input = t->s.start;
679 t->offset = 0;
680 t->size = t->s.size;
681 t->line = 1;
682 t->line_pos = 1;
684 t->in_key = 1;
685 t->in_brace = 0;
686 t->in_cond = 0;
687 return 0;
690 static int tokenizer_close(server *srv, tokenizer_t *t) {
691 UNUSED(srv);
693 buffer_free(t->file);
694 return stream_close(&(t->s));
696 #endif
697 static int config_skip_newline(tokenizer_t *t) {
698 int skipped = 1;
699 force_assert(t->input[t->offset] == '\r' || t->input[t->offset] == '\n');
700 if (t->input[t->offset] == '\r' && t->input[t->offset + 1] == '\n') {
701 skipped ++;
702 t->offset ++;
704 t->offset ++;
705 return skipped;
708 static int config_skip_comment(tokenizer_t *t) {
709 int i;
710 force_assert(t->input[t->offset] == '#');
711 for (i = 1; t->input[t->offset + i] &&
712 (t->input[t->offset + i] != '\n' && t->input[t->offset + i] != '\r');
713 i++);
714 t->offset += i;
715 return i;
718 static int config_tokenizer(server *srv, tokenizer_t *t, int *token_id, buffer *token) {
719 int tid = 0;
720 size_t i;
722 for (tid = 0; tid == 0 && t->offset < t->size && t->input[t->offset] ; ) {
723 char c = t->input[t->offset];
724 const char *start = NULL;
726 switch (c) {
727 case '=':
728 if (t->in_brace) {
729 if (t->input[t->offset + 1] == '>') {
730 t->offset += 2;
732 buffer_copy_string_len(token, CONST_STR_LEN("=>"));
734 tid = TK_ARRAY_ASSIGN;
735 } else {
736 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
737 "source:", t->source,
738 "line:", t->line, "pos:", t->line_pos,
739 "use => for assignments in arrays");
740 return -1;
742 } else if (t->in_cond) {
743 if (t->input[t->offset + 1] == '=') {
744 t->offset += 2;
746 buffer_copy_string_len(token, CONST_STR_LEN("=="));
748 tid = TK_EQ;
749 } else if (t->input[t->offset + 1] == '~') {
750 t->offset += 2;
752 buffer_copy_string_len(token, CONST_STR_LEN("=~"));
754 tid = TK_MATCH;
755 } else {
756 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
757 "source:", t->source,
758 "line:", t->line, "pos:", t->line_pos,
759 "only =~ and == are allowed in the condition");
760 return -1;
762 t->in_key = 1;
763 t->in_cond = 0;
764 } else if (t->in_key) {
765 tid = TK_ASSIGN;
767 buffer_copy_string_len(token, t->input + t->offset, 1);
769 t->offset++;
770 t->line_pos++;
771 } else {
772 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
773 "source:", t->source,
774 "line:", t->line, "pos:", t->line_pos,
775 "unexpected equal-sign: =");
776 return -1;
779 break;
780 case '!':
781 if (t->in_cond) {
782 if (t->input[t->offset + 1] == '=') {
783 t->offset += 2;
785 buffer_copy_string_len(token, CONST_STR_LEN("!="));
787 tid = TK_NE;
788 } else if (t->input[t->offset + 1] == '~') {
789 t->offset += 2;
791 buffer_copy_string_len(token, CONST_STR_LEN("!~"));
793 tid = TK_NOMATCH;
794 } else {
795 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
796 "source:", t->source,
797 "line:", t->line, "pos:", t->line_pos,
798 "only !~ and != are allowed in the condition");
799 return -1;
801 t->in_key = 1;
802 t->in_cond = 0;
803 } else {
804 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
805 "source:", t->source,
806 "line:", t->line, "pos:", t->line_pos,
807 "unexpected exclamation-marks: !");
808 return -1;
811 break;
812 case '\t':
813 case ' ':
814 t->offset++;
815 t->line_pos++;
816 break;
817 case '\n':
818 case '\r':
819 if (t->in_brace == 0) {
820 int done = 0;
821 while (!done && t->offset < t->size) {
822 switch (t->input[t->offset]) {
823 case '\r':
824 case '\n':
825 config_skip_newline(t);
826 t->line_pos = 1;
827 t->line++;
828 break;
830 case '#':
831 t->line_pos += config_skip_comment(t);
832 break;
834 case '\t':
835 case ' ':
836 t->offset++;
837 t->line_pos++;
838 break;
840 default:
841 done = 1;
844 t->in_key = 1;
845 tid = TK_EOL;
846 buffer_copy_string_len(token, CONST_STR_LEN("(EOL)"));
847 } else {
848 config_skip_newline(t);
849 t->line_pos = 1;
850 t->line++;
852 break;
853 case ',':
854 if (t->in_brace > 0) {
855 tid = TK_COMMA;
857 buffer_copy_string_len(token, CONST_STR_LEN("(COMMA)"));
860 t->offset++;
861 t->line_pos++;
862 break;
863 case '"':
864 /* search for the terminating " */
865 start = t->input + t->offset + 1;
866 buffer_copy_string_len(token, CONST_STR_LEN(""));
868 for (i = 1; t->input[t->offset + i]; i++) {
869 if (t->input[t->offset + i] == '\\' &&
870 t->input[t->offset + i + 1] == '"') {
872 buffer_append_string_len(token, start, t->input + t->offset + i - start);
874 start = t->input + t->offset + i + 1;
876 /* skip the " */
877 i++;
878 continue;
882 if (t->input[t->offset + i] == '"') {
883 tid = TK_STRING;
885 buffer_append_string_len(token, start, t->input + t->offset + i - start);
887 break;
891 if (t->input[t->offset + i] == '\0') {
892 /* ERROR */
894 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
895 "source:", t->source,
896 "line:", t->line, "pos:", t->line_pos,
897 "missing closing quote");
899 return -1;
902 t->offset += i + 1;
903 t->line_pos += i + 1;
905 break;
906 case '(':
907 t->offset++;
908 t->in_brace++;
910 tid = TK_LPARAN;
912 buffer_copy_string_len(token, CONST_STR_LEN("("));
913 break;
914 case ')':
915 t->offset++;
916 t->in_brace--;
918 tid = TK_RPARAN;
920 buffer_copy_string_len(token, CONST_STR_LEN(")"));
921 break;
922 case '$':
923 t->offset++;
925 tid = TK_DOLLAR;
926 t->in_cond = 1;
927 t->in_key = 0;
929 buffer_copy_string_len(token, CONST_STR_LEN("$"));
931 break;
933 case '+':
934 if (t->input[t->offset + 1] == '=') {
935 t->offset += 2;
936 buffer_copy_string_len(token, CONST_STR_LEN("+="));
937 tid = TK_APPEND;
938 } else {
939 t->offset++;
940 tid = TK_PLUS;
941 buffer_copy_string_len(token, CONST_STR_LEN("+"));
943 break;
945 case '{':
946 t->offset++;
948 tid = TK_LCURLY;
950 buffer_copy_string_len(token, CONST_STR_LEN("{"));
952 break;
954 case '}':
955 t->offset++;
957 tid = TK_RCURLY;
959 buffer_copy_string_len(token, CONST_STR_LEN("}"));
961 break;
963 case '[':
964 t->offset++;
966 tid = TK_LBRACKET;
968 buffer_copy_string_len(token, CONST_STR_LEN("["));
970 break;
972 case ']':
973 t->offset++;
975 tid = TK_RBRACKET;
977 buffer_copy_string_len(token, CONST_STR_LEN("]"));
979 break;
980 case '#':
981 t->line_pos += config_skip_comment(t);
983 break;
984 default:
985 if (t->in_cond) {
986 for (i = 0; t->input[t->offset + i] &&
987 (isalpha((unsigned char)t->input[t->offset + i])
988 ); i++);
990 if (i && t->input[t->offset + i]) {
991 tid = TK_SRVVARNAME;
992 buffer_copy_string_len(token, t->input + t->offset, i);
994 t->offset += i;
995 t->line_pos += i;
996 } else {
997 /* ERROR */
998 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
999 "source:", t->source,
1000 "line:", t->line, "pos:", t->line_pos,
1001 "invalid character in condition");
1002 return -1;
1004 } else if (isdigit((unsigned char)c)) {
1005 /* take all digits */
1006 for (i = 0; t->input[t->offset + i] && isdigit((unsigned char)t->input[t->offset + i]); i++);
1008 /* was there it least a digit ? */
1009 if (i) {
1010 tid = TK_INTEGER;
1012 buffer_copy_string_len(token, t->input + t->offset, i);
1014 t->offset += i;
1015 t->line_pos += i;
1017 } else {
1018 /* the key might consist of [-.0-9a-z] */
1019 for (i = 0; t->input[t->offset + i] &&
1020 (isalnum((unsigned char)t->input[t->offset + i]) ||
1021 t->input[t->offset + i] == '.' ||
1022 t->input[t->offset + i] == '_' || /* for env.* */
1023 t->input[t->offset + i] == '-'
1024 ); i++);
1026 if (i && t->input[t->offset + i]) {
1027 buffer_copy_string_len(token, t->input + t->offset, i);
1029 if (strcmp(token->ptr, "include") == 0) {
1030 tid = TK_INCLUDE;
1031 } else if (strcmp(token->ptr, "include_shell") == 0) {
1032 tid = TK_INCLUDE_SHELL;
1033 } else if (strcmp(token->ptr, "global") == 0) {
1034 tid = TK_GLOBAL;
1035 } else if (strcmp(token->ptr, "else") == 0) {
1036 tid = TK_ELSE;
1037 } else {
1038 tid = TK_LKEY;
1041 t->offset += i;
1042 t->line_pos += i;
1043 } else {
1044 /* ERROR */
1045 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
1046 "source:", t->source,
1047 "line:", t->line, "pos:", t->line_pos,
1048 "invalid character in variable name");
1049 return -1;
1052 break;
1056 if (tid) {
1057 *token_id = tid;
1058 #if 0
1059 log_error_write(srv, __FILE__, __LINE__, "sbsdsdbdd",
1060 "source:", t->source,
1061 "line:", t->line, "pos:", t->line_pos,
1062 token, token->used - 1, tid);
1063 #endif
1065 return 1;
1066 } else if (t->offset < t->size) {
1067 fprintf(stderr, "%s.%d: %d, %s\n",
1068 __FILE__, __LINE__,
1069 tid, token->ptr);
1071 return 0;
1074 static int config_parse(server *srv, config_t *context, tokenizer_t *t) {
1075 void *pParser;
1076 int token_id;
1077 buffer *token, *lasttoken;
1078 int ret;
1080 pParser = configparserAlloc( malloc );
1081 force_assert(pParser);
1082 lasttoken = buffer_init();
1083 token = buffer_init();
1084 while((1 == (ret = config_tokenizer(srv, t, &token_id, token))) && context->ok) {
1085 buffer_copy_buffer(lasttoken, token);
1086 configparser(pParser, token_id, token, context);
1088 token = buffer_init();
1090 buffer_free(token);
1092 if (ret != -1 && context->ok) {
1093 /* add an EOL at EOF, better than say sorry */
1094 configparser(pParser, TK_EOL, buffer_init_string("(EOL)"), context);
1095 if (context->ok) {
1096 configparser(pParser, 0, NULL, context);
1099 configparserFree(pParser, free);
1101 if (ret == -1) {
1102 log_error_write(srv, __FILE__, __LINE__, "sb",
1103 "configfile parser failed at:", lasttoken);
1104 } else if (context->ok == 0) {
1105 log_error_write(srv, __FILE__, __LINE__, "sbsdsdsb",
1106 "source:", t->source,
1107 "line:", t->line, "pos:", t->line_pos,
1108 "parser failed somehow near here:", lasttoken);
1109 ret = -1;
1111 buffer_free(lasttoken);
1113 return ret == -1 ? -1 : 0;
1116 static int tokenizer_init(tokenizer_t *t, const buffer *source, const char *input, size_t size) {
1118 t->source = source;
1119 t->input = input;
1120 t->size = size;
1121 t->offset = 0;
1122 t->line = 1;
1123 t->line_pos = 1;
1125 t->in_key = 1;
1126 t->in_brace = 0;
1127 t->in_cond = 0;
1128 return 0;
1131 static int config_parse_file_stream(server *srv, config_t *context, const buffer *filename) {
1132 tokenizer_t t;
1133 stream s;
1134 int ret;
1136 if (0 != stream_open(&s, filename)) {
1137 log_error_write(srv, __FILE__, __LINE__, "sbss",
1138 "opening configfile ", filename, "failed:", strerror(errno));
1139 return -1;
1140 } else {
1141 tokenizer_init(&t, filename, s.start, s.size);
1142 ret = config_parse(srv, context, &t);
1145 stream_close(&s);
1146 return ret;
1149 int config_parse_file(server *srv, config_t *context, const char *fn) {
1150 buffer *filename;
1151 size_t i;
1152 int ret = -1;
1153 #ifdef GLOB_BRACE
1154 int flags = GLOB_BRACE;
1155 #else
1156 int flags = 0;
1157 #endif
1158 glob_t gl;
1160 if ((fn[0] == '/' || fn[0] == '\\') ||
1161 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\')) ||
1162 (fn[0] == '.' && fn[1] == '.' && (fn[2] == '/' || fn[2] == '\\'))) {
1163 filename = buffer_init_string(fn);
1164 } else {
1165 filename = buffer_init_buffer(context->basedir);
1166 buffer_append_string(filename, fn);
1169 switch (glob(filename->ptr, flags, NULL, &gl)) {
1170 case 0:
1171 for (i = 0; i < gl.gl_pathc; ++i) {
1172 buffer_copy_string(filename, gl.gl_pathv[i]);
1173 ret = config_parse_file_stream(srv, context, filename);
1174 if (0 != ret) break;
1176 globfree(&gl);
1177 break;
1178 case GLOB_NOMATCH:
1179 if (filename->ptr[strcspn(filename->ptr, "*?[]{}")] != '\0') { /*(contains glob metachars)*/
1180 ret = 0; /* not an error if no files match glob pattern */
1182 else {
1183 log_error_write(srv, __FILE__, __LINE__, "sb", "include file not found: ", filename);
1185 break;
1186 case GLOB_ABORTED:
1187 case GLOB_NOSPACE:
1188 log_error_write(srv, __FILE__, __LINE__, "sbss", "glob()", filename, "failed:", strerror(errno));
1189 break;
1192 buffer_free(filename);
1193 return ret;
1196 static char* getCWD(void) {
1197 char *s, *s1;
1198 size_t len;
1199 #ifdef PATH_MAX
1200 len = PATH_MAX;
1201 #else
1202 len = 4096;
1203 #endif
1205 s = malloc(len);
1206 if (!s) return NULL;
1207 while (NULL == getcwd(s, len)) {
1208 if (errno != ERANGE || SSIZE_MAX - len < len) {
1209 free(s);
1210 return NULL;
1212 len *= 2;
1213 s1 = realloc(s, len);
1214 if (!s1) {
1215 free(s);
1216 return NULL;
1218 s = s1;
1220 return s;
1223 int config_parse_cmd(server *srv, config_t *context, const char *cmd) {
1224 tokenizer_t t;
1225 int ret;
1226 buffer *source;
1227 buffer *out;
1228 char *oldpwd;
1230 if (NULL == (oldpwd = getCWD())) {
1231 log_error_write(srv, __FILE__, __LINE__, "s",
1232 "cannot get cwd", strerror(errno));
1233 return -1;
1236 if (!buffer_string_is_empty(context->basedir)) {
1237 if (0 != chdir(context->basedir->ptr)) {
1238 log_error_write(srv, __FILE__, __LINE__, "sbs",
1239 "cannot change directory to", context->basedir, strerror(errno));
1240 free(oldpwd);
1241 return -1;
1245 source = buffer_init_string(cmd);
1246 out = buffer_init();
1248 if (0 != proc_open_buffer(cmd, NULL, out, NULL)) {
1249 log_error_write(srv, __FILE__, __LINE__, "sbss",
1250 "opening", source, "failed:", strerror(errno));
1251 ret = -1;
1252 } else {
1253 tokenizer_init(&t, source, CONST_BUF_LEN(out));
1254 ret = config_parse(srv, context, &t);
1257 buffer_free(source);
1258 buffer_free(out);
1259 if (0 != chdir(oldpwd)) {
1260 log_error_write(srv, __FILE__, __LINE__, "sss",
1261 "cannot change directory to", oldpwd, strerror(errno));
1262 free(oldpwd);
1263 return -1;
1265 free(oldpwd);
1266 return ret;
1269 static void context_init(server *srv, config_t *context) {
1270 context->srv = srv;
1271 context->ok = 1;
1272 vector_config_weak_init(&context->configs_stack);
1273 context->basedir = buffer_init();
1276 static void context_free(config_t *context) {
1277 vector_config_weak_clear(&context->configs_stack);
1278 buffer_free(context->basedir);
1281 int config_read(server *srv, const char *fn) {
1282 config_t context;
1283 data_config *dc;
1284 data_integer *dpid;
1285 data_string *dcwd;
1286 int ret;
1287 char *pos;
1288 buffer *filename;
1290 context_init(srv, &context);
1291 context.all_configs = srv->config_context;
1293 #ifdef __WIN32
1294 pos = strrchr(fn, '\\');
1295 #else
1296 pos = strrchr(fn, '/');
1297 #endif
1298 if (pos) {
1299 buffer_copy_string_len(context.basedir, fn, pos - fn + 1);
1302 dc = data_config_init();
1303 buffer_copy_string_len(dc->key, CONST_STR_LEN("global"));
1305 force_assert(context.all_configs->used == 0);
1306 dc->context_ndx = context.all_configs->used;
1307 array_insert_unique(context.all_configs, (data_unset *)dc);
1308 context.current = dc;
1310 /* default context */
1311 dpid = data_integer_init();
1312 dpid->value = getpid();
1313 buffer_copy_string_len(dpid->key, CONST_STR_LEN("var.PID"));
1314 array_insert_unique(dc->value, (data_unset *)dpid);
1316 dcwd = data_string_init();
1317 buffer_string_prepare_copy(dcwd->value, 1023);
1318 if (NULL != getcwd(dcwd->value->ptr, dcwd->value->size - 1)) {
1319 buffer_commit(dcwd->value, strlen(dcwd->value->ptr));
1320 buffer_copy_string_len(dcwd->key, CONST_STR_LEN("var.CWD"));
1321 array_insert_unique(dc->value, (data_unset *)dcwd);
1322 } else {
1323 dcwd->free((data_unset*) dcwd);
1326 filename = buffer_init_string(fn);
1327 ret = config_parse_file_stream(srv, &context, filename);
1328 buffer_free(filename);
1330 /* remains nothing if parser is ok */
1331 force_assert(!(0 == ret && context.ok && 0 != context.configs_stack.used));
1332 context_free(&context);
1334 if (0 != ret) {
1335 return ret;
1338 if (0 != config_insert(srv)) {
1339 return -1;
1342 return 0;
1345 int config_set_defaults(server *srv) {
1346 size_t i;
1347 specific_config *s = srv->config_storage[0];
1348 struct stat st1, st2;
1350 struct ev_map { fdevent_handler_t et; const char *name; } event_handlers[] =
1352 /* - epoll is most reliable
1353 * - select works everywhere
1355 #ifdef USE_LINUX_EPOLL
1356 { FDEVENT_HANDLER_LINUX_SYSEPOLL, "linux-sysepoll" },
1357 #endif
1358 #ifdef USE_POLL
1359 { FDEVENT_HANDLER_POLL, "poll" },
1360 #endif
1361 #ifdef USE_SELECT
1362 { FDEVENT_HANDLER_SELECT, "select" },
1363 #endif
1364 #ifdef USE_LIBEV
1365 { FDEVENT_HANDLER_LIBEV, "libev" },
1366 #endif
1367 #ifdef USE_SOLARIS_DEVPOLL
1368 { FDEVENT_HANDLER_SOLARIS_DEVPOLL,"solaris-devpoll" },
1369 #endif
1370 #ifdef USE_SOLARIS_PORT
1371 { FDEVENT_HANDLER_SOLARIS_PORT, "solaris-eventports" },
1372 #endif
1373 #ifdef USE_FREEBSD_KQUEUE
1374 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "freebsd-kqueue" },
1375 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "kqueue" },
1376 #endif
1377 { FDEVENT_HANDLER_UNSET, NULL }
1380 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1381 if (-1 == stat(srv->srvconf.changeroot->ptr, &st1)) {
1382 log_error_write(srv, __FILE__, __LINE__, "sb",
1383 "server.chroot doesn't exist:", srv->srvconf.changeroot);
1384 return -1;
1386 if (!S_ISDIR(st1.st_mode)) {
1387 log_error_write(srv, __FILE__, __LINE__, "sb",
1388 "server.chroot isn't a directory:", srv->srvconf.changeroot);
1389 return -1;
1393 if (srv->srvconf.upload_tempdirs->used) {
1394 buffer * const b = srv->tmp_buf;
1395 size_t len;
1396 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1397 buffer_copy_buffer(b, srv->srvconf.changeroot);
1398 buffer_append_slash(b);
1399 } else {
1400 buffer_reset(b);
1402 len = buffer_string_length(b);
1404 for (i = 0; i < srv->srvconf.upload_tempdirs->used; ++i) {
1405 const data_string * const ds = (data_string *)srv->srvconf.upload_tempdirs->data[i];
1406 buffer_string_set_length(b, len); /*(truncate)*/
1407 buffer_append_string_buffer(b, ds->value);
1408 if (-1 == stat(b->ptr, &st1)) {
1409 log_error_write(srv, __FILE__, __LINE__, "sb",
1410 "server.upload-dirs doesn't exist:", b);
1411 } else if (!S_ISDIR(st1.st_mode)) {
1412 log_error_write(srv, __FILE__, __LINE__, "sb",
1413 "server.upload-dirs isn't a directory:", b);
1418 if (buffer_string_is_empty(s->document_root)) {
1419 log_error_write(srv, __FILE__, __LINE__, "s",
1420 "a default document-root has to be set");
1422 return -1;
1425 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1427 buffer_to_lower(srv->tmp_buf);
1429 if (2 == s->force_lowercase_filenames) { /* user didn't configure it in global section? */
1430 s->force_lowercase_filenames = 0; /* default to 0 */
1432 if (0 == stat(srv->tmp_buf->ptr, &st1)) {
1433 int is_lower = 0;
1435 is_lower = buffer_is_equal(srv->tmp_buf, s->document_root);
1437 /* lower-case existed, check upper-case */
1438 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1440 buffer_to_upper(srv->tmp_buf);
1442 /* we have to handle the special case that upper and lower-casing results in the same filename
1443 * as in server.document-root = "/" or "/12345/" */
1445 if (is_lower && buffer_is_equal(srv->tmp_buf, s->document_root)) {
1446 /* lower-casing and upper-casing didn't result in
1447 * an other filename, no need to stat(),
1448 * just assume it is case-sensitive. */
1450 s->force_lowercase_filenames = 0;
1451 } else if (0 == stat(srv->tmp_buf->ptr, &st2)) {
1453 /* upper case exists too, doesn't the FS handle this ? */
1455 /* upper and lower have the same inode -> case-insensitve FS */
1457 if (st1.st_ino == st2.st_ino) {
1458 /* upper and lower have the same inode -> case-insensitve FS */
1460 s->force_lowercase_filenames = 1;
1466 if (srv->srvconf.port == 0) {
1467 srv->srvconf.port = s->ssl_enabled ? 443 : 80;
1470 if (buffer_string_is_empty(srv->srvconf.event_handler)) {
1471 /* choose a good default
1473 * the event_handler list is sorted by 'goodness'
1474 * taking the first available should be the best solution
1476 srv->event_handler = event_handlers[0].et;
1478 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1479 log_error_write(srv, __FILE__, __LINE__, "s",
1480 "sorry, there is no event handler for this system");
1482 return -1;
1484 } else {
1486 * User override
1489 for (i = 0; event_handlers[i].name; i++) {
1490 if (0 == strcmp(event_handlers[i].name, srv->srvconf.event_handler->ptr)) {
1491 srv->event_handler = event_handlers[i].et;
1492 break;
1496 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1497 log_error_write(srv, __FILE__, __LINE__, "sb",
1498 "the selected event-handler in unknown or not supported:",
1499 srv->srvconf.event_handler );
1501 return -1;
1505 if (s->ssl_enabled) {
1506 if (buffer_string_is_empty(s->ssl_pemfile)) {
1507 /* PEM file is require */
1509 log_error_write(srv, __FILE__, __LINE__, "s",
1510 "ssl.pemfile has to be set");
1511 return -1;
1514 #ifndef USE_OPENSSL
1515 log_error_write(srv, __FILE__, __LINE__, "s",
1516 "ssl support is missing, recompile with --with-openssl");
1518 return -1;
1519 #endif
1522 return 0;