2 d3a08f4d 2021-01-17 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 d3a08f4d 2021-01-17 op #include <sys/mman.h>
18 d3a08f4d 2021-01-17 op #include <sys/stat.h>
20 d3a08f4d 2021-01-17 op #include <netdb.h>
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>
29 d3a08f4d 2021-01-17 op #include "gmid.h"
31 d3a08f4d 2021-01-17 op int connected_clients;
34 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
36 c8b74339 2021-01-24 op struct location *loc;
38 caad0308 2021-01-27 op if (v == NULL || path == NULL)
41 6016a593 2021-01-30 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
42 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
43 c8b74339 2021-01-24 op if (loc->lang != NULL)
44 6016a593 2021-01-30 op return loc->lang;
48 6016a593 2021-01-30 op return v->locations[0].lang;
52 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
54 c8b74339 2021-01-24 op struct location *loc;
55 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
57 caad0308 2021-01-27 op if (v == NULL || path == NULL)
58 caad0308 2021-01-27 op return default_mime;
60 6016a593 2021-01-30 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
61 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
62 c8b74339 2021-01-24 op if (loc->default_mime != NULL)
63 6016a593 2021-01-30 op return loc->default_mime;
67 6016a593 2021-01-30 op if (v->locations[0].default_mime != NULL)
68 6016a593 2021-01-30 op return v->locations[0].default_mime;
69 c8b74339 2021-01-24 op return default_mime;
73 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
75 c8b74339 2021-01-24 op struct location *loc;
76 c8b74339 2021-01-24 op const char *index = "index.gmi";
78 caad0308 2021-01-27 op if (v == NULL || path == NULL)
81 6016a593 2021-01-30 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
82 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
83 c8b74339 2021-01-24 op if (loc->index != NULL)
84 6016a593 2021-01-30 op return loc->index;
88 b59f3cdd 2021-01-30 op if (v->locations[0].index != NULL)
89 b59f3cdd 2021-01-30 op return v->locations[0].index;
94 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
96 252908e6 2021-01-24 op struct location *loc;
98 6016a593 2021-01-30 op if (v == NULL || path == NULL)
101 252908e6 2021-01-24 op for (loc = v->locations; loc->match != NULL; ++loc) {
102 252908e6 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
103 6016a593 2021-01-30 op if (loc->auto_index != 0)
104 6016a593 2021-01-30 op return loc->auto_index == 1;
108 6016a593 2021-01-30 op return v->locations[0].auto_index == 1;
112 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
114 d3a08f4d 2021-01-17 op struct stat sb;
115 d1ca3911 2021-01-21 op const char *p;
118 d3a08f4d 2021-01-17 op assert(path != NULL);
120 d1ca3911 2021-01-21 op if (*path == '\0')
122 d1ca3911 2021-01-21 op else if (*path == '/')
123 d1ca3911 2021-01-21 op /* in send_dir we add an initial / (to be
124 d1ca3911 2021-01-21 op * redirect-friendly), but here we want to skip it */
129 252908e6 2021-01-24 op flags = O_RDONLY | O_NOFOLLOW;
131 252908e6 2021-01-24 op if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
132 d3a08f4d 2021-01-17 op return FILE_MISSING;
134 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
135 d3a08f4d 2021-01-17 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
136 d3a08f4d 2021-01-17 op return FILE_MISSING;
139 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
140 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
142 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
143 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
145 d3a08f4d 2021-01-17 op return FILE_EXISTS;
149 0be51733 2021-01-20 op open_file(struct pollfd *fds, struct client *c)
151 0be51733 2021-01-20 op switch (check_path(c, c->iri.path, &c->fd)) {
152 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
153 252908e6 2021-01-24 op if (starts_with(c->iri.path, c->host->cgi)) {
154 2fafa2d2 2021-02-01 op start_cgi(c->iri.path, "", fds, c);
158 d3a08f4d 2021-01-17 op /* fallthrough */
160 d3a08f4d 2021-01-17 op case FILE_EXISTS:
161 252908e6 2021-01-24 op load_file(fds, c);
164 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
165 252908e6 2021-01-24 op open_dir(fds, c);
168 d3a08f4d 2021-01-17 op case FILE_MISSING:
169 07b0a142 2021-01-24 op if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi)) {
170 2fafa2d2 2021-02-01 op check_for_cgi(fds, c);
173 a87f6625 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
177 d3a08f4d 2021-01-17 op /* unreachable */
183 252908e6 2021-01-24 op load_file(struct pollfd *fds, struct client *c)
185 252908e6 2021-01-24 op if ((c->len = filesize(c->fd)) == -1) {
186 252908e6 2021-01-24 op LOGE(c, "failed to get file size for %s", c->iri.path);
187 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
191 252908e6 2021-01-24 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
192 252908e6 2021-01-24 op c->fd, 0)) == MAP_FAILED) {
193 252908e6 2021-01-24 op LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
194 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
197 252908e6 2021-01-24 op c->i = c->buf;
198 112802ea 2021-02-01 op c->next = send_file;
199 252908e6 2021-01-24 op start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path));
203 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
204 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
205 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
206 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
207 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
210 2fafa2d2 2021-02-01 op check_for_cgi(struct pollfd *fds, struct client *c)
212 2fafa2d2 2021-02-01 op char path[PATH_MAX];
215 2fafa2d2 2021-02-01 op strlcpy(path, c->iri.path, sizeof(path));
216 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
218 d3a08f4d 2021-01-17 op /* NB: assume CGI is enabled and path matches cgi */
220 d3a08f4d 2021-01-17 op while (end > path) {
221 d3a08f4d 2021-01-17 op /* go up one level. UNIX paths are simple and POSIX
222 d3a08f4d 2021-01-17 op * dirname, with its ambiguities on if the given path
223 d3a08f4d 2021-01-17 op * is changed or not, gives me headaches. */
224 d3a08f4d 2021-01-17 op while (*end != '/')
228 d3a08f4d 2021-01-17 op switch (check_path(c, path, &c->fd)) {
229 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
230 2fafa2d2 2021-02-01 op start_cgi(path, end+1, fds, c);
232 d3a08f4d 2021-01-17 op case FILE_MISSING:
243 a87f6625 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
248 d3a08f4d 2021-01-17 op mark_nonblock(int fd)
252 d3a08f4d 2021-01-17 op if ((flags = fcntl(fd, F_GETFL)) == -1)
253 d3a08f4d 2021-01-17 op fatal("fcntl(F_GETFL): %s", strerror(errno));
254 d3a08f4d 2021-01-17 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
255 d3a08f4d 2021-01-17 op fatal("fcntl(F_SETFL): %s", strerror(errno));
259 d3a08f4d 2021-01-17 op handle_handshake(struct pollfd *fds, struct client *c)
261 d3a08f4d 2021-01-17 op struct vhost *h;
262 d3a08f4d 2021-01-17 op const char *servname;
263 a8d4a897 2021-01-29 op const char *parse_err = "unknown error";
265 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
266 d3a08f4d 2021-01-17 op case 0: /* success */
267 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
269 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
270 d3a08f4d 2021-01-17 op fds->events = POLLIN;
272 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
273 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
276 d3a08f4d 2021-01-17 op /* unreachable */
280 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
281 a8d4a897 2021-01-29 op if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
282 a8d4a897 2021-01-29 op LOGI(c, "%s", parse_err);
286 d3a08f4d 2021-01-17 op for (h = hosts; h->domain != NULL; ++h) {
287 3300cbe0 2021-01-27 op if (!fnmatch(h->domain, c->domain, 0))
291 90cb9eea 2021-01-28 op /* LOGD(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"", */
292 90cb9eea 2021-01-28 op /* servname != NULL ? servname : "(null)", */
293 90cb9eea 2021-01-28 op /* c->domain, */
294 90cb9eea 2021-01-28 op /* h->domain != NULL ? h->domain : "(null)"); */
296 d3a08f4d 2021-01-17 op if (h->domain != NULL) {
297 112802ea 2021-02-01 op c->state = handle_open_conn;
299 d3a08f4d 2021-01-17 op handle_open_conn(fds, c);
304 0ab65593 2021-01-21 op if (servname != NULL)
305 0ab65593 2021-01-21 op strncpy(c->req, servname, sizeof(c->req));
307 0ab65593 2021-01-21 op strncpy(c->req, "null", sizeof(c->req));
309 a8d4a897 2021-01-29 op start_reply(fds, c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
313 d3a08f4d 2021-01-17 op handle_open_conn(struct pollfd *fds, struct client *c)
315 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
316 3300cbe0 2021-01-27 op char decoded[DOMAIN_NAME_LEN];
318 0be51733 2021-01-20 op bzero(c->req, sizeof(c->req));
319 0be51733 2021-01-20 op bzero(&c->iri, sizeof(c->iri));
321 0be51733 2021-01-20 op switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
323 d3a08f4d 2021-01-17 op LOGE(c, "tls_read: %s", tls_error(c->ctx));
324 36162ed8 2021-01-22 op close_conn(fds, c);
327 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
328 d3a08f4d 2021-01-17 op fds->events = POLLIN;
331 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
332 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
336 c4f682f8 2021-01-27 op if (!trim_req_iri(c->req, &parse_err)
337 a2fd8013 2021-01-29 op || !parse_iri(c->req, &c->iri, &parse_err)
338 a2fd8013 2021-01-29 op || !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
339 c4f682f8 2021-01-27 op LOGI(c, "iri parse error: %s", parse_err);
340 c4f682f8 2021-01-27 op start_reply(fds, c, BAD_REQUEST, "invalid request");
344 3300cbe0 2021-01-27 op if (c->iri.port_no != conf.port
345 3300cbe0 2021-01-27 op || strcmp(c->iri.schema, "gemini")
346 3300cbe0 2021-01-27 op || strcmp(decoded, c->domain)) {
347 a87f6625 2021-01-24 op start_reply(fds, c, PROXY_REFUSED, "won't proxy request");
351 0be51733 2021-01-20 op open_file(fds, c);
355 df79b4c1 2021-01-19 op start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
357 112802ea 2021-02-01 op c->code = code;
358 112802ea 2021-02-01 op c->meta = meta;
359 112802ea 2021-02-01 op c->state = handle_start_reply;
360 112802ea 2021-02-01 op c->state(pfd, c);
364 112802ea 2021-02-01 op handle_start_reply(struct pollfd *pfd, struct client *c)
366 05c23a54 2021-01-19 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
367 c8b74339 2021-01-24 op const char *lang;
370 c8b74339 2021-01-24 op lang = vhost_lang(c->host, c->iri.path);
372 112802ea 2021-02-01 op snprintf(buf, sizeof(buf), "%d ", c->code);
373 112802ea 2021-02-01 op strlcat(buf, c->meta, sizeof(buf));
374 112802ea 2021-02-01 op if (!strcmp(c->meta, "text/gemini") && lang != NULL) {
375 05c23a54 2021-01-19 op strlcat(buf, "; lang=", sizeof(buf));
376 c8b74339 2021-01-24 op strlcat(buf, lang, sizeof(buf));
379 05c23a54 2021-01-19 op len = strlcat(buf, "\r\n", sizeof(buf));
380 0be51733 2021-01-20 op assert(len < sizeof(buf));
382 df79b4c1 2021-01-19 op switch (tls_write(c->ctx, buf, len)) {
384 a87f6625 2021-01-24 op close_conn(pfd, c);
386 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
387 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
389 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
390 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
394 a87f6625 2021-01-24 op log_request(c, buf, sizeof(buf));
396 a87f6625 2021-01-24 op /* we don't need a body */
397 a87f6625 2021-01-24 op if (c->code != SUCCESS) {
398 a87f6625 2021-01-24 op close_conn(pfd, c);
402 a87f6625 2021-01-24 op /* advance the state machine */
403 a87f6625 2021-01-24 op c->state = c->next;
404 112802ea 2021-02-01 op c->state(pfd, c);
408 2fafa2d2 2021-02-01 op start_cgi(const char *spath, const char *relpath,
409 d3a08f4d 2021-01-17 op struct pollfd *fds, struct client *c)
411 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
412 d3a08f4d 2021-01-17 op const char *ruser, *cissuer, *chash;
415 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
416 d3a08f4d 2021-01-17 op addr, sizeof(addr),
418 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
422 d3a08f4d 2021-01-17 op if (tls_peer_cert_provided(c->ctx)) {
423 d3a08f4d 2021-01-17 op ruser = tls_peer_cert_subject(c->ctx);
424 d3a08f4d 2021-01-17 op cissuer = tls_peer_cert_issuer(c->ctx);
425 d3a08f4d 2021-01-17 op chash = tls_peer_cert_hash(c->ctx);
427 d3a08f4d 2021-01-17 op ruser = NULL;
428 d3a08f4d 2021-01-17 op cissuer = NULL;
429 d3a08f4d 2021-01-17 op chash = NULL;
432 2fafa2d2 2021-02-01 op if (!send_iri(exfd, &c->iri)
433 2fafa2d2 2021-02-01 op || !send_string(exfd, spath)
434 d3a08f4d 2021-01-17 op || !send_string(exfd, relpath)
435 d3a08f4d 2021-01-17 op || !send_string(exfd, addr)
436 d3a08f4d 2021-01-17 op || !send_string(exfd, ruser)
437 d3a08f4d 2021-01-17 op || !send_string(exfd, cissuer)
438 d3a08f4d 2021-01-17 op || !send_string(exfd, chash)
439 d3a08f4d 2021-01-17 op || !send_vhost(exfd, c->host))
442 d3a08f4d 2021-01-17 op close(c->fd);
443 d3a08f4d 2021-01-17 op if ((c->fd = recv_fd(exfd)) == -1) {
444 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
447 112802ea 2021-02-01 op c->state = handle_cgi;
448 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
449 0be51733 2021-01-20 op c->code = -1;
450 d3a08f4d 2021-01-17 op /* handle_cgi(fds, c); */
454 d3a08f4d 2021-01-17 op /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
455 132cae8c 2021-01-18 op fatal("cannot talk to the executor process");
459 0be51733 2021-01-20 op send_file(struct pollfd *fds, struct client *c)
461 d3a08f4d 2021-01-17 op ssize_t ret, len;
463 d3a08f4d 2021-01-17 op len = (c->buf + c->len) - c->i;
465 d3a08f4d 2021-01-17 op while (len > 0) {
466 d3a08f4d 2021-01-17 op switch (ret = tls_write(c->ctx, c->i, len)) {
468 d3a08f4d 2021-01-17 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
469 36162ed8 2021-01-22 op close_conn(fds, c);
472 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
473 d3a08f4d 2021-01-17 op fds->events = POLLIN;
476 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
477 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
487 36162ed8 2021-01-22 op close_conn(fds, c);
491 252908e6 2021-01-24 op open_dir(struct pollfd *fds, struct client *c)
495 252908e6 2021-01-24 op char *before_file;
497 0be51733 2021-01-20 op len = strlen(c->iri.path);
498 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
499 252908e6 2021-01-24 op redirect_canonical_dir(fds, c);
503 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
504 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
505 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
506 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
507 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
508 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
509 c8b74339 2021-01-24 op sizeof(c->sbuf));
510 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
511 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
515 252908e6 2021-01-24 op c->iri.path = c->sbuf;
517 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
518 252908e6 2021-01-24 op dirfd = c->fd;
521 252908e6 2021-01-24 op switch (check_path(c, c->iri.path, &c->fd)) {
522 252908e6 2021-01-24 op case FILE_EXECUTABLE:
523 252908e6 2021-01-24 op if (starts_with(c->iri.path, c->host->cgi)) {
524 2fafa2d2 2021-02-01 op start_cgi(c->iri.path, "", fds, c);
528 252908e6 2021-01-24 op /* fallthrough */
530 252908e6 2021-01-24 op case FILE_EXISTS:
531 252908e6 2021-01-24 op load_file(fds, c);
534 252908e6 2021-01-24 op case FILE_DIRECTORY:
535 252908e6 2021-01-24 op start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
538 252908e6 2021-01-24 op case FILE_MISSING:
539 252908e6 2021-01-24 op *before_file = '\0';
541 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
542 252908e6 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
546 252908e6 2021-01-24 op c->fd = dirfd;
547 112802ea 2021-02-01 op c->next = send_directory_listing;
549 252908e6 2021-01-24 op if ((c->dir = fdopendir(c->fd)) == NULL) {
550 252908e6 2021-01-24 op LOGE(c, "can't fdopendir(%d) (vhost:%s) %s: %s",
551 252908e6 2021-01-24 op c->fd, c->host->domain, c->iri.path, strerror(errno));
552 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
557 252908e6 2021-01-24 op start_reply(fds, c, SUCCESS, "text/gemini");
561 252908e6 2021-01-24 op /* unreachable */
565 252908e6 2021-01-24 op close(dirfd);
569 252908e6 2021-01-24 op redirect_canonical_dir(struct pollfd *fds, struct client *c)
573 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
574 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
575 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
577 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
578 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
582 252908e6 2021-01-24 op start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
586 252908e6 2021-01-24 op read_next_dir_entry(struct client *c)
588 252908e6 2021-01-24 op struct dirent *d;
592 252908e6 2021-01-24 op if ((d = readdir(c->dir)) == NULL) {
593 252908e6 2021-01-24 op if (errno != 0)
594 252908e6 2021-01-24 op LOGE(c, "readdir: %s", strerror(errno));
597 252908e6 2021-01-24 op } while (!strcmp(d->d_name, "."));
599 252908e6 2021-01-24 op /* XXX: url escape */
600 252908e6 2021-01-24 op snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
601 252908e6 2021-01-24 op d->d_name, d->d_name);
602 252908e6 2021-01-24 op c->len = strlen(c->sbuf);
609 252908e6 2021-01-24 op send_directory_listing(struct pollfd *fds, struct client *c)
614 252908e6 2021-01-24 op if (c->len == 0) {
615 252908e6 2021-01-24 op if (!read_next_dir_entry(c))
619 252908e6 2021-01-24 op while (c->len > 0) {
620 252908e6 2021-01-24 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
624 252908e6 2021-01-24 op case TLS_WANT_POLLOUT:
625 252908e6 2021-01-24 op fds->events = POLLOUT;
628 252908e6 2021-01-24 op case TLS_WANT_POLLIN:
629 252908e6 2021-01-24 op fds->events = POLLIN;
641 252908e6 2021-01-24 op close_conn(fds, c);
645 d3a08f4d 2021-01-17 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
649 d3a08f4d 2021-01-17 op if (c->waiting_on_child)
651 d3a08f4d 2021-01-17 op c->waiting_on_child = 1;
653 d3a08f4d 2021-01-17 op fds->events = POLLIN;
655 d3a08f4d 2021-01-17 op fd = fds->fd;
656 d3a08f4d 2021-01-17 op fds->fd = c->fd;
661 d3a08f4d 2021-01-17 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
665 d3a08f4d 2021-01-17 op if (!c->waiting_on_child)
667 d3a08f4d 2021-01-17 op c->waiting_on_child = 0;
669 d3a08f4d 2021-01-17 op fd = fds->fd;
670 d3a08f4d 2021-01-17 op fds->fd = c->fd;
674 3309ef97 2021-01-23 op /* handle the read from the child process. Return like read(2) */
675 3309ef97 2021-01-23 op static ssize_t
676 3309ef97 2021-01-23 op read_from_cgi(struct client *c)
682 3309ef97 2021-01-23 op /* if we haven't read a whole response line, we want to
683 3309ef97 2021-01-23 op * continue reading. */
685 3309ef97 2021-01-23 op if (c->code == -1) {
686 3309ef97 2021-01-23 op buf = c->sbuf + c->len;
687 3309ef97 2021-01-23 op len = sizeof(c->sbuf) - c->len;
689 3309ef97 2021-01-23 op buf = c->sbuf;
690 3309ef97 2021-01-23 op len = sizeof(c->sbuf);
693 3309ef97 2021-01-23 op r = read(c->fd, buf, len);
694 3309ef97 2021-01-23 op if (r == 0 || r == -1)
700 3309ef97 2021-01-23 op if (c->code != -1)
703 3309ef97 2021-01-23 op if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
705 3309ef97 2021-01-23 op log_request(c, c->sbuf, c->len);
712 d3a08f4d 2021-01-17 op handle_cgi(struct pollfd *fds, struct client *c)
716 d3a08f4d 2021-01-17 op /* ensure c->fd is the child and fds->fd the client */
717 d3a08f4d 2021-01-17 op cgi_poll_on_client(fds, c);
720 3309ef97 2021-01-23 op if (c->code == -1 || c->len == 0) {
721 3309ef97 2021-01-23 op switch (r = read_from_cgi(c)) {
726 d3a08f4d 2021-01-17 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
727 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
734 3309ef97 2021-01-23 op if (c->code == -1) {
735 3309ef97 2021-01-23 op cgi_poll_on_child(fds, c);
739 d3a08f4d 2021-01-17 op while (c->len > 0) {
740 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
744 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
745 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
748 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
749 d3a08f4d 2021-01-17 op fds->events = POLLIN;
761 36162ed8 2021-01-22 op close_conn(fds, c);
765 36162ed8 2021-01-22 op close_conn(struct pollfd *pfd, struct client *c)
767 112802ea 2021-02-01 op c->state = close_conn;
769 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
770 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
771 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
773 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
774 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
778 d3a08f4d 2021-01-17 op connected_clients--;
780 d3a08f4d 2021-01-17 op tls_free(c->ctx);
781 d3a08f4d 2021-01-17 op c->ctx = NULL;
783 d3a08f4d 2021-01-17 op if (c->buf != MAP_FAILED)
784 d3a08f4d 2021-01-17 op munmap(c->buf, c->len);
786 d3a08f4d 2021-01-17 op if (c->fd != -1)
787 d3a08f4d 2021-01-17 op close(c->fd);
789 252908e6 2021-01-24 op if (c->dir != NULL)
790 252908e6 2021-01-24 op closedir(c->dir);
792 d3a08f4d 2021-01-17 op close(pfd->fd);
793 d3a08f4d 2021-01-17 op pfd->fd = -1;
797 d3a08f4d 2021-01-17 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
800 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
801 d3a08f4d 2021-01-17 op socklen_t len;
803 d3a08f4d 2021-01-17 op len = sizeof(addr);
804 d3a08f4d 2021-01-17 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
805 d3a08f4d 2021-01-17 op if (errno == EWOULDBLOCK)
807 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
810 d3a08f4d 2021-01-17 op mark_nonblock(fd);
812 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
813 d3a08f4d 2021-01-17 op if (fds[i].fd == -1) {
814 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
815 d3a08f4d 2021-01-17 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
816 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
818 d3a08f4d 2021-01-17 op fds[i].fd = fd;
819 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
821 112802ea 2021-02-01 op clients[i].state = handle_handshake;
822 112802ea 2021-02-01 op clients[i].next = send_file;
823 d3a08f4d 2021-01-17 op clients[i].fd = -1;
824 d3a08f4d 2021-01-17 op clients[i].waiting_on_child = 0;
825 d3a08f4d 2021-01-17 op clients[i].buf = MAP_FAILED;
826 252908e6 2021-01-24 op clients[i].dir = NULL;
827 d3a08f4d 2021-01-17 op clients[i].addr = addr;
829 d3a08f4d 2021-01-17 op connected_clients++;
838 d3a08f4d 2021-01-17 op loop(struct tls *ctx, int sock4, int sock6)
841 d3a08f4d 2021-01-17 op struct client clients[MAX_USERS];
842 d3a08f4d 2021-01-17 op struct pollfd fds[MAX_USERS];
844 d3a08f4d 2021-01-17 op connected_clients = 0;
846 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
847 d3a08f4d 2021-01-17 op fds[i].fd = -1;
848 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
849 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
852 d3a08f4d 2021-01-17 op fds[0].fd = sock4;
853 d3a08f4d 2021-01-17 op fds[1].fd = sock6;
856 d3a08f4d 2021-01-17 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
857 d3a08f4d 2021-01-17 op if (errno == EINTR) {
858 132cae8c 2021-01-18 op fprintf(stderr, "connected clients: %d\n",
859 d3a08f4d 2021-01-17 op connected_clients);
862 d3a08f4d 2021-01-17 op fatal("poll: %s", strerror(errno));
865 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; i++) {
866 d3a08f4d 2021-01-17 op if (fds[i].revents == 0)
869 d3a08f4d 2021-01-17 op if (fds[i].revents & (POLLERR|POLLNVAL))
870 d3a08f4d 2021-01-17 op fatal("bad fd %d: %s", fds[i].fd,
871 d3a08f4d 2021-01-17 op strerror(errno));
873 d3a08f4d 2021-01-17 op if (fds[i].revents & POLLHUP) {
874 d3a08f4d 2021-01-17 op /* fds[i] may be the fd of the stdin
875 d3a08f4d 2021-01-17 op * of a cgi script that has exited. */
876 d3a08f4d 2021-01-17 op if (!clients[i].waiting_on_child) {
877 36162ed8 2021-01-22 op close_conn(&fds[i], &clients[i]);
882 d3a08f4d 2021-01-17 op if (fds[i].fd == sock4)
883 d3a08f4d 2021-01-17 op do_accept(sock4, ctx, fds, clients);
884 d3a08f4d 2021-01-17 op else if (fds[i].fd == sock6)
885 d3a08f4d 2021-01-17 op do_accept(sock6, ctx, fds, clients);
887 112802ea 2021-02-01 op clients[i].state(&fds[i], &clients[i]);