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