2 d3a08f4d 2021-01-17 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 d3a08f4d 2021-01-17 op * Permission to use, copy, modify, and distribute this software for any
5 d3a08f4d 2021-01-17 op * purpose with or without fee is hereby granted, provided that the above
6 d3a08f4d 2021-01-17 op * copyright notice and this permission notice appear in all copies.
8 d3a08f4d 2021-01-17 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d3a08f4d 2021-01-17 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d3a08f4d 2021-01-17 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d3a08f4d 2021-01-17 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d3a08f4d 2021-01-17 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d3a08f4d 2021-01-17 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d3a08f4d 2021-01-17 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 52418c8d 2021-02-12 op #include "gmid.h"
19 d3a08f4d 2021-01-17 op #include <sys/stat.h>
21 d3a08f4d 2021-01-17 op #include <assert.h>
22 d3a08f4d 2021-01-17 op #include <errno.h>
23 abc007d2 2021-02-08 op #include <event.h>
24 d3a08f4d 2021-01-17 op #include <fcntl.h>
25 b4d409cf 2021-01-21 op #include <fnmatch.h>
26 2fafa2d2 2021-02-01 op #include <limits.h>
27 d3a08f4d 2021-01-17 op #include <string.h>
29 090b8a89 2021-07-06 op int shutting_down;
31 8ad1c570 2021-05-09 op struct client clients[MAX_USERS];
33 bc99d868 2021-03-19 op static struct tls *ctx;
35 bc99d868 2021-03-19 op static struct event e4, e6, imsgev, siginfo, sigusr2;
36 6b191ed5 2021-02-23 op static int has_ipv6, has_siginfo;
38 d3a08f4d 2021-01-17 op int connected_clients;
40 49b73ba1 2021-02-10 op static inline int matches(const char*, const char*);
42 74c0c7e4 2021-04-20 op static inline void yield_read(int, struct client*, statefn);
43 74c0c7e4 2021-04-20 op static inline void yield_write(int, struct client*, statefn);
45 fe406389 2021-02-02 op static int check_path(struct client*, const char*, int*);
46 abc007d2 2021-02-08 op static void open_file(struct client*);
47 abc007d2 2021-02-08 op static void check_for_cgi(struct client*);
48 abc007d2 2021-02-08 op static void handle_handshake(int, short, void*);
49 91b9f2a8 2021-05-15 op static const char *strip_path(const char*, int);
50 57ec3e77 2021-02-08 op static void fmt_sbuf(const char*, struct client*, const char*);
51 abc007d2 2021-02-08 op static int apply_block_return(struct client*);
52 02be96c6 2021-02-09 op static int apply_require_ca(struct client*);
53 abc007d2 2021-02-08 op static void handle_open_conn(int, short, void*);
54 abc007d2 2021-02-08 op static void handle_start_reply(int, short, void*);
55 b8e64ccd 2021-03-31 op static size_t host_nth(struct vhost*);
56 abc007d2 2021-02-08 op static void start_cgi(const char*, const char*, struct client*);
57 abc007d2 2021-02-08 op static void open_dir(struct client*);
58 abc007d2 2021-02-08 op static void redirect_canonical_dir(struct client*);
59 abc007d2 2021-02-08 op static void enter_handle_dirlist(int, short, void*);
60 abc007d2 2021-02-08 op static void handle_dirlist(int, short, void*);
61 fe406389 2021-02-02 op static int read_next_dir_entry(struct client*);
62 abc007d2 2021-02-08 op static void send_directory_listing(int, short, void*);
63 abc007d2 2021-02-08 op static void handle_cgi_reply(int, short, void*);
64 27b2fa9a 2021-02-12 op static void handle_copy(int, short, void*);
65 abc007d2 2021-02-08 op static void do_accept(int, short, void*);
66 bc99d868 2021-03-19 op static void handle_imsg_cgi_res(struct imsgbuf*, struct imsg*, size_t);
67 8ad1c570 2021-05-09 op static void handle_imsg_fcgi_fd(struct imsgbuf*, struct imsg*, size_t);
68 bc99d868 2021-03-19 op static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
69 bc99d868 2021-03-19 op static void handle_siginfo(int, short, void*);
71 bc99d868 2021-03-19 op static imsg_handlerfn *handlers[] = {
72 bc99d868 2021-03-19 op [IMSG_QUIT] = handle_imsg_quit,
73 bc99d868 2021-03-19 op [IMSG_CGI_RES] = handle_imsg_cgi_res,
74 8ad1c570 2021-05-09 op [IMSG_FCGI_FD] = handle_imsg_fcgi_fd,
77 49b73ba1 2021-02-10 op static inline int
78 49b73ba1 2021-02-10 op matches(const char *pattern, const char *path)
80 49b73ba1 2021-02-10 op if (*path == '/')
82 49b73ba1 2021-02-10 op return !fnmatch(pattern, path, 0);
85 abc007d2 2021-02-08 op static inline void
86 74c0c7e4 2021-04-20 op yield_read(int fd, struct client *c, statefn fn)
88 abc007d2 2021-02-08 op event_once(fd, EV_READ, fn, c, NULL);
91 c39b26d3 2021-02-12 op static inline void
92 74c0c7e4 2021-04-20 op yield_write(int fd, struct client *c, statefn fn)
94 abc007d2 2021-02-08 op event_once(fd, EV_WRITE, fn, c, NULL);
98 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
100 c8b74339 2021-01-24 op struct location *loc;
102 caad0308 2021-01-27 op if (v == NULL || path == NULL)
105 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
106 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
107 cd761624 2021-02-06 op if (loc->lang != NULL) {
108 49b73ba1 2021-02-10 op if (matches(loc->match, path))
109 6016a593 2021-01-30 op return loc->lang;
113 b8e64ccd 2021-03-31 op return TAILQ_FIRST(&v->locations)->lang;
117 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
119 c8b74339 2021-01-24 op struct location *loc;
120 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
122 caad0308 2021-01-27 op if (v == NULL || path == NULL)
123 caad0308 2021-01-27 op return default_mime;
125 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
126 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
127 cd761624 2021-02-06 op if (loc->default_mime != NULL) {
128 49b73ba1 2021-02-10 op if (matches(loc->match, path))
129 6016a593 2021-01-30 op return loc->default_mime;
133 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
134 b8e64ccd 2021-03-31 op if (loc->default_mime != NULL)
135 b8e64ccd 2021-03-31 op return loc->default_mime;
136 c8b74339 2021-01-24 op return default_mime;
140 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
142 c8b74339 2021-01-24 op struct location *loc;
143 c8b74339 2021-01-24 op const char *index = "index.gmi";
145 caad0308 2021-01-27 op if (v == NULL || path == NULL)
146 caad0308 2021-01-27 op return index;
148 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
149 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
150 cd761624 2021-02-06 op if (loc->index != NULL) {
151 49b73ba1 2021-02-10 op if (matches(loc->match, path))
152 6016a593 2021-01-30 op return loc->index;
156 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
157 b8e64ccd 2021-03-31 op if (loc->index != NULL)
158 b8e64ccd 2021-03-31 op return loc->index;
159 c8b74339 2021-01-24 op return index;
163 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
165 252908e6 2021-01-24 op struct location *loc;
167 6016a593 2021-01-30 op if (v == NULL || path == NULL)
170 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
171 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
172 cd761624 2021-02-06 op if (loc->auto_index != 0) {
173 49b73ba1 2021-02-10 op if (matches(loc->match, path))
174 6016a593 2021-01-30 op return loc->auto_index == 1;
178 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
179 b8e64ccd 2021-03-31 op return loc->auto_index == 1;
183 6abda252 2021-02-06 op vhost_block_return(struct vhost *v, const char *path, int *code, const char **fmt)
185 6abda252 2021-02-06 op struct location *loc;
187 6abda252 2021-02-06 op if (v == NULL || path == NULL)
190 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
191 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
192 cd761624 2021-02-06 op if (loc->block_code != 0) {
193 49b73ba1 2021-02-10 op if (matches(loc->match, path)) {
194 6abda252 2021-02-06 op *code = loc->block_code;
195 6abda252 2021-02-06 op *fmt = loc->block_fmt;
201 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
202 b8e64ccd 2021-03-31 op *code = loc->block_code;
203 b8e64ccd 2021-03-31 op *fmt = loc->block_fmt;
204 b8e64ccd 2021-03-31 op return loc->block_code != 0;
208 8ad1c570 2021-05-09 op vhost_fastcgi(struct vhost *v, const char *path)
210 8ad1c570 2021-05-09 op struct location *loc;
212 8ad1c570 2021-05-09 op if (v == NULL || path == NULL)
215 8ad1c570 2021-05-09 op loc = TAILQ_FIRST(&v->locations);
216 8ad1c570 2021-05-09 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
217 8ad1c570 2021-05-09 op if (loc->fcgi != -1)
218 8ad1c570 2021-05-09 op if (matches(loc->match, path))
219 8ad1c570 2021-05-09 op return loc->fcgi;
222 8ad1c570 2021-05-09 op loc = TAILQ_FIRST(&v->locations);
223 8ad1c570 2021-05-09 op return loc->fcgi;
227 1feaf2a6 2021-05-15 op vhost_dirfd(struct vhost *v, const char *path, size_t *retloc)
229 fdea6aa0 2021-04-30 op struct location *loc;
230 1feaf2a6 2021-05-15 op size_t l = 0;
232 fdea6aa0 2021-04-30 op if (v == NULL || path == NULL)
235 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
236 fdea6aa0 2021-04-30 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
238 fdea6aa0 2021-04-30 op if (loc->dirfd != -1)
239 1feaf2a6 2021-05-15 op if (matches(loc->match, path)) {
241 fdea6aa0 2021-04-30 op return loc->dirfd;
246 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
247 fdea6aa0 2021-04-30 op return loc->dirfd;
251 6abda252 2021-02-06 op vhost_strip(struct vhost *v, const char *path)
253 6abda252 2021-02-06 op struct location *loc;
255 6abda252 2021-02-06 op if (v == NULL || path == NULL)
258 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
259 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
260 cd761624 2021-02-06 op if (loc->strip != 0) {
261 49b73ba1 2021-02-10 op if (matches(loc->match, path))
262 6abda252 2021-02-06 op return loc->strip;
266 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
267 b8e64ccd 2021-03-31 op return loc->strip;
271 02be96c6 2021-02-09 op vhost_require_ca(struct vhost *v, const char *path)
273 02be96c6 2021-02-09 op struct location *loc;
275 02be96c6 2021-02-09 op if (v == NULL || path == NULL)
278 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
279 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
280 02be96c6 2021-02-09 op if (loc->reqca != NULL) {
281 49b73ba1 2021-02-10 op if (matches(loc->match, path))
282 02be96c6 2021-02-09 op return loc->reqca;
286 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
287 b8e64ccd 2021-03-31 op return loc->reqca;
291 793835cb 2021-02-23 op vhost_disable_log(struct vhost *v, const char *path)
293 793835cb 2021-02-23 op struct location *loc;
295 793835cb 2021-02-23 op if (v == NULL || path == NULL)
298 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
299 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
300 793835cb 2021-02-23 op if (loc->disable_log && matches(loc->match, path))
304 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
305 b8e64ccd 2021-03-31 op return loc->disable_log;
309 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
311 d3a08f4d 2021-01-17 op struct stat sb;
312 d1ca3911 2021-01-21 op const char *p;
313 efb48052 2021-07-27 op int dirfd, strip;
315 d3a08f4d 2021-01-17 op assert(path != NULL);
318 fdea6aa0 2021-04-30 op * in send_dir we add an initial / (to be redirect-friendly),
319 fdea6aa0 2021-04-30 op * but here we want to skip it
321 fdea6aa0 2021-04-30 op if (*path == '/')
324 fdea6aa0 2021-04-30 op strip = vhost_strip(c->host, path);
325 fdea6aa0 2021-04-30 op p = strip_path(path, strip);
327 fdea6aa0 2021-04-30 op if (*p == '/')
329 fdea6aa0 2021-04-30 op if (*p == '\0')
332 1feaf2a6 2021-05-15 op dirfd = vhost_dirfd(c->host, path, &c->loc);
333 fdea6aa0 2021-04-30 op log_debug(c, "check_path: strip=%d path=%s original=%s",
334 fdea6aa0 2021-04-30 op strip, p, path);
335 efb48052 2021-07-27 op if (*fd == -1 && (*fd = openat(dirfd, p, O_RDONLY)) == -1)
336 d3a08f4d 2021-01-17 op return FILE_MISSING;
338 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
339 3abf91b0 2021-02-07 op log_notice(c, "failed stat for %s: %s", path, strerror(errno));
340 d3a08f4d 2021-01-17 op return FILE_MISSING;
343 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
344 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
346 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
347 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
349 d3a08f4d 2021-01-17 op return FILE_EXISTS;
353 abc007d2 2021-02-08 op open_file(struct client *c)
355 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
356 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
357 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
358 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
362 d3a08f4d 2021-01-17 op /* fallthrough */
364 d3a08f4d 2021-01-17 op case FILE_EXISTS:
365 27b2fa9a 2021-02-12 op c->next = handle_copy;
366 27b2fa9a 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
369 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
373 d3a08f4d 2021-01-17 op case FILE_MISSING:
374 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
375 abc007d2 2021-02-08 op check_for_cgi(c);
378 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
382 d3a08f4d 2021-01-17 op /* unreachable */
388 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
389 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
390 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
391 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
392 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
395 abc007d2 2021-02-08 op check_for_cgi(struct client *c)
397 2fafa2d2 2021-02-01 op char path[PATH_MAX];
400 2fafa2d2 2021-02-01 op strlcpy(path, c->iri.path, sizeof(path));
401 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
403 d3a08f4d 2021-01-17 op while (end > path) {
405 571d20fb 2021-05-15 op * go up one level. UNIX paths are simple and POSIX
406 571d20fb 2021-05-15 op * dirname, with its ambiguities on if the given
407 571d20fb 2021-05-15 op * pointer is changed or not, gives me headaches.
409 d3a08f4d 2021-01-17 op while (*end != '/')
413 abc007d2 2021-02-08 op switch (check_path(c, path, &c->pfd)) {
414 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
415 abc007d2 2021-02-08 op start_cgi(path, end+1, c);
417 d3a08f4d 2021-01-17 op case FILE_MISSING:
428 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
433 9b8f5ed2 2021-02-03 op mark_nonblock(int fd)
437 9b8f5ed2 2021-02-03 op if ((flags = fcntl(fd, F_GETFL)) == -1)
438 9b8f5ed2 2021-02-03 op fatal("fcntl(F_GETFL): %s", strerror(errno));
439 9b8f5ed2 2021-02-03 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
440 9b8f5ed2 2021-02-03 op fatal("fcntl(F_SETFL): %s", strerror(errno));
444 abc007d2 2021-02-08 op handle_handshake(int fd, short ev, void *d)
446 abc007d2 2021-02-08 op struct client *c = d;
447 d3a08f4d 2021-01-17 op struct vhost *h;
448 cc8c2901 2021-04-29 op struct alist *a;
449 d3a08f4d 2021-01-17 op const char *servname;
450 a8d4a897 2021-01-29 op const char *parse_err = "unknown error";
452 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
453 d3a08f4d 2021-01-17 op case 0: /* success */
454 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
456 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
457 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_handshake);
459 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
460 74c0c7e4 2021-04-20 op yield_write(fd, c, &handle_handshake);
463 d3a08f4d 2021-01-17 op /* unreachable */
467 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
468 a8d4a897 2021-01-29 op if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
469 3abf91b0 2021-02-07 op log_info(c, "puny_decode: %s", parse_err);
473 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
474 eecad7a3 2021-02-12 op if (matches(h->domain, c->domain))
476 cc8c2901 2021-04-29 op TAILQ_FOREACH(a, &h->aliases, aliases) {
477 cc8c2901 2021-04-29 op if (matches(a->alias, c->domain))
483 3abf91b0 2021-02-07 op log_debug(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
484 3abf91b0 2021-02-07 op servname != NULL ? servname : "(null)",
486 b8e64ccd 2021-03-31 op h != NULL ? h->domain : "(null)");
488 b8e64ccd 2021-03-31 op if (h != NULL) {
490 abc007d2 2021-02-08 op handle_open_conn(fd, ev, c);
495 0ab65593 2021-01-21 op if (servname != NULL)
496 1b78bd56 2021-06-16 op strlcpy(c->req, servname, sizeof(c->req));
498 1b78bd56 2021-06-16 op strlcpy(c->req, "null", sizeof(c->req));
500 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
503 91b9f2a8 2021-05-15 op static const char *
504 91b9f2a8 2021-05-15 op strip_path(const char *path, int strip)
508 6abda252 2021-02-06 op while (strip > 0) {
509 6abda252 2021-02-06 op if ((t = strchr(path, '/')) == NULL) {
510 6abda252 2021-02-06 op path = strchr(path, '\0');
521 57ec3e77 2021-02-08 op fmt_sbuf(const char *fmt, struct client *c, const char *path)
524 57ec3e77 2021-02-08 op char buf[32];
526 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
527 6abda252 2021-02-06 op for (i = 0; *fmt; ++fmt) {
528 6abda252 2021-02-06 op if (i == sizeof(buf)-1 || *fmt == '%') {
529 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
530 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
534 6abda252 2021-02-06 op if (*fmt != '%') {
535 6abda252 2021-02-06 op buf[i++] = *fmt;
539 6abda252 2021-02-06 op switch (*++fmt) {
541 6abda252 2021-02-06 op strlcat(c->sbuf, "%", sizeof(c->sbuf));
544 737a6b50 2021-04-30 op if (*path != '/')
545 737a6b50 2021-04-30 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
546 6abda252 2021-02-06 op strlcat(c->sbuf, path, sizeof(c->sbuf));
549 6abda252 2021-02-06 op strlcat(c->sbuf, c->iri.query, sizeof(c->sbuf));
552 6abda252 2021-02-06 op snprintf(buf, sizeof(buf), "%d", conf.port);
553 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
554 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
557 6abda252 2021-02-06 op strlcat(c->sbuf, c->domain, sizeof(c->sbuf));
560 6abda252 2021-02-06 op fatal("%s: unknown fmt specifier %c",
561 6abda252 2021-02-06 op __func__, *fmt);
566 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
569 57ec3e77 2021-02-08 op /* 1 if a matching `block return' (and apply it), 0 otherwise */
571 57ec3e77 2021-02-08 op apply_block_return(struct client *c)
573 57ec3e77 2021-02-08 op const char *fmt, *path;
576 57ec3e77 2021-02-08 op if (!vhost_block_return(c->host, c->iri.path, &code, &fmt))
579 57ec3e77 2021-02-08 op path = strip_path(c->iri.path, vhost_strip(c->host, c->iri.path));
580 57ec3e77 2021-02-08 op fmt_sbuf(fmt, c, path);
582 abc007d2 2021-02-08 op start_reply(c, code, c->sbuf);
586 8ad1c570 2021-05-09 op /* 1 if matching `fcgi' (and apply it), 0 otherwise */
588 8ad1c570 2021-05-09 op apply_fastcgi(struct client *c)
591 8ad1c570 2021-05-09 op struct fcgi *f;
593 8ad1c570 2021-05-09 op if ((id = vhost_fastcgi(c->host, c->iri.path)) == -1)
596 8ad1c570 2021-05-09 op switch ((f = &fcgi[id])->s) {
597 8ad1c570 2021-05-09 op case FCGI_OFF:
598 8ad1c570 2021-05-09 op f->s = FCGI_INFLIGHT;
599 8ad1c570 2021-05-09 op log_info(c, "opening fastcgi connection for (%s,%s,%s)",
600 8ad1c570 2021-05-09 op f->path, f->port, f->prog);
601 8ad1c570 2021-05-09 op imsg_compose(&exibuf, IMSG_FCGI_REQ, 0, 0, -1,
602 8ad1c570 2021-05-09 op &id, sizeof(id));
603 8ad1c570 2021-05-09 op imsg_flush(&exibuf);
604 8ad1c570 2021-05-09 op /* fallthrough */
605 8ad1c570 2021-05-09 op case FCGI_INFLIGHT:
606 8ad1c570 2021-05-09 op c->fcgi = id;
608 8ad1c570 2021-05-09 op case FCGI_READY:
609 8ad1c570 2021-05-09 op c->fcgi = id;
610 8ad1c570 2021-05-09 op send_fcgi_req(f, c);
617 02be96c6 2021-02-09 op /* 1 if matching `require client ca' fails (and apply it), 0 otherwise */
619 02be96c6 2021-02-09 op apply_require_ca(struct client *c)
621 02be96c6 2021-02-09 op X509_STORE *store;
622 02be96c6 2021-02-09 op const uint8_t *cert;
625 02be96c6 2021-02-09 op if ((store = vhost_require_ca(c->host, c->iri.path)) == NULL)
628 02be96c6 2021-02-09 op if (!tls_peer_cert_provided(c->ctx)) {
629 02be96c6 2021-02-09 op start_reply(c, CLIENT_CERT_REQ, "client certificate required");
633 02be96c6 2021-02-09 op cert = tls_peer_cert_chain_pem(c->ctx, &len);
634 02be96c6 2021-02-09 op if (!validate_against_ca(store, cert, len)) {
635 02be96c6 2021-02-09 op start_reply(c, CERT_NOT_AUTH, "certificate not authorised");
643 abc007d2 2021-02-08 op handle_open_conn(int fd, short ev, void *d)
645 abc007d2 2021-02-08 op struct client *c = d;
646 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
647 3300cbe0 2021-01-27 op char decoded[DOMAIN_NAME_LEN];
649 0be51733 2021-01-20 op bzero(c->req, sizeof(c->req));
650 0be51733 2021-01-20 op bzero(&c->iri, sizeof(c->iri));
652 0be51733 2021-01-20 op switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
654 3abf91b0 2021-02-07 op log_err(c, "tls_read: %s", tls_error(c->ctx));
655 abc007d2 2021-02-08 op close_conn(fd, ev, c);
658 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
659 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_open_conn);
662 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
663 74c0c7e4 2021-04-20 op yield_write(fd, c, &handle_open_conn);
667 c4f682f8 2021-01-27 op if (!trim_req_iri(c->req, &parse_err)
668 a2fd8013 2021-01-29 op || !parse_iri(c->req, &c->iri, &parse_err)
669 a2fd8013 2021-01-29 op || !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
670 3abf91b0 2021-02-07 op log_info(c, "iri parse error: %s", parse_err);
671 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "invalid request");
675 3300cbe0 2021-01-27 op if (c->iri.port_no != conf.port
676 3300cbe0 2021-01-27 op || strcmp(c->iri.schema, "gemini")
677 3300cbe0 2021-01-27 op || strcmp(decoded, c->domain)) {
678 abc007d2 2021-02-08 op start_reply(c, PROXY_REFUSED, "won't proxy request");
682 02be96c6 2021-02-09 op if (apply_require_ca(c))
685 abc007d2 2021-02-08 op if (apply_block_return(c))
688 8ad1c570 2021-05-09 op if (apply_fastcgi(c))
691 e3ddf390 2021-02-06 op if (c->host->entrypoint != NULL) {
693 abc007d2 2021-02-08 op start_cgi(c->host->entrypoint, c->iri.path, c);
697 abc007d2 2021-02-08 op open_file(c);
701 abc007d2 2021-02-08 op start_reply(struct client *c, int code, const char *meta)
703 112802ea 2021-02-01 op c->code = code;
704 112802ea 2021-02-01 op c->meta = meta;
705 abc007d2 2021-02-08 op handle_start_reply(c->fd, 0, c);
709 abc007d2 2021-02-08 op handle_start_reply(int fd, short ev, void *d)
711 abc007d2 2021-02-08 op struct client *c = d;
712 05c23a54 2021-01-19 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
713 c8b74339 2021-01-24 op const char *lang;
716 c8b74339 2021-01-24 op lang = vhost_lang(c->host, c->iri.path);
718 112802ea 2021-02-01 op snprintf(buf, sizeof(buf), "%d ", c->code);
719 112802ea 2021-02-01 op strlcat(buf, c->meta, sizeof(buf));
720 112802ea 2021-02-01 op if (!strcmp(c->meta, "text/gemini") && lang != NULL) {
721 05c23a54 2021-01-19 op strlcat(buf, "; lang=", sizeof(buf));
722 c8b74339 2021-01-24 op strlcat(buf, lang, sizeof(buf));
725 05c23a54 2021-01-19 op len = strlcat(buf, "\r\n", sizeof(buf));
726 0be51733 2021-01-20 op assert(len < sizeof(buf));
728 df79b4c1 2021-01-19 op switch (tls_write(c->ctx, buf, len)) {
730 abc007d2 2021-02-08 op close_conn(fd, ev, c);
732 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
733 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_start_reply);
735 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
736 74c0c7e4 2021-04-20 op yield_write(fd, c, &handle_start_reply);
740 4604dc96 2021-02-23 op if (!vhost_disable_log(c->host, c->iri.path))
741 4604dc96 2021-02-23 op log_request(c, buf, sizeof(buf));
743 abc007d2 2021-02-08 op if (c->code != SUCCESS)
744 abc007d2 2021-02-08 op close_conn(fd, ev, c);
746 abc007d2 2021-02-08 op c->next(fd, ev, c);
749 b8e64ccd 2021-03-31 op static size_t
750 b8e64ccd 2021-03-31 op host_nth(struct vhost *h)
752 b8e64ccd 2021-03-31 op struct vhost *v;
753 b8e64ccd 2021-03-31 op size_t i = 0;
755 b8e64ccd 2021-03-31 op TAILQ_FOREACH(v, &hosts, vhosts) {
765 abc007d2 2021-02-08 op start_cgi(const char *spath, const char *relpath, struct client *c)
767 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
768 bc99d868 2021-03-19 op const char *t;
769 bc99d868 2021-03-19 op struct cgireq req;
772 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
773 d3a08f4d 2021-01-17 op addr, sizeof(addr),
775 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
777 bc99d868 2021-03-19 op fatal("getnameinfo failed");
779 bc99d868 2021-03-19 op memset(&req, 0, sizeof(req));
781 bc99d868 2021-03-19 op memcpy(req.buf, c->req, sizeof(req.buf));
783 bc99d868 2021-03-19 op req.iri_schema_off = c->iri.schema - c->req;
784 bc99d868 2021-03-19 op req.iri_host_off = c->iri.host - c->req;
785 bc99d868 2021-03-19 op req.iri_port_off = c->iri.port - c->req;
786 bc99d868 2021-03-19 op req.iri_path_off = c->iri.path - c->req;
787 bc99d868 2021-03-19 op req.iri_query_off = c->iri.query - c->req;
788 bc99d868 2021-03-19 op req.iri_fragment_off = c->iri.fragment - c->req;
790 bc99d868 2021-03-19 op req.iri_portno = c->iri.port_no;
792 bc99d868 2021-03-19 op strlcpy(req.spath, spath, sizeof(req.spath));
793 bc99d868 2021-03-19 op strlcpy(req.relpath, relpath, sizeof(req.relpath));
794 bc99d868 2021-03-19 op strlcpy(req.addr, addr, sizeof(req.addr));
796 bc99d868 2021-03-19 op if ((t = tls_peer_cert_subject(c->ctx)) != NULL)
797 bc99d868 2021-03-19 op strlcpy(req.subject, t, sizeof(req.subject));
798 bc99d868 2021-03-19 op if ((t = tls_peer_cert_issuer(c->ctx)) != NULL)
799 bc99d868 2021-03-19 op strlcpy(req.issuer, t, sizeof(req.issuer));
800 bc99d868 2021-03-19 op if ((t = tls_peer_cert_hash(c->ctx)) != NULL)
801 bc99d868 2021-03-19 op strlcpy(req.hash, t, sizeof(req.hash));
802 89541eee 2021-04-13 op if ((t = tls_conn_version(c->ctx)) != NULL)
803 89541eee 2021-04-13 op strlcpy(req.version, t, sizeof(req.version));
804 89541eee 2021-04-13 op if ((t = tls_conn_cipher(c->ctx)) != NULL)
805 89541eee 2021-04-13 op strlcpy(req.cipher, t, sizeof(req.cipher));
807 89541eee 2021-04-13 op req.cipher_strength = tls_conn_cipher_strength(c->ctx);
808 bc99d868 2021-03-19 op req.notbefore = tls_peer_cert_notbefore(c->ctx);
809 bc99d868 2021-03-19 op req.notafter = tls_peer_cert_notafter(c->ctx);
811 b8e64ccd 2021-03-31 op req.host_off = host_nth(c->host);
812 1feaf2a6 2021-05-15 op req.loc_off = c->loc;
814 bc99d868 2021-03-19 op imsg_compose(&exibuf, IMSG_CGI_REQ, c->id, 0, -1, &req, sizeof(req));
815 bc99d868 2021-03-19 op imsg_flush(&exibuf);
817 bc99d868 2021-03-19 op close(c->pfd);
821 abc007d2 2021-02-08 op open_dir(struct client *c)
824 11c98667 2021-04-25 op int dirfd, root;
825 252908e6 2021-01-24 op char *before_file;
827 11c98667 2021-04-25 op root = !strcmp(c->iri.path, "/") || *c->iri.path == '\0';
829 0be51733 2021-01-20 op len = strlen(c->iri.path);
830 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
831 abc007d2 2021-02-08 op redirect_canonical_dir(c);
835 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
836 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
837 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
838 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
839 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
840 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
841 c8b74339 2021-01-24 op sizeof(c->sbuf));
842 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
843 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
847 252908e6 2021-01-24 op c->iri.path = c->sbuf;
849 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
850 abc007d2 2021-02-08 op dirfd = c->pfd;
853 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
854 252908e6 2021-01-24 op case FILE_EXECUTABLE:
855 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
856 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
860 252908e6 2021-01-24 op /* fallthrough */
862 252908e6 2021-01-24 op case FILE_EXISTS:
863 98ee8406 2021-02-12 op c->next = handle_copy;
864 98ee8406 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
867 252908e6 2021-01-24 op case FILE_DIRECTORY:
868 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
871 252908e6 2021-01-24 op case FILE_MISSING:
872 252908e6 2021-01-24 op *before_file = '\0';
874 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
875 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
879 5f715ce4 2021-02-02 op c->next = enter_handle_dirlist;
881 e76f2c74 2021-04-25 op c->dirlen = scandir_fd(dirfd, &c->dir,
882 11c98667 2021-04-25 op root ? select_non_dotdot : select_non_dot,
884 11c98667 2021-04-25 op if (c->dirlen == -1) {
885 11c98667 2021-04-25 op log_err(c, "scandir_fd(%d) (vhost:%s) %s: %s",
886 abc007d2 2021-02-08 op c->pfd, c->host->domain, c->iri.path, strerror(errno));
887 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
890 11c98667 2021-04-25 op c->diroff = 0;
893 abc007d2 2021-02-08 op start_reply(c, SUCCESS, "text/gemini");
897 252908e6 2021-01-24 op /* unreachable */
901 252908e6 2021-01-24 op close(dirfd);
905 abc007d2 2021-02-08 op redirect_canonical_dir(struct client *c)
909 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
910 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
911 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
913 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
914 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
918 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
922 abc007d2 2021-02-08 op enter_handle_dirlist(int fd, short ev, void *d)
924 abc007d2 2021-02-08 op struct client *c = d;
925 5f715ce4 2021-02-02 op char b[PATH_MAX];
928 5f715ce4 2021-02-02 op strlcpy(b, c->iri.path, sizeof(b));
929 5f715ce4 2021-02-02 op l = snprintf(c->sbuf, sizeof(c->sbuf),
930 5f715ce4 2021-02-02 op "# Index of %s\n\n", b);
931 5f715ce4 2021-02-02 op if (l >= sizeof(c->sbuf)) {
933 a8a1f439 2021-07-07 op * This is impossible, given that we have enough space
934 5f715ce4 2021-02-02 op * in c->sbuf to hold the ancilliary string plus the
935 5f715ce4 2021-02-02 op * full path; but it wouldn't read nice without some
936 a8a1f439 2021-07-07 op * error checking, and I'd like to avoid a strlen.
938 abc007d2 2021-02-08 op close_conn(fd, ev, c);
943 abc007d2 2021-02-08 op handle_dirlist(fd, ev, c);
947 abc007d2 2021-02-08 op handle_dirlist(int fd, short ev, void *d)
949 abc007d2 2021-02-08 op struct client *c = d;
952 5f715ce4 2021-02-02 op while (c->len > 0) {
953 5f715ce4 2021-02-02 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
955 abc007d2 2021-02-08 op close_conn(fd, ev, c);
957 5f715ce4 2021-02-02 op case TLS_WANT_POLLOUT:
958 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_dirlist);
960 5f715ce4 2021-02-02 op case TLS_WANT_POLLIN:
961 74c0c7e4 2021-04-20 op yield_write(fd, c, &handle_dirlist);
969 abc007d2 2021-02-08 op send_directory_listing(fd, ev, c);
973 252908e6 2021-01-24 op read_next_dir_entry(struct client *c)
975 11c98667 2021-04-25 op if (c->diroff == c->dirlen)
978 252908e6 2021-01-24 op /* XXX: url escape */
979 11c98667 2021-04-25 op snprintf(c->sbuf, sizeof(c->sbuf), "=> %s\n",
980 11c98667 2021-04-25 op c->dir[c->diroff]->d_name);
982 11c98667 2021-04-25 op free(c->dir[c->diroff]);
985 252908e6 2021-01-24 op c->len = strlen(c->sbuf);
991 abc007d2 2021-02-08 op send_directory_listing(int fd, short ev, void *d)
993 abc007d2 2021-02-08 op struct client *c = d;
997 252908e6 2021-01-24 op if (c->len == 0) {
998 252908e6 2021-01-24 op if (!read_next_dir_entry(c))
1002 252908e6 2021-01-24 op while (c->len > 0) {
1003 252908e6 2021-01-24 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
1007 252908e6 2021-01-24 op case TLS_WANT_POLLOUT:
1008 74c0c7e4 2021-04-20 op yield_read(fd, c, &send_directory_listing);
1011 252908e6 2021-01-24 op case TLS_WANT_POLLIN:
1012 74c0c7e4 2021-04-20 op yield_write(fd, c, &send_directory_listing);
1016 252908e6 2021-01-24 op c->off += r;
1017 252908e6 2021-01-24 op c->len -= r;
1024 abc007d2 2021-02-08 op close_conn(fd, ev, d);
1027 35744950 2021-02-01 op /* accumulate the meta line from the cgi script. */
1029 abc007d2 2021-02-08 op handle_cgi_reply(int fd, short ev, void *d)
1031 abc007d2 2021-02-08 op struct client *c = d;
1032 35744950 2021-02-01 op void *buf, *e;
1037 35744950 2021-02-01 op buf = c->sbuf + c->len;
1038 35744950 2021-02-01 op len = sizeof(c->sbuf) - c->len;
1040 abc007d2 2021-02-08 op r = read(c->pfd, buf, len);
1041 35744950 2021-02-01 op if (r == 0 || r == -1) {
1042 abc007d2 2021-02-08 op start_reply(c, CGI_ERROR, "CGI error");
1046 3309ef97 2021-01-23 op c->len += r;
1048 35744950 2021-02-01 op /* TODO: error if the CGI script don't reply correctly */
1049 35744950 2021-02-01 op e = strchr(c->sbuf, '\n');
1050 35744950 2021-02-01 op if (e != NULL || c->len == sizeof(c->sbuf)) {
1051 3309ef97 2021-01-23 op log_request(c, c->sbuf, c->len);
1054 27b2fa9a 2021-02-12 op handle_copy(fd, ev, c);
1058 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_cgi_reply);
1062 27b2fa9a 2021-02-12 op handle_copy(int fd, short ev, void *d)
1064 abc007d2 2021-02-08 op struct client *c = d;
1068 d3a08f4d 2021-01-17 op while (c->len > 0) {
1069 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
1073 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
1074 74c0c7e4 2021-04-20 op yield_write(c->fd, c, &handle_copy);
1077 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
1078 74c0c7e4 2021-04-20 op yield_read(c->fd, c, &handle_copy);
1082 d3a08f4d 2021-01-17 op c->off += r;
1083 d3a08f4d 2021-01-17 op c->len -= r;
1088 27b2fa9a 2021-02-12 op switch (r = read(c->pfd, c->sbuf, sizeof(c->sbuf))) {
1092 27b2fa9a 2021-02-12 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
1093 74c0c7e4 2021-04-20 op yield_read(c->pfd, c, &handle_copy);
1104 abc007d2 2021-02-08 op close_conn(c->fd, ev, d);
1108 abc007d2 2021-02-08 op close_conn(int fd, short ev, void *d)
1110 8ad1c570 2021-05-09 op struct client *c = d;
1111 8ad1c570 2021-05-09 op struct mbuf *mbuf;
1113 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
1114 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
1115 24d362cd 2021-06-12 op yield_read(c->fd, c, &close_conn);
1117 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
1118 24d362cd 2021-06-12 op yield_read(c->fd, c, &close_conn);
1122 d3a08f4d 2021-01-17 op connected_clients--;
1124 8ad1c570 2021-05-09 op while ((mbuf = TAILQ_FIRST(&c->mbufhead)) != NULL) {
1125 8ad1c570 2021-05-09 op TAILQ_REMOVE(&c->mbufhead, mbuf, mbufs);
1129 d3a08f4d 2021-01-17 op tls_free(c->ctx);
1130 d3a08f4d 2021-01-17 op c->ctx = NULL;
1132 abc007d2 2021-02-08 op if (c->pfd != -1)
1133 abc007d2 2021-02-08 op close(c->pfd);
1135 252908e6 2021-01-24 op if (c->dir != NULL)
1136 11c98667 2021-04-25 op free(c->dir);
1138 abc007d2 2021-02-08 op close(c->fd);
1143 abc007d2 2021-02-08 op do_accept(int sock, short et, void *d)
1145 abc007d2 2021-02-08 op struct client *c;
1146 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
1147 abc007d2 2021-02-08 op struct sockaddr *saddr;
1148 d3a08f4d 2021-01-17 op socklen_t len;
1153 abc007d2 2021-02-08 op saddr = (struct sockaddr*)&addr;
1154 d3a08f4d 2021-01-17 op len = sizeof(addr);
1155 3cb3dd4d 2021-02-12 op if ((fd = accept(sock, saddr, &len)) == -1) {
1156 cfb8a77f 2021-02-07 op if (errno == EWOULDBLOCK || errno == EAGAIN)
1158 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
1161 3cb3dd4d 2021-02-12 op mark_nonblock(fd);
1163 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
1164 bc99d868 2021-03-19 op c = &clients[i];
1165 abc007d2 2021-02-08 op if (c->fd == -1) {
1166 abc007d2 2021-02-08 op memset(c, 0, sizeof(*c));
1168 bc99d868 2021-03-19 op if (tls_accept_socket(ctx, &c->ctx, fd) == -1)
1169 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
1172 abc007d2 2021-02-08 op c->pfd = -1;
1173 abc007d2 2021-02-08 op c->dir = NULL;
1174 abc007d2 2021-02-08 op c->addr = addr;
1175 8ad1c570 2021-05-09 op c->fcgi = -1;
1177 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_handshake);
1178 d3a08f4d 2021-01-17 op connected_clients++;
1186 8ad1c570 2021-05-09 op static struct client *
1187 bc99d868 2021-03-19 op client_by_id(int id)
1189 bc99d868 2021-03-19 op if ((size_t)id > sizeof(clients)/sizeof(clients[0]))
1190 bc99d868 2021-03-19 op fatal("in client_by_id: invalid id %d", id);
1191 bc99d868 2021-03-19 op return &clients[id];
1194 8ad1c570 2021-05-09 op struct client *
1195 8ad1c570 2021-05-09 op try_client_by_id(int id)
1197 8ad1c570 2021-05-09 op if ((size_t)id > sizeof(clients)/sizeof(clients[0]))
1198 8ad1c570 2021-05-09 op return NULL;
1199 8ad1c570 2021-05-09 op return &clients[id];
1203 bc99d868 2021-03-19 op handle_imsg_cgi_res(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1205 bc99d868 2021-03-19 op struct client *c;
1207 bc99d868 2021-03-19 op c = client_by_id(imsg->hdr.peerid);
1209 bc99d868 2021-03-19 op if ((c->pfd = imsg->fd) == -1)
1210 bc99d868 2021-03-19 op start_reply(c, TEMP_FAILURE, "internal server error");
1212 74c0c7e4 2021-04-20 op yield_read(c->pfd, c, &handle_cgi_reply);
1216 8ad1c570 2021-05-09 op handle_imsg_fcgi_fd(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1218 8ad1c570 2021-05-09 op struct client *c;
1219 8ad1c570 2021-05-09 op struct fcgi *f;
1222 8ad1c570 2021-05-09 op id = imsg->hdr.peerid;
1223 8ad1c570 2021-05-09 op f = &fcgi[id];
1225 8ad1c570 2021-05-09 op if ((f->fd = imsg->fd) != -1) {
1226 89c88caa 2021-06-12 op f->s = FCGI_READY;
1227 8ad1c570 2021-05-09 op event_set(&f->e, imsg->fd, EV_READ | EV_PERSIST, &handle_fcgi,
1229 8ad1c570 2021-05-09 op event_add(&f->e, NULL);
1231 8ad1c570 2021-05-09 op f->s = FCGI_OFF;
1234 8ad1c570 2021-05-09 op for (i = 0; i < MAX_USERS; ++i) {
1235 8ad1c570 2021-05-09 op c = &clients[i];
1236 8ad1c570 2021-05-09 op if (c->fd == -1)
1238 8ad1c570 2021-05-09 op if (c->fcgi != id)
1241 8ad1c570 2021-05-09 op if (f->fd == -1) {
1242 8ad1c570 2021-05-09 op c->fcgi = -1;
1243 8ad1c570 2021-05-09 op start_reply(c, TEMP_FAILURE, "internal server error");
1245 8ad1c570 2021-05-09 op send_fcgi_req(f, c);
1250 bc99d868 2021-03-19 op handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1258 a8a1f439 2021-07-07 op * don't call event_loopbreak since we want to finish to
1259 a8a1f439 2021-07-07 op * handle the ongoing connections.
1262 090b8a89 2021-07-06 op shutting_down = 1;
1264 a6e689d7 2021-02-12 op event_del(&e4);
1265 a6e689d7 2021-02-12 op if (has_ipv6)
1266 a6e689d7 2021-02-12 op event_del(&e6);
1267 a6e689d7 2021-02-12 op if (has_siginfo)
1268 a6e689d7 2021-02-12 op signal_del(&siginfo);
1269 bc99d868 2021-03-19 op event_del(&imsgev);
1270 a6e689d7 2021-02-12 op signal_del(&sigusr2);
1272 090b8a89 2021-07-06 op for (i = 0; i < FCGI_MAX; ++i) {
1273 090b8a89 2021-07-06 op if (fcgi[i].path == NULL && fcgi[i].prog == NULL)
1276 090b8a89 2021-07-06 op if (!event_pending(&fcgi[i].e, EV_READ, NULL) ||
1277 090b8a89 2021-07-06 op fcgi[i].pending != 0)
1280 090b8a89 2021-07-06 op fcgi_close_backend(&fcgi[i]);
1285 bc99d868 2021-03-19 op handle_dispatch_imsg(int fd, short ev, void *d)
1287 bc99d868 2021-03-19 op struct imsgbuf *ibuf = d;
1288 bc99d868 2021-03-19 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
1292 abc007d2 2021-02-08 op handle_siginfo(int fd, short ev, void *d)
1298 abc007d2 2021-02-08 op log_info(NULL, "%d connected clients", connected_clients);
1302 bc99d868 2021-03-19 op loop(struct tls *ctx_, int sock4, int sock6, struct imsgbuf *ibuf)
1308 abc007d2 2021-02-08 op event_init();
1310 bc99d868 2021-03-19 op memset(&clients, 0, sizeof(clients));
1311 abc007d2 2021-02-08 op for (i = 0; i < MAX_USERS; ++i)
1312 bc99d868 2021-03-19 op clients[i].fd = -1;
1314 bc99d868 2021-03-19 op event_set(&e4, sock4, EV_READ | EV_PERSIST, &do_accept, NULL);
1315 a6e689d7 2021-02-12 op event_add(&e4, NULL);
1317 abc007d2 2021-02-08 op if (sock6 != -1) {
1318 a6e689d7 2021-02-12 op has_ipv6 = 1;
1319 bc99d868 2021-03-19 op event_set(&e6, sock6, EV_READ | EV_PERSIST, &do_accept, NULL);
1320 a6e689d7 2021-02-12 op event_add(&e6, NULL);
1323 bc99d868 2021-03-19 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
1324 bc99d868 2021-03-19 op event_add(&imsgev, NULL);
1326 abc007d2 2021-02-08 op #ifdef SIGINFO
1327 a6e689d7 2021-02-12 op has_siginfo = 1;
1328 a6e689d7 2021-02-12 op signal_set(&siginfo, SIGINFO, &handle_siginfo, NULL);
1329 a6e689d7 2021-02-12 op signal_add(&siginfo, NULL);
1331 5e3285d5 2021-02-12 op signal_set(&sigusr2, SIGUSR2, &handle_siginfo, NULL);
1332 a6e689d7 2021-02-12 op signal_add(&sigusr2, NULL);
1334 62e001b0 2021-03-20 op sandbox_server_process();
1335 abc007d2 2021-02-08 op event_dispatch();