Blob


1 /*
2 * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/mman.h>
18 #include <sys/socket.h>
19 #include <sys/stat.h>
21 #include <assert.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <string.h>
29 #include "gmid.h"
31 const char *dir, *cgi;
32 int dirfd;
33 int port;
34 int foreground;
35 int connected_clients;
37 void
38 siginfo_handler(int sig)
39 {
40 (void)sig;
41 }
43 int
44 starts_with(const char *str, const char *prefix)
45 {
46 size_t i;
48 for (i = 0; prefix[i] != '\0'; ++i)
49 if (str[i] != prefix[i])
50 return 0;
51 return 1;
52 }
54 char *
55 url_after_proto(char *url)
56 {
57 char *s;
58 const char *proto = "gemini:";
59 const char *marker = "//";
61 /* a relative URL */
62 if ((s = strstr(url, marker)) == NULL)
63 return url;
65 /*
66 * if a protocol is not specified, gemini should be implied:
67 * this handles the case of //example.com
68 */
69 if (s == url)
70 return s + strlen(marker);
72 if (s - strlen(proto) != url)
73 return NULL;
75 if (!starts_with(url, proto))
76 return NULL;
78 return s + strlen(marker);
79 }
81 char *
82 url_start_of_request(char *url)
83 {
84 char *s, *t;
86 if ((s = url_after_proto(url)) == NULL)
87 return NULL;
89 /* non-absolute URL */
90 if (s == url)
91 return s;
93 if ((t = strstr(s, "/")) == NULL)
94 return s + strlen(s);
95 return t;
96 }
98 int
99 url_trim(struct client *c, char *url)
101 const char *e = "\r\n";
102 char *s;
104 if ((s = strstr(url, e)) == NULL)
105 return 0;
106 s[0] = '\0';
107 s[1] = '\0';
109 if (s[2] != '\0') {
110 LOGE(c, "%s", "request longer than 1024 bytes");
111 return 0;
114 return 1;
117 char *
118 adjust_path(char *path)
120 char *s, *query;
121 size_t len;
123 if ((query = strchr(path, '?')) != NULL) {
124 *query = '\0';
125 query++;
128 /* /.. -> / */
129 len = strlen(path);
130 if (len >= 3) {
131 if (!strcmp(&path[len-3], "/..")) {
132 path[len-2] = '\0';
136 /* if the path is only `..` trim out and exit */
137 if (!strcmp(path, "..")) {
138 path[0] = '\0';
139 return query;
142 /* remove every ../ in the path */
143 while (1) {
144 if ((s = strstr(path, "../")) == NULL)
145 return query;
146 memmove(s, s+3, strlen(s)+1); /* copy also the \0 */
150 int
151 start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
153 char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
154 int len;
155 int ret;
157 client->code = code;
158 client->meta = reason;
159 client->state = S_INITIALIZING;
161 len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
162 assert(len < (int)sizeof(buf));
163 ret = tls_write(client->ctx, buf, len);
164 if (ret == TLS_WANT_POLLIN) {
165 pfd->events = POLLIN;
166 return 0;
169 if (ret == TLS_WANT_POLLOUT) {
170 pfd->events = POLLOUT;
171 return 0;
174 return 1;
177 ssize_t
178 filesize(int fd)
180 ssize_t len;
182 if ((len = lseek(fd, 0, SEEK_END)) == -1)
183 return -1;
184 if (lseek(fd, 0, SEEK_SET) == -1)
185 return -1;
186 return len;
189 const char *
190 path_ext(const char *path)
192 const char *end;
194 end = path + strlen(path)-1; /* the last byte before the NUL */
195 for (; end != path; --end) {
196 if (*end == '.')
197 return end+1;
198 if (*end == '/')
199 break;
202 return NULL;
205 const char *
206 mime(const char *path)
208 const char *ext, *def = "application/octet-stream";
209 struct etm *t;
211 if ((ext = path_ext(path)) == NULL)
212 return def;
214 for (t = filetypes; t->mime != NULL; ++t)
215 if (!strcmp(ext, t->ext))
216 return t->mime;
218 return def;
221 int
222 check_path(struct client *c, const char *path, int *fd)
224 struct stat sb;
226 assert(path != NULL);
227 if ((*fd = openat(dirfd, path,
228 O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
229 return FILE_MISSING;
232 if (fstat(*fd, &sb) == -1) {
233 LOGN(c, "failed stat for %s: %s", path, strerror(errno));
234 return FILE_MISSING;
237 if (S_ISDIR(sb.st_mode))
238 return FILE_DIRECTORY;
240 if (sb.st_mode & S_IXUSR)
241 return FILE_EXECUTABLE;
243 return FILE_EXISTS;
246 /*
247 * the inverse of this algorithm, i.e. starting from the start of the
248 * path + strlen(cgi), and checking if each component, should be
249 * faster. But it's tedious to write. This does the opposite: starts
250 * from the end and strip one component at a time, until either an
251 * executable is found or we emptied the path.
252 */
253 int
254 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
256 char *end;
257 end = strchr(path, '\0');
259 /* NB: assume CGI is enabled and path matches cgi */
261 while (end > path) {
262 /* go up one level. UNIX paths are simple and POSIX
263 * dirname, with its ambiguities on if the given path
264 * is changed or not, gives me headaches. */
265 while (*end != '/')
266 end--;
267 *end = '\0';
269 switch (check_path(c, path, &c->fd)) {
270 case FILE_EXECUTABLE:
271 return start_cgi(path, end+1, query, fds,c);
272 case FILE_MISSING:
273 break;
274 default:
275 goto err;
278 *end = '/';
279 end--;
282 err:
283 if (!start_reply(fds, c, NOT_FOUND, "not found"))
284 return 0;
285 goodbye(fds, c);
286 return 0;
290 int
291 open_file(char *path, char *query, struct pollfd *fds, struct client *c)
293 char fpath[PATHBUF];
295 bzero(fpath, sizeof(fpath));
297 if (*path != '.')
298 fpath[0] = '.';
299 strlcat(fpath, path, PATHBUF);
301 switch (check_path(c, fpath, &c->fd)) {
302 case FILE_EXECUTABLE:
303 /* +2 to skip the ./ */
304 if (cgi != NULL && starts_with(fpath+2, cgi))
305 return start_cgi(fpath, "", query, fds, c);
307 /* fallthrough */
309 case FILE_EXISTS:
310 if ((c->len = filesize(c->fd)) == -1) {
311 LOGE(c, "failed to get file size for %s", fpath);
312 goodbye(fds, c);
313 return 0;
316 if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
317 c->fd, 0)) == MAP_FAILED) {
318 warn("mmap: %s", fpath);
319 goodbye(fds, c);
320 return 0;
322 c->i = c->buf;
323 return start_reply(fds, c, SUCCESS, mime(fpath));
325 case FILE_DIRECTORY:
326 LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
327 close(c->fd);
328 c->fd = -1;
329 send_dir(fpath, fds, c);
330 return 0;
332 case FILE_MISSING:
333 if (cgi != NULL && starts_with(fpath+2, cgi))
334 return check_for_cgi(fpath, query, fds, c);
336 if (!start_reply(fds, c, NOT_FOUND, "not found"))
337 return 0;
338 goodbye(fds, c);
339 return 0;
341 default:
342 /* unreachable */
343 abort();
347 int
348 start_cgi(const char *spath, const char *relpath, const char *query,
349 struct pollfd *fds, struct client *c)
351 pid_t pid;
352 int p[2]; /* read end, write end */
354 if (pipe(p) == -1)
355 goto err;
357 switch (pid = fork()) {
358 case -1:
359 goto err;
361 case 0: { /* child */
362 char *ex, *requri, *portno;
363 char addr[INET_ADDRSTRLEN];
364 char *argv[] = { NULL, NULL, NULL };
366 spath++;
368 close(p[0]);
369 if (dup2(p[1], 1) == -1)
370 goto childerr;
372 if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
373 goto childerr;
375 if (asprintf(&portno, "%d", port) == -1)
376 goto childerr;
378 if (asprintf(&ex, "%s%s", dir, spath+1) == -1)
379 goto childerr;
381 if (asprintf(&requri, "%s%s%s", spath,
382 *relpath == '\0' ? "" : "/",
383 relpath) == -1)
384 goto childerr;
386 argv[0] = argv[1] = ex;
388 /* fix the env */
389 SAFE_SETENV("GATEWAY_INTERFACE", "CGI/1.1");
390 SAFE_SETENV("SERVER_SOFTWARE", "gmid");
391 SAFE_SETENV("SERVER_PORT", portno);
392 /* setenv("SERVER_NAME", "", 1); */
393 SAFE_SETENV("SCRIPT_NAME", spath);
394 SAFE_SETENV("SCRIPT_EXECUTABLE", ex);
395 SAFE_SETENV("REQUEST_URI", requri);
396 SAFE_SETENV("REQUEST_RELATIVE", relpath);
397 SAFE_SETENV("QUERY_STRING", query);
398 SAFE_SETENV("REMOTE_HOST", addr);
399 SAFE_SETENV("REMOTE_ADDR", addr);
400 SAFE_SETENV("DOCUMENT_ROOT", dir);
402 if (tls_peer_cert_provided(c->ctx)) {
403 SAFE_SETENV("AUTH_TYPE", "Certificate");
404 SAFE_SETENV("REMOTE_USER", tls_peer_cert_subject(c->ctx));
405 SAFE_SETENV("TLS_CLIENT_ISSUER", tls_peer_cert_issuer(c->ctx));
406 SAFE_SETENV("TLS_CLIENT_HASH", tls_peer_cert_hash(c->ctx));
409 execvp(ex, argv);
410 goto childerr;
413 default: /* parent */
414 close(p[1]);
415 close(c->fd);
416 c->fd = p[0];
417 c->child = pid;
418 mark_nonblock(c->fd);
419 c->state = S_SENDING;
420 handle_cgi(fds, c);
421 return 0;
424 err:
425 if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
426 return 0;
427 goodbye(fds, c);
428 return 0;
430 childerr:
431 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
432 close(p[1]);
433 _exit(1);
436 void
437 cgi_setpoll_on_child(struct pollfd *fds, struct client *c)
439 int fd;
441 if (c->waiting_on_child)
442 return;
443 c->waiting_on_child = 1;
445 fds->events = POLLIN;
447 fd = fds->fd;
448 fds->fd = c->fd;
449 c->fd = fd;
452 void
453 cgi_setpoll_on_client(struct pollfd *fds, struct client *c)
455 int fd;
457 if (!c->waiting_on_child)
458 return;
459 c->waiting_on_child = 0;
461 fd = fds->fd;
462 fds->fd = c->fd;
463 c->fd = fd;
466 void
467 handle_cgi(struct pollfd *fds, struct client *c)
469 ssize_t r;
471 /* ensure c->fd is the child and fds->fd the client */
472 cgi_setpoll_on_client(fds, c);
474 while (1) {
475 if (c->len == 0) {
476 if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
477 goto end;
478 if (r == -1) {
479 if (errno == EAGAIN || errno == EWOULDBLOCK) {
480 cgi_setpoll_on_child(fds, c);
481 return;
483 goto end;
485 c->len = r;
486 c->off = 0;
489 while (c->len > 0) {
490 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
491 case -1:
492 goto end;
494 case TLS_WANT_POLLOUT:
495 fds->events = POLLOUT;
496 return;
498 case TLS_WANT_POLLIN:
499 fds->events = POLLIN;
500 return;
502 default:
503 c->off += r;
504 c->len -= r;
505 break;
510 end:
511 goodbye(fds, c);
514 void
515 send_file(char *path, char *query, struct pollfd *fds, struct client *c)
517 ssize_t ret, len;
519 if (c->fd == -1) {
520 if (!open_file(path, query, fds, c))
521 return;
522 c->state = S_SENDING;
525 len = (c->buf + c->len) - c->i;
527 while (len > 0) {
528 switch (ret = tls_write(c->ctx, c->i, len)) {
529 case -1:
530 LOGE(c, "tls_write: %s", tls_error(c->ctx));
531 goodbye(fds, c);
532 return;
534 case TLS_WANT_POLLIN:
535 fds->events = POLLIN;
536 return;
538 case TLS_WANT_POLLOUT:
539 fds->events = POLLOUT;
540 return;
542 default:
543 c->i += ret;
544 len -= ret;
545 break;
549 goodbye(fds, c);
552 void
553 send_dir(char *path, struct pollfd *fds, struct client *client)
555 char fpath[PATHBUF];
556 size_t len;
558 bzero(fpath, PATHBUF);
560 if (path[0] != '.')
561 fpath[0] = '.';
563 /* this cannot fail since sizeof(fpath) > maxlen of path */
564 strlcat(fpath, path, PATHBUF);
565 len = strlen(fpath);
567 /* add a trailing / in case. */
568 if (fpath[len-1] != '/') {
569 fpath[len] = '/';
572 strlcat(fpath, "index.gmi", sizeof(fpath));
574 send_file(fpath, NULL, fds, client);
577 void
578 handle(struct pollfd *fds, struct client *client)
580 char buf[GEMINI_URL_LEN];
581 char *path;
582 char *query;
584 switch (client->state) {
585 case S_OPEN:
586 bzero(buf, GEMINI_URL_LEN);
587 switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
588 case -1:
589 LOGE(client, "tls_read: %s", tls_error(client->ctx));
590 goodbye(fds, client);
591 return;
593 case TLS_WANT_POLLIN:
594 fds->events = POLLIN;
595 return;
597 case TLS_WANT_POLLOUT:
598 fds->events = POLLOUT;
599 return;
602 if (!url_trim(client, buf)) {
603 if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
604 return;
605 goodbye(fds, client);
606 return;
609 if ((path = url_start_of_request(buf)) == NULL) {
610 if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
611 return;
612 goodbye(fds, client);
613 return;
616 query = adjust_path(path);
617 LOGI(client, "GET %s%s%s", path,
618 query ? "?" : "",
619 query ? query : "");
621 send_file(path, query, fds, client);
622 break;
624 case S_INITIALIZING:
625 if (!start_reply(fds, client, client->code, client->meta))
626 return;
628 if (client->code != SUCCESS) {
629 /* we don't need a body */
630 goodbye(fds, client);
631 return;
634 client->state = S_SENDING;
636 /* fallthrough */
638 case S_SENDING:
639 if (client->child != -1)
640 handle_cgi(fds, client);
641 else
642 send_file(NULL, NULL, fds, client);
643 break;
645 case S_CLOSING:
646 goodbye(fds, client);
647 break;
649 default:
650 /* unreachable */
651 abort();
655 void
656 mark_nonblock(int fd)
658 int flags;
660 if ((flags = fcntl(fd, F_GETFL)) == -1)
661 FATAL("fcntl(F_GETFL): %s", strerror(errno));
662 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
663 FATAL("fcntl(F_SETFL): %s", strerror(errno));
666 int
667 make_socket(int port, int family)
669 int sock, v;
670 struct sockaddr_in addr4;
671 struct sockaddr_in6 addr6;
672 struct sockaddr *addr;
673 socklen_t len;
675 switch (family) {
676 case AF_INET:
677 bzero(&addr4, sizeof(addr4));
678 addr4.sin_family = family;
679 addr4.sin_port = htons(port);
680 addr4.sin_addr.s_addr = INADDR_ANY;
681 addr = (struct sockaddr*)&addr4;
682 len = sizeof(addr4);
683 break;
685 case AF_INET6:
686 bzero(&addr6, sizeof(addr6));
687 addr6.sin6_family = AF_INET6;
688 addr6.sin6_port = htons(port);
689 addr6.sin6_addr = in6addr_any;
690 addr = (struct sockaddr*)&addr6;
691 len = sizeof(addr6);
692 break;
694 default:
695 /* unreachable */
696 abort();
699 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
700 FATAL("socket: %s", strerror(errno));
702 v = 1;
703 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
704 FATAL("setsockopt(SO_REUSEADDR): %s", strerror(errno));
706 v = 1;
707 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
708 FATAL("setsockopt(SO_REUSEPORT): %s", strerror(errno));
710 mark_nonblock(sock);
712 if (bind(sock, addr, len) == -1)
713 FATAL("bind: %s", strerror(errno));
715 if (listen(sock, 16) == -1)
716 FATAL("listen: %s", strerror(errno));
718 return sock;
721 void
722 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
724 int i, fd;
725 struct sockaddr_in addr;
726 socklen_t len;
728 len = sizeof(addr);
729 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
730 if (errno == EWOULDBLOCK)
731 return;
732 FATAL("accept: %s", strerror(errno));
735 mark_nonblock(fd);
737 for (i = 0; i < MAX_USERS; ++i) {
738 if (fds[i].fd == -1) {
739 bzero(&clients[i], sizeof(struct client));
740 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
741 break; /* goodbye fd! */
743 fds[i].fd = fd;
744 fds[i].events = POLLIN;
746 clients[i].state = S_OPEN;
747 clients[i].fd = -1;
748 clients[i].child = -1;
749 clients[i].buf = MAP_FAILED;
750 clients[i].af = AF_INET;
751 clients[i].addr = addr.sin_addr;
753 connected_clients++;
754 return;
758 close(fd);
761 void
762 goodbye(struct pollfd *pfd, struct client *c)
764 ssize_t ret;
766 c->state = S_CLOSING;
768 ret = tls_close(c->ctx);
769 if (ret == TLS_WANT_POLLIN) {
770 pfd->events = POLLIN;
771 return;
773 if (ret == TLS_WANT_POLLOUT) {
774 pfd->events = POLLOUT;
775 return;
778 connected_clients--;
780 tls_free(c->ctx);
781 c->ctx = NULL;
783 if (c->buf != MAP_FAILED)
784 munmap(c->buf, c->len);
786 if (c->fd != -1)
787 close(c->fd);
789 close(pfd->fd);
790 pfd->fd = -1;
793 void
794 loop(struct tls *ctx, int sock)
796 int i, todo;
797 struct client clients[MAX_USERS];
798 struct pollfd fds[MAX_USERS];
800 for (i = 0; i < MAX_USERS; ++i) {
801 fds[i].fd = -1;
802 fds[i].events = POLLIN;
803 bzero(&clients[i], sizeof(struct client));
806 fds[0].fd = sock;
808 for (;;) {
809 if ((todo = poll(fds, MAX_USERS, INFTIM)) == -1) {
810 if (errno == EINTR) {
811 warnx("connected clients: %d",
812 connected_clients);
813 continue;
815 FATAL("poll: %s", strerror(errno));
818 for (i = 0; i < MAX_USERS; i++) {
819 assert(i < MAX_USERS);
821 if (fds[i].revents == 0)
822 continue;
824 if (fds[i].revents & (POLLERR|POLLNVAL))
825 FATAL("bad fd %d: %s", fds[i].fd,
826 strerror(errno));
828 if (fds[i].revents & POLLHUP) {
829 /* fds[i] may be the fd of the stdin
830 * of a cgi script that has exited. */
831 if (!clients[i].waiting_on_child) {
832 goodbye(&fds[i], &clients[i]);
833 continue;
837 todo--;
839 if (i == 0) { /* new client */
840 do_accept(sock, ctx, fds, clients);
841 continue;
844 handle(&fds[i], &clients[i]);
849 char *
850 absolutify_path(const char *path)
852 char *wd, *r;
854 if (*path == '/')
855 return strdup(path);
857 wd = getwd(NULL);
858 if (asprintf(&r, "%s/%s", wd, path) == -1)
859 err(1, "asprintf");
860 free(wd);
861 return r;
864 void
865 usage(const char *me)
867 fprintf(stderr,
868 "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] "
869 "[-l logfile] [-p port] [-x cgi-bin]\n",
870 me);
873 int
874 main(int argc, char **argv)
876 const char *cert = "cert.pem", *key = "key.pem";
877 struct tls *ctx = NULL;
878 struct tls_config *conf;
879 int sock, ch;
881 signal(SIGPIPE, SIG_IGN);
882 signal(SIGCHLD, SIG_IGN);
884 #ifdef SIGINFO
885 signal(SIGINFO, siginfo_handler);
886 #endif
887 signal(SIGUSR2, siginfo_handler);
889 connected_clients = 0;
891 if ((dir = absolutify_path("docs")) == NULL)
892 err(1, "absolutify_path");
894 cgi = NULL;
895 port = 1965;
896 foreground = 0;
898 while ((ch = getopt(argc, argv, "c:d:fhk:p:x:")) != -1) {
899 switch (ch) {
900 case 'c':
901 cert = optarg;
902 break;
904 case 'd':
905 free((char*)dir);
906 if ((dir = absolutify_path(optarg)) == NULL)
907 err(1, "absolutify_path");
908 break;
910 case 'f':
911 foreground = 1;
912 break;
914 case 'h':
915 usage(*argv);
916 return 0;
918 case 'k':
919 key = optarg;
920 break;
922 case 'p': {
923 char *ep;
924 long lval;
926 errno = 0;
927 lval = strtol(optarg, &ep, 10);
928 if (optarg[0] == '\0' || *ep != '\0')
929 err(1, "not a number: %s", optarg);
930 if (lval < 0 || lval > UINT16_MAX)
931 err(1, "port number out of range: %s", optarg);
932 port = lval;
933 break;
936 case 'x':
937 cgi = optarg;
938 break;
940 default:
941 usage(*argv);
942 return 1;
946 if ((conf = tls_config_new()) == NULL)
947 err(1, "tls_config_new");
949 /* optionally accept client certs, but don't try to verify them */
950 tls_config_verify_client_optional(conf);
951 tls_config_insecure_noverifycert(conf);
953 if (tls_config_set_protocols(conf,
954 TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
955 err(1, "tls_config_set_protocols");
957 if (tls_config_set_cert_file(conf, cert) == -1)
958 err(1, "tls_config_set_cert_file: %s", cert);
960 if (tls_config_set_key_file(conf, key) == -1)
961 err(1, "tls_config_set_key_file: %s", key);
963 if ((ctx = tls_server()) == NULL)
964 err(1, "tls_server");
966 if (tls_configure(ctx, conf) == -1)
967 errx(1, "tls_configure: %s", tls_error(ctx));
969 sock = make_socket(port, AF_INET);
971 if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
972 err(1, "open: %s", dir);
974 if (!foreground && daemon(0, 1) == -1)
975 exit(1);
977 if (cgi != NULL) {
978 if (unveil(dir, "rx") == -1)
979 err(1, "unveil");
980 if (pledge("stdio rpath inet proc exec", NULL) == -1)
981 err(1, "pledge");
982 } else {
983 if (unveil(dir, "r") == -1)
984 err(1, "unveil");
985 if (pledge("stdio rpath inet", NULL) == -1)
986 err(1, "pledge");
989 loop(ctx, sock);
991 close(sock);
992 tls_free(ctx);
993 tls_config_free(conf);