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 <stdarg.h>
28 #include <string.h>
30 #include "gmid.h"
32 #define LOGE(c, fmt, ...) logs(LOG_ERR, c, fmt, __VA_ARGS__)
33 #define LOGN(c, fmt, ...) logs(LOG_NOTICE, c, fmt, __VA_ARGS__)
34 #define LOGI(c, fmt, ...) logs(LOG_INFO, c, fmt, __VA_ARGS__)
35 #define LOGD(c, fmt, ...) logs(LOG_DEBUG, c, fmt, __VA_ARGS__)
37 const char *dir, *cgi;
38 int dirfd;
39 int port;
40 int foreground;
41 int connected_clients;
43 struct etm { /* file extension to mime */
44 const char *mime;
45 const char *ext;
46 } filetypes[] = {
47 {"application/pdf", "pdf"},
49 {"image/gif", "gif"},
50 {"image/jpeg", "jpg"},
51 {"image/jpeg", "jpeg"},
52 {"image/png", "png"},
53 {"image/svg+xml", "svg"},
55 {"text/gemini", "gemini"},
56 {"text/gemini", "gmi"},
57 {"text/markdown", "markdown"},
58 {"text/markdown", "md"},
59 {"text/plain", "txt"},
60 {"text/xml", "xml"},
62 {NULL, NULL}
63 };
65 static inline void
66 safe_setenv(const char *name, const char *val)
67 {
68 if (val == NULL)
69 val = "";
70 setenv(name, val, 1);
71 }
73 __attribute__ ((format (printf, 1, 2)))
74 static inline void __dead
75 fatal(const char *fmt, ...)
76 {
77 va_list ap;
79 va_start(ap, fmt);
81 if (foreground) {
82 vfprintf(stderr, fmt, ap);
83 fprintf(stderr, "\n");
84 } else
85 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
87 va_end(ap);
88 exit(1);
89 }
91 __attribute__ ((format (printf, 3, 4)))
92 static inline void
93 logs(int priority, struct client *c, const char *fmt, ...)
94 {
95 char buf[INET_ADDRSTRLEN];
96 va_list ap;
98 va_start(ap, fmt);
99 priority |= LOG_DAEMON;
101 if (inet_ntop(c->af, &c->addr, buf, sizeof(buf)) == NULL)
102 fatal("inet_ntop: %s", strerror(errno));
104 if (foreground) {
105 vfprintf(stderr, fmt, ap);
106 fprintf(stderr, "\n");
107 } else
108 vsyslog(priority, fmt, ap);
110 va_end(ap);
113 void
114 sig_handler(int sig)
116 (void)sig;
119 int
120 starts_with(const char *str, const char *prefix)
122 size_t i;
124 for (i = 0; prefix[i] != '\0'; ++i)
125 if (str[i] != prefix[i])
126 return 0;
127 return 1;
130 int
131 start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
133 char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
134 int len;
135 int ret;
137 client->code = code;
138 client->meta = reason;
139 client->state = S_INITIALIZING;
141 len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
142 assert(len < (int)sizeof(buf));
143 ret = tls_write(client->ctx, buf, len);
144 if (ret == TLS_WANT_POLLIN) {
145 pfd->events = POLLIN;
146 return 0;
149 if (ret == TLS_WANT_POLLOUT) {
150 pfd->events = POLLOUT;
151 return 0;
154 return 1;
157 ssize_t
158 filesize(int fd)
160 ssize_t len;
162 if ((len = lseek(fd, 0, SEEK_END)) == -1)
163 return -1;
164 if (lseek(fd, 0, SEEK_SET) == -1)
165 return -1;
166 return len;
169 const char *
170 path_ext(const char *path)
172 const char *end;
174 end = path + strlen(path)-1; /* the last byte before the NUL */
175 for (; end != path; --end) {
176 if (*end == '.')
177 return end+1;
178 if (*end == '/')
179 break;
182 return NULL;
185 const char *
186 mime(const char *path)
188 const char *ext, *def = "application/octet-stream";
189 struct etm *t;
191 if ((ext = path_ext(path)) == NULL)
192 return def;
194 for (t = filetypes; t->mime != NULL; ++t)
195 if (!strcmp(ext, t->ext))
196 return t->mime;
198 return def;
201 int
202 check_path(struct client *c, const char *path, int *fd)
204 struct stat sb;
206 assert(path != NULL);
207 if ((*fd = openat(dirfd, *path ? path : ".",
208 O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
209 return FILE_MISSING;
212 if (fstat(*fd, &sb) == -1) {
213 LOGN(c, "failed stat for %s: %s", path, strerror(errno));
214 return FILE_MISSING;
217 if (S_ISDIR(sb.st_mode))
218 return FILE_DIRECTORY;
220 if (sb.st_mode & S_IXUSR)
221 return FILE_EXECUTABLE;
223 return FILE_EXISTS;
226 /*
227 * the inverse of this algorithm, i.e. starting from the start of the
228 * path + strlen(cgi), and checking if each component, should be
229 * faster. But it's tedious to write. This does the opposite: starts
230 * from the end and strip one component at a time, until either an
231 * executable is found or we emptied the path.
232 */
233 int
234 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
236 char *end;
237 end = strchr(path, '\0');
239 /* NB: assume CGI is enabled and path matches cgi */
241 while (end > path) {
242 /* go up one level. UNIX paths are simple and POSIX
243 * dirname, with its ambiguities on if the given path
244 * is changed or not, gives me headaches. */
245 while (*end != '/')
246 end--;
247 *end = '\0';
249 switch (check_path(c, path, &c->fd)) {
250 case FILE_EXECUTABLE:
251 return start_cgi(path, end+1, query, fds,c);
252 case FILE_MISSING:
253 break;
254 default:
255 goto err;
258 *end = '/';
259 end--;
262 err:
263 if (!start_reply(fds, c, NOT_FOUND, "not found"))
264 return 0;
265 goodbye(fds, c);
266 return 0;
270 int
271 open_file(char *fpath, char *query, struct pollfd *fds, struct client *c)
273 switch (check_path(c, fpath, &c->fd)) {
274 case FILE_EXECUTABLE:
275 if (cgi != NULL && starts_with(fpath, cgi))
276 return start_cgi(fpath, "", query, fds, c);
278 /* fallthrough */
280 case FILE_EXISTS:
281 if ((c->len = filesize(c->fd)) == -1) {
282 LOGE(c, "failed to get file size for %s", fpath);
283 goodbye(fds, c);
284 return 0;
287 if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
288 c->fd, 0)) == MAP_FAILED) {
289 warn("mmap: %s", fpath);
290 goodbye(fds, c);
291 return 0;
293 c->i = c->buf;
294 return start_reply(fds, c, SUCCESS, mime(fpath));
296 case FILE_DIRECTORY:
297 LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
298 close(c->fd);
299 c->fd = -1;
300 send_dir(fpath, fds, c);
301 return 0;
303 case FILE_MISSING:
304 if (cgi != NULL && starts_with(fpath, cgi))
305 return check_for_cgi(fpath, query, fds, c);
307 if (!start_reply(fds, c, NOT_FOUND, "not found"))
308 return 0;
309 goodbye(fds, c);
310 return 0;
312 default:
313 /* unreachable */
314 abort();
318 int
319 start_cgi(const char *spath, const char *relpath, const char *query,
320 struct pollfd *fds, struct client *c)
322 pid_t pid;
323 int p[2]; /* read end, write end */
325 if (pipe(p) == -1)
326 goto err;
328 switch (pid = fork()) {
329 case -1:
330 goto err;
332 case 0: { /* child */
333 char *ex, *requri, *portno;
334 char addr[INET_ADDRSTRLEN];
335 char *argv[] = { NULL, NULL, NULL };
337 close(p[0]);
338 if (dup2(p[1], 1) == -1)
339 goto childerr;
341 if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
342 goto childerr;
344 if (asprintf(&portno, "%d", port) == -1)
345 goto childerr;
347 if (asprintf(&ex, "%s/%s", dir, spath) == -1)
348 goto childerr;
350 if (asprintf(&requri, "%s%s%s", spath,
351 *relpath == '\0' ? "" : "/",
352 relpath) == -1)
353 goto childerr;
355 argv[0] = argv[1] = ex;
357 /* fix the env */
358 safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
359 safe_setenv("SERVER_SOFTWARE", "gmid");
360 safe_setenv("SERVER_PORT", portno);
361 /* setenv("SERVER_NAME", "", 1); */
362 safe_setenv("SCRIPT_NAME", spath);
363 safe_setenv("SCRIPT_EXECUTABLE", ex);
364 safe_setenv("REQUEST_URI", requri);
365 safe_setenv("REQUEST_RELATIVE", relpath);
366 safe_setenv("QUERY_STRING", query);
367 safe_setenv("REMOTE_HOST", addr);
368 safe_setenv("REMOTE_ADDR", addr);
369 safe_setenv("DOCUMENT_ROOT", dir);
371 if (tls_peer_cert_provided(c->ctx)) {
372 safe_setenv("AUTH_TYPE", "Certificate");
373 safe_setenv("REMOTE_USER", tls_peer_cert_subject(c->ctx));
374 safe_setenv("TLS_CLIENT_ISSUER", tls_peer_cert_issuer(c->ctx));
375 safe_setenv("TLS_CLIENT_HASH", tls_peer_cert_hash(c->ctx));
378 execvp(ex, argv);
379 goto childerr;
382 default: /* parent */
383 close(p[1]);
384 close(c->fd);
385 c->fd = p[0];
386 c->child = pid;
387 mark_nonblock(c->fd);
388 c->state = S_SENDING;
389 handle_cgi(fds, c);
390 return 0;
393 err:
394 if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
395 return 0;
396 goodbye(fds, c);
397 return 0;
399 childerr:
400 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
401 close(p[1]);
402 _exit(1);
405 void
406 cgi_poll_on_child(struct pollfd *fds, struct client *c)
408 int fd;
410 if (c->waiting_on_child)
411 return;
412 c->waiting_on_child = 1;
414 fds->events = POLLIN;
416 fd = fds->fd;
417 fds->fd = c->fd;
418 c->fd = fd;
421 void
422 cgi_poll_on_client(struct pollfd *fds, struct client *c)
424 int fd;
426 if (!c->waiting_on_child)
427 return;
428 c->waiting_on_child = 0;
430 fd = fds->fd;
431 fds->fd = c->fd;
432 c->fd = fd;
435 void
436 handle_cgi(struct pollfd *fds, struct client *c)
438 ssize_t r;
440 /* ensure c->fd is the child and fds->fd the client */
441 cgi_poll_on_client(fds, c);
443 while (1) {
444 if (c->len == 0) {
445 if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
446 goto end;
447 if (r == -1) {
448 if (errno == EAGAIN || errno == EWOULDBLOCK) {
449 cgi_poll_on_child(fds, c);
450 return;
452 goto end;
454 c->len = r;
455 c->off = 0;
458 while (c->len > 0) {
459 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
460 case -1:
461 goto end;
463 case TLS_WANT_POLLOUT:
464 fds->events = POLLOUT;
465 return;
467 case TLS_WANT_POLLIN:
468 fds->events = POLLIN;
469 return;
471 default:
472 c->off += r;
473 c->len -= r;
474 break;
479 end:
480 goodbye(fds, c);
483 void
484 send_file(char *path, char *query, struct pollfd *fds, struct client *c)
486 ssize_t ret, len;
488 if (c->fd == -1) {
489 if (!open_file(path, query, fds, c))
490 return;
491 c->state = S_SENDING;
494 len = (c->buf + c->len) - c->i;
496 while (len > 0) {
497 switch (ret = tls_write(c->ctx, c->i, len)) {
498 case -1:
499 LOGE(c, "tls_write: %s", tls_error(c->ctx));
500 goodbye(fds, c);
501 return;
503 case TLS_WANT_POLLIN:
504 fds->events = POLLIN;
505 return;
507 case TLS_WANT_POLLOUT:
508 fds->events = POLLOUT;
509 return;
511 default:
512 c->i += ret;
513 len -= ret;
514 break;
518 goodbye(fds, c);
521 void
522 send_dir(char *path, struct pollfd *fds, struct client *client)
524 char fpath[PATHBUF];
525 size_t len;
527 bzero(fpath, PATHBUF);
529 if (path[0] != '.')
530 fpath[0] = '.';
532 /* this cannot fail since sizeof(fpath) > maxlen of path */
533 strlcat(fpath, path, PATHBUF);
534 len = strlen(fpath);
536 /* add a trailing / in case. */
537 if (fpath[len-1] != '/') {
538 fpath[len] = '/';
541 strlcat(fpath, "index.gmi", sizeof(fpath));
543 send_file(fpath, NULL, fds, client);
546 void
547 handle(struct pollfd *fds, struct client *client)
549 char buf[GEMINI_URL_LEN];
550 const char *parse_err;
551 struct uri uri;
553 switch (client->state) {
554 case S_OPEN:
555 bzero(buf, GEMINI_URL_LEN);
556 switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
557 case -1:
558 LOGE(client, "tls_read: %s", tls_error(client->ctx));
559 goodbye(fds, client);
560 return;
562 case TLS_WANT_POLLIN:
563 fds->events = POLLIN;
564 return;
566 case TLS_WANT_POLLOUT:
567 fds->events = POLLOUT;
568 return;
571 parse_err = "invalid request";
572 if (!trim_req_uri(buf) || !parse_uri(buf, &uri, &parse_err)) {
573 if (!start_reply(fds, client, BAD_REQUEST, parse_err))
574 return;
575 goodbye(fds, client);
576 return;
579 LOGI(client, "GET %s%s%s",
580 *uri.path ? uri.path : "/",
581 *uri.query ? "?" : "",
582 *uri.query ? uri.query : "");
584 send_file(uri.path, uri.query, fds, client);
585 break;
587 case S_INITIALIZING:
588 if (!start_reply(fds, client, client->code, client->meta))
589 return;
591 if (client->code != SUCCESS) {
592 /* we don't need a body */
593 goodbye(fds, client);
594 return;
597 client->state = S_SENDING;
599 /* fallthrough */
601 case S_SENDING:
602 if (client->child != -1)
603 handle_cgi(fds, client);
604 else
605 send_file(NULL, NULL, fds, client);
606 break;
608 case S_CLOSING:
609 goodbye(fds, client);
610 break;
612 default:
613 /* unreachable */
614 abort();
618 void
619 mark_nonblock(int fd)
621 int flags;
623 if ((flags = fcntl(fd, F_GETFL)) == -1)
624 fatal("fcntl(F_GETFL): %s", strerror(errno));
625 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
626 fatal("fcntl(F_SETFL): %s", strerror(errno));
629 int
630 make_socket(int port, int family)
632 int sock, v;
633 struct sockaddr_in addr4;
634 struct sockaddr_in6 addr6;
635 struct sockaddr *addr;
636 socklen_t len;
638 switch (family) {
639 case AF_INET:
640 bzero(&addr4, sizeof(addr4));
641 addr4.sin_family = family;
642 addr4.sin_port = htons(port);
643 addr4.sin_addr.s_addr = INADDR_ANY;
644 addr = (struct sockaddr*)&addr4;
645 len = sizeof(addr4);
646 break;
648 case AF_INET6:
649 bzero(&addr6, sizeof(addr6));
650 addr6.sin6_family = AF_INET6;
651 addr6.sin6_port = htons(port);
652 addr6.sin6_addr = in6addr_any;
653 addr = (struct sockaddr*)&addr6;
654 len = sizeof(addr6);
655 break;
657 default:
658 /* unreachable */
659 abort();
662 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
663 fatal("socket: %s", strerror(errno));
665 v = 1;
666 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
667 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
669 v = 1;
670 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
671 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
673 mark_nonblock(sock);
675 if (bind(sock, addr, len) == -1)
676 fatal("bind: %s", strerror(errno));
678 if (listen(sock, 16) == -1)
679 fatal("listen: %s", strerror(errno));
681 return sock;
684 void
685 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
687 int i, fd;
688 struct sockaddr_in addr;
689 socklen_t len;
691 len = sizeof(addr);
692 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
693 if (errno == EWOULDBLOCK)
694 return;
695 fatal("accept: %s", strerror(errno));
698 mark_nonblock(fd);
700 for (i = 0; i < MAX_USERS; ++i) {
701 if (fds[i].fd == -1) {
702 bzero(&clients[i], sizeof(struct client));
703 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
704 break; /* goodbye fd! */
706 fds[i].fd = fd;
707 fds[i].events = POLLIN;
709 clients[i].state = S_OPEN;
710 clients[i].fd = -1;
711 clients[i].child = -1;
712 clients[i].buf = MAP_FAILED;
713 clients[i].af = AF_INET;
714 clients[i].addr = addr.sin_addr;
716 connected_clients++;
717 return;
721 close(fd);
724 void
725 goodbye(struct pollfd *pfd, struct client *c)
727 ssize_t ret;
729 c->state = S_CLOSING;
731 ret = tls_close(c->ctx);
732 if (ret == TLS_WANT_POLLIN) {
733 pfd->events = POLLIN;
734 return;
736 if (ret == TLS_WANT_POLLOUT) {
737 pfd->events = POLLOUT;
738 return;
741 connected_clients--;
743 tls_free(c->ctx);
744 c->ctx = NULL;
746 if (c->buf != MAP_FAILED)
747 munmap(c->buf, c->len);
749 if (c->fd != -1)
750 close(c->fd);
752 close(pfd->fd);
753 pfd->fd = -1;
756 void
757 loop(struct tls *ctx, int sock)
759 int i;
760 struct client clients[MAX_USERS];
761 struct pollfd fds[MAX_USERS];
763 for (i = 0; i < MAX_USERS; ++i) {
764 fds[i].fd = -1;
765 fds[i].events = POLLIN;
766 bzero(&clients[i], sizeof(struct client));
769 fds[0].fd = sock;
771 for (;;) {
772 if (poll(fds, MAX_USERS, INFTIM) == -1) {
773 if (errno == EINTR) {
774 warnx("connected clients: %d",
775 connected_clients);
776 continue;
778 fatal("poll: %s", strerror(errno));
781 for (i = 0; i < MAX_USERS; i++) {
782 if (fds[i].revents == 0)
783 continue;
785 if (fds[i].revents & (POLLERR|POLLNVAL))
786 fatal("bad fd %d: %s", fds[i].fd,
787 strerror(errno));
789 if (fds[i].revents & POLLHUP) {
790 /* fds[i] may be the fd of the stdin
791 * of a cgi script that has exited. */
792 if (!clients[i].waiting_on_child) {
793 goodbye(&fds[i], &clients[i]);
794 continue;
798 if (i == 0) { /* new client */
799 do_accept(sock, ctx, fds, clients);
800 continue;
803 handle(&fds[i], &clients[i]);
808 char *
809 absolutify_path(const char *path)
811 char *wd, *r;
813 if (*path == '/')
814 return strdup(path);
816 wd = getwd(NULL);
817 if (asprintf(&r, "%s/%s", wd, path) == -1)
818 err(1, "asprintf");
819 free(wd);
820 return r;
823 void
824 usage(const char *me)
826 fprintf(stderr,
827 "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] "
828 "[-l logfile] [-p port] [-x cgi-bin]\n",
829 me);
832 int
833 main(int argc, char **argv)
835 const char *cert = "cert.pem", *key = "key.pem";
836 struct tls *ctx = NULL;
837 struct tls_config *conf;
838 int sock, ch;
839 connected_clients = 0;
841 if ((dir = absolutify_path("docs")) == NULL)
842 err(1, "absolutify_path");
844 cgi = NULL;
845 port = 1965;
846 foreground = 0;
848 while ((ch = getopt(argc, argv, "c:d:fhk:p:x:")) != -1) {
849 switch (ch) {
850 case 'c':
851 cert = optarg;
852 break;
854 case 'd':
855 free((char*)dir);
856 if ((dir = absolutify_path(optarg)) == NULL)
857 err(1, "absolutify_path");
858 break;
860 case 'f':
861 foreground = 1;
862 break;
864 case 'h':
865 usage(*argv);
866 return 0;
868 case 'k':
869 key = optarg;
870 break;
872 case 'p': {
873 char *ep;
874 long lval;
876 errno = 0;
877 lval = strtol(optarg, &ep, 10);
878 if (optarg[0] == '\0' || *ep != '\0')
879 err(1, "not a number: %s", optarg);
880 if (lval < 0 || lval > UINT16_MAX)
881 err(1, "port number out of range: %s", optarg);
882 port = lval;
883 break;
886 case 'x':
887 cgi = optarg;
888 break;
890 default:
891 usage(*argv);
892 return 1;
896 signal(SIGPIPE, SIG_IGN);
897 signal(SIGCHLD, SIG_IGN);
899 #ifdef SIGINFO
900 signal(SIGINFO, sig_handler);
901 #endif
902 signal(SIGUSR2, sig_handler);
904 if (!foreground)
905 signal(SIGHUP, SIG_IGN);
907 if ((conf = tls_config_new()) == NULL)
908 err(1, "tls_config_new");
910 /* optionally accept client certs, but don't try to verify them */
911 tls_config_verify_client_optional(conf);
912 tls_config_insecure_noverifycert(conf);
914 if (tls_config_set_protocols(conf,
915 TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
916 err(1, "tls_config_set_protocols");
918 if (tls_config_set_cert_file(conf, cert) == -1)
919 err(1, "tls_config_set_cert_file: %s", cert);
921 if (tls_config_set_key_file(conf, key) == -1)
922 err(1, "tls_config_set_key_file: %s", key);
924 if ((ctx = tls_server()) == NULL)
925 err(1, "tls_server");
927 if (tls_configure(ctx, conf) == -1)
928 errx(1, "tls_configure: %s", tls_error(ctx));
930 sock = make_socket(port, AF_INET);
932 if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
933 err(1, "open: %s", dir);
935 if (!foreground && daemon(0, 1) == -1)
936 exit(1);
938 if (unveil(dir, "rx") == -1)
939 err(1, "unveil");
941 if (pledge("stdio rpath inet proc exec", NULL) == -1)
942 err(1, "pledge");
944 /* drop proc and exec if cgi isn't enabled */
945 if (cgi == NULL && pledge("stdio rpath inet", NULL) == -1)
946 err(1, "pledge");
948 loop(ctx, sock);
950 close(sock);
951 tls_free(ctx);
952 tls_config_free(conf);