2018-06-28 13:27:44 +02:00
|
|
|
/*
|
2021-01-11 23:46:08 +01:00
|
|
|
* Copyright (c) 2019-2021 Joris Vink <joris@coders.se>
|
2018-06-28 13:27:44 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "kore.h"
|
|
|
|
|
|
|
|
#if !defined(KORE_NO_HTTP)
|
|
|
|
|
|
|
|
#include "http.h"
|
|
|
|
|
|
|
|
struct filemap_entry {
|
|
|
|
char *root;
|
|
|
|
size_t root_len;
|
|
|
|
struct kore_domain *domain;
|
|
|
|
char *ondisk;
|
2018-07-08 17:40:16 +02:00
|
|
|
size_t ondisk_len;
|
2018-06-28 13:27:44 +02:00
|
|
|
TAILQ_ENTRY(filemap_entry) list;
|
|
|
|
};
|
|
|
|
|
|
|
|
int filemap_resolve(struct http_request *);
|
|
|
|
|
|
|
|
static void filemap_serve(struct http_request *, struct filemap_entry *);
|
|
|
|
|
|
|
|
static TAILQ_HEAD(, filemap_entry) maps;
|
|
|
|
|
2018-07-03 19:58:43 +02:00
|
|
|
char *kore_filemap_ext = NULL;
|
2018-06-28 23:00:42 +02:00
|
|
|
char *kore_filemap_index = NULL;
|
|
|
|
|
2018-06-28 13:27:44 +02:00
|
|
|
void
|
|
|
|
kore_filemap_init(void)
|
|
|
|
{
|
|
|
|
TAILQ_INIT(&maps);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kore_filemap_create(struct kore_domain *dom, const char *path, const char *root)
|
|
|
|
{
|
2018-07-17 15:05:20 +02:00
|
|
|
size_t sz;
|
|
|
|
struct stat st;
|
|
|
|
int len;
|
|
|
|
struct kore_module_handle *hdlr;
|
|
|
|
struct filemap_entry *entry;
|
|
|
|
char regex[1024], fpath[PATH_MAX];
|
2018-06-28 13:27:44 +02:00
|
|
|
|
|
|
|
sz = strlen(root);
|
|
|
|
if (sz == 0)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
if (root[0] != '/' || root[sz - 1] != '/')
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
Rework worker startup/privsep config.
Starting with the privsep config, this commit changes the following:
- Removes the root, runas, keymgr_root, keymgr_runas, acme_root and
acme_runas configuration options.
Instead these are now configured via a privsep configuration context:
privsep worker {
root /tmp
runas nobody
}
This is also configurable via Python using the new kore.privsep() method:
kore.privsep("worker", root="/tmp", runas="nobody", skip=["chroot"])
Tied into this we also better handle worker startup:
- Per worker process, wait until it signalled it is ready.
- If a worker fails at startup, display its last log lines more clearly.
- Don't start acme process if no domain requires acme.
- Remove each process its individual startup log message in favour
of a generalized one that displays its PID, root and user.
- At startup, log the kore version and built-ins in a nicer way.
- The worker processes now check things they need to start running
before signaling they are ready (such as access to CA certs for
TLS client authentication).
2021-09-07 21:59:22 +02:00
|
|
|
if (worker_privsep.root != NULL) {
|
2018-07-11 11:08:44 +02:00
|
|
|
len = snprintf(fpath, sizeof(fpath), "%s/%s",
|
Rework worker startup/privsep config.
Starting with the privsep config, this commit changes the following:
- Removes the root, runas, keymgr_root, keymgr_runas, acme_root and
acme_runas configuration options.
Instead these are now configured via a privsep configuration context:
privsep worker {
root /tmp
runas nobody
}
This is also configurable via Python using the new kore.privsep() method:
kore.privsep("worker", root="/tmp", runas="nobody", skip=["chroot"])
Tied into this we also better handle worker startup:
- Per worker process, wait until it signalled it is ready.
- If a worker fails at startup, display its last log lines more clearly.
- Don't start acme process if no domain requires acme.
- Remove each process its individual startup log message in favour
of a generalized one that displays its PID, root and user.
- At startup, log the kore version and built-ins in a nicer way.
- The worker processes now check things they need to start running
before signaling they are ready (such as access to CA certs for
TLS client authentication).
2021-09-07 21:59:22 +02:00
|
|
|
worker_privsep.root, path);
|
2018-07-11 11:08:44 +02:00
|
|
|
if (len == -1 || (size_t)len >= sizeof(fpath))
|
|
|
|
fatal("kore_filemap_create: failed to concat paths");
|
|
|
|
} else {
|
|
|
|
if (kore_strlcpy(fpath, path, sizeof(fpath)) >= sizeof(fpath))
|
|
|
|
fatal("kore_filemap_create: failed to copy path");
|
|
|
|
}
|
2018-07-11 09:44:29 +02:00
|
|
|
|
|
|
|
if (stat(fpath, &st) == -1)
|
2018-07-08 17:51:35 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
2018-06-28 13:27:44 +02:00
|
|
|
len = snprintf(regex, sizeof(regex), "^%s.*$", root);
|
|
|
|
if (len == -1 || (size_t)len >= sizeof(regex))
|
|
|
|
fatal("kore_filemap_create: buffer too small");
|
|
|
|
|
2019-09-27 12:22:35 +02:00
|
|
|
if (!kore_module_handler_new(dom, regex, "filemap_resolve",
|
|
|
|
NULL, HANDLER_TYPE_DYNAMIC))
|
2018-06-28 13:27:44 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
2018-07-17 15:05:20 +02:00
|
|
|
hdlr = NULL;
|
|
|
|
TAILQ_FOREACH(hdlr, &dom->handlers, list) {
|
|
|
|
if (!strcmp(hdlr->path, regex))
|
|
|
|
break;
|
|
|
|
}
|
2018-06-28 13:27:44 +02:00
|
|
|
|
2018-07-17 15:05:20 +02:00
|
|
|
if (hdlr == NULL)
|
|
|
|
fatal("couldn't find newly created handler for filemap");
|
|
|
|
|
|
|
|
hdlr->methods = HTTP_METHOD_GET | HTTP_METHOD_HEAD;
|
|
|
|
|
|
|
|
entry = kore_calloc(1, sizeof(*entry));
|
2018-06-28 13:27:44 +02:00
|
|
|
entry->domain = dom;
|
|
|
|
entry->root_len = sz;
|
|
|
|
entry->root = kore_strdup(root);
|
2018-07-08 17:51:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Resolve the ondisk component inside the workers to make sure
|
|
|
|
* realpath() resolves the correct path (they maybe chrooted).
|
|
|
|
*/
|
|
|
|
entry->ondisk_len = strlen(path);
|
|
|
|
entry->ondisk = kore_strdup(path);
|
2018-06-28 13:27:44 +02:00
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&maps, entry, list);
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2018-07-08 17:51:35 +02:00
|
|
|
void
|
|
|
|
kore_filemap_resolve_paths(void)
|
|
|
|
{
|
|
|
|
struct filemap_entry *entry;
|
|
|
|
char rpath[PATH_MAX];
|
|
|
|
|
|
|
|
TAILQ_FOREACH(entry, &maps, list) {
|
|
|
|
if (realpath(entry->ondisk, rpath) == NULL)
|
|
|
|
fatal("realpath(%s): %s", entry->ondisk, errno_s);
|
|
|
|
|
|
|
|
kore_free(entry->ondisk);
|
|
|
|
entry->ondisk_len = strlen(rpath);
|
|
|
|
entry->ondisk = kore_strdup(rpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 13:27:44 +02:00
|
|
|
int
|
|
|
|
filemap_resolve(struct http_request *req)
|
|
|
|
{
|
|
|
|
size_t best_len;
|
|
|
|
struct filemap_entry *entry, *best;
|
|
|
|
|
2018-06-28 14:24:02 +02:00
|
|
|
if (req->method != HTTP_METHOD_GET &&
|
|
|
|
req->method != HTTP_METHOD_HEAD) {
|
|
|
|
http_response_header(req, "allow", "get, head");
|
|
|
|
http_response(req, HTTP_STATUS_BAD_REQUEST, NULL, 0);
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2018-06-28 13:27:44 +02:00
|
|
|
best = NULL;
|
|
|
|
best_len = 0;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(entry, &maps, list) {
|
2018-07-04 14:26:38 +02:00
|
|
|
if (entry->domain != req->hdlr->dom)
|
|
|
|
continue;
|
|
|
|
|
2018-06-28 13:27:44 +02:00
|
|
|
if (!strncmp(entry->root, req->path, entry->root_len)) {
|
|
|
|
if (best == NULL || entry->root_len > best_len) {
|
|
|
|
best = entry;
|
|
|
|
best_len = entry->root_len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (best == NULL) {
|
|
|
|
http_response(req, HTTP_STATUS_NOT_FOUND, NULL, 0);
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
filemap_serve(req, best);
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
filemap_serve(struct http_request *req, struct filemap_entry *map)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2019-09-27 12:22:35 +02:00
|
|
|
struct connection *c;
|
2018-06-28 13:27:44 +02:00
|
|
|
struct kore_fileref *ref;
|
2019-09-27 20:00:35 +02:00
|
|
|
struct kore_server *srv;
|
2018-07-03 19:58:43 +02:00
|
|
|
const char *path;
|
2018-06-28 23:00:42 +02:00
|
|
|
int len, fd, index;
|
2018-07-08 17:40:16 +02:00
|
|
|
char fpath[PATH_MAX], rpath[PATH_MAX];
|
2018-06-28 13:27:44 +02:00
|
|
|
|
2018-07-03 19:58:43 +02:00
|
|
|
path = req->path + map->root_len;
|
|
|
|
|
|
|
|
len = snprintf(fpath, sizeof(fpath), "%s/%s", map->ondisk, path);
|
2018-06-28 13:27:44 +02:00
|
|
|
if (len == -1 || (size_t)len >= sizeof(fpath)) {
|
|
|
|
http_response(req, HTTP_STATUS_INTERNAL_ERROR, NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-28 15:28:25 +02:00
|
|
|
if (!http_argument_urldecode(fpath)) {
|
|
|
|
http_response(req, HTTP_STATUS_BAD_REQUEST, NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-08 17:40:16 +02:00
|
|
|
index = 0;
|
|
|
|
|
|
|
|
lookup:
|
2018-07-08 17:57:00 +02:00
|
|
|
if (realpath(fpath, rpath) == NULL) {
|
|
|
|
if (errno == ENOENT) {
|
2018-07-08 17:58:56 +02:00
|
|
|
if (index == 0 && kore_filemap_ext != NULL) {
|
2018-07-08 17:57:00 +02:00
|
|
|
len = snprintf(fpath, sizeof(fpath),
|
|
|
|
"%s/%s%s", map->ondisk, path,
|
|
|
|
kore_filemap_ext);
|
|
|
|
if (len == -1 ||
|
|
|
|
(size_t)len >= sizeof(fpath)) {
|
|
|
|
http_response(req,
|
|
|
|
HTTP_STATUS_INTERNAL_ERROR,
|
|
|
|
NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
goto lookup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
http_response(req, HTTP_STATUS_NOT_FOUND, NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(rpath, fpath, map->ondisk_len)) {
|
2018-06-28 15:28:25 +02:00
|
|
|
http_response(req, HTTP_STATUS_NOT_FOUND, NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-27 12:22:35 +02:00
|
|
|
c = req->owner;
|
2019-09-27 20:00:35 +02:00
|
|
|
srv = c->owner->server;
|
2019-09-27 12:22:35 +02:00
|
|
|
|
2019-09-27 20:00:35 +02:00
|
|
|
if ((ref = kore_fileref_get(rpath, srv->tls)) == NULL) {
|
2018-06-28 13:27:44 +02:00
|
|
|
if ((fd = open(fpath, O_RDONLY | O_NOFOLLOW)) == -1) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
2018-07-03 19:58:43 +02:00
|
|
|
if (index || kore_filemap_ext == NULL) {
|
|
|
|
req->status = HTTP_STATUS_NOT_FOUND;
|
|
|
|
} else {
|
|
|
|
len = snprintf(fpath, sizeof(fpath),
|
|
|
|
"%s/%s%s", map->ondisk, path,
|
|
|
|
kore_filemap_ext);
|
|
|
|
if (len == -1 ||
|
|
|
|
(size_t)len >= sizeof(fpath)) {
|
|
|
|
http_response(req,
|
|
|
|
HTTP_STATUS_INTERNAL_ERROR,
|
|
|
|
NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
goto lookup;
|
|
|
|
}
|
2018-06-28 13:27:44 +02:00
|
|
|
break;
|
|
|
|
case EPERM:
|
|
|
|
case EACCES:
|
|
|
|
req->status = HTTP_STATUS_FORBIDDEN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
req->status = HTTP_STATUS_INTERNAL_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_response(req, req->status, NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fd, &st) == -1) {
|
|
|
|
http_response(req, HTTP_STATUS_INTERNAL_ERROR, NULL, 0);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISREG(st.st_mode)) {
|
|
|
|
if (st.st_size <= 0) {
|
|
|
|
http_response(req,
|
|
|
|
HTTP_STATUS_NOT_FOUND, NULL, 0);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* kore_fileref_create() takes ownership of the fd. */
|
2019-09-27 20:00:35 +02:00
|
|
|
ref = kore_fileref_create(srv, fpath, fd,
|
2018-07-24 19:56:36 +02:00
|
|
|
st.st_size, &st.st_mtim);
|
2018-06-28 13:27:44 +02:00
|
|
|
if (ref == NULL) {
|
|
|
|
http_response(req,
|
|
|
|
HTTP_STATUS_INTERNAL_ERROR, NULL, 0);
|
|
|
|
} else {
|
|
|
|
fd = -1;
|
|
|
|
}
|
2018-06-28 23:00:42 +02:00
|
|
|
} else if (S_ISDIR(st.st_mode) && index == 0) {
|
2018-12-17 16:22:36 +01:00
|
|
|
close(fd);
|
2018-07-09 06:28:28 +02:00
|
|
|
if (req->path[strlen(req->path) - 1] != '/') {
|
|
|
|
(void)snprintf(fpath,
|
|
|
|
sizeof(fpath), "%s/", req->path);
|
|
|
|
http_response_header(req, "location", fpath);
|
|
|
|
http_response(req, HTTP_STATUS_FOUND, NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-28 23:00:42 +02:00
|
|
|
len = snprintf(fpath, sizeof(fpath),
|
2018-07-03 19:58:43 +02:00
|
|
|
"%s/%s%s", map->ondisk, path,
|
2018-06-28 23:00:42 +02:00
|
|
|
kore_filemap_index != NULL ?
|
|
|
|
kore_filemap_index : "index.html");
|
|
|
|
if (len == -1 || (size_t)len >= sizeof(fpath)) {
|
|
|
|
http_response(req,
|
|
|
|
HTTP_STATUS_INTERNAL_ERROR, NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
goto lookup;
|
2018-06-28 13:27:44 +02:00
|
|
|
} else {
|
|
|
|
http_response(req, HTTP_STATUS_NOT_FOUND, NULL, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ref != NULL) {
|
|
|
|
http_response_fileref(req, HTTP_STATUS_OK, ref);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|