1 /* $NetBSD: bozohttpd.c,v 1.19 2010/05/15 06:48:27 mrg Exp $ */ 2 3 /* $eterna: bozohttpd.c,v 1.169 2010/05/13 04:17:58 mrg Exp $ */ 4 5 /* 6 * Copyright (c) 1997-2010 Matthew R. Green 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer and 16 * dedication in the documentation and/or other materials provided 17 * with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 */ 32 33 /* this program is dedicated to the Great God of Processed Cheese */ 34 35 /* 36 * bozohttpd.c: minimal httpd; provides only these features: 37 * - HTTP/0.9 (by virtue of ..) 38 * - HTTP/1.0 39 * - HTTP/1.1 40 * - CGI/1.1 this will only be provided for "system" scripts 41 * - automatic "missing trailing slash" redirections 42 * - configurable translation of /~user/ to ~user/public_html, 43 * however, this does not include cgi-bin support 44 * - access lists via libwrap via inetd/tcpd 45 * - virtual hosting 46 * - not that we do not even pretend to understand MIME, but 47 * rely only on the HTTP specification 48 * - ipv6 support 49 * - automatic `index.html' generation 50 * - configurable server name 51 * - directory index generation 52 * - daemon mode (lacks libwrap support) 53 * - .htpasswd support 54 */ 55 56 /* 57 * requirements for minimal http/1.1 (at least, as documented in 58 * <draft-ietf-http-v11-spec-rev-06> which expired may 18, 1999): 59 * 60 * - 14.15: content-encoding handling. [1] 61 * 62 * - 14.16: content-length handling. this is only a SHOULD header 63 * thus we could just not send it ever. [1] 64 * 65 * - 14.17: content-type handling. [1] 66 * 67 * - 14.25/28: if-{,un}modified-since handling. maybe do this, but 68 * i really don't want to have to parse 3 differnet date formats 69 * 70 * [1] need to revisit to ensure proper behaviour 71 * 72 * and the following is a list of features that we do not need 73 * to have due to other limits, or are too lazy. there are more 74 * of these than are listed, but these are of particular note, 75 * and could perhaps be implemented. 76 * 77 * - 3.5/3.6: content/transfer codings. probably can ignore 78 * this? we "SHOULD"n't. but 4.4 says we should ignore a 79 * `content-length' header upon reciept of a `transfer-encoding' 80 * header. 81 * 82 * - 5.1.1: request methods. only MUST support GET and HEAD, 83 * but there are new ones besides POST that are currently 84 * supported: OPTIONS PUT DELETE TRACE and CONNECT, plus 85 * extensions not yet known? 86 * 87 * - 10.1: we can ignore informational status codes 88 * 89 * - 10.3.3/10.3.4/10.3.8: just use '302' codes always. 90 * 91 * - 14.1/14.2/14.3/14.27: we do not support Accept: headers.. 92 * just ignore them and send the request anyway. they are 93 * only SHOULD. 94 * 95 * - 14.5/14.16/14.35: we don't do ranges. from section 14.35.2 96 * `A server MAY ignore the Range header'. but it might be nice. 97 * since 20080301 we support simple range headers. 98 * 99 * - 14.9: we aren't a cache. 100 * 101 * - 14.15: content-md5 would be nice... 102 * 103 * - 14.24/14.26/14.27: be nice to support this... 104 * 105 * - 14.44: not sure about this Vary: header. ignore it for now. 106 */ 107 108 #ifndef INDEX_HTML 109 #define INDEX_HTML "index.html" 110 #endif 111 #ifndef SERVER_SOFTWARE 112 #define SERVER_SOFTWARE "bozohttpd/20100512" 113 #endif 114 #ifndef DIRECT_ACCESS_FILE 115 #define DIRECT_ACCESS_FILE ".bzdirect" 116 #endif 117 #ifndef REDIRECT_FILE 118 #define REDIRECT_FILE ".bzredirect" 119 #endif 120 #ifndef ABSREDIRECT_FILE 121 #define ABSREDIRECT_FILE ".bzabsredirect" 122 #endif 123 #ifndef PUBLIC_HTML 124 #define PUBLIC_HTML "public_html" 125 #endif 126 127 #ifndef USE_ARG 128 #define USE_ARG(x) /*LINTED*/(void)&(x) 129 #endif 130 131 /* 132 * And so it begins .. 133 */ 134 135 #include <sys/param.h> 136 #include <sys/socket.h> 137 #include <sys/time.h> 138 #include <sys/mman.h> 139 140 #include <arpa/inet.h> 141 142 #include <ctype.h> 143 #include <dirent.h> 144 #include <errno.h> 145 #include <fcntl.h> 146 #include <netdb.h> 147 #include <pwd.h> 148 #include <grp.h> 149 #include <signal.h> 150 #include <stdarg.h> 151 #include <stdlib.h> 152 #include <string.h> 153 #include <syslog.h> 154 #include <time.h> 155 #include <unistd.h> 156 157 #ifndef __attribute__ 158 #define __attribute__(x) 159 #endif /* __attribute__ */ 160 161 #include "bozohttpd.h" 162 163 #ifndef MAX_WAIT_TIME 164 #define MAX_WAIT_TIME 60 /* hang around for 60 seconds max */ 165 #endif 166 167 /* variables and functions */ 168 #ifndef LOG_FTP 169 #define LOG_FTP LOG_DAEMON 170 #endif 171 172 volatile sig_atomic_t alarmhit; 173 174 /* 175 * check there's enough space in the prefs and names arrays. 176 */ 177 static int 178 size_arrays(bozoprefs_t *bozoprefs, unsigned needed) 179 { 180 char **temp; 181 182 if (bozoprefs->size == 0) { 183 /* only get here first time around */ 184 bozoprefs->size = needed; 185 if ((bozoprefs->name = calloc(sizeof(char *), needed)) == NULL) { 186 (void) fprintf(stderr, "size_arrays: bad alloc\n"); 187 return 0; 188 } 189 if ((bozoprefs->value = calloc(sizeof(char *), needed)) == NULL) { 190 free(bozoprefs->name); 191 (void) fprintf(stderr, "size_arrays: bad alloc\n"); 192 return 0; 193 } 194 } else if (bozoprefs->c == bozoprefs->size) { 195 /* only uses 'needed' when filled array */ 196 bozoprefs->size += needed; 197 temp = realloc(bozoprefs->name, sizeof(char *) * needed); 198 if (temp == NULL) { 199 (void) fprintf(stderr, "size_arrays: bad alloc\n"); 200 return 0; 201 } 202 bozoprefs->name = temp; 203 temp = realloc(bozoprefs->value, sizeof(char *) * needed); 204 if (temp == NULL) { 205 (void) fprintf(stderr, "size_arrays: bad alloc\n"); 206 return 0; 207 } 208 bozoprefs->value = temp; 209 } 210 return 1; 211 } 212 213 static int 214 findvar(bozoprefs_t *bozoprefs, const char *name) 215 { 216 unsigned i; 217 218 for (i = 0 ; i < bozoprefs->c && strcmp(bozoprefs->name[i], name) != 0; i++) 219 ; 220 return (i == bozoprefs->c) ? -1 : (int)i; 221 } 222 223 int 224 bozo_set_pref(bozoprefs_t *bozoprefs, const char *name, const char *value) 225 { 226 int i; 227 228 if ((i = findvar(bozoprefs, name)) < 0) { 229 /* add the element to the array */ 230 if (size_arrays(bozoprefs, bozoprefs->size + 15)) { 231 bozoprefs->name[i = bozoprefs->c++] = strdup(name); 232 } 233 } else { 234 /* replace the element in the array */ 235 if (bozoprefs->value[i]) { 236 free(bozoprefs->value[i]); 237 bozoprefs->value[i] = NULL; 238 } 239 } 240 /* sanity checks for range of values go here */ 241 bozoprefs->value[i] = strdup(value); 242 return 1; 243 } 244 245 /* 246 * get a variable's value, or NULL 247 */ 248 char * 249 bozo_get_pref(bozoprefs_t *bozoprefs, const char *name) 250 { 251 int i; 252 253 return ((i = findvar(bozoprefs, name)) < 0) ? NULL : 254 bozoprefs->value[i]; 255 } 256 257 char * 258 bozo_http_date(char *date, size_t datelen) 259 { 260 struct tm *tm; 261 time_t now; 262 263 /* Sun, 06 Nov 1994 08:49:37 GMT */ 264 now = time(NULL); 265 tm = gmtime(&now); /* HTTP/1.1 spec rev 06 sez GMT only */ 266 strftime(date, datelen, "%a, %d %b %Y %H:%M:%S GMT", tm); 267 return date; 268 } 269 270 /* 271 * convert "in" into the three parts of a request (first line). 272 * we allocate into file and query, but return pointers into 273 * "in" for proto and method. 274 */ 275 static void 276 parse_request(bozohttpd_t *httpd, char *in, char **method, char **file, 277 char **query, char **proto) 278 { 279 ssize_t len; 280 char *val; 281 282 USE_ARG(httpd); 283 debug((httpd, DEBUG_EXPLODING, "parse in: %s", in)); 284 *method = *file = *query = *proto = NULL; 285 286 len = (ssize_t)strlen(in); 287 val = bozostrnsep(&in, " \t\n\r", &len); 288 if (len < 1 || val == NULL) 289 return; 290 *method = val; 291 292 while (*in == ' ' || *in == '\t') 293 in++; 294 val = bozostrnsep(&in, " \t\n\r", &len); 295 if (len < 1) { 296 if (len == 0) 297 *file = val; 298 else 299 *file = in; 300 } else { 301 *file = val; 302 303 *query = strchr(*file, '?'); 304 if (*query) 305 *(*query)++ = '\0'; 306 307 if (in) { 308 while (*in && (*in == ' ' || *in == '\t')) 309 in++; 310 if (*in) 311 *proto = in; 312 } 313 } 314 315 /* allocate private copies */ 316 *file = bozostrdup(httpd, *file); 317 if (*query) 318 *query = bozostrdup(httpd, *query); 319 320 debug((httpd, DEBUG_FAT, 321 "url: method: \"%s\" file: \"%s\" query: \"%s\" proto: \"%s\"", 322 *method, *file, *query, *proto)); 323 } 324 325 /* 326 * cleanup a bozo_httpreq_t after use 327 */ 328 void 329 bozo_clean_request(bozo_httpreq_t *request) 330 { 331 struct bozoheaders *hdr, *ohdr = NULL; 332 333 if (request == NULL) 334 return; 335 336 /* clean up request */ 337 #define MF(x) if (request->x) free(request->x) 338 MF(hr_remotehost); 339 MF(hr_remoteaddr); 340 MF(hr_serverport); 341 MF(hr_file); 342 MF(hr_query); 343 #undef MF 344 bozo_auth_cleanup(request); 345 for (hdr = SIMPLEQ_FIRST(&request->hr_headers); hdr; 346 hdr = SIMPLEQ_NEXT(hdr, h_next)) { 347 free(hdr->h_value); 348 free(hdr->h_header); 349 if (ohdr) 350 free(ohdr); 351 ohdr = hdr; 352 } 353 if (ohdr) 354 free(ohdr); 355 356 free(request); 357 } 358 359 /* 360 * send a HTTP/1.1 408 response if we timeout. 361 */ 362 /* ARGSUSED */ 363 static void 364 alarmer(int sig) 365 { 366 alarmhit = 1; 367 } 368 369 /* 370 * add or merge this header (val: str) into the requests list 371 */ 372 static bozoheaders_t * 373 addmerge_header(bozo_httpreq_t *request, char *val, 374 char *str, ssize_t len) 375 { 376 struct bozoheaders *hdr; 377 378 USE_ARG(len); 379 /* do we exist already? */ 380 SIMPLEQ_FOREACH(hdr, &request->hr_headers, h_next) { 381 if (strcasecmp(val, hdr->h_header) == 0) 382 break; 383 } 384 385 if (hdr) { 386 /* yup, merge it in */ 387 char *nval; 388 389 if (asprintf(&nval, "%s, %s", hdr->h_value, str) == -1) { 390 (void)bozo_http_error(request->hr_httpd, 500, NULL, 391 "memory allocation failure"); 392 return NULL; 393 } 394 free(hdr->h_value); 395 hdr->h_value = nval; 396 } else { 397 /* nope, create a new one */ 398 399 hdr = bozomalloc(request->hr_httpd, sizeof *hdr); 400 hdr->h_header = bozostrdup(request->hr_httpd, val); 401 if (str && *str) 402 hdr->h_value = bozostrdup(request->hr_httpd, str); 403 else 404 hdr->h_value = bozostrdup(request->hr_httpd, " "); 405 406 SIMPLEQ_INSERT_TAIL(&request->hr_headers, hdr, h_next); 407 request->hr_nheaders++; 408 } 409 410 return hdr; 411 } 412 413 /* 414 * as the prototype string is not constant (eg, "HTTP/1.1" is equivalent 415 * to "HTTP/001.01"), we MUST parse this. 416 */ 417 static int 418 process_proto(bozo_httpreq_t *request, const char *proto) 419 { 420 char majorstr[16], *minorstr; 421 int majorint, minorint; 422 423 if (proto == NULL) { 424 got_proto_09: 425 request->hr_proto = request->hr_httpd->consts.http_09; 426 debug((request->hr_httpd, DEBUG_FAT, "request %s is http/0.9", 427 request->hr_file)); 428 return 0; 429 } 430 431 if (strncasecmp(proto, "HTTP/", 5) != 0) 432 goto bad; 433 strncpy(majorstr, proto + 5, sizeof majorstr); 434 majorstr[sizeof(majorstr)-1] = 0; 435 minorstr = strchr(majorstr, '.'); 436 if (minorstr == NULL) 437 goto bad; 438 *minorstr++ = 0; 439 440 majorint = atoi(majorstr); 441 minorint = atoi(minorstr); 442 443 switch (majorint) { 444 case 0: 445 if (minorint != 9) 446 break; 447 goto got_proto_09; 448 case 1: 449 if (minorint == 0) 450 request->hr_proto = request->hr_httpd->consts.http_10; 451 else if (minorint == 1) 452 request->hr_proto = request->hr_httpd->consts.http_11; 453 else 454 break; 455 456 debug((request->hr_httpd, DEBUG_FAT, "request %s is %s", 457 request->hr_file, request->hr_proto)); 458 SIMPLEQ_INIT(&request->hr_headers); 459 request->hr_nheaders = 0; 460 return 0; 461 } 462 bad: 463 return bozo_http_error(request->hr_httpd, 404, NULL, "unknown prototype"); 464 } 465 466 /* 467 * process each type of HTTP method, setting this HTTP requests 468 # method type. 469 */ 470 static struct method_map { 471 const char *name; 472 int type; 473 } method_map[] = { 474 { "GET", HTTP_GET, }, 475 { "POST", HTTP_POST, }, 476 { "HEAD", HTTP_HEAD, }, 477 #if 0 /* other non-required http/1.1 methods */ 478 { "OPTIONS", HTTP_OPTIONS, }, 479 { "PUT", HTTP_PUT, }, 480 { "DELETE", HTTP_DELETE, }, 481 { "TRACE", HTTP_TRACE, }, 482 { "CONNECT", HTTP_CONNECT, }, 483 #endif 484 { NULL, 0, }, 485 }; 486 487 static int 488 process_method(bozo_httpreq_t *request, const char *method) 489 { 490 struct method_map *mmp; 491 492 if (request->hr_proto == request->hr_httpd->consts.http_11) 493 request->hr_allow = "GET, HEAD, POST"; 494 495 for (mmp = method_map; mmp->name; mmp++) 496 if (strcasecmp(method, mmp->name) == 0) { 497 request->hr_method = mmp->type; 498 request->hr_methodstr = mmp->name; 499 return 0; 500 } 501 502 return bozo_http_error(request->hr_httpd, 404, request, "unknown method"); 503 } 504 505 /* 506 * This function reads a http request from stdin, returning a pointer to a 507 * bozo_httpreq_t structure, describing the request. 508 */ 509 bozo_httpreq_t * 510 bozo_read_request(bozohttpd_t *httpd) 511 { 512 struct sigaction sa; 513 char *str, *val, *method, *file, *proto, *query; 514 char *host, *addr, *port; 515 char bufport[10]; 516 char hbuf[NI_MAXHOST], abuf[NI_MAXHOST]; 517 struct sockaddr_storage ss; 518 ssize_t len; 519 int line = 0; 520 socklen_t slen; 521 bozo_httpreq_t *request; 522 523 /* 524 * if we're in daemon mode, bozo_daemon_fork() will return here once 525 * for each child, then we can setup SSL. 526 */ 527 bozo_daemon_fork(httpd); 528 bozo_ssl_accept(httpd); 529 530 request = bozomalloc(httpd, sizeof(*request)); 531 memset(request, 0, sizeof(*request)); 532 request->hr_httpd = httpd; 533 request->hr_allow = request->hr_host = NULL; 534 request->hr_content_type = request->hr_content_length = NULL; 535 request->hr_range = NULL; 536 request->hr_last_byte_pos = -1; 537 request->hr_if_modified_since = NULL; 538 request->hr_file = NULL; 539 540 slen = sizeof(ss); 541 if (getpeername(0, (struct sockaddr *)(void *)&ss, &slen) < 0) 542 host = addr = NULL; 543 else { 544 if (getnameinfo((struct sockaddr *)(void *)&ss, slen, 545 abuf, sizeof abuf, NULL, 0, NI_NUMERICHOST) == 0) 546 addr = abuf; 547 else 548 addr = NULL; 549 if (httpd->numeric == 0 && 550 getnameinfo((struct sockaddr *)(void *)&ss, slen, 551 hbuf, sizeof hbuf, NULL, 0, 0) == 0) 552 host = hbuf; 553 else 554 host = NULL; 555 } 556 if (host != NULL) 557 request->hr_remotehost = bozostrdup(request->hr_httpd, host); 558 if (addr != NULL) 559 request->hr_remoteaddr = bozostrdup(request->hr_httpd, addr); 560 slen = sizeof(ss); 561 if (getsockname(0, (struct sockaddr *)(void *)&ss, &slen) < 0) 562 port = NULL; 563 else { 564 if (getnameinfo((struct sockaddr *)(void *)&ss, slen, NULL, 0, 565 bufport, sizeof bufport, NI_NUMERICSERV) == 0) 566 port = bufport; 567 else 568 port = NULL; 569 } 570 if (port != NULL) 571 request->hr_serverport = bozostrdup(request->hr_httpd, port); 572 573 /* 574 * setup a timer to make sure the request is not hung 575 */ 576 sa.sa_handler = alarmer; 577 sigemptyset(&sa.sa_mask); 578 sigaddset(&sa.sa_mask, SIGALRM); 579 sa.sa_flags = 0; 580 sigaction(SIGALRM, &sa, NULL); /* XXX */ 581 582 alarm(MAX_WAIT_TIME); 583 while ((str = bozodgetln(httpd, STDIN_FILENO, &len, bozo_read)) != NULL) { 584 alarm(0); 585 if (alarmhit) { 586 (void)bozo_http_error(httpd, 408, NULL, 587 "request timed out"); 588 goto cleanup; 589 } 590 line++; 591 592 if (line == 1) { 593 594 if (len < 1) { 595 (void)bozo_http_error(httpd, 404, NULL, 596 "null method"); 597 goto cleanup; 598 } 599 600 bozo_warn(httpd, "got request ``%s'' from host %s to port %s", 601 str, 602 host ? host : addr ? addr : "<local>", 603 port ? port : "<stdin>"); 604 #if 0 605 debug((httpd, DEBUG_FAT, 606 "read_req, getting request: ``%s''", str)); 607 #endif 608 609 /* we allocate return space in file and query only */ 610 parse_request(httpd, str, &method, &file, &query, &proto); 611 request->hr_file = file; 612 request->hr_query = query; 613 if (method == NULL) { 614 (void)bozo_http_error(httpd, 404, NULL, 615 "null method"); 616 goto cleanup; 617 } 618 if (file == NULL) { 619 (void)bozo_http_error(httpd, 404, NULL, 620 "null file"); 621 goto cleanup; 622 } 623 624 /* 625 * note that we parse the proto first, so that we 626 * can more properly parse the method and the url. 627 */ 628 629 if (process_proto(request, proto) || 630 process_method(request, method)) { 631 goto cleanup; 632 } 633 634 debug((httpd, DEBUG_FAT, "got file \"%s\" query \"%s\"", 635 request->hr_file, 636 request->hr_query ? request->hr_query : "<none>")); 637 638 /* http/0.9 has no header processing */ 639 if (request->hr_proto == httpd->consts.http_09) 640 break; 641 } else { /* incoming headers */ 642 bozoheaders_t *hdr; 643 644 if (*str == '\0') 645 break; 646 647 val = bozostrnsep(&str, ":", &len); 648 debug((httpd, DEBUG_EXPLODING, 649 "read_req2: after bozostrnsep: str ``%s'' val ``%s''", 650 str, val)); 651 if (val == NULL || len == -1) { 652 (void)bozo_http_error(httpd, 404, request, 653 "no header"); 654 goto cleanup; 655 } 656 while (*str == ' ' || *str == '\t') 657 len--, str++; 658 while (*val == ' ' || *val == '\t') 659 val++; 660 661 if (bozo_auth_check_headers(request, val, str, len)) 662 goto next_header; 663 664 hdr = addmerge_header(request, val, str, len); 665 666 if (strcasecmp(hdr->h_header, "content-type") == 0) 667 request->hr_content_type = hdr->h_value; 668 else if (strcasecmp(hdr->h_header, "content-length") == 0) 669 request->hr_content_length = hdr->h_value; 670 else if (strcasecmp(hdr->h_header, "host") == 0) 671 request->hr_host = hdr->h_value; 672 /* HTTP/1.1 rev06 draft spec: 14.20 */ 673 else if (strcasecmp(hdr->h_header, "expect") == 0) { 674 (void)bozo_http_error(httpd, 417, request, 675 "we don't support Expect:"); 676 goto cleanup; 677 } 678 else if (strcasecmp(hdr->h_header, "referrer") == 0 || 679 strcasecmp(hdr->h_header, "referer") == 0) 680 request->hr_referrer = hdr->h_value; 681 else if (strcasecmp(hdr->h_header, "range") == 0) 682 request->hr_range = hdr->h_value; 683 else if (strcasecmp(hdr->h_header, 684 "if-modified-since") == 0) 685 request->hr_if_modified_since = hdr->h_value; 686 687 debug((httpd, DEBUG_FAT, "adding header %s: %s", 688 hdr->h_header, hdr->h_value)); 689 } 690 next_header: 691 alarm(MAX_WAIT_TIME); 692 } 693 694 /* now, clear it all out */ 695 alarm(0); 696 signal(SIGALRM, SIG_DFL); 697 698 /* RFC1945, 8.3 */ 699 if (request->hr_method == HTTP_POST && 700 request->hr_content_length == NULL) { 701 (void)bozo_http_error(httpd, 400, request, 702 "missing content length"); 703 goto cleanup; 704 } 705 706 /* HTTP/1.1 draft rev-06, 14.23 & 19.6.1.1 */ 707 if (request->hr_proto == httpd->consts.http_11 && 708 request->hr_host == NULL) { 709 (void)bozo_http_error(httpd, 400, request, 710 "missing Host header"); 711 goto cleanup; 712 } 713 714 if (request->hr_range != NULL) { 715 debug((httpd, DEBUG_FAT, "hr_range: %s", request->hr_range)); 716 /* support only simple ranges %d- and %d-%d */ 717 if (strchr(request->hr_range, ',') == NULL) { 718 const char *rstart, *dash; 719 720 rstart = strchr(request->hr_range, '='); 721 if (rstart != NULL) { 722 rstart++; 723 dash = strchr(rstart, '-'); 724 if (dash != NULL && dash != rstart) { 725 dash++; 726 request->hr_have_range = 1; 727 request->hr_first_byte_pos = 728 strtoll(rstart, NULL, 10); 729 if (request->hr_first_byte_pos < 0) 730 request->hr_first_byte_pos = 0; 731 if (*dash != '\0') { 732 request->hr_last_byte_pos = 733 strtoll(dash, NULL, 10); 734 if (request->hr_last_byte_pos < 0) 735 request->hr_last_byte_pos = -1; 736 } 737 } 738 } 739 } 740 } 741 742 debug((httpd, DEBUG_FAT, "bozo_read_request returns url %s in request", 743 request->hr_file)); 744 return request; 745 746 cleanup: 747 bozo_clean_request(request); 748 749 /* If SSL enabled cleanup SSL structure. */ 750 bozo_ssl_destroy(httpd); 751 752 return NULL; 753 } 754 755 static int 756 mmap_and_write_part(bozohttpd_t *httpd, int fd, off_t first_byte_pos, size_t sz) 757 { 758 size_t mappedsz, wroffset; 759 off_t mappedoffset; 760 char *addr; 761 void *mappedaddr; 762 763 /* 764 * we need to ensure that both the size *and* offset arguments to 765 * mmap() are page-aligned. our formala for this is: 766 * 767 * input offset: first_byte_pos 768 * input size: sz 769 * 770 * mapped offset = page align truncate (input offset) 771 * mapped size = 772 * page align extend (input offset - mapped offset + input size) 773 * write offset = input offset - mapped offset 774 * 775 * we use the write offset in all writes 776 */ 777 mappedoffset = first_byte_pos & ~(httpd->page_size - 1); 778 mappedsz = (size_t) 779 (first_byte_pos - mappedoffset + sz + httpd->page_size - 1) & 780 ~(httpd->page_size - 1); 781 wroffset = (size_t)(first_byte_pos - mappedoffset); 782 783 addr = mmap(0, mappedsz, PROT_READ, MAP_SHARED, fd, mappedoffset); 784 if (addr == (char *)-1) { 785 bozo_warn(httpd, "mmap failed: %s", strerror(errno)); 786 return -1; 787 } 788 mappedaddr = addr; 789 790 #ifdef MADV_SEQUENTIAL 791 (void)madvise(addr, sz, MADV_SEQUENTIAL); 792 #endif 793 while (sz > BOZO_WRSZ) { 794 if (bozo_write(httpd, STDOUT_FILENO, addr + wroffset, 795 BOZO_WRSZ) != BOZO_WRSZ) { 796 bozo_warn(httpd, "write failed: %s", strerror(errno)); 797 goto out; 798 } 799 debug((httpd, DEBUG_OBESE, "wrote %d bytes", BOZO_WRSZ)); 800 sz -= BOZO_WRSZ; 801 addr += BOZO_WRSZ; 802 } 803 if (sz && (size_t)bozo_write(httpd, STDOUT_FILENO, addr + wroffset, 804 sz) != sz) { 805 bozo_warn(httpd, "final write failed: %s", strerror(errno)); 806 goto out; 807 } 808 debug((httpd, DEBUG_OBESE, "wrote %d bytes", (int)sz)); 809 out: 810 if (munmap(mappedaddr, mappedsz) < 0) { 811 bozo_warn(httpd, "munmap failed"); 812 return -1; 813 } 814 815 return 0; 816 } 817 818 static int 819 parse_http_date(const char *val, time_t *timestamp) 820 { 821 char *remainder; 822 struct tm tm; 823 824 if ((remainder = strptime(val, "%a, %d %b %Y %T GMT", &tm)) == NULL && 825 (remainder = strptime(val, "%a, %d-%b-%y %T GMT", &tm)) == NULL && 826 (remainder = strptime(val, "%a %b %d %T %Y", &tm)) == NULL) 827 return 0; /* Invalid HTTP date format */ 828 829 if (*remainder) 830 return 0; /* No trailing garbage */ 831 832 *timestamp = timegm(&tm); 833 return 1; 834 } 835 836 /* 837 * checks to see if this request has a valid .bzdirect file. returns 838 * 0 on failure and 1 on success. 839 */ 840 static int 841 check_direct_access(bozo_httpreq_t *request) 842 { 843 FILE *fp; 844 struct stat sb; 845 char dir[MAXPATHLEN], dirfile[MAXPATHLEN], *basename; 846 847 snprintf(dir, sizeof(dir), "%s", request->hr_file + 1); 848 debug((request->hr_httpd, DEBUG_FAT, "check_bzredirect: dir %s", dir)); 849 basename = strrchr(dir, '/'); 850 851 if ((!basename || basename[1] != '\0') && 852 lstat(dir, &sb) == 0 && S_ISDIR(sb.st_mode)) 853 /* nothing */; 854 else if (basename == NULL) 855 strcpy(dir, "."); 856 else { 857 *basename++ = '\0'; 858 bozo_check_special_files(request, basename); 859 } 860 861 snprintf(dirfile, sizeof(dirfile), "%s/%s", dir, DIRECT_ACCESS_FILE); 862 if (stat(dirfile, &sb) < 0 || 863 (fp = fopen(dirfile, "r")) == NULL) 864 return 0; 865 fclose(fp); 866 return 1; 867 } 868 869 /* 870 * do automatic redirection -- if there are query parameters for the URL 871 * we will tack these on to the new (redirected) URL. 872 */ 873 static void 874 handle_redirect(bozo_httpreq_t *request, 875 const char *url, int absolute) 876 { 877 bozohttpd_t *httpd = request->hr_httpd; 878 char *urlbuf; 879 char portbuf[20]; 880 int query = 0; 881 882 if (url == NULL) { 883 if (asprintf(&urlbuf, "/%s/", request->hr_file) < 0) 884 bozo_err(httpd, 1, "asprintf"); 885 url = urlbuf; 886 } else 887 urlbuf = NULL; 888 889 if (request->hr_query && strlen(request->hr_query)) { 890 query = 1; 891 } 892 893 if (request->hr_serverport && strcmp(request->hr_serverport, "80") != 0) 894 snprintf(portbuf, sizeof(portbuf), ":%s", 895 request->hr_serverport); 896 else 897 portbuf[0] = '\0'; 898 bozo_warn(httpd, "redirecting %s%s%s", httpd->virthostname, portbuf, url); 899 debug((httpd, DEBUG_FAT, "redirecting %s", url)); 900 bozo_printf(httpd, "%s 301 Document Moved\r\n", request->hr_proto); 901 if (request->hr_proto != httpd->consts.http_09) 902 bozo_print_header(request, NULL, "text/html", NULL); 903 if (request->hr_proto != httpd->consts.http_09) { 904 bozo_printf(httpd, "Location: http://"); 905 if (absolute == 0) 906 bozo_printf(httpd, "%s%s", httpd->virthostname, portbuf); 907 if (query) { 908 bozo_printf(httpd, "%s?%s\r\n", url, request->hr_query); 909 } else { 910 bozo_printf(httpd, "%s\r\n", url); 911 } 912 } 913 bozo_printf(httpd, "\r\n"); 914 if (request->hr_method == HTTP_HEAD) 915 goto head; 916 bozo_printf(httpd, "<html><head><title>Document Moved</title></head>\n"); 917 bozo_printf(httpd, "<body><h1>Document Moved</h1>\n"); 918 bozo_printf(httpd, "This document had moved <a href=\"http://"); 919 if (query) { 920 if (absolute) 921 bozo_printf(httpd, "%s?%s", url, request->hr_query); 922 else 923 bozo_printf(httpd, "%s%s%s?%s", httpd->virthostname, portbuf, url, 924 request->hr_query); 925 } else { 926 if (absolute) 927 bozo_printf(httpd, "%s", url); 928 else 929 bozo_printf(httpd, "%s%s%s", httpd->virthostname, portbuf, url); 930 } 931 bozo_printf(httpd, "\">here</a>\n"); 932 bozo_printf(httpd, "</body></html>\n"); 933 head: 934 bozo_flush(httpd, stdout); 935 if (urlbuf) 936 free(urlbuf); 937 } 938 939 /* 940 * deal with virtual host names; we do this: 941 * if we have a virtual path root (httpd->virtbase), and we are given a 942 * virtual host spec (Host: ho.st or http://ho.st/), see if this 943 * directory exists under httpd->virtbase. if it does, use this as the 944 # new slashdir. 945 */ 946 static int 947 check_virtual(bozo_httpreq_t *request) 948 { 949 bozohttpd_t *httpd = request->hr_httpd; 950 char *file = request->hr_file, *s; 951 struct dirent **list; 952 size_t len; 953 int i; 954 955 if (!httpd->virtbase) 956 goto use_slashdir; 957 958 /* 959 * convert http://virtual.host/ to request->hr_host 960 */ 961 debug((httpd, DEBUG_OBESE, "checking for http:// virtual host in ``%s''", 962 file)); 963 if (strncasecmp(file, "http://", 7) == 0) { 964 /* we would do virtual hosting here? */ 965 file += 7; 966 s = strchr(file, '/'); 967 /* HTTP/1.1 draft rev-06, 5.2: URI takes precedence over Host: */ 968 request->hr_host = file; 969 request->hr_file = bozostrdup(request->hr_httpd, s ? s : "/"); 970 debug((httpd, DEBUG_OBESE, "got host ``%s'' file is now ``%s''", 971 request->hr_host, request->hr_file)); 972 } else if (!request->hr_host) 973 goto use_slashdir; 974 975 /* 976 * ok, we have a virtual host, use scandir(3) to find a case 977 * insensitive match for the virtual host we are asked for. 978 * note that if the virtual host is the same as the master, 979 * we don't need to do anything special. 980 */ 981 len = strlen(request->hr_host); 982 debug((httpd, DEBUG_OBESE, 983 "check_virtual: checking host `%s' under httpd->virtbase `%s' " 984 "for file `%s'", 985 request->hr_host, httpd->virtbase, request->hr_file)); 986 if (strncasecmp(httpd->virthostname, request->hr_host, len) != 0) { 987 s = 0; 988 for (i = scandir(httpd->virtbase, &list, 0, 0); i--; list++) { 989 debug((httpd, DEBUG_OBESE, "looking at dir``%s''", 990 (*list)->d_name)); 991 if (strncasecmp((*list)->d_name, request->hr_host, 992 len) == 0) { 993 /* found it, punch it */ 994 httpd->virthostname = (*list)->d_name; 995 if (asprintf(&s, "%s/%s", httpd->virtbase, 996 httpd->virthostname) < 0) 997 bozo_err(httpd, 1, "asprintf"); 998 break; 999 } 1000 } 1001 if (s == 0) { 1002 if (httpd->unknown_slash) 1003 goto use_slashdir; 1004 return bozo_http_error(httpd, 404, request, 1005 "unknown URL"); 1006 } 1007 } else 1008 use_slashdir: 1009 s = httpd->slashdir; 1010 1011 /* 1012 * ok, nailed the correct slashdir, chdir to it 1013 */ 1014 if (chdir(s) < 0) 1015 return bozo_http_error(httpd, 404, request, 1016 "can't chdir to slashdir"); 1017 return 0; 1018 } 1019 1020 /* 1021 * checks to see if this request has a valid .bzredirect file. returns 1022 * 0 on failure and 1 on success. 1023 */ 1024 static void 1025 check_bzredirect(bozo_httpreq_t *request) 1026 { 1027 struct stat sb; 1028 char dir[MAXPATHLEN], redir[MAXPATHLEN], redirpath[MAXPATHLEN + 1]; 1029 char *basename, *finalredir; 1030 int rv, absolute; 1031 1032 /* 1033 * if this pathname is really a directory, but doesn't end in /, 1034 * use it as the directory to look for the redir file. 1035 */ 1036 snprintf(dir, sizeof(dir), "%s", request->hr_file + 1); 1037 debug((request->hr_httpd, DEBUG_FAT, "check_bzredirect: dir %s", dir)); 1038 basename = strrchr(dir, '/'); 1039 1040 if ((!basename || basename[1] != '\0') && 1041 lstat(dir, &sb) == 0 && S_ISDIR(sb.st_mode)) 1042 /* nothing */; 1043 else if (basename == NULL) 1044 strcpy(dir, "."); 1045 else { 1046 *basename++ = '\0'; 1047 bozo_check_special_files(request, basename); 1048 } 1049 1050 snprintf(redir, sizeof(redir), "%s/%s", dir, REDIRECT_FILE); 1051 if (lstat(redir, &sb) == 0) { 1052 if (!S_ISLNK(sb.st_mode)) 1053 return; 1054 absolute = 0; 1055 } else { 1056 snprintf(redir, sizeof(redir), "%s/%s", dir, ABSREDIRECT_FILE); 1057 if (lstat(redir, &sb) < 0 || !S_ISLNK(sb.st_mode)) 1058 return; 1059 absolute = 1; 1060 } 1061 debug((request->hr_httpd, DEBUG_FAT, 1062 "check_bzredirect: calling readlink")); 1063 rv = readlink(redir, redirpath, sizeof redirpath - 1); 1064 if (rv == -1 || rv == 0) { 1065 debug((request->hr_httpd, DEBUG_FAT, "readlink failed")); 1066 return; 1067 } 1068 redirpath[rv] = '\0'; 1069 debug((request->hr_httpd, DEBUG_FAT, 1070 "readlink returned \"%s\"", redirpath)); 1071 1072 /* now we have the link pointer, redirect to the real place */ 1073 if (absolute) 1074 finalredir = redirpath; 1075 else 1076 snprintf(finalredir = redir, sizeof(redir), "/%s/%s", dir, 1077 redirpath); 1078 1079 debug((request->hr_httpd, DEBUG_FAT, 1080 "check_bzredirect: new redir %s", finalredir)); 1081 handle_redirect(request, finalredir, absolute); 1082 } 1083 1084 /* this fixes the %HH hack that RFC2396 requires. */ 1085 static void 1086 fix_url_percent(bozo_httpreq_t *request) 1087 { 1088 bozohttpd_t *httpd = request->hr_httpd; 1089 char *s, *t, buf[3], *url; 1090 char *end; /* if end is not-zero, we don't translate beyond that */ 1091 1092 url = request->hr_file; 1093 1094 end = url + strlen(url); 1095 1096 /* fast forward to the first % */ 1097 if ((s = strchr(url, '%')) == NULL) 1098 return; 1099 1100 t = s; 1101 do { 1102 if (end && s >= end) { 1103 debug((httpd, DEBUG_EXPLODING, 1104 "fu_%%: past end, filling out..")); 1105 while (*s) 1106 *t++ = *s++; 1107 break; 1108 } 1109 debug((httpd, DEBUG_EXPLODING, 1110 "fu_%%: got s == %%, s[1]s[2] == %c%c", 1111 s[1], s[2])); 1112 if (s[1] == '\0' || s[2] == '\0') { 1113 (void)bozo_http_error(httpd, 400, request, 1114 "percent hack missing two chars afterwards"); 1115 goto copy_rest; 1116 } 1117 if (s[1] == '0' && s[2] == '0') { 1118 (void)bozo_http_error(httpd, 404, request, 1119 "percent hack was %00"); 1120 goto copy_rest; 1121 } 1122 if (s[1] == '2' && s[2] == 'f') { 1123 (void)bozo_http_error(httpd, 404, request, 1124 "percent hack was %2f (/)"); 1125 goto copy_rest; 1126 } 1127 1128 buf[0] = *++s; 1129 buf[1] = *++s; 1130 buf[2] = '\0'; 1131 s++; 1132 *t = (char)strtol(buf, NULL, 16); 1133 debug((httpd, DEBUG_EXPLODING, 1134 "fu_%%: strtol put '%02x' into *t", *t)); 1135 if (*t++ == '\0') { 1136 (void)bozo_http_error(httpd, 400, request, 1137 "percent hack got a 0 back"); 1138 goto copy_rest; 1139 } 1140 1141 while (*s && *s != '%') { 1142 if (end && s >= end) 1143 break; 1144 *t++ = *s++; 1145 } 1146 } while (*s); 1147 copy_rest: 1148 while (*s) { 1149 if (s >= end) 1150 break; 1151 *t++ = *s++; 1152 } 1153 *t = '\0'; 1154 debug((httpd, DEBUG_FAT, "fix_url_percent returns %s in url", 1155 request->hr_file)); 1156 } 1157 1158 /* 1159 * transform_request does this: 1160 * - ``expand'' %20 crapola 1161 * - punt if it doesn't start with / 1162 * - check httpd->untrustedref / referrer 1163 * - look for "http://myname/" and deal with it. 1164 * - maybe call bozo_process_cgi() 1165 * - check for ~user and call bozo_user_transform() if so 1166 * - if the length > 1, check for trailing slash. if so, 1167 * add the index.html file 1168 * - if the length is 1, return the index.html file 1169 * - disallow anything ending up with a file starting 1170 * at "/" or having ".." in it. 1171 * - anything else is a really weird internal error 1172 * - returns malloced file to serve, if unhandled 1173 */ 1174 static int 1175 transform_request(bozo_httpreq_t *request, int *isindex) 1176 { 1177 bozohttpd_t *httpd = request->hr_httpd; 1178 char *file, *newfile = NULL; 1179 size_t len; 1180 1181 file = NULL; 1182 *isindex = 0; 1183 debug((httpd, DEBUG_FAT, "tf_req: file %s", request->hr_file)); 1184 fix_url_percent(request); 1185 if (check_virtual(request)) { 1186 goto bad_done; 1187 } 1188 file = request->hr_file; 1189 1190 if (file[0] != '/') { 1191 (void)bozo_http_error(httpd, 404, request, "unknown URL"); 1192 goto bad_done; 1193 } 1194 1195 check_bzredirect(request); 1196 1197 if (httpd->untrustedref) { 1198 int to_indexhtml = 0; 1199 1200 #define TOP_PAGE(x) (strcmp((x), "/") == 0 || \ 1201 strcmp((x) + 1, httpd->index_html) == 0 || \ 1202 strcmp((x) + 1, "favicon.ico") == 0) 1203 1204 debug((httpd, DEBUG_EXPLODING, "checking httpd->untrustedref")); 1205 /* 1206 * first check that this path isn't allowed via .bzdirect file, 1207 * and then check referrer; make sure that people come via the 1208 * real name... otherwise if we aren't looking at / or 1209 * /index.html, redirect... we also special case favicon.ico. 1210 */ 1211 if (check_direct_access(request)) 1212 /* nothing */; 1213 else if (request->hr_referrer) { 1214 const char *r = request->hr_referrer; 1215 1216 debug((httpd, DEBUG_FAT, 1217 "checking referrer \"%s\" vs virthostname %s", 1218 r, httpd->virthostname)); 1219 if (strncmp(r, "http://", 7) != 0 || 1220 (strncasecmp(r + 7, httpd->virthostname, 1221 strlen(httpd->virthostname)) != 0 && 1222 !TOP_PAGE(file))) 1223 to_indexhtml = 1; 1224 } else { 1225 const char *h = request->hr_host; 1226 1227 debug((httpd, DEBUG_FAT, "url has no referrer at all")); 1228 /* if there's no referrer, let / or /index.html past */ 1229 if (!TOP_PAGE(file) || 1230 (h && strncasecmp(h, httpd->virthostname, 1231 strlen(httpd->virthostname)) != 0)) 1232 to_indexhtml = 1; 1233 } 1234 1235 if (to_indexhtml) { 1236 char *slashindexhtml; 1237 1238 if (asprintf(&slashindexhtml, "/%s", 1239 httpd->index_html) < 0) 1240 bozo_err(httpd, 1, "asprintf"); 1241 debug((httpd, DEBUG_FAT, 1242 "httpd->untrustedref: redirecting %s to %s", 1243 file, slashindexhtml)); 1244 handle_redirect(request, slashindexhtml, 0); 1245 free(slashindexhtml); 1246 return 0; 1247 } 1248 } 1249 1250 len = strlen(file); 1251 if (/*CONSTCOND*/0) { 1252 #ifndef NO_USER_SUPPORT 1253 } else if (len > 1 && httpd->enable_users && file[1] == '~') { 1254 if (file[2] == '\0') { 1255 (void)bozo_http_error(httpd, 404, request, 1256 "missing username"); 1257 goto bad_done; 1258 } 1259 if (strchr(file + 2, '/') == NULL) { 1260 handle_redirect(request, NULL, 0); 1261 return 0; 1262 } 1263 debug((httpd, DEBUG_FAT, "calling bozo_user_transform")); 1264 1265 return bozo_user_transform(request, isindex); 1266 #endif /* NO_USER_SUPPORT */ 1267 } else if (len > 1) { 1268 debug((httpd, DEBUG_FAT, "file[len-1] == %c", file[len-1])); 1269 if (file[len-1] == '/') { /* append index.html */ 1270 *isindex = 1; 1271 debug((httpd, DEBUG_FAT, "appending index.html")); 1272 newfile = bozomalloc(httpd, 1273 len + strlen(httpd->index_html) + 1); 1274 strcpy(newfile, file + 1); 1275 strcat(newfile, httpd->index_html); 1276 } else 1277 newfile = bozostrdup(request->hr_httpd, file + 1); 1278 } else if (len == 1) { 1279 debug((httpd, DEBUG_EXPLODING, "tf_req: len == 1")); 1280 newfile = bozostrdup(request->hr_httpd, httpd->index_html); 1281 *isindex = 1; 1282 } else { /* len == 0 ? */ 1283 (void)bozo_http_error(httpd, 500, request, 1284 "request->hr_file is nul?"); 1285 goto bad_done; 1286 } 1287 1288 if (newfile == NULL) { 1289 (void)bozo_http_error(httpd, 500, request, "internal failure"); 1290 goto bad_done; 1291 } 1292 1293 /* 1294 * look for "http://myname/" and deal with it as necessary. 1295 */ 1296 1297 /* 1298 * stop traversing outside our domain 1299 * 1300 * XXX true security only comes from our parent using chroot(2) 1301 * before execve(2)'ing us. or our own built in chroot(2) support. 1302 */ 1303 if (*newfile == '/' || strcmp(newfile, "..") == 0 || 1304 strstr(newfile, "/..") || strstr(newfile, "../")) { 1305 (void)bozo_http_error(httpd, 403, request, "illegal request"); 1306 goto bad_done; 1307 } 1308 1309 if (bozo_auth_check(request, newfile)) 1310 goto bad_done; 1311 1312 if (strlen(newfile)) { 1313 free(request->hr_file); 1314 request->hr_file = newfile; 1315 } 1316 1317 if (bozo_process_cgi(request)) 1318 return 0; 1319 1320 debug((httpd, DEBUG_FAT, "transform_request set: %s", newfile)); 1321 return 1; 1322 bad_done: 1323 debug((httpd, DEBUG_FAT, "transform_request returning: 0")); 1324 if (newfile) 1325 free(newfile); 1326 return 0; 1327 } 1328 1329 /* 1330 * bozo_process_request does the following: 1331 * - check the request is valid 1332 * - process cgi-bin if necessary 1333 * - transform a filename if necesarry 1334 * - return the HTTP request 1335 */ 1336 void 1337 bozo_process_request(bozo_httpreq_t *request) 1338 { 1339 bozohttpd_t *httpd = request->hr_httpd; 1340 struct stat sb; 1341 time_t timestamp; 1342 char *file; 1343 const char *type, *encoding; 1344 int fd, isindex; 1345 1346 /* 1347 * note that transform_request chdir()'s if required. also note 1348 * that cgi is handed here. if transform_request() returns 0 1349 * then the request has been handled already. 1350 */ 1351 if (transform_request(request, &isindex) == 0) 1352 return; 1353 1354 file = request->hr_file; 1355 1356 fd = open(file, O_RDONLY); 1357 if (fd < 0) { 1358 debug((httpd, DEBUG_FAT, "open failed: %s", strerror(errno))); 1359 if (errno == EPERM) 1360 (void)bozo_http_error(httpd, 403, request, 1361 "no permission to open file"); 1362 else if (errno == ENOENT) { 1363 if (!bozo_dir_index(request, file, isindex)) 1364 (void)bozo_http_error(httpd, 404, request, 1365 "no file"); 1366 } else 1367 (void)bozo_http_error(httpd, 500, request, "open file"); 1368 goto cleanup_nofd; 1369 } 1370 if (fstat(fd, &sb) < 0) { 1371 (void)bozo_http_error(httpd, 500, request, "can't fstat"); 1372 goto cleanup; 1373 } 1374 if (S_ISDIR(sb.st_mode)) { 1375 handle_redirect(request, NULL, 0); 1376 goto cleanup; 1377 } 1378 1379 if (request->hr_if_modified_since && 1380 parse_http_date(request->hr_if_modified_since, ×tamp) && 1381 timestamp >= sb.st_mtime) { 1382 /* XXX ignore subsecond of timestamp */ 1383 bozo_printf(httpd, "%s 304 Not Modified\r\n", 1384 request->hr_proto); 1385 bozo_printf(httpd, "\r\n"); 1386 bozo_flush(httpd, stdout); 1387 goto cleanup; 1388 } 1389 1390 /* validate requested range */ 1391 if (request->hr_last_byte_pos == -1 || 1392 request->hr_last_byte_pos >= sb.st_size) 1393 request->hr_last_byte_pos = sb.st_size - 1; 1394 if (request->hr_have_range && 1395 request->hr_first_byte_pos > request->hr_last_byte_pos) { 1396 request->hr_have_range = 0; /* punt */ 1397 request->hr_first_byte_pos = 0; 1398 request->hr_last_byte_pos = sb.st_size - 1; 1399 } 1400 debug((httpd, DEBUG_FAT, "have_range %d first_pos %qd last_pos %qd", 1401 request->hr_have_range, 1402 request->hr_first_byte_pos, request->hr_last_byte_pos)); 1403 if (request->hr_have_range) 1404 bozo_printf(httpd, "%s 206 Partial Content\r\n", 1405 request->hr_proto); 1406 else 1407 bozo_printf(httpd, "%s 200 OK\r\n", request->hr_proto); 1408 1409 if (request->hr_proto != httpd->consts.http_09) { 1410 type = bozo_content_type(request, file); 1411 encoding = bozo_content_encoding(request, file); 1412 1413 bozo_print_header(request, &sb, type, encoding); 1414 bozo_printf(httpd, "\r\n"); 1415 } 1416 bozo_flush(httpd, stdout); 1417 1418 if (request->hr_method != HTTP_HEAD) { 1419 off_t szleft, cur_byte_pos; 1420 1421 szleft = 1422 request->hr_last_byte_pos - request->hr_first_byte_pos + 1; 1423 cur_byte_pos = request->hr_first_byte_pos; 1424 1425 retry: 1426 while (szleft) { 1427 size_t sz; 1428 1429 /* This should take care of the first unaligned chunk */ 1430 if ((cur_byte_pos & (httpd->page_size - 1)) != 0) 1431 sz = (size_t)(cur_byte_pos & ~httpd->page_size); 1432 if ((off_t)httpd->mmapsz < szleft) 1433 sz = httpd->mmapsz; 1434 else 1435 sz = (size_t)szleft; 1436 if (mmap_and_write_part(httpd, fd, cur_byte_pos, sz)) { 1437 if (errno == ENOMEM) { 1438 httpd->mmapsz /= 2; 1439 if (httpd->mmapsz >= httpd->page_size) 1440 goto retry; 1441 } 1442 goto cleanup; 1443 } 1444 cur_byte_pos += sz; 1445 szleft -= sz; 1446 } 1447 } 1448 cleanup: 1449 close(fd); 1450 cleanup_nofd: 1451 close(STDIN_FILENO); 1452 close(STDOUT_FILENO); 1453 /*close(STDERR_FILENO);*/ 1454 } 1455 1456 /* make sure we're not trying to access special files */ 1457 int 1458 bozo_check_special_files(bozo_httpreq_t *request, const char *name) 1459 { 1460 bozohttpd_t *httpd = request->hr_httpd; 1461 1462 /* ensure basename(name) != special files */ 1463 if (strcmp(name, DIRECT_ACCESS_FILE) == 0) 1464 return bozo_http_error(httpd, 403, request, 1465 "no permission to open direct access file"); 1466 if (strcmp(name, REDIRECT_FILE) == 0) 1467 return bozo_http_error(httpd, 403, request, 1468 "no permission to open redirect file"); 1469 if (strcmp(name, ABSREDIRECT_FILE) == 0) 1470 return bozo_http_error(httpd, 403, request, 1471 "no permission to open redirect file"); 1472 return bozo_auth_check_special_files(request, name); 1473 } 1474 1475 /* generic header printing routine */ 1476 void 1477 bozo_print_header(bozo_httpreq_t *request, 1478 struct stat *sbp, const char *type, const char *encoding) 1479 { 1480 bozohttpd_t *httpd = request->hr_httpd; 1481 off_t len; 1482 char date[40]; 1483 1484 bozo_printf(httpd, "Date: %s\r\n", bozo_http_date(date, sizeof(date))); 1485 bozo_printf(httpd, "Server: %s\r\n", httpd->server_software); 1486 bozo_printf(httpd, "Accept-Ranges: bytes\r\n"); 1487 if (sbp) { 1488 char filedate[40]; 1489 struct tm *tm; 1490 1491 tm = gmtime(&sbp->st_mtime); 1492 strftime(filedate, sizeof filedate, 1493 "%a, %d %b %Y %H:%M:%S GMT", tm); 1494 bozo_printf(httpd, "Last-Modified: %s\r\n", filedate); 1495 } 1496 if (type && *type) 1497 bozo_printf(httpd, "Content-Type: %s\r\n", type); 1498 if (encoding && *encoding) 1499 bozo_printf(httpd, "Content-Encoding: %s\r\n", encoding); 1500 if (sbp) { 1501 if (request->hr_have_range) { 1502 len = request->hr_last_byte_pos - 1503 request->hr_first_byte_pos +1; 1504 bozo_printf(httpd, 1505 "Content-Range: bytes %qd-%qd/%qd\r\n", 1506 (long long) request->hr_first_byte_pos, 1507 (long long) request->hr_last_byte_pos, 1508 (long long) sbp->st_size); 1509 } else 1510 len = sbp->st_size; 1511 bozo_printf(httpd, "Content-Length: %qd\r\n", (long long)len); 1512 } 1513 if (request && request->hr_proto == httpd->consts.http_11) 1514 bozo_printf(httpd, "Connection: close\r\n"); 1515 bozo_flush(httpd, stdout); 1516 } 1517 1518 #ifdef DEBUG 1519 void 1520 debug__(bozohttpd_t *httpd, int level, const char *fmt, ...) 1521 { 1522 va_list ap; 1523 int savederrno; 1524 1525 /* only log if the level is low enough */ 1526 if (httpd->debug < level) 1527 return; 1528 1529 savederrno = errno; 1530 va_start(ap, fmt); 1531 if (httpd->logstderr) { 1532 vfprintf(stderr, fmt, ap); 1533 fputs("\n", stderr); 1534 } else 1535 vsyslog(LOG_DEBUG, fmt, ap); 1536 va_end(ap); 1537 errno = savederrno; 1538 } 1539 #endif /* DEBUG */ 1540 1541 /* these are like warn() and err(), except for syslog not stderr */ 1542 void 1543 bozo_warn(bozohttpd_t *httpd, const char *fmt, ...) 1544 { 1545 va_list ap; 1546 1547 va_start(ap, fmt); 1548 if (httpd->logstderr || isatty(STDERR_FILENO)) { 1549 //fputs("warning: ", stderr); 1550 vfprintf(stderr, fmt, ap); 1551 fputs("\n", stderr); 1552 } else 1553 vsyslog(LOG_INFO, fmt, ap); 1554 va_end(ap); 1555 } 1556 1557 void 1558 bozo_err(bozohttpd_t *httpd, int code, const char *fmt, ...) 1559 { 1560 va_list ap; 1561 1562 va_start(ap, fmt); 1563 if (httpd->logstderr || isatty(STDERR_FILENO)) { 1564 //fputs("error: ", stderr); 1565 vfprintf(stderr, fmt, ap); 1566 fputs("\n", stderr); 1567 } else 1568 vsyslog(LOG_ERR, fmt, ap); 1569 va_end(ap); 1570 exit(code); 1571 } 1572 1573 /* this escape HTML tags */ 1574 static void 1575 escape_html(bozo_httpreq_t *request) 1576 { 1577 int i, j; 1578 char *url = request->hr_file, *tmp; 1579 1580 for (i = 0, j = 0; url[i]; i++) { 1581 switch (url[i]) { 1582 case '<': 1583 case '>': 1584 j += 4; 1585 break; 1586 case '&': 1587 j += 5; 1588 break; 1589 } 1590 } 1591 1592 if (j == 0) 1593 return; 1594 1595 if ((tmp = (char *) malloc(strlen(url) + j)) == 0) 1596 /* 1597 * ouch, but we are only called from an error context, and 1598 * most paths here come from malloc(3) failures anyway... 1599 * we could completely punt and just exit, but isn't returning 1600 * an not-quite-correct error better than nothing at all? 1601 */ 1602 return; 1603 1604 for (i = 0, j = 0; url[i]; i++) { 1605 switch (url[i]) { 1606 case '<': 1607 memcpy(tmp + j, "<", 4); 1608 j += 4; 1609 break; 1610 case '>': 1611 memcpy(tmp + j, ">", 4); 1612 j += 4; 1613 break; 1614 case '&': 1615 memcpy(tmp + j, "&", 5); 1616 j += 5; 1617 break; 1618 default: 1619 tmp[j++] = url[i]; 1620 } 1621 } 1622 tmp[j] = 0; 1623 1624 free(request->hr_file); 1625 request->hr_file = tmp; 1626 } 1627 1628 /* short map between error code, and short/long messages */ 1629 static struct errors_map { 1630 int code; /* HTTP return code */ 1631 const char *shortmsg; /* short version of message */ 1632 const char *longmsg; /* long version of message */ 1633 } errors_map[] = { 1634 { 400, "400 Bad Request", "The request was not valid", }, 1635 { 401, "401 Unauthorized", "No authorization", }, 1636 { 403, "403 Forbidden", "Access to this item has been denied",}, 1637 { 404, "404 Not Found", "This item has not been found", }, 1638 { 408, "408 Request Timeout", "This request took too long", }, 1639 { 417, "417 Expectation Failed","Expectations not available", }, 1640 { 500, "500 Internal Error", "An error occured on the server", }, 1641 { 501, "501 Not Implemented", "This request is not available", }, 1642 { 0, NULL, NULL, }, 1643 }; 1644 1645 static const char *help = "DANGER! WILL ROBINSON! DANGER!"; 1646 1647 static const char * 1648 http_errors_short(int code) 1649 { 1650 struct errors_map *ep; 1651 1652 for (ep = errors_map; ep->code; ep++) 1653 if (ep->code == code) 1654 return (ep->shortmsg); 1655 return (help); 1656 } 1657 1658 static const char * 1659 http_errors_long(int code) 1660 { 1661 struct errors_map *ep; 1662 1663 for (ep = errors_map; ep->code; ep++) 1664 if (ep->code == code) 1665 return (ep->longmsg); 1666 return (help); 1667 } 1668 1669 /* the follow functions and variables are used in handling HTTP errors */ 1670 /* ARGSUSED */ 1671 int 1672 bozo_http_error(bozohttpd_t *httpd, int code, bozo_httpreq_t *request, 1673 const char *msg) 1674 { 1675 char portbuf[20]; 1676 const char *header = http_errors_short(code); 1677 const char *reason = http_errors_long(code); 1678 const char *proto = (request && request->hr_proto) ? 1679 request->hr_proto : httpd->consts.http_11; 1680 int size; 1681 1682 debug((httpd, DEBUG_FAT, "bozo_http_error %d: %s", code, msg)); 1683 if (header == NULL || reason == NULL) { 1684 bozo_err(httpd, 1, 1685 "bozo_http_error() failed (short = %p, long = %p)", 1686 header, reason); 1687 return code; 1688 } 1689 1690 if (request && request->hr_serverport && 1691 strcmp(request->hr_serverport, "80") != 0) 1692 snprintf(portbuf, sizeof(portbuf), ":%s", 1693 request->hr_serverport); 1694 else 1695 portbuf[0] = '\0'; 1696 1697 if (request && request->hr_file) { 1698 escape_html(request); 1699 size = snprintf(httpd->errorbuf, BUFSIZ, 1700 "<html><head><title>%s</title></head>\n" 1701 "<body><h1>%s</h1>\n" 1702 "%s: <pre>%s</pre>\n" 1703 "<hr><address><a href=\"http://%s%s/\">%s%s</a></address>\n" 1704 "</body></html>\n", 1705 header, header, request->hr_file, reason, 1706 httpd->virthostname, portbuf, httpd->virthostname, portbuf); 1707 if (size >= (int)BUFSIZ) { 1708 bozo_warn(httpd, 1709 "bozo_http_error buffer too small, truncated"); 1710 size = (int)BUFSIZ; 1711 } 1712 } else 1713 size = 0; 1714 1715 bozo_printf(httpd, "%s %s\r\n", proto, header); 1716 bozo_auth_check_401(request, code); 1717 1718 bozo_printf(httpd, "Content-Type: text/html\r\n"); 1719 bozo_printf(httpd, "Content-Length: %d\r\n", size); 1720 bozo_printf(httpd, "Server: %s\r\n", httpd->server_software); 1721 if (request && request->hr_allow) 1722 bozo_printf(httpd, "Allow: %s\r\n", request->hr_allow); 1723 bozo_printf(httpd, "\r\n"); 1724 if (size) 1725 bozo_printf(httpd, "%s", httpd->errorbuf); 1726 bozo_flush(httpd, stdout); 1727 1728 return code; 1729 } 1730 1731 /* Below are various modified libc functions */ 1732 1733 /* 1734 * returns -1 in lenp if the string ran out before finding a delimiter, 1735 * but is otherwise the same as strsep. Note that the length must be 1736 * correctly passed in. 1737 */ 1738 char * 1739 bozostrnsep(char **strp, const char *delim, ssize_t *lenp) 1740 { 1741 char *s; 1742 const char *spanp; 1743 int c, sc; 1744 char *tok; 1745 1746 if ((s = *strp) == NULL) 1747 return (NULL); 1748 for (tok = s;;) { 1749 if (lenp && --(*lenp) == -1) 1750 return (NULL); 1751 c = *s++; 1752 spanp = delim; 1753 do { 1754 if ((sc = *spanp++) == c) { 1755 if (c == 0) 1756 s = NULL; 1757 else 1758 s[-1] = '\0'; 1759 *strp = s; 1760 return (tok); 1761 } 1762 } while (sc != 0); 1763 } 1764 /* NOTREACHED */ 1765 } 1766 1767 /* 1768 * inspired by fgetln(3), but works for fd's. should work identically 1769 * except it, however, does *not* return the newline, and it does nul 1770 * terminate the string. 1771 */ 1772 char * 1773 bozodgetln(bozohttpd_t *httpd, int fd, ssize_t *lenp, 1774 ssize_t (*readfn)(bozohttpd_t *, int, void *, size_t)) 1775 { 1776 ssize_t len; 1777 int got_cr = 0; 1778 char c, *nbuffer; 1779 1780 /* initialise */ 1781 if (httpd->getln_buflen == 0) { 1782 /* should be plenty for most requests */ 1783 httpd->getln_buflen = 128; 1784 httpd->getln_buffer = malloc((size_t)httpd->getln_buflen); 1785 if (httpd->getln_buffer == NULL) { 1786 httpd->getln_buflen = 0; 1787 return NULL; 1788 } 1789 } 1790 len = 0; 1791 1792 /* 1793 * we *have* to read one byte at a time, to not break cgi 1794 * programs (for we pass stdin off to them). could fix this 1795 * by becoming a fd-passing program instead of just exec'ing 1796 * the program 1797 * 1798 * the above is no longer true, we are the fd-passing 1799 * program already. 1800 */ 1801 for (; readfn(httpd, fd, &c, 1) == 1; ) { 1802 debug((httpd, DEBUG_EXPLODING, "bozodgetln read %c", c)); 1803 1804 if (len >= httpd->getln_buflen - 1) { 1805 httpd->getln_buflen *= 2; 1806 debug((httpd, DEBUG_EXPLODING, "bozodgetln: " 1807 "reallocating buffer to buflen %zu", 1808 httpd->getln_buflen)); 1809 nbuffer = bozorealloc(httpd, httpd->getln_buffer, 1810 (size_t)httpd->getln_buflen); 1811 httpd->getln_buffer = nbuffer; 1812 } 1813 1814 httpd->getln_buffer[len++] = c; 1815 if (c == '\r') { 1816 got_cr = 1; 1817 continue; 1818 } else if (c == '\n') { 1819 /* 1820 * HTTP/1.1 spec says to ignore CR and treat 1821 * LF as the real line terminator. even though 1822 * the same spec defines CRLF as the line 1823 * terminator, it is recommended in section 19.3 1824 * to do the LF trick for tolerance. 1825 */ 1826 if (got_cr) 1827 len -= 2; 1828 else 1829 len -= 1; 1830 break; 1831 } 1832 1833 } 1834 httpd->getln_buffer[len] = '\0'; 1835 debug((httpd, DEBUG_OBESE, "bozodgetln returns: ``%s'' with len %d", 1836 httpd->getln_buffer, len)); 1837 *lenp = len; 1838 return httpd->getln_buffer; 1839 } 1840 1841 void * 1842 bozorealloc(bozohttpd_t *httpd, void *ptr, size_t size) 1843 { 1844 void *p; 1845 1846 p = realloc(ptr, size); 1847 if (p == NULL) { 1848 (void)bozo_http_error(httpd, 500, NULL, 1849 "memory allocation failure"); 1850 exit(1); 1851 } 1852 return (p); 1853 } 1854 1855 void * 1856 bozomalloc(bozohttpd_t *httpd, size_t size) 1857 { 1858 void *p; 1859 1860 p = malloc(size); 1861 if (p == NULL) { 1862 (void)bozo_http_error(httpd, 500, NULL, 1863 "memory allocation failure"); 1864 exit(1); 1865 } 1866 return (p); 1867 } 1868 1869 char * 1870 bozostrdup(bozohttpd_t *httpd, const char *str) 1871 { 1872 char *p; 1873 1874 p = strdup(str); 1875 if (p == NULL) { 1876 (void)bozo_http_error(httpd, 500, NULL, 1877 "memory allocation failure"); 1878 exit(1); 1879 } 1880 return (p); 1881 } 1882 1883 /* set default values in bozohttpd_t struct */ 1884 int 1885 bozo_init_httpd(bozohttpd_t *httpd) 1886 { 1887 /* make sure everything is clean */ 1888 (void) memset(httpd, 0x0, sizeof(*httpd)); 1889 1890 /* constants */ 1891 httpd->consts.http_09 = "HTTP/0.9"; 1892 httpd->consts.http_10 = "HTTP/1.0"; 1893 httpd->consts.http_11 = "HTTP/1.1"; 1894 httpd->consts.text_plain = "text/plain"; 1895 1896 /* mmap region size */ 1897 httpd->mmapsz = BOZO_MMAPSZ; 1898 1899 /* error buffer for bozo_http_error() */ 1900 if ((httpd->errorbuf = malloc(BUFSIZ)) == NULL) { 1901 (void) fprintf(stderr, 1902 "bozohttpd: memory_allocation failure\n"); 1903 return 0; 1904 } 1905 return 1; 1906 } 1907 1908 /* set default values in bozoprefs_t struct */ 1909 int 1910 bozo_init_prefs(bozoprefs_t *prefs) 1911 { 1912 /* make sure everything is clean */ 1913 (void) memset(prefs, 0x0, sizeof(*prefs)); 1914 1915 /* set up default values */ 1916 bozo_set_pref(prefs, "server software", SERVER_SOFTWARE); 1917 bozo_set_pref(prefs, "index.html", INDEX_HTML); 1918 bozo_set_pref(prefs, "public_html", PUBLIC_HTML); 1919 1920 return 1; 1921 } 1922 1923 /* set default values */ 1924 int 1925 bozo_set_defaults(bozohttpd_t *httpd, bozoprefs_t *prefs) 1926 { 1927 return bozo_init_httpd(httpd) && bozo_init_prefs(prefs); 1928 } 1929 1930 /* set the virtual host name, port and root */ 1931 int 1932 bozo_setup(bozohttpd_t *httpd, bozoprefs_t *prefs, const char *vhost, 1933 const char *root) 1934 { 1935 struct passwd *pw; 1936 extern char **environ; 1937 uid_t uid; 1938 char *cleanenv[1]; 1939 char *chrootdir; 1940 char *username; 1941 char *portnum; 1942 char *cp; 1943 int dirtyenv; 1944 1945 dirtyenv = 0; 1946 1947 if (vhost == NULL) { 1948 httpd->virthostname = bozomalloc(httpd, MAXHOSTNAMELEN+1); 1949 /* XXX we do not check for FQDN here */ 1950 if (gethostname(httpd->virthostname, MAXHOSTNAMELEN+1) < 0) 1951 bozo_err(httpd, 1, "gethostname"); 1952 httpd->virthostname[MAXHOSTNAMELEN] = '\0'; 1953 } else { 1954 httpd->virthostname = strdup(vhost); 1955 } 1956 httpd->slashdir = strdup(root); 1957 if ((portnum = bozo_get_pref(prefs, "port number")) != NULL) { 1958 httpd->bindport = strdup(portnum); 1959 } 1960 1961 /* go over preferences now */ 1962 if ((cp = bozo_get_pref(prefs, "numeric")) != NULL && 1963 strcmp(cp, "true") == 0) { 1964 httpd->numeric = 1; 1965 } 1966 if ((cp = bozo_get_pref(prefs, "trusted referal")) != NULL && 1967 strcmp(cp, "true") == 0) { 1968 httpd->untrustedref = 1; 1969 } 1970 if ((cp = bozo_get_pref(prefs, "log to stderr")) != NULL && 1971 strcmp(cp, "true") == 0) { 1972 httpd->logstderr = 1; 1973 } 1974 if ((cp = bozo_get_pref(prefs, "bind address")) != NULL) { 1975 httpd->bindaddress = strdup(cp); 1976 } 1977 if ((cp = bozo_get_pref(prefs, "background")) != NULL) { 1978 httpd->background = atoi(cp); 1979 } 1980 if ((cp = bozo_get_pref(prefs, "foreground")) != NULL && 1981 strcmp(cp, "true") == 0) { 1982 httpd->foreground = 1; 1983 } 1984 if ((cp = bozo_get_pref(prefs, "unknown slash")) != NULL && 1985 strcmp(cp, "true") == 0) { 1986 httpd->unknown_slash = 1; 1987 } 1988 if ((cp = bozo_get_pref(prefs, "virtual base")) != NULL) { 1989 httpd->virtbase = strdup(cp); 1990 } 1991 if ((cp = bozo_get_pref(prefs, "enable users")) != NULL && 1992 strcmp(cp, "true") == 0) { 1993 httpd->enable_users = 1; 1994 } 1995 if ((cp = bozo_get_pref(prefs, "dirty environment")) != NULL && 1996 strcmp(cp, "true") == 0) { 1997 dirtyenv = 1; 1998 } 1999 if ((cp = bozo_get_pref(prefs, "hide dots")) != NULL && 2000 strcmp(cp, "true") == 0) { 2001 httpd->hide_dots = 1; 2002 } 2003 if ((cp = bozo_get_pref(prefs, "directory indexing")) != NULL && 2004 strcmp(cp, "true") == 0) { 2005 httpd->dir_indexing = 1; 2006 } 2007 httpd->server_software = 2008 strdup(bozo_get_pref(prefs, "server software")); 2009 httpd->index_html = strdup(bozo_get_pref(prefs, "index.html")); 2010 2011 /* 2012 * initialise ssl and daemon mode if necessary. 2013 */ 2014 bozo_ssl_init(httpd); 2015 bozo_daemon_init(httpd); 2016 2017 if ((username = bozo_get_pref(prefs, "username")) == NULL) { 2018 if ((pw = getpwuid(uid = 0)) == NULL) 2019 bozo_err(httpd, 1, "getpwuid(0): %s", strerror(errno)); 2020 httpd->username = strdup(pw->pw_name); 2021 } else { 2022 httpd->username = strdup(username); 2023 if ((pw = getpwnam(httpd->username)) == NULL) 2024 bozo_err(httpd, 1, "getpwnam(%s): %s", httpd->username, 2025 strerror(errno)); 2026 if (initgroups(pw->pw_name, pw->pw_gid) == -1) 2027 bozo_err(httpd, 1, "initgroups: %s", strerror(errno)); 2028 if (setgid(pw->pw_gid) == -1) 2029 bozo_err(httpd, 1, "setgid(%u): %s", pw->pw_gid, 2030 strerror(errno)); 2031 uid = pw->pw_uid; 2032 } 2033 /* 2034 * handle chroot. 2035 */ 2036 if ((chrootdir = bozo_get_pref(prefs, "chroot dir")) != NULL) { 2037 httpd->rootdir = strdup(chrootdir); 2038 if (chdir(httpd->rootdir) == -1) 2039 bozo_err(httpd, 1, "chdir(%s): %s", httpd->rootdir, 2040 strerror(errno)); 2041 if (chroot(httpd->rootdir) == -1) 2042 bozo_err(httpd, 1, "chroot(%s): %s", httpd->rootdir, 2043 strerror(errno)); 2044 } 2045 2046 if (username != NULL) 2047 if (setuid(uid) == -1) 2048 bozo_err(httpd, 1, "setuid(%d): %s", uid, 2049 strerror(errno)); 2050 2051 /* 2052 * prevent info leakage between different compartments. 2053 * some PATH values in the environment would be invalided 2054 * by chroot. cross-user settings might result in undesirable 2055 * effects. 2056 */ 2057 if ((chrootdir != NULL || username != NULL) && !dirtyenv) { 2058 cleanenv[0] = NULL; 2059 environ = cleanenv; 2060 } 2061 #ifdef _SC_PAGESIZE 2062 httpd->page_size = (long)sysconf(_SC_PAGESIZE); 2063 #else 2064 httpd->page_size = 4096; 2065 #endif 2066 debug((httpd, DEBUG_OBESE, "myname is %s, slashdir is %s", 2067 httpd->virthostname, httpd->slashdir)); 2068 2069 return 1; 2070 } 2071