2020-08-25 19:08:23 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <dirent.h>
|
2020-08-29 18:36:10 +00:00
|
|
|
#include <errno.h>
|
2020-08-25 19:08:23 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/sendfile.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-08-27 22:40:04 +00:00
|
|
|
#include "config.h"
|
2020-08-25 19:08:23 +00:00
|
|
|
#include "dirl.h"
|
|
|
|
#include "http.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2020-08-27 22:40:04 +00:00
|
|
|
static char*
|
|
|
|
suffix(int t)
|
|
|
|
{
|
2020-08-25 19:08:23 +00:00
|
|
|
switch (t) {
|
2020-08-27 22:40:04 +00:00
|
|
|
case DT_FIFO:
|
|
|
|
return "|";
|
|
|
|
case DT_DIR:
|
|
|
|
return "/";
|
|
|
|
case DT_LNK:
|
|
|
|
return "@";
|
|
|
|
case DT_SOCK:
|
|
|
|
return "=";
|
2020-08-25 19:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-08-29 18:36:10 +00:00
|
|
|
/* Try to find templates up until root
|
|
|
|
*
|
|
|
|
* Iterates the directory hierarchy upwards. Returns the closest path containing
|
2020-08-30 06:43:10 +00:00
|
|
|
* one of the template files or NULL if none could be found.
|
2020-08-29 18:36:10 +00:00
|
|
|
*
|
|
|
|
* Note that we are chrooted.
|
|
|
|
*/
|
|
|
|
static char*
|
|
|
|
dirl_find_templ_dir(const char* start_path)
|
2020-08-27 22:40:04 +00:00
|
|
|
{
|
2020-08-29 18:36:10 +00:00
|
|
|
char* path_buf = calloc(sizeof(char), strlen(start_path));
|
|
|
|
strcat(path_buf, start_path);
|
|
|
|
|
2020-08-30 07:38:02 +00:00
|
|
|
if(path_buf[strlen(path_buf)-1] == '/') {
|
|
|
|
// don't read last dir twice
|
|
|
|
path_buf[strlen(path_buf)-1] = '\0';
|
|
|
|
}
|
|
|
|
|
2020-08-29 18:36:10 +00:00
|
|
|
while (strlen(path_buf) != 0) {
|
|
|
|
DIR* cur = opendir(path_buf);
|
|
|
|
struct dirent* de;
|
|
|
|
errno = 0;
|
|
|
|
while ((de = readdir(cur))) {
|
|
|
|
if (de->d_type == DT_REG) {
|
|
|
|
if (strcmp(DIRL_HEADER, de->d_name) || strcmp(DIRL_ENTRY, de->d_name) ||
|
|
|
|
strcmp(DIRL_FOOTER, de->d_name)) {
|
|
|
|
closedir(cur);
|
|
|
|
return path_buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-30 07:38:02 +00:00
|
|
|
if (strlen(path_buf) > 1) {
|
|
|
|
char* parent = strrchr(path_buf, '/');
|
|
|
|
(*parent) = '\0'; // strip tail from path_buf
|
|
|
|
|
|
|
|
if(strlen(path_buf) == 0) { // we stripped root, let it loop once more
|
|
|
|
path_buf[0] = '/';
|
|
|
|
path_buf[1] = '\0';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
path_buf[0] = '\0'; // we checked root, now terminate loop
|
|
|
|
}
|
2020-08-25 19:08:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 18:36:10 +00:00
|
|
|
free(path_buf);
|
|
|
|
return NULL;
|
2020-08-25 19:08:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-30 06:43:10 +00:00
|
|
|
/* Helper function to fill template from base+name if file exists */
|
|
|
|
static void
|
|
|
|
dirl_fill_templ(char** templ, char* base, char* name, char* def)
|
|
|
|
{
|
2020-08-30 07:38:02 +00:00
|
|
|
if (!base || !name) {
|
|
|
|
*templ = def;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-30 06:43:10 +00:00
|
|
|
char* path = calloc(sizeof(char), strlen(base) + strlen(name));
|
|
|
|
strcpy(path, base);
|
|
|
|
strcat(path, name);
|
|
|
|
|
|
|
|
char* file_buf = read_file(path);
|
|
|
|
free(path);
|
|
|
|
|
|
|
|
if (file_buf) {
|
|
|
|
*templ = file_buf;
|
|
|
|
} else {
|
|
|
|
*templ = def;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 18:36:10 +00:00
|
|
|
struct dirl_templ
|
|
|
|
dirl_read_templ(const char* path)
|
2020-08-25 19:08:23 +00:00
|
|
|
{
|
2020-08-30 06:43:10 +00:00
|
|
|
struct dirl_templ templ;
|
2020-08-29 18:36:10 +00:00
|
|
|
|
|
|
|
char* templ_dir = dirl_find_templ_dir(path);
|
|
|
|
|
2020-08-30 06:43:10 +00:00
|
|
|
dirl_fill_templ(&templ.header, templ_dir, DIRL_HEADER, DIRL_HEADER_DEFAULT);
|
|
|
|
dirl_fill_templ(&templ.entry, templ_dir, DIRL_ENTRY, DIRL_ENTRY_DEFAULT);
|
|
|
|
dirl_fill_templ(&templ.footer, templ_dir, DIRL_FOOTER, DIRL_FOOTER_DEFAULT);
|
2020-08-29 18:36:10 +00:00
|
|
|
|
|
|
|
free(templ_dir);
|
|
|
|
|
2020-08-30 06:43:10 +00:00
|
|
|
return templ;
|
2020-08-29 18:36:10 +00:00
|
|
|
}
|
2020-08-25 19:08:23 +00:00
|
|
|
|
2020-08-29 18:36:10 +00:00
|
|
|
enum status
|
|
|
|
dirl_header(int fd, const struct response* res, const struct dirl_templ* templ)
|
|
|
|
{
|
2020-08-25 19:08:23 +00:00
|
|
|
/* Replace placeholder */
|
2020-08-30 06:43:10 +00:00
|
|
|
char* nhead = calloc(sizeof(char), strlen(templ->header));
|
|
|
|
memcpy(nhead, templ->header, strlen(templ->header));
|
2020-08-30 07:38:02 +00:00
|
|
|
replace(&nhead, "{uri}", res->uri);
|
2020-08-25 19:08:23 +00:00
|
|
|
|
|
|
|
/* Write header */
|
|
|
|
write(fd, nhead, strlen(nhead));
|
|
|
|
|
|
|
|
free(nhead);
|
|
|
|
|
|
|
|
/* listing header */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum status
|
2020-08-29 18:36:10 +00:00
|
|
|
dirl_entry(int fd, const struct dirent* entry, const struct dirl_templ* templ)
|
2020-08-27 22:40:04 +00:00
|
|
|
{
|
|
|
|
/* Replace placeholder */
|
2020-08-30 06:43:10 +00:00
|
|
|
char* nentry = calloc(sizeof(char), strlen(templ->entry));
|
|
|
|
memcpy(nentry, templ->entry, strlen(templ->entry));
|
|
|
|
replace(&nentry, "{entry}", entry->d_name);
|
|
|
|
replace(&nentry, "{suffix}", suffix(entry->d_type));
|
2020-08-27 22:40:04 +00:00
|
|
|
|
2020-08-25 19:08:23 +00:00
|
|
|
/* Write entry */
|
2020-08-27 22:40:04 +00:00
|
|
|
write(fd, nentry, strlen(nentry));
|
|
|
|
|
|
|
|
free(nentry);
|
2020-08-25 19:08:23 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum status
|
2020-08-29 18:36:10 +00:00
|
|
|
dirl_footer(int fd, const struct dirl_templ* templ)
|
2020-08-27 22:40:04 +00:00
|
|
|
{
|
|
|
|
/* Replace placeholder */
|
2020-08-30 06:43:10 +00:00
|
|
|
char* nfoot = calloc(sizeof(char), strlen(templ->footer));
|
|
|
|
memcpy(nfoot, templ->footer, strlen(templ->footer));
|
|
|
|
replace(&nfoot, "{idx}", "something");
|
2020-08-27 22:40:04 +00:00
|
|
|
|
2020-08-25 19:08:23 +00:00
|
|
|
/* Write footer */
|
2020-08-27 22:40:04 +00:00
|
|
|
write(fd, nfoot, strlen(nfoot));
|
2020-08-25 19:08:23 +00:00
|
|
|
|
2020-08-27 22:40:04 +00:00
|
|
|
free(nfoot);
|
|
|
|
return 0;
|
2020-08-25 19:08:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 22:40:04 +00:00
|
|
|
int
|
|
|
|
dirl_skip(const char* name)
|
|
|
|
{
|
2020-08-29 18:36:10 +00:00
|
|
|
return name[0] == '.' //
|
|
|
|
|| !strcmp(name, DIRL_HEADER) //
|
2020-08-27 22:40:04 +00:00
|
|
|
|| !strcmp(name, DIRL_ENTRY) //
|
|
|
|
|| !strcmp(name, DIRL_FOOTER) //
|
2020-08-29 18:36:10 +00:00
|
|
|
|| !strcmp(name, DIRL_STYLE); //
|
2020-08-25 19:08:23 +00:00
|
|
|
}
|