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 d3a08f4d 2021-01-17 op #include <errno.h>
23 abc007d2 2021-02-08 op #include <event.h>
24 d3a08f4d 2021-01-17 op #include <fcntl.h>
25 b4d409cf 2021-01-21 op #include <fnmatch.h>
26 2fafa2d2 2021-02-01 op #include <limits.h>
27 d3a08f4d 2021-01-17 op #include <string.h>
28 090b8a89 2021-07-06 op
29 090b8a89 2021-07-06 op int shutting_down;
30 d3a08f4d 2021-01-17 op
31 8ad1c570 2021-05-09 op struct client clients[MAX_USERS];
32 8ad1c570 2021-05-09 op
33 bc99d868 2021-03-19 op static struct tls *ctx;
34 abc007d2 2021-02-08 op
35 bc99d868 2021-03-19 op static struct event e4, e6, imsgev, siginfo, sigusr2;
36 6b191ed5 2021-02-23 op static int has_ipv6, has_siginfo;
37 abc007d2 2021-02-08 op
38 d3a08f4d 2021-01-17 op int connected_clients;
39 d3a08f4d 2021-01-17 op
40 49b73ba1 2021-02-10 op static inline int matches(const char*, const char*);
41 49b73ba1 2021-02-10 op
42 74c0c7e4 2021-04-20 op static inline void yield_read(int, struct client*, statefn);
43 74c0c7e4 2021-04-20 op static inline void yield_write(int, struct client*, statefn);
44 abc007d2 2021-02-08 op
45 fe406389 2021-02-02 op static int check_path(struct client*, const char*, int*);
46 abc007d2 2021-02-08 op static void open_file(struct client*);
47 abc007d2 2021-02-08 op static void check_for_cgi(struct client*);
48 abc007d2 2021-02-08 op static void handle_handshake(int, short, void*);
49 91b9f2a8 2021-05-15 op static const char *strip_path(const char*, int);
50 57ec3e77 2021-02-08 op static void fmt_sbuf(const char*, struct client*, const char*);
51 abc007d2 2021-02-08 op static int apply_block_return(struct client*);
52 02be96c6 2021-02-09 op static int apply_require_ca(struct client*);
53 abc007d2 2021-02-08 op static void handle_open_conn(int, short, void*);
54 abc007d2 2021-02-08 op static void handle_start_reply(int, short, void*);
55 b8e64ccd 2021-03-31 op static size_t host_nth(struct vhost*);
56 abc007d2 2021-02-08 op static void start_cgi(const char*, const char*, 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 abc007d2 2021-02-08 op static void enter_handle_dirlist(int, short, void*);
60 abc007d2 2021-02-08 op static void handle_dirlist(int, short, void*);
61 fe406389 2021-02-02 op static int read_next_dir_entry(struct client*);
62 abc007d2 2021-02-08 op static void send_directory_listing(int, short, void*);
63 abc007d2 2021-02-08 op static void handle_cgi_reply(int, short, void*);
64 27b2fa9a 2021-02-12 op static void handle_copy(int, short, void*);
65 abc007d2 2021-02-08 op static void do_accept(int, short, void*);
66 bc99d868 2021-03-19 op static void handle_imsg_cgi_res(struct imsgbuf*, struct imsg*, size_t);
67 8ad1c570 2021-05-09 op static void handle_imsg_fcgi_fd(struct imsgbuf*, struct imsg*, size_t);
68 bc99d868 2021-03-19 op static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
69 bc99d868 2021-03-19 op static void handle_siginfo(int, short, void*);
70 bc99d868 2021-03-19 op
71 bc99d868 2021-03-19 op static imsg_handlerfn *handlers[] = {
72 bc99d868 2021-03-19 op [IMSG_QUIT] = handle_imsg_quit,
73 bc99d868 2021-03-19 op [IMSG_CGI_RES] = handle_imsg_cgi_res,
74 8ad1c570 2021-05-09 op [IMSG_FCGI_FD] = handle_imsg_fcgi_fd,
75 bc99d868 2021-03-19 op };
76 fe406389 2021-02-02 op
77 49b73ba1 2021-02-10 op static inline int
78 49b73ba1 2021-02-10 op matches(const char *pattern, const char *path)
79 49b73ba1 2021-02-10 op {
80 49b73ba1 2021-02-10 op if (*path == '/')
81 49b73ba1 2021-02-10 op path++;
82 49b73ba1 2021-02-10 op return !fnmatch(pattern, path, 0);
83 49b73ba1 2021-02-10 op }
84 49b73ba1 2021-02-10 op
85 abc007d2 2021-02-08 op static inline void
86 74c0c7e4 2021-04-20 op yield_read(int fd, struct client *c, statefn fn)
87 abc007d2 2021-02-08 op {
88 abc007d2 2021-02-08 op event_once(fd, EV_READ, fn, c, NULL);
89 abc007d2 2021-02-08 op }
90 abc007d2 2021-02-08 op
91 c39b26d3 2021-02-12 op static inline void
92 74c0c7e4 2021-04-20 op yield_write(int fd, struct client *c, statefn fn)
93 abc007d2 2021-02-08 op {
94 abc007d2 2021-02-08 op event_once(fd, EV_WRITE, fn, c, NULL);
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 27b2fa9a 2021-02-12 op c->next = handle_copy;
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 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_handshake);
462 d3a08f4d 2021-01-17 op return;
463 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
464 74c0c7e4 2021-04-20 op yield_write(fd, c, &handle_handshake);
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 abc007d2 2021-02-08 op handle_open_conn(fd, ev, c);
499 d3a08f4d 2021-01-17 op return;
500 d3a08f4d 2021-01-17 op }
501 d3a08f4d 2021-01-17 op
502 a8d4a897 2021-01-29 op err:
503 0ab65593 2021-01-21 op if (servname != NULL)
504 1b78bd56 2021-06-16 op strlcpy(c->req, servname, sizeof(c->req));
505 0ab65593 2021-01-21 op else
506 1b78bd56 2021-06-16 op strlcpy(c->req, "null", sizeof(c->req));
507 0ab65593 2021-01-21 op
508 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
509 6abda252 2021-02-06 op }
510 6abda252 2021-02-06 op
511 91b9f2a8 2021-05-15 op static const char *
512 91b9f2a8 2021-05-15 op strip_path(const char *path, int strip)
513 6abda252 2021-02-06 op {
514 57ec3e77 2021-02-08 op char *t;
515 6abda252 2021-02-06 op
516 6abda252 2021-02-06 op while (strip > 0) {
517 6abda252 2021-02-06 op if ((t = strchr(path, '/')) == NULL) {
518 6abda252 2021-02-06 op path = strchr(path, '\0');
519 6abda252 2021-02-06 op break;
520 6abda252 2021-02-06 op }
521 6abda252 2021-02-06 op path = t;
522 6abda252 2021-02-06 op strip--;
523 6abda252 2021-02-06 op }
524 57ec3e77 2021-02-08 op
525 57ec3e77 2021-02-08 op return path;
526 57ec3e77 2021-02-08 op }
527 57ec3e77 2021-02-08 op
528 57ec3e77 2021-02-08 op static void
529 57ec3e77 2021-02-08 op fmt_sbuf(const char *fmt, struct client *c, const char *path)
530 57ec3e77 2021-02-08 op {
531 57ec3e77 2021-02-08 op size_t i;
532 57ec3e77 2021-02-08 op char buf[32];
533 6abda252 2021-02-06 op
534 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
535 6abda252 2021-02-06 op for (i = 0; *fmt; ++fmt) {
536 6abda252 2021-02-06 op if (i == sizeof(buf)-1 || *fmt == '%') {
537 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
538 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
539 6abda252 2021-02-06 op i = 0;
540 6abda252 2021-02-06 op }
541 6abda252 2021-02-06 op
542 6abda252 2021-02-06 op if (*fmt != '%') {
543 6abda252 2021-02-06 op buf[i++] = *fmt;
544 6abda252 2021-02-06 op continue;
545 6abda252 2021-02-06 op }
546 6abda252 2021-02-06 op
547 6abda252 2021-02-06 op switch (*++fmt) {
548 6abda252 2021-02-06 op case '%':
549 6abda252 2021-02-06 op strlcat(c->sbuf, "%", sizeof(c->sbuf));
550 6abda252 2021-02-06 op break;
551 6abda252 2021-02-06 op case 'p':
552 737a6b50 2021-04-30 op if (*path != '/')
553 737a6b50 2021-04-30 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
554 6abda252 2021-02-06 op strlcat(c->sbuf, path, sizeof(c->sbuf));
555 6abda252 2021-02-06 op break;
556 6abda252 2021-02-06 op case 'q':
557 6abda252 2021-02-06 op strlcat(c->sbuf, c->iri.query, sizeof(c->sbuf));
558 6abda252 2021-02-06 op break;
559 6abda252 2021-02-06 op case 'P':
560 6abda252 2021-02-06 op snprintf(buf, sizeof(buf), "%d", conf.port);
561 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
562 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
563 6abda252 2021-02-06 op break;
564 6abda252 2021-02-06 op case 'N':
565 6abda252 2021-02-06 op strlcat(c->sbuf, c->domain, sizeof(c->sbuf));
566 6abda252 2021-02-06 op break;
567 6abda252 2021-02-06 op default:
568 6abda252 2021-02-06 op fatal("%s: unknown fmt specifier %c",
569 6abda252 2021-02-06 op __func__, *fmt);
570 6abda252 2021-02-06 op }
571 6abda252 2021-02-06 op }
572 6abda252 2021-02-06 op
573 6abda252 2021-02-06 op if (i != 0)
574 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
575 57ec3e77 2021-02-08 op }
576 6abda252 2021-02-06 op
577 57ec3e77 2021-02-08 op /* 1 if a matching `block return' (and apply it), 0 otherwise */
578 57ec3e77 2021-02-08 op static int
579 57ec3e77 2021-02-08 op apply_block_return(struct client *c)
580 57ec3e77 2021-02-08 op {
581 57ec3e77 2021-02-08 op const char *fmt, *path;
582 57ec3e77 2021-02-08 op int code;
583 57ec3e77 2021-02-08 op
584 57ec3e77 2021-02-08 op if (!vhost_block_return(c->host, c->iri.path, &code, &fmt))
585 57ec3e77 2021-02-08 op return 0;
586 57ec3e77 2021-02-08 op
587 57ec3e77 2021-02-08 op path = strip_path(c->iri.path, vhost_strip(c->host, c->iri.path));
588 57ec3e77 2021-02-08 op fmt_sbuf(fmt, c, path);
589 57ec3e77 2021-02-08 op
590 abc007d2 2021-02-08 op start_reply(c, code, c->sbuf);
591 8ad1c570 2021-05-09 op return 1;
592 8ad1c570 2021-05-09 op }
593 8ad1c570 2021-05-09 op
594 8ad1c570 2021-05-09 op /* 1 if matching `fcgi' (and apply it), 0 otherwise */
595 8ad1c570 2021-05-09 op static int
596 8ad1c570 2021-05-09 op apply_fastcgi(struct client *c)
597 8ad1c570 2021-05-09 op {
598 8ad1c570 2021-05-09 op int id;
599 8ad1c570 2021-05-09 op struct fcgi *f;
600 8ad1c570 2021-05-09 op
601 8ad1c570 2021-05-09 op if ((id = vhost_fastcgi(c->host, c->iri.path)) == -1)
602 8ad1c570 2021-05-09 op return 0;
603 8ad1c570 2021-05-09 op
604 8ad1c570 2021-05-09 op switch ((f = &fcgi[id])->s) {
605 8ad1c570 2021-05-09 op case FCGI_OFF:
606 8ad1c570 2021-05-09 op f->s = FCGI_INFLIGHT;
607 8ad1c570 2021-05-09 op log_info(c, "opening fastcgi connection for (%s,%s,%s)",
608 8ad1c570 2021-05-09 op f->path, f->port, f->prog);
609 8ad1c570 2021-05-09 op imsg_compose(&exibuf, IMSG_FCGI_REQ, 0, 0, -1,
610 8ad1c570 2021-05-09 op &id, sizeof(id));
611 8ad1c570 2021-05-09 op imsg_flush(&exibuf);
612 8ad1c570 2021-05-09 op /* fallthrough */
613 8ad1c570 2021-05-09 op case FCGI_INFLIGHT:
614 8ad1c570 2021-05-09 op c->fcgi = id;
615 8ad1c570 2021-05-09 op break;
616 8ad1c570 2021-05-09 op case FCGI_READY:
617 8ad1c570 2021-05-09 op c->fcgi = id;
618 741b69be 2021-09-26 op fcgi_req(f, c);
619 8ad1c570 2021-05-09 op break;
620 8ad1c570 2021-05-09 op }
621 8ad1c570 2021-05-09 op
622 6abda252 2021-02-06 op return 1;
623 d3a08f4d 2021-01-17 op }
624 d3a08f4d 2021-01-17 op
625 02be96c6 2021-02-09 op /* 1 if matching `require client ca' fails (and apply it), 0 otherwise */
626 02be96c6 2021-02-09 op static int
627 02be96c6 2021-02-09 op apply_require_ca(struct client *c)
628 02be96c6 2021-02-09 op {
629 02be96c6 2021-02-09 op X509_STORE *store;
630 02be96c6 2021-02-09 op const uint8_t *cert;
631 02be96c6 2021-02-09 op size_t len;
632 02be96c6 2021-02-09 op
633 02be96c6 2021-02-09 op if ((store = vhost_require_ca(c->host, c->iri.path)) == NULL)
634 02be96c6 2021-02-09 op return 0;
635 02be96c6 2021-02-09 op
636 02be96c6 2021-02-09 op if (!tls_peer_cert_provided(c->ctx)) {
637 02be96c6 2021-02-09 op start_reply(c, CLIENT_CERT_REQ, "client certificate required");
638 02be96c6 2021-02-09 op return 1;
639 02be96c6 2021-02-09 op }
640 02be96c6 2021-02-09 op
641 02be96c6 2021-02-09 op cert = tls_peer_cert_chain_pem(c->ctx, &len);
642 02be96c6 2021-02-09 op if (!validate_against_ca(store, cert, len)) {
643 02be96c6 2021-02-09 op start_reply(c, CERT_NOT_AUTH, "certificate not authorised");
644 02be96c6 2021-02-09 op return 1;
645 02be96c6 2021-02-09 op }
646 02be96c6 2021-02-09 op
647 02be96c6 2021-02-09 op return 0;
648 02be96c6 2021-02-09 op }
649 02be96c6 2021-02-09 op
650 fe406389 2021-02-02 op static void
651 abc007d2 2021-02-08 op handle_open_conn(int fd, short ev, void *d)
652 d3a08f4d 2021-01-17 op {
653 abc007d2 2021-02-08 op struct client *c = d;
654 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
655 3300cbe0 2021-01-27 op char decoded[DOMAIN_NAME_LEN];
656 d3a08f4d 2021-01-17 op
657 0be51733 2021-01-20 op switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
658 d3a08f4d 2021-01-17 op case -1:
659 3abf91b0 2021-02-07 op log_err(c, "tls_read: %s", tls_error(c->ctx));
660 abc007d2 2021-02-08 op close_conn(fd, ev, c);
661 d3a08f4d 2021-01-17 op return;
662 d3a08f4d 2021-01-17 op
663 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
664 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_open_conn);
665 d3a08f4d 2021-01-17 op return;
666 d3a08f4d 2021-01-17 op
667 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
668 74c0c7e4 2021-04-20 op yield_write(fd, c, &handle_open_conn);
669 d3a08f4d 2021-01-17 op return;
670 d3a08f4d 2021-01-17 op }
671 d3a08f4d 2021-01-17 op
672 353e3c8e 2021-09-24 op if (!trim_req_iri(c->req, &parse_err) ||
673 353e3c8e 2021-09-24 op !parse_iri(c->req, &c->iri, &parse_err) ||
674 353e3c8e 2021-09-24 op !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
675 3abf91b0 2021-02-07 op log_info(c, "iri parse error: %s", parse_err);
676 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "invalid request");
677 d3a08f4d 2021-01-17 op return;
678 d3a08f4d 2021-01-17 op }
679 d3a08f4d 2021-01-17 op
680 353e3c8e 2021-09-24 op if (c->iri.port_no != conf.port ||
681 353e3c8e 2021-09-24 op strcmp(c->iri.schema, "gemini") ||
682 353e3c8e 2021-09-24 op strcmp(decoded, c->domain)) {
683 abc007d2 2021-02-08 op start_reply(c, PROXY_REFUSED, "won't proxy request");
684 d3a08f4d 2021-01-17 op return;
685 d3a08f4d 2021-01-17 op }
686 d3a08f4d 2021-01-17 op
687 02be96c6 2021-02-09 op if (apply_require_ca(c))
688 02be96c6 2021-02-09 op return;
689 02be96c6 2021-02-09 op
690 abc007d2 2021-02-08 op if (apply_block_return(c))
691 e3ddf390 2021-02-06 op return;
692 e3ddf390 2021-02-06 op
693 8ad1c570 2021-05-09 op if (apply_fastcgi(c))
694 8ad1c570 2021-05-09 op return;
695 8ad1c570 2021-05-09 op
696 e3ddf390 2021-02-06 op if (c->host->entrypoint != NULL) {
697 1feaf2a6 2021-05-15 op c->loc = 0;
698 abc007d2 2021-02-08 op start_cgi(c->host->entrypoint, c->iri.path, c);
699 e3ddf390 2021-02-06 op return;
700 e3ddf390 2021-02-06 op }
701 e3ddf390 2021-02-06 op
702 abc007d2 2021-02-08 op open_file(c);
703 d3a08f4d 2021-01-17 op }
704 d3a08f4d 2021-01-17 op
705 8ad1c570 2021-05-09 op void
706 abc007d2 2021-02-08 op start_reply(struct client *c, int code, const char *meta)
707 d3a08f4d 2021-01-17 op {
708 112802ea 2021-02-01 op c->code = code;
709 112802ea 2021-02-01 op c->meta = meta;
710 abc007d2 2021-02-08 op handle_start_reply(c->fd, 0, c);
711 112802ea 2021-02-01 op }
712 112802ea 2021-02-01 op
713 fe406389 2021-02-02 op static void
714 abc007d2 2021-02-08 op handle_start_reply(int fd, short ev, void *d)
715 112802ea 2021-02-01 op {
716 abc007d2 2021-02-08 op struct client *c = d;
717 05c23a54 2021-01-19 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
718 c8b74339 2021-01-24 op const char *lang;
719 0be51733 2021-01-20 op size_t len;
720 d3a08f4d 2021-01-17 op
721 c8b74339 2021-01-24 op lang = vhost_lang(c->host, c->iri.path);
722 c8b74339 2021-01-24 op
723 112802ea 2021-02-01 op snprintf(buf, sizeof(buf), "%d ", c->code);
724 112802ea 2021-02-01 op strlcat(buf, c->meta, sizeof(buf));
725 112802ea 2021-02-01 op if (!strcmp(c->meta, "text/gemini") && lang != NULL) {
726 05c23a54 2021-01-19 op strlcat(buf, "; lang=", sizeof(buf));
727 c8b74339 2021-01-24 op strlcat(buf, lang, sizeof(buf));
728 05c23a54 2021-01-19 op }
729 05c23a54 2021-01-19 op
730 05c23a54 2021-01-19 op len = strlcat(buf, "\r\n", sizeof(buf));
731 0be51733 2021-01-20 op assert(len < sizeof(buf));
732 d3a08f4d 2021-01-17 op
733 df79b4c1 2021-01-19 op switch (tls_write(c->ctx, buf, len)) {
734 a87f6625 2021-01-24 op case -1:
735 abc007d2 2021-02-08 op close_conn(fd, ev, c);
736 a87f6625 2021-01-24 op return;
737 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
738 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_start_reply);
739 a87f6625 2021-01-24 op return;
740 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
741 74c0c7e4 2021-04-20 op yield_write(fd, c, &handle_start_reply);
742 a87f6625 2021-01-24 op return;
743 d3a08f4d 2021-01-17 op }
744 a87f6625 2021-01-24 op
745 4604dc96 2021-02-23 op if (!vhost_disable_log(c->host, c->iri.path))
746 4604dc96 2021-02-23 op log_request(c, buf, sizeof(buf));
747 a87f6625 2021-01-24 op
748 abc007d2 2021-02-08 op if (c->code != SUCCESS)
749 abc007d2 2021-02-08 op close_conn(fd, ev, c);
750 abc007d2 2021-02-08 op else
751 abc007d2 2021-02-08 op c->next(fd, ev, c);
752 d3a08f4d 2021-01-17 op }
753 d3a08f4d 2021-01-17 op
754 b8e64ccd 2021-03-31 op static size_t
755 b8e64ccd 2021-03-31 op host_nth(struct vhost *h)
756 b8e64ccd 2021-03-31 op {
757 b8e64ccd 2021-03-31 op struct vhost *v;
758 b8e64ccd 2021-03-31 op size_t i = 0;
759 b8e64ccd 2021-03-31 op
760 b8e64ccd 2021-03-31 op TAILQ_FOREACH(v, &hosts, vhosts) {
761 b8e64ccd 2021-03-31 op if (v == h)
762 b8e64ccd 2021-03-31 op return i;
763 b8e64ccd 2021-03-31 op i++;
764 b8e64ccd 2021-03-31 op }
765 b8e64ccd 2021-03-31 op
766 b8e64ccd 2021-03-31 op abort();
767 b8e64ccd 2021-03-31 op }
768 b8e64ccd 2021-03-31 op
769 fe406389 2021-02-02 op static void
770 abc007d2 2021-02-08 op start_cgi(const char *spath, const char *relpath, struct client *c)
771 d3a08f4d 2021-01-17 op {
772 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
773 bc99d868 2021-03-19 op const char *t;
774 bc99d868 2021-03-19 op struct cgireq req;
775 d3a08f4d 2021-01-17 op int e;
776 d3a08f4d 2021-01-17 op
777 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
778 d3a08f4d 2021-01-17 op addr, sizeof(addr),
779 d3a08f4d 2021-01-17 op NULL, 0,
780 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
781 d3a08f4d 2021-01-17 op if (e != 0)
782 bc99d868 2021-03-19 op fatal("getnameinfo failed");
783 d3a08f4d 2021-01-17 op
784 bc99d868 2021-03-19 op memset(&req, 0, sizeof(req));
785 d3a08f4d 2021-01-17 op
786 bc99d868 2021-03-19 op memcpy(req.buf, c->req, sizeof(req.buf));
787 bc99d868 2021-03-19 op
788 bc99d868 2021-03-19 op req.iri_schema_off = c->iri.schema - c->req;
789 bc99d868 2021-03-19 op req.iri_host_off = c->iri.host - c->req;
790 bc99d868 2021-03-19 op req.iri_port_off = c->iri.port - c->req;
791 bc99d868 2021-03-19 op req.iri_path_off = c->iri.path - c->req;
792 bc99d868 2021-03-19 op req.iri_query_off = c->iri.query - c->req;
793 bc99d868 2021-03-19 op req.iri_fragment_off = c->iri.fragment - c->req;
794 35744950 2021-02-01 op
795 bc99d868 2021-03-19 op req.iri_portno = c->iri.port_no;
796 d3a08f4d 2021-01-17 op
797 bc99d868 2021-03-19 op strlcpy(req.spath, spath, sizeof(req.spath));
798 bc99d868 2021-03-19 op strlcpy(req.relpath, relpath, sizeof(req.relpath));
799 bc99d868 2021-03-19 op strlcpy(req.addr, addr, sizeof(req.addr));
800 bc99d868 2021-03-19 op
801 bc99d868 2021-03-19 op if ((t = tls_peer_cert_subject(c->ctx)) != NULL)
802 bc99d868 2021-03-19 op strlcpy(req.subject, t, sizeof(req.subject));
803 bc99d868 2021-03-19 op if ((t = tls_peer_cert_issuer(c->ctx)) != NULL)
804 bc99d868 2021-03-19 op strlcpy(req.issuer, t, sizeof(req.issuer));
805 bc99d868 2021-03-19 op if ((t = tls_peer_cert_hash(c->ctx)) != NULL)
806 bc99d868 2021-03-19 op strlcpy(req.hash, t, sizeof(req.hash));
807 89541eee 2021-04-13 op if ((t = tls_conn_version(c->ctx)) != NULL)
808 89541eee 2021-04-13 op strlcpy(req.version, t, sizeof(req.version));
809 89541eee 2021-04-13 op if ((t = tls_conn_cipher(c->ctx)) != NULL)
810 89541eee 2021-04-13 op strlcpy(req.cipher, t, sizeof(req.cipher));
811 bc99d868 2021-03-19 op
812 89541eee 2021-04-13 op req.cipher_strength = tls_conn_cipher_strength(c->ctx);
813 bc99d868 2021-03-19 op req.notbefore = tls_peer_cert_notbefore(c->ctx);
814 bc99d868 2021-03-19 op req.notafter = tls_peer_cert_notafter(c->ctx);
815 bc99d868 2021-03-19 op
816 b8e64ccd 2021-03-31 op req.host_off = host_nth(c->host);
817 1feaf2a6 2021-05-15 op req.loc_off = c->loc;
818 bc99d868 2021-03-19 op
819 bc99d868 2021-03-19 op imsg_compose(&exibuf, IMSG_CGI_REQ, c->id, 0, -1, &req, sizeof(req));
820 bc99d868 2021-03-19 op imsg_flush(&exibuf);
821 bc99d868 2021-03-19 op
822 bc99d868 2021-03-19 op close(c->pfd);
823 d3a08f4d 2021-01-17 op }
824 d3a08f4d 2021-01-17 op
825 fe406389 2021-02-02 op static void
826 abc007d2 2021-02-08 op open_dir(struct client *c)
827 d3a08f4d 2021-01-17 op {
828 d3a08f4d 2021-01-17 op size_t len;
829 11c98667 2021-04-25 op int dirfd, root;
830 252908e6 2021-01-24 op char *before_file;
831 11c98667 2021-04-25 op
832 11c98667 2021-04-25 op root = !strcmp(c->iri.path, "/") || *c->iri.path == '\0';
833 d1ca3911 2021-01-21 op
834 0be51733 2021-01-20 op len = strlen(c->iri.path);
835 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
836 abc007d2 2021-02-08 op redirect_canonical_dir(c);
837 0be51733 2021-01-20 op return;
838 d3a08f4d 2021-01-17 op }
839 d3a08f4d 2021-01-17 op
840 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
841 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
842 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
843 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
844 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
845 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
846 c8b74339 2021-01-24 op sizeof(c->sbuf));
847 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
848 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
849 252908e6 2021-01-24 op return;
850 252908e6 2021-01-24 op }
851 d3a08f4d 2021-01-17 op
852 252908e6 2021-01-24 op c->iri.path = c->sbuf;
853 252908e6 2021-01-24 op
854 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
855 abc007d2 2021-02-08 op dirfd = c->pfd;
856 abc007d2 2021-02-08 op c->pfd = -1;
857 252908e6 2021-01-24 op
858 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
859 252908e6 2021-01-24 op case FILE_EXECUTABLE:
860 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
861 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
862 252908e6 2021-01-24 op break;
863 252908e6 2021-01-24 op }
864 252908e6 2021-01-24 op
865 252908e6 2021-01-24 op /* fallthrough */
866 252908e6 2021-01-24 op
867 252908e6 2021-01-24 op case FILE_EXISTS:
868 98ee8406 2021-02-12 op c->next = handle_copy;
869 98ee8406 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
870 252908e6 2021-01-24 op break;
871 252908e6 2021-01-24 op
872 252908e6 2021-01-24 op case FILE_DIRECTORY:
873 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
874 252908e6 2021-01-24 op break;
875 252908e6 2021-01-24 op
876 252908e6 2021-01-24 op case FILE_MISSING:
877 252908e6 2021-01-24 op *before_file = '\0';
878 252908e6 2021-01-24 op
879 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
880 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
881 252908e6 2021-01-24 op break;
882 252908e6 2021-01-24 op }
883 252908e6 2021-01-24 op
884 5f715ce4 2021-02-02 op c->next = enter_handle_dirlist;
885 252908e6 2021-01-24 op
886 e76f2c74 2021-04-25 op c->dirlen = scandir_fd(dirfd, &c->dir,
887 11c98667 2021-04-25 op root ? select_non_dotdot : select_non_dot,
888 11c98667 2021-04-25 op alphasort);
889 11c98667 2021-04-25 op if (c->dirlen == -1) {
890 11c98667 2021-04-25 op log_err(c, "scandir_fd(%d) (vhost:%s) %s: %s",
891 abc007d2 2021-02-08 op c->pfd, c->host->domain, c->iri.path, strerror(errno));
892 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
893 252908e6 2021-01-24 op return;
894 252908e6 2021-01-24 op }
895 11c98667 2021-04-25 op c->diroff = 0;
896 252908e6 2021-01-24 op c->off = 0;
897 252908e6 2021-01-24 op
898 abc007d2 2021-02-08 op start_reply(c, SUCCESS, "text/gemini");
899 252908e6 2021-01-24 op return;
900 252908e6 2021-01-24 op
901 252908e6 2021-01-24 op default:
902 252908e6 2021-01-24 op /* unreachable */
903 252908e6 2021-01-24 op abort();
904 252908e6 2021-01-24 op }
905 252908e6 2021-01-24 op
906 252908e6 2021-01-24 op close(dirfd);
907 252908e6 2021-01-24 op }
908 252908e6 2021-01-24 op
909 fe406389 2021-02-02 op static void
910 abc007d2 2021-02-08 op redirect_canonical_dir(struct client *c)
911 252908e6 2021-01-24 op {
912 252908e6 2021-01-24 op size_t len;
913 252908e6 2021-01-24 op
914 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
915 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
916 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
917 252908e6 2021-01-24 op
918 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
919 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
920 0be51733 2021-01-20 op return;
921 0be51733 2021-01-20 op }
922 0be51733 2021-01-20 op
923 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
924 d3a08f4d 2021-01-17 op }
925 5f715ce4 2021-02-02 op
926 fe406389 2021-02-02 op static void
927 abc007d2 2021-02-08 op enter_handle_dirlist(int fd, short ev, void *d)
928 5f715ce4 2021-02-02 op {
929 abc007d2 2021-02-08 op struct client *c = d;
930 5f715ce4 2021-02-02 op char b[PATH_MAX];
931 5f715ce4 2021-02-02 op size_t l;
932 d3a08f4d 2021-01-17 op
933 5f715ce4 2021-02-02 op strlcpy(b, c->iri.path, sizeof(b));
934 5f715ce4 2021-02-02 op l = snprintf(c->sbuf, sizeof(c->sbuf),
935 5f715ce4 2021-02-02 op "# Index of %s\n\n", b);
936 5f715ce4 2021-02-02 op if (l >= sizeof(c->sbuf)) {
937 a8a1f439 2021-07-07 op /*
938 a8a1f439 2021-07-07 op * This is impossible, given that we have enough space
939 5f715ce4 2021-02-02 op * in c->sbuf to hold the ancilliary string plus the
940 5f715ce4 2021-02-02 op * full path; but it wouldn't read nice without some
941 a8a1f439 2021-07-07 op * error checking, and I'd like to avoid a strlen.
942 a8a1f439 2021-07-07 op */
943 abc007d2 2021-02-08 op close_conn(fd, ev, c);
944 5f715ce4 2021-02-02 op return;
945 5f715ce4 2021-02-02 op }
946 5f715ce4 2021-02-02 op
947 abc007d2 2021-02-08 op c->len = l;
948 abc007d2 2021-02-08 op handle_dirlist(fd, ev, c);
949 5f715ce4 2021-02-02 op }
950 5f715ce4 2021-02-02 op
951 fe406389 2021-02-02 op static void
952 abc007d2 2021-02-08 op handle_dirlist(int fd, short ev, void *d)
953 5f715ce4 2021-02-02 op {
954 abc007d2 2021-02-08 op struct client *c = d;
955 5f715ce4 2021-02-02 op ssize_t r;
956 5f715ce4 2021-02-02 op
957 5f715ce4 2021-02-02 op while (c->len > 0) {
958 5f715ce4 2021-02-02 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
959 5f715ce4 2021-02-02 op case -1:
960 abc007d2 2021-02-08 op close_conn(fd, ev, c);
961 5f715ce4 2021-02-02 op return;
962 5f715ce4 2021-02-02 op case TLS_WANT_POLLOUT:
963 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_dirlist);
964 5f715ce4 2021-02-02 op return;
965 5f715ce4 2021-02-02 op case TLS_WANT_POLLIN:
966 74c0c7e4 2021-04-20 op yield_write(fd, c, &handle_dirlist);
967 5f715ce4 2021-02-02 op return;
968 5f715ce4 2021-02-02 op default:
969 5f715ce4 2021-02-02 op c->off += r;
970 5f715ce4 2021-02-02 op c->len -= r;
971 5f715ce4 2021-02-02 op }
972 5f715ce4 2021-02-02 op }
973 5f715ce4 2021-02-02 op
974 abc007d2 2021-02-08 op send_directory_listing(fd, ev, c);
975 5f715ce4 2021-02-02 op }
976 5f715ce4 2021-02-02 op
977 fe406389 2021-02-02 op static int
978 252908e6 2021-01-24 op read_next_dir_entry(struct client *c)
979 252908e6 2021-01-24 op {
980 11c98667 2021-04-25 op if (c->diroff == c->dirlen)
981 11c98667 2021-04-25 op return 0;
982 252908e6 2021-01-24 op
983 252908e6 2021-01-24 op /* XXX: url escape */
984 11c98667 2021-04-25 op snprintf(c->sbuf, sizeof(c->sbuf), "=> %s\n",
985 11c98667 2021-04-25 op c->dir[c->diroff]->d_name);
986 11c98667 2021-04-25 op
987 11c98667 2021-04-25 op free(c->dir[c->diroff]);
988 11c98667 2021-04-25 op c->diroff++;
989 11c98667 2021-04-25 op
990 252908e6 2021-01-24 op c->len = strlen(c->sbuf);
991 252908e6 2021-01-24 op c->off = 0;
992 252908e6 2021-01-24 op return 1;
993 252908e6 2021-01-24 op }
994 252908e6 2021-01-24 op
995 fe406389 2021-02-02 op static void
996 abc007d2 2021-02-08 op send_directory_listing(int fd, short ev, void *d)
997 252908e6 2021-01-24 op {
998 abc007d2 2021-02-08 op struct client *c = d;
999 252908e6 2021-01-24 op ssize_t r;
1000 252908e6 2021-01-24 op
1001 252908e6 2021-01-24 op while (1) {
1002 252908e6 2021-01-24 op if (c->len == 0) {
1003 252908e6 2021-01-24 op if (!read_next_dir_entry(c))
1004 252908e6 2021-01-24 op goto end;
1005 252908e6 2021-01-24 op }
1006 252908e6 2021-01-24 op
1007 252908e6 2021-01-24 op while (c->len > 0) {
1008 252908e6 2021-01-24 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
1009 252908e6 2021-01-24 op case -1:
1010 252908e6 2021-01-24 op goto end;
1011 252908e6 2021-01-24 op
1012 252908e6 2021-01-24 op case TLS_WANT_POLLOUT:
1013 74c0c7e4 2021-04-20 op yield_read(fd, c, &send_directory_listing);
1014 252908e6 2021-01-24 op return;
1015 252908e6 2021-01-24 op
1016 252908e6 2021-01-24 op case TLS_WANT_POLLIN:
1017 74c0c7e4 2021-04-20 op yield_write(fd, c, &send_directory_listing);
1018 252908e6 2021-01-24 op return;
1019 252908e6 2021-01-24 op
1020 252908e6 2021-01-24 op default:
1021 252908e6 2021-01-24 op c->off += r;
1022 252908e6 2021-01-24 op c->len -= r;
1023 252908e6 2021-01-24 op break;
1024 252908e6 2021-01-24 op }
1025 252908e6 2021-01-24 op }
1026 252908e6 2021-01-24 op }
1027 252908e6 2021-01-24 op
1028 252908e6 2021-01-24 op end:
1029 abc007d2 2021-02-08 op close_conn(fd, ev, d);
1030 252908e6 2021-01-24 op }
1031 252908e6 2021-01-24 op
1032 35744950 2021-02-01 op /* accumulate the meta line from the cgi script. */
1033 fe406389 2021-02-02 op static void
1034 abc007d2 2021-02-08 op handle_cgi_reply(int fd, short ev, void *d)
1035 3309ef97 2021-01-23 op {
1036 abc007d2 2021-02-08 op struct client *c = d;
1037 35744950 2021-02-01 op void *buf, *e;
1038 3309ef97 2021-01-23 op size_t len;
1039 3309ef97 2021-01-23 op ssize_t r;
1040 3309ef97 2021-01-23 op
1041 abc007d2 2021-02-08 op
1042 35744950 2021-02-01 op buf = c->sbuf + c->len;
1043 35744950 2021-02-01 op len = sizeof(c->sbuf) - c->len;
1044 3309ef97 2021-01-23 op
1045 abc007d2 2021-02-08 op r = read(c->pfd, buf, len);
1046 35744950 2021-02-01 op if (r == 0 || r == -1) {
1047 abc007d2 2021-02-08 op start_reply(c, CGI_ERROR, "CGI error");
1048 35744950 2021-02-01 op return;
1049 3309ef97 2021-01-23 op }
1050 3309ef97 2021-01-23 op
1051 3309ef97 2021-01-23 op c->len += r;
1052 3309ef97 2021-01-23 op
1053 35744950 2021-02-01 op /* TODO: error if the CGI script don't reply correctly */
1054 35744950 2021-02-01 op e = strchr(c->sbuf, '\n');
1055 35744950 2021-02-01 op if (e != NULL || c->len == sizeof(c->sbuf)) {
1056 3309ef97 2021-01-23 op log_request(c, c->sbuf, c->len);
1057 3309ef97 2021-01-23 op
1058 35744950 2021-02-01 op c->off = 0;
1059 27b2fa9a 2021-02-12 op handle_copy(fd, ev, c);
1060 35744950 2021-02-01 op return;
1061 35744950 2021-02-01 op }
1062 abc007d2 2021-02-08 op
1063 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_cgi_reply);
1064 3309ef97 2021-01-23 op }
1065 3309ef97 2021-01-23 op
1066 fe406389 2021-02-02 op static void
1067 27b2fa9a 2021-02-12 op handle_copy(int fd, short ev, void *d)
1068 d3a08f4d 2021-01-17 op {
1069 abc007d2 2021-02-08 op struct client *c = d;
1070 d3a08f4d 2021-01-17 op ssize_t r;
1071 d3a08f4d 2021-01-17 op
1072 d3a08f4d 2021-01-17 op while (1) {
1073 d3a08f4d 2021-01-17 op while (c->len > 0) {
1074 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
1075 d3a08f4d 2021-01-17 op case -1:
1076 d3a08f4d 2021-01-17 op goto end;
1077 d3a08f4d 2021-01-17 op
1078 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
1079 74c0c7e4 2021-04-20 op yield_write(c->fd, c, &handle_copy);
1080 d3a08f4d 2021-01-17 op return;
1081 d3a08f4d 2021-01-17 op
1082 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
1083 74c0c7e4 2021-04-20 op yield_read(c->fd, c, &handle_copy);
1084 d3a08f4d 2021-01-17 op return;
1085 d3a08f4d 2021-01-17 op
1086 d3a08f4d 2021-01-17 op default:
1087 d3a08f4d 2021-01-17 op c->off += r;
1088 d3a08f4d 2021-01-17 op c->len -= r;
1089 d3a08f4d 2021-01-17 op break;
1090 d3a08f4d 2021-01-17 op }
1091 d3a08f4d 2021-01-17 op }
1092 27b2fa9a 2021-02-12 op
1093 27b2fa9a 2021-02-12 op switch (r = read(c->pfd, c->sbuf, sizeof(c->sbuf))) {
1094 27b2fa9a 2021-02-12 op case 0:
1095 27b2fa9a 2021-02-12 op goto end;
1096 27b2fa9a 2021-02-12 op case -1:
1097 27b2fa9a 2021-02-12 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
1098 74c0c7e4 2021-04-20 op yield_read(c->pfd, c, &handle_copy);
1099 27b2fa9a 2021-02-12 op return;
1100 27b2fa9a 2021-02-12 op }
1101 27b2fa9a 2021-02-12 op goto end;
1102 27b2fa9a 2021-02-12 op default:
1103 27b2fa9a 2021-02-12 op c->len = r;
1104 27b2fa9a 2021-02-12 op c->off = 0;
1105 27b2fa9a 2021-02-12 op }
1106 d3a08f4d 2021-01-17 op }
1107 d3a08f4d 2021-01-17 op
1108 d3a08f4d 2021-01-17 op end:
1109 abc007d2 2021-02-08 op close_conn(c->fd, ev, d);
1110 d3a08f4d 2021-01-17 op }
1111 d3a08f4d 2021-01-17 op
1112 8ad1c570 2021-05-09 op void
1113 abc007d2 2021-02-08 op close_conn(int fd, short ev, void *d)
1114 d3a08f4d 2021-01-17 op {
1115 8ad1c570 2021-05-09 op struct client *c = d;
1116 8ad1c570 2021-05-09 op struct mbuf *mbuf;
1117 d3a08f4d 2021-01-17 op
1118 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
1119 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
1120 24d362cd 2021-06-12 op yield_read(c->fd, c, &close_conn);
1121 d3a08f4d 2021-01-17 op return;
1122 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
1123 24d362cd 2021-06-12 op yield_read(c->fd, c, &close_conn);
1124 d3a08f4d 2021-01-17 op return;
1125 d3a08f4d 2021-01-17 op }
1126 d3a08f4d 2021-01-17 op
1127 d3a08f4d 2021-01-17 op connected_clients--;
1128 d3a08f4d 2021-01-17 op
1129 8ad1c570 2021-05-09 op while ((mbuf = TAILQ_FIRST(&c->mbufhead)) != NULL) {
1130 8ad1c570 2021-05-09 op TAILQ_REMOVE(&c->mbufhead, mbuf, mbufs);
1131 8ad1c570 2021-05-09 op free(mbuf);
1132 8ad1c570 2021-05-09 op }
1133 8ad1c570 2021-05-09 op
1134 d3a08f4d 2021-01-17 op tls_free(c->ctx);
1135 d3a08f4d 2021-01-17 op c->ctx = NULL;
1136 d3a08f4d 2021-01-17 op
1137 abc007d2 2021-02-08 op if (c->pfd != -1)
1138 abc007d2 2021-02-08 op close(c->pfd);
1139 d3a08f4d 2021-01-17 op
1140 252908e6 2021-01-24 op if (c->dir != NULL)
1141 11c98667 2021-04-25 op free(c->dir);
1142 252908e6 2021-01-24 op
1143 abc007d2 2021-02-08 op close(c->fd);
1144 abc007d2 2021-02-08 op c->fd = -1;
1145 f890c8c5 2021-01-22 op }
1146 f890c8c5 2021-01-22 op
1147 fe406389 2021-02-02 op static void
1148 abc007d2 2021-02-08 op do_accept(int sock, short et, void *d)
1149 d3a08f4d 2021-01-17 op {
1150 abc007d2 2021-02-08 op struct client *c;
1151 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
1152 abc007d2 2021-02-08 op struct sockaddr *saddr;
1153 d3a08f4d 2021-01-17 op socklen_t len;
1154 abc007d2 2021-02-08 op int i, fd;
1155 d3a08f4d 2021-01-17 op
1156 abc007d2 2021-02-08 op (void)et;
1157 abc007d2 2021-02-08 op
1158 abc007d2 2021-02-08 op saddr = (struct sockaddr*)&addr;
1159 d3a08f4d 2021-01-17 op len = sizeof(addr);
1160 3cb3dd4d 2021-02-12 op if ((fd = accept(sock, saddr, &len)) == -1) {
1161 cfb8a77f 2021-02-07 op if (errno == EWOULDBLOCK || errno == EAGAIN)
1162 d3a08f4d 2021-01-17 op return;
1163 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
1164 d3a08f4d 2021-01-17 op }
1165 d3a08f4d 2021-01-17 op
1166 3cb3dd4d 2021-02-12 op mark_nonblock(fd);
1167 3cb3dd4d 2021-02-12 op
1168 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
1169 bc99d868 2021-03-19 op c = &clients[i];
1170 abc007d2 2021-02-08 op if (c->fd == -1) {
1171 abc007d2 2021-02-08 op memset(c, 0, sizeof(*c));
1172 bc99d868 2021-03-19 op c->id = i;
1173 bc99d868 2021-03-19 op if (tls_accept_socket(ctx, &c->ctx, fd) == -1)
1174 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
1175 d3a08f4d 2021-01-17 op
1176 abc007d2 2021-02-08 op c->fd = fd;
1177 abc007d2 2021-02-08 op c->pfd = -1;
1178 abc007d2 2021-02-08 op c->dir = NULL;
1179 abc007d2 2021-02-08 op c->addr = addr;
1180 8ad1c570 2021-05-09 op c->fcgi = -1;
1181 83fe545a 2021-09-26 op
1182 83fe545a 2021-09-26 op TAILQ_INIT(&c->mbufhead);
1183 d3a08f4d 2021-01-17 op
1184 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_handshake);
1185 d3a08f4d 2021-01-17 op connected_clients++;
1186 d3a08f4d 2021-01-17 op return;
1187 d3a08f4d 2021-01-17 op }
1188 d3a08f4d 2021-01-17 op }
1189 d3a08f4d 2021-01-17 op
1190 d3a08f4d 2021-01-17 op close(fd);
1191 d3a08f4d 2021-01-17 op }
1192 d3a08f4d 2021-01-17 op
1193 8ad1c570 2021-05-09 op static struct client *
1194 bc99d868 2021-03-19 op client_by_id(int id)
1195 bc99d868 2021-03-19 op {
1196 bc99d868 2021-03-19 op if ((size_t)id > sizeof(clients)/sizeof(clients[0]))
1197 bc99d868 2021-03-19 op fatal("in client_by_id: invalid id %d", id);
1198 bc99d868 2021-03-19 op return &clients[id];
1199 bc99d868 2021-03-19 op }
1200 bc99d868 2021-03-19 op
1201 8ad1c570 2021-05-09 op struct client *
1202 8ad1c570 2021-05-09 op try_client_by_id(int id)
1203 8ad1c570 2021-05-09 op {
1204 8ad1c570 2021-05-09 op if ((size_t)id > sizeof(clients)/sizeof(clients[0]))
1205 8ad1c570 2021-05-09 op return NULL;
1206 8ad1c570 2021-05-09 op return &clients[id];
1207 8ad1c570 2021-05-09 op }
1208 8ad1c570 2021-05-09 op
1209 abc007d2 2021-02-08 op static void
1210 bc99d868 2021-03-19 op handle_imsg_cgi_res(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1211 d3a08f4d 2021-01-17 op {
1212 bc99d868 2021-03-19 op struct client *c;
1213 d3a08f4d 2021-01-17 op
1214 bc99d868 2021-03-19 op c = client_by_id(imsg->hdr.peerid);
1215 bc99d868 2021-03-19 op
1216 bc99d868 2021-03-19 op if ((c->pfd = imsg->fd) == -1)
1217 bc99d868 2021-03-19 op start_reply(c, TEMP_FAILURE, "internal server error");
1218 bc99d868 2021-03-19 op else
1219 74c0c7e4 2021-04-20 op yield_read(c->pfd, c, &handle_cgi_reply);
1220 8ad1c570 2021-05-09 op }
1221 8ad1c570 2021-05-09 op
1222 8ad1c570 2021-05-09 op static void
1223 8ad1c570 2021-05-09 op handle_imsg_fcgi_fd(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1224 8ad1c570 2021-05-09 op {
1225 8ad1c570 2021-05-09 op struct client *c;
1226 8ad1c570 2021-05-09 op struct fcgi *f;
1227 8ad1c570 2021-05-09 op int i, id;
1228 8ad1c570 2021-05-09 op
1229 8ad1c570 2021-05-09 op id = imsg->hdr.peerid;
1230 8ad1c570 2021-05-09 op f = &fcgi[id];
1231 8ad1c570 2021-05-09 op
1232 741b69be 2021-09-26 op if ((f->fd = imsg->fd) == -1)
1233 8ad1c570 2021-05-09 op f->s = FCGI_OFF;
1234 741b69be 2021-09-26 op else {
1235 741b69be 2021-09-26 op mark_nonblock(f->fd);
1236 741b69be 2021-09-26 op
1237 741b69be 2021-09-26 op f->s = FCGI_READY;
1238 741b69be 2021-09-26 op
1239 741b69be 2021-09-26 op f->bev = bufferevent_new(f->fd, fcgi_read, fcgi_write,
1240 741b69be 2021-09-26 op fcgi_error, f);
1241 741b69be 2021-09-26 op if (f->bev == NULL) {
1242 741b69be 2021-09-26 op close(f->fd);
1243 741b69be 2021-09-26 op log_err(NULL, "%s: failed to allocate client buffer",
1244 741b69be 2021-09-26 op __func__);
1245 741b69be 2021-09-26 op f->s = FCGI_OFF;
1246 741b69be 2021-09-26 op }
1247 741b69be 2021-09-26 op
1248 741b69be 2021-09-26 op bufferevent_enable(f->bev, EV_READ|EV_WRITE);
1249 8ad1c570 2021-05-09 op }
1250 8ad1c570 2021-05-09 op
1251 8ad1c570 2021-05-09 op for (i = 0; i < MAX_USERS; ++i) {
1252 8ad1c570 2021-05-09 op c = &clients[i];
1253 8ad1c570 2021-05-09 op if (c->fd == -1)
1254 8ad1c570 2021-05-09 op continue;
1255 8ad1c570 2021-05-09 op if (c->fcgi != id)
1256 8ad1c570 2021-05-09 op continue;
1257 8ad1c570 2021-05-09 op
1258 741b69be 2021-09-26 op if (f->s == FCGI_OFF) {
1259 8ad1c570 2021-05-09 op c->fcgi = -1;
1260 8ad1c570 2021-05-09 op start_reply(c, TEMP_FAILURE, "internal server error");
1261 8ad1c570 2021-05-09 op } else
1262 741b69be 2021-09-26 op fcgi_req(f, c);
1263 8ad1c570 2021-05-09 op }
1264 bc99d868 2021-03-19 op }
1265 bc99d868 2021-03-19 op
1266 bc99d868 2021-03-19 op static void
1267 bc99d868 2021-03-19 op handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1268 bc99d868 2021-03-19 op {
1269 090b8a89 2021-07-06 op size_t i;
1270 090b8a89 2021-07-06 op
1271 bc99d868 2021-03-19 op (void)imsg;
1272 bc99d868 2021-03-19 op (void)len;
1273 bc99d868 2021-03-19 op
1274 a8a1f439 2021-07-07 op /*
1275 a8a1f439 2021-07-07 op * don't call event_loopbreak since we want to finish to
1276 a8a1f439 2021-07-07 op * handle the ongoing connections.
1277 a8a1f439 2021-07-07 op */
1278 bc99d868 2021-03-19 op
1279 090b8a89 2021-07-06 op shutting_down = 1;
1280 090b8a89 2021-07-06 op
1281 a6e689d7 2021-02-12 op event_del(&e4);
1282 a6e689d7 2021-02-12 op if (has_ipv6)
1283 a6e689d7 2021-02-12 op event_del(&e6);
1284 a6e689d7 2021-02-12 op if (has_siginfo)
1285 a6e689d7 2021-02-12 op signal_del(&siginfo);
1286 bc99d868 2021-03-19 op event_del(&imsgev);
1287 a6e689d7 2021-02-12 op signal_del(&sigusr2);
1288 090b8a89 2021-07-06 op
1289 090b8a89 2021-07-06 op for (i = 0; i < FCGI_MAX; ++i) {
1290 090b8a89 2021-07-06 op if (fcgi[i].path == NULL && fcgi[i].prog == NULL)
1291 090b8a89 2021-07-06 op break;
1292 090b8a89 2021-07-06 op
1293 741b69be 2021-09-26 op if (fcgi[i].bev == NULL || fcgi[i].pending != 0)
1294 090b8a89 2021-07-06 op continue;
1295 090b8a89 2021-07-06 op
1296 090b8a89 2021-07-06 op fcgi_close_backend(&fcgi[i]);
1297 090b8a89 2021-07-06 op }
1298 abc007d2 2021-02-08 op }
1299 d3a08f4d 2021-01-17 op
1300 abc007d2 2021-02-08 op static void
1301 bc99d868 2021-03-19 op handle_dispatch_imsg(int fd, short ev, void *d)
1302 bc99d868 2021-03-19 op {
1303 bc99d868 2021-03-19 op struct imsgbuf *ibuf = d;
1304 bc99d868 2021-03-19 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
1305 bc99d868 2021-03-19 op }
1306 bc99d868 2021-03-19 op
1307 bc99d868 2021-03-19 op static void
1308 abc007d2 2021-02-08 op handle_siginfo(int fd, short ev, void *d)
1309 abc007d2 2021-02-08 op {
1310 abc007d2 2021-02-08 op (void)fd;
1311 abc007d2 2021-02-08 op (void)ev;
1312 abc007d2 2021-02-08 op (void)d;
1313 d3a08f4d 2021-01-17 op
1314 abc007d2 2021-02-08 op log_info(NULL, "%d connected clients", connected_clients);
1315 abc007d2 2021-02-08 op }
1316 d3a08f4d 2021-01-17 op
1317 abc007d2 2021-02-08 op void
1318 bc99d868 2021-03-19 op loop(struct tls *ctx_, int sock4, int sock6, struct imsgbuf *ibuf)
1319 abc007d2 2021-02-08 op {
1320 abc007d2 2021-02-08 op size_t i;
1321 d3a08f4d 2021-01-17 op
1322 bc99d868 2021-03-19 op ctx = ctx_;
1323 bc99d868 2021-03-19 op
1324 abc007d2 2021-02-08 op event_init();
1325 1e3ef7ab 2021-02-03 op
1326 bc99d868 2021-03-19 op memset(&clients, 0, sizeof(clients));
1327 abc007d2 2021-02-08 op for (i = 0; i < MAX_USERS; ++i)
1328 bc99d868 2021-03-19 op clients[i].fd = -1;
1329 d3a08f4d 2021-01-17 op
1330 bc99d868 2021-03-19 op event_set(&e4, sock4, EV_READ | EV_PERSIST, &do_accept, NULL);
1331 a6e689d7 2021-02-12 op event_add(&e4, NULL);
1332 ca21e100 2021-02-04 op
1333 abc007d2 2021-02-08 op if (sock6 != -1) {
1334 a6e689d7 2021-02-12 op has_ipv6 = 1;
1335 bc99d868 2021-03-19 op event_set(&e6, sock6, EV_READ | EV_PERSIST, &do_accept, NULL);
1336 a6e689d7 2021-02-12 op event_add(&e6, NULL);
1337 d3a08f4d 2021-01-17 op }
1338 abc007d2 2021-02-08 op
1339 bc99d868 2021-03-19 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
1340 bc99d868 2021-03-19 op event_add(&imsgev, NULL);
1341 abc007d2 2021-02-08 op
1342 abc007d2 2021-02-08 op #ifdef SIGINFO
1343 a6e689d7 2021-02-12 op has_siginfo = 1;
1344 a6e689d7 2021-02-12 op signal_set(&siginfo, SIGINFO, &handle_siginfo, NULL);
1345 a6e689d7 2021-02-12 op signal_add(&siginfo, NULL);
1346 abc007d2 2021-02-08 op #endif
1347 5e3285d5 2021-02-12 op signal_set(&sigusr2, SIGUSR2, &handle_siginfo, NULL);
1348 a6e689d7 2021-02-12 op signal_add(&sigusr2, NULL);
1349 abc007d2 2021-02-08 op
1350 62e001b0 2021-03-20 op sandbox_server_process();
1351 abc007d2 2021-02-08 op event_dispatch();
1352 df58efff 2021-02-08 op _exit(0);
1353 d3a08f4d 2021-01-17 op }