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 ,{ NULL, 0, 651 T_CONFIG_UNSET, 652 T_CONFIG_SCOPE_UNSET } 653 }; 654 655 int rc = 0; 656 plugin_data_base srvplug; 657 memset(&srvplug, 0, sizeof(srvplug)); 658 plugin_data_base * const p = &srvplug; 659 if (!config_plugin_values_init(srv, p, cpk, "global")) 660 return HANDLER_ERROR; 661 662 int ssl_enabled = 0; /*(directive checked here only to set default port)*/ 663 664 /* process and validate T_CONFIG_SCOPE_SERVER config directives */ 665 if (p->cvlist[0].v.u2[1]) { 666 config_plugin_value_t *cpv = p->cvlist + p->cvlist[0].v.u2[0]; 667 for (; -1 != cpv->k_id; ++cpv) { 668 switch (cpv->k_id) { 669 case 0: /* server.modules */ 670 array_copy_array(srv->srvconf.modules, cpv->v.a); 671 break; 672 case 1: /* server.compat-module-load */ 673 srv->srvconf.compat_module_load = (unsigned short)cpv->v.u; 674 break; 675 case 2: /* server.systemd-socket-activation */ 676 srv->srvconf.systemd_socket_activation=(unsigned short)cpv->v.u; 677 break; 678 case 3: /* server.port */ 679 srv->srvconf.port = cpv->v.shrt; 680 break; 681 case 4: /* server.bind */ 682 srv->srvconf.bindhost = cpv->v.b; 683 break; 684 case 5: /* server.network-backend */ 685 srv->srvconf.network_backend = cpv->v.b; 686 break; 687 case 6: /* server.chroot */ 688 srv->srvconf.changeroot = cpv->v.b; 689 break; 690 case 7: /* server.username */ 691 srv->srvconf.username = cpv->v.b; 692 break; 693 case 8: /* server.groupname */ 694 srv->srvconf.groupname = cpv->v.b; 695 break; 696 case 9: /* server.errorlog */ /* moved to config_insert() */ 697 /*srv->srvconf.errorlog_file = cpv->v.b;*/ 698 break; 699 case 10:/* server.breakagelog */ /* moved to config_insert() */ 700 /*srv->srvconf.breakagelog_file = cpv->v.b;*/ 701 break; 702 case 11:/* server.errorlog-use-syslog */ 703 srv->srvconf.errorlog_use_syslog = (unsigned short)cpv->v.u; 704 break; 705 case 12:/* server.syslog-facility */ 706 srv->srvconf.syslog_facility = cpv->v.b; 707 break; 708 case 13:/* server.core-files */ 709 srv->srvconf.enable_cores = (unsigned short)cpv->v.u; 710 break; 711 case 14:/* server.event-handler */ 712 srv->srvconf.event_handler = cpv->v.b->ptr; 713 break; 714 case 15:/* server.pid-file */ 715 *(const buffer **)&srv->srvconf.pid_file = cpv->v.b; 716 break; 717 case 16:/* server.max-worker */ 718 srv->srvconf.max_worker = (unsigned short)cpv->v.u; 719 break; 720 case 17:/* server.max-fds */ 721 srv->srvconf.max_fds = (unsigned short)cpv->v.u; 722 break; 723 case 18:/* server.max-connections */ 724 srv->srvconf.max_conns = (unsigned short)cpv->v.u; 725 break; 726 case 19:/* server.max-request-field-size */ 727 srv->srvconf.max_request_field_size = cpv->v.u; 728 break; 729 case 20:/* server.chunkqueue-chunk-sz */ 730 chunkqueue_set_chunk_size(cpv->v.u); 731 break; 732 case 21:/* server.upload-temp-file-size */ 733 srv->srvconf.upload_temp_file_size = cpv->v.u; 734 break; 735 case 22:/* server.upload-dirs */ 736 array_copy_array(srv->srvconf.upload_tempdirs, cpv->v.a); 737 break; 738 case 23:/* server.http-parseopts */ 739 if (!config_http_parseopts(srv, cpv->v.a)) 740 rc = HANDLER_ERROR; 741 break; 742 case 24:/* server.http-parseopt-header-strict */ 743 srv->srvconf.http_header_strict = (0 != cpv->v.u); 744 break; 745 case 25:/* server.http-parseopt-host-strict */ 746 srv->srvconf.http_host_strict = (0 != cpv->v.u); 747 break; 748 case 26:/* server.http-parseopt-host-normalize */ 749 srv->srvconf.http_host_normalize = (0 != cpv->v.u); 750 break; 751 case 27:/* server.reject-expect-100-with-417 *//*(ignored)*/ 752 break; 753 case 28:/* server.stat-cache-engine */ 754 if (0 != stat_cache_choose_engine(cpv->v.b, srv->errh)) 755 rc = HANDLER_ERROR; 756 break; 757 case 29:/* mimetype.xattr-name */ 758 stat_cache_xattrname(cpv->v.b->ptr); 759 break; 760 case 30:/* ssl.engine */ 761 ssl_enabled = (0 != cpv->v.u); 762 #if !defined(USE_OPENSSL_CRYPTO) \ 763 && !defined(USE_MBEDTLS_CRYPTO) \ 764 && !defined(USE_NSS_CRYPTO) \ 765 && !defined(USE_GNUTLS_CRYPTO) \ 766 && !defined(USE_WOLFSSL_CRYPTO) 767 if (ssl_enabled) { 768 log_error(srv->errh, __FILE__, __LINE__, 769 "ssl support is missing; " 770 "recompile with e.g. --with-openssl"); 771 rc = HANDLER_ERROR; 772 break; 773 } 774 #endif 775 break; 776 case 31:/* debug.log-request-header-on-error */ 777 srv->srvconf.log_request_header_on_error = (0 != cpv->v.u); 778 break; 779 case 32:/* debug.log-state-handling */ 780 srv->srvconf.log_state_handling = (0 != cpv->v.u); 781 break; 782 default:/* should not happen */ 783 break; 784 } 785 } 786 } 787 788 if (0 == srv->srvconf.port) 789 srv->srvconf.port = ssl_enabled ? 443 : 80; 790 791 if (srv->srvconf.compat_module_load) 792 config_compat_module_load(srv); 793 794 config_deprecate_module_compress(srv); 795 796 if (srv->srvconf.http_url_normalize && !config_burl_normalize_cond(srv)) 797 rc = HANDLER_ERROR; 798 799 free(srvplug.cvlist); 800 return rc; 801 } 802 803 static int config_insert(server *srv) { 804 static const config_plugin_keys_t cpk[] = { 805 { CONST_STR_LEN("server.document-root"), 806 T_CONFIG_STRING, 807 T_CONFIG_SCOPE_CONNECTION } 808 ,{ CONST_STR_LEN("server.name"), 809 T_CONFIG_STRING, 810 T_CONFIG_SCOPE_CONNECTION } 811 ,{ CONST_STR_LEN("server.tag"), 812 T_CONFIG_STRING, 813 T_CONFIG_SCOPE_CONNECTION } 814 ,{ CONST_STR_LEN("server.max-request-size"), 815 T_CONFIG_INT, 816 T_CONFIG_SCOPE_CONNECTION } 817 ,{ CONST_STR_LEN("server.max-keep-alive-requests"), 818 T_CONFIG_SHORT, 819 T_CONFIG_SCOPE_CONNECTION } 820 ,{ CONST_STR_LEN("server.max-keep-alive-idle"), 821 T_CONFIG_SHORT, 822 T_CONFIG_SCOPE_CONNECTION } 823 ,{ CONST_STR_LEN("server.max-read-idle"), 824 T_CONFIG_SHORT, 825 T_CONFIG_SCOPE_CONNECTION } 826 ,{ CONST_STR_LEN("server.max-write-idle"), 827 T_CONFIG_SHORT, 828 T_CONFIG_SCOPE_CONNECTION } 829 ,{ CONST_STR_LEN("server.errorfile-prefix"), 830 T_CONFIG_STRING, 831 T_CONFIG_SCOPE_CONNECTION } 832 ,{ CONST_STR_LEN("server.error-handler"), 833 T_CONFIG_STRING, 834 T_CONFIG_SCOPE_CONNECTION } 835 ,{ CONST_STR_LEN("server.error-handler-404"), 836 T_CONFIG_STRING, 837 T_CONFIG_SCOPE_CONNECTION } 838 ,{ CONST_STR_LEN("server.error-intercept"), 839 T_CONFIG_BOOL, 840 T_CONFIG_SCOPE_CONNECTION } 841 ,{ CONST_STR_LEN("server.force-lowercase-filenames"), 842 T_CONFIG_BOOL, 843 T_CONFIG_SCOPE_CONNECTION } 844 ,{ CONST_STR_LEN("server.follow-symlink"), 845 T_CONFIG_BOOL, 846 T_CONFIG_SCOPE_CONNECTION } 847 ,{ CONST_STR_LEN("server.protocol-http11"), 848 T_CONFIG_BOOL, 849 T_CONFIG_SCOPE_CONNECTION } 850 ,{ CONST_STR_LEN("server.range-requests"), 851 T_CONFIG_BOOL, 852 T_CONFIG_SCOPE_CONNECTION } 853 ,{ CONST_STR_LEN("server.stream-request-body"), 854 T_CONFIG_SHORT, 855 T_CONFIG_SCOPE_CONNECTION } 856 ,{ CONST_STR_LEN("server.stream-response-body"), 857 T_CONFIG_SHORT, 858 T_CONFIG_SCOPE_CONNECTION } 859 ,{ CONST_STR_LEN("server.kbytes-per-second"), 860 T_CONFIG_SHORT, 861 T_CONFIG_SCOPE_CONNECTION } 862 ,{ CONST_STR_LEN("connection.kbytes-per-second"), 863 T_CONFIG_SHORT, 864 T_CONFIG_SCOPE_CONNECTION } 865 ,{ CONST_STR_LEN("mimetype.assign"), 866 T_CONFIG_ARRAY_KVSTRING, 867 T_CONFIG_SCOPE_CONNECTION } 868 ,{ CONST_STR_LEN("mimetype.use-xattr"), 869 T_CONFIG_BOOL, 870 T_CONFIG_SCOPE_CONNECTION } 871 ,{ CONST_STR_LEN("etag.use-inode"), 872 T_CONFIG_BOOL, 873 T_CONFIG_SCOPE_CONNECTION } 874 ,{ CONST_STR_LEN("etag.use-mtime"), 875 T_CONFIG_BOOL, 876 T_CONFIG_SCOPE_CONNECTION } 877 ,{ CONST_STR_LEN("etag.use-size"), 878 T_CONFIG_BOOL, 879 T_CONFIG_SCOPE_CONNECTION } 880 ,{ CONST_STR_LEN("debug.log-condition-handling"), 881 T_CONFIG_BOOL, 882 T_CONFIG_SCOPE_CONNECTION } 883 ,{ CONST_STR_LEN("debug.log-file-not-found"), 884 T_CONFIG_BOOL, 885 T_CONFIG_SCOPE_CONNECTION } 886 ,{ CONST_STR_LEN("debug.log-request-handling"), 887 T_CONFIG_BOOL, 888 T_CONFIG_SCOPE_CONNECTION } 889 ,{ CONST_STR_LEN("debug.log-request-header"), 890 T_CONFIG_BOOL, 891 T_CONFIG_SCOPE_CONNECTION } 892 ,{ CONST_STR_LEN("debug.log-response-header"), 893 T_CONFIG_BOOL, 894 T_CONFIG_SCOPE_CONNECTION } 895 ,{ CONST_STR_LEN("debug.log-timeouts"), 896 T_CONFIG_BOOL, 897 T_CONFIG_SCOPE_CONNECTION } 898 ,{ CONST_STR_LEN("server.errorlog"), 899 T_CONFIG_STRING, 900 T_CONFIG_SCOPE_CONNECTION } 901 ,{ CONST_STR_LEN("server.breakagelog"), 902 T_CONFIG_STRING, 903 T_CONFIG_SCOPE_CONNECTION } 904 ,{ NULL, 0, 905 T_CONFIG_UNSET, 906 T_CONFIG_SCOPE_UNSET } 907 }; 908 909 int rc = 0; 910 config_data_base * const p = calloc(1, sizeof(config_data_base)); 911 force_assert(p); 912 srv->config_data_base = p; 913 914 if (!config_plugin_values_init(srv, p, cpk, "base")) 915 return HANDLER_ERROR; 916 917 /* process and validate T_CONFIG_SCOPE_CONNECTION config directives 918 * (init i to 0 if global context; to 1 to skip empty global context) */ 919 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 920 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 921 for (; -1 != cpv->k_id; ++cpv) { 922 switch (cpv->k_id) { 923 case 0: /* server.document-root */ 924 case 1: /* server.name */ 925 break; 926 case 2: /* server.tag */ 927 if (!buffer_string_is_empty(cpv->v.b)) { 928 buffer *b; 929 *(const buffer **)&b = cpv->v.b; 930 for (char *t=strchr(b->ptr,'\n'); t; t=strchr(t+2,'\n')) { 931 /* not expecting admin to define multi-line server.tag, 932 * but ensure server_tag has proper header continuation, 933 * if needed */ 934 if (t[1] == ' ' || t[1] == '\t') continue; 935 off_t off = t - b->ptr; 936 size_t len = buffer_string_length(b); 937 buffer_string_prepare_append(b, 1); 938 t = b->ptr+off; 939 memmove(t+2, t+1, len - off - 1); 940 t[1] = ' '; 941 buffer_commit(b, 1); 942 } 943 char *t = b->ptr; /*(make empty if tag is whitespace-only)*/ 944 while (*t==' ' || *t=='\t' || *t=='\r' || *t=='\n') ++t; 945 if (*t == '\0') buffer_string_set_length(b, 0); 946 } 947 break; 948 case 3: /* server.max-request-size */ 949 case 4: /* server.max-keep-alive-requests */ 950 case 5: /* server.max-keep-alive-idle */ 951 case 6: /* server.max-read-idle */ 952 case 7: /* server.max-write-idle */ 953 case 8: /* server.errorfile-prefix */ 954 case 9: /* server.error-handler */ 955 case 10:/* server.error-handler-404 */ 956 case 11:/* server.error-intercept */ 957 break; 958 case 12:/* server.force-lowercase-filenames */ 959 #ifndef HAVE_LSTAT 960 if (0 == cpv->v.u) 961 log_error(srv->errh, __FILE__, __LINE__, 962 "Your system lacks lstat(). " 963 "We can not differ symlinks from files. " 964 "Please remove server.follow-symlinks from your config."); 965 #endif 966 break; 967 case 13:/* server.follow-symlink */ 968 case 14:/* server.protocol-http11 */ 969 case 15:/* server.range-requests */ 970 break; 971 case 16:/* server.stream-request-body */ 972 if (cpv->v.shrt & FDEVENT_STREAM_REQUEST_BUFMIN) 973 cpv->v.shrt |=FDEVENT_STREAM_REQUEST; 974 break; 975 case 17:/* server.stream-response-body */ 976 if (cpv->v.shrt & FDEVENT_STREAM_RESPONSE_BUFMIN) 977 cpv->v.shrt |=FDEVENT_STREAM_RESPONSE; 978 break; 979 case 18:{/*server.kbytes-per-second */ 980 off_t * const cnt = malloc(2*sizeof(off_t)); 981 force_assert(cnt); 982 cnt[0] = 0; 983 cnt[1] = (off_t)cpv->v.shrt << 10; 984 cpv->v.v = cnt; 985 cpv->vtype = T_CONFIG_LOCAL; 986 break; 987 } 988 case 19:/* connection.kbytes-per-second */ 989 case 20:/* mimetype.assign */ 990 case 21:/* mimetype.use-xattr */ 991 case 22:/* etag.use-inode */ 992 case 23:/* etag.use-mtime */ 993 case 24:/* etag.use-size */ 994 case 25:/* debug.log-condition-handling */ 995 case 26:/* debug.log-file-not-found */ 996 case 27:/* debug.log-request-handling */ 997 case 28:/* debug.log-request-header */ 998 case 29:/* debug.log-response-header */ 999 case 30:/* debug.log-timeouts */ 1000 case 31:/* server.errorlog */ /*(idx in server.c must match)*/ 1001 case 32:/* server.breakagelog *//*(idx in server.c must match)*/ 1002 break; 1003 default:/* should not happen */ 1004 break; 1005 } 1006 } 1007 } 1008 1009 p->defaults.errh = srv->errh; 1010 p->defaults.max_keep_alive_requests = 100; 1011 p->defaults.max_keep_alive_idle = 5; 1012 p->defaults.max_read_idle = 60; 1013 p->defaults.max_write_idle = 360; 1014 p->defaults.follow_symlink = 1; 1015 p->defaults.allow_http11 = 1; 1016 p->defaults.etag_flags = ETAG_USE_INODE | ETAG_USE_MTIME | ETAG_USE_SIZE; 1017 p->defaults.range_requests = 1; 1018 /* use 2 to detect later if value is set by user config in global section */ 1019 p->defaults.force_lowercase_filenames = 2; 1020 1021 /*(global, but store in r->conf.http_parseopts)*/ 1022 p->defaults.http_parseopts = 1023 (srv->srvconf.http_header_strict ? HTTP_PARSEOPT_HEADER_STRICT :0) 1024 | (srv->srvconf.http_host_strict ? (HTTP_PARSEOPT_HOST_STRICT 1025 |HTTP_PARSEOPT_HOST_NORMALIZE) :0) 1026 | (srv->srvconf.http_host_normalize ? HTTP_PARSEOPT_HOST_NORMALIZE :0) 1027 | (srv->srvconf.http_method_get_body ? HTTP_PARSEOPT_METHOD_GET_BODY :0); 1028 p->defaults.http_parseopts |= srv->srvconf.http_url_normalize; 1029 p->defaults.mimetypes = &srv->srvconf.empty_array; /*(must not be NULL)*/ 1030 1031 /* initialize p->defaults from global config context */ 1032 if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 1033 const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 1034 if (-1 != cpv->k_id) 1035 config_merge_config(&p->defaults, cpv); 1036 } 1037 1038 /* (after processing config defaults) */ 1039 p->defaults.max_request_field_size = srv->srvconf.max_request_field_size; 1040 p->defaults.log_state_handling = srv->srvconf.log_state_handling; 1041 p->defaults.log_request_header_on_error = 1042 srv->srvconf.log_request_header_on_error; 1043 if (p->defaults.log_request_handling || p->defaults.log_request_header) 1044 p->defaults.log_request_header_on_error = 1; 1045 1046 return rc; 1047 } 1048 1049 int config_finalize(server *srv, const buffer *default_server_tag) { 1050 /* (call after plugins_call_set_defaults()) */ 1051 1052 config_data_base * const p = srv->config_data_base; 1053 1054 /* settings might be enabled during plugins_call_set_defaults() */ 1055 p->defaults.high_precision_timestamps = 1056 srv->srvconf.high_precision_timestamps; 1057 1058 /* configure default server_tag if not set 1059 * (if configured to blank, unset server_tag)*/ 1060 if (buffer_is_empty(p->defaults.server_tag)) 1061 p->defaults.server_tag = default_server_tag; 1062 else if (buffer_string_is_empty(p->defaults.server_tag)) 1063 p->defaults.server_tag = NULL; 1064 1065 /* dump unused config keys */ 1066 for (uint32_t i = 0; i < srv->config_context->used; ++i) { 1067 array *config = ((data_config *)srv->config_context->data[i])->value; 1068 for (uint32_t j = 0; config && j < config->used; ++j) { 1069 const buffer * const k = &config->data[j]->key; 1070 1071 /* all var.* is known as user defined variable */ 1072 if (strncmp(k->ptr, "var.", sizeof("var.") - 1) == 0) 1073 continue; 1074 1075 if (!array_get_element_klen(srv->srvconf.config_touched, 1076 CONST_BUF_LEN(k))) 1077 log_error(srv->errh, __FILE__, __LINE__, 1078 "WARNING: unknown config-key: %s (ignored)", k->ptr); 1079 } 1080 } 1081 1082 array_free(srv->srvconf.config_touched); 1083 srv->srvconf.config_touched = NULL; 1084 1085 if (srv->srvconf.config_unsupported || srv->srvconf.config_deprecated) { 1086 if (srv->srvconf.config_unsupported) 1087 log_error(srv->errh, __FILE__, __LINE__, 1088 "Configuration contains unsupported keys. Going down."); 1089 if (srv->srvconf.config_deprecated) 1090 log_error(srv->errh, __FILE__, __LINE__, 1091 "Configuration contains deprecated keys. Going down."); 1092 return 0; 1093 } 1094 1095 return 1; 1096 } 1097 1098 void config_print(server *srv) { 1099 data_unset *dc = srv->config_context->data[0]; 1100 dc->fn->print(dc, 0); 1101 } 1102 1103 void config_free(server *srv) { 1104 config_free_config(srv->config_data_base); 1105 1106 array_free(srv->config_context); 1107 array_free(srv->srvconf.config_touched); 1108 array_free(srv->srvconf.modules); 1109 buffer_free(srv->srvconf.modules_dir); 1110 array_free(srv->srvconf.upload_tempdirs); 1111 } 1112 1113 void config_init(server *srv) { 1114 srv->config_context = array_init(16); 1115 srv->srvconf.config_touched = array_init(128); 1116 1117 srv->srvconf.port = 0; 1118 srv->srvconf.dont_daemonize = 0; 1119 srv->srvconf.preflight_check = 0; 1120 srv->srvconf.compat_module_load = 1; 1121 srv->srvconf.systemd_socket_activation = 0; 1122 1123 srv->srvconf.high_precision_timestamps = 0; 1124 srv->srvconf.max_request_field_size = 8192; 1125 1126 srv->srvconf.http_header_strict = 1; 1127 srv->srvconf.http_host_strict = 1; /*(implies http_host_normalize)*/ 1128 srv->srvconf.http_host_normalize = 0; 1129 srv->srvconf.http_url_normalize = 1130 HTTP_PARSEOPT_URL_NORMALIZE 1131 | HTTP_PARSEOPT_URL_NORMALIZE_UNRESERVED 1132 | HTTP_PARSEOPT_URL_NORMALIZE_CTRLS_REJECT 1133 | HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_DECODE 1134 | HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REMOVE; 1135 1136 srv->srvconf.modules = array_init(16); 1137 srv->srvconf.modules_dir = buffer_init_string(LIBRARY_DIR); 1138 srv->srvconf.upload_tempdirs = array_init(2); 1139 } 1140 1141 /** 1142 * open the errorlog 1143 * 1144 * we have 4 possibilities: 1145 * - stderr (default) 1146 * - syslog 1147 * - logfile 1148 * - pipe 1149 * 1150 */ 1151 1152 static void config_log_error_open_syslog(server *srv, log_error_st *errh, const buffer *syslog_facility) { 1153 #ifdef HAVE_SYSLOG_H 1154 errh->errorlog_mode = ERRORLOG_SYSLOG; 1155 /* perhaps someone wants to use syslog() */ 1156 int facility = -1; 1157 if (!buffer_string_is_empty(syslog_facility)) { 1158 static const struct facility_name_st { 1159 const char *name; 1160 int val; 1161 } facility_names[] = { 1162 { "auth", LOG_AUTH } 1163 #ifdef LOG_AUTHPRIV 1164 ,{ "authpriv", LOG_AUTHPRIV } 1165 #endif 1166 #ifdef LOG_CRON 1167 ,{ "cron", LOG_CRON } 1168 #endif 1169 ,{ "daemon", LOG_DAEMON } 1170 #ifdef LOG_FTP 1171 ,{ "ftp", LOG_FTP } 1172 #endif 1173 #ifdef LOG_KERN 1174 ,{ "kern", LOG_KERN } 1175 #endif 1176 #ifdef LOG_LPR 1177 ,{ "lpr", LOG_LPR } 1178 #endif 1179 #ifdef LOG_MAIL 1180 ,{ "mail", LOG_MAIL } 1181 #endif 1182 #ifdef LOG_NEWS 1183 ,{ "news", LOG_NEWS } 1184 #endif 1185 ,{ "security", LOG_AUTH } /* DEPRECATED */ 1186 #ifdef LOG_SYSLOG 1187 ,{ "syslog", LOG_SYSLOG } 1188 #endif 1189 #ifdef LOG_USER 1190 ,{ "user", LOG_USER } 1191 #endif 1192 #ifdef LOG_UUCP 1193 ,{ "uucp", LOG_UUCP } 1194 #endif 1195 ,{ "local0", LOG_LOCAL0 } 1196 ,{ "local1", LOG_LOCAL1 } 1197 ,{ "local2", LOG_LOCAL2 } 1198 ,{ "local3", LOG_LOCAL3 } 1199 ,{ "local4", LOG_LOCAL4 } 1200 ,{ "local5", LOG_LOCAL5 } 1201 ,{ "local6", LOG_LOCAL6 } 1202 ,{ "local7", LOG_LOCAL7 } 1203 }; 1204 for (unsigned int i = 0; i < sizeof(facility_names)/sizeof(facility_names[0]); ++i) { 1205 const struct facility_name_st *f = facility_names+i; 1206 if (0 == strcmp(syslog_facility->ptr, f->name)) { 1207 facility = f->val; 1208 break; 1209 } 1210 } 1211 if (-1 == facility) { 1212 log_error(srv->errh, __FILE__, __LINE__, 1213 "unrecognized server.syslog-facility: \"%s\"; " 1214 "defaulting to \"daemon\" facility", 1215 syslog_facility->ptr); 1216 } 1217 } 1218 openlog("lighttpd", LOG_CONS|LOG_PID, -1==facility ? LOG_DAEMON : facility); 1219 #endif 1220 } 1221 1222 static int config_log_error_open_fn(server *srv, log_error_st *errh, const char *fn) { 1223 int fd = fdevent_open_logger(fn); 1224 if (-1 == fd) { 1225 log_perror(srv->errh, __FILE__, __LINE__, 1226 "opening errorlog '%s' failed", fn); 1227 return -1; 1228 } 1229 errh->errorlog_fd = fd; 1230 errh->errorlog_mode = fn[0] == '|' ? ERRORLOG_PIPE : ERRORLOG_FILE; 1231 errh->fn = fn; 1232 1233 return 0; 1234 } 1235 1236 int config_log_error_open(server *srv) { 1237 /* logs are opened after preflight check (srv->srvconf.preflight_check) 1238 * and after dropping privileges instead of being opened during config 1239 * processing */ 1240 #ifdef __clang_analyzer__ 1241 force_assert(srv->errh); 1242 #endif 1243 1244 /* Note: implementation does not de-dup repeated files or pipe commands */ 1245 1246 config_data_base * const p = srv->config_data_base; 1247 log_error_st *serrh = NULL; 1248 1249 /* future: might be slightly faster to have allocated array of open files 1250 * rather than walking config, but only might matter with many directives */ 1251 /* (init i to 0 if global context; to 1 to skip empty global context) */ 1252 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 1253 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 1254 for (; -1 != cpv->k_id; ++cpv) { 1255 const char *fn = NULL; 1256 log_error_st *errh = NULL; 1257 switch (cpv->k_id) { 1258 /* NB: these indexes are repeated below switch() block 1259 * and all must stay in sync with configfile.c */ 1260 case 31:/* server.errorlog */ 1261 if (0 == i) { 1262 if (srv->srvconf.errorlog_use_syslog) continue; 1263 errh = srv->errh; 1264 } 1265 __attribute_fallthrough__ 1266 case 32:/* server.breakagelog */ 1267 if (!buffer_string_is_empty(cpv->v.b)) fn = cpv->v.b->ptr; 1268 break; 1269 default: 1270 break; 1271 } 1272 1273 if (NULL == fn) continue; 1274 1275 if (NULL == errh) errh = log_error_st_init(); 1276 cpv->v.v = errh; 1277 cpv->vtype = T_CONFIG_LOCAL; 1278 1279 if (0 != config_log_error_open_fn(srv, errh, fn)) return -1; 1280 1281 if (0 == i && errh != srv->errh) /*(top-level server.breakagelog)*/ 1282 serrh = errh; 1283 } 1284 } 1285 1286 if (srv->srvconf.errorlog_use_syslog) /*(restricted to global scope)*/ 1287 config_log_error_open_syslog(srv, srv->errh, 1288 srv->srvconf.syslog_facility); 1289 else if (srv->errh->errorlog_mode == ERRORLOG_FD 1290 && !srv->srvconf.dont_daemonize) 1291 srv->errh->errorlog_fd = -1; 1292 /* We can only log to stderr in dont-daemonize mode; 1293 * if we do daemonize and no errorlog file is specified, 1294 * we log into /dev/null 1295 */ 1296 1297 /* Note: serrh should not be stored in p->defaults.serrh 1298 * If left as NULL, scripts (e.g. mod_cgi and mod_ssi exec) will inherit 1299 * the current STDERR_FILENO, which already is the top-level breakagelog. */ 1300 /*p->defaults.serrh = serrh;*/ 1301 1302 int errfd; 1303 if (NULL != serrh) { 1304 if (srv->errh->errorlog_mode == ERRORLOG_FD) { 1305 srv->errh->errorlog_fd = dup(STDERR_FILENO); 1306 fdevent_setfd_cloexec(srv->errh->errorlog_fd); 1307 } 1308 1309 errfd = serrh->errorlog_fd; 1310 if (*serrh->fn == '|') fdevent_breakagelog_logger_pipe(errfd); 1311 } 1312 else if (!srv->srvconf.dont_daemonize) { 1313 /* move STDERR_FILENO to /dev/null */ 1314 if (-1 == (errfd = fdevent_open_devnull())) { 1315 log_perror(srv->errh,__FILE__,__LINE__,"opening /dev/null failed"); 1316 return -1; 1317 } 1318 } 1319 else { 1320 /*(leave STDERR_FILENO as-is)*/ 1321 errfd = -1; 1322 } 1323 1324 if (0 != fdevent_set_stdin_stdout_stderr(-1, -1, errfd)) { 1325 log_perror(srv->errh, __FILE__, __LINE__, "setting stderr failed"); 1326 #ifdef FD_CLOEXEC 1327 if (-1 != errfd && NULL == serrh) close(errfd); 1328 #endif 1329 return -1; 1330 } 1331 #ifdef FD_CLOEXEC 1332 if (-1 != errfd && NULL == serrh) close(errfd); 1333 #endif 1334 1335 if (NULL != serrh) { 1336 close(errfd); /* serrh->errorlog_fd */ 1337 serrh->errorlog_fd = STDERR_FILENO; 1338 } 1339 1340 return 0; 1341 } 1342 1343 /** 1344 * cycle the errorlog 1345 * 1346 */ 1347 1348 void config_log_error_cycle(server *srv) { 1349 /* All logs are rotated in parent and children workers so that all have 1350 * valid filehandles. The parent might reap a child and respawn, so the 1351 * parent needs valid handles for more than top-level srv->errh */ 1352 1353 /*(no need to flush error log before cycling; error logs are not buffered)*/ 1354 1355 config_data_base * const p = srv->config_data_base; 1356 1357 /* future: might be slightly faster to have allocated array of open files 1358 * rather than walking config, but only might matter with many directives */ 1359 /* (init i to 0 if global context; to 1 to skip empty global context) */ 1360 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 1361 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 1362 for (; -1 != cpv->k_id; ++cpv) { 1363 if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; 1364 log_error_st *errh; 1365 switch (cpv->k_id) { 1366 case 31:/* server.errorlog */ 1367 case 32:/* server.breakagelog */ 1368 errh = cpv->v.v; /* cycle only if the error log is a file */ 1369 if (errh->errorlog_mode != ERRORLOG_FILE) continue; 1370 if (-1 == fdevent_cycle_logger(errh->fn, &errh->errorlog_fd)) { 1371 /* write to top-level error log 1372 * (the prior log if srv->errh is the one being cycled) */ 1373 log_perror(srv->errh, __FILE__, __LINE__, 1374 "cycling errorlog '%s' failed", errh->fn); 1375 } 1376 else if (0 == i && errh != srv->errh) { /*(server.breakagelog)*/ 1377 int fd = errh->errorlog_fd; 1378 if (STDERR_FILENO != dup2(fd, STDERR_FILENO)) { 1379 errh->errorlog_fd = STDERR_FILENO; 1380 close(fd); 1381 } 1382 else { 1383 log_perror(srv->errh, __FILE__, __LINE__, 1384 "dup2() %s to STDERR failed", errh->fn); 1385 } 1386 } 1387 break; 1388 default: 1389 break; 1390 } 1391 } 1392 } 1393 } 1394 1395 void config_log_error_close(server *srv) { 1396 config_data_base * const p = srv->config_data_base; 1397 if (NULL == p) return; 1398 1399 /* future: might be slightly faster to have allocated array of open files 1400 * rather than walking config, but only might matter with many directives */ 1401 /* (init i to 0 if global context; to 1 to skip empty global context) */ 1402 for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 1403 config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 1404 for (; -1 != cpv->k_id; ++cpv) { 1405 if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; 1406 log_error_st *errh = NULL; 1407 switch (cpv->k_id) { 1408 /* NB: these indexes are repeated below switch() block 1409 * and all must stay in sync with configfile.c */ 1410 case 31:/* server.errorlog */ 1411 if (0 == i) continue; /*(srv->errh is free'd later)*/ 1412 __attribute_fallthrough__ 1413 case 32:/* server.breakagelog */ 1414 errh = cpv->v.v; /* cycle only if the error log is a file */ 1415 break; 1416 default: 1417 break; 1418 } 1419 1420 if (NULL == errh) continue; 1421 1422 switch(errh->errorlog_mode) { 1423 case ERRORLOG_PIPE: 1424 case ERRORLOG_FILE: 1425 case ERRORLOG_FD: 1426 if (-1 != errh->errorlog_fd) { 1427 /* don't close STDERR */ 1428 /* fdevent_close_logger_pipes() closes ERRORLOG_PIPE */ 1429 if (STDERR_FILENO != errh->errorlog_fd 1430 && ERRORLOG_PIPE != errh->errorlog_mode) { 1431 close(errh->errorlog_fd); 1432 } 1433 errh->errorlog_fd = -1; 1434 } 1435 break; 1436 case ERRORLOG_SYSLOG: /*(restricted to global scope)*/ 1437 #ifdef HAVE_SYSLOG_H 1438 closelog(); 1439 #endif 1440 break; 1441 } 1442 1443 log_error_st_free(errh); 1444 } 1445 } 1446 1447 fdevent_close_logger_pipes(); 1448 1449 if (srv->errh->errorlog_mode == ERRORLOG_SYSLOG) { 1450 srv->errh->errorlog_mode = ERRORLOG_FD; 1451 srv->errh->errorlog_fd = STDERR_FILENO; 1452 #ifdef HAVE_SYSLOG_H 1453 closelog(); 1454 #endif 1455 } 1456 } 1457 1458 1459 1460 typedef struct { 1461 const char *source; 1462 const char *input; 1463 size_t offset; 1464 size_t size; 1465 1466 int line_pos; 1467 int line; 1468 1469 int in_key; 1470 int in_brace; 1471 int in_cond; 1472 int simulate_eol; 1473 } tokenizer_t; 1474 1475 static int config_skip_newline(tokenizer_t *t) { 1476 int skipped = 1; 1477 force_assert(t->input[t->offset] == '\r' || t->input[t->offset] == '\n'); 1478 if (t->input[t->offset] == '\r' && t->input[t->offset + 1] == '\n') { 1479 skipped ++; 1480 t->offset ++; 1481 } 1482 t->offset ++; 1483 return skipped; 1484 } 1485 1486 static int config_skip_comment(tokenizer_t *t) { 1487 int i; 1488 force_assert(t->input[t->offset] == '#'); 1489 for (i = 1; t->input[t->offset + i] && 1490 (t->input[t->offset + i] != '\n' && t->input[t->offset + i] != '\r'); 1491 i++); 1492 t->offset += i; 1493 return i; 1494 } 1495 1496 __attribute_cold__ 1497 static int config_tokenizer_err(server *srv, const char *file, unsigned int line, tokenizer_t *t, const char *msg) { 1498 log_error(srv->errh, file, line, "source: %s line: %d pos: %d %s", 1499 t->source, t->line, t->line_pos, msg); 1500 return -1; 1501 } 1502 1503 static int config_tokenizer(server *srv, tokenizer_t *t, int *token_id, buffer *token) { 1504 int tid = 0; 1505 size_t i; 1506 1507 if (t->simulate_eol) { 1508 t->simulate_eol = 0; 1509 t->in_key = 1; 1510 tid = TK_EOL; 1511 buffer_copy_string_len(token, CONST_STR_LEN("(EOL)")); 1512 } 1513 1514 while (tid == 0 && t->offset < t->size && t->input[t->offset]) { 1515 char c = t->input[t->offset]; 1516 const char *start = NULL; 1517 1518 switch (c) { 1519 case '=': 1520 if (t->in_brace) { 1521 if (t->input[t->offset + 1] == '>') { 1522 t->offset += 2; 1523 1524 buffer_copy_string_len(token, CONST_STR_LEN("=>")); 1525 1526 tid = TK_ARRAY_ASSIGN; 1527 } else { 1528 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1529 "use => for assignments in arrays"); 1530 } 1531 } else if (t->in_cond) { 1532 if (t->input[t->offset + 1] == '=') { 1533 t->offset += 2; 1534 1535 buffer_copy_string_len(token, CONST_STR_LEN("==")); 1536 1537 tid = TK_EQ; 1538 } else if (t->input[t->offset + 1] == '~') { 1539 t->offset += 2; 1540 1541 buffer_copy_string_len(token, CONST_STR_LEN("=~")); 1542 1543 tid = TK_MATCH; 1544 } else { 1545 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1546 "only =~ and == are allowed in the condition"); 1547 } 1548 t->in_key = 1; 1549 t->in_cond = 0; 1550 } else if (t->in_key) { 1551 tid = TK_ASSIGN; 1552 1553 buffer_copy_string_len(token, t->input + t->offset, 1); 1554 1555 t->offset++; 1556 t->line_pos++; 1557 } else { 1558 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1559 "unexpected equal-sign: ="); 1560 } 1561 1562 break; 1563 case '!': 1564 if (t->in_cond) { 1565 if (t->input[t->offset + 1] == '=') { 1566 t->offset += 2; 1567 1568 buffer_copy_string_len(token, CONST_STR_LEN("!=")); 1569 1570 tid = TK_NE; 1571 } else if (t->input[t->offset + 1] == '~') { 1572 t->offset += 2; 1573 1574 buffer_copy_string_len(token, CONST_STR_LEN("!~")); 1575 1576 tid = TK_NOMATCH; 1577 } else { 1578 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1579 "only !~ and != are allowed in the condition"); 1580 } 1581 t->in_key = 1; 1582 t->in_cond = 0; 1583 } else { 1584 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1585 "unexpected exclamation-marks: !"); 1586 } 1587 1588 break; 1589 case '\t': 1590 case ' ': 1591 t->offset++; 1592 t->line_pos++; 1593 break; 1594 case '\n': 1595 case '\r': 1596 if (t->in_brace == 0) { 1597 int done = 0; 1598 while (!done && t->offset < t->size) { 1599 switch (t->input[t->offset]) { 1600 case '\r': 1601 case '\n': 1602 config_skip_newline(t); 1603 t->line_pos = 1; 1604 t->line++; 1605 break; 1606 1607 case '#': 1608 t->line_pos += config_skip_comment(t); 1609 break; 1610 1611 case '\t': 1612 case ' ': 1613 t->offset++; 1614 t->line_pos++; 1615 break; 1616 1617 default: 1618 done = 1; 1619 } 1620 } 1621 t->in_key = 1; 1622 tid = TK_EOL; 1623 buffer_copy_string_len(token, CONST_STR_LEN("(EOL)")); 1624 } else { 1625 config_skip_newline(t); 1626 t->line_pos = 1; 1627 t->line++; 1628 } 1629 break; 1630 case ',': 1631 if (t->in_brace > 0) { 1632 tid = TK_COMMA; 1633 1634 buffer_copy_string_len(token, CONST_STR_LEN("(COMMA)")); 1635 } 1636 1637 t->offset++; 1638 t->line_pos++; 1639 break; 1640 case '"': 1641 /* search for the terminating " */ 1642 start = t->input + t->offset + 1; 1643 buffer_copy_string_len(token, CONST_STR_LEN("")); 1644 1645 for (i = 1; t->input[t->offset + i]; i++) { 1646 if (t->input[t->offset + i] == '\\' && 1647 t->input[t->offset + i + 1] == '"') { 1648 1649 buffer_append_string_len(token, start, t->input + t->offset + i - start); 1650 1651 start = t->input + t->offset + i + 1; 1652 1653 /* skip the " */ 1654 i++; 1655 continue; 1656 } 1657 1658 1659 if (t->input[t->offset + i] == '"') { 1660 tid = TK_STRING; 1661 1662 buffer_append_string_len(token, start, t->input + t->offset + i - start); 1663 1664 break; 1665 } 1666 } 1667 1668 if (t->input[t->offset + i] == '\0') { 1669 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1670 "missing closing quote"); 1671 } 1672 1673 t->offset += i + 1; 1674 t->line_pos += i + 1; 1675 1676 break; 1677 case '(': 1678 t->offset++; 1679 t->in_brace++; 1680 1681 tid = TK_LPARAN; 1682 1683 buffer_copy_string_len(token, CONST_STR_LEN("(")); 1684 break; 1685 case ')': 1686 t->offset++; 1687 t->in_brace--; 1688 1689 tid = TK_RPARAN; 1690 1691 buffer_copy_string_len(token, CONST_STR_LEN(")")); 1692 break; 1693 case '$': 1694 t->offset++; 1695 1696 tid = TK_DOLLAR; 1697 t->in_cond = 1; 1698 t->in_key = 0; 1699 1700 buffer_copy_string_len(token, CONST_STR_LEN("$")); 1701 1702 break; 1703 1704 case '+': 1705 if (t->input[t->offset + 1] == '=') { 1706 t->offset += 2; 1707 buffer_copy_string_len(token, CONST_STR_LEN("+=")); 1708 tid = TK_APPEND; 1709 } else { 1710 t->offset++; 1711 tid = TK_PLUS; 1712 buffer_copy_string_len(token, CONST_STR_LEN("+")); 1713 } 1714 break; 1715 1716 case ':': 1717 if (t->input[t->offset+1] == '=') { 1718 t->offset += 2; 1719 tid = TK_FORCE_ASSIGN; 1720 buffer_copy_string_len(token, CONST_STR_LEN(":=")); 1721 } else { 1722 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1723 "unexpected character ':'"); 1724 } 1725 break; 1726 1727 case '{': 1728 t->offset++; 1729 1730 tid = TK_LCURLY; 1731 1732 buffer_copy_string_len(token, CONST_STR_LEN("{")); 1733 1734 break; 1735 1736 case '}': 1737 t->offset++; 1738 1739 tid = TK_RCURLY; 1740 1741 buffer_copy_string_len(token, CONST_STR_LEN("}")); 1742 1743 for (; t->offset < t->size; ++t->offset,++t->line_pos) { 1744 c = t->input[t->offset]; 1745 if (c == '\r' || c == '\n') { 1746 break; 1747 } 1748 else if (c == '#') { 1749 t->line_pos += config_skip_comment(t); 1750 break; 1751 } 1752 else if (c != ' ' && c != '\t') { 1753 t->simulate_eol = 1; 1754 break; 1755 } /* else (c == ' ' || c == '\t') */ 1756 } 1757 1758 break; 1759 1760 case '[': 1761 t->offset++; 1762 1763 tid = TK_LBRACKET; 1764 1765 buffer_copy_string_len(token, CONST_STR_LEN("[")); 1766 1767 break; 1768 1769 case ']': 1770 t->offset++; 1771 1772 tid = TK_RBRACKET; 1773 1774 buffer_copy_string_len(token, CONST_STR_LEN("]")); 1775 1776 break; 1777 case '#': 1778 t->line_pos += config_skip_comment(t); 1779 1780 break; 1781 default: 1782 if (t->in_cond) { 1783 for (i = 0; t->input[t->offset + i] && 1784 (isalpha((unsigned char)t->input[t->offset + i]) 1785 || t->input[t->offset + i] == '_'); ++i); 1786 1787 if (i && t->input[t->offset + i]) { 1788 tid = TK_SRVVARNAME; 1789 buffer_copy_string_len(token, t->input + t->offset, i); 1790 1791 t->offset += i; 1792 t->line_pos += i; 1793 } else { 1794 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1795 "invalid character in condition"); 1796 } 1797 } else if (isdigit((unsigned char)c)) { 1798 /* take all digits */ 1799 for (i = 0; t->input[t->offset + i] && isdigit((unsigned char)t->input[t->offset + i]); i++); 1800 1801 /* was there it least a digit ? */ 1802 if (i) { 1803 tid = TK_INTEGER; 1804 1805 buffer_copy_string_len(token, t->input + t->offset, i); 1806 1807 t->offset += i; 1808 t->line_pos += i; 1809 } 1810 } else { 1811 /* the key might consist of [-.0-9a-z] */ 1812 for (i = 0; t->input[t->offset + i] && 1813 (isalnum((unsigned char)t->input[t->offset + i]) || 1814 t->input[t->offset + i] == '.' || 1815 t->input[t->offset + i] == '_' || /* for env.* */ 1816 t->input[t->offset + i] == '-' 1817 ); i++); 1818 1819 if (i && t->input[t->offset + i]) { 1820 buffer_copy_string_len(token, t->input + t->offset, i); 1821 1822 if (strcmp(token->ptr, "include") == 0) { 1823 tid = TK_INCLUDE; 1824 } else if (strcmp(token->ptr, "include_shell") == 0) { 1825 tid = TK_INCLUDE_SHELL; 1826 } else if (strcmp(token->ptr, "global") == 0) { 1827 tid = TK_GLOBAL; 1828 } else if (strcmp(token->ptr, "else") == 0) { 1829 tid = TK_ELSE; 1830 } else { 1831 tid = TK_LKEY; 1832 } 1833 1834 t->offset += i; 1835 t->line_pos += i; 1836 } else { 1837 return config_tokenizer_err(srv, __FILE__, __LINE__, t, 1838 "invalid character in variable name"); 1839 } 1840 } 1841 break; 1842 } 1843 } 1844 1845 if (tid) { 1846 *token_id = tid; 1847 return 1; 1848 } else if (t->offset < t->size) { 1849 log_error(srv->errh, __FILE__, __LINE__, "%d, %s", tid, token->ptr); 1850 } 1851 return 0; 1852 } 1853 1854 static int config_parse(server *srv, config_t *context, const char *source, const char *input, size_t isize) { 1855 void *pParser; 1856 buffer *token, *lasttoken; 1857 int token_id = 0; 1858 int ret; 1859 tokenizer_t t; 1860 1861 t.source = source; 1862 t.input = input; 1863 t.size = isize; 1864 t.offset = 0; 1865 t.line = 1; 1866 t.line_pos = 1; 1867 1868 t.in_key = 1; 1869 t.in_brace = 0; 1870 t.in_cond = 0; 1871 t.simulate_eol = 0; 1872 1873 pParser = configparserAlloc( malloc ); 1874 force_assert(pParser); 1875 lasttoken = buffer_init(); 1876 token = buffer_init(); 1877 while((1 == (ret = config_tokenizer(srv, &t, &token_id, token))) && context->ok) { 1878 buffer_copy_buffer(lasttoken, token); 1879 configparser(pParser, token_id, token, context); 1880 1881 token = buffer_init(); 1882 } 1883 buffer_free(token); 1884 1885 if (ret != -1 && context->ok) { 1886 /* add an EOL at EOF, better than say sorry */ 1887 configparser(pParser, TK_EOL, buffer_init_string("(EOL)"), context); 1888 if (context->ok) { 1889 configparser(pParser, 0, NULL, context); 1890 } 1891 } 1892 configparserFree(pParser, free); 1893 1894 if (ret == -1) { 1895 log_error(srv->errh, __FILE__, __LINE__, 1896 "configfile parser failed at: %s", lasttoken->ptr); 1897 } else if (context->ok == 0) { 1898 log_error(srv->errh, __FILE__, __LINE__, "source: %s line: %d pos: %d " 1899 "parser failed somehow near here: %s", 1900 t.source, t.line, t.line_pos, lasttoken->ptr); 1901 ret = -1; 1902 } 1903 buffer_free(lasttoken); 1904 1905 return ret == -1 ? -1 : 0; 1906 } 1907 1908 static int config_parse_file_stream(server *srv, config_t *context, const char *fn) { 1909 stream s; 1910 1911 if (0 != stream_open(&s, fn)) { 1912 log_perror(srv->errh, __FILE__, __LINE__, 1913 "opening configfile %s failed", fn); 1914 return -1; 1915 } 1916 1917 int ret = config_parse(srv, context, fn, s.start, (size_t)s.size); 1918 stream_close(&s); 1919 return ret; 1920 } 1921 1922 int config_parse_file(server *srv, config_t *context, const char *fn) { 1923 buffer *filename; 1924 int ret = -1; 1925 #ifdef GLOB_BRACE 1926 int flags = GLOB_BRACE; 1927 #else 1928 int flags = 0; 1929 #endif 1930 glob_t gl; 1931 1932 if ((fn[0] == '/' || fn[0] == '\\') || 1933 (fn[0] == '.' && (fn[1] == '/' || fn[1] == '\\')) || 1934 (fn[0] == '.' && fn[1] == '.' && (fn[2] == '/' || fn[2] == '\\'))) { 1935 filename = buffer_init_string(fn); 1936 } else { 1937 filename = buffer_init_buffer(context->basedir); 1938 buffer_append_string(filename, fn); 1939 } 1940 1941 switch (glob(filename->ptr, flags, NULL, &gl)) { 1942 case 0: 1943 for (size_t i = 0; i < gl.gl_pathc; ++i) { 1944 ret = config_parse_file_stream(srv, context, gl.gl_pathv[i]); 1945 if (0 != ret) break; 1946 } 1947 globfree(&gl); 1948 break; 1949 case GLOB_NOMATCH: 1950 if (filename->ptr[strcspn(filename->ptr, "*?[]{}")] != '\0') { /*(contains glob metachars)*/ 1951 ret = 0; /* not an error if no files match glob pattern */ 1952 } 1953 else { 1954 log_error(srv->errh, __FILE__, __LINE__, "include file not found: %s", filename->ptr); 1955 } 1956 break; 1957 case GLOB_ABORTED: 1958 case GLOB_NOSPACE: 1959 log_perror(srv->errh, __FILE__, __LINE__, "glob() %s failed", filename->ptr); 1960 break; 1961 } 1962 1963 buffer_free(filename); 1964 return ret; 1965 } 1966 1967 #ifdef __CYGWIN__ 1968 1969 static char* getCWD(char *buf, size_t sz) { 1970 if (NULL == getcwd(buf, sz)) { 1971 return NULL; 1972 } 1973 for (size_t i = 0; buf[i]; ++i) { 1974 if (buf[i] == '\\') buf[i] = '/'; 1975 } 1976 return buf; 1977 } 1978 1979 #define getcwd(buf, sz) getCWD((buf),(sz)) 1980 1981 #endif /* __CYGWIN__ */ 1982 1983 int config_parse_cmd(server *srv, config_t *context, const char *cmd) { 1984 int ret = 0; 1985 int fds[2]; 1986 char oldpwd[PATH_MAX]; 1987 1988 if (NULL == getcwd(oldpwd, sizeof(oldpwd))) { 1989 log_perror(srv->errh, __FILE__, __LINE__, "getcwd()"); 1990 return -1; 1991 } 1992 1993 if (!buffer_string_is_empty(context->basedir)) { 1994 if (0 != chdir(context->basedir->ptr)) { 1995 log_perror(srv->errh, __FILE__, __LINE__, 1996 "cannot change directory to %s", context->basedir->ptr); 1997 return -1; 1998 } 1999 } 2000 2001 if (pipe(fds)) { 2002 log_perror(srv->errh, __FILE__, __LINE__, "pipe()"); 2003 ret = -1; 2004 } 2005 else { 2006 char *shell = getenv("SHELL"); 2007 char *args[4]; 2008 pid_t pid; 2009 *(const char **)&args[0] = shell ? shell : "/bin/sh"; 2010 *(const char **)&args[1] = "-c"; 2011 *(const char **)&args[2] = cmd; 2012 args[3] = NULL; 2013 2014 fdevent_setfd_cloexec(fds[0]); 2015 pid = fdevent_fork_execve(args[0], args, NULL, -1, fds[1], -1, -1); 2016 if (-1 == pid) { 2017 log_perror(srv->errh, __FILE__, __LINE__, "fork/exec(%s)", cmd); 2018 ret = -1; 2019 } 2020 else { 2021 ssize_t rd; 2022 pid_t wpid; 2023 int wstatus; 2024 buffer *out = buffer_init(); 2025 close(fds[1]); 2026 fds[1] = -1; 2027 do { 2028 rd = read(fds[0], buffer_string_prepare_append(out, 1023), 1023); 2029 if (rd >= 0) buffer_commit(out, (size_t)rd); 2030 } while (rd > 0 || (-1 == rd && errno == EINTR)); 2031 if (0 != rd) { 2032 log_perror(srv->errh, __FILE__, __LINE__, "read \"%s\"", cmd); 2033 ret = -1; 2034 } 2035 close(fds[0]); 2036 fds[0] = -1; 2037 while (-1 == (wpid = waitpid(pid, &wstatus, 0)) && errno == EINTR) ; 2038 if (wpid != pid) { 2039 log_perror(srv->errh, __FILE__, __LINE__, "waitpid \"%s\"",cmd); 2040 ret = -1; 2041 } 2042 if (0 != wstatus) { 2043 log_error(srv->errh, __FILE__, __LINE__, 2044 "command \"%s\" exited non-zero: %d", 2045 cmd, WEXITSTATUS(wstatus)); 2046 ret = -1; 2047 } 2048 2049 if (-1 != ret) { 2050 ret = config_parse(srv, context, cmd, CONST_BUF_LEN(out)); 2051 } 2052 buffer_free(out); 2053 } 2054 if (-1 != fds[0]) close(fds[0]); 2055 if (-1 != fds[1]) close(fds[1]); 2056 } 2057 2058 if (0 != chdir(oldpwd)) { 2059 log_perror(srv->errh, __FILE__, __LINE__, 2060 "cannot change directory to %s", oldpwd); 2061 ret = -1; 2062 } 2063 return ret; 2064 } 2065 2066 static void context_init(server *srv, config_t *context) { 2067 context->srv = srv; 2068 context->ok = 1; 2069 vector_config_weak_init(&context->configs_stack); 2070 context->basedir = buffer_init(); 2071 } 2072 2073 static void context_free(config_t *context) { 2074 vector_config_weak_clear(&context->configs_stack); 2075 buffer_free(context->basedir); 2076 } 2077 2078 int config_read(server *srv, const char *fn) { 2079 config_t context; 2080 data_config *dc; 2081 buffer *dcwd; 2082 int ret; 2083 char *pos; 2084 2085 context_init(srv, &context); 2086 context.all_configs = srv->config_context; 2087 2088 #ifdef __WIN32 2089 pos = strrchr(fn, '\\'); 2090 #else 2091 pos = strrchr(fn, '/'); 2092 #endif 2093 if (pos) { 2094 buffer_copy_string_len(context.basedir, fn, pos - fn + 1); 2095 } 2096 2097 dc = data_config_init(); 2098 buffer_copy_string_len(&dc->key, CONST_STR_LEN("global")); 2099 2100 force_assert(context.all_configs->used == 0); 2101 dc->context_ndx = context.all_configs->used; 2102 array_insert_unique(context.all_configs, (data_unset *)dc); 2103 context.current = dc; 2104 2105 /* default context */ 2106 *array_get_int_ptr(dc->value, CONST_STR_LEN("var.PID")) = getpid(); 2107 2108 dcwd = srv->tmp_buf; 2109 buffer_string_prepare_copy(dcwd, PATH_MAX-1); 2110 if (NULL != getcwd(dcwd->ptr, buffer_string_space(dcwd)+1)) { 2111 buffer_commit(dcwd, strlen(dcwd->ptr)); 2112 array_set_key_value(dc->value, CONST_STR_LEN("var.CWD"), CONST_BUF_LEN(dcwd)); 2113 } 2114 2115 ret = config_parse_file_stream(srv, &context, fn); 2116 2117 /* remains nothing if parser is ok */ 2118 force_assert(!(0 == ret && context.ok && 0 != context.configs_stack.used)); 2119 context_free(&context); 2120 2121 if (0 != ret) { 2122 return ret; 2123 } 2124 2125 if (0 != config_insert_srvconf(srv)) { 2126 return -1; 2127 } 2128 2129 if (0 != config_insert(srv)) { 2130 return -1; 2131 } 2132 2133 return 0; 2134 } 2135 2136 int config_set_defaults(server *srv) { 2137 size_t i; 2138 request_config *s = &((config_data_base *)srv->config_data_base)->defaults; 2139 struct stat st1, st2; 2140 2141 if (fdevent_config(&srv->srvconf.event_handler, srv->errh) <= 0) 2142 return -1; 2143 2144 if (!buffer_string_is_empty(srv->srvconf.changeroot)) { 2145 if (-1 == stat(srv->srvconf.changeroot->ptr, &st1)) { 2146 log_error(srv->errh, __FILE__, __LINE__, 2147 "server.chroot doesn't exist: %s", 2148 srv->srvconf.changeroot->ptr); 2149 return -1; 2150 } 2151 if (!S_ISDIR(st1.st_mode)) { 2152 log_error(srv->errh, __FILE__, __LINE__, 2153 "server.chroot isn't a directory: %s", 2154 srv->srvconf.changeroot->ptr); 2155 return -1; 2156 } 2157 } 2158 2159 if (!srv->srvconf.upload_tempdirs->used) { 2160 const char *tmpdir = getenv("TMPDIR"); 2161 if (NULL == tmpdir) tmpdir = "/var/tmp"; 2162 array_insert_value(srv->srvconf.upload_tempdirs, tmpdir, strlen(tmpdir)); 2163 } 2164 2165 if (srv->srvconf.upload_tempdirs->used) { 2166 buffer * const b = srv->tmp_buf; 2167 size_t len; 2168 buffer_clear(b); 2169 if (!buffer_string_is_empty(srv->srvconf.changeroot)) { 2170 buffer_copy_buffer(b, srv->srvconf.changeroot); 2171 buffer_append_slash(b); 2172 } 2173 len = buffer_string_length(b); 2174 2175 for (i = 0; i < srv->srvconf.upload_tempdirs->used; ++i) { 2176 const data_string * const ds = (data_string *)srv->srvconf.upload_tempdirs->data[i]; 2177 buffer_string_set_length(b, len); /*(truncate)*/ 2178 buffer_append_string_buffer(b, &ds->value); 2179 if (-1 == stat(b->ptr, &st1)) { 2180 log_error(srv->errh, __FILE__, __LINE__, 2181 "server.upload-dirs doesn't exist: %s", b->ptr); 2182 } else if (!S_ISDIR(st1.st_mode)) { 2183 log_error(srv->errh, __FILE__, __LINE__, 2184 "server.upload-dirs isn't a directory: %s", b->ptr); 2185 } 2186 } 2187 } 2188 2189 chunkqueue_set_tempdirs_default( 2190 srv->srvconf.upload_tempdirs, 2191 srv->srvconf.upload_temp_file_size); 2192 2193 if (buffer_string_is_empty(s->document_root)) { 2194 log_error(srv->errh, __FILE__, __LINE__, "document-root is not set"); 2195 return -1; 2196 } 2197 2198 if (2 == s->force_lowercase_filenames) { /* user didn't configure it in global section? */ 2199 s->force_lowercase_filenames = 0; /* default to 0 */ 2200 2201 buffer * const tb = srv->tmp_buf; 2202 buffer_copy_buffer(tb, s->document_root); 2203 2204 buffer_to_lower(tb); 2205 2206 if (0 == stat(tb->ptr, &st1)) { 2207 int is_lower = 0; 2208 2209 is_lower = buffer_is_equal(tb, s->document_root); 2210 2211 /* lower-case existed, check upper-case */ 2212 buffer_copy_buffer(tb, s->document_root); 2213 2214 buffer_to_upper(tb); 2215 2216 /* we have to handle the special case that upper and lower-casing results in the same filename 2217 * as in server.document-root = "/" or "/12345/" */ 2218 2219 if (is_lower && buffer_is_equal(tb, s->document_root)) { 2220 /* lower-casing and upper-casing didn't result in 2221 * an other filename, no need to stat(), 2222 * just assume it is case-sensitive. */ 2223 2224 s->force_lowercase_filenames = 0; 2225 } else if (0 == stat(tb->ptr, &st2)) { 2226 2227 /* upper case exists too, doesn't the FS handle this ? */ 2228 2229 /* upper and lower have the same inode -> case-insensitve FS */ 2230 2231 if (st1.st_ino == st2.st_ino) { 2232 /* upper and lower have the same inode -> case-insensitve FS */ 2233 2234 s->force_lowercase_filenames = 1; 2235 } 2236 } 2237 } 2238 } 2239 2240 return 0; 2241 } 2242