2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "base.h"
|
2019-10-26 21:58:30 +00:00
|
|
|
#include "array.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "buffer.h"
|
2019-10-26 21:58:30 +00:00
|
|
|
#include "log.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "response.h"
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <sys/types.h>
|
2018-03-25 07:45:05 +00:00
|
|
|
#include <sys/stat.h>
|
2009-10-11 14:31:42 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2009-10-11 14:31:42 +00:00
|
|
|
# include <pwd.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct {
|
2019-10-26 21:58:30 +00:00
|
|
|
const array *exclude_user;
|
|
|
|
const array *include_user;
|
|
|
|
const buffer *path;
|
|
|
|
const buffer *basepath;
|
|
|
|
unsigned short letterhomes;
|
|
|
|
unsigned short active;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-10-26 21:58:30 +00:00
|
|
|
PLUGIN_DATA;
|
|
|
|
plugin_config defaults;
|
|
|
|
plugin_config conf;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
INIT_FUNC(mod_userdir_init) {
|
2019-10-26 21:58:30 +00:00
|
|
|
return calloc(1, sizeof(plugin_data));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 21:58:30 +00:00
|
|
|
static void mod_userdir_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
|
|
|
|
switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
|
|
|
|
case 0: /* userdir.path */
|
|
|
|
pconf->path = cpv->v.b;
|
|
|
|
break;
|
|
|
|
case 1: /* userdir.exclude-user */
|
|
|
|
pconf->exclude_user = cpv->v.a;
|
|
|
|
break;
|
|
|
|
case 2: /* userdir.include-user */
|
|
|
|
pconf->include_user = cpv->v.a;
|
|
|
|
break;
|
|
|
|
case 3: /* userdir.basepath */
|
|
|
|
pconf->basepath = cpv->v.b;
|
|
|
|
break;
|
|
|
|
case 4: /* userdir.letterhomes */
|
|
|
|
pconf->letterhomes = cpv->v.u;
|
|
|
|
break;
|
|
|
|
case 5: /* userdir.active */
|
|
|
|
pconf->active = cpv->v.u;
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-26 21:58:30 +00:00
|
|
|
static void mod_userdir_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
|
|
|
|
do {
|
|
|
|
mod_userdir_merge_config_cpv(pconf, cpv);
|
|
|
|
} while ((++cpv)->k_id != -1);
|
|
|
|
}
|
2015-05-14 09:38:33 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void mod_userdir_patch_config(request_st * const r, plugin_data * const p) {
|
2019-10-26 21:58:30 +00:00
|
|
|
memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
|
|
|
|
for (int i = 1, used = p->nconfig; i < used; ++i) {
|
2020-01-13 02:51:12 +00:00
|
|
|
if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
|
2019-10-26 21:58:30 +00:00
|
|
|
mod_userdir_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-26 21:58:30 +00:00
|
|
|
SETDEFAULTS_FUNC(mod_userdir_set_defaults) {
|
|
|
|
static const config_plugin_keys_t cpk[] = {
|
|
|
|
{ CONST_STR_LEN("userdir.path"),
|
|
|
|
T_CONFIG_STRING,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("userdir.exclude-user"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_VLIST,
|
2019-10-26 21:58:30 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("userdir.include-user"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_VLIST,
|
2019-10-26 21:58:30 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("userdir.basepath"),
|
|
|
|
T_CONFIG_STRING,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("userdir.letterhomes"),
|
|
|
|
T_CONFIG_BOOL,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("userdir.active"),
|
|
|
|
T_CONFIG_BOOL,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ NULL, 0,
|
|
|
|
T_CONFIG_UNSET,
|
|
|
|
T_CONFIG_SCOPE_UNSET }
|
|
|
|
};
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-26 21:58:30 +00:00
|
|
|
plugin_data * const p = p_d;
|
|
|
|
if (!config_plugin_values_init(srv, p, cpk, "mod_userdir"))
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
|
|
|
|
/* enabled by default for backward compatibility;
|
|
|
|
* if userdir.path isn't set userdir is disabled too,
|
|
|
|
* but you can't disable it by setting it to an empty string. */
|
|
|
|
p->defaults.active = 1;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2019-10-26 21:58:30 +00:00
|
|
|
/* initialize p->defaults from global config context */
|
|
|
|
if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
|
|
|
|
const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
|
|
|
|
if (-1 != cpv->k_id)
|
|
|
|
mod_userdir_merge_config(&p->defaults, cpv);
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2019-10-26 21:58:30 +00:00
|
|
|
return HANDLER_GO_ON;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 21:27:20 +00:00
|
|
|
static int mod_userdir_in_vlist_nc(const array * const a, const char * const k, const size_t klen) {
|
|
|
|
for (uint32_t i = 0, used = a->used; i < used; ++i) {
|
|
|
|
const data_string * const ds = (const data_string *)a->data[i];
|
|
|
|
if (buffer_eq_icase_slen(&ds->value, k, klen)) return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-26 21:27:20 +00:00
|
|
|
static int mod_userdir_in_vlist(const array * const a, const char * const k, const size_t klen) {
|
|
|
|
for (uint32_t i = 0, used = a->used; i < used; ++i) {
|
|
|
|
const data_string * const ds = (const data_string *)a->data[i];
|
|
|
|
if (buffer_eq_slen(&ds->value, k, klen)) return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2005-10-31 07:58:17 +00:00
|
|
|
|
2019-10-26 21:27:20 +00:00
|
|
|
__attribute_noinline__
|
2020-01-13 02:51:12 +00:00
|
|
|
static handler_t mod_userdir_docroot_construct(request_st * const r, plugin_data * const p, const char * const uptr, const size_t ulen) {
|
2019-10-26 21:27:20 +00:00
|
|
|
char u[256];
|
|
|
|
if (ulen >= sizeof(u)) return HANDLER_GO_ON;
|
|
|
|
|
|
|
|
memcpy(u, uptr, ulen);
|
|
|
|
u[ulen] = '\0';
|
|
|
|
|
|
|
|
/* we build the physical path */
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer * const b = r->tmp_buf;
|
2019-10-26 21:27:20 +00:00
|
|
|
|
|
|
|
if (buffer_string_is_empty(p->conf.basepath)) {
|
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
/* XXX: future: might add cache; getpwnam() lookup is expensive */
|
|
|
|
struct passwd *pwd = getpwnam(u);
|
|
|
|
if (pwd) {
|
|
|
|
struct stat st;
|
|
|
|
buffer_copy_string(b, pwd->pw_dir);
|
|
|
|
buffer_append_path_len(b, CONST_BUF_LEN(p->conf.path));
|
|
|
|
if (0 != stat(b->ptr, &st) || !S_ISDIR(st.st_mode)) {
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* user not found */
|
|
|
|
#endif
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
} else {
|
|
|
|
/* check if the username is valid
|
|
|
|
* a request for /~../ should lead to a directory traversal
|
|
|
|
* limiting to [-_a-z0-9.] should fix it */
|
|
|
|
if (ulen <= 2 && (u[0] == '.' && (1 == ulen || u[1] == '.'))) {
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ulen; ++i) {
|
|
|
|
const int c = u[i];
|
|
|
|
if (!(light_isalnum(c) || c == '-' || c == '_' || c == '.')) {
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->conf.force_lowercase_filenames) {
|
2019-10-26 21:27:20 +00:00
|
|
|
for (size_t i = 0; i < ulen; ++i) {
|
2020-09-10 04:15:29 +00:00
|
|
|
if (light_isupper(u[i])) u[i] |= 0x20;
|
2019-10-26 21:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_copy_buffer(b, p->conf.basepath);
|
|
|
|
if (p->conf.letterhomes) {
|
|
|
|
if (u[0] == '.') return HANDLER_GO_ON;
|
|
|
|
buffer_append_path_len(b, u, 1);
|
|
|
|
}
|
|
|
|
buffer_append_path_len(b, u, ulen);
|
|
|
|
buffer_append_path_len(b, CONST_BUF_LEN(p->conf.path));
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_copy_buffer(&r->physical.basedir, b);
|
|
|
|
buffer_copy_buffer(&r->physical.path, b);
|
2019-10-26 21:27:20 +00:00
|
|
|
|
|
|
|
/* the physical rel_path is basically the same as uri.path;
|
|
|
|
* but it is converted to lowercase in case of force_lowercase_filenames
|
|
|
|
* and some special handling for trailing '.', ' ' and '/' on windows
|
|
|
|
* we assume that no docroot/physical handler changed this
|
2020-02-22 22:24:12 +00:00
|
|
|
* (docroot should only set the docroot/server name, physical should only
|
2019-10-26 21:27:20 +00:00
|
|
|
* change the physical.path;
|
|
|
|
* the exception mod_secdownload doesn't work with userdir anyway)
|
|
|
|
*/
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_append_slash(&r->physical.path);
|
2019-10-26 21:27:20 +00:00
|
|
|
/* if no second '/' is found, we assume that it was stripped from the
|
|
|
|
* uri.path for the special handling on windows. we do not care about the
|
|
|
|
* trailing slash here on windows, as we already ensured it is a directory
|
|
|
|
*
|
|
|
|
* TODO: what to do with trailing dots in usernames on windows?
|
|
|
|
* they may result in the same directory as a username without them.
|
|
|
|
*/
|
|
|
|
char *rel_url;
|
2020-01-13 02:51:12 +00:00
|
|
|
if (NULL != (rel_url = strchr(r->physical.rel_path.ptr + 2, '/'))) {
|
|
|
|
buffer_append_string(&r->physical.path, rel_url + 1); /* skip the / */
|
2019-10-26 21:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
2005-10-31 07:58:17 +00:00
|
|
|
|
2019-10-26 21:27:20 +00:00
|
|
|
URIHANDLER_FUNC(mod_userdir_docroot_handler) {
|
|
|
|
/* /~user/foo.html -> /home/user/public_html/foo.html */
|
|
|
|
|
2020-01-17 06:03:06 +00:00
|
|
|
#ifdef __COVERITY__
|
2020-01-13 02:51:12 +00:00
|
|
|
if (buffer_is_empty(&r->uri.path)) return HANDLER_GO_ON;
|
2020-01-17 06:03:06 +00:00
|
|
|
#endif
|
2019-10-26 21:27:20 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (r->uri.path.ptr[0] != '/' ||
|
|
|
|
r->uri.path.ptr[1] != '~') return HANDLER_GO_ON;
|
2019-10-26 21:27:20 +00:00
|
|
|
|
|
|
|
plugin_data * const p = p_d;
|
2020-01-13 02:51:12 +00:00
|
|
|
mod_userdir_patch_config(r, p);
|
2019-10-26 21:27:20 +00:00
|
|
|
|
|
|
|
/* enforce the userdir.path to be set in the config, ugly fix for #1587;
|
|
|
|
* should be replaced with a clean .enabled option in 1.5
|
|
|
|
*/
|
|
|
|
if (!p->conf.active || buffer_is_empty(p->conf.path)) return HANDLER_GO_ON;
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
const char * const uptr = r->uri.path.ptr + 2;
|
2019-10-26 21:27:20 +00:00
|
|
|
const char * const rel_url = strchr(uptr, '/');
|
|
|
|
if (NULL == rel_url) {
|
2020-01-20 03:10:46 +00:00
|
|
|
if (!*uptr) return HANDLER_GO_ON; /* "/~" is not a valid userdir path */
|
2019-10-26 21:27:20 +00:00
|
|
|
/* / is missing -> redirect to .../ as we are a user - DIRECTORY ! :) */
|
2020-01-13 02:51:12 +00:00
|
|
|
http_response_redirect_to_directory(r, 301);
|
2019-10-26 21:27:20 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* /~/ is a empty username, catch it directly */
|
|
|
|
const size_t ulen = (size_t)(rel_url - uptr);
|
|
|
|
if (0 == ulen) return HANDLER_GO_ON;
|
|
|
|
|
|
|
|
/* vlists could be turned into sorted array at config time,
|
|
|
|
* but these lists are expected to be relatively short in most cases
|
|
|
|
* so there is not a huge benefit to doing so in the common case */
|
|
|
|
|
|
|
|
if (p->conf.exclude_user) {
|
|
|
|
/* use case-insensitive comparison for exclude list
|
2020-01-13 02:51:12 +00:00
|
|
|
* if r->conf.force_lowercase_filenames */
|
|
|
|
if (!r->conf.force_lowercase_filenames
|
2019-10-26 21:27:20 +00:00
|
|
|
? mod_userdir_in_vlist(p->conf.exclude_user, uptr, ulen)
|
|
|
|
: mod_userdir_in_vlist_nc(p->conf.exclude_user, uptr, ulen))
|
|
|
|
return HANDLER_GO_ON; /* user in exclude list */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->conf.include_user) {
|
|
|
|
if (!mod_userdir_in_vlist(p->conf.include_user, uptr, ulen))
|
|
|
|
return HANDLER_GO_ON; /* user not in include list */
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
return mod_userdir_docroot_construct(r, p, uptr, ulen);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
int mod_userdir_plugin_init(plugin *p);
|
2005-02-20 14:27:00 +00:00
|
|
|
int mod_userdir_plugin_init(plugin *p) {
|
|
|
|
p->version = LIGHTTPD_VERSION_ID;
|
2019-10-19 04:30:54 +00:00
|
|
|
p->name = "userdir";
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->init = mod_userdir_init;
|
2005-08-18 20:53:02 +00:00
|
|
|
p->handle_physical = mod_userdir_docroot_handler;
|
2005-02-20 14:27:00 +00:00
|
|
|
p->set_defaults = mod_userdir_set_defaults;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|