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 #define LOG(priority, c, fmt, ...) \
32 do { \
33 char buf[INET_ADDRSTRLEN]; \
34 if (inet_ntop((c)->af, &(c)->addr, \
35 buf, sizeof(buf)) == NULL) \
36 FATAL("inet_ntop: %s", strerror(errno)); \
37 if (foreground) \
38 fprintf(stderr, \
39 "%s " fmt "\n", buf, __VA_ARGS__); \
40 else \
41 syslog((priority) | LOG_DAEMON, \
42 "%s " fmt, buf, __VA_ARGS__); \
43 } while (0)
45 #define LOGE(c, fmt, ...) LOG(LOG_ERR, c, fmt, __VA_ARGS__)
46 #define LOGN(c, fmt, ...) LOG(LOG_NOTICE, c, fmt, __VA_ARGS__)
47 #define LOGI(c, fmt, ...) LOG(LOG_INFO, c, fmt, __VA_ARGS__)
48 #define LOGD(c, fmt, ...) LOG(LOG_DEBUG, c, fmt, __VA_ARGS__)
50 #define FATAL(fmt, ...) \
51 do { \
52 if (foreground) \
53 fprintf(stderr, fmt "\n", __VA_ARGS__); \
54 else \
55 syslog(LOG_DAEMON | LOG_CRIT, \
56 fmt, __VA_ARGS__); \
57 exit(1); \
58 } while (0)
60 const char *dir, *cgi;
61 int dirfd;
62 int port;
63 int foreground;
64 int connected_clients;
66 struct etm { /* file extension to mime */
67 const char *mime;
68 const char *ext;
69 } filetypes[] = {
70 {"application/pdf", "pdf"},
72 {"image/gif", "gif"},
73 {"image/jpeg", "jpg"},
74 {"image/jpeg", "jpeg"},
75 {"image/png", "png"},
76 {"image/svg+xml", "svg"},
78 {"text/gemini", "gemini"},
79 {"text/gemini", "gmi"},
80 {"text/markdown", "markdown"},
81 {"text/markdown", "md"},
82 {"text/plain", "txt"},
83 {"text/xml", "xml"},
85 {NULL, NULL}
86 };
88 static inline void
89 safe_setenv(const char *name, const char *val)
90 {
91 if (val == NULL)
92 val = "";
93 setenv(name, val, 1);
94 }
96 void
97 sig_handler(int sig)
98 {
99 (void)sig;
102 int
103 starts_with(const char *str, const char *prefix)
105 size_t i;
107 for (i = 0; prefix[i] != '\0'; ++i)
108 if (str[i] != prefix[i])
109 return 0;
110 return 1;
113 int
114 start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
116 char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
117 int len;
118 int ret;
120 client->code = code;
121 client->meta = reason;
122 client->state = S_INITIALIZING;
124 len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
125 assert(len < (int)sizeof(buf));
126 ret = tls_write(client->ctx, buf, len);
127 if (ret == TLS_WANT_POLLIN) {
128 pfd->events = POLLIN;
129 return 0;
132 if (ret == TLS_WANT_POLLOUT) {
133 pfd->events = POLLOUT;
134 return 0;
137 return 1;
140 ssize_t
141 filesize(int fd)
143 ssize_t len;
145 if ((len = lseek(fd, 0, SEEK_END)) == -1)
146 return -1;
147 if (lseek(fd, 0, SEEK_SET) == -1)
148 return -1;
149 return len;
152 const char *
153 path_ext(const char *path)
155 const char *end;
157 end = path + strlen(path)-1; /* the last byte before the NUL */
158 for (; end != path; --end) {
159 if (*end == '.')
160 return end+1;
161 if (*end == '/')
162 break;
165 return NULL;
168 const char *
169 mime(const char *path)
171 const char *ext, *def = "application/octet-stream";
172 struct etm *t;
174 if ((ext = path_ext(path)) == NULL)
175 return def;
177 for (t = filetypes; t->mime != NULL; ++t)
178 if (!strcmp(ext, t->ext))
179 return t->mime;
181 return def;
184 int
185 check_path(struct client *c, const char *path, int *fd)
187 struct stat sb;
189 assert(path != NULL);
190 if ((*fd = openat(dirfd, *path ? path : ".",
191 O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
192 return FILE_MISSING;
195 if (fstat(*fd, &sb) == -1) {
196 LOGN(c, "failed stat for %s: %s", path, strerror(errno));
197 return FILE_MISSING;
200 if (S_ISDIR(sb.st_mode))
201 return FILE_DIRECTORY;
203 if (sb.st_mode & S_IXUSR)
204 return FILE_EXECUTABLE;
206 return FILE_EXISTS;
209 /*
210 * the inverse of this algorithm, i.e. starting from the start of the
211 * path + strlen(cgi), and checking if each component, should be
212 * faster. But it's tedious to write. This does the opposite: starts
213 * from the end and strip one component at a time, until either an
214 * executable is found or we emptied the path.
215 */
216 int
217 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
219 char *end;
220 end = strchr(path, '\0');
222 /* NB: assume CGI is enabled and path matches cgi */
224 while (end > path) {
225 /* go up one level. UNIX paths are simple and POSIX
226 * dirname, with its ambiguities on if the given path
227 * is changed or not, gives me headaches. */
228 while (*end != '/')
229 end--;
230 *end = '\0';
232 switch (check_path(c, path, &c->fd)) {
233 case FILE_EXECUTABLE:
234 return start_cgi(path, end+1, query, fds,c);
235 case FILE_MISSING:
236 break;
237 default:
238 goto err;
241 *end = '/';
242 end--;
245 err:
246 if (!start_reply(fds, c, NOT_FOUND, "not found"))
247 return 0;
248 goodbye(fds, c);
249 return 0;
253 int
254 open_file(char *fpath, char *query, struct pollfd *fds, struct client *c)
256 switch (check_path(c, fpath, &c->fd)) {
257 case FILE_EXECUTABLE:
258 if (cgi != NULL && starts_with(fpath, cgi))
259 return start_cgi(fpath, "", query, fds, c);
261 /* fallthrough */
263 case FILE_EXISTS:
264 if ((c->len = filesize(c->fd)) == -1) {
265 LOGE(c, "failed to get file size for %s", fpath);
266 goodbye(fds, c);
267 return 0;
270 if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
271 c->fd, 0)) == MAP_FAILED) {
272 warn("mmap: %s", fpath);
273 goodbye(fds, c);
274 return 0;
276 c->i = c->buf;
277 return start_reply(fds, c, SUCCESS, mime(fpath));
279 case FILE_DIRECTORY:
280 LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
281 close(c->fd);
282 c->fd = -1;
283 send_dir(fpath, fds, c);
284 return 0;
286 case FILE_MISSING:
287 if (cgi != NULL && starts_with(fpath, cgi))
288 return check_for_cgi(fpath, query, fds, c);
290 if (!start_reply(fds, c, NOT_FOUND, "not found"))
291 return 0;
292 goodbye(fds, c);
293 return 0;
295 default:
296 /* unreachable */
297 abort();
301 int
302 start_cgi(const char *spath, const char *relpath, const char *query,
303 struct pollfd *fds, struct client *c)
305 pid_t pid;
306 int p[2]; /* read end, write end */
308 if (pipe(p) == -1)
309 goto err;
311 switch (pid = fork()) {
312 case -1:
313 goto err;
315 case 0: { /* child */
316 char *ex, *requri, *portno;
317 char addr[INET_ADDRSTRLEN];
318 char *argv[] = { NULL, NULL, NULL };
320 close(p[0]);
321 if (dup2(p[1], 1) == -1)
322 goto childerr;
324 if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
325 goto childerr;
327 if (asprintf(&portno, "%d", port) == -1)
328 goto childerr;
330 if (asprintf(&ex, "%s/%s", dir, spath) == -1)
331 goto childerr;
333 if (asprintf(&requri, "%s%s%s", spath,
334 *relpath == '\0' ? "" : "/",
335 relpath) == -1)
336 goto childerr;
338 argv[0] = argv[1] = ex;
340 /* fix the env */
341 safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
342 safe_setenv("SERVER_SOFTWARE", "gmid");
343 safe_setenv("SERVER_PORT", portno);
344 /* setenv("SERVER_NAME", "", 1); */
345 safe_setenv("SCRIPT_NAME", spath);
346 safe_setenv("SCRIPT_EXECUTABLE", ex);
347 safe_setenv("REQUEST_URI", requri);
348 safe_setenv("REQUEST_RELATIVE", relpath);
349 safe_setenv("QUERY_STRING", query);
350 safe_setenv("REMOTE_HOST", addr);
351 safe_setenv("REMOTE_ADDR", addr);
352 safe_setenv("DOCUMENT_ROOT", dir);
354 if (tls_peer_cert_provided(c->ctx)) {
355 safe_setenv("AUTH_TYPE", "Certificate");
356 safe_setenv("REMOTE_USER", tls_peer_cert_subject(c->ctx));
357 safe_setenv("TLS_CLIENT_ISSUER", tls_peer_cert_issuer(c->ctx));
358 safe_setenv("TLS_CLIENT_HASH", tls_peer_cert_hash(c->ctx));
361 execvp(ex, argv);
362 goto childerr;
365 default: /* parent */
366 close(p[1]);
367 close(c->fd);
368 c->fd = p[0];
369 c->child = pid;
370 mark_nonblock(c->fd);
371 c->state = S_SENDING;
372 handle_cgi(fds, c);
373 return 0;
376 err:
377 if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
378 return 0;
379 goodbye(fds, c);
380 return 0;
382 childerr:
383 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
384 close(p[1]);
385 _exit(1);
388 void
389 cgi_poll_on_child(struct pollfd *fds, struct client *c)
391 int fd;
393 if (c->waiting_on_child)
394 return;
395 c->waiting_on_child = 1;
397 fds->events = POLLIN;
399 fd = fds->fd;
400 fds->fd = c->fd;
401 c->fd = fd;
404 void
405 cgi_poll_on_client(struct pollfd *fds, struct client *c)
407 int fd;
409 if (!c->waiting_on_child)
410 return;
411 c->waiting_on_child = 0;
413 fd = fds->fd;
414 fds->fd = c->fd;
415 c->fd = fd;
418 void
419 handle_cgi(struct pollfd *fds, struct client *c)
421 ssize_t r;
423 /* ensure c->fd is the child and fds->fd the client */
424 cgi_poll_on_client(fds, c);
426 while (1) {
427 if (c->len == 0) {
428 if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
429 goto end;
430 if (r == -1) {
431 if (errno == EAGAIN || errno == EWOULDBLOCK) {
432 cgi_poll_on_child(fds, c);
433 return;
435 goto end;
437 c->len = r;
438 c->off = 0;
441 while (c->len > 0) {
442 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
443 case -1:
444 goto end;
446 case TLS_WANT_POLLOUT:
447 fds->events = POLLOUT;
448 return;
450 case TLS_WANT_POLLIN:
451 fds->events = POLLIN;
452 return;
454 default:
455 c->off += r;
456 c->len -= r;
457 break;
462 end:
463 goodbye(fds, c);
466 void
467 send_file(char *path, char *query, struct pollfd *fds, struct client *c)
469 ssize_t ret, len;
471 if (c->fd == -1) {
472 if (!open_file(path, query, fds, c))
473 return;
474 c->state = S_SENDING;
477 len = (c->buf + c->len) - c->i;
479 while (len > 0) {
480 switch (ret = tls_write(c->ctx, c->i, len)) {
481 case -1:
482 LOGE(c, "tls_write: %s", tls_error(c->ctx));
483 goodbye(fds, c);
484 return;
486 case TLS_WANT_POLLIN:
487 fds->events = POLLIN;
488 return;
490 case TLS_WANT_POLLOUT:
491 fds->events = POLLOUT;
492 return;
494 default:
495 c->i += ret;
496 len -= ret;
497 break;
501 goodbye(fds, c);
504 void
505 send_dir(char *path, struct pollfd *fds, struct client *client)
507 char fpath[PATHBUF];
508 size_t len;
510 bzero(fpath, PATHBUF);
512 if (path[0] != '.')
513 fpath[0] = '.';
515 /* this cannot fail since sizeof(fpath) > maxlen of path */
516 strlcat(fpath, path, PATHBUF);
517 len = strlen(fpath);
519 /* add a trailing / in case. */
520 if (fpath[len-1] != '/') {
521 fpath[len] = '/';
524 strlcat(fpath, "index.gmi", sizeof(fpath));
526 send_file(fpath, NULL, fds, client);
529 void
530 handle(struct pollfd *fds, struct client *client)
532 char buf[GEMINI_URL_LEN];
533 const char *parse_err;
534 struct uri uri;
536 switch (client->state) {
537 case S_OPEN:
538 bzero(buf, GEMINI_URL_LEN);
539 switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
540 case -1:
541 LOGE(client, "tls_read: %s", tls_error(client->ctx));
542 goodbye(fds, client);
543 return;
545 case TLS_WANT_POLLIN:
546 fds->events = POLLIN;
547 return;
549 case TLS_WANT_POLLOUT:
550 fds->events = POLLOUT;
551 return;
554 parse_err = "invalid request";
555 if (!trim_req_uri(buf) || !parse_uri(buf, &uri, &parse_err)) {
556 if (!start_reply(fds, client, BAD_REQUEST, parse_err))
557 return;
558 goodbye(fds, client);
559 return;
562 LOGI(client, "GET %s%s%s",
563 *uri.path ? uri.path : "/",
564 *uri.query ? "?" : "",
565 *uri.query ? uri.query : "");
567 send_file(uri.path, uri.query, fds, client);
568 break;
570 case S_INITIALIZING:
571 if (!start_reply(fds, client, client->code, client->meta))
572 return;
574 if (client->code != SUCCESS) {
575 /* we don't need a body */
576 goodbye(fds, client);
577 return;
580 client->state = S_SENDING;
582 /* fallthrough */
584 case S_SENDING:
585 if (client->child != -1)
586 handle_cgi(fds, client);
587 else
588 send_file(NULL, NULL, fds, client);
589 break;
591 case S_CLOSING:
592 goodbye(fds, client);
593 break;
595 default:
596 /* unreachable */
597 abort();
601 void
602 mark_nonblock(int fd)
604 int flags;
606 if ((flags = fcntl(fd, F_GETFL)) == -1)
607 FATAL("fcntl(F_GETFL): %s", strerror(errno));
608 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
609 FATAL("fcntl(F_SETFL): %s", strerror(errno));
612 int
613 make_socket(int port, int family)
615 int sock, v;
616 struct sockaddr_in addr4;
617 struct sockaddr_in6 addr6;
618 struct sockaddr *addr;
619 socklen_t len;
621 switch (family) {
622 case AF_INET:
623 bzero(&addr4, sizeof(addr4));
624 addr4.sin_family = family;
625 addr4.sin_port = htons(port);
626 addr4.sin_addr.s_addr = INADDR_ANY;
627 addr = (struct sockaddr*)&addr4;
628 len = sizeof(addr4);
629 break;
631 case AF_INET6:
632 bzero(&addr6, sizeof(addr6));
633 addr6.sin6_family = AF_INET6;
634 addr6.sin6_port = htons(port);
635 addr6.sin6_addr = in6addr_any;
636 addr = (struct sockaddr*)&addr6;
637 len = sizeof(addr6);
638 break;
640 default:
641 /* unreachable */
642 abort();
645 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
646 FATAL("socket: %s", strerror(errno));
648 v = 1;
649 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
650 FATAL("setsockopt(SO_REUSEADDR): %s", strerror(errno));
652 v = 1;
653 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
654 FATAL("setsockopt(SO_REUSEPORT): %s", strerror(errno));
656 mark_nonblock(sock);
658 if (bind(sock, addr, len) == -1)
659 FATAL("bind: %s", strerror(errno));
661 if (listen(sock, 16) == -1)
662 FATAL("listen: %s", strerror(errno));
664 return sock;
667 void
668 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
670 int i, fd;
671 struct sockaddr_in addr;
672 socklen_t len;
674 len = sizeof(addr);
675 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
676 if (errno == EWOULDBLOCK)
677 return;
678 FATAL("accept: %s", strerror(errno));
681 mark_nonblock(fd);
683 for (i = 0; i < MAX_USERS; ++i) {
684 if (fds[i].fd == -1) {
685 bzero(&clients[i], sizeof(struct client));
686 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
687 break; /* goodbye fd! */
689 fds[i].fd = fd;
690 fds[i].events = POLLIN;
692 clients[i].state = S_OPEN;
693 clients[i].fd = -1;
694 clients[i].child = -1;
695 clients[i].buf = MAP_FAILED;
696 clients[i].af = AF_INET;
697 clients[i].addr = addr.sin_addr;
699 connected_clients++;
700 return;
704 close(fd);
707 void
708 goodbye(struct pollfd *pfd, struct client *c)
710 ssize_t ret;
712 c->state = S_CLOSING;
714 ret = tls_close(c->ctx);
715 if (ret == TLS_WANT_POLLIN) {
716 pfd->events = POLLIN;
717 return;
719 if (ret == TLS_WANT_POLLOUT) {
720 pfd->events = POLLOUT;
721 return;
724 connected_clients--;
726 tls_free(c->ctx);
727 c->ctx = NULL;
729 if (c->buf != MAP_FAILED)
730 munmap(c->buf, c->len);
732 if (c->fd != -1)
733 close(c->fd);
735 close(pfd->fd);
736 pfd->fd = -1;
739 void
740 loop(struct tls *ctx, int sock)
742 int i;
743 struct client clients[MAX_USERS];
744 struct pollfd fds[MAX_USERS];
746 for (i = 0; i < MAX_USERS; ++i) {
747 fds[i].fd = -1;
748 fds[i].events = POLLIN;
749 bzero(&clients[i], sizeof(struct client));
752 fds[0].fd = sock;
754 for (;;) {
755 if (poll(fds, MAX_USERS, INFTIM) == -1) {
756 if (errno == EINTR) {
757 warnx("connected clients: %d",
758 connected_clients);
759 continue;
761 FATAL("poll: %s", strerror(errno));
764 for (i = 0; i < MAX_USERS; i++) {
765 if (fds[i].revents == 0)
766 continue;
768 if (fds[i].revents & (POLLERR|POLLNVAL))
769 FATAL("bad fd %d: %s", fds[i].fd,
770 strerror(errno));
772 if (fds[i].revents & POLLHUP) {
773 /* fds[i] may be the fd of the stdin
774 * of a cgi script that has exited. */
775 if (!clients[i].waiting_on_child) {
776 goodbye(&fds[i], &clients[i]);
777 continue;
781 if (i == 0) { /* new client */
782 do_accept(sock, ctx, fds, clients);
783 continue;
786 handle(&fds[i], &clients[i]);
791 char *
792 absolutify_path(const char *path)
794 char *wd, *r;
796 if (*path == '/')
797 return strdup(path);
799 wd = getwd(NULL);
800 if (asprintf(&r, "%s/%s", wd, path) == -1)
801 err(1, "asprintf");
802 free(wd);
803 return r;
806 void
807 usage(const char *me)
809 fprintf(stderr,
810 "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] "
811 "[-l logfile] [-p port] [-x cgi-bin]\n",
812 me);
815 int
816 main(int argc, char **argv)
818 const char *cert = "cert.pem", *key = "key.pem";
819 struct tls *ctx = NULL;
820 struct tls_config *conf;
821 int sock, ch;
822 connected_clients = 0;
824 if ((dir = absolutify_path("docs")) == NULL)
825 err(1, "absolutify_path");
827 cgi = NULL;
828 port = 1965;
829 foreground = 0;
831 while ((ch = getopt(argc, argv, "c:d:fhk:p:x:")) != -1) {
832 switch (ch) {
833 case 'c':
834 cert = optarg;
835 break;
837 case 'd':
838 free((char*)dir);
839 if ((dir = absolutify_path(optarg)) == NULL)
840 err(1, "absolutify_path");
841 break;
843 case 'f':
844 foreground = 1;
845 break;
847 case 'h':
848 usage(*argv);
849 return 0;
851 case 'k':
852 key = optarg;
853 break;
855 case 'p': {
856 char *ep;
857 long lval;
859 errno = 0;
860 lval = strtol(optarg, &ep, 10);
861 if (optarg[0] == '\0' || *ep != '\0')
862 err(1, "not a number: %s", optarg);
863 if (lval < 0 || lval > UINT16_MAX)
864 err(1, "port number out of range: %s", optarg);
865 port = lval;
866 break;
869 case 'x':
870 cgi = optarg;
871 break;
873 default:
874 usage(*argv);
875 return 1;
879 signal(SIGPIPE, SIG_IGN);
880 signal(SIGCHLD, SIG_IGN);
882 #ifdef SIGINFO
883 signal(SIGINFO, sig_handler);
884 #endif
885 signal(SIGUSR2, sig_handler);
887 if (!foreground)
888 signal(SIGHUP, SIG_IGN);
890 if ((conf = tls_config_new()) == NULL)
891 err(1, "tls_config_new");
893 /* optionally accept client certs, but don't try to verify them */
894 tls_config_verify_client_optional(conf);
895 tls_config_insecure_noverifycert(conf);
897 if (tls_config_set_protocols(conf,
898 TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
899 err(1, "tls_config_set_protocols");
901 if (tls_config_set_cert_file(conf, cert) == -1)
902 err(1, "tls_config_set_cert_file: %s", cert);
904 if (tls_config_set_key_file(conf, key) == -1)
905 err(1, "tls_config_set_key_file: %s", key);
907 if ((ctx = tls_server()) == NULL)
908 err(1, "tls_server");
910 if (tls_configure(ctx, conf) == -1)
911 errx(1, "tls_configure: %s", tls_error(ctx));
913 sock = make_socket(port, AF_INET);
915 if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
916 err(1, "open: %s", dir);
918 if (!foreground && daemon(0, 1) == -1)
919 exit(1);
921 if (unveil(dir, "rx") == -1)
922 err(1, "unveil");
924 if (pledge("stdio rpath inet proc exec", NULL) == -1)
925 err(1, "pledge");
927 /* drop proc and exec if cgi isn't enabled */
928 if (cgi == NULL && pledge("stdio rpath inet", NULL) == -1)
929 err(1, "pledge");
931 loop(ctx, sock);
933 close(sock);
934 tls_free(ctx);
935 tls_config_free(conf);