Blame


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