8 #include "configparser.h"
9 #include "configfile.h"
10 #include "proc_open.h"
28 static int config_insert(server
*srv
) {
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 */
61 { "server.follow-symlink", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 24 */
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
},
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
172 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
173 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
176 s
= calloc(1, sizeof(specific_config
));
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)
194 : buffer_init_buffer(srv
->config_storage
[0]->bsd_accept_filter
);
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;
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
;
211 s
->follow_symlink
= 1;
213 s
->kbytes_per_second
= 0;
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
;
250 cv
[24].destination
= &(s
->follow_symlink
);
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
;
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
))) {
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
;
326 } else if (buffer_is_equal_string(stat_cache_string
, CONST_STR_LEN("fam"))) {
327 srv
->srvconf
.stat_cache_engine
= STAT_CACHE_ENGINE_FAM
;
329 } else if (buffer_is_equal_string(stat_cache_string
, CONST_STR_LEN("disable"))) {
330 srv
->srvconf
.stat_cache_engine
= STAT_CACHE_ENGINE_NONE
;
332 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
333 "server.stat-cache-engine can be one of \"disable\", \"simple\","
337 " but not:", stat_cache_string
);
341 buffer_free(stat_cache_string
);
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
) {
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
);
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
);
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
);
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
);
473 PATCH(error_handler
);
474 PATCH(error_handler_404
);
475 PATCH(errorfile_prefix
);
477 PATCH(follow_symlink
);
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
);
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
);
505 PATCH(ssl_pemfile_x509
);
506 PATCH(ssl_pemfile_pkey
);
510 PATCH(ssl_ca_file_cert_names
);
512 PATCH(ssl_cipher_list
);
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
);
533 int config_patch_connection(server
*srv
, connection
*con
) {
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;
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"))) {
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"))) {
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"))) {
581 PATCH(ssl_pemfile_x509
);
582 PATCH(ssl_pemfile_pkey
);
584 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.ca-file"))) {
587 PATCH(ssl_ca_file_cert_names
);
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"))) {
601 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.dh-file"))) {
603 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.ec-curve"))) {
606 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.follow-symlink"))) {
607 PATCH(follow_symlink
);
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"))) {
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"))) {
633 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.protocol-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
);
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);
673 const buffer
*source
;
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
);
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
);
704 t
->input
= t
->s
.start
;
716 static int tokenizer_close(server
*srv
, tokenizer_t
*t
) {
719 buffer_free(t
->file
);
720 return stream_close(&(t
->s
));
723 static int config_skip_newline(tokenizer_t
*t
) {
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') {
734 static int config_skip_comment(tokenizer_t
*t
) {
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');
744 static int config_tokenizer(server
*srv
, tokenizer_t
*t
, int *token_id
, buffer
*token
) {
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
;
755 if (t
->input
[t
->offset
+ 1] == '>') {
758 buffer_copy_string_len(token
, CONST_STR_LEN("=>"));
760 tid
= TK_ARRAY_ASSIGN
;
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");
768 } else if (t
->in_cond
) {
769 if (t
->input
[t
->offset
+ 1] == '=') {
772 buffer_copy_string_len(token
, CONST_STR_LEN("=="));
775 } else if (t
->input
[t
->offset
+ 1] == '~') {
778 buffer_copy_string_len(token
, CONST_STR_LEN("=~"));
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");
790 } else if (t
->in_key
) {
793 buffer_copy_string_len(token
, t
->input
+ t
->offset
, 1);
798 log_error_write(srv
, __FILE__
, __LINE__
, "sbsdsds",
799 "source:", t
->source
,
800 "line:", t
->line
, "pos:", t
->line_pos
,
801 "unexpected equal-sign: =");
808 if (t
->input
[t
->offset
+ 1] == '=') {
811 buffer_copy_string_len(token
, CONST_STR_LEN("!="));
814 } else if (t
->input
[t
->offset
+ 1] == '~') {
817 buffer_copy_string_len(token
, CONST_STR_LEN("!~"));
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");
830 log_error_write(srv
, __FILE__
, __LINE__
, "sbsdsds",
831 "source:", t
->source
,
832 "line:", t
->line
, "pos:", t
->line_pos
,
833 "unexpected exclamation-marks: !");
845 if (t
->in_brace
== 0) {
847 while (!done
&& t
->offset
< t
->size
) {
848 switch (t
->input
[t
->offset
]) {
851 config_skip_newline(t
);
857 t
->line_pos
+= config_skip_comment(t
);
872 buffer_copy_string_len(token
, CONST_STR_LEN("(EOL)"));
874 config_skip_newline(t
);
880 if (t
->in_brace
> 0) {
883 buffer_copy_string_len(token
, CONST_STR_LEN("(COMMA)"));
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;
908 if (t
->input
[t
->offset
+ i
] == '"') {
911 buffer_append_string_len(token
, start
, t
->input
+ t
->offset
+ i
- start
);
917 if (t
->input
[t
->offset
+ i
] == '\0') {
920 log_error_write(srv
, __FILE__
, __LINE__
, "sbsdsds",
921 "source:", t
->source
,
922 "line:", t
->line
, "pos:", t
->line_pos
,
923 "missing closing quote");
929 t
->line_pos
+= i
+ 1;
938 buffer_copy_string_len(token
, CONST_STR_LEN("("));
946 buffer_copy_string_len(token
, CONST_STR_LEN(")"));
955 buffer_copy_string_len(token
, CONST_STR_LEN("$"));
960 if (t
->input
[t
->offset
+ 1] == '=') {
962 buffer_copy_string_len(token
, CONST_STR_LEN("+="));
967 buffer_copy_string_len(token
, CONST_STR_LEN("+"));
976 buffer_copy_string_len(token
, CONST_STR_LEN("{"));
985 buffer_copy_string_len(token
, CONST_STR_LEN("}"));
994 buffer_copy_string_len(token
, CONST_STR_LEN("["));
1003 buffer_copy_string_len(token
, CONST_STR_LEN("]"));
1007 t
->line_pos
+= config_skip_comment(t
);
1012 for (i
= 0; t
->input
[t
->offset
+ i
] &&
1013 (isalpha((unsigned char)t
->input
[t
->offset
+ i
])
1016 if (i
&& t
->input
[t
->offset
+ i
]) {
1017 tid
= TK_SRVVARNAME
;
1018 buffer_copy_string_len(token
, t
->input
+ t
->offset
, i
);
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");
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 ? */
1038 buffer_copy_string_len(token
, t
->input
+ t
->offset
, i
);
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
] == '-'
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) {
1057 } else if (strcmp(token
->ptr
, "include_shell") == 0) {
1058 tid
= TK_INCLUDE_SHELL
;
1059 } else if (strcmp(token
->ptr
, "global") == 0) {
1061 } else if (strcmp(token
->ptr
, "else") == 0) {
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");
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
);
1092 } else if (t
->offset
< t
->size
) {
1093 fprintf(stderr
, "%s.%d: %d, %s\n",
1100 static int config_parse(server
*srv
, config_t
*context
, tokenizer_t
*t
) {
1103 buffer
*token
, *lasttoken
;
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();
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
);
1122 configparser(pParser
, 0, NULL
, context
);
1125 configparserFree(pParser
, free
);
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
);
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
) {
1157 static int config_parse_file_stream(server
*srv
, config_t
*context
, const buffer
*filename
) {
1162 if (0 != stream_open(&s
, filename
)) {
1163 log_error_write(srv
, __FILE__
, __LINE__
, "sbss",
1164 "opening configfile ", filename
, "failed:", strerror(errno
));
1167 tokenizer_init(&t
, filename
, s
.start
, s
.size
);
1168 ret
= config_parse(srv
, context
, &t
);
1175 int config_parse_file(server
*srv
, config_t
*context
, const char *fn
) {
1180 int flags
= GLOB_BRACE
;
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
);
1191 filename
= buffer_init_buffer(context
->basedir
);
1192 buffer_append_string(filename
, fn
);
1195 switch (glob(filename
->ptr
, flags
, NULL
, &gl
)) {
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;
1205 if (filename
->ptr
[strcspn(filename
->ptr
, "*?[]{}")] != '\0') { /*(contains glob metachars)*/
1206 ret
= 0; /* not an error if no files match glob pattern */
1209 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "include file not found: ", filename
);
1214 log_error_write(srv
, __FILE__
, __LINE__
, "sbss", "glob()", filename
, "failed:", strerror(errno
));
1218 buffer_free(filename
);
1222 static char* getCWD(void) {
1232 if (!s
) return NULL
;
1233 while (NULL
== getcwd(s
, len
)) {
1234 if (errno
!= ERANGE
|| SSIZE_MAX
- len
< len
) {
1239 s1
= realloc(s
, len
);
1249 int config_parse_cmd(server
*srv
, config_t
*context
, const char *cmd
) {
1256 if (NULL
== (oldpwd
= getCWD())) {
1257 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1258 "cannot get cwd", strerror(errno
));
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
));
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
));
1279 tokenizer_init(&t
, source
, CONST_BUF_LEN(out
));
1280 ret
= config_parse(srv
, context
, &t
);
1283 buffer_free(source
);
1285 if (0 != chdir(oldpwd
)) {
1286 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
1287 "cannot change directory to", oldpwd
, strerror(errno
));
1295 static void context_init(server
*srv
, config_t
*context
) {
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
) {
1316 context_init(srv
, &context
);
1317 context
.all_configs
= srv
->config_context
;
1320 pos
= strrchr(fn
, '\\');
1322 pos
= strrchr(fn
, '/');
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
);
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
);
1364 if (0 != config_insert(srv
)) {
1371 int config_set_defaults(server
*srv
) {
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" },
1385 { FDEVENT_HANDLER_POLL
, "poll" },
1388 { FDEVENT_HANDLER_SELECT
, "select" },
1391 { FDEVENT_HANDLER_LIBEV
, "libev" },
1393 #ifdef USE_SOLARIS_DEVPOLL
1394 { FDEVENT_HANDLER_SOLARIS_DEVPOLL
,"solaris-devpoll" },
1396 #ifdef USE_SOLARIS_PORT
1397 { FDEVENT_HANDLER_SOLARIS_PORT
, "solaris-eventports" },
1399 #ifdef USE_FREEBSD_KQUEUE
1400 { FDEVENT_HANDLER_FREEBSD_KQUEUE
, "freebsd-kqueue" },
1401 { FDEVENT_HANDLER_FREEBSD_KQUEUE
, "kqueue" },
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
);
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
);
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
;
1430 if (!buffer_string_is_empty(srv
->srvconf
.changeroot
)) {
1431 buffer_copy_buffer(b
, srv
->srvconf
.changeroot
);
1432 buffer_append_slash(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");
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
)) {
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");
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
;
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
);
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");
1553 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1554 "ssl support is missing, recompile with --with-openssl");