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