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