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 <assert.h>
22 3e4749f7 2020-10-02 op #include <err.h>
23 592fd624 2020-10-07 op #include <errno.h>
24 3e4749f7 2020-10-02 op #include <fcntl.h>
25 231bfcdc 2020-12-21 op #include <limits.h>
26 0cf902af 2020-11-03 op #include <signal.h>
27 3e4749f7 2020-10-02 op #include <string.h>
29 488f059a 2020-12-24 op #include "gmid.h"
31 d7802bb4 2020-12-02 op const char *dir, *cgi;
34 d7802bb4 2020-12-02 op int foreground;
35 120381c9 2020-11-06 op int connected_clients;
38 120381c9 2020-11-06 op siginfo_handler(int sig)
44 72f653b6 2020-11-06 op starts_with(const char *str, const char *prefix)
48 72f653b6 2020-11-06 op for (i = 0; prefix[i] != '\0'; ++i)
49 72f653b6 2020-11-06 op if (str[i] != prefix[i])
55 3e4749f7 2020-10-02 op url_after_proto(char *url)
58 c603f126 2020-11-18 op const char *proto = "gemini:";
59 ae2ad03e 2020-11-18 op const char *marker = "//";
61 3c19febb 2020-11-06 op /* a relative URL */
62 3e4749f7 2020-10-02 op if ((s = strstr(url, marker)) == NULL)
66 ae2ad03e 2020-11-18 op * if a protocol is not specified, gemini should be implied:
67 ae2ad03e 2020-11-18 op * this handles the case of //example.com
70 ae2ad03e 2020-11-18 op return s + strlen(marker);
72 3c19febb 2020-11-06 op if (s - strlen(proto) != url)
75 72f653b6 2020-11-06 op if (!starts_with(url, proto))
78 3e4749f7 2020-10-02 op return s + strlen(marker);
82 3e4749f7 2020-10-02 op url_start_of_request(char *url)
86 3e4749f7 2020-10-02 op if ((s = url_after_proto(url)) == NULL)
89 7cd6880b 2020-11-18 op /* non-absolute URL */
93 3e4749f7 2020-10-02 op if ((t = strstr(s, "/")) == NULL)
94 3e4749f7 2020-10-02 op return s + strlen(s);
99 2c3a40fa 2020-11-06 op url_trim(struct client *c, char *url)
101 3e4749f7 2020-10-02 op const char *e = "\r\n";
104 3e4749f7 2020-10-02 op if ((s = strstr(url, e)) == NULL)
109 3e4749f7 2020-10-02 op if (s[2] != '\0') {
110 d7802bb4 2020-12-02 op LOGE(c, "%s", "request longer than 1024 bytes");
118 3e4749f7 2020-10-02 op adjust_path(char *path)
120 aa0fe0cf 2020-11-06 op char *s, *query;
123 aa0fe0cf 2020-11-06 op if ((query = strchr(path, '?')) != NULL) {
124 aa0fe0cf 2020-11-06 op *query = '\0';
128 f28f9311 2020-10-14 op /* /.. -> / */
129 3e4749f7 2020-10-02 op len = strlen(path);
130 3e4749f7 2020-10-02 op if (len >= 3) {
131 3e4749f7 2020-10-02 op if (!strcmp(&path[len-3], "/..")) {
132 3e4749f7 2020-10-02 op path[len-2] = '\0';
136 3e4749f7 2020-10-02 op /* if the path is only `..` trim out and exit */
137 3e4749f7 2020-10-02 op if (!strcmp(path, "..")) {
138 3e4749f7 2020-10-02 op path[0] = '\0';
139 aa0fe0cf 2020-11-06 op return query;
142 3e4749f7 2020-10-02 op /* remove every ../ in the path */
144 3e4749f7 2020-10-02 op if ((s = strstr(path, "../")) == NULL)
145 aa0fe0cf 2020-11-06 op return query;
146 67328d23 2020-10-03 op memmove(s, s+3, strlen(s)+1); /* copy also the \0 */
151 592fd624 2020-10-07 op start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
153 3e4749f7 2020-10-02 op char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
157 592fd624 2020-10-07 op client->code = code;
158 592fd624 2020-10-07 op client->meta = reason;
159 592fd624 2020-10-07 op client->state = S_INITIALIZING;
161 3e4749f7 2020-10-02 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
162 3e4749f7 2020-10-02 op assert(len < (int)sizeof(buf));
163 592fd624 2020-10-07 op ret = tls_write(client->ctx, buf, len);
164 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
165 592fd624 2020-10-07 op pfd->events = POLLIN;
169 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
170 592fd624 2020-10-07 op pfd->events = POLLOUT;
178 f28f9311 2020-10-14 op filesize(int fd)
182 f28f9311 2020-10-14 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
184 f28f9311 2020-10-14 op if (lseek(fd, 0, SEEK_SET) == -1)
190 cc68fe70 2020-10-07 op path_ext(const char *path)
192 cc68fe70 2020-10-07 op const char *end;
194 cc68fe70 2020-10-07 op end = path + strlen(path)-1; /* the last byte before the NUL */
195 cc68fe70 2020-10-07 op for (; end != path; --end) {
196 cc68fe70 2020-10-07 op if (*end == '.')
197 cc68fe70 2020-10-07 op return end+1;
198 cc68fe70 2020-10-07 op if (*end == '/')
206 cc68fe70 2020-10-07 op mime(const char *path)
208 cc68fe70 2020-10-07 op const char *ext, *def = "application/octet-stream";
209 cc68fe70 2020-10-07 op struct etm *t;
211 cc68fe70 2020-10-07 op if ((ext = path_ext(path)) == NULL)
214 cc68fe70 2020-10-07 op for (t = filetypes; t->mime != NULL; ++t)
215 cc68fe70 2020-10-07 op if (!strcmp(ext, t->ext))
216 cc68fe70 2020-10-07 op return t->mime;
222 d7802bb4 2020-12-02 op check_path(struct client *c, const char *path, int *fd)
224 72342dc9 2020-11-06 op struct stat sb;
226 f28f9311 2020-10-14 op assert(path != NULL);
227 a5d310bc 2020-11-10 op if ((*fd = openat(dirfd, path,
228 a5d310bc 2020-11-10 op O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
229 a5d310bc 2020-11-10 op return FILE_MISSING;
232 a5d310bc 2020-11-10 op if (fstat(*fd, &sb) == -1) {
233 d7802bb4 2020-12-02 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
234 a5d310bc 2020-11-10 op return FILE_MISSING;
237 a5d310bc 2020-11-10 op if (S_ISDIR(sb.st_mode))
238 a5d310bc 2020-11-10 op return FILE_DIRECTORY;
240 a5d310bc 2020-11-10 op if (sb.st_mode & S_IXUSR)
241 a5d310bc 2020-11-10 op return FILE_EXECUTABLE;
243 a5d310bc 2020-11-10 op return FILE_EXISTS;
247 a5d310bc 2020-11-10 op * the inverse of this algorithm, i.e. starting from the start of the
248 a5d310bc 2020-11-10 op * path + strlen(cgi), and checking if each component, should be
249 a5d310bc 2020-11-10 op * faster. But it's tedious to write. This does the opposite: starts
250 a5d310bc 2020-11-10 op * from the end and strip one component at a time, until either an
251 a5d310bc 2020-11-10 op * executable is found or we emptied the path.
254 a5d310bc 2020-11-10 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
257 a5d310bc 2020-11-10 op end = strchr(path, '\0');
259 a5d310bc 2020-11-10 op /* NB: assume CGI is enabled and path matches cgi */
261 a5d310bc 2020-11-10 op while (end > path) {
262 a5d310bc 2020-11-10 op /* go up one level. UNIX paths are simple and POSIX
263 a5d310bc 2020-11-10 op * dirname, with its ambiguities on if the given path
264 a5d310bc 2020-11-10 op * is changed or not, gives me headaches. */
265 a5d310bc 2020-11-10 op while (*end != '/')
269 d7802bb4 2020-12-02 op switch (check_path(c, path, &c->fd)) {
270 a5d310bc 2020-11-10 op case FILE_EXECUTABLE:
271 a5d310bc 2020-11-10 op return start_cgi(path, end+1, query, fds,c);
272 a5d310bc 2020-11-10 op case FILE_MISSING:
283 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
285 a5d310bc 2020-11-10 op goodbye(fds, c);
291 a5d310bc 2020-11-10 op open_file(char *path, char *query, struct pollfd *fds, struct client *c)
293 a5d310bc 2020-11-10 op char fpath[PATHBUF];
295 f28f9311 2020-10-14 op bzero(fpath, sizeof(fpath));
297 f28f9311 2020-10-14 op if (*path != '.')
298 f28f9311 2020-10-14 op fpath[0] = '.';
299 f28f9311 2020-10-14 op strlcat(fpath, path, PATHBUF);
301 d7802bb4 2020-12-02 op switch (check_path(c, fpath, &c->fd)) {
302 a5d310bc 2020-11-10 op case FILE_EXECUTABLE:
303 a5d310bc 2020-11-10 op /* +2 to skip the ./ */
304 a5d310bc 2020-11-10 op if (cgi != NULL && starts_with(fpath+2, cgi))
305 a5d310bc 2020-11-10 op return start_cgi(fpath, "", query, fds, c);
307 a5d310bc 2020-11-10 op /* fallthrough */
309 a5d310bc 2020-11-10 op case FILE_EXISTS:
310 a5d310bc 2020-11-10 op if ((c->len = filesize(c->fd)) == -1) {
311 d7802bb4 2020-12-02 op LOGE(c, "failed to get file size for %s", fpath);
312 a5d310bc 2020-11-10 op goodbye(fds, c);
316 a5d310bc 2020-11-10 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
317 a5d310bc 2020-11-10 op c->fd, 0)) == MAP_FAILED) {
318 a5d310bc 2020-11-10 op warn("mmap: %s", fpath);
319 a5d310bc 2020-11-10 op goodbye(fds, c);
322 a5d310bc 2020-11-10 op c->i = c->buf;
323 a5d310bc 2020-11-10 op return start_reply(fds, c, SUCCESS, mime(fpath));
325 a5d310bc 2020-11-10 op case FILE_DIRECTORY:
326 d7802bb4 2020-12-02 op LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
327 f28f9311 2020-10-14 op close(c->fd);
329 f28f9311 2020-10-14 op send_dir(fpath, fds, c);
332 a5d310bc 2020-11-10 op case FILE_MISSING:
333 a5d310bc 2020-11-10 op if (cgi != NULL && starts_with(fpath+2, cgi))
334 a5d310bc 2020-11-10 op return check_for_cgi(fpath, query, fds, c);
336 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
338 f28f9311 2020-10-14 op goodbye(fds, c);
342 a5d310bc 2020-11-10 op /* unreachable */
348 a5d310bc 2020-11-10 op start_cgi(const char *spath, const char *relpath, const char *query,
349 75d233f0 2020-11-06 op struct pollfd *fds, struct client *c)
352 a5d310bc 2020-11-10 op int p[2]; /* read end, write end */
354 72342dc9 2020-11-06 op if (pipe(p) == -1)
357 72342dc9 2020-11-06 op switch (pid = fork()) {
361 a5d310bc 2020-11-10 op case 0: { /* child */
362 721e2325 2020-11-18 op char *ex, *requri, *portno;
363 72342dc9 2020-11-06 op char addr[INET_ADDRSTRLEN];
364 72342dc9 2020-11-06 op char *argv[] = { NULL, NULL, NULL };
369 72342dc9 2020-11-06 op if (dup2(p[1], 1) == -1)
370 72342dc9 2020-11-06 op goto childerr;
372 72342dc9 2020-11-06 op if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
373 a5d310bc 2020-11-10 op goto childerr;
375 721e2325 2020-11-18 op if (asprintf(&portno, "%d", port) == -1)
376 721e2325 2020-11-18 op goto childerr;
378 a5d310bc 2020-11-10 op if (asprintf(&ex, "%s%s", dir, spath+1) == -1)
379 72342dc9 2020-11-06 op goto childerr;
381 a5d310bc 2020-11-10 op if (asprintf(&requri, "%s%s%s", spath,
382 a5d310bc 2020-11-10 op *relpath == '\0' ? "" : "/",
383 a5d310bc 2020-11-10 op relpath) == -1)
384 a5d310bc 2020-11-10 op goto childerr;
386 a5d310bc 2020-11-10 op argv[0] = argv[1] = ex;
388 60ba426e 2020-11-06 op /* fix the env */
389 677afbd3 2020-12-02 op SAFE_SETENV("GATEWAY_INTERFACE", "CGI/1.1");
390 677afbd3 2020-12-02 op SAFE_SETENV("SERVER_SOFTWARE", "gmid");
391 677afbd3 2020-12-02 op SAFE_SETENV("SERVER_PORT", portno);
392 a5d310bc 2020-11-10 op /* setenv("SERVER_NAME", "", 1); */
393 677afbd3 2020-12-02 op SAFE_SETENV("SCRIPT_NAME", spath);
394 677afbd3 2020-12-02 op SAFE_SETENV("SCRIPT_EXECUTABLE", ex);
395 677afbd3 2020-12-02 op SAFE_SETENV("REQUEST_URI", requri);
396 677afbd3 2020-12-02 op SAFE_SETENV("REQUEST_RELATIVE", relpath);
397 677afbd3 2020-12-02 op SAFE_SETENV("QUERY_STRING", query);
398 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_HOST", addr);
399 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_ADDR", addr);
400 677afbd3 2020-12-02 op SAFE_SETENV("DOCUMENT_ROOT", dir);
402 677afbd3 2020-12-02 op if (tls_peer_cert_provided(c->ctx)) {
403 677afbd3 2020-12-02 op SAFE_SETENV("AUTH_TYPE", "Certificate");
404 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_USER", tls_peer_cert_subject(c->ctx));
405 677afbd3 2020-12-02 op SAFE_SETENV("TLS_CLIENT_ISSUER", tls_peer_cert_issuer(c->ctx));
406 677afbd3 2020-12-02 op SAFE_SETENV("TLS_CLIENT_HASH", tls_peer_cert_hash(c->ctx));
409 a5d310bc 2020-11-10 op execvp(ex, argv);
410 72342dc9 2020-11-06 op goto childerr;
413 72342dc9 2020-11-06 op default: /* parent */
415 72342dc9 2020-11-06 op close(c->fd);
416 72342dc9 2020-11-06 op c->fd = p[0];
417 72342dc9 2020-11-06 op c->child = pid;
418 aff8d190 2020-11-06 op mark_nonblock(c->fd);
419 aff8d190 2020-11-06 op c->state = S_SENDING;
420 72342dc9 2020-11-06 op handle_cgi(fds, c);
425 72342dc9 2020-11-06 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
427 72342dc9 2020-11-06 op goodbye(fds, c);
431 72342dc9 2020-11-06 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
437 aff8d190 2020-11-06 op cgi_setpoll_on_child(struct pollfd *fds, struct client *c)
441 aff8d190 2020-11-06 op if (c->waiting_on_child)
443 aff8d190 2020-11-06 op c->waiting_on_child = 1;
445 aff8d190 2020-11-06 op fds->events = POLLIN;
447 aff8d190 2020-11-06 op fd = fds->fd;
448 aff8d190 2020-11-06 op fds->fd = c->fd;
453 aff8d190 2020-11-06 op cgi_setpoll_on_client(struct pollfd *fds, struct client *c)
457 aff8d190 2020-11-06 op if (!c->waiting_on_child)
459 aff8d190 2020-11-06 op c->waiting_on_child = 0;
461 aff8d190 2020-11-06 op fd = fds->fd;
462 aff8d190 2020-11-06 op fds->fd = c->fd;
467 aff8d190 2020-11-06 op handle_cgi(struct pollfd *fds, struct client *c)
471 aff8d190 2020-11-06 op /* ensure c->fd is the child and fds->fd the client */
472 aff8d190 2020-11-06 op cgi_setpoll_on_client(fds, c);
475 aff8d190 2020-11-06 op if (c->len == 0) {
476 aff8d190 2020-11-06 op if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
478 aff8d190 2020-11-06 op if (r == -1) {
479 aff8d190 2020-11-06 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
480 aff8d190 2020-11-06 op cgi_setpoll_on_child(fds, c);
489 aff8d190 2020-11-06 op while (c->len > 0) {
490 aff8d190 2020-11-06 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
494 72342dc9 2020-11-06 op case TLS_WANT_POLLOUT:
495 aff8d190 2020-11-06 op fds->events = POLLOUT;
498 72342dc9 2020-11-06 op case TLS_WANT_POLLIN:
499 aff8d190 2020-11-06 op fds->events = POLLIN;
511 72342dc9 2020-11-06 op goodbye(fds, c);
515 75d233f0 2020-11-06 op send_file(char *path, char *query, struct pollfd *fds, struct client *c)
517 f28f9311 2020-10-14 op ssize_t ret, len;
519 f28f9311 2020-10-14 op if (c->fd == -1) {
520 75d233f0 2020-11-06 op if (!open_file(path, query, fds, c))
522 f28f9311 2020-10-14 op c->state = S_SENDING;
525 f28f9311 2020-10-14 op len = (c->buf + c->len) - c->i;
527 f28f9311 2020-10-14 op while (len > 0) {
528 f28f9311 2020-10-14 op switch (ret = tls_write(c->ctx, c->i, len)) {
530 d7802bb4 2020-12-02 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
531 f28f9311 2020-10-14 op goodbye(fds, c);
534 f28f9311 2020-10-14 op case TLS_WANT_POLLIN:
535 f28f9311 2020-10-14 op fds->events = POLLIN;
538 f28f9311 2020-10-14 op case TLS_WANT_POLLOUT:
539 f28f9311 2020-10-14 op fds->events = POLLOUT;
549 f28f9311 2020-10-14 op goodbye(fds, c);
553 592fd624 2020-10-07 op send_dir(char *path, struct pollfd *fds, struct client *client)
555 3e4749f7 2020-10-02 op char fpath[PATHBUF];
558 3e4749f7 2020-10-02 op bzero(fpath, PATHBUF);
560 9c56b0a7 2020-10-14 op if (path[0] != '.')
561 3e4749f7 2020-10-02 op fpath[0] = '.';
563 3e4749f7 2020-10-02 op /* this cannot fail since sizeof(fpath) > maxlen of path */
564 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
565 3e4749f7 2020-10-02 op len = strlen(fpath);
567 3e4749f7 2020-10-02 op /* add a trailing / in case. */
568 3e4749f7 2020-10-02 op if (fpath[len-1] != '/') {
569 3e4749f7 2020-10-02 op fpath[len] = '/';
572 3e4749f7 2020-10-02 op strlcat(fpath, "index.gmi", sizeof(fpath));
574 75d233f0 2020-11-06 op send_file(fpath, NULL, fds, client);
578 592fd624 2020-10-07 op handle(struct pollfd *fds, struct client *client)
580 592fd624 2020-10-07 op char buf[GEMINI_URL_LEN];
584 592fd624 2020-10-07 op switch (client->state) {
586 592fd624 2020-10-07 op bzero(buf, GEMINI_URL_LEN);
587 592fd624 2020-10-07 op switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
589 d7802bb4 2020-12-02 op LOGE(client, "tls_read: %s", tls_error(client->ctx));
590 592fd624 2020-10-07 op goodbye(fds, client);
593 592fd624 2020-10-07 op case TLS_WANT_POLLIN:
594 592fd624 2020-10-07 op fds->events = POLLIN;
597 592fd624 2020-10-07 op case TLS_WANT_POLLOUT:
598 592fd624 2020-10-07 op fds->events = POLLOUT;
602 2c3a40fa 2020-11-06 op if (!url_trim(client, buf)) {
603 592fd624 2020-10-07 op if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
605 592fd624 2020-10-07 op goodbye(fds, client);
609 592fd624 2020-10-07 op if ((path = url_start_of_request(buf)) == NULL) {
610 592fd624 2020-10-07 op if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
612 592fd624 2020-10-07 op goodbye(fds, client);
616 aa0fe0cf 2020-11-06 op query = adjust_path(path);
617 d7802bb4 2020-12-02 op LOGI(client, "GET %s%s%s", path,
618 aa0fe0cf 2020-11-06 op query ? "?" : "",
619 aa0fe0cf 2020-11-06 op query ? query : "");
621 a5d310bc 2020-11-10 op send_file(path, query, fds, client);
624 592fd624 2020-10-07 op case S_INITIALIZING:
625 592fd624 2020-10-07 op if (!start_reply(fds, client, client->code, client->meta))
628 592fd624 2020-10-07 op if (client->code != SUCCESS) {
629 592fd624 2020-10-07 op /* we don't need a body */
630 592fd624 2020-10-07 op goodbye(fds, client);
634 592fd624 2020-10-07 op client->state = S_SENDING;
636 592fd624 2020-10-07 op /* fallthrough */
638 592fd624 2020-10-07 op case S_SENDING:
639 aff8d190 2020-11-06 op if (client->child != -1)
640 aff8d190 2020-11-06 op handle_cgi(fds, client);
642 aff8d190 2020-11-06 op send_file(NULL, NULL, fds, client);
645 592fd624 2020-10-07 op case S_CLOSING:
646 592fd624 2020-10-07 op goodbye(fds, client);
650 592fd624 2020-10-07 op /* unreachable */
656 592fd624 2020-10-07 op mark_nonblock(int fd)
660 592fd624 2020-10-07 op if ((flags = fcntl(fd, F_GETFL)) == -1)
661 d7802bb4 2020-12-02 op FATAL("fcntl(F_GETFL): %s", strerror(errno));
662 592fd624 2020-10-07 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
663 d7802bb4 2020-12-02 op FATAL("fcntl(F_SETFL): %s", strerror(errno));
667 9468027b 2020-10-15 op make_socket(int port, int family)
670 9468027b 2020-10-15 op struct sockaddr_in addr4;
671 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
672 9468027b 2020-10-15 op struct sockaddr *addr;
673 9468027b 2020-10-15 op socklen_t len;
675 9468027b 2020-10-15 op switch (family) {
676 9468027b 2020-10-15 op case AF_INET:
677 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
678 9468027b 2020-10-15 op addr4.sin_family = family;
679 9468027b 2020-10-15 op addr4.sin_port = htons(port);
680 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
681 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
682 9468027b 2020-10-15 op len = sizeof(addr4);
685 9468027b 2020-10-15 op case AF_INET6:
686 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
687 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
688 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
689 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
690 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
691 9468027b 2020-10-15 op len = sizeof(addr6);
695 9468027b 2020-10-15 op /* unreachable */
699 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
700 d7802bb4 2020-12-02 op FATAL("socket: %s", strerror(errno));
703 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
704 d7802bb4 2020-12-02 op FATAL("setsockopt(SO_REUSEADDR): %s", strerror(errno));
707 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
708 d7802bb4 2020-12-02 op FATAL("setsockopt(SO_REUSEPORT): %s", strerror(errno));
710 592fd624 2020-10-07 op mark_nonblock(sock);
712 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
713 d7802bb4 2020-12-02 op FATAL("bind: %s", strerror(errno));
715 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
716 d7802bb4 2020-12-02 op FATAL("listen: %s", strerror(errno));
722 592fd624 2020-10-07 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
725 592fd624 2020-10-07 op struct sockaddr_in addr;
726 3e4749f7 2020-10-02 op socklen_t len;
728 592fd624 2020-10-07 op len = sizeof(addr);
729 592fd624 2020-10-07 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
730 592fd624 2020-10-07 op if (errno == EWOULDBLOCK)
732 d7802bb4 2020-12-02 op FATAL("accept: %s", strerror(errno));
735 592fd624 2020-10-07 op mark_nonblock(fd);
737 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
738 592fd624 2020-10-07 op if (fds[i].fd == -1) {
739 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
740 592fd624 2020-10-07 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
741 592fd624 2020-10-07 op break; /* goodbye fd! */
743 592fd624 2020-10-07 op fds[i].fd = fd;
744 592fd624 2020-10-07 op fds[i].events = POLLIN;
746 592fd624 2020-10-07 op clients[i].state = S_OPEN;
747 592fd624 2020-10-07 op clients[i].fd = -1;
748 72342dc9 2020-11-06 op clients[i].child = -1;
749 f28f9311 2020-10-14 op clients[i].buf = MAP_FAILED;
750 2c3a40fa 2020-11-06 op clients[i].af = AF_INET;
751 2c3a40fa 2020-11-06 op clients[i].addr = addr.sin_addr;
753 120381c9 2020-11-06 op connected_clients++;
762 592fd624 2020-10-07 op goodbye(struct pollfd *pfd, struct client *c)
766 592fd624 2020-10-07 op c->state = S_CLOSING;
768 592fd624 2020-10-07 op ret = tls_close(c->ctx);
769 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
770 592fd624 2020-10-07 op pfd->events = POLLIN;
773 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
774 592fd624 2020-10-07 op pfd->events = POLLOUT;
778 120381c9 2020-11-06 op connected_clients--;
780 592fd624 2020-10-07 op tls_free(c->ctx);
781 592fd624 2020-10-07 op c->ctx = NULL;
783 f28f9311 2020-10-14 op if (c->buf != MAP_FAILED)
784 f28f9311 2020-10-14 op munmap(c->buf, c->len);
786 592fd624 2020-10-07 op if (c->fd != -1)
787 592fd624 2020-10-07 op close(c->fd);
789 592fd624 2020-10-07 op close(pfd->fd);
790 592fd624 2020-10-07 op pfd->fd = -1;
794 592fd624 2020-10-07 op loop(struct tls *ctx, int sock)
797 592fd624 2020-10-07 op struct client clients[MAX_USERS];
798 592fd624 2020-10-07 op struct pollfd fds[MAX_USERS];
800 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
801 592fd624 2020-10-07 op fds[i].fd = -1;
802 592fd624 2020-10-07 op fds[i].events = POLLIN;
803 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
806 592fd624 2020-10-07 op fds[0].fd = sock;
809 120381c9 2020-11-06 op if ((todo = poll(fds, MAX_USERS, INFTIM)) == -1) {
810 120381c9 2020-11-06 op if (errno == EINTR) {
811 d7802bb4 2020-12-02 op warnx("connected clients: %d",
812 d7802bb4 2020-12-02 op connected_clients);
815 d7802bb4 2020-12-02 op FATAL("poll: %s", strerror(errno));
818 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; i++) {
819 592fd624 2020-10-07 op assert(i < MAX_USERS);
821 592fd624 2020-10-07 op if (fds[i].revents == 0)
824 592fd624 2020-10-07 op if (fds[i].revents & (POLLERR|POLLNVAL))
825 d7802bb4 2020-12-02 op FATAL("bad fd %d: %s", fds[i].fd,
826 d7802bb4 2020-12-02 op strerror(errno));
828 592fd624 2020-10-07 op if (fds[i].revents & POLLHUP) {
829 aff8d190 2020-11-06 op /* fds[i] may be the fd of the stdin
830 aff8d190 2020-11-06 op * of a cgi script that has exited. */
831 aff8d190 2020-11-06 op if (!clients[i].waiting_on_child) {
832 aff8d190 2020-11-06 op goodbye(&fds[i], &clients[i]);
839 592fd624 2020-10-07 op if (i == 0) { /* new client */
840 592fd624 2020-10-07 op do_accept(sock, ctx, fds, clients);
844 592fd624 2020-10-07 op handle(&fds[i], &clients[i]);
850 231bfcdc 2020-12-21 op absolutify_path(const char *path)
852 231bfcdc 2020-12-21 op char *wd, *r;
854 231bfcdc 2020-12-21 op if (*path == '/')
855 231bfcdc 2020-12-21 op return strdup(path);
857 231bfcdc 2020-12-21 op wd = getwd(NULL);
858 231bfcdc 2020-12-21 op if (asprintf(&r, "%s/%s", wd, path) == -1)
859 231bfcdc 2020-12-21 op err(1, "asprintf");
865 3e4749f7 2020-10-02 op usage(const char *me)
867 3e4749f7 2020-10-02 op fprintf(stderr,
868 72f653b6 2020-11-06 op "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] "
869 721e2325 2020-11-18 op "[-l logfile] [-p port] [-x cgi-bin]\n",
874 3e4749f7 2020-10-02 op main(int argc, char **argv)
876 72342dc9 2020-11-06 op const char *cert = "cert.pem", *key = "key.pem";
877 3e4749f7 2020-10-02 op struct tls *ctx = NULL;
878 3e4749f7 2020-10-02 op struct tls_config *conf;
879 3e4749f7 2020-10-02 op int sock, ch;
881 0cf902af 2020-11-03 op signal(SIGPIPE, SIG_IGN);
882 72342dc9 2020-11-06 op signal(SIGCHLD, SIG_IGN);
884 120381c9 2020-11-06 op #ifdef SIGINFO
885 120381c9 2020-11-06 op signal(SIGINFO, siginfo_handler);
887 120381c9 2020-11-06 op signal(SIGUSR2, siginfo_handler);
889 120381c9 2020-11-06 op connected_clients = 0;
891 51d876f0 2020-12-21 op if ((dir = absolutify_path("docs")) == NULL)
892 51d876f0 2020-12-21 op err(1, "absolutify_path");
896 d7802bb4 2020-12-02 op foreground = 0;
898 d7802bb4 2020-12-02 op while ((ch = getopt(argc, argv, "c:d:fhk:p:x:")) != -1) {
899 3e4749f7 2020-10-02 op switch (ch) {
901 3e4749f7 2020-10-02 op cert = optarg;
905 231bfcdc 2020-12-21 op free((char*)dir);
906 231bfcdc 2020-12-21 op if ((dir = absolutify_path(optarg)) == NULL)
907 231bfcdc 2020-12-21 op err(1, "absolutify_path");
911 d7802bb4 2020-12-02 op foreground = 1;
915 3e4749f7 2020-10-02 op usage(*argv);
919 3e4749f7 2020-10-02 op key = optarg;
927 721e2325 2020-11-18 op lval = strtol(optarg, &ep, 10);
928 721e2325 2020-11-18 op if (optarg[0] == '\0' || *ep != '\0')
929 721e2325 2020-11-18 op err(1, "not a number: %s", optarg);
930 721e2325 2020-11-18 op if (lval < 0 || lval > UINT16_MAX)
931 721e2325 2020-11-18 op err(1, "port number out of range: %s", optarg);
937 72f653b6 2020-11-06 op cgi = optarg;
941 3e4749f7 2020-10-02 op usage(*argv);
946 3e4749f7 2020-10-02 op if ((conf = tls_config_new()) == NULL)
947 3e4749f7 2020-10-02 op err(1, "tls_config_new");
949 677afbd3 2020-12-02 op /* optionally accept client certs, but don't try to verify them */
950 677afbd3 2020-12-02 op tls_config_verify_client_optional(conf);
951 677afbd3 2020-12-02 op tls_config_insecure_noverifycert(conf);
953 0d8ca45a 2020-10-03 op if (tls_config_set_protocols(conf,
954 0d8ca45a 2020-10-03 op TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
955 0d8ca45a 2020-10-03 op err(1, "tls_config_set_protocols");
957 4d4f0e19 2020-10-03 op if (tls_config_set_cert_file(conf, cert) == -1)
958 4d4f0e19 2020-10-03 op err(1, "tls_config_set_cert_file: %s", cert);
960 4d4f0e19 2020-10-03 op if (tls_config_set_key_file(conf, key) == -1)
961 4d4f0e19 2020-10-03 op err(1, "tls_config_set_key_file: %s", key);
963 3e4749f7 2020-10-02 op if ((ctx = tls_server()) == NULL)
964 3e4749f7 2020-10-02 op err(1, "tls_server");
966 3e4749f7 2020-10-02 op if (tls_configure(ctx, conf) == -1)
967 3e4749f7 2020-10-02 op errx(1, "tls_configure: %s", tls_error(ctx));
969 721e2325 2020-11-18 op sock = make_socket(port, AF_INET);
971 3e4749f7 2020-10-02 op if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
972 3e4749f7 2020-10-02 op err(1, "open: %s", dir);
974 d7802bb4 2020-12-02 op if (!foreground && daemon(0, 1) == -1)
977 72f653b6 2020-11-06 op if (cgi != NULL) {
978 72f653b6 2020-11-06 op if (unveil(dir, "rx") == -1)
979 72f653b6 2020-11-06 op err(1, "unveil");
980 72f653b6 2020-11-06 op if (pledge("stdio rpath inet proc exec", NULL) == -1)
981 72f653b6 2020-11-06 op err(1, "pledge");
983 72f653b6 2020-11-06 op if (unveil(dir, "r") == -1)
984 72f653b6 2020-11-06 op err(1, "unveil");
985 72f653b6 2020-11-06 op if (pledge("stdio rpath inet", NULL) == -1)
986 72f653b6 2020-11-06 op err(1, "pledge");
989 3e4749f7 2020-10-02 op loop(ctx, sock);
992 3e4749f7 2020-10-02 op tls_free(ctx);
993 3e4749f7 2020-10-02 op tls_config_free(conf);