Blame


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