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 d3a08f4d 2021-01-17 op
29 bc99d868 2021-03-19 op static struct client clients[MAX_USERS];
30 bc99d868 2021-03-19 op static struct tls *ctx;
31 abc007d2 2021-02-08 op
32 bc99d868 2021-03-19 op static struct event e4, e6, imsgev, siginfo, sigusr2;
33 6b191ed5 2021-02-23 op static int has_ipv6, has_siginfo;
34 abc007d2 2021-02-08 op
35 d3a08f4d 2021-01-17 op int connected_clients;
36 d3a08f4d 2021-01-17 op
37 49b73ba1 2021-02-10 op static inline int matches(const char*, const char*);
38 49b73ba1 2021-02-10 op
39 74c0c7e4 2021-04-20 op static inline void yield_read(int, struct client*, statefn);
40 74c0c7e4 2021-04-20 op static inline void yield_write(int, struct client*, statefn);
41 abc007d2 2021-02-08 op
42 fe406389 2021-02-02 op static int check_path(struct client*, const char*, int*);
43 abc007d2 2021-02-08 op static void open_file(struct client*);
44 abc007d2 2021-02-08 op static void check_for_cgi(struct client*);
45 abc007d2 2021-02-08 op static void handle_handshake(int, short, void*);
46 57ec3e77 2021-02-08 op static char *strip_path(char*, int);
47 57ec3e77 2021-02-08 op static void fmt_sbuf(const char*, struct client*, const char*);
48 abc007d2 2021-02-08 op static int apply_block_return(struct client*);
49 02be96c6 2021-02-09 op static int apply_require_ca(struct client*);
50 abc007d2 2021-02-08 op static void handle_open_conn(int, short, void*);
51 abc007d2 2021-02-08 op static void start_reply(struct client*, int, const char*);
52 abc007d2 2021-02-08 op static void handle_start_reply(int, short, void*);
53 b8e64ccd 2021-03-31 op static size_t host_nth(struct vhost*);
54 abc007d2 2021-02-08 op static void start_cgi(const char*, const char*, struct client*);
55 abc007d2 2021-02-08 op static void open_dir(struct client*);
56 abc007d2 2021-02-08 op static void redirect_canonical_dir(struct client*);
57 abc007d2 2021-02-08 op static void enter_handle_dirlist(int, short, void*);
58 abc007d2 2021-02-08 op static void handle_dirlist(int, short, void*);
59 fe406389 2021-02-02 op static int read_next_dir_entry(struct client*);
60 abc007d2 2021-02-08 op static void send_directory_listing(int, short, void*);
61 abc007d2 2021-02-08 op static void handle_cgi_reply(int, short, void*);
62 27b2fa9a 2021-02-12 op static void handle_copy(int, short, void*);
63 abc007d2 2021-02-08 op static void close_conn(int, short, void*);
64 abc007d2 2021-02-08 op static void do_accept(int, short, void*);
65 bc99d868 2021-03-19 op struct client *client_by_id(int);
66 bc99d868 2021-03-19 op static void handle_imsg_cgi_res(struct imsgbuf*, struct imsg*, size_t);
67 bc99d868 2021-03-19 op static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
68 bc99d868 2021-03-19 op static void handle_siginfo(int, short, void*);
69 bc99d868 2021-03-19 op
70 bc99d868 2021-03-19 op static imsg_handlerfn *handlers[] = {
71 bc99d868 2021-03-19 op [IMSG_QUIT] = handle_imsg_quit,
72 bc99d868 2021-03-19 op [IMSG_CGI_RES] = handle_imsg_cgi_res,
73 bc99d868 2021-03-19 op };
74 fe406389 2021-02-02 op
75 49b73ba1 2021-02-10 op static inline int
76 49b73ba1 2021-02-10 op matches(const char *pattern, const char *path)
77 49b73ba1 2021-02-10 op {
78 49b73ba1 2021-02-10 op if (*path == '/')
79 49b73ba1 2021-02-10 op path++;
80 49b73ba1 2021-02-10 op return !fnmatch(pattern, path, 0);
81 49b73ba1 2021-02-10 op }
82 49b73ba1 2021-02-10 op
83 abc007d2 2021-02-08 op static inline void
84 74c0c7e4 2021-04-20 op yield_read(int fd, struct client *c, statefn fn)
85 abc007d2 2021-02-08 op {
86 abc007d2 2021-02-08 op event_once(fd, EV_READ, fn, c, NULL);
87 abc007d2 2021-02-08 op }
88 abc007d2 2021-02-08 op
89 c39b26d3 2021-02-12 op static inline void
90 74c0c7e4 2021-04-20 op yield_write(int fd, struct client *c, statefn fn)
91 abc007d2 2021-02-08 op {
92 abc007d2 2021-02-08 op event_once(fd, EV_WRITE, fn, c, NULL);
93 abc007d2 2021-02-08 op }
94 abc007d2 2021-02-08 op
95 c8b74339 2021-01-24 op const char *
96 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
97 c8b74339 2021-01-24 op {
98 c8b74339 2021-01-24 op struct location *loc;
99 c8b74339 2021-01-24 op
100 caad0308 2021-01-27 op if (v == NULL || path == NULL)
101 6016a593 2021-01-30 op return NULL;
102 8443bff7 2021-01-25 op
103 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
104 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
105 cd761624 2021-02-06 op if (loc->lang != NULL) {
106 49b73ba1 2021-02-10 op if (matches(loc->match, path))
107 6016a593 2021-01-30 op return loc->lang;
108 c8b74339 2021-01-24 op }
109 c8b74339 2021-01-24 op }
110 c8b74339 2021-01-24 op
111 b8e64ccd 2021-03-31 op return TAILQ_FIRST(&v->locations)->lang;
112 c8b74339 2021-01-24 op }
113 c8b74339 2021-01-24 op
114 c8b74339 2021-01-24 op const char *
115 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
116 c8b74339 2021-01-24 op {
117 c8b74339 2021-01-24 op struct location *loc;
118 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
119 caad0308 2021-01-27 op
120 caad0308 2021-01-27 op if (v == NULL || path == NULL)
121 caad0308 2021-01-27 op return default_mime;
122 c8b74339 2021-01-24 op
123 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
124 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
125 cd761624 2021-02-06 op if (loc->default_mime != NULL) {
126 49b73ba1 2021-02-10 op if (matches(loc->match, path))
127 6016a593 2021-01-30 op return loc->default_mime;
128 c8b74339 2021-01-24 op }
129 c8b74339 2021-01-24 op }
130 c8b74339 2021-01-24 op
131 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
132 b8e64ccd 2021-03-31 op if (loc->default_mime != NULL)
133 b8e64ccd 2021-03-31 op return loc->default_mime;
134 c8b74339 2021-01-24 op return default_mime;
135 c8b74339 2021-01-24 op }
136 c8b74339 2021-01-24 op
137 c8b74339 2021-01-24 op const char *
138 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
139 c8b74339 2021-01-24 op {
140 c8b74339 2021-01-24 op struct location *loc;
141 c8b74339 2021-01-24 op const char *index = "index.gmi";
142 c8b74339 2021-01-24 op
143 caad0308 2021-01-27 op if (v == NULL || path == NULL)
144 caad0308 2021-01-27 op return index;
145 caad0308 2021-01-27 op
146 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
147 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
148 cd761624 2021-02-06 op if (loc->index != NULL) {
149 49b73ba1 2021-02-10 op if (matches(loc->match, path))
150 6016a593 2021-01-30 op return loc->index;
151 c8b74339 2021-01-24 op }
152 c8b74339 2021-01-24 op }
153 c8b74339 2021-01-24 op
154 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
155 b8e64ccd 2021-03-31 op if (loc->index != NULL)
156 b8e64ccd 2021-03-31 op return loc->index;
157 c8b74339 2021-01-24 op return index;
158 c8b74339 2021-01-24 op }
159 c8b74339 2021-01-24 op
160 d3a08f4d 2021-01-17 op int
161 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
162 252908e6 2021-01-24 op {
163 252908e6 2021-01-24 op struct location *loc;
164 252908e6 2021-01-24 op
165 6016a593 2021-01-30 op if (v == NULL || path == NULL)
166 6016a593 2021-01-30 op return 0;
167 6016a593 2021-01-30 op
168 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
169 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
170 cd761624 2021-02-06 op if (loc->auto_index != 0) {
171 49b73ba1 2021-02-10 op if (matches(loc->match, path))
172 6016a593 2021-01-30 op return loc->auto_index == 1;
173 252908e6 2021-01-24 op }
174 252908e6 2021-01-24 op }
175 252908e6 2021-01-24 op
176 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
177 b8e64ccd 2021-03-31 op return loc->auto_index == 1;
178 6abda252 2021-02-06 op }
179 6abda252 2021-02-06 op
180 6abda252 2021-02-06 op int
181 6abda252 2021-02-06 op vhost_block_return(struct vhost *v, const char *path, int *code, const char **fmt)
182 6abda252 2021-02-06 op {
183 6abda252 2021-02-06 op struct location *loc;
184 6abda252 2021-02-06 op
185 6abda252 2021-02-06 op if (v == NULL || path == NULL)
186 6abda252 2021-02-06 op return 0;
187 6abda252 2021-02-06 op
188 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
189 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
190 cd761624 2021-02-06 op if (loc->block_code != 0) {
191 49b73ba1 2021-02-10 op if (matches(loc->match, path)) {
192 6abda252 2021-02-06 op *code = loc->block_code;
193 6abda252 2021-02-06 op *fmt = loc->block_fmt;
194 6abda252 2021-02-06 op return 1;
195 6abda252 2021-02-06 op }
196 6abda252 2021-02-06 op }
197 6abda252 2021-02-06 op }
198 6abda252 2021-02-06 op
199 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
200 b8e64ccd 2021-03-31 op *code = loc->block_code;
201 b8e64ccd 2021-03-31 op *fmt = loc->block_fmt;
202 b8e64ccd 2021-03-31 op return loc->block_code != 0;
203 6abda252 2021-02-06 op }
204 6abda252 2021-02-06 op
205 6abda252 2021-02-06 op int
206 fdea6aa0 2021-04-30 op vhost_dirfd(struct vhost *v, const char *path)
207 fdea6aa0 2021-04-30 op {
208 fdea6aa0 2021-04-30 op struct location *loc;
209 fdea6aa0 2021-04-30 op
210 fdea6aa0 2021-04-30 op if (v == NULL || path == NULL)
211 fdea6aa0 2021-04-30 op return -1;
212 fdea6aa0 2021-04-30 op
213 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
214 fdea6aa0 2021-04-30 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
215 fdea6aa0 2021-04-30 op if (loc->dirfd != -1)
216 fdea6aa0 2021-04-30 op if (matches(loc->match, path))
217 fdea6aa0 2021-04-30 op return loc->dirfd;
218 fdea6aa0 2021-04-30 op }
219 fdea6aa0 2021-04-30 op
220 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
221 fdea6aa0 2021-04-30 op return loc->dirfd;
222 fdea6aa0 2021-04-30 op }
223 fdea6aa0 2021-04-30 op
224 fdea6aa0 2021-04-30 op int
225 6abda252 2021-02-06 op vhost_strip(struct vhost *v, const char *path)
226 6abda252 2021-02-06 op {
227 6abda252 2021-02-06 op struct location *loc;
228 6abda252 2021-02-06 op
229 6abda252 2021-02-06 op if (v == NULL || path == NULL)
230 6abda252 2021-02-06 op return 0;
231 6abda252 2021-02-06 op
232 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
233 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
234 cd761624 2021-02-06 op if (loc->strip != 0) {
235 49b73ba1 2021-02-10 op if (matches(loc->match, path))
236 6abda252 2021-02-06 op return loc->strip;
237 6abda252 2021-02-06 op }
238 6abda252 2021-02-06 op }
239 6abda252 2021-02-06 op
240 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
241 b8e64ccd 2021-03-31 op return loc->strip;
242 252908e6 2021-01-24 op }
243 252908e6 2021-01-24 op
244 02be96c6 2021-02-09 op X509_STORE *
245 02be96c6 2021-02-09 op vhost_require_ca(struct vhost *v, const char *path)
246 02be96c6 2021-02-09 op {
247 02be96c6 2021-02-09 op struct location *loc;
248 02be96c6 2021-02-09 op
249 02be96c6 2021-02-09 op if (v == NULL || path == NULL)
250 02be96c6 2021-02-09 op return NULL;
251 02be96c6 2021-02-09 op
252 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
253 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
254 02be96c6 2021-02-09 op if (loc->reqca != NULL) {
255 49b73ba1 2021-02-10 op if (matches(loc->match, path))
256 02be96c6 2021-02-09 op return loc->reqca;
257 02be96c6 2021-02-09 op }
258 02be96c6 2021-02-09 op }
259 02be96c6 2021-02-09 op
260 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
261 b8e64ccd 2021-03-31 op return loc->reqca;
262 793835cb 2021-02-23 op }
263 793835cb 2021-02-23 op
264 793835cb 2021-02-23 op int
265 793835cb 2021-02-23 op vhost_disable_log(struct vhost *v, const char *path)
266 793835cb 2021-02-23 op {
267 793835cb 2021-02-23 op struct location *loc;
268 793835cb 2021-02-23 op
269 793835cb 2021-02-23 op if (v == NULL || path == NULL)
270 793835cb 2021-02-23 op return 0;
271 793835cb 2021-02-23 op
272 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
273 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
274 793835cb 2021-02-23 op if (loc->disable_log && matches(loc->match, path))
275 793835cb 2021-02-23 op return 1;
276 793835cb 2021-02-23 op }
277 793835cb 2021-02-23 op
278 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
279 b8e64ccd 2021-03-31 op return loc->disable_log;
280 02be96c6 2021-02-09 op }
281 02be96c6 2021-02-09 op
282 fe406389 2021-02-02 op static int
283 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
284 d3a08f4d 2021-01-17 op {
285 d3a08f4d 2021-01-17 op struct stat sb;
286 d1ca3911 2021-01-21 op const char *p;
287 fdea6aa0 2021-04-30 op int flags, dirfd, strip;
288 d3a08f4d 2021-01-17 op
289 d3a08f4d 2021-01-17 op assert(path != NULL);
290 d1ca3911 2021-01-21 op
291 fdea6aa0 2021-04-30 op /*
292 fdea6aa0 2021-04-30 op * in send_dir we add an initial / (to be redirect-friendly),
293 fdea6aa0 2021-04-30 op * but here we want to skip it
294 fdea6aa0 2021-04-30 op */
295 fdea6aa0 2021-04-30 op if (*path == '/')
296 fdea6aa0 2021-04-30 op path++;
297 fdea6aa0 2021-04-30 op
298 fdea6aa0 2021-04-30 op strip = vhost_strip(c->host, path);
299 fdea6aa0 2021-04-30 op p = strip_path(path, strip);
300 fdea6aa0 2021-04-30 op
301 fdea6aa0 2021-04-30 op if (*p == '/')
302 fdea6aa0 2021-04-30 op p = p+1;
303 fdea6aa0 2021-04-30 op if (*p == '\0')
304 d1ca3911 2021-01-21 op p = ".";
305 d1ca3911 2021-01-21 op
306 fdea6aa0 2021-04-30 op dirfd = vhost_dirfd(c->host, path);
307 fdea6aa0 2021-04-30 op log_debug(c, "check_path: strip=%d path=%s original=%s",
308 fdea6aa0 2021-04-30 op strip, p, path);
309 252908e6 2021-01-24 op flags = O_RDONLY | O_NOFOLLOW;
310 fdea6aa0 2021-04-30 op if (*fd == -1 && (*fd = openat(dirfd, p, flags)) == -1)
311 d3a08f4d 2021-01-17 op return FILE_MISSING;
312 d3a08f4d 2021-01-17 op
313 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
314 3abf91b0 2021-02-07 op log_notice(c, "failed stat for %s: %s", path, strerror(errno));
315 d3a08f4d 2021-01-17 op return FILE_MISSING;
316 d3a08f4d 2021-01-17 op }
317 d3a08f4d 2021-01-17 op
318 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
319 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
320 d3a08f4d 2021-01-17 op
321 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
322 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
323 d3a08f4d 2021-01-17 op
324 d3a08f4d 2021-01-17 op return FILE_EXISTS;
325 d3a08f4d 2021-01-17 op }
326 d3a08f4d 2021-01-17 op
327 fe406389 2021-02-02 op static void
328 abc007d2 2021-02-08 op open_file(struct client *c)
329 d3a08f4d 2021-01-17 op {
330 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
331 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
332 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
333 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
334 07b0a142 2021-01-24 op return;
335 07b0a142 2021-01-24 op }
336 d3a08f4d 2021-01-17 op
337 d3a08f4d 2021-01-17 op /* fallthrough */
338 d3a08f4d 2021-01-17 op
339 d3a08f4d 2021-01-17 op case FILE_EXISTS:
340 27b2fa9a 2021-02-12 op c->next = handle_copy;
341 27b2fa9a 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
342 07b0a142 2021-01-24 op return;
343 d3a08f4d 2021-01-17 op
344 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
345 abc007d2 2021-02-08 op open_dir(c);
346 07b0a142 2021-01-24 op return;
347 d3a08f4d 2021-01-17 op
348 d3a08f4d 2021-01-17 op case FILE_MISSING:
349 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
350 abc007d2 2021-02-08 op check_for_cgi(c);
351 07b0a142 2021-01-24 op return;
352 07b0a142 2021-01-24 op }
353 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
354 07b0a142 2021-01-24 op return;
355 d3a08f4d 2021-01-17 op
356 d3a08f4d 2021-01-17 op default:
357 d3a08f4d 2021-01-17 op /* unreachable */
358 d3a08f4d 2021-01-17 op abort();
359 252908e6 2021-01-24 op }
360 252908e6 2021-01-24 op }
361 252908e6 2021-01-24 op
362 d3a08f4d 2021-01-17 op /*
363 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
364 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
365 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
366 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
367 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
368 d3a08f4d 2021-01-17 op */
369 fe406389 2021-02-02 op static void
370 abc007d2 2021-02-08 op check_for_cgi(struct client *c)
371 d3a08f4d 2021-01-17 op {
372 2fafa2d2 2021-02-01 op char path[PATH_MAX];
373 d3a08f4d 2021-01-17 op char *end;
374 2fafa2d2 2021-02-01 op
375 2fafa2d2 2021-02-01 op strlcpy(path, c->iri.path, sizeof(path));
376 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
377 d3a08f4d 2021-01-17 op
378 d3a08f4d 2021-01-17 op while (end > path) {
379 d3a08f4d 2021-01-17 op /* go up one level. UNIX paths are simple and POSIX
380 d3a08f4d 2021-01-17 op * dirname, with its ambiguities on if the given path
381 d3a08f4d 2021-01-17 op * is changed or not, gives me headaches. */
382 d3a08f4d 2021-01-17 op while (*end != '/')
383 d3a08f4d 2021-01-17 op end--;
384 d3a08f4d 2021-01-17 op *end = '\0';
385 d3a08f4d 2021-01-17 op
386 abc007d2 2021-02-08 op switch (check_path(c, path, &c->pfd)) {
387 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
388 abc007d2 2021-02-08 op start_cgi(path, end+1, c);
389 07b0a142 2021-01-24 op return;
390 d3a08f4d 2021-01-17 op case FILE_MISSING:
391 d3a08f4d 2021-01-17 op break;
392 d3a08f4d 2021-01-17 op default:
393 d3a08f4d 2021-01-17 op goto err;
394 d3a08f4d 2021-01-17 op }
395 d3a08f4d 2021-01-17 op
396 d3a08f4d 2021-01-17 op *end = '/';
397 d3a08f4d 2021-01-17 op end--;
398 d3a08f4d 2021-01-17 op }
399 d3a08f4d 2021-01-17 op
400 d3a08f4d 2021-01-17 op err:
401 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
402 07b0a142 2021-01-24 op return;
403 d3a08f4d 2021-01-17 op }
404 d3a08f4d 2021-01-17 op
405 9b8f5ed2 2021-02-03 op void
406 9b8f5ed2 2021-02-03 op mark_nonblock(int fd)
407 9b8f5ed2 2021-02-03 op {
408 9b8f5ed2 2021-02-03 op int flags;
409 9b8f5ed2 2021-02-03 op
410 9b8f5ed2 2021-02-03 op if ((flags = fcntl(fd, F_GETFL)) == -1)
411 9b8f5ed2 2021-02-03 op fatal("fcntl(F_GETFL): %s", strerror(errno));
412 9b8f5ed2 2021-02-03 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
413 9b8f5ed2 2021-02-03 op fatal("fcntl(F_SETFL): %s", strerror(errno));
414 9b8f5ed2 2021-02-03 op }
415 9b8f5ed2 2021-02-03 op
416 fe406389 2021-02-02 op static void
417 abc007d2 2021-02-08 op handle_handshake(int fd, short ev, void *d)
418 d3a08f4d 2021-01-17 op {
419 abc007d2 2021-02-08 op struct client *c = d;
420 d3a08f4d 2021-01-17 op struct vhost *h;
421 cc8c2901 2021-04-29 op struct alist *a;
422 d3a08f4d 2021-01-17 op const char *servname;
423 a8d4a897 2021-01-29 op const char *parse_err = "unknown error";
424 d3a08f4d 2021-01-17 op
425 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
426 d3a08f4d 2021-01-17 op case 0: /* success */
427 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
428 d3a08f4d 2021-01-17 op break;
429 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
430 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_handshake);
431 d3a08f4d 2021-01-17 op return;
432 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
433 74c0c7e4 2021-04-20 op yield_write(fd, c, &handle_handshake);
434 d3a08f4d 2021-01-17 op return;
435 d3a08f4d 2021-01-17 op default:
436 d3a08f4d 2021-01-17 op /* unreachable */
437 d3a08f4d 2021-01-17 op abort();
438 d3a08f4d 2021-01-17 op }
439 d3a08f4d 2021-01-17 op
440 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
441 a8d4a897 2021-01-29 op if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
442 3abf91b0 2021-02-07 op log_info(c, "puny_decode: %s", parse_err);
443 a8d4a897 2021-01-29 op goto err;
444 a8d4a897 2021-01-29 op }
445 d3a08f4d 2021-01-17 op
446 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
447 eecad7a3 2021-02-12 op if (matches(h->domain, c->domain))
448 cc8c2901 2021-04-29 op goto found;
449 cc8c2901 2021-04-29 op TAILQ_FOREACH(a, &h->aliases, aliases) {
450 cc8c2901 2021-04-29 op if (matches(a->alias, c->domain))
451 cc8c2901 2021-04-29 op goto found;
452 cc8c2901 2021-04-29 op }
453 d3a08f4d 2021-01-17 op }
454 d3a08f4d 2021-01-17 op
455 cc8c2901 2021-04-29 op found:
456 3abf91b0 2021-02-07 op log_debug(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
457 3abf91b0 2021-02-07 op servname != NULL ? servname : "(null)",
458 3abf91b0 2021-02-07 op c->domain,
459 b8e64ccd 2021-03-31 op h != NULL ? h->domain : "(null)");
460 22c6d633 2021-01-27 op
461 b8e64ccd 2021-03-31 op if (h != NULL) {
462 d3a08f4d 2021-01-17 op c->host = h;
463 abc007d2 2021-02-08 op handle_open_conn(fd, ev, c);
464 d3a08f4d 2021-01-17 op return;
465 d3a08f4d 2021-01-17 op }
466 d3a08f4d 2021-01-17 op
467 a8d4a897 2021-01-29 op err:
468 0ab65593 2021-01-21 op if (servname != NULL)
469 0ab65593 2021-01-21 op strncpy(c->req, servname, sizeof(c->req));
470 0ab65593 2021-01-21 op else
471 0ab65593 2021-01-21 op strncpy(c->req, "null", sizeof(c->req));
472 0ab65593 2021-01-21 op
473 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
474 6abda252 2021-02-06 op }
475 6abda252 2021-02-06 op
476 57ec3e77 2021-02-08 op static char *
477 57ec3e77 2021-02-08 op strip_path(char *path, int strip)
478 6abda252 2021-02-06 op {
479 57ec3e77 2021-02-08 op char *t;
480 6abda252 2021-02-06 op
481 6abda252 2021-02-06 op while (strip > 0) {
482 6abda252 2021-02-06 op if ((t = strchr(path, '/')) == NULL) {
483 6abda252 2021-02-06 op path = strchr(path, '\0');
484 6abda252 2021-02-06 op break;
485 6abda252 2021-02-06 op }
486 6abda252 2021-02-06 op path = t;
487 6abda252 2021-02-06 op strip--;
488 6abda252 2021-02-06 op }
489 57ec3e77 2021-02-08 op
490 57ec3e77 2021-02-08 op return path;
491 57ec3e77 2021-02-08 op }
492 57ec3e77 2021-02-08 op
493 57ec3e77 2021-02-08 op static void
494 57ec3e77 2021-02-08 op fmt_sbuf(const char *fmt, struct client *c, const char *path)
495 57ec3e77 2021-02-08 op {
496 57ec3e77 2021-02-08 op size_t i;
497 57ec3e77 2021-02-08 op char buf[32];
498 6abda252 2021-02-06 op
499 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
500 6abda252 2021-02-06 op for (i = 0; *fmt; ++fmt) {
501 6abda252 2021-02-06 op if (i == sizeof(buf)-1 || *fmt == '%') {
502 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
503 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
504 6abda252 2021-02-06 op i = 0;
505 6abda252 2021-02-06 op }
506 6abda252 2021-02-06 op
507 6abda252 2021-02-06 op if (*fmt != '%') {
508 6abda252 2021-02-06 op buf[i++] = *fmt;
509 6abda252 2021-02-06 op continue;
510 6abda252 2021-02-06 op }
511 6abda252 2021-02-06 op
512 6abda252 2021-02-06 op switch (*++fmt) {
513 6abda252 2021-02-06 op case '%':
514 6abda252 2021-02-06 op strlcat(c->sbuf, "%", sizeof(c->sbuf));
515 6abda252 2021-02-06 op break;
516 6abda252 2021-02-06 op case 'p':
517 737a6b50 2021-04-30 op if (*path != '/')
518 737a6b50 2021-04-30 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
519 6abda252 2021-02-06 op strlcat(c->sbuf, path, sizeof(c->sbuf));
520 6abda252 2021-02-06 op break;
521 6abda252 2021-02-06 op case 'q':
522 6abda252 2021-02-06 op strlcat(c->sbuf, c->iri.query, sizeof(c->sbuf));
523 6abda252 2021-02-06 op break;
524 6abda252 2021-02-06 op case 'P':
525 6abda252 2021-02-06 op snprintf(buf, sizeof(buf), "%d", conf.port);
526 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
527 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
528 6abda252 2021-02-06 op break;
529 6abda252 2021-02-06 op case 'N':
530 6abda252 2021-02-06 op strlcat(c->sbuf, c->domain, sizeof(c->sbuf));
531 6abda252 2021-02-06 op break;
532 6abda252 2021-02-06 op default:
533 6abda252 2021-02-06 op fatal("%s: unknown fmt specifier %c",
534 6abda252 2021-02-06 op __func__, *fmt);
535 6abda252 2021-02-06 op }
536 6abda252 2021-02-06 op }
537 6abda252 2021-02-06 op
538 6abda252 2021-02-06 op if (i != 0)
539 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
540 57ec3e77 2021-02-08 op }
541 6abda252 2021-02-06 op
542 57ec3e77 2021-02-08 op /* 1 if a matching `block return' (and apply it), 0 otherwise */
543 57ec3e77 2021-02-08 op static int
544 57ec3e77 2021-02-08 op apply_block_return(struct client *c)
545 57ec3e77 2021-02-08 op {
546 57ec3e77 2021-02-08 op const char *fmt, *path;
547 57ec3e77 2021-02-08 op int code;
548 57ec3e77 2021-02-08 op
549 57ec3e77 2021-02-08 op if (!vhost_block_return(c->host, c->iri.path, &code, &fmt))
550 57ec3e77 2021-02-08 op return 0;
551 57ec3e77 2021-02-08 op
552 57ec3e77 2021-02-08 op path = strip_path(c->iri.path, vhost_strip(c->host, c->iri.path));
553 57ec3e77 2021-02-08 op fmt_sbuf(fmt, c, path);
554 57ec3e77 2021-02-08 op
555 abc007d2 2021-02-08 op start_reply(c, code, c->sbuf);
556 6abda252 2021-02-06 op return 1;
557 d3a08f4d 2021-01-17 op }
558 d3a08f4d 2021-01-17 op
559 02be96c6 2021-02-09 op /* 1 if matching `require client ca' fails (and apply it), 0 otherwise */
560 02be96c6 2021-02-09 op static int
561 02be96c6 2021-02-09 op apply_require_ca(struct client *c)
562 02be96c6 2021-02-09 op {
563 02be96c6 2021-02-09 op X509_STORE *store;
564 02be96c6 2021-02-09 op const uint8_t *cert;
565 02be96c6 2021-02-09 op size_t len;
566 02be96c6 2021-02-09 op
567 02be96c6 2021-02-09 op if ((store = vhost_require_ca(c->host, c->iri.path)) == NULL)
568 02be96c6 2021-02-09 op return 0;
569 02be96c6 2021-02-09 op
570 02be96c6 2021-02-09 op if (!tls_peer_cert_provided(c->ctx)) {
571 02be96c6 2021-02-09 op start_reply(c, CLIENT_CERT_REQ, "client certificate required");
572 02be96c6 2021-02-09 op return 1;
573 02be96c6 2021-02-09 op }
574 02be96c6 2021-02-09 op
575 02be96c6 2021-02-09 op cert = tls_peer_cert_chain_pem(c->ctx, &len);
576 02be96c6 2021-02-09 op if (!validate_against_ca(store, cert, len)) {
577 02be96c6 2021-02-09 op start_reply(c, CERT_NOT_AUTH, "certificate not authorised");
578 02be96c6 2021-02-09 op return 1;
579 02be96c6 2021-02-09 op }
580 02be96c6 2021-02-09 op
581 02be96c6 2021-02-09 op return 0;
582 02be96c6 2021-02-09 op }
583 02be96c6 2021-02-09 op
584 fe406389 2021-02-02 op static void
585 abc007d2 2021-02-08 op handle_open_conn(int fd, short ev, void *d)
586 d3a08f4d 2021-01-17 op {
587 abc007d2 2021-02-08 op struct client *c = d;
588 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
589 3300cbe0 2021-01-27 op char decoded[DOMAIN_NAME_LEN];
590 d3a08f4d 2021-01-17 op
591 0be51733 2021-01-20 op bzero(c->req, sizeof(c->req));
592 0be51733 2021-01-20 op bzero(&c->iri, sizeof(c->iri));
593 d3a08f4d 2021-01-17 op
594 0be51733 2021-01-20 op switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
595 d3a08f4d 2021-01-17 op case -1:
596 3abf91b0 2021-02-07 op log_err(c, "tls_read: %s", tls_error(c->ctx));
597 abc007d2 2021-02-08 op close_conn(fd, ev, c);
598 d3a08f4d 2021-01-17 op return;
599 d3a08f4d 2021-01-17 op
600 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
601 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_open_conn);
602 d3a08f4d 2021-01-17 op return;
603 d3a08f4d 2021-01-17 op
604 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
605 74c0c7e4 2021-04-20 op yield_write(fd, c, &handle_open_conn);
606 d3a08f4d 2021-01-17 op return;
607 d3a08f4d 2021-01-17 op }
608 d3a08f4d 2021-01-17 op
609 c4f682f8 2021-01-27 op if (!trim_req_iri(c->req, &parse_err)
610 a2fd8013 2021-01-29 op || !parse_iri(c->req, &c->iri, &parse_err)
611 a2fd8013 2021-01-29 op || !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
612 3abf91b0 2021-02-07 op log_info(c, "iri parse error: %s", parse_err);
613 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "invalid request");
614 d3a08f4d 2021-01-17 op return;
615 d3a08f4d 2021-01-17 op }
616 d3a08f4d 2021-01-17 op
617 3300cbe0 2021-01-27 op if (c->iri.port_no != conf.port
618 3300cbe0 2021-01-27 op || strcmp(c->iri.schema, "gemini")
619 3300cbe0 2021-01-27 op || strcmp(decoded, c->domain)) {
620 abc007d2 2021-02-08 op start_reply(c, PROXY_REFUSED, "won't proxy request");
621 d3a08f4d 2021-01-17 op return;
622 d3a08f4d 2021-01-17 op }
623 d3a08f4d 2021-01-17 op
624 02be96c6 2021-02-09 op if (apply_require_ca(c))
625 02be96c6 2021-02-09 op return;
626 02be96c6 2021-02-09 op
627 abc007d2 2021-02-08 op if (apply_block_return(c))
628 e3ddf390 2021-02-06 op return;
629 e3ddf390 2021-02-06 op
630 e3ddf390 2021-02-06 op if (c->host->entrypoint != NULL) {
631 abc007d2 2021-02-08 op start_cgi(c->host->entrypoint, c->iri.path, c);
632 e3ddf390 2021-02-06 op return;
633 e3ddf390 2021-02-06 op }
634 e3ddf390 2021-02-06 op
635 abc007d2 2021-02-08 op open_file(c);
636 d3a08f4d 2021-01-17 op }
637 d3a08f4d 2021-01-17 op
638 fe406389 2021-02-02 op static void
639 abc007d2 2021-02-08 op start_reply(struct client *c, int code, const char *meta)
640 d3a08f4d 2021-01-17 op {
641 112802ea 2021-02-01 op c->code = code;
642 112802ea 2021-02-01 op c->meta = meta;
643 abc007d2 2021-02-08 op handle_start_reply(c->fd, 0, c);
644 112802ea 2021-02-01 op }
645 112802ea 2021-02-01 op
646 fe406389 2021-02-02 op static void
647 abc007d2 2021-02-08 op handle_start_reply(int fd, short ev, void *d)
648 112802ea 2021-02-01 op {
649 abc007d2 2021-02-08 op struct client *c = d;
650 05c23a54 2021-01-19 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
651 c8b74339 2021-01-24 op const char *lang;
652 0be51733 2021-01-20 op size_t len;
653 d3a08f4d 2021-01-17 op
654 c8b74339 2021-01-24 op lang = vhost_lang(c->host, c->iri.path);
655 c8b74339 2021-01-24 op
656 112802ea 2021-02-01 op snprintf(buf, sizeof(buf), "%d ", c->code);
657 112802ea 2021-02-01 op strlcat(buf, c->meta, sizeof(buf));
658 112802ea 2021-02-01 op if (!strcmp(c->meta, "text/gemini") && lang != NULL) {
659 05c23a54 2021-01-19 op strlcat(buf, "; lang=", sizeof(buf));
660 c8b74339 2021-01-24 op strlcat(buf, lang, sizeof(buf));
661 05c23a54 2021-01-19 op }
662 05c23a54 2021-01-19 op
663 05c23a54 2021-01-19 op len = strlcat(buf, "\r\n", sizeof(buf));
664 0be51733 2021-01-20 op assert(len < sizeof(buf));
665 d3a08f4d 2021-01-17 op
666 df79b4c1 2021-01-19 op switch (tls_write(c->ctx, buf, len)) {
667 a87f6625 2021-01-24 op case -1:
668 abc007d2 2021-02-08 op close_conn(fd, ev, c);
669 a87f6625 2021-01-24 op return;
670 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
671 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_start_reply);
672 a87f6625 2021-01-24 op return;
673 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
674 74c0c7e4 2021-04-20 op yield_write(fd, c, &handle_start_reply);
675 a87f6625 2021-01-24 op return;
676 d3a08f4d 2021-01-17 op }
677 a87f6625 2021-01-24 op
678 4604dc96 2021-02-23 op if (!vhost_disable_log(c->host, c->iri.path))
679 4604dc96 2021-02-23 op log_request(c, buf, sizeof(buf));
680 a87f6625 2021-01-24 op
681 abc007d2 2021-02-08 op if (c->code != SUCCESS)
682 abc007d2 2021-02-08 op close_conn(fd, ev, c);
683 abc007d2 2021-02-08 op else
684 abc007d2 2021-02-08 op c->next(fd, ev, c);
685 d3a08f4d 2021-01-17 op }
686 d3a08f4d 2021-01-17 op
687 b8e64ccd 2021-03-31 op static size_t
688 b8e64ccd 2021-03-31 op host_nth(struct vhost *h)
689 b8e64ccd 2021-03-31 op {
690 b8e64ccd 2021-03-31 op struct vhost *v;
691 b8e64ccd 2021-03-31 op size_t i = 0;
692 b8e64ccd 2021-03-31 op
693 b8e64ccd 2021-03-31 op TAILQ_FOREACH(v, &hosts, vhosts) {
694 b8e64ccd 2021-03-31 op if (v == h)
695 b8e64ccd 2021-03-31 op return i;
696 b8e64ccd 2021-03-31 op i++;
697 b8e64ccd 2021-03-31 op }
698 b8e64ccd 2021-03-31 op
699 b8e64ccd 2021-03-31 op abort();
700 b8e64ccd 2021-03-31 op }
701 b8e64ccd 2021-03-31 op
702 fe406389 2021-02-02 op static void
703 abc007d2 2021-02-08 op start_cgi(const char *spath, const char *relpath, struct client *c)
704 d3a08f4d 2021-01-17 op {
705 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
706 bc99d868 2021-03-19 op const char *t;
707 bc99d868 2021-03-19 op struct cgireq req;
708 d3a08f4d 2021-01-17 op int e;
709 d3a08f4d 2021-01-17 op
710 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
711 d3a08f4d 2021-01-17 op addr, sizeof(addr),
712 d3a08f4d 2021-01-17 op NULL, 0,
713 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
714 d3a08f4d 2021-01-17 op if (e != 0)
715 bc99d868 2021-03-19 op fatal("getnameinfo failed");
716 d3a08f4d 2021-01-17 op
717 bc99d868 2021-03-19 op memset(&req, 0, sizeof(req));
718 d3a08f4d 2021-01-17 op
719 bc99d868 2021-03-19 op memcpy(req.buf, c->req, sizeof(req.buf));
720 bc99d868 2021-03-19 op
721 bc99d868 2021-03-19 op req.iri_schema_off = c->iri.schema - c->req;
722 bc99d868 2021-03-19 op req.iri_host_off = c->iri.host - c->req;
723 bc99d868 2021-03-19 op req.iri_port_off = c->iri.port - c->req;
724 bc99d868 2021-03-19 op req.iri_path_off = c->iri.path - c->req;
725 bc99d868 2021-03-19 op req.iri_query_off = c->iri.query - c->req;
726 bc99d868 2021-03-19 op req.iri_fragment_off = c->iri.fragment - c->req;
727 35744950 2021-02-01 op
728 bc99d868 2021-03-19 op req.iri_portno = c->iri.port_no;
729 d3a08f4d 2021-01-17 op
730 bc99d868 2021-03-19 op strlcpy(req.spath, spath, sizeof(req.spath));
731 bc99d868 2021-03-19 op strlcpy(req.relpath, relpath, sizeof(req.relpath));
732 bc99d868 2021-03-19 op strlcpy(req.addr, addr, sizeof(req.addr));
733 bc99d868 2021-03-19 op
734 bc99d868 2021-03-19 op if ((t = tls_peer_cert_subject(c->ctx)) != NULL)
735 bc99d868 2021-03-19 op strlcpy(req.subject, t, sizeof(req.subject));
736 bc99d868 2021-03-19 op if ((t = tls_peer_cert_issuer(c->ctx)) != NULL)
737 bc99d868 2021-03-19 op strlcpy(req.issuer, t, sizeof(req.issuer));
738 bc99d868 2021-03-19 op if ((t = tls_peer_cert_hash(c->ctx)) != NULL)
739 bc99d868 2021-03-19 op strlcpy(req.hash, t, sizeof(req.hash));
740 89541eee 2021-04-13 op if ((t = tls_conn_version(c->ctx)) != NULL)
741 89541eee 2021-04-13 op strlcpy(req.version, t, sizeof(req.version));
742 89541eee 2021-04-13 op if ((t = tls_conn_cipher(c->ctx)) != NULL)
743 89541eee 2021-04-13 op strlcpy(req.cipher, t, sizeof(req.cipher));
744 bc99d868 2021-03-19 op
745 89541eee 2021-04-13 op req.cipher_strength = tls_conn_cipher_strength(c->ctx);
746 bc99d868 2021-03-19 op req.notbefore = tls_peer_cert_notbefore(c->ctx);
747 bc99d868 2021-03-19 op req.notafter = tls_peer_cert_notafter(c->ctx);
748 bc99d868 2021-03-19 op
749 b8e64ccd 2021-03-31 op req.host_off = host_nth(c->host);
750 bc99d868 2021-03-19 op
751 bc99d868 2021-03-19 op imsg_compose(&exibuf, IMSG_CGI_REQ, c->id, 0, -1, &req, sizeof(req));
752 bc99d868 2021-03-19 op imsg_flush(&exibuf);
753 bc99d868 2021-03-19 op
754 bc99d868 2021-03-19 op close(c->pfd);
755 d3a08f4d 2021-01-17 op }
756 d3a08f4d 2021-01-17 op
757 fe406389 2021-02-02 op static void
758 abc007d2 2021-02-08 op open_dir(struct client *c)
759 d3a08f4d 2021-01-17 op {
760 d3a08f4d 2021-01-17 op size_t len;
761 11c98667 2021-04-25 op int dirfd, root;
762 252908e6 2021-01-24 op char *before_file;
763 11c98667 2021-04-25 op
764 11c98667 2021-04-25 op root = !strcmp(c->iri.path, "/") || *c->iri.path == '\0';
765 d1ca3911 2021-01-21 op
766 0be51733 2021-01-20 op len = strlen(c->iri.path);
767 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
768 abc007d2 2021-02-08 op redirect_canonical_dir(c);
769 0be51733 2021-01-20 op return;
770 d3a08f4d 2021-01-17 op }
771 d3a08f4d 2021-01-17 op
772 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
773 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
774 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
775 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
776 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
777 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
778 c8b74339 2021-01-24 op sizeof(c->sbuf));
779 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
780 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
781 252908e6 2021-01-24 op return;
782 252908e6 2021-01-24 op }
783 d3a08f4d 2021-01-17 op
784 252908e6 2021-01-24 op c->iri.path = c->sbuf;
785 252908e6 2021-01-24 op
786 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
787 abc007d2 2021-02-08 op dirfd = c->pfd;
788 abc007d2 2021-02-08 op c->pfd = -1;
789 252908e6 2021-01-24 op
790 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
791 252908e6 2021-01-24 op case FILE_EXECUTABLE:
792 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
793 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
794 252908e6 2021-01-24 op break;
795 252908e6 2021-01-24 op }
796 252908e6 2021-01-24 op
797 252908e6 2021-01-24 op /* fallthrough */
798 252908e6 2021-01-24 op
799 252908e6 2021-01-24 op case FILE_EXISTS:
800 98ee8406 2021-02-12 op c->next = handle_copy;
801 98ee8406 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
802 252908e6 2021-01-24 op break;
803 252908e6 2021-01-24 op
804 252908e6 2021-01-24 op case FILE_DIRECTORY:
805 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
806 252908e6 2021-01-24 op break;
807 252908e6 2021-01-24 op
808 252908e6 2021-01-24 op case FILE_MISSING:
809 252908e6 2021-01-24 op *before_file = '\0';
810 252908e6 2021-01-24 op
811 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
812 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
813 252908e6 2021-01-24 op break;
814 252908e6 2021-01-24 op }
815 252908e6 2021-01-24 op
816 5f715ce4 2021-02-02 op c->next = enter_handle_dirlist;
817 252908e6 2021-01-24 op
818 e76f2c74 2021-04-25 op c->dirlen = scandir_fd(dirfd, &c->dir,
819 11c98667 2021-04-25 op root ? select_non_dotdot : select_non_dot,
820 11c98667 2021-04-25 op alphasort);
821 11c98667 2021-04-25 op if (c->dirlen == -1) {
822 11c98667 2021-04-25 op log_err(c, "scandir_fd(%d) (vhost:%s) %s: %s",
823 abc007d2 2021-02-08 op c->pfd, c->host->domain, c->iri.path, strerror(errno));
824 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
825 252908e6 2021-01-24 op return;
826 252908e6 2021-01-24 op }
827 11c98667 2021-04-25 op c->diroff = 0;
828 252908e6 2021-01-24 op c->off = 0;
829 252908e6 2021-01-24 op
830 abc007d2 2021-02-08 op start_reply(c, SUCCESS, "text/gemini");
831 252908e6 2021-01-24 op return;
832 252908e6 2021-01-24 op
833 252908e6 2021-01-24 op default:
834 252908e6 2021-01-24 op /* unreachable */
835 252908e6 2021-01-24 op abort();
836 252908e6 2021-01-24 op }
837 252908e6 2021-01-24 op
838 252908e6 2021-01-24 op close(dirfd);
839 252908e6 2021-01-24 op }
840 252908e6 2021-01-24 op
841 fe406389 2021-02-02 op static void
842 abc007d2 2021-02-08 op redirect_canonical_dir(struct client *c)
843 252908e6 2021-01-24 op {
844 252908e6 2021-01-24 op size_t len;
845 252908e6 2021-01-24 op
846 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
847 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
848 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
849 252908e6 2021-01-24 op
850 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
851 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
852 0be51733 2021-01-20 op return;
853 0be51733 2021-01-20 op }
854 0be51733 2021-01-20 op
855 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
856 d3a08f4d 2021-01-17 op }
857 5f715ce4 2021-02-02 op
858 fe406389 2021-02-02 op static void
859 abc007d2 2021-02-08 op enter_handle_dirlist(int fd, short ev, void *d)
860 5f715ce4 2021-02-02 op {
861 abc007d2 2021-02-08 op struct client *c = d;
862 5f715ce4 2021-02-02 op char b[PATH_MAX];
863 5f715ce4 2021-02-02 op size_t l;
864 d3a08f4d 2021-01-17 op
865 5f715ce4 2021-02-02 op strlcpy(b, c->iri.path, sizeof(b));
866 5f715ce4 2021-02-02 op l = snprintf(c->sbuf, sizeof(c->sbuf),
867 5f715ce4 2021-02-02 op "# Index of %s\n\n", b);
868 5f715ce4 2021-02-02 op if (l >= sizeof(c->sbuf)) {
869 5f715ce4 2021-02-02 op /* this is impossible, given that we have enough space
870 5f715ce4 2021-02-02 op * in c->sbuf to hold the ancilliary string plus the
871 5f715ce4 2021-02-02 op * full path; but it wouldn't read nice without some
872 5f715ce4 2021-02-02 op * error checking, and I'd like to avoid a strlen. */
873 abc007d2 2021-02-08 op close_conn(fd, ev, c);
874 5f715ce4 2021-02-02 op return;
875 5f715ce4 2021-02-02 op }
876 5f715ce4 2021-02-02 op
877 abc007d2 2021-02-08 op c->len = l;
878 abc007d2 2021-02-08 op handle_dirlist(fd, ev, c);
879 5f715ce4 2021-02-02 op }
880 5f715ce4 2021-02-02 op
881 fe406389 2021-02-02 op static void
882 abc007d2 2021-02-08 op handle_dirlist(int fd, short ev, void *d)
883 5f715ce4 2021-02-02 op {
884 abc007d2 2021-02-08 op struct client *c = d;
885 5f715ce4 2021-02-02 op ssize_t r;
886 5f715ce4 2021-02-02 op
887 5f715ce4 2021-02-02 op while (c->len > 0) {
888 5f715ce4 2021-02-02 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
889 5f715ce4 2021-02-02 op case -1:
890 abc007d2 2021-02-08 op close_conn(fd, ev, c);
891 5f715ce4 2021-02-02 op return;
892 5f715ce4 2021-02-02 op case TLS_WANT_POLLOUT:
893 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_dirlist);
894 5f715ce4 2021-02-02 op return;
895 5f715ce4 2021-02-02 op case TLS_WANT_POLLIN:
896 74c0c7e4 2021-04-20 op yield_write(fd, c, &handle_dirlist);
897 5f715ce4 2021-02-02 op return;
898 5f715ce4 2021-02-02 op default:
899 5f715ce4 2021-02-02 op c->off += r;
900 5f715ce4 2021-02-02 op c->len -= r;
901 5f715ce4 2021-02-02 op }
902 5f715ce4 2021-02-02 op }
903 5f715ce4 2021-02-02 op
904 abc007d2 2021-02-08 op send_directory_listing(fd, ev, c);
905 5f715ce4 2021-02-02 op }
906 5f715ce4 2021-02-02 op
907 fe406389 2021-02-02 op static int
908 252908e6 2021-01-24 op read_next_dir_entry(struct client *c)
909 252908e6 2021-01-24 op {
910 11c98667 2021-04-25 op if (c->diroff == c->dirlen)
911 11c98667 2021-04-25 op return 0;
912 252908e6 2021-01-24 op
913 252908e6 2021-01-24 op /* XXX: url escape */
914 11c98667 2021-04-25 op snprintf(c->sbuf, sizeof(c->sbuf), "=> %s\n",
915 11c98667 2021-04-25 op c->dir[c->diroff]->d_name);
916 11c98667 2021-04-25 op
917 11c98667 2021-04-25 op free(c->dir[c->diroff]);
918 11c98667 2021-04-25 op c->diroff++;
919 11c98667 2021-04-25 op
920 252908e6 2021-01-24 op c->len = strlen(c->sbuf);
921 252908e6 2021-01-24 op c->off = 0;
922 252908e6 2021-01-24 op return 1;
923 252908e6 2021-01-24 op }
924 252908e6 2021-01-24 op
925 fe406389 2021-02-02 op static void
926 abc007d2 2021-02-08 op send_directory_listing(int fd, short ev, void *d)
927 252908e6 2021-01-24 op {
928 abc007d2 2021-02-08 op struct client *c = d;
929 252908e6 2021-01-24 op ssize_t r;
930 252908e6 2021-01-24 op
931 252908e6 2021-01-24 op while (1) {
932 252908e6 2021-01-24 op if (c->len == 0) {
933 252908e6 2021-01-24 op if (!read_next_dir_entry(c))
934 252908e6 2021-01-24 op goto end;
935 252908e6 2021-01-24 op }
936 252908e6 2021-01-24 op
937 252908e6 2021-01-24 op while (c->len > 0) {
938 252908e6 2021-01-24 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
939 252908e6 2021-01-24 op case -1:
940 252908e6 2021-01-24 op goto end;
941 252908e6 2021-01-24 op
942 252908e6 2021-01-24 op case TLS_WANT_POLLOUT:
943 74c0c7e4 2021-04-20 op yield_read(fd, c, &send_directory_listing);
944 252908e6 2021-01-24 op return;
945 252908e6 2021-01-24 op
946 252908e6 2021-01-24 op case TLS_WANT_POLLIN:
947 74c0c7e4 2021-04-20 op yield_write(fd, c, &send_directory_listing);
948 252908e6 2021-01-24 op return;
949 252908e6 2021-01-24 op
950 252908e6 2021-01-24 op default:
951 252908e6 2021-01-24 op c->off += r;
952 252908e6 2021-01-24 op c->len -= r;
953 252908e6 2021-01-24 op break;
954 252908e6 2021-01-24 op }
955 252908e6 2021-01-24 op }
956 252908e6 2021-01-24 op }
957 252908e6 2021-01-24 op
958 252908e6 2021-01-24 op end:
959 abc007d2 2021-02-08 op close_conn(fd, ev, d);
960 252908e6 2021-01-24 op }
961 252908e6 2021-01-24 op
962 35744950 2021-02-01 op /* accumulate the meta line from the cgi script. */
963 fe406389 2021-02-02 op static void
964 abc007d2 2021-02-08 op handle_cgi_reply(int fd, short ev, void *d)
965 3309ef97 2021-01-23 op {
966 abc007d2 2021-02-08 op struct client *c = d;
967 35744950 2021-02-01 op void *buf, *e;
968 3309ef97 2021-01-23 op size_t len;
969 3309ef97 2021-01-23 op ssize_t r;
970 3309ef97 2021-01-23 op
971 abc007d2 2021-02-08 op
972 35744950 2021-02-01 op buf = c->sbuf + c->len;
973 35744950 2021-02-01 op len = sizeof(c->sbuf) - c->len;
974 3309ef97 2021-01-23 op
975 abc007d2 2021-02-08 op r = read(c->pfd, buf, len);
976 35744950 2021-02-01 op if (r == 0 || r == -1) {
977 abc007d2 2021-02-08 op start_reply(c, CGI_ERROR, "CGI error");
978 35744950 2021-02-01 op return;
979 3309ef97 2021-01-23 op }
980 3309ef97 2021-01-23 op
981 3309ef97 2021-01-23 op c->len += r;
982 3309ef97 2021-01-23 op
983 35744950 2021-02-01 op /* TODO: error if the CGI script don't reply correctly */
984 35744950 2021-02-01 op e = strchr(c->sbuf, '\n');
985 35744950 2021-02-01 op if (e != NULL || c->len == sizeof(c->sbuf)) {
986 3309ef97 2021-01-23 op log_request(c, c->sbuf, c->len);
987 3309ef97 2021-01-23 op
988 35744950 2021-02-01 op c->off = 0;
989 27b2fa9a 2021-02-12 op handle_copy(fd, ev, c);
990 35744950 2021-02-01 op return;
991 35744950 2021-02-01 op }
992 abc007d2 2021-02-08 op
993 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_cgi_reply);
994 3309ef97 2021-01-23 op }
995 3309ef97 2021-01-23 op
996 fe406389 2021-02-02 op static void
997 27b2fa9a 2021-02-12 op handle_copy(int fd, short ev, void *d)
998 d3a08f4d 2021-01-17 op {
999 abc007d2 2021-02-08 op struct client *c = d;
1000 d3a08f4d 2021-01-17 op ssize_t r;
1001 d3a08f4d 2021-01-17 op
1002 d3a08f4d 2021-01-17 op while (1) {
1003 d3a08f4d 2021-01-17 op while (c->len > 0) {
1004 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
1005 d3a08f4d 2021-01-17 op case -1:
1006 d3a08f4d 2021-01-17 op goto end;
1007 d3a08f4d 2021-01-17 op
1008 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
1009 74c0c7e4 2021-04-20 op yield_write(c->fd, c, &handle_copy);
1010 d3a08f4d 2021-01-17 op return;
1011 d3a08f4d 2021-01-17 op
1012 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
1013 74c0c7e4 2021-04-20 op yield_read(c->fd, c, &handle_copy);
1014 d3a08f4d 2021-01-17 op return;
1015 d3a08f4d 2021-01-17 op
1016 d3a08f4d 2021-01-17 op default:
1017 d3a08f4d 2021-01-17 op c->off += r;
1018 d3a08f4d 2021-01-17 op c->len -= r;
1019 d3a08f4d 2021-01-17 op break;
1020 d3a08f4d 2021-01-17 op }
1021 d3a08f4d 2021-01-17 op }
1022 27b2fa9a 2021-02-12 op
1023 27b2fa9a 2021-02-12 op switch (r = read(c->pfd, c->sbuf, sizeof(c->sbuf))) {
1024 27b2fa9a 2021-02-12 op case 0:
1025 27b2fa9a 2021-02-12 op goto end;
1026 27b2fa9a 2021-02-12 op case -1:
1027 27b2fa9a 2021-02-12 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
1028 74c0c7e4 2021-04-20 op yield_read(c->pfd, c, &handle_copy);
1029 27b2fa9a 2021-02-12 op return;
1030 27b2fa9a 2021-02-12 op }
1031 27b2fa9a 2021-02-12 op goto end;
1032 27b2fa9a 2021-02-12 op default:
1033 27b2fa9a 2021-02-12 op c->len = r;
1034 27b2fa9a 2021-02-12 op c->off = 0;
1035 27b2fa9a 2021-02-12 op }
1036 d3a08f4d 2021-01-17 op }
1037 d3a08f4d 2021-01-17 op
1038 d3a08f4d 2021-01-17 op end:
1039 abc007d2 2021-02-08 op close_conn(c->fd, ev, d);
1040 d3a08f4d 2021-01-17 op }
1041 d3a08f4d 2021-01-17 op
1042 fe406389 2021-02-02 op static void
1043 abc007d2 2021-02-08 op close_conn(int fd, short ev, void *d)
1044 d3a08f4d 2021-01-17 op {
1045 abc007d2 2021-02-08 op struct client *c = d;
1046 d3a08f4d 2021-01-17 op
1047 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
1048 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
1049 74c0c7e4 2021-04-20 op yield_read(fd, c, &close_conn);
1050 d3a08f4d 2021-01-17 op return;
1051 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
1052 74c0c7e4 2021-04-20 op yield_read(fd, c, &close_conn);
1053 d3a08f4d 2021-01-17 op return;
1054 d3a08f4d 2021-01-17 op }
1055 d3a08f4d 2021-01-17 op
1056 d3a08f4d 2021-01-17 op connected_clients--;
1057 d3a08f4d 2021-01-17 op
1058 d3a08f4d 2021-01-17 op tls_free(c->ctx);
1059 d3a08f4d 2021-01-17 op c->ctx = NULL;
1060 d3a08f4d 2021-01-17 op
1061 abc007d2 2021-02-08 op if (c->pfd != -1)
1062 abc007d2 2021-02-08 op close(c->pfd);
1063 d3a08f4d 2021-01-17 op
1064 252908e6 2021-01-24 op if (c->dir != NULL)
1065 11c98667 2021-04-25 op free(c->dir);
1066 252908e6 2021-01-24 op
1067 abc007d2 2021-02-08 op close(c->fd);
1068 abc007d2 2021-02-08 op c->fd = -1;
1069 f890c8c5 2021-01-22 op }
1070 f890c8c5 2021-01-22 op
1071 fe406389 2021-02-02 op static void
1072 abc007d2 2021-02-08 op do_accept(int sock, short et, void *d)
1073 d3a08f4d 2021-01-17 op {
1074 abc007d2 2021-02-08 op struct client *c;
1075 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
1076 abc007d2 2021-02-08 op struct sockaddr *saddr;
1077 d3a08f4d 2021-01-17 op socklen_t len;
1078 abc007d2 2021-02-08 op int i, fd;
1079 d3a08f4d 2021-01-17 op
1080 abc007d2 2021-02-08 op (void)et;
1081 abc007d2 2021-02-08 op
1082 abc007d2 2021-02-08 op saddr = (struct sockaddr*)&addr;
1083 d3a08f4d 2021-01-17 op len = sizeof(addr);
1084 3cb3dd4d 2021-02-12 op if ((fd = accept(sock, saddr, &len)) == -1) {
1085 cfb8a77f 2021-02-07 op if (errno == EWOULDBLOCK || errno == EAGAIN)
1086 d3a08f4d 2021-01-17 op return;
1087 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
1088 d3a08f4d 2021-01-17 op }
1089 d3a08f4d 2021-01-17 op
1090 3cb3dd4d 2021-02-12 op mark_nonblock(fd);
1091 3cb3dd4d 2021-02-12 op
1092 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
1093 bc99d868 2021-03-19 op c = &clients[i];
1094 abc007d2 2021-02-08 op if (c->fd == -1) {
1095 abc007d2 2021-02-08 op memset(c, 0, sizeof(*c));
1096 bc99d868 2021-03-19 op c->id = i;
1097 bc99d868 2021-03-19 op if (tls_accept_socket(ctx, &c->ctx, fd) == -1)
1098 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
1099 d3a08f4d 2021-01-17 op
1100 abc007d2 2021-02-08 op c->fd = fd;
1101 abc007d2 2021-02-08 op c->pfd = -1;
1102 abc007d2 2021-02-08 op c->dir = NULL;
1103 abc007d2 2021-02-08 op c->addr = addr;
1104 d3a08f4d 2021-01-17 op
1105 74c0c7e4 2021-04-20 op yield_read(fd, c, &handle_handshake);
1106 d3a08f4d 2021-01-17 op connected_clients++;
1107 d3a08f4d 2021-01-17 op return;
1108 d3a08f4d 2021-01-17 op }
1109 d3a08f4d 2021-01-17 op }
1110 d3a08f4d 2021-01-17 op
1111 d3a08f4d 2021-01-17 op close(fd);
1112 d3a08f4d 2021-01-17 op }
1113 d3a08f4d 2021-01-17 op
1114 bc99d868 2021-03-19 op struct client *
1115 bc99d868 2021-03-19 op client_by_id(int id)
1116 bc99d868 2021-03-19 op {
1117 bc99d868 2021-03-19 op if ((size_t)id > sizeof(clients)/sizeof(clients[0]))
1118 bc99d868 2021-03-19 op fatal("in client_by_id: invalid id %d", id);
1119 bc99d868 2021-03-19 op return &clients[id];
1120 bc99d868 2021-03-19 op }
1121 bc99d868 2021-03-19 op
1122 abc007d2 2021-02-08 op static void
1123 bc99d868 2021-03-19 op handle_imsg_cgi_res(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1124 d3a08f4d 2021-01-17 op {
1125 bc99d868 2021-03-19 op struct client *c;
1126 d3a08f4d 2021-01-17 op
1127 bc99d868 2021-03-19 op c = client_by_id(imsg->hdr.peerid);
1128 bc99d868 2021-03-19 op
1129 bc99d868 2021-03-19 op if ((c->pfd = imsg->fd) == -1)
1130 bc99d868 2021-03-19 op start_reply(c, TEMP_FAILURE, "internal server error");
1131 bc99d868 2021-03-19 op else
1132 74c0c7e4 2021-04-20 op yield_read(c->pfd, c, &handle_cgi_reply);
1133 bc99d868 2021-03-19 op }
1134 bc99d868 2021-03-19 op
1135 bc99d868 2021-03-19 op static void
1136 bc99d868 2021-03-19 op handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1137 bc99d868 2021-03-19 op {
1138 bc99d868 2021-03-19 op (void)imsg;
1139 bc99d868 2021-03-19 op (void)len;
1140 bc99d868 2021-03-19 op
1141 bc99d868 2021-03-19 op /* don't call event_loopbreak since we want to finish to
1142 bc99d868 2021-03-19 op * handle the ongoing connections. */
1143 bc99d868 2021-03-19 op
1144 a6e689d7 2021-02-12 op event_del(&e4);
1145 a6e689d7 2021-02-12 op if (has_ipv6)
1146 a6e689d7 2021-02-12 op event_del(&e6);
1147 a6e689d7 2021-02-12 op if (has_siginfo)
1148 a6e689d7 2021-02-12 op signal_del(&siginfo);
1149 bc99d868 2021-03-19 op event_del(&imsgev);
1150 a6e689d7 2021-02-12 op signal_del(&sigusr2);
1151 abc007d2 2021-02-08 op }
1152 d3a08f4d 2021-01-17 op
1153 abc007d2 2021-02-08 op static void
1154 bc99d868 2021-03-19 op handle_dispatch_imsg(int fd, short ev, void *d)
1155 bc99d868 2021-03-19 op {
1156 bc99d868 2021-03-19 op struct imsgbuf *ibuf = d;
1157 bc99d868 2021-03-19 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
1158 bc99d868 2021-03-19 op }
1159 bc99d868 2021-03-19 op
1160 bc99d868 2021-03-19 op static void
1161 abc007d2 2021-02-08 op handle_siginfo(int fd, short ev, void *d)
1162 abc007d2 2021-02-08 op {
1163 abc007d2 2021-02-08 op (void)fd;
1164 abc007d2 2021-02-08 op (void)ev;
1165 abc007d2 2021-02-08 op (void)d;
1166 d3a08f4d 2021-01-17 op
1167 abc007d2 2021-02-08 op log_info(NULL, "%d connected clients", connected_clients);
1168 abc007d2 2021-02-08 op }
1169 d3a08f4d 2021-01-17 op
1170 abc007d2 2021-02-08 op void
1171 bc99d868 2021-03-19 op loop(struct tls *ctx_, int sock4, int sock6, struct imsgbuf *ibuf)
1172 abc007d2 2021-02-08 op {
1173 abc007d2 2021-02-08 op size_t i;
1174 d3a08f4d 2021-01-17 op
1175 bc99d868 2021-03-19 op ctx = ctx_;
1176 bc99d868 2021-03-19 op
1177 abc007d2 2021-02-08 op event_init();
1178 1e3ef7ab 2021-02-03 op
1179 bc99d868 2021-03-19 op memset(&clients, 0, sizeof(clients));
1180 abc007d2 2021-02-08 op for (i = 0; i < MAX_USERS; ++i)
1181 bc99d868 2021-03-19 op clients[i].fd = -1;
1182 d3a08f4d 2021-01-17 op
1183 bc99d868 2021-03-19 op event_set(&e4, sock4, EV_READ | EV_PERSIST, &do_accept, NULL);
1184 a6e689d7 2021-02-12 op event_add(&e4, NULL);
1185 ca21e100 2021-02-04 op
1186 abc007d2 2021-02-08 op if (sock6 != -1) {
1187 a6e689d7 2021-02-12 op has_ipv6 = 1;
1188 bc99d868 2021-03-19 op event_set(&e6, sock6, EV_READ | EV_PERSIST, &do_accept, NULL);
1189 a6e689d7 2021-02-12 op event_add(&e6, NULL);
1190 d3a08f4d 2021-01-17 op }
1191 abc007d2 2021-02-08 op
1192 bc99d868 2021-03-19 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
1193 bc99d868 2021-03-19 op event_add(&imsgev, NULL);
1194 abc007d2 2021-02-08 op
1195 abc007d2 2021-02-08 op #ifdef SIGINFO
1196 a6e689d7 2021-02-12 op has_siginfo = 1;
1197 a6e689d7 2021-02-12 op signal_set(&siginfo, SIGINFO, &handle_siginfo, NULL);
1198 a6e689d7 2021-02-12 op signal_add(&siginfo, NULL);
1199 abc007d2 2021-02-08 op #endif
1200 5e3285d5 2021-02-12 op signal_set(&sigusr2, SIGUSR2, &handle_siginfo, NULL);
1201 a6e689d7 2021-02-12 op signal_add(&sigusr2, NULL);
1202 abc007d2 2021-02-08 op
1203 62e001b0 2021-03-20 op sandbox_server_process();
1204 abc007d2 2021-02-08 op event_dispatch();
1205 df58efff 2021-02-08 op _exit(0);
1206 d3a08f4d 2021-01-17 op }