Blame


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