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 d3a08f4d 2021-01-17 op #include <string.h>
28 d3a08f4d 2021-01-17 op #include "gmid.h"
30 d3a08f4d 2021-01-17 op int connected_clients;
33 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
35 c8b74339 2021-01-24 op struct location *loc;
36 c8b74339 2021-01-24 op const char *lang = NULL;
38 8443bff7 2021-01-25 op if (v == NULL)
41 c8b74339 2021-01-24 op for (loc = v->locations; 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 c8b74339 2021-01-24 op lang = loc->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 c8b74339 2021-01-24 op for (loc = v->locations; loc->match != NULL; ++loc) {
58 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
59 c8b74339 2021-01-24 op if (loc->default_mime != NULL)
60 c8b74339 2021-01-24 op default_mime = loc->default_mime;
64 c8b74339 2021-01-24 op return default_mime;
68 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
70 c8b74339 2021-01-24 op struct location *loc;
71 c8b74339 2021-01-24 op const char *index = "index.gmi";
73 c8b74339 2021-01-24 op for (loc = v->locations; loc->match != NULL; ++loc) {
74 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
75 c8b74339 2021-01-24 op if (loc->index != NULL)
76 c8b74339 2021-01-24 op index = loc->index;
84 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
86 252908e6 2021-01-24 op struct location *loc;
87 252908e6 2021-01-24 op int auto_index = 0;
89 252908e6 2021-01-24 op for (loc = v->locations; loc->match != NULL; ++loc) {
90 252908e6 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
91 252908e6 2021-01-24 op if (loc->auto_index)
92 252908e6 2021-01-24 op auto_index = loc->auto_index;
96 252908e6 2021-01-24 op return auto_index == 1;
100 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
102 d3a08f4d 2021-01-17 op struct stat sb;
103 d1ca3911 2021-01-21 op const char *p;
106 d3a08f4d 2021-01-17 op assert(path != NULL);
108 d1ca3911 2021-01-21 op if (*path == '\0')
110 d1ca3911 2021-01-21 op else if (*path == '/')
111 d1ca3911 2021-01-21 op /* in send_dir we add an initial / (to be
112 d1ca3911 2021-01-21 op * redirect-friendly), but here we want to skip it */
117 252908e6 2021-01-24 op flags = O_RDONLY | O_NOFOLLOW;
119 252908e6 2021-01-24 op if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
120 d3a08f4d 2021-01-17 op return FILE_MISSING;
122 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
123 d3a08f4d 2021-01-17 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
124 d3a08f4d 2021-01-17 op return FILE_MISSING;
127 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
128 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
130 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
131 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
133 d3a08f4d 2021-01-17 op return FILE_EXISTS;
137 0be51733 2021-01-20 op open_file(struct pollfd *fds, struct client *c)
139 0be51733 2021-01-20 op switch (check_path(c, c->iri.path, &c->fd)) {
140 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
141 252908e6 2021-01-24 op if (starts_with(c->iri.path, c->host->cgi)) {
142 07b0a142 2021-01-24 op start_cgi(c->iri.path, "", c->iri.query, fds, c);
146 d3a08f4d 2021-01-17 op /* fallthrough */
148 d3a08f4d 2021-01-17 op case FILE_EXISTS:
149 252908e6 2021-01-24 op load_file(fds, c);
152 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
153 252908e6 2021-01-24 op open_dir(fds, c);
156 d3a08f4d 2021-01-17 op case FILE_MISSING:
157 07b0a142 2021-01-24 op if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi)) {
158 07b0a142 2021-01-24 op check_for_cgi(c->iri.path, c->iri.query, fds, c);
161 a87f6625 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
165 d3a08f4d 2021-01-17 op /* unreachable */
171 252908e6 2021-01-24 op load_file(struct pollfd *fds, struct client *c)
173 252908e6 2021-01-24 op if ((c->len = filesize(c->fd)) == -1) {
174 252908e6 2021-01-24 op LOGE(c, "failed to get file size for %s", c->iri.path);
175 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
179 252908e6 2021-01-24 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
180 252908e6 2021-01-24 op c->fd, 0)) == MAP_FAILED) {
181 252908e6 2021-01-24 op LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
182 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
185 252908e6 2021-01-24 op c->i = c->buf;
186 252908e6 2021-01-24 op c->next = S_SENDING_FILE;
187 252908e6 2021-01-24 op start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path));
191 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
192 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
193 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
194 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
195 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
198 d3a08f4d 2021-01-17 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
201 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
203 d3a08f4d 2021-01-17 op /* NB: assume CGI is enabled and path matches cgi */
205 d3a08f4d 2021-01-17 op while (end > path) {
206 d3a08f4d 2021-01-17 op /* go up one level. UNIX paths are simple and POSIX
207 d3a08f4d 2021-01-17 op * dirname, with its ambiguities on if the given path
208 d3a08f4d 2021-01-17 op * is changed or not, gives me headaches. */
209 d3a08f4d 2021-01-17 op while (*end != '/')
213 d3a08f4d 2021-01-17 op switch (check_path(c, path, &c->fd)) {
214 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
215 07b0a142 2021-01-24 op start_cgi(path, end+1, query, fds,c);
217 d3a08f4d 2021-01-17 op case FILE_MISSING:
228 a87f6625 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
233 d3a08f4d 2021-01-17 op mark_nonblock(int fd)
237 d3a08f4d 2021-01-17 op if ((flags = fcntl(fd, F_GETFL)) == -1)
238 d3a08f4d 2021-01-17 op fatal("fcntl(F_GETFL): %s", strerror(errno));
239 d3a08f4d 2021-01-17 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
240 d3a08f4d 2021-01-17 op fatal("fcntl(F_SETFL): %s", strerror(errno));
244 d3a08f4d 2021-01-17 op handle_handshake(struct pollfd *fds, struct client *c)
246 d3a08f4d 2021-01-17 op struct vhost *h;
247 d3a08f4d 2021-01-17 op const char *servname;
249 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
250 d3a08f4d 2021-01-17 op case 0: /* success */
251 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
253 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
254 d3a08f4d 2021-01-17 op fds->events = POLLIN;
256 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
257 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
260 d3a08f4d 2021-01-17 op /* unreachable */
264 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
266 d3a08f4d 2021-01-17 op for (h = hosts; h->domain != NULL; ++h) {
267 ce79c944 2021-01-21 op if (!strcmp(h->domain, "*"))
270 b4d409cf 2021-01-21 op if (servname != NULL && !fnmatch(h->domain, servname, 0))
274 d3a08f4d 2021-01-17 op if (h->domain != NULL) {
275 d3a08f4d 2021-01-17 op c->state = S_OPEN;
277 d3a08f4d 2021-01-17 op handle_open_conn(fds, c);
281 0ab65593 2021-01-21 op if (servname != NULL)
282 0ab65593 2021-01-21 op strncpy(c->req, servname, sizeof(c->req));
284 0ab65593 2021-01-21 op strncpy(c->req, "null", sizeof(c->req));
286 a87f6625 2021-01-24 op start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI");
290 d3a08f4d 2021-01-17 op handle_open_conn(struct pollfd *fds, struct client *c)
292 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
294 0be51733 2021-01-20 op bzero(c->req, sizeof(c->req));
295 0be51733 2021-01-20 op bzero(&c->iri, sizeof(c->iri));
297 0be51733 2021-01-20 op switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
299 d3a08f4d 2021-01-17 op LOGE(c, "tls_read: %s", tls_error(c->ctx));
300 36162ed8 2021-01-22 op close_conn(fds, c);
303 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
304 d3a08f4d 2021-01-17 op fds->events = POLLIN;
307 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
308 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
312 0be51733 2021-01-20 op if (!trim_req_iri(c->req) || !parse_iri(c->req, &c->iri, &parse_err)) {
313 a87f6625 2021-01-24 op start_reply(fds, c, BAD_REQUEST, parse_err);
317 0be51733 2021-01-20 op /* XXX: we should check that the SNI matches the requested host */
318 0be51733 2021-01-20 op if (strcmp(c->iri.schema, "gemini") || c->iri.port_no != conf.port) {
319 a87f6625 2021-01-24 op start_reply(fds, c, PROXY_REFUSED, "won't proxy request");
323 0be51733 2021-01-20 op open_file(fds, c);
327 df79b4c1 2021-01-19 op start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
329 05c23a54 2021-01-19 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
330 c8b74339 2021-01-24 op const char *lang;
333 df79b4c1 2021-01-19 op c->code = code;
334 df79b4c1 2021-01-19 op c->meta = meta;
335 df79b4c1 2021-01-19 op c->state = S_INITIALIZING;
337 c8b74339 2021-01-24 op lang = vhost_lang(c->host, c->iri.path);
339 05c23a54 2021-01-19 op snprintf(buf, sizeof(buf), "%d ", code);
340 df79b4c1 2021-01-19 op strlcat(buf, meta, sizeof(buf));
341 c8b74339 2021-01-24 op if (!strcmp(meta, "text/gemini") && lang != NULL) {
342 05c23a54 2021-01-19 op strlcat(buf, "; lang=", sizeof(buf));
343 c8b74339 2021-01-24 op strlcat(buf, lang, sizeof(buf));
346 05c23a54 2021-01-19 op len = strlcat(buf, "\r\n", sizeof(buf));
347 0be51733 2021-01-20 op assert(len < sizeof(buf));
349 df79b4c1 2021-01-19 op switch (tls_write(c->ctx, buf, len)) {
351 a87f6625 2021-01-24 op close_conn(pfd, c);
353 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
354 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
356 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
357 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
361 a87f6625 2021-01-24 op log_request(c, buf, sizeof(buf));
363 a87f6625 2021-01-24 op /* we don't need a body */
364 a87f6625 2021-01-24 op if (c->code != SUCCESS) {
365 a87f6625 2021-01-24 op close_conn(pfd, c);
369 a87f6625 2021-01-24 op /* advance the state machine */
370 a87f6625 2021-01-24 op c->state = c->next;
371 a87f6625 2021-01-24 op handle(pfd, c);
375 d3a08f4d 2021-01-17 op start_cgi(const char *spath, const char *relpath, const char *query,
376 d3a08f4d 2021-01-17 op struct pollfd *fds, struct client *c)
378 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
379 d3a08f4d 2021-01-17 op const char *ruser, *cissuer, *chash;
382 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
383 d3a08f4d 2021-01-17 op addr, sizeof(addr),
385 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
389 d3a08f4d 2021-01-17 op if (tls_peer_cert_provided(c->ctx)) {
390 d3a08f4d 2021-01-17 op ruser = tls_peer_cert_subject(c->ctx);
391 d3a08f4d 2021-01-17 op cissuer = tls_peer_cert_issuer(c->ctx);
392 d3a08f4d 2021-01-17 op chash = tls_peer_cert_hash(c->ctx);
394 d3a08f4d 2021-01-17 op ruser = NULL;
395 d3a08f4d 2021-01-17 op cissuer = NULL;
396 d3a08f4d 2021-01-17 op chash = NULL;
399 d3a08f4d 2021-01-17 op if (!send_string(exfd, spath)
400 d3a08f4d 2021-01-17 op || !send_string(exfd, relpath)
401 d3a08f4d 2021-01-17 op || !send_string(exfd, query)
402 d3a08f4d 2021-01-17 op || !send_string(exfd, addr)
403 d3a08f4d 2021-01-17 op || !send_string(exfd, ruser)
404 d3a08f4d 2021-01-17 op || !send_string(exfd, cissuer)
405 d3a08f4d 2021-01-17 op || !send_string(exfd, chash)
406 d3a08f4d 2021-01-17 op || !send_vhost(exfd, c->host))
409 d3a08f4d 2021-01-17 op close(c->fd);
410 d3a08f4d 2021-01-17 op if ((c->fd = recv_fd(exfd)) == -1) {
411 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
414 a87f6625 2021-01-24 op c->state = S_SENDING_CGI;
415 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
416 0be51733 2021-01-20 op c->code = -1;
417 d3a08f4d 2021-01-17 op /* handle_cgi(fds, c); */
421 d3a08f4d 2021-01-17 op /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
422 132cae8c 2021-01-18 op fatal("cannot talk to the executor process");
426 0be51733 2021-01-20 op send_file(struct pollfd *fds, struct client *c)
428 d3a08f4d 2021-01-17 op ssize_t ret, len;
430 06f233ad 2021-01-21 op /* ensure the correct state */
431 a87f6625 2021-01-24 op c->state = S_SENDING_FILE;
433 d3a08f4d 2021-01-17 op len = (c->buf + c->len) - c->i;
435 d3a08f4d 2021-01-17 op while (len > 0) {
436 d3a08f4d 2021-01-17 op switch (ret = tls_write(c->ctx, c->i, len)) {
438 d3a08f4d 2021-01-17 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
439 36162ed8 2021-01-22 op close_conn(fds, c);
442 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
443 d3a08f4d 2021-01-17 op fds->events = POLLIN;
446 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
447 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
457 36162ed8 2021-01-22 op close_conn(fds, c);
461 252908e6 2021-01-24 op open_dir(struct pollfd *fds, struct client *c)
465 252908e6 2021-01-24 op char *before_file;
467 0be51733 2021-01-20 op len = strlen(c->iri.path);
468 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
469 252908e6 2021-01-24 op redirect_canonical_dir(fds, c);
473 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
474 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
475 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
476 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
477 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
478 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
479 c8b74339 2021-01-24 op sizeof(c->sbuf));
480 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
481 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
485 252908e6 2021-01-24 op c->iri.path = c->sbuf;
487 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
488 252908e6 2021-01-24 op dirfd = c->fd;
491 252908e6 2021-01-24 op switch (check_path(c, c->iri.path, &c->fd)) {
492 252908e6 2021-01-24 op case FILE_EXECUTABLE:
493 252908e6 2021-01-24 op if (starts_with(c->iri.path, c->host->cgi)) {
494 252908e6 2021-01-24 op start_cgi(c->iri.path, "", c->iri.query, fds, c);
498 252908e6 2021-01-24 op /* fallthrough */
500 252908e6 2021-01-24 op case FILE_EXISTS:
501 252908e6 2021-01-24 op load_file(fds, c);
504 252908e6 2021-01-24 op case FILE_DIRECTORY:
505 252908e6 2021-01-24 op start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
508 252908e6 2021-01-24 op case FILE_MISSING:
509 252908e6 2021-01-24 op *before_file = '\0';
511 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
512 252908e6 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
516 252908e6 2021-01-24 op c->fd = dirfd;
517 252908e6 2021-01-24 op c->next = S_SENDING_DIR;
519 252908e6 2021-01-24 op if ((c->dir = fdopendir(c->fd)) == NULL) {
520 252908e6 2021-01-24 op LOGE(c, "can't fdopendir(%d) (vhost:%s) %s: %s",
521 252908e6 2021-01-24 op c->fd, c->host->domain, c->iri.path, strerror(errno));
522 252908e6 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
527 252908e6 2021-01-24 op start_reply(fds, c, SUCCESS, "text/gemini");
531 252908e6 2021-01-24 op /* unreachable */
535 252908e6 2021-01-24 op close(dirfd);
539 252908e6 2021-01-24 op redirect_canonical_dir(struct pollfd *fds, struct client *c)
543 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
544 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
545 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
547 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
548 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
552 252908e6 2021-01-24 op start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
556 252908e6 2021-01-24 op read_next_dir_entry(struct client *c)
558 252908e6 2021-01-24 op struct dirent *d;
562 252908e6 2021-01-24 op if ((d = readdir(c->dir)) == NULL) {
563 252908e6 2021-01-24 op if (errno != 0)
564 252908e6 2021-01-24 op LOGE(c, "readdir: %s", strerror(errno));
567 252908e6 2021-01-24 op } while (!strcmp(d->d_name, "."));
569 252908e6 2021-01-24 op /* XXX: url escape */
570 252908e6 2021-01-24 op snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
571 252908e6 2021-01-24 op d->d_name, d->d_name);
572 252908e6 2021-01-24 op c->len = strlen(c->sbuf);
579 252908e6 2021-01-24 op send_directory_listing(struct pollfd *fds, struct client *c)
584 252908e6 2021-01-24 op if (c->len == 0) {
585 252908e6 2021-01-24 op if (!read_next_dir_entry(c))
589 252908e6 2021-01-24 op while (c->len > 0) {
590 252908e6 2021-01-24 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
594 252908e6 2021-01-24 op case TLS_WANT_POLLOUT:
595 252908e6 2021-01-24 op fds->events = POLLOUT;
598 252908e6 2021-01-24 op case TLS_WANT_POLLIN:
599 252908e6 2021-01-24 op fds->events = POLLIN;
611 252908e6 2021-01-24 op close_conn(fds, c);
615 d3a08f4d 2021-01-17 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
619 d3a08f4d 2021-01-17 op if (c->waiting_on_child)
621 d3a08f4d 2021-01-17 op c->waiting_on_child = 1;
623 d3a08f4d 2021-01-17 op fds->events = POLLIN;
625 d3a08f4d 2021-01-17 op fd = fds->fd;
626 d3a08f4d 2021-01-17 op fds->fd = c->fd;
631 d3a08f4d 2021-01-17 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
635 d3a08f4d 2021-01-17 op if (!c->waiting_on_child)
637 d3a08f4d 2021-01-17 op c->waiting_on_child = 0;
639 d3a08f4d 2021-01-17 op fd = fds->fd;
640 d3a08f4d 2021-01-17 op fds->fd = c->fd;
644 3309ef97 2021-01-23 op /* handle the read from the child process. Return like read(2) */
645 3309ef97 2021-01-23 op static ssize_t
646 3309ef97 2021-01-23 op read_from_cgi(struct client *c)
652 3309ef97 2021-01-23 op /* if we haven't read a whole response line, we want to
653 3309ef97 2021-01-23 op * continue reading. */
655 3309ef97 2021-01-23 op if (c->code == -1) {
656 3309ef97 2021-01-23 op buf = c->sbuf + c->len;
657 3309ef97 2021-01-23 op len = sizeof(c->sbuf) - c->len;
659 3309ef97 2021-01-23 op buf = c->sbuf;
660 3309ef97 2021-01-23 op len = sizeof(c->sbuf);
663 3309ef97 2021-01-23 op r = read(c->fd, buf, len);
664 3309ef97 2021-01-23 op if (r == 0 || r == -1)
670 3309ef97 2021-01-23 op if (c->code != -1)
673 3309ef97 2021-01-23 op if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
675 3309ef97 2021-01-23 op log_request(c, c->sbuf, c->len);
682 d3a08f4d 2021-01-17 op handle_cgi(struct pollfd *fds, struct client *c)
686 d3a08f4d 2021-01-17 op /* ensure c->fd is the child and fds->fd the client */
687 d3a08f4d 2021-01-17 op cgi_poll_on_client(fds, c);
690 3309ef97 2021-01-23 op if (c->code == -1 || c->len == 0) {
691 3309ef97 2021-01-23 op switch (r = read_from_cgi(c)) {
696 d3a08f4d 2021-01-17 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
697 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
704 3309ef97 2021-01-23 op if (c->code == -1) {
705 3309ef97 2021-01-23 op cgi_poll_on_child(fds, c);
709 d3a08f4d 2021-01-17 op while (c->len > 0) {
710 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
714 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
715 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
718 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
719 d3a08f4d 2021-01-17 op fds->events = POLLIN;
731 36162ed8 2021-01-22 op close_conn(fds, c);
735 36162ed8 2021-01-22 op close_conn(struct pollfd *pfd, struct client *c)
737 d3a08f4d 2021-01-17 op c->state = S_CLOSING;
739 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
740 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
741 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
743 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
744 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
748 d3a08f4d 2021-01-17 op connected_clients--;
750 d3a08f4d 2021-01-17 op tls_free(c->ctx);
751 d3a08f4d 2021-01-17 op c->ctx = NULL;
753 d3a08f4d 2021-01-17 op if (c->buf != MAP_FAILED)
754 d3a08f4d 2021-01-17 op munmap(c->buf, c->len);
756 d3a08f4d 2021-01-17 op if (c->fd != -1)
757 d3a08f4d 2021-01-17 op close(c->fd);
759 252908e6 2021-01-24 op if (c->dir != NULL)
760 252908e6 2021-01-24 op closedir(c->dir);
762 d3a08f4d 2021-01-17 op close(pfd->fd);
763 d3a08f4d 2021-01-17 op pfd->fd = -1;
767 d3a08f4d 2021-01-17 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
770 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
771 d3a08f4d 2021-01-17 op socklen_t len;
773 d3a08f4d 2021-01-17 op len = sizeof(addr);
774 d3a08f4d 2021-01-17 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
775 d3a08f4d 2021-01-17 op if (errno == EWOULDBLOCK)
777 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
780 d3a08f4d 2021-01-17 op mark_nonblock(fd);
782 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
783 d3a08f4d 2021-01-17 op if (fds[i].fd == -1) {
784 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
785 d3a08f4d 2021-01-17 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
786 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
788 d3a08f4d 2021-01-17 op fds[i].fd = fd;
789 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
791 d3a08f4d 2021-01-17 op clients[i].state = S_HANDSHAKE;
792 a87f6625 2021-01-24 op clients[i].next = S_SENDING_FILE;
793 d3a08f4d 2021-01-17 op clients[i].fd = -1;
794 d3a08f4d 2021-01-17 op clients[i].waiting_on_child = 0;
795 d3a08f4d 2021-01-17 op clients[i].buf = MAP_FAILED;
796 252908e6 2021-01-24 op clients[i].dir = NULL;
797 d3a08f4d 2021-01-17 op clients[i].addr = addr;
799 d3a08f4d 2021-01-17 op connected_clients++;
808 d3a08f4d 2021-01-17 op handle(struct pollfd *fds, struct client *client)
810 d3a08f4d 2021-01-17 op switch (client->state) {
811 d3a08f4d 2021-01-17 op case S_HANDSHAKE:
812 d3a08f4d 2021-01-17 op handle_handshake(fds, client);
816 d3a08f4d 2021-01-17 op handle_open_conn(fds, client);
819 d3a08f4d 2021-01-17 op case S_INITIALIZING:
820 a87f6625 2021-01-24 op start_reply(fds, client, client->code, client->meta);
823 a87f6625 2021-01-24 op case S_SENDING_FILE:
824 a87f6625 2021-01-24 op send_file(fds, client);
827 252908e6 2021-01-24 op case S_SENDING_DIR:
828 252908e6 2021-01-24 op send_directory_listing(fds, client);
831 a87f6625 2021-01-24 op case S_SENDING_CGI:
832 a87f6625 2021-01-24 op handle_cgi(fds, client);
835 d3a08f4d 2021-01-17 op case S_CLOSING:
836 36162ed8 2021-01-22 op close_conn(fds, client);
840 d3a08f4d 2021-01-17 op /* unreachable */
846 d3a08f4d 2021-01-17 op loop(struct tls *ctx, int sock4, int sock6)
849 d3a08f4d 2021-01-17 op struct client clients[MAX_USERS];
850 d3a08f4d 2021-01-17 op struct pollfd fds[MAX_USERS];
852 d3a08f4d 2021-01-17 op connected_clients = 0;
854 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
855 d3a08f4d 2021-01-17 op fds[i].fd = -1;
856 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
857 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
860 d3a08f4d 2021-01-17 op fds[0].fd = sock4;
861 d3a08f4d 2021-01-17 op fds[1].fd = sock6;
864 d3a08f4d 2021-01-17 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
865 d3a08f4d 2021-01-17 op if (errno == EINTR) {
866 132cae8c 2021-01-18 op fprintf(stderr, "connected clients: %d\n",
867 d3a08f4d 2021-01-17 op connected_clients);
870 d3a08f4d 2021-01-17 op fatal("poll: %s", strerror(errno));
873 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; i++) {
874 d3a08f4d 2021-01-17 op if (fds[i].revents == 0)
877 d3a08f4d 2021-01-17 op if (fds[i].revents & (POLLERR|POLLNVAL))
878 d3a08f4d 2021-01-17 op fatal("bad fd %d: %s", fds[i].fd,
879 d3a08f4d 2021-01-17 op strerror(errno));
881 d3a08f4d 2021-01-17 op if (fds[i].revents & POLLHUP) {
882 d3a08f4d 2021-01-17 op /* fds[i] may be the fd of the stdin
883 d3a08f4d 2021-01-17 op * of a cgi script that has exited. */
884 d3a08f4d 2021-01-17 op if (!clients[i].waiting_on_child) {
885 36162ed8 2021-01-22 op close_conn(&fds[i], &clients[i]);
890 d3a08f4d 2021-01-17 op if (fds[i].fd == sock4)
891 d3a08f4d 2021-01-17 op do_accept(sock4, ctx, fds, clients);
892 d3a08f4d 2021-01-17 op else if (fds[i].fd == sock6)
893 d3a08f4d 2021-01-17 op do_accept(sock6, ctx, fds, clients);
895 d3a08f4d 2021-01-17 op handle(&fds[i], &clients[i]);