remove excess calls to joblist_append()
[lighttpd.git] / src / configfile.c
blobffcc7a52aca6c8f93c78577876138ffc3bba7982
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"
13 #include <sys/stat.h>
15 #include <stdlib.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <limits.h>
23 #include <assert.h>
24 #include <glob.h>
27 static int config_insert(server *srv) {
28 size_t i;
29 int ret = 0;
30 buffer *stat_cache_string;
32 config_values_t cv[] = {
33 { "server.bind", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 0 */
34 { "server.errorlog", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 1 */
35 { "server.errorfile-prefix", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
36 { "server.chroot", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 3 */
37 { "server.username", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 4 */
38 { "server.groupname", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 5 */
39 { "server.port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 6 */
40 { "server.tag", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
41 { "server.use-ipv6", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
42 { "server.modules", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_SERVER }, /* 9 */
44 { "server.event-handler", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 10 */
45 { "server.pid-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 11 */
46 { "server.max-request-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_SERVER }, /* 12 */
47 { "server.max-worker", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 13 */
48 { "server.document-root", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
49 { "server.force-lowercase-filenames", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 15 */
50 { "debug.log-condition-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 16 */
51 { "server.max-keep-alive-requests", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 17 */
52 { "server.name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 18 */
53 { "server.max-keep-alive-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 19 */
55 { "server.max-read-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 20 */
56 { "server.max-write-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 21 */
57 { "server.error-handler", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 22 */
58 { "server.max-fds", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 23 */
59 #ifdef HAVE_LSTAT
60 { "server.follow-symlink", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 24 */
61 #else
62 { "server.follow-symlink",
63 "Your system lacks lstat(). We can not differ symlinks from files."
64 "Please remove server.follow-symlinks from your config.",
65 T_CONFIG_UNSUPPORTED, T_CONFIG_SCOPE_UNSET },
66 #endif
67 { "server.kbytes-per-second", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 25 */
68 { "connection.kbytes-per-second", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 26 */
69 { "mimetype.use-xattr", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 27 */
70 { "mimetype.assign", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 28 */
71 { "ssl.pemfile", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 29 */
73 { "ssl.engine", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 30 */
74 { "debug.log-file-not-found", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 31 */
75 { "debug.log-request-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 32 */
76 { "debug.log-response-header", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 33 */
77 { "debug.log-request-header", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 34 */
78 { "debug.log-ssl-noise", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 35 */
79 { "server.protocol-http11", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 36 */
80 { "debug.log-request-header-on-error", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 37 */
81 { "debug.log-state-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 38 */
82 { "ssl.ca-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 39 */
84 { "server.errorlog-use-syslog", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 40 */
85 { "server.range-requests", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 41 */
86 { "server.stat-cache-engine", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 42 */
87 { "server.max-connections", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 43 */
88 { "server.network-backend", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 44 */
89 { "server.upload-dirs", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_SERVER }, /* 45 */
90 { "server.core-files", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 46 */
91 { "ssl.cipher-list", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 47 */
92 { "ssl.use-sslv2", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 48 */
93 { "etag.use-inode", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 49 */
95 { "etag.use-mtime", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 50 */
96 { "etag.use-size", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 51 */
97 { "server.reject-expect-100-with-417", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 52 */
98 { "debug.log-timeouts", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 53 */
99 { "server.defer-accept", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 54 */
100 { "server.breakagelog", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 55 */
101 { "ssl.verifyclient.activate", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 56 */
102 { "ssl.verifyclient.enforce", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 57 */
103 { "ssl.verifyclient.depth", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 58 */
104 { "ssl.verifyclient.username", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 59 */
106 { "ssl.verifyclient.exportcert", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 60 */
107 { "server.set-v6only", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 61 */
108 { "ssl.use-sslv3", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 62 */
109 { "ssl.dh-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 63 */
110 { "ssl.ec-curve", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 64 */
111 { "ssl.disable-client-renegotiation", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 65 */
112 { "ssl.honor-cipher-order", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 66 */
113 { "ssl.empty-fragments", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 67 */
114 { "server.upload-temp-file-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_SERVER }, /* 68 */
115 { "mimetype.xattr-name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 69 */
116 { "server.listen-backlog", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 70 */
117 { "server.error-handler-404", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 71 */
118 { "server.http-parseopt-header-strict",NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 72 */
119 { "server.http-parseopt-host-strict", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 73 */
120 { "server.http-parseopt-host-normalize",NULL,T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 74 */
121 { "server.bsd-accept-filter", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 75 */
122 { "server.stream-request-body", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 76 */
123 { "server.stream-response-body", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 77 */
125 { "server.host",
126 "use server.bind instead",
127 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
128 { "server.docroot",
129 "use server.document-root instead",
130 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
131 { "server.virtual-root",
132 "load mod_simple_vhost and use simple-vhost.server-root instead",
133 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
134 { "server.virtual-default-host",
135 "load mod_simple_vhost and use simple-vhost.default-host instead",
136 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
137 { "server.virtual-docroot",
138 "load mod_simple_vhost and use simple-vhost.document-root instead",
139 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
140 { "server.userid",
141 "use server.username instead",
142 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
143 { "server.groupid",
144 "use server.groupname instead",
145 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
146 { "server.use-keep-alive",
147 "use server.max-keep-alive-requests = 0 instead",
148 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
149 { "server.force-lower-case-files",
150 "use server.force-lowercase-filenames instead",
151 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
153 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
156 /* all T_CONFIG_SCOPE_SERVER options */
157 cv[0].destination = srv->srvconf.bindhost;
158 cv[1].destination = srv->srvconf.errorlog_file;
159 cv[3].destination = srv->srvconf.changeroot;
160 cv[4].destination = srv->srvconf.username;
161 cv[5].destination = srv->srvconf.groupname;
162 cv[6].destination = &(srv->srvconf.port);
163 cv[9].destination = srv->srvconf.modules;
165 cv[10].destination = srv->srvconf.event_handler;
166 cv[11].destination = srv->srvconf.pid_file;
167 cv[12].destination = &(srv->srvconf.max_request_size);
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();
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->use_xattr = 0;
225 s->ssl_enabled = 0;
226 s->ssl_honor_cipher_order = 1;
227 s->ssl_empty_fragments = 0;
228 s->ssl_use_sslv2 = 0;
229 s->ssl_use_sslv3 = 0;
230 s->use_ipv6 = 0;
231 s->set_v6only = 1;
232 s->defer_accept = (i == 0) ? 0 : srv->config_storage[0]->defer_accept;
233 #ifdef HAVE_LSTAT
234 s->follow_symlink = 1;
235 #endif
236 s->kbytes_per_second = 0;
237 s->allow_http11 = 1;
238 s->etag_use_inode = 1;
239 s->etag_use_mtime = 1;
240 s->etag_use_size = 1;
241 s->range_requests = 1;
242 s->force_lowercase_filenames = (i == 0) ? 2 : 0; /* we wan't to detect later if user changed this for global section */
243 s->global_kbytes_per_second = 0;
244 s->global_bytes_per_second_cnt = 0;
245 s->global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
246 s->ssl_verifyclient = 0;
247 s->ssl_verifyclient_enforce = 1;
248 s->ssl_verifyclient_username = buffer_init();
249 s->ssl_verifyclient_depth = 9;
250 s->ssl_verifyclient_export_cert = 0;
251 s->ssl_disable_client_renegotiation = 1;
252 s->listen_backlog = (0 == i ? 1024 : srv->config_storage[0]->listen_backlog);
253 s->stream_request_body = 0;
254 s->stream_response_body = 0;
256 /* all T_CONFIG_SCOPE_CONNECTION options */
257 cv[2].destination = s->errorfile_prefix;
258 cv[7].destination = s->server_tag;
259 cv[8].destination = &(s->use_ipv6);
261 cv[14].destination = s->document_root;
262 cv[15].destination = &(s->force_lowercase_filenames);
263 cv[16].destination = &(s->log_condition_handling);
264 cv[17].destination = &(s->max_keep_alive_requests);
265 cv[18].destination = s->server_name;
266 cv[19].destination = &(s->max_keep_alive_idle);
268 cv[20].destination = &(s->max_read_idle);
269 cv[21].destination = &(s->max_write_idle);
270 cv[22].destination = s->error_handler;
271 #ifdef HAVE_LSTAT
272 cv[24].destination = &(s->follow_symlink);
273 #endif
274 cv[25].destination = &(s->global_kbytes_per_second);
275 cv[26].destination = &(s->kbytes_per_second);
276 cv[27].destination = &(s->use_xattr);
277 cv[28].destination = s->mimetypes;
278 cv[29].destination = s->ssl_pemfile;
280 cv[30].destination = &(s->ssl_enabled);
281 cv[31].destination = &(s->log_file_not_found);
282 cv[32].destination = &(s->log_request_handling);
283 cv[33].destination = &(s->log_response_header);
284 cv[34].destination = &(s->log_request_header);
285 cv[35].destination = &(s->log_ssl_noise);
286 cv[36].destination = &(s->allow_http11);
287 cv[39].destination = s->ssl_ca_file;
289 cv[41].destination = &(s->range_requests);
290 cv[47].destination = s->ssl_cipher_list;
291 cv[48].destination = &(s->ssl_use_sslv2);
292 cv[49].destination = &(s->etag_use_inode);
294 cv[50].destination = &(s->etag_use_mtime);
295 cv[51].destination = &(s->etag_use_size);
296 cv[53].destination = &(s->log_timeouts);
297 cv[54].destination = &(s->defer_accept);
298 cv[56].destination = &(s->ssl_verifyclient);
299 cv[57].destination = &(s->ssl_verifyclient_enforce);
300 cv[58].destination = &(s->ssl_verifyclient_depth);
301 cv[59].destination = s->ssl_verifyclient_username;
303 cv[60].destination = &(s->ssl_verifyclient_export_cert);
304 cv[61].destination = &(s->set_v6only);
305 cv[62].destination = &(s->ssl_use_sslv3);
306 cv[63].destination = s->ssl_dh_file;
307 cv[64].destination = s->ssl_ec_curve;
308 cv[65].destination = &(s->ssl_disable_client_renegotiation);
309 cv[66].destination = &(s->ssl_honor_cipher_order);
310 cv[67].destination = &(s->ssl_empty_fragments);
311 cv[70].destination = &(s->listen_backlog);
312 cv[71].destination = s->error_handler_404;
313 #if defined(__FreeBSD__) || defined(__NetBSD__) \
314 || defined(__OpenBSD__) || defined(__DragonflyBSD__)
315 cv[75].destination = s->bsd_accept_filter;
316 #endif
317 cv[76].destination = &(s->stream_request_body);
318 cv[77].destination = &(s->stream_response_body);
320 srv->config_storage[i] = s;
322 if (0 != (ret = config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION))) {
323 break;
326 if (s->stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN) {
327 s->stream_request_body |= FDEVENT_STREAM_REQUEST;
329 if (s->stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) {
330 s->stream_response_body |= FDEVENT_STREAM_RESPONSE;
335 specific_config *s = srv->config_storage[0];
336 s->http_parseopts= /*(global, but stored in con->conf.http_parseopts)*/
337 (srv->srvconf.http_header_strict ?(HTTP_PARSEOPT_HEADER_STRICT) :0)
338 |(srv->srvconf.http_host_strict ?(HTTP_PARSEOPT_HOST_STRICT
339 |HTTP_PARSEOPT_HOST_NORMALIZE):0)
340 |(srv->srvconf.http_host_normalize ?(HTTP_PARSEOPT_HOST_NORMALIZE):0);
343 if (buffer_string_is_empty(stat_cache_string)) {
344 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
345 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("simple"))) {
346 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
347 #ifdef HAVE_FAM_H
348 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("fam"))) {
349 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_FAM;
350 #endif
351 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("disable"))) {
352 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_NONE;
353 } else {
354 log_error_write(srv, __FILE__, __LINE__, "sb",
355 "server.stat-cache-engine can be one of \"disable\", \"simple\","
356 #ifdef HAVE_FAM_H
357 " \"fam\","
358 #endif
359 " but not:", stat_cache_string);
360 ret = HANDLER_ERROR;
363 buffer_free(stat_cache_string);
366 data_string *ds;
367 int prepend_mod_indexfile = 1;
368 int append_mod_dirlisting = 1;
369 int append_mod_staticfile = 1;
371 /* prepend default modules */
372 for (i = 0; i < srv->srvconf.modules->used; i++) {
373 ds = (data_string *)srv->srvconf.modules->data[i];
375 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_indexfile"))) {
376 prepend_mod_indexfile = 0;
379 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_staticfile"))) {
380 append_mod_staticfile = 0;
383 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_dirlisting"))) {
384 append_mod_dirlisting = 0;
387 if (0 == prepend_mod_indexfile &&
388 0 == append_mod_dirlisting &&
389 0 == append_mod_staticfile) {
390 break;
394 if (prepend_mod_indexfile) {
395 /* mod_indexfile has to be loaded before mod_fastcgi and friends */
396 array *modules = array_init();
398 ds = data_string_init();
399 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_indexfile"));
400 array_insert_unique(modules, (data_unset *)ds);
402 for (i = 0; i < srv->srvconf.modules->used; i++) {
403 data_unset *du = srv->srvconf.modules->data[i];
404 array_insert_unique(modules, du->copy(du));
407 array_free(srv->srvconf.modules);
408 srv->srvconf.modules = modules;
411 /* append default modules */
412 if (append_mod_dirlisting) {
413 ds = data_string_init();
414 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_dirlisting"));
415 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
418 if (append_mod_staticfile) {
419 ds = data_string_init();
420 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_staticfile"));
421 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
425 return ret;
430 #define PATCH(x) con->conf.x = s->x
431 int config_setup_connection(server *srv, connection *con) {
432 specific_config *s = srv->config_storage[0];
434 PATCH(http_parseopts);
436 PATCH(allow_http11);
437 PATCH(mimetypes);
438 PATCH(document_root);
439 PATCH(max_keep_alive_requests);
440 PATCH(max_keep_alive_idle);
441 PATCH(max_read_idle);
442 PATCH(max_write_idle);
443 PATCH(use_xattr);
444 PATCH(error_handler);
445 PATCH(error_handler_404);
446 PATCH(errorfile_prefix);
447 #ifdef HAVE_LSTAT
448 PATCH(follow_symlink);
449 #endif
450 PATCH(server_tag);
451 PATCH(kbytes_per_second);
452 PATCH(global_kbytes_per_second);
453 PATCH(global_bytes_per_second_cnt);
455 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
456 buffer_copy_buffer(con->server_name, s->server_name);
458 PATCH(log_request_header);
459 PATCH(log_response_header);
460 PATCH(log_request_handling);
461 PATCH(log_condition_handling);
462 PATCH(log_file_not_found);
463 PATCH(log_ssl_noise);
464 PATCH(log_timeouts);
466 PATCH(range_requests);
467 PATCH(force_lowercase_filenames);
468 /*PATCH(listen_backlog);*//*(not necessary; used only at startup)*/
469 PATCH(stream_request_body);
470 PATCH(stream_response_body);
472 PATCH(ssl_enabled);
474 PATCH(ssl_pemfile);
475 #ifdef USE_OPENSSL
476 PATCH(ssl_pemfile_x509);
477 PATCH(ssl_pemfile_pkey);
478 #endif
479 PATCH(ssl_ca_file);
480 #ifdef USE_OPENSSL
481 PATCH(ssl_ca_file_cert_names);
482 #endif
483 PATCH(ssl_cipher_list);
484 PATCH(ssl_dh_file);
485 PATCH(ssl_ec_curve);
486 PATCH(ssl_honor_cipher_order);
487 PATCH(ssl_empty_fragments);
488 PATCH(ssl_use_sslv2);
489 PATCH(ssl_use_sslv3);
490 PATCH(etag_use_inode);
491 PATCH(etag_use_mtime);
492 PATCH(etag_use_size);
494 PATCH(ssl_verifyclient);
495 PATCH(ssl_verifyclient_enforce);
496 PATCH(ssl_verifyclient_depth);
497 PATCH(ssl_verifyclient_username);
498 PATCH(ssl_verifyclient_export_cert);
499 PATCH(ssl_disable_client_renegotiation);
501 return 0;
504 int config_patch_connection(server *srv, connection *con) {
505 size_t i, j;
507 /* skip the first, the global context */
508 for (i = 1; i < srv->config_context->used; i++) {
509 data_config *dc = (data_config *)srv->config_context->data[i];
510 specific_config *s = srv->config_storage[i];
512 /* condition didn't match */
513 if (!config_check_cond(srv, con, dc)) continue;
515 /* merge config */
516 for (j = 0; j < dc->value->used; j++) {
517 data_unset *du = dc->value->data[j];
519 if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.document-root"))) {
520 PATCH(document_root);
521 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.range-requests"))) {
522 PATCH(range_requests);
523 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler"))) {
524 PATCH(error_handler);
525 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler-404"))) {
526 PATCH(error_handler_404);
527 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.errorfile-prefix"))) {
528 PATCH(errorfile_prefix);
529 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.assign"))) {
530 PATCH(mimetypes);
531 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-requests"))) {
532 PATCH(max_keep_alive_requests);
533 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-idle"))) {
534 PATCH(max_keep_alive_idle);
535 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-write-idle"))) {
536 PATCH(max_write_idle);
537 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-read-idle"))) {
538 PATCH(max_read_idle);
539 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.use-xattr"))) {
540 PATCH(use_xattr);
541 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-inode"))) {
542 PATCH(etag_use_inode);
543 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-mtime"))) {
544 PATCH(etag_use_mtime);
545 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-size"))) {
546 PATCH(etag_use_size);
547 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.pemfile"))) {
548 PATCH(ssl_pemfile);
549 #ifdef USE_OPENSSL
550 PATCH(ssl_pemfile_x509);
551 PATCH(ssl_pemfile_pkey);
552 #endif
553 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ca-file"))) {
554 PATCH(ssl_ca_file);
555 #ifdef USE_OPENSSL
556 PATCH(ssl_ca_file_cert_names);
557 #endif
558 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.honor-cipher-order"))) {
559 PATCH(ssl_honor_cipher_order);
560 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.empty-fragments"))) {
561 PATCH(ssl_empty_fragments);
562 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv2"))) {
563 PATCH(ssl_use_sslv2);
564 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv3"))) {
565 PATCH(ssl_use_sslv3);
566 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.cipher-list"))) {
567 PATCH(ssl_cipher_list);
568 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.engine"))) {
569 PATCH(ssl_enabled);
570 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.dh-file"))) {
571 PATCH(ssl_dh_file);
572 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ec-curve"))) {
573 PATCH(ssl_ec_curve);
574 #ifdef HAVE_LSTAT
575 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.follow-symlink"))) {
576 PATCH(follow_symlink);
577 #endif
578 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.name"))) {
579 buffer_copy_buffer(con->server_name, s->server_name);
580 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.tag"))) {
581 PATCH(server_tag);
582 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-request-body"))) {
583 PATCH(stream_request_body);
584 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-response-body"))) {
585 PATCH(stream_response_body);
586 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("connection.kbytes-per-second"))) {
587 PATCH(kbytes_per_second);
588 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-handling"))) {
589 PATCH(log_request_handling);
590 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-header"))) {
591 PATCH(log_request_header);
592 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-response-header"))) {
593 PATCH(log_response_header);
594 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-condition-handling"))) {
595 PATCH(log_condition_handling);
596 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-file-not-found"))) {
597 PATCH(log_file_not_found);
598 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-ssl-noise"))) {
599 PATCH(log_ssl_noise);
600 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-timeouts"))) {
601 PATCH(log_timeouts);
602 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.protocol-http11"))) {
603 PATCH(allow_http11);
604 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.force-lowercase-filenames"))) {
605 PATCH(force_lowercase_filenames);
606 #if 0 /*(not necessary; used only at startup)*/
607 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.listen-backlog"))) {
608 PATCH(listen_backlog);
609 #endif
610 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.kbytes-per-second"))) {
611 PATCH(global_kbytes_per_second);
612 PATCH(global_bytes_per_second_cnt);
613 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
614 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.activate"))) {
615 PATCH(ssl_verifyclient);
616 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.enforce"))) {
617 PATCH(ssl_verifyclient_enforce);
618 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.depth"))) {
619 PATCH(ssl_verifyclient_depth);
620 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.username"))) {
621 PATCH(ssl_verifyclient_username);
622 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.exportcert"))) {
623 PATCH(ssl_verifyclient_export_cert);
624 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.disable-client-renegotiation"))) {
625 PATCH(ssl_disable_client_renegotiation);
630 con->etag_flags = (con->conf.etag_use_mtime ? ETAG_USE_MTIME : 0) |
631 (con->conf.etag_use_inode ? ETAG_USE_INODE : 0) |
632 (con->conf.etag_use_size ? ETAG_USE_SIZE : 0);
634 return 0;
636 #undef PATCH
638 typedef struct {
639 int foo;
640 int bar;
642 const buffer *source;
643 const char *input;
644 size_t offset;
645 size_t size;
647 int line_pos;
648 int line;
650 int in_key;
651 int in_brace;
652 int in_cond;
653 } tokenizer_t;
655 #if 0
656 static int tokenizer_open(server *srv, tokenizer_t *t, buffer *basedir, const char *fn) {
657 if (buffer_string_is_empty(basedir) ||
658 (fn[0] == '/' || fn[0] == '\\') ||
659 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\'))) {
660 t->file = buffer_init_string(fn);
661 } else {
662 t->file = buffer_init_buffer(basedir);
663 buffer_append_string(t->file, fn);
666 if (0 != stream_open(&(t->s), t->file)) {
667 log_error_write(srv, __FILE__, __LINE__, "sbss",
668 "opening configfile ", t->file, "failed:", strerror(errno));
669 buffer_free(t->file);
670 return -1;
673 t->input = t->s.start;
674 t->offset = 0;
675 t->size = t->s.size;
676 t->line = 1;
677 t->line_pos = 1;
679 t->in_key = 1;
680 t->in_brace = 0;
681 t->in_cond = 0;
682 return 0;
685 static int tokenizer_close(server *srv, tokenizer_t *t) {
686 UNUSED(srv);
688 buffer_free(t->file);
689 return stream_close(&(t->s));
691 #endif
692 static int config_skip_newline(tokenizer_t *t) {
693 int skipped = 1;
694 force_assert(t->input[t->offset] == '\r' || t->input[t->offset] == '\n');
695 if (t->input[t->offset] == '\r' && t->input[t->offset + 1] == '\n') {
696 skipped ++;
697 t->offset ++;
699 t->offset ++;
700 return skipped;
703 static int config_skip_comment(tokenizer_t *t) {
704 int i;
705 force_assert(t->input[t->offset] == '#');
706 for (i = 1; t->input[t->offset + i] &&
707 (t->input[t->offset + i] != '\n' && t->input[t->offset + i] != '\r');
708 i++);
709 t->offset += i;
710 return i;
713 static int config_tokenizer(server *srv, tokenizer_t *t, int *token_id, buffer *token) {
714 int tid = 0;
715 size_t i;
717 for (tid = 0; tid == 0 && t->offset < t->size && t->input[t->offset] ; ) {
718 char c = t->input[t->offset];
719 const char *start = NULL;
721 switch (c) {
722 case '=':
723 if (t->in_brace) {
724 if (t->input[t->offset + 1] == '>') {
725 t->offset += 2;
727 buffer_copy_string_len(token, CONST_STR_LEN("=>"));
729 tid = TK_ARRAY_ASSIGN;
730 } else {
731 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
732 "source:", t->source,
733 "line:", t->line, "pos:", t->line_pos,
734 "use => for assignments in arrays");
735 return -1;
737 } else if (t->in_cond) {
738 if (t->input[t->offset + 1] == '=') {
739 t->offset += 2;
741 buffer_copy_string_len(token, CONST_STR_LEN("=="));
743 tid = TK_EQ;
744 } else if (t->input[t->offset + 1] == '~') {
745 t->offset += 2;
747 buffer_copy_string_len(token, CONST_STR_LEN("=~"));
749 tid = TK_MATCH;
750 } else {
751 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
752 "source:", t->source,
753 "line:", t->line, "pos:", t->line_pos,
754 "only =~ and == are allowed in the condition");
755 return -1;
757 t->in_key = 1;
758 t->in_cond = 0;
759 } else if (t->in_key) {
760 tid = TK_ASSIGN;
762 buffer_copy_string_len(token, t->input + t->offset, 1);
764 t->offset++;
765 t->line_pos++;
766 } else {
767 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
768 "source:", t->source,
769 "line:", t->line, "pos:", t->line_pos,
770 "unexpected equal-sign: =");
771 return -1;
774 break;
775 case '!':
776 if (t->in_cond) {
777 if (t->input[t->offset + 1] == '=') {
778 t->offset += 2;
780 buffer_copy_string_len(token, CONST_STR_LEN("!="));
782 tid = TK_NE;
783 } else if (t->input[t->offset + 1] == '~') {
784 t->offset += 2;
786 buffer_copy_string_len(token, CONST_STR_LEN("!~"));
788 tid = TK_NOMATCH;
789 } else {
790 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
791 "source:", t->source,
792 "line:", t->line, "pos:", t->line_pos,
793 "only !~ and != are allowed in the condition");
794 return -1;
796 t->in_key = 1;
797 t->in_cond = 0;
798 } else {
799 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
800 "source:", t->source,
801 "line:", t->line, "pos:", t->line_pos,
802 "unexpected exclamation-marks: !");
803 return -1;
806 break;
807 case '\t':
808 case ' ':
809 t->offset++;
810 t->line_pos++;
811 break;
812 case '\n':
813 case '\r':
814 if (t->in_brace == 0) {
815 int done = 0;
816 while (!done && t->offset < t->size) {
817 switch (t->input[t->offset]) {
818 case '\r':
819 case '\n':
820 config_skip_newline(t);
821 t->line_pos = 1;
822 t->line++;
823 break;
825 case '#':
826 t->line_pos += config_skip_comment(t);
827 break;
829 case '\t':
830 case ' ':
831 t->offset++;
832 t->line_pos++;
833 break;
835 default:
836 done = 1;
839 t->in_key = 1;
840 tid = TK_EOL;
841 buffer_copy_string_len(token, CONST_STR_LEN("(EOL)"));
842 } else {
843 config_skip_newline(t);
844 t->line_pos = 1;
845 t->line++;
847 break;
848 case ',':
849 if (t->in_brace > 0) {
850 tid = TK_COMMA;
852 buffer_copy_string_len(token, CONST_STR_LEN("(COMMA)"));
855 t->offset++;
856 t->line_pos++;
857 break;
858 case '"':
859 /* search for the terminating " */
860 start = t->input + t->offset + 1;
861 buffer_copy_string_len(token, CONST_STR_LEN(""));
863 for (i = 1; t->input[t->offset + i]; i++) {
864 if (t->input[t->offset + i] == '\\' &&
865 t->input[t->offset + i + 1] == '"') {
867 buffer_append_string_len(token, start, t->input + t->offset + i - start);
869 start = t->input + t->offset + i + 1;
871 /* skip the " */
872 i++;
873 continue;
877 if (t->input[t->offset + i] == '"') {
878 tid = TK_STRING;
880 buffer_append_string_len(token, start, t->input + t->offset + i - start);
882 break;
886 if (t->input[t->offset + i] == '\0') {
887 /* ERROR */
889 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
890 "source:", t->source,
891 "line:", t->line, "pos:", t->line_pos,
892 "missing closing quote");
894 return -1;
897 t->offset += i + 1;
898 t->line_pos += i + 1;
900 break;
901 case '(':
902 t->offset++;
903 t->in_brace++;
905 tid = TK_LPARAN;
907 buffer_copy_string_len(token, CONST_STR_LEN("("));
908 break;
909 case ')':
910 t->offset++;
911 t->in_brace--;
913 tid = TK_RPARAN;
915 buffer_copy_string_len(token, CONST_STR_LEN(")"));
916 break;
917 case '$':
918 t->offset++;
920 tid = TK_DOLLAR;
921 t->in_cond = 1;
922 t->in_key = 0;
924 buffer_copy_string_len(token, CONST_STR_LEN("$"));
926 break;
928 case '+':
929 if (t->input[t->offset + 1] == '=') {
930 t->offset += 2;
931 buffer_copy_string_len(token, CONST_STR_LEN("+="));
932 tid = TK_APPEND;
933 } else {
934 t->offset++;
935 tid = TK_PLUS;
936 buffer_copy_string_len(token, CONST_STR_LEN("+"));
938 break;
940 case '{':
941 t->offset++;
943 tid = TK_LCURLY;
945 buffer_copy_string_len(token, CONST_STR_LEN("{"));
947 break;
949 case '}':
950 t->offset++;
952 tid = TK_RCURLY;
954 buffer_copy_string_len(token, CONST_STR_LEN("}"));
956 break;
958 case '[':
959 t->offset++;
961 tid = TK_LBRACKET;
963 buffer_copy_string_len(token, CONST_STR_LEN("["));
965 break;
967 case ']':
968 t->offset++;
970 tid = TK_RBRACKET;
972 buffer_copy_string_len(token, CONST_STR_LEN("]"));
974 break;
975 case '#':
976 t->line_pos += config_skip_comment(t);
978 break;
979 default:
980 if (t->in_cond) {
981 for (i = 0; t->input[t->offset + i] &&
982 (isalpha((unsigned char)t->input[t->offset + i])
983 ); i++);
985 if (i && t->input[t->offset + i]) {
986 tid = TK_SRVVARNAME;
987 buffer_copy_string_len(token, t->input + t->offset, i);
989 t->offset += i;
990 t->line_pos += i;
991 } else {
992 /* ERROR */
993 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
994 "source:", t->source,
995 "line:", t->line, "pos:", t->line_pos,
996 "invalid character in condition");
997 return -1;
999 } else if (isdigit((unsigned char)c)) {
1000 /* take all digits */
1001 for (i = 0; t->input[t->offset + i] && isdigit((unsigned char)t->input[t->offset + i]); i++);
1003 /* was there it least a digit ? */
1004 if (i) {
1005 tid = TK_INTEGER;
1007 buffer_copy_string_len(token, t->input + t->offset, i);
1009 t->offset += i;
1010 t->line_pos += i;
1012 } else {
1013 /* the key might consist of [-.0-9a-z] */
1014 for (i = 0; t->input[t->offset + i] &&
1015 (isalnum((unsigned char)t->input[t->offset + i]) ||
1016 t->input[t->offset + i] == '.' ||
1017 t->input[t->offset + i] == '_' || /* for env.* */
1018 t->input[t->offset + i] == '-'
1019 ); i++);
1021 if (i && t->input[t->offset + i]) {
1022 buffer_copy_string_len(token, t->input + t->offset, i);
1024 if (strcmp(token->ptr, "include") == 0) {
1025 tid = TK_INCLUDE;
1026 } else if (strcmp(token->ptr, "include_shell") == 0) {
1027 tid = TK_INCLUDE_SHELL;
1028 } else if (strcmp(token->ptr, "global") == 0) {
1029 tid = TK_GLOBAL;
1030 } else if (strcmp(token->ptr, "else") == 0) {
1031 tid = TK_ELSE;
1032 } else {
1033 tid = TK_LKEY;
1036 t->offset += i;
1037 t->line_pos += i;
1038 } else {
1039 /* ERROR */
1040 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
1041 "source:", t->source,
1042 "line:", t->line, "pos:", t->line_pos,
1043 "invalid character in variable name");
1044 return -1;
1047 break;
1051 if (tid) {
1052 *token_id = tid;
1053 #if 0
1054 log_error_write(srv, __FILE__, __LINE__, "sbsdsdbdd",
1055 "source:", t->source,
1056 "line:", t->line, "pos:", t->line_pos,
1057 token, token->used - 1, tid);
1058 #endif
1060 return 1;
1061 } else if (t->offset < t->size) {
1062 fprintf(stderr, "%s.%d: %d, %s\n",
1063 __FILE__, __LINE__,
1064 tid, token->ptr);
1066 return 0;
1069 static int config_parse(server *srv, config_t *context, tokenizer_t *t) {
1070 void *pParser;
1071 int token_id;
1072 buffer *token, *lasttoken;
1073 int ret;
1075 pParser = configparserAlloc( malloc );
1076 force_assert(pParser);
1077 lasttoken = buffer_init();
1078 token = buffer_init();
1079 while((1 == (ret = config_tokenizer(srv, t, &token_id, token))) && context->ok) {
1080 buffer_copy_buffer(lasttoken, token);
1081 configparser(pParser, token_id, token, context);
1083 token = buffer_init();
1085 buffer_free(token);
1087 if (ret != -1 && context->ok) {
1088 /* add an EOL at EOF, better than say sorry */
1089 configparser(pParser, TK_EOL, buffer_init_string("(EOL)"), context);
1090 if (context->ok) {
1091 configparser(pParser, 0, NULL, context);
1094 configparserFree(pParser, free);
1096 if (ret == -1) {
1097 log_error_write(srv, __FILE__, __LINE__, "sb",
1098 "configfile parser failed at:", lasttoken);
1099 } else if (context->ok == 0) {
1100 log_error_write(srv, __FILE__, __LINE__, "sbsdsdsb",
1101 "source:", t->source,
1102 "line:", t->line, "pos:", t->line_pos,
1103 "parser failed somehow near here:", lasttoken);
1104 ret = -1;
1106 buffer_free(lasttoken);
1108 return ret == -1 ? -1 : 0;
1111 static int tokenizer_init(tokenizer_t *t, const buffer *source, const char *input, size_t size) {
1113 t->source = source;
1114 t->input = input;
1115 t->size = size;
1116 t->offset = 0;
1117 t->line = 1;
1118 t->line_pos = 1;
1120 t->in_key = 1;
1121 t->in_brace = 0;
1122 t->in_cond = 0;
1123 return 0;
1126 static int config_parse_file_stream(server *srv, config_t *context, const buffer *filename) {
1127 tokenizer_t t;
1128 stream s;
1129 int ret;
1131 if (0 != stream_open(&s, filename)) {
1132 log_error_write(srv, __FILE__, __LINE__, "sbss",
1133 "opening configfile ", filename, "failed:", strerror(errno));
1134 return -1;
1135 } else {
1136 tokenizer_init(&t, filename, s.start, s.size);
1137 ret = config_parse(srv, context, &t);
1140 stream_close(&s);
1141 return ret;
1144 int config_parse_file(server *srv, config_t *context, const char *fn) {
1145 buffer *filename;
1146 size_t i;
1147 int ret = -1;
1148 #ifdef GLOB_BRACE
1149 int flags = GLOB_BRACE;
1150 #else
1151 int flags = 0;
1152 #endif
1153 glob_t gl;
1155 if ((fn[0] == '/' || fn[0] == '\\') ||
1156 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\')) ||
1157 (fn[0] == '.' && fn[1] == '.' && (fn[2] == '/' || fn[2] == '\\'))) {
1158 filename = buffer_init_string(fn);
1159 } else {
1160 filename = buffer_init_buffer(context->basedir);
1161 buffer_append_string(filename, fn);
1164 switch (glob(filename->ptr, flags, NULL, &gl)) {
1165 case 0:
1166 for (i = 0; i < gl.gl_pathc; ++i) {
1167 buffer_copy_string(filename, gl.gl_pathv[i]);
1168 ret = config_parse_file_stream(srv, context, filename);
1169 if (0 != ret) break;
1171 globfree(&gl);
1172 break;
1173 case GLOB_NOMATCH:
1174 if (filename->ptr[strcspn(filename->ptr, "*?[]{}")] != '\0') { /*(contains glob metachars)*/
1175 ret = 0; /* not an error if no files match glob pattern */
1177 else {
1178 log_error_write(srv, __FILE__, __LINE__, "sb", "include file not found: ", filename);
1180 break;
1181 case GLOB_ABORTED:
1182 case GLOB_NOSPACE:
1183 log_error_write(srv, __FILE__, __LINE__, "sbss", "glob()", filename, "failed:", strerror(errno));
1184 break;
1187 buffer_free(filename);
1188 return ret;
1191 static char* getCWD(void) {
1192 char *s, *s1;
1193 size_t len;
1194 #ifdef PATH_MAX
1195 len = PATH_MAX;
1196 #else
1197 len = 4096;
1198 #endif
1200 s = malloc(len);
1201 if (!s) return NULL;
1202 while (NULL == getcwd(s, len)) {
1203 if (errno != ERANGE || SSIZE_MAX - len < len) {
1204 free(s);
1205 return NULL;
1207 len *= 2;
1208 s1 = realloc(s, len);
1209 if (!s1) {
1210 free(s);
1211 return NULL;
1213 s = s1;
1215 return s;
1218 int config_parse_cmd(server *srv, config_t *context, const char *cmd) {
1219 tokenizer_t t;
1220 int ret;
1221 buffer *source;
1222 buffer *out;
1223 char *oldpwd;
1225 if (NULL == (oldpwd = getCWD())) {
1226 log_error_write(srv, __FILE__, __LINE__, "s",
1227 "cannot get cwd", strerror(errno));
1228 return -1;
1231 if (!buffer_string_is_empty(context->basedir)) {
1232 if (0 != chdir(context->basedir->ptr)) {
1233 log_error_write(srv, __FILE__, __LINE__, "sbs",
1234 "cannot change directory to", context->basedir, strerror(errno));
1235 free(oldpwd);
1236 return -1;
1240 source = buffer_init_string(cmd);
1241 out = buffer_init();
1243 if (0 != proc_open_buffer(cmd, NULL, out, NULL)) {
1244 log_error_write(srv, __FILE__, __LINE__, "sbss",
1245 "opening", source, "failed:", strerror(errno));
1246 ret = -1;
1247 } else {
1248 tokenizer_init(&t, source, CONST_BUF_LEN(out));
1249 ret = config_parse(srv, context, &t);
1252 buffer_free(source);
1253 buffer_free(out);
1254 if (0 != chdir(oldpwd)) {
1255 log_error_write(srv, __FILE__, __LINE__, "sss",
1256 "cannot change directory to", oldpwd, strerror(errno));
1257 free(oldpwd);
1258 return -1;
1260 free(oldpwd);
1261 return ret;
1264 static void context_init(server *srv, config_t *context) {
1265 context->srv = srv;
1266 context->ok = 1;
1267 vector_config_weak_init(&context->configs_stack);
1268 context->basedir = buffer_init();
1271 static void context_free(config_t *context) {
1272 vector_config_weak_clear(&context->configs_stack);
1273 buffer_free(context->basedir);
1276 int config_read(server *srv, const char *fn) {
1277 config_t context;
1278 data_config *dc;
1279 data_integer *dpid;
1280 data_string *dcwd;
1281 int ret;
1282 char *pos;
1283 buffer *filename;
1285 context_init(srv, &context);
1286 context.all_configs = srv->config_context;
1288 #ifdef __WIN32
1289 pos = strrchr(fn, '\\');
1290 #else
1291 pos = strrchr(fn, '/');
1292 #endif
1293 if (pos) {
1294 buffer_copy_string_len(context.basedir, fn, pos - fn + 1);
1297 dc = data_config_init();
1298 buffer_copy_string_len(dc->key, CONST_STR_LEN("global"));
1300 force_assert(context.all_configs->used == 0);
1301 dc->context_ndx = context.all_configs->used;
1302 array_insert_unique(context.all_configs, (data_unset *)dc);
1303 context.current = dc;
1305 /* default context */
1306 dpid = data_integer_init();
1307 dpid->value = getpid();
1308 buffer_copy_string_len(dpid->key, CONST_STR_LEN("var.PID"));
1309 array_insert_unique(dc->value, (data_unset *)dpid);
1311 dcwd = data_string_init();
1312 buffer_string_prepare_copy(dcwd->value, 1023);
1313 if (NULL != getcwd(dcwd->value->ptr, dcwd->value->size - 1)) {
1314 buffer_commit(dcwd->value, strlen(dcwd->value->ptr));
1315 buffer_copy_string_len(dcwd->key, CONST_STR_LEN("var.CWD"));
1316 array_insert_unique(dc->value, (data_unset *)dcwd);
1317 } else {
1318 dcwd->free((data_unset*) dcwd);
1321 filename = buffer_init_string(fn);
1322 ret = config_parse_file_stream(srv, &context, filename);
1323 buffer_free(filename);
1325 /* remains nothing if parser is ok */
1326 force_assert(!(0 == ret && context.ok && 0 != context.configs_stack.used));
1327 context_free(&context);
1329 if (0 != ret) {
1330 return ret;
1333 if (0 != config_insert(srv)) {
1334 return -1;
1337 return 0;
1340 int config_set_defaults(server *srv) {
1341 size_t i;
1342 specific_config *s = srv->config_storage[0];
1343 struct stat st1, st2;
1345 struct ev_map { fdevent_handler_t et; const char *name; } event_handlers[] =
1347 /* - epoll is most reliable
1348 * - select works everywhere
1350 #ifdef USE_LINUX_EPOLL
1351 { FDEVENT_HANDLER_LINUX_SYSEPOLL, "linux-sysepoll" },
1352 #endif
1353 #ifdef USE_POLL
1354 { FDEVENT_HANDLER_POLL, "poll" },
1355 #endif
1356 #ifdef USE_SELECT
1357 { FDEVENT_HANDLER_SELECT, "select" },
1358 #endif
1359 #ifdef USE_LIBEV
1360 { FDEVENT_HANDLER_LIBEV, "libev" },
1361 #endif
1362 #ifdef USE_SOLARIS_DEVPOLL
1363 { FDEVENT_HANDLER_SOLARIS_DEVPOLL,"solaris-devpoll" },
1364 #endif
1365 #ifdef USE_SOLARIS_PORT
1366 { FDEVENT_HANDLER_SOLARIS_PORT, "solaris-eventports" },
1367 #endif
1368 #ifdef USE_FREEBSD_KQUEUE
1369 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "freebsd-kqueue" },
1370 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "kqueue" },
1371 #endif
1372 { FDEVENT_HANDLER_UNSET, NULL }
1375 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1376 if (-1 == stat(srv->srvconf.changeroot->ptr, &st1)) {
1377 log_error_write(srv, __FILE__, __LINE__, "sb",
1378 "server.chroot doesn't exist:", srv->srvconf.changeroot);
1379 return -1;
1381 if (!S_ISDIR(st1.st_mode)) {
1382 log_error_write(srv, __FILE__, __LINE__, "sb",
1383 "server.chroot isn't a directory:", srv->srvconf.changeroot);
1384 return -1;
1388 if (srv->srvconf.upload_tempdirs->used) {
1389 buffer * const b = srv->tmp_buf;
1390 size_t len;
1391 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1392 buffer_copy_buffer(b, srv->srvconf.changeroot);
1393 buffer_append_slash(b);
1394 } else {
1395 buffer_reset(b);
1397 len = buffer_string_length(b);
1399 for (i = 0; i < srv->srvconf.upload_tempdirs->used; ++i) {
1400 const data_string * const ds = (data_string *)srv->srvconf.upload_tempdirs->data[i];
1401 buffer_string_set_length(b, len); /*(truncate)*/
1402 buffer_append_string_buffer(b, ds->value);
1403 if (-1 == stat(b->ptr, &st1)) {
1404 log_error_write(srv, __FILE__, __LINE__, "sb",
1405 "server.upload-dirs doesn't exist:", b);
1406 } else if (!S_ISDIR(st1.st_mode)) {
1407 log_error_write(srv, __FILE__, __LINE__, "sb",
1408 "server.upload-dirs isn't a directory:", b);
1413 if (buffer_string_is_empty(s->document_root)) {
1414 log_error_write(srv, __FILE__, __LINE__, "s",
1415 "a default document-root has to be set");
1417 return -1;
1420 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1422 buffer_to_lower(srv->tmp_buf);
1424 if (2 == s->force_lowercase_filenames) { /* user didn't configure it in global section? */
1425 s->force_lowercase_filenames = 0; /* default to 0 */
1427 if (0 == stat(srv->tmp_buf->ptr, &st1)) {
1428 int is_lower = 0;
1430 is_lower = buffer_is_equal(srv->tmp_buf, s->document_root);
1432 /* lower-case existed, check upper-case */
1433 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1435 buffer_to_upper(srv->tmp_buf);
1437 /* we have to handle the special case that upper and lower-casing results in the same filename
1438 * as in server.document-root = "/" or "/12345/" */
1440 if (is_lower && buffer_is_equal(srv->tmp_buf, s->document_root)) {
1441 /* lower-casing and upper-casing didn't result in
1442 * an other filename, no need to stat(),
1443 * just assume it is case-sensitive. */
1445 s->force_lowercase_filenames = 0;
1446 } else if (0 == stat(srv->tmp_buf->ptr, &st2)) {
1448 /* upper case exists too, doesn't the FS handle this ? */
1450 /* upper and lower have the same inode -> case-insensitve FS */
1452 if (st1.st_ino == st2.st_ino) {
1453 /* upper and lower have the same inode -> case-insensitve FS */
1455 s->force_lowercase_filenames = 1;
1461 if (srv->srvconf.port == 0) {
1462 srv->srvconf.port = s->ssl_enabled ? 443 : 80;
1465 if (buffer_string_is_empty(srv->srvconf.event_handler)) {
1466 /* choose a good default
1468 * the event_handler list is sorted by 'goodness'
1469 * taking the first available should be the best solution
1471 srv->event_handler = event_handlers[0].et;
1473 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1474 log_error_write(srv, __FILE__, __LINE__, "s",
1475 "sorry, there is no event handler for this system");
1477 return -1;
1479 } else {
1481 * User override
1484 for (i = 0; event_handlers[i].name; i++) {
1485 if (0 == strcmp(event_handlers[i].name, srv->srvconf.event_handler->ptr)) {
1486 srv->event_handler = event_handlers[i].et;
1487 break;
1491 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1492 log_error_write(srv, __FILE__, __LINE__, "sb",
1493 "the selected event-handler in unknown or not supported:",
1494 srv->srvconf.event_handler );
1496 return -1;
1500 if (s->ssl_enabled) {
1501 if (buffer_string_is_empty(s->ssl_pemfile)) {
1502 /* PEM file is require */
1504 log_error_write(srv, __FILE__, __LINE__, "s",
1505 "ssl.pemfile has to be set");
1506 return -1;
1509 #ifndef USE_OPENSSL
1510 log_error_write(srv, __FILE__, __LINE__, "s",
1511 "ssl support is missing, recompile with --with-openssl");
1513 return -1;
1514 #endif
1517 return 0;