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