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 9b374f41 2021-01-10 op #define LOG(priority, c, fmt, ...) \
33 9b374f41 2021-01-10 op char buf[INET_ADDRSTRLEN]; \
34 9b374f41 2021-01-10 op if (inet_ntop((c)->af, &(c)->addr, \
35 9b374f41 2021-01-10 op buf, sizeof(buf)) == NULL) \
36 9b374f41 2021-01-10 op FATAL("inet_ntop: %s", strerror(errno)); \
37 9b374f41 2021-01-10 op if (foreground) \
38 9b374f41 2021-01-10 op fprintf(stderr, \
39 9b374f41 2021-01-10 op "%s " fmt "\n", buf, __VA_ARGS__); \
41 9b374f41 2021-01-10 op syslog((priority) | LOG_DAEMON, \
42 9b374f41 2021-01-10 op "%s " fmt, buf, __VA_ARGS__); \
45 9b374f41 2021-01-10 op #define LOGE(c, fmt, ...) LOG(LOG_ERR, c, fmt, __VA_ARGS__)
46 9b374f41 2021-01-10 op #define LOGN(c, fmt, ...) LOG(LOG_NOTICE, c, fmt, __VA_ARGS__)
47 9b374f41 2021-01-10 op #define LOGI(c, fmt, ...) LOG(LOG_INFO, c, fmt, __VA_ARGS__)
48 9b374f41 2021-01-10 op #define LOGD(c, fmt, ...) LOG(LOG_DEBUG, c, fmt, __VA_ARGS__)
50 9b374f41 2021-01-10 op #define FATAL(fmt, ...) \
52 9b374f41 2021-01-10 op if (foreground) \
53 9b374f41 2021-01-10 op fprintf(stderr, fmt "\n", __VA_ARGS__); \
55 9b374f41 2021-01-10 op syslog(LOG_DAEMON | LOG_CRIT, \
56 9b374f41 2021-01-10 op fmt, __VA_ARGS__); \
60 d7802bb4 2020-12-02 op const char *dir, *cgi;
63 d7802bb4 2020-12-02 op int foreground;
64 120381c9 2020-11-06 op int connected_clients;
66 33d32d1f 2020-12-25 op struct etm { /* file extension to mime */
67 33d32d1f 2020-12-25 op const char *mime;
68 33d32d1f 2020-12-25 op const char *ext;
69 33d32d1f 2020-12-25 op } filetypes[] = {
70 33d32d1f 2020-12-25 op {"application/pdf", "pdf"},
72 33d32d1f 2020-12-25 op {"image/gif", "gif"},
73 33d32d1f 2020-12-25 op {"image/jpeg", "jpg"},
74 33d32d1f 2020-12-25 op {"image/jpeg", "jpeg"},
75 33d32d1f 2020-12-25 op {"image/png", "png"},
76 33d32d1f 2020-12-25 op {"image/svg+xml", "svg"},
78 33d32d1f 2020-12-25 op {"text/gemini", "gemini"},
79 33d32d1f 2020-12-25 op {"text/gemini", "gmi"},
80 33d32d1f 2020-12-25 op {"text/markdown", "markdown"},
81 33d32d1f 2020-12-25 op {"text/markdown", "md"},
82 33d32d1f 2020-12-25 op {"text/plain", "txt"},
83 33d32d1f 2020-12-25 op {"text/xml", "xml"},
88 9b374f41 2021-01-10 op static inline void
89 9b374f41 2021-01-10 op safe_setenv(const char *name, const char *val)
91 9b374f41 2021-01-10 op if (val == NULL)
93 9b374f41 2021-01-10 op setenv(name, val, 1);
97 4a28dd01 2020-12-28 op sig_handler(int sig)
103 72f653b6 2020-11-06 op starts_with(const char *str, const char *prefix)
107 72f653b6 2020-11-06 op for (i = 0; prefix[i] != '\0'; ++i)
108 72f653b6 2020-11-06 op if (str[i] != prefix[i])
114 592fd624 2020-10-07 op start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
116 3e4749f7 2020-10-02 op char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
120 592fd624 2020-10-07 op client->code = code;
121 592fd624 2020-10-07 op client->meta = reason;
122 592fd624 2020-10-07 op client->state = S_INITIALIZING;
124 3e4749f7 2020-10-02 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
125 3e4749f7 2020-10-02 op assert(len < (int)sizeof(buf));
126 592fd624 2020-10-07 op ret = tls_write(client->ctx, buf, len);
127 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
128 592fd624 2020-10-07 op pfd->events = POLLIN;
132 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
133 592fd624 2020-10-07 op pfd->events = POLLOUT;
141 f28f9311 2020-10-14 op filesize(int fd)
145 f28f9311 2020-10-14 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
147 f28f9311 2020-10-14 op if (lseek(fd, 0, SEEK_SET) == -1)
153 cc68fe70 2020-10-07 op path_ext(const char *path)
155 cc68fe70 2020-10-07 op const char *end;
157 cc68fe70 2020-10-07 op end = path + strlen(path)-1; /* the last byte before the NUL */
158 cc68fe70 2020-10-07 op for (; end != path; --end) {
159 cc68fe70 2020-10-07 op if (*end == '.')
160 cc68fe70 2020-10-07 op return end+1;
161 cc68fe70 2020-10-07 op if (*end == '/')
169 cc68fe70 2020-10-07 op mime(const char *path)
171 cc68fe70 2020-10-07 op const char *ext, *def = "application/octet-stream";
172 cc68fe70 2020-10-07 op struct etm *t;
174 cc68fe70 2020-10-07 op if ((ext = path_ext(path)) == NULL)
177 cc68fe70 2020-10-07 op for (t = filetypes; t->mime != NULL; ++t)
178 cc68fe70 2020-10-07 op if (!strcmp(ext, t->ext))
179 cc68fe70 2020-10-07 op return t->mime;
185 d7802bb4 2020-12-02 op check_path(struct client *c, const char *path, int *fd)
187 72342dc9 2020-11-06 op struct stat sb;
189 f28f9311 2020-10-14 op assert(path != NULL);
190 33d32d1f 2020-12-25 op if ((*fd = openat(dirfd, *path ? path : ".",
191 a5d310bc 2020-11-10 op O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
192 a5d310bc 2020-11-10 op return FILE_MISSING;
195 a5d310bc 2020-11-10 op if (fstat(*fd, &sb) == -1) {
196 d7802bb4 2020-12-02 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
197 a5d310bc 2020-11-10 op return FILE_MISSING;
200 a5d310bc 2020-11-10 op if (S_ISDIR(sb.st_mode))
201 a5d310bc 2020-11-10 op return FILE_DIRECTORY;
203 a5d310bc 2020-11-10 op if (sb.st_mode & S_IXUSR)
204 a5d310bc 2020-11-10 op return FILE_EXECUTABLE;
206 a5d310bc 2020-11-10 op return FILE_EXISTS;
210 a5d310bc 2020-11-10 op * the inverse of this algorithm, i.e. starting from the start of the
211 a5d310bc 2020-11-10 op * path + strlen(cgi), and checking if each component, should be
212 a5d310bc 2020-11-10 op * faster. But it's tedious to write. This does the opposite: starts
213 a5d310bc 2020-11-10 op * from the end and strip one component at a time, until either an
214 a5d310bc 2020-11-10 op * executable is found or we emptied the path.
217 a5d310bc 2020-11-10 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
220 a5d310bc 2020-11-10 op end = strchr(path, '\0');
222 a5d310bc 2020-11-10 op /* NB: assume CGI is enabled and path matches cgi */
224 a5d310bc 2020-11-10 op while (end > path) {
225 a5d310bc 2020-11-10 op /* go up one level. UNIX paths are simple and POSIX
226 a5d310bc 2020-11-10 op * dirname, with its ambiguities on if the given path
227 a5d310bc 2020-11-10 op * is changed or not, gives me headaches. */
228 a5d310bc 2020-11-10 op while (*end != '/')
232 d7802bb4 2020-12-02 op switch (check_path(c, path, &c->fd)) {
233 a5d310bc 2020-11-10 op case FILE_EXECUTABLE:
234 a5d310bc 2020-11-10 op return start_cgi(path, end+1, query, fds,c);
235 a5d310bc 2020-11-10 op case FILE_MISSING:
246 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
248 a5d310bc 2020-11-10 op goodbye(fds, c);
254 33d32d1f 2020-12-25 op open_file(char *fpath, char *query, struct pollfd *fds, struct client *c)
256 d7802bb4 2020-12-02 op switch (check_path(c, fpath, &c->fd)) {
257 a5d310bc 2020-11-10 op case FILE_EXECUTABLE:
258 5a40216f 2020-12-28 op if (cgi != NULL && starts_with(fpath, cgi))
259 a5d310bc 2020-11-10 op return start_cgi(fpath, "", query, fds, c);
261 a5d310bc 2020-11-10 op /* fallthrough */
263 a5d310bc 2020-11-10 op case FILE_EXISTS:
264 a5d310bc 2020-11-10 op if ((c->len = filesize(c->fd)) == -1) {
265 d7802bb4 2020-12-02 op LOGE(c, "failed to get file size for %s", fpath);
266 a5d310bc 2020-11-10 op goodbye(fds, c);
270 a5d310bc 2020-11-10 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
271 a5d310bc 2020-11-10 op c->fd, 0)) == MAP_FAILED) {
272 a5d310bc 2020-11-10 op warn("mmap: %s", fpath);
273 a5d310bc 2020-11-10 op goodbye(fds, c);
276 a5d310bc 2020-11-10 op c->i = c->buf;
277 a5d310bc 2020-11-10 op return start_reply(fds, c, SUCCESS, mime(fpath));
279 a5d310bc 2020-11-10 op case FILE_DIRECTORY:
280 d7802bb4 2020-12-02 op LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
281 f28f9311 2020-10-14 op close(c->fd);
283 f28f9311 2020-10-14 op send_dir(fpath, fds, c);
286 a5d310bc 2020-11-10 op case FILE_MISSING:
287 5a40216f 2020-12-28 op if (cgi != NULL && starts_with(fpath, cgi))
288 a5d310bc 2020-11-10 op return check_for_cgi(fpath, query, fds, c);
290 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
292 f28f9311 2020-10-14 op goodbye(fds, c);
296 a5d310bc 2020-11-10 op /* unreachable */
302 a5d310bc 2020-11-10 op start_cgi(const char *spath, const char *relpath, const char *query,
303 75d233f0 2020-11-06 op struct pollfd *fds, struct client *c)
306 a5d310bc 2020-11-10 op int p[2]; /* read end, write end */
308 72342dc9 2020-11-06 op if (pipe(p) == -1)
311 72342dc9 2020-11-06 op switch (pid = fork()) {
315 a5d310bc 2020-11-10 op case 0: { /* child */
316 721e2325 2020-11-18 op char *ex, *requri, *portno;
317 72342dc9 2020-11-06 op char addr[INET_ADDRSTRLEN];
318 72342dc9 2020-11-06 op char *argv[] = { NULL, NULL, NULL };
321 72342dc9 2020-11-06 op if (dup2(p[1], 1) == -1)
322 72342dc9 2020-11-06 op goto childerr;
324 72342dc9 2020-11-06 op if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
325 a5d310bc 2020-11-10 op goto childerr;
327 721e2325 2020-11-18 op if (asprintf(&portno, "%d", port) == -1)
328 721e2325 2020-11-18 op goto childerr;
330 5a40216f 2020-12-28 op if (asprintf(&ex, "%s/%s", dir, spath) == -1)
331 72342dc9 2020-11-06 op goto childerr;
333 a5d310bc 2020-11-10 op if (asprintf(&requri, "%s%s%s", spath,
334 a5d310bc 2020-11-10 op *relpath == '\0' ? "" : "/",
335 a5d310bc 2020-11-10 op relpath) == -1)
336 a5d310bc 2020-11-10 op goto childerr;
338 a5d310bc 2020-11-10 op argv[0] = argv[1] = ex;
340 60ba426e 2020-11-06 op /* fix the env */
341 9b374f41 2021-01-10 op safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
342 9b374f41 2021-01-10 op safe_setenv("SERVER_SOFTWARE", "gmid");
343 9b374f41 2021-01-10 op safe_setenv("SERVER_PORT", portno);
344 a5d310bc 2020-11-10 op /* setenv("SERVER_NAME", "", 1); */
345 9b374f41 2021-01-10 op safe_setenv("SCRIPT_NAME", spath);
346 9b374f41 2021-01-10 op safe_setenv("SCRIPT_EXECUTABLE", ex);
347 9b374f41 2021-01-10 op safe_setenv("REQUEST_URI", requri);
348 9b374f41 2021-01-10 op safe_setenv("REQUEST_RELATIVE", relpath);
349 9b374f41 2021-01-10 op safe_setenv("QUERY_STRING", query);
350 9b374f41 2021-01-10 op safe_setenv("REMOTE_HOST", addr);
351 9b374f41 2021-01-10 op safe_setenv("REMOTE_ADDR", addr);
352 9b374f41 2021-01-10 op safe_setenv("DOCUMENT_ROOT", dir);
354 677afbd3 2020-12-02 op if (tls_peer_cert_provided(c->ctx)) {
355 9b374f41 2021-01-10 op safe_setenv("AUTH_TYPE", "Certificate");
356 9b374f41 2021-01-10 op safe_setenv("REMOTE_USER", tls_peer_cert_subject(c->ctx));
357 9b374f41 2021-01-10 op safe_setenv("TLS_CLIENT_ISSUER", tls_peer_cert_issuer(c->ctx));
358 9b374f41 2021-01-10 op safe_setenv("TLS_CLIENT_HASH", tls_peer_cert_hash(c->ctx));
361 a5d310bc 2020-11-10 op execvp(ex, argv);
362 72342dc9 2020-11-06 op goto childerr;
365 72342dc9 2020-11-06 op default: /* parent */
367 72342dc9 2020-11-06 op close(c->fd);
368 72342dc9 2020-11-06 op c->fd = p[0];
369 72342dc9 2020-11-06 op c->child = pid;
370 aff8d190 2020-11-06 op mark_nonblock(c->fd);
371 aff8d190 2020-11-06 op c->state = S_SENDING;
372 72342dc9 2020-11-06 op handle_cgi(fds, c);
377 72342dc9 2020-11-06 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
379 72342dc9 2020-11-06 op goodbye(fds, c);
383 72342dc9 2020-11-06 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
389 6c6c7a0e 2020-12-28 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
393 aff8d190 2020-11-06 op if (c->waiting_on_child)
395 aff8d190 2020-11-06 op c->waiting_on_child = 1;
397 aff8d190 2020-11-06 op fds->events = POLLIN;
399 aff8d190 2020-11-06 op fd = fds->fd;
400 aff8d190 2020-11-06 op fds->fd = c->fd;
405 6c6c7a0e 2020-12-28 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
409 aff8d190 2020-11-06 op if (!c->waiting_on_child)
411 aff8d190 2020-11-06 op c->waiting_on_child = 0;
413 aff8d190 2020-11-06 op fd = fds->fd;
414 aff8d190 2020-11-06 op fds->fd = c->fd;
419 aff8d190 2020-11-06 op handle_cgi(struct pollfd *fds, struct client *c)
423 aff8d190 2020-11-06 op /* ensure c->fd is the child and fds->fd the client */
424 6c6c7a0e 2020-12-28 op cgi_poll_on_client(fds, c);
427 aff8d190 2020-11-06 op if (c->len == 0) {
428 aff8d190 2020-11-06 op if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
430 aff8d190 2020-11-06 op if (r == -1) {
431 aff8d190 2020-11-06 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
432 6c6c7a0e 2020-12-28 op cgi_poll_on_child(fds, c);
441 aff8d190 2020-11-06 op while (c->len > 0) {
442 aff8d190 2020-11-06 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
446 72342dc9 2020-11-06 op case TLS_WANT_POLLOUT:
447 aff8d190 2020-11-06 op fds->events = POLLOUT;
450 72342dc9 2020-11-06 op case TLS_WANT_POLLIN:
451 aff8d190 2020-11-06 op fds->events = POLLIN;
463 72342dc9 2020-11-06 op goodbye(fds, c);
467 75d233f0 2020-11-06 op send_file(char *path, char *query, struct pollfd *fds, struct client *c)
469 f28f9311 2020-10-14 op ssize_t ret, len;
471 f28f9311 2020-10-14 op if (c->fd == -1) {
472 75d233f0 2020-11-06 op if (!open_file(path, query, fds, c))
474 f28f9311 2020-10-14 op c->state = S_SENDING;
477 f28f9311 2020-10-14 op len = (c->buf + c->len) - c->i;
479 f28f9311 2020-10-14 op while (len > 0) {
480 f28f9311 2020-10-14 op switch (ret = tls_write(c->ctx, c->i, len)) {
482 d7802bb4 2020-12-02 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
483 f28f9311 2020-10-14 op goodbye(fds, c);
486 f28f9311 2020-10-14 op case TLS_WANT_POLLIN:
487 f28f9311 2020-10-14 op fds->events = POLLIN;
490 f28f9311 2020-10-14 op case TLS_WANT_POLLOUT:
491 f28f9311 2020-10-14 op fds->events = POLLOUT;
501 f28f9311 2020-10-14 op goodbye(fds, c);
505 592fd624 2020-10-07 op send_dir(char *path, struct pollfd *fds, struct client *client)
507 3e4749f7 2020-10-02 op char fpath[PATHBUF];
510 3e4749f7 2020-10-02 op bzero(fpath, PATHBUF);
512 9c56b0a7 2020-10-14 op if (path[0] != '.')
513 3e4749f7 2020-10-02 op fpath[0] = '.';
515 3e4749f7 2020-10-02 op /* this cannot fail since sizeof(fpath) > maxlen of path */
516 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
517 3e4749f7 2020-10-02 op len = strlen(fpath);
519 3e4749f7 2020-10-02 op /* add a trailing / in case. */
520 3e4749f7 2020-10-02 op if (fpath[len-1] != '/') {
521 3e4749f7 2020-10-02 op fpath[len] = '/';
524 3e4749f7 2020-10-02 op strlcat(fpath, "index.gmi", sizeof(fpath));
526 75d233f0 2020-11-06 op send_file(fpath, NULL, fds, client);
530 592fd624 2020-10-07 op handle(struct pollfd *fds, struct client *client)
532 592fd624 2020-10-07 op char buf[GEMINI_URL_LEN];
533 33d32d1f 2020-12-25 op const char *parse_err;
534 33d32d1f 2020-12-25 op struct uri uri;
536 592fd624 2020-10-07 op switch (client->state) {
538 592fd624 2020-10-07 op bzero(buf, GEMINI_URL_LEN);
539 592fd624 2020-10-07 op switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
541 d7802bb4 2020-12-02 op LOGE(client, "tls_read: %s", tls_error(client->ctx));
542 592fd624 2020-10-07 op goodbye(fds, client);
545 592fd624 2020-10-07 op case TLS_WANT_POLLIN:
546 592fd624 2020-10-07 op fds->events = POLLIN;
549 592fd624 2020-10-07 op case TLS_WANT_POLLOUT:
550 592fd624 2020-10-07 op fds->events = POLLOUT;
554 b3deee7b 2020-12-28 op parse_err = "invalid request";
555 33d32d1f 2020-12-25 op if (!trim_req_uri(buf) || !parse_uri(buf, &uri, &parse_err)) {
556 33d32d1f 2020-12-25 op if (!start_reply(fds, client, BAD_REQUEST, parse_err))
558 592fd624 2020-10-07 op goodbye(fds, client);
562 33d32d1f 2020-12-25 op LOGI(client, "GET %s%s%s",
563 33d32d1f 2020-12-25 op *uri.path ? uri.path : "/",
564 33d32d1f 2020-12-25 op *uri.query ? "?" : "",
565 33d32d1f 2020-12-25 op *uri.query ? uri.query : "");
567 33d32d1f 2020-12-25 op send_file(uri.path, uri.query, fds, client);
570 592fd624 2020-10-07 op case S_INITIALIZING:
571 592fd624 2020-10-07 op if (!start_reply(fds, client, client->code, client->meta))
574 592fd624 2020-10-07 op if (client->code != SUCCESS) {
575 592fd624 2020-10-07 op /* we don't need a body */
576 592fd624 2020-10-07 op goodbye(fds, client);
580 592fd624 2020-10-07 op client->state = S_SENDING;
582 592fd624 2020-10-07 op /* fallthrough */
584 592fd624 2020-10-07 op case S_SENDING:
585 aff8d190 2020-11-06 op if (client->child != -1)
586 aff8d190 2020-11-06 op handle_cgi(fds, client);
588 aff8d190 2020-11-06 op send_file(NULL, NULL, fds, client);
591 592fd624 2020-10-07 op case S_CLOSING:
592 592fd624 2020-10-07 op goodbye(fds, client);
596 592fd624 2020-10-07 op /* unreachable */
602 592fd624 2020-10-07 op mark_nonblock(int fd)
606 592fd624 2020-10-07 op if ((flags = fcntl(fd, F_GETFL)) == -1)
607 d7802bb4 2020-12-02 op FATAL("fcntl(F_GETFL): %s", strerror(errno));
608 592fd624 2020-10-07 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
609 d7802bb4 2020-12-02 op FATAL("fcntl(F_SETFL): %s", strerror(errno));
613 9468027b 2020-10-15 op make_socket(int port, int family)
616 9468027b 2020-10-15 op struct sockaddr_in addr4;
617 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
618 9468027b 2020-10-15 op struct sockaddr *addr;
619 9468027b 2020-10-15 op socklen_t len;
621 9468027b 2020-10-15 op switch (family) {
622 9468027b 2020-10-15 op case AF_INET:
623 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
624 9468027b 2020-10-15 op addr4.sin_family = family;
625 9468027b 2020-10-15 op addr4.sin_port = htons(port);
626 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
627 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
628 9468027b 2020-10-15 op len = sizeof(addr4);
631 9468027b 2020-10-15 op case AF_INET6:
632 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
633 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
634 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
635 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
636 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
637 9468027b 2020-10-15 op len = sizeof(addr6);
641 9468027b 2020-10-15 op /* unreachable */
645 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
646 d7802bb4 2020-12-02 op FATAL("socket: %s", strerror(errno));
649 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
650 d7802bb4 2020-12-02 op FATAL("setsockopt(SO_REUSEADDR): %s", strerror(errno));
653 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
654 d7802bb4 2020-12-02 op FATAL("setsockopt(SO_REUSEPORT): %s", strerror(errno));
656 592fd624 2020-10-07 op mark_nonblock(sock);
658 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
659 d7802bb4 2020-12-02 op FATAL("bind: %s", strerror(errno));
661 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
662 d7802bb4 2020-12-02 op FATAL("listen: %s", strerror(errno));
668 592fd624 2020-10-07 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
671 592fd624 2020-10-07 op struct sockaddr_in addr;
672 3e4749f7 2020-10-02 op socklen_t len;
674 592fd624 2020-10-07 op len = sizeof(addr);
675 592fd624 2020-10-07 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
676 592fd624 2020-10-07 op if (errno == EWOULDBLOCK)
678 d7802bb4 2020-12-02 op FATAL("accept: %s", strerror(errno));
681 592fd624 2020-10-07 op mark_nonblock(fd);
683 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
684 592fd624 2020-10-07 op if (fds[i].fd == -1) {
685 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
686 592fd624 2020-10-07 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
687 592fd624 2020-10-07 op break; /* goodbye fd! */
689 592fd624 2020-10-07 op fds[i].fd = fd;
690 592fd624 2020-10-07 op fds[i].events = POLLIN;
692 592fd624 2020-10-07 op clients[i].state = S_OPEN;
693 592fd624 2020-10-07 op clients[i].fd = -1;
694 72342dc9 2020-11-06 op clients[i].child = -1;
695 f28f9311 2020-10-14 op clients[i].buf = MAP_FAILED;
696 2c3a40fa 2020-11-06 op clients[i].af = AF_INET;
697 2c3a40fa 2020-11-06 op clients[i].addr = addr.sin_addr;
699 120381c9 2020-11-06 op connected_clients++;
708 592fd624 2020-10-07 op goodbye(struct pollfd *pfd, struct client *c)
712 592fd624 2020-10-07 op c->state = S_CLOSING;
714 592fd624 2020-10-07 op ret = tls_close(c->ctx);
715 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
716 592fd624 2020-10-07 op pfd->events = POLLIN;
719 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
720 592fd624 2020-10-07 op pfd->events = POLLOUT;
724 120381c9 2020-11-06 op connected_clients--;
726 592fd624 2020-10-07 op tls_free(c->ctx);
727 592fd624 2020-10-07 op c->ctx = NULL;
729 f28f9311 2020-10-14 op if (c->buf != MAP_FAILED)
730 f28f9311 2020-10-14 op munmap(c->buf, c->len);
732 592fd624 2020-10-07 op if (c->fd != -1)
733 592fd624 2020-10-07 op close(c->fd);
735 592fd624 2020-10-07 op close(pfd->fd);
736 592fd624 2020-10-07 op pfd->fd = -1;
740 592fd624 2020-10-07 op loop(struct tls *ctx, int sock)
743 592fd624 2020-10-07 op struct client clients[MAX_USERS];
744 592fd624 2020-10-07 op struct pollfd fds[MAX_USERS];
746 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
747 592fd624 2020-10-07 op fds[i].fd = -1;
748 592fd624 2020-10-07 op fds[i].events = POLLIN;
749 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
752 592fd624 2020-10-07 op fds[0].fd = sock;
755 16ace04d 2021-01-08 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
756 120381c9 2020-11-06 op if (errno == EINTR) {
757 d7802bb4 2020-12-02 op warnx("connected clients: %d",
758 d7802bb4 2020-12-02 op connected_clients);
761 d7802bb4 2020-12-02 op FATAL("poll: %s", strerror(errno));
764 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; i++) {
765 592fd624 2020-10-07 op if (fds[i].revents == 0)
768 592fd624 2020-10-07 op if (fds[i].revents & (POLLERR|POLLNVAL))
769 d7802bb4 2020-12-02 op FATAL("bad fd %d: %s", fds[i].fd,
770 d7802bb4 2020-12-02 op strerror(errno));
772 592fd624 2020-10-07 op if (fds[i].revents & POLLHUP) {
773 aff8d190 2020-11-06 op /* fds[i] may be the fd of the stdin
774 aff8d190 2020-11-06 op * of a cgi script that has exited. */
775 aff8d190 2020-11-06 op if (!clients[i].waiting_on_child) {
776 aff8d190 2020-11-06 op goodbye(&fds[i], &clients[i]);
781 592fd624 2020-10-07 op if (i == 0) { /* new client */
782 592fd624 2020-10-07 op do_accept(sock, ctx, fds, clients);
786 592fd624 2020-10-07 op handle(&fds[i], &clients[i]);
792 231bfcdc 2020-12-21 op absolutify_path(const char *path)
794 231bfcdc 2020-12-21 op char *wd, *r;
796 231bfcdc 2020-12-21 op if (*path == '/')
797 231bfcdc 2020-12-21 op return strdup(path);
799 231bfcdc 2020-12-21 op wd = getwd(NULL);
800 231bfcdc 2020-12-21 op if (asprintf(&r, "%s/%s", wd, path) == -1)
801 231bfcdc 2020-12-21 op err(1, "asprintf");
807 3e4749f7 2020-10-02 op usage(const char *me)
809 3e4749f7 2020-10-02 op fprintf(stderr,
810 72f653b6 2020-11-06 op "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem] "
811 721e2325 2020-11-18 op "[-l logfile] [-p port] [-x cgi-bin]\n",
816 3e4749f7 2020-10-02 op main(int argc, char **argv)
818 72342dc9 2020-11-06 op const char *cert = "cert.pem", *key = "key.pem";
819 3e4749f7 2020-10-02 op struct tls *ctx = NULL;
820 3e4749f7 2020-10-02 op struct tls_config *conf;
821 3e4749f7 2020-10-02 op int sock, ch;
822 120381c9 2020-11-06 op connected_clients = 0;
824 51d876f0 2020-12-21 op if ((dir = absolutify_path("docs")) == NULL)
825 51d876f0 2020-12-21 op err(1, "absolutify_path");
829 d7802bb4 2020-12-02 op foreground = 0;
831 d7802bb4 2020-12-02 op while ((ch = getopt(argc, argv, "c:d:fhk:p:x:")) != -1) {
832 3e4749f7 2020-10-02 op switch (ch) {
834 3e4749f7 2020-10-02 op cert = optarg;
838 231bfcdc 2020-12-21 op free((char*)dir);
839 231bfcdc 2020-12-21 op if ((dir = absolutify_path(optarg)) == NULL)
840 231bfcdc 2020-12-21 op err(1, "absolutify_path");
844 d7802bb4 2020-12-02 op foreground = 1;
848 3e4749f7 2020-10-02 op usage(*argv);
852 3e4749f7 2020-10-02 op key = optarg;
860 721e2325 2020-11-18 op lval = strtol(optarg, &ep, 10);
861 721e2325 2020-11-18 op if (optarg[0] == '\0' || *ep != '\0')
862 721e2325 2020-11-18 op err(1, "not a number: %s", optarg);
863 721e2325 2020-11-18 op if (lval < 0 || lval > UINT16_MAX)
864 721e2325 2020-11-18 op err(1, "port number out of range: %s", optarg);
870 72f653b6 2020-11-06 op cgi = optarg;
874 3e4749f7 2020-10-02 op usage(*argv);
879 4a28dd01 2020-12-28 op signal(SIGPIPE, SIG_IGN);
880 4a28dd01 2020-12-28 op signal(SIGCHLD, SIG_IGN);
882 4a28dd01 2020-12-28 op #ifdef SIGINFO
883 4a28dd01 2020-12-28 op signal(SIGINFO, sig_handler);
885 4a28dd01 2020-12-28 op signal(SIGUSR2, sig_handler);
887 4a28dd01 2020-12-28 op if (!foreground)
888 4a28dd01 2020-12-28 op signal(SIGHUP, SIG_IGN);
890 3e4749f7 2020-10-02 op if ((conf = tls_config_new()) == NULL)
891 3e4749f7 2020-10-02 op err(1, "tls_config_new");
893 677afbd3 2020-12-02 op /* optionally accept client certs, but don't try to verify them */
894 677afbd3 2020-12-02 op tls_config_verify_client_optional(conf);
895 677afbd3 2020-12-02 op tls_config_insecure_noverifycert(conf);
897 0d8ca45a 2020-10-03 op if (tls_config_set_protocols(conf,
898 0d8ca45a 2020-10-03 op TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
899 0d8ca45a 2020-10-03 op err(1, "tls_config_set_protocols");
901 4d4f0e19 2020-10-03 op if (tls_config_set_cert_file(conf, cert) == -1)
902 4d4f0e19 2020-10-03 op err(1, "tls_config_set_cert_file: %s", cert);
904 4d4f0e19 2020-10-03 op if (tls_config_set_key_file(conf, key) == -1)
905 4d4f0e19 2020-10-03 op err(1, "tls_config_set_key_file: %s", key);
907 3e4749f7 2020-10-02 op if ((ctx = tls_server()) == NULL)
908 3e4749f7 2020-10-02 op err(1, "tls_server");
910 3e4749f7 2020-10-02 op if (tls_configure(ctx, conf) == -1)
911 3e4749f7 2020-10-02 op errx(1, "tls_configure: %s", tls_error(ctx));
913 721e2325 2020-11-18 op sock = make_socket(port, AF_INET);
915 3e4749f7 2020-10-02 op if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
916 3e4749f7 2020-10-02 op err(1, "open: %s", dir);
918 d7802bb4 2020-12-02 op if (!foreground && daemon(0, 1) == -1)
921 4c416739 2020-01-01 op if (unveil(dir, "rx") == -1)
922 4c416739 2020-01-01 op err(1, "unveil");
924 4c416739 2020-01-01 op if (pledge("stdio rpath inet proc exec", NULL) == -1)
925 4c416739 2020-01-01 op err(1, "pledge");
927 4c416739 2020-01-01 op /* drop proc and exec if cgi isn't enabled */
928 4c416739 2020-01-01 op if (cgi == NULL && pledge("stdio rpath inet", NULL) == -1)
929 4c416739 2020-01-01 op err(1, "pledge");
931 3e4749f7 2020-10-02 op loop(ctx, sock);
934 3e4749f7 2020-10-02 op tls_free(ctx);
935 3e4749f7 2020-10-02 op tls_config_free(conf);