commit 5eb2160269e4ba620417df0213363303161ac9fa
parent 10cf14f75693a9a78b4a9aa7aeb280c7a06aefb0
Author: Joris Vink <joris@coders.se>
Date: Sun, 8 Jul 2018 17:51:35 +0200
resolve filemap paths after workers chrooted.
otherwise the paths inside chrooted workers are incorrect.
Diffstat:
3 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/include/kore/kore.h b/include/kore/kore.h
@@ -647,6 +647,7 @@ int kore_msg_register(u_int8_t,
#if !defined(KORE_NO_HTTP)
void kore_filemap_init(void);
+void kore_filemap_resolve_paths(void);
int kore_filemap_create(struct kore_domain *, const char *,
const char *);
extern char *kore_filemap_ext;
diff --git a/src/filemap.c b/src/filemap.c
@@ -56,9 +56,10 @@ int
kore_filemap_create(struct kore_domain *dom, const char *path, const char *root)
{
size_t sz;
+ struct stat st;
int len;
struct filemap_entry *entry;
- char regex[1024], rpath[PATH_MAX];
+ char regex[1024];
sz = strlen(root);
if (sz == 0)
@@ -67,6 +68,9 @@ kore_filemap_create(struct kore_domain *dom, const char *path, const char *root)
if (root[0] != '/' || root[sz - 1] != '/')
return (KORE_RESULT_ERROR);
+ if (stat(path, &st) == -1)
+ return (KORE_RESULT_ERROR);
+
len = snprintf(regex, sizeof(regex), "^%s.*$", root);
if (len == -1 || (size_t)len >= sizeof(regex))
fatal("kore_filemap_create: buffer too small");
@@ -75,22 +79,40 @@ kore_filemap_create(struct kore_domain *dom, const char *path, const char *root)
"filemap_resolve", NULL, HANDLER_TYPE_DYNAMIC))
return (KORE_RESULT_ERROR);
- if (realpath(path, rpath) == NULL)
- return (KORE_RESULT_ERROR);
-
entry = kore_calloc(1, sizeof(*entry));
entry->domain = dom;
entry->root_len = sz;
entry->root = kore_strdup(root);
- entry->ondisk_len = strlen(rpath);
- entry->ondisk = kore_strdup(rpath);
+
+ /*
+ * 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);
TAILQ_INSERT_TAIL(&maps, entry, list);
return (KORE_RESULT_OK);
}
+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);
+ }
+}
+
int
filemap_resolve(struct http_request *req)
{
diff --git a/src/worker.c b/src/worker.c
@@ -314,6 +314,7 @@ kore_worker_entry(struct kore_worker *kw)
net_init();
#if !defined(KORE_NO_HTTP)
http_init();
+ kore_filemap_resolve_paths();
kore_accesslog_worker_init();
#endif
kore_timer_init();