Blame


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