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