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 c8b74339 2021-01-24 op for (loc = v->locations; loc->match != NULL; ++loc) {
39 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
40 c8b74339 2021-01-24 op if (loc->lang != NULL)
41 c8b74339 2021-01-24 op lang = loc->lang;
49 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
51 c8b74339 2021-01-24 op struct location *loc;
52 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
54 c8b74339 2021-01-24 op for (loc = v->locations; loc->match != NULL; ++loc) {
55 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
56 c8b74339 2021-01-24 op if (loc->default_mime != NULL)
57 c8b74339 2021-01-24 op default_mime = loc->default_mime;
61 c8b74339 2021-01-24 op return default_mime;
65 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
67 c8b74339 2021-01-24 op struct location *loc;
68 c8b74339 2021-01-24 op const char *index = "index.gmi";
70 c8b74339 2021-01-24 op for (loc = v->locations; loc->match != NULL; ++loc) {
71 c8b74339 2021-01-24 op if (!fnmatch(loc->match, path, 0)) {
72 c8b74339 2021-01-24 op if (loc->index != NULL)
73 c8b74339 2021-01-24 op index = loc->index;
81 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
83 d3a08f4d 2021-01-17 op struct stat sb;
84 d1ca3911 2021-01-21 op const char *p;
86 d3a08f4d 2021-01-17 op assert(path != NULL);
88 d1ca3911 2021-01-21 op if (*path == '\0')
90 d1ca3911 2021-01-21 op else if (*path == '/')
91 d1ca3911 2021-01-21 op /* in send_dir we add an initial / (to be
92 d1ca3911 2021-01-21 op * redirect-friendly), but here we want to skip it */
97 d1ca3911 2021-01-21 op if ((*fd = openat(c->host->dirfd, p, O_RDONLY | O_NOFOLLOW)) == -1) {
98 d3a08f4d 2021-01-17 op return FILE_MISSING;
101 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
102 d3a08f4d 2021-01-17 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
103 d3a08f4d 2021-01-17 op return FILE_MISSING;
106 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
107 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
109 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
110 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
112 d3a08f4d 2021-01-17 op return FILE_EXISTS;
116 0be51733 2021-01-20 op open_file(struct pollfd *fds, struct client *c)
118 0be51733 2021-01-20 op switch (check_path(c, c->iri.path, &c->fd)) {
119 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
120 07b0a142 2021-01-24 op if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi)) {
121 07b0a142 2021-01-24 op start_cgi(c->iri.path, "", c->iri.query, fds, c);
125 d3a08f4d 2021-01-17 op /* fallthrough */
127 d3a08f4d 2021-01-17 op case FILE_EXISTS:
128 d3a08f4d 2021-01-17 op if ((c->len = filesize(c->fd)) == -1) {
129 0be51733 2021-01-20 op LOGE(c, "failed to get file size for %s", c->iri.path);
130 36162ed8 2021-01-22 op close_conn(fds, c);
134 d3a08f4d 2021-01-17 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
135 d3a08f4d 2021-01-17 op c->fd, 0)) == MAP_FAILED) {
136 0be51733 2021-01-20 op LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
137 36162ed8 2021-01-22 op close_conn(fds, c);
140 d3a08f4d 2021-01-17 op c->i = c->buf;
141 a87f6625 2021-01-24 op c->next = S_SENDING_FILE;
142 a87f6625 2021-01-24 op start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path));
145 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
146 d3a08f4d 2021-01-17 op close(c->fd);
148 0be51733 2021-01-20 op send_dir(fds, c);
151 d3a08f4d 2021-01-17 op case FILE_MISSING:
152 07b0a142 2021-01-24 op if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi)) {
153 07b0a142 2021-01-24 op check_for_cgi(c->iri.path, c->iri.query, fds, c);
156 a87f6625 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
160 d3a08f4d 2021-01-17 op /* unreachable */
167 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
168 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
169 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
170 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
171 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
174 d3a08f4d 2021-01-17 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
177 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
179 d3a08f4d 2021-01-17 op /* NB: assume CGI is enabled and path matches cgi */
181 d3a08f4d 2021-01-17 op while (end > path) {
182 d3a08f4d 2021-01-17 op /* go up one level. UNIX paths are simple and POSIX
183 d3a08f4d 2021-01-17 op * dirname, with its ambiguities on if the given path
184 d3a08f4d 2021-01-17 op * is changed or not, gives me headaches. */
185 d3a08f4d 2021-01-17 op while (*end != '/')
189 d3a08f4d 2021-01-17 op switch (check_path(c, path, &c->fd)) {
190 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
191 07b0a142 2021-01-24 op start_cgi(path, end+1, query, fds,c);
193 d3a08f4d 2021-01-17 op case FILE_MISSING:
204 a87f6625 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
209 d3a08f4d 2021-01-17 op mark_nonblock(int fd)
213 d3a08f4d 2021-01-17 op if ((flags = fcntl(fd, F_GETFL)) == -1)
214 d3a08f4d 2021-01-17 op fatal("fcntl(F_GETFL): %s", strerror(errno));
215 d3a08f4d 2021-01-17 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
216 d3a08f4d 2021-01-17 op fatal("fcntl(F_SETFL): %s", strerror(errno));
220 d3a08f4d 2021-01-17 op handle_handshake(struct pollfd *fds, struct client *c)
222 d3a08f4d 2021-01-17 op struct vhost *h;
223 d3a08f4d 2021-01-17 op const char *servname;
225 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
226 d3a08f4d 2021-01-17 op case 0: /* success */
227 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
229 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
230 d3a08f4d 2021-01-17 op fds->events = POLLIN;
232 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
233 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
236 d3a08f4d 2021-01-17 op /* unreachable */
240 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
242 d3a08f4d 2021-01-17 op for (h = hosts; h->domain != NULL; ++h) {
243 ce79c944 2021-01-21 op if (!strcmp(h->domain, "*"))
246 b4d409cf 2021-01-21 op if (servname != NULL && !fnmatch(h->domain, servname, 0))
250 d3a08f4d 2021-01-17 op if (h->domain != NULL) {
251 d3a08f4d 2021-01-17 op c->state = S_OPEN;
253 d3a08f4d 2021-01-17 op handle_open_conn(fds, c);
257 0ab65593 2021-01-21 op if (servname != NULL)
258 0ab65593 2021-01-21 op strncpy(c->req, servname, sizeof(c->req));
260 0ab65593 2021-01-21 op strncpy(c->req, "null", sizeof(c->req));
262 a87f6625 2021-01-24 op start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI");
266 d3a08f4d 2021-01-17 op handle_open_conn(struct pollfd *fds, struct client *c)
268 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
270 0be51733 2021-01-20 op bzero(c->req, sizeof(c->req));
271 0be51733 2021-01-20 op bzero(&c->iri, sizeof(c->iri));
273 0be51733 2021-01-20 op switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
275 d3a08f4d 2021-01-17 op LOGE(c, "tls_read: %s", tls_error(c->ctx));
276 36162ed8 2021-01-22 op close_conn(fds, c);
279 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
280 d3a08f4d 2021-01-17 op fds->events = POLLIN;
283 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
284 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
288 0be51733 2021-01-20 op if (!trim_req_iri(c->req) || !parse_iri(c->req, &c->iri, &parse_err)) {
289 a87f6625 2021-01-24 op start_reply(fds, c, BAD_REQUEST, parse_err);
293 0be51733 2021-01-20 op /* XXX: we should check that the SNI matches the requested host */
294 0be51733 2021-01-20 op if (strcmp(c->iri.schema, "gemini") || c->iri.port_no != conf.port) {
295 a87f6625 2021-01-24 op start_reply(fds, c, PROXY_REFUSED, "won't proxy request");
299 0be51733 2021-01-20 op open_file(fds, c);
303 df79b4c1 2021-01-19 op start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
305 05c23a54 2021-01-19 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
306 c8b74339 2021-01-24 op const char *lang;
309 df79b4c1 2021-01-19 op c->code = code;
310 df79b4c1 2021-01-19 op c->meta = meta;
311 df79b4c1 2021-01-19 op c->state = S_INITIALIZING;
313 c8b74339 2021-01-24 op lang = vhost_lang(c->host, c->iri.path);
315 05c23a54 2021-01-19 op snprintf(buf, sizeof(buf), "%d ", code);
316 df79b4c1 2021-01-19 op strlcat(buf, meta, sizeof(buf));
317 c8b74339 2021-01-24 op if (!strcmp(meta, "text/gemini") && lang != NULL) {
318 05c23a54 2021-01-19 op strlcat(buf, "; lang=", sizeof(buf));
319 c8b74339 2021-01-24 op strlcat(buf, lang, sizeof(buf));
322 05c23a54 2021-01-19 op len = strlcat(buf, "\r\n", sizeof(buf));
323 0be51733 2021-01-20 op assert(len < sizeof(buf));
325 df79b4c1 2021-01-19 op switch (tls_write(c->ctx, buf, len)) {
327 a87f6625 2021-01-24 op close_conn(pfd, c);
329 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
330 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
332 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
333 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
337 a87f6625 2021-01-24 op log_request(c, buf, sizeof(buf));
339 a87f6625 2021-01-24 op /* we don't need a body */
340 a87f6625 2021-01-24 op if (c->code != SUCCESS) {
341 a87f6625 2021-01-24 op close_conn(pfd, c);
345 a87f6625 2021-01-24 op /* advance the state machine */
346 a87f6625 2021-01-24 op c->state = c->next;
347 a87f6625 2021-01-24 op handle(pfd, c);
351 d3a08f4d 2021-01-17 op start_cgi(const char *spath, const char *relpath, const char *query,
352 d3a08f4d 2021-01-17 op struct pollfd *fds, struct client *c)
354 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
355 d3a08f4d 2021-01-17 op const char *ruser, *cissuer, *chash;
358 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
359 d3a08f4d 2021-01-17 op addr, sizeof(addr),
361 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
365 d3a08f4d 2021-01-17 op if (tls_peer_cert_provided(c->ctx)) {
366 d3a08f4d 2021-01-17 op ruser = tls_peer_cert_subject(c->ctx);
367 d3a08f4d 2021-01-17 op cissuer = tls_peer_cert_issuer(c->ctx);
368 d3a08f4d 2021-01-17 op chash = tls_peer_cert_hash(c->ctx);
370 d3a08f4d 2021-01-17 op ruser = NULL;
371 d3a08f4d 2021-01-17 op cissuer = NULL;
372 d3a08f4d 2021-01-17 op chash = NULL;
375 d3a08f4d 2021-01-17 op if (!send_string(exfd, spath)
376 d3a08f4d 2021-01-17 op || !send_string(exfd, relpath)
377 d3a08f4d 2021-01-17 op || !send_string(exfd, query)
378 d3a08f4d 2021-01-17 op || !send_string(exfd, addr)
379 d3a08f4d 2021-01-17 op || !send_string(exfd, ruser)
380 d3a08f4d 2021-01-17 op || !send_string(exfd, cissuer)
381 d3a08f4d 2021-01-17 op || !send_string(exfd, chash)
382 d3a08f4d 2021-01-17 op || !send_vhost(exfd, c->host))
385 d3a08f4d 2021-01-17 op close(c->fd);
386 d3a08f4d 2021-01-17 op if ((c->fd = recv_fd(exfd)) == -1) {
387 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
390 a87f6625 2021-01-24 op c->state = S_SENDING_CGI;
391 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
392 0be51733 2021-01-20 op c->code = -1;
393 d3a08f4d 2021-01-17 op /* handle_cgi(fds, c); */
397 d3a08f4d 2021-01-17 op /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
398 132cae8c 2021-01-18 op fatal("cannot talk to the executor process");
402 0be51733 2021-01-20 op send_file(struct pollfd *fds, struct client *c)
404 d3a08f4d 2021-01-17 op ssize_t ret, len;
406 06f233ad 2021-01-21 op /* ensure the correct state */
407 a87f6625 2021-01-24 op c->state = S_SENDING_FILE;
409 d3a08f4d 2021-01-17 op len = (c->buf + c->len) - c->i;
411 d3a08f4d 2021-01-17 op while (len > 0) {
412 d3a08f4d 2021-01-17 op switch (ret = tls_write(c->ctx, c->i, len)) {
414 d3a08f4d 2021-01-17 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
415 36162ed8 2021-01-22 op close_conn(fds, c);
418 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
419 d3a08f4d 2021-01-17 op fds->events = POLLIN;
422 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
423 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
433 36162ed8 2021-01-22 op close_conn(fds, c);
437 0be51733 2021-01-20 op send_dir(struct pollfd *fds, struct client *c)
441 e7a2a99b 2021-01-24 op /* guard against a re-entrant call: open_file -> send_dir ->
442 e7a2a99b 2021-01-24 op * open_file -> send_dir. This can happen only if:
444 0be51733 2021-01-20 op * - user requested a dir, say foo/
445 e7a2a99b 2021-01-24 op * - we try to serve foo/$INDEX
446 e7a2a99b 2021-01-24 op * - foo/$INDEX is a directory.
448 0be51733 2021-01-20 op if (c->iri.path == c->sbuf) {
449 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
453 d1ca3911 2021-01-21 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
455 0be51733 2021-01-20 op len = strlen(c->iri.path);
456 0be51733 2021-01-20 op if (len > 0 && c->iri.path[len-1] != '/') {
457 0be51733 2021-01-20 op /* redirect to url with the trailing / */
458 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
459 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
460 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
464 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
466 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
467 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
469 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
470 c8b74339 2021-01-24 op sizeof(c->sbuf));
472 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
473 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
477 0be51733 2021-01-20 op close(c->fd);
478 0be51733 2021-01-20 op c->iri.path = c->sbuf;
479 0be51733 2021-01-20 op open_file(fds, c);
483 d3a08f4d 2021-01-17 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
487 d3a08f4d 2021-01-17 op if (c->waiting_on_child)
489 d3a08f4d 2021-01-17 op c->waiting_on_child = 1;
491 d3a08f4d 2021-01-17 op fds->events = POLLIN;
493 d3a08f4d 2021-01-17 op fd = fds->fd;
494 d3a08f4d 2021-01-17 op fds->fd = c->fd;
499 d3a08f4d 2021-01-17 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
503 d3a08f4d 2021-01-17 op if (!c->waiting_on_child)
505 d3a08f4d 2021-01-17 op c->waiting_on_child = 0;
507 d3a08f4d 2021-01-17 op fd = fds->fd;
508 d3a08f4d 2021-01-17 op fds->fd = c->fd;
512 3309ef97 2021-01-23 op /* handle the read from the child process. Return like read(2) */
513 3309ef97 2021-01-23 op static ssize_t
514 3309ef97 2021-01-23 op read_from_cgi(struct client *c)
520 3309ef97 2021-01-23 op /* if we haven't read a whole response line, we want to
521 3309ef97 2021-01-23 op * continue reading. */
523 3309ef97 2021-01-23 op if (c->code == -1) {
524 3309ef97 2021-01-23 op buf = c->sbuf + c->len;
525 3309ef97 2021-01-23 op len = sizeof(c->sbuf) - c->len;
527 3309ef97 2021-01-23 op buf = c->sbuf;
528 3309ef97 2021-01-23 op len = sizeof(c->sbuf);
531 3309ef97 2021-01-23 op r = read(c->fd, buf, len);
532 3309ef97 2021-01-23 op if (r == 0 || r == -1)
538 3309ef97 2021-01-23 op if (c->code != -1)
541 3309ef97 2021-01-23 op if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
543 3309ef97 2021-01-23 op log_request(c, c->sbuf, c->len);
550 d3a08f4d 2021-01-17 op handle_cgi(struct pollfd *fds, struct client *c)
554 d3a08f4d 2021-01-17 op /* ensure c->fd is the child and fds->fd the client */
555 d3a08f4d 2021-01-17 op cgi_poll_on_client(fds, c);
558 3309ef97 2021-01-23 op if (c->code == -1 || c->len == 0) {
559 3309ef97 2021-01-23 op switch (r = read_from_cgi(c)) {
564 d3a08f4d 2021-01-17 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
565 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
572 3309ef97 2021-01-23 op if (c->code == -1) {
573 3309ef97 2021-01-23 op cgi_poll_on_child(fds, c);
577 d3a08f4d 2021-01-17 op while (c->len > 0) {
578 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
582 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
583 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
586 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
587 d3a08f4d 2021-01-17 op fds->events = POLLIN;
599 36162ed8 2021-01-22 op close_conn(fds, c);
603 36162ed8 2021-01-22 op close_conn(struct pollfd *pfd, struct client *c)
605 d3a08f4d 2021-01-17 op c->state = S_CLOSING;
607 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
608 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
609 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
611 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
612 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
616 d3a08f4d 2021-01-17 op connected_clients--;
618 d3a08f4d 2021-01-17 op tls_free(c->ctx);
619 d3a08f4d 2021-01-17 op c->ctx = NULL;
621 d3a08f4d 2021-01-17 op if (c->buf != MAP_FAILED)
622 d3a08f4d 2021-01-17 op munmap(c->buf, c->len);
624 d3a08f4d 2021-01-17 op if (c->fd != -1)
625 d3a08f4d 2021-01-17 op close(c->fd);
627 d3a08f4d 2021-01-17 op close(pfd->fd);
628 d3a08f4d 2021-01-17 op pfd->fd = -1;
632 d3a08f4d 2021-01-17 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
635 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
636 d3a08f4d 2021-01-17 op socklen_t len;
638 d3a08f4d 2021-01-17 op len = sizeof(addr);
639 d3a08f4d 2021-01-17 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
640 d3a08f4d 2021-01-17 op if (errno == EWOULDBLOCK)
642 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
645 d3a08f4d 2021-01-17 op mark_nonblock(fd);
647 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
648 d3a08f4d 2021-01-17 op if (fds[i].fd == -1) {
649 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
650 d3a08f4d 2021-01-17 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
651 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
653 d3a08f4d 2021-01-17 op fds[i].fd = fd;
654 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
656 d3a08f4d 2021-01-17 op clients[i].state = S_HANDSHAKE;
657 a87f6625 2021-01-24 op clients[i].next = S_SENDING_FILE;
658 d3a08f4d 2021-01-17 op clients[i].fd = -1;
659 d3a08f4d 2021-01-17 op clients[i].waiting_on_child = 0;
660 d3a08f4d 2021-01-17 op clients[i].buf = MAP_FAILED;
661 d3a08f4d 2021-01-17 op clients[i].addr = addr;
663 d3a08f4d 2021-01-17 op connected_clients++;
672 d3a08f4d 2021-01-17 op handle(struct pollfd *fds, struct client *client)
674 d3a08f4d 2021-01-17 op switch (client->state) {
675 d3a08f4d 2021-01-17 op case S_HANDSHAKE:
676 d3a08f4d 2021-01-17 op handle_handshake(fds, client);
680 d3a08f4d 2021-01-17 op handle_open_conn(fds, client);
683 d3a08f4d 2021-01-17 op case S_INITIALIZING:
684 a87f6625 2021-01-24 op start_reply(fds, client, client->code, client->meta);
687 a87f6625 2021-01-24 op case S_SENDING_FILE:
688 a87f6625 2021-01-24 op send_file(fds, client);
691 a87f6625 2021-01-24 op case S_SENDING_CGI:
692 a87f6625 2021-01-24 op handle_cgi(fds, client);
695 d3a08f4d 2021-01-17 op case S_CLOSING:
696 36162ed8 2021-01-22 op close_conn(fds, client);
700 d3a08f4d 2021-01-17 op /* unreachable */
706 d3a08f4d 2021-01-17 op loop(struct tls *ctx, int sock4, int sock6)
709 d3a08f4d 2021-01-17 op struct client clients[MAX_USERS];
710 d3a08f4d 2021-01-17 op struct pollfd fds[MAX_USERS];
712 d3a08f4d 2021-01-17 op connected_clients = 0;
714 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
715 d3a08f4d 2021-01-17 op fds[i].fd = -1;
716 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
717 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
720 d3a08f4d 2021-01-17 op fds[0].fd = sock4;
721 d3a08f4d 2021-01-17 op fds[1].fd = sock6;
724 d3a08f4d 2021-01-17 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
725 d3a08f4d 2021-01-17 op if (errno == EINTR) {
726 132cae8c 2021-01-18 op fprintf(stderr, "connected clients: %d\n",
727 d3a08f4d 2021-01-17 op connected_clients);
730 d3a08f4d 2021-01-17 op fatal("poll: %s", strerror(errno));
733 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; i++) {
734 d3a08f4d 2021-01-17 op if (fds[i].revents == 0)
737 d3a08f4d 2021-01-17 op if (fds[i].revents & (POLLERR|POLLNVAL))
738 d3a08f4d 2021-01-17 op fatal("bad fd %d: %s", fds[i].fd,
739 d3a08f4d 2021-01-17 op strerror(errno));
741 d3a08f4d 2021-01-17 op if (fds[i].revents & POLLHUP) {
742 d3a08f4d 2021-01-17 op /* fds[i] may be the fd of the stdin
743 d3a08f4d 2021-01-17 op * of a cgi script that has exited. */
744 d3a08f4d 2021-01-17 op if (!clients[i].waiting_on_child) {
745 36162ed8 2021-01-22 op close_conn(&fds[i], &clients[i]);
750 d3a08f4d 2021-01-17 op if (fds[i].fd == sock4)
751 d3a08f4d 2021-01-17 op do_accept(sock4, ctx, fds, clients);
752 d3a08f4d 2021-01-17 op else if (fds[i].fd == sock6)
753 d3a08f4d 2021-01-17 op do_accept(sock6, ctx, fds, clients);
755 d3a08f4d 2021-01-17 op handle(&fds[i], &clients[i]);