Blame


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