2021-01-18 00:23:58 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-02-12 13:47:20 +01:00
|
|
|
#include "gmid.h"
|
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2021-02-08 11:01:45 +01:00
|
|
|
#include <event.h>
|
2021-01-18 00:23:58 +01:00
|
|
|
#include <fcntl.h>
|
2021-01-21 10:07:26 +01:00
|
|
|
#include <fnmatch.h>
|
2021-02-01 12:11:43 +01:00
|
|
|
#include <limits.h>
|
2021-01-18 00:23:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
struct server {
|
|
|
|
struct client clients[MAX_USERS];
|
|
|
|
struct tls *ctx;
|
|
|
|
};
|
|
|
|
|
2021-02-14 14:11:47 +01:00
|
|
|
static struct event e4, e6, sighup, siginfo, sigusr2;
|
|
|
|
static int has_ipv6, has_siginfo;
|
2021-02-08 11:01:45 +01:00
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
int connected_clients;
|
|
|
|
|
2021-02-10 17:37:08 +01:00
|
|
|
static inline int matches(const char*, const char*);
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
static inline void reschedule_read(int, struct client*, statefn);
|
|
|
|
static inline void reschedule_write(int, struct client*, statefn);
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static int check_path(struct client*, const char*, int*);
|
2021-02-08 11:01:45 +01:00
|
|
|
static void open_file(struct client*);
|
|
|
|
static void check_for_cgi(struct client*);
|
|
|
|
static void handle_handshake(int, short, void*);
|
2021-02-08 21:50:30 +01:00
|
|
|
static char *strip_path(char*, int);
|
|
|
|
static void fmt_sbuf(const char*, struct client*, const char*);
|
2021-02-08 11:01:45 +01:00
|
|
|
static int apply_block_return(struct client*);
|
2021-02-09 23:30:04 +01:00
|
|
|
static int apply_require_ca(struct client*);
|
2021-02-08 11:01:45 +01:00
|
|
|
static void handle_open_conn(int, short, void*);
|
|
|
|
static void start_reply(struct client*, int, const char*);
|
|
|
|
static void handle_start_reply(int, short, void*);
|
|
|
|
static void start_cgi(const char*, const char*, struct client*);
|
|
|
|
static void open_dir(struct client*);
|
|
|
|
static void redirect_canonical_dir(struct client*);
|
|
|
|
static void enter_handle_dirlist(int, short, void*);
|
|
|
|
static void handle_dirlist(int, short, void*);
|
2021-02-03 00:01:09 +01:00
|
|
|
static int read_next_dir_entry(struct client*);
|
2021-02-08 11:01:45 +01:00
|
|
|
static void send_directory_listing(int, short, void*);
|
|
|
|
static void handle_cgi_reply(int, short, void*);
|
2021-02-12 12:27:33 +01:00
|
|
|
static void handle_copy(int, short, void*);
|
2021-02-08 11:01:45 +01:00
|
|
|
static void close_conn(int, short, void*);
|
|
|
|
static void do_accept(int, short, void*);
|
|
|
|
static void handle_sighup(int, short, void*);
|
|
|
|
|
2021-02-10 17:37:08 +01:00
|
|
|
static inline int
|
|
|
|
matches(const char *pattern, const char *path)
|
|
|
|
{
|
|
|
|
if (*path == '/')
|
|
|
|
path++;
|
|
|
|
return !fnmatch(pattern, path, 0);
|
|
|
|
}
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
static inline void
|
|
|
|
reschedule_read(int fd, struct client *c, statefn fn)
|
|
|
|
{
|
|
|
|
event_once(fd, EV_READ, fn, c, NULL);
|
|
|
|
}
|
|
|
|
|
2021-02-12 21:25:48 +01:00
|
|
|
static inline void
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_write(int fd, struct client *c, statefn fn)
|
|
|
|
{
|
|
|
|
event_once(fd, EV_WRITE, fn, c, NULL);
|
|
|
|
}
|
2021-02-03 00:01:09 +01:00
|
|
|
|
2021-01-24 15:11:40 +01:00
|
|
|
const char *
|
|
|
|
vhost_lang(struct vhost *v, const char *path)
|
|
|
|
{
|
|
|
|
struct location *loc;
|
|
|
|
|
2021-01-27 16:05:50 +01:00
|
|
|
if (v == NULL || path == NULL)
|
2021-01-30 13:04:20 +01:00
|
|
|
return NULL;
|
2021-01-25 15:08:31 +01:00
|
|
|
|
2021-01-30 13:04:20 +01:00
|
|
|
for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
|
2021-02-06 18:31:03 +01:00
|
|
|
if (loc->lang != NULL) {
|
2021-02-10 17:37:08 +01:00
|
|
|
if (matches(loc->match, path))
|
2021-01-30 13:04:20 +01:00
|
|
|
return loc->lang;
|
2021-01-24 15:11:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 13:04:20 +01:00
|
|
|
return v->locations[0].lang;
|
2021-01-24 15:11:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
vhost_default_mime(struct vhost *v, const char *path)
|
|
|
|
{
|
|
|
|
struct location *loc;
|
|
|
|
const char *default_mime = "application/octet-stream";
|
|
|
|
|
2021-01-27 16:05:50 +01:00
|
|
|
if (v == NULL || path == NULL)
|
|
|
|
return default_mime;
|
|
|
|
|
2021-01-30 13:04:20 +01:00
|
|
|
for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
|
2021-02-06 18:31:03 +01:00
|
|
|
if (loc->default_mime != NULL) {
|
2021-02-10 17:37:08 +01:00
|
|
|
if (matches(loc->match, path))
|
2021-01-30 13:04:20 +01:00
|
|
|
return loc->default_mime;
|
2021-01-24 15:11:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 13:04:20 +01:00
|
|
|
if (v->locations[0].default_mime != NULL)
|
|
|
|
return v->locations[0].default_mime;
|
2021-01-24 15:11:40 +01:00
|
|
|
return default_mime;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
vhost_index(struct vhost *v, const char *path)
|
|
|
|
{
|
|
|
|
struct location *loc;
|
|
|
|
const char *index = "index.gmi";
|
|
|
|
|
2021-01-27 16:05:50 +01:00
|
|
|
if (v == NULL || path == NULL)
|
|
|
|
return index;
|
|
|
|
|
2021-01-30 13:04:20 +01:00
|
|
|
for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
|
2021-02-06 18:31:03 +01:00
|
|
|
if (loc->index != NULL) {
|
2021-02-10 17:37:08 +01:00
|
|
|
if (matches(loc->match, path))
|
2021-01-30 13:04:20 +01:00
|
|
|
return loc->index;
|
2021-01-24 15:11:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 13:12:37 +01:00
|
|
|
if (v->locations[0].index != NULL)
|
|
|
|
return v->locations[0].index;
|
2021-01-24 15:11:40 +01:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2021-01-24 19:53:26 +01:00
|
|
|
int
|
|
|
|
vhost_auto_index(struct vhost *v, const char *path)
|
|
|
|
{
|
|
|
|
struct location *loc;
|
2021-01-30 13:04:20 +01:00
|
|
|
|
|
|
|
if (v == NULL || path == NULL)
|
|
|
|
return 0;
|
2021-01-24 19:53:26 +01:00
|
|
|
|
2021-02-06 15:36:26 +01:00
|
|
|
for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
|
2021-02-06 18:31:03 +01:00
|
|
|
if (loc->auto_index != 0) {
|
2021-02-10 17:37:08 +01:00
|
|
|
if (matches(loc->match, path))
|
2021-01-30 13:04:20 +01:00
|
|
|
return loc->auto_index == 1;
|
2021-01-24 19:53:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 13:04:20 +01:00
|
|
|
return v->locations[0].auto_index == 1;
|
2021-01-24 19:53:26 +01:00
|
|
|
}
|
|
|
|
|
2021-02-06 18:22:37 +01:00
|
|
|
int
|
|
|
|
vhost_block_return(struct vhost *v, const char *path, int *code, const char **fmt)
|
|
|
|
{
|
|
|
|
struct location *loc;
|
|
|
|
|
|
|
|
if (v == NULL || path == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
|
2021-02-06 18:31:03 +01:00
|
|
|
if (loc->block_code != 0) {
|
2021-02-10 17:37:08 +01:00
|
|
|
if (matches(loc->match, path)) {
|
2021-02-06 18:22:37 +01:00
|
|
|
*code = loc->block_code;
|
|
|
|
*fmt = loc->block_fmt;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*code = v->locations[0].block_code;
|
|
|
|
*fmt = v->locations[0].block_fmt;
|
|
|
|
return v->locations[0].block_code != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
vhost_strip(struct vhost *v, const char *path)
|
|
|
|
{
|
|
|
|
struct location *loc;
|
|
|
|
|
|
|
|
if (v == NULL || path == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
|
2021-02-06 18:31:03 +01:00
|
|
|
if (loc->strip != 0) {
|
2021-02-10 17:37:08 +01:00
|
|
|
if (matches(loc->match, path))
|
2021-02-06 18:22:37 +01:00
|
|
|
return loc->strip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return v->locations[0].strip;
|
|
|
|
}
|
|
|
|
|
2021-02-09 23:30:04 +01:00
|
|
|
X509_STORE *
|
|
|
|
vhost_require_ca(struct vhost *v, const char *path)
|
|
|
|
{
|
|
|
|
struct location *loc;
|
|
|
|
|
|
|
|
if (v == NULL || path == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
|
|
|
|
if (loc->reqca != NULL) {
|
2021-02-10 17:37:08 +01:00
|
|
|
if (matches(loc->match, path))
|
2021-02-09 23:30:04 +01:00
|
|
|
return loc->reqca;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return v->locations[0].reqca;
|
|
|
|
}
|
|
|
|
|
2021-02-22 09:53:14 +01:00
|
|
|
int
|
|
|
|
vhost_disable_log(struct vhost *v, const char *path)
|
|
|
|
{
|
|
|
|
struct location *loc;
|
|
|
|
|
|
|
|
if (v == NULL || path == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
|
|
|
|
if (loc->disable_log && matches(loc->match, path))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v->locations[0].disable_log;
|
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static int
|
2021-01-18 00:23:58 +01:00
|
|
|
check_path(struct client *c, const char *path, int *fd)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
2021-01-21 23:44:41 +01:00
|
|
|
const char *p;
|
2021-01-24 19:53:26 +01:00
|
|
|
int flags;
|
2021-01-18 00:23:58 +01:00
|
|
|
|
|
|
|
assert(path != NULL);
|
2021-01-21 23:44:41 +01:00
|
|
|
|
|
|
|
if (*path == '\0')
|
|
|
|
p = ".";
|
|
|
|
else if (*path == '/')
|
|
|
|
/* in send_dir we add an initial / (to be
|
|
|
|
* redirect-friendly), but here we want to skip it */
|
|
|
|
p = path+1;
|
|
|
|
else
|
|
|
|
p = path;
|
|
|
|
|
2021-01-24 19:53:26 +01:00
|
|
|
flags = O_RDONLY | O_NOFOLLOW;
|
|
|
|
|
|
|
|
if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
|
2021-01-18 00:23:58 +01:00
|
|
|
return FILE_MISSING;
|
|
|
|
|
|
|
|
if (fstat(*fd, &sb) == -1) {
|
2021-02-07 16:30:28 +01:00
|
|
|
log_notice(c, "failed stat for %s: %s", path, strerror(errno));
|
2021-01-18 00:23:58 +01:00
|
|
|
return FILE_MISSING;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(sb.st_mode))
|
|
|
|
return FILE_DIRECTORY;
|
|
|
|
|
|
|
|
if (sb.st_mode & S_IXUSR)
|
|
|
|
return FILE_EXECUTABLE;
|
|
|
|
|
|
|
|
return FILE_EXISTS;
|
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
open_file(struct client *c)
|
2021-01-18 00:23:58 +01:00
|
|
|
{
|
2021-02-08 11:01:45 +01:00
|
|
|
switch (check_path(c, c->iri.path, &c->pfd)) {
|
2021-01-18 00:23:58 +01:00
|
|
|
case FILE_EXECUTABLE:
|
2021-02-12 20:51:54 +01:00
|
|
|
if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
|
2021-02-08 11:01:45 +01:00
|
|
|
start_cgi(c->iri.path, "", c);
|
2021-01-24 10:54:44 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-01-18 00:23:58 +01:00
|
|
|
|
|
|
|
/* fallthrough */
|
|
|
|
|
|
|
|
case FILE_EXISTS:
|
2021-02-12 12:27:33 +01:00
|
|
|
c->next = handle_copy;
|
|
|
|
start_reply(c, SUCCESS, mime(c->host, c->iri.path));
|
2021-01-24 10:54:44 +01:00
|
|
|
return;
|
2021-01-18 00:23:58 +01:00
|
|
|
|
|
|
|
case FILE_DIRECTORY:
|
2021-02-08 11:01:45 +01:00
|
|
|
open_dir(c);
|
2021-01-24 10:54:44 +01:00
|
|
|
return;
|
2021-01-18 00:23:58 +01:00
|
|
|
|
|
|
|
case FILE_MISSING:
|
2021-02-12 20:51:54 +01:00
|
|
|
if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
|
2021-02-08 11:01:45 +01:00
|
|
|
check_for_cgi(c);
|
2021-01-24 10:54:44 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(c, NOT_FOUND, "not found");
|
2021-01-24 10:54:44 +01:00
|
|
|
return;
|
2021-01-18 00:23:58 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* unreachable */
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the inverse of this algorithm, i.e. starting from the start of the
|
|
|
|
* path + strlen(cgi), and checking if each component, should be
|
|
|
|
* faster. But it's tedious to write. This does the opposite: starts
|
|
|
|
* from the end and strip one component at a time, until either an
|
|
|
|
* executable is found or we emptied the path.
|
|
|
|
*/
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
check_for_cgi(struct client *c)
|
2021-01-18 00:23:58 +01:00
|
|
|
{
|
2021-02-01 12:11:43 +01:00
|
|
|
char path[PATH_MAX];
|
2021-01-18 00:23:58 +01:00
|
|
|
char *end;
|
2021-02-01 12:11:43 +01:00
|
|
|
|
|
|
|
strlcpy(path, c->iri.path, sizeof(path));
|
2021-01-18 00:23:58 +01:00
|
|
|
end = strchr(path, '\0');
|
|
|
|
|
|
|
|
while (end > path) {
|
|
|
|
/* go up one level. UNIX paths are simple and POSIX
|
|
|
|
* dirname, with its ambiguities on if the given path
|
|
|
|
* is changed or not, gives me headaches. */
|
|
|
|
while (*end != '/')
|
|
|
|
end--;
|
|
|
|
*end = '\0';
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
switch (check_path(c, path, &c->pfd)) {
|
2021-01-18 00:23:58 +01:00
|
|
|
case FILE_EXECUTABLE:
|
2021-02-08 11:01:45 +01:00
|
|
|
start_cgi(path, end+1, c);
|
2021-01-24 10:54:44 +01:00
|
|
|
return;
|
2021-01-18 00:23:58 +01:00
|
|
|
case FILE_MISSING:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
*end = '/';
|
|
|
|
end--;
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(c, NOT_FOUND, "not found");
|
2021-01-24 10:54:44 +01:00
|
|
|
return;
|
2021-01-18 00:23:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 15:16:39 +01:00
|
|
|
void
|
|
|
|
mark_nonblock(int fd)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
if ((flags = fcntl(fd, F_GETFL)) == -1)
|
|
|
|
fatal("fcntl(F_GETFL): %s", strerror(errno));
|
|
|
|
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
|
|
|
|
fatal("fcntl(F_SETFL): %s", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
handle_handshake(int fd, short ev, void *d)
|
2021-01-18 00:23:58 +01:00
|
|
|
{
|
2021-02-08 11:01:45 +01:00
|
|
|
struct client *c = d;
|
2021-01-18 00:23:58 +01:00
|
|
|
struct vhost *h;
|
|
|
|
const char *servname;
|
2021-01-29 18:29:14 +01:00
|
|
|
const char *parse_err = "unknown error";
|
2021-01-18 00:23:58 +01:00
|
|
|
|
|
|
|
switch (tls_handshake(c->ctx)) {
|
|
|
|
case 0: /* success */
|
|
|
|
case -1: /* already handshaked */
|
|
|
|
break;
|
|
|
|
case TLS_WANT_POLLIN:
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_read(fd, c, &handle_handshake);
|
2021-01-18 00:23:58 +01:00
|
|
|
return;
|
|
|
|
case TLS_WANT_POLLOUT:
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_write(fd, c, &handle_handshake);
|
2021-01-18 00:23:58 +01:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
/* unreachable */
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
servname = tls_conn_servername(c->ctx);
|
2021-01-29 18:29:14 +01:00
|
|
|
if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
|
2021-02-07 16:30:28 +01:00
|
|
|
log_info(c, "puny_decode: %s", parse_err);
|
2021-01-29 18:29:14 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2021-01-18 00:23:58 +01:00
|
|
|
|
|
|
|
for (h = hosts; h->domain != NULL; ++h) {
|
2021-02-12 20:51:54 +01:00
|
|
|
if (matches(h->domain, c->domain))
|
2021-01-18 00:23:58 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-02-07 16:30:28 +01:00
|
|
|
log_debug(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
|
|
|
|
servname != NULL ? servname : "(null)",
|
|
|
|
c->domain,
|
|
|
|
h->domain != NULL ? h->domain : "(null)");
|
2021-01-27 16:06:15 +01:00
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
if (h->domain != NULL) {
|
|
|
|
c->host = h;
|
2021-02-08 11:01:45 +01:00
|
|
|
handle_open_conn(fd, ev, c);
|
2021-01-18 00:23:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:29:14 +01:00
|
|
|
err:
|
2021-01-21 09:26:21 +01:00
|
|
|
if (servname != NULL)
|
|
|
|
strncpy(c->req, servname, sizeof(c->req));
|
|
|
|
else
|
|
|
|
strncpy(c->req, "null", sizeof(c->req));
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
|
2021-01-18 00:23:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 21:50:30 +01:00
|
|
|
static char *
|
|
|
|
strip_path(char *path, int strip)
|
2021-02-06 18:22:37 +01:00
|
|
|
{
|
2021-02-08 21:50:30 +01:00
|
|
|
char *t;
|
2021-02-06 18:22:37 +01:00
|
|
|
|
|
|
|
while (strip > 0) {
|
|
|
|
if ((t = strchr(path, '/')) == NULL) {
|
|
|
|
path = strchr(path, '\0');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
path = t;
|
|
|
|
strip--;
|
|
|
|
}
|
|
|
|
|
2021-02-08 21:50:30 +01:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fmt_sbuf(const char *fmt, struct client *c, const char *path)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
char buf[32];
|
|
|
|
|
2021-02-06 18:22:37 +01:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
for (i = 0; *fmt; ++fmt) {
|
|
|
|
if (i == sizeof(buf)-1 || *fmt == '%') {
|
|
|
|
strlcat(c->sbuf, buf, sizeof(c->sbuf));
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*fmt != '%') {
|
|
|
|
buf[i++] = *fmt;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*++fmt) {
|
|
|
|
case '%':
|
|
|
|
strlcat(c->sbuf, "%", sizeof(c->sbuf));
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
strlcat(c->sbuf, path, sizeof(c->sbuf));
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
strlcat(c->sbuf, c->iri.query, sizeof(c->sbuf));
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
snprintf(buf, sizeof(buf), "%d", conf.port);
|
|
|
|
strlcat(c->sbuf, buf, sizeof(c->sbuf));
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
strlcat(c->sbuf, c->domain, sizeof(c->sbuf));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("%s: unknown fmt specifier %c",
|
|
|
|
__func__, *fmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != 0)
|
|
|
|
strlcat(c->sbuf, buf, sizeof(c->sbuf));
|
2021-02-08 21:50:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 1 if a matching `block return' (and apply it), 0 otherwise */
|
|
|
|
static int
|
|
|
|
apply_block_return(struct client *c)
|
|
|
|
{
|
|
|
|
const char *fmt, *path;
|
|
|
|
int code;
|
|
|
|
|
|
|
|
if (!vhost_block_return(c->host, c->iri.path, &code, &fmt))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
path = strip_path(c->iri.path, vhost_strip(c->host, c->iri.path));
|
|
|
|
fmt_sbuf(fmt, c, path);
|
2021-02-06 18:22:37 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(c, code, c->sbuf);
|
2021-02-06 18:22:37 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-02-09 23:30:04 +01:00
|
|
|
/* 1 if matching `require client ca' fails (and apply it), 0 otherwise */
|
|
|
|
static int
|
|
|
|
apply_require_ca(struct client *c)
|
|
|
|
{
|
|
|
|
X509_STORE *store;
|
|
|
|
const uint8_t *cert;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if ((store = vhost_require_ca(c->host, c->iri.path)) == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!tls_peer_cert_provided(c->ctx)) {
|
|
|
|
start_reply(c, CLIENT_CERT_REQ, "client certificate required");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cert = tls_peer_cert_chain_pem(c->ctx, &len);
|
|
|
|
if (!validate_against_ca(store, cert, len)) {
|
|
|
|
start_reply(c, CERT_NOT_AUTH, "certificate not authorised");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
handle_open_conn(int fd, short ev, void *d)
|
2021-01-18 00:23:58 +01:00
|
|
|
{
|
2021-02-08 11:01:45 +01:00
|
|
|
struct client *c = d;
|
2021-01-18 00:23:58 +01:00
|
|
|
const char *parse_err = "invalid request";
|
2021-01-27 11:47:49 +01:00
|
|
|
char decoded[DOMAIN_NAME_LEN];
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-01-20 19:54:44 +01:00
|
|
|
bzero(c->req, sizeof(c->req));
|
|
|
|
bzero(&c->iri, sizeof(c->iri));
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-01-20 19:54:44 +01:00
|
|
|
switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
|
2021-01-18 00:23:58 +01:00
|
|
|
case -1:
|
2021-02-07 16:30:28 +01:00
|
|
|
log_err(c, "tls_read: %s", tls_error(c->ctx));
|
2021-02-08 11:01:45 +01:00
|
|
|
close_conn(fd, ev, c);
|
2021-01-18 00:23:58 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
case TLS_WANT_POLLIN:
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_read(fd, c, &handle_open_conn);
|
2021-01-18 00:23:58 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
case TLS_WANT_POLLOUT:
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_write(fd, c, &handle_open_conn);
|
2021-01-18 00:23:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-27 16:05:16 +01:00
|
|
|
if (!trim_req_iri(c->req, &parse_err)
|
2021-01-29 18:11:03 +01:00
|
|
|
|| !parse_iri(c->req, &c->iri, &parse_err)
|
|
|
|
|| !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
|
2021-02-07 16:30:28 +01:00
|
|
|
log_info(c, "iri parse error: %s", parse_err);
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(c, BAD_REQUEST, "invalid request");
|
2021-01-18 00:23:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-27 11:47:49 +01:00
|
|
|
if (c->iri.port_no != conf.port
|
|
|
|
|| strcmp(c->iri.schema, "gemini")
|
|
|
|
|| strcmp(decoded, c->domain)) {
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(c, PROXY_REFUSED, "won't proxy request");
|
2021-01-18 00:23:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-09 23:30:04 +01:00
|
|
|
if (apply_require_ca(c))
|
|
|
|
return;
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
if (apply_block_return(c))
|
2021-02-06 19:28:43 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (c->host->entrypoint != NULL) {
|
2021-02-08 11:01:45 +01:00
|
|
|
start_cgi(c->host->entrypoint, c->iri.path, c);
|
2021-02-06 19:28:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
open_file(c);
|
2021-01-18 00:23:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(struct client *c, int code, const char *meta)
|
2021-02-01 21:00:33 +01:00
|
|
|
{
|
|
|
|
c->code = code;
|
|
|
|
c->meta = meta;
|
2021-02-08 11:01:45 +01:00
|
|
|
handle_start_reply(c->fd, 0, c);
|
2021-02-01 21:00:33 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
handle_start_reply(int fd, short ev, void *d)
|
2021-01-18 00:23:58 +01:00
|
|
|
{
|
2021-02-08 11:01:45 +01:00
|
|
|
struct client *c = d;
|
2021-01-19 11:58:29 +01:00
|
|
|
char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
|
2021-01-24 15:11:40 +01:00
|
|
|
const char *lang;
|
2021-01-20 19:54:44 +01:00
|
|
|
size_t len;
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-01-24 15:11:40 +01:00
|
|
|
lang = vhost_lang(c->host, c->iri.path);
|
|
|
|
|
2021-02-01 21:00:33 +01:00
|
|
|
snprintf(buf, sizeof(buf), "%d ", c->code);
|
|
|
|
strlcat(buf, c->meta, sizeof(buf));
|
|
|
|
if (!strcmp(c->meta, "text/gemini") && lang != NULL) {
|
2021-01-19 11:58:29 +01:00
|
|
|
strlcat(buf, "; lang=", sizeof(buf));
|
2021-01-24 15:11:40 +01:00
|
|
|
strlcat(buf, lang, sizeof(buf));
|
2021-01-19 11:58:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
len = strlcat(buf, "\r\n", sizeof(buf));
|
2021-01-20 19:54:44 +01:00
|
|
|
assert(len < sizeof(buf));
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-01-19 12:00:06 +01:00
|
|
|
switch (tls_write(c->ctx, buf, len)) {
|
2021-01-24 10:49:09 +01:00
|
|
|
case -1:
|
2021-02-08 11:01:45 +01:00
|
|
|
close_conn(fd, ev, c);
|
2021-01-24 10:49:09 +01:00
|
|
|
return;
|
2021-01-18 00:23:58 +01:00
|
|
|
case TLS_WANT_POLLIN:
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_read(fd, c, &handle_start_reply);
|
2021-01-24 10:49:09 +01:00
|
|
|
return;
|
2021-01-18 00:23:58 +01:00
|
|
|
case TLS_WANT_POLLOUT:
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_write(fd, c, &handle_start_reply);
|
2021-01-24 10:49:09 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:19:14 +01:00
|
|
|
if (!vhost_disable_log(c->host, c->iri.path))
|
|
|
|
log_request(c, buf, sizeof(buf));
|
2021-01-24 10:49:09 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
if (c->code != SUCCESS)
|
|
|
|
close_conn(fd, ev, c);
|
|
|
|
else
|
|
|
|
c->next(fd, ev, c);
|
2021-01-18 00:23:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
start_cgi(const char *spath, const char *relpath, struct client *c)
|
2021-01-18 00:23:58 +01:00
|
|
|
{
|
|
|
|
char addr[NI_MAXHOST];
|
|
|
|
int e;
|
|
|
|
|
|
|
|
e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
|
|
|
|
addr, sizeof(addr),
|
|
|
|
NULL, 0,
|
|
|
|
NI_NUMERICHOST);
|
|
|
|
if (e != 0)
|
|
|
|
goto err;
|
|
|
|
|
2021-02-01 12:11:43 +01:00
|
|
|
if (!send_iri(exfd, &c->iri)
|
|
|
|
|| !send_string(exfd, spath)
|
2021-01-18 00:23:58 +01:00
|
|
|
|| !send_string(exfd, relpath)
|
|
|
|
|| !send_string(exfd, addr)
|
2021-02-07 22:47:01 +01:00
|
|
|
|| !send_string(exfd, tls_peer_cert_subject(c->ctx))
|
|
|
|
|| !send_string(exfd, tls_peer_cert_issuer(c->ctx))
|
|
|
|
|| !send_string(exfd, tls_peer_cert_hash(c->ctx))
|
|
|
|
|| !send_time(exfd, tls_peer_cert_notbefore(c->ctx))
|
|
|
|
|| !send_time(exfd, tls_peer_cert_notafter(c->ctx))
|
2021-01-18 00:23:58 +01:00
|
|
|
|| !send_vhost(exfd, c->host))
|
|
|
|
goto err;
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
close(c->pfd);
|
|
|
|
if ((c->pfd = recv_fd(exfd)) == -1) {
|
|
|
|
start_reply(c, TEMP_FAILURE, "internal server error");
|
2021-01-24 10:54:44 +01:00
|
|
|
return;
|
2021-01-18 00:23:58 +01:00
|
|
|
}
|
2021-02-01 23:04:51 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_read(c->pfd, c, &handle_cgi_reply);
|
2021-01-24 10:54:44 +01:00
|
|
|
return;
|
2021-01-18 00:23:58 +01:00
|
|
|
|
|
|
|
err:
|
|
|
|
/* fatal("cannot talk to the executor process: %s", strerror(errno)); */
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("cannot talk to the executor process");
|
2021-01-18 00:23:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
open_dir(struct client *c)
|
2021-01-18 00:23:58 +01:00
|
|
|
{
|
|
|
|
size_t len;
|
2021-01-24 19:53:26 +01:00
|
|
|
int dirfd;
|
|
|
|
char *before_file;
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-01-24 19:53:26 +01:00
|
|
|
len = strlen(c->iri.path);
|
|
|
|
if (len > 0 && !ends_with(c->iri.path, "/")) {
|
2021-02-08 11:01:45 +01:00
|
|
|
redirect_canonical_dir(c);
|
2021-01-20 19:54:44 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-01-21 23:44:41 +01:00
|
|
|
strlcpy(c->sbuf, "/", sizeof(c->sbuf));
|
2021-01-24 19:53:26 +01:00
|
|
|
strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
|
|
|
|
if (!ends_with(c->sbuf, "/"))
|
2021-01-20 19:54:44 +01:00
|
|
|
strlcat(c->sbuf, "/", sizeof(c->sbuf));
|
2021-01-24 19:53:26 +01:00
|
|
|
before_file = strchr(c->sbuf, '\0');
|
|
|
|
len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
|
|
|
|
sizeof(c->sbuf));
|
|
|
|
if (len >= sizeof(c->sbuf)) {
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(c, TEMP_FAILURE, "internal server error");
|
2021-01-24 19:53:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->iri.path = c->sbuf;
|
|
|
|
|
|
|
|
/* close later unless we have to generate the dir listing */
|
2021-02-08 11:01:45 +01:00
|
|
|
dirfd = c->pfd;
|
|
|
|
c->pfd = -1;
|
2021-01-24 19:53:26 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
switch (check_path(c, c->iri.path, &c->pfd)) {
|
2021-01-24 19:53:26 +01:00
|
|
|
case FILE_EXECUTABLE:
|
2021-02-12 20:51:54 +01:00
|
|
|
if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
|
2021-02-08 11:01:45 +01:00
|
|
|
start_cgi(c->iri.path, "", c);
|
2021-01-24 19:53:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fallthrough */
|
|
|
|
|
|
|
|
case FILE_EXISTS:
|
2021-02-12 12:32:49 +01:00
|
|
|
c->next = handle_copy;
|
|
|
|
start_reply(c, SUCCESS, mime(c->host, c->iri.path));
|
2021-01-24 19:53:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_DIRECTORY:
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(c, TEMP_REDIRECT, c->sbuf);
|
2021-01-24 19:53:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FILE_MISSING:
|
|
|
|
*before_file = '\0';
|
|
|
|
|
|
|
|
if (!vhost_auto_index(c->host, c->iri.path)) {
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(c, NOT_FOUND, "not found");
|
2021-01-24 19:53:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
c->pfd = dirfd;
|
2021-02-02 10:48:32 +01:00
|
|
|
c->next = enter_handle_dirlist;
|
2021-01-24 19:53:26 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
if ((c->dir = fdopendir(c->pfd)) == NULL) {
|
2021-02-07 16:30:28 +01:00
|
|
|
log_err(c, "fdopendir(%d) (vhost:%s) %s: %s",
|
2021-02-08 11:01:45 +01:00
|
|
|
c->pfd, c->host->domain, c->iri.path, strerror(errno));
|
|
|
|
start_reply(c, TEMP_FAILURE, "internal server error");
|
2021-01-24 19:53:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
c->off = 0;
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(c, SUCCESS, "text/gemini");
|
2021-01-20 19:54:44 +01:00
|
|
|
return;
|
2021-01-24 19:53:26 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* unreachable */
|
|
|
|
abort();
|
2021-01-20 19:54:44 +01:00
|
|
|
}
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-01-24 19:53:26 +01:00
|
|
|
close(dirfd);
|
|
|
|
}
|
2021-01-21 23:44:41 +01:00
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
redirect_canonical_dir(struct client *c)
|
2021-01-24 19:53:26 +01:00
|
|
|
{
|
|
|
|
size_t len;
|
2021-01-21 23:44:41 +01:00
|
|
|
|
2021-01-24 19:53:26 +01:00
|
|
|
strlcpy(c->sbuf, "/", sizeof(c->sbuf));
|
|
|
|
strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
|
|
|
|
len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-01-20 19:54:44 +01:00
|
|
|
if (len >= sizeof(c->sbuf)) {
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(c, TEMP_FAILURE, "internal server error");
|
2021-01-20 19:54:44 +01:00
|
|
|
return;
|
2021-01-18 00:23:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(c, TEMP_REDIRECT, c->sbuf);
|
2021-01-24 19:53:26 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
enter_handle_dirlist(int fd, short ev, void *d)
|
2021-02-02 10:48:32 +01:00
|
|
|
{
|
2021-02-08 11:01:45 +01:00
|
|
|
struct client *c = d;
|
2021-02-02 10:48:32 +01:00
|
|
|
char b[PATH_MAX];
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
strlcpy(b, c->iri.path, sizeof(b));
|
|
|
|
l = snprintf(c->sbuf, sizeof(c->sbuf),
|
|
|
|
"# Index of %s\n\n", b);
|
|
|
|
if (l >= sizeof(c->sbuf)) {
|
|
|
|
/* this is impossible, given that we have enough space
|
|
|
|
* in c->sbuf to hold the ancilliary string plus the
|
|
|
|
* full path; but it wouldn't read nice without some
|
|
|
|
* error checking, and I'd like to avoid a strlen. */
|
2021-02-08 11:01:45 +01:00
|
|
|
close_conn(fd, ev, c);
|
2021-02-02 10:48:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
c->len = l;
|
|
|
|
handle_dirlist(fd, ev, c);
|
2021-02-02 10:48:32 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
handle_dirlist(int fd, short ev, void *d)
|
2021-02-02 10:48:32 +01:00
|
|
|
{
|
2021-02-08 11:01:45 +01:00
|
|
|
struct client *c = d;
|
2021-02-02 10:48:32 +01:00
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
while (c->len > 0) {
|
|
|
|
switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
|
|
|
|
case -1:
|
2021-02-08 11:01:45 +01:00
|
|
|
close_conn(fd, ev, c);
|
2021-02-02 10:48:32 +01:00
|
|
|
return;
|
|
|
|
case TLS_WANT_POLLOUT:
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_read(fd, c, &handle_dirlist);
|
2021-02-02 10:48:32 +01:00
|
|
|
return;
|
|
|
|
case TLS_WANT_POLLIN:
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_write(fd, c, &handle_dirlist);
|
2021-02-02 10:48:32 +01:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
c->off += r;
|
|
|
|
c->len -= r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
send_directory_listing(fd, ev, c);
|
2021-02-02 10:48:32 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static int
|
2021-01-24 19:53:26 +01:00
|
|
|
read_next_dir_entry(struct client *c)
|
|
|
|
{
|
|
|
|
struct dirent *d;
|
|
|
|
|
|
|
|
do {
|
|
|
|
errno = 0;
|
|
|
|
if ((d = readdir(c->dir)) == NULL) {
|
|
|
|
if (errno != 0)
|
2021-02-07 16:30:28 +01:00
|
|
|
log_err(c, "readdir: %s", strerror(errno));
|
2021-01-24 19:53:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} while (!strcmp(d->d_name, "."));
|
|
|
|
|
|
|
|
/* XXX: url escape */
|
|
|
|
snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
|
|
|
|
d->d_name, d->d_name);
|
|
|
|
c->len = strlen(c->sbuf);
|
|
|
|
c->off = 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
send_directory_listing(int fd, short ev, void *d)
|
2021-01-24 19:53:26 +01:00
|
|
|
{
|
2021-02-08 11:01:45 +01:00
|
|
|
struct client *c = d;
|
2021-01-24 19:53:26 +01:00
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (c->len == 0) {
|
|
|
|
if (!read_next_dir_entry(c))
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (c->len > 0) {
|
|
|
|
switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
|
|
|
|
case -1:
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
case TLS_WANT_POLLOUT:
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_read(fd, c, &send_directory_listing);
|
2021-01-24 19:53:26 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
case TLS_WANT_POLLIN:
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_write(fd, c, &send_directory_listing);
|
2021-01-24 19:53:26 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
c->off += r;
|
|
|
|
c->len -= r;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
2021-02-08 11:01:45 +01:00
|
|
|
close_conn(fd, ev, d);
|
2021-01-18 00:23:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-01 23:04:51 +01:00
|
|
|
/* accumulate the meta line from the cgi script. */
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
handle_cgi_reply(int fd, short ev, void *d)
|
2021-01-23 16:32:38 +01:00
|
|
|
{
|
2021-02-08 11:01:45 +01:00
|
|
|
struct client *c = d;
|
2021-02-01 23:04:51 +01:00
|
|
|
void *buf, *e;
|
2021-01-23 16:32:38 +01:00
|
|
|
size_t len;
|
|
|
|
ssize_t r;
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
|
2021-02-01 23:04:51 +01:00
|
|
|
buf = c->sbuf + c->len;
|
|
|
|
len = sizeof(c->sbuf) - c->len;
|
2021-01-23 16:32:38 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
r = read(c->pfd, buf, len);
|
2021-02-01 23:04:51 +01:00
|
|
|
if (r == 0 || r == -1) {
|
2021-02-08 11:01:45 +01:00
|
|
|
start_reply(c, CGI_ERROR, "CGI error");
|
2021-02-01 23:04:51 +01:00
|
|
|
return;
|
2021-01-23 16:32:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c->len += r;
|
|
|
|
|
2021-02-01 23:04:51 +01:00
|
|
|
/* TODO: error if the CGI script don't reply correctly */
|
|
|
|
e = strchr(c->sbuf, '\n');
|
|
|
|
if (e != NULL || c->len == sizeof(c->sbuf)) {
|
2021-01-23 16:32:38 +01:00
|
|
|
log_request(c, c->sbuf, c->len);
|
|
|
|
|
2021-02-01 23:04:51 +01:00
|
|
|
c->off = 0;
|
2021-02-12 12:27:33 +01:00
|
|
|
handle_copy(fd, ev, c);
|
2021-02-01 23:04:51 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-02-08 11:01:45 +01:00
|
|
|
|
|
|
|
reschedule_read(fd, c, &handle_cgi_reply);
|
2021-01-23 16:32:38 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-12 12:27:33 +01:00
|
|
|
handle_copy(int fd, short ev, void *d)
|
2021-01-18 00:23:58 +01:00
|
|
|
{
|
2021-02-08 11:01:45 +01:00
|
|
|
struct client *c = d;
|
2021-01-18 00:23:58 +01:00
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
while (c->len > 0) {
|
|
|
|
switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
|
|
|
|
case -1:
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
case TLS_WANT_POLLOUT:
|
2021-02-12 12:27:33 +01:00
|
|
|
reschedule_write(c->fd, c, &handle_copy);
|
2021-01-18 00:23:58 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
case TLS_WANT_POLLIN:
|
2021-02-12 12:27:33 +01:00
|
|
|
reschedule_read(c->fd, c, &handle_copy);
|
2021-01-18 00:23:58 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
c->off += r;
|
|
|
|
c->len -= r;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-12 12:27:33 +01:00
|
|
|
|
|
|
|
switch (r = read(c->pfd, c->sbuf, sizeof(c->sbuf))) {
|
|
|
|
case 0:
|
|
|
|
goto end;
|
|
|
|
case -1:
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
reschedule_read(c->pfd, c, &handle_copy);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
goto end;
|
|
|
|
default:
|
|
|
|
c->len = r;
|
|
|
|
c->off = 0;
|
|
|
|
}
|
2021-01-18 00:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
2021-02-08 11:01:45 +01:00
|
|
|
close_conn(c->fd, ev, d);
|
2021-01-18 00:23:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
close_conn(int fd, short ev, void *d)
|
2021-01-18 00:23:58 +01:00
|
|
|
{
|
2021-02-08 11:01:45 +01:00
|
|
|
struct client *c = d;
|
2021-01-18 00:23:58 +01:00
|
|
|
|
|
|
|
switch (tls_close(c->ctx)) {
|
|
|
|
case TLS_WANT_POLLIN:
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_read(fd, c, &close_conn);
|
2021-01-18 00:23:58 +01:00
|
|
|
return;
|
|
|
|
case TLS_WANT_POLLOUT:
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_read(fd, c, &close_conn);
|
2021-01-18 00:23:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
connected_clients--;
|
|
|
|
|
|
|
|
tls_free(c->ctx);
|
|
|
|
c->ctx = NULL;
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
if (c->pfd != -1)
|
|
|
|
close(c->pfd);
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-01-24 19:53:26 +01:00
|
|
|
if (c->dir != NULL)
|
|
|
|
closedir(c->dir);
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
close(c->fd);
|
|
|
|
c->fd = -1;
|
2021-01-18 00:23:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 00:01:09 +01:00
|
|
|
static void
|
2021-02-08 11:01:45 +01:00
|
|
|
do_accept(int sock, short et, void *d)
|
2021-01-18 00:23:58 +01:00
|
|
|
{
|
2021-02-08 11:01:45 +01:00
|
|
|
struct client *c;
|
|
|
|
struct server *s = d;
|
2021-01-18 00:23:58 +01:00
|
|
|
struct sockaddr_storage addr;
|
2021-02-08 11:01:45 +01:00
|
|
|
struct sockaddr *saddr;
|
2021-01-18 00:23:58 +01:00
|
|
|
socklen_t len;
|
2021-02-08 11:01:45 +01:00
|
|
|
int i, fd;
|
|
|
|
|
|
|
|
(void)et;
|
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
saddr = (struct sockaddr*)&addr;
|
2021-01-18 00:23:58 +01:00
|
|
|
len = sizeof(addr);
|
2021-02-12 12:59:03 +01:00
|
|
|
if ((fd = accept(sock, saddr, &len)) == -1) {
|
2021-02-07 13:04:11 +01:00
|
|
|
if (errno == EWOULDBLOCK || errno == EAGAIN)
|
2021-01-18 00:23:58 +01:00
|
|
|
return;
|
|
|
|
fatal("accept: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2021-02-12 12:59:03 +01:00
|
|
|
mark_nonblock(fd);
|
|
|
|
|
2021-01-18 00:23:58 +01:00
|
|
|
for (i = 0; i < MAX_USERS; ++i) {
|
2021-02-08 11:01:45 +01:00
|
|
|
c = &s->clients[i];
|
|
|
|
if (c->fd == -1) {
|
|
|
|
memset(c, 0, sizeof(*c));
|
|
|
|
if (tls_accept_socket(s->ctx, &c->ctx, fd) == -1)
|
2021-01-18 00:23:58 +01:00
|
|
|
break; /* goodbye fd! */
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
c->fd = fd;
|
|
|
|
c->pfd = -1;
|
|
|
|
c->dir = NULL;
|
|
|
|
c->addr = addr;
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
reschedule_read(fd, c, &handle_handshake);
|
2021-01-18 00:23:58 +01:00
|
|
|
connected_clients++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
static void
|
|
|
|
handle_sighup(int fd, short ev, void *d)
|
2021-01-18 00:23:58 +01:00
|
|
|
{
|
2021-02-08 11:01:45 +01:00
|
|
|
(void)fd;
|
|
|
|
(void)ev;
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-02-12 09:50:25 +01:00
|
|
|
event_del(&e4);
|
|
|
|
if (has_ipv6)
|
|
|
|
event_del(&e6);
|
|
|
|
if (has_siginfo)
|
|
|
|
signal_del(&siginfo);
|
|
|
|
signal_del(&sigusr2);
|
|
|
|
signal_del(&sighup);
|
2021-02-08 11:01:45 +01:00
|
|
|
}
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
static void
|
|
|
|
handle_siginfo(int fd, short ev, void *d)
|
|
|
|
{
|
|
|
|
(void)fd;
|
|
|
|
(void)ev;
|
|
|
|
(void)d;
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
log_info(NULL, "%d connected clients", connected_clients);
|
|
|
|
}
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
void
|
|
|
|
loop(struct tls *ctx, int sock4, int sock6)
|
|
|
|
{
|
|
|
|
struct server server;
|
|
|
|
size_t i;
|
2021-02-03 22:14:48 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
event_init();
|
2021-01-18 00:23:58 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
memset(&server, 0, sizeof(server));
|
|
|
|
for (i = 0; i < MAX_USERS; ++i)
|
|
|
|
server.clients[i].fd = -1;
|
2021-02-04 14:23:15 +01:00
|
|
|
|
2021-02-12 09:50:25 +01:00
|
|
|
event_set(&e4, sock4, EV_READ | EV_PERSIST, &do_accept, &server);
|
|
|
|
event_add(&e4, NULL);
|
2021-02-04 14:23:15 +01:00
|
|
|
|
2021-02-08 11:01:45 +01:00
|
|
|
if (sock6 != -1) {
|
2021-02-12 09:50:25 +01:00
|
|
|
has_ipv6 = 1;
|
|
|
|
event_set(&e6, sock6, EV_READ | EV_PERSIST, &do_accept, &server);
|
|
|
|
event_add(&e6, NULL);
|
2021-01-18 00:23:58 +01:00
|
|
|
}
|
2021-02-08 11:01:45 +01:00
|
|
|
|
2021-02-12 09:50:25 +01:00
|
|
|
signal_set(&sighup, SIGHUP, &handle_sighup, NULL);
|
|
|
|
signal_add(&sighup, NULL);
|
2021-02-08 11:01:45 +01:00
|
|
|
|
|
|
|
#ifdef SIGINFO
|
2021-02-12 09:50:25 +01:00
|
|
|
has_siginfo = 1;
|
|
|
|
signal_set(&siginfo, SIGINFO, &handle_siginfo, NULL);
|
|
|
|
signal_add(&siginfo, NULL);
|
2021-02-08 11:01:45 +01:00
|
|
|
#endif
|
2021-02-12 12:34:17 +01:00
|
|
|
signal_set(&sigusr2, SIGUSR2, &handle_siginfo, NULL);
|
2021-02-12 09:50:25 +01:00
|
|
|
signal_add(&sigusr2, NULL);
|
2021-02-08 11:01:45 +01:00
|
|
|
|
|
|
|
server.ctx = ctx;
|
|
|
|
|
2021-02-08 13:46:46 +01:00
|
|
|
sandbox();
|
2021-02-08 11:01:45 +01:00
|
|
|
event_dispatch();
|
2021-02-08 13:46:46 +01:00
|
|
|
_exit(0);
|
2021-01-18 00:23:58 +01:00
|
|
|
}
|