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