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