[config] opts for http header parsing strictness (fixes #551, fixes #1086, fixes...
[lighttpd.git] / src / configfile.c
blob11f1cc7fe36c6eb1e825e4153860f37175f8ee7e
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 */
122 { "server.host",
123 "use server.bind instead",
124 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
125 { "server.docroot",
126 "use server.document-root instead",
127 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
128 { "server.virtual-root",
129 "load mod_simple_vhost and use simple-vhost.server-root instead",
130 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
131 { "server.virtual-default-host",
132 "load mod_simple_vhost and use simple-vhost.default-host instead",
133 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
134 { "server.virtual-docroot",
135 "load mod_simple_vhost and use simple-vhost.document-root instead",
136 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
137 { "server.userid",
138 "use server.username instead",
139 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
140 { "server.groupid",
141 "use server.groupname instead",
142 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
143 { "server.use-keep-alive",
144 "use server.max-keep-alive-requests = 0 instead",
145 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
146 { "server.force-lower-case-files",
147 "use server.force-lowercase-filenames instead",
148 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
150 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
153 /* all T_CONFIG_SCOPE_SERVER options */
154 cv[0].destination = srv->srvconf.bindhost;
155 cv[1].destination = srv->srvconf.errorlog_file;
156 cv[3].destination = srv->srvconf.changeroot;
157 cv[4].destination = srv->srvconf.username;
158 cv[5].destination = srv->srvconf.groupname;
159 cv[6].destination = &(srv->srvconf.port);
160 cv[9].destination = srv->srvconf.modules;
162 cv[10].destination = srv->srvconf.event_handler;
163 cv[11].destination = srv->srvconf.pid_file;
164 cv[12].destination = &(srv->srvconf.max_request_size);
165 cv[13].destination = &(srv->srvconf.max_worker);
167 cv[23].destination = &(srv->srvconf.max_fds);
169 cv[37].destination = &(srv->srvconf.log_request_header_on_error);
170 cv[38].destination = &(srv->srvconf.log_state_handling);
172 cv[40].destination = &(srv->srvconf.errorlog_use_syslog);
173 stat_cache_string = buffer_init();
174 cv[42].destination = stat_cache_string;
175 cv[43].destination = &(srv->srvconf.max_conns);
176 cv[44].destination = srv->srvconf.network_backend;
177 cv[45].destination = srv->srvconf.upload_tempdirs;
178 cv[46].destination = &(srv->srvconf.enable_cores);
180 cv[52].destination = &(srv->srvconf.reject_expect_100_with_417);
181 cv[55].destination = srv->srvconf.breakagelog_file;
183 cv[68].destination = &(srv->srvconf.upload_temp_file_size);
184 cv[69].destination = srv->srvconf.xattr_name;
185 cv[72].destination = &(srv->srvconf.http_header_strict);
186 cv[73].destination = &(srv->srvconf.http_host_strict);
187 cv[74].destination = &(srv->srvconf.http_host_normalize);
189 srv->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
191 force_assert(srv->config_storage);
193 for (i = 0; i < srv->config_context->used; i++) {
194 data_config const* config = (data_config const*)srv->config_context->data[i];
195 specific_config *s;
197 s = calloc(1, sizeof(specific_config));
198 force_assert(s);
199 s->document_root = buffer_init();
200 s->mimetypes = array_init();
201 s->server_name = buffer_init();
202 s->ssl_pemfile = buffer_init();
203 s->ssl_ca_file = buffer_init();
204 s->error_handler = buffer_init();
205 s->error_handler_404 = buffer_init();
206 s->server_tag = buffer_init();
207 s->ssl_cipher_list = buffer_init();
208 s->ssl_dh_file = buffer_init();
209 s->ssl_ec_curve = buffer_init();
210 s->errorfile_prefix = buffer_init();
211 s->max_keep_alive_requests = 16;
212 s->max_keep_alive_idle = 5;
213 s->max_read_idle = 60;
214 s->max_write_idle = 360;
215 s->use_xattr = 0;
216 s->ssl_enabled = 0;
217 s->ssl_honor_cipher_order = 1;
218 s->ssl_empty_fragments = 0;
219 s->ssl_use_sslv2 = 0;
220 s->ssl_use_sslv3 = 0;
221 s->use_ipv6 = 0;
222 s->set_v6only = 1;
223 s->defer_accept = 0;
224 #ifdef HAVE_LSTAT
225 s->follow_symlink = 1;
226 #endif
227 s->kbytes_per_second = 0;
228 s->allow_http11 = 1;
229 s->etag_use_inode = 1;
230 s->etag_use_mtime = 1;
231 s->etag_use_size = 1;
232 s->range_requests = 1;
233 s->force_lowercase_filenames = (i == 0) ? 2 : 0; /* we wan't to detect later if user changed this for global section */
234 s->global_kbytes_per_second = 0;
235 s->global_bytes_per_second_cnt = 0;
236 s->global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
237 s->ssl_verifyclient = 0;
238 s->ssl_verifyclient_enforce = 1;
239 s->ssl_verifyclient_username = buffer_init();
240 s->ssl_verifyclient_depth = 9;
241 s->ssl_verifyclient_export_cert = 0;
242 s->ssl_disable_client_renegotiation = 1;
243 s->listen_backlog = (0 == i ? 1024 : srv->config_storage[0]->listen_backlog);
245 /* all T_CONFIG_SCOPE_CONNECTION options */
246 cv[2].destination = s->errorfile_prefix;
247 cv[7].destination = s->server_tag;
248 cv[8].destination = &(s->use_ipv6);
250 cv[14].destination = s->document_root;
251 cv[15].destination = &(s->force_lowercase_filenames);
252 cv[16].destination = &(s->log_condition_handling);
253 cv[17].destination = &(s->max_keep_alive_requests);
254 cv[18].destination = s->server_name;
255 cv[19].destination = &(s->max_keep_alive_idle);
257 cv[20].destination = &(s->max_read_idle);
258 cv[21].destination = &(s->max_write_idle);
259 cv[22].destination = s->error_handler;
260 #ifdef HAVE_LSTAT
261 cv[24].destination = &(s->follow_symlink);
262 #endif
263 cv[25].destination = &(s->global_kbytes_per_second);
264 cv[26].destination = &(s->kbytes_per_second);
265 cv[27].destination = &(s->use_xattr);
266 cv[28].destination = s->mimetypes;
267 cv[29].destination = s->ssl_pemfile;
269 cv[30].destination = &(s->ssl_enabled);
270 cv[31].destination = &(s->log_file_not_found);
271 cv[32].destination = &(s->log_request_handling);
272 cv[33].destination = &(s->log_response_header);
273 cv[34].destination = &(s->log_request_header);
274 cv[35].destination = &(s->log_ssl_noise);
275 cv[36].destination = &(s->allow_http11);
276 cv[39].destination = s->ssl_ca_file;
278 cv[41].destination = &(s->range_requests);
279 cv[47].destination = s->ssl_cipher_list;
280 cv[48].destination = &(s->ssl_use_sslv2);
281 cv[49].destination = &(s->etag_use_inode);
283 cv[50].destination = &(s->etag_use_mtime);
284 cv[51].destination = &(s->etag_use_size);
285 cv[53].destination = &(s->log_timeouts);
286 cv[54].destination = &(s->defer_accept);
287 cv[56].destination = &(s->ssl_verifyclient);
288 cv[57].destination = &(s->ssl_verifyclient_enforce);
289 cv[58].destination = &(s->ssl_verifyclient_depth);
290 cv[59].destination = s->ssl_verifyclient_username;
292 cv[60].destination = &(s->ssl_verifyclient_export_cert);
293 cv[61].destination = &(s->set_v6only);
294 cv[62].destination = &(s->ssl_use_sslv3);
295 cv[63].destination = s->ssl_dh_file;
296 cv[64].destination = s->ssl_ec_curve;
297 cv[65].destination = &(s->ssl_disable_client_renegotiation);
298 cv[66].destination = &(s->ssl_honor_cipher_order);
299 cv[67].destination = &(s->ssl_empty_fragments);
300 cv[70].destination = &(s->listen_backlog);
301 cv[71].destination = s->error_handler_404;
303 srv->config_storage[i] = s;
305 if (0 != (ret = config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION))) {
306 break;
311 specific_config *s = srv->config_storage[0];
312 s->http_parseopts= /*(global, but stored in con->conf.http_parseopts)*/
313 (srv->srvconf.http_header_strict ?(HTTP_PARSEOPT_HEADER_STRICT) :0)
314 |(srv->srvconf.http_host_strict ?(HTTP_PARSEOPT_HOST_STRICT
315 |HTTP_PARSEOPT_HOST_NORMALIZE):0)
316 |(srv->srvconf.http_host_normalize ?(HTTP_PARSEOPT_HOST_NORMALIZE):0);
319 if (buffer_string_is_empty(stat_cache_string)) {
320 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
321 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("simple"))) {
322 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
323 #ifdef HAVE_FAM_H
324 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("fam"))) {
325 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_FAM;
326 #endif
327 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("disable"))) {
328 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_NONE;
329 } else {
330 log_error_write(srv, __FILE__, __LINE__, "sb",
331 "server.stat-cache-engine can be one of \"disable\", \"simple\","
332 #ifdef HAVE_FAM_H
333 " \"fam\","
334 #endif
335 " but not:", stat_cache_string);
336 ret = HANDLER_ERROR;
339 buffer_free(stat_cache_string);
342 data_string *ds;
343 int prepend_mod_indexfile = 1;
344 int append_mod_dirlisting = 1;
345 int append_mod_staticfile = 1;
347 /* prepend default modules */
348 for (i = 0; i < srv->srvconf.modules->used; i++) {
349 ds = (data_string *)srv->srvconf.modules->data[i];
351 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_indexfile"))) {
352 prepend_mod_indexfile = 0;
355 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_staticfile"))) {
356 append_mod_staticfile = 0;
359 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_dirlisting"))) {
360 append_mod_dirlisting = 0;
363 if (0 == prepend_mod_indexfile &&
364 0 == append_mod_dirlisting &&
365 0 == append_mod_staticfile) {
366 break;
370 if (prepend_mod_indexfile) {
371 /* mod_indexfile has to be loaded before mod_fastcgi and friends */
372 array *modules = array_init();
374 ds = data_string_init();
375 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_indexfile"));
376 array_insert_unique(modules, (data_unset *)ds);
378 for (i = 0; i < srv->srvconf.modules->used; i++) {
379 data_unset *du = srv->srvconf.modules->data[i];
380 array_insert_unique(modules, du->copy(du));
383 array_free(srv->srvconf.modules);
384 srv->srvconf.modules = modules;
387 /* append default modules */
388 if (append_mod_dirlisting) {
389 ds = data_string_init();
390 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_dirlisting"));
391 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
394 if (append_mod_staticfile) {
395 ds = data_string_init();
396 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_staticfile"));
397 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
401 return ret;
406 #define PATCH(x) con->conf.x = s->x
407 int config_setup_connection(server *srv, connection *con) {
408 specific_config *s = srv->config_storage[0];
410 PATCH(http_parseopts);
412 PATCH(allow_http11);
413 PATCH(mimetypes);
414 PATCH(document_root);
415 PATCH(max_keep_alive_requests);
416 PATCH(max_keep_alive_idle);
417 PATCH(max_read_idle);
418 PATCH(max_write_idle);
419 PATCH(use_xattr);
420 PATCH(error_handler);
421 PATCH(error_handler_404);
422 PATCH(errorfile_prefix);
423 #ifdef HAVE_LSTAT
424 PATCH(follow_symlink);
425 #endif
426 PATCH(server_tag);
427 PATCH(kbytes_per_second);
428 PATCH(global_kbytes_per_second);
429 PATCH(global_bytes_per_second_cnt);
431 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
432 buffer_copy_buffer(con->server_name, s->server_name);
434 PATCH(log_request_header);
435 PATCH(log_response_header);
436 PATCH(log_request_handling);
437 PATCH(log_condition_handling);
438 PATCH(log_file_not_found);
439 PATCH(log_ssl_noise);
440 PATCH(log_timeouts);
442 PATCH(range_requests);
443 PATCH(force_lowercase_filenames);
444 /*PATCH(listen_backlog);*//*(not necessary; used only at startup)*/
445 PATCH(ssl_enabled);
447 PATCH(ssl_pemfile);
448 #ifdef USE_OPENSSL
449 PATCH(ssl_pemfile_x509);
450 PATCH(ssl_pemfile_pkey);
451 #endif
452 PATCH(ssl_ca_file);
453 #ifdef USE_OPENSSL
454 PATCH(ssl_ca_file_cert_names);
455 #endif
456 PATCH(ssl_cipher_list);
457 PATCH(ssl_dh_file);
458 PATCH(ssl_ec_curve);
459 PATCH(ssl_honor_cipher_order);
460 PATCH(ssl_empty_fragments);
461 PATCH(ssl_use_sslv2);
462 PATCH(ssl_use_sslv3);
463 PATCH(etag_use_inode);
464 PATCH(etag_use_mtime);
465 PATCH(etag_use_size);
467 PATCH(ssl_verifyclient);
468 PATCH(ssl_verifyclient_enforce);
469 PATCH(ssl_verifyclient_depth);
470 PATCH(ssl_verifyclient_username);
471 PATCH(ssl_verifyclient_export_cert);
472 PATCH(ssl_disable_client_renegotiation);
474 return 0;
477 int config_patch_connection(server *srv, connection *con) {
478 size_t i, j;
480 /* skip the first, the global context */
481 for (i = 1; i < srv->config_context->used; i++) {
482 data_config *dc = (data_config *)srv->config_context->data[i];
483 specific_config *s = srv->config_storage[i];
485 /* condition didn't match */
486 if (!config_check_cond(srv, con, dc)) continue;
488 /* merge config */
489 for (j = 0; j < dc->value->used; j++) {
490 data_unset *du = dc->value->data[j];
492 if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.document-root"))) {
493 PATCH(document_root);
494 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.range-requests"))) {
495 PATCH(range_requests);
496 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler"))) {
497 PATCH(error_handler);
498 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler-404"))) {
499 PATCH(error_handler_404);
500 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.errorfile-prefix"))) {
501 PATCH(errorfile_prefix);
502 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.assign"))) {
503 PATCH(mimetypes);
504 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-requests"))) {
505 PATCH(max_keep_alive_requests);
506 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-idle"))) {
507 PATCH(max_keep_alive_idle);
508 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-write-idle"))) {
509 PATCH(max_write_idle);
510 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-read-idle"))) {
511 PATCH(max_read_idle);
512 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.use-xattr"))) {
513 PATCH(use_xattr);
514 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-inode"))) {
515 PATCH(etag_use_inode);
516 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-mtime"))) {
517 PATCH(etag_use_mtime);
518 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-size"))) {
519 PATCH(etag_use_size);
520 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.pemfile"))) {
521 PATCH(ssl_pemfile);
522 #ifdef USE_OPENSSL
523 PATCH(ssl_pemfile_x509);
524 PATCH(ssl_pemfile_pkey);
525 #endif
526 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ca-file"))) {
527 PATCH(ssl_ca_file);
528 #ifdef USE_OPENSSL
529 PATCH(ssl_ca_file_cert_names);
530 #endif
531 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.honor-cipher-order"))) {
532 PATCH(ssl_honor_cipher_order);
533 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.empty-fragments"))) {
534 PATCH(ssl_empty_fragments);
535 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv2"))) {
536 PATCH(ssl_use_sslv2);
537 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv3"))) {
538 PATCH(ssl_use_sslv3);
539 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.cipher-list"))) {
540 PATCH(ssl_cipher_list);
541 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.engine"))) {
542 PATCH(ssl_enabled);
543 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.dh-file"))) {
544 PATCH(ssl_dh_file);
545 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ec-curve"))) {
546 PATCH(ssl_ec_curve);
547 #ifdef HAVE_LSTAT
548 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.follow-symlink"))) {
549 PATCH(follow_symlink);
550 #endif
551 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.name"))) {
552 buffer_copy_buffer(con->server_name, s->server_name);
553 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.tag"))) {
554 PATCH(server_tag);
555 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("connection.kbytes-per-second"))) {
556 PATCH(kbytes_per_second);
557 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-handling"))) {
558 PATCH(log_request_handling);
559 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-header"))) {
560 PATCH(log_request_header);
561 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-response-header"))) {
562 PATCH(log_response_header);
563 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-condition-handling"))) {
564 PATCH(log_condition_handling);
565 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-file-not-found"))) {
566 PATCH(log_file_not_found);
567 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-ssl-noise"))) {
568 PATCH(log_ssl_noise);
569 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-timeouts"))) {
570 PATCH(log_timeouts);
571 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.protocol-http11"))) {
572 PATCH(allow_http11);
573 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.force-lowercase-filenames"))) {
574 PATCH(force_lowercase_filenames);
575 #if 0 /*(not necessary; used only at startup)*/
576 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.listen-backlog"))) {
577 PATCH(listen_backlog);
578 #endif
579 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.kbytes-per-second"))) {
580 PATCH(global_kbytes_per_second);
581 PATCH(global_bytes_per_second_cnt);
582 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
583 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.activate"))) {
584 PATCH(ssl_verifyclient);
585 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.enforce"))) {
586 PATCH(ssl_verifyclient_enforce);
587 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.depth"))) {
588 PATCH(ssl_verifyclient_depth);
589 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.username"))) {
590 PATCH(ssl_verifyclient_username);
591 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.exportcert"))) {
592 PATCH(ssl_verifyclient_export_cert);
593 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.disable-client-renegotiation"))) {
594 PATCH(ssl_disable_client_renegotiation);
599 con->etag_flags = (con->conf.etag_use_mtime ? ETAG_USE_MTIME : 0) |
600 (con->conf.etag_use_inode ? ETAG_USE_INODE : 0) |
601 (con->conf.etag_use_size ? ETAG_USE_SIZE : 0);
603 return 0;
605 #undef PATCH
607 typedef struct {
608 int foo;
609 int bar;
611 const buffer *source;
612 const char *input;
613 size_t offset;
614 size_t size;
616 int line_pos;
617 int line;
619 int in_key;
620 int in_brace;
621 int in_cond;
622 } tokenizer_t;
624 #if 0
625 static int tokenizer_open(server *srv, tokenizer_t *t, buffer *basedir, const char *fn) {
626 if (buffer_string_is_empty(basedir) ||
627 (fn[0] == '/' || fn[0] == '\\') ||
628 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\'))) {
629 t->file = buffer_init_string(fn);
630 } else {
631 t->file = buffer_init_buffer(basedir);
632 buffer_append_string(t->file, fn);
635 if (0 != stream_open(&(t->s), t->file)) {
636 log_error_write(srv, __FILE__, __LINE__, "sbss",
637 "opening configfile ", t->file, "failed:", strerror(errno));
638 buffer_free(t->file);
639 return -1;
642 t->input = t->s.start;
643 t->offset = 0;
644 t->size = t->s.size;
645 t->line = 1;
646 t->line_pos = 1;
648 t->in_key = 1;
649 t->in_brace = 0;
650 t->in_cond = 0;
651 return 0;
654 static int tokenizer_close(server *srv, tokenizer_t *t) {
655 UNUSED(srv);
657 buffer_free(t->file);
658 return stream_close(&(t->s));
660 #endif
661 static int config_skip_newline(tokenizer_t *t) {
662 int skipped = 1;
663 force_assert(t->input[t->offset] == '\r' || t->input[t->offset] == '\n');
664 if (t->input[t->offset] == '\r' && t->input[t->offset + 1] == '\n') {
665 skipped ++;
666 t->offset ++;
668 t->offset ++;
669 return skipped;
672 static int config_skip_comment(tokenizer_t *t) {
673 int i;
674 force_assert(t->input[t->offset] == '#');
675 for (i = 1; t->input[t->offset + i] &&
676 (t->input[t->offset + i] != '\n' && t->input[t->offset + i] != '\r');
677 i++);
678 t->offset += i;
679 return i;
682 static int config_tokenizer(server *srv, tokenizer_t *t, int *token_id, buffer *token) {
683 int tid = 0;
684 size_t i;
686 for (tid = 0; tid == 0 && t->offset < t->size && t->input[t->offset] ; ) {
687 char c = t->input[t->offset];
688 const char *start = NULL;
690 switch (c) {
691 case '=':
692 if (t->in_brace) {
693 if (t->input[t->offset + 1] == '>') {
694 t->offset += 2;
696 buffer_copy_string_len(token, CONST_STR_LEN("=>"));
698 tid = TK_ARRAY_ASSIGN;
699 } else {
700 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
701 "source:", t->source,
702 "line:", t->line, "pos:", t->line_pos,
703 "use => for assignments in arrays");
704 return -1;
706 } else if (t->in_cond) {
707 if (t->input[t->offset + 1] == '=') {
708 t->offset += 2;
710 buffer_copy_string_len(token, CONST_STR_LEN("=="));
712 tid = TK_EQ;
713 } else if (t->input[t->offset + 1] == '~') {
714 t->offset += 2;
716 buffer_copy_string_len(token, CONST_STR_LEN("=~"));
718 tid = TK_MATCH;
719 } else {
720 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
721 "source:", t->source,
722 "line:", t->line, "pos:", t->line_pos,
723 "only =~ and == are allowed in the condition");
724 return -1;
726 t->in_key = 1;
727 t->in_cond = 0;
728 } else if (t->in_key) {
729 tid = TK_ASSIGN;
731 buffer_copy_string_len(token, t->input + t->offset, 1);
733 t->offset++;
734 t->line_pos++;
735 } else {
736 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
737 "source:", t->source,
738 "line:", t->line, "pos:", t->line_pos,
739 "unexpected equal-sign: =");
740 return -1;
743 break;
744 case '!':
745 if (t->in_cond) {
746 if (t->input[t->offset + 1] == '=') {
747 t->offset += 2;
749 buffer_copy_string_len(token, CONST_STR_LEN("!="));
751 tid = TK_NE;
752 } else if (t->input[t->offset + 1] == '~') {
753 t->offset += 2;
755 buffer_copy_string_len(token, CONST_STR_LEN("!~"));
757 tid = TK_NOMATCH;
758 } else {
759 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
760 "source:", t->source,
761 "line:", t->line, "pos:", t->line_pos,
762 "only !~ and != are allowed in the condition");
763 return -1;
765 t->in_key = 1;
766 t->in_cond = 0;
767 } else {
768 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
769 "source:", t->source,
770 "line:", t->line, "pos:", t->line_pos,
771 "unexpected exclamation-marks: !");
772 return -1;
775 break;
776 case '\t':
777 case ' ':
778 t->offset++;
779 t->line_pos++;
780 break;
781 case '\n':
782 case '\r':
783 if (t->in_brace == 0) {
784 int done = 0;
785 while (!done && t->offset < t->size) {
786 switch (t->input[t->offset]) {
787 case '\r':
788 case '\n':
789 config_skip_newline(t);
790 t->line_pos = 1;
791 t->line++;
792 break;
794 case '#':
795 t->line_pos += config_skip_comment(t);
796 break;
798 case '\t':
799 case ' ':
800 t->offset++;
801 t->line_pos++;
802 break;
804 default:
805 done = 1;
808 t->in_key = 1;
809 tid = TK_EOL;
810 buffer_copy_string_len(token, CONST_STR_LEN("(EOL)"));
811 } else {
812 config_skip_newline(t);
813 t->line_pos = 1;
814 t->line++;
816 break;
817 case ',':
818 if (t->in_brace > 0) {
819 tid = TK_COMMA;
821 buffer_copy_string_len(token, CONST_STR_LEN("(COMMA)"));
824 t->offset++;
825 t->line_pos++;
826 break;
827 case '"':
828 /* search for the terminating " */
829 start = t->input + t->offset + 1;
830 buffer_copy_string_len(token, CONST_STR_LEN(""));
832 for (i = 1; t->input[t->offset + i]; i++) {
833 if (t->input[t->offset + i] == '\\' &&
834 t->input[t->offset + i + 1] == '"') {
836 buffer_append_string_len(token, start, t->input + t->offset + i - start);
838 start = t->input + t->offset + i + 1;
840 /* skip the " */
841 i++;
842 continue;
846 if (t->input[t->offset + i] == '"') {
847 tid = TK_STRING;
849 buffer_append_string_len(token, start, t->input + t->offset + i - start);
851 break;
855 if (t->input[t->offset + i] == '\0') {
856 /* ERROR */
858 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
859 "source:", t->source,
860 "line:", t->line, "pos:", t->line_pos,
861 "missing closing quote");
863 return -1;
866 t->offset += i + 1;
867 t->line_pos += i + 1;
869 break;
870 case '(':
871 t->offset++;
872 t->in_brace++;
874 tid = TK_LPARAN;
876 buffer_copy_string_len(token, CONST_STR_LEN("("));
877 break;
878 case ')':
879 t->offset++;
880 t->in_brace--;
882 tid = TK_RPARAN;
884 buffer_copy_string_len(token, CONST_STR_LEN(")"));
885 break;
886 case '$':
887 t->offset++;
889 tid = TK_DOLLAR;
890 t->in_cond = 1;
891 t->in_key = 0;
893 buffer_copy_string_len(token, CONST_STR_LEN("$"));
895 break;
897 case '+':
898 if (t->input[t->offset + 1] == '=') {
899 t->offset += 2;
900 buffer_copy_string_len(token, CONST_STR_LEN("+="));
901 tid = TK_APPEND;
902 } else {
903 t->offset++;
904 tid = TK_PLUS;
905 buffer_copy_string_len(token, CONST_STR_LEN("+"));
907 break;
909 case '{':
910 t->offset++;
912 tid = TK_LCURLY;
914 buffer_copy_string_len(token, CONST_STR_LEN("{"));
916 break;
918 case '}':
919 t->offset++;
921 tid = TK_RCURLY;
923 buffer_copy_string_len(token, CONST_STR_LEN("}"));
925 break;
927 case '[':
928 t->offset++;
930 tid = TK_LBRACKET;
932 buffer_copy_string_len(token, CONST_STR_LEN("["));
934 break;
936 case ']':
937 t->offset++;
939 tid = TK_RBRACKET;
941 buffer_copy_string_len(token, CONST_STR_LEN("]"));
943 break;
944 case '#':
945 t->line_pos += config_skip_comment(t);
947 break;
948 default:
949 if (t->in_cond) {
950 for (i = 0; t->input[t->offset + i] &&
951 (isalpha((unsigned char)t->input[t->offset + i])
952 ); i++);
954 if (i && t->input[t->offset + i]) {
955 tid = TK_SRVVARNAME;
956 buffer_copy_string_len(token, t->input + t->offset, i);
958 t->offset += i;
959 t->line_pos += i;
960 } else {
961 /* ERROR */
962 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
963 "source:", t->source,
964 "line:", t->line, "pos:", t->line_pos,
965 "invalid character in condition");
966 return -1;
968 } else if (isdigit((unsigned char)c)) {
969 /* take all digits */
970 for (i = 0; t->input[t->offset + i] && isdigit((unsigned char)t->input[t->offset + i]); i++);
972 /* was there it least a digit ? */
973 if (i) {
974 tid = TK_INTEGER;
976 buffer_copy_string_len(token, t->input + t->offset, i);
978 t->offset += i;
979 t->line_pos += i;
981 } else {
982 /* the key might consist of [-.0-9a-z] */
983 for (i = 0; t->input[t->offset + i] &&
984 (isalnum((unsigned char)t->input[t->offset + i]) ||
985 t->input[t->offset + i] == '.' ||
986 t->input[t->offset + i] == '_' || /* for env.* */
987 t->input[t->offset + i] == '-'
988 ); i++);
990 if (i && t->input[t->offset + i]) {
991 buffer_copy_string_len(token, t->input + t->offset, i);
993 if (strcmp(token->ptr, "include") == 0) {
994 tid = TK_INCLUDE;
995 } else if (strcmp(token->ptr, "include_shell") == 0) {
996 tid = TK_INCLUDE_SHELL;
997 } else if (strcmp(token->ptr, "global") == 0) {
998 tid = TK_GLOBAL;
999 } else if (strcmp(token->ptr, "else") == 0) {
1000 tid = TK_ELSE;
1001 } else {
1002 tid = TK_LKEY;
1005 t->offset += i;
1006 t->line_pos += i;
1007 } else {
1008 /* ERROR */
1009 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
1010 "source:", t->source,
1011 "line:", t->line, "pos:", t->line_pos,
1012 "invalid character in variable name");
1013 return -1;
1016 break;
1020 if (tid) {
1021 *token_id = tid;
1022 #if 0
1023 log_error_write(srv, __FILE__, __LINE__, "sbsdsdbdd",
1024 "source:", t->source,
1025 "line:", t->line, "pos:", t->line_pos,
1026 token, token->used - 1, tid);
1027 #endif
1029 return 1;
1030 } else if (t->offset < t->size) {
1031 fprintf(stderr, "%s.%d: %d, %s\n",
1032 __FILE__, __LINE__,
1033 tid, token->ptr);
1035 return 0;
1038 static int config_parse(server *srv, config_t *context, tokenizer_t *t) {
1039 void *pParser;
1040 int token_id;
1041 buffer *token, *lasttoken;
1042 int ret;
1044 pParser = configparserAlloc( malloc );
1045 force_assert(pParser);
1046 lasttoken = buffer_init();
1047 token = buffer_init();
1048 while((1 == (ret = config_tokenizer(srv, t, &token_id, token))) && context->ok) {
1049 buffer_copy_buffer(lasttoken, token);
1050 configparser(pParser, token_id, token, context);
1052 token = buffer_init();
1054 buffer_free(token);
1056 if (ret != -1 && context->ok) {
1057 /* add an EOL at EOF, better than say sorry */
1058 configparser(pParser, TK_EOL, buffer_init_string("(EOL)"), context);
1059 if (context->ok) {
1060 configparser(pParser, 0, NULL, context);
1063 configparserFree(pParser, free);
1065 if (ret == -1) {
1066 log_error_write(srv, __FILE__, __LINE__, "sb",
1067 "configfile parser failed at:", lasttoken);
1068 } else if (context->ok == 0) {
1069 log_error_write(srv, __FILE__, __LINE__, "sbsdsdsb",
1070 "source:", t->source,
1071 "line:", t->line, "pos:", t->line_pos,
1072 "parser failed somehow near here:", lasttoken);
1073 ret = -1;
1075 buffer_free(lasttoken);
1077 return ret == -1 ? -1 : 0;
1080 static int tokenizer_init(tokenizer_t *t, const buffer *source, const char *input, size_t size) {
1082 t->source = source;
1083 t->input = input;
1084 t->size = size;
1085 t->offset = 0;
1086 t->line = 1;
1087 t->line_pos = 1;
1089 t->in_key = 1;
1090 t->in_brace = 0;
1091 t->in_cond = 0;
1092 return 0;
1095 static int config_parse_file_stream(server *srv, config_t *context, const buffer *filename) {
1096 tokenizer_t t;
1097 stream s;
1098 int ret;
1100 if (0 != stream_open(&s, filename)) {
1101 log_error_write(srv, __FILE__, __LINE__, "sbss",
1102 "opening configfile ", filename, "failed:", strerror(errno));
1103 return -1;
1104 } else {
1105 tokenizer_init(&t, filename, s.start, s.size);
1106 ret = config_parse(srv, context, &t);
1109 stream_close(&s);
1110 return ret;
1113 int config_parse_file(server *srv, config_t *context, const char *fn) {
1114 buffer *filename;
1115 size_t i;
1116 int ret = -1;
1117 #ifdef GLOB_BRACE
1118 int flags = GLOB_BRACE;
1119 #else
1120 int flags = 0;
1121 #endif
1122 glob_t gl;
1124 if ((fn[0] == '/' || fn[0] == '\\') ||
1125 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\')) ||
1126 (fn[0] == '.' && fn[1] == '.' && (fn[2] == '/' || fn[2] == '\\'))) {
1127 filename = buffer_init_string(fn);
1128 } else {
1129 filename = buffer_init_buffer(context->basedir);
1130 buffer_append_string(filename, fn);
1133 switch (glob(filename->ptr, flags, NULL, &gl)) {
1134 case 0:
1135 for (i = 0; i < gl.gl_pathc; ++i) {
1136 buffer_copy_string(filename, gl.gl_pathv[i]);
1137 ret = config_parse_file_stream(srv, context, filename);
1138 if (0 != ret) break;
1140 globfree(&gl);
1141 break;
1142 case GLOB_NOMATCH:
1143 if (filename->ptr[strcspn(filename->ptr, "*?[]{}")] != '\0') { /*(contains glob metachars)*/
1144 ret = 0; /* not an error if no files match glob pattern */
1146 else {
1147 log_error_write(srv, __FILE__, __LINE__, "sb", "include file not found: ", filename);
1149 break;
1150 case GLOB_ABORTED:
1151 case GLOB_NOSPACE:
1152 log_error_write(srv, __FILE__, __LINE__, "sbss", "glob()", filename, "failed:", strerror(errno));
1153 break;
1156 buffer_free(filename);
1157 return ret;
1160 static char* getCWD(void) {
1161 char *s, *s1;
1162 size_t len;
1163 #ifdef PATH_MAX
1164 len = PATH_MAX;
1165 #else
1166 len = 4096;
1167 #endif
1169 s = malloc(len);
1170 if (!s) return NULL;
1171 while (NULL == getcwd(s, len)) {
1172 if (errno != ERANGE || SSIZE_MAX - len < len) {
1173 free(s);
1174 return NULL;
1176 len *= 2;
1177 s1 = realloc(s, len);
1178 if (!s1) {
1179 free(s);
1180 return NULL;
1182 s = s1;
1184 return s;
1187 int config_parse_cmd(server *srv, config_t *context, const char *cmd) {
1188 tokenizer_t t;
1189 int ret;
1190 buffer *source;
1191 buffer *out;
1192 char *oldpwd;
1194 if (NULL == (oldpwd = getCWD())) {
1195 log_error_write(srv, __FILE__, __LINE__, "s",
1196 "cannot get cwd", strerror(errno));
1197 return -1;
1200 if (!buffer_string_is_empty(context->basedir)) {
1201 if (0 != chdir(context->basedir->ptr)) {
1202 log_error_write(srv, __FILE__, __LINE__, "sbs",
1203 "cannot change directory to", context->basedir, strerror(errno));
1204 free(oldpwd);
1205 return -1;
1209 source = buffer_init_string(cmd);
1210 out = buffer_init();
1212 if (0 != proc_open_buffer(cmd, NULL, out, NULL)) {
1213 log_error_write(srv, __FILE__, __LINE__, "sbss",
1214 "opening", source, "failed:", strerror(errno));
1215 ret = -1;
1216 } else {
1217 tokenizer_init(&t, source, CONST_BUF_LEN(out));
1218 ret = config_parse(srv, context, &t);
1221 buffer_free(source);
1222 buffer_free(out);
1223 if (0 != chdir(oldpwd)) {
1224 log_error_write(srv, __FILE__, __LINE__, "sss",
1225 "cannot change directory to", oldpwd, strerror(errno));
1226 free(oldpwd);
1227 return -1;
1229 free(oldpwd);
1230 return ret;
1233 static void context_init(server *srv, config_t *context) {
1234 context->srv = srv;
1235 context->ok = 1;
1236 vector_config_weak_init(&context->configs_stack);
1237 context->basedir = buffer_init();
1240 static void context_free(config_t *context) {
1241 vector_config_weak_clear(&context->configs_stack);
1242 buffer_free(context->basedir);
1245 int config_read(server *srv, const char *fn) {
1246 config_t context;
1247 data_config *dc;
1248 data_integer *dpid;
1249 data_string *dcwd;
1250 int ret;
1251 char *pos;
1252 buffer *filename;
1254 context_init(srv, &context);
1255 context.all_configs = srv->config_context;
1257 #ifdef __WIN32
1258 pos = strrchr(fn, '\\');
1259 #else
1260 pos = strrchr(fn, '/');
1261 #endif
1262 if (pos) {
1263 buffer_copy_string_len(context.basedir, fn, pos - fn + 1);
1266 dc = data_config_init();
1267 buffer_copy_string_len(dc->key, CONST_STR_LEN("global"));
1269 force_assert(context.all_configs->used == 0);
1270 dc->context_ndx = context.all_configs->used;
1271 array_insert_unique(context.all_configs, (data_unset *)dc);
1272 context.current = dc;
1274 /* default context */
1275 dpid = data_integer_init();
1276 dpid->value = getpid();
1277 buffer_copy_string_len(dpid->key, CONST_STR_LEN("var.PID"));
1278 array_insert_unique(dc->value, (data_unset *)dpid);
1280 dcwd = data_string_init();
1281 buffer_string_prepare_copy(dcwd->value, 1023);
1282 if (NULL != getcwd(dcwd->value->ptr, dcwd->value->size - 1)) {
1283 buffer_commit(dcwd->value, strlen(dcwd->value->ptr));
1284 buffer_copy_string_len(dcwd->key, CONST_STR_LEN("var.CWD"));
1285 array_insert_unique(dc->value, (data_unset *)dcwd);
1286 } else {
1287 dcwd->free((data_unset*) dcwd);
1290 filename = buffer_init_string(fn);
1291 ret = config_parse_file_stream(srv, &context, filename);
1292 buffer_free(filename);
1294 /* remains nothing if parser is ok */
1295 force_assert(!(0 == ret && context.ok && 0 != context.configs_stack.used));
1296 context_free(&context);
1298 if (0 != ret) {
1299 return ret;
1302 if (0 != config_insert(srv)) {
1303 return -1;
1306 return 0;
1309 int config_set_defaults(server *srv) {
1310 size_t i;
1311 specific_config *s = srv->config_storage[0];
1312 struct stat st1, st2;
1314 struct ev_map { fdevent_handler_t et; const char *name; } event_handlers[] =
1316 /* - epoll is most reliable
1317 * - select works everywhere
1319 #ifdef USE_LINUX_EPOLL
1320 { FDEVENT_HANDLER_LINUX_SYSEPOLL, "linux-sysepoll" },
1321 #endif
1322 #ifdef USE_POLL
1323 { FDEVENT_HANDLER_POLL, "poll" },
1324 #endif
1325 #ifdef USE_SELECT
1326 { FDEVENT_HANDLER_SELECT, "select" },
1327 #endif
1328 #ifdef USE_LIBEV
1329 { FDEVENT_HANDLER_LIBEV, "libev" },
1330 #endif
1331 #ifdef USE_SOLARIS_DEVPOLL
1332 { FDEVENT_HANDLER_SOLARIS_DEVPOLL,"solaris-devpoll" },
1333 #endif
1334 #ifdef USE_SOLARIS_PORT
1335 { FDEVENT_HANDLER_SOLARIS_PORT, "solaris-eventports" },
1336 #endif
1337 #ifdef USE_FREEBSD_KQUEUE
1338 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "freebsd-kqueue" },
1339 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "kqueue" },
1340 #endif
1341 { FDEVENT_HANDLER_UNSET, NULL }
1344 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1345 if (-1 == stat(srv->srvconf.changeroot->ptr, &st1)) {
1346 log_error_write(srv, __FILE__, __LINE__, "sb",
1347 "server.chroot doesn't exist:", srv->srvconf.changeroot);
1348 return -1;
1350 if (!S_ISDIR(st1.st_mode)) {
1351 log_error_write(srv, __FILE__, __LINE__, "sb",
1352 "server.chroot isn't a directory:", srv->srvconf.changeroot);
1353 return -1;
1357 if (srv->srvconf.upload_tempdirs->used) {
1358 buffer * const b = srv->tmp_buf;
1359 size_t len;
1360 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1361 buffer_copy_buffer(b, srv->srvconf.changeroot);
1362 buffer_append_slash(b);
1363 } else {
1364 buffer_reset(b);
1366 len = buffer_string_length(b);
1368 for (i = 0; i < srv->srvconf.upload_tempdirs->used; ++i) {
1369 const data_string * const ds = (data_string *)srv->srvconf.upload_tempdirs->data[i];
1370 buffer_string_set_length(b, len); /*(truncate)*/
1371 buffer_append_string_buffer(b, ds->value);
1372 if (-1 == stat(b->ptr, &st1)) {
1373 log_error_write(srv, __FILE__, __LINE__, "sb",
1374 "server.upload-dirs doesn't exist:", b);
1375 } else if (!S_ISDIR(st1.st_mode)) {
1376 log_error_write(srv, __FILE__, __LINE__, "sb",
1377 "server.upload-dirs isn't a directory:", b);
1382 if (buffer_string_is_empty(s->document_root)) {
1383 log_error_write(srv, __FILE__, __LINE__, "s",
1384 "a default document-root has to be set");
1386 return -1;
1389 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1391 buffer_to_lower(srv->tmp_buf);
1393 if (2 == s->force_lowercase_filenames) { /* user didn't configure it in global section? */
1394 s->force_lowercase_filenames = 0; /* default to 0 */
1396 if (0 == stat(srv->tmp_buf->ptr, &st1)) {
1397 int is_lower = 0;
1399 is_lower = buffer_is_equal(srv->tmp_buf, s->document_root);
1401 /* lower-case existed, check upper-case */
1402 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1404 buffer_to_upper(srv->tmp_buf);
1406 /* we have to handle the special case that upper and lower-casing results in the same filename
1407 * as in server.document-root = "/" or "/12345/" */
1409 if (is_lower && buffer_is_equal(srv->tmp_buf, s->document_root)) {
1410 /* lower-casing and upper-casing didn't result in
1411 * an other filename, no need to stat(),
1412 * just assume it is case-sensitive. */
1414 s->force_lowercase_filenames = 0;
1415 } else if (0 == stat(srv->tmp_buf->ptr, &st2)) {
1417 /* upper case exists too, doesn't the FS handle this ? */
1419 /* upper and lower have the same inode -> case-insensitve FS */
1421 if (st1.st_ino == st2.st_ino) {
1422 /* upper and lower have the same inode -> case-insensitve FS */
1424 s->force_lowercase_filenames = 1;
1430 if (srv->srvconf.port == 0) {
1431 srv->srvconf.port = s->ssl_enabled ? 443 : 80;
1434 if (buffer_string_is_empty(srv->srvconf.event_handler)) {
1435 /* choose a good default
1437 * the event_handler list is sorted by 'goodness'
1438 * taking the first available should be the best solution
1440 srv->event_handler = event_handlers[0].et;
1442 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1443 log_error_write(srv, __FILE__, __LINE__, "s",
1444 "sorry, there is no event handler for this system");
1446 return -1;
1448 } else {
1450 * User override
1453 for (i = 0; event_handlers[i].name; i++) {
1454 if (0 == strcmp(event_handlers[i].name, srv->srvconf.event_handler->ptr)) {
1455 srv->event_handler = event_handlers[i].et;
1456 break;
1460 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1461 log_error_write(srv, __FILE__, __LINE__, "sb",
1462 "the selected event-handler in unknown or not supported:",
1463 srv->srvconf.event_handler );
1465 return -1;
1469 if (s->ssl_enabled) {
1470 if (buffer_string_is_empty(s->ssl_pemfile)) {
1471 /* PEM file is require */
1473 log_error_write(srv, __FILE__, __LINE__, "s",
1474 "ssl.pemfile has to be set");
1475 return -1;
1478 #ifndef USE_OPENSSL
1479 log_error_write(srv, __FILE__, __LINE__, "s",
1480 "ssl support is missing, recompile with --with-openssl");
1482 return -1;
1483 #endif
1486 return 0;