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 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
35 d3a08f4d 2021-01-17 op struct stat sb;
36 d1ca3911 2021-01-21 op const char *p;
38 d3a08f4d 2021-01-17 op assert(path != NULL);
40 d1ca3911 2021-01-21 op if (*path == '\0')
42 d1ca3911 2021-01-21 op else if (*path == '/')
43 d1ca3911 2021-01-21 op /* in send_dir we add an initial / (to be
44 d1ca3911 2021-01-21 op * redirect-friendly), but here we want to skip it */
49 d1ca3911 2021-01-21 op if ((*fd = openat(c->host->dirfd, p, O_RDONLY | O_NOFOLLOW)) == -1) {
50 d3a08f4d 2021-01-17 op return FILE_MISSING;
53 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
54 d3a08f4d 2021-01-17 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
55 d3a08f4d 2021-01-17 op return FILE_MISSING;
58 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
59 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
61 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
62 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
64 d3a08f4d 2021-01-17 op return FILE_EXISTS;
68 0be51733 2021-01-20 op open_file(struct pollfd *fds, struct client *c)
70 0be51733 2021-01-20 op switch (check_path(c, c->iri.path, &c->fd)) {
71 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
72 07b0a142 2021-01-24 op if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi)) {
73 07b0a142 2021-01-24 op start_cgi(c->iri.path, "", c->iri.query, fds, c);
77 d3a08f4d 2021-01-17 op /* fallthrough */
79 d3a08f4d 2021-01-17 op case FILE_EXISTS:
80 d3a08f4d 2021-01-17 op if ((c->len = filesize(c->fd)) == -1) {
81 0be51733 2021-01-20 op LOGE(c, "failed to get file size for %s", c->iri.path);
82 36162ed8 2021-01-22 op close_conn(fds, c);
86 d3a08f4d 2021-01-17 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
87 d3a08f4d 2021-01-17 op c->fd, 0)) == MAP_FAILED) {
88 0be51733 2021-01-20 op LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
89 36162ed8 2021-01-22 op close_conn(fds, c);
92 d3a08f4d 2021-01-17 op c->i = c->buf;
93 a87f6625 2021-01-24 op c->next = S_SENDING_FILE;
94 a87f6625 2021-01-24 op start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path));
97 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
100 0be51733 2021-01-20 op send_dir(fds, c);
103 d3a08f4d 2021-01-17 op case FILE_MISSING:
104 07b0a142 2021-01-24 op if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi)) {
105 07b0a142 2021-01-24 op check_for_cgi(c->iri.path, c->iri.query, fds, c);
108 a87f6625 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
112 d3a08f4d 2021-01-17 op /* unreachable */
119 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
120 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
121 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
122 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
123 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
126 d3a08f4d 2021-01-17 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
129 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
131 d3a08f4d 2021-01-17 op /* NB: assume CGI is enabled and path matches cgi */
133 d3a08f4d 2021-01-17 op while (end > path) {
134 d3a08f4d 2021-01-17 op /* go up one level. UNIX paths are simple and POSIX
135 d3a08f4d 2021-01-17 op * dirname, with its ambiguities on if the given path
136 d3a08f4d 2021-01-17 op * is changed or not, gives me headaches. */
137 d3a08f4d 2021-01-17 op while (*end != '/')
141 d3a08f4d 2021-01-17 op switch (check_path(c, path, &c->fd)) {
142 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
143 07b0a142 2021-01-24 op start_cgi(path, end+1, query, fds,c);
145 d3a08f4d 2021-01-17 op case FILE_MISSING:
156 a87f6625 2021-01-24 op start_reply(fds, c, NOT_FOUND, "not found");
161 d3a08f4d 2021-01-17 op mark_nonblock(int fd)
165 d3a08f4d 2021-01-17 op if ((flags = fcntl(fd, F_GETFL)) == -1)
166 d3a08f4d 2021-01-17 op fatal("fcntl(F_GETFL): %s", strerror(errno));
167 d3a08f4d 2021-01-17 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
168 d3a08f4d 2021-01-17 op fatal("fcntl(F_SETFL): %s", strerror(errno));
172 d3a08f4d 2021-01-17 op handle_handshake(struct pollfd *fds, struct client *c)
174 d3a08f4d 2021-01-17 op struct vhost *h;
175 d3a08f4d 2021-01-17 op const char *servname;
177 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
178 d3a08f4d 2021-01-17 op case 0: /* success */
179 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
181 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
182 d3a08f4d 2021-01-17 op fds->events = POLLIN;
184 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
185 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
188 d3a08f4d 2021-01-17 op /* unreachable */
192 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
194 d3a08f4d 2021-01-17 op for (h = hosts; h->domain != NULL; ++h) {
195 ce79c944 2021-01-21 op if (!strcmp(h->domain, "*"))
198 b4d409cf 2021-01-21 op if (servname != NULL && !fnmatch(h->domain, servname, 0))
202 d3a08f4d 2021-01-17 op if (h->domain != NULL) {
203 d3a08f4d 2021-01-17 op c->state = S_OPEN;
205 d3a08f4d 2021-01-17 op handle_open_conn(fds, c);
209 0ab65593 2021-01-21 op if (servname != NULL)
210 0ab65593 2021-01-21 op strncpy(c->req, servname, sizeof(c->req));
212 0ab65593 2021-01-21 op strncpy(c->req, "null", sizeof(c->req));
214 a87f6625 2021-01-24 op start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI");
218 d3a08f4d 2021-01-17 op handle_open_conn(struct pollfd *fds, struct client *c)
220 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
222 0be51733 2021-01-20 op bzero(c->req, sizeof(c->req));
223 0be51733 2021-01-20 op bzero(&c->iri, sizeof(c->iri));
225 0be51733 2021-01-20 op switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
227 d3a08f4d 2021-01-17 op LOGE(c, "tls_read: %s", tls_error(c->ctx));
228 36162ed8 2021-01-22 op close_conn(fds, c);
231 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
232 d3a08f4d 2021-01-17 op fds->events = POLLIN;
235 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
236 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
240 0be51733 2021-01-20 op if (!trim_req_iri(c->req) || !parse_iri(c->req, &c->iri, &parse_err)) {
241 a87f6625 2021-01-24 op start_reply(fds, c, BAD_REQUEST, parse_err);
245 0be51733 2021-01-20 op /* XXX: we should check that the SNI matches the requested host */
246 0be51733 2021-01-20 op if (strcmp(c->iri.schema, "gemini") || c->iri.port_no != conf.port) {
247 a87f6625 2021-01-24 op start_reply(fds, c, PROXY_REFUSED, "won't proxy request");
251 0be51733 2021-01-20 op open_file(fds, c);
255 df79b4c1 2021-01-19 op start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
257 05c23a54 2021-01-19 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
260 df79b4c1 2021-01-19 op c->code = code;
261 df79b4c1 2021-01-19 op c->meta = meta;
262 df79b4c1 2021-01-19 op c->state = S_INITIALIZING;
264 05c23a54 2021-01-19 op snprintf(buf, sizeof(buf), "%d ", code);
265 df79b4c1 2021-01-19 op strlcat(buf, meta, sizeof(buf));
266 df79b4c1 2021-01-19 op if (!strcmp(meta, "text/gemini") && c->host->lang != NULL) {
267 05c23a54 2021-01-19 op strlcat(buf, "; lang=", sizeof(buf));
268 df79b4c1 2021-01-19 op strlcat(buf, c->host->lang, sizeof(buf));
271 05c23a54 2021-01-19 op len = strlcat(buf, "\r\n", sizeof(buf));
272 0be51733 2021-01-20 op assert(len < sizeof(buf));
274 df79b4c1 2021-01-19 op switch (tls_write(c->ctx, buf, len)) {
276 a87f6625 2021-01-24 op close_conn(pfd, c);
278 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
279 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
281 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
282 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
286 a87f6625 2021-01-24 op log_request(c, buf, sizeof(buf));
288 a87f6625 2021-01-24 op /* we don't need a body */
289 a87f6625 2021-01-24 op if (c->code != SUCCESS) {
290 a87f6625 2021-01-24 op close_conn(pfd, c);
294 a87f6625 2021-01-24 op /* advance the state machine */
295 a87f6625 2021-01-24 op c->state = c->next;
296 a87f6625 2021-01-24 op handle(pfd, c);
300 d3a08f4d 2021-01-17 op start_cgi(const char *spath, const char *relpath, const char *query,
301 d3a08f4d 2021-01-17 op struct pollfd *fds, struct client *c)
303 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
304 d3a08f4d 2021-01-17 op const char *ruser, *cissuer, *chash;
307 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
308 d3a08f4d 2021-01-17 op addr, sizeof(addr),
310 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
314 d3a08f4d 2021-01-17 op if (tls_peer_cert_provided(c->ctx)) {
315 d3a08f4d 2021-01-17 op ruser = tls_peer_cert_subject(c->ctx);
316 d3a08f4d 2021-01-17 op cissuer = tls_peer_cert_issuer(c->ctx);
317 d3a08f4d 2021-01-17 op chash = tls_peer_cert_hash(c->ctx);
319 d3a08f4d 2021-01-17 op ruser = NULL;
320 d3a08f4d 2021-01-17 op cissuer = NULL;
321 d3a08f4d 2021-01-17 op chash = NULL;
324 d3a08f4d 2021-01-17 op if (!send_string(exfd, spath)
325 d3a08f4d 2021-01-17 op || !send_string(exfd, relpath)
326 d3a08f4d 2021-01-17 op || !send_string(exfd, query)
327 d3a08f4d 2021-01-17 op || !send_string(exfd, addr)
328 d3a08f4d 2021-01-17 op || !send_string(exfd, ruser)
329 d3a08f4d 2021-01-17 op || !send_string(exfd, cissuer)
330 d3a08f4d 2021-01-17 op || !send_string(exfd, chash)
331 d3a08f4d 2021-01-17 op || !send_vhost(exfd, c->host))
334 d3a08f4d 2021-01-17 op close(c->fd);
335 d3a08f4d 2021-01-17 op if ((c->fd = recv_fd(exfd)) == -1) {
336 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
339 a87f6625 2021-01-24 op c->state = S_SENDING_CGI;
340 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
341 0be51733 2021-01-20 op c->code = -1;
342 d3a08f4d 2021-01-17 op /* handle_cgi(fds, c); */
346 d3a08f4d 2021-01-17 op /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
347 132cae8c 2021-01-18 op fatal("cannot talk to the executor process");
351 0be51733 2021-01-20 op send_file(struct pollfd *fds, struct client *c)
353 d3a08f4d 2021-01-17 op ssize_t ret, len;
355 06f233ad 2021-01-21 op /* ensure the correct state */
356 a87f6625 2021-01-24 op c->state = S_SENDING_FILE;
358 d3a08f4d 2021-01-17 op len = (c->buf + c->len) - c->i;
360 d3a08f4d 2021-01-17 op while (len > 0) {
361 d3a08f4d 2021-01-17 op switch (ret = tls_write(c->ctx, c->i, len)) {
363 d3a08f4d 2021-01-17 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
364 36162ed8 2021-01-22 op close_conn(fds, c);
367 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
368 d3a08f4d 2021-01-17 op fds->events = POLLIN;
371 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
372 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
382 36162ed8 2021-01-22 op close_conn(fds, c);
386 0be51733 2021-01-20 op send_dir(struct pollfd *fds, struct client *c)
389 e7a2a99b 2021-01-24 op const char *index = "index.gmi";
391 e7a2a99b 2021-01-24 op /* guard against a re-entrant call: open_file -> send_dir ->
392 e7a2a99b 2021-01-24 op * open_file -> send_dir. This can happen only if:
394 0be51733 2021-01-20 op * - user requested a dir, say foo/
395 e7a2a99b 2021-01-24 op * - we try to serve foo/$INDEX
396 e7a2a99b 2021-01-24 op * - foo/$INDEX is a directory.
398 0be51733 2021-01-20 op if (c->iri.path == c->sbuf) {
399 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
403 d1ca3911 2021-01-21 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
405 0be51733 2021-01-20 op len = strlen(c->iri.path);
406 0be51733 2021-01-20 op if (len > 0 && c->iri.path[len-1] != '/') {
407 0be51733 2021-01-20 op /* redirect to url with the trailing / */
408 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
409 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
410 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
414 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
416 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
417 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
419 e7a2a99b 2021-01-24 op if (c->host->index != NULL)
420 e7a2a99b 2021-01-24 op index = c->host->index;
421 e7a2a99b 2021-01-24 op len = strlcat(c->sbuf, index, sizeof(c->sbuf));
423 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
424 a87f6625 2021-01-24 op start_reply(fds, c, TEMP_FAILURE, "internal server error");
428 0be51733 2021-01-20 op close(c->fd);
429 0be51733 2021-01-20 op c->iri.path = c->sbuf;
430 0be51733 2021-01-20 op open_file(fds, c);
434 d3a08f4d 2021-01-17 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
438 d3a08f4d 2021-01-17 op if (c->waiting_on_child)
440 d3a08f4d 2021-01-17 op c->waiting_on_child = 1;
442 d3a08f4d 2021-01-17 op fds->events = POLLIN;
444 d3a08f4d 2021-01-17 op fd = fds->fd;
445 d3a08f4d 2021-01-17 op fds->fd = c->fd;
450 d3a08f4d 2021-01-17 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
454 d3a08f4d 2021-01-17 op if (!c->waiting_on_child)
456 d3a08f4d 2021-01-17 op c->waiting_on_child = 0;
458 d3a08f4d 2021-01-17 op fd = fds->fd;
459 d3a08f4d 2021-01-17 op fds->fd = c->fd;
463 3309ef97 2021-01-23 op /* handle the read from the child process. Return like read(2) */
464 3309ef97 2021-01-23 op static ssize_t
465 3309ef97 2021-01-23 op read_from_cgi(struct client *c)
471 3309ef97 2021-01-23 op /* if we haven't read a whole response line, we want to
472 3309ef97 2021-01-23 op * continue reading. */
474 3309ef97 2021-01-23 op if (c->code == -1) {
475 3309ef97 2021-01-23 op buf = c->sbuf + c->len;
476 3309ef97 2021-01-23 op len = sizeof(c->sbuf) - c->len;
478 3309ef97 2021-01-23 op buf = c->sbuf;
479 3309ef97 2021-01-23 op len = sizeof(c->sbuf);
482 3309ef97 2021-01-23 op r = read(c->fd, buf, len);
483 3309ef97 2021-01-23 op if (r == 0 || r == -1)
489 3309ef97 2021-01-23 op if (c->code != -1)
492 3309ef97 2021-01-23 op if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
494 3309ef97 2021-01-23 op log_request(c, c->sbuf, c->len);
501 d3a08f4d 2021-01-17 op handle_cgi(struct pollfd *fds, struct client *c)
505 d3a08f4d 2021-01-17 op /* ensure c->fd is the child and fds->fd the client */
506 d3a08f4d 2021-01-17 op cgi_poll_on_client(fds, c);
509 3309ef97 2021-01-23 op if (c->code == -1 || c->len == 0) {
510 3309ef97 2021-01-23 op switch (r = read_from_cgi(c)) {
515 d3a08f4d 2021-01-17 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
516 d3a08f4d 2021-01-17 op cgi_poll_on_child(fds, c);
523 3309ef97 2021-01-23 op if (c->code == -1) {
524 3309ef97 2021-01-23 op cgi_poll_on_child(fds, c);
528 d3a08f4d 2021-01-17 op while (c->len > 0) {
529 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
533 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
534 d3a08f4d 2021-01-17 op fds->events = POLLOUT;
537 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
538 d3a08f4d 2021-01-17 op fds->events = POLLIN;
550 36162ed8 2021-01-22 op close_conn(fds, c);
554 36162ed8 2021-01-22 op close_conn(struct pollfd *pfd, struct client *c)
556 d3a08f4d 2021-01-17 op c->state = S_CLOSING;
558 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
559 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
560 d3a08f4d 2021-01-17 op pfd->events = POLLIN;
562 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
563 d3a08f4d 2021-01-17 op pfd->events = POLLOUT;
567 d3a08f4d 2021-01-17 op connected_clients--;
569 d3a08f4d 2021-01-17 op tls_free(c->ctx);
570 d3a08f4d 2021-01-17 op c->ctx = NULL;
572 d3a08f4d 2021-01-17 op if (c->buf != MAP_FAILED)
573 d3a08f4d 2021-01-17 op munmap(c->buf, c->len);
575 d3a08f4d 2021-01-17 op if (c->fd != -1)
576 d3a08f4d 2021-01-17 op close(c->fd);
578 d3a08f4d 2021-01-17 op close(pfd->fd);
579 d3a08f4d 2021-01-17 op pfd->fd = -1;
583 d3a08f4d 2021-01-17 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
586 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
587 d3a08f4d 2021-01-17 op socklen_t len;
589 d3a08f4d 2021-01-17 op len = sizeof(addr);
590 d3a08f4d 2021-01-17 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
591 d3a08f4d 2021-01-17 op if (errno == EWOULDBLOCK)
593 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
596 d3a08f4d 2021-01-17 op mark_nonblock(fd);
598 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
599 d3a08f4d 2021-01-17 op if (fds[i].fd == -1) {
600 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
601 d3a08f4d 2021-01-17 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
602 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
604 d3a08f4d 2021-01-17 op fds[i].fd = fd;
605 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
607 d3a08f4d 2021-01-17 op clients[i].state = S_HANDSHAKE;
608 a87f6625 2021-01-24 op clients[i].next = S_SENDING_FILE;
609 d3a08f4d 2021-01-17 op clients[i].fd = -1;
610 d3a08f4d 2021-01-17 op clients[i].waiting_on_child = 0;
611 d3a08f4d 2021-01-17 op clients[i].buf = MAP_FAILED;
612 d3a08f4d 2021-01-17 op clients[i].addr = addr;
614 d3a08f4d 2021-01-17 op connected_clients++;
623 d3a08f4d 2021-01-17 op handle(struct pollfd *fds, struct client *client)
625 d3a08f4d 2021-01-17 op switch (client->state) {
626 d3a08f4d 2021-01-17 op case S_HANDSHAKE:
627 d3a08f4d 2021-01-17 op handle_handshake(fds, client);
631 d3a08f4d 2021-01-17 op handle_open_conn(fds, client);
634 d3a08f4d 2021-01-17 op case S_INITIALIZING:
635 a87f6625 2021-01-24 op start_reply(fds, client, client->code, client->meta);
638 a87f6625 2021-01-24 op case S_SENDING_FILE:
639 a87f6625 2021-01-24 op send_file(fds, client);
642 a87f6625 2021-01-24 op case S_SENDING_CGI:
643 a87f6625 2021-01-24 op handle_cgi(fds, client);
646 d3a08f4d 2021-01-17 op case S_CLOSING:
647 36162ed8 2021-01-22 op close_conn(fds, client);
651 d3a08f4d 2021-01-17 op /* unreachable */
657 d3a08f4d 2021-01-17 op loop(struct tls *ctx, int sock4, int sock6)
660 d3a08f4d 2021-01-17 op struct client clients[MAX_USERS];
661 d3a08f4d 2021-01-17 op struct pollfd fds[MAX_USERS];
663 d3a08f4d 2021-01-17 op connected_clients = 0;
665 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
666 d3a08f4d 2021-01-17 op fds[i].fd = -1;
667 d3a08f4d 2021-01-17 op fds[i].events = POLLIN;
668 d3a08f4d 2021-01-17 op bzero(&clients[i], sizeof(struct client));
671 d3a08f4d 2021-01-17 op fds[0].fd = sock4;
672 d3a08f4d 2021-01-17 op fds[1].fd = sock6;
675 d3a08f4d 2021-01-17 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
676 d3a08f4d 2021-01-17 op if (errno == EINTR) {
677 132cae8c 2021-01-18 op fprintf(stderr, "connected clients: %d\n",
678 d3a08f4d 2021-01-17 op connected_clients);
681 d3a08f4d 2021-01-17 op fatal("poll: %s", strerror(errno));
684 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; i++) {
685 d3a08f4d 2021-01-17 op if (fds[i].revents == 0)
688 d3a08f4d 2021-01-17 op if (fds[i].revents & (POLLERR|POLLNVAL))
689 d3a08f4d 2021-01-17 op fatal("bad fd %d: %s", fds[i].fd,
690 d3a08f4d 2021-01-17 op strerror(errno));
692 d3a08f4d 2021-01-17 op if (fds[i].revents & POLLHUP) {
693 d3a08f4d 2021-01-17 op /* fds[i] may be the fd of the stdin
694 d3a08f4d 2021-01-17 op * of a cgi script that has exited. */
695 d3a08f4d 2021-01-17 op if (!clients[i].waiting_on_child) {
696 36162ed8 2021-01-22 op close_conn(&fds[i], &clients[i]);
701 d3a08f4d 2021-01-17 op if (fds[i].fd == sock4)
702 d3a08f4d 2021-01-17 op do_accept(sock4, ctx, fds, clients);
703 d3a08f4d 2021-01-17 op else if (fds[i].fd == sock6)
704 d3a08f4d 2021-01-17 op do_accept(sock6, ctx, fds, clients);
706 d3a08f4d 2021-01-17 op handle(&fds[i], &clients[i]);