[core] set server.max-keep-alive-requests = 100 (fixes #2205)
[lighttpd.git] / src / configfile.c
blobdbf01234b75d3e882767fd72e7a107c34235352c
1 #include "first.h"
3 #include "server.h"
4 #include "fdevent.h"
5 #include "log.h"
6 #include "stream.h"
7 #include "plugin.h"
9 #include "configparser.h"
10 #include "configfile.h"
11 #include "proc_open.h"
12 #include "request.h"
14 #include <sys/stat.h>
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <limits.h>
24 #include <glob.h>
27 #if defined(HAVE_MYSQL) || (defined(HAVE_LDAP_H) && defined(HAVE_LBER_H) && defined(HAVE_LIBLDAP) && defined(HAVE_LIBLBER))
28 static void config_warn_authn_module (server *srv, const char *module) {
29 size_t len = strlen(module);
30 for (size_t i = 0; i < srv->config_context->used; ++i) {
31 const data_config *config = (data_config const*)srv->config_context->data[i];
32 const data_unset *du = array_get_element(config->value, "auth.backend");
33 if (NULL != du && du->type == TYPE_STRING) {
34 data_string *ds = (data_string *)du;
35 if (buffer_is_equal_string(ds->value, module, len)) {
36 ds = data_string_init();
37 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_authn_"));
38 buffer_append_string(ds->value, module);
39 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
40 log_error_write(srv, __FILE__, __LINE__, "SSSsSSS", "Warning: please add \"mod_authn_", module, "\" to server.modules list in lighttpd.conf. A future release of lighttpd 1.4.x will not automatically load mod_authn_", module, "and lighttpd will fail to start up since your lighttpd.conf uses auth.backend = \"", module, "\".");
41 return;
46 #endif
48 #if defined HAVE_LIBSSL && defined HAVE_OPENSSL_SSL_H
49 static void config_warn_openssl_module (server *srv) {
50 for (size_t i = 0; i < srv->config_context->used; ++i) {
51 const data_config *config = (data_config const*)srv->config_context->data[i];
52 for (size_t j = 0; j < config->value->used; ++j) {
53 data_unset *du = config->value->data[j];
54 if (0 == strncmp(du->key->ptr, "ssl.", sizeof("ssl.")-1)) {
55 /* mod_openssl should be loaded after mod_extforward */
56 data_string *ds = data_string_init();
57 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_openssl"));
58 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
59 log_error_write(srv, __FILE__, __LINE__, "S", "Warning: please add \"mod_openssl\" to server.modules list in lighttpd.conf. A future release of lighttpd 1.4.x *will not* automatically load mod_openssl and lighttpd *will not* use SSL/TLS where your lighttpd.conf contains ssl.* directives");
60 return;
65 #endif
67 static int config_insert(server *srv) {
68 size_t i;
69 int ret = 0;
70 buffer *stat_cache_string;
72 config_values_t cv[] = {
73 { "server.bind", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 0 */
74 { "server.errorlog", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 1 */
75 { "server.errorfile-prefix", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
76 { "server.chroot", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 3 */
77 { "server.username", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 4 */
78 { "server.groupname", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 5 */
79 { "server.port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 6 */
80 { "server.tag", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
81 { "server.use-ipv6", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
82 { "server.modules", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_SERVER }, /* 9 */
84 { "server.event-handler", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 10 */
85 { "server.pid-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 11 */
86 { "server.max-request-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
87 { "server.max-worker", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 13 */
88 { "server.document-root", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
89 { "server.force-lowercase-filenames", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 15 */
90 { "debug.log-condition-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 16 */
91 { "server.max-keep-alive-requests", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 17 */
92 { "server.name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 18 */
93 { "server.max-keep-alive-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 19 */
95 { "server.max-read-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 20 */
96 { "server.max-write-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 21 */
97 { "server.error-handler", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 22 */
98 { "server.max-fds", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 23 */
99 #ifdef HAVE_LSTAT
100 { "server.follow-symlink", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 24 */
101 #else
102 { "server.follow-symlink",
103 "Your system lacks lstat(). We can not differ symlinks from files."
104 "Please remove server.follow-symlinks from your config.",
105 T_CONFIG_UNSUPPORTED, T_CONFIG_SCOPE_UNSET },
106 #endif
107 { "server.kbytes-per-second", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 25 */
108 { "connection.kbytes-per-second", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 26 */
109 { "mimetype.use-xattr", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 27 */
110 { "mimetype.assign", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 28 */
111 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 29 */
113 { "ssl.engine", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 30 */
114 { "debug.log-file-not-found", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 31 */
115 { "debug.log-request-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 32 */
116 { "debug.log-response-header", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 33 */
117 { "debug.log-request-header", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 34 */
118 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 35 */
119 { "server.protocol-http11", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 36 */
120 { "debug.log-request-header-on-error", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 37 */
121 { "debug.log-state-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 38 */
122 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 39 */
124 { "server.errorlog-use-syslog", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 40 */
125 { "server.range-requests", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 41 */
126 { "server.stat-cache-engine", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 42 */
127 { "server.max-connections", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 43 */
128 { "server.network-backend", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 44 */
129 { "server.upload-dirs", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_SERVER }, /* 45 */
130 { "server.core-files", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 46 */
131 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 47 */
132 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 48 */
133 { "etag.use-inode", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 49 */
135 { "etag.use-mtime", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 50 */
136 { "etag.use-size", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 51 */
137 { "server.reject-expect-100-with-417", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 52 */
138 { "debug.log-timeouts", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 53 */
139 { "server.defer-accept", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 54 */
140 { "server.breakagelog", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 55 */
141 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 56 */
142 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 57 */
143 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 58 */
144 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 59 */
146 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 60 */
147 { "server.set-v6only", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 61 */
148 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 62 */
149 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 63 */
150 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 64 */
151 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 65 */
152 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 66 */
153 { "unused-slot-moved-to-mod-openssl", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 67 */
154 { "server.upload-temp-file-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_SERVER }, /* 68 */
155 { "mimetype.xattr-name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 69 */
156 { "server.listen-backlog", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 70 */
157 { "server.error-handler-404", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 71 */
158 { "server.http-parseopt-header-strict",NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 72 */
159 { "server.http-parseopt-host-strict", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 73 */
160 { "server.http-parseopt-host-normalize",NULL,T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 74 */
161 { "server.bsd-accept-filter", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 75 */
162 { "server.stream-request-body", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 76 */
163 { "server.stream-response-body", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 77 */
164 { "server.max-request-field-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_SERVER }, /* 78 */
165 { "server.error-intercept", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 79 */
166 { "server.syslog-facility", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 80 */
168 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
171 /* all T_CONFIG_SCOPE_SERVER options */
172 cv[0].destination = srv->srvconf.bindhost;
173 cv[1].destination = srv->srvconf.errorlog_file;
174 cv[3].destination = srv->srvconf.changeroot;
175 cv[4].destination = srv->srvconf.username;
176 cv[5].destination = srv->srvconf.groupname;
177 cv[6].destination = &(srv->srvconf.port);
178 cv[9].destination = srv->srvconf.modules;
180 cv[10].destination = srv->srvconf.event_handler;
181 cv[11].destination = srv->srvconf.pid_file;
182 cv[13].destination = &(srv->srvconf.max_worker);
184 cv[23].destination = &(srv->srvconf.max_fds);
186 cv[37].destination = &(srv->srvconf.log_request_header_on_error);
187 cv[38].destination = &(srv->srvconf.log_state_handling);
189 cv[40].destination = &(srv->srvconf.errorlog_use_syslog);
190 stat_cache_string = buffer_init();
191 cv[42].destination = stat_cache_string;
192 cv[43].destination = &(srv->srvconf.max_conns);
193 cv[44].destination = srv->srvconf.network_backend;
194 cv[45].destination = srv->srvconf.upload_tempdirs;
195 cv[46].destination = &(srv->srvconf.enable_cores);
197 cv[52].destination = &(srv->srvconf.reject_expect_100_with_417);
198 cv[55].destination = srv->srvconf.breakagelog_file;
200 cv[68].destination = &(srv->srvconf.upload_temp_file_size);
201 cv[69].destination = srv->srvconf.xattr_name;
202 cv[72].destination = &(srv->srvconf.http_header_strict);
203 cv[73].destination = &(srv->srvconf.http_host_strict);
204 cv[74].destination = &(srv->srvconf.http_host_normalize);
205 cv[78].destination = &(srv->srvconf.max_request_field_size);
206 cv[80].destination = srv->srvconf.syslog_facility;
208 srv->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
210 force_assert(srv->config_storage);
211 force_assert(srv->config_context->used); /* static analysis hint for ccc
212 -analyzer */
214 for (i = 0; i < srv->config_context->used; i++) {
215 data_config const* config = (data_config const*)srv->config_context->data[i];
216 specific_config *s;
218 s = calloc(1, sizeof(specific_config));
219 force_assert(s);
220 s->document_root = buffer_init();
221 s->mimetypes = array_init();
222 s->server_name = buffer_init();
223 s->error_handler = buffer_init();
224 s->error_handler_404 = buffer_init();
225 s->server_tag = buffer_init();
226 s->errorfile_prefix = buffer_init();
227 #if defined(__FreeBSD__) || defined(__NetBSD__) \
228 || defined(__OpenBSD__) || defined(__DragonFly__)
229 s->bsd_accept_filter = (i == 0)
230 ? buffer_init()
231 : buffer_init_buffer(srv->config_storage[0]->bsd_accept_filter);
232 #endif
233 s->max_keep_alive_requests = 100;
234 s->max_keep_alive_idle = 5;
235 s->max_read_idle = 60;
236 s->max_write_idle = 360;
237 s->max_request_size = 0;
238 s->use_xattr = 0;
239 s->ssl_enabled = 0;
240 s->use_ipv6 = (i == 0) ? 0 : srv->config_storage[0]->use_ipv6;
241 s->set_v6only = (i == 0) ? 1 : srv->config_storage[0]->set_v6only;
242 s->defer_accept = (i == 0) ? 0 : srv->config_storage[0]->defer_accept;
243 #ifdef HAVE_LSTAT
244 s->follow_symlink = 1;
245 #endif
246 s->kbytes_per_second = 0;
247 s->allow_http11 = 1;
248 s->etag_use_inode = 1;
249 s->etag_use_mtime = 1;
250 s->etag_use_size = 1;
251 s->range_requests = 1;
252 s->force_lowercase_filenames = (i == 0) ? 2 : 0; /* we wan't to detect later if user changed this for global section */
253 s->global_kbytes_per_second = 0;
254 s->global_bytes_per_second_cnt = 0;
255 s->global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
256 s->listen_backlog = (0 == i ? 1024 : srv->config_storage[0]->listen_backlog);
257 s->stream_request_body = 0;
258 s->stream_response_body = 0;
259 s->error_intercept = 0;
261 /* all T_CONFIG_SCOPE_CONNECTION options */
262 cv[2].destination = s->errorfile_prefix;
263 cv[7].destination = s->server_tag;
264 cv[8].destination = &(s->use_ipv6);
266 cv[12].destination = &(s->max_request_size);
267 cv[14].destination = s->document_root;
268 cv[15].destination = &(s->force_lowercase_filenames);
269 cv[16].destination = &(s->log_condition_handling);
270 cv[17].destination = &(s->max_keep_alive_requests);
271 cv[18].destination = s->server_name;
272 cv[19].destination = &(s->max_keep_alive_idle);
274 cv[20].destination = &(s->max_read_idle);
275 cv[21].destination = &(s->max_write_idle);
276 cv[22].destination = s->error_handler;
277 #ifdef HAVE_LSTAT
278 cv[24].destination = &(s->follow_symlink);
279 #endif
280 cv[25].destination = &(s->global_kbytes_per_second);
281 cv[26].destination = &(s->kbytes_per_second);
282 cv[27].destination = &(s->use_xattr);
283 cv[28].destination = s->mimetypes;
284 /*cv[29].destination = s->unused;*/
286 cv[30].destination = &(s->ssl_enabled);
287 cv[31].destination = &(s->log_file_not_found);
288 cv[32].destination = &(s->log_request_handling);
289 cv[33].destination = &(s->log_response_header);
290 cv[34].destination = &(s->log_request_header);
291 /*cv[35].destination = &(s->unused);*/
292 cv[36].destination = &(s->allow_http11);
293 /*cv[39].destination = s->unused;*/
295 cv[41].destination = &(s->range_requests);
296 /*cv[47].destination = s->unused;*/
297 /*cv[48].destination = &(s->unused);*/
298 cv[49].destination = &(s->etag_use_inode);
300 cv[50].destination = &(s->etag_use_mtime);
301 cv[51].destination = &(s->etag_use_size);
302 cv[53].destination = &(s->log_timeouts);
303 cv[54].destination = &(s->defer_accept);
304 /*cv[56].destination = &(s->unused);*/
305 /*cv[57].destination = &(s->unused);*/
306 /*cv[58].destination = &(s->unused);*/
307 /*cv[59].destination = s->unused;*/
309 /*cv[60].destination = &(s->unused);*/
310 cv[61].destination = &(s->set_v6only);
311 /*cv[62].destination = &(s->unused);*/
312 /*cv[63].destination = s->unused;*/
313 /*cv[64].destination = s->unused;*/
314 /*cv[65].destination = &(s->unused);*/
315 /*cv[66].destination = &(s->unused);*/
316 /*cv[67].destination = &(s->unused);*/
317 cv[70].destination = &(s->listen_backlog);
318 cv[71].destination = s->error_handler_404;
319 #if defined(__FreeBSD__) || defined(__NetBSD__) \
320 || defined(__OpenBSD__) || defined(__DragonFly__)
321 cv[75].destination = s->bsd_accept_filter;
322 #endif
323 cv[76].destination = &(s->stream_request_body);
324 cv[77].destination = &(s->stream_response_body);
325 cv[79].destination = &(s->error_intercept);
327 srv->config_storage[i] = s;
329 if (0 != (ret = config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION))) {
330 break;
333 if (s->stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN) {
334 s->stream_request_body |= FDEVENT_STREAM_REQUEST;
336 if (s->stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) {
337 s->stream_response_body |= FDEVENT_STREAM_RESPONSE;
340 if (!array_is_kvstring(s->mimetypes)) {
341 log_error_write(srv, __FILE__, __LINE__, "s",
342 "unexpected value for mimetype.assign; expected list of \"ext\" => \"mimetype\"");
345 #if !(defined HAVE_LIBSSL && defined HAVE_OPENSSL_SSL_H)
346 if (s->ssl_enabled) {
347 log_error_write(srv, __FILE__, __LINE__, "s",
348 "ssl support is missing, recompile with --with-openssl");
349 ret = HANDLER_ERROR;
350 break;
352 #endif
356 specific_config *s = srv->config_storage[0];
357 s->http_parseopts= /*(global, but stored in con->conf.http_parseopts)*/
358 (srv->srvconf.http_header_strict ?(HTTP_PARSEOPT_HEADER_STRICT) :0)
359 |(srv->srvconf.http_host_strict ?(HTTP_PARSEOPT_HOST_STRICT
360 |HTTP_PARSEOPT_HOST_NORMALIZE):0)
361 |(srv->srvconf.http_host_normalize ?(HTTP_PARSEOPT_HOST_NORMALIZE):0);
364 if (buffer_string_is_empty(stat_cache_string)) {
365 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
366 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("simple"))) {
367 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
368 #ifdef HAVE_FAM_H
369 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("fam"))) {
370 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_FAM;
371 #endif
372 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("disable"))) {
373 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_NONE;
374 } else {
375 log_error_write(srv, __FILE__, __LINE__, "sb",
376 "server.stat-cache-engine can be one of \"disable\", \"simple\","
377 #ifdef HAVE_FAM_H
378 " \"fam\","
379 #endif
380 " but not:", stat_cache_string);
381 ret = HANDLER_ERROR;
384 buffer_free(stat_cache_string);
386 if (!array_is_vlist(srv->srvconf.upload_tempdirs)) {
387 log_error_write(srv, __FILE__, __LINE__, "s",
388 "unexpected value for server.upload-dirs; expected list of \"path\" strings");
389 ret = HANDLER_ERROR;
392 if (!array_is_vlist(srv->srvconf.modules)) {
393 log_error_write(srv, __FILE__, __LINE__, "s",
394 "unexpected value for server.modules; expected list of \"mod_xxxxxx\" strings");
395 ret = HANDLER_ERROR;
396 } else {
397 data_string *ds;
398 int prepend_mod_indexfile = 1;
399 int append_mod_dirlisting = 1;
400 int append_mod_staticfile = 1;
401 int append_mod_authn_file = 1;
402 int append_mod_authn_ldap = 1;
403 int append_mod_authn_mysql = 1;
404 int append_mod_openssl = 1;
405 int contains_mod_auth = 0;
407 /* prepend default modules */
408 for (i = 0; i < srv->srvconf.modules->used; i++) {
409 ds = (data_string *)srv->srvconf.modules->data[i];
411 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_indexfile"))) {
412 prepend_mod_indexfile = 0;
415 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_staticfile"))) {
416 append_mod_staticfile = 0;
419 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_dirlisting"))) {
420 append_mod_dirlisting = 0;
423 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_openssl"))) {
424 append_mod_openssl = 0;
427 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_authn_file"))) {
428 append_mod_authn_file = 0;
431 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_authn_ldap"))) {
432 append_mod_authn_ldap = 0;
435 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_authn_mysql"))) {
436 append_mod_authn_mysql = 0;
439 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_auth"))) {
440 contains_mod_auth = 1;
443 if (0 == prepend_mod_indexfile &&
444 0 == append_mod_dirlisting &&
445 0 == append_mod_staticfile &&
446 0 == append_mod_openssl &&
447 0 == append_mod_authn_file &&
448 0 == append_mod_authn_ldap &&
449 0 == append_mod_authn_mysql &&
450 1 == contains_mod_auth) {
451 break;
455 if (prepend_mod_indexfile) {
456 /* mod_indexfile has to be loaded before mod_fastcgi and friends */
457 array *modules = array_init();
459 ds = data_string_init();
460 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_indexfile"));
461 array_insert_unique(modules, (data_unset *)ds);
463 for (i = 0; i < srv->srvconf.modules->used; i++) {
464 data_unset *du = srv->srvconf.modules->data[i];
465 array_insert_unique(modules, du->copy(du));
468 array_free(srv->srvconf.modules);
469 srv->srvconf.modules = modules;
472 /* append default modules */
473 if (append_mod_dirlisting) {
474 ds = data_string_init();
475 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_dirlisting"));
476 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
479 if (append_mod_staticfile) {
480 ds = data_string_init();
481 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_staticfile"));
482 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
485 if (append_mod_openssl) {
486 #if defined HAVE_LIBSSL && defined HAVE_OPENSSL_SSL_H
487 config_warn_openssl_module(srv);
488 #endif
491 /* mod_auth.c,http_auth.c auth backends were split into separate modules
492 * Automatically load auth backend modules for compatibility with
493 * existing lighttpd 1.4.x configs */
494 if (contains_mod_auth) {
495 if (append_mod_authn_file) {
496 ds = data_string_init();
497 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_authn_file"));
498 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
500 if (append_mod_authn_ldap) {
501 #if defined(HAVE_LDAP_H) && defined(HAVE_LBER_H) && defined(HAVE_LIBLDAP) && defined(HAVE_LIBLBER)
502 config_warn_authn_module(srv, "ldap");
503 #endif
505 if (append_mod_authn_mysql) {
506 #if defined(HAVE_MYSQL)
507 config_warn_authn_module(srv, "mysql");
508 #endif
513 return ret;
518 #define PATCH(x) con->conf.x = s->x
519 int config_setup_connection(server *srv, connection *con) {
520 specific_config *s = srv->config_storage[0];
522 PATCH(http_parseopts);
524 PATCH(allow_http11);
525 PATCH(mimetypes);
526 PATCH(document_root);
527 PATCH(high_precision_timestamps);
528 PATCH(max_keep_alive_requests);
529 PATCH(max_keep_alive_idle);
530 PATCH(max_read_idle);
531 PATCH(max_write_idle);
532 PATCH(max_request_size);
533 PATCH(use_xattr);
534 PATCH(error_handler);
535 PATCH(error_handler_404);
536 PATCH(error_intercept);
537 PATCH(errorfile_prefix);
538 #ifdef HAVE_LSTAT
539 PATCH(follow_symlink);
540 #endif
541 PATCH(server_tag);
542 PATCH(kbytes_per_second);
543 PATCH(global_kbytes_per_second);
544 PATCH(global_bytes_per_second_cnt);
546 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
547 buffer_copy_buffer(con->server_name, s->server_name);
549 PATCH(log_request_header);
550 PATCH(log_response_header);
551 PATCH(log_request_handling);
552 PATCH(log_condition_handling);
553 PATCH(log_file_not_found);
554 PATCH(log_timeouts);
556 PATCH(range_requests);
557 PATCH(force_lowercase_filenames);
558 /*PATCH(listen_backlog);*//*(not necessary; used only at startup)*/
559 PATCH(stream_request_body);
560 PATCH(stream_response_body);
562 PATCH(etag_use_inode);
563 PATCH(etag_use_mtime);
564 PATCH(etag_use_size);
566 return 0;
569 int config_patch_connection(server *srv, connection *con) {
570 size_t i, j;
572 /* skip the first, the global context */
573 for (i = 1; i < srv->config_context->used; i++) {
574 data_config *dc = (data_config *)srv->config_context->data[i];
575 specific_config *s = srv->config_storage[i];
577 /* condition didn't match */
578 if (!config_check_cond(srv, con, dc)) continue;
580 /* merge config */
581 for (j = 0; j < dc->value->used; j++) {
582 data_unset *du = dc->value->data[j];
584 if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.document-root"))) {
585 PATCH(document_root);
586 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.range-requests"))) {
587 PATCH(range_requests);
588 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler"))) {
589 PATCH(error_handler);
590 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler-404"))) {
591 PATCH(error_handler_404);
592 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-intercept"))) {
593 PATCH(error_intercept);
594 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.errorfile-prefix"))) {
595 PATCH(errorfile_prefix);
596 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.assign"))) {
597 PATCH(mimetypes);
598 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-requests"))) {
599 PATCH(max_keep_alive_requests);
600 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-idle"))) {
601 PATCH(max_keep_alive_idle);
602 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-write-idle"))) {
603 PATCH(max_write_idle);
604 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-read-idle"))) {
605 PATCH(max_read_idle);
606 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-request-size"))) {
607 PATCH(max_request_size);
608 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.use-xattr"))) {
609 PATCH(use_xattr);
610 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-inode"))) {
611 PATCH(etag_use_inode);
612 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-mtime"))) {
613 PATCH(etag_use_mtime);
614 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-size"))) {
615 PATCH(etag_use_size);
616 #ifdef HAVE_LSTAT
617 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.follow-symlink"))) {
618 PATCH(follow_symlink);
619 #endif
620 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.name"))) {
621 buffer_copy_buffer(con->server_name, s->server_name);
622 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.tag"))) {
623 PATCH(server_tag);
624 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-request-body"))) {
625 PATCH(stream_request_body);
626 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-response-body"))) {
627 PATCH(stream_response_body);
628 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("connection.kbytes-per-second"))) {
629 PATCH(kbytes_per_second);
630 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-handling"))) {
631 PATCH(log_request_handling);
632 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-header"))) {
633 PATCH(log_request_header);
634 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-response-header"))) {
635 PATCH(log_response_header);
636 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-condition-handling"))) {
637 PATCH(log_condition_handling);
638 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-file-not-found"))) {
639 PATCH(log_file_not_found);
640 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-timeouts"))) {
641 PATCH(log_timeouts);
642 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.protocol-http11"))) {
643 PATCH(allow_http11);
644 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.force-lowercase-filenames"))) {
645 PATCH(force_lowercase_filenames);
646 #if 0 /*(not necessary; used only at startup)*/
647 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.listen-backlog"))) {
648 PATCH(listen_backlog);
649 #endif
650 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.kbytes-per-second"))) {
651 PATCH(global_kbytes_per_second);
652 PATCH(global_bytes_per_second_cnt);
653 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
658 con->etag_flags = (con->conf.etag_use_mtime ? ETAG_USE_MTIME : 0) |
659 (con->conf.etag_use_inode ? ETAG_USE_INODE : 0) |
660 (con->conf.etag_use_size ? ETAG_USE_SIZE : 0);
662 return 0;
664 #undef PATCH
666 typedef struct {
667 int foo;
668 int bar;
670 const buffer *source;
671 const char *input;
672 size_t offset;
673 size_t size;
675 int line_pos;
676 int line;
678 int in_key;
679 int in_brace;
680 int in_cond;
681 } tokenizer_t;
683 #if 0
684 static int tokenizer_open(server *srv, tokenizer_t *t, buffer *basedir, const char *fn) {
685 if (buffer_string_is_empty(basedir) ||
686 (fn[0] == '/' || fn[0] == '\\') ||
687 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\'))) {
688 t->file = buffer_init_string(fn);
689 } else {
690 t->file = buffer_init_buffer(basedir);
691 buffer_append_string(t->file, fn);
694 if (0 != stream_open(&(t->s), t->file)) {
695 log_error_write(srv, __FILE__, __LINE__, "sbss",
696 "opening configfile ", t->file, "failed:", strerror(errno));
697 buffer_free(t->file);
698 return -1;
701 t->input = t->s.start;
702 t->offset = 0;
703 t->size = t->s.size;
704 t->line = 1;
705 t->line_pos = 1;
707 t->in_key = 1;
708 t->in_brace = 0;
709 t->in_cond = 0;
710 return 0;
713 static int tokenizer_close(server *srv, tokenizer_t *t) {
714 UNUSED(srv);
716 buffer_free(t->file);
717 return stream_close(&(t->s));
719 #endif
720 static int config_skip_newline(tokenizer_t *t) {
721 int skipped = 1;
722 force_assert(t->input[t->offset] == '\r' || t->input[t->offset] == '\n');
723 if (t->input[t->offset] == '\r' && t->input[t->offset + 1] == '\n') {
724 skipped ++;
725 t->offset ++;
727 t->offset ++;
728 return skipped;
731 static int config_skip_comment(tokenizer_t *t) {
732 int i;
733 force_assert(t->input[t->offset] == '#');
734 for (i = 1; t->input[t->offset + i] &&
735 (t->input[t->offset + i] != '\n' && t->input[t->offset + i] != '\r');
736 i++);
737 t->offset += i;
738 return i;
741 static int config_tokenizer(server *srv, tokenizer_t *t, int *token_id, buffer *token) {
742 int tid = 0;
743 size_t i;
745 for (tid = 0; tid == 0 && t->offset < t->size && t->input[t->offset] ; ) {
746 char c = t->input[t->offset];
747 const char *start = NULL;
749 switch (c) {
750 case '=':
751 if (t->in_brace) {
752 if (t->input[t->offset + 1] == '>') {
753 t->offset += 2;
755 buffer_copy_string_len(token, CONST_STR_LEN("=>"));
757 tid = TK_ARRAY_ASSIGN;
758 } else {
759 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
760 "source:", t->source,
761 "line:", t->line, "pos:", t->line_pos,
762 "use => for assignments in arrays");
763 return -1;
765 } else if (t->in_cond) {
766 if (t->input[t->offset + 1] == '=') {
767 t->offset += 2;
769 buffer_copy_string_len(token, CONST_STR_LEN("=="));
771 tid = TK_EQ;
772 } else if (t->input[t->offset + 1] == '~') {
773 t->offset += 2;
775 buffer_copy_string_len(token, CONST_STR_LEN("=~"));
777 tid = TK_MATCH;
778 } else {
779 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
780 "source:", t->source,
781 "line:", t->line, "pos:", t->line_pos,
782 "only =~ and == are allowed in the condition");
783 return -1;
785 t->in_key = 1;
786 t->in_cond = 0;
787 } else if (t->in_key) {
788 tid = TK_ASSIGN;
790 buffer_copy_string_len(token, t->input + t->offset, 1);
792 t->offset++;
793 t->line_pos++;
794 } else {
795 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
796 "source:", t->source,
797 "line:", t->line, "pos:", t->line_pos,
798 "unexpected equal-sign: =");
799 return -1;
802 break;
803 case '!':
804 if (t->in_cond) {
805 if (t->input[t->offset + 1] == '=') {
806 t->offset += 2;
808 buffer_copy_string_len(token, CONST_STR_LEN("!="));
810 tid = TK_NE;
811 } else if (t->input[t->offset + 1] == '~') {
812 t->offset += 2;
814 buffer_copy_string_len(token, CONST_STR_LEN("!~"));
816 tid = TK_NOMATCH;
817 } else {
818 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
819 "source:", t->source,
820 "line:", t->line, "pos:", t->line_pos,
821 "only !~ and != are allowed in the condition");
822 return -1;
824 t->in_key = 1;
825 t->in_cond = 0;
826 } else {
827 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
828 "source:", t->source,
829 "line:", t->line, "pos:", t->line_pos,
830 "unexpected exclamation-marks: !");
831 return -1;
834 break;
835 case '\t':
836 case ' ':
837 t->offset++;
838 t->line_pos++;
839 break;
840 case '\n':
841 case '\r':
842 if (t->in_brace == 0) {
843 int done = 0;
844 while (!done && t->offset < t->size) {
845 switch (t->input[t->offset]) {
846 case '\r':
847 case '\n':
848 config_skip_newline(t);
849 t->line_pos = 1;
850 t->line++;
851 break;
853 case '#':
854 t->line_pos += config_skip_comment(t);
855 break;
857 case '\t':
858 case ' ':
859 t->offset++;
860 t->line_pos++;
861 break;
863 default:
864 done = 1;
867 t->in_key = 1;
868 tid = TK_EOL;
869 buffer_copy_string_len(token, CONST_STR_LEN("(EOL)"));
870 } else {
871 config_skip_newline(t);
872 t->line_pos = 1;
873 t->line++;
875 break;
876 case ',':
877 if (t->in_brace > 0) {
878 tid = TK_COMMA;
880 buffer_copy_string_len(token, CONST_STR_LEN("(COMMA)"));
883 t->offset++;
884 t->line_pos++;
885 break;
886 case '"':
887 /* search for the terminating " */
888 start = t->input + t->offset + 1;
889 buffer_copy_string_len(token, CONST_STR_LEN(""));
891 for (i = 1; t->input[t->offset + i]; i++) {
892 if (t->input[t->offset + i] == '\\' &&
893 t->input[t->offset + i + 1] == '"') {
895 buffer_append_string_len(token, start, t->input + t->offset + i - start);
897 start = t->input + t->offset + i + 1;
899 /* skip the " */
900 i++;
901 continue;
905 if (t->input[t->offset + i] == '"') {
906 tid = TK_STRING;
908 buffer_append_string_len(token, start, t->input + t->offset + i - start);
910 break;
914 if (t->input[t->offset + i] == '\0') {
915 /* ERROR */
917 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
918 "source:", t->source,
919 "line:", t->line, "pos:", t->line_pos,
920 "missing closing quote");
922 return -1;
925 t->offset += i + 1;
926 t->line_pos += i + 1;
928 break;
929 case '(':
930 t->offset++;
931 t->in_brace++;
933 tid = TK_LPARAN;
935 buffer_copy_string_len(token, CONST_STR_LEN("("));
936 break;
937 case ')':
938 t->offset++;
939 t->in_brace--;
941 tid = TK_RPARAN;
943 buffer_copy_string_len(token, CONST_STR_LEN(")"));
944 break;
945 case '$':
946 t->offset++;
948 tid = TK_DOLLAR;
949 t->in_cond = 1;
950 t->in_key = 0;
952 buffer_copy_string_len(token, CONST_STR_LEN("$"));
954 break;
956 case '+':
957 if (t->input[t->offset + 1] == '=') {
958 t->offset += 2;
959 buffer_copy_string_len(token, CONST_STR_LEN("+="));
960 tid = TK_APPEND;
961 } else {
962 t->offset++;
963 tid = TK_PLUS;
964 buffer_copy_string_len(token, CONST_STR_LEN("+"));
966 break;
968 case ':':
969 if (t->input[t->offset+1] == '=') {
970 t->offset += 2;
971 tid = TK_FORCE_ASSIGN;
972 buffer_copy_string_len(token, CONST_STR_LEN(":="));
974 break;
976 case '{':
977 t->offset++;
979 tid = TK_LCURLY;
981 buffer_copy_string_len(token, CONST_STR_LEN("{"));
983 break;
985 case '}':
986 t->offset++;
988 tid = TK_RCURLY;
990 buffer_copy_string_len(token, CONST_STR_LEN("}"));
992 break;
994 case '[':
995 t->offset++;
997 tid = TK_LBRACKET;
999 buffer_copy_string_len(token, CONST_STR_LEN("["));
1001 break;
1003 case ']':
1004 t->offset++;
1006 tid = TK_RBRACKET;
1008 buffer_copy_string_len(token, CONST_STR_LEN("]"));
1010 break;
1011 case '#':
1012 t->line_pos += config_skip_comment(t);
1014 break;
1015 default:
1016 if (t->in_cond) {
1017 for (i = 0; t->input[t->offset + i] &&
1018 (isalpha((unsigned char)t->input[t->offset + i])
1019 ); i++);
1021 if (i && t->input[t->offset + i]) {
1022 tid = TK_SRVVARNAME;
1023 buffer_copy_string_len(token, t->input + t->offset, i);
1025 t->offset += i;
1026 t->line_pos += i;
1027 } else {
1028 /* ERROR */
1029 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
1030 "source:", t->source,
1031 "line:", t->line, "pos:", t->line_pos,
1032 "invalid character in condition");
1033 return -1;
1035 } else if (isdigit((unsigned char)c)) {
1036 /* take all digits */
1037 for (i = 0; t->input[t->offset + i] && isdigit((unsigned char)t->input[t->offset + i]); i++);
1039 /* was there it least a digit ? */
1040 if (i) {
1041 tid = TK_INTEGER;
1043 buffer_copy_string_len(token, t->input + t->offset, i);
1045 t->offset += i;
1046 t->line_pos += i;
1048 } else {
1049 /* the key might consist of [-.0-9a-z] */
1050 for (i = 0; t->input[t->offset + i] &&
1051 (isalnum((unsigned char)t->input[t->offset + i]) ||
1052 t->input[t->offset + i] == '.' ||
1053 t->input[t->offset + i] == '_' || /* for env.* */
1054 t->input[t->offset + i] == '-'
1055 ); i++);
1057 if (i && t->input[t->offset + i]) {
1058 buffer_copy_string_len(token, t->input + t->offset, i);
1060 if (strcmp(token->ptr, "include") == 0) {
1061 tid = TK_INCLUDE;
1062 } else if (strcmp(token->ptr, "include_shell") == 0) {
1063 tid = TK_INCLUDE_SHELL;
1064 } else if (strcmp(token->ptr, "global") == 0) {
1065 tid = TK_GLOBAL;
1066 } else if (strcmp(token->ptr, "else") == 0) {
1067 tid = TK_ELSE;
1068 } else {
1069 tid = TK_LKEY;
1072 t->offset += i;
1073 t->line_pos += i;
1074 } else {
1075 /* ERROR */
1076 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
1077 "source:", t->source,
1078 "line:", t->line, "pos:", t->line_pos,
1079 "invalid character in variable name");
1080 return -1;
1083 break;
1087 if (tid) {
1088 *token_id = tid;
1089 #if 0
1090 log_error_write(srv, __FILE__, __LINE__, "sbsdsdbdd",
1091 "source:", t->source,
1092 "line:", t->line, "pos:", t->line_pos,
1093 token, token->used - 1, tid);
1094 #endif
1096 return 1;
1097 } else if (t->offset < t->size) {
1098 fprintf(stderr, "%s.%d: %d, %s\n",
1099 __FILE__, __LINE__,
1100 tid, token->ptr);
1102 return 0;
1105 static int config_parse(server *srv, config_t *context, tokenizer_t *t) {
1106 void *pParser;
1107 int token_id;
1108 buffer *token, *lasttoken;
1109 int ret;
1111 pParser = configparserAlloc( malloc );
1112 force_assert(pParser);
1113 lasttoken = buffer_init();
1114 token = buffer_init();
1115 while((1 == (ret = config_tokenizer(srv, t, &token_id, token))) && context->ok) {
1116 buffer_copy_buffer(lasttoken, token);
1117 configparser(pParser, token_id, token, context);
1119 token = buffer_init();
1121 buffer_free(token);
1123 if (ret != -1 && context->ok) {
1124 /* add an EOL at EOF, better than say sorry */
1125 configparser(pParser, TK_EOL, buffer_init_string("(EOL)"), context);
1126 if (context->ok) {
1127 configparser(pParser, 0, NULL, context);
1130 configparserFree(pParser, free);
1132 if (ret == -1) {
1133 log_error_write(srv, __FILE__, __LINE__, "sb",
1134 "configfile parser failed at:", lasttoken);
1135 } else if (context->ok == 0) {
1136 log_error_write(srv, __FILE__, __LINE__, "sbsdsdsb",
1137 "source:", t->source,
1138 "line:", t->line, "pos:", t->line_pos,
1139 "parser failed somehow near here:", lasttoken);
1140 ret = -1;
1142 buffer_free(lasttoken);
1144 return ret == -1 ? -1 : 0;
1147 static int tokenizer_init(tokenizer_t *t, const buffer *source, const char *input, size_t size) {
1149 t->source = source;
1150 t->input = input;
1151 t->size = size;
1152 t->offset = 0;
1153 t->line = 1;
1154 t->line_pos = 1;
1156 t->in_key = 1;
1157 t->in_brace = 0;
1158 t->in_cond = 0;
1159 return 0;
1162 static int config_parse_file_stream(server *srv, config_t *context, const buffer *filename) {
1163 tokenizer_t t;
1164 stream s;
1165 int ret;
1167 if (0 != stream_open(&s, filename)) {
1168 log_error_write(srv, __FILE__, __LINE__, "sbss",
1169 "opening configfile ", filename, "failed:", strerror(errno));
1170 return -1;
1171 } else {
1172 tokenizer_init(&t, filename, s.start, s.size);
1173 ret = config_parse(srv, context, &t);
1176 stream_close(&s);
1177 return ret;
1180 int config_parse_file(server *srv, config_t *context, const char *fn) {
1181 buffer *filename;
1182 size_t i;
1183 int ret = -1;
1184 #ifdef GLOB_BRACE
1185 int flags = GLOB_BRACE;
1186 #else
1187 int flags = 0;
1188 #endif
1189 glob_t gl;
1191 if ((fn[0] == '/' || fn[0] == '\\') ||
1192 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\')) ||
1193 (fn[0] == '.' && fn[1] == '.' && (fn[2] == '/' || fn[2] == '\\'))) {
1194 filename = buffer_init_string(fn);
1195 } else {
1196 filename = buffer_init_buffer(context->basedir);
1197 buffer_append_string(filename, fn);
1200 switch (glob(filename->ptr, flags, NULL, &gl)) {
1201 case 0:
1202 for (i = 0; i < gl.gl_pathc; ++i) {
1203 buffer_copy_string(filename, gl.gl_pathv[i]);
1204 ret = config_parse_file_stream(srv, context, filename);
1205 if (0 != ret) break;
1207 globfree(&gl);
1208 break;
1209 case GLOB_NOMATCH:
1210 if (filename->ptr[strcspn(filename->ptr, "*?[]{}")] != '\0') { /*(contains glob metachars)*/
1211 ret = 0; /* not an error if no files match glob pattern */
1213 else {
1214 log_error_write(srv, __FILE__, __LINE__, "sb", "include file not found: ", filename);
1216 break;
1217 case GLOB_ABORTED:
1218 case GLOB_NOSPACE:
1219 log_error_write(srv, __FILE__, __LINE__, "sbss", "glob()", filename, "failed:", strerror(errno));
1220 break;
1223 buffer_free(filename);
1224 return ret;
1227 static char* getCWD(void) {
1228 char *s, *s1;
1229 size_t len;
1230 #ifdef PATH_MAX
1231 len = PATH_MAX;
1232 #else
1233 len = 4096;
1234 #endif
1236 s = malloc(len);
1237 if (!s) return NULL;
1238 while (NULL == getcwd(s, len)) {
1239 if (errno != ERANGE || SSIZE_MAX - len < len) {
1240 free(s);
1241 return NULL;
1243 len *= 2;
1244 s1 = realloc(s, len);
1245 if (!s1) {
1246 free(s);
1247 return NULL;
1249 s = s1;
1251 return s;
1254 int config_parse_cmd(server *srv, config_t *context, const char *cmd) {
1255 tokenizer_t t;
1256 int ret;
1257 buffer *source;
1258 buffer *out;
1259 char *oldpwd;
1261 if (NULL == (oldpwd = getCWD())) {
1262 log_error_write(srv, __FILE__, __LINE__, "s",
1263 "cannot get cwd", strerror(errno));
1264 return -1;
1267 if (!buffer_string_is_empty(context->basedir)) {
1268 if (0 != chdir(context->basedir->ptr)) {
1269 log_error_write(srv, __FILE__, __LINE__, "sbs",
1270 "cannot change directory to", context->basedir, strerror(errno));
1271 free(oldpwd);
1272 return -1;
1276 source = buffer_init_string(cmd);
1277 out = buffer_init();
1279 if (0 != proc_open_buffer(cmd, NULL, out, NULL)) {
1280 log_error_write(srv, __FILE__, __LINE__, "sbss",
1281 "opening", source, "failed:", strerror(errno));
1282 ret = -1;
1283 } else {
1284 tokenizer_init(&t, source, CONST_BUF_LEN(out));
1285 ret = config_parse(srv, context, &t);
1288 buffer_free(source);
1289 buffer_free(out);
1290 if (0 != chdir(oldpwd)) {
1291 log_error_write(srv, __FILE__, __LINE__, "sss",
1292 "cannot change directory to", oldpwd, strerror(errno));
1293 free(oldpwd);
1294 return -1;
1296 free(oldpwd);
1297 return ret;
1300 static void context_init(server *srv, config_t *context) {
1301 context->srv = srv;
1302 context->ok = 1;
1303 vector_config_weak_init(&context->configs_stack);
1304 context->basedir = buffer_init();
1307 static void context_free(config_t *context) {
1308 vector_config_weak_clear(&context->configs_stack);
1309 buffer_free(context->basedir);
1312 int config_read(server *srv, const char *fn) {
1313 config_t context;
1314 data_config *dc;
1315 data_integer *dpid;
1316 data_string *dcwd;
1317 int ret;
1318 char *pos;
1319 buffer *filename;
1321 context_init(srv, &context);
1322 context.all_configs = srv->config_context;
1324 #ifdef __WIN32
1325 pos = strrchr(fn, '\\');
1326 #else
1327 pos = strrchr(fn, '/');
1328 #endif
1329 if (pos) {
1330 buffer_copy_string_len(context.basedir, fn, pos - fn + 1);
1333 dc = data_config_init();
1334 buffer_copy_string_len(dc->key, CONST_STR_LEN("global"));
1336 force_assert(context.all_configs->used == 0);
1337 dc->context_ndx = context.all_configs->used;
1338 array_insert_unique(context.all_configs, (data_unset *)dc);
1339 context.current = dc;
1341 /* default context */
1342 dpid = data_integer_init();
1343 dpid->value = getpid();
1344 buffer_copy_string_len(dpid->key, CONST_STR_LEN("var.PID"));
1345 array_insert_unique(dc->value, (data_unset *)dpid);
1347 dcwd = data_string_init();
1348 buffer_string_prepare_copy(dcwd->value, 1023);
1349 if (NULL != getcwd(dcwd->value->ptr, dcwd->value->size - 1)) {
1350 buffer_commit(dcwd->value, strlen(dcwd->value->ptr));
1351 buffer_copy_string_len(dcwd->key, CONST_STR_LEN("var.CWD"));
1352 array_insert_unique(dc->value, (data_unset *)dcwd);
1353 } else {
1354 dcwd->free((data_unset*) dcwd);
1357 filename = buffer_init_string(fn);
1358 ret = config_parse_file_stream(srv, &context, filename);
1359 buffer_free(filename);
1361 /* remains nothing if parser is ok */
1362 force_assert(!(0 == ret && context.ok && 0 != context.configs_stack.used));
1363 context_free(&context);
1365 if (0 != ret) {
1366 return ret;
1369 if (0 != config_insert(srv)) {
1370 return -1;
1373 return 0;
1376 int config_set_defaults(server *srv) {
1377 size_t i;
1378 specific_config *s = srv->config_storage[0];
1379 struct stat st1, st2;
1381 struct ev_map { fdevent_handler_t et; const char *name; } event_handlers[] =
1383 /* - epoll is most reliable
1384 * - select works everywhere
1386 #ifdef USE_LINUX_EPOLL
1387 { FDEVENT_HANDLER_LINUX_SYSEPOLL, "linux-sysepoll" },
1388 #endif
1389 #ifdef USE_POLL
1390 { FDEVENT_HANDLER_POLL, "poll" },
1391 #endif
1392 #ifdef USE_SELECT
1393 { FDEVENT_HANDLER_SELECT, "select" },
1394 #endif
1395 #ifdef USE_LIBEV
1396 { FDEVENT_HANDLER_LIBEV, "libev" },
1397 #endif
1398 #ifdef USE_SOLARIS_DEVPOLL
1399 { FDEVENT_HANDLER_SOLARIS_DEVPOLL,"solaris-devpoll" },
1400 #endif
1401 #ifdef USE_SOLARIS_PORT
1402 { FDEVENT_HANDLER_SOLARIS_PORT, "solaris-eventports" },
1403 #endif
1404 #ifdef USE_FREEBSD_KQUEUE
1405 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "freebsd-kqueue" },
1406 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "kqueue" },
1407 #endif
1408 { FDEVENT_HANDLER_UNSET, NULL }
1411 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1412 if (-1 == stat(srv->srvconf.changeroot->ptr, &st1)) {
1413 log_error_write(srv, __FILE__, __LINE__, "sb",
1414 "server.chroot doesn't exist:", srv->srvconf.changeroot);
1415 return -1;
1417 if (!S_ISDIR(st1.st_mode)) {
1418 log_error_write(srv, __FILE__, __LINE__, "sb",
1419 "server.chroot isn't a directory:", srv->srvconf.changeroot);
1420 return -1;
1424 if (!srv->srvconf.upload_tempdirs->used) {
1425 data_string *ds = data_string_init();
1426 const char *tmpdir = getenv("TMPDIR");
1427 if (NULL == tmpdir) tmpdir = "/var/tmp";
1428 buffer_copy_string(ds->value, tmpdir);
1429 array_insert_unique(srv->srvconf.upload_tempdirs, (data_unset *)ds);
1432 if (srv->srvconf.upload_tempdirs->used) {
1433 buffer * const b = srv->tmp_buf;
1434 size_t len;
1435 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1436 buffer_copy_buffer(b, srv->srvconf.changeroot);
1437 buffer_append_slash(b);
1438 } else {
1439 buffer_reset(b);
1441 len = buffer_string_length(b);
1443 for (i = 0; i < srv->srvconf.upload_tempdirs->used; ++i) {
1444 const data_string * const ds = (data_string *)srv->srvconf.upload_tempdirs->data[i];
1445 buffer_string_set_length(b, len); /*(truncate)*/
1446 buffer_append_string_buffer(b, ds->value);
1447 if (-1 == stat(b->ptr, &st1)) {
1448 log_error_write(srv, __FILE__, __LINE__, "sb",
1449 "server.upload-dirs doesn't exist:", b);
1450 } else if (!S_ISDIR(st1.st_mode)) {
1451 log_error_write(srv, __FILE__, __LINE__, "sb",
1452 "server.upload-dirs isn't a directory:", b);
1457 chunkqueue_set_tempdirs_default(
1458 srv->srvconf.upload_tempdirs,
1459 srv->srvconf.upload_temp_file_size);
1461 if (buffer_string_is_empty(s->document_root)) {
1462 log_error_write(srv, __FILE__, __LINE__, "s",
1463 "a default document-root has to be set");
1465 return -1;
1468 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1470 buffer_to_lower(srv->tmp_buf);
1472 if (2 == s->force_lowercase_filenames) { /* user didn't configure it in global section? */
1473 s->force_lowercase_filenames = 0; /* default to 0 */
1475 if (0 == stat(srv->tmp_buf->ptr, &st1)) {
1476 int is_lower = 0;
1478 is_lower = buffer_is_equal(srv->tmp_buf, s->document_root);
1480 /* lower-case existed, check upper-case */
1481 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1483 buffer_to_upper(srv->tmp_buf);
1485 /* we have to handle the special case that upper and lower-casing results in the same filename
1486 * as in server.document-root = "/" or "/12345/" */
1488 if (is_lower && buffer_is_equal(srv->tmp_buf, s->document_root)) {
1489 /* lower-casing and upper-casing didn't result in
1490 * an other filename, no need to stat(),
1491 * just assume it is case-sensitive. */
1493 s->force_lowercase_filenames = 0;
1494 } else if (0 == stat(srv->tmp_buf->ptr, &st2)) {
1496 /* upper case exists too, doesn't the FS handle this ? */
1498 /* upper and lower have the same inode -> case-insensitve FS */
1500 if (st1.st_ino == st2.st_ino) {
1501 /* upper and lower have the same inode -> case-insensitve FS */
1503 s->force_lowercase_filenames = 1;
1509 if (srv->srvconf.port == 0) {
1510 srv->srvconf.port = s->ssl_enabled ? 443 : 80;
1513 if (buffer_string_is_empty(srv->srvconf.event_handler)) {
1514 /* choose a good default
1516 * the event_handler list is sorted by 'goodness'
1517 * taking the first available should be the best solution
1519 srv->event_handler = event_handlers[0].et;
1521 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1522 log_error_write(srv, __FILE__, __LINE__, "s",
1523 "sorry, there is no event handler for this system");
1525 return -1;
1527 } else {
1529 * User override
1532 for (i = 0; event_handlers[i].name; i++) {
1533 if (0 == strcmp(event_handlers[i].name, srv->srvconf.event_handler->ptr)) {
1534 srv->event_handler = event_handlers[i].et;
1535 break;
1539 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1540 log_error_write(srv, __FILE__, __LINE__, "sb",
1541 "the selected event-handler in unknown or not supported:",
1542 srv->srvconf.event_handler );
1544 return -1;
1548 return 0;