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