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