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 2c3a40fa 2020-11-06 op #include <arpa/inet.h>
22 3e4749f7 2020-10-02 op #include <netinet/in.h>
24 3e4749f7 2020-10-02 op #include <assert.h>
25 3e4749f7 2020-10-02 op #include <err.h>
26 592fd624 2020-10-07 op #include <errno.h>
27 3e4749f7 2020-10-02 op #include <fcntl.h>
28 592fd624 2020-10-07 op #include <poll.h>
29 0cf902af 2020-11-03 op #include <signal.h>
30 3e4749f7 2020-10-02 op #include <stdio.h>
31 3e4749f7 2020-10-02 op #include <stdlib.h>
32 3e4749f7 2020-10-02 op #include <string.h>
33 3e4749f7 2020-10-02 op #include <tls.h>
34 3e4749f7 2020-10-02 op #include <unistd.h>
36 3e4749f7 2020-10-02 op #ifndef __OpenBSD__
37 3e4749f7 2020-10-02 op # define pledge(a, b) 0
38 3e4749f7 2020-10-02 op # define unveil(a, b) 0
39 3e4749f7 2020-10-02 op #endif /* __OpenBSD__ */
41 592fd624 2020-10-07 op #ifndef INFTIM
42 592fd624 2020-10-07 op # define INFTIM -1
43 592fd624 2020-10-07 op #endif /* INFTIM */
45 3e4749f7 2020-10-02 op #define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
47 4d4f0e19 2020-10-03 op /* large enough to hold a copy of a gemini URL and still have extra room */
48 d95b77a0 2020-10-07 op #define PATHBUF 2048
50 3e4749f7 2020-10-02 op #define SUCCESS 20
51 72342dc9 2020-11-06 op #define TEMP_FAILURE 40
52 3e4749f7 2020-10-02 op #define NOT_FOUND 51
53 3e4749f7 2020-10-02 op #define BAD_REQUEST 59
55 592fd624 2020-10-07 op #ifndef MAX_USERS
56 592fd624 2020-10-07 op #define MAX_USERS 64
61 592fd624 2020-10-07 op S_INITIALIZING,
66 592fd624 2020-10-07 op struct client {
67 592fd624 2020-10-07 op struct tls *ctx;
70 592fd624 2020-10-07 op const char *meta;
71 aff8d190 2020-11-06 op int fd, waiting_on_child;
73 aff8d190 2020-11-06 op char sbuf[1024]; /* static buffer */
74 aff8d190 2020-11-06 op void *buf, *i; /* mmap buffer */
75 aff8d190 2020-11-06 op ssize_t len, off; /* mmap/static buffer */
77 2c3a40fa 2020-11-06 op struct in_addr addr;
80 cc68fe70 2020-10-07 op struct etm { /* file extension to mime */
81 cc68fe70 2020-10-07 op const char *mime;
82 cc68fe70 2020-10-07 op const char *ext;
83 cc68fe70 2020-10-07 op } filetypes[] = {
84 cc68fe70 2020-10-07 op {"application/pdf", "pdf"},
86 cc68fe70 2020-10-07 op {"image/gif", "gif"},
87 cc68fe70 2020-10-07 op {"image/jpeg", "jpg"},
88 cc68fe70 2020-10-07 op {"image/jpeg", "jpeg"},
89 cc68fe70 2020-10-07 op {"image/png", "png"},
90 cc68fe70 2020-10-07 op {"image/svg+xml", "svg"},
92 cc68fe70 2020-10-07 op {"text/gemini", "gemini"},
93 cc68fe70 2020-10-07 op {"text/gemini", "gmi"},
94 cc68fe70 2020-10-07 op {"text/markdown", "markdown"},
95 cc68fe70 2020-10-07 op {"text/markdown", "md"},
96 cc68fe70 2020-10-07 op {"text/plain", "txt"},
97 932b001a 2020-11-06 op {"text/xml", "xml"},
102 2c3a40fa 2020-11-06 op #define LOG(c, fmt, ...) \
104 2c3a40fa 2020-11-06 op char buf[INET_ADDRSTRLEN]; \
105 2c3a40fa 2020-11-06 op if (inet_ntop((c)->af, &(c)->addr, buf, sizeof(buf)) == NULL) \
106 2c3a40fa 2020-11-06 op err(1, "inet_ntop"); \
107 2c3a40fa 2020-11-06 op dprintf(logfd, "[%s] " fmt "\n", buf, __VA_ARGS__); \
110 72342dc9 2020-11-06 op const char *dir;
111 2c3a40fa 2020-11-06 op int dirfd, logfd;
114 592fd624 2020-10-07 op char *url_after_proto(char*);
115 592fd624 2020-10-07 op char *url_start_of_request(char*);
116 2c3a40fa 2020-11-06 op int url_trim(struct client*, char*);
117 aa0fe0cf 2020-11-06 op char *adjust_path(char*);
118 592fd624 2020-10-07 op int path_isdir(char*);
119 f28f9311 2020-10-14 op ssize_t filesize(int);
121 592fd624 2020-10-07 op int start_reply(struct pollfd*, struct client*, int, const char*);
122 cc68fe70 2020-10-07 op const char *path_ext(const char*);
123 cc68fe70 2020-10-07 op const char *mime(const char*);
124 75d233f0 2020-11-06 op int open_file(char*, char*, struct pollfd*, struct client*);
125 75d233f0 2020-11-06 op void start_cgi(const char*, const char*, struct pollfd*, struct client*);
126 aff8d190 2020-11-06 op void cgi_setpoll_on_child(struct pollfd*, struct client*);
127 aff8d190 2020-11-06 op void cgi_setpoll_on_client(struct pollfd*, struct client*);
128 72342dc9 2020-11-06 op void handle_cgi(struct pollfd*, struct client*);
129 75d233f0 2020-11-06 op void send_file(char*, char*, struct pollfd*, struct client*);
130 592fd624 2020-10-07 op void send_dir(char*, struct pollfd*, struct client*);
131 592fd624 2020-10-07 op void handle(struct pollfd*, struct client*);
133 592fd624 2020-10-07 op void mark_nonblock(int);
134 592fd624 2020-10-07 op int make_soket(int);
135 592fd624 2020-10-07 op void do_accept(int, struct tls*, struct pollfd*, struct client*);
136 592fd624 2020-10-07 op void goodbye(struct pollfd*, struct client*);
137 592fd624 2020-10-07 op void loop(struct tls*, int);
139 592fd624 2020-10-07 op void usage(const char*);
142 3e4749f7 2020-10-02 op url_after_proto(char *url)
145 3e4749f7 2020-10-02 op const char *proto = "gemini";
146 3e4749f7 2020-10-02 op const char *marker = "://";
149 3c19febb 2020-11-06 op /* a relative URL */
150 3e4749f7 2020-10-02 op if ((s = strstr(url, marker)) == NULL)
153 3c19febb 2020-11-06 op if (s - strlen(proto) != url)
156 3c19febb 2020-11-06 op for (i = 0; proto[i] != '\0'; ++i)
157 3c19febb 2020-11-06 op if (url[i] != proto[i])
160 3e4749f7 2020-10-02 op /* a valid gemini:// URL */
161 3e4749f7 2020-10-02 op return s + strlen(marker);
165 3e4749f7 2020-10-02 op url_start_of_request(char *url)
169 3e4749f7 2020-10-02 op if ((s = url_after_proto(url)) == NULL)
172 3e4749f7 2020-10-02 op if ((t = strstr(s, "/")) == NULL)
173 3e4749f7 2020-10-02 op return s + strlen(s);
178 2c3a40fa 2020-11-06 op url_trim(struct client *c, char *url)
180 3e4749f7 2020-10-02 op const char *e = "\r\n";
183 3e4749f7 2020-10-02 op if ((s = strstr(url, e)) == NULL)
188 3e4749f7 2020-10-02 op if (s[2] != '\0') {
189 2c3a40fa 2020-11-06 op LOG(c, "%s", "request longer than 1024 bytes\n");
197 3e4749f7 2020-10-02 op adjust_path(char *path)
199 aa0fe0cf 2020-11-06 op char *s, *query;
202 aa0fe0cf 2020-11-06 op if ((query = strchr(path, '?')) != NULL) {
203 aa0fe0cf 2020-11-06 op *query = '\0';
207 f28f9311 2020-10-14 op /* /.. -> / */
208 3e4749f7 2020-10-02 op len = strlen(path);
209 3e4749f7 2020-10-02 op if (len >= 3) {
210 3e4749f7 2020-10-02 op if (!strcmp(&path[len-3], "/..")) {
211 3e4749f7 2020-10-02 op path[len-2] = '\0';
215 3e4749f7 2020-10-02 op /* if the path is only `..` trim out and exit */
216 3e4749f7 2020-10-02 op if (!strcmp(path, "..")) {
217 3e4749f7 2020-10-02 op path[0] = '\0';
218 aa0fe0cf 2020-11-06 op return query;
221 3e4749f7 2020-10-02 op /* remove every ../ in the path */
223 3e4749f7 2020-10-02 op if ((s = strstr(path, "../")) == NULL)
224 aa0fe0cf 2020-11-06 op return query;
225 67328d23 2020-10-03 op memmove(s, s+3, strlen(s)+1); /* copy also the \0 */
230 3e4749f7 2020-10-02 op path_isdir(char *path)
232 3e4749f7 2020-10-02 op if (*path == '\0')
234 4d4f0e19 2020-10-03 op return path[strlen(path)-1] == '/';
238 592fd624 2020-10-07 op start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
240 3e4749f7 2020-10-02 op char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
244 592fd624 2020-10-07 op client->code = code;
245 592fd624 2020-10-07 op client->meta = reason;
246 592fd624 2020-10-07 op client->state = S_INITIALIZING;
248 3e4749f7 2020-10-02 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
249 3e4749f7 2020-10-02 op assert(len < (int)sizeof(buf));
250 592fd624 2020-10-07 op ret = tls_write(client->ctx, buf, len);
251 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
252 592fd624 2020-10-07 op pfd->events = POLLIN;
256 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
257 592fd624 2020-10-07 op pfd->events = POLLOUT;
265 f28f9311 2020-10-14 op filesize(int fd)
269 f28f9311 2020-10-14 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
271 f28f9311 2020-10-14 op if (lseek(fd, 0, SEEK_SET) == -1)
277 cc68fe70 2020-10-07 op path_ext(const char *path)
279 cc68fe70 2020-10-07 op const char *end;
281 cc68fe70 2020-10-07 op end = path + strlen(path)-1; /* the last byte before the NUL */
282 cc68fe70 2020-10-07 op for (; end != path; --end) {
283 cc68fe70 2020-10-07 op if (*end == '.')
284 cc68fe70 2020-10-07 op return end+1;
285 cc68fe70 2020-10-07 op if (*end == '/')
293 cc68fe70 2020-10-07 op mime(const char *path)
295 cc68fe70 2020-10-07 op const char *ext, *def = "application/octet-stream";
296 cc68fe70 2020-10-07 op struct etm *t;
298 cc68fe70 2020-10-07 op if ((ext = path_ext(path)) == NULL)
301 cc68fe70 2020-10-07 op for (t = filetypes; t->mime != NULL; ++t)
302 cc68fe70 2020-10-07 op if (!strcmp(ext, t->ext))
303 cc68fe70 2020-10-07 op return t->mime;
309 75d233f0 2020-11-06 op open_file(char *path, char *query, struct pollfd *fds, struct client *c)
311 3e4749f7 2020-10-02 op char fpath[PATHBUF];
312 72342dc9 2020-11-06 op struct stat sb;
314 f28f9311 2020-10-14 op assert(path != NULL);
316 f28f9311 2020-10-14 op bzero(fpath, sizeof(fpath));
318 f28f9311 2020-10-14 op if (*path != '.')
319 f28f9311 2020-10-14 op fpath[0] = '.';
320 f28f9311 2020-10-14 op strlcat(fpath, path, PATHBUF);
322 e8cac16e 2020-11-06 op if ((c->fd = openat(dirfd, fpath,
323 e8cac16e 2020-11-06 op O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
324 2c3a40fa 2020-11-06 op LOG(c, "open failed: %s", fpath);
325 f28f9311 2020-10-14 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
327 f28f9311 2020-10-14 op goodbye(fds, c);
331 72342dc9 2020-11-06 op if (fstat(c->fd, &sb) == -1) {
332 72342dc9 2020-11-06 op LOG(c, "fstat failed for %s", fpath);
333 72342dc9 2020-11-06 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
335 72342dc9 2020-11-06 op goodbye(fds, c);
339 72342dc9 2020-11-06 op if (S_ISDIR(sb.st_mode)) {
340 2c3a40fa 2020-11-06 op LOG(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
341 f28f9311 2020-10-14 op close(c->fd);
343 f28f9311 2020-10-14 op send_dir(fpath, fds, c);
347 72342dc9 2020-11-06 op if (cgi && (sb.st_mode & S_IXUSR)) {
348 75d233f0 2020-11-06 op start_cgi(fpath, query, fds, c);
352 f28f9311 2020-10-14 op if ((c->len = filesize(c->fd)) == -1) {
353 2c3a40fa 2020-11-06 op LOG(c, "failed to get file size for %s", fpath);
354 f28f9311 2020-10-14 op goodbye(fds, c);
358 f28f9311 2020-10-14 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
359 f28f9311 2020-10-14 op c->fd, 0)) == MAP_FAILED) {
360 f28f9311 2020-10-14 op warn("mmap: %s", fpath);
361 f28f9311 2020-10-14 op goodbye(fds, c);
364 f28f9311 2020-10-14 op c->i = c->buf;
366 f28f9311 2020-10-14 op return start_reply(fds, c, SUCCESS, mime(fpath));
370 75d233f0 2020-11-06 op start_cgi(const char *path, const char *query,
371 75d233f0 2020-11-06 op struct pollfd *fds, struct client *c)
376 72342dc9 2020-11-06 op if (pipe(p) == -1)
379 72342dc9 2020-11-06 op switch (pid = fork()) {
383 72342dc9 2020-11-06 op case 0: { /* child */
384 72342dc9 2020-11-06 op char *expath;
385 72342dc9 2020-11-06 op char addr[INET_ADDRSTRLEN];
386 72342dc9 2020-11-06 op char *argv[] = { NULL, NULL, NULL };
388 60ba426e 2020-11-06 op /* skip the initial ./ */
391 72342dc9 2020-11-06 op close(p[0]); /* close the read end */
392 72342dc9 2020-11-06 op if (dup2(p[1], 1) == -1)
393 72342dc9 2020-11-06 op goto childerr;
395 72342dc9 2020-11-06 op if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
396 72342dc9 2020-11-06 op goto childerr;
398 72342dc9 2020-11-06 op /* skip the ./ at the start of path*/
399 60ba426e 2020-11-06 op if (asprintf(&expath, "%s%s", dir, path) == -1)
400 72342dc9 2020-11-06 op goto childerr;
401 72342dc9 2020-11-06 op argv[0] = argv[1] = expath;
403 60ba426e 2020-11-06 op /* fix the env */
404 60ba426e 2020-11-06 op setenv("SERVER_SOFTWARE", "gmid", 1);
405 60ba426e 2020-11-06 op /* setenv("SERVER_NAME", "", 1); */
406 60ba426e 2020-11-06 op /* setenv("GATEWAY_INTERFACE", "CGI/version", 1); */
407 60ba426e 2020-11-06 op setenv("SERVER_PROTOCOL", "gemini", 1);
408 60ba426e 2020-11-06 op setenv("SERVER_PORT", "1965", 1);
409 60ba426e 2020-11-06 op setenv("PATH_INFO", path, 1);
410 60ba426e 2020-11-06 op setenv("PATH_TRANSLATED", expath, 1);
411 60ba426e 2020-11-06 op setenv("QUERY_STRING", query ? query : "", 1);
412 60ba426e 2020-11-06 op setenv("REMOTE_ADDR", addr, 1);
414 60ba426e 2020-11-06 op execvp(expath, argv);
415 72342dc9 2020-11-06 op goto childerr;
418 72342dc9 2020-11-06 op default: /* parent */
419 72342dc9 2020-11-06 op close(p[1]); /* close the write end */
420 72342dc9 2020-11-06 op close(c->fd);
421 72342dc9 2020-11-06 op c->fd = p[0];
422 72342dc9 2020-11-06 op c->child = pid;
423 aff8d190 2020-11-06 op mark_nonblock(c->fd);
424 aff8d190 2020-11-06 op c->state = S_SENDING;
425 72342dc9 2020-11-06 op handle_cgi(fds, c);
430 72342dc9 2020-11-06 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
432 72342dc9 2020-11-06 op goodbye(fds, c);
436 72342dc9 2020-11-06 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
439 aff8d190 2020-11-06 op /* don't call atexit stuff */
444 aff8d190 2020-11-06 op cgi_setpoll_on_child(struct pollfd *fds, struct client *c)
448 aff8d190 2020-11-06 op if (c->waiting_on_child)
450 aff8d190 2020-11-06 op c->waiting_on_child = 1;
452 aff8d190 2020-11-06 op fds->events = POLLIN;
454 aff8d190 2020-11-06 op fd = fds->fd;
455 aff8d190 2020-11-06 op fds->fd = c->fd;
460 aff8d190 2020-11-06 op cgi_setpoll_on_client(struct pollfd *fds, struct client *c)
464 aff8d190 2020-11-06 op if (!c->waiting_on_child)
466 aff8d190 2020-11-06 op c->waiting_on_child = 0;
468 aff8d190 2020-11-06 op fd = fds->fd;
469 aff8d190 2020-11-06 op fds->fd = c->fd;
474 aff8d190 2020-11-06 op handle_cgi(struct pollfd *fds, struct client *c)
478 aff8d190 2020-11-06 op /* ensure c->fd is the child and fds->fd the client */
479 aff8d190 2020-11-06 op cgi_setpoll_on_client(fds, c);
482 aff8d190 2020-11-06 op if (c->len == 0) {
483 aff8d190 2020-11-06 op if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
485 aff8d190 2020-11-06 op if (r == -1) {
486 aff8d190 2020-11-06 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
487 aff8d190 2020-11-06 op cgi_setpoll_on_child(fds, c);
496 aff8d190 2020-11-06 op while (c->len > 0) {
497 aff8d190 2020-11-06 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
501 72342dc9 2020-11-06 op case TLS_WANT_POLLOUT:
502 aff8d190 2020-11-06 op fds->events = POLLOUT;
505 72342dc9 2020-11-06 op case TLS_WANT_POLLIN:
506 aff8d190 2020-11-06 op fds->events = POLLIN;
518 72342dc9 2020-11-06 op goodbye(fds, c);
522 75d233f0 2020-11-06 op send_file(char *path, char *query, struct pollfd *fds, struct client *c)
524 f28f9311 2020-10-14 op ssize_t ret, len;
526 f28f9311 2020-10-14 op if (c->fd == -1) {
527 75d233f0 2020-11-06 op if (!open_file(path, query, fds, c))
529 f28f9311 2020-10-14 op c->state = S_SENDING;
532 f28f9311 2020-10-14 op len = (c->buf + c->len) - c->i;
534 f28f9311 2020-10-14 op while (len > 0) {
535 f28f9311 2020-10-14 op switch (ret = tls_write(c->ctx, c->i, len)) {
537 2c3a40fa 2020-11-06 op LOG(c, "tls_write: %s", tls_error(c->ctx));
538 f28f9311 2020-10-14 op goodbye(fds, c);
541 f28f9311 2020-10-14 op case TLS_WANT_POLLIN:
542 f28f9311 2020-10-14 op fds->events = POLLIN;
545 f28f9311 2020-10-14 op case TLS_WANT_POLLOUT:
546 f28f9311 2020-10-14 op fds->events = POLLOUT;
556 f28f9311 2020-10-14 op goodbye(fds, c);
560 592fd624 2020-10-07 op send_dir(char *path, struct pollfd *fds, struct client *client)
562 3e4749f7 2020-10-02 op char fpath[PATHBUF];
565 3e4749f7 2020-10-02 op bzero(fpath, PATHBUF);
567 9c56b0a7 2020-10-14 op if (path[0] != '.')
568 3e4749f7 2020-10-02 op fpath[0] = '.';
570 3e4749f7 2020-10-02 op /* this cannot fail since sizeof(fpath) > maxlen of path */
571 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
572 3e4749f7 2020-10-02 op len = strlen(fpath);
574 3e4749f7 2020-10-02 op /* add a trailing / in case. */
575 3e4749f7 2020-10-02 op if (fpath[len-1] != '/') {
576 3e4749f7 2020-10-02 op fpath[len] = '/';
579 3e4749f7 2020-10-02 op strlcat(fpath, "index.gmi", sizeof(fpath));
581 75d233f0 2020-11-06 op send_file(fpath, NULL, fds, client);
585 592fd624 2020-10-07 op handle(struct pollfd *fds, struct client *client)
587 592fd624 2020-10-07 op char buf[GEMINI_URL_LEN];
591 592fd624 2020-10-07 op switch (client->state) {
593 592fd624 2020-10-07 op bzero(buf, GEMINI_URL_LEN);
594 592fd624 2020-10-07 op switch (tls_read(client->ctx, buf, sizeof(buf)-1)) {
596 2c3a40fa 2020-11-06 op LOG(client, "tls_read: %s", tls_error(client->ctx));
597 592fd624 2020-10-07 op goodbye(fds, client);
600 592fd624 2020-10-07 op case TLS_WANT_POLLIN:
601 592fd624 2020-10-07 op fds->events = POLLIN;
604 592fd624 2020-10-07 op case TLS_WANT_POLLOUT:
605 592fd624 2020-10-07 op fds->events = POLLOUT;
609 2c3a40fa 2020-11-06 op if (!url_trim(client, buf)) {
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 592fd624 2020-10-07 op if ((path = url_start_of_request(buf)) == NULL) {
617 592fd624 2020-10-07 op if (!start_reply(fds, client, BAD_REQUEST, "bad request"))
619 592fd624 2020-10-07 op goodbye(fds, client);
623 aa0fe0cf 2020-11-06 op query = adjust_path(path);
624 aa0fe0cf 2020-11-06 op LOG(client, "get %s%s%s", path,
625 aa0fe0cf 2020-11-06 op query ? "?" : "",
626 aa0fe0cf 2020-11-06 op query ? query : "");
628 592fd624 2020-10-07 op if (path_isdir(path))
629 592fd624 2020-10-07 op send_dir(path, fds, client);
631 75d233f0 2020-11-06 op send_file(path, query, fds, client);
634 592fd624 2020-10-07 op case S_INITIALIZING:
635 592fd624 2020-10-07 op if (!start_reply(fds, client, client->code, client->meta))
638 592fd624 2020-10-07 op if (client->code != SUCCESS) {
639 592fd624 2020-10-07 op /* we don't need a body */
640 592fd624 2020-10-07 op goodbye(fds, client);
644 592fd624 2020-10-07 op client->state = S_SENDING;
646 592fd624 2020-10-07 op /* fallthrough */
648 592fd624 2020-10-07 op case S_SENDING:
649 aff8d190 2020-11-06 op if (client->child != -1)
650 aff8d190 2020-11-06 op handle_cgi(fds, client);
652 aff8d190 2020-11-06 op send_file(NULL, NULL, fds, client);
655 592fd624 2020-10-07 op case S_CLOSING:
656 592fd624 2020-10-07 op goodbye(fds, client);
660 592fd624 2020-10-07 op /* unreachable */
666 592fd624 2020-10-07 op mark_nonblock(int fd)
670 592fd624 2020-10-07 op if ((flags = fcntl(fd, F_GETFL)) == -1)
671 592fd624 2020-10-07 op err(1, "fcntl(F_GETFL)");
672 592fd624 2020-10-07 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
673 592fd624 2020-10-07 op err(1, "fcntl(F_SETFL)");
677 9468027b 2020-10-15 op make_socket(int port, int family)
680 9468027b 2020-10-15 op struct sockaddr_in addr4;
681 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
682 9468027b 2020-10-15 op struct sockaddr *addr;
683 9468027b 2020-10-15 op socklen_t len;
685 9468027b 2020-10-15 op switch (family) {
686 9468027b 2020-10-15 op case AF_INET:
687 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
688 9468027b 2020-10-15 op addr4.sin_family = family;
689 9468027b 2020-10-15 op addr4.sin_port = htons(port);
690 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
691 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
692 9468027b 2020-10-15 op len = sizeof(addr4);
695 9468027b 2020-10-15 op case AF_INET6:
696 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
697 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
698 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
699 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
700 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
701 9468027b 2020-10-15 op len = sizeof(addr6);
705 9468027b 2020-10-15 op /* unreachable */
709 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
710 f28f9311 2020-10-14 op err(1, "socket");
713 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
714 3e4749f7 2020-10-02 op err(1, "setsockopt(SO_REUSEADDR)");
717 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
718 3e4749f7 2020-10-02 op err(1, "setsockopt(SO_REUSEPORT)");
720 592fd624 2020-10-07 op mark_nonblock(sock);
722 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
723 f28f9311 2020-10-14 op err(1, "bind");
725 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
726 f28f9311 2020-10-14 op err(1, "listen");
732 592fd624 2020-10-07 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
735 592fd624 2020-10-07 op struct sockaddr_in addr;
736 3e4749f7 2020-10-02 op socklen_t len;
738 592fd624 2020-10-07 op len = sizeof(addr);
739 592fd624 2020-10-07 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
740 592fd624 2020-10-07 op if (errno == EWOULDBLOCK)
742 592fd624 2020-10-07 op err(1, "accept");
745 592fd624 2020-10-07 op mark_nonblock(fd);
747 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
748 592fd624 2020-10-07 op if (fds[i].fd == -1) {
749 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
750 592fd624 2020-10-07 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
751 592fd624 2020-10-07 op break; /* goodbye fd! */
753 592fd624 2020-10-07 op fds[i].fd = fd;
754 592fd624 2020-10-07 op fds[i].events = POLLIN;
756 592fd624 2020-10-07 op clients[i].state = S_OPEN;
757 592fd624 2020-10-07 op clients[i].fd = -1;
758 72342dc9 2020-11-06 op clients[i].child = -1;
759 f28f9311 2020-10-14 op clients[i].buf = MAP_FAILED;
760 2c3a40fa 2020-11-06 op clients[i].af = AF_INET;
761 2c3a40fa 2020-11-06 op clients[i].addr = addr.sin_addr;
771 592fd624 2020-10-07 op goodbye(struct pollfd *pfd, struct client *c)
775 592fd624 2020-10-07 op c->state = S_CLOSING;
777 592fd624 2020-10-07 op ret = tls_close(c->ctx);
778 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
779 592fd624 2020-10-07 op pfd->events = POLLIN;
782 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
783 592fd624 2020-10-07 op pfd->events = POLLOUT;
787 592fd624 2020-10-07 op tls_free(c->ctx);
788 592fd624 2020-10-07 op c->ctx = NULL;
790 f28f9311 2020-10-14 op if (c->buf != MAP_FAILED)
791 f28f9311 2020-10-14 op munmap(c->buf, c->len);
793 592fd624 2020-10-07 op if (c->fd != -1)
794 592fd624 2020-10-07 op close(c->fd);
796 592fd624 2020-10-07 op close(pfd->fd);
797 592fd624 2020-10-07 op pfd->fd = -1;
801 592fd624 2020-10-07 op loop(struct tls *ctx, int sock)
804 592fd624 2020-10-07 op struct client clients[MAX_USERS];
805 592fd624 2020-10-07 op struct pollfd fds[MAX_USERS];
807 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
808 592fd624 2020-10-07 op fds[i].fd = -1;
809 592fd624 2020-10-07 op fds[i].events = POLLIN;
810 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
813 592fd624 2020-10-07 op fds[0].fd = sock;
816 f28f9311 2020-10-14 op if ((todo = poll(fds, MAX_USERS, INFTIM)) == -1)
817 592fd624 2020-10-07 op err(1, "poll");
819 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; i++) {
820 592fd624 2020-10-07 op assert(i < MAX_USERS);
822 592fd624 2020-10-07 op if (fds[i].revents == 0)
825 592fd624 2020-10-07 op if (fds[i].revents & (POLLERR|POLLNVAL))
826 592fd624 2020-10-07 op err(1, "bad fd %d", fds[i].fd);
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 3e4749f7 2020-10-02 op usage(const char *me)
852 3e4749f7 2020-10-02 op fprintf(stderr,
853 3e4749f7 2020-10-02 op "USAGE: %s [-h] [-c cert.pem] [-d docs] [-k key.pem]\n",
858 3e4749f7 2020-10-02 op main(int argc, char **argv)
860 72342dc9 2020-11-06 op const char *cert = "cert.pem", *key = "key.pem";
861 3e4749f7 2020-10-02 op struct tls *ctx = NULL;
862 3e4749f7 2020-10-02 op struct tls_config *conf;
863 3e4749f7 2020-10-02 op int sock, ch;
865 0cf902af 2020-11-03 op signal(SIGPIPE, SIG_IGN);
866 72342dc9 2020-11-06 op signal(SIGCHLD, SIG_IGN);
868 72342dc9 2020-11-06 op dir = "docs/";
869 2c3a40fa 2020-11-06 op logfd = 2; /* stderr */
872 72342dc9 2020-11-06 op while ((ch = getopt(argc, argv, "c:d:hk:l:x")) != -1) {
873 3e4749f7 2020-10-02 op switch (ch) {
875 3e4749f7 2020-10-02 op cert = optarg;
879 3e4749f7 2020-10-02 op dir = optarg;
883 3e4749f7 2020-10-02 op usage(*argv);
887 3e4749f7 2020-10-02 op key = optarg;
891 2c3a40fa 2020-11-06 op /* open log file or create it with 644 */
892 e8cac16e 2020-11-06 op if ((logfd = open(optarg, O_WRONLY | O_CREAT | O_CLOEXEC,
893 2c3a40fa 2020-11-06 op S_IRUSR | S_IWUSR | S_IRGRP | S_IWOTH)) == -1)
894 2c3a40fa 2020-11-06 op err(1, "%s", optarg);
902 3e4749f7 2020-10-02 op usage(*argv);
907 3e4749f7 2020-10-02 op if ((conf = tls_config_new()) == NULL)
908 3e4749f7 2020-10-02 op err(1, "tls_config_new");
910 0d8ca45a 2020-10-03 op if (tls_config_set_protocols(conf,
911 0d8ca45a 2020-10-03 op TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
912 0d8ca45a 2020-10-03 op err(1, "tls_config_set_protocols");
914 4d4f0e19 2020-10-03 op if (tls_config_set_cert_file(conf, cert) == -1)
915 4d4f0e19 2020-10-03 op err(1, "tls_config_set_cert_file: %s", cert);
917 4d4f0e19 2020-10-03 op if (tls_config_set_key_file(conf, key) == -1)
918 4d4f0e19 2020-10-03 op err(1, "tls_config_set_key_file: %s", key);
920 3e4749f7 2020-10-02 op if ((ctx = tls_server()) == NULL)
921 3e4749f7 2020-10-02 op err(1, "tls_server");
923 3e4749f7 2020-10-02 op if (tls_configure(ctx, conf) == -1)
924 3e4749f7 2020-10-02 op errx(1, "tls_configure: %s", tls_error(ctx));
926 9468027b 2020-10-15 op sock = make_socket(1965, AF_INET);
928 3e4749f7 2020-10-02 op if ((dirfd = open(dir, O_RDONLY | O_DIRECTORY)) == -1)
929 3e4749f7 2020-10-02 op err(1, "open: %s", dir);
931 72342dc9 2020-11-06 op if (unveil(dir, cgi ? "rx" : "r") == -1)
932 3e4749f7 2020-10-02 op err(1, "unveil");
934 72342dc9 2020-11-06 op if (pledge(cgi ? "stdio rpath inet proc exec" : "stdio rpath inet", NULL) == -1)
935 3e4749f7 2020-10-02 op err(1, "pledge");
937 3e4749f7 2020-10-02 op loop(ctx, sock);
940 3e4749f7 2020-10-02 op tls_free(ctx);
941 3e4749f7 2020-10-02 op tls_config_free(conf);