[mod_ssi] produce content in subrequest hook
[lighttpd.git] / src / configfile.c
blobf40248f30d2dfb797439557076e7f0af6f851789
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 #if defined(HAVE_MYSQL) || (defined(HAVE_LDAP_H) && defined(HAVE_LBER_H) && defined(HAVE_LIBLDAP) && defined(HAVE_LIBLBER))
29 static void config_warn_authn_module (server *srv, const char *module) {
30 size_t len = strlen(module);
31 for (size_t i = 0; i < srv->config_context->used; ++i) {
32 const data_config *config = (data_config const*)srv->config_context->data[i];
33 const data_unset *du = array_get_element(config->value, "auth.backend");
34 if (NULL != du && du->type == TYPE_STRING) {
35 data_string *ds = (data_string *)du;
36 if (buffer_is_equal_string(ds->value, module, len)) {
37 ds = data_string_init();
38 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_authn_"));
39 buffer_append_string(ds->value, module);
40 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
41 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, "\".");
42 return;
47 #endif
49 static int config_insert(server *srv) {
50 size_t i;
51 int ret = 0;
52 buffer *stat_cache_string;
54 config_values_t cv[] = {
55 { "server.bind", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 0 */
56 { "server.errorlog", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 1 */
57 { "server.errorfile-prefix", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
58 { "server.chroot", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 3 */
59 { "server.username", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 4 */
60 { "server.groupname", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 5 */
61 { "server.port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 6 */
62 { "server.tag", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
63 { "server.use-ipv6", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
64 { "server.modules", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_SERVER }, /* 9 */
66 { "server.event-handler", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 10 */
67 { "server.pid-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 11 */
68 { "server.max-request-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
69 { "server.max-worker", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 13 */
70 { "server.document-root", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
71 { "server.force-lowercase-filenames", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 15 */
72 { "debug.log-condition-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 16 */
73 { "server.max-keep-alive-requests", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 17 */
74 { "server.name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 18 */
75 { "server.max-keep-alive-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 19 */
77 { "server.max-read-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 20 */
78 { "server.max-write-idle", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 21 */
79 { "server.error-handler", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 22 */
80 { "server.max-fds", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 23 */
81 #ifdef HAVE_LSTAT
82 { "server.follow-symlink", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 24 */
83 #else
84 { "server.follow-symlink",
85 "Your system lacks lstat(). We can not differ symlinks from files."
86 "Please remove server.follow-symlinks from your config.",
87 T_CONFIG_UNSUPPORTED, T_CONFIG_SCOPE_UNSET },
88 #endif
89 { "server.kbytes-per-second", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 25 */
90 { "connection.kbytes-per-second", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 26 */
91 { "mimetype.use-xattr", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 27 */
92 { "mimetype.assign", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 28 */
93 { "ssl.pemfile", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 29 */
95 { "ssl.engine", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 30 */
96 { "debug.log-file-not-found", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 31 */
97 { "debug.log-request-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 32 */
98 { "debug.log-response-header", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 33 */
99 { "debug.log-request-header", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 34 */
100 { "debug.log-ssl-noise", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 35 */
101 { "server.protocol-http11", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 36 */
102 { "debug.log-request-header-on-error", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 37 */
103 { "debug.log-state-handling", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 38 */
104 { "ssl.ca-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 39 */
106 { "server.errorlog-use-syslog", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 40 */
107 { "server.range-requests", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 41 */
108 { "server.stat-cache-engine", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 42 */
109 { "server.max-connections", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_SERVER }, /* 43 */
110 { "server.network-backend", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 44 */
111 { "server.upload-dirs", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_SERVER }, /* 45 */
112 { "server.core-files", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 46 */
113 { "ssl.cipher-list", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 47 */
114 { "ssl.use-sslv2", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 48 */
115 { "etag.use-inode", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 49 */
117 { "etag.use-mtime", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 50 */
118 { "etag.use-size", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 51 */
119 { "server.reject-expect-100-with-417", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 52 */
120 { "debug.log-timeouts", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 53 */
121 { "server.defer-accept", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 54 */
122 { "server.breakagelog", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 55 */
123 { "ssl.verifyclient.activate", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 56 */
124 { "ssl.verifyclient.enforce", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 57 */
125 { "ssl.verifyclient.depth", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 58 */
126 { "ssl.verifyclient.username", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 59 */
128 { "ssl.verifyclient.exportcert", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 60 */
129 { "server.set-v6only", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 61 */
130 { "ssl.use-sslv3", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 62 */
131 { "ssl.dh-file", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 63 */
132 { "ssl.ec-curve", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 64 */
133 { "ssl.disable-client-renegotiation", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 65 */
134 { "ssl.honor-cipher-order", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 66 */
135 { "ssl.empty-fragments", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 67 */
136 { "server.upload-temp-file-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_SERVER }, /* 68 */
137 { "mimetype.xattr-name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_SERVER }, /* 69 */
138 { "server.listen-backlog", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 70 */
139 { "server.error-handler-404", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 71 */
140 { "server.http-parseopt-header-strict",NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 72 */
141 { "server.http-parseopt-host-strict", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 73 */
142 { "server.http-parseopt-host-normalize",NULL,T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER }, /* 74 */
143 { "server.bsd-accept-filter", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 75 */
144 { "server.stream-request-body", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 76 */
145 { "server.stream-response-body", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 77 */
146 { "server.max-request-field-size", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_SERVER }, /* 78 */
148 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
151 /* all T_CONFIG_SCOPE_SERVER options */
152 cv[0].destination = srv->srvconf.bindhost;
153 cv[1].destination = srv->srvconf.errorlog_file;
154 cv[3].destination = srv->srvconf.changeroot;
155 cv[4].destination = srv->srvconf.username;
156 cv[5].destination = srv->srvconf.groupname;
157 cv[6].destination = &(srv->srvconf.port);
158 cv[9].destination = srv->srvconf.modules;
160 cv[10].destination = srv->srvconf.event_handler;
161 cv[11].destination = srv->srvconf.pid_file;
162 cv[13].destination = &(srv->srvconf.max_worker);
164 cv[23].destination = &(srv->srvconf.max_fds);
166 cv[37].destination = &(srv->srvconf.log_request_header_on_error);
167 cv[38].destination = &(srv->srvconf.log_state_handling);
169 cv[40].destination = &(srv->srvconf.errorlog_use_syslog);
170 stat_cache_string = buffer_init();
171 cv[42].destination = stat_cache_string;
172 cv[43].destination = &(srv->srvconf.max_conns);
173 cv[44].destination = srv->srvconf.network_backend;
174 cv[45].destination = srv->srvconf.upload_tempdirs;
175 cv[46].destination = &(srv->srvconf.enable_cores);
177 cv[52].destination = &(srv->srvconf.reject_expect_100_with_417);
178 cv[55].destination = srv->srvconf.breakagelog_file;
180 cv[68].destination = &(srv->srvconf.upload_temp_file_size);
181 cv[69].destination = srv->srvconf.xattr_name;
182 cv[72].destination = &(srv->srvconf.http_header_strict);
183 cv[73].destination = &(srv->srvconf.http_host_strict);
184 cv[74].destination = &(srv->srvconf.http_host_normalize);
185 cv[78].destination = &(srv->srvconf.max_request_field_size);
187 srv->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
189 force_assert(srv->config_storage);
190 force_assert(srv->config_context->used); /* static analysis hint for ccc
191 -analyzer */
193 for (i = 0; i < srv->config_context->used; i++) {
194 data_config const* config = (data_config const*)srv->config_context->data[i];
195 specific_config *s;
197 s = calloc(1, sizeof(specific_config));
198 force_assert(s);
199 s->document_root = buffer_init();
200 s->mimetypes = array_init();
201 s->server_name = buffer_init();
202 s->ssl_pemfile = buffer_init();
203 s->ssl_ca_file = buffer_init();
204 s->error_handler = buffer_init();
205 s->error_handler_404 = buffer_init();
206 s->server_tag = buffer_init_string(PACKAGE_DESC);
207 s->ssl_cipher_list = buffer_init();
208 s->ssl_dh_file = buffer_init();
209 s->ssl_ec_curve = buffer_init();
210 s->errorfile_prefix = buffer_init();
211 #if defined(__FreeBSD__) || defined(__NetBSD__) \
212 || defined(__OpenBSD__) || defined(__DragonFly__)
213 s->bsd_accept_filter = (i == 0)
214 ? buffer_init()
215 : buffer_init_buffer(srv->config_storage[0]->bsd_accept_filter);
216 #endif
217 s->max_keep_alive_requests = 16;
218 s->max_keep_alive_idle = 5;
219 s->max_read_idle = 60;
220 s->max_write_idle = 360;
221 s->max_request_size = 0;
222 s->use_xattr = 0;
223 s->ssl_enabled = 0;
224 s->ssl_honor_cipher_order = 1;
225 s->ssl_empty_fragments = 0;
226 s->ssl_use_sslv2 = 0;
227 s->ssl_use_sslv3 = 0;
228 s->use_ipv6 = (i == 0) ? 0 : srv->config_storage[0]->use_ipv6;
229 s->set_v6only = (i == 0) ? 1 : srv->config_storage[0]->set_v6only;
230 s->defer_accept = (i == 0) ? 0 : srv->config_storage[0]->defer_accept;
231 #ifdef HAVE_LSTAT
232 s->follow_symlink = 1;
233 #endif
234 s->kbytes_per_second = 0;
235 s->allow_http11 = 1;
236 s->etag_use_inode = 1;
237 s->etag_use_mtime = 1;
238 s->etag_use_size = 1;
239 s->range_requests = 1;
240 s->force_lowercase_filenames = (i == 0) ? 2 : 0; /* we wan't to detect later if user changed this for global section */
241 s->global_kbytes_per_second = 0;
242 s->global_bytes_per_second_cnt = 0;
243 s->global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
244 s->ssl_verifyclient = 0;
245 s->ssl_verifyclient_enforce = 1;
246 s->ssl_verifyclient_username = buffer_init();
247 s->ssl_verifyclient_depth = 9;
248 s->ssl_verifyclient_export_cert = 0;
249 s->ssl_disable_client_renegotiation = 1;
250 s->listen_backlog = (0 == i ? 1024 : srv->config_storage[0]->listen_backlog);
251 s->stream_request_body = 0;
252 s->stream_response_body = 0;
254 /* all T_CONFIG_SCOPE_CONNECTION options */
255 cv[2].destination = s->errorfile_prefix;
256 cv[7].destination = s->server_tag;
257 cv[8].destination = &(s->use_ipv6);
259 cv[12].destination = &(s->max_request_size);
260 cv[14].destination = s->document_root;
261 cv[15].destination = &(s->force_lowercase_filenames);
262 cv[16].destination = &(s->log_condition_handling);
263 cv[17].destination = &(s->max_keep_alive_requests);
264 cv[18].destination = s->server_name;
265 cv[19].destination = &(s->max_keep_alive_idle);
267 cv[20].destination = &(s->max_read_idle);
268 cv[21].destination = &(s->max_write_idle);
269 cv[22].destination = s->error_handler;
270 #ifdef HAVE_LSTAT
271 cv[24].destination = &(s->follow_symlink);
272 #endif
273 cv[25].destination = &(s->global_kbytes_per_second);
274 cv[26].destination = &(s->kbytes_per_second);
275 cv[27].destination = &(s->use_xattr);
276 cv[28].destination = s->mimetypes;
277 cv[29].destination = s->ssl_pemfile;
279 cv[30].destination = &(s->ssl_enabled);
280 cv[31].destination = &(s->log_file_not_found);
281 cv[32].destination = &(s->log_request_handling);
282 cv[33].destination = &(s->log_response_header);
283 cv[34].destination = &(s->log_request_header);
284 cv[35].destination = &(s->log_ssl_noise);
285 cv[36].destination = &(s->allow_http11);
286 cv[39].destination = s->ssl_ca_file;
288 cv[41].destination = &(s->range_requests);
289 cv[47].destination = s->ssl_cipher_list;
290 cv[48].destination = &(s->ssl_use_sslv2);
291 cv[49].destination = &(s->etag_use_inode);
293 cv[50].destination = &(s->etag_use_mtime);
294 cv[51].destination = &(s->etag_use_size);
295 cv[53].destination = &(s->log_timeouts);
296 cv[54].destination = &(s->defer_accept);
297 cv[56].destination = &(s->ssl_verifyclient);
298 cv[57].destination = &(s->ssl_verifyclient_enforce);
299 cv[58].destination = &(s->ssl_verifyclient_depth);
300 cv[59].destination = s->ssl_verifyclient_username;
302 cv[60].destination = &(s->ssl_verifyclient_export_cert);
303 cv[61].destination = &(s->set_v6only);
304 cv[62].destination = &(s->ssl_use_sslv3);
305 cv[63].destination = s->ssl_dh_file;
306 cv[64].destination = s->ssl_ec_curve;
307 cv[65].destination = &(s->ssl_disable_client_renegotiation);
308 cv[66].destination = &(s->ssl_honor_cipher_order);
309 cv[67].destination = &(s->ssl_empty_fragments);
310 cv[70].destination = &(s->listen_backlog);
311 cv[71].destination = s->error_handler_404;
312 #if defined(__FreeBSD__) || defined(__NetBSD__) \
313 || defined(__OpenBSD__) || defined(__DragonFly__)
314 cv[75].destination = s->bsd_accept_filter;
315 #endif
316 cv[76].destination = &(s->stream_request_body);
317 cv[77].destination = &(s->stream_response_body);
319 srv->config_storage[i] = s;
321 if (0 != (ret = config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION))) {
322 break;
325 if (s->stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN) {
326 s->stream_request_body |= FDEVENT_STREAM_REQUEST;
328 if (s->stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) {
329 s->stream_response_body |= FDEVENT_STREAM_RESPONSE;
334 specific_config *s = srv->config_storage[0];
335 s->http_parseopts= /*(global, but stored in con->conf.http_parseopts)*/
336 (srv->srvconf.http_header_strict ?(HTTP_PARSEOPT_HEADER_STRICT) :0)
337 |(srv->srvconf.http_host_strict ?(HTTP_PARSEOPT_HOST_STRICT
338 |HTTP_PARSEOPT_HOST_NORMALIZE):0)
339 |(srv->srvconf.http_host_normalize ?(HTTP_PARSEOPT_HOST_NORMALIZE):0);
342 if (buffer_string_is_empty(stat_cache_string)) {
343 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
344 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("simple"))) {
345 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_SIMPLE;
346 #ifdef HAVE_FAM_H
347 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("fam"))) {
348 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_FAM;
349 #endif
350 } else if (buffer_is_equal_string(stat_cache_string, CONST_STR_LEN("disable"))) {
351 srv->srvconf.stat_cache_engine = STAT_CACHE_ENGINE_NONE;
352 } else {
353 log_error_write(srv, __FILE__, __LINE__, "sb",
354 "server.stat-cache-engine can be one of \"disable\", \"simple\","
355 #ifdef HAVE_FAM_H
356 " \"fam\","
357 #endif
358 " but not:", stat_cache_string);
359 ret = HANDLER_ERROR;
362 buffer_free(stat_cache_string);
365 data_string *ds;
366 int prepend_mod_indexfile = 1;
367 int append_mod_dirlisting = 1;
368 int append_mod_staticfile = 1;
369 int append_mod_authn_file = 1;
370 int append_mod_authn_ldap = 1;
371 int append_mod_authn_mysql = 1;
372 int contains_mod_auth = 0;
374 /* prepend default modules */
375 for (i = 0; i < srv->srvconf.modules->used; i++) {
376 ds = (data_string *)srv->srvconf.modules->data[i];
378 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_indexfile"))) {
379 prepend_mod_indexfile = 0;
382 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_staticfile"))) {
383 append_mod_staticfile = 0;
386 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_dirlisting"))) {
387 append_mod_dirlisting = 0;
390 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_authn_file"))) {
391 append_mod_authn_file = 0;
394 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_authn_ldap"))) {
395 append_mod_authn_ldap = 0;
398 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_authn_mysql"))) {
399 append_mod_authn_mysql = 0;
402 if (buffer_is_equal_string(ds->value, CONST_STR_LEN("mod_auth"))) {
403 contains_mod_auth = 1;
406 if (0 == prepend_mod_indexfile &&
407 0 == append_mod_dirlisting &&
408 0 == append_mod_staticfile &&
409 0 == append_mod_authn_file &&
410 0 == append_mod_authn_ldap &&
411 0 == append_mod_authn_mysql &&
412 1 == contains_mod_auth) {
413 break;
417 if (prepend_mod_indexfile) {
418 /* mod_indexfile has to be loaded before mod_fastcgi and friends */
419 array *modules = array_init();
421 ds = data_string_init();
422 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_indexfile"));
423 array_insert_unique(modules, (data_unset *)ds);
425 for (i = 0; i < srv->srvconf.modules->used; i++) {
426 data_unset *du = srv->srvconf.modules->data[i];
427 array_insert_unique(modules, du->copy(du));
430 array_free(srv->srvconf.modules);
431 srv->srvconf.modules = modules;
434 /* append default modules */
435 if (append_mod_dirlisting) {
436 ds = data_string_init();
437 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_dirlisting"));
438 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
441 if (append_mod_staticfile) {
442 ds = data_string_init();
443 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_staticfile"));
444 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
447 /* mod_auth.c,http_auth.c auth backends were split into separate modules
448 * Automatically load auth backend modules for compatibility with
449 * existing lighttpd 1.4.x configs */
450 if (contains_mod_auth) {
451 if (append_mod_authn_file) {
452 ds = data_string_init();
453 buffer_copy_string_len(ds->value, CONST_STR_LEN("mod_authn_file"));
454 array_insert_unique(srv->srvconf.modules, (data_unset *)ds);
456 if (append_mod_authn_ldap) {
457 #if defined(HAVE_LDAP_H) && defined(HAVE_LBER_H) && defined(HAVE_LIBLDAP) && defined(HAVE_LIBLBER)
458 config_warn_authn_module(srv, "ldap");
459 #endif
461 if (append_mod_authn_mysql) {
462 #if defined(HAVE_MYSQL)
463 config_warn_authn_module(srv, "mysql");
464 #endif
469 return ret;
474 #define PATCH(x) con->conf.x = s->x
475 int config_setup_connection(server *srv, connection *con) {
476 specific_config *s = srv->config_storage[0];
478 PATCH(http_parseopts);
480 PATCH(allow_http11);
481 PATCH(mimetypes);
482 PATCH(document_root);
483 PATCH(high_precision_timestamps);
484 PATCH(max_keep_alive_requests);
485 PATCH(max_keep_alive_idle);
486 PATCH(max_read_idle);
487 PATCH(max_write_idle);
488 PATCH(max_request_size);
489 PATCH(use_xattr);
490 PATCH(error_handler);
491 PATCH(error_handler_404);
492 PATCH(errorfile_prefix);
493 #ifdef HAVE_LSTAT
494 PATCH(follow_symlink);
495 #endif
496 PATCH(server_tag);
497 PATCH(kbytes_per_second);
498 PATCH(global_kbytes_per_second);
499 PATCH(global_bytes_per_second_cnt);
501 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
502 buffer_copy_buffer(con->server_name, s->server_name);
504 PATCH(log_request_header);
505 PATCH(log_response_header);
506 PATCH(log_request_handling);
507 PATCH(log_condition_handling);
508 PATCH(log_file_not_found);
509 PATCH(log_ssl_noise);
510 PATCH(log_timeouts);
512 PATCH(range_requests);
513 PATCH(force_lowercase_filenames);
514 /*PATCH(listen_backlog);*//*(not necessary; used only at startup)*/
515 PATCH(stream_request_body);
516 PATCH(stream_response_body);
518 PATCH(ssl_enabled);
520 PATCH(ssl_pemfile);
521 #ifdef USE_OPENSSL
522 PATCH(ssl_pemfile_x509);
523 PATCH(ssl_pemfile_pkey);
524 #endif
525 PATCH(ssl_ca_file);
526 #ifdef USE_OPENSSL
527 PATCH(ssl_ca_file_cert_names);
528 #endif
529 PATCH(ssl_cipher_list);
530 PATCH(ssl_dh_file);
531 PATCH(ssl_ec_curve);
532 PATCH(ssl_honor_cipher_order);
533 PATCH(ssl_empty_fragments);
534 PATCH(ssl_use_sslv2);
535 PATCH(ssl_use_sslv3);
536 PATCH(etag_use_inode);
537 PATCH(etag_use_mtime);
538 PATCH(etag_use_size);
540 PATCH(ssl_verifyclient);
541 PATCH(ssl_verifyclient_enforce);
542 PATCH(ssl_verifyclient_depth);
543 PATCH(ssl_verifyclient_username);
544 PATCH(ssl_verifyclient_export_cert);
545 PATCH(ssl_disable_client_renegotiation);
547 return 0;
550 int config_patch_connection(server *srv, connection *con) {
551 size_t i, j;
553 /* skip the first, the global context */
554 for (i = 1; i < srv->config_context->used; i++) {
555 data_config *dc = (data_config *)srv->config_context->data[i];
556 specific_config *s = srv->config_storage[i];
558 /* condition didn't match */
559 if (!config_check_cond(srv, con, dc)) continue;
561 /* merge config */
562 for (j = 0; j < dc->value->used; j++) {
563 data_unset *du = dc->value->data[j];
565 if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.document-root"))) {
566 PATCH(document_root);
567 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.range-requests"))) {
568 PATCH(range_requests);
569 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler"))) {
570 PATCH(error_handler);
571 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.error-handler-404"))) {
572 PATCH(error_handler_404);
573 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.errorfile-prefix"))) {
574 PATCH(errorfile_prefix);
575 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.assign"))) {
576 PATCH(mimetypes);
577 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-requests"))) {
578 PATCH(max_keep_alive_requests);
579 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-keep-alive-idle"))) {
580 PATCH(max_keep_alive_idle);
581 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-write-idle"))) {
582 PATCH(max_write_idle);
583 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-read-idle"))) {
584 PATCH(max_read_idle);
585 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.max-request-size"))) {
586 PATCH(max_request_size);
587 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("mimetype.use-xattr"))) {
588 PATCH(use_xattr);
589 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-inode"))) {
590 PATCH(etag_use_inode);
591 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-mtime"))) {
592 PATCH(etag_use_mtime);
593 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("etag.use-size"))) {
594 PATCH(etag_use_size);
595 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.pemfile"))) {
596 PATCH(ssl_pemfile);
597 #ifdef USE_OPENSSL
598 PATCH(ssl_pemfile_x509);
599 PATCH(ssl_pemfile_pkey);
600 #endif
601 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ca-file"))) {
602 PATCH(ssl_ca_file);
603 #ifdef USE_OPENSSL
604 PATCH(ssl_ca_file_cert_names);
605 #endif
606 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.honor-cipher-order"))) {
607 PATCH(ssl_honor_cipher_order);
608 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.empty-fragments"))) {
609 PATCH(ssl_empty_fragments);
610 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv2"))) {
611 PATCH(ssl_use_sslv2);
612 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.use-sslv3"))) {
613 PATCH(ssl_use_sslv3);
614 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.cipher-list"))) {
615 PATCH(ssl_cipher_list);
616 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.engine"))) {
617 PATCH(ssl_enabled);
618 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.dh-file"))) {
619 PATCH(ssl_dh_file);
620 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.ec-curve"))) {
621 PATCH(ssl_ec_curve);
622 #ifdef HAVE_LSTAT
623 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.follow-symlink"))) {
624 PATCH(follow_symlink);
625 #endif
626 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.name"))) {
627 buffer_copy_buffer(con->server_name, s->server_name);
628 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.tag"))) {
629 PATCH(server_tag);
630 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-request-body"))) {
631 PATCH(stream_request_body);
632 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.stream-response-body"))) {
633 PATCH(stream_response_body);
634 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("connection.kbytes-per-second"))) {
635 PATCH(kbytes_per_second);
636 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-handling"))) {
637 PATCH(log_request_handling);
638 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-request-header"))) {
639 PATCH(log_request_header);
640 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-response-header"))) {
641 PATCH(log_response_header);
642 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-condition-handling"))) {
643 PATCH(log_condition_handling);
644 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-file-not-found"))) {
645 PATCH(log_file_not_found);
646 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-ssl-noise"))) {
647 PATCH(log_ssl_noise);
648 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("debug.log-timeouts"))) {
649 PATCH(log_timeouts);
650 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.protocol-http11"))) {
651 PATCH(allow_http11);
652 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.force-lowercase-filenames"))) {
653 PATCH(force_lowercase_filenames);
654 #if 0 /*(not necessary; used only at startup)*/
655 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.listen-backlog"))) {
656 PATCH(listen_backlog);
657 #endif
658 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("server.kbytes-per-second"))) {
659 PATCH(global_kbytes_per_second);
660 PATCH(global_bytes_per_second_cnt);
661 con->conf.global_bytes_per_second_cnt_ptr = &s->global_bytes_per_second_cnt;
662 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.activate"))) {
663 PATCH(ssl_verifyclient);
664 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.enforce"))) {
665 PATCH(ssl_verifyclient_enforce);
666 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.depth"))) {
667 PATCH(ssl_verifyclient_depth);
668 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.username"))) {
669 PATCH(ssl_verifyclient_username);
670 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.verifyclient.exportcert"))) {
671 PATCH(ssl_verifyclient_export_cert);
672 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("ssl.disable-client-renegotiation"))) {
673 PATCH(ssl_disable_client_renegotiation);
678 con->etag_flags = (con->conf.etag_use_mtime ? ETAG_USE_MTIME : 0) |
679 (con->conf.etag_use_inode ? ETAG_USE_INODE : 0) |
680 (con->conf.etag_use_size ? ETAG_USE_SIZE : 0);
682 return 0;
684 #undef PATCH
686 typedef struct {
687 int foo;
688 int bar;
690 const buffer *source;
691 const char *input;
692 size_t offset;
693 size_t size;
695 int line_pos;
696 int line;
698 int in_key;
699 int in_brace;
700 int in_cond;
701 } tokenizer_t;
703 #if 0
704 static int tokenizer_open(server *srv, tokenizer_t *t, buffer *basedir, const char *fn) {
705 if (buffer_string_is_empty(basedir) ||
706 (fn[0] == '/' || fn[0] == '\\') ||
707 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\'))) {
708 t->file = buffer_init_string(fn);
709 } else {
710 t->file = buffer_init_buffer(basedir);
711 buffer_append_string(t->file, fn);
714 if (0 != stream_open(&(t->s), t->file)) {
715 log_error_write(srv, __FILE__, __LINE__, "sbss",
716 "opening configfile ", t->file, "failed:", strerror(errno));
717 buffer_free(t->file);
718 return -1;
721 t->input = t->s.start;
722 t->offset = 0;
723 t->size = t->s.size;
724 t->line = 1;
725 t->line_pos = 1;
727 t->in_key = 1;
728 t->in_brace = 0;
729 t->in_cond = 0;
730 return 0;
733 static int tokenizer_close(server *srv, tokenizer_t *t) {
734 UNUSED(srv);
736 buffer_free(t->file);
737 return stream_close(&(t->s));
739 #endif
740 static int config_skip_newline(tokenizer_t *t) {
741 int skipped = 1;
742 force_assert(t->input[t->offset] == '\r' || t->input[t->offset] == '\n');
743 if (t->input[t->offset] == '\r' && t->input[t->offset + 1] == '\n') {
744 skipped ++;
745 t->offset ++;
747 t->offset ++;
748 return skipped;
751 static int config_skip_comment(tokenizer_t *t) {
752 int i;
753 force_assert(t->input[t->offset] == '#');
754 for (i = 1; t->input[t->offset + i] &&
755 (t->input[t->offset + i] != '\n' && t->input[t->offset + i] != '\r');
756 i++);
757 t->offset += i;
758 return i;
761 static int config_tokenizer(server *srv, tokenizer_t *t, int *token_id, buffer *token) {
762 int tid = 0;
763 size_t i;
765 for (tid = 0; tid == 0 && t->offset < t->size && t->input[t->offset] ; ) {
766 char c = t->input[t->offset];
767 const char *start = NULL;
769 switch (c) {
770 case '=':
771 if (t->in_brace) {
772 if (t->input[t->offset + 1] == '>') {
773 t->offset += 2;
775 buffer_copy_string_len(token, CONST_STR_LEN("=>"));
777 tid = TK_ARRAY_ASSIGN;
778 } else {
779 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
780 "source:", t->source,
781 "line:", t->line, "pos:", t->line_pos,
782 "use => for assignments in arrays");
783 return -1;
785 } else if (t->in_cond) {
786 if (t->input[t->offset + 1] == '=') {
787 t->offset += 2;
789 buffer_copy_string_len(token, CONST_STR_LEN("=="));
791 tid = TK_EQ;
792 } else if (t->input[t->offset + 1] == '~') {
793 t->offset += 2;
795 buffer_copy_string_len(token, CONST_STR_LEN("=~"));
797 tid = TK_MATCH;
798 } else {
799 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
800 "source:", t->source,
801 "line:", t->line, "pos:", t->line_pos,
802 "only =~ and == are allowed in the condition");
803 return -1;
805 t->in_key = 1;
806 t->in_cond = 0;
807 } else if (t->in_key) {
808 tid = TK_ASSIGN;
810 buffer_copy_string_len(token, t->input + t->offset, 1);
812 t->offset++;
813 t->line_pos++;
814 } else {
815 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
816 "source:", t->source,
817 "line:", t->line, "pos:", t->line_pos,
818 "unexpected equal-sign: =");
819 return -1;
822 break;
823 case '!':
824 if (t->in_cond) {
825 if (t->input[t->offset + 1] == '=') {
826 t->offset += 2;
828 buffer_copy_string_len(token, CONST_STR_LEN("!="));
830 tid = TK_NE;
831 } else if (t->input[t->offset + 1] == '~') {
832 t->offset += 2;
834 buffer_copy_string_len(token, CONST_STR_LEN("!~"));
836 tid = TK_NOMATCH;
837 } else {
838 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
839 "source:", t->source,
840 "line:", t->line, "pos:", t->line_pos,
841 "only !~ and != are allowed in the condition");
842 return -1;
844 t->in_key = 1;
845 t->in_cond = 0;
846 } else {
847 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
848 "source:", t->source,
849 "line:", t->line, "pos:", t->line_pos,
850 "unexpected exclamation-marks: !");
851 return -1;
854 break;
855 case '\t':
856 case ' ':
857 t->offset++;
858 t->line_pos++;
859 break;
860 case '\n':
861 case '\r':
862 if (t->in_brace == 0) {
863 int done = 0;
864 while (!done && t->offset < t->size) {
865 switch (t->input[t->offset]) {
866 case '\r':
867 case '\n':
868 config_skip_newline(t);
869 t->line_pos = 1;
870 t->line++;
871 break;
873 case '#':
874 t->line_pos += config_skip_comment(t);
875 break;
877 case '\t':
878 case ' ':
879 t->offset++;
880 t->line_pos++;
881 break;
883 default:
884 done = 1;
887 t->in_key = 1;
888 tid = TK_EOL;
889 buffer_copy_string_len(token, CONST_STR_LEN("(EOL)"));
890 } else {
891 config_skip_newline(t);
892 t->line_pos = 1;
893 t->line++;
895 break;
896 case ',':
897 if (t->in_brace > 0) {
898 tid = TK_COMMA;
900 buffer_copy_string_len(token, CONST_STR_LEN("(COMMA)"));
903 t->offset++;
904 t->line_pos++;
905 break;
906 case '"':
907 /* search for the terminating " */
908 start = t->input + t->offset + 1;
909 buffer_copy_string_len(token, CONST_STR_LEN(""));
911 for (i = 1; t->input[t->offset + i]; i++) {
912 if (t->input[t->offset + i] == '\\' &&
913 t->input[t->offset + i + 1] == '"') {
915 buffer_append_string_len(token, start, t->input + t->offset + i - start);
917 start = t->input + t->offset + i + 1;
919 /* skip the " */
920 i++;
921 continue;
925 if (t->input[t->offset + i] == '"') {
926 tid = TK_STRING;
928 buffer_append_string_len(token, start, t->input + t->offset + i - start);
930 break;
934 if (t->input[t->offset + i] == '\0') {
935 /* ERROR */
937 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
938 "source:", t->source,
939 "line:", t->line, "pos:", t->line_pos,
940 "missing closing quote");
942 return -1;
945 t->offset += i + 1;
946 t->line_pos += i + 1;
948 break;
949 case '(':
950 t->offset++;
951 t->in_brace++;
953 tid = TK_LPARAN;
955 buffer_copy_string_len(token, CONST_STR_LEN("("));
956 break;
957 case ')':
958 t->offset++;
959 t->in_brace--;
961 tid = TK_RPARAN;
963 buffer_copy_string_len(token, CONST_STR_LEN(")"));
964 break;
965 case '$':
966 t->offset++;
968 tid = TK_DOLLAR;
969 t->in_cond = 1;
970 t->in_key = 0;
972 buffer_copy_string_len(token, CONST_STR_LEN("$"));
974 break;
976 case '+':
977 if (t->input[t->offset + 1] == '=') {
978 t->offset += 2;
979 buffer_copy_string_len(token, CONST_STR_LEN("+="));
980 tid = TK_APPEND;
981 } else {
982 t->offset++;
983 tid = TK_PLUS;
984 buffer_copy_string_len(token, CONST_STR_LEN("+"));
986 break;
988 case '{':
989 t->offset++;
991 tid = TK_LCURLY;
993 buffer_copy_string_len(token, CONST_STR_LEN("{"));
995 break;
997 case '}':
998 t->offset++;
1000 tid = TK_RCURLY;
1002 buffer_copy_string_len(token, CONST_STR_LEN("}"));
1004 break;
1006 case '[':
1007 t->offset++;
1009 tid = TK_LBRACKET;
1011 buffer_copy_string_len(token, CONST_STR_LEN("["));
1013 break;
1015 case ']':
1016 t->offset++;
1018 tid = TK_RBRACKET;
1020 buffer_copy_string_len(token, CONST_STR_LEN("]"));
1022 break;
1023 case '#':
1024 t->line_pos += config_skip_comment(t);
1026 break;
1027 default:
1028 if (t->in_cond) {
1029 for (i = 0; t->input[t->offset + i] &&
1030 (isalpha((unsigned char)t->input[t->offset + i])
1031 ); i++);
1033 if (i && t->input[t->offset + i]) {
1034 tid = TK_SRVVARNAME;
1035 buffer_copy_string_len(token, t->input + t->offset, i);
1037 t->offset += i;
1038 t->line_pos += i;
1039 } else {
1040 /* ERROR */
1041 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
1042 "source:", t->source,
1043 "line:", t->line, "pos:", t->line_pos,
1044 "invalid character in condition");
1045 return -1;
1047 } else if (isdigit((unsigned char)c)) {
1048 /* take all digits */
1049 for (i = 0; t->input[t->offset + i] && isdigit((unsigned char)t->input[t->offset + i]); i++);
1051 /* was there it least a digit ? */
1052 if (i) {
1053 tid = TK_INTEGER;
1055 buffer_copy_string_len(token, t->input + t->offset, i);
1057 t->offset += i;
1058 t->line_pos += i;
1060 } else {
1061 /* the key might consist of [-.0-9a-z] */
1062 for (i = 0; t->input[t->offset + i] &&
1063 (isalnum((unsigned char)t->input[t->offset + i]) ||
1064 t->input[t->offset + i] == '.' ||
1065 t->input[t->offset + i] == '_' || /* for env.* */
1066 t->input[t->offset + i] == '-'
1067 ); i++);
1069 if (i && t->input[t->offset + i]) {
1070 buffer_copy_string_len(token, t->input + t->offset, i);
1072 if (strcmp(token->ptr, "include") == 0) {
1073 tid = TK_INCLUDE;
1074 } else if (strcmp(token->ptr, "include_shell") == 0) {
1075 tid = TK_INCLUDE_SHELL;
1076 } else if (strcmp(token->ptr, "global") == 0) {
1077 tid = TK_GLOBAL;
1078 } else if (strcmp(token->ptr, "else") == 0) {
1079 tid = TK_ELSE;
1080 } else {
1081 tid = TK_LKEY;
1084 t->offset += i;
1085 t->line_pos += i;
1086 } else {
1087 /* ERROR */
1088 log_error_write(srv, __FILE__, __LINE__, "sbsdsds",
1089 "source:", t->source,
1090 "line:", t->line, "pos:", t->line_pos,
1091 "invalid character in variable name");
1092 return -1;
1095 break;
1099 if (tid) {
1100 *token_id = tid;
1101 #if 0
1102 log_error_write(srv, __FILE__, __LINE__, "sbsdsdbdd",
1103 "source:", t->source,
1104 "line:", t->line, "pos:", t->line_pos,
1105 token, token->used - 1, tid);
1106 #endif
1108 return 1;
1109 } else if (t->offset < t->size) {
1110 fprintf(stderr, "%s.%d: %d, %s\n",
1111 __FILE__, __LINE__,
1112 tid, token->ptr);
1114 return 0;
1117 static int config_parse(server *srv, config_t *context, tokenizer_t *t) {
1118 void *pParser;
1119 int token_id;
1120 buffer *token, *lasttoken;
1121 int ret;
1123 pParser = configparserAlloc( malloc );
1124 force_assert(pParser);
1125 lasttoken = buffer_init();
1126 token = buffer_init();
1127 while((1 == (ret = config_tokenizer(srv, t, &token_id, token))) && context->ok) {
1128 buffer_copy_buffer(lasttoken, token);
1129 configparser(pParser, token_id, token, context);
1131 token = buffer_init();
1133 buffer_free(token);
1135 if (ret != -1 && context->ok) {
1136 /* add an EOL at EOF, better than say sorry */
1137 configparser(pParser, TK_EOL, buffer_init_string("(EOL)"), context);
1138 if (context->ok) {
1139 configparser(pParser, 0, NULL, context);
1142 configparserFree(pParser, free);
1144 if (ret == -1) {
1145 log_error_write(srv, __FILE__, __LINE__, "sb",
1146 "configfile parser failed at:", lasttoken);
1147 } else if (context->ok == 0) {
1148 log_error_write(srv, __FILE__, __LINE__, "sbsdsdsb",
1149 "source:", t->source,
1150 "line:", t->line, "pos:", t->line_pos,
1151 "parser failed somehow near here:", lasttoken);
1152 ret = -1;
1154 buffer_free(lasttoken);
1156 return ret == -1 ? -1 : 0;
1159 static int tokenizer_init(tokenizer_t *t, const buffer *source, const char *input, size_t size) {
1161 t->source = source;
1162 t->input = input;
1163 t->size = size;
1164 t->offset = 0;
1165 t->line = 1;
1166 t->line_pos = 1;
1168 t->in_key = 1;
1169 t->in_brace = 0;
1170 t->in_cond = 0;
1171 return 0;
1174 static int config_parse_file_stream(server *srv, config_t *context, const buffer *filename) {
1175 tokenizer_t t;
1176 stream s;
1177 int ret;
1179 if (0 != stream_open(&s, filename)) {
1180 log_error_write(srv, __FILE__, __LINE__, "sbss",
1181 "opening configfile ", filename, "failed:", strerror(errno));
1182 return -1;
1183 } else {
1184 tokenizer_init(&t, filename, s.start, s.size);
1185 ret = config_parse(srv, context, &t);
1188 stream_close(&s);
1189 return ret;
1192 int config_parse_file(server *srv, config_t *context, const char *fn) {
1193 buffer *filename;
1194 size_t i;
1195 int ret = -1;
1196 #ifdef GLOB_BRACE
1197 int flags = GLOB_BRACE;
1198 #else
1199 int flags = 0;
1200 #endif
1201 glob_t gl;
1203 if ((fn[0] == '/' || fn[0] == '\\') ||
1204 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\')) ||
1205 (fn[0] == '.' && fn[1] == '.' && (fn[2] == '/' || fn[2] == '\\'))) {
1206 filename = buffer_init_string(fn);
1207 } else {
1208 filename = buffer_init_buffer(context->basedir);
1209 buffer_append_string(filename, fn);
1212 switch (glob(filename->ptr, flags, NULL, &gl)) {
1213 case 0:
1214 for (i = 0; i < gl.gl_pathc; ++i) {
1215 buffer_copy_string(filename, gl.gl_pathv[i]);
1216 ret = config_parse_file_stream(srv, context, filename);
1217 if (0 != ret) break;
1219 globfree(&gl);
1220 break;
1221 case GLOB_NOMATCH:
1222 if (filename->ptr[strcspn(filename->ptr, "*?[]{}")] != '\0') { /*(contains glob metachars)*/
1223 ret = 0; /* not an error if no files match glob pattern */
1225 else {
1226 log_error_write(srv, __FILE__, __LINE__, "sb", "include file not found: ", filename);
1228 break;
1229 case GLOB_ABORTED:
1230 case GLOB_NOSPACE:
1231 log_error_write(srv, __FILE__, __LINE__, "sbss", "glob()", filename, "failed:", strerror(errno));
1232 break;
1235 buffer_free(filename);
1236 return ret;
1239 static char* getCWD(void) {
1240 char *s, *s1;
1241 size_t len;
1242 #ifdef PATH_MAX
1243 len = PATH_MAX;
1244 #else
1245 len = 4096;
1246 #endif
1248 s = malloc(len);
1249 if (!s) return NULL;
1250 while (NULL == getcwd(s, len)) {
1251 if (errno != ERANGE || SSIZE_MAX - len < len) {
1252 free(s);
1253 return NULL;
1255 len *= 2;
1256 s1 = realloc(s, len);
1257 if (!s1) {
1258 free(s);
1259 return NULL;
1261 s = s1;
1263 return s;
1266 int config_parse_cmd(server *srv, config_t *context, const char *cmd) {
1267 tokenizer_t t;
1268 int ret;
1269 buffer *source;
1270 buffer *out;
1271 char *oldpwd;
1273 if (NULL == (oldpwd = getCWD())) {
1274 log_error_write(srv, __FILE__, __LINE__, "s",
1275 "cannot get cwd", strerror(errno));
1276 return -1;
1279 if (!buffer_string_is_empty(context->basedir)) {
1280 if (0 != chdir(context->basedir->ptr)) {
1281 log_error_write(srv, __FILE__, __LINE__, "sbs",
1282 "cannot change directory to", context->basedir, strerror(errno));
1283 free(oldpwd);
1284 return -1;
1288 source = buffer_init_string(cmd);
1289 out = buffer_init();
1291 if (0 != proc_open_buffer(cmd, NULL, out, NULL)) {
1292 log_error_write(srv, __FILE__, __LINE__, "sbss",
1293 "opening", source, "failed:", strerror(errno));
1294 ret = -1;
1295 } else {
1296 tokenizer_init(&t, source, CONST_BUF_LEN(out));
1297 ret = config_parse(srv, context, &t);
1300 buffer_free(source);
1301 buffer_free(out);
1302 if (0 != chdir(oldpwd)) {
1303 log_error_write(srv, __FILE__, __LINE__, "sss",
1304 "cannot change directory to", oldpwd, strerror(errno));
1305 free(oldpwd);
1306 return -1;
1308 free(oldpwd);
1309 return ret;
1312 static void context_init(server *srv, config_t *context) {
1313 context->srv = srv;
1314 context->ok = 1;
1315 vector_config_weak_init(&context->configs_stack);
1316 context->basedir = buffer_init();
1319 static void context_free(config_t *context) {
1320 vector_config_weak_clear(&context->configs_stack);
1321 buffer_free(context->basedir);
1324 int config_read(server *srv, const char *fn) {
1325 config_t context;
1326 data_config *dc;
1327 data_integer *dpid;
1328 data_string *dcwd;
1329 int ret;
1330 char *pos;
1331 buffer *filename;
1333 context_init(srv, &context);
1334 context.all_configs = srv->config_context;
1336 #ifdef __WIN32
1337 pos = strrchr(fn, '\\');
1338 #else
1339 pos = strrchr(fn, '/');
1340 #endif
1341 if (pos) {
1342 buffer_copy_string_len(context.basedir, fn, pos - fn + 1);
1345 dc = data_config_init();
1346 buffer_copy_string_len(dc->key, CONST_STR_LEN("global"));
1348 force_assert(context.all_configs->used == 0);
1349 dc->context_ndx = context.all_configs->used;
1350 array_insert_unique(context.all_configs, (data_unset *)dc);
1351 context.current = dc;
1353 /* default context */
1354 dpid = data_integer_init();
1355 dpid->value = getpid();
1356 buffer_copy_string_len(dpid->key, CONST_STR_LEN("var.PID"));
1357 array_insert_unique(dc->value, (data_unset *)dpid);
1359 dcwd = data_string_init();
1360 buffer_string_prepare_copy(dcwd->value, 1023);
1361 if (NULL != getcwd(dcwd->value->ptr, dcwd->value->size - 1)) {
1362 buffer_commit(dcwd->value, strlen(dcwd->value->ptr));
1363 buffer_copy_string_len(dcwd->key, CONST_STR_LEN("var.CWD"));
1364 array_insert_unique(dc->value, (data_unset *)dcwd);
1365 } else {
1366 dcwd->free((data_unset*) dcwd);
1369 filename = buffer_init_string(fn);
1370 ret = config_parse_file_stream(srv, &context, filename);
1371 buffer_free(filename);
1373 /* remains nothing if parser is ok */
1374 force_assert(!(0 == ret && context.ok && 0 != context.configs_stack.used));
1375 context_free(&context);
1377 if (0 != ret) {
1378 return ret;
1381 if (0 != config_insert(srv)) {
1382 return -1;
1385 return 0;
1388 int config_set_defaults(server *srv) {
1389 size_t i;
1390 specific_config *s = srv->config_storage[0];
1391 struct stat st1, st2;
1393 struct ev_map { fdevent_handler_t et; const char *name; } event_handlers[] =
1395 /* - epoll is most reliable
1396 * - select works everywhere
1398 #ifdef USE_LINUX_EPOLL
1399 { FDEVENT_HANDLER_LINUX_SYSEPOLL, "linux-sysepoll" },
1400 #endif
1401 #ifdef USE_POLL
1402 { FDEVENT_HANDLER_POLL, "poll" },
1403 #endif
1404 #ifdef USE_SELECT
1405 { FDEVENT_HANDLER_SELECT, "select" },
1406 #endif
1407 #ifdef USE_LIBEV
1408 { FDEVENT_HANDLER_LIBEV, "libev" },
1409 #endif
1410 #ifdef USE_SOLARIS_DEVPOLL
1411 { FDEVENT_HANDLER_SOLARIS_DEVPOLL,"solaris-devpoll" },
1412 #endif
1413 #ifdef USE_SOLARIS_PORT
1414 { FDEVENT_HANDLER_SOLARIS_PORT, "solaris-eventports" },
1415 #endif
1416 #ifdef USE_FREEBSD_KQUEUE
1417 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "freebsd-kqueue" },
1418 { FDEVENT_HANDLER_FREEBSD_KQUEUE, "kqueue" },
1419 #endif
1420 { FDEVENT_HANDLER_UNSET, NULL }
1423 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1424 if (-1 == stat(srv->srvconf.changeroot->ptr, &st1)) {
1425 log_error_write(srv, __FILE__, __LINE__, "sb",
1426 "server.chroot doesn't exist:", srv->srvconf.changeroot);
1427 return -1;
1429 if (!S_ISDIR(st1.st_mode)) {
1430 log_error_write(srv, __FILE__, __LINE__, "sb",
1431 "server.chroot isn't a directory:", srv->srvconf.changeroot);
1432 return -1;
1436 if (!srv->srvconf.upload_tempdirs->used) {
1437 data_string *ds = data_string_init();
1438 const char *tmpdir = getenv("TMPDIR");
1439 if (NULL == tmpdir) tmpdir = "/var/tmp";
1440 buffer_copy_string(ds->value, tmpdir);
1441 array_insert_unique(srv->srvconf.upload_tempdirs, (data_unset *)ds);
1444 if (srv->srvconf.upload_tempdirs->used) {
1445 buffer * const b = srv->tmp_buf;
1446 size_t len;
1447 if (!buffer_string_is_empty(srv->srvconf.changeroot)) {
1448 buffer_copy_buffer(b, srv->srvconf.changeroot);
1449 buffer_append_slash(b);
1450 } else {
1451 buffer_reset(b);
1453 len = buffer_string_length(b);
1455 for (i = 0; i < srv->srvconf.upload_tempdirs->used; ++i) {
1456 const data_string * const ds = (data_string *)srv->srvconf.upload_tempdirs->data[i];
1457 buffer_string_set_length(b, len); /*(truncate)*/
1458 buffer_append_string_buffer(b, ds->value);
1459 if (-1 == stat(b->ptr, &st1)) {
1460 log_error_write(srv, __FILE__, __LINE__, "sb",
1461 "server.upload-dirs doesn't exist:", b);
1462 } else if (!S_ISDIR(st1.st_mode)) {
1463 log_error_write(srv, __FILE__, __LINE__, "sb",
1464 "server.upload-dirs isn't a directory:", b);
1469 chunkqueue_set_tempdirs_default(
1470 srv->srvconf.upload_tempdirs,
1471 srv->srvconf.upload_temp_file_size);
1473 if (buffer_string_is_empty(s->document_root)) {
1474 log_error_write(srv, __FILE__, __LINE__, "s",
1475 "a default document-root has to be set");
1477 return -1;
1480 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1482 buffer_to_lower(srv->tmp_buf);
1484 if (2 == s->force_lowercase_filenames) { /* user didn't configure it in global section? */
1485 s->force_lowercase_filenames = 0; /* default to 0 */
1487 if (0 == stat(srv->tmp_buf->ptr, &st1)) {
1488 int is_lower = 0;
1490 is_lower = buffer_is_equal(srv->tmp_buf, s->document_root);
1492 /* lower-case existed, check upper-case */
1493 buffer_copy_buffer(srv->tmp_buf, s->document_root);
1495 buffer_to_upper(srv->tmp_buf);
1497 /* we have to handle the special case that upper and lower-casing results in the same filename
1498 * as in server.document-root = "/" or "/12345/" */
1500 if (is_lower && buffer_is_equal(srv->tmp_buf, s->document_root)) {
1501 /* lower-casing and upper-casing didn't result in
1502 * an other filename, no need to stat(),
1503 * just assume it is case-sensitive. */
1505 s->force_lowercase_filenames = 0;
1506 } else if (0 == stat(srv->tmp_buf->ptr, &st2)) {
1508 /* upper case exists too, doesn't the FS handle this ? */
1510 /* upper and lower have the same inode -> case-insensitve FS */
1512 if (st1.st_ino == st2.st_ino) {
1513 /* upper and lower have the same inode -> case-insensitve FS */
1515 s->force_lowercase_filenames = 1;
1521 if (srv->srvconf.port == 0) {
1522 srv->srvconf.port = s->ssl_enabled ? 443 : 80;
1525 if (buffer_string_is_empty(srv->srvconf.event_handler)) {
1526 /* choose a good default
1528 * the event_handler list is sorted by 'goodness'
1529 * taking the first available should be the best solution
1531 srv->event_handler = event_handlers[0].et;
1533 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1534 log_error_write(srv, __FILE__, __LINE__, "s",
1535 "sorry, there is no event handler for this system");
1537 return -1;
1539 } else {
1541 * User override
1544 for (i = 0; event_handlers[i].name; i++) {
1545 if (0 == strcmp(event_handlers[i].name, srv->srvconf.event_handler->ptr)) {
1546 srv->event_handler = event_handlers[i].et;
1547 break;
1551 if (FDEVENT_HANDLER_UNSET == srv->event_handler) {
1552 log_error_write(srv, __FILE__, __LINE__, "sb",
1553 "the selected event-handler in unknown or not supported:",
1554 srv->srvconf.event_handler );
1556 return -1;
1560 if (s->ssl_enabled) {
1561 if (buffer_string_is_empty(s->ssl_pemfile)) {
1562 /* PEM file is require */
1564 log_error_write(srv, __FILE__, __LINE__, "s",
1565 "ssl.pemfile has to be set");
1566 return -1;
1569 #ifndef USE_OPENSSL
1570 log_error_write(srv, __FILE__, __LINE__, "s",
1571 "ssl support is missing, recompile with --with-openssl");
1573 return -1;
1574 #endif
1577 return 0;