0002
2022-07-04
op
* Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
0004
2021-01-17
op
* Permission to use, copy, modify, and distribute this software for any
0005
2021-01-17
op
* purpose with or without fee is hereby granted, provided that the above
0006
2021-01-17
op
* copyright notice and this permission notice appear in all copies.
0008
2021-01-17
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0009
2021-01-17
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0010
2021-01-17
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0011
2021-01-17
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0012
2021-01-17
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0013
2021-01-17
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0014
2021-01-17
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0017
2021-02-12
op
#include "gmid.h"
0019
2021-01-17
op
#include <sys/stat.h>
0020
2022-09-06
op
#include <sys/un.h>
0022
2021-01-17
op
#include <assert.h>
0023
2021-10-02
op
#include <ctype.h>
0024
2021-01-17
op
#include <errno.h>
0025
2021-02-08
op
#include <event.h>
0026
2021-01-17
op
#include <fcntl.h>
0027
2021-01-21
op
#include <fnmatch.h>
0028
2021-02-01
op
#include <limits.h>
0029
2021-01-17
op
#include <string.h>
0031
2021-10-02
op
#define MIN(a, b) ((a) < (b) ? (a) : (b))
0033
2021-07-06
op
int shutting_down;
0035
2021-03-19
op
static struct tls *ctx;
0037
2021-03-19
op
static struct event e4, e6, imsgev, siginfo, sigusr2;
0038
2021-02-23
op
static int has_ipv6, has_siginfo;
0040
2021-01-17
op
int connected_clients;
0042
2021-02-10
op
static inline int matches(const char*, const char*);
0044
2021-02-02
op
static int check_path(struct client*, const char*, int*);
0045
2021-02-08
op
static void open_file(struct client*);
0046
2021-02-08
op
static void handle_handshake(int, short, void*);
0047
2021-05-15
op
static const char *strip_path(const char*, int);
0048
2021-02-08
op
static void fmt_sbuf(const char*, struct client*, const char*);
0049
2021-02-08
op
static int apply_block_return(struct client*);
0050
2022-01-04
op
static int check_matching_certificate(X509_STORE *, struct client *);
0051
2021-12-29
op
static int apply_reverse_proxy(struct client *);
0052
2021-10-02
op
static int apply_fastcgi(struct client*);
0053
2021-02-09
op
static int apply_require_ca(struct client*);
0054
2021-02-08
op
static void open_dir(struct client*);
0055
2021-02-08
op
static void redirect_canonical_dir(struct client*);
0057
2021-10-02
op
static void client_tls_readcb(int, short, void *);
0058
2021-10-02
op
static void client_tls_writecb(int, short, void *);
0060
2021-10-02
op
static void client_read(struct bufferevent *, void *);
0061
2021-10-02
op
void client_write(struct bufferevent *, void *);
0062
2021-10-02
op
static void client_error(struct bufferevent *, short, void *);
0064
2021-10-02
op
static void client_close_ev(int, short, void *);
0066
2021-02-08
op
static void do_accept(int, short, void*);
0068
2021-10-02
op
static void handle_dispatch_imsg(int, short, void *);
0069
2021-03-19
op
static void handle_siginfo(int, short, void*);
0071
2021-10-07
op
static uint32_t server_client_id;
0073
2021-10-07
op
struct client_tree_id clients;
0075
2021-02-10
op
static inline int
0076
2021-02-10
op
matches(const char *pattern, const char *path)
0078
2021-02-10
op
if (*path == '/')
0080
2021-02-10
op
return !fnmatch(pattern, path, 0);
0083
2021-01-24
op
const char *
0084
2021-01-24
op
vhost_lang(struct vhost *v, const char *path)
0086
2021-01-24
op
struct location *loc;
0088
2021-01-27
op
if (v == NULL || path == NULL)
0089
2021-01-30
op
return NULL;
0091
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0092
2021-03-31
op
while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
0093
2022-10-05
op
if (*loc->lang != '\0') {
0094
2021-02-10
op
if (matches(loc->match, path))
0095
2021-01-30
op
return loc->lang;
0099
2022-10-05
op
loc = TAILQ_FIRST(&v->locations);
0100
2022-10-05
op
if (*loc->lang == '\0')
0101
2022-10-05
op
return NULL;
0102
2022-10-05
op
return loc->lang;
0105
2021-01-24
op
const char *
0106
2021-01-24
op
vhost_default_mime(struct vhost *v, const char *path)
0108
2021-01-24
op
struct location *loc;
0109
2021-01-24
op
const char *default_mime = "application/octet-stream";
0111
2021-01-27
op
if (v == NULL || path == NULL)
0112
2021-01-27
op
return default_mime;
0114
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0115
2021-03-31
op
while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
0116
2022-10-05
op
if (*loc->default_mime != '\0') {
0117
2021-02-10
op
if (matches(loc->match, path))
0118
2021-01-30
op
return loc->default_mime;
0122
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0123
2022-10-05
op
if (*loc->default_mime != '\0')
0124
2021-03-31
op
return loc->default_mime;
0125
2021-01-24
op
return default_mime;
0128
2021-01-24
op
const char *
0129
2021-01-24
op
vhost_index(struct vhost *v, const char *path)
0131
2021-01-24
op
struct location *loc;
0132
2021-01-24
op
const char *index = "index.gmi";
0134
2021-01-27
op
if (v == NULL || path == NULL)
0135
2021-01-27
op
return index;
0137
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0138
2021-03-31
op
while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
0139
2022-10-05
op
if (*loc->index != '\0') {
0140
2021-02-10
op
if (matches(loc->match, path))
0141
2021-01-30
op
return loc->index;
0145
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0146
2022-10-05
op
if (*loc->index != '\0')
0147
2021-03-31
op
return loc->index;
0148
2021-01-24
op
return index;
0152
2021-01-24
op
vhost_auto_index(struct vhost *v, const char *path)
0154
2021-01-24
op
struct location *loc;
0156
2021-01-30
op
if (v == NULL || path == NULL)
0157
2021-01-30
op
return 0;
0159
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0160
2021-03-31
op
while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
0161
2021-02-06
op
if (loc->auto_index != 0) {
0162
2021-02-10
op
if (matches(loc->match, path))
0163
2021-01-30
op
return loc->auto_index == 1;
0167
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0168
2021-03-31
op
return loc->auto_index == 1;
0172
2021-02-06
op
vhost_block_return(struct vhost *v, const char *path, int *code, const char **fmt)
0174
2021-02-06
op
struct location *loc;
0176
2021-02-06
op
if (v == NULL || path == NULL)
0177
2021-02-06
op
return 0;
0179
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0180
2021-03-31
op
while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
0181
2021-02-06
op
if (loc->block_code != 0) {
0182
2021-02-10
op
if (matches(loc->match, path)) {
0183
2021-02-06
op
*code = loc->block_code;
0184
2021-02-06
op
*fmt = loc->block_fmt;
0185
2021-02-06
op
return 1;
0190
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0191
2021-03-31
op
*code = loc->block_code;
0192
2021-03-31
op
*fmt = loc->block_fmt;
0193
2021-03-31
op
return loc->block_code != 0;
0197
2021-05-09
op
vhost_fastcgi(struct vhost *v, const char *path)
0199
2021-05-09
op
struct location *loc;
0201
2021-05-09
op
if (v == NULL || path == NULL)
0202
2021-05-09
op
return -1;
0204
2021-05-09
op
loc = TAILQ_FIRST(&v->locations);
0205
2021-05-09
op
while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
0206
2021-05-09
op
if (loc->fcgi != -1)
0207
2021-05-09
op
if (matches(loc->match, path))
0208
2021-05-09
op
return loc->fcgi;
0211
2021-05-09
op
loc = TAILQ_FIRST(&v->locations);
0212
2021-05-09
op
return loc->fcgi;
0216
2021-05-15
op
vhost_dirfd(struct vhost *v, const char *path, size_t *retloc)
0218
2021-04-30
op
struct location *loc;
0219
2021-05-15
op
size_t l = 0;
0221
2021-04-30
op
if (v == NULL || path == NULL)
0222
2021-04-30
op
return -1;
0224
2021-04-30
op
loc = TAILQ_FIRST(&v->locations);
0225
2021-04-30
op
while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
0227
2021-04-30
op
if (loc->dirfd != -1)
0228
2021-05-15
op
if (matches(loc->match, path)) {
0229
2021-05-15
op
*retloc = l;
0230
2021-04-30
op
return loc->dirfd;
0234
2021-05-15
op
*retloc = 0;
0235
2021-04-30
op
loc = TAILQ_FIRST(&v->locations);
0236
2021-04-30
op
return loc->dirfd;
0240
2021-02-06
op
vhost_strip(struct vhost *v, const char *path)
0242
2021-02-06
op
struct location *loc;
0244
2021-02-06
op
if (v == NULL || path == NULL)
0245
2021-02-06
op
return 0;
0247
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0248
2021-03-31
op
while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
0249
2021-02-06
op
if (loc->strip != 0) {
0250
2021-02-10
op
if (matches(loc->match, path))
0251
2021-02-06
op
return loc->strip;
0255
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0256
2021-03-31
op
return loc->strip;
0259
2021-02-09
op
X509_STORE *
0260
2021-02-09
op
vhost_require_ca(struct vhost *v, const char *path)
0262
2021-02-09
op
struct location *loc;
0264
2021-02-09
op
if (v == NULL || path == NULL)
0265
2021-02-09
op
return NULL;
0267
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0268
2021-03-31
op
while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
0269
2021-02-09
op
if (loc->reqca != NULL) {
0270
2021-02-10
op
if (matches(loc->match, path))
0271
2021-02-09
op
return loc->reqca;
0275
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0276
2021-03-31
op
return loc->reqca;
0280
2021-02-23
op
vhost_disable_log(struct vhost *v, const char *path)
0282
2021-02-23
op
struct location *loc;
0284
2021-02-23
op
if (v == NULL || path == NULL)
0285
2021-02-23
op
return 0;
0287
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0288
2021-03-31
op
while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
0289
2021-02-23
op
if (loc->disable_log && matches(loc->match, path))
0290
2021-02-23
op
return 1;
0293
2021-03-31
op
loc = TAILQ_FIRST(&v->locations);
0294
2021-03-31
op
return loc->disable_log;
0297
2021-02-02
op
static int
0298
2021-01-17
op
check_path(struct client *c, const char *path, int *fd)
0300
2021-01-17
op
struct stat sb;
0301
2021-01-21
op
const char *p;
0302
2021-07-27
op
int dirfd, strip;
0304
2021-01-17
op
assert(path != NULL);
0307
2021-04-30
op
* in send_dir we add an initial / (to be redirect-friendly),
0308
2021-04-30
op
* but here we want to skip it
0310
2021-04-30
op
if (*path == '/')
0313
2021-04-30
op
strip = vhost_strip(c->host, path);
0314
2021-04-30
op
p = strip_path(path, strip);
0316
2021-04-30
op
if (*p == '/')
0317
2021-04-30
op
p = p+1;
0318
2021-04-30
op
if (*p == '\0')
0319
2021-01-21
op
p = ".";
0321
2021-05-15
op
dirfd = vhost_dirfd(c->host, path, &c->loc);
0322
2021-04-30
op
log_debug(c, "check_path: strip=%d path=%s original=%s",
0323
2021-04-30
op
strip, p, path);
0324
2022-07-04
op
if (*fd == -1 && (*fd = openat(dirfd, p, O_RDONLY)) == -1) {
0325
2022-07-04
op
if (errno == EACCES)
0326
2022-07-04
op
log_info(c, "can't open %s: %s", p, strerror(errno));
0327
2021-01-17
op
return FILE_MISSING;
0330
2021-01-17
op
if (fstat(*fd, &sb) == -1) {
0331
2021-02-07
op
log_notice(c, "failed stat for %s: %s", path, strerror(errno));
0332
2021-01-17
op
return FILE_MISSING;
0335
2021-01-17
op
if (S_ISDIR(sb.st_mode))
0336
2021-01-17
op
return FILE_DIRECTORY;
0338
2021-01-17
op
return FILE_EXISTS;
0341
2021-02-02
op
static void
0342
2021-02-08
op
open_file(struct client *c)
0344
2021-02-08
op
switch (check_path(c, c->iri.path, &c->pfd)) {
0345
2021-01-17
op
case FILE_EXISTS:
0346
2021-10-02
op
c->type = REQUEST_FILE;
0347
2021-02-12
op
start_reply(c, SUCCESS, mime(c->host, c->iri.path));
0350
2021-01-17
op
case FILE_DIRECTORY:
0351
2021-02-08
op
open_dir(c);
0354
2021-01-17
op
case FILE_MISSING:
0355
2021-02-08
op
start_reply(c, NOT_FOUND, "not found");
0358
2021-01-17
op
default:
0359
2021-01-17
op
/* unreachable */
0360
2021-01-17
op
abort();
0365
2021-02-03
op
mark_nonblock(int fd)
0367
2021-02-03
op
int flags;
0369
2021-02-03
op
if ((flags = fcntl(fd, F_GETFL)) == -1)
0370
2021-02-03
op
fatal("fcntl(F_GETFL): %s", strerror(errno));
0371
2021-02-03
op
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
0372
2021-02-03
op
fatal("fcntl(F_SETFL): %s", strerror(errno));
0375
2021-02-02
op
static void
0376
2021-02-08
op
handle_handshake(int fd, short ev, void *d)
0378
2021-02-08
op
struct client *c = d;
0379
2021-01-17
op
struct vhost *h;
0380
2021-04-29
op
struct alist *a;
0381
2021-01-17
op
const char *servname;
0382
2021-01-29
op
const char *parse_err = "unknown error";
0384
2021-01-17
op
switch (tls_handshake(c->ctx)) {
0385
2021-01-17
op
case 0: /* success */
0386
2021-01-17
op
case -1: /* already handshaked */
0388
2021-01-17
op
case TLS_WANT_POLLIN:
0389
2021-10-02
op
event_once(c->fd, EV_READ, handle_handshake, c, NULL);
0391
2021-01-17
op
case TLS_WANT_POLLOUT:
0392
2021-10-02
op
event_once(c->fd, EV_WRITE, handle_handshake, c, NULL);
0394
2021-01-17
op
default:
0395
2021-01-17
op
/* unreachable */
0396
2021-01-17
op
abort();
0399
2021-10-15
op
c->bev = bufferevent_new(fd, client_read, client_write,
0400
2021-10-15
op
client_error, c);
0401
2021-10-15
op
if (c->bev == NULL)
0402
2021-10-15
op
fatal("%s: failed to allocate client buffer: %s",
0403
2021-10-15
op
__func__, strerror(errno));
0405
2021-10-15
op
event_set(&c->bev->ev_read, c->fd, EV_READ,
0406
2021-10-15
op
client_tls_readcb, c->bev);
0407
2021-10-15
op
event_set(&c->bev->ev_write, c->fd, EV_WRITE,
0408
2021-10-15
op
client_tls_writecb, c->bev);
0410
2021-10-15
op
#if HAVE_LIBEVENT2
0411
2021-10-15
op
evbuffer_unfreeze(c->bev->input, 0);
0412
2021-10-15
op
evbuffer_unfreeze(c->bev->output, 1);
0415
2021-09-24
op
if ((servname = tls_conn_servername(c->ctx)) == NULL) {
0416
2021-09-24
op
log_debug(c, "handshake: missing SNI");
0417
2021-09-24
op
goto err;
0420
2021-01-29
op
if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
0421
2021-02-07
op
log_info(c, "puny_decode: %s", parse_err);
0422
2021-01-29
op
goto err;
0425
2021-03-31
op
TAILQ_FOREACH(h, &hosts, vhosts) {
0426
2021-02-12
op
if (matches(h->domain, c->domain))
0427
2021-04-29
op
goto found;
0428
2021-04-29
op
TAILQ_FOREACH(a, &h->aliases, aliases) {
0429
2021-04-29
op
if (matches(a->alias, c->domain))
0430
2021-04-29
op
goto found;
0435
2021-02-07
op
log_debug(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
0436
2021-02-07
op
servname != NULL ? servname : "(null)",
0437
2021-02-07
op
c->domain,
0438
2021-03-31
op
h != NULL ? h->domain : "(null)");
0440
2021-03-31
op
if (h != NULL) {
0441
2021-01-17
op
c->host = h;
0442
2021-10-02
op
bufferevent_enable(c->bev, EV_READ);
0447
2021-02-08
op
start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
0450
2021-05-15
op
static const char *
0451
2021-05-15
op
strip_path(const char *path, int strip)
0453
2021-02-08
op
char *t;
0455
2021-02-06
op
while (strip > 0) {
0456
2021-02-06
op
if ((t = strchr(path, '/')) == NULL) {
0457
2021-02-06
op
path = strchr(path, '\0');
0460
2021-02-06
op
path = t;
0461
2021-02-06
op
strip--;
0464
2021-02-08
op
return path;
0467
2021-02-08
op
static void
0468
2021-02-08
op
fmt_sbuf(const char *fmt, struct client *c, const char *path)
0470
2021-02-08
op
size_t i;
0471
2021-10-18
op
char buf[32];
0473
2021-02-06
op
memset(buf, 0, sizeof(buf));
0474
2021-02-06
op
for (i = 0; *fmt; ++fmt) {
0475
2021-02-06
op
if (i == sizeof(buf)-1 || *fmt == '%') {
0476
2021-02-06
op
strlcat(c->sbuf, buf, sizeof(c->sbuf));
0477
2021-02-06
op
memset(buf, 0, sizeof(buf));
0481
2021-02-06
op
if (*fmt != '%') {
0482
2021-02-06
op
buf[i++] = *fmt;
0483
2021-02-06
op
continue;
0486
2021-02-06
op
switch (*++fmt) {
0487
2021-02-06
op
case '%':
0488
2021-02-06
op
strlcat(c->sbuf, "%", sizeof(c->sbuf));
0490
2021-02-06
op
case 'p':
0491
2021-04-30
op
if (*path != '/')
0492
2021-04-30
op
strlcat(c->sbuf, "/", sizeof(c->sbuf));
0493
2021-02-06
op
strlcat(c->sbuf, path, sizeof(c->sbuf));
0495
2021-02-06
op
case 'q':
0496
2021-02-06
op
strlcat(c->sbuf, c->iri.query, sizeof(c->sbuf));
0498
2021-02-06
op
case 'P':
0499
2021-02-06
op
snprintf(buf, sizeof(buf), "%d", conf.port);
0500
2021-02-06
op
strlcat(c->sbuf, buf, sizeof(c->sbuf));
0501
2021-02-06
op
memset(buf, 0, sizeof(buf));
0503
2021-02-06
op
case 'N':
0504
2021-02-06
op
strlcat(c->sbuf, c->domain, sizeof(c->sbuf));
0506
2021-02-06
op
default:
0507
2021-02-06
op
fatal("%s: unknown fmt specifier %c",
0508
2021-02-06
op
__func__, *fmt);
0512
2021-02-06
op
if (i != 0)
0513
2021-02-06
op
strlcat(c->sbuf, buf, sizeof(c->sbuf));
0516
2021-02-08
op
/* 1 if a matching `block return' (and apply it), 0 otherwise */
0517
2021-02-08
op
static int
0518
2021-02-08
op
apply_block_return(struct client *c)
0520
2021-02-08
op
const char *fmt, *path;
0521
2021-02-08
op
int code;
0523
2021-02-08
op
if (!vhost_block_return(c->host, c->iri.path, &code, &fmt))
0524
2021-02-08
op
return 0;
0526
2021-02-08
op
path = strip_path(c->iri.path, vhost_strip(c->host, c->iri.path));
0527
2021-02-08
op
fmt_sbuf(fmt, c, path);
0529
2021-02-08
op
start_reply(c, code, c->sbuf);
0530
2021-12-29
op
return 1;
0533
2021-01-02
op
static struct proxy *
0534
2021-01-02
op
matched_proxy(struct client *c)
0536
2021-01-02
op
struct proxy *p;
0537
2021-01-02
op
const char *proto;
0538
2021-01-02
op
const char *host;
0539
2021-01-02
op
const char *port;
0541
2021-01-02
op
TAILQ_FOREACH(p, &c->host->proxies, proxies) {
0542
2022-10-05
op
if (*(proto = p->match_proto) == '\0')
0543
2021-01-02
op
proto = "gemini";
0544
2022-10-05
op
if (*(host = p->match_host) == '\0')
0545
2021-01-02
op
host = "*";
0546
2022-10-05
op
if (*(port = p->match_port) == '\0')
0547
2021-01-02
op
port = "*";
0549
2021-01-02
op
if (matches(proto, c->iri.schema) &&
0550
2021-01-02
op
matches(host, c->domain) &&
0551
2021-01-02
op
matches(port, c->iri.port))
0552
2021-01-02
op
return p;
0555
2021-01-02
op
return NULL;
0558
2022-01-04
op
static int
0559
2022-01-04
op
check_matching_certificate(X509_STORE *store, struct client *c)
0561
2022-01-04
op
const uint8_t *cert;
0562
2022-01-04
op
size_t len;
0564
2022-01-04
op
if (!tls_peer_cert_provided(c->ctx)) {
0565
2022-01-04
op
start_reply(c, CLIENT_CERT_REQ, "client certificate required");
0566
2022-01-04
op
return 1;
0569
2022-01-04
op
cert = tls_peer_cert_chain_pem(c->ctx, &len);
0570
2022-01-04
op
if (!validate_against_ca(store, cert, len)) {
0571
2022-01-04
op
start_reply(c, CERT_NOT_AUTH, "certificate not authorised");
0572
2022-01-04
op
return 1;
0575
2022-01-04
op
return 0;
0578
2022-09-06
op
static int
0579
2022-09-06
op
proxy_socket(struct client *c, const char *host, const char *port)
0581
2022-09-06
op
struct addrinfo hints, *res, *res0;
0582
2022-09-06
op
int r, sock;
0584
2022-09-06
op
memset(&hints, 0, sizeof(hints));
0585
2022-09-06
op
hints.ai_family = AF_UNSPEC;
0586
2022-09-06
op
hints.ai_socktype = SOCK_STREAM;
0588
2022-09-06
op
/* XXX: asr_run? :> */
0589
2022-09-06
op
r = getaddrinfo(host, port, &hints, &res0);
0590
2022-09-06
op
if (r != 0) {
0591
2022-09-06
op
log_warn(c, "getaddrinfo(\"%s\", \"%s\"): %s",
0592
2022-09-06
op
host, port, gai_strerror(r));
0593
2022-09-06
op
return -1;
0596
2022-09-06
op
for (res = res0; res; res = res->ai_next) {
0597
2022-09-06
op
sock = socket(res->ai_family, res->ai_socktype,
0598
2022-09-06
op
res->ai_protocol);
0599
2022-09-06
op
if (sock == -1)
0600
2022-09-06
op
continue;
0602
2022-09-06
op
if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
0603
2022-09-06
op
close(sock);
0604
2022-09-06
op
sock = -1;
0605
2022-09-06
op
continue;
0611
2022-09-06
op
freeaddrinfo(res0);
0613
2022-09-06
op
if (sock == -1)
0614
2022-09-06
op
log_warn(c, "can't connect to %s:%s", host, port);
0616
2022-09-06
op
return sock;
0619
2021-12-29
op
/* 1 if matching a proxy relay-to (and apply it), 0 otherwise */
0620
2021-12-29
op
static int
0621
2021-12-29
op
apply_reverse_proxy(struct client *c)
0623
2021-01-01
op
struct proxy *p;
0625
2021-01-02
op
if ((p = matched_proxy(c)) == NULL)
0626
2021-12-29
op
return 0;
0628
2021-01-02
op
c->proxy = p;
0630
2022-01-04
op
if (p->reqca != NULL && check_matching_certificate(p->reqca, c))
0631
2022-01-04
op
return 1;
0633
2021-12-29
op
log_debug(c, "opening proxy connection for %s:%s",
0634
2021-01-01
op
p->host, p->port);
0636
2022-09-06
op
if ((c->pfd = proxy_socket(c, p->host, p->port)) == -1) {
0637
2022-09-06
op
start_reply(c, PROXY_ERROR, "proxy error");
0638
2022-09-06
op
return 1;
0641
2022-09-06
op
mark_nonblock(c->pfd);
0642
2022-09-06
op
if (proxy_init(c) == -1)
0643
2022-09-06
op
start_reply(c, PROXY_ERROR, "proxy error");
0645
2021-05-09
op
return 1;
0648
2022-09-06
op
static int
0649
2022-09-06
op
fcgi_open_sock(struct fcgi *f)
0651
2022-09-06
op
struct sockaddr_un addr;
0654
2022-09-06
op
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
0655
2022-09-06
op
log_err(NULL, "socket: %s", strerror(errno));
0656
2022-09-06
op
return -1;
0659
2022-09-06
op
memset(&addr, 0, sizeof(addr));
0660
2022-09-06
op
addr.sun_family = AF_UNIX;
0661
2022-09-06
op
strlcpy(addr.sun_path, f->path, sizeof(addr.sun_path));
0663
2022-09-06
op
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
0664
2022-09-06
op
log_warn(NULL, "failed to connect to %s: %s", f->path,
0665
2022-09-06
op
strerror(errno));
0666
2022-09-06
op
close(fd);
0667
2022-09-06
op
return -1;
0670
2022-09-06
op
return fd;
0673
2022-09-06
op
static int
0674
2022-09-06
op
fcgi_open_conn(struct fcgi *f)
0676
2022-09-06
op
struct addrinfo hints, *servinfo, *p;
0677
2022-09-06
op
int r, sock;
0679
2022-09-06
op
memset(&hints, 0, sizeof(hints));
0680
2022-09-06
op
hints.ai_family = AF_UNSPEC;
0681
2022-09-06
op
hints.ai_socktype = SOCK_STREAM;
0682
2022-09-06
op
hints.ai_flags = AI_ADDRCONFIG;
0684
2022-09-06
op
if ((r = getaddrinfo(f->path, f->port, &hints, &servinfo)) != 0) {
0685
2022-09-06
op
log_warn(NULL, "getaddrinfo %s:%s: %s", f->path, f->port,
0686
2022-09-06
op
gai_strerror(r));
0687
2022-09-06
op
return -1;
0690
2022-09-06
op
for (p = servinfo; p != NULL; p = p->ai_next) {
0691
2022-09-06
op
sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
0692
2022-09-06
op
if (sock == -1)
0693
2022-09-06
op
continue;
0694
2022-09-06
op
if (connect(sock, p->ai_addr, p->ai_addrlen) == -1) {
0695
2022-09-06
op
close(sock);
0696
2022-09-06
op
continue;
0701
2022-09-06
op
if (p == NULL) {
0702
2022-09-06
op
log_warn(NULL, "couldn't connect to %s:%s", f->path, f->port);
0703
2022-09-06
op
sock = -1;
0706
2022-09-06
op
freeaddrinfo(servinfo);
0707
2022-09-06
op
return sock;
0710
2021-05-09
op
/* 1 if matching `fcgi' (and apply it), 0 otherwise */
0711
2021-05-09
op
static int
0712
2021-05-09
op
apply_fastcgi(struct client *c)
0715
2021-05-09
op
struct fcgi *f;
0717
2021-05-09
op
if ((id = vhost_fastcgi(c->host, c->iri.path)) == -1)
0718
2021-05-09
op
return 0;
0720
2021-10-07
op
f = &fcgi[id];
0722
2022-09-06
op
log_debug(c, "opening fastcgi connection for (%s,%s)",
0723
2022-09-06
op
f->path, f->port);
0725
2022-10-05
op
if (*f->port != '\0')
0726
2022-09-06
op
c->pfd = fcgi_open_sock(f);
0728
2022-09-06
op
c->pfd = fcgi_open_conn(f);
0730
2022-09-06
op
if (c->pfd == -1) {
0731
2022-09-06
op
start_reply(c, CGI_ERROR, "CGI error");
0732
2022-09-06
op
return 1;
0735
2022-09-06
op
mark_nonblock(c->pfd);
0737
2022-09-06
op
c->cgibev = bufferevent_new(c->pfd, fcgi_read, fcgi_write,
0738
2022-09-06
op
fcgi_error, c);
0739
2022-09-06
op
if (c->cgibev == NULL) {
0740
2022-09-06
op
start_reply(c, TEMP_FAILURE, "internal server error");
0741
2022-09-06
op
return 1;
0744
2022-09-06
op
bufferevent_enable(c->cgibev, EV_READ|EV_WRITE);
0745
2022-09-06
op
fcgi_req(c);
0747
2021-02-06
op
return 1;
0750
2021-02-09
op
/* 1 if matching `require client ca' fails (and apply it), 0 otherwise */
0751
2021-02-09
op
static int
0752
2021-02-09
op
apply_require_ca(struct client *c)
0754
2021-02-09
op
X509_STORE *store;
0756
2021-02-09
op
if ((store = vhost_require_ca(c->host, c->iri.path)) == NULL)
0757
2021-02-09
op
return 0;
0758
2022-01-04
op
return check_matching_certificate(store, c);
0761
2021-02-02
op
static void
0762
2021-02-08
op
open_dir(struct client *c)
0764
2021-01-17
op
size_t len;
0765
2021-04-25
op
int dirfd, root;
0766
2021-01-24
op
char *before_file;
0768
2022-09-06
op
log_debug(c, "in open_dir");
0770
2021-04-25
op
root = !strcmp(c->iri.path, "/") || *c->iri.path == '\0';
0772
2021-01-20
op
len = strlen(c->iri.path);
0773
2021-01-24
op
if (len > 0 && !ends_with(c->iri.path, "/")) {
0774
2021-02-08
op
redirect_canonical_dir(c);
0778
2021-01-24
op
strlcpy(c->sbuf, "/", sizeof(c->sbuf));
0779
2021-01-21
op
strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
0780
2021-01-21
op
if (!ends_with(c->sbuf, "/"))
0781
2021-01-20
op
strlcat(c->sbuf, "/", sizeof(c->sbuf));
0782
2021-01-24
op
before_file = strchr(c->sbuf, '\0');
0783
2021-01-24
op
len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
0784
2021-01-24
op
sizeof(c->sbuf));
0785
2021-01-24
op
if (len >= sizeof(c->sbuf)) {
0786
2021-02-08
op
start_reply(c, TEMP_FAILURE, "internal server error");
0790
2021-01-24
op
c->iri.path = c->sbuf;
0792
2021-01-24
op
/* close later unless we have to generate the dir listing */
0793
2021-02-08
op
dirfd = c->pfd;
0794
2021-02-08
op
c->pfd = -1;
0796
2021-02-08
op
switch (check_path(c, c->iri.path, &c->pfd)) {
0797
2021-01-24
op
case FILE_EXISTS:
0798
2021-10-02
op
c->type = REQUEST_FILE;
0799
2021-02-12
op
start_reply(c, SUCCESS, mime(c->host, c->iri.path));
0802
2021-01-24
op
case FILE_DIRECTORY:
0803
2021-02-08
op
start_reply(c, TEMP_REDIRECT, c->sbuf);
0806
2021-01-24
op
case FILE_MISSING:
0807
2021-01-24
op
*before_file = '\0';
0809
2021-01-24
op
if (!vhost_auto_index(c->host, c->iri.path)) {
0810
2021-02-08
op
start_reply(c, NOT_FOUND, "not found");
0814
2021-10-02
op
c->type = REQUEST_DIR;
0816
2021-04-25
op
c->dirlen = scandir_fd(dirfd, &c->dir,
0817
2021-04-25
op
root ? select_non_dotdot : select_non_dot,
0818
2021-04-25
op
alphasort);
0819
2021-04-25
op
if (c->dirlen == -1) {
0820
2021-04-25
op
log_err(c, "scandir_fd(%d) (vhost:%s) %s: %s",
0821
2021-02-08
op
c->pfd, c->host->domain, c->iri.path, strerror(errno));
0822
2021-02-08
op
start_reply(c, TEMP_FAILURE, "internal server error");
0825
2021-04-25
op
c->diroff = 0;
0826
2021-01-24
op
c->off = 0;
0828
2021-10-18
op
start_reply(c, SUCCESS, "text/gemini");
0829
2021-10-02
op
evbuffer_add_printf(EVBUFFER_OUTPUT(c->bev),
0830
2021-10-02
op
"# Index of %s\n\n", c->iri.path);
0833
2021-01-24
op
default:
0834
2021-01-24
op
/* unreachable */
0835
2021-01-24
op
abort();
0838
2021-01-24
op
close(dirfd);
0841
2021-02-02
op
static void
0842
2021-02-08
op
redirect_canonical_dir(struct client *c)
0844
2021-01-24
op
size_t len;
0846
2021-01-24
op
strlcpy(c->sbuf, "/", sizeof(c->sbuf));
0847
2021-01-24
op
strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
0848
2021-01-24
op
len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
0850
2021-01-20
op
if (len >= sizeof(c->sbuf)) {
0851
2021-02-08
op
start_reply(c, TEMP_FAILURE, "internal server error");
0855
2021-02-08
op
start_reply(c, TEMP_REDIRECT, c->sbuf);
0858
2021-02-02
op
static void
0859
2021-10-02
op
client_tls_readcb(int fd, short event, void *d)
0861
2021-10-02
op
struct bufferevent *bufev = d;
0862
2021-10-02
op
struct client *client = bufev->cbarg;
0863
2021-10-02
op
ssize_t ret;
0864
2021-10-02
op
size_t len;
0865
2021-10-02
op
int what = EVBUFFER_READ;
0866
2021-10-02
op
int howmuch = IBUF_READ_SIZE;
0867
2021-10-02
op
char buf[IBUF_READ_SIZE];
0869
2021-10-02
op
if (event == EV_TIMEOUT) {
0870
2021-10-02
op
what |= EVBUFFER_TIMEOUT;
0871
2021-10-02
op
goto err;
0874
2021-10-02
op
if (bufev->wm_read.high != 0)
0875
2021-10-02
op
howmuch = MIN(sizeof(buf), bufev->wm_read.high);
0877
2021-10-02
op
switch (ret = tls_read(client->ctx, buf, howmuch)) {
0878
2021-10-02
op
case TLS_WANT_POLLIN:
0879
2021-10-02
op
case TLS_WANT_POLLOUT:
0880
2021-10-02
op
goto retry;
0881
2021-10-02
op
case -1:
0882
2021-10-02
op
what |= EVBUFFER_ERROR;
0883
2021-10-02
op
goto err;
0885
2021-10-02
op
len = ret;
0887
2021-10-02
op
if (len == 0) {
0888
2021-10-02
op
what |= EVBUFFER_EOF;
0889
2021-10-02
op
goto err;
0892
2021-10-02
op
if (evbuffer_add(bufev->input, buf, len) == -1) {
0893
2021-10-02
op
what |= EVBUFFER_ERROR;
0894
2021-10-02
op
goto err;
0897
2021-10-02
op
event_add(&bufev->ev_read, NULL);
0898
2021-10-02
op
if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
0900
2021-10-02
op
if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
0902
2021-10-02
op
* here we could implement a read pressure policy.
0906
2021-10-02
op
if (bufev->readcb != NULL)
0907
2021-10-02
op
(*bufev->readcb)(bufev, bufev->cbarg);
0912
2021-10-02
op
event_add(&bufev->ev_read, NULL);
0916
2021-10-02
op
(*bufev->errorcb)(bufev, what, bufev->cbarg);
0919
2021-02-02
op
static void
0920
2021-10-02
op
client_tls_writecb(int fd, short event, void *d)
0922
2021-10-02
op
struct bufferevent *bufev = d;
0923
2021-10-02
op
struct client *client = bufev->cbarg;
0924
2021-10-02
op
ssize_t ret;
0925
2021-10-02
op
size_t len;
0926
2021-10-02
op
short what = EVBUFFER_WRITE;
0928
2021-10-02
op
if (event == EV_TIMEOUT) {
0929
2021-10-02
op
what |= EVBUFFER_TIMEOUT;
0930
2021-10-02
op
goto err;
0933
2021-10-02
op
if (EVBUFFER_LENGTH(bufev->output) != 0) {
0934
2021-10-02
op
ret = tls_write(client->ctx,
0935
2021-10-02
op
EVBUFFER_DATA(bufev->output),
0936
2021-10-02
op
EVBUFFER_LENGTH(bufev->output));
0937
2021-10-02
op
switch (ret) {
0938
2021-10-02
op
case TLS_WANT_POLLIN:
0939
2021-10-02
op
case TLS_WANT_POLLOUT:
0940
2021-10-02
op
goto retry;
0941
2021-10-02
op
case -1:
0942
2021-10-02
op
what |= EVBUFFER_ERROR;
0943
2021-10-02
op
goto err;
0945
2021-10-02
op
len = ret;
0946
2021-10-02
op
evbuffer_drain(bufev->output, len);
0949
2021-10-02
op
if (EVBUFFER_LENGTH(bufev->output) != 0)
0950
2021-10-02
op
event_add(&bufev->ev_write, NULL);
0952
2021-10-02
op
if (bufev->writecb != NULL &&
0953
2021-10-02
op
EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
0954
2021-10-02
op
(*bufev->writecb)(bufev, bufev->cbarg);
0958
2021-10-02
op
event_add(&bufev->ev_write, NULL);
0961
2021-10-02
op
log_err(client, "tls error: %s", tls_error(client->ctx));
0962
2021-10-02
op
(*bufev->errorcb)(bufev, what, bufev->cbarg);
0965
2021-02-02
op
static void
0966
2021-10-02
op
client_read(struct bufferevent *bev, void *d)
0968
2021-10-02
op
struct client *c = d;
0969
2021-10-02
op
struct evbuffer *src = EVBUFFER_INPUT(bev);
0970
2021-10-02
op
const char *parse_err = "invalid request";
0971
2021-10-02
op
char decoded[DOMAIN_NAME_LEN];
0972
2021-10-02
op
size_t len;
0974
2021-10-02
op
bufferevent_disable(bev, EVBUFFER_READ);
0977
2022-01-05
op
* libevent2 can still somehow call this function, even
0978
2022-01-05
op
* though I never enable EV_READ in the bufferevent. If
0979
2022-01-05
op
* that's the case, bail out.
0981
2022-01-05
op
if (c->type != REQUEST_UNDECIDED)
0984
2021-10-02
op
/* max url len + \r\n */
0985
2021-10-02
op
if (EVBUFFER_LENGTH(src) > 1024 + 2) {
0986
2021-10-02
op
log_err(c, "too much data received");
0987
2021-10-02
op
start_reply(c, BAD_REQUEST, "bad request");
0991
2021-10-02
op
c->req = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
0992
2021-10-02
op
if (c->req == NULL) {
0993
2021-10-02
op
/* not enough data yet. */
0994
2021-10-02
op
bufferevent_enable(bev, EVBUFFER_READ);
0997
2022-03-27
op
c->reqlen = strlen(c->req);
0998
2022-03-27
op
if (c->reqlen > 1024+2) {
0999
2022-03-27
op
log_err(c, "URL too long");
1000
2022-03-27
op
start_reply(c, BAD_REQUEST, "bad request");
1004
2021-10-02
op
if (!parse_iri(c->req, &c->iri, &parse_err) ||
1005
2021-10-02
op
!puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
1006
2021-10-02
op
log_err(c, "IRI parse error: %s", parse_err);
1007
2021-10-06
op
start_reply(c, BAD_REQUEST, "bad request");
1011
2021-12-29
op
if (apply_reverse_proxy(c))
1014
2021-12-09
op
/* ignore the port number */
1015
2021-12-09
op
if (strcmp(c->iri.schema, "gemini") ||
1016
2021-10-02
op
strcmp(decoded, c->domain)) {
1017
2021-10-02
op
start_reply(c, PROXY_REFUSED, "won't proxy request");
1021
2021-10-02
op
if (apply_require_ca(c) ||
1022
2021-10-02
op
apply_block_return(c)||
1023
2021-10-02
op
apply_fastcgi(c))
1026
2021-10-02
op
open_file(c);
1030
2021-10-02
op
client_write(struct bufferevent *bev, void *d)
1032
2021-10-02
op
struct client *c = d;
1033
2021-10-02
op
struct evbuffer *out = EVBUFFER_OUTPUT(bev);
1034
2022-07-04
op
char nam[PATH_MAX];
1035
2021-10-02
op
char buf[BUFSIZ];
1036
2021-10-02
op
ssize_t r;
1038
2021-10-02
op
switch (c->type) {
1039
2021-10-02
op
case REQUEST_UNDECIDED:
1041
2021-10-02
op
* Ignore spurious calls when we still don't have idea
1042
2021-10-02
op
* what to do with the request.
1046
2021-10-02
op
case REQUEST_FILE:
1047
2021-10-02
op
if ((r = read(c->pfd, buf, sizeof(buf))) == -1) {
1048
2021-10-02
op
log_warn(c, "read: %s", strerror(errno));
1049
2021-10-02
op
client_error(bev, EVBUFFER_ERROR, c);
1051
2021-10-02
op
} else if (r == 0) {
1052
2021-10-02
op
client_close(c);
1054
2021-10-02
op
} else if (r != sizeof(buf))
1055
2021-10-02
op
c->type = REQUEST_DONE;
1056
2021-10-02
op
bufferevent_write(bev, buf, r);
1059
2021-10-02
op
case REQUEST_DIR:
1060
2021-10-02
op
/* TODO: handle big big directories better */
1061
2021-10-02
op
for (c->diroff = 0; c->diroff < c->dirlen; ++c->diroff) {
1062
2022-07-04
op
const char *sufx = "";
1064
2022-07-04
op
encode_path(nam, sizeof(nam),
1065
2021-10-02
op
c->dir[c->diroff]->d_name);
1066
2022-07-04
op
if (c->dir[c->diroff]->d_type == DT_DIR)
1067
2022-07-04
op
sufx = "/";
1068
2022-07-04
op
evbuffer_add_printf(out, "=> ./%s%s\n", nam, sufx);
1069
2021-10-02
op
free(c->dir[c->diroff]);
1071
2021-10-02
op
free(c->dir);
1072
2021-10-02
op
c->dir = NULL;
1074
2021-10-02
op
c->type = REQUEST_DONE;
1076
2021-10-02
op
event_add(&c->bev->ev_write, NULL);
1079
2021-10-02
op
case REQUEST_FCGI:
1080
2021-12-29
op
case REQUEST_PROXY:
1082
2022-09-06
op
* Here we depend on fastcgi or proxy connection to
1083
2022-09-06
op
* provide data.
1087
2021-10-02
op
case REQUEST_DONE:
1088
2021-10-02
op
if (EVBUFFER_LENGTH(out) == 0)
1089
2021-10-02
op
client_close(c);
1094
2021-10-02
op
static void
1095
2021-10-02
op
client_error(struct bufferevent *bev, short error, void *d)
1097
2021-10-02
op
struct client *c = d;
1099
2021-10-02
op
c->type = REQUEST_DONE;
1101
2021-10-02
op
if (error & EVBUFFER_TIMEOUT) {
1102
2021-10-02
op
log_warn(c, "timeout reached, "
1103
2021-10-02
op
"forcefully closing the connection");
1104
2021-10-02
op
if (c->code == 0)
1105
2021-10-02
op
start_reply(c, BAD_REQUEST, "timeout");
1107
2021-10-02
op
client_close(c);
1111
2021-10-02
op
if (error & EVBUFFER_EOF) {
1112
2021-10-02
op
client_close(c);
1116
2022-02-19
op
log_err(c, "unknown bufferevent error %x", error);
1117
2021-10-02
op
client_close(c);
1121
2021-10-02
op
start_reply(struct client *c, int code, const char *meta)
1123
2021-10-02
op
struct evbuffer *evb = EVBUFFER_OUTPUT(c->bev);
1124
2021-10-02
op
const char *lang;
1125
2021-10-02
op
int r, rr;
1127
2021-10-02
op
bufferevent_enable(c->bev, EVBUFFER_WRITE);
1129
2021-10-02
op
c->code = code;
1130
2021-10-02
op
c->meta = meta;
1132
2021-10-02
op
r = evbuffer_add_printf(evb, "%d %s", code, meta);
1133
2021-10-02
op
if (r == -1)
1134
2021-10-02
op
goto err;
1136
2021-10-02
op
/* 2 digit status + space + 1024 max reply */
1137
2021-10-02
op
if (r > 1027)
1138
2021-10-02
op
goto overflow;
1140
2022-09-06
op
if (c->type != REQUEST_FCGI &&
1141
2021-12-29
op
c->type != REQUEST_PROXY &&
1142
2021-10-02
op
!strcmp(meta, "text/gemini") &&
1143
2021-10-02
op
(lang = vhost_lang(c->host, c->iri.path)) != NULL) {
1144
2021-10-18
op
rr = evbuffer_add_printf(evb, ";lang=%s", lang);
1145
2021-10-02
op
if (rr == -1)
1146
2021-10-02
op
goto err;
1147
2021-10-02
op
if (r + rr > 1027)
1148
2021-10-02
op
goto overflow;
1151
2021-10-02
op
bufferevent_write(c->bev, "\r\n", 2);
1153
2021-10-02
op
if (!vhost_disable_log(c->host, c->iri.path))
1154
2021-10-02
op
log_request(c, EVBUFFER_DATA(evb), EVBUFFER_LENGTH(evb));
1156
2022-01-27
op
if (code != 20)
1157
2021-10-02
op
c->type = REQUEST_DONE;
1162
2021-10-02
op
log_err(c, "evbuffer_add_printf error: no memory");
1163
2021-10-02
op
evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
1164
2021-10-02
op
client_close(c);
1167
2021-10-02
op
overflow:
1168
2021-10-02
op
log_warn(c, "reply header overflow");
1169
2021-10-02
op
evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
1170
2021-10-02
op
start_reply(c, TEMP_FAILURE, "internal error");
1173
2021-10-02
op
static void
1174
2021-10-02
op
client_close_ev(int fd, short event, void *d)
1176
2021-05-09
op
struct client *c = d;
1178
2021-01-17
op
switch (tls_close(c->ctx)) {
1179
2021-01-17
op
case TLS_WANT_POLLIN:
1180
2021-10-02
op
event_once(c->fd, EV_READ, client_close_ev, c, NULL);
1182
2021-01-17
op
case TLS_WANT_POLLOUT:
1183
2021-10-02
op
event_once(c->fd, EV_WRITE, client_close_ev, c, NULL);
1187
2021-01-17
op
connected_clients--;
1189
2021-10-06
op
free(c->req);
1191
2021-01-17
op
tls_free(c->ctx);
1192
2021-01-17
op
c->ctx = NULL;
1194
2021-10-02
op
free(c->header);
1196
2021-02-08
op
if (c->pfd != -1)
1197
2021-02-08
op
close(c->pfd);
1199
2021-01-24
op
if (c->dir != NULL)
1200
2021-04-25
op
free(c->dir);
1202
2021-02-08
op
close(c->fd);
1203
2021-02-08
op
c->fd = -1;
1206
2021-12-29
op
static void
1207
2021-12-29
op
client_proxy_close(int fd, short event, void *d)
1209
2021-12-29
op
struct tls *ctx = d;
1211
2021-12-29
op
if (ctx == NULL) {
1212
2021-12-29
op
close(fd);
1216
2021-12-29
op
switch (tls_close(ctx)) {
1217
2021-12-29
op
case TLS_WANT_POLLIN:
1218
2021-12-29
op
event_once(fd, EV_READ, client_proxy_close, d, NULL);
1220
2021-12-29
op
case TLS_WANT_POLLOUT:
1221
2021-12-29
op
event_once(fd, EV_WRITE, client_proxy_close, d, NULL);
1225
2021-12-29
op
tls_free(ctx);
1226
2021-12-29
op
close(fd);
1230
2021-10-02
op
client_close(struct client *c)
1233
2021-10-02
op
* We may end up calling client_close in various situations
1234
2021-10-02
op
* and for the most unexpected reasons. Therefore, we need to
1235
2022-01-27
op
* ensure that everything gets properly released once we reach
1236
2021-10-02
op
* this point.
1239
2021-10-07
op
SPLAY_REMOVE(client_tree_id, &clients, c);
1241
2021-10-02
op
if (c->cgibev != NULL) {
1242
2021-10-02
op
bufferevent_disable(c->cgibev, EVBUFFER_READ|EVBUFFER_WRITE);
1243
2021-10-02
op
bufferevent_free(c->cgibev);
1244
2021-10-02
op
c->cgibev = NULL;
1245
2021-10-02
op
close(c->pfd);
1246
2021-10-02
op
c->pfd = -1;
1249
2021-10-02
op
bufferevent_disable(c->bev, EVBUFFER_READ|EVBUFFER_WRITE);
1250
2021-10-02
op
bufferevent_free(c->bev);
1251
2021-10-02
op
c->bev = NULL;
1253
2022-01-27
op
if (c->proxyevset &&
1254
2022-01-27
op
event_pending(&c->proxyev, EV_READ|EV_WRITE, NULL)) {
1255
2022-01-27
op
c->proxyevset = 0;
1256
2022-01-27
op
event_del(&c->proxyev);
1259
2022-01-27
op
if (c->pfd != -1 && c->proxyctx != NULL) {
1260
2022-01-27
op
/* shut down the proxy TLS connection */
1261
2022-01-27
op
client_proxy_close(c->pfd, 0, c->proxyctx);
1262
2022-01-27
op
c->pfd = -1;
1265
2022-01-27
op
if (c->proxybev != NULL)
1266
2022-01-27
op
bufferevent_free(c->proxybev);
1268
2021-10-02
op
client_close_ev(c->fd, 0, c);
1271
2021-02-02
op
static void
1272
2021-02-08
op
do_accept(int sock, short et, void *d)
1274
2021-02-08
op
struct client *c;
1275
2021-01-17
op
struct sockaddr_storage addr;
1276
2021-02-08
op
struct sockaddr *saddr;
1277
2021-01-17
op
socklen_t len;
1280
2021-02-08
op
saddr = (struct sockaddr*)&addr;
1281
2021-01-17
op
len = sizeof(addr);
1282
2021-02-12
op
if ((fd = accept(sock, saddr, &len)) == -1) {
1283
2021-10-13
op
if (errno == EWOULDBLOCK || errno == EAGAIN ||
1284
2021-10-13
op
errno == ECONNABORTED)
1286
2021-01-17
op
fatal("accept: %s", strerror(errno));
1289
2021-02-12
op
mark_nonblock(fd);
1291
2021-10-07
op
c = xcalloc(1, sizeof(*c));
1292
2021-10-07
op
c->id = ++server_client_id;
1293
2021-10-07
op
c->fd = fd;
1294
2021-10-07
op
c->pfd = -1;
1295
2021-10-07
op
c->addr = addr;
1297
2021-10-07
op
if (tls_accept_socket(ctx, &c->ctx, fd) == -1) {
1298
2021-10-07
op
log_warn(c, "failed to accept socket: %s", tls_error(c->ctx));
1299
2021-10-07
op
close(c->fd);
1300
2021-10-07
op
free(c);
1304
2021-10-07
op
SPLAY_INSERT(client_tree_id, &clients, c);
1305
2021-10-07
op
event_once(c->fd, EV_READ|EV_WRITE, handle_handshake, c, NULL);
1306
2021-10-07
op
connected_clients++;
1309
2021-05-09
op
struct client *
1310
2022-03-26
op
client_by_id(int id)
1312
2021-10-07
op
struct client find;
1314
2021-10-07
op
find.id = id;
1315
2021-10-07
op
return SPLAY_FIND(client_tree_id, &clients, &find);
1318
2021-02-08
op
static void
1319
2022-09-10
op
handle_dispatch_imsg(int fd, short ev, void *d)
1321
2022-09-10
op
struct imsgbuf *ibuf = d;
1322
2022-09-10
op
struct imsg imsg;
1323
2022-09-10
op
ssize_t n;
1325
2022-09-10
op
if ((n = imsg_read(ibuf)) == -1) {
1326
2022-09-10
op
if (errno == EAGAIN || errno == EWOULDBLOCK)
1328
2022-09-10
op
fatal("imsg_read");
1331
2022-09-10
op
if (n == 0)
1332
2022-09-10
op
fatal("connection closed."); /* XXX: fatalx */
1334
2022-09-10
op
for (;;) {
1335
2022-09-10
op
if ((n = imsg_get(ibuf, &imsg)) == -1)
1336
2022-09-10
op
fatal("imsg_get");
1337
2022-09-10
op
if (n == 0)
1340
2022-09-10
op
switch (imsg.hdr.type) {
1341
2022-09-10
op
case IMSG_QUIT:
1343
2022-09-10
op
* Don't call event_loopbreak since we want to
1344
2022-09-10
op
* finish handling the ongoing connections.
1346
2022-09-10
op
shutting_down = 1;
1348
2022-09-10
op
event_del(&e4);
1349
2022-09-10
op
if (has_ipv6)
1350
2022-09-10
op
event_del(&e6);
1351
2022-09-10
op
if (has_siginfo)
1352
2022-09-10
op
signal_del(&siginfo);
1353
2022-09-10
op
event_del(&imsgev);
1354
2022-09-10
op
signal_del(&sigusr2);
1356
2022-09-10
op
default:
1357
2022-09-10
op
/* XXX: fatalx */
1358
2022-09-10
op
fatal("Unknown message %d", imsg.hdr.type);
1360
2022-09-10
op
imsg_free(&imsg);
1364
2021-03-19
op
static void
1365
2021-02-08
op
handle_siginfo(int fd, short ev, void *d)
1367
2021-02-08
op
log_info(NULL, "%d connected clients", connected_clients);
1371
2021-03-19
op
loop(struct tls *ctx_, int sock4, int sock6, struct imsgbuf *ibuf)
1373
2021-03-19
op
ctx = ctx_;
1375
2021-10-07
op
SPLAY_INIT(&clients);
1377
2021-02-08
op
event_init();
1379
2021-03-19
op
event_set(&e4, sock4, EV_READ | EV_PERSIST, &do_accept, NULL);
1380
2021-02-12
op
event_add(&e4, NULL);
1382
2021-02-08
op
if (sock6 != -1) {
1383
2021-02-12
op
has_ipv6 = 1;
1384
2021-03-19
op
event_set(&e6, sock6, EV_READ | EV_PERSIST, &do_accept, NULL);
1385
2021-02-12
op
event_add(&e6, NULL);
1388
2022-09-07
op
if (ibuf) {
1389
2022-09-07
op
event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST,
1390
2022-09-07
op
handle_dispatch_imsg, ibuf);
1391
2022-09-07
op
event_add(&imsgev, NULL);
1394
2021-02-08
op
#ifdef SIGINFO
1395
2021-02-12
op
has_siginfo = 1;
1396
2021-02-12
op
signal_set(&siginfo, SIGINFO, &handle_siginfo, NULL);
1397
2021-02-12
op
signal_add(&siginfo, NULL);
1399
2021-02-12
op
signal_set(&sigusr2, SIGUSR2, &handle_siginfo, NULL);
1400
2021-02-12
op
signal_add(&sigusr2, NULL);
1402
2022-09-06
op
sandbox_server_process(conf.can_open_sockets);
1403
2021-02-08
op
event_dispatch();
1404
2021-02-08
op
_exit(0);
1408
2021-10-07
op
client_tree_cmp(struct client *a, struct client *b)
1410
2021-10-07
op
if (a->id == b->id)
1411
2021-10-07
op
return 0;
1412
2021-10-07
op
else if (a->id < b->id)
1413
2021-10-07
op
return -1;
1415
2021-10-07
op
return +1;
1418
2021-10-07
op
SPLAY_GENERATE(client_tree_id, client, entry, client_tree_cmp)