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 efe7d180 2021-10-02 op #include <ctype.h>
23 d3a08f4d 2021-01-17 op #include <errno.h>
24 abc007d2 2021-02-08 op #include <event.h>
25 d3a08f4d 2021-01-17 op #include <fcntl.h>
26 b4d409cf 2021-01-21 op #include <fnmatch.h>
27 2fafa2d2 2021-02-01 op #include <limits.h>
28 d3a08f4d 2021-01-17 op #include <string.h>
30 efe7d180 2021-10-02 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
32 090b8a89 2021-07-06 op int shutting_down;
34 bc99d868 2021-03-19 op static struct tls *ctx;
36 bc99d868 2021-03-19 op static struct event e4, e6, imsgev, siginfo, sigusr2;
37 6b191ed5 2021-02-23 op static int has_ipv6, has_siginfo;
39 d3a08f4d 2021-01-17 op int connected_clients;
41 49b73ba1 2021-02-10 op static inline int matches(const char*, const char*);
43 fe406389 2021-02-02 op static int check_path(struct client*, const char*, int*);
44 abc007d2 2021-02-08 op static void open_file(struct client*);
45 abc007d2 2021-02-08 op static void check_for_cgi(struct client*);
46 abc007d2 2021-02-08 op static void handle_handshake(int, short, void*);
47 91b9f2a8 2021-05-15 op static const char *strip_path(const char*, int);
48 57ec3e77 2021-02-08 op static void fmt_sbuf(const char*, struct client*, const char*);
49 abc007d2 2021-02-08 op static int apply_block_return(struct client*);
50 72b033ef 2021-12-29 op static int apply_reverse_proxy(struct client *);
51 efe7d180 2021-10-02 op static int apply_fastcgi(struct client*);
52 02be96c6 2021-02-09 op static int apply_require_ca(struct client*);
53 b8e64ccd 2021-03-31 op static size_t host_nth(struct vhost*);
54 abc007d2 2021-02-08 op static void start_cgi(const char*, const char*, struct client*);
55 abc007d2 2021-02-08 op static void open_dir(struct client*);
56 abc007d2 2021-02-08 op static void redirect_canonical_dir(struct client*);
58 efe7d180 2021-10-02 op static void client_tls_readcb(int, short, void *);
59 efe7d180 2021-10-02 op static void client_tls_writecb(int, short, void *);
61 efe7d180 2021-10-02 op static void client_read(struct bufferevent *, void *);
62 efe7d180 2021-10-02 op void client_write(struct bufferevent *, void *);
63 efe7d180 2021-10-02 op static void client_error(struct bufferevent *, short, void *);
65 efe7d180 2021-10-02 op static void client_close_ev(int, short, void *);
67 efe7d180 2021-10-02 op static void cgi_read(struct bufferevent *, void *);
68 efe7d180 2021-10-02 op static void cgi_write(struct bufferevent *, void *);
69 efe7d180 2021-10-02 op static void cgi_error(struct bufferevent *, short, void *);
71 abc007d2 2021-02-08 op static void do_accept(int, short, void*);
72 efe7d180 2021-10-02 op static struct client *client_by_id(int);
74 bc99d868 2021-03-19 op static void handle_imsg_cgi_res(struct imsgbuf*, struct imsg*, size_t);
75 8ad1c570 2021-05-09 op static void handle_imsg_fcgi_fd(struct imsgbuf*, struct imsg*, size_t);
76 72b033ef 2021-12-29 op static void handle_imsg_conn_fd(struct imsgbuf*, struct imsg*, size_t);
77 bc99d868 2021-03-19 op static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
78 efe7d180 2021-10-02 op static void handle_dispatch_imsg(int, short, void *);
79 bc99d868 2021-03-19 op static void handle_siginfo(int, short, void*);
81 bc99d868 2021-03-19 op static imsg_handlerfn *handlers[] = {
82 bc99d868 2021-03-19 op [IMSG_QUIT] = handle_imsg_quit,
83 bc99d868 2021-03-19 op [IMSG_CGI_RES] = handle_imsg_cgi_res,
84 8ad1c570 2021-05-09 op [IMSG_FCGI_FD] = handle_imsg_fcgi_fd,
85 72b033ef 2021-12-29 op [IMSG_CONN_FD] = handle_imsg_conn_fd,
88 207b3e80 2021-10-07 op static uint32_t server_client_id;
90 207b3e80 2021-10-07 op struct client_tree_id clients;
92 49b73ba1 2021-02-10 op static inline int
93 49b73ba1 2021-02-10 op matches(const char *pattern, const char *path)
95 49b73ba1 2021-02-10 op if (*path == '/')
97 49b73ba1 2021-02-10 op return !fnmatch(pattern, path, 0);
101 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
103 c8b74339 2021-01-24 op struct location *loc;
105 caad0308 2021-01-27 op if (v == NULL || path == NULL)
108 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
109 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
110 cd761624 2021-02-06 op if (loc->lang != NULL) {
111 49b73ba1 2021-02-10 op if (matches(loc->match, path))
112 6016a593 2021-01-30 op return loc->lang;
116 b8e64ccd 2021-03-31 op return TAILQ_FIRST(&v->locations)->lang;
120 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
122 c8b74339 2021-01-24 op struct location *loc;
123 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
125 caad0308 2021-01-27 op if (v == NULL || path == NULL)
126 caad0308 2021-01-27 op return default_mime;
128 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
129 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
130 cd761624 2021-02-06 op if (loc->default_mime != NULL) {
131 49b73ba1 2021-02-10 op if (matches(loc->match, path))
132 6016a593 2021-01-30 op return loc->default_mime;
136 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
137 b8e64ccd 2021-03-31 op if (loc->default_mime != NULL)
138 b8e64ccd 2021-03-31 op return loc->default_mime;
139 c8b74339 2021-01-24 op return default_mime;
143 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
145 c8b74339 2021-01-24 op struct location *loc;
146 c8b74339 2021-01-24 op const char *index = "index.gmi";
148 caad0308 2021-01-27 op if (v == NULL || path == NULL)
149 caad0308 2021-01-27 op return index;
151 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
152 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
153 cd761624 2021-02-06 op if (loc->index != NULL) {
154 49b73ba1 2021-02-10 op if (matches(loc->match, path))
155 6016a593 2021-01-30 op return loc->index;
159 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
160 b8e64ccd 2021-03-31 op if (loc->index != NULL)
161 b8e64ccd 2021-03-31 op return loc->index;
162 c8b74339 2021-01-24 op return index;
166 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
168 252908e6 2021-01-24 op struct location *loc;
170 6016a593 2021-01-30 op if (v == NULL || path == NULL)
173 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
174 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
175 cd761624 2021-02-06 op if (loc->auto_index != 0) {
176 49b73ba1 2021-02-10 op if (matches(loc->match, path))
177 6016a593 2021-01-30 op return loc->auto_index == 1;
181 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
182 b8e64ccd 2021-03-31 op return loc->auto_index == 1;
186 6abda252 2021-02-06 op vhost_block_return(struct vhost *v, const char *path, int *code, const char **fmt)
188 6abda252 2021-02-06 op struct location *loc;
190 6abda252 2021-02-06 op if (v == NULL || path == NULL)
193 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
194 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
195 cd761624 2021-02-06 op if (loc->block_code != 0) {
196 49b73ba1 2021-02-10 op if (matches(loc->match, path)) {
197 6abda252 2021-02-06 op *code = loc->block_code;
198 6abda252 2021-02-06 op *fmt = loc->block_fmt;
204 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
205 b8e64ccd 2021-03-31 op *code = loc->block_code;
206 b8e64ccd 2021-03-31 op *fmt = loc->block_fmt;
207 b8e64ccd 2021-03-31 op return loc->block_code != 0;
211 8ad1c570 2021-05-09 op vhost_fastcgi(struct vhost *v, const char *path)
213 8ad1c570 2021-05-09 op struct location *loc;
215 8ad1c570 2021-05-09 op if (v == NULL || path == NULL)
218 8ad1c570 2021-05-09 op loc = TAILQ_FIRST(&v->locations);
219 8ad1c570 2021-05-09 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
220 8ad1c570 2021-05-09 op if (loc->fcgi != -1)
221 8ad1c570 2021-05-09 op if (matches(loc->match, path))
222 8ad1c570 2021-05-09 op return loc->fcgi;
225 8ad1c570 2021-05-09 op loc = TAILQ_FIRST(&v->locations);
226 8ad1c570 2021-05-09 op return loc->fcgi;
230 1feaf2a6 2021-05-15 op vhost_dirfd(struct vhost *v, const char *path, size_t *retloc)
232 fdea6aa0 2021-04-30 op struct location *loc;
233 1feaf2a6 2021-05-15 op size_t l = 0;
235 fdea6aa0 2021-04-30 op if (v == NULL || path == NULL)
238 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
239 fdea6aa0 2021-04-30 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
241 fdea6aa0 2021-04-30 op if (loc->dirfd != -1)
242 1feaf2a6 2021-05-15 op if (matches(loc->match, path)) {
244 fdea6aa0 2021-04-30 op return loc->dirfd;
249 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
250 fdea6aa0 2021-04-30 op return loc->dirfd;
254 6abda252 2021-02-06 op vhost_strip(struct vhost *v, const char *path)
256 6abda252 2021-02-06 op struct location *loc;
258 6abda252 2021-02-06 op if (v == NULL || path == NULL)
261 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
262 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
263 cd761624 2021-02-06 op if (loc->strip != 0) {
264 49b73ba1 2021-02-10 op if (matches(loc->match, path))
265 6abda252 2021-02-06 op return loc->strip;
269 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
270 b8e64ccd 2021-03-31 op return loc->strip;
274 02be96c6 2021-02-09 op vhost_require_ca(struct vhost *v, const char *path)
276 02be96c6 2021-02-09 op struct location *loc;
278 02be96c6 2021-02-09 op if (v == NULL || path == NULL)
281 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
282 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
283 02be96c6 2021-02-09 op if (loc->reqca != NULL) {
284 49b73ba1 2021-02-10 op if (matches(loc->match, path))
285 02be96c6 2021-02-09 op return loc->reqca;
289 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
290 b8e64ccd 2021-03-31 op return loc->reqca;
294 793835cb 2021-02-23 op vhost_disable_log(struct vhost *v, const char *path)
296 793835cb 2021-02-23 op struct location *loc;
298 793835cb 2021-02-23 op if (v == NULL || path == NULL)
301 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
302 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
303 793835cb 2021-02-23 op if (loc->disable_log && matches(loc->match, path))
307 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
308 b8e64ccd 2021-03-31 op return loc->disable_log;
312 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
314 d3a08f4d 2021-01-17 op struct stat sb;
315 d1ca3911 2021-01-21 op const char *p;
316 efb48052 2021-07-27 op int dirfd, strip;
318 d3a08f4d 2021-01-17 op assert(path != NULL);
321 fdea6aa0 2021-04-30 op * in send_dir we add an initial / (to be redirect-friendly),
322 fdea6aa0 2021-04-30 op * but here we want to skip it
324 fdea6aa0 2021-04-30 op if (*path == '/')
327 fdea6aa0 2021-04-30 op strip = vhost_strip(c->host, path);
328 fdea6aa0 2021-04-30 op p = strip_path(path, strip);
330 fdea6aa0 2021-04-30 op if (*p == '/')
332 fdea6aa0 2021-04-30 op if (*p == '\0')
335 1feaf2a6 2021-05-15 op dirfd = vhost_dirfd(c->host, path, &c->loc);
336 fdea6aa0 2021-04-30 op log_debug(c, "check_path: strip=%d path=%s original=%s",
337 fdea6aa0 2021-04-30 op strip, p, path);
338 efb48052 2021-07-27 op if (*fd == -1 && (*fd = openat(dirfd, p, O_RDONLY)) == -1)
339 d3a08f4d 2021-01-17 op return FILE_MISSING;
341 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
342 3abf91b0 2021-02-07 op log_notice(c, "failed stat for %s: %s", path, strerror(errno));
343 d3a08f4d 2021-01-17 op return FILE_MISSING;
346 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
347 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
349 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
350 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
352 d3a08f4d 2021-01-17 op return FILE_EXISTS;
356 abc007d2 2021-02-08 op open_file(struct client *c)
358 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
359 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
360 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
361 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
365 d3a08f4d 2021-01-17 op /* fallthrough */
367 d3a08f4d 2021-01-17 op case FILE_EXISTS:
368 efe7d180 2021-10-02 op c->type = REQUEST_FILE;
369 27b2fa9a 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
372 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
376 d3a08f4d 2021-01-17 op case FILE_MISSING:
377 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
378 abc007d2 2021-02-08 op check_for_cgi(c);
381 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
385 d3a08f4d 2021-01-17 op /* unreachable */
391 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
392 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
393 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
394 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
395 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
398 abc007d2 2021-02-08 op check_for_cgi(struct client *c)
400 2fafa2d2 2021-02-01 op char path[PATH_MAX];
403 2fafa2d2 2021-02-01 op strlcpy(path, c->iri.path, sizeof(path));
404 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
406 d3a08f4d 2021-01-17 op while (end > path) {
408 571d20fb 2021-05-15 op * go up one level. UNIX paths are simple and POSIX
409 571d20fb 2021-05-15 op * dirname, with its ambiguities on if the given
410 571d20fb 2021-05-15 op * pointer is changed or not, gives me headaches.
412 3571854e 2021-09-24 op while (*end != '/' && end > path)
415 3571854e 2021-09-24 op if (end == path)
420 abc007d2 2021-02-08 op switch (check_path(c, path, &c->pfd)) {
421 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
422 abc007d2 2021-02-08 op start_cgi(path, end+1, c);
424 d3a08f4d 2021-01-17 op case FILE_MISSING:
435 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
440 9b8f5ed2 2021-02-03 op mark_nonblock(int fd)
444 9b8f5ed2 2021-02-03 op if ((flags = fcntl(fd, F_GETFL)) == -1)
445 9b8f5ed2 2021-02-03 op fatal("fcntl(F_GETFL): %s", strerror(errno));
446 9b8f5ed2 2021-02-03 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
447 9b8f5ed2 2021-02-03 op fatal("fcntl(F_SETFL): %s", strerror(errno));
451 abc007d2 2021-02-08 op handle_handshake(int fd, short ev, void *d)
453 abc007d2 2021-02-08 op struct client *c = d;
454 d3a08f4d 2021-01-17 op struct vhost *h;
455 cc8c2901 2021-04-29 op struct alist *a;
456 d3a08f4d 2021-01-17 op const char *servname;
457 a8d4a897 2021-01-29 op const char *parse_err = "unknown error";
459 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
460 d3a08f4d 2021-01-17 op case 0: /* success */
461 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
463 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
464 efe7d180 2021-10-02 op event_once(c->fd, EV_READ, handle_handshake, c, NULL);
466 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
467 efe7d180 2021-10-02 op event_once(c->fd, EV_WRITE, handle_handshake, c, NULL);
470 d3a08f4d 2021-01-17 op /* unreachable */
474 80444938 2021-10-15 op c->bev = bufferevent_new(fd, client_read, client_write,
475 80444938 2021-10-15 op client_error, c);
476 80444938 2021-10-15 op if (c->bev == NULL)
477 80444938 2021-10-15 op fatal("%s: failed to allocate client buffer: %s",
478 80444938 2021-10-15 op __func__, strerror(errno));
480 80444938 2021-10-15 op event_set(&c->bev->ev_read, c->fd, EV_READ,
481 80444938 2021-10-15 op client_tls_readcb, c->bev);
482 80444938 2021-10-15 op event_set(&c->bev->ev_write, c->fd, EV_WRITE,
483 80444938 2021-10-15 op client_tls_writecb, c->bev);
485 80444938 2021-10-15 op #if HAVE_LIBEVENT2
486 80444938 2021-10-15 op evbuffer_unfreeze(c->bev->input, 0);
487 80444938 2021-10-15 op evbuffer_unfreeze(c->bev->output, 1);
490 79288c8b 2021-09-24 op if ((servname = tls_conn_servername(c->ctx)) == NULL) {
491 79288c8b 2021-09-24 op log_debug(c, "handshake: missing SNI");
495 a8d4a897 2021-01-29 op if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
496 3abf91b0 2021-02-07 op log_info(c, "puny_decode: %s", parse_err);
500 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
501 eecad7a3 2021-02-12 op if (matches(h->domain, c->domain))
503 cc8c2901 2021-04-29 op TAILQ_FOREACH(a, &h->aliases, aliases) {
504 cc8c2901 2021-04-29 op if (matches(a->alias, c->domain))
510 3abf91b0 2021-02-07 op log_debug(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
511 3abf91b0 2021-02-07 op servname != NULL ? servname : "(null)",
513 b8e64ccd 2021-03-31 op h != NULL ? h->domain : "(null)");
515 b8e64ccd 2021-03-31 op if (h != NULL) {
517 efe7d180 2021-10-02 op bufferevent_enable(c->bev, EV_READ);
522 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
525 91b9f2a8 2021-05-15 op static const char *
526 91b9f2a8 2021-05-15 op strip_path(const char *path, int strip)
530 6abda252 2021-02-06 op while (strip > 0) {
531 6abda252 2021-02-06 op if ((t = strchr(path, '/')) == NULL) {
532 6abda252 2021-02-06 op path = strchr(path, '\0');
543 57ec3e77 2021-02-08 op fmt_sbuf(const char *fmt, struct client *c, const char *path)
546 4842c72d 2021-10-18 op char buf[32];
548 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
549 6abda252 2021-02-06 op for (i = 0; *fmt; ++fmt) {
550 6abda252 2021-02-06 op if (i == sizeof(buf)-1 || *fmt == '%') {
551 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
552 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
556 6abda252 2021-02-06 op if (*fmt != '%') {
557 6abda252 2021-02-06 op buf[i++] = *fmt;
561 6abda252 2021-02-06 op switch (*++fmt) {
563 6abda252 2021-02-06 op strlcat(c->sbuf, "%", sizeof(c->sbuf));
566 737a6b50 2021-04-30 op if (*path != '/')
567 737a6b50 2021-04-30 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
568 6abda252 2021-02-06 op strlcat(c->sbuf, path, sizeof(c->sbuf));
571 6abda252 2021-02-06 op strlcat(c->sbuf, c->iri.query, sizeof(c->sbuf));
574 6abda252 2021-02-06 op snprintf(buf, sizeof(buf), "%d", conf.port);
575 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
576 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
579 6abda252 2021-02-06 op strlcat(c->sbuf, c->domain, sizeof(c->sbuf));
582 6abda252 2021-02-06 op fatal("%s: unknown fmt specifier %c",
583 6abda252 2021-02-06 op __func__, *fmt);
588 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
591 57ec3e77 2021-02-08 op /* 1 if a matching `block return' (and apply it), 0 otherwise */
593 57ec3e77 2021-02-08 op apply_block_return(struct client *c)
595 57ec3e77 2021-02-08 op const char *fmt, *path;
598 57ec3e77 2021-02-08 op if (!vhost_block_return(c->host, c->iri.path, &code, &fmt))
601 57ec3e77 2021-02-08 op path = strip_path(c->iri.path, vhost_strip(c->host, c->iri.path));
602 57ec3e77 2021-02-08 op fmt_sbuf(fmt, c, path);
604 abc007d2 2021-02-08 op start_reply(c, code, c->sbuf);
608 b7967bc1 2021-01-02 op static struct proxy *
609 b7967bc1 2021-01-02 op matched_proxy(struct client *c)
611 b7967bc1 2021-01-02 op struct proxy *p;
612 b7967bc1 2021-01-02 op const char *proto;
613 b7967bc1 2021-01-02 op const char *host;
614 b7967bc1 2021-01-02 op const char *port;
616 b7967bc1 2021-01-02 op TAILQ_FOREACH(p, &c->host->proxies, proxies) {
617 b7967bc1 2021-01-02 op if ((proto = p->match_proto) == NULL)
618 b7967bc1 2021-01-02 op proto = "gemini";
619 b7967bc1 2021-01-02 op if ((host = p->match_host) == NULL)
621 b7967bc1 2021-01-02 op if ((port = p->match_port) == NULL)
624 b7967bc1 2021-01-02 op if (matches(proto, c->iri.schema) &&
625 b7967bc1 2021-01-02 op matches(host, c->domain) &&
626 b7967bc1 2021-01-02 op matches(port, c->iri.port))
633 72b033ef 2021-12-29 op /* 1 if matching a proxy relay-to (and apply it), 0 otherwise */
635 72b033ef 2021-12-29 op apply_reverse_proxy(struct client *c)
637 7bdcc91e 2021-01-01 op struct proxy *p;
638 7bdcc91e 2021-01-01 op struct connreq r;
640 b7967bc1 2021-01-02 op if ((p = matched_proxy(c)) == NULL)
643 b7967bc1 2021-01-02 op c->proxy = p;
645 72b033ef 2021-12-29 op log_debug(c, "opening proxy connection for %s:%s",
646 7bdcc91e 2021-01-01 op p->host, p->port);
648 7bdcc91e 2021-01-01 op strlcpy(r.host, p->host, sizeof(r.host));
649 7bdcc91e 2021-01-01 op strlcpy(r.port, p->port, sizeof(r.port));
651 72b033ef 2021-12-29 op imsg_compose(&exibuf, IMSG_CONN_REQ, c->id, 0, -1, &r, sizeof(r));
652 72b033ef 2021-12-29 op imsg_flush(&exibuf);
657 8ad1c570 2021-05-09 op /* 1 if matching `fcgi' (and apply it), 0 otherwise */
659 8ad1c570 2021-05-09 op apply_fastcgi(struct client *c)
662 8ad1c570 2021-05-09 op struct fcgi *f;
664 8ad1c570 2021-05-09 op if ((id = vhost_fastcgi(c->host, c->iri.path)) == -1)
667 4cd25209 2021-10-07 op f = &fcgi[id];
669 4cd25209 2021-10-07 op log_debug(c, "opening fastcgi connection for (%s,%s,%s)",
670 4cd25209 2021-10-07 op f->path, f->port, f->prog);
672 4cd25209 2021-10-07 op imsg_compose(&exibuf, IMSG_FCGI_REQ, c->id, 0, -1,
673 4cd25209 2021-10-07 op &id, sizeof(id));
674 4cd25209 2021-10-07 op imsg_flush(&exibuf);
678 02be96c6 2021-02-09 op /* 1 if matching `require client ca' fails (and apply it), 0 otherwise */
680 02be96c6 2021-02-09 op apply_require_ca(struct client *c)
682 02be96c6 2021-02-09 op X509_STORE *store;
683 02be96c6 2021-02-09 op const uint8_t *cert;
686 02be96c6 2021-02-09 op if ((store = vhost_require_ca(c->host, c->iri.path)) == NULL)
689 02be96c6 2021-02-09 op if (!tls_peer_cert_provided(c->ctx)) {
690 02be96c6 2021-02-09 op start_reply(c, CLIENT_CERT_REQ, "client certificate required");
694 02be96c6 2021-02-09 op cert = tls_peer_cert_chain_pem(c->ctx, &len);
695 02be96c6 2021-02-09 op if (!validate_against_ca(store, cert, len)) {
696 02be96c6 2021-02-09 op start_reply(c, CERT_NOT_AUTH, "certificate not authorised");
703 b8e64ccd 2021-03-31 op static size_t
704 b8e64ccd 2021-03-31 op host_nth(struct vhost *h)
706 b8e64ccd 2021-03-31 op struct vhost *v;
707 b8e64ccd 2021-03-31 op size_t i = 0;
709 b8e64ccd 2021-03-31 op TAILQ_FOREACH(v, &hosts, vhosts) {
719 abc007d2 2021-02-08 op start_cgi(const char *spath, const char *relpath, struct client *c)
721 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
722 bc99d868 2021-03-19 op const char *t;
723 bc99d868 2021-03-19 op struct cgireq req;
726 efe7d180 2021-10-02 op c->type = REQUEST_CGI;
728 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
729 d3a08f4d 2021-01-17 op addr, sizeof(addr),
731 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
733 bc99d868 2021-03-19 op fatal("getnameinfo failed");
735 bc99d868 2021-03-19 op memset(&req, 0, sizeof(req));
737 bc99d868 2021-03-19 op memcpy(req.buf, c->req, sizeof(req.buf));
739 bc99d868 2021-03-19 op req.iri_schema_off = c->iri.schema - c->req;
740 bc99d868 2021-03-19 op req.iri_host_off = c->iri.host - c->req;
741 bc99d868 2021-03-19 op req.iri_port_off = c->iri.port - c->req;
742 bc99d868 2021-03-19 op req.iri_path_off = c->iri.path - c->req;
743 bc99d868 2021-03-19 op req.iri_query_off = c->iri.query - c->req;
744 bc99d868 2021-03-19 op req.iri_fragment_off = c->iri.fragment - c->req;
746 bc99d868 2021-03-19 op req.iri_portno = c->iri.port_no;
748 bc99d868 2021-03-19 op strlcpy(req.spath, spath, sizeof(req.spath));
749 bc99d868 2021-03-19 op strlcpy(req.relpath, relpath, sizeof(req.relpath));
750 bc99d868 2021-03-19 op strlcpy(req.addr, addr, sizeof(req.addr));
752 bc99d868 2021-03-19 op if ((t = tls_peer_cert_subject(c->ctx)) != NULL)
753 bc99d868 2021-03-19 op strlcpy(req.subject, t, sizeof(req.subject));
754 bc99d868 2021-03-19 op if ((t = tls_peer_cert_issuer(c->ctx)) != NULL)
755 bc99d868 2021-03-19 op strlcpy(req.issuer, t, sizeof(req.issuer));
756 bc99d868 2021-03-19 op if ((t = tls_peer_cert_hash(c->ctx)) != NULL)
757 bc99d868 2021-03-19 op strlcpy(req.hash, t, sizeof(req.hash));
758 89541eee 2021-04-13 op if ((t = tls_conn_version(c->ctx)) != NULL)
759 89541eee 2021-04-13 op strlcpy(req.version, t, sizeof(req.version));
760 89541eee 2021-04-13 op if ((t = tls_conn_cipher(c->ctx)) != NULL)
761 89541eee 2021-04-13 op strlcpy(req.cipher, t, sizeof(req.cipher));
763 89541eee 2021-04-13 op req.cipher_strength = tls_conn_cipher_strength(c->ctx);
764 bc99d868 2021-03-19 op req.notbefore = tls_peer_cert_notbefore(c->ctx);
765 bc99d868 2021-03-19 op req.notafter = tls_peer_cert_notafter(c->ctx);
767 b8e64ccd 2021-03-31 op req.host_off = host_nth(c->host);
768 1feaf2a6 2021-05-15 op req.loc_off = c->loc;
770 bc99d868 2021-03-19 op imsg_compose(&exibuf, IMSG_CGI_REQ, c->id, 0, -1, &req, sizeof(req));
771 bc99d868 2021-03-19 op imsg_flush(&exibuf);
773 bc99d868 2021-03-19 op close(c->pfd);
777 abc007d2 2021-02-08 op open_dir(struct client *c)
780 11c98667 2021-04-25 op int dirfd, root;
781 252908e6 2021-01-24 op char *before_file;
783 11c98667 2021-04-25 op root = !strcmp(c->iri.path, "/") || *c->iri.path == '\0';
785 0be51733 2021-01-20 op len = strlen(c->iri.path);
786 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
787 abc007d2 2021-02-08 op redirect_canonical_dir(c);
791 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
792 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
793 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
794 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
795 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
796 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
797 c8b74339 2021-01-24 op sizeof(c->sbuf));
798 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
799 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
803 252908e6 2021-01-24 op c->iri.path = c->sbuf;
805 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
806 abc007d2 2021-02-08 op dirfd = c->pfd;
809 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
810 252908e6 2021-01-24 op case FILE_EXECUTABLE:
811 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
812 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
816 252908e6 2021-01-24 op /* fallthrough */
818 252908e6 2021-01-24 op case FILE_EXISTS:
819 efe7d180 2021-10-02 op c->type = REQUEST_FILE;
820 98ee8406 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
823 252908e6 2021-01-24 op case FILE_DIRECTORY:
824 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
827 252908e6 2021-01-24 op case FILE_MISSING:
828 252908e6 2021-01-24 op *before_file = '\0';
830 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
831 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
835 efe7d180 2021-10-02 op c->type = REQUEST_DIR;
837 e76f2c74 2021-04-25 op c->dirlen = scandir_fd(dirfd, &c->dir,
838 11c98667 2021-04-25 op root ? select_non_dotdot : select_non_dot,
840 11c98667 2021-04-25 op if (c->dirlen == -1) {
841 11c98667 2021-04-25 op log_err(c, "scandir_fd(%d) (vhost:%s) %s: %s",
842 abc007d2 2021-02-08 op c->pfd, c->host->domain, c->iri.path, strerror(errno));
843 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
846 11c98667 2021-04-25 op c->diroff = 0;
849 4842c72d 2021-10-18 op start_reply(c, SUCCESS, "text/gemini");
850 efe7d180 2021-10-02 op evbuffer_add_printf(EVBUFFER_OUTPUT(c->bev),
851 efe7d180 2021-10-02 op "# Index of %s\n\n", c->iri.path);
855 252908e6 2021-01-24 op /* unreachable */
859 252908e6 2021-01-24 op close(dirfd);
863 abc007d2 2021-02-08 op redirect_canonical_dir(struct client *c)
867 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
868 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
869 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
871 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
872 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
876 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
880 efe7d180 2021-10-02 op client_tls_readcb(int fd, short event, void *d)
882 efe7d180 2021-10-02 op struct bufferevent *bufev = d;
883 efe7d180 2021-10-02 op struct client *client = bufev->cbarg;
886 efe7d180 2021-10-02 op int what = EVBUFFER_READ;
887 efe7d180 2021-10-02 op int howmuch = IBUF_READ_SIZE;
888 efe7d180 2021-10-02 op char buf[IBUF_READ_SIZE];
890 efe7d180 2021-10-02 op if (event == EV_TIMEOUT) {
891 efe7d180 2021-10-02 op what |= EVBUFFER_TIMEOUT;
895 efe7d180 2021-10-02 op if (bufev->wm_read.high != 0)
896 efe7d180 2021-10-02 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
898 efe7d180 2021-10-02 op switch (ret = tls_read(client->ctx, buf, howmuch)) {
899 efe7d180 2021-10-02 op case TLS_WANT_POLLIN:
900 efe7d180 2021-10-02 op case TLS_WANT_POLLOUT:
903 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
908 efe7d180 2021-10-02 op if (len == 0) {
909 efe7d180 2021-10-02 op what |= EVBUFFER_EOF;
913 efe7d180 2021-10-02 op if (evbuffer_add(bufev->input, buf, len) == -1) {
914 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
918 efe7d180 2021-10-02 op event_add(&bufev->ev_read, NULL);
919 efe7d180 2021-10-02 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
921 efe7d180 2021-10-02 op if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
923 efe7d180 2021-10-02 op * here we could implement a read pressure policy.
927 efe7d180 2021-10-02 op if (bufev->readcb != NULL)
928 efe7d180 2021-10-02 op (*bufev->readcb)(bufev, bufev->cbarg);
933 efe7d180 2021-10-02 op event_add(&bufev->ev_read, NULL);
937 efe7d180 2021-10-02 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
941 efe7d180 2021-10-02 op client_tls_writecb(int fd, short event, void *d)
943 efe7d180 2021-10-02 op struct bufferevent *bufev = d;
944 efe7d180 2021-10-02 op struct client *client = bufev->cbarg;
947 efe7d180 2021-10-02 op short what = EVBUFFER_WRITE;
949 efe7d180 2021-10-02 op if (event == EV_TIMEOUT) {
950 efe7d180 2021-10-02 op what |= EVBUFFER_TIMEOUT;
954 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
955 efe7d180 2021-10-02 op ret = tls_write(client->ctx,
956 efe7d180 2021-10-02 op EVBUFFER_DATA(bufev->output),
957 efe7d180 2021-10-02 op EVBUFFER_LENGTH(bufev->output));
958 efe7d180 2021-10-02 op switch (ret) {
959 efe7d180 2021-10-02 op case TLS_WANT_POLLIN:
960 efe7d180 2021-10-02 op case TLS_WANT_POLLOUT:
963 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
967 efe7d180 2021-10-02 op evbuffer_drain(bufev->output, len);
970 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(bufev->output) != 0)
971 efe7d180 2021-10-02 op event_add(&bufev->ev_write, NULL);
973 efe7d180 2021-10-02 op if (bufev->writecb != NULL &&
974 efe7d180 2021-10-02 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
975 efe7d180 2021-10-02 op (*bufev->writecb)(bufev, bufev->cbarg);
979 efe7d180 2021-10-02 op event_add(&bufev->ev_write, NULL);
982 efe7d180 2021-10-02 op log_err(client, "tls error: %s", tls_error(client->ctx));
983 efe7d180 2021-10-02 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
987 efe7d180 2021-10-02 op client_read(struct bufferevent *bev, void *d)
989 efe7d180 2021-10-02 op struct client *c = d;
990 efe7d180 2021-10-02 op struct evbuffer *src = EVBUFFER_INPUT(bev);
991 efe7d180 2021-10-02 op const char *parse_err = "invalid request";
992 efe7d180 2021-10-02 op char decoded[DOMAIN_NAME_LEN];
995 efe7d180 2021-10-02 op bufferevent_disable(bev, EVBUFFER_READ);
997 efe7d180 2021-10-02 op /* max url len + \r\n */
998 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(src) > 1024 + 2) {
999 efe7d180 2021-10-02 op log_err(c, "too much data received");
1000 efe7d180 2021-10-02 op start_reply(c, BAD_REQUEST, "bad request");
1004 efe7d180 2021-10-02 op c->req = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
1005 efe7d180 2021-10-02 op if (c->req == NULL) {
1006 efe7d180 2021-10-02 op /* not enough data yet. */
1007 efe7d180 2021-10-02 op bufferevent_enable(bev, EVBUFFER_READ);
1011 efe7d180 2021-10-02 op if (!parse_iri(c->req, &c->iri, &parse_err) ||
1012 efe7d180 2021-10-02 op !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
1013 efe7d180 2021-10-02 op log_err(c, "IRI parse error: %s", parse_err);
1014 807a80cb 2021-10-06 op start_reply(c, BAD_REQUEST, "bad request");
1018 72b033ef 2021-12-29 op if (apply_reverse_proxy(c))
1021 52c92ef6 2021-12-09 op /* ignore the port number */
1022 52c92ef6 2021-12-09 op if (strcmp(c->iri.schema, "gemini") ||
1023 efe7d180 2021-10-02 op strcmp(decoded, c->domain)) {
1024 efe7d180 2021-10-02 op start_reply(c, PROXY_REFUSED, "won't proxy request");
1028 efe7d180 2021-10-02 op if (apply_require_ca(c) ||
1029 efe7d180 2021-10-02 op apply_block_return(c)||
1030 efe7d180 2021-10-02 op apply_fastcgi(c))
1033 efe7d180 2021-10-02 op if (c->host->entrypoint != NULL) {
1035 efe7d180 2021-10-02 op start_cgi(c->host->entrypoint, c->iri.path, c);
1039 efe7d180 2021-10-02 op open_file(c);
1043 efe7d180 2021-10-02 op client_write(struct bufferevent *bev, void *d)
1045 efe7d180 2021-10-02 op struct client *c = d;
1046 efe7d180 2021-10-02 op struct evbuffer *out = EVBUFFER_OUTPUT(bev);
1047 efe7d180 2021-10-02 op char buf[BUFSIZ];
1050 efe7d180 2021-10-02 op switch (c->type) {
1051 efe7d180 2021-10-02 op case REQUEST_UNDECIDED:
1053 efe7d180 2021-10-02 op * Ignore spurious calls when we still don't have idea
1054 efe7d180 2021-10-02 op * what to do with the request.
1058 efe7d180 2021-10-02 op case REQUEST_FILE:
1059 efe7d180 2021-10-02 op if ((r = read(c->pfd, buf, sizeof(buf))) == -1) {
1060 efe7d180 2021-10-02 op log_warn(c, "read: %s", strerror(errno));
1061 efe7d180 2021-10-02 op client_error(bev, EVBUFFER_ERROR, c);
1063 efe7d180 2021-10-02 op } else if (r == 0) {
1064 efe7d180 2021-10-02 op client_close(c);
1066 efe7d180 2021-10-02 op } else if (r != sizeof(buf))
1067 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1068 efe7d180 2021-10-02 op bufferevent_write(bev, buf, r);
1071 efe7d180 2021-10-02 op case REQUEST_DIR:
1072 efe7d180 2021-10-02 op /* TODO: handle big big directories better */
1073 efe7d180 2021-10-02 op for (c->diroff = 0; c->diroff < c->dirlen; ++c->diroff) {
1074 efe7d180 2021-10-02 op evbuffer_add_printf(out, "=> %s\n",
1075 efe7d180 2021-10-02 op c->dir[c->diroff]->d_name);
1076 efe7d180 2021-10-02 op free(c->dir[c->diroff]);
1078 efe7d180 2021-10-02 op free(c->dir);
1079 efe7d180 2021-10-02 op c->dir = NULL;
1081 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1083 efe7d180 2021-10-02 op event_add(&c->bev->ev_write, NULL);
1086 efe7d180 2021-10-02 op case REQUEST_CGI:
1087 efe7d180 2021-10-02 op case REQUEST_FCGI:
1088 72b033ef 2021-12-29 op case REQUEST_PROXY:
1090 efe7d180 2021-10-02 op * Here we depend on on the cgi script or fastcgi
1091 efe7d180 2021-10-02 op * connection to provide data.
1095 efe7d180 2021-10-02 op case REQUEST_DONE:
1096 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(out) == 0)
1097 efe7d180 2021-10-02 op client_close(c);
1103 efe7d180 2021-10-02 op client_error(struct bufferevent *bev, short error, void *d)
1105 efe7d180 2021-10-02 op struct client *c = d;
1107 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1109 efe7d180 2021-10-02 op if (error & EVBUFFER_TIMEOUT) {
1110 efe7d180 2021-10-02 op log_warn(c, "timeout reached, "
1111 efe7d180 2021-10-02 op "forcefully closing the connection");
1112 efe7d180 2021-10-02 op if (c->code == 0)
1113 efe7d180 2021-10-02 op start_reply(c, BAD_REQUEST, "timeout");
1115 efe7d180 2021-10-02 op client_close(c);
1119 efe7d180 2021-10-02 op if (error & EVBUFFER_EOF) {
1120 efe7d180 2021-10-02 op client_close(c);
1124 efe7d180 2021-10-02 op log_err(c, "unknown bufferevent error: %s", strerror(errno));
1125 efe7d180 2021-10-02 op client_close(c);
1129 efe7d180 2021-10-02 op start_reply(struct client *c, int code, const char *meta)
1131 efe7d180 2021-10-02 op struct evbuffer *evb = EVBUFFER_OUTPUT(c->bev);
1132 efe7d180 2021-10-02 op const char *lang;
1135 efe7d180 2021-10-02 op bufferevent_enable(c->bev, EVBUFFER_WRITE);
1137 efe7d180 2021-10-02 op c->code = code;
1138 efe7d180 2021-10-02 op c->meta = meta;
1140 efe7d180 2021-10-02 op r = evbuffer_add_printf(evb, "%d %s", code, meta);
1141 efe7d180 2021-10-02 op if (r == -1)
1144 efe7d180 2021-10-02 op /* 2 digit status + space + 1024 max reply */
1145 efe7d180 2021-10-02 op if (r > 1027)
1146 efe7d180 2021-10-02 op goto overflow;
1148 efe7d180 2021-10-02 op if (c->type != REQUEST_CGI &&
1149 efe7d180 2021-10-02 op c->type != REQUEST_FCGI &&
1150 72b033ef 2021-12-29 op c->type != REQUEST_PROXY &&
1151 efe7d180 2021-10-02 op !strcmp(meta, "text/gemini") &&
1152 efe7d180 2021-10-02 op (lang = vhost_lang(c->host, c->iri.path)) != NULL) {
1153 4842c72d 2021-10-18 op rr = evbuffer_add_printf(evb, ";lang=%s", lang);
1154 efe7d180 2021-10-02 op if (rr == -1)
1156 efe7d180 2021-10-02 op if (r + rr > 1027)
1157 efe7d180 2021-10-02 op goto overflow;
1160 efe7d180 2021-10-02 op bufferevent_write(c->bev, "\r\n", 2);
1162 efe7d180 2021-10-02 op if (!vhost_disable_log(c->host, c->iri.path))
1163 efe7d180 2021-10-02 op log_request(c, EVBUFFER_DATA(evb), EVBUFFER_LENGTH(evb));
1165 efe7d180 2021-10-02 op if (code != 20 && IS_INTERNAL_REQUEST(c->type))
1166 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1171 efe7d180 2021-10-02 op log_err(c, "evbuffer_add_printf error: no memory");
1172 efe7d180 2021-10-02 op evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
1173 efe7d180 2021-10-02 op client_close(c);
1177 efe7d180 2021-10-02 op log_warn(c, "reply header overflow");
1178 efe7d180 2021-10-02 op evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
1179 efe7d180 2021-10-02 op start_reply(c, TEMP_FAILURE, "internal error");
1183 efe7d180 2021-10-02 op client_close_ev(int fd, short event, void *d)
1185 8ad1c570 2021-05-09 op struct client *c = d;
1187 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
1188 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
1189 efe7d180 2021-10-02 op event_once(c->fd, EV_READ, client_close_ev, c, NULL);
1191 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
1192 efe7d180 2021-10-02 op event_once(c->fd, EV_WRITE, client_close_ev, c, NULL);
1196 d3a08f4d 2021-01-17 op connected_clients--;
1198 e4daebe4 2021-10-06 op free(c->req);
1200 d3a08f4d 2021-01-17 op tls_free(c->ctx);
1201 d3a08f4d 2021-01-17 op c->ctx = NULL;
1203 efe7d180 2021-10-02 op free(c->header);
1205 abc007d2 2021-02-08 op if (c->pfd != -1)
1206 abc007d2 2021-02-08 op close(c->pfd);
1208 252908e6 2021-01-24 op if (c->dir != NULL)
1209 11c98667 2021-04-25 op free(c->dir);
1211 abc007d2 2021-02-08 op close(c->fd);
1216 72b033ef 2021-12-29 op client_proxy_close(int fd, short event, void *d)
1218 72b033ef 2021-12-29 op struct tls *ctx = d;
1220 72b033ef 2021-12-29 op if (ctx == NULL) {
1225 72b033ef 2021-12-29 op switch (tls_close(ctx)) {
1226 72b033ef 2021-12-29 op case TLS_WANT_POLLIN:
1227 72b033ef 2021-12-29 op event_once(fd, EV_READ, client_proxy_close, d, NULL);
1229 72b033ef 2021-12-29 op case TLS_WANT_POLLOUT:
1230 72b033ef 2021-12-29 op event_once(fd, EV_WRITE, client_proxy_close, d, NULL);
1234 72b033ef 2021-12-29 op tls_free(ctx);
1239 efe7d180 2021-10-02 op client_close(struct client *c)
1242 efe7d180 2021-10-02 op * We may end up calling client_close in various situations
1243 efe7d180 2021-10-02 op * and for the most unexpected reasons. Therefore, we need to
1244 efe7d180 2021-10-02 op * ensure that everything is properly released once we reach
1245 efe7d180 2021-10-02 op * this point.
1248 207b3e80 2021-10-07 op SPLAY_REMOVE(client_tree_id, &clients, c);
1250 efe7d180 2021-10-02 op if (c->cgibev != NULL) {
1251 efe7d180 2021-10-02 op bufferevent_disable(c->cgibev, EVBUFFER_READ|EVBUFFER_WRITE);
1252 efe7d180 2021-10-02 op bufferevent_free(c->cgibev);
1253 efe7d180 2021-10-02 op c->cgibev = NULL;
1254 efe7d180 2021-10-02 op close(c->pfd);
1255 efe7d180 2021-10-02 op c->pfd = -1;
1258 efe7d180 2021-10-02 op bufferevent_disable(c->bev, EVBUFFER_READ|EVBUFFER_WRITE);
1259 efe7d180 2021-10-02 op bufferevent_free(c->bev);
1260 efe7d180 2021-10-02 op c->bev = NULL;
1262 72b033ef 2021-12-29 op if (c->proxybev != NULL) {
1263 72b033ef 2021-12-29 op if (event_pending(&c->proxyev, EV_READ|EV_WRITE, NULL))
1264 72b033ef 2021-12-29 op event_del(&c->proxyev);
1266 593e412b 2021-01-01 op if (c->pfd != -1 && c->proxyctx != NULL) {
1267 593e412b 2021-01-01 op /* shut down the proxy TLS connection */
1268 72b033ef 2021-12-29 op client_proxy_close(c->pfd, 0, c->proxyctx);
1269 72b033ef 2021-12-29 op c->pfd = -1;
1272 72b033ef 2021-12-29 op bufferevent_free(c->proxybev);
1275 efe7d180 2021-10-02 op client_close_ev(c->fd, 0, c);
1279 efe7d180 2021-10-02 op cgi_read(struct bufferevent *bev, void *d)
1281 efe7d180 2021-10-02 op struct client *client = d;
1282 efe7d180 2021-10-02 op struct evbuffer *src = EVBUFFER_INPUT(bev);
1283 efe7d180 2021-10-02 op char *header;
1287 efe7d180 2021-10-02 op /* intercept the header */
1288 efe7d180 2021-10-02 op if (client->code == 0) {
1289 efe7d180 2021-10-02 op header = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
1290 efe7d180 2021-10-02 op if (header == NULL) {
1291 efe7d180 2021-10-02 op /* max reply + \r\n */
1292 6a6b4a2a 2021-12-29 op if (EVBUFFER_LENGTH(src) > 1029) {
1293 efe7d180 2021-10-02 op log_warn(client, "CGI script is trying to "
1294 efe7d180 2021-10-02 op "send a header too long.");
1295 efe7d180 2021-10-02 op cgi_error(bev, EVBUFFER_READ, client);
1298 efe7d180 2021-10-02 op /* wait a bit */
1302 efe7d180 2021-10-02 op if (len < 3 || len > 1029 ||
1303 efe7d180 2021-10-02 op !isdigit(header[0]) ||
1304 efe7d180 2021-10-02 op !isdigit(header[1]) ||
1305 efe7d180 2021-10-02 op !isspace(header[2])) {
1306 efe7d180 2021-10-02 op free(header);
1307 efe7d180 2021-10-02 op log_warn(client, "CGI script is trying to send a "
1308 efe7d180 2021-10-02 op "malformed header");
1309 efe7d180 2021-10-02 op cgi_error(bev, EVBUFFER_READ, client);
1313 efe7d180 2021-10-02 op client->header = header;
1314 efe7d180 2021-10-02 op code = (header[0] - '0') * 10 + (header[1] - '0');
1316 efe7d180 2021-10-02 op if (code < 10 || code >= 70) {
1317 efe7d180 2021-10-02 op log_warn(client, "CGI script is trying to send an "
1318 efe7d180 2021-10-02 op "invalid reply code (%d)", code);
1319 efe7d180 2021-10-02 op cgi_error(bev, EVBUFFER_READ, client);
1323 efe7d180 2021-10-02 op start_reply(client, code, header + 3);
1325 efe7d180 2021-10-02 op if (client->code < 20 || client->code > 29) {
1326 efe7d180 2021-10-02 op cgi_error(client->cgibev, EVBUFFER_EOF, client);
1331 efe7d180 2021-10-02 op bufferevent_write_buffer(client->bev, src);
1335 efe7d180 2021-10-02 op cgi_write(struct bufferevent *bev, void *d)
1338 efe7d180 2021-10-02 op * Never called. We don't send data to a CGI script.
1344 efe7d180 2021-10-02 op cgi_error(struct bufferevent *bev, short error, void *d)
1346 efe7d180 2021-10-02 op struct client *client = d;
1348 efe7d180 2021-10-02 op if (error & EVBUFFER_ERROR)
1349 efe7d180 2021-10-02 op log_err(client, "%s: evbuffer error (%x): %s",
1350 efe7d180 2021-10-02 op __func__, error, strerror(errno));
1352 efe7d180 2021-10-02 op bufferevent_disable(bev, EVBUFFER_READ|EVBUFFER_WRITE);
1353 efe7d180 2021-10-02 op bufferevent_free(bev);
1354 efe7d180 2021-10-02 op client->cgibev = NULL;
1356 efe7d180 2021-10-02 op close(client->pfd);
1357 efe7d180 2021-10-02 op client->pfd = -1;
1359 efe7d180 2021-10-02 op client->type = REQUEST_DONE;
1360 efe7d180 2021-10-02 op if (client->code != 0)
1361 efe7d180 2021-10-02 op client_write(client->bev, client);
1363 efe7d180 2021-10-02 op start_reply(client, CGI_ERROR, "CGI error");
1367 abc007d2 2021-02-08 op do_accept(int sock, short et, void *d)
1369 abc007d2 2021-02-08 op struct client *c;
1370 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
1371 abc007d2 2021-02-08 op struct sockaddr *saddr;
1372 d3a08f4d 2021-01-17 op socklen_t len;
1375 abc007d2 2021-02-08 op saddr = (struct sockaddr*)&addr;
1376 d3a08f4d 2021-01-17 op len = sizeof(addr);
1377 3cb3dd4d 2021-02-12 op if ((fd = accept(sock, saddr, &len)) == -1) {
1378 c62a411f 2021-10-13 op if (errno == EWOULDBLOCK || errno == EAGAIN ||
1379 c62a411f 2021-10-13 op errno == ECONNABORTED)
1381 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
1384 3cb3dd4d 2021-02-12 op mark_nonblock(fd);
1386 207b3e80 2021-10-07 op c = xcalloc(1, sizeof(*c));
1387 207b3e80 2021-10-07 op c->id = ++server_client_id;
1389 207b3e80 2021-10-07 op c->pfd = -1;
1390 207b3e80 2021-10-07 op c->addr = addr;
1392 207b3e80 2021-10-07 op if (tls_accept_socket(ctx, &c->ctx, fd) == -1) {
1393 207b3e80 2021-10-07 op log_warn(c, "failed to accept socket: %s", tls_error(c->ctx));
1394 207b3e80 2021-10-07 op close(c->fd);
1399 207b3e80 2021-10-07 op SPLAY_INSERT(client_tree_id, &clients, c);
1400 207b3e80 2021-10-07 op event_once(c->fd, EV_READ|EV_WRITE, handle_handshake, c, NULL);
1401 207b3e80 2021-10-07 op connected_clients++;
1404 8ad1c570 2021-05-09 op static struct client *
1405 bc99d868 2021-03-19 op client_by_id(int id)
1407 207b3e80 2021-10-07 op struct client *c;
1409 207b3e80 2021-10-07 op if ((c = try_client_by_id(id)) == NULL)
1410 bc99d868 2021-03-19 op fatal("in client_by_id: invalid id %d", id);
1414 8ad1c570 2021-05-09 op struct client *
1415 8ad1c570 2021-05-09 op try_client_by_id(int id)
1417 207b3e80 2021-10-07 op struct client find;
1419 207b3e80 2021-10-07 op find.id = id;
1420 207b3e80 2021-10-07 op return SPLAY_FIND(client_tree_id, &clients, &find);
1424 bc99d868 2021-03-19 op handle_imsg_cgi_res(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1426 bc99d868 2021-03-19 op struct client *c;
1428 bc99d868 2021-03-19 op c = client_by_id(imsg->hdr.peerid);
1430 efe7d180 2021-10-02 op if ((c->pfd = imsg->fd) == -1) {
1431 bc99d868 2021-03-19 op start_reply(c, TEMP_FAILURE, "internal server error");
1435 efe7d180 2021-10-02 op c->type = REQUEST_CGI;
1437 efe7d180 2021-10-02 op c->cgibev = bufferevent_new(c->pfd, cgi_read, cgi_write,
1438 efe7d180 2021-10-02 op cgi_error, c);
1440 efe7d180 2021-10-02 op bufferevent_enable(c->cgibev, EV_READ);
1444 8ad1c570 2021-05-09 op handle_imsg_fcgi_fd(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1446 8ad1c570 2021-05-09 op struct client *c;
1449 8ad1c570 2021-05-09 op id = imsg->hdr.peerid;
1451 4cd25209 2021-10-07 op if ((c = try_client_by_id(id)) == NULL) {
1452 4cd25209 2021-10-07 op if (imsg->fd != -1)
1453 4cd25209 2021-10-07 op close(imsg->fd);
1457 4cd25209 2021-10-07 op if ((c->pfd = imsg->fd) == -1) {
1458 4cd25209 2021-10-07 op start_reply(c, CGI_ERROR, "CGI error");
1462 4cd25209 2021-10-07 op mark_nonblock(c->pfd);
1464 4cd25209 2021-10-07 op c->cgibev = bufferevent_new(c->pfd, fcgi_read, fcgi_write,
1465 4cd25209 2021-10-07 op fcgi_error, c);
1466 4cd25209 2021-10-07 op if (c->cgibev == NULL) {
1467 4cd25209 2021-10-07 op start_reply(c, TEMP_FAILURE, "internal server error");
1471 4cd25209 2021-10-07 op bufferevent_enable(c->cgibev, EV_READ|EV_WRITE);
1472 4cd25209 2021-10-07 op fcgi_req(c);
1476 72b033ef 2021-12-29 op handle_imsg_conn_fd(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1478 72b033ef 2021-12-29 op struct client *c;
1481 72b033ef 2021-12-29 op id = imsg->hdr.peerid;
1482 72b033ef 2021-12-29 op if ((c = try_client_by_id(id)) == NULL) {
1483 72b033ef 2021-12-29 op if (imsg->fd != -1)
1484 72b033ef 2021-12-29 op close(imsg->fd);
1488 72b033ef 2021-12-29 op if ((c->pfd = imsg->fd) == -1) {
1489 72b033ef 2021-12-29 op start_reply(c, PROXY_ERROR, "proxy error");
1493 72b033ef 2021-12-29 op mark_nonblock(c->pfd);
1495 72b033ef 2021-12-29 op if (proxy_init(c) == -1)
1496 72b033ef 2021-12-29 op start_reply(c, PROXY_ERROR, "proxy error");
1500 bc99d868 2021-03-19 op handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1503 a8a1f439 2021-07-07 op * don't call event_loopbreak since we want to finish to
1504 a8a1f439 2021-07-07 op * handle the ongoing connections.
1507 090b8a89 2021-07-06 op shutting_down = 1;
1509 a6e689d7 2021-02-12 op event_del(&e4);
1510 a6e689d7 2021-02-12 op if (has_ipv6)
1511 a6e689d7 2021-02-12 op event_del(&e6);
1512 a6e689d7 2021-02-12 op if (has_siginfo)
1513 a6e689d7 2021-02-12 op signal_del(&siginfo);
1514 bc99d868 2021-03-19 op event_del(&imsgev);
1515 a6e689d7 2021-02-12 op signal_del(&sigusr2);
1519 bc99d868 2021-03-19 op handle_dispatch_imsg(int fd, short ev, void *d)
1521 bc99d868 2021-03-19 op struct imsgbuf *ibuf = d;
1522 bc99d868 2021-03-19 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
1526 abc007d2 2021-02-08 op handle_siginfo(int fd, short ev, void *d)
1528 abc007d2 2021-02-08 op log_info(NULL, "%d connected clients", connected_clients);
1532 bc99d868 2021-03-19 op loop(struct tls *ctx_, int sock4, int sock6, struct imsgbuf *ibuf)
1536 207b3e80 2021-10-07 op SPLAY_INIT(&clients);
1538 abc007d2 2021-02-08 op event_init();
1540 bc99d868 2021-03-19 op event_set(&e4, sock4, EV_READ | EV_PERSIST, &do_accept, NULL);
1541 a6e689d7 2021-02-12 op event_add(&e4, NULL);
1543 abc007d2 2021-02-08 op if (sock6 != -1) {
1544 a6e689d7 2021-02-12 op has_ipv6 = 1;
1545 bc99d868 2021-03-19 op event_set(&e6, sock6, EV_READ | EV_PERSIST, &do_accept, NULL);
1546 a6e689d7 2021-02-12 op event_add(&e6, NULL);
1549 bc99d868 2021-03-19 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
1550 bc99d868 2021-03-19 op event_add(&imsgev, NULL);
1552 abc007d2 2021-02-08 op #ifdef SIGINFO
1553 a6e689d7 2021-02-12 op has_siginfo = 1;
1554 a6e689d7 2021-02-12 op signal_set(&siginfo, SIGINFO, &handle_siginfo, NULL);
1555 a6e689d7 2021-02-12 op signal_add(&siginfo, NULL);
1557 5e3285d5 2021-02-12 op signal_set(&sigusr2, SIGUSR2, &handle_siginfo, NULL);
1558 a6e689d7 2021-02-12 op signal_add(&sigusr2, NULL);
1560 62e001b0 2021-03-20 op sandbox_server_process();
1561 abc007d2 2021-02-08 op event_dispatch();
1566 207b3e80 2021-10-07 op client_tree_cmp(struct client *a, struct client *b)
1568 207b3e80 2021-10-07 op if (a->id == b->id)
1570 207b3e80 2021-10-07 op else if (a->id < b->id)
1576 207b3e80 2021-10-07 op SPLAY_GENERATE(client_tree_id, client, entry, client_tree_cmp)