8 #include "configparser.h"
9 #include "configfile.h"
10 #include "proc_open.h"
27 static int config_insert(server
*srv
) {
30 buffer
*stat_cache_string
;
32 config_values_t cv
[] = {
33 { "server.bind", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_SERVER
}, /* 0 */
34 { "server.errorlog", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_SERVER
}, /* 1 */
35 { "server.errorfile-prefix", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
36 { "server.chroot", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_SERVER
}, /* 3 */
37 { "server.username", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_SERVER
}, /* 4 */
38 { "server.groupname", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_SERVER
}, /* 5 */
39 { "server.port", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_SERVER
}, /* 6 */
40 { "server.tag", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 7 */
41 { "server.use-ipv6", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 8 */
42 { "server.modules", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_SERVER
}, /* 9 */
44 { "server.event-handler", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_SERVER
}, /* 10 */
45 { "server.pid-file", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_SERVER
}, /* 11 */
46 { "server.max-request-size", NULL
, T_CONFIG_INT
, T_CONFIG_SCOPE_SERVER
}, /* 12 */
47 { "server.max-worker", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_SERVER
}, /* 13 */
48 { "server.document-root", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 14 */
49 { "server.force-lowercase-filenames", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 15 */
50 { "debug.log-condition-handling", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 16 */
51 { "server.max-keep-alive-requests", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 17 */
52 { "server.name", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 18 */
53 { "server.max-keep-alive-idle", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 19 */
55 { "server.max-read-idle", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 20 */
56 { "server.max-write-idle", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 21 */
57 { "server.error-handler", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 22 */
58 { "server.max-fds", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_SERVER
}, /* 23 */
60 { "server.follow-symlink", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 24 */
62 { "server.follow-symlink",
63 "Your system lacks lstat(). We can not differ symlinks from files."
64 "Please remove server.follow-symlinks from your config.",
65 T_CONFIG_UNSUPPORTED
, T_CONFIG_SCOPE_UNSET
},
67 { "server.kbytes-per-second", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 25 */
68 { "connection.kbytes-per-second", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 26 */
69 { "mimetype.use-xattr", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 27 */
70 { "mimetype.assign", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 28 */
71 { "ssl.pemfile", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 29 */
73 { "ssl.engine", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 30 */
74 { "debug.log-file-not-found", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 31 */
75 { "debug.log-request-handling", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 32 */
76 { "debug.log-response-header", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 33 */
77 { "debug.log-request-header", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 34 */
78 { "debug.log-ssl-noise", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 35 */
79 { "server.protocol-http11", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 36 */
80 { "debug.log-request-header-on-error", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_SERVER
}, /* 37 */
81 { "debug.log-state-handling", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_SERVER
}, /* 38 */
82 { "ssl.ca-file", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 39 */
84 { "server.errorlog-use-syslog", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_SERVER
}, /* 40 */
85 { "server.range-requests", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 41 */
86 { "server.stat-cache-engine", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_SERVER
}, /* 42 */
87 { "server.max-connections", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_SERVER
}, /* 43 */
88 { "server.network-backend", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_SERVER
}, /* 44 */
89 { "server.upload-dirs", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_SERVER
}, /* 45 */
90 { "server.core-files", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_SERVER
}, /* 46 */
91 { "ssl.cipher-list", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 47 */
92 { "ssl.use-sslv2", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 48 */
93 { "etag.use-inode", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 49 */
95 { "etag.use-mtime", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 50 */
96 { "etag.use-size", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 51 */
97 { "server.reject-expect-100-with-417", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_SERVER
}, /* 52 */
98 { "debug.log-timeouts", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 53 */
99 { "server.defer-accept", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 54 */
100 { "server.breakagelog", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_SERVER
}, /* 55 */
101 { "ssl.verifyclient.activate", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 56 */
102 { "ssl.verifyclient.enforce", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 57 */
103 { "ssl.verifyclient.depth", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 58 */
104 { "ssl.verifyclient.username", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 59 */
106 { "ssl.verifyclient.exportcert", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 60 */
107 { "server.set-v6only", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 61 */
108 { "ssl.use-sslv3", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 62 */
109 { "ssl.dh-file", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 63 */
110 { "ssl.ec-curve", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 64 */
111 { "ssl.disable-client-renegotiation", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 65 */
112 { "ssl.honor-cipher-order", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 66 */
113 { "ssl.empty-fragments", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 67 */
114 { "server.upload-temp-file-size", NULL
, T_CONFIG_INT
, T_CONFIG_SCOPE_SERVER
}, /* 68 */
115 { "mimetype.xattr-name", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_SERVER
}, /* 69 */
116 { "server.listen-backlog", NULL
, T_CONFIG_INT
, T_CONFIG_SCOPE_CONNECTION
}, /* 70 */
117 { "server.error-handler-404", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 71 */
118 { "server.http-parseopt-header-strict",NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_SERVER
}, /* 72 */
119 { "server.http-parseopt-host-strict", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_SERVER
}, /* 73 */
120 { "server.http-parseopt-host-normalize",NULL
,T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_SERVER
}, /* 74 */
123 "use server.bind instead",
124 T_CONFIG_DEPRECATED
, T_CONFIG_SCOPE_UNSET
},
126 "use server.document-root instead",
127 T_CONFIG_DEPRECATED
, T_CONFIG_SCOPE_UNSET
},
128 { "server.virtual-root",
129 "load mod_simple_vhost and use simple-vhost.server-root instead",
130 T_CONFIG_DEPRECATED
, T_CONFIG_SCOPE_UNSET
},
131 { "server.virtual-default-host",
132 "load mod_simple_vhost and use simple-vhost.default-host instead",
133 T_CONFIG_DEPRECATED
, T_CONFIG_SCOPE_UNSET
},
134 { "server.virtual-docroot",
135 "load mod_simple_vhost and use simple-vhost.document-root instead",
136 T_CONFIG_DEPRECATED
, T_CONFIG_SCOPE_UNSET
},
138 "use server.username instead",
139 T_CONFIG_DEPRECATED
, T_CONFIG_SCOPE_UNSET
},
141 "use server.groupname instead",
142 T_CONFIG_DEPRECATED
, T_CONFIG_SCOPE_UNSET
},
143 { "server.use-keep-alive",
144 "use server.max-keep-alive-requests = 0 instead",
145 T_CONFIG_DEPRECATED
, T_CONFIG_SCOPE_UNSET
},
146 { "server.force-lower-case-files",
147 "use server.force-lowercase-filenames instead",
148 T_CONFIG_DEPRECATED
, T_CONFIG_SCOPE_UNSET
},
150 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
153 /* all T_CONFIG_SCOPE_SERVER options */
154 cv
[0].destination
= srv
->srvconf
.bindhost
;
155 cv
[1].destination
= srv
->srvconf
.errorlog_file
;
156 cv
[3].destination
= srv
->srvconf
.changeroot
;
157 cv
[4].destination
= srv
->srvconf
.username
;
158 cv
[5].destination
= srv
->srvconf
.groupname
;
159 cv
[6].destination
= &(srv
->srvconf
.port
);
160 cv
[9].destination
= srv
->srvconf
.modules
;
162 cv
[10].destination
= srv
->srvconf
.event_handler
;
163 cv
[11].destination
= srv
->srvconf
.pid_file
;
164 cv
[12].destination
= &(srv
->srvconf
.max_request_size
);
165 cv
[13].destination
= &(srv
->srvconf
.max_worker
);
167 cv
[23].destination
= &(srv
->srvconf
.max_fds
);
169 cv
[37].destination
= &(srv
->srvconf
.log_request_header_on_error
);
170 cv
[38].destination
= &(srv
->srvconf
.log_state_handling
);
172 cv
[40].destination
= &(srv
->srvconf
.errorlog_use_syslog
);
173 stat_cache_string
= buffer_init();
174 cv
[42].destination
= stat_cache_string
;
175 cv
[43].destination
= &(srv
->srvconf
.max_conns
);
176 cv
[44].destination
= srv
->srvconf
.network_backend
;
177 cv
[45].destination
= srv
->srvconf
.upload_tempdirs
;
178 cv
[46].destination
= &(srv
->srvconf
.enable_cores
);
180 cv
[52].destination
= &(srv
->srvconf
.reject_expect_100_with_417
);
181 cv
[55].destination
= srv
->srvconf
.breakagelog_file
;
183 cv
[68].destination
= &(srv
->srvconf
.upload_temp_file_size
);
184 cv
[69].destination
= srv
->srvconf
.xattr_name
;
185 cv
[72].destination
= &(srv
->srvconf
.http_header_strict
);
186 cv
[73].destination
= &(srv
->srvconf
.http_host_strict
);
187 cv
[74].destination
= &(srv
->srvconf
.http_host_normalize
);
189 srv
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(specific_config
*));
191 force_assert(srv
->config_storage
);
193 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
194 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
197 s
= calloc(1, sizeof(specific_config
));
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();
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 s
->max_keep_alive_requests
= 16;
212 s
->max_keep_alive_idle
= 5;
213 s
->max_read_idle
= 60;
214 s
->max_write_idle
= 360;
217 s
->ssl_honor_cipher_order
= 1;
218 s
->ssl_empty_fragments
= 0;
219 s
->ssl_use_sslv2
= 0;
220 s
->ssl_use_sslv3
= 0;
225 s
->follow_symlink
= 1;
227 s
->kbytes_per_second
= 0;
229 s
->etag_use_inode
= 1;
230 s
->etag_use_mtime
= 1;
231 s
->etag_use_size
= 1;
232 s
->range_requests
= 1;
233 s
->force_lowercase_filenames
= (i
== 0) ? 2 : 0; /* we wan't to detect later if user changed this for global section */
234 s
->global_kbytes_per_second
= 0;
235 s
->global_bytes_per_second_cnt
= 0;
236 s
->global_bytes_per_second_cnt_ptr
= &s
->global_bytes_per_second_cnt
;
237 s
->ssl_verifyclient
= 0;
238 s
->ssl_verifyclient_enforce
= 1;
239 s
->ssl_verifyclient_username
= buffer_init();
240 s
->ssl_verifyclient_depth
= 9;
241 s
->ssl_verifyclient_export_cert
= 0;
242 s
->ssl_disable_client_renegotiation
= 1;
243 s
->listen_backlog
= (0 == i
? 1024 : srv
->config_storage
[0]->listen_backlog
);
245 /* all T_CONFIG_SCOPE_CONNECTION options */
246 cv
[2].destination
= s
->errorfile_prefix
;
247 cv
[7].destination
= s
->server_tag
;
248 cv
[8].destination
= &(s
->use_ipv6
);
250 cv
[14].destination
= s
->document_root
;
251 cv
[15].destination
= &(s
->force_lowercase_filenames
);
252 cv
[16].destination
= &(s
->log_condition_handling
);
253 cv
[17].destination
= &(s
->max_keep_alive_requests
);
254 cv
[18].destination
= s
->server_name
;
255 cv
[19].destination
= &(s
->max_keep_alive_idle
);
257 cv
[20].destination
= &(s
->max_read_idle
);
258 cv
[21].destination
= &(s
->max_write_idle
);
259 cv
[22].destination
= s
->error_handler
;
261 cv
[24].destination
= &(s
->follow_symlink
);
263 cv
[25].destination
= &(s
->global_kbytes_per_second
);
264 cv
[26].destination
= &(s
->kbytes_per_second
);
265 cv
[27].destination
= &(s
->use_xattr
);
266 cv
[28].destination
= s
->mimetypes
;
267 cv
[29].destination
= s
->ssl_pemfile
;
269 cv
[30].destination
= &(s
->ssl_enabled
);
270 cv
[31].destination
= &(s
->log_file_not_found
);
271 cv
[32].destination
= &(s
->log_request_handling
);
272 cv
[33].destination
= &(s
->log_response_header
);
273 cv
[34].destination
= &(s
->log_request_header
);
274 cv
[35].destination
= &(s
->log_ssl_noise
);
275 cv
[36].destination
= &(s
->allow_http11
);
276 cv
[39].destination
= s
->ssl_ca_file
;
278 cv
[41].destination
= &(s
->range_requests
);
279 cv
[47].destination
= s
->ssl_cipher_list
;
280 cv
[48].destination
= &(s
->ssl_use_sslv2
);
281 cv
[49].destination
= &(s
->etag_use_inode
);
283 cv
[50].destination
= &(s
->etag_use_mtime
);
284 cv
[51].destination
= &(s
->etag_use_size
);
285 cv
[53].destination
= &(s
->log_timeouts
);
286 cv
[54].destination
= &(s
->defer_accept
);
287 cv
[56].destination
= &(s
->ssl_verifyclient
);
288 cv
[57].destination
= &(s
->ssl_verifyclient_enforce
);
289 cv
[58].destination
= &(s
->ssl_verifyclient_depth
);
290 cv
[59].destination
= s
->ssl_verifyclient_username
;
292 cv
[60].destination
= &(s
->ssl_verifyclient_export_cert
);
293 cv
[61].destination
= &(s
->set_v6only
);
294 cv
[62].destination
= &(s
->ssl_use_sslv3
);
295 cv
[63].destination
= s
->ssl_dh_file
;
296 cv
[64].destination
= s
->ssl_ec_curve
;
297 cv
[65].destination
= &(s
->ssl_disable_client_renegotiation
);
298 cv
[66].destination
= &(s
->ssl_honor_cipher_order
);
299 cv
[67].destination
= &(s
->ssl_empty_fragments
);
300 cv
[70].destination
= &(s
->listen_backlog
);
301 cv
[71].destination
= s
->error_handler_404
;
303 srv
->config_storage
[i
] = s
;
305 if (0 != (ret
= config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
))) {
311 specific_config
*s
= srv
->config_storage
[0];
312 s
->http_parseopts
= /*(global, but stored in con->conf.http_parseopts)*/
313 (srv
->srvconf
.http_header_strict
?(HTTP_PARSEOPT_HEADER_STRICT
) :0)
314 |(srv
->srvconf
.http_host_strict
?(HTTP_PARSEOPT_HOST_STRICT
315 |HTTP_PARSEOPT_HOST_NORMALIZE
):0)
316 |(srv
->srvconf
.http_host_normalize
?(HTTP_PARSEOPT_HOST_NORMALIZE
):0);
319 if (buffer_string_is_empty(stat_cache_string
)) {
320 srv
->srvconf
.stat_cache_engine
= STAT_CACHE_ENGINE_SIMPLE
;
321 } else if (buffer_is_equal_string(stat_cache_string
, CONST_STR_LEN("simple"))) {
322 srv
->srvconf
.stat_cache_engine
= STAT_CACHE_ENGINE_SIMPLE
;
324 } else if (buffer_is_equal_string(stat_cache_string
, CONST_STR_LEN("fam"))) {
325 srv
->srvconf
.stat_cache_engine
= STAT_CACHE_ENGINE_FAM
;
327 } else if (buffer_is_equal_string(stat_cache_string
, CONST_STR_LEN("disable"))) {
328 srv
->srvconf
.stat_cache_engine
= STAT_CACHE_ENGINE_NONE
;
330 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
331 "server.stat-cache-engine can be one of \"disable\", \"simple\","
335 " but not:", stat_cache_string
);
339 buffer_free(stat_cache_string
);
343 int prepend_mod_indexfile
= 1;
344 int append_mod_dirlisting
= 1;
345 int append_mod_staticfile
= 1;
347 /* prepend default modules */
348 for (i
= 0; i
< srv
->srvconf
.modules
->used
; i
++) {
349 ds
= (data_string
*)srv
->srvconf
.modules
->data
[i
];
351 if (buffer_is_equal_string(ds
->value
, CONST_STR_LEN("mod_indexfile"))) {
352 prepend_mod_indexfile
= 0;
355 if (buffer_is_equal_string(ds
->value
, CONST_STR_LEN("mod_staticfile"))) {
356 append_mod_staticfile
= 0;
359 if (buffer_is_equal_string(ds
->value
, CONST_STR_LEN("mod_dirlisting"))) {
360 append_mod_dirlisting
= 0;
363 if (0 == prepend_mod_indexfile
&&
364 0 == append_mod_dirlisting
&&
365 0 == append_mod_staticfile
) {
370 if (prepend_mod_indexfile
) {
371 /* mod_indexfile has to be loaded before mod_fastcgi and friends */
372 array
*modules
= array_init();
374 ds
= data_string_init();
375 buffer_copy_string_len(ds
->value
, CONST_STR_LEN("mod_indexfile"));
376 array_insert_unique(modules
, (data_unset
*)ds
);
378 for (i
= 0; i
< srv
->srvconf
.modules
->used
; i
++) {
379 data_unset
*du
= srv
->srvconf
.modules
->data
[i
];
380 array_insert_unique(modules
, du
->copy(du
));
383 array_free(srv
->srvconf
.modules
);
384 srv
->srvconf
.modules
= modules
;
387 /* append default modules */
388 if (append_mod_dirlisting
) {
389 ds
= data_string_init();
390 buffer_copy_string_len(ds
->value
, CONST_STR_LEN("mod_dirlisting"));
391 array_insert_unique(srv
->srvconf
.modules
, (data_unset
*)ds
);
394 if (append_mod_staticfile
) {
395 ds
= data_string_init();
396 buffer_copy_string_len(ds
->value
, CONST_STR_LEN("mod_staticfile"));
397 array_insert_unique(srv
->srvconf
.modules
, (data_unset
*)ds
);
406 #define PATCH(x) con->conf.x = s->x
407 int config_setup_connection(server
*srv
, connection
*con
) {
408 specific_config
*s
= srv
->config_storage
[0];
410 PATCH(http_parseopts
);
414 PATCH(document_root
);
415 PATCH(max_keep_alive_requests
);
416 PATCH(max_keep_alive_idle
);
417 PATCH(max_read_idle
);
418 PATCH(max_write_idle
);
420 PATCH(error_handler
);
421 PATCH(error_handler_404
);
422 PATCH(errorfile_prefix
);
424 PATCH(follow_symlink
);
427 PATCH(kbytes_per_second
);
428 PATCH(global_kbytes_per_second
);
429 PATCH(global_bytes_per_second_cnt
);
431 con
->conf
.global_bytes_per_second_cnt_ptr
= &s
->global_bytes_per_second_cnt
;
432 buffer_copy_buffer(con
->server_name
, s
->server_name
);
434 PATCH(log_request_header
);
435 PATCH(log_response_header
);
436 PATCH(log_request_handling
);
437 PATCH(log_condition_handling
);
438 PATCH(log_file_not_found
);
439 PATCH(log_ssl_noise
);
442 PATCH(range_requests
);
443 PATCH(force_lowercase_filenames
);
444 /*PATCH(listen_backlog);*//*(not necessary; used only at startup)*/
449 PATCH(ssl_pemfile_x509
);
450 PATCH(ssl_pemfile_pkey
);
454 PATCH(ssl_ca_file_cert_names
);
456 PATCH(ssl_cipher_list
);
459 PATCH(ssl_honor_cipher_order
);
460 PATCH(ssl_empty_fragments
);
461 PATCH(ssl_use_sslv2
);
462 PATCH(ssl_use_sslv3
);
463 PATCH(etag_use_inode
);
464 PATCH(etag_use_mtime
);
465 PATCH(etag_use_size
);
467 PATCH(ssl_verifyclient
);
468 PATCH(ssl_verifyclient_enforce
);
469 PATCH(ssl_verifyclient_depth
);
470 PATCH(ssl_verifyclient_username
);
471 PATCH(ssl_verifyclient_export_cert
);
472 PATCH(ssl_disable_client_renegotiation
);
477 int config_patch_connection(server
*srv
, connection
*con
) {
480 /* skip the first, the global context */
481 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
482 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
483 specific_config
*s
= srv
->config_storage
[i
];
485 /* condition didn't match */
486 if (!config_check_cond(srv
, con
, dc
)) continue;
489 for (j
= 0; j
< dc
->value
->used
; j
++) {
490 data_unset
*du
= dc
->value
->data
[j
];
492 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.document-root"))) {
493 PATCH(document_root
);
494 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.range-requests"))) {
495 PATCH(range_requests
);
496 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.error-handler"))) {
497 PATCH(error_handler
);
498 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.error-handler-404"))) {
499 PATCH(error_handler_404
);
500 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.errorfile-prefix"))) {
501 PATCH(errorfile_prefix
);
502 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("mimetype.assign"))) {
504 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.max-keep-alive-requests"))) {
505 PATCH(max_keep_alive_requests
);
506 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.max-keep-alive-idle"))) {
507 PATCH(max_keep_alive_idle
);
508 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.max-write-idle"))) {
509 PATCH(max_write_idle
);
510 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.max-read-idle"))) {
511 PATCH(max_read_idle
);
512 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("mimetype.use-xattr"))) {
514 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("etag.use-inode"))) {
515 PATCH(etag_use_inode
);
516 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("etag.use-mtime"))) {
517 PATCH(etag_use_mtime
);
518 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("etag.use-size"))) {
519 PATCH(etag_use_size
);
520 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.pemfile"))) {
523 PATCH(ssl_pemfile_x509
);
524 PATCH(ssl_pemfile_pkey
);
526 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.ca-file"))) {
529 PATCH(ssl_ca_file_cert_names
);
531 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.honor-cipher-order"))) {
532 PATCH(ssl_honor_cipher_order
);
533 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.empty-fragments"))) {
534 PATCH(ssl_empty_fragments
);
535 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.use-sslv2"))) {
536 PATCH(ssl_use_sslv2
);
537 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.use-sslv3"))) {
538 PATCH(ssl_use_sslv3
);
539 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.cipher-list"))) {
540 PATCH(ssl_cipher_list
);
541 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.engine"))) {
543 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.dh-file"))) {
545 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.ec-curve"))) {
548 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.follow-symlink"))) {
549 PATCH(follow_symlink
);
551 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.name"))) {
552 buffer_copy_buffer(con
->server_name
, s
->server_name
);
553 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.tag"))) {
555 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("connection.kbytes-per-second"))) {
556 PATCH(kbytes_per_second
);
557 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("debug.log-request-handling"))) {
558 PATCH(log_request_handling
);
559 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("debug.log-request-header"))) {
560 PATCH(log_request_header
);
561 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("debug.log-response-header"))) {
562 PATCH(log_response_header
);
563 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("debug.log-condition-handling"))) {
564 PATCH(log_condition_handling
);
565 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("debug.log-file-not-found"))) {
566 PATCH(log_file_not_found
);
567 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("debug.log-ssl-noise"))) {
568 PATCH(log_ssl_noise
);
569 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("debug.log-timeouts"))) {
571 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.protocol-http11"))) {
573 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.force-lowercase-filenames"))) {
574 PATCH(force_lowercase_filenames
);
575 #if 0 /*(not necessary; used only at startup)*/
576 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.listen-backlog"))) {
577 PATCH(listen_backlog
);
579 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("server.kbytes-per-second"))) {
580 PATCH(global_kbytes_per_second
);
581 PATCH(global_bytes_per_second_cnt
);
582 con
->conf
.global_bytes_per_second_cnt_ptr
= &s
->global_bytes_per_second_cnt
;
583 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.verifyclient.activate"))) {
584 PATCH(ssl_verifyclient
);
585 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.verifyclient.enforce"))) {
586 PATCH(ssl_verifyclient_enforce
);
587 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.verifyclient.depth"))) {
588 PATCH(ssl_verifyclient_depth
);
589 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.verifyclient.username"))) {
590 PATCH(ssl_verifyclient_username
);
591 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.verifyclient.exportcert"))) {
592 PATCH(ssl_verifyclient_export_cert
);
593 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("ssl.disable-client-renegotiation"))) {
594 PATCH(ssl_disable_client_renegotiation
);
599 con
->etag_flags
= (con
->conf
.etag_use_mtime
? ETAG_USE_MTIME
: 0) |
600 (con
->conf
.etag_use_inode
? ETAG_USE_INODE
: 0) |
601 (con
->conf
.etag_use_size
? ETAG_USE_SIZE
: 0);
611 const buffer
*source
;
625 static int tokenizer_open(server
*srv
, tokenizer_t
*t
, buffer
*basedir
, const char *fn
) {
626 if (buffer_string_is_empty(basedir
) ||
627 (fn
[0] == '/' || fn
[0] == '\\') ||
628 (fn
[0] == '.' && (fn
[1] == '/' || fn
[1] == '\\'))) {
629 t
->file
= buffer_init_string(fn
);
631 t
->file
= buffer_init_buffer(basedir
);
632 buffer_append_string(t
->file
, fn
);
635 if (0 != stream_open(&(t
->s
), t
->file
)) {
636 log_error_write(srv
, __FILE__
, __LINE__
, "sbss",
637 "opening configfile ", t
->file
, "failed:", strerror(errno
));
638 buffer_free(t
->file
);
642 t
->input
= t
->s
.start
;
654 static int tokenizer_close(server
*srv
, tokenizer_t
*t
) {
657 buffer_free(t
->file
);
658 return stream_close(&(t
->s
));
661 static int config_skip_newline(tokenizer_t
*t
) {
663 force_assert(t
->input
[t
->offset
] == '\r' || t
->input
[t
->offset
] == '\n');
664 if (t
->input
[t
->offset
] == '\r' && t
->input
[t
->offset
+ 1] == '\n') {
672 static int config_skip_comment(tokenizer_t
*t
) {
674 force_assert(t
->input
[t
->offset
] == '#');
675 for (i
= 1; t
->input
[t
->offset
+ i
] &&
676 (t
->input
[t
->offset
+ i
] != '\n' && t
->input
[t
->offset
+ i
] != '\r');
682 static int config_tokenizer(server
*srv
, tokenizer_t
*t
, int *token_id
, buffer
*token
) {
686 for (tid
= 0; tid
== 0 && t
->offset
< t
->size
&& t
->input
[t
->offset
] ; ) {
687 char c
= t
->input
[t
->offset
];
688 const char *start
= NULL
;
693 if (t
->input
[t
->offset
+ 1] == '>') {
696 buffer_copy_string_len(token
, CONST_STR_LEN("=>"));
698 tid
= TK_ARRAY_ASSIGN
;
700 log_error_write(srv
, __FILE__
, __LINE__
, "sbsdsds",
701 "source:", t
->source
,
702 "line:", t
->line
, "pos:", t
->line_pos
,
703 "use => for assignments in arrays");
706 } else if (t
->in_cond
) {
707 if (t
->input
[t
->offset
+ 1] == '=') {
710 buffer_copy_string_len(token
, CONST_STR_LEN("=="));
713 } else if (t
->input
[t
->offset
+ 1] == '~') {
716 buffer_copy_string_len(token
, CONST_STR_LEN("=~"));
720 log_error_write(srv
, __FILE__
, __LINE__
, "sbsdsds",
721 "source:", t
->source
,
722 "line:", t
->line
, "pos:", t
->line_pos
,
723 "only =~ and == are allowed in the condition");
728 } else if (t
->in_key
) {
731 buffer_copy_string_len(token
, t
->input
+ t
->offset
, 1);
736 log_error_write(srv
, __FILE__
, __LINE__
, "sbsdsds",
737 "source:", t
->source
,
738 "line:", t
->line
, "pos:", t
->line_pos
,
739 "unexpected equal-sign: =");
746 if (t
->input
[t
->offset
+ 1] == '=') {
749 buffer_copy_string_len(token
, CONST_STR_LEN("!="));
752 } else if (t
->input
[t
->offset
+ 1] == '~') {
755 buffer_copy_string_len(token
, CONST_STR_LEN("!~"));
759 log_error_write(srv
, __FILE__
, __LINE__
, "sbsdsds",
760 "source:", t
->source
,
761 "line:", t
->line
, "pos:", t
->line_pos
,
762 "only !~ and != are allowed in the condition");
768 log_error_write(srv
, __FILE__
, __LINE__
, "sbsdsds",
769 "source:", t
->source
,
770 "line:", t
->line
, "pos:", t
->line_pos
,
771 "unexpected exclamation-marks: !");
783 if (t
->in_brace
== 0) {
785 while (!done
&& t
->offset
< t
->size
) {
786 switch (t
->input
[t
->offset
]) {
789 config_skip_newline(t
);
795 t
->line_pos
+= config_skip_comment(t
);
810 buffer_copy_string_len(token
, CONST_STR_LEN("(EOL)"));
812 config_skip_newline(t
);
818 if (t
->in_brace
> 0) {
821 buffer_copy_string_len(token
, CONST_STR_LEN("(COMMA)"));
828 /* search for the terminating " */
829 start
= t
->input
+ t
->offset
+ 1;
830 buffer_copy_string_len(token
, CONST_STR_LEN(""));
832 for (i
= 1; t
->input
[t
->offset
+ i
]; i
++) {
833 if (t
->input
[t
->offset
+ i
] == '\\' &&
834 t
->input
[t
->offset
+ i
+ 1] == '"') {
836 buffer_append_string_len(token
, start
, t
->input
+ t
->offset
+ i
- start
);
838 start
= t
->input
+ t
->offset
+ i
+ 1;
846 if (t
->input
[t
->offset
+ i
] == '"') {
849 buffer_append_string_len(token
, start
, t
->input
+ t
->offset
+ i
- start
);
855 if (t
->input
[t
->offset
+ i
] == '\0') {
858 log_error_write(srv
, __FILE__
, __LINE__
, "sbsdsds",
859 "source:", t
->source
,
860 "line:", t
->line
, "pos:", t
->line_pos
,
861 "missing closing quote");
867 t
->line_pos
+= i
+ 1;
876 buffer_copy_string_len(token
, CONST_STR_LEN("("));
884 buffer_copy_string_len(token
, CONST_STR_LEN(")"));
893 buffer_copy_string_len(token
, CONST_STR_LEN("$"));
898 if (t
->input
[t
->offset
+ 1] == '=') {
900 buffer_copy_string_len(token
, CONST_STR_LEN("+="));
905 buffer_copy_string_len(token
, CONST_STR_LEN("+"));
914 buffer_copy_string_len(token
, CONST_STR_LEN("{"));
923 buffer_copy_string_len(token
, CONST_STR_LEN("}"));
932 buffer_copy_string_len(token
, CONST_STR_LEN("["));
941 buffer_copy_string_len(token
, CONST_STR_LEN("]"));
945 t
->line_pos
+= config_skip_comment(t
);
950 for (i
= 0; t
->input
[t
->offset
+ i
] &&
951 (isalpha((unsigned char)t
->input
[t
->offset
+ i
])
954 if (i
&& t
->input
[t
->offset
+ i
]) {
956 buffer_copy_string_len(token
, t
->input
+ t
->offset
, i
);
962 log_error_write(srv
, __FILE__
, __LINE__
, "sbsdsds",
963 "source:", t
->source
,
964 "line:", t
->line
, "pos:", t
->line_pos
,
965 "invalid character in condition");
968 } else if (isdigit((unsigned char)c
)) {
969 /* take all digits */
970 for (i
= 0; t
->input
[t
->offset
+ i
] && isdigit((unsigned char)t
->input
[t
->offset
+ i
]); i
++);
972 /* was there it least a digit ? */
976 buffer_copy_string_len(token
, t
->input
+ t
->offset
, i
);
982 /* the key might consist of [-.0-9a-z] */
983 for (i
= 0; t
->input
[t
->offset
+ i
] &&
984 (isalnum((unsigned char)t
->input
[t
->offset
+ i
]) ||
985 t
->input
[t
->offset
+ i
] == '.' ||
986 t
->input
[t
->offset
+ i
] == '_' || /* for env.* */
987 t
->input
[t
->offset
+ i
] == '-'
990 if (i
&& t
->input
[t
->offset
+ i
]) {
991 buffer_copy_string_len(token
, t
->input
+ t
->offset
, i
);
993 if (strcmp(token
->ptr
, "include") == 0) {
995 } else if (strcmp(token
->ptr
, "include_shell") == 0) {
996 tid
= TK_INCLUDE_SHELL
;
997 } else if (strcmp(token
->ptr
, "global") == 0) {
999 } else if (strcmp(token
->ptr
, "else") == 0) {
1009 log_error_write(srv
, __FILE__
, __LINE__
, "sbsdsds",
1010 "source:", t
->source
,
1011 "line:", t
->line
, "pos:", t
->line_pos
,
1012 "invalid character in variable name");
1023 log_error_write(srv
, __FILE__
, __LINE__
, "sbsdsdbdd",
1024 "source:", t
->source
,
1025 "line:", t
->line
, "pos:", t
->line_pos
,
1026 token
, token
->used
- 1, tid
);
1030 } else if (t
->offset
< t
->size
) {
1031 fprintf(stderr
, "%s.%d: %d, %s\n",
1038 static int config_parse(server
*srv
, config_t
*context
, tokenizer_t
*t
) {
1041 buffer
*token
, *lasttoken
;
1044 pParser
= configparserAlloc( malloc
);
1045 force_assert(pParser
);
1046 lasttoken
= buffer_init();
1047 token
= buffer_init();
1048 while((1 == (ret
= config_tokenizer(srv
, t
, &token_id
, token
))) && context
->ok
) {
1049 buffer_copy_buffer(lasttoken
, token
);
1050 configparser(pParser
, token_id
, token
, context
);
1052 token
= buffer_init();
1056 if (ret
!= -1 && context
->ok
) {
1057 /* add an EOL at EOF, better than say sorry */
1058 configparser(pParser
, TK_EOL
, buffer_init_string("(EOL)"), context
);
1060 configparser(pParser
, 0, NULL
, context
);
1063 configparserFree(pParser
, free
);
1066 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1067 "configfile parser failed at:", lasttoken
);
1068 } else if (context
->ok
== 0) {
1069 log_error_write(srv
, __FILE__
, __LINE__
, "sbsdsdsb",
1070 "source:", t
->source
,
1071 "line:", t
->line
, "pos:", t
->line_pos
,
1072 "parser failed somehow near here:", lasttoken
);
1075 buffer_free(lasttoken
);
1077 return ret
== -1 ? -1 : 0;
1080 static int tokenizer_init(tokenizer_t
*t
, const buffer
*source
, const char *input
, size_t size
) {
1095 static int config_parse_file_stream(server
*srv
, config_t
*context
, const buffer
*filename
) {
1100 if (0 != stream_open(&s
, filename
)) {
1101 log_error_write(srv
, __FILE__
, __LINE__
, "sbss",
1102 "opening configfile ", filename
, "failed:", strerror(errno
));
1105 tokenizer_init(&t
, filename
, s
.start
, s
.size
);
1106 ret
= config_parse(srv
, context
, &t
);
1113 int config_parse_file(server
*srv
, config_t
*context
, const char *fn
) {
1118 int flags
= GLOB_BRACE
;
1124 if ((fn
[0] == '/' || fn
[0] == '\\') ||
1125 (fn
[0] == '.' && (fn
[1] == '/' || fn
[1] == '\\')) ||
1126 (fn
[0] == '.' && fn
[1] == '.' && (fn
[2] == '/' || fn
[2] == '\\'))) {
1127 filename
= buffer_init_string(fn
);
1129 filename
= buffer_init_buffer(context
->basedir
);
1130 buffer_append_string(filename
, fn
);
1133 switch (glob(filename
->ptr
, flags
, NULL
, &gl
)) {
1135 for (i
= 0; i
< gl
.gl_pathc
; ++i
) {
1136 buffer_copy_string(filename
, gl
.gl_pathv
[i
]);
1137 ret
= config_parse_file_stream(srv
, context
, filename
);
1138 if (0 != ret
) break;
1143 if (filename
->ptr
[strcspn(filename
->ptr
, "*?[]{}")] != '\0') { /*(contains glob metachars)*/
1144 ret
= 0; /* not an error if no files match glob pattern */
1147 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "include file not found: ", filename
);
1152 log_error_write(srv
, __FILE__
, __LINE__
, "sbss", "glob()", filename
, "failed:", strerror(errno
));
1156 buffer_free(filename
);
1160 static char* getCWD(void) {
1170 if (!s
) return NULL
;
1171 while (NULL
== getcwd(s
, len
)) {
1172 if (errno
!= ERANGE
|| SSIZE_MAX
- len
< len
) {
1177 s1
= realloc(s
, len
);
1187 int config_parse_cmd(server
*srv
, config_t
*context
, const char *cmd
) {
1194 if (NULL
== (oldpwd
= getCWD())) {
1195 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1196 "cannot get cwd", strerror(errno
));
1200 if (!buffer_string_is_empty(context
->basedir
)) {
1201 if (0 != chdir(context
->basedir
->ptr
)) {
1202 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1203 "cannot change directory to", context
->basedir
, strerror(errno
));
1209 source
= buffer_init_string(cmd
);
1210 out
= buffer_init();
1212 if (0 != proc_open_buffer(cmd
, NULL
, out
, NULL
)) {
1213 log_error_write(srv
, __FILE__
, __LINE__
, "sbss",
1214 "opening", source
, "failed:", strerror(errno
));
1217 tokenizer_init(&t
, source
, CONST_BUF_LEN(out
));
1218 ret
= config_parse(srv
, context
, &t
);
1221 buffer_free(source
);
1223 if (0 != chdir(oldpwd
)) {
1224 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
1225 "cannot change directory to", oldpwd
, strerror(errno
));
1233 static void context_init(server
*srv
, config_t
*context
) {
1236 vector_config_weak_init(&context
->configs_stack
);
1237 context
->basedir
= buffer_init();
1240 static void context_free(config_t
*context
) {
1241 vector_config_weak_clear(&context
->configs_stack
);
1242 buffer_free(context
->basedir
);
1245 int config_read(server
*srv
, const char *fn
) {
1254 context_init(srv
, &context
);
1255 context
.all_configs
= srv
->config_context
;
1258 pos
= strrchr(fn
, '\\');
1260 pos
= strrchr(fn
, '/');
1263 buffer_copy_string_len(context
.basedir
, fn
, pos
- fn
+ 1);
1266 dc
= data_config_init();
1267 buffer_copy_string_len(dc
->key
, CONST_STR_LEN("global"));
1269 force_assert(context
.all_configs
->used
== 0);
1270 dc
->context_ndx
= context
.all_configs
->used
;
1271 array_insert_unique(context
.all_configs
, (data_unset
*)dc
);
1272 context
.current
= dc
;
1274 /* default context */
1275 dpid
= data_integer_init();
1276 dpid
->value
= getpid();
1277 buffer_copy_string_len(dpid
->key
, CONST_STR_LEN("var.PID"));
1278 array_insert_unique(dc
->value
, (data_unset
*)dpid
);
1280 dcwd
= data_string_init();
1281 buffer_string_prepare_copy(dcwd
->value
, 1023);
1282 if (NULL
!= getcwd(dcwd
->value
->ptr
, dcwd
->value
->size
- 1)) {
1283 buffer_commit(dcwd
->value
, strlen(dcwd
->value
->ptr
));
1284 buffer_copy_string_len(dcwd
->key
, CONST_STR_LEN("var.CWD"));
1285 array_insert_unique(dc
->value
, (data_unset
*)dcwd
);
1287 dcwd
->free((data_unset
*) dcwd
);
1290 filename
= buffer_init_string(fn
);
1291 ret
= config_parse_file_stream(srv
, &context
, filename
);
1292 buffer_free(filename
);
1294 /* remains nothing if parser is ok */
1295 force_assert(!(0 == ret
&& context
.ok
&& 0 != context
.configs_stack
.used
));
1296 context_free(&context
);
1302 if (0 != config_insert(srv
)) {
1309 int config_set_defaults(server
*srv
) {
1311 specific_config
*s
= srv
->config_storage
[0];
1312 struct stat st1
, st2
;
1314 struct ev_map
{ fdevent_handler_t et
; const char *name
; } event_handlers
[] =
1316 /* - epoll is most reliable
1317 * - select works everywhere
1319 #ifdef USE_LINUX_EPOLL
1320 { FDEVENT_HANDLER_LINUX_SYSEPOLL
, "linux-sysepoll" },
1323 { FDEVENT_HANDLER_POLL
, "poll" },
1326 { FDEVENT_HANDLER_SELECT
, "select" },
1329 { FDEVENT_HANDLER_LIBEV
, "libev" },
1331 #ifdef USE_SOLARIS_DEVPOLL
1332 { FDEVENT_HANDLER_SOLARIS_DEVPOLL
,"solaris-devpoll" },
1334 #ifdef USE_SOLARIS_PORT
1335 { FDEVENT_HANDLER_SOLARIS_PORT
, "solaris-eventports" },
1337 #ifdef USE_FREEBSD_KQUEUE
1338 { FDEVENT_HANDLER_FREEBSD_KQUEUE
, "freebsd-kqueue" },
1339 { FDEVENT_HANDLER_FREEBSD_KQUEUE
, "kqueue" },
1341 { FDEVENT_HANDLER_UNSET
, NULL
}
1344 if (!buffer_string_is_empty(srv
->srvconf
.changeroot
)) {
1345 if (-1 == stat(srv
->srvconf
.changeroot
->ptr
, &st1
)) {
1346 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1347 "server.chroot doesn't exist:", srv
->srvconf
.changeroot
);
1350 if (!S_ISDIR(st1
.st_mode
)) {
1351 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1352 "server.chroot isn't a directory:", srv
->srvconf
.changeroot
);
1357 if (srv
->srvconf
.upload_tempdirs
->used
) {
1358 buffer
* const b
= srv
->tmp_buf
;
1360 if (!buffer_string_is_empty(srv
->srvconf
.changeroot
)) {
1361 buffer_copy_buffer(b
, srv
->srvconf
.changeroot
);
1362 buffer_append_slash(b
);
1366 len
= buffer_string_length(b
);
1368 for (i
= 0; i
< srv
->srvconf
.upload_tempdirs
->used
; ++i
) {
1369 const data_string
* const ds
= (data_string
*)srv
->srvconf
.upload_tempdirs
->data
[i
];
1370 buffer_string_set_length(b
, len
); /*(truncate)*/
1371 buffer_append_string_buffer(b
, ds
->value
);
1372 if (-1 == stat(b
->ptr
, &st1
)) {
1373 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1374 "server.upload-dirs doesn't exist:", b
);
1375 } else if (!S_ISDIR(st1
.st_mode
)) {
1376 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1377 "server.upload-dirs isn't a directory:", b
);
1382 if (buffer_string_is_empty(s
->document_root
)) {
1383 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1384 "a default document-root has to be set");
1389 buffer_copy_buffer(srv
->tmp_buf
, s
->document_root
);
1391 buffer_to_lower(srv
->tmp_buf
);
1393 if (2 == s
->force_lowercase_filenames
) { /* user didn't configure it in global section? */
1394 s
->force_lowercase_filenames
= 0; /* default to 0 */
1396 if (0 == stat(srv
->tmp_buf
->ptr
, &st1
)) {
1399 is_lower
= buffer_is_equal(srv
->tmp_buf
, s
->document_root
);
1401 /* lower-case existed, check upper-case */
1402 buffer_copy_buffer(srv
->tmp_buf
, s
->document_root
);
1404 buffer_to_upper(srv
->tmp_buf
);
1406 /* we have to handle the special case that upper and lower-casing results in the same filename
1407 * as in server.document-root = "/" or "/12345/" */
1409 if (is_lower
&& buffer_is_equal(srv
->tmp_buf
, s
->document_root
)) {
1410 /* lower-casing and upper-casing didn't result in
1411 * an other filename, no need to stat(),
1412 * just assume it is case-sensitive. */
1414 s
->force_lowercase_filenames
= 0;
1415 } else if (0 == stat(srv
->tmp_buf
->ptr
, &st2
)) {
1417 /* upper case exists too, doesn't the FS handle this ? */
1419 /* upper and lower have the same inode -> case-insensitve FS */
1421 if (st1
.st_ino
== st2
.st_ino
) {
1422 /* upper and lower have the same inode -> case-insensitve FS */
1424 s
->force_lowercase_filenames
= 1;
1430 if (srv
->srvconf
.port
== 0) {
1431 srv
->srvconf
.port
= s
->ssl_enabled
? 443 : 80;
1434 if (buffer_string_is_empty(srv
->srvconf
.event_handler
)) {
1435 /* choose a good default
1437 * the event_handler list is sorted by 'goodness'
1438 * taking the first available should be the best solution
1440 srv
->event_handler
= event_handlers
[0].et
;
1442 if (FDEVENT_HANDLER_UNSET
== srv
->event_handler
) {
1443 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1444 "sorry, there is no event handler for this system");
1453 for (i
= 0; event_handlers
[i
].name
; i
++) {
1454 if (0 == strcmp(event_handlers
[i
].name
, srv
->srvconf
.event_handler
->ptr
)) {
1455 srv
->event_handler
= event_handlers
[i
].et
;
1460 if (FDEVENT_HANDLER_UNSET
== srv
->event_handler
) {
1461 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1462 "the selected event-handler in unknown or not supported:",
1463 srv
->srvconf
.event_handler
);
1469 if (s
->ssl_enabled
) {
1470 if (buffer_string_is_empty(s
->ssl_pemfile
)) {
1471 /* PEM file is require */
1473 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1474 "ssl.pemfile has to be set");
1479 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1480 "ssl support is missing, recompile with --with-openssl");