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