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 f28f9311 2020-10-14 op #include <sys/mman.h>
18 3e4749f7 2020-10-02 op #include <sys/socket.h>
19 3e4749f7 2020-10-02 op #include <sys/stat.h>
21 3e4749f7 2020-10-02 op #include <netinet/in.h>
23 3e4749f7 2020-10-02 op #include <assert.h>
24 3e4749f7 2020-10-02 op #include <err.h>
25 592fd624 2020-10-07 op #include <errno.h>
26 3e4749f7 2020-10-02 op #include <fcntl.h>
27 592fd624 2020-10-07 op #include <poll.h>
28 3e4749f7 2020-10-02 op #include <stdio.h>
29 3e4749f7 2020-10-02 op #include <stdlib.h>
30 3e4749f7 2020-10-02 op #include <string.h>
31 3e4749f7 2020-10-02 op #include <tls.h>
32 3e4749f7 2020-10-02 op #include <unistd.h>
34 3e4749f7 2020-10-02 op #ifndef __OpenBSD__
35 3e4749f7 2020-10-02 op # define pledge(a, b) 0
36 3e4749f7 2020-10-02 op # define unveil(a, b) 0
37 3e4749f7 2020-10-02 op #endif /* __OpenBSD__ */
39 592fd624 2020-10-07 op #ifndef INFTIM
40 592fd624 2020-10-07 op # define INFTIM -1
41 592fd624 2020-10-07 op #endif /* INFTIM */
43 3e4749f7 2020-10-02 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
45 4d4f0e19 2020-10-03 op /* large enough to hold a copy of a gemini URL and still have extra room */
46 d95b77a0 2020-10-07 op #define PATHBUF 2048
48 3e4749f7 2020-10-02 op #define SUCCESS 20
49 3e4749f7 2020-10-02 op #define NOT_FOUND 51
50 3e4749f7 2020-10-02 op #define BAD_REQUEST 59
52 592fd624 2020-10-07 op #ifndef MAX_USERS
53 592fd624 2020-10-07 op #define MAX_USERS 64
58 592fd624 2020-10-07 op S_INITIALIZING,
63 592fd624 2020-10-07 op struct client {
64 592fd624 2020-10-07 op struct tls *ctx;
67 592fd624 2020-10-07 op const char *meta;
69 f28f9311 2020-10-14 op void *buf, *i;
70 f28f9311 2020-10-14 op ssize_t len, off;
73 cc68fe70 2020-10-07 op struct etm { /* file extension to mime */
74 cc68fe70 2020-10-07 op const char *mime;
75 cc68fe70 2020-10-07 op const char *ext;
76 cc68fe70 2020-10-07 op } filetypes[] = {
77 cc68fe70 2020-10-07 op {"application/pdf", "pdf"},
79 cc68fe70 2020-10-07 op {"image/gif", "gif"},
80 cc68fe70 2020-10-07 op {"image/jpeg", "jpg"},
81 cc68fe70 2020-10-07 op {"image/jpeg", "jpeg"},
82 cc68fe70 2020-10-07 op {"image/png", "png"},
83 cc68fe70 2020-10-07 op {"image/svg+xml", "svg"},
85 cc68fe70 2020-10-07 op {"text/gemini", "gemini"},
86 cc68fe70 2020-10-07 op {"text/gemini", "gmi"},
87 cc68fe70 2020-10-07 op {"text/markdown", "markdown"},
88 cc68fe70 2020-10-07 op {"text/markdown", "md"},
89 cc68fe70 2020-10-07 op {"text/plain", "txt"},
96 592fd624 2020-10-07 op char *url_after_proto(char*);
97 592fd624 2020-10-07 op char *url_start_of_request(char*);
98 592fd624 2020-10-07 op int url_trim(char*);
99 592fd624 2020-10-07 op void adjust_path(char*);
100 592fd624 2020-10-07 op int path_isdir(char*);
101 f28f9311 2020-10-14 op ssize_t filesize(int);
103 592fd624 2020-10-07 op int start_reply(struct pollfd*, struct client*, int, const char*);
104 592fd624 2020-10-07 op int isdir(int);
105 cc68fe70 2020-10-07 op const char *path_ext(const char*);
106 cc68fe70 2020-10-07 op const char *mime(const char*);
107 f28f9311 2020-10-14 op int open_file(char*, struct pollfd*, struct client*);
108 592fd624 2020-10-07 op void send_file(char*, struct pollfd*, struct client*);
109 592fd624 2020-10-07 op void send_dir(char*, struct pollfd*, struct client*);
110 592fd624 2020-10-07 op void handle(struct pollfd*, struct client*);
112 592fd624 2020-10-07 op void mark_nonblock(int);
113 592fd624 2020-10-07 op int make_soket(int);
114 592fd624 2020-10-07 op void do_accept(int, struct tls*, struct pollfd*, struct client*);
115 592fd624 2020-10-07 op void goodbye(struct pollfd*, struct client*);
116 592fd624 2020-10-07 op void loop(struct tls*, int);
118 592fd624 2020-10-07 op void usage(const char*);
121 3e4749f7 2020-10-02 op url_after_proto(char *url)
124 3e4749f7 2020-10-02 op const char *proto = "gemini";
125 3e4749f7 2020-10-02 op const char *marker = "://";
127 3e4749f7 2020-10-02 op if ((s = strstr(url, marker)) == NULL)
130 3e4749f7 2020-10-02 op /* not a gemini:// URL */
132 3e4749f7 2020-10-02 op if (s - strlen(proto) < url)
135 3e4749f7 2020-10-02 op /* if (strcmp(s - strlen(proto), proto)) */
136 3e4749f7 2020-10-02 op /* return NULL; */
138 3e4749f7 2020-10-02 op /* a valid gemini:// URL */
139 3e4749f7 2020-10-02 op return s + strlen(marker);
143 3e4749f7 2020-10-02 op url_start_of_request(char *url)
147 3e4749f7 2020-10-02 op if ((s = url_after_proto(url)) == NULL)
150 3e4749f7 2020-10-02 op if ((t = strstr(s, "/")) == NULL)
151 3e4749f7 2020-10-02 op return s + strlen(s);
156 3e4749f7 2020-10-02 op url_trim(char *url)
158 3e4749f7 2020-10-02 op const char *e = "\r\n";
161 3e4749f7 2020-10-02 op if ((s = strstr(url, e)) == NULL)
166 3e4749f7 2020-10-02 op if (s[2] != '\0') {
167 4d4f0e19 2020-10-03 op fprintf(stderr, "the request was longer than 1024 bytes\n");
175 3e4749f7 2020-10-02 op adjust_path(char *path)
180 f28f9311 2020-10-14 op /* /.. -> / */
181 3e4749f7 2020-10-02 op len = strlen(path);
182 3e4749f7 2020-10-02 op if (len >= 3) {
183 3e4749f7 2020-10-02 op if (!strcmp(&path[len-3], "/..")) {
184 3e4749f7 2020-10-02 op path[len-2] = '\0';
188 3e4749f7 2020-10-02 op /* if the path is only `..` trim out and exit */
189 3e4749f7 2020-10-02 op if (!strcmp(path, "..")) {
190 3e4749f7 2020-10-02 op path[0] = '\0';
194 3e4749f7 2020-10-02 op /* remove every ../ in the path */
196 3e4749f7 2020-10-02 op if ((s = strstr(path, "../")) == NULL)
198 67328d23 2020-10-03 op memmove(s, s+3, strlen(s)+1); /* copy also the \0 */
203 3e4749f7 2020-10-02 op path_isdir(char *path)
205 3e4749f7 2020-10-02 op if (*path == '\0')
207 4d4f0e19 2020-10-03 op return path[strlen(path)-1] == '/';
211 592fd624 2020-10-07 op start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
213 3e4749f7 2020-10-02 op char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
217 592fd624 2020-10-07 op client->code = code;
218 592fd624 2020-10-07 op client->meta = reason;
219 592fd624 2020-10-07 op client->state = S_INITIALIZING;
221 3e4749f7 2020-10-02 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
222 3e4749f7 2020-10-02 op assert(len < (int)sizeof(buf));
223 592fd624 2020-10-07 op ret = tls_write(client->ctx, buf, len);
224 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
225 592fd624 2020-10-07 op pfd->events = POLLIN;
229 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
230 592fd624 2020-10-07 op pfd->events = POLLOUT;
238 3e4749f7 2020-10-02 op isdir(int fd)
240 3e4749f7 2020-10-02 op struct stat sb;
242 3e4749f7 2020-10-02 op if (fstat(fd, &sb) == -1) {
243 3e4749f7 2020-10-02 op warn("fstat");
244 4d4f0e19 2020-10-03 op return 1; /* we'll probably fail later on anyway */
247 3e4749f7 2020-10-02 op return S_ISDIR(sb.st_mode);
251 f28f9311 2020-10-14 op filesize(int fd)
255 f28f9311 2020-10-14 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
257 f28f9311 2020-10-14 op if (lseek(fd, 0, SEEK_SET) == -1)
263 cc68fe70 2020-10-07 op path_ext(const char *path)
265 cc68fe70 2020-10-07 op const char *end;
267 cc68fe70 2020-10-07 op end = path + strlen(path)-1; /* the last byte before the NUL */
268 cc68fe70 2020-10-07 op for (; end != path; --end) {
269 cc68fe70 2020-10-07 op if (*end == '.')
270 cc68fe70 2020-10-07 op return end+1;
271 cc68fe70 2020-10-07 op if (*end == '/')
279 cc68fe70 2020-10-07 op mime(const char *path)
281 cc68fe70 2020-10-07 op const char *ext, *def = "application/octet-stream";
282 cc68fe70 2020-10-07 op struct etm *t;
284 cc68fe70 2020-10-07 op if ((ext = path_ext(path)) == NULL)
287 cc68fe70 2020-10-07 op for (t = filetypes; t->mime != NULL; ++t)
288 cc68fe70 2020-10-07 op if (!strcmp(ext, t->ext))
289 cc68fe70 2020-10-07 op return t->mime;
295 f28f9311 2020-10-14 op open_file(char *path, struct pollfd *fds, struct client *c)
297 3e4749f7 2020-10-02 op char fpath[PATHBUF];
299 f28f9311 2020-10-14 op assert(path != NULL);
301 f28f9311 2020-10-14 op bzero(fpath, sizeof(fpath));
303 f28f9311 2020-10-14 op if (*path != '.')
304 f28f9311 2020-10-14 op fpath[0] = '.';
305 f28f9311 2020-10-14 op strlcat(fpath, path, PATHBUF);
307 f28f9311 2020-10-14 op if ((c->fd = openat(dirfd, fpath, O_RDONLY | O_NOFOLLOW)) == -1) {
308 f28f9311 2020-10-14 op warn("open: %s", fpath);
309 f28f9311 2020-10-14 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
311 f28f9311 2020-10-14 op goodbye(fds, c);
315 f28f9311 2020-10-14 op if (isdir(c->fd)) {
316 f28f9311 2020-10-14 op warnx("%s is a directory, trying %s/index.gmi", fpath, fpath);
317 f28f9311 2020-10-14 op close(c->fd);
319 f28f9311 2020-10-14 op send_dir(fpath, fds, c);
323 f28f9311 2020-10-14 op if ((c->len = filesize(c->fd)) == -1) {
324 f28f9311 2020-10-14 op warn("filesize: %s", fpath);
325 f28f9311 2020-10-14 op goodbye(fds, c);
329 f28f9311 2020-10-14 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
330 f28f9311 2020-10-14 op c->fd, 0)) == MAP_FAILED) {
331 f28f9311 2020-10-14 op warn("mmap: %s", fpath);
332 f28f9311 2020-10-14 op goodbye(fds, c);
335 f28f9311 2020-10-14 op c->i = c->buf;
337 f28f9311 2020-10-14 op return start_reply(fds, c, SUCCESS, mime(fpath));
341 f28f9311 2020-10-14 op send_file(char *path, struct pollfd *fds, struct client *c)
343 f28f9311 2020-10-14 op ssize_t ret, len;
345 f28f9311 2020-10-14 op if (c->fd == -1) {
346 f28f9311 2020-10-14 op if (!open_file(path, fds, c))
348 f28f9311 2020-10-14 op c->state = S_SENDING;
351 f28f9311 2020-10-14 op len = (c->buf + c->len) - c->i;
353 f28f9311 2020-10-14 op while (len > 0) {
354 f28f9311 2020-10-14 op switch (ret = tls_write(c->ctx, c->i, len)) {
356 f28f9311 2020-10-14 op warnx("tls_write: %s", tls_error(c->ctx));
357 f28f9311 2020-10-14 op goodbye(fds, c);
360 f28f9311 2020-10-14 op case TLS_WANT_POLLIN:
361 f28f9311 2020-10-14 op fds->events = POLLIN;
364 f28f9311 2020-10-14 op case TLS_WANT_POLLOUT:
365 f28f9311 2020-10-14 op fds->events = POLLOUT;
375 f28f9311 2020-10-14 op goodbye(fds, c);
379 592fd624 2020-10-07 op send_dir(char *path, struct pollfd *fds, struct client *client)
381 3e4749f7 2020-10-02 op char fpath[PATHBUF];
384 3e4749f7 2020-10-02 op bzero(fpath, PATHBUF);
386 9c56b0a7 2020-10-14 op if (path[0] != '.')
387 3e4749f7 2020-10-02 op fpath[0] = '.';
389 3e4749f7 2020-10-02 op /* this cannot fail since sizeof(fpath) > maxlen of path */
390 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
391 3e4749f7 2020-10-02 op len = strlen(fpath);
393 3e4749f7 2020-10-02 op /* add a trailing / in case. */
394 3e4749f7 2020-10-02 op if (fpath[len-1] != '/') {
395 3e4749f7 2020-10-02 op fpath[len] = '/';
398 3e4749f7 2020-10-02 op strlcat(fpath, "index.gmi", sizeof(fpath));
400 592fd624 2020-10-07 op send_file(fpath, fds, client);
404 592fd624 2020-10-07 op handle(struct pollfd *fds, struct client *client)
406 592fd624 2020-10-07 op char buf[GEMINI_URL_LEN];
409 592fd624 2020-10-07 op switch (client->state) {
411 592fd624 2020-10-07 op bzero(buf, GEMINI_URL_LEN);
412 592fd624 2020-10-07 op switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
414 592fd624 2020-10-07 op warnx("tls_read: %s", tls_error(client->ctx));
415 592fd624 2020-10-07 op goodbye(fds, client);
418 592fd624 2020-10-07 op case TLS_WANT_POLLIN:
419 592fd624 2020-10-07 op fds->events = POLLIN;
422 592fd624 2020-10-07 op case TLS_WANT_POLLOUT:
423 592fd624 2020-10-07 op fds->events = POLLOUT;
427 592fd624 2020-10-07 op if (!url_trim(buf)) {
428 592fd624 2020-10-07 op if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
430 592fd624 2020-10-07 op goodbye(fds, client);
434 592fd624 2020-10-07 op if ((path = url_start_of_request(buf)) == NULL) {
435 592fd624 2020-10-07 op if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
437 592fd624 2020-10-07 op goodbye(fds, client);
441 592fd624 2020-10-07 op adjust_path(path);
442 592fd624 2020-10-07 op fprintf(stderr, "requested path: %s\n", path);
444 592fd624 2020-10-07 op if (path_isdir(path))
445 592fd624 2020-10-07 op send_dir(path, fds, client);
447 592fd624 2020-10-07 op send_file(path, fds, client);
450 592fd624 2020-10-07 op case S_INITIALIZING:
451 592fd624 2020-10-07 op if (!start_reply(fds, client, client->code, client->meta))
454 592fd624 2020-10-07 op if (client->code != SUCCESS) {
455 592fd624 2020-10-07 op /* we don't need a body */
456 592fd624 2020-10-07 op goodbye(fds, client);
460 592fd624 2020-10-07 op client->state = S_SENDING;
462 592fd624 2020-10-07 op /* fallthrough */
464 592fd624 2020-10-07 op case S_SENDING:
465 592fd624 2020-10-07 op send_file(NULL, fds, client);
468 592fd624 2020-10-07 op case S_CLOSING:
469 592fd624 2020-10-07 op goodbye(fds, client);
473 592fd624 2020-10-07 op /* unreachable */
479 592fd624 2020-10-07 op mark_nonblock(int fd)
483 592fd624 2020-10-07 op if ((flags = fcntl(fd, F_GETFL)) == -1)
484 592fd624 2020-10-07 op err(1, "fcntl(F_GETFL)");
485 592fd624 2020-10-07 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
486 592fd624 2020-10-07 op err(1, "fcntl(F_SETFL)");
490 3e4749f7 2020-10-02 op make_socket(int port)
493 3e4749f7 2020-10-02 op struct sockaddr_in addr;
495 3e4749f7 2020-10-02 op if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
496 f28f9311 2020-10-14 op err(1, "socket");
499 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
500 3e4749f7 2020-10-02 op err(1, "setsockopt(SO_REUSEADDR)");
503 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
504 3e4749f7 2020-10-02 op err(1, "setsockopt(SO_REUSEPORT)");
506 592fd624 2020-10-07 op mark_nonblock(sock);
508 3e4749f7 2020-10-02 op bzero(&addr, sizeof(addr));
509 3e4749f7 2020-10-02 op addr.sin_family = AF_INET;
510 3e4749f7 2020-10-02 op addr.sin_port = htons(port);
511 3e4749f7 2020-10-02 op addr.sin_addr.s_addr = INADDR_ANY;
513 3e4749f7 2020-10-02 op if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
514 f28f9311 2020-10-14 op err(1, "bind");
516 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
517 f28f9311 2020-10-14 op err(1, "listen");
523 592fd624 2020-10-07 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
526 592fd624 2020-10-07 op struct sockaddr_in addr;
527 3e4749f7 2020-10-02 op socklen_t len;
529 592fd624 2020-10-07 op len = sizeof(addr);
530 592fd624 2020-10-07 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
531 592fd624 2020-10-07 op if (errno == EWOULDBLOCK)
533 592fd624 2020-10-07 op err(1, "accept");
536 592fd624 2020-10-07 op mark_nonblock(fd);
538 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
539 592fd624 2020-10-07 op if (fds[i].fd == -1) {
540 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
541 592fd624 2020-10-07 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
542 592fd624 2020-10-07 op break; /* goodbye fd! */
544 592fd624 2020-10-07 op fds[i].fd = fd;
545 592fd624 2020-10-07 op fds[i].events = POLLIN;
547 592fd624 2020-10-07 op clients[i].state = S_OPEN;
548 592fd624 2020-10-07 op clients[i].fd = -1;
549 f28f9311 2020-10-14 op clients[i].buf = MAP_FAILED;
559 592fd624 2020-10-07 op goodbye(struct pollfd *pfd, struct client *c)
563 592fd624 2020-10-07 op c->state = S_CLOSING;
565 592fd624 2020-10-07 op ret = tls_close(c->ctx);
566 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
567 592fd624 2020-10-07 op pfd->events = POLLIN;
570 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
571 592fd624 2020-10-07 op pfd->events = POLLOUT;
575 592fd624 2020-10-07 op tls_free(c->ctx);
576 592fd624 2020-10-07 op c->ctx = NULL;
578 f28f9311 2020-10-14 op if (c->buf != MAP_FAILED)
579 f28f9311 2020-10-14 op munmap(c->buf, c->len);
581 592fd624 2020-10-07 op if (c->fd != -1)
582 592fd624 2020-10-07 op close(c->fd);
584 592fd624 2020-10-07 op close(pfd->fd);
585 592fd624 2020-10-07 op pfd->fd = -1;
589 592fd624 2020-10-07 op loop(struct tls *ctx, int sock)
592 592fd624 2020-10-07 op struct client clients[MAX_USERS];
593 592fd624 2020-10-07 op struct pollfd fds[MAX_USERS];
595 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
596 592fd624 2020-10-07 op fds[i].fd = -1;
597 592fd624 2020-10-07 op fds[i].events = POLLIN;
598 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
601 592fd624 2020-10-07 op fds[0].fd = sock;
604 f28f9311 2020-10-14 op if ((todo = poll(fds, MAX_USERS, INFTIM)) == -1)
605 592fd624 2020-10-07 op err(1, "poll");
607 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; i++) {
608 592fd624 2020-10-07 op assert(i < MAX_USERS);
610 592fd624 2020-10-07 op if (fds[i].revents == 0)
613 592fd624 2020-10-07 op if (fds[i].revents & (POLLERR|POLLNVAL))
614 592fd624 2020-10-07 op err(1, "bad fd %d", fds[i].fd);
616 592fd624 2020-10-07 op if (fds[i].revents & POLLHUP) {
617 592fd624 2020-10-07 op goodbye(&fds[i], &clients[i]);
623 592fd624 2020-10-07 op if (i == 0) { /* new client */
624 592fd624 2020-10-07 op do_accept(sock, ctx, fds, clients);
628 592fd624 2020-10-07 op handle(&fds[i], &clients[i]);
634 3e4749f7 2020-10-02 op usage(const char *me)
636 3e4749f7 2020-10-02 op fprintf(stderr,
637 3e4749f7 2020-10-02 op "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem]\n",
642 3e4749f7 2020-10-02 op main(int argc, char **argv)
644 3e4749f7 2020-10-02 op const char *cert = "cert.pem", *key = "key.pem", *dir = "docs";
645 3e4749f7 2020-10-02 op struct tls *ctx = NULL;
646 3e4749f7 2020-10-02 op struct tls_config *conf;
647 3e4749f7 2020-10-02 op int sock, ch;
649 3e4749f7 2020-10-02 op while ((ch = getopt(argc, argv, "c:d:hk:")) != -1) {
650 3e4749f7 2020-10-02 op switch (ch) {
652 3e4749f7 2020-10-02 op cert = optarg;
656 3e4749f7 2020-10-02 op dir = optarg;
660 3e4749f7 2020-10-02 op usage(*argv);
664 3e4749f7 2020-10-02 op key = optarg;
668 3e4749f7 2020-10-02 op usage(*argv);
673 3e4749f7 2020-10-02 op if ((conf = tls_config_new()) == NULL)
674 3e4749f7 2020-10-02 op err(1, "tls_config_new");
676 0d8ca45a 2020-10-03 op if (tls_config_set_protocols(conf,
677 0d8ca45a 2020-10-03 op TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
678 0d8ca45a 2020-10-03 op err(1, "tls_config_set_protocols");
680 4d4f0e19 2020-10-03 op if (tls_config_set_cert_file(conf, cert) == -1)
681 4d4f0e19 2020-10-03 op err(1, "tls_config_set_cert_file: %s", cert);
683 4d4f0e19 2020-10-03 op if (tls_config_set_key_file(conf, key) == -1)
684 4d4f0e19 2020-10-03 op err(1, "tls_config_set_key_file: %s", key);
686 3e4749f7 2020-10-02 op if ((ctx = tls_server()) == NULL)
687 3e4749f7 2020-10-02 op err(1, "tls_server");
689 3e4749f7 2020-10-02 op if (tls_configure(ctx, conf) == -1)
690 3e4749f7 2020-10-02 op errx(1, "tls_configure: %s", tls_error(ctx));
692 3e4749f7 2020-10-02 op sock = make_socket(1965);
694 3e4749f7 2020-10-02 op if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
695 3e4749f7 2020-10-02 op err(1, "open: %s", dir);
697 3e4749f7 2020-10-02 op if (unveil(dir, "r") == -1)
698 3e4749f7 2020-10-02 op err(1, "unveil");
700 3e4749f7 2020-10-02 op if (pledge("stdio rpath inet", "") == -1)
701 3e4749f7 2020-10-02 op err(1, "pledge");
703 3e4749f7 2020-10-02 op loop(ctx, sock);
706 3e4749f7 2020-10-02 op tls_free(ctx);
707 3e4749f7 2020-10-02 op tls_config_free(conf);