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