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