Blame


1 d3a08f4d 2021-01-17 op /*
2 a555e0d6 2022-07-04 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
3 d3a08f4d 2021-01-17 op *
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.
7 d3a08f4d 2021-01-17 op *
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.
15 d3a08f4d 2021-01-17 op */
16 52418c8d 2021-02-12 op
17 52418c8d 2021-02-12 op #include "gmid.h"
18 d3a08f4d 2021-01-17 op
19 d3a08f4d 2021-01-17 op #include <sys/stat.h>
20 d29a2ee2 2022-09-06 op #include <sys/un.h>
21 d3a08f4d 2021-01-17 op
22 d3a08f4d 2021-01-17 op #include <assert.h>
23 efe7d180 2021-10-02 op #include <ctype.h>
24 d3a08f4d 2021-01-17 op #include <errno.h>
25 abc007d2 2021-02-08 op #include <event.h>
26 d3a08f4d 2021-01-17 op #include <fcntl.h>
27 b4d409cf 2021-01-21 op #include <fnmatch.h>
28 2fafa2d2 2021-02-01 op #include <limits.h>
29 d3a08f4d 2021-01-17 op #include <string.h>
30 090b8a89 2021-07-06 op
31 efe7d180 2021-10-02 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
32 efe7d180 2021-10-02 op
33 090b8a89 2021-07-06 op int shutting_down;
34 d3a08f4d 2021-01-17 op
35 bc99d868 2021-03-19 op static struct tls *ctx;
36 abc007d2 2021-02-08 op
37 bc99d868 2021-03-19 op static struct event e4, e6, imsgev, siginfo, sigusr2;
38 6b191ed5 2021-02-23 op static int has_ipv6, has_siginfo;
39 abc007d2 2021-02-08 op
40 d3a08f4d 2021-01-17 op int connected_clients;
41 d3a08f4d 2021-01-17 op
42 49b73ba1 2021-02-10 op static inline int matches(const char*, const char*);
43 49b73ba1 2021-02-10 op
44 fe406389 2021-02-02 op static int check_path(struct client*, const char*, int*);
45 abc007d2 2021-02-08 op static void open_file(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 d474a979 2022-01-04 op static int check_matching_certificate(X509_STORE *, struct client *);
51 72b033ef 2021-12-29 op static int apply_reverse_proxy(struct client *);
52 efe7d180 2021-10-02 op static int apply_fastcgi(struct client*);
53 02be96c6 2021-02-09 op static int apply_require_ca(struct client*);
54 abc007d2 2021-02-08 op static void open_dir(struct client*);
55 abc007d2 2021-02-08 op static void redirect_canonical_dir(struct client*);
56 efe7d180 2021-10-02 op
57 efe7d180 2021-10-02 op static void client_tls_readcb(int, short, void *);
58 efe7d180 2021-10-02 op static void client_tls_writecb(int, short, void *);
59 efe7d180 2021-10-02 op
60 efe7d180 2021-10-02 op static void client_read(struct bufferevent *, void *);
61 efe7d180 2021-10-02 op void client_write(struct bufferevent *, void *);
62 efe7d180 2021-10-02 op static void client_error(struct bufferevent *, short, void *);
63 efe7d180 2021-10-02 op
64 efe7d180 2021-10-02 op static void client_close_ev(int, short, void *);
65 efe7d180 2021-10-02 op
66 abc007d2 2021-02-08 op static void do_accept(int, short, void*);
67 efe7d180 2021-10-02 op
68 efe7d180 2021-10-02 op static void handle_dispatch_imsg(int, short, void *);
69 bc99d868 2021-03-19 op static void handle_siginfo(int, short, void*);
70 bc99d868 2021-03-19 op
71 207b3e80 2021-10-07 op static uint32_t server_client_id;
72 207b3e80 2021-10-07 op
73 207b3e80 2021-10-07 op struct client_tree_id clients;
74 fe406389 2021-02-02 op
75 49b73ba1 2021-02-10 op static inline int
76 49b73ba1 2021-02-10 op matches(const char *pattern, const char *path)
77 49b73ba1 2021-02-10 op {
78 49b73ba1 2021-02-10 op if (*path == '/')
79 49b73ba1 2021-02-10 op path++;
80 49b73ba1 2021-02-10 op return !fnmatch(pattern, path, 0);
81 abc007d2 2021-02-08 op }
82 abc007d2 2021-02-08 op
83 c8b74339 2021-01-24 op const char *
84 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
85 c8b74339 2021-01-24 op {
86 c8b74339 2021-01-24 op struct location *loc;
87 c8b74339 2021-01-24 op
88 caad0308 2021-01-27 op if (v == NULL || path == NULL)
89 6016a593 2021-01-30 op return NULL;
90 8443bff7 2021-01-25 op
91 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
92 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
93 cd761624 2021-02-06 op if (loc->lang != NULL) {
94 49b73ba1 2021-02-10 op if (matches(loc->match, path))
95 6016a593 2021-01-30 op return loc->lang;
96 c8b74339 2021-01-24 op }
97 c8b74339 2021-01-24 op }
98 c8b74339 2021-01-24 op
99 b8e64ccd 2021-03-31 op return TAILQ_FIRST(&v->locations)->lang;
100 c8b74339 2021-01-24 op }
101 c8b74339 2021-01-24 op
102 c8b74339 2021-01-24 op const char *
103 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
104 c8b74339 2021-01-24 op {
105 c8b74339 2021-01-24 op struct location *loc;
106 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
107 caad0308 2021-01-27 op
108 caad0308 2021-01-27 op if (v == NULL || path == NULL)
109 caad0308 2021-01-27 op return default_mime;
110 c8b74339 2021-01-24 op
111 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
112 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
113 cd761624 2021-02-06 op if (loc->default_mime != NULL) {
114 49b73ba1 2021-02-10 op if (matches(loc->match, path))
115 6016a593 2021-01-30 op return loc->default_mime;
116 c8b74339 2021-01-24 op }
117 c8b74339 2021-01-24 op }
118 c8b74339 2021-01-24 op
119 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
120 b8e64ccd 2021-03-31 op if (loc->default_mime != NULL)
121 b8e64ccd 2021-03-31 op return loc->default_mime;
122 c8b74339 2021-01-24 op return default_mime;
123 c8b74339 2021-01-24 op }
124 c8b74339 2021-01-24 op
125 c8b74339 2021-01-24 op const char *
126 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
127 c8b74339 2021-01-24 op {
128 c8b74339 2021-01-24 op struct location *loc;
129 c8b74339 2021-01-24 op const char *index = "index.gmi";
130 c8b74339 2021-01-24 op
131 caad0308 2021-01-27 op if (v == NULL || path == NULL)
132 caad0308 2021-01-27 op return index;
133 caad0308 2021-01-27 op
134 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
135 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
136 cd761624 2021-02-06 op if (loc->index != NULL) {
137 49b73ba1 2021-02-10 op if (matches(loc->match, path))
138 6016a593 2021-01-30 op return loc->index;
139 c8b74339 2021-01-24 op }
140 c8b74339 2021-01-24 op }
141 c8b74339 2021-01-24 op
142 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
143 b8e64ccd 2021-03-31 op if (loc->index != NULL)
144 b8e64ccd 2021-03-31 op return loc->index;
145 c8b74339 2021-01-24 op return index;
146 c8b74339 2021-01-24 op }
147 c8b74339 2021-01-24 op
148 d3a08f4d 2021-01-17 op int
149 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
150 252908e6 2021-01-24 op {
151 252908e6 2021-01-24 op struct location *loc;
152 252908e6 2021-01-24 op
153 6016a593 2021-01-30 op if (v == NULL || path == NULL)
154 6016a593 2021-01-30 op return 0;
155 6016a593 2021-01-30 op
156 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
157 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
158 cd761624 2021-02-06 op if (loc->auto_index != 0) {
159 49b73ba1 2021-02-10 op if (matches(loc->match, path))
160 6016a593 2021-01-30 op return loc->auto_index == 1;
161 252908e6 2021-01-24 op }
162 252908e6 2021-01-24 op }
163 252908e6 2021-01-24 op
164 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
165 b8e64ccd 2021-03-31 op return loc->auto_index == 1;
166 6abda252 2021-02-06 op }
167 6abda252 2021-02-06 op
168 6abda252 2021-02-06 op int
169 6abda252 2021-02-06 op vhost_block_return(struct vhost *v, const char *path, int *code, const char **fmt)
170 6abda252 2021-02-06 op {
171 6abda252 2021-02-06 op struct location *loc;
172 6abda252 2021-02-06 op
173 6abda252 2021-02-06 op if (v == NULL || path == NULL)
174 6abda252 2021-02-06 op return 0;
175 6abda252 2021-02-06 op
176 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
177 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
178 cd761624 2021-02-06 op if (loc->block_code != 0) {
179 49b73ba1 2021-02-10 op if (matches(loc->match, path)) {
180 6abda252 2021-02-06 op *code = loc->block_code;
181 6abda252 2021-02-06 op *fmt = loc->block_fmt;
182 6abda252 2021-02-06 op return 1;
183 6abda252 2021-02-06 op }
184 6abda252 2021-02-06 op }
185 6abda252 2021-02-06 op }
186 6abda252 2021-02-06 op
187 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
188 b8e64ccd 2021-03-31 op *code = loc->block_code;
189 b8e64ccd 2021-03-31 op *fmt = loc->block_fmt;
190 b8e64ccd 2021-03-31 op return loc->block_code != 0;
191 72b033ef 2021-12-29 op }
192 72b033ef 2021-12-29 op
193 6abda252 2021-02-06 op int
194 8ad1c570 2021-05-09 op vhost_fastcgi(struct vhost *v, const char *path)
195 8ad1c570 2021-05-09 op {
196 8ad1c570 2021-05-09 op struct location *loc;
197 8ad1c570 2021-05-09 op
198 8ad1c570 2021-05-09 op if (v == NULL || path == NULL)
199 8ad1c570 2021-05-09 op return -1;
200 8ad1c570 2021-05-09 op
201 8ad1c570 2021-05-09 op loc = TAILQ_FIRST(&v->locations);
202 8ad1c570 2021-05-09 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
203 8ad1c570 2021-05-09 op if (loc->fcgi != -1)
204 8ad1c570 2021-05-09 op if (matches(loc->match, path))
205 8ad1c570 2021-05-09 op return loc->fcgi;
206 8ad1c570 2021-05-09 op }
207 8ad1c570 2021-05-09 op
208 8ad1c570 2021-05-09 op loc = TAILQ_FIRST(&v->locations);
209 8ad1c570 2021-05-09 op return loc->fcgi;
210 8ad1c570 2021-05-09 op }
211 8ad1c570 2021-05-09 op
212 8ad1c570 2021-05-09 op int
213 1feaf2a6 2021-05-15 op vhost_dirfd(struct vhost *v, const char *path, size_t *retloc)
214 fdea6aa0 2021-04-30 op {
215 fdea6aa0 2021-04-30 op struct location *loc;
216 1feaf2a6 2021-05-15 op size_t l = 0;
217 fdea6aa0 2021-04-30 op
218 fdea6aa0 2021-04-30 op if (v == NULL || path == NULL)
219 fdea6aa0 2021-04-30 op return -1;
220 fdea6aa0 2021-04-30 op
221 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
222 fdea6aa0 2021-04-30 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
223 1feaf2a6 2021-05-15 op l++;
224 fdea6aa0 2021-04-30 op if (loc->dirfd != -1)
225 1feaf2a6 2021-05-15 op if (matches(loc->match, path)) {
226 1feaf2a6 2021-05-15 op *retloc = l;
227 fdea6aa0 2021-04-30 op return loc->dirfd;
228 1feaf2a6 2021-05-15 op }
229 fdea6aa0 2021-04-30 op }
230 fdea6aa0 2021-04-30 op
231 1feaf2a6 2021-05-15 op *retloc = 0;
232 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
233 fdea6aa0 2021-04-30 op return loc->dirfd;
234 fdea6aa0 2021-04-30 op }
235 fdea6aa0 2021-04-30 op
236 fdea6aa0 2021-04-30 op int
237 6abda252 2021-02-06 op vhost_strip(struct vhost *v, const char *path)
238 6abda252 2021-02-06 op {
239 6abda252 2021-02-06 op struct location *loc;
240 6abda252 2021-02-06 op
241 6abda252 2021-02-06 op if (v == NULL || path == NULL)
242 6abda252 2021-02-06 op return 0;
243 6abda252 2021-02-06 op
244 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
245 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
246 cd761624 2021-02-06 op if (loc->strip != 0) {
247 49b73ba1 2021-02-10 op if (matches(loc->match, path))
248 6abda252 2021-02-06 op return loc->strip;
249 6abda252 2021-02-06 op }
250 6abda252 2021-02-06 op }
251 6abda252 2021-02-06 op
252 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
253 b8e64ccd 2021-03-31 op return loc->strip;
254 252908e6 2021-01-24 op }
255 252908e6 2021-01-24 op
256 02be96c6 2021-02-09 op X509_STORE *
257 02be96c6 2021-02-09 op vhost_require_ca(struct vhost *v, const char *path)
258 02be96c6 2021-02-09 op {
259 02be96c6 2021-02-09 op struct location *loc;
260 02be96c6 2021-02-09 op
261 02be96c6 2021-02-09 op if (v == NULL || path == NULL)
262 02be96c6 2021-02-09 op return NULL;
263 02be96c6 2021-02-09 op
264 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
265 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
266 02be96c6 2021-02-09 op if (loc->reqca != NULL) {
267 49b73ba1 2021-02-10 op if (matches(loc->match, path))
268 02be96c6 2021-02-09 op return loc->reqca;
269 02be96c6 2021-02-09 op }
270 02be96c6 2021-02-09 op }
271 02be96c6 2021-02-09 op
272 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
273 b8e64ccd 2021-03-31 op return loc->reqca;
274 793835cb 2021-02-23 op }
275 793835cb 2021-02-23 op
276 793835cb 2021-02-23 op int
277 793835cb 2021-02-23 op vhost_disable_log(struct vhost *v, const char *path)
278 793835cb 2021-02-23 op {
279 793835cb 2021-02-23 op struct location *loc;
280 793835cb 2021-02-23 op
281 793835cb 2021-02-23 op if (v == NULL || path == NULL)
282 793835cb 2021-02-23 op return 0;
283 793835cb 2021-02-23 op
284 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
285 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
286 793835cb 2021-02-23 op if (loc->disable_log && matches(loc->match, path))
287 793835cb 2021-02-23 op return 1;
288 793835cb 2021-02-23 op }
289 793835cb 2021-02-23 op
290 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
291 b8e64ccd 2021-03-31 op return loc->disable_log;
292 02be96c6 2021-02-09 op }
293 02be96c6 2021-02-09 op
294 fe406389 2021-02-02 op static int
295 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
296 d3a08f4d 2021-01-17 op {
297 d3a08f4d 2021-01-17 op struct stat sb;
298 d1ca3911 2021-01-21 op const char *p;
299 efb48052 2021-07-27 op int dirfd, strip;
300 d3a08f4d 2021-01-17 op
301 d3a08f4d 2021-01-17 op assert(path != NULL);
302 d1ca3911 2021-01-21 op
303 fdea6aa0 2021-04-30 op /*
304 fdea6aa0 2021-04-30 op * in send_dir we add an initial / (to be redirect-friendly),
305 fdea6aa0 2021-04-30 op * but here we want to skip it
306 fdea6aa0 2021-04-30 op */
307 fdea6aa0 2021-04-30 op if (*path == '/')
308 fdea6aa0 2021-04-30 op path++;
309 fdea6aa0 2021-04-30 op
310 fdea6aa0 2021-04-30 op strip = vhost_strip(c->host, path);
311 fdea6aa0 2021-04-30 op p = strip_path(path, strip);
312 fdea6aa0 2021-04-30 op
313 fdea6aa0 2021-04-30 op if (*p == '/')
314 fdea6aa0 2021-04-30 op p = p+1;
315 fdea6aa0 2021-04-30 op if (*p == '\0')
316 d1ca3911 2021-01-21 op p = ".";
317 d1ca3911 2021-01-21 op
318 1feaf2a6 2021-05-15 op dirfd = vhost_dirfd(c->host, path, &c->loc);
319 fdea6aa0 2021-04-30 op log_debug(c, "check_path: strip=%d path=%s original=%s",
320 fdea6aa0 2021-04-30 op strip, p, path);
321 3bd4a6de 2022-07-04 op if (*fd == -1 && (*fd = openat(dirfd, p, O_RDONLY)) == -1) {
322 3bd4a6de 2022-07-04 op if (errno == EACCES)
323 3bd4a6de 2022-07-04 op log_info(c, "can't open %s: %s", p, strerror(errno));
324 d3a08f4d 2021-01-17 op return FILE_MISSING;
325 3bd4a6de 2022-07-04 op }
326 d3a08f4d 2021-01-17 op
327 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
328 3abf91b0 2021-02-07 op log_notice(c, "failed stat for %s: %s", path, strerror(errno));
329 d3a08f4d 2021-01-17 op return FILE_MISSING;
330 d3a08f4d 2021-01-17 op }
331 d3a08f4d 2021-01-17 op
332 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
333 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
334 d3a08f4d 2021-01-17 op
335 d3a08f4d 2021-01-17 op return FILE_EXISTS;
336 d3a08f4d 2021-01-17 op }
337 d3a08f4d 2021-01-17 op
338 fe406389 2021-02-02 op static void
339 abc007d2 2021-02-08 op open_file(struct client *c)
340 d3a08f4d 2021-01-17 op {
341 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
342 d3a08f4d 2021-01-17 op case FILE_EXISTS:
343 efe7d180 2021-10-02 op c->type = REQUEST_FILE;
344 27b2fa9a 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
345 07b0a142 2021-01-24 op return;
346 d3a08f4d 2021-01-17 op
347 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
348 abc007d2 2021-02-08 op open_dir(c);
349 07b0a142 2021-01-24 op return;
350 d3a08f4d 2021-01-17 op
351 d3a08f4d 2021-01-17 op case FILE_MISSING:
352 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
353 07b0a142 2021-01-24 op return;
354 d3a08f4d 2021-01-17 op
355 d3a08f4d 2021-01-17 op default:
356 d3a08f4d 2021-01-17 op /* unreachable */
357 d3a08f4d 2021-01-17 op abort();
358 252908e6 2021-01-24 op }
359 252908e6 2021-01-24 op }
360 252908e6 2021-01-24 op
361 9b8f5ed2 2021-02-03 op void
362 9b8f5ed2 2021-02-03 op mark_nonblock(int fd)
363 9b8f5ed2 2021-02-03 op {
364 9b8f5ed2 2021-02-03 op int flags;
365 9b8f5ed2 2021-02-03 op
366 9b8f5ed2 2021-02-03 op if ((flags = fcntl(fd, F_GETFL)) == -1)
367 9b8f5ed2 2021-02-03 op fatal("fcntl(F_GETFL): %s", strerror(errno));
368 9b8f5ed2 2021-02-03 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
369 9b8f5ed2 2021-02-03 op fatal("fcntl(F_SETFL): %s", strerror(errno));
370 9b8f5ed2 2021-02-03 op }
371 9b8f5ed2 2021-02-03 op
372 fe406389 2021-02-02 op static void
373 abc007d2 2021-02-08 op handle_handshake(int fd, short ev, void *d)
374 d3a08f4d 2021-01-17 op {
375 abc007d2 2021-02-08 op struct client *c = d;
376 d3a08f4d 2021-01-17 op struct vhost *h;
377 cc8c2901 2021-04-29 op struct alist *a;
378 d3a08f4d 2021-01-17 op const char *servname;
379 a8d4a897 2021-01-29 op const char *parse_err = "unknown error";
380 d3a08f4d 2021-01-17 op
381 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
382 d3a08f4d 2021-01-17 op case 0: /* success */
383 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
384 d3a08f4d 2021-01-17 op break;
385 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
386 efe7d180 2021-10-02 op event_once(c->fd, EV_READ, handle_handshake, c, NULL);
387 d3a08f4d 2021-01-17 op return;
388 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
389 efe7d180 2021-10-02 op event_once(c->fd, EV_WRITE, handle_handshake, c, NULL);
390 d3a08f4d 2021-01-17 op return;
391 d3a08f4d 2021-01-17 op default:
392 d3a08f4d 2021-01-17 op /* unreachable */
393 d3a08f4d 2021-01-17 op abort();
394 d3a08f4d 2021-01-17 op }
395 80444938 2021-10-15 op
396 80444938 2021-10-15 op c->bev = bufferevent_new(fd, client_read, client_write,
397 80444938 2021-10-15 op client_error, c);
398 80444938 2021-10-15 op if (c->bev == NULL)
399 80444938 2021-10-15 op fatal("%s: failed to allocate client buffer: %s",
400 80444938 2021-10-15 op __func__, strerror(errno));
401 d3a08f4d 2021-01-17 op
402 80444938 2021-10-15 op event_set(&c->bev->ev_read, c->fd, EV_READ,
403 80444938 2021-10-15 op client_tls_readcb, c->bev);
404 80444938 2021-10-15 op event_set(&c->bev->ev_write, c->fd, EV_WRITE,
405 80444938 2021-10-15 op client_tls_writecb, c->bev);
406 80444938 2021-10-15 op
407 80444938 2021-10-15 op #if HAVE_LIBEVENT2
408 80444938 2021-10-15 op evbuffer_unfreeze(c->bev->input, 0);
409 80444938 2021-10-15 op evbuffer_unfreeze(c->bev->output, 1);
410 80444938 2021-10-15 op #endif
411 80444938 2021-10-15 op
412 79288c8b 2021-09-24 op if ((servname = tls_conn_servername(c->ctx)) == NULL) {
413 79288c8b 2021-09-24 op log_debug(c, "handshake: missing SNI");
414 79288c8b 2021-09-24 op goto err;
415 79288c8b 2021-09-24 op }
416 79288c8b 2021-09-24 op
417 a8d4a897 2021-01-29 op if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
418 3abf91b0 2021-02-07 op log_info(c, "puny_decode: %s", parse_err);
419 a8d4a897 2021-01-29 op goto err;
420 a8d4a897 2021-01-29 op }
421 d3a08f4d 2021-01-17 op
422 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
423 eecad7a3 2021-02-12 op if (matches(h->domain, c->domain))
424 cc8c2901 2021-04-29 op goto found;
425 cc8c2901 2021-04-29 op TAILQ_FOREACH(a, &h->aliases, aliases) {
426 cc8c2901 2021-04-29 op if (matches(a->alias, c->domain))
427 cc8c2901 2021-04-29 op goto found;
428 cc8c2901 2021-04-29 op }
429 d3a08f4d 2021-01-17 op }
430 d3a08f4d 2021-01-17 op
431 cc8c2901 2021-04-29 op found:
432 3abf91b0 2021-02-07 op log_debug(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
433 3abf91b0 2021-02-07 op servname != NULL ? servname : "(null)",
434 3abf91b0 2021-02-07 op c->domain,
435 b8e64ccd 2021-03-31 op h != NULL ? h->domain : "(null)");
436 22c6d633 2021-01-27 op
437 b8e64ccd 2021-03-31 op if (h != NULL) {
438 d3a08f4d 2021-01-17 op c->host = h;
439 efe7d180 2021-10-02 op bufferevent_enable(c->bev, EV_READ);
440 d3a08f4d 2021-01-17 op return;
441 d3a08f4d 2021-01-17 op }
442 d3a08f4d 2021-01-17 op
443 a8d4a897 2021-01-29 op err:
444 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
445 6abda252 2021-02-06 op }
446 6abda252 2021-02-06 op
447 91b9f2a8 2021-05-15 op static const char *
448 91b9f2a8 2021-05-15 op strip_path(const char *path, int strip)
449 6abda252 2021-02-06 op {
450 57ec3e77 2021-02-08 op char *t;
451 6abda252 2021-02-06 op
452 6abda252 2021-02-06 op while (strip > 0) {
453 6abda252 2021-02-06 op if ((t = strchr(path, '/')) == NULL) {
454 6abda252 2021-02-06 op path = strchr(path, '\0');
455 6abda252 2021-02-06 op break;
456 6abda252 2021-02-06 op }
457 6abda252 2021-02-06 op path = t;
458 6abda252 2021-02-06 op strip--;
459 6abda252 2021-02-06 op }
460 57ec3e77 2021-02-08 op
461 57ec3e77 2021-02-08 op return path;
462 57ec3e77 2021-02-08 op }
463 57ec3e77 2021-02-08 op
464 57ec3e77 2021-02-08 op static void
465 57ec3e77 2021-02-08 op fmt_sbuf(const char *fmt, struct client *c, const char *path)
466 57ec3e77 2021-02-08 op {
467 57ec3e77 2021-02-08 op size_t i;
468 4842c72d 2021-10-18 op char buf[32];
469 6abda252 2021-02-06 op
470 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
471 6abda252 2021-02-06 op for (i = 0; *fmt; ++fmt) {
472 6abda252 2021-02-06 op if (i == sizeof(buf)-1 || *fmt == '%') {
473 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
474 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
475 6abda252 2021-02-06 op i = 0;
476 6abda252 2021-02-06 op }
477 6abda252 2021-02-06 op
478 6abda252 2021-02-06 op if (*fmt != '%') {
479 6abda252 2021-02-06 op buf[i++] = *fmt;
480 6abda252 2021-02-06 op continue;
481 6abda252 2021-02-06 op }
482 6abda252 2021-02-06 op
483 6abda252 2021-02-06 op switch (*++fmt) {
484 6abda252 2021-02-06 op case '%':
485 6abda252 2021-02-06 op strlcat(c->sbuf, "%", sizeof(c->sbuf));
486 6abda252 2021-02-06 op break;
487 6abda252 2021-02-06 op case 'p':
488 737a6b50 2021-04-30 op if (*path != '/')
489 737a6b50 2021-04-30 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
490 6abda252 2021-02-06 op strlcat(c->sbuf, path, sizeof(c->sbuf));
491 6abda252 2021-02-06 op break;
492 6abda252 2021-02-06 op case 'q':
493 6abda252 2021-02-06 op strlcat(c->sbuf, c->iri.query, sizeof(c->sbuf));
494 6abda252 2021-02-06 op break;
495 6abda252 2021-02-06 op case 'P':
496 6abda252 2021-02-06 op snprintf(buf, sizeof(buf), "%d", conf.port);
497 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
498 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
499 6abda252 2021-02-06 op break;
500 6abda252 2021-02-06 op case 'N':
501 6abda252 2021-02-06 op strlcat(c->sbuf, c->domain, sizeof(c->sbuf));
502 6abda252 2021-02-06 op break;
503 6abda252 2021-02-06 op default:
504 6abda252 2021-02-06 op fatal("%s: unknown fmt specifier %c",
505 6abda252 2021-02-06 op __func__, *fmt);
506 6abda252 2021-02-06 op }
507 6abda252 2021-02-06 op }
508 6abda252 2021-02-06 op
509 6abda252 2021-02-06 op if (i != 0)
510 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
511 57ec3e77 2021-02-08 op }
512 6abda252 2021-02-06 op
513 57ec3e77 2021-02-08 op /* 1 if a matching `block return' (and apply it), 0 otherwise */
514 57ec3e77 2021-02-08 op static int
515 57ec3e77 2021-02-08 op apply_block_return(struct client *c)
516 57ec3e77 2021-02-08 op {
517 57ec3e77 2021-02-08 op const char *fmt, *path;
518 57ec3e77 2021-02-08 op int code;
519 57ec3e77 2021-02-08 op
520 57ec3e77 2021-02-08 op if (!vhost_block_return(c->host, c->iri.path, &code, &fmt))
521 57ec3e77 2021-02-08 op return 0;
522 57ec3e77 2021-02-08 op
523 57ec3e77 2021-02-08 op path = strip_path(c->iri.path, vhost_strip(c->host, c->iri.path));
524 57ec3e77 2021-02-08 op fmt_sbuf(fmt, c, path);
525 57ec3e77 2021-02-08 op
526 abc007d2 2021-02-08 op start_reply(c, code, c->sbuf);
527 72b033ef 2021-12-29 op return 1;
528 b7967bc1 2021-01-02 op }
529 b7967bc1 2021-01-02 op
530 b7967bc1 2021-01-02 op static struct proxy *
531 b7967bc1 2021-01-02 op matched_proxy(struct client *c)
532 b7967bc1 2021-01-02 op {
533 b7967bc1 2021-01-02 op struct proxy *p;
534 b7967bc1 2021-01-02 op const char *proto;
535 b7967bc1 2021-01-02 op const char *host;
536 b7967bc1 2021-01-02 op const char *port;
537 b7967bc1 2021-01-02 op
538 b7967bc1 2021-01-02 op TAILQ_FOREACH(p, &c->host->proxies, proxies) {
539 b7967bc1 2021-01-02 op if ((proto = p->match_proto) == NULL)
540 b7967bc1 2021-01-02 op proto = "gemini";
541 b7967bc1 2021-01-02 op if ((host = p->match_host) == NULL)
542 b7967bc1 2021-01-02 op host = "*";
543 b7967bc1 2021-01-02 op if ((port = p->match_port) == NULL)
544 b7967bc1 2021-01-02 op port = "*";
545 b7967bc1 2021-01-02 op
546 b7967bc1 2021-01-02 op if (matches(proto, c->iri.schema) &&
547 b7967bc1 2021-01-02 op matches(host, c->domain) &&
548 b7967bc1 2021-01-02 op matches(port, c->iri.port))
549 b7967bc1 2021-01-02 op return p;
550 b7967bc1 2021-01-02 op }
551 b7967bc1 2021-01-02 op
552 b7967bc1 2021-01-02 op return NULL;
553 ba94a608 2022-01-04 op }
554 ba94a608 2022-01-04 op
555 ba94a608 2022-01-04 op static int
556 ba94a608 2022-01-04 op check_matching_certificate(X509_STORE *store, struct client *c)
557 ba94a608 2022-01-04 op {
558 ba94a608 2022-01-04 op const uint8_t *cert;
559 ba94a608 2022-01-04 op size_t len;
560 ba94a608 2022-01-04 op
561 ba94a608 2022-01-04 op if (!tls_peer_cert_provided(c->ctx)) {
562 ba94a608 2022-01-04 op start_reply(c, CLIENT_CERT_REQ, "client certificate required");
563 ba94a608 2022-01-04 op return 1;
564 ba94a608 2022-01-04 op }
565 ba94a608 2022-01-04 op
566 ba94a608 2022-01-04 op cert = tls_peer_cert_chain_pem(c->ctx, &len);
567 ba94a608 2022-01-04 op if (!validate_against_ca(store, cert, len)) {
568 ba94a608 2022-01-04 op start_reply(c, CERT_NOT_AUTH, "certificate not authorised");
569 ba94a608 2022-01-04 op return 1;
570 ba94a608 2022-01-04 op }
571 ba94a608 2022-01-04 op
572 ba94a608 2022-01-04 op return 0;
573 72b033ef 2021-12-29 op }
574 72b033ef 2021-12-29 op
575 d29a2ee2 2022-09-06 op static int
576 d29a2ee2 2022-09-06 op proxy_socket(struct client *c, const char *host, const char *port)
577 d29a2ee2 2022-09-06 op {
578 d29a2ee2 2022-09-06 op struct addrinfo hints, *res, *res0;
579 d29a2ee2 2022-09-06 op int r, sock;
580 d29a2ee2 2022-09-06 op
581 d29a2ee2 2022-09-06 op memset(&hints, 0, sizeof(hints));
582 d29a2ee2 2022-09-06 op hints.ai_family = AF_UNSPEC;
583 d29a2ee2 2022-09-06 op hints.ai_socktype = SOCK_STREAM;
584 d29a2ee2 2022-09-06 op
585 d29a2ee2 2022-09-06 op /* XXX: asr_run? :> */
586 d29a2ee2 2022-09-06 op r = getaddrinfo(host, port, &hints, &res0);
587 d29a2ee2 2022-09-06 op if (r != 0) {
588 d29a2ee2 2022-09-06 op log_warn(c, "getaddrinfo(\"%s\", \"%s\"): %s",
589 d29a2ee2 2022-09-06 op host, port, gai_strerror(r));
590 d29a2ee2 2022-09-06 op return -1;
591 d29a2ee2 2022-09-06 op }
592 d29a2ee2 2022-09-06 op
593 d29a2ee2 2022-09-06 op for (res = res0; res; res = res->ai_next) {
594 d29a2ee2 2022-09-06 op sock = socket(res->ai_family, res->ai_socktype,
595 d29a2ee2 2022-09-06 op res->ai_protocol);
596 d29a2ee2 2022-09-06 op if (sock == -1)
597 d29a2ee2 2022-09-06 op continue;
598 d29a2ee2 2022-09-06 op
599 d29a2ee2 2022-09-06 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
600 d29a2ee2 2022-09-06 op close(sock);
601 d29a2ee2 2022-09-06 op sock = -1;
602 d29a2ee2 2022-09-06 op continue;
603 d29a2ee2 2022-09-06 op }
604 d29a2ee2 2022-09-06 op
605 d29a2ee2 2022-09-06 op break;
606 d29a2ee2 2022-09-06 op }
607 d29a2ee2 2022-09-06 op
608 d29a2ee2 2022-09-06 op freeaddrinfo(res0);
609 d29a2ee2 2022-09-06 op
610 d29a2ee2 2022-09-06 op if (sock == -1)
611 d29a2ee2 2022-09-06 op log_warn(c, "can't connect to %s:%s", host, port);
612 d29a2ee2 2022-09-06 op
613 d29a2ee2 2022-09-06 op return sock;
614 d29a2ee2 2022-09-06 op }
615 d29a2ee2 2022-09-06 op
616 72b033ef 2021-12-29 op /* 1 if matching a proxy relay-to (and apply it), 0 otherwise */
617 72b033ef 2021-12-29 op static int
618 72b033ef 2021-12-29 op apply_reverse_proxy(struct client *c)
619 72b033ef 2021-12-29 op {
620 7bdcc91e 2021-01-01 op struct proxy *p;
621 72b033ef 2021-12-29 op
622 b7967bc1 2021-01-02 op if ((p = matched_proxy(c)) == NULL)
623 72b033ef 2021-12-29 op return 0;
624 d49093c1 2021-01-01 op
625 b7967bc1 2021-01-02 op c->proxy = p;
626 b7967bc1 2021-01-02 op
627 ba94a608 2022-01-04 op if (p->reqca != NULL && check_matching_certificate(p->reqca, c))
628 ba94a608 2022-01-04 op return 1;
629 ba94a608 2022-01-04 op
630 72b033ef 2021-12-29 op log_debug(c, "opening proxy connection for %s:%s",
631 7bdcc91e 2021-01-01 op p->host, p->port);
632 7bdcc91e 2021-01-01 op
633 d29a2ee2 2022-09-06 op if ((c->pfd = proxy_socket(c, p->host, p->port)) == -1) {
634 d29a2ee2 2022-09-06 op start_reply(c, PROXY_ERROR, "proxy error");
635 d29a2ee2 2022-09-06 op return 1;
636 d29a2ee2 2022-09-06 op }
637 72b033ef 2021-12-29 op
638 d29a2ee2 2022-09-06 op mark_nonblock(c->pfd);
639 d29a2ee2 2022-09-06 op if (proxy_init(c) == -1)
640 d29a2ee2 2022-09-06 op start_reply(c, PROXY_ERROR, "proxy error");
641 72b033ef 2021-12-29 op
642 8ad1c570 2021-05-09 op return 1;
643 8ad1c570 2021-05-09 op }
644 8ad1c570 2021-05-09 op
645 d29a2ee2 2022-09-06 op static int
646 d29a2ee2 2022-09-06 op fcgi_open_sock(struct fcgi *f)
647 d29a2ee2 2022-09-06 op {
648 d29a2ee2 2022-09-06 op struct sockaddr_un addr;
649 d29a2ee2 2022-09-06 op int fd;
650 d29a2ee2 2022-09-06 op
651 d29a2ee2 2022-09-06 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
652 d29a2ee2 2022-09-06 op log_err(NULL, "socket: %s", strerror(errno));
653 d29a2ee2 2022-09-06 op return -1;
654 d29a2ee2 2022-09-06 op }
655 d29a2ee2 2022-09-06 op
656 d29a2ee2 2022-09-06 op memset(&addr, 0, sizeof(addr));
657 d29a2ee2 2022-09-06 op addr.sun_family = AF_UNIX;
658 d29a2ee2 2022-09-06 op strlcpy(addr.sun_path, f->path, sizeof(addr.sun_path));
659 d29a2ee2 2022-09-06 op
660 d29a2ee2 2022-09-06 op if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
661 d29a2ee2 2022-09-06 op log_warn(NULL, "failed to connect to %s: %s", f->path,
662 d29a2ee2 2022-09-06 op strerror(errno));
663 d29a2ee2 2022-09-06 op close(fd);
664 d29a2ee2 2022-09-06 op return -1;
665 d29a2ee2 2022-09-06 op }
666 d29a2ee2 2022-09-06 op
667 d29a2ee2 2022-09-06 op return fd;
668 d29a2ee2 2022-09-06 op }
669 d29a2ee2 2022-09-06 op
670 d29a2ee2 2022-09-06 op static int
671 d29a2ee2 2022-09-06 op fcgi_open_conn(struct fcgi *f)
672 d29a2ee2 2022-09-06 op {
673 d29a2ee2 2022-09-06 op struct addrinfo hints, *servinfo, *p;
674 d29a2ee2 2022-09-06 op int r, sock;
675 d29a2ee2 2022-09-06 op
676 d29a2ee2 2022-09-06 op memset(&hints, 0, sizeof(hints));
677 d29a2ee2 2022-09-06 op hints.ai_family = AF_UNSPEC;
678 d29a2ee2 2022-09-06 op hints.ai_socktype = SOCK_STREAM;
679 d29a2ee2 2022-09-06 op hints.ai_flags = AI_ADDRCONFIG;
680 d29a2ee2 2022-09-06 op
681 d29a2ee2 2022-09-06 op if ((r = getaddrinfo(f->path, f->port, &hints, &servinfo)) != 0) {
682 d29a2ee2 2022-09-06 op log_warn(NULL, "getaddrinfo %s:%s: %s", f->path, f->port,
683 d29a2ee2 2022-09-06 op gai_strerror(r));
684 d29a2ee2 2022-09-06 op return -1;
685 d29a2ee2 2022-09-06 op }
686 d29a2ee2 2022-09-06 op
687 d29a2ee2 2022-09-06 op for (p = servinfo; p != NULL; p = p->ai_next) {
688 d29a2ee2 2022-09-06 op sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
689 d29a2ee2 2022-09-06 op if (sock == -1)
690 d29a2ee2 2022-09-06 op continue;
691 d29a2ee2 2022-09-06 op if (connect(sock, p->ai_addr, p->ai_addrlen) == -1) {
692 d29a2ee2 2022-09-06 op close(sock);
693 d29a2ee2 2022-09-06 op continue;
694 d29a2ee2 2022-09-06 op }
695 d29a2ee2 2022-09-06 op break;
696 d29a2ee2 2022-09-06 op }
697 d29a2ee2 2022-09-06 op
698 d29a2ee2 2022-09-06 op if (p == NULL) {
699 d29a2ee2 2022-09-06 op log_warn(NULL, "couldn't connect to %s:%s", f->path, f->port);
700 d29a2ee2 2022-09-06 op sock = -1;
701 d29a2ee2 2022-09-06 op }
702 d29a2ee2 2022-09-06 op
703 d29a2ee2 2022-09-06 op freeaddrinfo(servinfo);
704 d29a2ee2 2022-09-06 op return sock;
705 d29a2ee2 2022-09-06 op }
706 d29a2ee2 2022-09-06 op
707 8ad1c570 2021-05-09 op /* 1 if matching `fcgi' (and apply it), 0 otherwise */
708 8ad1c570 2021-05-09 op static int
709 8ad1c570 2021-05-09 op apply_fastcgi(struct client *c)
710 8ad1c570 2021-05-09 op {
711 8ad1c570 2021-05-09 op int id;
712 8ad1c570 2021-05-09 op struct fcgi *f;
713 8ad1c570 2021-05-09 op
714 8ad1c570 2021-05-09 op if ((id = vhost_fastcgi(c->host, c->iri.path)) == -1)
715 8ad1c570 2021-05-09 op return 0;
716 8ad1c570 2021-05-09 op
717 4cd25209 2021-10-07 op f = &fcgi[id];
718 4cd25209 2021-10-07 op
719 d29a2ee2 2022-09-06 op log_debug(c, "opening fastcgi connection for (%s,%s)",
720 d29a2ee2 2022-09-06 op f->path, f->port);
721 8ad1c570 2021-05-09 op
722 d29a2ee2 2022-09-06 op if (f->port != NULL)
723 d29a2ee2 2022-09-06 op c->pfd = fcgi_open_sock(f);
724 d29a2ee2 2022-09-06 op else
725 d29a2ee2 2022-09-06 op c->pfd = fcgi_open_conn(f);
726 d29a2ee2 2022-09-06 op
727 d29a2ee2 2022-09-06 op if (c->pfd == -1) {
728 d29a2ee2 2022-09-06 op start_reply(c, CGI_ERROR, "CGI error");
729 d29a2ee2 2022-09-06 op return 1;
730 d29a2ee2 2022-09-06 op }
731 d29a2ee2 2022-09-06 op
732 d29a2ee2 2022-09-06 op mark_nonblock(c->pfd);
733 d29a2ee2 2022-09-06 op
734 d29a2ee2 2022-09-06 op c->cgibev = bufferevent_new(c->pfd, fcgi_read, fcgi_write,
735 d29a2ee2 2022-09-06 op fcgi_error, c);
736 d29a2ee2 2022-09-06 op if (c->cgibev == NULL) {
737 d29a2ee2 2022-09-06 op start_reply(c, TEMP_FAILURE, "internal server error");
738 d29a2ee2 2022-09-06 op return 1;
739 d29a2ee2 2022-09-06 op }
740 d29a2ee2 2022-09-06 op
741 d29a2ee2 2022-09-06 op bufferevent_enable(c->cgibev, EV_READ|EV_WRITE);
742 d29a2ee2 2022-09-06 op fcgi_req(c);
743 d29a2ee2 2022-09-06 op
744 6abda252 2021-02-06 op return 1;
745 d3a08f4d 2021-01-17 op }
746 d3a08f4d 2021-01-17 op
747 02be96c6 2021-02-09 op /* 1 if matching `require client ca' fails (and apply it), 0 otherwise */
748 02be96c6 2021-02-09 op static int
749 02be96c6 2021-02-09 op apply_require_ca(struct client *c)
750 02be96c6 2021-02-09 op {
751 02be96c6 2021-02-09 op X509_STORE *store;
752 02be96c6 2021-02-09 op
753 02be96c6 2021-02-09 op if ((store = vhost_require_ca(c->host, c->iri.path)) == NULL)
754 02be96c6 2021-02-09 op return 0;
755 ba94a608 2022-01-04 op return check_matching_certificate(store, c);
756 02be96c6 2021-02-09 op }
757 02be96c6 2021-02-09 op
758 fe406389 2021-02-02 op static void
759 abc007d2 2021-02-08 op open_dir(struct client *c)
760 d3a08f4d 2021-01-17 op {
761 d3a08f4d 2021-01-17 op size_t len;
762 11c98667 2021-04-25 op int dirfd, root;
763 252908e6 2021-01-24 op char *before_file;
764 11c98667 2021-04-25 op
765 d29a2ee2 2022-09-06 op log_debug(c, "in open_dir");
766 d29a2ee2 2022-09-06 op
767 11c98667 2021-04-25 op root = !strcmp(c->iri.path, "/") || *c->iri.path == '\0';
768 d1ca3911 2021-01-21 op
769 0be51733 2021-01-20 op len = strlen(c->iri.path);
770 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
771 abc007d2 2021-02-08 op redirect_canonical_dir(c);
772 0be51733 2021-01-20 op return;
773 d3a08f4d 2021-01-17 op }
774 d3a08f4d 2021-01-17 op
775 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
776 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
777 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
778 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
779 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
780 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
781 c8b74339 2021-01-24 op sizeof(c->sbuf));
782 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
783 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
784 252908e6 2021-01-24 op return;
785 252908e6 2021-01-24 op }
786 d3a08f4d 2021-01-17 op
787 252908e6 2021-01-24 op c->iri.path = c->sbuf;
788 252908e6 2021-01-24 op
789 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
790 abc007d2 2021-02-08 op dirfd = c->pfd;
791 abc007d2 2021-02-08 op c->pfd = -1;
792 252908e6 2021-01-24 op
793 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
794 252908e6 2021-01-24 op case FILE_EXISTS:
795 efe7d180 2021-10-02 op c->type = REQUEST_FILE;
796 98ee8406 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
797 252908e6 2021-01-24 op break;
798 252908e6 2021-01-24 op
799 252908e6 2021-01-24 op case FILE_DIRECTORY:
800 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
801 252908e6 2021-01-24 op break;
802 252908e6 2021-01-24 op
803 252908e6 2021-01-24 op case FILE_MISSING:
804 252908e6 2021-01-24 op *before_file = '\0';
805 252908e6 2021-01-24 op
806 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
807 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
808 252908e6 2021-01-24 op break;
809 252908e6 2021-01-24 op }
810 252908e6 2021-01-24 op
811 efe7d180 2021-10-02 op c->type = REQUEST_DIR;
812 252908e6 2021-01-24 op
813 e76f2c74 2021-04-25 op c->dirlen = scandir_fd(dirfd, &c->dir,
814 11c98667 2021-04-25 op root ? select_non_dotdot : select_non_dot,
815 11c98667 2021-04-25 op alphasort);
816 11c98667 2021-04-25 op if (c->dirlen == -1) {
817 11c98667 2021-04-25 op log_err(c, "scandir_fd(%d) (vhost:%s) %s: %s",
818 abc007d2 2021-02-08 op c->pfd, c->host->domain, c->iri.path, strerror(errno));
819 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
820 252908e6 2021-01-24 op return;
821 252908e6 2021-01-24 op }
822 11c98667 2021-04-25 op c->diroff = 0;
823 252908e6 2021-01-24 op c->off = 0;
824 252908e6 2021-01-24 op
825 4842c72d 2021-10-18 op start_reply(c, SUCCESS, "text/gemini");
826 efe7d180 2021-10-02 op evbuffer_add_printf(EVBUFFER_OUTPUT(c->bev),
827 efe7d180 2021-10-02 op "# Index of %s\n\n", c->iri.path);
828 252908e6 2021-01-24 op return;
829 252908e6 2021-01-24 op
830 252908e6 2021-01-24 op default:
831 252908e6 2021-01-24 op /* unreachable */
832 252908e6 2021-01-24 op abort();
833 252908e6 2021-01-24 op }
834 252908e6 2021-01-24 op
835 252908e6 2021-01-24 op close(dirfd);
836 252908e6 2021-01-24 op }
837 252908e6 2021-01-24 op
838 fe406389 2021-02-02 op static void
839 abc007d2 2021-02-08 op redirect_canonical_dir(struct client *c)
840 252908e6 2021-01-24 op {
841 252908e6 2021-01-24 op size_t len;
842 252908e6 2021-01-24 op
843 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
844 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
845 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
846 252908e6 2021-01-24 op
847 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
848 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
849 0be51733 2021-01-20 op return;
850 0be51733 2021-01-20 op }
851 0be51733 2021-01-20 op
852 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
853 d3a08f4d 2021-01-17 op }
854 5f715ce4 2021-02-02 op
855 fe406389 2021-02-02 op static void
856 efe7d180 2021-10-02 op client_tls_readcb(int fd, short event, void *d)
857 5f715ce4 2021-02-02 op {
858 efe7d180 2021-10-02 op struct bufferevent *bufev = d;
859 efe7d180 2021-10-02 op struct client *client = bufev->cbarg;
860 efe7d180 2021-10-02 op ssize_t ret;
861 efe7d180 2021-10-02 op size_t len;
862 efe7d180 2021-10-02 op int what = EVBUFFER_READ;
863 efe7d180 2021-10-02 op int howmuch = IBUF_READ_SIZE;
864 efe7d180 2021-10-02 op char buf[IBUF_READ_SIZE];
865 d3a08f4d 2021-01-17 op
866 efe7d180 2021-10-02 op if (event == EV_TIMEOUT) {
867 efe7d180 2021-10-02 op what |= EVBUFFER_TIMEOUT;
868 efe7d180 2021-10-02 op goto err;
869 5f715ce4 2021-02-02 op }
870 5f715ce4 2021-02-02 op
871 efe7d180 2021-10-02 op if (bufev->wm_read.high != 0)
872 efe7d180 2021-10-02 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
873 5f715ce4 2021-02-02 op
874 efe7d180 2021-10-02 op switch (ret = tls_read(client->ctx, buf, howmuch)) {
875 efe7d180 2021-10-02 op case TLS_WANT_POLLIN:
876 efe7d180 2021-10-02 op case TLS_WANT_POLLOUT:
877 efe7d180 2021-10-02 op goto retry;
878 efe7d180 2021-10-02 op case -1:
879 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
880 efe7d180 2021-10-02 op goto err;
881 efe7d180 2021-10-02 op }
882 efe7d180 2021-10-02 op len = ret;
883 5f715ce4 2021-02-02 op
884 efe7d180 2021-10-02 op if (len == 0) {
885 efe7d180 2021-10-02 op what |= EVBUFFER_EOF;
886 efe7d180 2021-10-02 op goto err;
887 5f715ce4 2021-02-02 op }
888 5f715ce4 2021-02-02 op
889 efe7d180 2021-10-02 op if (evbuffer_add(bufev->input, buf, len) == -1) {
890 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
891 efe7d180 2021-10-02 op goto err;
892 efe7d180 2021-10-02 op }
893 5f715ce4 2021-02-02 op
894 efe7d180 2021-10-02 op event_add(&bufev->ev_read, NULL);
895 efe7d180 2021-10-02 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
896 efe7d180 2021-10-02 op return;
897 efe7d180 2021-10-02 op if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
898 efe7d180 2021-10-02 op /*
899 efe7d180 2021-10-02 op * here we could implement a read pressure policy.
900 efe7d180 2021-10-02 op */
901 efe7d180 2021-10-02 op }
902 252908e6 2021-01-24 op
903 efe7d180 2021-10-02 op if (bufev->readcb != NULL)
904 efe7d180 2021-10-02 op (*bufev->readcb)(bufev, bufev->cbarg);
905 11c98667 2021-04-25 op
906 efe7d180 2021-10-02 op return;
907 11c98667 2021-04-25 op
908 efe7d180 2021-10-02 op retry:
909 efe7d180 2021-10-02 op event_add(&bufev->ev_read, NULL);
910 efe7d180 2021-10-02 op return;
911 efe7d180 2021-10-02 op
912 efe7d180 2021-10-02 op err:
913 efe7d180 2021-10-02 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
914 252908e6 2021-01-24 op }
915 252908e6 2021-01-24 op
916 fe406389 2021-02-02 op static void
917 efe7d180 2021-10-02 op client_tls_writecb(int fd, short event, void *d)
918 252908e6 2021-01-24 op {
919 efe7d180 2021-10-02 op struct bufferevent *bufev = d;
920 efe7d180 2021-10-02 op struct client *client = bufev->cbarg;
921 efe7d180 2021-10-02 op ssize_t ret;
922 efe7d180 2021-10-02 op size_t len;
923 efe7d180 2021-10-02 op short what = EVBUFFER_WRITE;
924 252908e6 2021-01-24 op
925 efe7d180 2021-10-02 op if (event == EV_TIMEOUT) {
926 efe7d180 2021-10-02 op what |= EVBUFFER_TIMEOUT;
927 efe7d180 2021-10-02 op goto err;
928 efe7d180 2021-10-02 op }
929 252908e6 2021-01-24 op
930 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
931 efe7d180 2021-10-02 op ret = tls_write(client->ctx,
932 efe7d180 2021-10-02 op EVBUFFER_DATA(bufev->output),
933 efe7d180 2021-10-02 op EVBUFFER_LENGTH(bufev->output));
934 efe7d180 2021-10-02 op switch (ret) {
935 efe7d180 2021-10-02 op case TLS_WANT_POLLIN:
936 efe7d180 2021-10-02 op case TLS_WANT_POLLOUT:
937 efe7d180 2021-10-02 op goto retry;
938 efe7d180 2021-10-02 op case -1:
939 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
940 efe7d180 2021-10-02 op goto err;
941 252908e6 2021-01-24 op }
942 efe7d180 2021-10-02 op len = ret;
943 efe7d180 2021-10-02 op evbuffer_drain(bufev->output, len);
944 252908e6 2021-01-24 op }
945 252908e6 2021-01-24 op
946 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(bufev->output) != 0)
947 efe7d180 2021-10-02 op event_add(&bufev->ev_write, NULL);
948 efe7d180 2021-10-02 op
949 efe7d180 2021-10-02 op if (bufev->writecb != NULL &&
950 efe7d180 2021-10-02 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
951 efe7d180 2021-10-02 op (*bufev->writecb)(bufev, bufev->cbarg);
952 efe7d180 2021-10-02 op return;
953 efe7d180 2021-10-02 op
954 efe7d180 2021-10-02 op retry:
955 efe7d180 2021-10-02 op event_add(&bufev->ev_write, NULL);
956 efe7d180 2021-10-02 op return;
957 efe7d180 2021-10-02 op err:
958 efe7d180 2021-10-02 op log_err(client, "tls error: %s", tls_error(client->ctx));
959 efe7d180 2021-10-02 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
960 252908e6 2021-01-24 op }
961 252908e6 2021-01-24 op
962 fe406389 2021-02-02 op static void
963 efe7d180 2021-10-02 op client_read(struct bufferevent *bev, void *d)
964 3309ef97 2021-01-23 op {
965 efe7d180 2021-10-02 op struct client *c = d;
966 efe7d180 2021-10-02 op struct evbuffer *src = EVBUFFER_INPUT(bev);
967 efe7d180 2021-10-02 op const char *parse_err = "invalid request";
968 efe7d180 2021-10-02 op char decoded[DOMAIN_NAME_LEN];
969 efe7d180 2021-10-02 op size_t len;
970 3309ef97 2021-01-23 op
971 efe7d180 2021-10-02 op bufferevent_disable(bev, EVBUFFER_READ);
972 901905e0 2022-01-05 op
973 901905e0 2022-01-05 op /*
974 901905e0 2022-01-05 op * libevent2 can still somehow call this function, even
975 901905e0 2022-01-05 op * though I never enable EV_READ in the bufferevent. If
976 901905e0 2022-01-05 op * that's the case, bail out.
977 901905e0 2022-01-05 op */
978 901905e0 2022-01-05 op if (c->type != REQUEST_UNDECIDED)
979 901905e0 2022-01-05 op return;
980 abc007d2 2021-02-08 op
981 efe7d180 2021-10-02 op /* max url len + \r\n */
982 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(src) > 1024 + 2) {
983 efe7d180 2021-10-02 op log_err(c, "too much data received");
984 efe7d180 2021-10-02 op start_reply(c, BAD_REQUEST, "bad request");
985 efe7d180 2021-10-02 op return;
986 efe7d180 2021-10-02 op }
987 3309ef97 2021-01-23 op
988 efe7d180 2021-10-02 op c->req = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
989 efe7d180 2021-10-02 op if (c->req == NULL) {
990 efe7d180 2021-10-02 op /* not enough data yet. */
991 efe7d180 2021-10-02 op bufferevent_enable(bev, EVBUFFER_READ);
992 ea27eaaa 2022-03-27 op return;
993 ea27eaaa 2022-03-27 op }
994 ea27eaaa 2022-03-27 op c->reqlen = strlen(c->req);
995 ea27eaaa 2022-03-27 op if (c->reqlen > 1024+2) {
996 ea27eaaa 2022-03-27 op log_err(c, "URL too long");
997 ea27eaaa 2022-03-27 op start_reply(c, BAD_REQUEST, "bad request");
998 35744950 2021-02-01 op return;
999 3309ef97 2021-01-23 op }
1000 3309ef97 2021-01-23 op
1001 efe7d180 2021-10-02 op if (!parse_iri(c->req, &c->iri, &parse_err) ||
1002 efe7d180 2021-10-02 op !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
1003 efe7d180 2021-10-02 op log_err(c, "IRI parse error: %s", parse_err);
1004 807a80cb 2021-10-06 op start_reply(c, BAD_REQUEST, "bad request");
1005 efe7d180 2021-10-02 op return;
1006 efe7d180 2021-10-02 op }
1007 3309ef97 2021-01-23 op
1008 72b033ef 2021-12-29 op if (apply_reverse_proxy(c))
1009 72b033ef 2021-12-29 op return;
1010 72b033ef 2021-12-29 op
1011 52c92ef6 2021-12-09 op /* ignore the port number */
1012 52c92ef6 2021-12-09 op if (strcmp(c->iri.schema, "gemini") ||
1013 efe7d180 2021-10-02 op strcmp(decoded, c->domain)) {
1014 efe7d180 2021-10-02 op start_reply(c, PROXY_REFUSED, "won't proxy request");
1015 efe7d180 2021-10-02 op return;
1016 efe7d180 2021-10-02 op }
1017 3309ef97 2021-01-23 op
1018 efe7d180 2021-10-02 op if (apply_require_ca(c) ||
1019 efe7d180 2021-10-02 op apply_block_return(c)||
1020 efe7d180 2021-10-02 op apply_fastcgi(c))
1021 35744950 2021-02-01 op return;
1022 efe7d180 2021-10-02 op
1023 efe7d180 2021-10-02 op open_file(c);
1024 3309ef97 2021-01-23 op }
1025 3309ef97 2021-01-23 op
1026 efe7d180 2021-10-02 op void
1027 efe7d180 2021-10-02 op client_write(struct bufferevent *bev, void *d)
1028 d3a08f4d 2021-01-17 op {
1029 efe7d180 2021-10-02 op struct client *c = d;
1030 efe7d180 2021-10-02 op struct evbuffer *out = EVBUFFER_OUTPUT(bev);
1031 f2f8eb35 2022-07-04 op char nam[PATH_MAX];
1032 efe7d180 2021-10-02 op char buf[BUFSIZ];
1033 efe7d180 2021-10-02 op ssize_t r;
1034 d3a08f4d 2021-01-17 op
1035 efe7d180 2021-10-02 op switch (c->type) {
1036 efe7d180 2021-10-02 op case REQUEST_UNDECIDED:
1037 efe7d180 2021-10-02 op /*
1038 efe7d180 2021-10-02 op * Ignore spurious calls when we still don't have idea
1039 efe7d180 2021-10-02 op * what to do with the request.
1040 efe7d180 2021-10-02 op */
1041 efe7d180 2021-10-02 op break;
1042 d3a08f4d 2021-01-17 op
1043 efe7d180 2021-10-02 op case REQUEST_FILE:
1044 efe7d180 2021-10-02 op if ((r = read(c->pfd, buf, sizeof(buf))) == -1) {
1045 efe7d180 2021-10-02 op log_warn(c, "read: %s", strerror(errno));
1046 efe7d180 2021-10-02 op client_error(bev, EVBUFFER_ERROR, c);
1047 efe7d180 2021-10-02 op return;
1048 efe7d180 2021-10-02 op } else if (r == 0) {
1049 efe7d180 2021-10-02 op client_close(c);
1050 efe7d180 2021-10-02 op return;
1051 efe7d180 2021-10-02 op } else if (r != sizeof(buf))
1052 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1053 efe7d180 2021-10-02 op bufferevent_write(bev, buf, r);
1054 efe7d180 2021-10-02 op break;
1055 d3a08f4d 2021-01-17 op
1056 efe7d180 2021-10-02 op case REQUEST_DIR:
1057 efe7d180 2021-10-02 op /* TODO: handle big big directories better */
1058 efe7d180 2021-10-02 op for (c->diroff = 0; c->diroff < c->dirlen; ++c->diroff) {
1059 543f4a66 2022-07-04 op const char *sufx = "";
1060 543f4a66 2022-07-04 op
1061 f2f8eb35 2022-07-04 op encode_path(nam, sizeof(nam),
1062 efe7d180 2021-10-02 op c->dir[c->diroff]->d_name);
1063 543f4a66 2022-07-04 op if (c->dir[c->diroff]->d_type == DT_DIR)
1064 543f4a66 2022-07-04 op sufx = "/";
1065 543f4a66 2022-07-04 op evbuffer_add_printf(out, "=> ./%s%s\n", nam, sufx);
1066 efe7d180 2021-10-02 op free(c->dir[c->diroff]);
1067 d3a08f4d 2021-01-17 op }
1068 efe7d180 2021-10-02 op free(c->dir);
1069 efe7d180 2021-10-02 op c->dir = NULL;
1070 27b2fa9a 2021-02-12 op
1071 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1072 efe7d180 2021-10-02 op
1073 efe7d180 2021-10-02 op event_add(&c->bev->ev_write, NULL);
1074 efe7d180 2021-10-02 op break;
1075 efe7d180 2021-10-02 op
1076 efe7d180 2021-10-02 op case REQUEST_FCGI:
1077 72b033ef 2021-12-29 op case REQUEST_PROXY:
1078 efe7d180 2021-10-02 op /*
1079 d29a2ee2 2022-09-06 op * Here we depend on fastcgi or proxy connection to
1080 d29a2ee2 2022-09-06 op * provide data.
1081 efe7d180 2021-10-02 op */
1082 efe7d180 2021-10-02 op break;
1083 efe7d180 2021-10-02 op
1084 efe7d180 2021-10-02 op case REQUEST_DONE:
1085 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(out) == 0)
1086 efe7d180 2021-10-02 op client_close(c);
1087 efe7d180 2021-10-02 op break;
1088 d3a08f4d 2021-01-17 op }
1089 efe7d180 2021-10-02 op }
1090 d3a08f4d 2021-01-17 op
1091 efe7d180 2021-10-02 op static void
1092 efe7d180 2021-10-02 op client_error(struct bufferevent *bev, short error, void *d)
1093 efe7d180 2021-10-02 op {
1094 efe7d180 2021-10-02 op struct client *c = d;
1095 efe7d180 2021-10-02 op
1096 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1097 efe7d180 2021-10-02 op
1098 efe7d180 2021-10-02 op if (error & EVBUFFER_TIMEOUT) {
1099 efe7d180 2021-10-02 op log_warn(c, "timeout reached, "
1100 efe7d180 2021-10-02 op "forcefully closing the connection");
1101 efe7d180 2021-10-02 op if (c->code == 0)
1102 efe7d180 2021-10-02 op start_reply(c, BAD_REQUEST, "timeout");
1103 efe7d180 2021-10-02 op else
1104 efe7d180 2021-10-02 op client_close(c);
1105 efe7d180 2021-10-02 op return;
1106 efe7d180 2021-10-02 op }
1107 efe7d180 2021-10-02 op
1108 efe7d180 2021-10-02 op if (error & EVBUFFER_EOF) {
1109 efe7d180 2021-10-02 op client_close(c);
1110 efe7d180 2021-10-02 op return;
1111 efe7d180 2021-10-02 op }
1112 efe7d180 2021-10-02 op
1113 d98ae929 2022-02-19 op log_err(c, "unknown bufferevent error %x", error);
1114 efe7d180 2021-10-02 op client_close(c);
1115 d3a08f4d 2021-01-17 op }
1116 d3a08f4d 2021-01-17 op
1117 8ad1c570 2021-05-09 op void
1118 efe7d180 2021-10-02 op start_reply(struct client *c, int code, const char *meta)
1119 d3a08f4d 2021-01-17 op {
1120 efe7d180 2021-10-02 op struct evbuffer *evb = EVBUFFER_OUTPUT(c->bev);
1121 efe7d180 2021-10-02 op const char *lang;
1122 efe7d180 2021-10-02 op int r, rr;
1123 efe7d180 2021-10-02 op
1124 efe7d180 2021-10-02 op bufferevent_enable(c->bev, EVBUFFER_WRITE);
1125 efe7d180 2021-10-02 op
1126 efe7d180 2021-10-02 op c->code = code;
1127 efe7d180 2021-10-02 op c->meta = meta;
1128 efe7d180 2021-10-02 op
1129 efe7d180 2021-10-02 op r = evbuffer_add_printf(evb, "%d %s", code, meta);
1130 efe7d180 2021-10-02 op if (r == -1)
1131 efe7d180 2021-10-02 op goto err;
1132 efe7d180 2021-10-02 op
1133 efe7d180 2021-10-02 op /* 2 digit status + space + 1024 max reply */
1134 efe7d180 2021-10-02 op if (r > 1027)
1135 efe7d180 2021-10-02 op goto overflow;
1136 efe7d180 2021-10-02 op
1137 d29a2ee2 2022-09-06 op if (c->type != REQUEST_FCGI &&
1138 72b033ef 2021-12-29 op c->type != REQUEST_PROXY &&
1139 efe7d180 2021-10-02 op !strcmp(meta, "text/gemini") &&
1140 efe7d180 2021-10-02 op (lang = vhost_lang(c->host, c->iri.path)) != NULL) {
1141 4842c72d 2021-10-18 op rr = evbuffer_add_printf(evb, ";lang=%s", lang);
1142 efe7d180 2021-10-02 op if (rr == -1)
1143 efe7d180 2021-10-02 op goto err;
1144 efe7d180 2021-10-02 op if (r + rr > 1027)
1145 efe7d180 2021-10-02 op goto overflow;
1146 efe7d180 2021-10-02 op }
1147 efe7d180 2021-10-02 op
1148 efe7d180 2021-10-02 op bufferevent_write(c->bev, "\r\n", 2);
1149 efe7d180 2021-10-02 op
1150 efe7d180 2021-10-02 op if (!vhost_disable_log(c->host, c->iri.path))
1151 efe7d180 2021-10-02 op log_request(c, EVBUFFER_DATA(evb), EVBUFFER_LENGTH(evb));
1152 efe7d180 2021-10-02 op
1153 d28bd963 2022-01-27 op if (code != 20)
1154 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1155 efe7d180 2021-10-02 op
1156 efe7d180 2021-10-02 op return;
1157 efe7d180 2021-10-02 op
1158 efe7d180 2021-10-02 op err:
1159 efe7d180 2021-10-02 op log_err(c, "evbuffer_add_printf error: no memory");
1160 efe7d180 2021-10-02 op evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
1161 efe7d180 2021-10-02 op client_close(c);
1162 efe7d180 2021-10-02 op return;
1163 efe7d180 2021-10-02 op
1164 efe7d180 2021-10-02 op overflow:
1165 efe7d180 2021-10-02 op log_warn(c, "reply header overflow");
1166 efe7d180 2021-10-02 op evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
1167 efe7d180 2021-10-02 op start_reply(c, TEMP_FAILURE, "internal error");
1168 efe7d180 2021-10-02 op }
1169 efe7d180 2021-10-02 op
1170 efe7d180 2021-10-02 op static void
1171 efe7d180 2021-10-02 op client_close_ev(int fd, short event, void *d)
1172 efe7d180 2021-10-02 op {
1173 8ad1c570 2021-05-09 op struct client *c = d;
1174 d3a08f4d 2021-01-17 op
1175 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
1176 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
1177 efe7d180 2021-10-02 op event_once(c->fd, EV_READ, client_close_ev, c, NULL);
1178 efe7d180 2021-10-02 op break;
1179 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
1180 efe7d180 2021-10-02 op event_once(c->fd, EV_WRITE, client_close_ev, c, NULL);
1181 efe7d180 2021-10-02 op break;
1182 d3a08f4d 2021-01-17 op }
1183 d3a08f4d 2021-01-17 op
1184 d3a08f4d 2021-01-17 op connected_clients--;
1185 e4daebe4 2021-10-06 op
1186 e4daebe4 2021-10-06 op free(c->req);
1187 d3a08f4d 2021-01-17 op
1188 d3a08f4d 2021-01-17 op tls_free(c->ctx);
1189 d3a08f4d 2021-01-17 op c->ctx = NULL;
1190 d3a08f4d 2021-01-17 op
1191 efe7d180 2021-10-02 op free(c->header);
1192 efe7d180 2021-10-02 op
1193 abc007d2 2021-02-08 op if (c->pfd != -1)
1194 abc007d2 2021-02-08 op close(c->pfd);
1195 d3a08f4d 2021-01-17 op
1196 252908e6 2021-01-24 op if (c->dir != NULL)
1197 11c98667 2021-04-25 op free(c->dir);
1198 252908e6 2021-01-24 op
1199 abc007d2 2021-02-08 op close(c->fd);
1200 abc007d2 2021-02-08 op c->fd = -1;
1201 72b033ef 2021-12-29 op }
1202 72b033ef 2021-12-29 op
1203 72b033ef 2021-12-29 op static void
1204 72b033ef 2021-12-29 op client_proxy_close(int fd, short event, void *d)
1205 72b033ef 2021-12-29 op {
1206 72b033ef 2021-12-29 op struct tls *ctx = d;
1207 72b033ef 2021-12-29 op
1208 72b033ef 2021-12-29 op if (ctx == NULL) {
1209 72b033ef 2021-12-29 op close(fd);
1210 72b033ef 2021-12-29 op return;
1211 72b033ef 2021-12-29 op }
1212 72b033ef 2021-12-29 op
1213 72b033ef 2021-12-29 op switch (tls_close(ctx)) {
1214 72b033ef 2021-12-29 op case TLS_WANT_POLLIN:
1215 72b033ef 2021-12-29 op event_once(fd, EV_READ, client_proxy_close, d, NULL);
1216 72b033ef 2021-12-29 op break;
1217 72b033ef 2021-12-29 op case TLS_WANT_POLLOUT:
1218 72b033ef 2021-12-29 op event_once(fd, EV_WRITE, client_proxy_close, d, NULL);
1219 72b033ef 2021-12-29 op break;
1220 72b033ef 2021-12-29 op }
1221 72b033ef 2021-12-29 op
1222 72b033ef 2021-12-29 op tls_free(ctx);
1223 72b033ef 2021-12-29 op close(fd);
1224 f890c8c5 2021-01-22 op }
1225 f890c8c5 2021-01-22 op
1226 efe7d180 2021-10-02 op void
1227 efe7d180 2021-10-02 op client_close(struct client *c)
1228 efe7d180 2021-10-02 op {
1229 efe7d180 2021-10-02 op /*
1230 efe7d180 2021-10-02 op * We may end up calling client_close in various situations
1231 efe7d180 2021-10-02 op * and for the most unexpected reasons. Therefore, we need to
1232 b9b77f53 2022-01-27 op * ensure that everything gets properly released once we reach
1233 efe7d180 2021-10-02 op * this point.
1234 efe7d180 2021-10-02 op */
1235 207b3e80 2021-10-07 op
1236 207b3e80 2021-10-07 op SPLAY_REMOVE(client_tree_id, &clients, c);
1237 efe7d180 2021-10-02 op
1238 efe7d180 2021-10-02 op if (c->cgibev != NULL) {
1239 efe7d180 2021-10-02 op bufferevent_disable(c->cgibev, EVBUFFER_READ|EVBUFFER_WRITE);
1240 efe7d180 2021-10-02 op bufferevent_free(c->cgibev);
1241 efe7d180 2021-10-02 op c->cgibev = NULL;
1242 efe7d180 2021-10-02 op close(c->pfd);
1243 efe7d180 2021-10-02 op c->pfd = -1;
1244 efe7d180 2021-10-02 op }
1245 efe7d180 2021-10-02 op
1246 efe7d180 2021-10-02 op bufferevent_disable(c->bev, EVBUFFER_READ|EVBUFFER_WRITE);
1247 efe7d180 2021-10-02 op bufferevent_free(c->bev);
1248 efe7d180 2021-10-02 op c->bev = NULL;
1249 efe7d180 2021-10-02 op
1250 e0f6dc64 2022-01-27 op if (c->proxyevset &&
1251 e0f6dc64 2022-01-27 op event_pending(&c->proxyev, EV_READ|EV_WRITE, NULL)) {
1252 e0f6dc64 2022-01-27 op c->proxyevset = 0;
1253 e0f6dc64 2022-01-27 op event_del(&c->proxyev);
1254 e0f6dc64 2022-01-27 op }
1255 72b033ef 2021-12-29 op
1256 e0f6dc64 2022-01-27 op if (c->pfd != -1 && c->proxyctx != NULL) {
1257 e0f6dc64 2022-01-27 op /* shut down the proxy TLS connection */
1258 e0f6dc64 2022-01-27 op client_proxy_close(c->pfd, 0, c->proxyctx);
1259 e0f6dc64 2022-01-27 op c->pfd = -1;
1260 72b033ef 2021-12-29 op }
1261 e0f6dc64 2022-01-27 op
1262 e0f6dc64 2022-01-27 op if (c->proxybev != NULL)
1263 e0f6dc64 2022-01-27 op bufferevent_free(c->proxybev);
1264 72b033ef 2021-12-29 op
1265 efe7d180 2021-10-02 op client_close_ev(c->fd, 0, c);
1266 efe7d180 2021-10-02 op }
1267 efe7d180 2021-10-02 op
1268 fe406389 2021-02-02 op static void
1269 abc007d2 2021-02-08 op do_accept(int sock, short et, void *d)
1270 d3a08f4d 2021-01-17 op {
1271 abc007d2 2021-02-08 op struct client *c;
1272 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
1273 abc007d2 2021-02-08 op struct sockaddr *saddr;
1274 d3a08f4d 2021-01-17 op socklen_t len;
1275 207b3e80 2021-10-07 op int fd;
1276 abc007d2 2021-02-08 op
1277 abc007d2 2021-02-08 op saddr = (struct sockaddr*)&addr;
1278 d3a08f4d 2021-01-17 op len = sizeof(addr);
1279 3cb3dd4d 2021-02-12 op if ((fd = accept(sock, saddr, &len)) == -1) {
1280 c62a411f 2021-10-13 op if (errno == EWOULDBLOCK || errno == EAGAIN ||
1281 c62a411f 2021-10-13 op errno == ECONNABORTED)
1282 d3a08f4d 2021-01-17 op return;
1283 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
1284 d3a08f4d 2021-01-17 op }
1285 d3a08f4d 2021-01-17 op
1286 3cb3dd4d 2021-02-12 op mark_nonblock(fd);
1287 3cb3dd4d 2021-02-12 op
1288 207b3e80 2021-10-07 op c = xcalloc(1, sizeof(*c));
1289 207b3e80 2021-10-07 op c->id = ++server_client_id;
1290 207b3e80 2021-10-07 op c->fd = fd;
1291 207b3e80 2021-10-07 op c->pfd = -1;
1292 207b3e80 2021-10-07 op c->addr = addr;
1293 d3a08f4d 2021-01-17 op
1294 207b3e80 2021-10-07 op if (tls_accept_socket(ctx, &c->ctx, fd) == -1) {
1295 207b3e80 2021-10-07 op log_warn(c, "failed to accept socket: %s", tls_error(c->ctx));
1296 207b3e80 2021-10-07 op close(c->fd);
1297 207b3e80 2021-10-07 op free(c);
1298 207b3e80 2021-10-07 op return;
1299 d3a08f4d 2021-01-17 op }
1300 d3a08f4d 2021-01-17 op
1301 207b3e80 2021-10-07 op SPLAY_INSERT(client_tree_id, &clients, c);
1302 207b3e80 2021-10-07 op event_once(c->fd, EV_READ|EV_WRITE, handle_handshake, c, NULL);
1303 207b3e80 2021-10-07 op connected_clients++;
1304 d3a08f4d 2021-01-17 op }
1305 d3a08f4d 2021-01-17 op
1306 8ad1c570 2021-05-09 op struct client *
1307 3fdc457c 2022-03-26 op client_by_id(int id)
1308 8ad1c570 2021-05-09 op {
1309 207b3e80 2021-10-07 op struct client find;
1310 207b3e80 2021-10-07 op
1311 207b3e80 2021-10-07 op find.id = id;
1312 207b3e80 2021-10-07 op return SPLAY_FIND(client_tree_id, &clients, &find);
1313 8ad1c570 2021-05-09 op }
1314 8ad1c570 2021-05-09 op
1315 abc007d2 2021-02-08 op static void
1316 d040746a 2022-09-10 op handle_dispatch_imsg(int fd, short ev, void *d)
1317 bc99d868 2021-03-19 op {
1318 d040746a 2022-09-10 op struct imsgbuf *ibuf = d;
1319 d040746a 2022-09-10 op struct imsg imsg;
1320 d040746a 2022-09-10 op ssize_t n;
1321 bc99d868 2021-03-19 op
1322 d040746a 2022-09-10 op if ((n = imsg_read(ibuf)) == -1) {
1323 d040746a 2022-09-10 op if (errno == EAGAIN || errno == EWOULDBLOCK)
1324 d040746a 2022-09-10 op return;
1325 d040746a 2022-09-10 op fatal("imsg_read");
1326 d040746a 2022-09-10 op }
1327 090b8a89 2021-07-06 op
1328 d040746a 2022-09-10 op if (n == 0)
1329 d040746a 2022-09-10 op fatal("connection closed."); /* XXX: fatalx */
1330 d3a08f4d 2021-01-17 op
1331 d040746a 2022-09-10 op for (;;) {
1332 d040746a 2022-09-10 op if ((n = imsg_get(ibuf, &imsg)) == -1)
1333 d040746a 2022-09-10 op fatal("imsg_get");
1334 d040746a 2022-09-10 op if (n == 0)
1335 d040746a 2022-09-10 op return;
1336 d040746a 2022-09-10 op
1337 d040746a 2022-09-10 op switch (imsg.hdr.type) {
1338 d040746a 2022-09-10 op case IMSG_QUIT:
1339 d040746a 2022-09-10 op /*
1340 d040746a 2022-09-10 op * Don't call event_loopbreak since we want to
1341 d040746a 2022-09-10 op * finish handling the ongoing connections.
1342 d040746a 2022-09-10 op */
1343 d040746a 2022-09-10 op shutting_down = 1;
1344 d040746a 2022-09-10 op
1345 d040746a 2022-09-10 op event_del(&e4);
1346 d040746a 2022-09-10 op if (has_ipv6)
1347 d040746a 2022-09-10 op event_del(&e6);
1348 d040746a 2022-09-10 op if (has_siginfo)
1349 d040746a 2022-09-10 op signal_del(&siginfo);
1350 d040746a 2022-09-10 op event_del(&imsgev);
1351 d040746a 2022-09-10 op signal_del(&sigusr2);
1352 d040746a 2022-09-10 op break;
1353 d040746a 2022-09-10 op default:
1354 d040746a 2022-09-10 op /* XXX: fatalx */
1355 d040746a 2022-09-10 op fatal("Unknown message %d", imsg.hdr.type);
1356 d040746a 2022-09-10 op }
1357 d040746a 2022-09-10 op imsg_free(&imsg);
1358 d040746a 2022-09-10 op }
1359 bc99d868 2021-03-19 op }
1360 bc99d868 2021-03-19 op
1361 bc99d868 2021-03-19 op static void
1362 abc007d2 2021-02-08 op handle_siginfo(int fd, short ev, void *d)
1363 abc007d2 2021-02-08 op {
1364 abc007d2 2021-02-08 op log_info(NULL, "%d connected clients", connected_clients);
1365 abc007d2 2021-02-08 op }
1366 d3a08f4d 2021-01-17 op
1367 abc007d2 2021-02-08 op void
1368 bc99d868 2021-03-19 op loop(struct tls *ctx_, int sock4, int sock6, struct imsgbuf *ibuf)
1369 abc007d2 2021-02-08 op {
1370 bc99d868 2021-03-19 op ctx = ctx_;
1371 bc99d868 2021-03-19 op
1372 207b3e80 2021-10-07 op SPLAY_INIT(&clients);
1373 207b3e80 2021-10-07 op
1374 abc007d2 2021-02-08 op event_init();
1375 1e3ef7ab 2021-02-03 op
1376 bc99d868 2021-03-19 op event_set(&e4, sock4, EV_READ | EV_PERSIST, &do_accept, NULL);
1377 a6e689d7 2021-02-12 op event_add(&e4, NULL);
1378 ca21e100 2021-02-04 op
1379 abc007d2 2021-02-08 op if (sock6 != -1) {
1380 a6e689d7 2021-02-12 op has_ipv6 = 1;
1381 bc99d868 2021-03-19 op event_set(&e6, sock6, EV_READ | EV_PERSIST, &do_accept, NULL);
1382 a6e689d7 2021-02-12 op event_add(&e6, NULL);
1383 d3a08f4d 2021-01-17 op }
1384 abc007d2 2021-02-08 op
1385 0126d91d 2022-09-07 op if (ibuf) {
1386 0126d91d 2022-09-07 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST,
1387 0126d91d 2022-09-07 op handle_dispatch_imsg, ibuf);
1388 0126d91d 2022-09-07 op event_add(&imsgev, NULL);
1389 0126d91d 2022-09-07 op }
1390 abc007d2 2021-02-08 op
1391 abc007d2 2021-02-08 op #ifdef SIGINFO
1392 a6e689d7 2021-02-12 op has_siginfo = 1;
1393 a6e689d7 2021-02-12 op signal_set(&siginfo, SIGINFO, &handle_siginfo, NULL);
1394 a6e689d7 2021-02-12 op signal_add(&siginfo, NULL);
1395 abc007d2 2021-02-08 op #endif
1396 5e3285d5 2021-02-12 op signal_set(&sigusr2, SIGUSR2, &handle_siginfo, NULL);
1397 a6e689d7 2021-02-12 op signal_add(&sigusr2, NULL);
1398 abc007d2 2021-02-08 op
1399 76000995 2022-09-06 op sandbox_server_process(conf.can_open_sockets);
1400 abc007d2 2021-02-08 op event_dispatch();
1401 df58efff 2021-02-08 op _exit(0);
1402 d3a08f4d 2021-01-17 op }
1403 207b3e80 2021-10-07 op
1404 207b3e80 2021-10-07 op int
1405 207b3e80 2021-10-07 op client_tree_cmp(struct client *a, struct client *b)
1406 207b3e80 2021-10-07 op {
1407 207b3e80 2021-10-07 op if (a->id == b->id)
1408 207b3e80 2021-10-07 op return 0;
1409 207b3e80 2021-10-07 op else if (a->id < b->id)
1410 207b3e80 2021-10-07 op return -1;
1411 207b3e80 2021-10-07 op else
1412 207b3e80 2021-10-07 op return +1;
1413 207b3e80 2021-10-07 op }
1414 207b3e80 2021-10-07 op
1415 207b3e80 2021-10-07 op SPLAY_GENERATE(client_tree_id, client, entry, client_tree_cmp)