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;
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 fe406389 2021-02-02 op static void handle_open_conn(struct pollfd*, struct client*);
39 fe406389 2021-02-02 op static void start_reply(struct pollfd*, struct client*, int, const char*);
40 fe406389 2021-02-02 op static void handle_start_reply(struct pollfd*, struct client*);
41 fe406389 2021-02-02 op static void start_cgi(const char*, const char*, struct pollfd*, struct client*);
42 fe406389 2021-02-02 op static void send_file(struct pollfd*, struct client*);
43 fe406389 2021-02-02 op static void open_dir(struct pollfd*, struct client*);
44 fe406389 2021-02-02 op static void redirect_canonical_dir(struct pollfd*, struct client*);
45 fe406389 2021-02-02 op static void enter_handle_dirlist(struct pollfd*, struct client*);
46 fe406389 2021-02-02 op static void handle_dirlist(struct pollfd*, struct client*);
47 fe406389 2021-02-02 op static int read_next_dir_entry(struct client*);
48 fe406389 2021-02-02 op static void send_directory_listing(struct pollfd*, struct client*);
49 fe406389 2021-02-02 op static void cgi_poll_on_child(struct pollfd*, struct client*);
50 fe406389 2021-02-02 op static void cgi_poll_on_client(struct pollfd*, struct client*);
51 fe406389 2021-02-02 op static void handle_cgi_reply(struct pollfd*, struct client*);
52 fe406389 2021-02-02 op static void handle_cgi(struct pollfd*, struct client*);
53 fe406389 2021-02-02 op static void close_conn(struct pollfd*, struct client*);
54 fe406389 2021-02-02 op static void do_accept(int, struct tls*, struct pollfd*, struct client*);
57 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
59 c8b74339 2021-01-24 op struct location *loc;
61 caad0308 2021-01-27 op if (v == NULL || path == NULL)
64 6016a593 2021-01-30 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
65 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
66 c8b74339 2021-01-24 op if (loc->lang != NULL)
67 6016a593 2021-01-30 op return loc->lang;
71 6016a593 2021-01-30 op return v->locations[0].lang;
75 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
77 c8b74339 2021-01-24 op struct location *loc;
78 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
80 caad0308 2021-01-27 op if (v == NULL || path == NULL)
81 caad0308 2021-01-27 op return default_mime;
83 6016a593 2021-01-30 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
84 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
85 c8b74339 2021-01-24 op if (loc->default_mime != NULL)
86 6016a593 2021-01-30 op return loc->default_mime;
90 6016a593 2021-01-30 op if (v->locations[0].default_mime != NULL)
91 6016a593 2021-01-30 op return v->locations[0].default_mime;
92 c8b74339 2021-01-24 op return default_mime;
96 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
98 c8b74339 2021-01-24 op struct location *loc;
99 c8b74339 2021-01-24 op const char *index = "index.gmi";
101 caad0308 2021-01-27 op if (v == NULL || path == NULL)
102 caad0308 2021-01-27 op return index;
104 6016a593 2021-01-30 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
105 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
106 c8b74339 2021-01-24 op if (loc->index != NULL)
107 6016a593 2021-01-30 op return loc->index;
111 b59f3cdd 2021-01-30 op if (v->locations[0].index != NULL)
112 b59f3cdd 2021-01-30 op return v->locations[0].index;
113 c8b74339 2021-01-24 op return index;
117 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
119 252908e6 2021-01-24 op struct location *loc;
121 6016a593 2021-01-30 op if (v == NULL || path == NULL)
124 252908e6 2021-01-24 op for (loc = v->locations; loc->match != NULL; ++loc) {
125 252908e6 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
126 6016a593 2021-01-30 op if (loc->auto_index != 0)
127 6016a593 2021-01-30 op return loc->auto_index == 1;
131 6016a593 2021-01-30 op return v->locations[0].auto_index == 1;
135 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
137 d3a08f4d 2021-01-17 op struct stat sb;
138 d1ca3911 2021-01-21 op const char *p;
141 d3a08f4d 2021-01-17 op assert(path != NULL);
143 d1ca3911 2021-01-21 op if (*path == '\0')
145 d1ca3911 2021-01-21 op else if (*path == '/')
146 d1ca3911 2021-01-21 op /* in send_dir we add an initial / (to be
147 d1ca3911 2021-01-21 op * redirect-friendly), but here we want to skip it */
152 252908e6 2021-01-24 op flags = O_RDONLY | O_NOFOLLOW;
154 252908e6 2021-01-24 op if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
155 d3a08f4d 2021-01-17 op return FILE_MISSING;
157 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
158 d3a08f4d 2021-01-17 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
159 d3a08f4d 2021-01-17 op return FILE_MISSING;
162 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
163 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
165 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
166 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
168 d3a08f4d 2021-01-17 op return FILE_EXISTS;
172 0be51733 2021-01-20 op open_file(struct pollfd *fds, struct client *c)
174 0be51733 2021-01-20 op switch (check_path(c, c->iri.path, &c->fd)) {
175 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
176 87f2b68b 2021-02-02 op if (c->host->cgi != NULL && !fnmatch(c->host->cgi, c->iri.path, 0)) {
177 2fafa2d2 2021-02-01 op start_cgi(c->iri.path, "", fds, c);
181 d3a08f4d 2021-01-17 op /* fallthrough */
183 d3a08f4d 2021-01-17 op case FILE_EXISTS:
184 252908e6 2021-01-24 op load_file(fds, c);
187 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
188 252908e6 2021-01-24 op open_dir(fds, c);
191 d3a08f4d 2021-01-17 op case FILE_MISSING:
192 87f2b68b 2021-02-02 op if (c->host->cgi != NULL && !fnmatch(c->host->cgi, c->iri.path, 0)) {
193 2fafa2d2 2021-02-01 op check_for_cgi(fds, c);
196 a87f6625 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
200 d3a08f4d 2021-01-17 op /* unreachable */
206 252908e6 2021-01-24 op load_file(struct pollfd *fds, struct client *c)
208 252908e6 2021-01-24 op if ((c->len = filesize(c->fd)) == -1) {
209 87f2b68b 2021-02-02 op LOGE(c, "failed to get file size for %s: %s",
210 87f2b68b 2021-02-02 op c->iri.path, strerror(errno));
211 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
215 252908e6 2021-01-24 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
216 252908e6 2021-01-24 op c->fd, 0)) == MAP_FAILED) {
217 252908e6 2021-01-24 op LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
218 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
221 252908e6 2021-01-24 op c->i = c->buf;
222 112802ea 2021-02-01 op c->next = send_file;
223 252908e6 2021-01-24 op start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path));
227 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
228 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
229 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
230 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
231 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
234 2fafa2d2 2021-02-01 op check_for_cgi(struct pollfd *fds, struct client *c)
236 2fafa2d2 2021-02-01 op char path[PATH_MAX];
239 2fafa2d2 2021-02-01 op strlcpy(path, c->iri.path, sizeof(path));
240 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
242 d3a08f4d 2021-01-17 op /* NB: assume CGI is enabled and path matches cgi */
244 d3a08f4d 2021-01-17 op while (end > path) {
245 d3a08f4d 2021-01-17 op /* go up one level. UNIX paths are simple and POSIX
246 d3a08f4d 2021-01-17 op * dirname, with its ambiguities on if the given path
247 d3a08f4d 2021-01-17 op * is changed or not, gives me headaches. */
248 d3a08f4d 2021-01-17 op while (*end != '/')
252 d3a08f4d 2021-01-17 op switch (check_path(c, path, &c->fd)) {
253 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
254 2fafa2d2 2021-02-01 op start_cgi(path, end+1, fds, c);
256 d3a08f4d 2021-01-17 op case FILE_MISSING:
267 a87f6625 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
272 d3a08f4d 2021-01-17 op handle_handshake(struct pollfd *fds, struct client *c)
274 d3a08f4d 2021-01-17 op struct vhost *h;
275 d3a08f4d 2021-01-17 op const char *servname;
276 a8d4a897 2021-01-29 op const char *parse_err = "unknown error";
278 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
279 d3a08f4d 2021-01-17 op case 0: /* success */
280 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
282 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
283 d3a08f4d 2021-01-17 op fds->events = POLLIN;
285 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
286 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
289 d3a08f4d 2021-01-17 op /* unreachable */
293 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
294 a8d4a897 2021-01-29 op if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
295 a8d4a897 2021-01-29 op LOGI(c, "%s", parse_err);
299 d3a08f4d 2021-01-17 op for (h = hosts; h->domain != NULL; ++h) {
300 3300cbe0 2021-01-27 op if (!fnmatch(h->domain, c->domain, 0))
304 90cb9eea 2021-01-28 op /* LOGD(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"", */
305 90cb9eea 2021-01-28 op /* servname != NULL ? servname : "(null)", */
306 90cb9eea 2021-01-28 op /* c->domain, */
307 90cb9eea 2021-01-28 op /* h->domain != NULL ? h->domain : "(null)"); */
309 d3a08f4d 2021-01-17 op if (h->domain != NULL) {
311 b06f80cd 2021-02-01 op c->state = handle_open_conn;
312 b06f80cd 2021-02-01 op c->state(fds, c);
317 0ab65593 2021-01-21 op if (servname != NULL)
318 0ab65593 2021-01-21 op strncpy(c->req, servname, sizeof(c->req));
320 0ab65593 2021-01-21 op strncpy(c->req, "null", sizeof(c->req));
322 a8d4a897 2021-01-29 op start_reply(fds, c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
326 d3a08f4d 2021-01-17 op handle_open_conn(struct pollfd *fds, struct client *c)
328 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
329 3300cbe0 2021-01-27 op char decoded[DOMAIN_NAME_LEN];
331 0be51733 2021-01-20 op bzero(c->req, sizeof(c->req));
332 0be51733 2021-01-20 op bzero(&c->iri, sizeof(c->iri));
334 0be51733 2021-01-20 op switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
336 d3a08f4d 2021-01-17 op LOGE(c, "tls_read: %s", tls_error(c->ctx));
337 36162ed8 2021-01-22 op close_conn(fds, c);
340 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
341 d3a08f4d 2021-01-17 op fds->events = POLLIN;
344 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
345 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
349 c4f682f8 2021-01-27 op if (!trim_req_iri(c->req, &parse_err)
350 a2fd8013 2021-01-29 op || !parse_iri(c->req, &c->iri, &parse_err)
351 a2fd8013 2021-01-29 op || !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
352 c4f682f8 2021-01-27 op LOGI(c, "iri parse error: %s", parse_err);
353 c4f682f8 2021-01-27 op start_reply(fds, c, BAD_REQUEST, "invalid request");
357 3300cbe0 2021-01-27 op if (c->iri.port_no != conf.port
358 3300cbe0 2021-01-27 op || strcmp(c->iri.schema, "gemini")
359 3300cbe0 2021-01-27 op || strcmp(decoded, c->domain)) {
360 a87f6625 2021-01-24 op start_reply(fds, c, PROXY_REFUSED, "won't proxy request");
364 0be51733 2021-01-20 op open_file(fds, c);
368 df79b4c1 2021-01-19 op start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
370 112802ea 2021-02-01 op c->code = code;
371 112802ea 2021-02-01 op c->meta = meta;
372 112802ea 2021-02-01 op c->state = handle_start_reply;
373 fe406389 2021-02-02 op handle_start_reply(pfd, c);
377 112802ea 2021-02-01 op handle_start_reply(struct pollfd *pfd, struct client *c)
379 05c23a54 2021-01-19 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
380 c8b74339 2021-01-24 op const char *lang;
383 c8b74339 2021-01-24 op lang = vhost_lang(c->host, c->iri.path);
385 112802ea 2021-02-01 op snprintf(buf, sizeof(buf), "%d ", c->code);
386 112802ea 2021-02-01 op strlcat(buf, c->meta, sizeof(buf));
387 112802ea 2021-02-01 op if (!strcmp(c->meta, "text/gemini") && lang != NULL) {
388 05c23a54 2021-01-19 op strlcat(buf, "; lang=", sizeof(buf));
389 c8b74339 2021-01-24 op strlcat(buf, lang, sizeof(buf));
392 05c23a54 2021-01-19 op len = strlcat(buf, "\r\n", sizeof(buf));
393 0be51733 2021-01-20 op assert(len < sizeof(buf));
395 df79b4c1 2021-01-19 op switch (tls_write(c->ctx, buf, len)) {
397 a87f6625 2021-01-24 op close_conn(pfd, c);
399 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
400 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
402 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
403 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
407 a87f6625 2021-01-24 op log_request(c, buf, sizeof(buf));
409 a87f6625 2021-01-24 op /* we don't need a body */
410 a87f6625 2021-01-24 op if (c->code != SUCCESS) {
411 a87f6625 2021-01-24 op close_conn(pfd, c);
415 a87f6625 2021-01-24 op /* advance the state machine */
416 a87f6625 2021-01-24 op c->state = c->next;
417 112802ea 2021-02-01 op c->state(pfd, c);
421 2fafa2d2 2021-02-01 op start_cgi(const char *spath, const char *relpath,
422 d3a08f4d 2021-01-17 op struct pollfd *fds, struct client *c)
424 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
425 d3a08f4d 2021-01-17 op const char *ruser, *cissuer, *chash;
428 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
429 d3a08f4d 2021-01-17 op addr, sizeof(addr),
431 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
435 d3a08f4d 2021-01-17 op if (tls_peer_cert_provided(c->ctx)) {
436 d3a08f4d 2021-01-17 op ruser = tls_peer_cert_subject(c->ctx);
437 d3a08f4d 2021-01-17 op cissuer = tls_peer_cert_issuer(c->ctx);
438 d3a08f4d 2021-01-17 op chash = tls_peer_cert_hash(c->ctx);
440 d3a08f4d 2021-01-17 op ruser = NULL;
441 d3a08f4d 2021-01-17 op cissuer = NULL;
442 d3a08f4d 2021-01-17 op chash = NULL;
445 2fafa2d2 2021-02-01 op if (!send_iri(exfd, &c->iri)
446 2fafa2d2 2021-02-01 op || !send_string(exfd, spath)
447 d3a08f4d 2021-01-17 op || !send_string(exfd, relpath)
448 d3a08f4d 2021-01-17 op || !send_string(exfd, addr)
449 d3a08f4d 2021-01-17 op || !send_string(exfd, ruser)
450 d3a08f4d 2021-01-17 op || !send_string(exfd, cissuer)
451 d3a08f4d 2021-01-17 op || !send_string(exfd, chash)
452 d3a08f4d 2021-01-17 op || !send_vhost(exfd, c->host))
455 d3a08f4d 2021-01-17 op close(c->fd);
456 d3a08f4d 2021-01-17 op if ((c->fd = recv_fd(exfd)) == -1) {
457 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
461 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
462 35744950 2021-02-01 op c->state = handle_cgi_reply;
466 d3a08f4d 2021-01-17 op /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
467 132cae8c 2021-01-18 op fatal("cannot talk to the executor process");
471 0be51733 2021-01-20 op send_file(struct pollfd *fds, struct client *c)
473 d3a08f4d 2021-01-17 op ssize_t ret, len;
475 d3a08f4d 2021-01-17 op len = (c->buf + c->len) - c->i;
477 d3a08f4d 2021-01-17 op while (len > 0) {
478 d3a08f4d 2021-01-17 op switch (ret = tls_write(c->ctx, c->i, len)) {
480 d3a08f4d 2021-01-17 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
481 36162ed8 2021-01-22 op close_conn(fds, c);
484 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
485 d3a08f4d 2021-01-17 op fds->events = POLLIN;
488 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
489 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
499 36162ed8 2021-01-22 op close_conn(fds, c);
503 252908e6 2021-01-24 op open_dir(struct pollfd *fds, struct client *c)
507 252908e6 2021-01-24 op char *before_file;
509 0be51733 2021-01-20 op len = strlen(c->iri.path);
510 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
511 252908e6 2021-01-24 op redirect_canonical_dir(fds, c);
515 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
516 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
517 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
518 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
519 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
520 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
521 c8b74339 2021-01-24 op sizeof(c->sbuf));
522 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
523 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
527 252908e6 2021-01-24 op c->iri.path = c->sbuf;
529 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
530 252908e6 2021-01-24 op dirfd = c->fd;
533 252908e6 2021-01-24 op switch (check_path(c, c->iri.path, &c->fd)) {
534 252908e6 2021-01-24 op case FILE_EXECUTABLE:
535 87f2b68b 2021-02-02 op if (c->host->cgi != NULL && !fnmatch(c->host->cgi, c->iri.path, 0)) {
536 2fafa2d2 2021-02-01 op start_cgi(c->iri.path, "", fds, c);
540 252908e6 2021-01-24 op /* fallthrough */
542 252908e6 2021-01-24 op case FILE_EXISTS:
543 252908e6 2021-01-24 op load_file(fds, c);
546 252908e6 2021-01-24 op case FILE_DIRECTORY:
547 252908e6 2021-01-24 op start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
550 252908e6 2021-01-24 op case FILE_MISSING:
551 252908e6 2021-01-24 op *before_file = '\0';
553 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
554 252908e6 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
558 252908e6 2021-01-24 op c->fd = dirfd;
559 5f715ce4 2021-02-02 op c->next = enter_handle_dirlist;
561 252908e6 2021-01-24 op if ((c->dir = fdopendir(c->fd)) == NULL) {
562 252908e6 2021-01-24 op LOGE(c, "can't fdopendir(%d) (vhost:%s) %s: %s",
563 252908e6 2021-01-24 op c->fd, c->host->domain, c->iri.path, strerror(errno));
564 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
569 252908e6 2021-01-24 op start_reply(fds, c, SUCCESS, "text/gemini");
573 252908e6 2021-01-24 op /* unreachable */
577 252908e6 2021-01-24 op close(dirfd);
581 252908e6 2021-01-24 op redirect_canonical_dir(struct pollfd *fds, struct client *c)
585 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
586 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
587 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
589 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
590 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
594 252908e6 2021-01-24 op start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
598 5f715ce4 2021-02-02 op enter_handle_dirlist(struct pollfd *fds, struct client *c)
600 5f715ce4 2021-02-02 op char b[PATH_MAX];
603 5f715ce4 2021-02-02 op strlcpy(b, c->iri.path, sizeof(b));
604 5f715ce4 2021-02-02 op l = snprintf(c->sbuf, sizeof(c->sbuf),
605 5f715ce4 2021-02-02 op "# Index of %s\n\n", b);
606 5f715ce4 2021-02-02 op if (l >= sizeof(c->sbuf)) {
607 5f715ce4 2021-02-02 op /* this is impossible, given that we have enough space
608 5f715ce4 2021-02-02 op * in c->sbuf to hold the ancilliary string plus the
609 5f715ce4 2021-02-02 op * full path; but it wouldn't read nice without some
610 5f715ce4 2021-02-02 op * error checking, and I'd like to avoid a strlen. */
611 5f715ce4 2021-02-02 op close_conn(fds, c);
616 5f715ce4 2021-02-02 op c->state = handle_dirlist;
617 5f715ce4 2021-02-02 op handle_dirlist(fds, c);
621 5f715ce4 2021-02-02 op handle_dirlist(struct pollfd *fds, struct client *c)
625 5f715ce4 2021-02-02 op while (c->len > 0) {
626 5f715ce4 2021-02-02 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
628 5f715ce4 2021-02-02 op close_conn(fds, c);
630 5f715ce4 2021-02-02 op case TLS_WANT_POLLOUT:
631 5f715ce4 2021-02-02 op fds->events = POLLOUT;
633 5f715ce4 2021-02-02 op case TLS_WANT_POLLIN:
634 5f715ce4 2021-02-02 op fds->events = POLLIN;
642 5f715ce4 2021-02-02 op c->state = send_directory_listing;
643 5f715ce4 2021-02-02 op send_directory_listing(fds, c);
647 252908e6 2021-01-24 op read_next_dir_entry(struct client *c)
649 252908e6 2021-01-24 op struct dirent *d;
653 252908e6 2021-01-24 op if ((d = readdir(c->dir)) == NULL) {
654 252908e6 2021-01-24 op if (errno != 0)
655 252908e6 2021-01-24 op LOGE(c, "readdir: %s", strerror(errno));
658 252908e6 2021-01-24 op } while (!strcmp(d->d_name, "."));
660 252908e6 2021-01-24 op /* XXX: url escape */
661 252908e6 2021-01-24 op snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
662 252908e6 2021-01-24 op d->d_name, d->d_name);
663 252908e6 2021-01-24 op c->len = strlen(c->sbuf);
670 252908e6 2021-01-24 op send_directory_listing(struct pollfd *fds, struct client *c)
675 252908e6 2021-01-24 op if (c->len == 0) {
676 252908e6 2021-01-24 op if (!read_next_dir_entry(c))
680 252908e6 2021-01-24 op while (c->len > 0) {
681 252908e6 2021-01-24 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
685 252908e6 2021-01-24 op case TLS_WANT_POLLOUT:
686 252908e6 2021-01-24 op fds->events = POLLOUT;
689 252908e6 2021-01-24 op case TLS_WANT_POLLIN:
690 252908e6 2021-01-24 op fds->events = POLLIN;
702 252908e6 2021-01-24 op close_conn(fds, c);
705 fe406389 2021-02-02 op static inline void
706 d3a08f4d 2021-01-17 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
710 d3a08f4d 2021-01-17 op if (c->waiting_on_child)
712 d3a08f4d 2021-01-17 op c->waiting_on_child = 1;
714 d3a08f4d 2021-01-17 op fds->events = POLLIN;
716 d3a08f4d 2021-01-17 op fd = fds->fd;
717 d3a08f4d 2021-01-17 op fds->fd = c->fd;
721 fe406389 2021-02-02 op static inline void
722 d3a08f4d 2021-01-17 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
726 d3a08f4d 2021-01-17 op if (!c->waiting_on_child)
728 d3a08f4d 2021-01-17 op c->waiting_on_child = 0;
730 d3a08f4d 2021-01-17 op fd = fds->fd;
731 d3a08f4d 2021-01-17 op fds->fd = c->fd;
735 35744950 2021-02-01 op /* accumulate the meta line from the cgi script. */
737 35744950 2021-02-01 op handle_cgi_reply(struct pollfd *fds, struct client *c)
739 35744950 2021-02-01 op void *buf, *e;
743 35744950 2021-02-01 op buf = c->sbuf + c->len;
744 35744950 2021-02-01 op len = sizeof(c->sbuf) - c->len;
746 35744950 2021-02-01 op /* we're polling on the child! */
747 35744950 2021-02-01 op r = read(fds->fd, buf, len);
748 35744950 2021-02-01 op if (r == 0 || r == -1) {
749 35744950 2021-02-01 op cgi_poll_on_client(fds, c);
750 35744950 2021-02-01 op start_reply(fds, c, CGI_ERROR, "CGI error");
756 35744950 2021-02-01 op /* TODO: error if the CGI script don't reply correctly */
757 35744950 2021-02-01 op e = strchr(c->sbuf, '\n');
758 35744950 2021-02-01 op if (e != NULL || c->len == sizeof(c->sbuf)) {
759 3309ef97 2021-01-23 op log_request(c, c->sbuf, c->len);
762 35744950 2021-02-01 op c->state = handle_cgi;
763 35744950 2021-02-01 op c->state(fds, c);
769 d3a08f4d 2021-01-17 op handle_cgi(struct pollfd *fds, struct client *c)
773 d3a08f4d 2021-01-17 op /* ensure c->fd is the child and fds->fd the client */
774 d3a08f4d 2021-01-17 op cgi_poll_on_client(fds, c);
777 35744950 2021-02-01 op if (c->len == 0) {
778 35744950 2021-02-01 op switch (r = read(c->fd, c->sbuf, sizeof(c->sbuf))) {
782 d3a08f4d 2021-01-17 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
783 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
793 d3a08f4d 2021-01-17 op while (c->len > 0) {
794 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
798 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
799 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
802 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
803 d3a08f4d 2021-01-17 op fds->events = POLLIN;
815 36162ed8 2021-01-22 op close_conn(fds, c);
819 36162ed8 2021-01-22 op close_conn(struct pollfd *pfd, struct client *c)
821 112802ea 2021-02-01 op c->state = close_conn;
823 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
824 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
825 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
827 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
828 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
832 d3a08f4d 2021-01-17 op connected_clients--;
834 d3a08f4d 2021-01-17 op tls_free(c->ctx);
835 d3a08f4d 2021-01-17 op c->ctx = NULL;
837 d3a08f4d 2021-01-17 op if (c->buf != MAP_FAILED)
838 d3a08f4d 2021-01-17 op munmap(c->buf, c->len);
840 d3a08f4d 2021-01-17 op if (c->fd != -1)
841 d3a08f4d 2021-01-17 op close(c->fd);
843 252908e6 2021-01-24 op if (c->dir != NULL)
844 252908e6 2021-01-24 op closedir(c->dir);
846 d3a08f4d 2021-01-17 op close(pfd->fd);
847 d3a08f4d 2021-01-17 op pfd->fd = -1;
851 d3a08f4d 2021-01-17 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
854 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
855 d3a08f4d 2021-01-17 op socklen_t len;
857 d3a08f4d 2021-01-17 op len = sizeof(addr);
858 d3a08f4d 2021-01-17 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
859 d3a08f4d 2021-01-17 op if (errno == EWOULDBLOCK)
861 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
864 d3a08f4d 2021-01-17 op mark_nonblock(fd);
866 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
867 d3a08f4d 2021-01-17 op if (fds[i].fd == -1) {
868 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
869 d3a08f4d 2021-01-17 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
870 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
872 d3a08f4d 2021-01-17 op fds[i].fd = fd;
873 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
875 112802ea 2021-02-01 op clients[i].state = handle_handshake;
876 112802ea 2021-02-01 op clients[i].next = send_file;
877 d3a08f4d 2021-01-17 op clients[i].fd = -1;
878 d3a08f4d 2021-01-17 op clients[i].waiting_on_child = 0;
879 d3a08f4d 2021-01-17 op clients[i].buf = MAP_FAILED;
880 252908e6 2021-01-24 op clients[i].dir = NULL;
881 d3a08f4d 2021-01-17 op clients[i].addr = addr;
883 d3a08f4d 2021-01-17 op connected_clients++;
892 d3a08f4d 2021-01-17 op loop(struct tls *ctx, int sock4, int sock6)
895 d3a08f4d 2021-01-17 op struct client clients[MAX_USERS];
896 d3a08f4d 2021-01-17 op struct pollfd fds[MAX_USERS];
898 d3a08f4d 2021-01-17 op connected_clients = 0;
900 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
901 d3a08f4d 2021-01-17 op fds[i].fd = -1;
902 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
903 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
906 d3a08f4d 2021-01-17 op fds[0].fd = sock4;
907 d3a08f4d 2021-01-17 op fds[1].fd = sock6;
910 d3a08f4d 2021-01-17 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
911 d3a08f4d 2021-01-17 op if (errno == EINTR) {
912 132cae8c 2021-01-18 op fprintf(stderr, "connected clients: %d\n",
913 d3a08f4d 2021-01-17 op connected_clients);
916 d3a08f4d 2021-01-17 op fatal("poll: %s", strerror(errno));
919 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; i++) {
920 d3a08f4d 2021-01-17 op if (fds[i].revents == 0)
923 d3a08f4d 2021-01-17 op if (fds[i].revents & (POLLERR|POLLNVAL))
924 d3a08f4d 2021-01-17 op fatal("bad fd %d: %s", fds[i].fd,
925 d3a08f4d 2021-01-17 op strerror(errno));
927 d3a08f4d 2021-01-17 op if (fds[i].revents & POLLHUP) {
928 d3a08f4d 2021-01-17 op /* fds[i] may be the fd of the stdin
929 d3a08f4d 2021-01-17 op * of a cgi script that has exited. */
930 d3a08f4d 2021-01-17 op if (!clients[i].waiting_on_child) {
931 36162ed8 2021-01-22 op close_conn(&fds[i], &clients[i]);
936 d3a08f4d 2021-01-17 op if (fds[i].fd == sock4)
937 d3a08f4d 2021-01-17 op do_accept(sock4, ctx, fds, clients);
938 d3a08f4d 2021-01-17 op else if (fds[i].fd == sock6)
939 d3a08f4d 2021-01-17 op do_accept(sock6, ctx, fds, clients);
941 112802ea 2021-02-01 op clients[i].state(&fds[i], &clients[i]);