[core] replace array weakref with vector
[lighttpd.git] / src / configfile.c
blob170d5b6f84d66f5cf3db7859f2d873286fa640d7
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"
12 #include <sys/stat.h>
14 #include <stdlib.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <errno.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <ctype.h>
21 #include <limits.h>
22 #include <assert.h>
25 static int config_insert(server *srv) {
26 size_t i;
27 int ret = 0;
28 buffer *stat_cache_string;
30 config_values_t cv[] = {
31 { "server.bind", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 0 */
32 { "server.errorlog", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 1 */
33 { "server.errorfile-prefix", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
34 { "server.chroot", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 3 */
35 { "server.username", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 4 */
36 { "server.groupname", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 5 */
37 { "server.port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 6 */
38 { "server.tag", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
39 { "server.use-ipv6", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
40 { "server.modules", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_SERVER }, /* 9 */
42 { "server.event-handler", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 10 */
43 { "server.pid-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 11 */
44 { "server.max-request-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_SERVER }, /* 12 */
45 { "server.max-worker", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 13 */
46 { "server.document-root", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
47 { "server.force-lowercase-filenames", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 15 */
48 { "debug.log-condition-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 16 */
49 { "server.max-keep-alive-requests", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 17 */
50 { "server.name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 18 */
51 { "server.max-keep-alive-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 19 */
53 { "server.max-read-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 20 */
54 { "server.max-write-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 21 */
55 { "server.error-handler-404", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 22 */
56 { "server.max-fds", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 23 */
57 #ifdef HAVE_LSTAT
58 { "server.follow-symlink", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 24 */
59 #else
60 { "server.follow-symlink",
61 "Your system lacks lstat(). We can not differ symlinks from files."
62 "Please remove server.follow-symlinks from your config.",
63 T_CONFIG_UNSUPPORTED, T_CONFIG_SCOPE_UNSET },
64 #endif
65 { "server.kbytes-per-second", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 25 */
66 { "connection.kbytes-per-second", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 26 */
67 { "mimetype.use-xattr", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 27 */
68 { "mimetype.assign", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 28 */
69 { "ssl.pemfile", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 29 */
71 { "ssl.engine", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 30 */
72 { "debug.log-file-not-found", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 31 */
73 { "debug.log-request-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 32 */
74 { "debug.log-response-header", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 33 */
75 { "debug.log-request-header", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 34 */
76 { "debug.log-ssl-noise", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 35 */
77 { "server.protocol-http11", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 36 */
78 { "debug.log-request-header-on-error", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 37 */
79 { "debug.log-state-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 38 */
80 { "ssl.ca-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 39 */
82 { "server.errorlog-use-syslog", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 40 */
83 { "server.range-requests", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 41 */
84 { "server.stat-cache-engine", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 42 */
85 { "server.max-connections", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 43 */
86 { "server.network-backend", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 44 */
87 { "server.upload-dirs", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_SERVER }, /* 45 */
88 { "server.core-files", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 46 */
89 { "ssl.cipher-list", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 47 */
90 { "ssl.use-sslv2", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 48 */
91 { "etag.use-inode", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 49 */
93 { "etag.use-mtime", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 50 */
94 { "etag.use-size", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 51 */
95 { "server.reject-expect-100-with-417", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 52 */
96 { "debug.log-timeouts", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 53 */
97 { "server.defer-accept", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 54 */
98 { "server.breakagelog", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 55 */
99 { "ssl.verifyclient.activate", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 56 */
100 { "ssl.verifyclient.enforce", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 57 */
101 { "ssl.verifyclient.depth", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 58 */
102 { "ssl.verifyclient.username", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 59 */
104 { "ssl.verifyclient.exportcert", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 60 */
105 { "server.set-v6only", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 61 */
106 { "ssl.use-sslv3", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 62 */
107 { "ssl.dh-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 63 */
108 { "ssl.ec-curve", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 64 */
109 { "ssl.disable-client-renegotiation", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 65 */
110 { "ssl.honor-cipher-order", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 66 */
111 { "ssl.empty-fragments", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 67 */
112 { "server.upload-temp-file-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_SERVER }, /* 68 */
114 { "server.host",
115 "use server.bind instead",
116 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
117 { "server.docroot",
118 "use server.document-root instead",
119 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
120 { "server.virtual-root",
121 "load mod_simple_vhost and use simple-vhost.server-root instead",
122 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
123 { "server.virtual-default-host",
124 "load mod_simple_vhost and use simple-vhost.default-host instead",
125 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
126 { "server.virtual-docroot",
127 "load mod_simple_vhost and use simple-vhost.document-root instead",
128 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
129 { "server.userid",
130 "use server.username instead",
131 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
132 { "server.groupid",
133 "use server.groupname instead",
134 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
135 { "server.use-keep-alive",
136 "use server.max-keep-alive-requests = 0 instead",
137 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
138 { "server.force-lower-case-files",
139 "use server.force-lowercase-filenames instead",
140 T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET },
142 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
145 /* all T_CONFIG_SCOPE_SERVER options */
146 cv[0].destination = srv->srvconf.bindhost;
147 cv[1].destination = srv->srvconf.errorlog_file;
148 cv[3].destination = srv->srvconf.changeroot;
149 cv[4].destination = srv->srvconf.username;
150 cv[5].destination = srv->srvconf.groupname;
151 cv[6].destination = &(srv->srvconf.port);
152 cv[9].destination = srv->srvconf.modules;
154 cv[10].destination = srv->srvconf.event_handler;
155 cv[11].destination = srv->srvconf.pid_file;
156 cv[12].destination = &(srv->srvconf.max_request_size);
157 cv[13].destination = &(srv->srvconf.max_worker);
159 cv[23].destination = &(srv->srvconf.max_fds);
161 cv[37].destination = &(srv->srvconf.log_request_header_on_error);
162 cv[38].destination = &(srv->srvconf.log_state_handling);
164 cv[40].destination = &(srv->srvconf.errorlog_use_syslog);
165 stat_cache_string = buffer_init();
166 cv[42].destination = stat_cache_string;
167 cv[43].destination = &(srv->srvconf.max_conns);
168 cv[44].destination = srv->srvconf.network_backend;
169 cv[45].destination = srv->srvconf.upload_tempdirs;
170 cv[46].destination = &(srv->srvconf.enable_cores);
172 cv[52].destination = &(srv->srvconf.reject_expect_100_with_417);
173 cv[55].destination = srv->srvconf.breakagelog_file;
175 cv[68].destination = &(srv->srvconf.upload_temp_file_size);
177 srv->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
179 force_assert(srv->config_storage);
181 for (i = 0; i < srv->config_context->used; i++) {
182 data_config const* config = (data_config const*)srv->config_context->data[i];
183 specific_config *s;
185 s = calloc(1, sizeof(specific_config));
186 force_assert(s);
187 s->document_root = buffer_init();
188 s->mimetypes = array_init();
189 s->server_name = buffer_init();
190 s->ssl_pemfile = buffer_init();
191 s->ssl_ca_file = buffer_init();
192 s->error_handler = buffer_init();
193 s->server_tag = buffer_init();
194 s->ssl_cipher_list = buffer_init();
195 s->ssl_dh_file = buffer_init();
196 s->ssl_ec_curve = buffer_init();
197 s->errorfile_prefix = buffer_init();
198 s->max_keep_alive_requests = 16;
199 s->max_keep_alive_idle = 5;
200 s->max_read_idle = 60;
201 s->max_write_idle = 360;
202 s->use_xattr = 0;
203 s->ssl_enabled = 0;
204 s->ssl_honor_cipher_order = 1;
205 s->ssl_empty_fragments = 0;
206 s->ssl_use_sslv2 = 0;
207 s->ssl_use_sslv3 = 0;
208 s->use_ipv6 = 0;
209 s->set_v6only = 1;
210 s->defer_accept = 0;
211 #ifdef HAVE_LSTAT
212 s->follow_symlink = 1;
213 #endif
214 s->kbytes_per_second = 0;
215 s->allow_http11 = 1;
216 s->etag_use_inode = 1;
217 s->etag_use_mtime = 1;
218 s->etag_use_size = 1;
219 s->range_requests = 1;
220 s->force_lowercase_filenames = (i == 0) ? 2 : 0; /* we wan't to detect later if user changed this for global section */
221 s->global_kbytes_per_second = 0;
222 s->global_bytes_per_second_cnt = 0;
223 s->global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
224 s->ssl_verifyclient = 0;
225 s->ssl_verifyclient_enforce = 1;
226 s->ssl_verifyclient_username = buffer_init();
227 s->ssl_verifyclient_depth = 9;
228 s->ssl_verifyclient_export_cert = 0;
229 s->ssl_disable_client_renegotiation = 1;
231 /* all T_CONFIG_SCOPE_CONNECTION options */
232 cv[2].destination = s->errorfile_prefix;
233 cv[7].destination = s->server_tag;
234 cv[8].destination = &(s->use_ipv6);
236 cv[14].destination = s->document_root;
237 cv[15].destination = &(s->force_lowercase_filenames);
238 cv[16].destination = &(s->log_condition_handling);
239 cv[17].destination = &(s->max_keep_alive_requests);
240 cv[18].destination = s->server_name;
241 cv[19].destination = &(s->max_keep_alive_idle);
243 cv[20].destination = &(s->max_read_idle);
244 cv[21].destination = &(s->max_write_idle);
245 cv[22].destination = s->error_handler;
246 #ifdef HAVE_LSTAT
247 cv[24].destination = &(s->follow_symlink);
248 #endif
249 cv[25].destination = &(s->global_kbytes_per_second);
250 cv[26].destination = &(s->kbytes_per_second);
251 cv[27].destination = &(s->use_xattr);
252 cv[28].destination = s->mimetypes;
253 cv[29].destination = s->ssl_pemfile;
255 cv[30].destination = &(s->ssl_enabled);
256 cv[31].destination = &(s->log_file_not_found);
257 cv[32].destination = &(s->log_request_handling);
258 cv[33].destination = &(s->log_response_header);
259 cv[34].destination = &(s->log_request_header);
260 cv[35].destination = &(s->log_ssl_noise);
261 cv[36].destination = &(s->allow_http11);
262 cv[39].destination = s->ssl_ca_file;
264 cv[41].destination = &(s->range_requests);
265 cv[47].destination = s->ssl_cipher_list;
266 cv[48].destination = &(s->ssl_use_sslv2);
267 cv[49].destination = &(s->etag_use_inode);
269 cv[50].destination = &(s->etag_use_mtime);
270 cv[51].destination = &(s->etag_use_size);
271 cv[53].destination = &(s->log_timeouts);
272 cv[54].destination = &(s->defer_accept);
273 cv[56].destination = &(s->ssl_verifyclient);
274 cv[57].destination = &(s->ssl_verifyclient_enforce);
275 cv[58].destination = &(s->ssl_verifyclient_depth);
276 cv[59].destination = s->ssl_verifyclient_username;
278 cv[60].destination = &(s->ssl_verifyclient_export_cert);
279 cv[61].destination = &(s->set_v6only);
280 cv[62].destination = &(s->ssl_use_sslv3);
281 cv[63].destination = s->ssl_dh_file;
282 cv[64].destination = s->ssl_ec_curve;
283 cv[65].destination = &(s->ssl_disable_client_renegotiation);
284 cv[66].destination = &(s->ssl_honor_cipher_order);
285 cv[67].destination = &(s->ssl_empty_fragments);
287 srv->config_storage[i] = s;
289 if (0 != (ret = config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION))) {
290 break;
294 if (buffer_string_is_empty(stat_cache_string)) {
295 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
296 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("simple"))) {
297 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
298 #ifdef HAVE_FAM_H
299 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("fam"))) {
300 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_FAM;
301 #endif
302 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("disable"))) {
303 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_NONE;
304 } else {
305 log_error_write(srv, __FILE__, __LINE__, "sb",
306 "server.stat-cache-engine can be one of \"disable\", \"simple\","
307 #ifdef HAVE_FAM_H
308 " \"fam\","
309 #endif
310 " but not:", stat_cache_string);
311 ret = HANDLER_ERROR;
314 buffer_free(stat_cache_string);
316 return ret;
321 #define PATCH(x) con->conf.x = s->x
322 int config_setup_connection(server *srv, connection *con) {
323 specific_config *s = srv->config_storage[0];
325 PATCH(allow_http11);
326 PATCH(mimetypes);
327 PATCH(document_root);
328 PATCH(max_keep_alive_requests);
329 PATCH(max_keep_alive_idle);
330 PATCH(max_read_idle);
331 PATCH(max_write_idle);
332 PATCH(use_xattr);
333 PATCH(error_handler);
334 PATCH(errorfile_prefix);
335 #ifdef HAVE_LSTAT
336 PATCH(follow_symlink);
337 #endif
338 PATCH(server_tag);
339 PATCH(kbytes_per_second);
340 PATCH(global_kbytes_per_second);
341 PATCH(global_bytes_per_second_cnt);
343 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
344 buffer_copy_buffer(con->server_name, s->server_name);
346 PATCH(log_request_header);
347 PATCH(log_response_header);
348 PATCH(log_request_handling);
349 PATCH(log_condition_handling);
350 PATCH(log_file_not_found);
351 PATCH(log_ssl_noise);
352 PATCH(log_timeouts);
354 PATCH(range_requests);
355 PATCH(force_lowercase_filenames);
356 PATCH(ssl_enabled);
358 PATCH(ssl_pemfile);
359 #ifdef USE_OPENSSL
360 PATCH(ssl_pemfile_x509);
361 PATCH(ssl_pemfile_pkey);
362 #endif
363 PATCH(ssl_ca_file);
364 #ifdef USE_OPENSSL
365 PATCH(ssl_ca_file_cert_names);
366 #endif
367 PATCH(ssl_cipher_list);
368 PATCH(ssl_dh_file);
369 PATCH(ssl_ec_curve);
370 PATCH(ssl_honor_cipher_order);
371 PATCH(ssl_empty_fragments);
372 PATCH(ssl_use_sslv2);
373 PATCH(ssl_use_sslv3);
374 PATCH(etag_use_inode);
375 PATCH(etag_use_mtime);
376 PATCH(etag_use_size);
378 PATCH(ssl_verifyclient);
379 PATCH(ssl_verifyclient_enforce);
380 PATCH(ssl_verifyclient_depth);
381 PATCH(ssl_verifyclient_username);
382 PATCH(ssl_verifyclient_export_cert);
383 PATCH(ssl_disable_client_renegotiation);
385 return 0;
388 int config_patch_connection(server *srv, connection *con) {
389 size_t i, j;
391 /* skip the first, the global context */
392 for (i = 1; i < srv->config_context->used; i++) {
393 data_config *dc = (data_config *)srv->config_context->data[i];
394 specific_config *s = srv->config_storage[i];
396 /* condition didn't match */
397 if (!config_check_cond(srv, con, dc)) continue;
399 /* merge config */
400 for (j = 0; j < dc->value->used; j++) {
401 data_unset *du = dc->value->data[j];
403 if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.document-root"))) {
404 PATCH(document_root);
405 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.range-requests"))) {
406 PATCH(range_requests);
407 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler-404"))) {
408 PATCH(error_handler);
409 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.errorfile-prefix"))) {
410 PATCH(errorfile_prefix);
411 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.assign"))) {
412 PATCH(mimetypes);
413 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-requests"))) {
414 PATCH(max_keep_alive_requests);
415 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-idle"))) {
416 PATCH(max_keep_alive_idle);
417 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-write-idle"))) {
418 PATCH(max_write_idle);
419 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-read-idle"))) {
420 PATCH(max_read_idle);
421 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.use-xattr"))) {
422 PATCH(use_xattr);
423 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-inode"))) {
424 PATCH(etag_use_inode);
425 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-mtime"))) {
426 PATCH(etag_use_mtime);
427 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-size"))) {
428 PATCH(etag_use_size);
429 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.pemfile"))) {
430 PATCH(ssl_pemfile);
431 #ifdef USE_OPENSSL
432 PATCH(ssl_pemfile_x509);
433 PATCH(ssl_pemfile_pkey);
434 #endif
435 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ca-file"))) {
436 PATCH(ssl_ca_file);
437 #ifdef USE_OPENSSL
438 PATCH(ssl_ca_file_cert_names);
439 #endif
440 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.honor-cipher-order"))) {
441 PATCH(ssl_honor_cipher_order);
442 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.empty-fragments"))) {
443 PATCH(ssl_empty_fragments);
444 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv2"))) {
445 PATCH(ssl_use_sslv2);
446 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv3"))) {
447 PATCH(ssl_use_sslv3);
448 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.cipher-list"))) {
449 PATCH(ssl_cipher_list);
450 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.engine"))) {
451 PATCH(ssl_enabled);
452 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.dh-file"))) {
453 PATCH(ssl_dh_file);
454 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ec-curve"))) {
455 PATCH(ssl_ec_curve);
456 #ifdef HAVE_LSTAT
457 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.follow-symlink"))) {
458 PATCH(follow_symlink);
459 #endif
460 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.name"))) {
461 buffer_copy_buffer(con->server_name, s->server_name);
462 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.tag"))) {
463 PATCH(server_tag);
464 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("connection.kbytes-per-second"))) {
465 PATCH(kbytes_per_second);
466 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-handling"))) {
467 PATCH(log_request_handling);
468 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-header"))) {
469 PATCH(log_request_header);
470 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-response-header"))) {
471 PATCH(log_response_header);
472 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-condition-handling"))) {
473 PATCH(log_condition_handling);
474 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-file-not-found"))) {
475 PATCH(log_file_not_found);
476 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-ssl-noise"))) {
477 PATCH(log_ssl_noise);
478 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-timeouts"))) {
479 PATCH(log_timeouts);
480 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.protocol-http11"))) {
481 PATCH(allow_http11);
482 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.force-lowercase-filenames"))) {
483 PATCH(force_lowercase_filenames);
484 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.kbytes-per-second"))) {
485 PATCH(global_kbytes_per_second);
486 PATCH(global_bytes_per_second_cnt);
487 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
488 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.activate"))) {
489 PATCH(ssl_verifyclient);
490 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.enforce"))) {
491 PATCH(ssl_verifyclient_enforce);
492 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.depth"))) {
493 PATCH(ssl_verifyclient_depth);
494 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.username"))) {
495 PATCH(ssl_verifyclient_username);
496 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.exportcert"))) {
497 PATCH(ssl_verifyclient_export_cert);
498 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.disable-client-renegotiation"))) {
499 PATCH(ssl_disable_client_renegotiation);
504 con->etag_flags = (con->conf.etag_use_mtime ? ETAG_USE_MTIME : 0) |
505 (con->conf.etag_use_inode ? ETAG_USE_INODE : 0) |
506 (con->conf.etag_use_size ? ETAG_USE_SIZE : 0);
508 return 0;
510 #undef PATCH
512 typedef struct {
513 int foo;
514 int bar;
516 const buffer *source;
517 const char *input;
518 size_t offset;
519 size_t size;
521 int line_pos;
522 int line;
524 int in_key;
525 int in_brace;
526 int in_cond;
527 } tokenizer_t;
529 #if 0
530 static int tokenizer_open(server *srv, tokenizer_t *t, buffer *basedir, const char *fn) {
531 if (buffer_string_is_empty(basedir) ||
532 (fn[0] == '/' || fn[0] == '\\') ||
533 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\'))) {
534 t->file = buffer_init_string(fn);
535 } else {
536 t->file = buffer_init_buffer(basedir);
537 buffer_append_string(t->file, fn);
540 if (0 != stream_open(&(t->s), t->file)) {
541 log_error_write(srv, __FILE__, __LINE__, "sbss",
542 "opening configfile ", t->file, "failed:", strerror(errno));
543 buffer_free(t->file);
544 return -1;
547 t->input = t->s.start;
548 t->offset = 0;
549 t->size = t->s.size;
550 t->line = 1;
551 t->line_pos = 1;
553 t->in_key = 1;
554 t->in_brace = 0;
555 t->in_cond = 0;
556 return 0;
559 static int tokenizer_close(server *srv, tokenizer_t *t) {
560 UNUSED(srv);
562 buffer_free(t->file);
563 return stream_close(&(t->s));
565 #endif
566 static int config_skip_newline(tokenizer_t *t) {
567 int skipped = 1;
568 force_assert(t->input[t->offset] == '\r' || t->input[t->offset] == '\n');
569 if (t->input[t->offset] == '\r' && t->input[t->offset + 1] == '\n') {
570 skipped ++;
571 t->offset ++;
573 t->offset ++;
574 return skipped;
577 static int config_skip_comment(tokenizer_t *t) {
578 int i;
579 force_assert(t->input[t->offset] == '#');
580 for (i = 1; t->input[t->offset + i] &&
581 (t->input[t->offset + i] != '\n' && t->input[t->offset + i] != '\r');
582 i++);
583 t->offset += i;
584 return i;
587 static int config_tokenizer(server *srv, tokenizer_t *t, int *token_id, buffer *token) {
588 int tid = 0;
589 size_t i;
591 for (tid = 0; tid == 0 && t->offset < t->size && t->input[t->offset] ; ) {
592 char c = t->input[t->offset];
593 const char *start = NULL;
595 switch (c) {
596 case '=':
597 if (t->in_brace) {
598 if (t->input[t->offset + 1] == '>') {
599 t->offset += 2;
601 buffer_copy_string_len(token, CONST_STR_LEN("=>"));
603 tid = TK_ARRAY_ASSIGN;
604 } else {
605 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
606 "source:", t->source,
607 "line:", t->line, "pos:", t->line_pos,
608 "use => for assignments in arrays");
609 return -1;
611 } else if (t->in_cond) {
612 if (t->input[t->offset + 1] == '=') {
613 t->offset += 2;
615 buffer_copy_string_len(token, CONST_STR_LEN("=="));
617 tid = TK_EQ;
618 } else if (t->input[t->offset + 1] == '~') {
619 t->offset += 2;
621 buffer_copy_string_len(token, CONST_STR_LEN("=~"));
623 tid = TK_MATCH;
624 } else {
625 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
626 "source:", t->source,
627 "line:", t->line, "pos:", t->line_pos,
628 "only =~ and == are allowed in the condition");
629 return -1;
631 t->in_key = 1;
632 t->in_cond = 0;
633 } else if (t->in_key) {
634 tid = TK_ASSIGN;
636 buffer_copy_string_len(token, t->input + t->offset, 1);
638 t->offset++;
639 t->line_pos++;
640 } else {
641 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
642 "source:", t->source,
643 "line:", t->line, "pos:", t->line_pos,
644 "unexpected equal-sign: =");
645 return -1;
648 break;
649 case '!':
650 if (t->in_cond) {
651 if (t->input[t->offset + 1] == '=') {
652 t->offset += 2;
654 buffer_copy_string_len(token, CONST_STR_LEN("!="));
656 tid = TK_NE;
657 } else if (t->input[t->offset + 1] == '~') {
658 t->offset += 2;
660 buffer_copy_string_len(token, CONST_STR_LEN("!~"));
662 tid = TK_NOMATCH;
663 } else {
664 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
665 "source:", t->source,
666 "line:", t->line, "pos:", t->line_pos,
667 "only !~ and != are allowed in the condition");
668 return -1;
670 t->in_key = 1;
671 t->in_cond = 0;
672 } else {
673 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
674 "source:", t->source,
675 "line:", t->line, "pos:", t->line_pos,
676 "unexpected exclamation-marks: !");
677 return -1;
680 break;
681 case '\t':
682 case ' ':
683 t->offset++;
684 t->line_pos++;
685 break;
686 case '\n':
687 case '\r':
688 if (t->in_brace == 0) {
689 int done = 0;
690 while (!done && t->offset < t->size) {
691 switch (t->input[t->offset]) {
692 case '\r':
693 case '\n':
694 config_skip_newline(t);
695 t->line_pos = 1;
696 t->line++;
697 break;
699 case '#':
700 t->line_pos += config_skip_comment(t);
701 break;
703 case '\t':
704 case ' ':
705 t->offset++;
706 t->line_pos++;
707 break;
709 default:
710 done = 1;
713 t->in_key = 1;
714 tid = TK_EOL;
715 buffer_copy_string_len(token, CONST_STR_LEN("(EOL)"));
716 } else {
717 config_skip_newline(t);
718 t->line_pos = 1;
719 t->line++;
721 break;
722 case ',':
723 if (t->in_brace > 0) {
724 tid = TK_COMMA;
726 buffer_copy_string_len(token, CONST_STR_LEN("(COMMA)"));
729 t->offset++;
730 t->line_pos++;
731 break;
732 case '"':
733 /* search for the terminating " */
734 start = t->input + t->offset + 1;
735 buffer_copy_string_len(token, CONST_STR_LEN(""));
737 for (i = 1; t->input[t->offset + i]; i++) {
738 if (t->input[t->offset + i] == '\\' &&
739 t->input[t->offset + i + 1] == '"') {
741 buffer_append_string_len(token, start, t->input + t->offset + i - start);
743 start = t->input + t->offset + i + 1;
745 /* skip the " */
746 i++;
747 continue;
751 if (t->input[t->offset + i] == '"') {
752 tid = TK_STRING;
754 buffer_append_string_len(token, start, t->input + t->offset + i - start);
756 break;
760 if (t->input[t->offset + i] == '\0') {
761 /* ERROR */
763 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
764 "source:", t->source,
765 "line:", t->line, "pos:", t->line_pos,
766 "missing closing quote");
768 return -1;
771 t->offset += i + 1;
772 t->line_pos += i + 1;
774 break;
775 case '(':
776 t->offset++;
777 t->in_brace++;
779 tid = TK_LPARAN;
781 buffer_copy_string_len(token, CONST_STR_LEN("("));
782 break;
783 case ')':
784 t->offset++;
785 t->in_brace--;
787 tid = TK_RPARAN;
789 buffer_copy_string_len(token, CONST_STR_LEN(")"));
790 break;
791 case '$':
792 t->offset++;
794 tid = TK_DOLLAR;
795 t->in_cond = 1;
796 t->in_key = 0;
798 buffer_copy_string_len(token, CONST_STR_LEN("$"));
800 break;
802 case '+':
803 if (t->input[t->offset + 1] == '=') {
804 t->offset += 2;
805 buffer_copy_string_len(token, CONST_STR_LEN("+="));
806 tid = TK_APPEND;
807 } else {
808 t->offset++;
809 tid = TK_PLUS;
810 buffer_copy_string_len(token, CONST_STR_LEN("+"));
812 break;
814 case '{':
815 t->offset++;
817 tid = TK_LCURLY;
819 buffer_copy_string_len(token, CONST_STR_LEN("{"));
821 break;
823 case '}':
824 t->offset++;
826 tid = TK_RCURLY;
828 buffer_copy_string_len(token, CONST_STR_LEN("}"));
830 break;
832 case '[':
833 t->offset++;
835 tid = TK_LBRACKET;
837 buffer_copy_string_len(token, CONST_STR_LEN("["));
839 break;
841 case ']':
842 t->offset++;
844 tid = TK_RBRACKET;
846 buffer_copy_string_len(token, CONST_STR_LEN("]"));
848 break;
849 case '#':
850 t->line_pos += config_skip_comment(t);
852 break;
853 default:
854 if (t->in_cond) {
855 for (i = 0; t->input[t->offset + i] &&
856 (isalpha((unsigned char)t->input[t->offset + i])
857 ); i++);
859 if (i && t->input[t->offset + i]) {
860 tid = TK_SRVVARNAME;
861 buffer_copy_string_len(token, t->input + t->offset, i);
863 t->offset += i;
864 t->line_pos += i;
865 } else {
866 /* ERROR */
867 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
868 "source:", t->source,
869 "line:", t->line, "pos:", t->line_pos,
870 "invalid character in condition");
871 return -1;
873 } else if (isdigit((unsigned char)c)) {
874 /* take all digits */
875 for (i = 0; t->input[t->offset + i] && isdigit((unsigned char)t->input[t->offset + i]); i++);
877 /* was there it least a digit ? */
878 if (i) {
879 tid = TK_INTEGER;
881 buffer_copy_string_len(token, t->input + t->offset, i);
883 t->offset += i;
884 t->line_pos += i;
886 } else {
887 /* the key might consist of [-.0-9a-z] */
888 for (i = 0; t->input[t->offset + i] &&
889 (isalnum((unsigned char)t->input[t->offset + i]) ||
890 t->input[t->offset + i] == '.' ||
891 t->input[t->offset + i] == '_' || /* for env.* */
892 t->input[t->offset + i] == '-'
893 ); i++);
895 if (i && t->input[t->offset + i]) {
896 buffer_copy_string_len(token, t->input + t->offset, i);
898 if (strcmp(token->ptr, "include") == 0) {
899 tid = TK_INCLUDE;
900 } else if (strcmp(token->ptr, "include_shell") == 0) {
901 tid = TK_INCLUDE_SHELL;
902 } else if (strcmp(token->ptr, "global") == 0) {
903 tid = TK_GLOBAL;
904 } else if (strcmp(token->ptr, "else") == 0) {
905 tid = TK_ELSE;
906 } else {
907 tid = TK_LKEY;
910 t->offset += i;
911 t->line_pos += i;
912 } else {
913 /* ERROR */
914 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
915 "source:", t->source,
916 "line:", t->line, "pos:", t->line_pos,
917 "invalid character in variable name");
918 return -1;
921 break;
925 if (tid) {
926 *token_id = tid;
927 #if 0
928 log_error_write(srv, __FILE__, __LINE__, "sbsdsdbdd",
929 "source:", t->source,
930 "line:", t->line, "pos:", t->line_pos,
931 token, token->used - 1, tid);
932 #endif
934 return 1;
935 } else if (t->offset < t->size) {
936 fprintf(stderr, "%s.%d: %d, %s\n",
937 __FILE__, __LINE__,
938 tid, token->ptr);
940 return 0;
943 static int config_parse(server *srv, config_t *context, tokenizer_t *t) {
944 void *pParser;
945 int token_id;
946 buffer *token, *lasttoken;
947 int ret;
949 pParser = configparserAlloc( malloc );
950 force_assert(pParser);
951 lasttoken = buffer_init();
952 token = buffer_init();
953 while((1 == (ret = config_tokenizer(srv, t, &token_id, token))) && context->ok) {
954 buffer_copy_buffer(lasttoken, token);
955 configparser(pParser, token_id, token, context);
957 token = buffer_init();
959 buffer_free(token);
961 if (ret != -1 && context->ok) {
962 /* add an EOL at EOF, better than say sorry */
963 configparser(pParser, TK_EOL, buffer_init_string("(EOL)"), context);
964 if (context->ok) {
965 configparser(pParser, 0, NULL, context);
968 configparserFree(pParser, free);
970 if (ret == -1) {
971 log_error_write(srv, __FILE__, __LINE__, "sb",
972 "configfile parser failed at:", lasttoken);
973 } else if (context->ok == 0) {
974 log_error_write(srv, __FILE__, __LINE__, "sbsdsdsb",
975 "source:", t->source,
976 "line:", t->line, "pos:", t->line_pos,
977 "parser failed somehow near here:", lasttoken);
978 ret = -1;
980 buffer_free(lasttoken);
982 return ret == -1 ? -1 : 0;
985 static int tokenizer_init(tokenizer_t *t, const buffer *source, const char *input, size_t size) {
987 t->source = source;
988 t->input = input;
989 t->size = size;
990 t->offset = 0;
991 t->line = 1;
992 t->line_pos = 1;
994 t->in_key = 1;
995 t->in_brace = 0;
996 t->in_cond = 0;
997 return 0;
1000 int config_parse_file(server *srv, config_t *context, const char *fn) {
1001 tokenizer_t t;
1002 stream s;
1003 int ret;
1004 buffer *filename;
1006 if (buffer_string_is_empty(context->basedir) ||
1007 (fn[0] == '/' || fn[0] == '\\') ||
1008 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\'))) {
1009 filename = buffer_init_string(fn);
1010 } else {
1011 filename = buffer_init_buffer(context->basedir);
1012 buffer_append_string(filename, fn);
1015 if (0 != stream_open(&s, filename)) {
1016 log_error_write(srv, __FILE__, __LINE__, "sbss",
1017 "opening configfile ", filename, "failed:", strerror(errno));
1018 ret = -1;
1019 } else {
1020 tokenizer_init(&t, filename, s.start, s.size);
1021 ret = config_parse(srv, context, &t);
1024 stream_close(&s);
1025 buffer_free(filename);
1026 return ret;
1029 static char* getCWD(void) {
1030 char *s, *s1;
1031 size_t len;
1032 #ifdef PATH_MAX
1033 len = PATH_MAX;
1034 #else
1035 len = 4096;
1036 #endif
1038 s = malloc(len);
1039 if (!s) return NULL;
1040 while (NULL == getcwd(s, len)) {
1041 if (errno != ERANGE || SSIZE_MAX - len < len) {
1042 free(s);
1043 return NULL;
1045 len *= 2;
1046 s1 = realloc(s, len);
1047 if (!s1) {
1048 free(s);
1049 return NULL;
1051 s = s1;
1053 return s;
1056 int config_parse_cmd(server *srv, config_t *context, const char *cmd) {
1057 tokenizer_t t;
1058 int ret;
1059 buffer *source;
1060 buffer *out;
1061 char *oldpwd;
1063 if (NULL == (oldpwd = getCWD())) {
1064 log_error_write(srv, __FILE__, __LINE__, "s",
1065 "cannot get cwd", strerror(errno));
1066 return -1;
1069 if (!buffer_string_is_empty(context->basedir)) {
1070 if (0 != chdir(context->basedir->ptr)) {
1071 log_error_write(srv, __FILE__, __LINE__, "sbs",
1072 "cannot change directory to", context->basedir, strerror(errno));
1073 free(oldpwd);
1074 return -1;
1078 source = buffer_init_string(cmd);
1079 out = buffer_init();
1081 if (0 != proc_open_buffer(cmd, NULL, out, NULL)) {
1082 log_error_write(srv, __FILE__, __LINE__, "sbss",
1083 "opening", source, "failed:", strerror(errno));
1084 ret = -1;
1085 } else {
1086 tokenizer_init(&t, source, CONST_BUF_LEN(out));
1087 ret = config_parse(srv, context, &t);
1090 buffer_free(source);
1091 buffer_free(out);
1092 if (0 != chdir(oldpwd)) {
1093 log_error_write(srv, __FILE__, __LINE__, "sss",
1094 "cannot change directory to", oldpwd, strerror(errno));
1095 free(oldpwd);
1096 return -1;
1098 free(oldpwd);
1099 return ret;
1102 static void context_init(server *srv, config_t *context) {
1103 context->srv = srv;
1104 context->ok = 1;
1105 vector_config_weak_init(&context->configs_stack);
1106 context->basedir = buffer_init();
1109 static void context_free(config_t *context) {
1110 vector_config_weak_clear(&context->configs_stack);
1111 buffer_free(context->basedir);
1114 int config_read(server *srv, const char *fn) {
1115 config_t context;
1116 data_config *dc;
1117 data_integer *dpid;
1118 data_string *dcwd;
1119 int ret;
1120 char *pos;
1121 data_array *modules;
1123 context_init(srv, &context);
1124 context.all_configs = srv->config_context;
1126 #ifdef __WIN32
1127 pos = strrchr(fn, '\\');
1128 #else
1129 pos = strrchr(fn, '/');
1130 #endif
1131 if (pos) {
1132 buffer_copy_string_len(context.basedir, fn, pos - fn + 1);
1133 fn = pos + 1;
1136 dc = data_config_init();
1137 buffer_copy_string_len(dc->key, CONST_STR_LEN("global"));
1139 force_assert(context.all_configs->used == 0);
1140 dc->context_ndx = context.all_configs->used;
1141 array_insert_unique(context.all_configs, (data_unset *)dc);
1142 context.current = dc;
1144 /* default context */
1145 srv->config = dc->value;
1146 dpid = data_integer_init();
1147 dpid->value = getpid();
1148 buffer_copy_string_len(dpid->key, CONST_STR_LEN("var.PID"));
1149 array_insert_unique(srv->config, (data_unset *)dpid);
1151 dcwd = data_string_init();
1152 buffer_string_prepare_copy(dcwd->value, 1023);
1153 if (NULL != getcwd(dcwd->value->ptr, dcwd->value->size - 1)) {
1154 buffer_commit(dcwd->value, strlen(dcwd->value->ptr));
1155 buffer_copy_string_len(dcwd->key, CONST_STR_LEN("var.CWD"));
1156 array_insert_unique(srv->config, (data_unset *)dcwd);
1157 } else {
1158 dcwd->free((data_unset*) dcwd);
1161 ret = config_parse_file(srv, &context, fn);
1163 /* remains nothing if parser is ok */
1164 force_assert(!(0 == ret && context.ok && 0 != context.configs_stack.used));
1165 context_free(&context);
1167 if (0 != ret) {
1168 return ret;
1171 if (NULL != (dc = (data_config *)array_get_element(srv->config_context, "global"))) {
1172 srv->config = dc->value;
1173 } else {
1174 return -1;
1177 if (NULL != (modules = (data_array *)array_get_element(srv->config, "server.modules"))) {
1178 data_string *ds;
1179 data_array *prepends;
1181 if (modules->type != TYPE_ARRAY) {
1182 fprintf(stderr, "server.modules must be an array");
1183 return -1;
1186 prepends = data_array_init();
1188 /* prepend default modules */
1189 if (NULL == array_get_element(modules->value, "mod_indexfile")) {
1190 ds = data_string_init();
1191 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_indexfile"));
1192 array_insert_unique(prepends->value, (data_unset *)ds);
1195 prepends = (data_array *)configparser_merge_data((data_unset *)prepends, (data_unset *)modules);
1196 force_assert(NULL != prepends);
1197 buffer_copy_buffer(prepends->key, modules->key);
1198 array_replace(srv->config, (data_unset *)prepends);
1199 modules = prepends;
1201 /* append default modules */
1202 if (NULL == array_get_element(modules->value, "mod_dirlisting")) {
1203 ds = data_string_init();
1204 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_dirlisting"));
1205 array_insert_unique(modules->value, (data_unset *)ds);
1208 if (NULL == array_get_element(modules->value, "mod_staticfile")) {
1209 ds = data_string_init();
1210 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_staticfile"));
1211 array_insert_unique(modules->value, (data_unset *)ds);
1213 } else {
1214 data_string *ds;
1216 modules = data_array_init();
1218 /* server.modules is not set */
1219 ds = data_string_init();
1220 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_indexfile"));
1221 array_insert_unique(modules->value, (data_unset *)ds);
1223 ds = data_string_init();
1224 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_dirlisting"));
1225 array_insert_unique(modules->value, (data_unset *)ds);
1227 ds = data_string_init();
1228 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_staticfile"));
1229 array_insert_unique(modules->value, (data_unset *)ds);
1231 buffer_copy_string_len(modules->key, CONST_STR_LEN("server.modules"));
1232 array_insert_unique(srv->config, (data_unset *)modules);
1236 if (0 != config_insert(srv)) {
1237 return -1;
1240 return 0;
1243 int config_set_defaults(server *srv) {
1244 size_t i;
1245 specific_config *s = srv->config_storage[0];
1246 struct stat st1, st2;
1248 struct ev_map { fdevent_handler_t et; const char *name; } event_handlers[] =
1250 /* - epoll is most reliable
1251 * - select works everywhere
1253 #ifdef USE_LINUX_EPOLL
1254 { FDEVENT_HANDLER_LINUX_SYSEPOLL, "linux-sysepoll" },
1255 #endif
1256 #ifdef USE_POLL
1257 { FDEVENT_HANDLER_POLL, "poll" },
1258 #endif
1259 #ifdef USE_SELECT
1260 { FDEVENT_HANDLER_SELECT, "select" },
1261 #endif
1262 #ifdef USE_LIBEV
1263 { FDEVENT_HANDLER_LIBEV, "libev" },
1264 #endif
1265 #ifdef USE_SOLARIS_DEVPOLL
1266 { FDEVENT_HANDLER_SOLARIS_DEVPOLL,"solaris-devpoll" },
1267 #endif
1268 #ifdef USE_SOLARIS_PORT
1269 { FDEVENT_HANDLER_SOLARIS_PORT, "solaris-eventports" },
1270 #endif
1271 #ifdef USE_FREEBSD_KQUEUE
1272 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "freebsd-kqueue" },
1273 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "kqueue" },
1274 #endif
1275 { FDEVENT_HANDLER_UNSET, NULL }
1278 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1279 if (-1 == stat(srv->srvconf.changeroot->ptr, &st1)) {
1280 log_error_write(srv, __FILE__, __LINE__, "sb",
1281 "server.chroot doesn't exist:", srv->srvconf.changeroot);
1282 return -1;
1284 if (!S_ISDIR(st1.st_mode)) {
1285 log_error_write(srv, __FILE__, __LINE__, "sb",
1286 "server.chroot isn't a directory:", srv->srvconf.changeroot);
1287 return -1;
1291 if (buffer_string_is_empty(s->document_root)) {
1292 log_error_write(srv, __FILE__, __LINE__, "s",
1293 "a default document-root has to be set");
1295 return -1;
1298 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1300 buffer_to_lower(srv->tmp_buf);
1302 if (2 == s->force_lowercase_filenames) { /* user didn't configure it in global section? */
1303 s->force_lowercase_filenames = 0; /* default to 0 */
1305 if (0 == stat(srv->tmp_buf->ptr, &st1)) {
1306 int is_lower = 0;
1308 is_lower = buffer_is_equal(srv->tmp_buf, s->document_root);
1310 /* lower-case existed, check upper-case */
1311 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1313 buffer_to_upper(srv->tmp_buf);
1315 /* we have to handle the special case that upper and lower-casing results in the same filename
1316 * as in server.document-root = "/" or "/12345/" */
1318 if (is_lower && buffer_is_equal(srv->tmp_buf, s->document_root)) {
1319 /* lower-casing and upper-casing didn't result in
1320 * an other filename, no need to stat(),
1321 * just assume it is case-sensitive. */
1323 s->force_lowercase_filenames = 0;
1324 } else if (0 == stat(srv->tmp_buf->ptr, &st2)) {
1326 /* upper case exists too, doesn't the FS handle this ? */
1328 /* upper and lower have the same inode -> case-insensitve FS */
1330 if (st1.st_ino == st2.st_ino) {
1331 /* upper and lower have the same inode -> case-insensitve FS */
1333 s->force_lowercase_filenames = 1;
1339 if (srv->srvconf.port == 0) {
1340 srv->srvconf.port = s->ssl_enabled ? 443 : 80;
1343 if (buffer_string_is_empty(srv->srvconf.event_handler)) {
1344 /* choose a good default
1346 * the event_handler list is sorted by 'goodness'
1347 * taking the first available should be the best solution
1349 srv->event_handler = event_handlers[0].et;
1351 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1352 log_error_write(srv, __FILE__, __LINE__, "s",
1353 "sorry, there is no event handler for this system");
1355 return -1;
1357 } else {
1359 * User override
1362 for (i = 0; event_handlers[i].name; i++) {
1363 if (0 == strcmp(event_handlers[i].name, srv->srvconf.event_handler->ptr)) {
1364 srv->event_handler = event_handlers[i].et;
1365 break;
1369 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1370 log_error_write(srv, __FILE__, __LINE__, "sb",
1371 "the selected event-handler in unknown or not supported:",
1372 srv->srvconf.event_handler );
1374 return -1;
1378 if (s->ssl_enabled) {
1379 if (buffer_string_is_empty(s->ssl_pemfile)) {
1380 /* PEM file is require */
1382 log_error_write(srv, __FILE__, __LINE__, "s",
1383 "ssl.pemfile has to be set");
1384 return -1;
1387 #ifndef USE_OPENSSL
1388 log_error_write(srv, __FILE__, __LINE__, "s",
1389 "ssl support is missing, recompile with --with-openssl");
1391 return -1;
1392 #endif
1395 return 0;