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