2 3e4749f7 2020-10-02 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
4 3e4749f7 2020-10-02 op * Permission to use, copy, modify, and distribute this software for any
5 3e4749f7 2020-10-02 op * purpose with or without fee is hereby granted, provided that the above
6 3e4749f7 2020-10-02 op * copyright notice and this permission notice appear in all copies.
8 3e4749f7 2020-10-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3e4749f7 2020-10-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3e4749f7 2020-10-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3e4749f7 2020-10-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3e4749f7 2020-10-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3e4749f7 2020-10-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3e4749f7 2020-10-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3e4749f7 2020-10-02 op #include <sys/socket.h>
18 3e4749f7 2020-10-02 op #include <sys/stat.h>
20 3e4749f7 2020-10-02 op #include <netinet/in.h>
22 3e4749f7 2020-10-02 op #include <assert.h>
23 3e4749f7 2020-10-02 op #include <err.h>
24 592fd624 2020-10-07 op #include <errno.h>
25 3e4749f7 2020-10-02 op #include <fcntl.h>
26 592fd624 2020-10-07 op #include <poll.h>
27 3e4749f7 2020-10-02 op #include <stdio.h>
28 3e4749f7 2020-10-02 op #include <stdlib.h>
29 3e4749f7 2020-10-02 op #include <string.h>
30 3e4749f7 2020-10-02 op #include <tls.h>
31 3e4749f7 2020-10-02 op #include <unistd.h>
33 3e4749f7 2020-10-02 op #ifndef __OpenBSD__
34 3e4749f7 2020-10-02 op # define pledge(a, b) 0
35 3e4749f7 2020-10-02 op # define unveil(a, b) 0
36 3e4749f7 2020-10-02 op #endif /* __OpenBSD__ */
38 592fd624 2020-10-07 op #ifndef INFTIM
39 592fd624 2020-10-07 op # define INFTIM -1
40 592fd624 2020-10-07 op #endif /* INFTIM */
42 3e4749f7 2020-10-02 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
44 4d4f0e19 2020-10-03 op /* large enough to hold a copy of a gemini URL and still have extra room */
45 d95b77a0 2020-10-07 op #define PATHBUF 2048
47 d95b77a0 2020-10-07 op #define FILEBUF 1024
49 3e4749f7 2020-10-02 op #define SUCCESS 20
50 3e4749f7 2020-10-02 op #define NOT_FOUND 51
51 3e4749f7 2020-10-02 op #define BAD_REQUEST 59
53 592fd624 2020-10-07 op #ifndef MAX_USERS
54 592fd624 2020-10-07 op #define MAX_USERS 64
59 592fd624 2020-10-07 op S_INITIALIZING,
64 592fd624 2020-10-07 op struct client {
65 592fd624 2020-10-07 op struct tls *ctx;
68 592fd624 2020-10-07 op const char *meta;
72 cc68fe70 2020-10-07 op struct etm { /* file extension to mime */
73 cc68fe70 2020-10-07 op const char *mime;
74 cc68fe70 2020-10-07 op const char *ext;
75 cc68fe70 2020-10-07 op } filetypes[] = {
76 cc68fe70 2020-10-07 op {"application/pdf", "pdf"},
78 cc68fe70 2020-10-07 op {"image/gif", "gif"},
79 cc68fe70 2020-10-07 op {"image/jpeg", "jpg"},
80 cc68fe70 2020-10-07 op {"image/jpeg", "jpeg"},
81 cc68fe70 2020-10-07 op {"image/png", "png"},
82 cc68fe70 2020-10-07 op {"image/svg+xml", "svg"},
84 cc68fe70 2020-10-07 op {"text/gemini", "gemini"},
85 cc68fe70 2020-10-07 op {"text/gemini", "gmi"},
86 cc68fe70 2020-10-07 op {"text/markdown", "markdown"},
87 cc68fe70 2020-10-07 op {"text/markdown", "md"},
88 cc68fe70 2020-10-07 op {"text/plain", "txt"},
95 592fd624 2020-10-07 op char *url_after_proto(char*);
96 592fd624 2020-10-07 op char *url_start_of_request(char*);
97 592fd624 2020-10-07 op int url_trim(char*);
98 592fd624 2020-10-07 op void adjust_path(char*);
99 592fd624 2020-10-07 op int path_isdir(char*);
101 592fd624 2020-10-07 op int start_reply(struct pollfd*, struct client*, int, const char*);
102 592fd624 2020-10-07 op int isdir(int);
103 cc68fe70 2020-10-07 op const char *path_ext(const char*);
104 cc68fe70 2020-10-07 op const char *mime(const char*);
105 592fd624 2020-10-07 op void send_file(char*, struct pollfd*, struct client*);
106 592fd624 2020-10-07 op void send_dir(char*, struct pollfd*, struct client*);
107 592fd624 2020-10-07 op void handle(struct pollfd*, struct client*);
109 592fd624 2020-10-07 op void mark_nonblock(int);
110 592fd624 2020-10-07 op int make_soket(int);
111 592fd624 2020-10-07 op void do_accept(int, struct tls*, struct pollfd*, struct client*);
112 592fd624 2020-10-07 op void goodbye(struct pollfd*, struct client*);
113 592fd624 2020-10-07 op void loop(struct tls*, int);
115 592fd624 2020-10-07 op void usage(const char*);
118 3e4749f7 2020-10-02 op url_after_proto(char *url)
121 3e4749f7 2020-10-02 op const char *proto = "gemini";
122 3e4749f7 2020-10-02 op const char *marker = "://";
124 3e4749f7 2020-10-02 op if ((s = strstr(url, marker)) == NULL)
127 3e4749f7 2020-10-02 op /* not a gemini:// URL */
129 3e4749f7 2020-10-02 op if (s - strlen(proto) < url)
132 3e4749f7 2020-10-02 op /* if (strcmp(s - strlen(proto), proto)) */
133 3e4749f7 2020-10-02 op /* return NULL; */
135 3e4749f7 2020-10-02 op /* a valid gemini:// URL */
136 3e4749f7 2020-10-02 op return s + strlen(marker);
140 3e4749f7 2020-10-02 op url_start_of_request(char *url)
144 3e4749f7 2020-10-02 op if ((s = url_after_proto(url)) == NULL)
147 3e4749f7 2020-10-02 op if ((t = strstr(s, "/")) == NULL)
148 3e4749f7 2020-10-02 op return s + strlen(s);
153 3e4749f7 2020-10-02 op url_trim(char *url)
155 3e4749f7 2020-10-02 op const char *e = "\r\n";
158 3e4749f7 2020-10-02 op if ((s = strstr(url, e)) == NULL)
163 3e4749f7 2020-10-02 op if (s[2] != '\0') {
164 4d4f0e19 2020-10-03 op fprintf(stderr, "the request was longer than 1024 bytes\n");
172 3e4749f7 2020-10-02 op adjust_path(char *path)
177 3e4749f7 2020-10-02 op /* /.. -> / */
178 3e4749f7 2020-10-02 op len = strlen(path);
179 3e4749f7 2020-10-02 op if (len >= 3) {
180 3e4749f7 2020-10-02 op if (!strcmp(&path[len-3], "/..")) {
181 3e4749f7 2020-10-02 op path[len-2] = '\0';
185 3e4749f7 2020-10-02 op /* if the path is only `..` trim out and exit */
186 3e4749f7 2020-10-02 op if (!strcmp(path, "..")) {
187 3e4749f7 2020-10-02 op path[0] = '\0';
191 3e4749f7 2020-10-02 op /* remove every ../ in the path */
193 3e4749f7 2020-10-02 op if ((s = strstr(path, "../")) == NULL)
195 67328d23 2020-10-03 op memmove(s, s+3, strlen(s)+1); /* copy also the \0 */
200 3e4749f7 2020-10-02 op path_isdir(char *path)
202 3e4749f7 2020-10-02 op if (*path == '\0')
204 4d4f0e19 2020-10-03 op return path[strlen(path)-1] == '/';
208 592fd624 2020-10-07 op start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
210 3e4749f7 2020-10-02 op char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
214 592fd624 2020-10-07 op client->code = code;
215 592fd624 2020-10-07 op client->meta = reason;
216 592fd624 2020-10-07 op client->state = S_INITIALIZING;
218 3e4749f7 2020-10-02 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
219 3e4749f7 2020-10-02 op assert(len < (int)sizeof(buf));
220 592fd624 2020-10-07 op ret = tls_write(client->ctx, buf, len);
221 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
222 592fd624 2020-10-07 op pfd->events = POLLIN;
226 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
227 592fd624 2020-10-07 op pfd->events = POLLOUT;
235 3e4749f7 2020-10-02 op isdir(int fd)
237 3e4749f7 2020-10-02 op struct stat sb;
239 3e4749f7 2020-10-02 op if (fstat(fd, &sb) == -1) {
240 3e4749f7 2020-10-02 op warn("fstat");
241 4d4f0e19 2020-10-03 op return 1; /* we'll probably fail later on anyway */
244 3e4749f7 2020-10-02 op return S_ISDIR(sb.st_mode);
248 cc68fe70 2020-10-07 op path_ext(const char *path)
250 cc68fe70 2020-10-07 op const char *end;
252 cc68fe70 2020-10-07 op end = path + strlen(path)-1; /* the last byte before the NUL */
253 cc68fe70 2020-10-07 op for (; end != path; --end) {
254 cc68fe70 2020-10-07 op if (*end == '.')
255 cc68fe70 2020-10-07 op return end+1;
256 cc68fe70 2020-10-07 op if (*end == '/')
264 cc68fe70 2020-10-07 op mime(const char *path)
266 cc68fe70 2020-10-07 op const char *ext, *def = "application/octet-stream";
267 cc68fe70 2020-10-07 op struct etm *t;
269 cc68fe70 2020-10-07 op if ((ext = path_ext(path)) == NULL)
272 cc68fe70 2020-10-07 op for (t = filetypes; t->mime != NULL; ++t)
273 cc68fe70 2020-10-07 op if (!strcmp(ext, t->ext))
274 cc68fe70 2020-10-07 op return t->mime;
280 592fd624 2020-10-07 op send_file(char *path, struct pollfd *fds, struct client *client)
282 3e4749f7 2020-10-02 op char fpath[PATHBUF];
283 3e4749f7 2020-10-02 op char buf[FILEBUF];
285 09a1b978 2020-10-07 op ssize_t ret, len;
287 592fd624 2020-10-07 op if (client->fd == -1) {
288 592fd624 2020-10-07 op assert(path != NULL);
290 592fd624 2020-10-07 op bzero(fpath, sizeof(fpath));
292 592fd624 2020-10-07 op if (*path != '.')
293 592fd624 2020-10-07 op fpath[0] = '.';
294 592fd624 2020-10-07 op strlcat(fpath, path, PATHBUF);
296 592fd624 2020-10-07 op if ((client->fd = openat(dirfd, fpath, O_RDONLY | O_NOFOLLOW)) == -1) {
297 592fd624 2020-10-07 op warn("open: %s", fpath);
298 592fd624 2020-10-07 op if (!start_reply(fds, client, NOT_FOUND, "not found"))
300 592fd624 2020-10-07 op goodbye(fds, client);
304 592fd624 2020-10-07 op if (isdir(client->fd)) {
305 592fd624 2020-10-07 op warnx("%s is a directory, trying %s/index.gmi", fpath, fpath);
306 592fd624 2020-10-07 op close(client->fd);
307 592fd624 2020-10-07 op client->fd = -1;
308 592fd624 2020-10-07 op send_dir(fpath, fds, client);
312 cc68fe70 2020-10-07 op if (!start_reply(fds, client, SUCCESS, mime(fpath)))
317 09a1b978 2020-10-07 op len = read(client->fd, buf, sizeof(buf));
318 09a1b978 2020-10-07 op if (len == -1)
319 592fd624 2020-10-07 op warn("read");
320 09a1b978 2020-10-07 op if (len == 0 || len == -1) {
321 592fd624 2020-10-07 op goodbye(fds, client);
326 09a1b978 2020-10-07 op while (len > 0) {
327 09a1b978 2020-10-07 op ret = tls_write(client->ctx, buf+off, len);
328 09a1b978 2020-10-07 op switch (ret) {
330 592fd624 2020-10-07 op warnx("tls_write: %s", tls_error(client->ctx));
331 592fd624 2020-10-07 op goodbye(fds, client);
334 592fd624 2020-10-07 op case TLS_WANT_POLLIN:
335 592fd624 2020-10-07 op case TLS_WANT_POLLOUT:
336 09a1b978 2020-10-07 op fds->events = ret == TLS_WANT_POLLIN ? POLLIN : POLLOUT;
337 09a1b978 2020-10-07 op if (lseek(client->fd, -1 * len, SEEK_CUR) == -1) {
338 09a1b978 2020-10-07 op warnx("lseek");
339 09a1b978 2020-10-07 op goodbye(fds, client);
353 592fd624 2020-10-07 op send_dir(char *path, struct pollfd *fds, struct client *client)
355 3e4749f7 2020-10-02 op char fpath[PATHBUF];
358 3e4749f7 2020-10-02 op bzero(fpath, PATHBUF);
360 9c56b0a7 2020-10-14 op if (path[0] != '.')
361 3e4749f7 2020-10-02 op fpath[0] = '.';
363 3e4749f7 2020-10-02 op /* this cannot fail since sizeof(fpath) > maxlen of path */
364 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
365 3e4749f7 2020-10-02 op len = strlen(fpath);
367 3e4749f7 2020-10-02 op /* add a trailing / in case. */
368 3e4749f7 2020-10-02 op if (fpath[len-1] != '/') {
369 3e4749f7 2020-10-02 op fpath[len] = '/';
372 3e4749f7 2020-10-02 op strlcat(fpath, "index.gmi", sizeof(fpath));
374 592fd624 2020-10-07 op send_file(fpath, fds, client);
378 592fd624 2020-10-07 op handle(struct pollfd *fds, struct client *client)
380 592fd624 2020-10-07 op char buf[GEMINI_URL_LEN];
383 592fd624 2020-10-07 op switch (client->state) {
385 592fd624 2020-10-07 op bzero(buf, GEMINI_URL_LEN);
386 592fd624 2020-10-07 op switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
388 592fd624 2020-10-07 op warnx("tls_read: %s", tls_error(client->ctx));
389 592fd624 2020-10-07 op goodbye(fds, client);
392 592fd624 2020-10-07 op case TLS_WANT_POLLIN:
393 592fd624 2020-10-07 op fds->events = POLLIN;
396 592fd624 2020-10-07 op case TLS_WANT_POLLOUT:
397 592fd624 2020-10-07 op fds->events = POLLOUT;
401 592fd624 2020-10-07 op if (!url_trim(buf)) {
402 592fd624 2020-10-07 op if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
404 592fd624 2020-10-07 op goodbye(fds, client);
408 592fd624 2020-10-07 op if ((path = url_start_of_request(buf)) == NULL) {
409 592fd624 2020-10-07 op if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
411 592fd624 2020-10-07 op goodbye(fds, client);
415 592fd624 2020-10-07 op adjust_path(path);
416 592fd624 2020-10-07 op fprintf(stderr, "requested path: %s\n", path);
418 592fd624 2020-10-07 op if (path_isdir(path))
419 592fd624 2020-10-07 op send_dir(path, fds, client);
421 592fd624 2020-10-07 op send_file(path, fds, client);
424 592fd624 2020-10-07 op case S_INITIALIZING:
425 592fd624 2020-10-07 op if (!start_reply(fds, client, client->code, client->meta))
428 592fd624 2020-10-07 op if (client->code != SUCCESS) {
429 592fd624 2020-10-07 op /* we don't need a body */
430 592fd624 2020-10-07 op goodbye(fds, client);
434 592fd624 2020-10-07 op client->state = S_SENDING;
436 592fd624 2020-10-07 op /* fallthrough */
438 592fd624 2020-10-07 op case S_SENDING:
439 592fd624 2020-10-07 op send_file(NULL, fds, client);
442 592fd624 2020-10-07 op case S_CLOSING:
443 592fd624 2020-10-07 op goodbye(fds, client);
447 592fd624 2020-10-07 op /* unreachable */
453 592fd624 2020-10-07 op mark_nonblock(int fd)
457 592fd624 2020-10-07 op if ((flags = fcntl(fd, F_GETFL)) == -1)
458 592fd624 2020-10-07 op err(1, "fcntl(F_GETFL)");
459 592fd624 2020-10-07 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
460 592fd624 2020-10-07 op err(1, "fcntl(F_SETFL)");
464 3e4749f7 2020-10-02 op make_socket(int port)
467 3e4749f7 2020-10-02 op struct sockaddr_in addr;
469 3e4749f7 2020-10-02 op if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
470 3e4749f7 2020-10-02 op err(1, "socket");
473 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
474 3e4749f7 2020-10-02 op err(1, "setsockopt(SO_REUSEADDR)");
477 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
478 3e4749f7 2020-10-02 op err(1, "setsockopt(SO_REUSEPORT)");
480 592fd624 2020-10-07 op mark_nonblock(sock);
482 3e4749f7 2020-10-02 op bzero(&addr, sizeof(addr));
483 3e4749f7 2020-10-02 op addr.sin_family = AF_INET;
484 3e4749f7 2020-10-02 op addr.sin_port = htons(port);
485 3e4749f7 2020-10-02 op addr.sin_addr.s_addr = INADDR_ANY;
487 3e4749f7 2020-10-02 op if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
488 3e4749f7 2020-10-02 op err(1, "bind");
490 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
491 3e4749f7 2020-10-02 op err(1, "listen");
497 592fd624 2020-10-07 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
500 592fd624 2020-10-07 op struct sockaddr_in addr;
501 3e4749f7 2020-10-02 op socklen_t len;
503 592fd624 2020-10-07 op len = sizeof(addr);
504 592fd624 2020-10-07 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
505 592fd624 2020-10-07 op if (errno == EWOULDBLOCK)
507 592fd624 2020-10-07 op err(1, "accept");
510 592fd624 2020-10-07 op mark_nonblock(fd);
512 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
513 592fd624 2020-10-07 op if (fds[i].fd == -1) {
514 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
515 592fd624 2020-10-07 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
516 592fd624 2020-10-07 op break; /* goodbye fd! */
518 592fd624 2020-10-07 op fds[i].fd = fd;
519 592fd624 2020-10-07 op fds[i].events = POLLIN;
521 592fd624 2020-10-07 op clients[i].state = S_OPEN;
522 592fd624 2020-10-07 op clients[i].fd = -1;
532 592fd624 2020-10-07 op goodbye(struct pollfd *pfd, struct client *c)
536 592fd624 2020-10-07 op c->state = S_CLOSING;
538 592fd624 2020-10-07 op ret = tls_close(c->ctx);
539 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
540 592fd624 2020-10-07 op pfd->events = POLLIN;
543 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
544 592fd624 2020-10-07 op pfd->events = POLLOUT;
548 592fd624 2020-10-07 op tls_free(c->ctx);
549 592fd624 2020-10-07 op c->ctx = NULL;
551 592fd624 2020-10-07 op if (c->fd != -1)
552 592fd624 2020-10-07 op close(c->fd);
554 592fd624 2020-10-07 op close(pfd->fd);
555 592fd624 2020-10-07 op pfd->fd = -1;
559 592fd624 2020-10-07 op loop(struct tls *ctx, int sock)
562 592fd624 2020-10-07 op struct client clients[MAX_USERS];
563 592fd624 2020-10-07 op struct pollfd fds[MAX_USERS];
565 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
566 592fd624 2020-10-07 op fds[i].fd = -1;
567 592fd624 2020-10-07 op fds[i].events = POLLIN;
568 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
571 592fd624 2020-10-07 op fds[0].fd = sock;
574 592fd624 2020-10-07 op if ((todo = poll(fds, MAX_USERS, INFTIM)) == -1)
575 592fd624 2020-10-07 op err(1, "poll");
577 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; i++) {
578 592fd624 2020-10-07 op assert(i < MAX_USERS);
580 592fd624 2020-10-07 op if (fds[i].revents == 0)
583 592fd624 2020-10-07 op if (fds[i].revents & (POLLERR|POLLNVAL))
584 592fd624 2020-10-07 op err(1, "bad fd %d", fds[i].fd);
586 592fd624 2020-10-07 op if (fds[i].revents & POLLHUP) {
587 592fd624 2020-10-07 op goodbye(&fds[i], &clients[i]);
593 592fd624 2020-10-07 op if (i == 0) { /* new client */
594 592fd624 2020-10-07 op do_accept(sock, ctx, fds, clients);
598 592fd624 2020-10-07 op handle(&fds[i], &clients[i]);
604 3e4749f7 2020-10-02 op usage(const char *me)
606 3e4749f7 2020-10-02 op fprintf(stderr,
607 3e4749f7 2020-10-02 op "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem]\n",
612 3e4749f7 2020-10-02 op main(int argc, char **argv)
614 3e4749f7 2020-10-02 op const char *cert = "cert.pem", *key = "key.pem", *dir = "docs";
615 3e4749f7 2020-10-02 op struct tls *ctx = NULL;
616 3e4749f7 2020-10-02 op struct tls_config *conf;
617 3e4749f7 2020-10-02 op int sock, ch;
619 3e4749f7 2020-10-02 op while ((ch = getopt(argc, argv, "c:d:hk:")) != -1) {
620 3e4749f7 2020-10-02 op switch (ch) {
622 3e4749f7 2020-10-02 op cert = optarg;
626 3e4749f7 2020-10-02 op dir = optarg;
630 3e4749f7 2020-10-02 op usage(*argv);
634 3e4749f7 2020-10-02 op key = optarg;
638 3e4749f7 2020-10-02 op usage(*argv);
643 3e4749f7 2020-10-02 op if ((conf = tls_config_new()) == NULL)
644 3e4749f7 2020-10-02 op err(1, "tls_config_new");
646 0d8ca45a 2020-10-03 op if (tls_config_set_protocols(conf,
647 0d8ca45a 2020-10-03 op TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
648 0d8ca45a 2020-10-03 op err(1, "tls_config_set_protocols");
650 4d4f0e19 2020-10-03 op if (tls_config_set_cert_file(conf, cert) == -1)
651 4d4f0e19 2020-10-03 op err(1, "tls_config_set_cert_file: %s", cert);
653 4d4f0e19 2020-10-03 op if (tls_config_set_key_file(conf, key) == -1)
654 4d4f0e19 2020-10-03 op err(1, "tls_config_set_key_file: %s", key);
656 3e4749f7 2020-10-02 op if ((ctx = tls_server()) == NULL)
657 3e4749f7 2020-10-02 op err(1, "tls_server");
659 3e4749f7 2020-10-02 op if (tls_configure(ctx, conf) == -1)
660 3e4749f7 2020-10-02 op errx(1, "tls_configure: %s", tls_error(ctx));
662 3e4749f7 2020-10-02 op sock = make_socket(1965);
664 3e4749f7 2020-10-02 op if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
665 3e4749f7 2020-10-02 op err(1, "open: %s", dir);
667 3e4749f7 2020-10-02 op if (unveil(dir, "r") == -1)
668 3e4749f7 2020-10-02 op err(1, "unveil");
670 3e4749f7 2020-10-02 op if (pledge("stdio rpath inet", "") == -1)
671 3e4749f7 2020-10-02 op err(1, "pledge");
673 3e4749f7 2020-10-02 op loop(ctx, sock);
676 3e4749f7 2020-10-02 op tls_free(ctx);
677 3e4749f7 2020-10-02 op tls_config_free(conf);