minor: make packdist.sh more convenient for me
[lighttpd.git] / src / configfile.c
blob2d024841fc7ad5261d5ce0ec7a5d35506872011d
1 #include "first.h"
3 #include "server.h"
4 #include "log.h"
5 #include "stream.h"
6 #include "plugin.h"
8 #include "configparser.h"
9 #include "configfile.h"
10 #include "proc_open.h"
11 #include "request.h"
12 #include "version.h"
14 #include <sys/stat.h>
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <limits.h>
24 #include <assert.h>
25 #include <glob.h>
28 static int config_insert(server *srv) {
29 size_t i;
30 int ret = 0;
31 buffer *stat_cache_string;
33 config_values_t cv[] = {
34 { "server.bind", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 0 */
35 { "server.errorlog", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 1 */
36 { "server.errorfile-prefix", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
37 { "server.chroot", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 3 */
38 { "server.username", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 4 */
39 { "server.groupname", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 5 */
40 { "server.port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 6 */
41 { "server.tag", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
42 { "server.use-ipv6", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
43 { "server.modules", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_SERVER }, /* 9 */
45 { "server.event-handler", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 10 */
46 { "server.pid-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 11 */
47 { "server.max-request-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
48 { "server.max-worker", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 13 */
49 { "server.document-root", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
50 { "server.force-lowercase-filenames", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 15 */
51 { "debug.log-condition-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 16 */
52 { "server.max-keep-alive-requests", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 17 */
53 { "server.name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 18 */
54 { "server.max-keep-alive-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 19 */
56 { "server.max-read-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 20 */
57 { "server.max-write-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 21 */
58 { "server.error-handler", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 22 */
59 { "server.max-fds", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 23 */
60 #ifdef HAVE_LSTAT
61 { "server.follow-symlink", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 24 */
62 #else
63 { "server.follow-symlink",
64 "Your system lacks lstat(). We can not differ symlinks from files."
65 "Please remove server.follow-symlinks from your config.",
66 T_CONFIG_UNSUPPORTED, T_CONFIG_SCOPE_UNSET },
67 #endif
68 { "server.kbytes-per-second", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 25 */
69 { "connection.kbytes-per-second", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 26 */
70 { "mimetype.use-xattr", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 27 */
71 { "mimetype.assign", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 28 */
72 { "ssl.pemfile", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 29 */
74 { "ssl.engine", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 30 */
75 { "debug.log-file-not-found", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 31 */
76 { "debug.log-request-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 32 */
77 { "debug.log-response-header", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 33 */
78 { "debug.log-request-header", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 34 */
79 { "debug.log-ssl-noise", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 35 */
80 { "server.protocol-http11", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 36 */
81 { "debug.log-request-header-on-error", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 37 */
82 { "debug.log-state-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 38 */
83 { "ssl.ca-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 39 */
85 { "server.errorlog-use-syslog", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 40 */
86 { "server.range-requests", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 41 */
87 { "server.stat-cache-engine", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 42 */
88 { "server.max-connections", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 43 */
89 { "server.network-backend", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 44 */
90 { "server.upload-dirs", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_SERVER }, /* 45 */
91 { "server.core-files", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 46 */
92 { "ssl.cipher-list", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 47 */
93 { "ssl.use-sslv2", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 48 */
94 { "etag.use-inode", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 49 */
96 { "etag.use-mtime", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 50 */
97 { "etag.use-size", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 51 */
98 { "server.reject-expect-100-with-417", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 52 */
99 { "debug.log-timeouts", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 53 */
100 { "server.defer-accept", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 54 */
101 { "server.breakagelog", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 55 */
102 { "ssl.verifyclient.activate", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 56 */
103 { "ssl.verifyclient.enforce", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 57 */
104 { "ssl.verifyclient.depth", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 58 */
105 { "ssl.verifyclient.username", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 59 */
107 { "ssl.verifyclient.exportcert", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 60 */
108 { "server.set-v6only", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 61 */
109 { "ssl.use-sslv3", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 62 */
110 { "ssl.dh-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 63 */
111 { "ssl.ec-curve", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 64 */
112 { "ssl.disable-client-renegotiation", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 65 */
113 { "ssl.honor-cipher-order", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 66 */
114 { "ssl.empty-fragments", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 67 */
115 { "server.upload-temp-file-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_SERVER }, /* 68 */
116 { "mimetype.xattr-name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 69 */
117 { "server.listen-backlog", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 70 */
118 { "server.error-handler-404", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 71 */
119 { "server.http-parseopt-header-strict",NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 72 */
120 { "server.http-parseopt-host-strict", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 73 */
121 { "server.http-parseopt-host-normalize",NULL,T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 74 */
122 { "server.bsd-accept-filter", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 75 */
123 { "server.stream-request-body", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 76 */
124 { "server.stream-response-body", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 77 */
125 { "server.max-request-field-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_SERVER }, /* 78 */
127 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
130 /* all T_CONFIG_SCOPE_SERVER options */
131 cv[0].destination = srv->srvconf.bindhost;
132 cv[1].destination = srv->srvconf.errorlog_file;
133 cv[3].destination = srv->srvconf.changeroot;
134 cv[4].destination = srv->srvconf.username;
135 cv[5].destination = srv->srvconf.groupname;
136 cv[6].destination = &(srv->srvconf.port);
137 cv[9].destination = srv->srvconf.modules;
139 cv[10].destination = srv->srvconf.event_handler;
140 cv[11].destination = srv->srvconf.pid_file;
141 cv[13].destination = &(srv->srvconf.max_worker);
143 cv[23].destination = &(srv->srvconf.max_fds);
145 cv[37].destination = &(srv->srvconf.log_request_header_on_error);
146 cv[38].destination = &(srv->srvconf.log_state_handling);
148 cv[40].destination = &(srv->srvconf.errorlog_use_syslog);
149 stat_cache_string = buffer_init();
150 cv[42].destination = stat_cache_string;
151 cv[43].destination = &(srv->srvconf.max_conns);
152 cv[44].destination = srv->srvconf.network_backend;
153 cv[45].destination = srv->srvconf.upload_tempdirs;
154 cv[46].destination = &(srv->srvconf.enable_cores);
156 cv[52].destination = &(srv->srvconf.reject_expect_100_with_417);
157 cv[55].destination = srv->srvconf.breakagelog_file;
159 cv[68].destination = &(srv->srvconf.upload_temp_file_size);
160 cv[69].destination = srv->srvconf.xattr_name;
161 cv[72].destination = &(srv->srvconf.http_header_strict);
162 cv[73].destination = &(srv->srvconf.http_host_strict);
163 cv[74].destination = &(srv->srvconf.http_host_normalize);
164 cv[78].destination = &(srv->srvconf.max_request_field_size);
166 srv->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
168 force_assert(srv->config_storage);
169 force_assert(srv->config_context->used); /* static analysis hint for ccc
170 -analyzer */
172 for (i = 0; i < srv->config_context->used; i++) {
173 data_config const* config = (data_config const*)srv->config_context->data[i];
174 specific_config *s;
176 s = calloc(1, sizeof(specific_config));
177 force_assert(s);
178 s->document_root = buffer_init();
179 s->mimetypes = array_init();
180 s->server_name = buffer_init();
181 s->ssl_pemfile = buffer_init();
182 s->ssl_ca_file = buffer_init();
183 s->error_handler = buffer_init();
184 s->error_handler_404 = buffer_init();
185 s->server_tag = buffer_init_string(PACKAGE_DESC);
186 s->ssl_cipher_list = buffer_init();
187 s->ssl_dh_file = buffer_init();
188 s->ssl_ec_curve = buffer_init();
189 s->errorfile_prefix = buffer_init();
190 #if defined(__FreeBSD__) || defined(__NetBSD__) \
191 || defined(__OpenBSD__) || defined(__DragonFly__)
192 s->bsd_accept_filter = (i == 0)
193 ? buffer_init()
194 : buffer_init_buffer(srv->config_storage[0]->bsd_accept_filter);
195 #endif
196 s->max_keep_alive_requests = 16;
197 s->max_keep_alive_idle = 5;
198 s->max_read_idle = 60;
199 s->max_write_idle = 360;
200 s->max_request_size = 0;
201 s->use_xattr = 0;
202 s->ssl_enabled = 0;
203 s->ssl_honor_cipher_order = 1;
204 s->ssl_empty_fragments = 0;
205 s->ssl_use_sslv2 = 0;
206 s->ssl_use_sslv3 = 0;
207 s->use_ipv6 = (i == 0) ? 0 : srv->config_storage[0]->use_ipv6;
208 s->set_v6only = (i == 0) ? 1 : srv->config_storage[0]->set_v6only;
209 s->defer_accept = (i == 0) ? 0 : srv->config_storage[0]->defer_accept;
210 #ifdef HAVE_LSTAT
211 s->follow_symlink = 1;
212 #endif
213 s->kbytes_per_second = 0;
214 s->allow_http11 = 1;
215 s->etag_use_inode = 1;
216 s->etag_use_mtime = 1;
217 s->etag_use_size = 1;
218 s->range_requests = 1;
219 s->force_lowercase_filenames = (i == 0) ? 2 : 0; /* we wan't to detect later if user changed this for global section */
220 s->global_kbytes_per_second = 0;
221 s->global_bytes_per_second_cnt = 0;
222 s->global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
223 s->ssl_verifyclient = 0;
224 s->ssl_verifyclient_enforce = 1;
225 s->ssl_verifyclient_username = buffer_init();
226 s->ssl_verifyclient_depth = 9;
227 s->ssl_verifyclient_export_cert = 0;
228 s->ssl_disable_client_renegotiation = 1;
229 s->listen_backlog = (0 == i ? 1024 : srv->config_storage[0]->listen_backlog);
230 s->stream_request_body = 0;
231 s->stream_response_body = 0;
233 /* all T_CONFIG_SCOPE_CONNECTION options */
234 cv[2].destination = s->errorfile_prefix;
235 cv[7].destination = s->server_tag;
236 cv[8].destination = &(s->use_ipv6);
238 cv[12].destination = &(s->max_request_size);
239 cv[14].destination = s->document_root;
240 cv[15].destination = &(s->force_lowercase_filenames);
241 cv[16].destination = &(s->log_condition_handling);
242 cv[17].destination = &(s->max_keep_alive_requests);
243 cv[18].destination = s->server_name;
244 cv[19].destination = &(s->max_keep_alive_idle);
246 cv[20].destination = &(s->max_read_idle);
247 cv[21].destination = &(s->max_write_idle);
248 cv[22].destination = s->error_handler;
249 #ifdef HAVE_LSTAT
250 cv[24].destination = &(s->follow_symlink);
251 #endif
252 cv[25].destination = &(s->global_kbytes_per_second);
253 cv[26].destination = &(s->kbytes_per_second);
254 cv[27].destination = &(s->use_xattr);
255 cv[28].destination = s->mimetypes;
256 cv[29].destination = s->ssl_pemfile;
258 cv[30].destination = &(s->ssl_enabled);
259 cv[31].destination = &(s->log_file_not_found);
260 cv[32].destination = &(s->log_request_handling);
261 cv[33].destination = &(s->log_response_header);
262 cv[34].destination = &(s->log_request_header);
263 cv[35].destination = &(s->log_ssl_noise);
264 cv[36].destination = &(s->allow_http11);
265 cv[39].destination = s->ssl_ca_file;
267 cv[41].destination = &(s->range_requests);
268 cv[47].destination = s->ssl_cipher_list;
269 cv[48].destination = &(s->ssl_use_sslv2);
270 cv[49].destination = &(s->etag_use_inode);
272 cv[50].destination = &(s->etag_use_mtime);
273 cv[51].destination = &(s->etag_use_size);
274 cv[53].destination = &(s->log_timeouts);
275 cv[54].destination = &(s->defer_accept);
276 cv[56].destination = &(s->ssl_verifyclient);
277 cv[57].destination = &(s->ssl_verifyclient_enforce);
278 cv[58].destination = &(s->ssl_verifyclient_depth);
279 cv[59].destination = s->ssl_verifyclient_username;
281 cv[60].destination = &(s->ssl_verifyclient_export_cert);
282 cv[61].destination = &(s->set_v6only);
283 cv[62].destination = &(s->ssl_use_sslv3);
284 cv[63].destination = s->ssl_dh_file;
285 cv[64].destination = s->ssl_ec_curve;
286 cv[65].destination = &(s->ssl_disable_client_renegotiation);
287 cv[66].destination = &(s->ssl_honor_cipher_order);
288 cv[67].destination = &(s->ssl_empty_fragments);
289 cv[70].destination = &(s->listen_backlog);
290 cv[71].destination = s->error_handler_404;
291 #if defined(__FreeBSD__) || defined(__NetBSD__) \
292 || defined(__OpenBSD__) || defined(__DragonFly__)
293 cv[75].destination = s->bsd_accept_filter;
294 #endif
295 cv[76].destination = &(s->stream_request_body);
296 cv[77].destination = &(s->stream_response_body);
298 srv->config_storage[i] = s;
300 if (0 != (ret = config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION))) {
301 break;
304 if (s->stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN) {
305 s->stream_request_body |= FDEVENT_STREAM_REQUEST;
307 if (s->stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) {
308 s->stream_response_body |= FDEVENT_STREAM_RESPONSE;
313 specific_config *s = srv->config_storage[0];
314 s->http_parseopts= /*(global, but stored in con->conf.http_parseopts)*/
315 (srv->srvconf.http_header_strict ?(HTTP_PARSEOPT_HEADER_STRICT) :0)
316 |(srv->srvconf.http_host_strict ?(HTTP_PARSEOPT_HOST_STRICT
317 |HTTP_PARSEOPT_HOST_NORMALIZE):0)
318 |(srv->srvconf.http_host_normalize ?(HTTP_PARSEOPT_HOST_NORMALIZE):0);
321 if (buffer_string_is_empty(stat_cache_string)) {
322 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
323 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("simple"))) {
324 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
325 #ifdef HAVE_FAM_H
326 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("fam"))) {
327 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_FAM;
328 #endif
329 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("disable"))) {
330 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_NONE;
331 } else {
332 log_error_write(srv, __FILE__, __LINE__, "sb",
333 "server.stat-cache-engine can be one of \"disable\", \"simple\","
334 #ifdef HAVE_FAM_H
335 " \"fam\","
336 #endif
337 " but not:", stat_cache_string);
338 ret = HANDLER_ERROR;
341 buffer_free(stat_cache_string);
344 data_string *ds;
345 int prepend_mod_indexfile = 1;
346 int append_mod_dirlisting = 1;
347 int append_mod_staticfile = 1;
348 int append_mod_authn_file = 1;
349 int append_mod_authn_ldap = 1;
350 int append_mod_authn_mysql = 1;
351 int contains_mod_auth = 0;
353 /* prepend default modules */
354 for (i = 0; i < srv->srvconf.modules->used; i++) {
355 ds = (data_string *)srv->srvconf.modules->data[i];
357 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_indexfile"))) {
358 prepend_mod_indexfile = 0;
361 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_staticfile"))) {
362 append_mod_staticfile = 0;
365 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_dirlisting"))) {
366 append_mod_dirlisting = 0;
369 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_authn_file"))) {
370 append_mod_authn_file = 0;
373 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_authn_ldap"))) {
374 append_mod_authn_ldap = 0;
377 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_authn_mysql"))) {
378 append_mod_authn_mysql = 0;
381 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_auth"))) {
382 contains_mod_auth = 1;
385 if (0 == prepend_mod_indexfile &&
386 0 == append_mod_dirlisting &&
387 0 == append_mod_staticfile &&
388 0 == append_mod_authn_file &&
389 0 == append_mod_authn_ldap &&
390 0 == append_mod_authn_mysql &&
391 1 == contains_mod_auth) {
392 break;
396 if (prepend_mod_indexfile) {
397 /* mod_indexfile has to be loaded before mod_fastcgi and friends */
398 array *modules = array_init();
400 ds = data_string_init();
401 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_indexfile"));
402 array_insert_unique(modules, (data_unset *)ds);
404 for (i = 0; i < srv->srvconf.modules->used; i++) {
405 data_unset *du = srv->srvconf.modules->data[i];
406 array_insert_unique(modules, du->copy(du));
409 array_free(srv->srvconf.modules);
410 srv->srvconf.modules = modules;
413 /* append default modules */
414 if (append_mod_dirlisting) {
415 ds = data_string_init();
416 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_dirlisting"));
417 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
420 if (append_mod_staticfile) {
421 ds = data_string_init();
422 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_staticfile"));
423 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
426 /* mod_auth.c,http_auth.c auth backends were split into separate modules
427 * Automatically load auth backend modules for compatibility with
428 * existing lighttpd 1.4.x configs */
429 if (contains_mod_auth) {
430 if (append_mod_authn_file) {
431 ds = data_string_init();
432 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_authn_file"));
433 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
435 if (append_mod_authn_ldap) {
436 #if defined(HAVE_LDAP_H) && defined(HAVE_LBER_H) && defined(HAVE_LIBLDAP) && defined(HAVE_LIBLBER)
437 ds = data_string_init();
438 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_authn_ldap"));
439 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
440 #endif
442 if (append_mod_authn_mysql) {
443 #if defined(HAVE_MYSQL)
444 ds = data_string_init();
445 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_authn_mysql"));
446 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
447 #endif
452 return ret;
457 #define PATCH(x) con->conf.x = s->x
458 int config_setup_connection(server *srv, connection *con) {
459 specific_config *s = srv->config_storage[0];
461 PATCH(http_parseopts);
463 PATCH(allow_http11);
464 PATCH(mimetypes);
465 PATCH(document_root);
466 PATCH(high_precision_timestamps);
467 PATCH(max_keep_alive_requests);
468 PATCH(max_keep_alive_idle);
469 PATCH(max_read_idle);
470 PATCH(max_write_idle);
471 PATCH(max_request_size);
472 PATCH(use_xattr);
473 PATCH(error_handler);
474 PATCH(error_handler_404);
475 PATCH(errorfile_prefix);
476 #ifdef HAVE_LSTAT
477 PATCH(follow_symlink);
478 #endif
479 PATCH(server_tag);
480 PATCH(kbytes_per_second);
481 PATCH(global_kbytes_per_second);
482 PATCH(global_bytes_per_second_cnt);
484 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
485 buffer_copy_buffer(con->server_name, s->server_name);
487 PATCH(log_request_header);
488 PATCH(log_response_header);
489 PATCH(log_request_handling);
490 PATCH(log_condition_handling);
491 PATCH(log_file_not_found);
492 PATCH(log_ssl_noise);
493 PATCH(log_timeouts);
495 PATCH(range_requests);
496 PATCH(force_lowercase_filenames);
497 /*PATCH(listen_backlog);*//*(not necessary; used only at startup)*/
498 PATCH(stream_request_body);
499 PATCH(stream_response_body);
501 PATCH(ssl_enabled);
503 PATCH(ssl_pemfile);
504 #ifdef USE_OPENSSL
505 PATCH(ssl_pemfile_x509);
506 PATCH(ssl_pemfile_pkey);
507 #endif
508 PATCH(ssl_ca_file);
509 #ifdef USE_OPENSSL
510 PATCH(ssl_ca_file_cert_names);
511 #endif
512 PATCH(ssl_cipher_list);
513 PATCH(ssl_dh_file);
514 PATCH(ssl_ec_curve);
515 PATCH(ssl_honor_cipher_order);
516 PATCH(ssl_empty_fragments);
517 PATCH(ssl_use_sslv2);
518 PATCH(ssl_use_sslv3);
519 PATCH(etag_use_inode);
520 PATCH(etag_use_mtime);
521 PATCH(etag_use_size);
523 PATCH(ssl_verifyclient);
524 PATCH(ssl_verifyclient_enforce);
525 PATCH(ssl_verifyclient_depth);
526 PATCH(ssl_verifyclient_username);
527 PATCH(ssl_verifyclient_export_cert);
528 PATCH(ssl_disable_client_renegotiation);
530 return 0;
533 int config_patch_connection(server *srv, connection *con) {
534 size_t i, j;
536 /* skip the first, the global context */
537 for (i = 1; i < srv->config_context->used; i++) {
538 data_config *dc = (data_config *)srv->config_context->data[i];
539 specific_config *s = srv->config_storage[i];
541 /* condition didn't match */
542 if (!config_check_cond(srv, con, dc)) continue;
544 /* merge config */
545 for (j = 0; j < dc->value->used; j++) {
546 data_unset *du = dc->value->data[j];
548 if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.document-root"))) {
549 PATCH(document_root);
550 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.range-requests"))) {
551 PATCH(range_requests);
552 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler"))) {
553 PATCH(error_handler);
554 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler-404"))) {
555 PATCH(error_handler_404);
556 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.errorfile-prefix"))) {
557 PATCH(errorfile_prefix);
558 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.assign"))) {
559 PATCH(mimetypes);
560 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-requests"))) {
561 PATCH(max_keep_alive_requests);
562 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-idle"))) {
563 PATCH(max_keep_alive_idle);
564 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-write-idle"))) {
565 PATCH(max_write_idle);
566 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-read-idle"))) {
567 PATCH(max_read_idle);
568 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-request-size"))) {
569 PATCH(max_request_size);
570 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.use-xattr"))) {
571 PATCH(use_xattr);
572 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-inode"))) {
573 PATCH(etag_use_inode);
574 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-mtime"))) {
575 PATCH(etag_use_mtime);
576 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-size"))) {
577 PATCH(etag_use_size);
578 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.pemfile"))) {
579 PATCH(ssl_pemfile);
580 #ifdef USE_OPENSSL
581 PATCH(ssl_pemfile_x509);
582 PATCH(ssl_pemfile_pkey);
583 #endif
584 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ca-file"))) {
585 PATCH(ssl_ca_file);
586 #ifdef USE_OPENSSL
587 PATCH(ssl_ca_file_cert_names);
588 #endif
589 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.honor-cipher-order"))) {
590 PATCH(ssl_honor_cipher_order);
591 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.empty-fragments"))) {
592 PATCH(ssl_empty_fragments);
593 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv2"))) {
594 PATCH(ssl_use_sslv2);
595 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv3"))) {
596 PATCH(ssl_use_sslv3);
597 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.cipher-list"))) {
598 PATCH(ssl_cipher_list);
599 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.engine"))) {
600 PATCH(ssl_enabled);
601 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.dh-file"))) {
602 PATCH(ssl_dh_file);
603 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ec-curve"))) {
604 PATCH(ssl_ec_curve);
605 #ifdef HAVE_LSTAT
606 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.follow-symlink"))) {
607 PATCH(follow_symlink);
608 #endif
609 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.name"))) {
610 buffer_copy_buffer(con->server_name, s->server_name);
611 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.tag"))) {
612 PATCH(server_tag);
613 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-request-body"))) {
614 PATCH(stream_request_body);
615 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-response-body"))) {
616 PATCH(stream_response_body);
617 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("connection.kbytes-per-second"))) {
618 PATCH(kbytes_per_second);
619 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-handling"))) {
620 PATCH(log_request_handling);
621 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-header"))) {
622 PATCH(log_request_header);
623 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-response-header"))) {
624 PATCH(log_response_header);
625 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-condition-handling"))) {
626 PATCH(log_condition_handling);
627 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-file-not-found"))) {
628 PATCH(log_file_not_found);
629 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-ssl-noise"))) {
630 PATCH(log_ssl_noise);
631 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-timeouts"))) {
632 PATCH(log_timeouts);
633 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.protocol-http11"))) {
634 PATCH(allow_http11);
635 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.force-lowercase-filenames"))) {
636 PATCH(force_lowercase_filenames);
637 #if 0 /*(not necessary; used only at startup)*/
638 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.listen-backlog"))) {
639 PATCH(listen_backlog);
640 #endif
641 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.kbytes-per-second"))) {
642 PATCH(global_kbytes_per_second);
643 PATCH(global_bytes_per_second_cnt);
644 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
645 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.activate"))) {
646 PATCH(ssl_verifyclient);
647 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.enforce"))) {
648 PATCH(ssl_verifyclient_enforce);
649 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.depth"))) {
650 PATCH(ssl_verifyclient_depth);
651 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.username"))) {
652 PATCH(ssl_verifyclient_username);
653 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.exportcert"))) {
654 PATCH(ssl_verifyclient_export_cert);
655 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.disable-client-renegotiation"))) {
656 PATCH(ssl_disable_client_renegotiation);
661 con->etag_flags = (con->conf.etag_use_mtime ? ETAG_USE_MTIME : 0) |
662 (con->conf.etag_use_inode ? ETAG_USE_INODE : 0) |
663 (con->conf.etag_use_size ? ETAG_USE_SIZE : 0);
665 return 0;
667 #undef PATCH
669 typedef struct {
670 int foo;
671 int bar;
673 const buffer *source;
674 const char *input;
675 size_t offset;
676 size_t size;
678 int line_pos;
679 int line;
681 int in_key;
682 int in_brace;
683 int in_cond;
684 } tokenizer_t;
686 #if 0
687 static int tokenizer_open(server *srv, tokenizer_t *t, buffer *basedir, const char *fn) {
688 if (buffer_string_is_empty(basedir) ||
689 (fn[0] == '/' || fn[0] == '\\') ||
690 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\'))) {
691 t->file = buffer_init_string(fn);
692 } else {
693 t->file = buffer_init_buffer(basedir);
694 buffer_append_string(t->file, fn);
697 if (0 != stream_open(&(t->s), t->file)) {
698 log_error_write(srv, __FILE__, __LINE__, "sbss",
699 "opening configfile ", t->file, "failed:", strerror(errno));
700 buffer_free(t->file);
701 return -1;
704 t->input = t->s.start;
705 t->offset = 0;
706 t->size = t->s.size;
707 t->line = 1;
708 t->line_pos = 1;
710 t->in_key = 1;
711 t->in_brace = 0;
712 t->in_cond = 0;
713 return 0;
716 static int tokenizer_close(server *srv, tokenizer_t *t) {
717 UNUSED(srv);
719 buffer_free(t->file);
720 return stream_close(&(t->s));
722 #endif
723 static int config_skip_newline(tokenizer_t *t) {
724 int skipped = 1;
725 force_assert(t->input[t->offset] == '\r' || t->input[t->offset] == '\n');
726 if (t->input[t->offset] == '\r' && t->input[t->offset + 1] == '\n') {
727 skipped ++;
728 t->offset ++;
730 t->offset ++;
731 return skipped;
734 static int config_skip_comment(tokenizer_t *t) {
735 int i;
736 force_assert(t->input[t->offset] == '#');
737 for (i = 1; t->input[t->offset + i] &&
738 (t->input[t->offset + i] != '\n' && t->input[t->offset + i] != '\r');
739 i++);
740 t->offset += i;
741 return i;
744 static int config_tokenizer(server *srv, tokenizer_t *t, int *token_id, buffer *token) {
745 int tid = 0;
746 size_t i;
748 for (tid = 0; tid == 0 && t->offset < t->size && t->input[t->offset] ; ) {
749 char c = t->input[t->offset];
750 const char *start = NULL;
752 switch (c) {
753 case '=':
754 if (t->in_brace) {
755 if (t->input[t->offset + 1] == '>') {
756 t->offset += 2;
758 buffer_copy_string_len(token, CONST_STR_LEN("=>"));
760 tid = TK_ARRAY_ASSIGN;
761 } else {
762 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
763 "source:", t->source,
764 "line:", t->line, "pos:", t->line_pos,
765 "use => for assignments in arrays");
766 return -1;
768 } else if (t->in_cond) {
769 if (t->input[t->offset + 1] == '=') {
770 t->offset += 2;
772 buffer_copy_string_len(token, CONST_STR_LEN("=="));
774 tid = TK_EQ;
775 } else if (t->input[t->offset + 1] == '~') {
776 t->offset += 2;
778 buffer_copy_string_len(token, CONST_STR_LEN("=~"));
780 tid = TK_MATCH;
781 } else {
782 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
783 "source:", t->source,
784 "line:", t->line, "pos:", t->line_pos,
785 "only =~ and == are allowed in the condition");
786 return -1;
788 t->in_key = 1;
789 t->in_cond = 0;
790 } else if (t->in_key) {
791 tid = TK_ASSIGN;
793 buffer_copy_string_len(token, t->input + t->offset, 1);
795 t->offset++;
796 t->line_pos++;
797 } else {
798 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
799 "source:", t->source,
800 "line:", t->line, "pos:", t->line_pos,
801 "unexpected equal-sign: =");
802 return -1;
805 break;
806 case '!':
807 if (t->in_cond) {
808 if (t->input[t->offset + 1] == '=') {
809 t->offset += 2;
811 buffer_copy_string_len(token, CONST_STR_LEN("!="));
813 tid = TK_NE;
814 } else if (t->input[t->offset + 1] == '~') {
815 t->offset += 2;
817 buffer_copy_string_len(token, CONST_STR_LEN("!~"));
819 tid = TK_NOMATCH;
820 } else {
821 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
822 "source:", t->source,
823 "line:", t->line, "pos:", t->line_pos,
824 "only !~ and != are allowed in the condition");
825 return -1;
827 t->in_key = 1;
828 t->in_cond = 0;
829 } else {
830 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
831 "source:", t->source,
832 "line:", t->line, "pos:", t->line_pos,
833 "unexpected exclamation-marks: !");
834 return -1;
837 break;
838 case '\t':
839 case ' ':
840 t->offset++;
841 t->line_pos++;
842 break;
843 case '\n':
844 case '\r':
845 if (t->in_brace == 0) {
846 int done = 0;
847 while (!done && t->offset < t->size) {
848 switch (t->input[t->offset]) {
849 case '\r':
850 case '\n':
851 config_skip_newline(t);
852 t->line_pos = 1;
853 t->line++;
854 break;
856 case '#':
857 t->line_pos += config_skip_comment(t);
858 break;
860 case '\t':
861 case ' ':
862 t->offset++;
863 t->line_pos++;
864 break;
866 default:
867 done = 1;
870 t->in_key = 1;
871 tid = TK_EOL;
872 buffer_copy_string_len(token, CONST_STR_LEN("(EOL)"));
873 } else {
874 config_skip_newline(t);
875 t->line_pos = 1;
876 t->line++;
878 break;
879 case ',':
880 if (t->in_brace > 0) {
881 tid = TK_COMMA;
883 buffer_copy_string_len(token, CONST_STR_LEN("(COMMA)"));
886 t->offset++;
887 t->line_pos++;
888 break;
889 case '"':
890 /* search for the terminating " */
891 start = t->input + t->offset + 1;
892 buffer_copy_string_len(token, CONST_STR_LEN(""));
894 for (i = 1; t->input[t->offset + i]; i++) {
895 if (t->input[t->offset + i] == '\\' &&
896 t->input[t->offset + i + 1] == '"') {
898 buffer_append_string_len(token, start, t->input + t->offset + i - start);
900 start = t->input + t->offset + i + 1;
902 /* skip the " */
903 i++;
904 continue;
908 if (t->input[t->offset + i] == '"') {
909 tid = TK_STRING;
911 buffer_append_string_len(token, start, t->input + t->offset + i - start);
913 break;
917 if (t->input[t->offset + i] == '\0') {
918 /* ERROR */
920 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
921 "source:", t->source,
922 "line:", t->line, "pos:", t->line_pos,
923 "missing closing quote");
925 return -1;
928 t->offset += i + 1;
929 t->line_pos += i + 1;
931 break;
932 case '(':
933 t->offset++;
934 t->in_brace++;
936 tid = TK_LPARAN;
938 buffer_copy_string_len(token, CONST_STR_LEN("("));
939 break;
940 case ')':
941 t->offset++;
942 t->in_brace--;
944 tid = TK_RPARAN;
946 buffer_copy_string_len(token, CONST_STR_LEN(")"));
947 break;
948 case '$':
949 t->offset++;
951 tid = TK_DOLLAR;
952 t->in_cond = 1;
953 t->in_key = 0;
955 buffer_copy_string_len(token, CONST_STR_LEN("$"));
957 break;
959 case '+':
960 if (t->input[t->offset + 1] == '=') {
961 t->offset += 2;
962 buffer_copy_string_len(token, CONST_STR_LEN("+="));
963 tid = TK_APPEND;
964 } else {
965 t->offset++;
966 tid = TK_PLUS;
967 buffer_copy_string_len(token, CONST_STR_LEN("+"));
969 break;
971 case '{':
972 t->offset++;
974 tid = TK_LCURLY;
976 buffer_copy_string_len(token, CONST_STR_LEN("{"));
978 break;
980 case '}':
981 t->offset++;
983 tid = TK_RCURLY;
985 buffer_copy_string_len(token, CONST_STR_LEN("}"));
987 break;
989 case '[':
990 t->offset++;
992 tid = TK_LBRACKET;
994 buffer_copy_string_len(token, CONST_STR_LEN("["));
996 break;
998 case ']':
999 t->offset++;
1001 tid = TK_RBRACKET;
1003 buffer_copy_string_len(token, CONST_STR_LEN("]"));
1005 break;
1006 case '#':
1007 t->line_pos += config_skip_comment(t);
1009 break;
1010 default:
1011 if (t->in_cond) {
1012 for (i = 0; t->input[t->offset + i] &&
1013 (isalpha((unsigned char)t->input[t->offset + i])
1014 ); i++);
1016 if (i && t->input[t->offset + i]) {
1017 tid = TK_SRVVARNAME;
1018 buffer_copy_string_len(token, t->input + t->offset, i);
1020 t->offset += i;
1021 t->line_pos += i;
1022 } else {
1023 /* ERROR */
1024 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
1025 "source:", t->source,
1026 "line:", t->line, "pos:", t->line_pos,
1027 "invalid character in condition");
1028 return -1;
1030 } else if (isdigit((unsigned char)c)) {
1031 /* take all digits */
1032 for (i = 0; t->input[t->offset + i] && isdigit((unsigned char)t->input[t->offset + i]); i++);
1034 /* was there it least a digit ? */
1035 if (i) {
1036 tid = TK_INTEGER;
1038 buffer_copy_string_len(token, t->input + t->offset, i);
1040 t->offset += i;
1041 t->line_pos += i;
1043 } else {
1044 /* the key might consist of [-.0-9a-z] */
1045 for (i = 0; t->input[t->offset + i] &&
1046 (isalnum((unsigned char)t->input[t->offset + i]) ||
1047 t->input[t->offset + i] == '.' ||
1048 t->input[t->offset + i] == '_' || /* for env.* */
1049 t->input[t->offset + i] == '-'
1050 ); i++);
1052 if (i && t->input[t->offset + i]) {
1053 buffer_copy_string_len(token, t->input + t->offset, i);
1055 if (strcmp(token->ptr, "include") == 0) {
1056 tid = TK_INCLUDE;
1057 } else if (strcmp(token->ptr, "include_shell") == 0) {
1058 tid = TK_INCLUDE_SHELL;
1059 } else if (strcmp(token->ptr, "global") == 0) {
1060 tid = TK_GLOBAL;
1061 } else if (strcmp(token->ptr, "else") == 0) {
1062 tid = TK_ELSE;
1063 } else {
1064 tid = TK_LKEY;
1067 t->offset += i;
1068 t->line_pos += i;
1069 } else {
1070 /* ERROR */
1071 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
1072 "source:", t->source,
1073 "line:", t->line, "pos:", t->line_pos,
1074 "invalid character in variable name");
1075 return -1;
1078 break;
1082 if (tid) {
1083 *token_id = tid;
1084 #if 0
1085 log_error_write(srv, __FILE__, __LINE__, "sbsdsdbdd",
1086 "source:", t->source,
1087 "line:", t->line, "pos:", t->line_pos,
1088 token, token->used - 1, tid);
1089 #endif
1091 return 1;
1092 } else if (t->offset < t->size) {
1093 fprintf(stderr, "%s.%d: %d, %s\n",
1094 __FILE__, __LINE__,
1095 tid, token->ptr);
1097 return 0;
1100 static int config_parse(server *srv, config_t *context, tokenizer_t *t) {
1101 void *pParser;
1102 int token_id;
1103 buffer *token, *lasttoken;
1104 int ret;
1106 pParser = configparserAlloc( malloc );
1107 force_assert(pParser);
1108 lasttoken = buffer_init();
1109 token = buffer_init();
1110 while((1 == (ret = config_tokenizer(srv, t, &token_id, token))) && context->ok) {
1111 buffer_copy_buffer(lasttoken, token);
1112 configparser(pParser, token_id, token, context);
1114 token = buffer_init();
1116 buffer_free(token);
1118 if (ret != -1 && context->ok) {
1119 /* add an EOL at EOF, better than say sorry */
1120 configparser(pParser, TK_EOL, buffer_init_string("(EOL)"), context);
1121 if (context->ok) {
1122 configparser(pParser, 0, NULL, context);
1125 configparserFree(pParser, free);
1127 if (ret == -1) {
1128 log_error_write(srv, __FILE__, __LINE__, "sb",
1129 "configfile parser failed at:", lasttoken);
1130 } else if (context->ok == 0) {
1131 log_error_write(srv, __FILE__, __LINE__, "sbsdsdsb",
1132 "source:", t->source,
1133 "line:", t->line, "pos:", t->line_pos,
1134 "parser failed somehow near here:", lasttoken);
1135 ret = -1;
1137 buffer_free(lasttoken);
1139 return ret == -1 ? -1 : 0;
1142 static int tokenizer_init(tokenizer_t *t, const buffer *source, const char *input, size_t size) {
1144 t->source = source;
1145 t->input = input;
1146 t->size = size;
1147 t->offset = 0;
1148 t->line = 1;
1149 t->line_pos = 1;
1151 t->in_key = 1;
1152 t->in_brace = 0;
1153 t->in_cond = 0;
1154 return 0;
1157 static int config_parse_file_stream(server *srv, config_t *context, const buffer *filename) {
1158 tokenizer_t t;
1159 stream s;
1160 int ret;
1162 if (0 != stream_open(&s, filename)) {
1163 log_error_write(srv, __FILE__, __LINE__, "sbss",
1164 "opening configfile ", filename, "failed:", strerror(errno));
1165 return -1;
1166 } else {
1167 tokenizer_init(&t, filename, s.start, s.size);
1168 ret = config_parse(srv, context, &t);
1171 stream_close(&s);
1172 return ret;
1175 int config_parse_file(server *srv, config_t *context, const char *fn) {
1176 buffer *filename;
1177 size_t i;
1178 int ret = -1;
1179 #ifdef GLOB_BRACE
1180 int flags = GLOB_BRACE;
1181 #else
1182 int flags = 0;
1183 #endif
1184 glob_t gl;
1186 if ((fn[0] == '/' || fn[0] == '\\') ||
1187 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\')) ||
1188 (fn[0] == '.' && fn[1] == '.' && (fn[2] == '/' || fn[2] == '\\'))) {
1189 filename = buffer_init_string(fn);
1190 } else {
1191 filename = buffer_init_buffer(context->basedir);
1192 buffer_append_string(filename, fn);
1195 switch (glob(filename->ptr, flags, NULL, &gl)) {
1196 case 0:
1197 for (i = 0; i < gl.gl_pathc; ++i) {
1198 buffer_copy_string(filename, gl.gl_pathv[i]);
1199 ret = config_parse_file_stream(srv, context, filename);
1200 if (0 != ret) break;
1202 globfree(&gl);
1203 break;
1204 case GLOB_NOMATCH:
1205 if (filename->ptr[strcspn(filename->ptr, "*?[]{}")] != '\0') { /*(contains glob metachars)*/
1206 ret = 0; /* not an error if no files match glob pattern */
1208 else {
1209 log_error_write(srv, __FILE__, __LINE__, "sb", "include file not found: ", filename);
1211 break;
1212 case GLOB_ABORTED:
1213 case GLOB_NOSPACE:
1214 log_error_write(srv, __FILE__, __LINE__, "sbss", "glob()", filename, "failed:", strerror(errno));
1215 break;
1218 buffer_free(filename);
1219 return ret;
1222 static char* getCWD(void) {
1223 char *s, *s1;
1224 size_t len;
1225 #ifdef PATH_MAX
1226 len = PATH_MAX;
1227 #else
1228 len = 4096;
1229 #endif
1231 s = malloc(len);
1232 if (!s) return NULL;
1233 while (NULL == getcwd(s, len)) {
1234 if (errno != ERANGE || SSIZE_MAX - len < len) {
1235 free(s);
1236 return NULL;
1238 len *= 2;
1239 s1 = realloc(s, len);
1240 if (!s1) {
1241 free(s);
1242 return NULL;
1244 s = s1;
1246 return s;
1249 int config_parse_cmd(server *srv, config_t *context, const char *cmd) {
1250 tokenizer_t t;
1251 int ret;
1252 buffer *source;
1253 buffer *out;
1254 char *oldpwd;
1256 if (NULL == (oldpwd = getCWD())) {
1257 log_error_write(srv, __FILE__, __LINE__, "s",
1258 "cannot get cwd", strerror(errno));
1259 return -1;
1262 if (!buffer_string_is_empty(context->basedir)) {
1263 if (0 != chdir(context->basedir->ptr)) {
1264 log_error_write(srv, __FILE__, __LINE__, "sbs",
1265 "cannot change directory to", context->basedir, strerror(errno));
1266 free(oldpwd);
1267 return -1;
1271 source = buffer_init_string(cmd);
1272 out = buffer_init();
1274 if (0 != proc_open_buffer(cmd, NULL, out, NULL)) {
1275 log_error_write(srv, __FILE__, __LINE__, "sbss",
1276 "opening", source, "failed:", strerror(errno));
1277 ret = -1;
1278 } else {
1279 tokenizer_init(&t, source, CONST_BUF_LEN(out));
1280 ret = config_parse(srv, context, &t);
1283 buffer_free(source);
1284 buffer_free(out);
1285 if (0 != chdir(oldpwd)) {
1286 log_error_write(srv, __FILE__, __LINE__, "sss",
1287 "cannot change directory to", oldpwd, strerror(errno));
1288 free(oldpwd);
1289 return -1;
1291 free(oldpwd);
1292 return ret;
1295 static void context_init(server *srv, config_t *context) {
1296 context->srv = srv;
1297 context->ok = 1;
1298 vector_config_weak_init(&context->configs_stack);
1299 context->basedir = buffer_init();
1302 static void context_free(config_t *context) {
1303 vector_config_weak_clear(&context->configs_stack);
1304 buffer_free(context->basedir);
1307 int config_read(server *srv, const char *fn) {
1308 config_t context;
1309 data_config *dc;
1310 data_integer *dpid;
1311 data_string *dcwd;
1312 int ret;
1313 char *pos;
1314 buffer *filename;
1316 context_init(srv, &context);
1317 context.all_configs = srv->config_context;
1319 #ifdef __WIN32
1320 pos = strrchr(fn, '\\');
1321 #else
1322 pos = strrchr(fn, '/');
1323 #endif
1324 if (pos) {
1325 buffer_copy_string_len(context.basedir, fn, pos - fn + 1);
1328 dc = data_config_init();
1329 buffer_copy_string_len(dc->key, CONST_STR_LEN("global"));
1331 force_assert(context.all_configs->used == 0);
1332 dc->context_ndx = context.all_configs->used;
1333 array_insert_unique(context.all_configs, (data_unset *)dc);
1334 context.current = dc;
1336 /* default context */
1337 dpid = data_integer_init();
1338 dpid->value = getpid();
1339 buffer_copy_string_len(dpid->key, CONST_STR_LEN("var.PID"));
1340 array_insert_unique(dc->value, (data_unset *)dpid);
1342 dcwd = data_string_init();
1343 buffer_string_prepare_copy(dcwd->value, 1023);
1344 if (NULL != getcwd(dcwd->value->ptr, dcwd->value->size - 1)) {
1345 buffer_commit(dcwd->value, strlen(dcwd->value->ptr));
1346 buffer_copy_string_len(dcwd->key, CONST_STR_LEN("var.CWD"));
1347 array_insert_unique(dc->value, (data_unset *)dcwd);
1348 } else {
1349 dcwd->free((data_unset*) dcwd);
1352 filename = buffer_init_string(fn);
1353 ret = config_parse_file_stream(srv, &context, filename);
1354 buffer_free(filename);
1356 /* remains nothing if parser is ok */
1357 force_assert(!(0 == ret && context.ok && 0 != context.configs_stack.used));
1358 context_free(&context);
1360 if (0 != ret) {
1361 return ret;
1364 if (0 != config_insert(srv)) {
1365 return -1;
1368 return 0;
1371 int config_set_defaults(server *srv) {
1372 size_t i;
1373 specific_config *s = srv->config_storage[0];
1374 struct stat st1, st2;
1376 struct ev_map { fdevent_handler_t et; const char *name; } event_handlers[] =
1378 /* - epoll is most reliable
1379 * - select works everywhere
1381 #ifdef USE_LINUX_EPOLL
1382 { FDEVENT_HANDLER_LINUX_SYSEPOLL, "linux-sysepoll" },
1383 #endif
1384 #ifdef USE_POLL
1385 { FDEVENT_HANDLER_POLL, "poll" },
1386 #endif
1387 #ifdef USE_SELECT
1388 { FDEVENT_HANDLER_SELECT, "select" },
1389 #endif
1390 #ifdef USE_LIBEV
1391 { FDEVENT_HANDLER_LIBEV, "libev" },
1392 #endif
1393 #ifdef USE_SOLARIS_DEVPOLL
1394 { FDEVENT_HANDLER_SOLARIS_DEVPOLL,"solaris-devpoll" },
1395 #endif
1396 #ifdef USE_SOLARIS_PORT
1397 { FDEVENT_HANDLER_SOLARIS_PORT, "solaris-eventports" },
1398 #endif
1399 #ifdef USE_FREEBSD_KQUEUE
1400 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "freebsd-kqueue" },
1401 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "kqueue" },
1402 #endif
1403 { FDEVENT_HANDLER_UNSET, NULL }
1406 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1407 if (-1 == stat(srv->srvconf.changeroot->ptr, &st1)) {
1408 log_error_write(srv, __FILE__, __LINE__, "sb",
1409 "server.chroot doesn't exist:", srv->srvconf.changeroot);
1410 return -1;
1412 if (!S_ISDIR(st1.st_mode)) {
1413 log_error_write(srv, __FILE__, __LINE__, "sb",
1414 "server.chroot isn't a directory:", srv->srvconf.changeroot);
1415 return -1;
1419 if (!srv->srvconf.upload_tempdirs->used) {
1420 data_string *ds = data_string_init();
1421 const char *tmpdir = getenv("TMPDIR");
1422 if (NULL == tmpdir) tmpdir = "/var/tmp";
1423 buffer_copy_string(ds->value, tmpdir);
1424 array_insert_unique(srv->srvconf.upload_tempdirs, (data_unset *)ds);
1427 if (srv->srvconf.upload_tempdirs->used) {
1428 buffer * const b = srv->tmp_buf;
1429 size_t len;
1430 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1431 buffer_copy_buffer(b, srv->srvconf.changeroot);
1432 buffer_append_slash(b);
1433 } else {
1434 buffer_reset(b);
1436 len = buffer_string_length(b);
1438 for (i = 0; i < srv->srvconf.upload_tempdirs->used; ++i) {
1439 const data_string * const ds = (data_string *)srv->srvconf.upload_tempdirs->data[i];
1440 buffer_string_set_length(b, len); /*(truncate)*/
1441 buffer_append_string_buffer(b, ds->value);
1442 if (-1 == stat(b->ptr, &st1)) {
1443 log_error_write(srv, __FILE__, __LINE__, "sb",
1444 "server.upload-dirs doesn't exist:", b);
1445 } else if (!S_ISDIR(st1.st_mode)) {
1446 log_error_write(srv, __FILE__, __LINE__, "sb",
1447 "server.upload-dirs isn't a directory:", b);
1452 chunkqueue_set_tempdirs_default(
1453 srv->srvconf.upload_tempdirs,
1454 srv->srvconf.upload_temp_file_size);
1456 if (buffer_string_is_empty(s->document_root)) {
1457 log_error_write(srv, __FILE__, __LINE__, "s",
1458 "a default document-root has to be set");
1460 return -1;
1463 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1465 buffer_to_lower(srv->tmp_buf);
1467 if (2 == s->force_lowercase_filenames) { /* user didn't configure it in global section? */
1468 s->force_lowercase_filenames = 0; /* default to 0 */
1470 if (0 == stat(srv->tmp_buf->ptr, &st1)) {
1471 int is_lower = 0;
1473 is_lower = buffer_is_equal(srv->tmp_buf, s->document_root);
1475 /* lower-case existed, check upper-case */
1476 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1478 buffer_to_upper(srv->tmp_buf);
1480 /* we have to handle the special case that upper and lower-casing results in the same filename
1481 * as in server.document-root = "/" or "/12345/" */
1483 if (is_lower && buffer_is_equal(srv->tmp_buf, s->document_root)) {
1484 /* lower-casing and upper-casing didn't result in
1485 * an other filename, no need to stat(),
1486 * just assume it is case-sensitive. */
1488 s->force_lowercase_filenames = 0;
1489 } else if (0 == stat(srv->tmp_buf->ptr, &st2)) {
1491 /* upper case exists too, doesn't the FS handle this ? */
1493 /* upper and lower have the same inode -> case-insensitve FS */
1495 if (st1.st_ino == st2.st_ino) {
1496 /* upper and lower have the same inode -> case-insensitve FS */
1498 s->force_lowercase_filenames = 1;
1504 if (srv->srvconf.port == 0) {
1505 srv->srvconf.port = s->ssl_enabled ? 443 : 80;
1508 if (buffer_string_is_empty(srv->srvconf.event_handler)) {
1509 /* choose a good default
1511 * the event_handler list is sorted by 'goodness'
1512 * taking the first available should be the best solution
1514 srv->event_handler = event_handlers[0].et;
1516 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1517 log_error_write(srv, __FILE__, __LINE__, "s",
1518 "sorry, there is no event handler for this system");
1520 return -1;
1522 } else {
1524 * User override
1527 for (i = 0; event_handlers[i].name; i++) {
1528 if (0 == strcmp(event_handlers[i].name, srv->srvconf.event_handler->ptr)) {
1529 srv->event_handler = event_handlers[i].et;
1530 break;
1534 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1535 log_error_write(srv, __FILE__, __LINE__, "sb",
1536 "the selected event-handler in unknown or not supported:",
1537 srv->srvconf.event_handler );
1539 return -1;
1543 if (s->ssl_enabled) {
1544 if (buffer_string_is_empty(s->ssl_pemfile)) {
1545 /* PEM file is require */
1547 log_error_write(srv, __FILE__, __LINE__, "s",
1548 "ssl.pemfile has to be set");
1549 return -1;
1552 #ifndef USE_OPENSSL
1553 log_error_write(srv, __FILE__, __LINE__, "s",
1554 "ssl support is missing, recompile with --with-openssl");
1556 return -1;
1557 #endif
1560 return 0;