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