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 <arpa/inet.h>
22 #include <netinet/in.h>
24 #include <assert.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <poll.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <tls.h>
34 #include <unistd.h>
36 #ifndef __OpenBSD__
37 # define pledge(a, b) 0
38 # define unveil(a, b) 0
39 #endif /* __OpenBSD__ */
41 #ifndef INFTIM
42 # define INFTIM -1
43 #endif /* INFTIM */
45 #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
47 /* large enough to hold a copy of a gemini URL and still have extra room */
48 #define PATHBUF 2048
50 #define SUCCESS 20
51 #define TEMP_FAILURE 40
52 #define NOT_FOUND 51
53 #define BAD_REQUEST 59
55 #ifndef MAX_USERS
56 #define MAX_USERS 64
57 #endif
59 enum {
60 S_OPEN,
61 S_INITIALIZING,
62 S_SENDING,
63 S_CLOSING,
64 };
66 struct client {
67 struct tls *ctx;
68 int state;
69 int code;
70 const char *meta;
71 int fd, waiting_on_child;
72 pid_t child;
73 char sbuf[1024]; /* static buffer */
74 void *buf, *i; /* mmap buffer */
75 ssize_t len, off; /* mmap/static buffer */
76 int af;
77 struct in_addr addr;
78 };
80 struct etm { /* file extension to mime */
81 const char *mime;
82 const char *ext;
83 } filetypes[] = {
84 {"application/pdf", "pdf"},
86 {"image/gif", "gif"},
87 {"image/jpeg", "jpg"},
88 {"image/jpeg", "jpeg"},
89 {"image/png", "png"},
90 {"image/svg+xml", "svg"},
92 {"text/gemini", "gemini"},
93 {"text/gemini", "gmi"},
94 {"text/markdown", "markdown"},
95 {"text/markdown", "md"},
96 {"text/plain", "txt"},
97 {"text/xml", "xml"},
99 {NULL, NULL}
100 };
102 #define LOG(c, fmt, ...) \
103 do { \
104 char buf[INET_ADDRSTRLEN]; \
105 if (inet_ntop((c)->af, &(c)->addr, buf, sizeof(buf)) == NULL) \
106 err(1, "inet_ntop"); \
107 dprintf(logfd, "[%s] " fmt "\n", buf, __VA_ARGS__); \
108 } while (0)
110 const char *dir, *cgi;
111 int dirfd, logfd;
112 int connected_clients;
114 void siginfo_handler(int);
115 int starts_with(const char*, const char*);
117 char *url_after_proto(char*);
118 char *url_start_of_request(char*);
119 int url_trim(struct client*, char*);
120 char *adjust_path(char*);
121 int path_isdir(char*);
122 ssize_t filesize(int);
124 int start_reply(struct pollfd*, struct client*, int, const char*);
125 const char *path_ext(const char*);
126 const char *mime(const char*);
127 int open_file(char*, char*, struct pollfd*, struct client*);
128 void start_cgi(const char*, const char*, struct pollfd*, struct client*);
129 void cgi_setpoll_on_child(struct pollfd*, struct client*);
130 void cgi_setpoll_on_client(struct pollfd*, struct client*);
131 void handle_cgi(struct pollfd*, struct client*);
132 void send_file(char*, char*, struct pollfd*, struct client*);
133 void send_dir(char*, struct pollfd*, struct client*);
134 void handle(struct pollfd*, struct client*);
136 void mark_nonblock(int);
137 int make_soket(int);
138 void do_accept(int, struct tls*, struct pollfd*, struct client*);
139 void goodbye(struct pollfd*, struct client*);
140 void loop(struct tls*, int);
142 void usage(const char*);
144 void
145 siginfo_handler(int sig)
147 (void)sig;
150 int
151 starts_with(const char *str, const char *prefix)
153 size_t i;
155 for (i = 0; prefix[i] != '\0'; ++i)
156 if (str[i] != prefix[i])
157 return 0;
158 return 1;
161 char *
162 url_after_proto(char *url)
164 char *s;
165 const char *proto = "gemini";
166 const char *marker = "://";
168 /* a relative URL */
169 if ((s = strstr(url, marker)) == NULL)
170 return url;
172 if (s - strlen(proto) != url)
173 return NULL;
175 if (!starts_with(url, proto))
176 return NULL;
178 /* a valid gemini:// URL */
179 return s + strlen(marker);
182 char *
183 url_start_of_request(char *url)
185 char *s, *t;
187 if ((s = url_after_proto(url)) == NULL)
188 return NULL;
190 if ((t = strstr(s, "/")) == NULL)
191 return s + strlen(s);
192 return t;
195 int
196 url_trim(struct client *c, char *url)
198 const char *e = "\r\n";
199 char *s;
201 if ((s = strstr(url, e)) == NULL)
202 return 0;
203 s[0] = '\0';
204 s[1] = '\0';
206 if (s[2] != '\0') {
207 LOG(c, "%s", "request longer than 1024 bytes\n");
208 return 0;
211 return 1;
214 char *
215 adjust_path(char *path)
217 char *s, *query;
218 size_t len;
220 if ((query = strchr(path, '?')) != NULL) {
221 *query = '\0';
222 query++;
225 /* /.. -> / */
226 len = strlen(path);
227 if (len >= 3) {
228 if (!strcmp(&path[len-3], "/..")) {
229 path[len-2] = '\0';
233 /* if the path is only `..` trim out and exit */
234 if (!strcmp(path, "..")) {
235 path[0] = '\0';
236 return query;
239 /* remove every ../ in the path */
240 while (1) {
241 if ((s = strstr(path, "../")) == NULL)
242 return query;
243 memmove(s, s+3, strlen(s)+1); /* copy also the \0 */
247 int
248 path_isdir(char *path)
250 if (*path == '\0')
251 return 1;
252 return path[strlen(path)-1] == '/';
255 int
256 start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
258 char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
259 int len;
260 int ret;
262 client->code = code;
263 client->meta = reason;
264 client->state = S_INITIALIZING;
266 len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
267 assert(len < (int)sizeof(buf));
268 ret = tls_write(client->ctx, buf, len);
269 if (ret == TLS_WANT_POLLIN) {
270 pfd->events = POLLIN;
271 return 0;
274 if (ret == TLS_WANT_POLLOUT) {
275 pfd->events = POLLOUT;
276 return 0;
279 return 1;
282 ssize_t
283 filesize(int fd)
285 ssize_t len;
287 if ((len = lseek(fd, 0, SEEK_END)) == -1)
288 return -1;
289 if (lseek(fd, 0, SEEK_SET) == -1)
290 return -1;
291 return len;
294 const char *
295 path_ext(const char *path)
297 const char *end;
299 end = path + strlen(path)-1; /* the last byte before the NUL */
300 for (; end != path; --end) {
301 if (*end == '.')
302 return end+1;
303 if (*end == '/')
304 break;
307 return NULL;
310 const char *
311 mime(const char *path)
313 const char *ext, *def = "application/octet-stream";
314 struct etm *t;
316 if ((ext = path_ext(path)) == NULL)
317 return def;
319 for (t = filetypes; t->mime != NULL; ++t)
320 if (!strcmp(ext, t->ext))
321 return t->mime;
323 return def;
326 int
327 open_file(char *path, char *query, struct pollfd *fds, struct client *c)
329 char fpath[PATHBUF];
330 struct stat sb;
332 assert(path != NULL);
334 bzero(fpath, sizeof(fpath));
336 if (*path != '.')
337 fpath[0] = '.';
338 strlcat(fpath, path, PATHBUF);
340 if ((c->fd = openat(dirfd, fpath,
341 O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
342 LOG(c, "open failed: %s", fpath);
343 if (!start_reply(fds, c, NOT_FOUND, "not found"))
344 return 0;
345 goodbye(fds, c);
346 return 0;
349 if (fstat(c->fd, &sb) == -1) {
350 LOG(c, "fstat failed for %s", fpath);
351 if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
352 return 0;
353 goodbye(fds, c);
354 return 0;
357 if (S_ISDIR(sb.st_mode)) {
358 LOG(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
359 close(c->fd);
360 c->fd = -1;
361 send_dir(fpath, fds, c);
362 return 0;
365 /* +2 to skip the ./ */
366 if ((sb.st_mode & S_IXUSR) && cgi != NULL && starts_with(fpath+2, cgi)) {
367 start_cgi(fpath, query, fds, c);
368 return 0;
371 if ((c->len = filesize(c->fd)) == -1) {
372 LOG(c, "failed to get file size for %s", fpath);
373 goodbye(fds, c);
374 return 0;
377 if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
378 c->fd, 0)) == MAP_FAILED) {
379 warn("mmap: %s", fpath);
380 goodbye(fds, c);
381 return 0;
383 c->i = c->buf;
385 return start_reply(fds, c, SUCCESS, mime(fpath));
388 void
389 start_cgi(const char *path, const char *query,
390 struct pollfd *fds, struct client *c)
392 pid_t pid;
393 int p[2];
395 if (pipe(p) == -1)
396 goto err;
398 switch (pid = fork()) {
399 case -1:
400 goto err;
402 case 0: { /* child */
403 char *expath;
404 char addr[INET_ADDRSTRLEN];
405 char *argv[] = { NULL, NULL, NULL };
407 /* skip the initial ./ */
408 path += 2;
410 close(p[0]); /* close the read end */
411 if (dup2(p[1], 1) == -1)
412 goto childerr;
414 if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
415 goto childerr;
417 /* skip the ./ at the start of path*/
418 if (asprintf(&expath, "%s%s", dir, path) == -1)
419 goto childerr;
420 argv[0] = argv[1] = expath;
422 /* fix the env */
423 setenv("SERVER_SOFTWARE", "gmid", 1);
424 /* setenv("SERVER_NAME", "", 1); */
425 /* setenv("GATEWAY_INTERFACE", "CGI/version", 1); */
426 setenv("SERVER_PROTOCOL", "gemini", 1);
427 setenv("SERVER_PORT", "1965", 1);
428 setenv("PATH_INFO", path, 1);
429 setenv("PATH_TRANSLATED", expath, 1);
430 if (query != NULL)
431 setenv("QUERY_STRING", query, 1);
432 setenv("REMOTE_ADDR", addr, 1);
434 execvp(expath, argv);
435 goto childerr;
438 default: /* parent */
439 close(p[1]); /* close the write end */
440 close(c->fd);
441 c->fd = p[0];
442 c->child = pid;
443 mark_nonblock(c->fd);
444 c->state = S_SENDING;
445 handle_cgi(fds, c);
446 return;
449 err:
450 if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
451 return;
452 goodbye(fds, c);
453 return;
455 childerr:
456 dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
457 close(p[1]);
459 /* don't call atexit stuff */
460 _exit(1);
463 void
464 cgi_setpoll_on_child(struct pollfd *fds, struct client *c)
466 int fd;
468 if (c->waiting_on_child)
469 return;
470 c->waiting_on_child = 1;
472 fds->events = POLLIN;
474 fd = fds->fd;
475 fds->fd = c->fd;
476 c->fd = fd;
479 void
480 cgi_setpoll_on_client(struct pollfd *fds, struct client *c)
482 int fd;
484 if (!c->waiting_on_child)
485 return;
486 c->waiting_on_child = 0;
488 fd = fds->fd;
489 fds->fd = c->fd;
490 c->fd = fd;
493 void
494 handle_cgi(struct pollfd *fds, struct client *c)
496 ssize_t r;
498 /* ensure c->fd is the child and fds->fd the client */
499 cgi_setpoll_on_client(fds, c);
501 while (1) {
502 if (c->len == 0) {
503 if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
504 goto end;
505 if (r == -1) {
506 if (errno == EAGAIN || errno == EWOULDBLOCK) {
507 cgi_setpoll_on_child(fds, c);
508 return;
510 goto end;
512 c->len = r;
513 c->off = 0;
516 while (c->len > 0) {
517 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
518 case -1:
519 goto end;
521 case TLS_WANT_POLLOUT:
522 fds->events = POLLOUT;
523 return;
525 case TLS_WANT_POLLIN:
526 fds->events = POLLIN;
527 return;
529 default:
530 c->off += r;
531 c->len -= r;
532 break;
537 end:
538 goodbye(fds, c);
541 void
542 send_file(char *path, char *query, struct pollfd *fds, struct client *c)
544 ssize_t ret, len;
546 if (c->fd == -1) {
547 if (!open_file(path, query, fds, c))
548 return;
549 c->state = S_SENDING;
552 len = (c->buf + c->len) - c->i;
554 while (len > 0) {
555 switch (ret = tls_write(c->ctx, c->i, len)) {
556 case -1:
557 LOG(c, "tls_write: %s", tls_error(c->ctx));
558 goodbye(fds, c);
559 return;
561 case TLS_WANT_POLLIN:
562 fds->events = POLLIN;
563 return;
565 case TLS_WANT_POLLOUT:
566 fds->events = POLLOUT;
567 return;
569 default:
570 c->i += ret;
571 len -= ret;
572 break;
576 goodbye(fds, c);
579 void
580 send_dir(char *path, struct pollfd *fds, struct client *client)
582 char fpath[PATHBUF];
583 size_t len;
585 bzero(fpath, PATHBUF);
587 if (path[0] != '.')
588 fpath[0] = '.';
590 /* this cannot fail since sizeof(fpath) > maxlen of path */
591 strlcat(fpath, path, PATHBUF);
592 len = strlen(fpath);
594 /* add a trailing / in case. */
595 if (fpath[len-1] != '/') {
596 fpath[len] = '/';
599 strlcat(fpath, "index.gmi", sizeof(fpath));
601 send_file(fpath, NULL, fds, client);
604 void
605 handle(struct pollfd *fds, struct client *client)
607 char buf[GEMINI_URL_LEN];
608 char *path;
609 char *query;
611 switch (client->state) {
612 case S_OPEN:
613 bzero(buf, GEMINI_URL_LEN);
614 switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
615 case -1:
616 LOG(client, "tls_read: %s", tls_error(client->ctx));
617 goodbye(fds, client);
618 return;
620 case TLS_WANT_POLLIN:
621 fds->events = POLLIN;
622 return;
624 case TLS_WANT_POLLOUT:
625 fds->events = POLLOUT;
626 return;
629 if (!url_trim(client, buf)) {
630 if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
631 return;
632 goodbye(fds, client);
633 return;
636 if ((path = url_start_of_request(buf)) == NULL) {
637 if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
638 return;
639 goodbye(fds, client);
640 return;
643 query = adjust_path(path);
644 LOG(client, "get %s%s%s", path,
645 query ? "?" : "",
646 query ? query : "");
648 if (path_isdir(path))
649 send_dir(path, fds, client);
650 else
651 send_file(path, query, fds, client);
652 break;
654 case S_INITIALIZING:
655 if (!start_reply(fds, client, client->code, client->meta))
656 return;
658 if (client->code != SUCCESS) {
659 /* we don't need a body */
660 goodbye(fds, client);
661 return;
664 client->state = S_SENDING;
666 /* fallthrough */
668 case S_SENDING:
669 if (client->child != -1)
670 handle_cgi(fds, client);
671 else
672 send_file(NULL, NULL, fds, client);
673 break;
675 case S_CLOSING:
676 goodbye(fds, client);
677 break;
679 default:
680 /* unreachable */
681 abort();
685 void
686 mark_nonblock(int fd)
688 int flags;
690 if ((flags = fcntl(fd, F_GETFL)) == -1)
691 err(1, "fcntl(F_GETFL)");
692 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
693 err(1, "fcntl(F_SETFL)");
696 int
697 make_socket(int port, int family)
699 int sock, v;
700 struct sockaddr_in addr4;
701 struct sockaddr_in6 addr6;
702 struct sockaddr *addr;
703 socklen_t len;
705 switch (family) {
706 case AF_INET:
707 bzero(&addr4, sizeof(addr4));
708 addr4.sin_family = family;
709 addr4.sin_port = htons(port);
710 addr4.sin_addr.s_addr = INADDR_ANY;
711 addr = (struct sockaddr*)&addr4;
712 len = sizeof(addr4);
713 break;
715 case AF_INET6:
716 bzero(&addr6, sizeof(addr6));
717 addr6.sin6_family = AF_INET6;
718 addr6.sin6_port = htons(port);
719 addr6.sin6_addr = in6addr_any;
720 addr = (struct sockaddr*)&addr6;
721 len = sizeof(addr6);
722 break;
724 default:
725 /* unreachable */
726 abort();
729 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
730 err(1, "socket");
732 v = 1;
733 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
734 err(1, "setsockopt(SO_REUSEADDR)");
736 v = 1;
737 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
738 err(1, "setsockopt(SO_REUSEPORT)");
740 mark_nonblock(sock);
742 if (bind(sock, addr, len) == -1)
743 err(1, "bind");
745 if (listen(sock, 16) == -1)
746 err(1, "listen");
748 return sock;
751 void
752 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
754 int i, fd;
755 struct sockaddr_in addr;
756 socklen_t len;
758 len = sizeof(addr);
759 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
760 if (errno == EWOULDBLOCK)
761 return;
762 err(1, "accept");
765 mark_nonblock(fd);
767 for (i = 0; i < MAX_USERS; ++i) {
768 if (fds[i].fd == -1) {
769 bzero(&clients[i], sizeof(struct client));
770 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
771 break; /* goodbye fd! */
773 fds[i].fd = fd;
774 fds[i].events = POLLIN;
776 clients[i].state = S_OPEN;
777 clients[i].fd = -1;
778 clients[i].child = -1;
779 clients[i].buf = MAP_FAILED;
780 clients[i].af = AF_INET;
781 clients[i].addr = addr.sin_addr;
783 connected_clients++;
784 return;
788 close(fd);
791 void
792 goodbye(struct pollfd *pfd, struct client *c)
794 ssize_t ret;
796 c->state = S_CLOSING;
798 ret = tls_close(c->ctx);
799 if (ret == TLS_WANT_POLLIN) {
800 pfd->events = POLLIN;
801 return;
803 if (ret == TLS_WANT_POLLOUT) {
804 pfd->events = POLLOUT;
805 return;
808 connected_clients--;
810 tls_free(c->ctx);
811 c->ctx = NULL;
813 if (c->buf != MAP_FAILED)
814 munmap(c->buf, c->len);
816 if (c->fd != -1)
817 close(c->fd);
819 close(pfd->fd);
820 pfd->fd = -1;
823 void
824 loop(struct tls *ctx, int sock)
826 int i, todo;
827 struct client clients[MAX_USERS];
828 struct pollfd fds[MAX_USERS];
830 for (i = 0; i < MAX_USERS; ++i) {
831 fds[i].fd = -1;
832 fds[i].events = POLLIN;
833 bzero(&clients[i], sizeof(struct client));
836 fds[0].fd = sock;
838 for (;;) {
839 if ((todo = poll(fds, MAX_USERS, INFTIM)) == -1) {
840 if (errno == EINTR) {
841 warnx("connected clients: %d", connected_clients);
842 continue;
844 err(1, "poll");
847 for (i = 0; i < MAX_USERS; i++) {
848 assert(i < MAX_USERS);
850 if (fds[i].revents == 0)
851 continue;
853 if (fds[i].revents & (POLLERR|POLLNVAL))
854 err(1, "bad fd %d", fds[i].fd);
856 if (fds[i].revents & POLLHUP) {
857 /* fds[i] may be the fd of the stdin
858 * of a cgi script that has exited. */
859 if (!clients[i].waiting_on_child) {
860 goodbye(&fds[i], &clients[i]);
861 continue;
865 todo--;
867 if (i == 0) { /* new client */
868 do_accept(sock, ctx, fds, clients);
869 continue;
872 handle(&fds[i], &clients[i]);
877 void
878 usage(const char *me)
880 fprintf(stderr,
881 "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] "
882 "[-l logfile] [-x cgi-bin]\n",
883 me);
886 int
887 main(int argc, char **argv)
889 const char *cert = "cert.pem", *key = "key.pem";
890 struct tls *ctx = NULL;
891 struct tls_config *conf;
892 int sock, ch;
894 signal(SIGPIPE, SIG_IGN);
895 signal(SIGCHLD, SIG_IGN);
897 #ifdef SIGINFO
898 signal(SIGINFO, siginfo_handler);
899 #endif
900 signal(SIGUSR2, siginfo_handler);
902 connected_clients = 0;
904 dir = "docs/";
905 logfd = 2; /* stderr */
906 cgi = NULL;
908 while ((ch = getopt(argc, argv, "c:d:hk:l:x:")) != -1) {
909 switch (ch) {
910 case 'c':
911 cert = optarg;
912 break;
914 case 'd':
915 dir = optarg;
916 break;
918 case 'h':
919 usage(*argv);
920 return 0;
922 case 'k':
923 key = optarg;
924 break;
926 case 'l':
927 /* open log file or create it with 644 */
928 if ((logfd = open(optarg, O_WRONLY | O_CREAT | O_CLOEXEC,
929 S_IRUSR | S_IWUSR | S_IRGRP | S_IWOTH)) == -1)
930 err(1, "%s", optarg);
931 break;
933 case 'x':
934 cgi = optarg;
935 break;
937 default:
938 usage(*argv);
939 return 1;
943 if ((conf = tls_config_new()) == NULL)
944 err(1, "tls_config_new");
946 if (tls_config_set_protocols(conf,
947 TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
948 err(1, "tls_config_set_protocols");
950 if (tls_config_set_cert_file(conf, cert) == -1)
951 err(1, "tls_config_set_cert_file: %s", cert);
953 if (tls_config_set_key_file(conf, key) == -1)
954 err(1, "tls_config_set_key_file: %s", key);
956 if ((ctx = tls_server()) == NULL)
957 err(1, "tls_server");
959 if (tls_configure(ctx, conf) == -1)
960 errx(1, "tls_configure: %s", tls_error(ctx));
962 sock = make_socket(1965, AF_INET);
964 if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
965 err(1, "open: %s", dir);
967 if (cgi != NULL) {
968 if (unveil(dir, "rx") == -1)
969 err(1, "unveil");
970 if (pledge("stdio rpath inet proc exec", NULL) == -1)
971 err(1, "pledge");
972 } else {
973 if (unveil(dir, "r") == -1)
974 err(1, "unveil");
975 if (pledge("stdio rpath inet", NULL) == -1)
976 err(1, "pledge");
979 loop(ctx, sock);
981 close(sock);
982 tls_free(ctx);
983 tls_config_free(conf);