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;
37 33d32d1f 2020-12-25 op struct etm { /* file extension to mime */
38 33d32d1f 2020-12-25 op const char *mime;
39 33d32d1f 2020-12-25 op const char *ext;
40 33d32d1f 2020-12-25 op } filetypes[] = {
41 33d32d1f 2020-12-25 op {"application/pdf", "pdf"},
43 33d32d1f 2020-12-25 op {"image/gif", "gif"},
44 33d32d1f 2020-12-25 op {"image/jpeg", "jpg"},
45 33d32d1f 2020-12-25 op {"image/jpeg", "jpeg"},
46 33d32d1f 2020-12-25 op {"image/png", "png"},
47 33d32d1f 2020-12-25 op {"image/svg+xml", "svg"},
49 33d32d1f 2020-12-25 op {"text/gemini", "gemini"},
50 33d32d1f 2020-12-25 op {"text/gemini", "gmi"},
51 33d32d1f 2020-12-25 op {"text/markdown", "markdown"},
52 33d32d1f 2020-12-25 op {"text/markdown", "md"},
53 33d32d1f 2020-12-25 op {"text/plain", "txt"},
54 33d32d1f 2020-12-25 op {"text/xml", "xml"},
60 4a28dd01 2020-12-28 op sig_handler(int sig)
66 72f653b6 2020-11-06 op starts_with(const char *str, const char *prefix)
70 72f653b6 2020-11-06 op for (i = 0; prefix[i] != '\0'; ++i)
71 72f653b6 2020-11-06 op if (str[i] != prefix[i])
77 592fd624 2020-10-07 op start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
79 3e4749f7 2020-10-02 op char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
83 592fd624 2020-10-07 op client->code = code;
84 592fd624 2020-10-07 op client->meta = reason;
85 592fd624 2020-10-07 op client->state = S_INITIALIZING;
87 3e4749f7 2020-10-02 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
88 3e4749f7 2020-10-02 op assert(len < (int)sizeof(buf));
89 592fd624 2020-10-07 op ret = tls_write(client->ctx, buf, len);
90 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
91 592fd624 2020-10-07 op pfd->events = POLLIN;
95 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
96 592fd624 2020-10-07 op pfd->events = POLLOUT;
104 f28f9311 2020-10-14 op filesize(int fd)
108 f28f9311 2020-10-14 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
110 f28f9311 2020-10-14 op if (lseek(fd, 0, SEEK_SET) == -1)
116 cc68fe70 2020-10-07 op path_ext(const char *path)
118 cc68fe70 2020-10-07 op const char *end;
120 cc68fe70 2020-10-07 op end = path + strlen(path)-1; /* the last byte before the NUL */
121 cc68fe70 2020-10-07 op for (; end != path; --end) {
122 cc68fe70 2020-10-07 op if (*end == '.')
123 cc68fe70 2020-10-07 op return end+1;
124 cc68fe70 2020-10-07 op if (*end == '/')
132 cc68fe70 2020-10-07 op mime(const char *path)
134 cc68fe70 2020-10-07 op const char *ext, *def = "application/octet-stream";
135 cc68fe70 2020-10-07 op struct etm *t;
137 cc68fe70 2020-10-07 op if ((ext = path_ext(path)) == NULL)
140 cc68fe70 2020-10-07 op for (t = filetypes; t->mime != NULL; ++t)
141 cc68fe70 2020-10-07 op if (!strcmp(ext, t->ext))
142 cc68fe70 2020-10-07 op return t->mime;
148 d7802bb4 2020-12-02 op check_path(struct client *c, const char *path, int *fd)
150 72342dc9 2020-11-06 op struct stat sb;
152 f28f9311 2020-10-14 op assert(path != NULL);
153 33d32d1f 2020-12-25 op if ((*fd = openat(dirfd, *path ? path : ".",
154 a5d310bc 2020-11-10 op O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
155 a5d310bc 2020-11-10 op return FILE_MISSING;
158 a5d310bc 2020-11-10 op if (fstat(*fd, &sb) == -1) {
159 d7802bb4 2020-12-02 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
160 a5d310bc 2020-11-10 op return FILE_MISSING;
163 a5d310bc 2020-11-10 op if (S_ISDIR(sb.st_mode))
164 a5d310bc 2020-11-10 op return FILE_DIRECTORY;
166 a5d310bc 2020-11-10 op if (sb.st_mode & S_IXUSR)
167 a5d310bc 2020-11-10 op return FILE_EXECUTABLE;
169 a5d310bc 2020-11-10 op return FILE_EXISTS;
173 a5d310bc 2020-11-10 op * the inverse of this algorithm, i.e. starting from the start of the
174 a5d310bc 2020-11-10 op * path + strlen(cgi), and checking if each component, should be
175 a5d310bc 2020-11-10 op * faster. But it's tedious to write. This does the opposite: starts
176 a5d310bc 2020-11-10 op * from the end and strip one component at a time, until either an
177 a5d310bc 2020-11-10 op * executable is found or we emptied the path.
180 a5d310bc 2020-11-10 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
183 a5d310bc 2020-11-10 op end = strchr(path, '\0');
185 a5d310bc 2020-11-10 op /* NB: assume CGI is enabled and path matches cgi */
187 a5d310bc 2020-11-10 op while (end > path) {
188 a5d310bc 2020-11-10 op /* go up one level. UNIX paths are simple and POSIX
189 a5d310bc 2020-11-10 op * dirname, with its ambiguities on if the given path
190 a5d310bc 2020-11-10 op * is changed or not, gives me headaches. */
191 a5d310bc 2020-11-10 op while (*end != '/')
195 d7802bb4 2020-12-02 op switch (check_path(c, path, &c->fd)) {
196 a5d310bc 2020-11-10 op case FILE_EXECUTABLE:
197 a5d310bc 2020-11-10 op return start_cgi(path, end+1, query, fds,c);
198 a5d310bc 2020-11-10 op case FILE_MISSING:
209 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
211 a5d310bc 2020-11-10 op goodbye(fds, c);
217 33d32d1f 2020-12-25 op open_file(char *fpath, char *query, struct pollfd *fds, struct client *c)
219 d7802bb4 2020-12-02 op switch (check_path(c, fpath, &c->fd)) {
220 a5d310bc 2020-11-10 op case FILE_EXECUTABLE:
221 5a40216f 2020-12-28 op if (cgi != NULL && starts_with(fpath, cgi))
222 a5d310bc 2020-11-10 op return start_cgi(fpath, "", query, fds, c);
224 a5d310bc 2020-11-10 op /* fallthrough */
226 a5d310bc 2020-11-10 op case FILE_EXISTS:
227 a5d310bc 2020-11-10 op if ((c->len = filesize(c->fd)) == -1) {
228 d7802bb4 2020-12-02 op LOGE(c, "failed to get file size for %s", fpath);
229 a5d310bc 2020-11-10 op goodbye(fds, c);
233 a5d310bc 2020-11-10 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
234 a5d310bc 2020-11-10 op c->fd, 0)) == MAP_FAILED) {
235 a5d310bc 2020-11-10 op warn("mmap: %s", fpath);
236 a5d310bc 2020-11-10 op goodbye(fds, c);
239 a5d310bc 2020-11-10 op c->i = c->buf;
240 a5d310bc 2020-11-10 op return start_reply(fds, c, SUCCESS, mime(fpath));
242 a5d310bc 2020-11-10 op case FILE_DIRECTORY:
243 d7802bb4 2020-12-02 op LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
244 f28f9311 2020-10-14 op close(c->fd);
246 f28f9311 2020-10-14 op send_dir(fpath, fds, c);
249 a5d310bc 2020-11-10 op case FILE_MISSING:
250 5a40216f 2020-12-28 op if (cgi != NULL && starts_with(fpath, cgi))
251 a5d310bc 2020-11-10 op return check_for_cgi(fpath, query, fds, c);
253 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
255 f28f9311 2020-10-14 op goodbye(fds, c);
259 a5d310bc 2020-11-10 op /* unreachable */
265 a5d310bc 2020-11-10 op start_cgi(const char *spath, const char *relpath, const char *query,
266 75d233f0 2020-11-06 op struct pollfd *fds, struct client *c)
269 a5d310bc 2020-11-10 op int p[2]; /* read end, write end */
271 72342dc9 2020-11-06 op if (pipe(p) == -1)
274 72342dc9 2020-11-06 op switch (pid = fork()) {
278 a5d310bc 2020-11-10 op case 0: { /* child */
279 721e2325 2020-11-18 op char *ex, *requri, *portno;
280 72342dc9 2020-11-06 op char addr[INET_ADDRSTRLEN];
281 72342dc9 2020-11-06 op char *argv[] = { NULL, NULL, NULL };
284 72342dc9 2020-11-06 op if (dup2(p[1], 1) == -1)
285 72342dc9 2020-11-06 op goto childerr;
287 72342dc9 2020-11-06 op if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
288 a5d310bc 2020-11-10 op goto childerr;
290 721e2325 2020-11-18 op if (asprintf(&portno, "%d", port) == -1)
291 721e2325 2020-11-18 op goto childerr;
293 5a40216f 2020-12-28 op if (asprintf(&ex, "%s/%s", dir, spath) == -1)
294 72342dc9 2020-11-06 op goto childerr;
296 a5d310bc 2020-11-10 op if (asprintf(&requri, "%s%s%s", spath,
297 a5d310bc 2020-11-10 op *relpath == '\0' ? "" : "/",
298 a5d310bc 2020-11-10 op relpath) == -1)
299 a5d310bc 2020-11-10 op goto childerr;
301 a5d310bc 2020-11-10 op argv[0] = argv[1] = ex;
303 60ba426e 2020-11-06 op /* fix the env */
304 677afbd3 2020-12-02 op SAFE_SETENV("GATEWAY_INTERFACE", "CGI/1.1");
305 677afbd3 2020-12-02 op SAFE_SETENV("SERVER_SOFTWARE", "gmid");
306 677afbd3 2020-12-02 op SAFE_SETENV("SERVER_PORT", portno);
307 a5d310bc 2020-11-10 op /* setenv("SERVER_NAME", "", 1); */
308 677afbd3 2020-12-02 op SAFE_SETENV("SCRIPT_NAME", spath);
309 677afbd3 2020-12-02 op SAFE_SETENV("SCRIPT_EXECUTABLE", ex);
310 677afbd3 2020-12-02 op SAFE_SETENV("REQUEST_URI", requri);
311 677afbd3 2020-12-02 op SAFE_SETENV("REQUEST_RELATIVE", relpath);
312 677afbd3 2020-12-02 op SAFE_SETENV("QUERY_STRING", query);
313 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_HOST", addr);
314 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_ADDR", addr);
315 677afbd3 2020-12-02 op SAFE_SETENV("DOCUMENT_ROOT", dir);
317 677afbd3 2020-12-02 op if (tls_peer_cert_provided(c->ctx)) {
318 677afbd3 2020-12-02 op SAFE_SETENV("AUTH_TYPE", "Certificate");
319 677afbd3 2020-12-02 op SAFE_SETENV("REMOTE_USER", tls_peer_cert_subject(c->ctx));
320 677afbd3 2020-12-02 op SAFE_SETENV("TLS_CLIENT_ISSUER", tls_peer_cert_issuer(c->ctx));
321 677afbd3 2020-12-02 op SAFE_SETENV("TLS_CLIENT_HASH", tls_peer_cert_hash(c->ctx));
324 a5d310bc 2020-11-10 op execvp(ex, argv);
325 72342dc9 2020-11-06 op goto childerr;
328 72342dc9 2020-11-06 op default: /* parent */
330 72342dc9 2020-11-06 op close(c->fd);
331 72342dc9 2020-11-06 op c->fd = p[0];
332 72342dc9 2020-11-06 op c->child = pid;
333 aff8d190 2020-11-06 op mark_nonblock(c->fd);
334 aff8d190 2020-11-06 op c->state = S_SENDING;
335 72342dc9 2020-11-06 op handle_cgi(fds, c);
340 72342dc9 2020-11-06 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
342 72342dc9 2020-11-06 op goodbye(fds, c);
346 72342dc9 2020-11-06 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
352 6c6c7a0e 2020-12-28 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
356 aff8d190 2020-11-06 op if (c->waiting_on_child)
358 aff8d190 2020-11-06 op c->waiting_on_child = 1;
360 aff8d190 2020-11-06 op fds->events = POLLIN;
362 aff8d190 2020-11-06 op fd = fds->fd;
363 aff8d190 2020-11-06 op fds->fd = c->fd;
368 6c6c7a0e 2020-12-28 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
372 aff8d190 2020-11-06 op if (!c->waiting_on_child)
374 aff8d190 2020-11-06 op c->waiting_on_child = 0;
376 aff8d190 2020-11-06 op fd = fds->fd;
377 aff8d190 2020-11-06 op fds->fd = c->fd;
382 aff8d190 2020-11-06 op handle_cgi(struct pollfd *fds, struct client *c)
386 aff8d190 2020-11-06 op /* ensure c->fd is the child and fds->fd the client */
387 6c6c7a0e 2020-12-28 op cgi_poll_on_client(fds, c);
390 aff8d190 2020-11-06 op if (c->len == 0) {
391 aff8d190 2020-11-06 op if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
393 aff8d190 2020-11-06 op if (r == -1) {
394 aff8d190 2020-11-06 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
395 6c6c7a0e 2020-12-28 op cgi_poll_on_child(fds, c);
404 aff8d190 2020-11-06 op while (c->len > 0) {
405 aff8d190 2020-11-06 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
409 72342dc9 2020-11-06 op case TLS_WANT_POLLOUT:
410 aff8d190 2020-11-06 op fds->events = POLLOUT;
413 72342dc9 2020-11-06 op case TLS_WANT_POLLIN:
414 aff8d190 2020-11-06 op fds->events = POLLIN;
426 72342dc9 2020-11-06 op goodbye(fds, c);
430 75d233f0 2020-11-06 op send_file(char *path, char *query, struct pollfd *fds, struct client *c)
432 f28f9311 2020-10-14 op ssize_t ret, len;
434 f28f9311 2020-10-14 op if (c->fd == -1) {
435 75d233f0 2020-11-06 op if (!open_file(path, query, fds, c))
437 f28f9311 2020-10-14 op c->state = S_SENDING;
440 f28f9311 2020-10-14 op len = (c->buf + c->len) - c->i;
442 f28f9311 2020-10-14 op while (len > 0) {
443 f28f9311 2020-10-14 op switch (ret = tls_write(c->ctx, c->i, len)) {
445 d7802bb4 2020-12-02 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
446 f28f9311 2020-10-14 op goodbye(fds, c);
449 f28f9311 2020-10-14 op case TLS_WANT_POLLIN:
450 f28f9311 2020-10-14 op fds->events = POLLIN;
453 f28f9311 2020-10-14 op case TLS_WANT_POLLOUT:
454 f28f9311 2020-10-14 op fds->events = POLLOUT;
464 f28f9311 2020-10-14 op goodbye(fds, c);
468 592fd624 2020-10-07 op send_dir(char *path, struct pollfd *fds, struct client *client)
470 3e4749f7 2020-10-02 op char fpath[PATHBUF];
473 3e4749f7 2020-10-02 op bzero(fpath, PATHBUF);
475 9c56b0a7 2020-10-14 op if (path[0] != '.')
476 3e4749f7 2020-10-02 op fpath[0] = '.';
478 3e4749f7 2020-10-02 op /* this cannot fail since sizeof(fpath) > maxlen of path */
479 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
480 3e4749f7 2020-10-02 op len = strlen(fpath);
482 3e4749f7 2020-10-02 op /* add a trailing / in case. */
483 3e4749f7 2020-10-02 op if (fpath[len-1] != '/') {
484 3e4749f7 2020-10-02 op fpath[len] = '/';
487 3e4749f7 2020-10-02 op strlcat(fpath, "index.gmi", sizeof(fpath));
489 75d233f0 2020-11-06 op send_file(fpath, NULL, fds, client);
493 592fd624 2020-10-07 op handle(struct pollfd *fds, struct client *client)
495 592fd624 2020-10-07 op char buf[GEMINI_URL_LEN];
496 33d32d1f 2020-12-25 op const char *parse_err;
497 33d32d1f 2020-12-25 op struct uri uri;
499 592fd624 2020-10-07 op switch (client->state) {
501 592fd624 2020-10-07 op bzero(buf, GEMINI_URL_LEN);
502 592fd624 2020-10-07 op switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
504 d7802bb4 2020-12-02 op LOGE(client, "tls_read: %s", tls_error(client->ctx));
505 592fd624 2020-10-07 op goodbye(fds, client);
508 592fd624 2020-10-07 op case TLS_WANT_POLLIN:
509 592fd624 2020-10-07 op fds->events = POLLIN;
512 592fd624 2020-10-07 op case TLS_WANT_POLLOUT:
513 592fd624 2020-10-07 op fds->events = POLLOUT;
517 b3deee7b 2020-12-28 op parse_err = "invalid request";
518 33d32d1f 2020-12-25 op if (!trim_req_uri(buf) || !parse_uri(buf, &uri, &parse_err)) {
519 33d32d1f 2020-12-25 op if (!start_reply(fds, client, BAD_REQUEST, parse_err))
521 592fd624 2020-10-07 op goodbye(fds, client);
525 33d32d1f 2020-12-25 op LOGI(client, "GET %s%s%s",
526 33d32d1f 2020-12-25 op *uri.path ? uri.path : "/",
527 33d32d1f 2020-12-25 op *uri.query ? "?" : "",
528 33d32d1f 2020-12-25 op *uri.query ? uri.query : "");
530 33d32d1f 2020-12-25 op send_file(uri.path, uri.query, fds, client);
533 592fd624 2020-10-07 op case S_INITIALIZING:
534 592fd624 2020-10-07 op if (!start_reply(fds, client, client->code, client->meta))
537 592fd624 2020-10-07 op if (client->code != SUCCESS) {
538 592fd624 2020-10-07 op /* we don't need a body */
539 592fd624 2020-10-07 op goodbye(fds, client);
543 592fd624 2020-10-07 op client->state = S_SENDING;
545 592fd624 2020-10-07 op /* fallthrough */
547 592fd624 2020-10-07 op case S_SENDING:
548 aff8d190 2020-11-06 op if (client->child != -1)
549 aff8d190 2020-11-06 op handle_cgi(fds, client);
551 aff8d190 2020-11-06 op send_file(NULL, NULL, fds, client);
554 592fd624 2020-10-07 op case S_CLOSING:
555 592fd624 2020-10-07 op goodbye(fds, client);
559 592fd624 2020-10-07 op /* unreachable */
565 592fd624 2020-10-07 op mark_nonblock(int fd)
569 592fd624 2020-10-07 op if ((flags = fcntl(fd, F_GETFL)) == -1)
570 d7802bb4 2020-12-02 op FATAL("fcntl(F_GETFL): %s", strerror(errno));
571 592fd624 2020-10-07 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
572 d7802bb4 2020-12-02 op FATAL("fcntl(F_SETFL): %s", strerror(errno));
576 9468027b 2020-10-15 op make_socket(int port, int family)
579 9468027b 2020-10-15 op struct sockaddr_in addr4;
580 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
581 9468027b 2020-10-15 op struct sockaddr *addr;
582 9468027b 2020-10-15 op socklen_t len;
584 9468027b 2020-10-15 op switch (family) {
585 9468027b 2020-10-15 op case AF_INET:
586 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
587 9468027b 2020-10-15 op addr4.sin_family = family;
588 9468027b 2020-10-15 op addr4.sin_port = htons(port);
589 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
590 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
591 9468027b 2020-10-15 op len = sizeof(addr4);
594 9468027b 2020-10-15 op case AF_INET6:
595 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
596 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
597 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
598 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
599 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
600 9468027b 2020-10-15 op len = sizeof(addr6);
604 9468027b 2020-10-15 op /* unreachable */
608 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
609 d7802bb4 2020-12-02 op FATAL("socket: %s", strerror(errno));
612 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
613 d7802bb4 2020-12-02 op FATAL("setsockopt(SO_REUSEADDR): %s", strerror(errno));
616 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
617 d7802bb4 2020-12-02 op FATAL("setsockopt(SO_REUSEPORT): %s", strerror(errno));
619 592fd624 2020-10-07 op mark_nonblock(sock);
621 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
622 d7802bb4 2020-12-02 op FATAL("bind: %s", strerror(errno));
624 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
625 d7802bb4 2020-12-02 op FATAL("listen: %s", strerror(errno));
631 592fd624 2020-10-07 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
634 592fd624 2020-10-07 op struct sockaddr_in addr;
635 3e4749f7 2020-10-02 op socklen_t len;
637 592fd624 2020-10-07 op len = sizeof(addr);
638 592fd624 2020-10-07 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
639 592fd624 2020-10-07 op if (errno == EWOULDBLOCK)
641 d7802bb4 2020-12-02 op FATAL("accept: %s", strerror(errno));
644 592fd624 2020-10-07 op mark_nonblock(fd);
646 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
647 592fd624 2020-10-07 op if (fds[i].fd == -1) {
648 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
649 592fd624 2020-10-07 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
650 592fd624 2020-10-07 op break; /* goodbye fd! */
652 592fd624 2020-10-07 op fds[i].fd = fd;
653 592fd624 2020-10-07 op fds[i].events = POLLIN;
655 592fd624 2020-10-07 op clients[i].state = S_OPEN;
656 592fd624 2020-10-07 op clients[i].fd = -1;
657 72342dc9 2020-11-06 op clients[i].child = -1;
658 f28f9311 2020-10-14 op clients[i].buf = MAP_FAILED;
659 2c3a40fa 2020-11-06 op clients[i].af = AF_INET;
660 2c3a40fa 2020-11-06 op clients[i].addr = addr.sin_addr;
662 120381c9 2020-11-06 op connected_clients++;
671 592fd624 2020-10-07 op goodbye(struct pollfd *pfd, struct client *c)
675 592fd624 2020-10-07 op c->state = S_CLOSING;
677 592fd624 2020-10-07 op ret = tls_close(c->ctx);
678 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
679 592fd624 2020-10-07 op pfd->events = POLLIN;
682 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
683 592fd624 2020-10-07 op pfd->events = POLLOUT;
687 120381c9 2020-11-06 op connected_clients--;
689 592fd624 2020-10-07 op tls_free(c->ctx);
690 592fd624 2020-10-07 op c->ctx = NULL;
692 f28f9311 2020-10-14 op if (c->buf != MAP_FAILED)
693 f28f9311 2020-10-14 op munmap(c->buf, c->len);
695 592fd624 2020-10-07 op if (c->fd != -1)
696 592fd624 2020-10-07 op close(c->fd);
698 592fd624 2020-10-07 op close(pfd->fd);
699 592fd624 2020-10-07 op pfd->fd = -1;
703 592fd624 2020-10-07 op loop(struct tls *ctx, int sock)
706 592fd624 2020-10-07 op struct client clients[MAX_USERS];
707 592fd624 2020-10-07 op struct pollfd fds[MAX_USERS];
709 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
710 592fd624 2020-10-07 op fds[i].fd = -1;
711 592fd624 2020-10-07 op fds[i].events = POLLIN;
712 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
715 592fd624 2020-10-07 op fds[0].fd = sock;
718 16ace04d 2021-01-08 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
719 120381c9 2020-11-06 op if (errno == EINTR) {
720 d7802bb4 2020-12-02 op warnx("connected clients: %d",
721 d7802bb4 2020-12-02 op connected_clients);
724 d7802bb4 2020-12-02 op FATAL("poll: %s", strerror(errno));
727 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; i++) {
728 592fd624 2020-10-07 op if (fds[i].revents == 0)
731 592fd624 2020-10-07 op if (fds[i].revents & (POLLERR|POLLNVAL))
732 d7802bb4 2020-12-02 op FATAL("bad fd %d: %s", fds[i].fd,
733 d7802bb4 2020-12-02 op strerror(errno));
735 592fd624 2020-10-07 op if (fds[i].revents & POLLHUP) {
736 aff8d190 2020-11-06 op /* fds[i] may be the fd of the stdin
737 aff8d190 2020-11-06 op * of a cgi script that has exited. */
738 aff8d190 2020-11-06 op if (!clients[i].waiting_on_child) {
739 aff8d190 2020-11-06 op goodbye(&fds[i], &clients[i]);
744 592fd624 2020-10-07 op if (i == 0) { /* new client */
745 592fd624 2020-10-07 op do_accept(sock, ctx, fds, clients);
749 592fd624 2020-10-07 op handle(&fds[i], &clients[i]);
755 231bfcdc 2020-12-21 op absolutify_path(const char *path)
757 231bfcdc 2020-12-21 op char *wd, *r;
759 231bfcdc 2020-12-21 op if (*path == '/')
760 231bfcdc 2020-12-21 op return strdup(path);
762 231bfcdc 2020-12-21 op wd = getwd(NULL);
763 231bfcdc 2020-12-21 op if (asprintf(&r, "%s/%s", wd, path) == -1)
764 231bfcdc 2020-12-21 op err(1, "asprintf");
770 3e4749f7 2020-10-02 op usage(const char *me)
772 3e4749f7 2020-10-02 op fprintf(stderr,
773 72f653b6 2020-11-06 op "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] "
774 721e2325 2020-11-18 op "[-l logfile] [-p port] [-x cgi-bin]\n",
779 3e4749f7 2020-10-02 op main(int argc, char **argv)
781 72342dc9 2020-11-06 op const char *cert = "cert.pem", *key = "key.pem";
782 3e4749f7 2020-10-02 op struct tls *ctx = NULL;
783 3e4749f7 2020-10-02 op struct tls_config *conf;
784 3e4749f7 2020-10-02 op int sock, ch;
785 120381c9 2020-11-06 op connected_clients = 0;
787 51d876f0 2020-12-21 op if ((dir = absolutify_path("docs")) == NULL)
788 51d876f0 2020-12-21 op err(1, "absolutify_path");
792 d7802bb4 2020-12-02 op foreground = 0;
794 d7802bb4 2020-12-02 op while ((ch = getopt(argc, argv, "c:d:fhk:p:x:")) != -1) {
795 3e4749f7 2020-10-02 op switch (ch) {
797 3e4749f7 2020-10-02 op cert = optarg;
801 231bfcdc 2020-12-21 op free((char*)dir);
802 231bfcdc 2020-12-21 op if ((dir = absolutify_path(optarg)) == NULL)
803 231bfcdc 2020-12-21 op err(1, "absolutify_path");
807 d7802bb4 2020-12-02 op foreground = 1;
811 3e4749f7 2020-10-02 op usage(*argv);
815 3e4749f7 2020-10-02 op key = optarg;
823 721e2325 2020-11-18 op lval = strtol(optarg, &ep, 10);
824 721e2325 2020-11-18 op if (optarg[0] == '\0' || *ep != '\0')
825 721e2325 2020-11-18 op err(1, "not a number: %s", optarg);
826 721e2325 2020-11-18 op if (lval < 0 || lval > UINT16_MAX)
827 721e2325 2020-11-18 op err(1, "port number out of range: %s", optarg);
833 72f653b6 2020-11-06 op cgi = optarg;
837 3e4749f7 2020-10-02 op usage(*argv);
842 4a28dd01 2020-12-28 op signal(SIGPIPE, SIG_IGN);
843 4a28dd01 2020-12-28 op signal(SIGCHLD, SIG_IGN);
845 4a28dd01 2020-12-28 op #ifdef SIGINFO
846 4a28dd01 2020-12-28 op signal(SIGINFO, sig_handler);
848 4a28dd01 2020-12-28 op signal(SIGUSR2, sig_handler);
850 4a28dd01 2020-12-28 op if (!foreground)
851 4a28dd01 2020-12-28 op signal(SIGHUP, SIG_IGN);
853 3e4749f7 2020-10-02 op if ((conf = tls_config_new()) == NULL)
854 3e4749f7 2020-10-02 op err(1, "tls_config_new");
856 677afbd3 2020-12-02 op /* optionally accept client certs, but don't try to verify them */
857 677afbd3 2020-12-02 op tls_config_verify_client_optional(conf);
858 677afbd3 2020-12-02 op tls_config_insecure_noverifycert(conf);
860 0d8ca45a 2020-10-03 op if (tls_config_set_protocols(conf,
861 0d8ca45a 2020-10-03 op TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
862 0d8ca45a 2020-10-03 op err(1, "tls_config_set_protocols");
864 4d4f0e19 2020-10-03 op if (tls_config_set_cert_file(conf, cert) == -1)
865 4d4f0e19 2020-10-03 op err(1, "tls_config_set_cert_file: %s", cert);
867 4d4f0e19 2020-10-03 op if (tls_config_set_key_file(conf, key) == -1)
868 4d4f0e19 2020-10-03 op err(1, "tls_config_set_key_file: %s", key);
870 3e4749f7 2020-10-02 op if ((ctx = tls_server()) == NULL)
871 3e4749f7 2020-10-02 op err(1, "tls_server");
873 3e4749f7 2020-10-02 op if (tls_configure(ctx, conf) == -1)
874 3e4749f7 2020-10-02 op errx(1, "tls_configure: %s", tls_error(ctx));
876 721e2325 2020-11-18 op sock = make_socket(port, AF_INET);
878 3e4749f7 2020-10-02 op if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
879 3e4749f7 2020-10-02 op err(1, "open: %s", dir);
881 d7802bb4 2020-12-02 op if (!foreground && daemon(0, 1) == -1)
884 4c416739 2020-01-01 op if (unveil(dir, "rx") == -1)
885 4c416739 2020-01-01 op err(1, "unveil");
887 4c416739 2020-01-01 op if (pledge("stdio rpath inet proc exec", NULL) == -1)
888 4c416739 2020-01-01 op err(1, "pledge");
890 4c416739 2020-01-01 op /* drop proc and exec if cgi isn't enabled */
891 4c416739 2020-01-01 op if (cgi == NULL && pledge("stdio rpath inet", NULL) == -1)
892 4c416739 2020-01-01 op err(1, "pledge");
894 3e4749f7 2020-10-02 op loop(ctx, sock);
897 3e4749f7 2020-10-02 op tls_free(ctx);
898 3e4749f7 2020-10-02 op tls_config_free(conf);