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