1 #include "first.h" 2 3 #include "base.h" 4 #include "burl.h" 5 #include "etag.h" 6 #include "fdevent.h" 7 #include "keyvalue.h" 8 #include "log.h" 9 #include "stream.h" 10 11 #include "configparser.h" 12 #include "configfile.h" 13 #include "plugin.h" 14 #include "stat_cache.h" 15 #include "sys-crypto.h" 16 17 #include <sys/stat.h> 18 #include <sys/wait.h> 19 20 #include <stdlib.h> 21 #include <fcntl.h> 22 #include <unistd.h> 23 #include <errno.h> 24 #include <string.h> 25 #include <ctype.h> 26 #include <limits.h> 27 #include <glob.h> 28 29 #ifdef HAVE_SYSLOG_H 30 # include <syslog.h> 31 #endif 32 33 #ifndef PATH_MAX 34 #define PATH_MAX 4096 35 #endif 36 37 typedef struct { 38 PLUGIN_DATA; 39 request_config defaults; 40 } config_data_base; 41 42 static void config_free_config(void * const p_d) { 43 plugin_data_base * const p = p_d; 44 if (NULL == p) return; 45 if (NULL == p->cvlist) { free(p); return; } 46 /* (init i to 0 if global context; to 1 to skip empty global context) */ 47 for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 48 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 49 for (; -1 != cpv->k_id; ++cpv) { 50 switch (cpv->k_id) { 51 case 18:/* server.kbytes-per-second */ 52 if (cpv->vtype == T_CONFIG_LOCAL) free(cpv->v.v); 53 break; 54 default: 55 break; 56 } 57 } 58 } 59 free(p->cvlist); 60 free(p); 61 } 62 63 void config_reset_config_bytes_sec(void * const p_d) { 64 plugin_data_base * const p = p_d; 65 if (NULL == p->cvlist) return; 66 /* (init i to 0 if global context; to 1 to skip empty global context) */ 67 for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 68 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 69 for (; -1 != cpv->k_id; ++cpv) { 70 switch (cpv->k_id) { 71 case 18:/* server.kbytes-per-second */ 72 if (cpv->vtype == T_CONFIG_LOCAL) ((off_t *)cpv->v.v)[0] = 0; 73 break; 74 default: 75 break; 76 } 77 } 78 } 79 } 80 81 static void config_merge_config_cpv(request_config * const pconf, const config_plugin_value_t * const cpv) { 82 switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ 83 case 0: /* server.document-root */ 84 pconf->document_root = cpv->v.b; 85 break; 86 case 1: /* server.name */ 87 pconf->server_name = cpv->v.b; 88 break; 89 case 2: /* server.tag */ 90 pconf->server_tag = cpv->v.b; 91 break; 92 case 3: /* server.max-request-size */ 93 pconf->max_request_size = cpv->v.u; 94 break; 95 case 4: /* server.max-keep-alive-requests */ 96 pconf->max_keep_alive_requests = cpv->v.shrt; 97 break; 98 case 5: /* server.max-keep-alive-idle */ 99 pconf->max_keep_alive_idle = cpv->v.shrt; 100 break; 101 case 6: /* server.max-read-idle */ 102 pconf->max_read_idle = cpv->v.shrt; 103 break; 104 case 7: /* server.max-write-idle */ 105 pconf->max_write_idle = cpv->v.shrt; 106 break; 107 case 8: /* server.errorfile-prefix */ 108 pconf->errorfile_prefix = cpv->v.b; 109 break; 110 case 9: /* server.error-handler */ 111 pconf->error_handler = cpv->v.b; 112 break; 113 case 10:/* server.error-handler-404 */ 114 pconf->error_handler_404 = cpv->v.b; 115 break; 116 case 11:/* server.error-intercept */ 117 pconf->error_intercept = (0 != cpv->v.u); 118 break; 119 case 12:/* server.force-lowercase-filenames */ 120 pconf->force_lowercase_filenames = (0 != cpv->v.u); 121 break; 122 case 13:/* server.follow-symlink */ 123 pconf->follow_symlink = (0 != cpv->v.u); 124 break; 125 case 14:/* server.protocol-http11 */ 126 pconf->allow_http11 = (0 != cpv->v.u); 127 break; 128 case 15:/* server.range-requests */ 129 pconf->range_requests = (0 != cpv->v.u); 130 break; 131 case 16:/* server.stream-request-body */ 132 pconf->stream_request_body = cpv->v.shrt; 133 break; 134 case 17:/* server.stream-response-body */ 135 pconf->stream_response_body = cpv->v.shrt; 136 break; 137 case 18:/* server.kbytes-per-second */ 138 pconf->global_bytes_per_second = (unsigned int)((off_t *)cpv->v.v)[1]; 139 pconf->global_bytes_per_second_cnt_ptr = cpv->v.v; 140 break; 141 case 19:/* connection.kbytes-per-second */ 142 pconf->bytes_per_second = (unsigned int)cpv->v.shrt << 10;/* (*=1024) */ 143 break; 144 case 20:/* mimetype.assign */ 145 pconf->mimetypes = cpv->v.a; 146 break; 147 case 21:/* mimetype.use-xattr */ 148 pconf->use_xattr = (0 != cpv->v.u); 149 break; 150 case 22:/* etag.use-inode */ 151 cpv->v.u 152 ? (pconf->etag_flags |= ETAG_USE_INODE) 153 : (pconf->etag_flags &= ~ETAG_USE_INODE); 154 break; 155 case 23:/* etag.use-mtime */ 156 cpv->v.u 157 ? (pconf->etag_flags |= ETAG_USE_MTIME) 158 : (pconf->etag_flags &= ~ETAG_USE_MTIME); 159 break; 160 case 24:/* etag.use-size */ 161 cpv->v.u 162 ? (pconf->etag_flags |= ETAG_USE_SIZE) 163 : (pconf->etag_flags &= ~ETAG_USE_SIZE); 164 break; 165 case 25:/* debug.log-condition-handling */ 166 pconf->log_condition_handling = (0 != cpv->v.u); 167 break; 168 case 26:/* debug.log-file-not-found */ 169 pconf->log_file_not_found = (0 != cpv->v.u); 170 break; 171 case 27:/* debug.log-request-handling */ 172 pconf->log_request_handling = (0 != cpv->v.u); 173 break; 174 case 28:/* debug.log-request-header */ 175 pconf->log_request_header = (0 != cpv->v.u); 176 break; 177 case 29:/* debug.log-response-header */ 178 pconf->log_response_header = (0 != cpv->v.u); 179 break; 180 case 30:/* debug.log-timeouts */ 181 pconf->log_timeouts = (0 != cpv->v.u); 182 break; 183 case 31: /* server.errorlog */ 184 if (cpv->vtype == T_CONFIG_LOCAL) pconf->errh = cpv->v.v; 185 break; 186 case 32:/* server.breakagelog */ 187 if (cpv->vtype == T_CONFIG_LOCAL) pconf->serrh = cpv->v.v; 188 break; 189 default:/* should not happen */ 190 return; 191 } 192 } 193 194 static void config_merge_config(request_config * const pconf, const config_plugin_value_t *cpv) { 195 do { 196 config_merge_config_cpv(pconf, cpv); 197 } while ((++cpv)->k_id != -1); 198 } 199 200 void config_patch_config(request_st * const r) { 201 config_data_base * const p = r->con->config_data_base; 202 203 /* performed by config_reset_config() */ 204 /*memcpy(&r->conf, &p->defaults, sizeof(request_config));*/ 205 206 for (int i = 1, used = p->nconfig; i < used; ++i) { 207 if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id)) 208 config_merge_config(&r->conf, p->cvlist + p->cvlist[i].v.u2[0]); 209 } 210 } 211 212 void config_reset_config(request_st * const r) { 213 /* initialize request_config (r->conf) from top-level request_config */ 214 config_data_base * const p = r->con->config_data_base; 215 r->server_name = p->defaults.server_name; 216 memcpy(&r->conf, &p->defaults, sizeof(request_config)); 217 } 218 219 static int config_burl_normalize_cond (server *srv) { 220 buffer * const tb = srv->tmp_buf; 221 for (uint32_t i = 0; i < srv->config_context->used; ++i) { 222 data_config * const config =(data_config *)srv->config_context->data[i]; 223 if (COMP_HTTP_QUERY_STRING != config->comp) continue; 224 switch(config->cond) { 225 case CONFIG_COND_NE: 226 case CONFIG_COND_EQ: 227 /* (can use this routine as long as it does not perform 228 * any regex-specific normalization of first arg) */ 229 pcre_keyvalue_burl_normalize_key(&config->string, tb); 230 break; 231 case CONFIG_COND_NOMATCH: 232 case CONFIG_COND_MATCH: 233 pcre_keyvalue_burl_normalize_key(&config->string, tb); 234 if (!data_config_pcre_compile(config)) return 0; 235 break; 236 default: 237 break; 238 } 239 } 240 241 return 1; 242 } 243 244 #if defined(HAVE_MYSQL) || (defined(HAVE_LDAP_H) && defined(HAVE_LBER_H) && defined(HAVE_LIBLDAP) && defined(HAVE_LIBLBER)) 245 static void config_warn_authn_module (server *srv, const char *module, size_t len) { 246 for (uint32_t i = 0; i < srv->config_context->used; ++i) { 247 const data_config *config = (data_config const*)srv->config_context->data[i]; 248 const data_unset *du = array_get_element_klen(config->value, CONST_STR_LEN("auth.backend")); 249 if (NULL != du && du->type == TYPE_STRING) { 250 data_string *ds = (data_string *)du; 251 if (buffer_is_equal_string(&ds->value, module, len)) { 252 buffer * const tb = srv->tmp_buf; 253 buffer_copy_string_len(tb, CONST_STR_LEN("mod_authn_")); 254 buffer_append_string_len(tb, module, len); 255 array_insert_value(srv->srvconf.modules, CONST_BUF_LEN(tb)); 256 log_error(srv->errh, __FILE__, __LINE__, 257 "Warning: please add \"mod_authn_%s\" to server.modules list " 258 "in lighttpd.conf. A future release of lighttpd 1.4.x will " 259 "not automatically load mod_authn_%s and lighttpd will fail " 260 "to start up since your lighttpd.conf uses " 261 "auth.backend = \"%s\".", module, module, module); 262 return; 263 } 264 } 265 } 266 } 267 #endif 268 269 #ifdef USE_OPENSSL_CRYPTO 270 static void config_warn_openssl_module (server *srv) { 271 for (uint32_t i = 0; i < srv->config_context->used; ++i) { 272 const data_config *config = (data_config const*)srv->config_context->data[i]; 273 for (uint32_t j = 0; j < config->value->used; ++j) { 274 data_unset *du = config->value->data[j]; 275 if (0 == strncmp(du->key.ptr, "ssl.", sizeof("ssl.")-1)) { 276 /* mod_openssl should be loaded after mod_extforward */ 277 array_insert_value(srv->srvconf.modules, CONST_STR_LEN("mod_openssl")); 278 log_error(srv->errh, __FILE__, __LINE__, 279 "Warning: please add \"mod_openssl\" to server.modules list " 280 "in lighttpd.conf. A future release of lighttpd 1.4.x " 281 "*will not* automatically load mod_openssl and lighttpd " 282 "*will not* use SSL/TLS where your lighttpd.conf contains " 283 "ssl.* directives"); 284 return; 285 } 286 } 287 } 288 } 289 #endif 290 291 static void config_compat_module_load (server *srv) { 292 int prepend_mod_indexfile = 1; 293 int append_mod_dirlisting = 1; 294 int append_mod_staticfile = 1; 295 int append_mod_authn_file = 1; 296 int append_mod_authn_ldap = 1; 297 int append_mod_authn_mysql = 1; 298 int append_mod_openssl = 1; 299 int contains_mod_auth = 0; 300 301 for (uint32_t i = 0; i < srv->srvconf.modules->used; ++i) { 302 buffer *m = &((data_string *)srv->srvconf.modules->data[i])->value; 303 304 if (buffer_eq_slen(m, CONST_STR_LEN("mod_indexfile"))) 305 prepend_mod_indexfile = 0; 306 else if (buffer_eq_slen(m, CONST_STR_LEN("mod_staticfile"))) 307 append_mod_staticfile = 0; 308 else if (buffer_eq_slen(m, CONST_STR_LEN("mod_dirlisting"))) 309 append_mod_dirlisting = 0; 310 else if (buffer_eq_slen(m, CONST_STR_LEN("mod_gnutls"))) 311 append_mod_openssl = 0; 312 else if (buffer_eq_slen(m, CONST_STR_LEN("mod_mbedtls"))) 313 append_mod_openssl = 0; 314 else if (buffer_eq_slen(m, CONST_STR_LEN("mod_nss"))) 315 append_mod_openssl = 0; 316 else if (buffer_eq_slen(m, CONST_STR_LEN("mod_openssl"))) 317 append_mod_openssl = 0; 318 else if (buffer_eq_slen(m, CONST_STR_LEN("mod_authn_file"))) 319 append_mod_authn_file = 0; 320 else if (buffer_eq_slen(m, CONST_STR_LEN("mod_authn_ldap"))) 321 append_mod_authn_ldap = 0; 322 else if (buffer_eq_slen(m, CONST_STR_LEN("mod_authn_mysql"))) 323 append_mod_authn_mysql = 0; 324 else if (buffer_eq_slen(m, CONST_STR_LEN("mod_auth"))) 325 contains_mod_auth = 1; 326 327 if (0 == prepend_mod_indexfile && 328 0 == append_mod_dirlisting && 329 0 == append_mod_staticfile && 330 0 == append_mod_openssl && 331 0 == append_mod_authn_file && 332 0 == append_mod_authn_ldap && 333 0 == append_mod_authn_mysql && 334 1 == contains_mod_auth) { 335 break; 336 } 337 } 338 339 /* prepend default modules */ 340 341 if (prepend_mod_indexfile) { 342 /* mod_indexfile has to be loaded before mod_fastcgi and friends */ 343 array *modules = array_init(srv->srvconf.modules->used+4); 344 array_insert_value(modules, CONST_STR_LEN("mod_indexfile")); 345 346 for (uint32_t i = 0; i < srv->srvconf.modules->used; ++i) { 347 data_string *ds = (data_string *)srv->srvconf.modules->data[i]; 348 array_insert_value(modules, CONST_BUF_LEN(&ds->value)); 349 } 350 351 array_free(srv->srvconf.modules); 352 srv->srvconf.modules = modules; 353 } 354 355 /* append default modules */ 356 357 if (append_mod_dirlisting) { 358 array_insert_value(srv->srvconf.modules, CONST_STR_LEN("mod_dirlisting")); 359 } 360 361 if (append_mod_staticfile) { 362 array_insert_value(srv->srvconf.modules, CONST_STR_LEN("mod_staticfile")); 363 } 364 365 if (append_mod_openssl) { 366 #ifdef USE_OPENSSL_CRYPTO 367 config_warn_openssl_module(srv); 368 #endif 369 } 370 371 /* mod_auth.c,http_auth.c auth backends were split into separate modules 372 * Automatically load auth backend modules for compatibility with 373 * existing lighttpd 1.4.x configs */ 374 if (contains_mod_auth) { 375 if (append_mod_authn_file) { 376 array_insert_value(srv->srvconf.modules, CONST_STR_LEN("mod_authn_file")); 377 } 378 if (append_mod_authn_ldap) { 379 #if defined(HAVE_LDAP_H) && defined(HAVE_LBER_H) && defined(HAVE_LIBLDAP) && defined(HAVE_LIBLBER) 380 config_warn_authn_module(srv, CONST_STR_LEN("ldap")); 381 #endif 382 } 383 if (append_mod_authn_mysql) { 384 #if defined(HAVE_MYSQL) 385 config_warn_authn_module(srv, CONST_STR_LEN("mysql")); 386 #endif 387 } 388 } 389 } 390 391 static void config_deprecate_module_compress (server *srv) { 392 int mod_compress_idx = -1; 393 int mod_deflate_idx = -1; 394 for (uint32_t i = 0; i < srv->srvconf.modules->used; ++i) { 395 buffer *m = &((data_string *)srv->srvconf.modules->data[i])->value; 396 if (buffer_eq_slen(m, CONST_STR_LEN("mod_compress"))) 397 mod_compress_idx = (int)i; 398 else if (buffer_eq_slen(m, CONST_STR_LEN("mod_deflate"))) 399 mod_deflate_idx = (int)i; 400 } 401 if (mod_compress_idx < 0) return; 402 403 int has_compress_directive = 0; 404 for (uint32_t i = 0; i < srv->config_context->used; ++i) { 405 const data_config *config = 406 (data_config const *)srv->config_context->data[i]; 407 for (uint32_t j = 0; j < config->value->used; ++j) { 408 buffer *k = &config->value->data[j]->key; 409 if (0 == strncmp(k->ptr, "compress.", sizeof("compress.")-1)) { 410 has_compress_directive = 1; 411 break; 412 } 413 } 414 if (has_compress_directive) { 415 log_error(srv->errh, __FILE__, __LINE__, 416 "Warning: \"mod_compress\" is DEPRECATED and has been replaced " 417 "with \"mod_deflate\". A future release of lighttpd 1.4.x will " 418 "not contain mod_compress and lighttpd may fail to start up"); 419 break; 420 } 421 } 422 423 if (mod_deflate_idx >= 0 || !has_compress_directive) { 424 /* create new modules value list without mod_compress */ 425 array *a = array_init(srv->srvconf.modules->used-1); 426 for (uint32_t i = 0; i < srv->srvconf.modules->used; ++i) { 427 buffer *m = &((data_string *)srv->srvconf.modules->data[i])->value; 428 if (buffer_eq_slen(m, CONST_STR_LEN("mod_compress"))) 429 continue; 430 array_insert_value(a, CONST_BUF_LEN(m)); 431 } 432 array_free(srv->srvconf.modules); 433 srv->srvconf.modules = a; 434 } 435 else { 436 /* replace "mod_compress" value with "mod_deflate" value */ 437 buffer *m = &((data_string *)srv->srvconf.modules->data[mod_compress_idx])->value; 438 buffer_copy_string_len(m, CONST_STR_LEN("mod_deflate")); 439 } 440 } 441 442 static int config_http_parseopts (server *srv, const array *a) { 443 unsigned short int opts = srv->srvconf.http_url_normalize; 444 unsigned short int decode_2f = 1; 445 int rc = 1; 446 for (size_t i = 0; i < a->used; ++i) { 447 const data_string * const ds = (const data_string *)a->data[i]; 448 const buffer *k = &ds->key; 449 const buffer *v = &ds->value; 450 unsigned short int opt; 451 int val = 0; 452 if (buffer_eq_slen(v, CONST_STR_LEN("enable"))) 453 val = 1; 454 else if (buffer_eq_slen(v, CONST_STR_LEN("disable"))) 455 val = 0; 456 else { 457 log_error(srv->errh, __FILE__, __LINE__, 458 "unrecognized value for server.http-parseopts: " 459 "%s => %s (expect \"[enable|disable]\")", k->ptr, v->ptr); 460 rc = 0; 461 } 462 if (buffer_eq_slen(k, CONST_STR_LEN("url-normalize"))) 463 opt = HTTP_PARSEOPT_URL_NORMALIZE; 464 else if (buffer_eq_slen(k, CONST_STR_LEN("url-normalize-unreserved"))) 465 opt = HTTP_PARSEOPT_URL_NORMALIZE_UNRESERVED; 466 else if (buffer_eq_slen(k, CONST_STR_LEN("url-normalize-required"))) 467 opt = HTTP_PARSEOPT_URL_NORMALIZE_REQUIRED; 468 else if (buffer_eq_slen(k, CONST_STR_LEN("url-ctrls-reject"))) 469 opt = HTTP_PARSEOPT_URL_NORMALIZE_CTRLS_REJECT; 470 else if (buffer_eq_slen(k, CONST_STR_LEN("url-path-backslash-trans"))) 471 opt = HTTP_PARSEOPT_URL_NORMALIZE_PATH_BACKSLASH_TRANS; 472 else if (buffer_eq_slen(k, CONST_STR_LEN("url-path-2f-decode"))) 473 opt = HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_DECODE; 474 else if (buffer_eq_slen(k, CONST_STR_LEN("url-path-2f-reject"))) 475 opt = HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_REJECT; 476 else if (buffer_eq_slen(k, CONST_STR_LEN("url-path-dotseg-remove"))) 477 opt = HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REMOVE; 478 else if (buffer_eq_slen(k, CONST_STR_LEN("url-path-dotseg-reject"))) 479 opt = HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REJECT; 480 else if (buffer_eq_slen(k, CONST_STR_LEN("url-query-20-plus"))) 481 opt = HTTP_PARSEOPT_URL_NORMALIZE_QUERY_20_PLUS; 482 else if (buffer_eq_slen(k, CONST_STR_LEN("header-strict"))) { 483 srv->srvconf.http_header_strict = val; 484 continue; 485 } 486 else if (buffer_eq_slen(k, CONST_STR_LEN("host-strict"))) { 487 srv->srvconf.http_host_strict = val; 488 continue; 489 } 490 else if (buffer_eq_slen(k, CONST_STR_LEN("host-normalize"))) { 491 srv->srvconf.http_host_normalize = val; 492 continue; 493 } 494 else if (buffer_eq_slen(k, CONST_STR_LEN("method-get-body"))) { 495 srv->srvconf.http_method_get_body = val; 496 continue; 497 } 498 else { 499 log_error(srv->errh, __FILE__, __LINE__, 500 "unrecognized key for server.http-parseopts: %s", k->ptr); 501 rc = 0; 502 continue; 503 } 504 if (val) 505 opts |= opt; 506 else { 507 opts &= ~opt; 508 if (opt == HTTP_PARSEOPT_URL_NORMALIZE) { 509 opts = 0; 510 break; 511 } 512 if (opt == HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_DECODE) { 513 decode_2f = 0; 514 } 515 } 516 } 517 if (opts != 0) { 518 opts |= HTTP_PARSEOPT_URL_NORMALIZE; 519 if ((opts & (HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_DECODE 520 |HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_REJECT)) 521 == (HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_DECODE 522 |HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_REJECT)) { 523 log_error(srv->errh, __FILE__, __LINE__, 524 "conflicting options in server.http-parseopts:" 525 "url-path-2f-decode, url-path-2f-reject"); 526 rc = 0; 527 } 528 if ((opts & (HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REMOVE 529 |HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REJECT)) 530 == (HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REMOVE 531 |HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REJECT)) { 532 log_error(srv->errh, __FILE__, __LINE__, 533 "conflicting options in server.http-parseopts:" 534 "url-path-dotseg-remove, url-path-dotseg-reject"); 535 rc = 0; 536 } 537 if (!(opts & (HTTP_PARSEOPT_URL_NORMALIZE_UNRESERVED 538 |HTTP_PARSEOPT_URL_NORMALIZE_REQUIRED))) { 539 opts |= HTTP_PARSEOPT_URL_NORMALIZE_UNRESERVED; 540 if (decode_2f 541 && !(opts & HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_REJECT)) 542 opts |= HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_DECODE; 543 } 544 } 545 srv->srvconf.http_url_normalize = opts; 546 return rc; 547 } 548 549 static int config_insert_srvconf(server *srv) { 550 static const config_plugin_keys_t cpk[] = { 551 { CONST_STR_LEN("server.modules"), 552 T_CONFIG_ARRAY_VLIST, 553 T_CONFIG_SCOPE_SERVER } 554 ,{ CONST_STR_LEN("server.compat-module-load"), 555 T_CONFIG_BOOL, 556 T_CONFIG_SCOPE_SERVER } 557 ,{ CONST_STR_LEN("server.systemd-socket-activation"), 558 T_CONFIG_BOOL, 559 T_CONFIG_SCOPE_SERVER } 560 ,{ CONST_STR_LEN("server.port"), 561 T_CONFIG_SHORT, 562 T_CONFIG_SCOPE_SERVER } 563 ,{ CONST_STR_LEN("server.bind"), 564 T_CONFIG_STRING, 565 T_CONFIG_SCOPE_SERVER } 566 ,{ CONST_STR_LEN("server.network-backend"), 567 T_CONFIG_STRING, 568 T_CONFIG_SCOPE_SERVER } 569 ,{ CONST_STR_LEN("server.chroot"), 570 T_CONFIG_STRING, 571 T_CONFIG_SCOPE_SERVER } 572 ,{ CONST_STR_LEN("server.username"), 573 T_CONFIG_STRING, 574 T_CONFIG_SCOPE_SERVER } 575 ,{ CONST_STR_LEN("server.groupname"), 576 T_CONFIG_STRING, 577 T_CONFIG_SCOPE_SERVER } 578 ,{ CONST_STR_LEN("server.errorlog-placeholder-moved-to-config-insert"), 579 T_CONFIG_STRING, 580 T_CONFIG_SCOPE_SERVER } 581 ,{ CONST_STR_LEN("server.breakagelog-placeholder-moved-to-config-insert"), 582 T_CONFIG_STRING, 583 T_CONFIG_SCOPE_SERVER } 584 ,{ CONST_STR_LEN("server.errorlog-use-syslog"), 585 T_CONFIG_BOOL, 586 T_CONFIG_SCOPE_SERVER } 587 ,{ CONST_STR_LEN("server.syslog-facility"), 588 T_CONFIG_STRING, 589 T_CONFIG_SCOPE_SERVER } 590 ,{ CONST_STR_LEN("server.core-files"), 591 T_CONFIG_BOOL, 592 T_CONFIG_SCOPE_SERVER } 593 ,{ CONST_STR_LEN("server.event-handler"), 594 T_CONFIG_STRING, 595 T_CONFIG_SCOPE_SERVER } 596 ,{ CONST_STR_LEN("server.pid-file"), 597 T_CONFIG_STRING, 598 T_CONFIG_SCOPE_SERVER } 599 ,{ CONST_STR_LEN("server.max-worker"), 600 T_CONFIG_SHORT, 601 T_CONFIG_SCOPE_SERVER } 602 ,{ CONST_STR_LEN("server.max-fds"), 603 T_CONFIG_SHORT, 604 T_CONFIG_SCOPE_SERVER } 605 ,{ CONST_STR_LEN("server.max-connections"), 606 T_CONFIG_SHORT, 607 T_CONFIG_SCOPE_SERVER } 608 ,{ CONST_STR_LEN("server.max-request-field-size"), 609 T_CONFIG_INT, 610 T_CONFIG_SCOPE_SERVER } 611 ,{ CONST_STR_LEN("server.chunkqueue-chunk-sz"), 612 T_CONFIG_INT, 613 T_CONFIG_SCOPE_SERVER } 614 ,{ CONST_STR_LEN("server.upload-temp-file-size"), 615 T_CONFIG_INT, 616 T_CONFIG_SCOPE_SERVER } 617 ,{ CONST_STR_LEN("server.upload-dirs"), 618 T_CONFIG_ARRAY_VLIST, 619 T_CONFIG_SCOPE_SERVER } 620 ,{ CONST_STR_LEN("server.http-parseopts"), 621 T_CONFIG_ARRAY_KVSTRING, 622 T_CONFIG_SCOPE_SERVER } 623 ,{ CONST_STR_LEN("server.http-parseopt-header-strict"), 624 T_CONFIG_BOOL, 625 T_CONFIG_SCOPE_SERVER } 626 ,{ CONST_STR_LEN("server.http-parseopt-host-strict"), 627 T_CONFIG_BOOL, 628 T_CONFIG_SCOPE_SERVER } 629 ,{ CONST_STR_LEN("server.http-parseopt-host-normalize"), 630 T_CONFIG_BOOL, 631 T_CONFIG_SCOPE_SERVER } 632 ,{ CONST_STR_LEN("server.reject-expect-100-with-417"), /*(ignored)*/ 633 T_CONFIG_BOOL, 634 T_CONFIG_SCOPE_SERVER } 635 ,{ CONST_STR_LEN("server.stat-cache-engine"), 636 T_CONFIG_STRING, 637 T_CONFIG_SCOPE_SERVER } 638 ,{ CONST_STR_LEN("mimetype.xattr-name"), 639 T_CONFIG_STRING, 640 T_CONFIG_SCOPE_SERVER } 641 ,{ CONST_STR_LEN("ssl.engine"), 642 T_CONFIG_BOOL, 643 T_CONFIG_SCOPE_CONNECTION } 644 ,{ CONST_STR_LEN("debug.log-request-header-on-error"), 645 T_CONFIG_BOOL, 646 T_CONFIG_SCOPE_SERVER } 647 ,{ CONST_STR_LEN("debug.log-state-handling"), 648 T_CONFIG_BOOL, 649 T_CONFIG_SCOPE_SERVER } 650 ,{ CONST_STR_LEN("server.feature-flags"), 651 T_CONFIG_ARRAY_KVANY, 652 T_CONFIG_SCOPE_SERVER } 653 ,{ NULL, 0, 654 T_CONFIG_UNSET, 655 T_CONFIG_SCOPE_UNSET } 656 }; 657 658 int rc = 0; 659 plugin_data_base srvplug; 660 memset(&srvplug, 0, sizeof(srvplug)); 661 plugin_data_base * const p = &srvplug; 662 if (!config_plugin_values_init(srv, p, cpk, "global")) 663 return HANDLER_ERROR; 664 665 int ssl_enabled = 0; /*(directive checked here only to set default port)*/ 666 667 /* process and validate T_CONFIG_SCOPE_SERVER config directives */ 668 if (p->cvlist[0].v.u2[1]) { 669 config_plugin_value_t *cpv = p->cvlist + p->cvlist[0].v.u2[0]; 670 for (; -1 != cpv->k_id; ++cpv) { 671 switch (cpv->k_id) { 672 case 0: /* server.modules */ 673 array_copy_array(srv->srvconf.modules, cpv->v.a); 674 break; 675 case 1: /* server.compat-module-load */ 676 srv->srvconf.compat_module_load = (unsigned short)cpv->v.u; 677 break; 678 case 2: /* server.systemd-socket-activation */ 679 srv->srvconf.systemd_socket_activation=(unsigned short)cpv->v.u; 680 break; 681 case 3: /* server.port */ 682 srv->srvconf.port = cpv->v.shrt; 683 break; 684 case 4: /* server.bind */ 685 srv->srvconf.bindhost = cpv->v.b; 686 break; 687 case 5: /* server.network-backend */ 688 srv->srvconf.network_backend = cpv->v.b; 689 break; 690 case 6: /* server.chroot */ 691 srv->srvconf.changeroot = cpv->v.b; 692 break; 693 case 7: /* server.username */ 694 srv->srvconf.username = cpv->v.b; 695 break; 696 case 8: /* server.groupname */ 697 srv->srvconf.groupname = cpv->v.b; 698 break; 699 case 9: /* server.errorlog */ /* moved to config_insert() */ 700 /*srv->srvconf.errorlog_file = cpv->v.b;*/ 701 break; 702 case 10:/* server.breakagelog */ /* moved to config_insert() */ 703 /*srv->srvconf.breakagelog_file = cpv->v.b;*/ 704 break; 705 case 11:/* server.errorlog-use-syslog */ 706 srv->srvconf.errorlog_use_syslog = (unsigned short)cpv->v.u; 707 break; 708 case 12:/* server.syslog-facility */ 709 srv->srvconf.syslog_facility = cpv->v.b; 710 break; 711 case 13:/* server.core-files */ 712 srv->srvconf.enable_cores = (unsigned short)cpv->v.u; 713 break; 714 case 14:/* server.event-handler */ 715 srv->srvconf.event_handler = cpv->v.b->ptr; 716 break; 717 case 15:/* server.pid-file */ 718 *(const buffer **)&srv->srvconf.pid_file = cpv->v.b; 719 break; 720 case 16:/* server.max-worker */ 721 srv->srvconf.max_worker = (unsigned short)cpv->v.u; 722 break; 723 case 17:/* server.max-fds */ 724 srv->srvconf.max_fds = (unsigned short)cpv->v.u; 725 break; 726 case 18:/* server.max-connections */ 727 srv->srvconf.max_conns = (unsigned short)cpv->v.u; 728 break; 729 case 19:/* server.max-request-field-size */ 730 srv->srvconf.max_request_field_size = cpv->v.u; 731 break; 732 case 20:/* server.chunkqueue-chunk-sz */ 733 chunkqueue_set_chunk_size(cpv->v.u); 734 break; 735 case 21:/* server.upload-temp-file-size */ 736 srv->srvconf.upload_temp_file_size = cpv->v.u; 737 break; 738 case 22:/* server.upload-dirs */ 739 array_copy_array(srv->srvconf.upload_tempdirs, cpv->v.a); 740 break; 741 case 23:/* server.http-parseopts */ 742 if (!config_http_parseopts(srv, cpv->v.a)) 743 rc = HANDLER_ERROR; 744 break; 745 case 24:/* server.http-parseopt-header-strict */ 746 srv->srvconf.http_header_strict = (0 != cpv->v.u); 747 break; 748 case 25:/* server.http-parseopt-host-strict */ 749 srv->srvconf.http_host_strict = (0 != cpv->v.u); 750 break; 751 case 26:/* server.http-parseopt-host-normalize */ 752 srv->srvconf.http_host_normalize = (0 != cpv->v.u); 753 break; 754 case 27:/* server.reject-expect-100-with-417 *//*(ignored)*/ 755 break; 756 case 28:/* server.stat-cache-engine */ 757 if (0 != stat_cache_choose_engine(cpv->v.b, srv->errh)) 758 rc = HANDLER_ERROR; 759 break; 760 case 29:/* mimetype.xattr-name */ 761 stat_cache_xattrname(cpv->v.b->ptr); 762 break; 763 case 30:/* ssl.engine */ 764 ssl_enabled = (0 != cpv->v.u); 765 #if !defined(USE_OPENSSL_CRYPTO) \ 766 && !defined(USE_MBEDTLS_CRYPTO) \ 767 && !defined(USE_NSS_CRYPTO) \ 768 && !defined(USE_GNUTLS_CRYPTO) \ 769 && !defined(USE_WOLFSSL_CRYPTO) 770 if (ssl_enabled) { 771 log_error(srv->errh, __FILE__, __LINE__, 772 "ssl support is missing; " 773 "recompile with e.g. --with-openssl"); 774 rc = HANDLER_ERROR; 775 break; 776 } 777 #endif 778 break; 779 case 31:/* debug.log-request-header-on-error */ 780 srv->srvconf.log_request_header_on_error = (0 != cpv->v.u); 781 break; 782 case 32:/* debug.log-state-handling */ 783 srv->srvconf.log_state_handling = (0 != cpv->v.u); 784 break; 785 case 33:/* server.feature-flags */ 786 srv->srvconf.feature_flags = cpv->v.a; 787 break; 788 default:/* should not happen */ 789 break; 790 } 791 } 792 } 793 794 if (0 == srv->srvconf.port) 795 srv->srvconf.port = ssl_enabled ? 443 : 80; 796 797 if (srv->srvconf.compat_module_load) 798 config_compat_module_load(srv); 799 800 config_deprecate_module_compress(srv); 801 802 if (srv->srvconf.http_url_normalize && !config_burl_normalize_cond(srv)) 803 rc = HANDLER_ERROR; 804 805 free(srvplug.cvlist); 806 return rc; 807 } 808 809 static int config_insert(server *srv) { 810 static const config_plugin_keys_t cpk[] = { 811 { CONST_STR_LEN("server.document-root"), 812 T_CONFIG_STRING, 813 T_CONFIG_SCOPE_CONNECTION } 814 ,{ CONST_STR_LEN("server.name"), 815 T_CONFIG_STRING, 816 T_CONFIG_SCOPE_CONNECTION } 817 ,{ CONST_STR_LEN("server.tag"), 818 T_CONFIG_STRING, 819 T_CONFIG_SCOPE_CONNECTION } 820 ,{ CONST_STR_LEN("server.max-request-size"), 821 T_CONFIG_INT, 822 T_CONFIG_SCOPE_CONNECTION } 823 ,{ CONST_STR_LEN("server.max-keep-alive-requests"), 824 T_CONFIG_SHORT, 825 T_CONFIG_SCOPE_CONNECTION } 826 ,{ CONST_STR_LEN("server.max-keep-alive-idle"), 827 T_CONFIG_SHORT, 828 T_CONFIG_SCOPE_CONNECTION } 829 ,{ CONST_STR_LEN("server.max-read-idle"), 830 T_CONFIG_SHORT, 831 T_CONFIG_SCOPE_CONNECTION } 832 ,{ CONST_STR_LEN("server.max-write-idle"), 833 T_CONFIG_SHORT, 834 T_CONFIG_SCOPE_CONNECTION } 835 ,{ CONST_STR_LEN("server.errorfile-prefix"), 836 T_CONFIG_STRING, 837 T_CONFIG_SCOPE_CONNECTION } 838 ,{ CONST_STR_LEN("server.error-handler"), 839 T_CONFIG_STRING, 840 T_CONFIG_SCOPE_CONNECTION } 841 ,{ CONST_STR_LEN("server.error-handler-404"), 842 T_CONFIG_STRING, 843 T_CONFIG_SCOPE_CONNECTION } 844 ,{ CONST_STR_LEN("server.error-intercept"), 845 T_CONFIG_BOOL, 846 T_CONFIG_SCOPE_CONNECTION } 847 ,{ CONST_STR_LEN("server.force-lowercase-filenames"), 848 T_CONFIG_BOOL, 849 T_CONFIG_SCOPE_CONNECTION } 850 ,{ CONST_STR_LEN("server.follow-symlink"), 851 T_CONFIG_BOOL, 852 T_CONFIG_SCOPE_CONNECTION } 853 ,{ CONST_STR_LEN("server.protocol-http11"), 854 T_CONFIG_BOOL, 855 T_CONFIG_SCOPE_CONNECTION } 856 ,{ CONST_STR_LEN("server.range-requests"), 857 T_CONFIG_BOOL, 858 T_CONFIG_SCOPE_CONNECTION } 859 ,{ CONST_STR_LEN("server.stream-request-body"), 860 T_CONFIG_SHORT, 861 T_CONFIG_SCOPE_CONNECTION } 862 ,{ CONST_STR_LEN("server.stream-response-body"), 863 T_CONFIG_SHORT, 864 T_CONFIG_SCOPE_CONNECTION } 865 ,{ CONST_STR_LEN("server.kbytes-per-second"), 866 T_CONFIG_SHORT, 867 T_CONFIG_SCOPE_CONNECTION } 868 ,{ CONST_STR_LEN("connection.kbytes-per-second"), 869 T_CONFIG_SHORT, 870 T_CONFIG_SCOPE_CONNECTION } 871 ,{ CONST_STR_LEN("mimetype.assign"), 872 T_CONFIG_ARRAY_KVSTRING, 873 T_CONFIG_SCOPE_CONNECTION } 874 ,{ CONST_STR_LEN("mimetype.use-xattr"), 875 T_CONFIG_BOOL, 876 T_CONFIG_SCOPE_CONNECTION } 877 ,{ CONST_STR_LEN("etag.use-inode"), 878 T_CONFIG_BOOL, 879 T_CONFIG_SCOPE_CONNECTION } 880 ,{ CONST_STR_LEN("etag.use-mtime"), 881 T_CONFIG_BOOL, 882 T_CONFIG_SCOPE_CONNECTION } 883 ,{ CONST_STR_LEN("etag.use-size"), 884 T_CONFIG_BOOL, 885 T_CONFIG_SCOPE_CONNECTION } 886 ,{ CONST_STR_LEN("debug.log-condition-handling"), 887 T_CONFIG_BOOL, 888 T_CONFIG_SCOPE_CONNECTION } 889 ,{ CONST_STR_LEN("debug.log-file-not-found"), 890 T_CONFIG_BOOL, 891 T_CONFIG_SCOPE_CONNECTION } 892 ,{ CONST_STR_LEN("debug.log-request-handling"), 893 T_CONFIG_BOOL, 894 T_CONFIG_SCOPE_CONNECTION } 895 ,{ CONST_STR_LEN("debug.log-request-header"), 896 T_CONFIG_BOOL, 897 T_CONFIG_SCOPE_CONNECTION } 898 ,{ CONST_STR_LEN("debug.log-response-header"), 899 T_CONFIG_BOOL, 900 T_CONFIG_SCOPE_CONNECTION } 901 ,{ CONST_STR_LEN("debug.log-timeouts"), 902 T_CONFIG_BOOL, 903 T_CONFIG_SCOPE_CONNECTION } 904 ,{ CONST_STR_LEN("server.errorlog"), 905 T_CONFIG_STRING, 906 T_CONFIG_SCOPE_CONNECTION } 907 ,{ CONST_STR_LEN("server.breakagelog"), 908 T_CONFIG_STRING, 909 T_CONFIG_SCOPE_CONNECTION } 910 ,{ NULL, 0, 911 T_CONFIG_UNSET, 912 T_CONFIG_SCOPE_UNSET } 913 }; 914 915 int rc = 0; 916 config_data_base * const p = calloc(1, sizeof(config_data_base)); 917 force_assert(p); 918 srv->config_data_base = p; 919 920 if (!config_plugin_values_init(srv, p, cpk, "base")) 921 return HANDLER_ERROR; 922 923 /* process and validate T_CONFIG_SCOPE_CONNECTION config directives 924 * (init i to 0 if global context; to 1 to skip empty global context) */ 925 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 926 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 927 for (; -1 != cpv->k_id; ++cpv) { 928 switch (cpv->k_id) { 929 case 0: /* server.document-root */ 930 case 1: /* server.name */ 931 break; 932 case 2: /* server.tag */ 933 if (!buffer_string_is_empty(cpv->v.b)) { 934 buffer *b; 935 *(const buffer **)&b = cpv->v.b; 936 for (char *t=strchr(b->ptr,'\n'); t; t=strchr(t+2,'\n')) { 937 /* not expecting admin to define multi-line server.tag, 938 * but ensure server_tag has proper header continuation, 939 * if needed */ 940 if (t[1] == ' ' || t[1] == '\t') continue; 941 off_t off = t - b->ptr; 942 size_t len = buffer_string_length(b); 943 buffer_string_prepare_append(b, 1); 944 t = b->ptr+off; 945 memmove(t+2, t+1, len - off - 1); 946 t[1] = ' '; 947 buffer_commit(b, 1); 948 } 949 char *t = b->ptr; /*(make empty if tag is whitespace-only)*/ 950 while (*t==' ' || *t=='\t' || *t=='\r' || *t=='\n') ++t; 951 if (*t == '\0') buffer_string_set_length(b, 0); 952 } 953 break; 954 case 3: /* server.max-request-size */ 955 case 4: /* server.max-keep-alive-requests */ 956 case 5: /* server.max-keep-alive-idle */ 957 case 6: /* server.max-read-idle */ 958 case 7: /* server.max-write-idle */ 959 case 8: /* server.errorfile-prefix */ 960 case 9: /* server.error-handler */ 961 case 10:/* server.error-handler-404 */ 962 case 11:/* server.error-intercept */ 963 break; 964 case 12:/* server.force-lowercase-filenames */ 965 #ifndef HAVE_LSTAT 966 if (0 == cpv->v.u) 967 log_error(srv->errh, __FILE__, __LINE__, 968 "Your system lacks lstat(). " 969 "We can not differ symlinks from files. " 970 "Please remove server.follow-symlinks from your config."); 971 #endif 972 break; 973 case 13:/* server.follow-symlink */ 974 case 14:/* server.protocol-http11 */ 975 case 15:/* server.range-requests */ 976 break; 977 case 16:/* server.stream-request-body */ 978 if (cpv->v.shrt & FDEVENT_STREAM_REQUEST_BUFMIN) 979 cpv->v.shrt |=FDEVENT_STREAM_REQUEST; 980 break; 981 case 17:/* server.stream-response-body */ 982 if (cpv->v.shrt & FDEVENT_STREAM_RESPONSE_BUFMIN) 983 cpv->v.shrt |=FDEVENT_STREAM_RESPONSE; 984 break; 985 case 18:{/*server.kbytes-per-second */ 986 off_t * const cnt = malloc(2*sizeof(off_t)); 987 force_assert(cnt); 988 cnt[0] = 0; 989 cnt[1] = (off_t)cpv->v.shrt << 10; 990 cpv->v.v = cnt; 991 cpv->vtype = T_CONFIG_LOCAL; 992 break; 993 } 994 case 19:/* connection.kbytes-per-second */ 995 case 20:/* mimetype.assign */ 996 case 21:/* mimetype.use-xattr */ 997 case 22:/* etag.use-inode */ 998 case 23:/* etag.use-mtime */ 999 case 24:/* etag.use-size */ 1000 case 25:/* debug.log-condition-handling */ 1001 case 26:/* debug.log-file-not-found */ 1002 case 27:/* debug.log-request-handling */ 1003 case 28:/* debug.log-request-header */ 1004 case 29:/* debug.log-response-header */ 1005 case 30:/* debug.log-timeouts */ 1006 case 31:/* server.errorlog */ /*(idx in server.c must match)*/ 1007 case 32:/* server.breakagelog *//*(idx in server.c must match)*/ 1008 break; 1009 default:/* should not happen */ 1010 break; 1011 } 1012 } 1013 } 1014 1015 p->defaults.errh = srv->errh; 1016 p->defaults.max_keep_alive_requests = 100; 1017 p->defaults.max_keep_alive_idle = 5; 1018 p->defaults.max_read_idle = 60; 1019 p->defaults.max_write_idle = 360; 1020 p->defaults.follow_symlink = 1; 1021 p->defaults.allow_http11 = 1; 1022 p->defaults.etag_flags = ETAG_USE_INODE | ETAG_USE_MTIME | ETAG_USE_SIZE; 1023 p->defaults.range_requests = 1; 1024 /* use 2 to detect later if value is set by user config in global section */ 1025 p->defaults.force_lowercase_filenames = 2; 1026 1027 /*(global, but store in r->conf.http_parseopts)*/ 1028 p->defaults.http_parseopts = 1029 (srv->srvconf.http_header_strict ? HTTP_PARSEOPT_HEADER_STRICT :0) 1030 | (srv->srvconf.http_host_strict ? (HTTP_PARSEOPT_HOST_STRICT 1031 |HTTP_PARSEOPT_HOST_NORMALIZE) :0) 1032 | (srv->srvconf.http_host_normalize ? HTTP_PARSEOPT_HOST_NORMALIZE :0) 1033 | (srv->srvconf.http_method_get_body ? HTTP_PARSEOPT_METHOD_GET_BODY :0); 1034 p->defaults.http_parseopts |= srv->srvconf.http_url_normalize; 1035 p->defaults.mimetypes = &srv->srvconf.empty_array; /*(must not be NULL)*/ 1036 1037 /* initialize p->defaults from global config context */ 1038 if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 1039 const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 1040 if (-1 != cpv->k_id) 1041 config_merge_config(&p->defaults, cpv); 1042 } 1043 1044 /* (after processing config defaults) */ 1045 p->defaults.max_request_field_size = srv->srvconf.max_request_field_size; 1046 p->defaults.log_state_handling = srv->srvconf.log_state_handling; 1047 p->defaults.log_request_header_on_error = 1048 srv->srvconf.log_request_header_on_error; 1049 if (p->defaults.log_request_handling || p->defaults.log_request_header) 1050 p->defaults.log_request_header_on_error = 1; 1051 1052 return rc; 1053 } 1054 1055 int config_finalize(server *srv, const buffer *default_server_tag) { 1056 /* (call after plugins_call_set_defaults()) */ 1057 1058 config_data_base * const p = srv->config_data_base; 1059 1060 /* settings might be enabled during plugins_call_set_defaults() */ 1061 p->defaults.high_precision_timestamps = 1062 srv->srvconf.high_precision_timestamps; 1063 1064 /* configure default server_tag if not set 1065 * (if configured to blank, unset server_tag)*/ 1066 if (buffer_is_empty(p->defaults.server_tag)) 1067 p->defaults.server_tag = default_server_tag; 1068 else if (buffer_string_is_empty(p->defaults.server_tag)) 1069 p->defaults.server_tag = NULL; 1070 1071 /* dump unused config keys */ 1072 for (uint32_t i = 0; i < srv->config_context->used; ++i) { 1073 array *config = ((data_config *)srv->config_context->data[i])->value; 1074 for (uint32_t j = 0; config && j < config->used; ++j) { 1075 const buffer * const k = &config->data[j]->key; 1076 1077 /* all var.* is known as user defined variable */ 1078 if (strncmp(k->ptr, "var.", sizeof("var.") - 1) == 0) 1079 continue; 1080 1081 if (!array_get_element_klen(srv->srvconf.config_touched, 1082 CONST_BUF_LEN(k))) 1083 log_error(srv->errh, __FILE__, __LINE__, 1084 "WARNING: unknown config-key: %s (ignored)", k->ptr); 1085 } 1086 } 1087 1088 array_free(srv->srvconf.config_touched); 1089 srv->srvconf.config_touched = NULL; 1090 1091 if (srv->srvconf.config_unsupported || srv->srvconf.config_deprecated) { 1092 if (srv->srvconf.config_unsupported) 1093 log_error(srv->errh, __FILE__, __LINE__, 1094 "Configuration contains unsupported keys. Going down."); 1095 if (srv->srvconf.config_deprecated) 1096 log_error(srv->errh, __FILE__, __LINE__, 1097 "Configuration contains deprecated keys. Going down."); 1098 return 0; 1099 } 1100 1101 return 1; 1102 } 1103 1104 void config_print(server *srv) { 1105 data_unset *dc = srv->config_context->data[0]; 1106 dc->fn->print(dc, 0); 1107 } 1108 1109 void config_free(server *srv) { 1110 config_free_config(srv->config_data_base); 1111 1112 array_free(srv->config_context); 1113 array_free(srv->srvconf.config_touched); 1114 array_free(srv->srvconf.modules); 1115 buffer_free(srv->srvconf.modules_dir); 1116 array_free(srv->srvconf.upload_tempdirs); 1117 } 1118 1119 void config_init(server *srv) { 1120 srv->config_context = array_init(16); 1121 srv->srvconf.config_touched = array_init(128); 1122 1123 srv->srvconf.port = 0; 1124 srv->srvconf.dont_daemonize = 0; 1125 srv->srvconf.preflight_check = 0; 1126 srv->srvconf.compat_module_load = 1; 1127 srv->srvconf.systemd_socket_activation = 0; 1128 1129 srv->srvconf.high_precision_timestamps = 0; 1130 srv->srvconf.max_request_field_size = 8192; 1131 1132 srv->srvconf.http_header_strict = 1; 1133 srv->srvconf.http_host_strict = 1; /*(implies http_host_normalize)*/ 1134 srv->srvconf.http_host_normalize = 0; 1135 srv->srvconf.http_url_normalize = 1136 HTTP_PARSEOPT_URL_NORMALIZE 1137 | HTTP_PARSEOPT_URL_NORMALIZE_UNRESERVED 1138 | HTTP_PARSEOPT_URL_NORMALIZE_CTRLS_REJECT 1139 | HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_DECODE 1140 | HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REMOVE; 1141 1142 srv->srvconf.modules = array_init(16); 1143 srv->srvconf.modules_dir = buffer_init_string(LIBRARY_DIR); 1144 srv->srvconf.upload_tempdirs = array_init(2); 1145 } 1146 1147 /** 1148 * open the errorlog 1149 * 1150 * we have 4 possibilities: 1151 * - stderr (default) 1152 * - syslog 1153 * - logfile 1154 * - pipe 1155 * 1156 */ 1157 1158 static void config_log_error_open_syslog(server *srv, log_error_st *errh, const buffer *syslog_facility) { 1159 #ifdef HAVE_SYSLOG_H 1160 errh->errorlog_mode = ERRORLOG_SYSLOG; 1161 /* perhaps someone wants to use syslog() */ 1162 int facility = -1; 1163 if (!buffer_string_is_empty(syslog_facility)) { 1164 static const struct facility_name_st { 1165 const char *name; 1166 int val; 1167 } facility_names[] = { 1168 { "auth", LOG_AUTH } 1169 #ifdef LOG_AUTHPRIV 1170 ,{ "authpriv", LOG_AUTHPRIV } 1171 #endif 1172 #ifdef LOG_CRON 1173 ,{ "cron", LOG_CRON } 1174 #endif 1175 ,{ "daemon", LOG_DAEMON } 1176 #ifdef LOG_FTP 1177 ,{ "ftp", LOG_FTP } 1178 #endif 1179 #ifdef LOG_KERN 1180 ,{ "kern", LOG_KERN } 1181 #endif 1182 #ifdef LOG_LPR 1183 ,{ "lpr", LOG_LPR } 1184 #endif 1185 #ifdef LOG_MAIL 1186 ,{ "mail", LOG_MAIL } 1187 #endif 1188 #ifdef LOG_NEWS 1189 ,{ "news", LOG_NEWS } 1190 #endif 1191 ,{ "security", LOG_AUTH } /* DEPRECATED */ 1192 #ifdef LOG_SYSLOG 1193 ,{ "syslog", LOG_SYSLOG } 1194 #endif 1195 #ifdef LOG_USER 1196 ,{ "user", LOG_USER } 1197 #endif 1198 #ifdef LOG_UUCP 1199 ,{ "uucp", LOG_UUCP } 1200 #endif 1201 ,{ "local0", LOG_LOCAL0 } 1202 ,{ "local1", LOG_LOCAL1 } 1203 ,{ "local2", LOG_LOCAL2 } 1204 ,{ "local3", LOG_LOCAL3 } 1205 ,{ "local4", LOG_LOCAL4 } 1206 ,{ "local5", LOG_LOCAL5 } 1207 ,{ "local6", LOG_LOCAL6 } 1208 ,{ "local7", LOG_LOCAL7 } 1209 }; 1210 for (unsigned int i = 0; i < sizeof(facility_names)/sizeof(facility_names[0]); ++i) { 1211 const struct facility_name_st *f = facility_names+i; 1212 if (0 == strcmp(syslog_facility->ptr, f->name)) { 1213 facility = f->val; 1214 break; 1215 } 1216 } 1217 if (-1 == facility) { 1218 log_error(srv->errh, __FILE__, __LINE__, 1219 "unrecognized server.syslog-facility: \"%s\"; " 1220 "defaulting to \"daemon\" facility", 1221 syslog_facility->ptr); 1222 } 1223 } 1224 openlog("lighttpd", LOG_CONS|LOG_PID, -1==facility ? LOG_DAEMON : facility); 1225 #endif 1226 } 1227 1228 static int config_log_error_open_fn(server *srv, log_error_st *errh, const char *fn) { 1229 int fd = fdevent_open_logger(fn); 1230 if (-1 == fd) { 1231 log_perror(srv->errh, __FILE__, __LINE__, 1232 "opening errorlog '%s' failed", fn); 1233 return -1; 1234 } 1235 errh->errorlog_fd = fd; 1236 errh->errorlog_mode = fn[0] == '|' ? ERRORLOG_PIPE : ERRORLOG_FILE; 1237 errh->fn = fn; 1238 1239 return 0; 1240 } 1241 1242 int config_log_error_open(server *srv) { 1243 /* logs are opened after preflight check (srv->srvconf.preflight_check) 1244 * and after dropping privileges instead of being opened during config 1245 * processing */ 1246 #ifdef __clang_analyzer__ 1247 force_assert(srv->errh); 1248 #endif 1249 1250 /* Note: implementation does not de-dup repeated files or pipe commands */ 1251 1252 config_data_base * const p = srv->config_data_base; 1253 log_error_st *serrh = NULL; 1254 1255 /* future: might be slightly faster to have allocated array of open files 1256 * rather than walking config, but only might matter with many directives */ 1257 /* (init i to 0 if global context; to 1 to skip empty global context) */ 1258 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 1259 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 1260 for (; -1 != cpv->k_id; ++cpv) { 1261 const char *fn = NULL; 1262 log_error_st *errh = NULL; 1263 switch (cpv->k_id) { 1264 /* NB: these indexes are repeated below switch() block 1265 * and all must stay in sync with configfile.c */ 1266 case 31:/* server.errorlog */ 1267 if (0 == i) { 1268 if (srv->srvconf.errorlog_use_syslog) continue; 1269 errh = srv->errh; 1270 } 1271 __attribute_fallthrough__ 1272 case 32:/* server.breakagelog */ 1273 if (!buffer_string_is_empty(cpv->v.b)) fn = cpv->v.b->ptr; 1274 break; 1275 default: 1276 break; 1277 } 1278 1279 if (NULL == fn) continue; 1280 1281 if (NULL == errh) errh = log_error_st_init(); 1282 cpv->v.v = errh; 1283 cpv->vtype = T_CONFIG_LOCAL; 1284 1285 if (0 != config_log_error_open_fn(srv, errh, fn)) return -1; 1286 1287 if (0 == i && errh != srv->errh) /*(top-level server.breakagelog)*/ 1288 serrh = errh; 1289 } 1290 } 1291 1292 if (srv->srvconf.errorlog_use_syslog) /*(restricted to global scope)*/ 1293 config_log_error_open_syslog(srv, srv->errh, 1294 srv->srvconf.syslog_facility); 1295 else if (srv->errh->errorlog_mode == ERRORLOG_FD 1296 && !srv->srvconf.dont_daemonize) 1297 srv->errh->errorlog_fd = -1; 1298 /* We can only log to stderr in dont-daemonize mode; 1299 * if we do daemonize and no errorlog file is specified, 1300 * we log into /dev/null 1301 */ 1302 1303 /* Note: serrh should not be stored in p->defaults.serrh 1304 * If left as NULL, scripts (e.g. mod_cgi and mod_ssi exec) will inherit 1305 * the current STDERR_FILENO, which already is the top-level breakagelog. */ 1306 /*p->defaults.serrh = serrh;*/ 1307 1308 int errfd; 1309 if (NULL != serrh) { 1310 if (srv->errh->errorlog_mode == ERRORLOG_FD) { 1311 srv->errh->errorlog_fd = dup(STDERR_FILENO); 1312 fdevent_setfd_cloexec(srv->errh->errorlog_fd); 1313 } 1314 1315 errfd = serrh->errorlog_fd; 1316 if (*serrh->fn == '|') fdevent_breakagelog_logger_pipe(errfd); 1317 } 1318 else if (!srv->srvconf.dont_daemonize) { 1319 /* move STDERR_FILENO to /dev/null */ 1320 if (-1 == (errfd = fdevent_open_devnull())) { 1321 log_perror(srv->errh,__FILE__,__LINE__,"opening /dev/null failed"); 1322 return -1; 1323 } 1324 } 1325 else { 1326 /*(leave STDERR_FILENO as-is)*/ 1327 errfd = -1; 1328 } 1329 1330 if (0 != fdevent_set_stdin_stdout_stderr(-1, -1, errfd)) { 1331 log_perror(srv->errh, __FILE__, __LINE__, "setting stderr failed"); 1332 #ifdef FD_CLOEXEC 1333 if (-1 != errfd && NULL == serrh) close(errfd); 1334 #endif 1335 return -1; 1336 } 1337 #ifdef FD_CLOEXEC 1338 if (-1 != errfd && NULL == serrh) close(errfd); 1339 #endif 1340 1341 if (NULL != serrh) { 1342 close(errfd); /* serrh->errorlog_fd */ 1343 serrh->errorlog_fd = STDERR_FILENO; 1344 } 1345 1346 return 0; 1347 } 1348 1349 /** 1350 * cycle the errorlog 1351 * 1352 */ 1353 1354 void config_log_error_cycle(server *srv) { 1355 /* All logs are rotated in parent and children workers so that all have 1356 * valid filehandles. The parent might reap a child and respawn, so the 1357 * parent needs valid handles for more than top-level srv->errh */ 1358 1359 /*(no need to flush error log before cycling; error logs are not buffered)*/ 1360 1361 config_data_base * const p = srv->config_data_base; 1362 1363 /* future: might be slightly faster to have allocated array of open files 1364 * rather than walking config, but only might matter with many directives */ 1365 /* (init i to 0 if global context; to 1 to skip empty global context) */ 1366 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 1367 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 1368 for (; -1 != cpv->k_id; ++cpv) { 1369 if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; 1370 log_error_st *errh; 1371 switch (cpv->k_id) { 1372 case 31:/* server.errorlog */ 1373 case 32:/* server.breakagelog */ 1374 errh = cpv->v.v; /* cycle only if the error log is a file */ 1375 if (errh->errorlog_mode != ERRORLOG_FILE) continue; 1376 if (-1 == fdevent_cycle_logger(errh->fn, &errh->errorlog_fd)) { 1377 /* write to top-level error log 1378 * (the prior log if srv->errh is the one being cycled) */ 1379 log_perror(srv->errh, __FILE__, __LINE__, 1380 "cycling errorlog '%s' failed", errh->fn); 1381 } 1382 else if (0 == i && errh != srv->errh) { /*(server.breakagelog)*/ 1383 int fd = errh->errorlog_fd; 1384 if (STDERR_FILENO != dup2(fd, STDERR_FILENO)) { 1385 errh->errorlog_fd = STDERR_FILENO; 1386 close(fd); 1387 } 1388 else { 1389 log_perror(srv->errh, __FILE__, __LINE__, 1390 "dup2() %s to STDERR failed", errh->fn); 1391 } 1392 } 1393 break; 1394 default: 1395 break; 1396 } 1397 } 1398 } 1399 } 1400 1401 void config_log_error_close(server *srv) { 1402 config_data_base * const p = srv->config_data_base; 1403 if (NULL == p) return; 1404 1405 /* future: might be slightly faster to have allocated array of open files 1406 * rather than walking config, but only might matter with many directives */ 1407 /* (init i to 0 if global context; to 1 to skip empty global context) */ 1408 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 1409 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 1410 for (; -1 != cpv->k_id; ++cpv) { 1411 if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; 1412 log_error_st *errh = NULL; 1413 switch (cpv->k_id) { 1414 /* NB: these indexes are repeated below switch() block 1415 * and all must stay in sync with configfile.c */ 1416 case 31:/* server.errorlog */ 1417 if (0 == i) continue; /*(srv->errh is free'd later)*/ 1418 __attribute_fallthrough__ 1419 case 32:/* server.breakagelog */ 1420 errh = cpv->v.v; /* cycle only if the error log is a file */ 1421 break; 1422 default: 1423 break; 1424 } 1425 1426 if (NULL == errh) continue; 1427 1428 switch(errh->errorlog_mode) { 1429 case ERRORLOG_PIPE: 1430 case ERRORLOG_FILE: 1431 case ERRORLOG_FD: 1432 if (-1 != errh->errorlog_fd) { 1433 /* don't close STDERR */ 1434 /* fdevent_close_logger_pipes() closes ERRORLOG_PIPE */ 1435 if (STDERR_FILENO != errh->errorlog_fd 1436 && ERRORLOG_PIPE != errh->errorlog_mode) { 1437 close(errh->errorlog_fd); 1438 } 1439 errh->errorlog_fd = -1; 1440 } 1441 break; 1442 case ERRORLOG_SYSLOG: /*(restricted to global scope)*/ 1443 #ifdef HAVE_SYSLOG_H 1444 closelog(); 1445 #endif 1446 break; 1447 } 1448 1449 log_error_st_free(errh); 1450 } 1451 } 1452 1453 fdevent_close_logger_pipes(); 1454 1455 if (srv->errh->errorlog_mode == ERRORLOG_SYSLOG) { 1456 srv->errh->errorlog_mode = ERRORLOG_FD; 1457 srv->errh->errorlog_fd = STDERR_FILENO; 1458 #ifdef HAVE_SYSLOG_H 1459 closelog(); 1460 #endif 1461 } 1462 } 1463 1464 1465 1466 typedef struct { 1467 const char *source; 1468 const char *input; 1469 size_t offset; 1470 size_t size; 1471 1472 int line_pos; 1473 int line; 1474 1475 int in_key; 1476 int in_brace; 1477 int in_cond; 1478 int simulate_eol; 1479 } tokenizer_t; 1480 1481 static int config_skip_newline(tokenizer_t *t) { 1482 int skipped = 1; 1483 force_assert(t->input[t->offset] == '\r' || t->input[t->offset] == '\n'); 1484 if (t->input[t->offset] == '\r' && t->input[t->offset + 1] == '\n') { 1485 skipped ++; 1486 t->offset ++; 1487 } 1488 t->offset ++; 1489 return skipped; 1490 } 1491 1492 static int config_skip_comment(tokenizer_t *t) { 1493 int i; 1494 force_assert(t->input[t->offset] == '#'); 1495 for (i = 1; t->input[t->offset + i] && 1496 (t->input[t->offset + i] != '\n' && t->input[t->offset + i] != '\r'); 1497 i++); 1498 t->offset += i; 1499 return i; 1500 } 1501 1502 __attribute_cold__ 1503 static int config_tokenizer_err(server *srv, const char *file, unsigned int line, tokenizer_t *t, const char *msg) { 1504 log_error(srv->errh, file, line, "source: %s line: %d pos: %d %s", 1505 t->source, t->line, t->line_pos, msg); 1506 return -1; 1507 } 1508 1509 static int config_tokenizer(server *srv, tokenizer_t *t, int *token_id, buffer *token) { 1510 int tid = 0; 1511 size_t i; 1512 1513 if (t->simulate_eol) { 1514 t->simulate_eol = 0; 1515 t->in_key = 1; 1516 tid = TK_EOL; 1517 buffer_copy_string_len(token, CONST_STR_LEN("(EOL)")); 1518 } 1519 1520 while (tid == 0 && t->offset < t->size && t->input[t->offset]) { 1521 char c = t->input[t->offset]; 1522 const char *start = NULL; 1523 1524 switch (c) { 1525 case '=': 1526 if (t->in_brace) { 1527 if (t->input[t->offset + 1] == '>') { 1528 t->offset += 2; 1529 1530 buffer_copy_string_len(token, CONST_STR_LEN("=>")); 1531 1532 tid = TK_ARRAY_ASSIGN; 1533 } else { 1534 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1535 "use => for assignments in arrays"); 1536 } 1537 } else if (t->in_cond) { 1538 if (t->input[t->offset + 1] == '=') { 1539 t->offset += 2; 1540 1541 buffer_copy_string_len(token, CONST_STR_LEN("==")); 1542 1543 tid = TK_EQ; 1544 } else if (t->input[t->offset + 1] == '~') { 1545 t->offset += 2; 1546 1547 buffer_copy_string_len(token, CONST_STR_LEN("=~")); 1548 1549 tid = TK_MATCH; 1550 } else { 1551 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1552 "only =~ and == are allowed in the condition"); 1553 } 1554 t->in_key = 1; 1555 t->in_cond = 0; 1556 } else if (t->in_key) { 1557 tid = TK_ASSIGN; 1558 1559 buffer_copy_string_len(token, t->input + t->offset, 1); 1560 1561 t->offset++; 1562 t->line_pos++; 1563 } else { 1564 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1565 "unexpected equal-sign: ="); 1566 } 1567 1568 break; 1569 case '!': 1570 if (t->in_cond) { 1571 if (t->input[t->offset + 1] == '=') { 1572 t->offset += 2; 1573 1574 buffer_copy_string_len(token, CONST_STR_LEN("!=")); 1575 1576 tid = TK_NE; 1577 } else if (t->input[t->offset + 1] == '~') { 1578 t->offset += 2; 1579 1580 buffer_copy_string_len(token, CONST_STR_LEN("!~")); 1581 1582 tid = TK_NOMATCH; 1583 } else { 1584 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1585 "only !~ and != are allowed in the condition"); 1586 } 1587 t->in_key = 1; 1588 t->in_cond = 0; 1589 } else { 1590 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1591 "unexpected exclamation-marks: !"); 1592 } 1593 1594 break; 1595 case '\t': 1596 case ' ': 1597 t->offset++; 1598 t->line_pos++; 1599 break; 1600 case '\n': 1601 case '\r': 1602 if (t->in_brace == 0) { 1603 int done = 0; 1604 while (!done && t->offset < t->size) { 1605 switch (t->input[t->offset]) { 1606 case '\r': 1607 case '\n': 1608 config_skip_newline(t); 1609 t->line_pos = 1; 1610 t->line++; 1611 break; 1612 1613 case '#': 1614 t->line_pos += config_skip_comment(t); 1615 break; 1616 1617 case '\t': 1618 case ' ': 1619 t->offset++; 1620 t->line_pos++; 1621 break; 1622 1623 default: 1624 done = 1; 1625 } 1626 } 1627 t->in_key = 1; 1628 tid = TK_EOL; 1629 buffer_copy_string_len(token, CONST_STR_LEN("(EOL)")); 1630 } else { 1631 config_skip_newline(t); 1632 t->line_pos = 1; 1633 t->line++; 1634 } 1635 break; 1636 case ',': 1637 if (t->in_brace > 0) { 1638 tid = TK_COMMA; 1639 1640 buffer_copy_string_len(token, CONST_STR_LEN("(COMMA)")); 1641 } 1642 1643 t->offset++; 1644 t->line_pos++; 1645 break; 1646 case '"': 1647 /* search for the terminating " */ 1648 start = t->input + t->offset + 1; 1649 buffer_copy_string_len(token, CONST_STR_LEN("")); 1650 1651 for (i = 1; t->input[t->offset + i]; i++) { 1652 if (t->input[t->offset + i] == '\\' && 1653 t->input[t->offset + i + 1] == '"') { 1654 1655 buffer_append_string_len(token, start, t->input + t->offset + i - start); 1656 1657 start = t->input + t->offset + i + 1; 1658 1659 /* skip the " */ 1660 i++; 1661 continue; 1662 } 1663 1664 1665 if (t->input[t->offset + i] == '"') { 1666 tid = TK_STRING; 1667 1668 buffer_append_string_len(token, start, t->input + t->offset + i - start); 1669 1670 break; 1671 } 1672 } 1673 1674 if (t->input[t->offset + i] == '\0') { 1675 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1676 "missing closing quote"); 1677 } 1678 1679 t->offset += i + 1; 1680 t->line_pos += i + 1; 1681 1682 break; 1683 case '(': 1684 t->offset++; 1685 t->in_brace++; 1686 1687 tid = TK_LPARAN; 1688 1689 buffer_copy_string_len(token, CONST_STR_LEN("(")); 1690 break; 1691 case ')': 1692 t->offset++; 1693 t->in_brace--; 1694 1695 tid = TK_RPARAN; 1696 1697 buffer_copy_string_len(token, CONST_STR_LEN(")")); 1698 break; 1699 case '$': 1700 t->offset++; 1701 1702 tid = TK_DOLLAR; 1703 t->in_cond = 1; 1704 t->in_key = 0; 1705 1706 buffer_copy_string_len(token, CONST_STR_LEN("$")); 1707 1708 break; 1709 1710 case '+': 1711 if (t->input[t->offset + 1] == '=') { 1712 t->offset += 2; 1713 buffer_copy_string_len(token, CONST_STR_LEN("+=")); 1714 tid = TK_APPEND; 1715 } else { 1716 t->offset++; 1717 tid = TK_PLUS; 1718 buffer_copy_string_len(token, CONST_STR_LEN("+")); 1719 } 1720 break; 1721 1722 case ':': 1723 if (t->input[t->offset+1] == '=') { 1724 t->offset += 2; 1725 tid = TK_FORCE_ASSIGN; 1726 buffer_copy_string_len(token, CONST_STR_LEN(":=")); 1727 } else { 1728 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1729 "unexpected character ':'"); 1730 } 1731 break; 1732 1733 case '{': 1734 t->offset++; 1735 1736 tid = TK_LCURLY; 1737 1738 buffer_copy_string_len(token, CONST_STR_LEN("{")); 1739 1740 break; 1741 1742 case '}': 1743 t->offset++; 1744 1745 tid = TK_RCURLY; 1746 1747 buffer_copy_string_len(token, CONST_STR_LEN("}")); 1748 1749 for (; t->offset < t->size; ++t->offset,++t->line_pos) { 1750 c = t->input[t->offset]; 1751 if (c == '\r' || c == '\n') { 1752 break; 1753 } 1754 else if (c == '#') { 1755 t->line_pos += config_skip_comment(t); 1756 break; 1757 } 1758 else if (c != ' ' && c != '\t') { 1759 t->simulate_eol = 1; 1760 break; 1761 } /* else (c == ' ' || c == '\t') */ 1762 } 1763 1764 break; 1765 1766 case '[': 1767 t->offset++; 1768 1769 tid = TK_LBRACKET; 1770 1771 buffer_copy_string_len(token, CONST_STR_LEN("[")); 1772 1773 break; 1774 1775 case ']': 1776 t->offset++; 1777 1778 tid = TK_RBRACKET; 1779 1780 buffer_copy_string_len(token, CONST_STR_LEN("]")); 1781 1782 break; 1783 case '#': 1784 t->line_pos += config_skip_comment(t); 1785 1786 break; 1787 default: 1788 if (t->in_cond) { 1789 for (i = 0; t->input[t->offset + i] && 1790 (isalpha((unsigned char)t->input[t->offset + i]) 1791 || t->input[t->offset + i] == '_'); ++i); 1792 1793 if (i && t->input[t->offset + i]) { 1794 tid = TK_SRVVARNAME; 1795 buffer_copy_string_len(token, t->input + t->offset, i); 1796 1797 t->offset += i; 1798 t->line_pos += i; 1799 } else { 1800 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1801 "invalid character in condition"); 1802 } 1803 } else if (isdigit((unsigned char)c)) { 1804 /* take all digits */ 1805 for (i = 0; t->input[t->offset + i] && isdigit((unsigned char)t->input[t->offset + i]); i++); 1806 1807 /* was there it least a digit ? */ 1808 if (i) { 1809 tid = TK_INTEGER; 1810 1811 buffer_copy_string_len(token, t->input + t->offset, i); 1812 1813 t->offset += i; 1814 t->line_pos += i; 1815 } 1816 } else { 1817 /* the key might consist of [-.0-9a-z] */ 1818 for (i = 0; t->input[t->offset + i] && 1819 (isalnum((unsigned char)t->input[t->offset + i]) || 1820 t->input[t->offset + i] == '.' || 1821 t->input[t->offset + i] == '_' || /* for env.* */ 1822 t->input[t->offset + i] == '-' 1823 ); i++); 1824 1825 if (i && t->input[t->offset + i]) { 1826 buffer_copy_string_len(token, t->input + t->offset, i); 1827 1828 if (strcmp(token->ptr, "include") == 0) { 1829 tid = TK_INCLUDE; 1830 } else if (strcmp(token->ptr, "include_shell") == 0) { 1831 tid = TK_INCLUDE_SHELL; 1832 } else if (strcmp(token->ptr, "global") == 0) { 1833 tid = TK_GLOBAL; 1834 } else if (strcmp(token->ptr, "else") == 0) { 1835 tid = TK_ELSE; 1836 } else { 1837 tid = TK_LKEY; 1838 } 1839 1840 t->offset += i; 1841 t->line_pos += i; 1842 } else { 1843 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1844 "invalid character in variable name"); 1845 } 1846 } 1847 break; 1848 } 1849 } 1850 1851 if (tid) { 1852 *token_id = tid; 1853 return 1; 1854 } else if (t->offset < t->size) { 1855 log_error(srv->errh, __FILE__, __LINE__, "%d, %s", tid, token->ptr); 1856 } 1857 return 0; 1858 } 1859 1860 static int config_parse(server *srv, config_t *context, const char *source, const char *input, size_t isize) { 1861 void *pParser; 1862 buffer *token, *lasttoken; 1863 int token_id = 0; 1864 int ret; 1865 tokenizer_t t; 1866 1867 t.source = source; 1868 t.input = input; 1869 t.size = isize; 1870 t.offset = 0; 1871 t.line = 1; 1872 t.line_pos = 1; 1873 1874 t.in_key = 1; 1875 t.in_brace = 0; 1876 t.in_cond = 0; 1877 t.simulate_eol = 0; 1878 1879 pParser = configparserAlloc( malloc ); 1880 force_assert(pParser); 1881 lasttoken = buffer_init(); 1882 token = buffer_init(); 1883 while((1 == (ret = config_tokenizer(srv, &t, &token_id, token))) && context->ok) { 1884 buffer_copy_buffer(lasttoken, token); 1885 configparser(pParser, token_id, token, context); 1886 1887 token = buffer_init(); 1888 } 1889 buffer_free(token); 1890 1891 if (ret != -1 && context->ok) { 1892 /* add an EOL at EOF, better than say sorry */ 1893 configparser(pParser, TK_EOL, buffer_init_string("(EOL)"), context); 1894 if (context->ok) { 1895 configparser(pParser, 0, NULL, context); 1896 } 1897 } 1898 configparserFree(pParser, free); 1899 1900 if (ret == -1) { 1901 log_error(srv->errh, __FILE__, __LINE__, 1902 "configfile parser failed at: %s", lasttoken->ptr); 1903 } else if (context->ok == 0) { 1904 log_error(srv->errh, __FILE__, __LINE__, "source: %s line: %d pos: %d " 1905 "parser failed somehow near here: %s", 1906 t.source, t.line, t.line_pos, lasttoken->ptr); 1907 ret = -1; 1908 } 1909 buffer_free(lasttoken); 1910 1911 return ret == -1 ? -1 : 0; 1912 } 1913 1914 static int config_parse_file_stream(server *srv, config_t *context, const char *fn) { 1915 stream s; 1916 1917 if (0 != stream_open(&s, fn)) { 1918 log_perror(srv->errh, __FILE__, __LINE__, 1919 "opening configfile %s failed", fn); 1920 return -1; 1921 } 1922 1923 int ret = config_parse(srv, context, fn, s.start, (size_t)s.size); 1924 stream_close(&s); 1925 return ret; 1926 } 1927 1928 int config_parse_file(server *srv, config_t *context, const char *fn) { 1929 buffer *filename; 1930 int ret = -1; 1931 #ifdef GLOB_BRACE 1932 int flags = GLOB_BRACE; 1933 #else 1934 int flags = 0; 1935 #endif 1936 glob_t gl; 1937 1938 if ((fn[0] == '/' || fn[0] == '\\') || 1939 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\')) || 1940 (fn[0] == '.' && fn[1] == '.' && (fn[2] == '/' || fn[2] == '\\'))) { 1941 filename = buffer_init_string(fn); 1942 } else { 1943 filename = buffer_init_buffer(context->basedir); 1944 buffer_append_string(filename, fn); 1945 } 1946 1947 switch (glob(filename->ptr, flags, NULL, &gl)) { 1948 case 0: 1949 for (size_t i = 0; i < gl.gl_pathc; ++i) { 1950 ret = config_parse_file_stream(srv, context, gl.gl_pathv[i]); 1951 if (0 != ret) break; 1952 } 1953 globfree(&gl); 1954 break; 1955 case GLOB_NOMATCH: 1956 if (filename->ptr[strcspn(filename->ptr, "*?[]{}")] != '\0') { /*(contains glob metachars)*/ 1957 ret = 0; /* not an error if no files match glob pattern */ 1958 } 1959 else { 1960 log_error(srv->errh, __FILE__, __LINE__, "include file not found: %s", filename->ptr); 1961 } 1962 break; 1963 case GLOB_ABORTED: 1964 case GLOB_NOSPACE: 1965 log_perror(srv->errh, __FILE__, __LINE__, "glob() %s failed", filename->ptr); 1966 break; 1967 } 1968 1969 buffer_free(filename); 1970 return ret; 1971 } 1972 1973 #ifdef __CYGWIN__ 1974 1975 static char* getCWD(char *buf, size_t sz) { 1976 if (NULL == getcwd(buf, sz)) { 1977 return NULL; 1978 } 1979 for (size_t i = 0; buf[i]; ++i) { 1980 if (buf[i] == '\\') buf[i] = '/'; 1981 } 1982 return buf; 1983 } 1984 1985 #define getcwd(buf, sz) getCWD((buf),(sz)) 1986 1987 #endif /* __CYGWIN__ */ 1988 1989 int config_parse_cmd(server *srv, config_t *context, const char *cmd) { 1990 int ret = 0; 1991 int fds[2]; 1992 char oldpwd[PATH_MAX]; 1993 1994 if (NULL == getcwd(oldpwd, sizeof(oldpwd))) { 1995 log_perror(srv->errh, __FILE__, __LINE__, "getcwd()"); 1996 return -1; 1997 } 1998 1999 if (!buffer_string_is_empty(context->basedir)) { 2000 if (0 != chdir(context->basedir->ptr)) { 2001 log_perror(srv->errh, __FILE__, __LINE__, 2002 "cannot change directory to %s", context->basedir->ptr); 2003 return -1; 2004 } 2005 } 2006 2007 if (pipe(fds)) { 2008 log_perror(srv->errh, __FILE__, __LINE__, "pipe()"); 2009 ret = -1; 2010 } 2011 else { 2012 char *shell = getenv("SHELL"); 2013 char *args[4]; 2014 pid_t pid; 2015 *(const char **)&args[0] = shell ? shell : "/bin/sh"; 2016 *(const char **)&args[1] = "-c"; 2017 *(const char **)&args[2] = cmd; 2018 args[3] = NULL; 2019 2020 fdevent_setfd_cloexec(fds[0]); 2021 pid = fdevent_fork_execve(args[0], args, NULL, -1, fds[1], -1, -1); 2022 if (-1 == pid) { 2023 log_perror(srv->errh, __FILE__, __LINE__, "fork/exec(%s)", cmd); 2024 ret = -1; 2025 } 2026 else { 2027 ssize_t rd; 2028 pid_t wpid; 2029 int wstatus; 2030 buffer *out = buffer_init(); 2031 close(fds[1]); 2032 fds[1] = -1; 2033 do { 2034 rd = read(fds[0], buffer_string_prepare_append(out, 1023), 1023); 2035 if (rd >= 0) buffer_commit(out, (size_t)rd); 2036 } while (rd > 0 || (-1 == rd && errno == EINTR)); 2037 if (0 != rd) { 2038 log_perror(srv->errh, __FILE__, __LINE__, "read \"%s\"", cmd); 2039 ret = -1; 2040 } 2041 close(fds[0]); 2042 fds[0] = -1; 2043 while (-1 == (wpid = waitpid(pid, &wstatus, 0)) && errno == EINTR) ; 2044 if (wpid != pid) { 2045 log_perror(srv->errh, __FILE__, __LINE__, "waitpid \"%s\"",cmd); 2046 ret = -1; 2047 } 2048 if (0 != wstatus) { 2049 log_error(srv->errh, __FILE__, __LINE__, 2050 "command \"%s\" exited non-zero: %d", 2051 cmd, WEXITSTATUS(wstatus)); 2052 ret = -1; 2053 } 2054 2055 if (-1 != ret) { 2056 ret = config_parse(srv, context, cmd, CONST_BUF_LEN(out)); 2057 } 2058 buffer_free(out); 2059 } 2060 if (-1 != fds[0]) close(fds[0]); 2061 if (-1 != fds[1]) close(fds[1]); 2062 } 2063 2064 if (0 != chdir(oldpwd)) { 2065 log_perror(srv->errh, __FILE__, __LINE__, 2066 "cannot change directory to %s", oldpwd); 2067 ret = -1; 2068 } 2069 return ret; 2070 } 2071 2072 static void context_init(server *srv, config_t *context) { 2073 context->srv = srv; 2074 context->ok = 1; 2075 vector_config_weak_init(&context->configs_stack); 2076 context->basedir = buffer_init(); 2077 } 2078 2079 static void context_free(config_t *context) { 2080 vector_config_weak_clear(&context->configs_stack); 2081 buffer_free(context->basedir); 2082 } 2083 2084 int config_read(server *srv, const char *fn) { 2085 config_t context; 2086 data_config *dc; 2087 buffer *dcwd; 2088 int ret; 2089 char *pos; 2090 2091 context_init(srv, &context); 2092 context.all_configs = srv->config_context; 2093 2094 #ifdef __WIN32 2095 pos = strrchr(fn, '\\'); 2096 #else 2097 pos = strrchr(fn, '/'); 2098 #endif 2099 if (pos) { 2100 buffer_copy_string_len(context.basedir, fn, pos - fn + 1); 2101 } 2102 2103 dc = data_config_init(); 2104 buffer_copy_string_len(&dc->key, CONST_STR_LEN("global")); 2105 2106 force_assert(context.all_configs->used == 0); 2107 dc->context_ndx = context.all_configs->used; 2108 array_insert_unique(context.all_configs, (data_unset *)dc); 2109 context.current = dc; 2110 2111 /* default context */ 2112 *array_get_int_ptr(dc->value, CONST_STR_LEN("var.PID")) = getpid(); 2113 2114 dcwd = srv->tmp_buf; 2115 buffer_string_prepare_copy(dcwd, PATH_MAX-1); 2116 if (NULL != getcwd(dcwd->ptr, buffer_string_space(dcwd)+1)) { 2117 buffer_commit(dcwd, strlen(dcwd->ptr)); 2118 array_set_key_value(dc->value, CONST_STR_LEN("var.CWD"), CONST_BUF_LEN(dcwd)); 2119 } 2120 2121 ret = config_parse_file_stream(srv, &context, fn); 2122 2123 /* remains nothing if parser is ok */ 2124 force_assert(!(0 == ret && context.ok && 0 != context.configs_stack.used)); 2125 context_free(&context); 2126 2127 if (0 != ret) { 2128 return ret; 2129 } 2130 2131 if (0 != config_insert_srvconf(srv)) { 2132 return -1; 2133 } 2134 2135 if (0 != config_insert(srv)) { 2136 return -1; 2137 } 2138 2139 return 0; 2140 } 2141 2142 int config_set_defaults(server *srv) { 2143 size_t i; 2144 request_config *s = &((config_data_base *)srv->config_data_base)->defaults; 2145 struct stat st1, st2; 2146 2147 if (fdevent_config(&srv->srvconf.event_handler, srv->errh) <= 0) 2148 return -1; 2149 2150 if (!buffer_string_is_empty(srv->srvconf.changeroot)) { 2151 if (-1 == stat(srv->srvconf.changeroot->ptr, &st1)) { 2152 log_error(srv->errh, __FILE__, __LINE__, 2153 "server.chroot doesn't exist: %s", 2154 srv->srvconf.changeroot->ptr); 2155 return -1; 2156 } 2157 if (!S_ISDIR(st1.st_mode)) { 2158 log_error(srv->errh, __FILE__, __LINE__, 2159 "server.chroot isn't a directory: %s", 2160 srv->srvconf.changeroot->ptr); 2161 return -1; 2162 } 2163 } 2164 2165 if (!srv->srvconf.upload_tempdirs->used) { 2166 const char *tmpdir = getenv("TMPDIR"); 2167 if (NULL == tmpdir) tmpdir = "/var/tmp"; 2168 array_insert_value(srv->srvconf.upload_tempdirs, tmpdir, strlen(tmpdir)); 2169 } 2170 2171 if (srv->srvconf.upload_tempdirs->used) { 2172 buffer * const b = srv->tmp_buf; 2173 size_t len; 2174 buffer_clear(b); 2175 if (!buffer_string_is_empty(srv->srvconf.changeroot)) { 2176 buffer_copy_buffer(b, srv->srvconf.changeroot); 2177 buffer_append_slash(b); 2178 } 2179 len = buffer_string_length(b); 2180 2181 for (i = 0; i < srv->srvconf.upload_tempdirs->used; ++i) { 2182 const data_string * const ds = (data_string *)srv->srvconf.upload_tempdirs->data[i]; 2183 buffer_string_set_length(b, len); /*(truncate)*/ 2184 buffer_append_string_buffer(b, &ds->value); 2185 if (-1 == stat(b->ptr, &st1)) { 2186 log_error(srv->errh, __FILE__, __LINE__, 2187 "server.upload-dirs doesn't exist: %s", b->ptr); 2188 } else if (!S_ISDIR(st1.st_mode)) { 2189 log_error(srv->errh, __FILE__, __LINE__, 2190 "server.upload-dirs isn't a directory: %s", b->ptr); 2191 } 2192 } 2193 } 2194 2195 chunkqueue_set_tempdirs_default( 2196 srv->srvconf.upload_tempdirs, 2197 srv->srvconf.upload_temp_file_size); 2198 2199 if (buffer_string_is_empty(s->document_root)) { 2200 log_error(srv->errh, __FILE__, __LINE__, "document-root is not set"); 2201 return -1; 2202 } 2203 2204 if (2 == s->force_lowercase_filenames) { /* user didn't configure it in global section? */ 2205 s->force_lowercase_filenames = 0; /* default to 0 */ 2206 2207 buffer * const tb = srv->tmp_buf; 2208 buffer_copy_buffer(tb, s->document_root); 2209 2210 buffer_to_lower(tb); 2211 2212 if (0 == stat(tb->ptr, &st1)) { 2213 int is_lower = 0; 2214 2215 is_lower = buffer_is_equal(tb, s->document_root); 2216 2217 /* lower-case existed, check upper-case */ 2218 buffer_copy_buffer(tb, s->document_root); 2219 2220 buffer_to_upper(tb); 2221 2222 /* we have to handle the special case that upper and lower-casing results in the same filename 2223 * as in server.document-root = "/" or "/12345/" */ 2224 2225 if (is_lower && buffer_is_equal(tb, s->document_root)) { 2226 /* lower-casing and upper-casing didn't result in 2227 * an other filename, no need to stat(), 2228 * just assume it is case-sensitive. */ 2229 2230 s->force_lowercase_filenames = 0; 2231 } else if (0 == stat(tb->ptr, &st2)) { 2232 2233 /* upper case exists too, doesn't the FS handle this ? */ 2234 2235 /* upper and lower have the same inode -> case-insensitve FS */ 2236 2237 if (st1.st_ino == st2.st_ino) { 2238 /* upper and lower have the same inode -> case-insensitve FS */ 2239 2240 s->force_lowercase_filenames = 1; 2241 } 2242 } 2243 } 2244 } 2245 2246 return 0; 2247 } 2248