2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "base.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
|
|
|
|
#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 {
|
|
|
|
array *exclude_user;
|
|
|
|
array *include_user;
|
|
|
|
buffer *path;
|
2005-10-31 07:58:17 +00:00
|
|
|
buffer *basepath;
|
2008-01-18 12:50:09 +00:00
|
|
|
unsigned short letterhomes;
|
2013-06-29 10:07:43 +00:00
|
|
|
unsigned short active;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_config **config_storage;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
plugin_config conf;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
INIT_FUNC(mod_userdir_init) {
|
|
|
|
plugin_data *p;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p = calloc(1, sizeof(*p));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
FREE_FUNC(mod_userdir_free) {
|
|
|
|
plugin_data *p = p_d;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!p) return HANDLER_GO_ON;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (p->config_storage) {
|
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; i < srv->config_context->used; i++) {
|
|
|
|
plugin_config *s = p->config_storage[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-05-14 09:38:33 +00:00
|
|
|
if (NULL == s) continue;
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
array_free(s->include_user);
|
|
|
|
array_free(s->exclude_user);
|
|
|
|
buffer_free(s->path);
|
2005-10-31 07:58:17 +00:00
|
|
|
buffer_free(s->basepath);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
free(p->config_storage);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(p);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
SETDEFAULTS_FUNC(mod_userdir_set_defaults) {
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
config_values_t cv[] = {
|
2005-02-20 14:27:00 +00:00
|
|
|
{ "userdir.path", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
|
2005-10-31 07:58:17 +00:00
|
|
|
{ "userdir.exclude-user", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
|
|
|
|
{ "userdir.include-user", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
|
|
|
|
{ "userdir.basepath", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
|
2013-06-29 10:07:43 +00:00
|
|
|
{ "userdir.letterhomes", NULL, T_CONFIG_BOOLEAN,T_CONFIG_SCOPE_CONNECTION }, /* 4 */
|
|
|
|
{ "userdir.active", NULL, T_CONFIG_BOOLEAN,T_CONFIG_SCOPE_CONNECTION }, /* 5 */
|
2005-10-31 07:58:17 +00:00
|
|
|
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
|
2005-02-20 14:27:00 +00:00
|
|
|
};
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!p) return HANDLER_ERROR;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-02-27 12:42:48 +00:00
|
|
|
p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; i < srv->config_context->used; i++) {
|
2015-11-07 12:51:11 +00:00
|
|
|
data_config const* config = (data_config const*)srv->config_context->data[i];
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_config *s;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-16 13:07:46 +00:00
|
|
|
s = calloc(1, sizeof(plugin_config));
|
2005-02-20 14:27:00 +00:00
|
|
|
s->exclude_user = array_init();
|
|
|
|
s->include_user = array_init();
|
|
|
|
s->path = buffer_init();
|
2005-10-31 07:58:17 +00:00
|
|
|
s->basepath = buffer_init();
|
2008-01-18 12:50:09 +00:00
|
|
|
s->letterhomes = 0;
|
2013-06-29 10:07:43 +00:00
|
|
|
/* 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. */
|
|
|
|
s->active = 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
cv[0].destination = s->path;
|
|
|
|
cv[1].destination = s->exclude_user;
|
|
|
|
cv[2].destination = s->include_user;
|
2005-10-31 07:58:17 +00:00
|
|
|
cv[3].destination = s->basepath;
|
2008-01-18 12:50:09 +00:00
|
|
|
cv[4].destination = &(s->letterhomes);
|
2013-06-29 10:07:43 +00:00
|
|
|
cv[5].destination = &(s->active);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->config_storage[i] = s;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-11-07 12:51:11 +00:00
|
|
|
if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2017-03-05 20:39:45 +00:00
|
|
|
|
|
|
|
if (!array_is_vlist(s->exclude_user)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s",
|
|
|
|
"unexpected value for userdir.exclude-user; expected list of \"suffix\"");
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!array_is_vlist(s->include_user)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s",
|
|
|
|
"unexpected value for userdir.include-user; expected list of \"suffix\"");
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PATCH(x) \
|
|
|
|
p->conf.x = s->x;
|
2005-08-08 10:27:07 +00:00
|
|
|
static int mod_userdir_patch_connection(server *srv, connection *con, plugin_data *p) {
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t i, j;
|
2005-08-08 10:27:07 +00:00
|
|
|
plugin_config *s = p->config_storage[0];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-08 10:27:07 +00:00
|
|
|
PATCH(path);
|
|
|
|
PATCH(exclude_user);
|
|
|
|
PATCH(include_user);
|
2005-10-31 07:58:17 +00:00
|
|
|
PATCH(basepath);
|
2008-01-18 12:50:09 +00:00
|
|
|
PATCH(letterhomes);
|
2013-06-29 10:07:43 +00:00
|
|
|
PATCH(active);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* skip the first, the global context */
|
|
|
|
for (i = 1; i < srv->config_context->used; i++) {
|
2019-10-17 05:27:52 +00:00
|
|
|
if (!config_check_cond(con, i)) continue; /* condition not matched */
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
data_config *dc = (data_config *)srv->config_context->data[i];
|
2005-08-08 10:27:07 +00:00
|
|
|
s = p->config_storage[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* merge config */
|
|
|
|
for (j = 0; j < dc->value->used; j++) {
|
|
|
|
data_unset *du = dc->value->data[j];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-13 07:40:03 +00:00
|
|
|
if (buffer_is_equal_string(&du->key, CONST_STR_LEN("userdir.path"))) {
|
2005-02-20 14:27:00 +00:00
|
|
|
PATCH(path);
|
2019-10-13 07:40:03 +00:00
|
|
|
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("userdir.exclude-user"))) {
|
2005-02-20 14:27:00 +00:00
|
|
|
PATCH(exclude_user);
|
2019-10-13 07:40:03 +00:00
|
|
|
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("userdir.include-user"))) {
|
2005-02-20 14:27:00 +00:00
|
|
|
PATCH(include_user);
|
2019-10-13 07:40:03 +00:00
|
|
|
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("userdir.basepath"))) {
|
2005-10-31 07:58:17 +00:00
|
|
|
PATCH(basepath);
|
2019-10-13 07:40:03 +00:00
|
|
|
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("userdir.letterhomes"))) {
|
2008-01-18 12:50:09 +00:00
|
|
|
PATCH(letterhomes);
|
2019-10-13 07:40:03 +00:00
|
|
|
} else if (buffer_is_equal_string(&du->key, CONST_STR_LEN("userdir.active"))) {
|
2013-06-29 10:07:43 +00:00
|
|
|
PATCH(active);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#undef PATCH
|
|
|
|
|
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__
|
|
|
|
static handler_t mod_userdir_docroot_construct(connection * const con, plugin_data * const p, const char * const uptr, const size_t ulen) {
|
|
|
|
char u[256];
|
|
|
|
if (ulen >= sizeof(u)) return HANDLER_GO_ON;
|
|
|
|
|
|
|
|
memcpy(u, uptr, ulen);
|
|
|
|
u[ulen] = '\0';
|
|
|
|
|
|
|
|
/* we build the physical path */
|
|
|
|
buffer * const b = con->srv->tmp_buf;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (con->conf.force_lowercase_filenames) {
|
|
|
|
for (size_t i = 0; i < ulen; ++i) {
|
|
|
|
if (u[i] >= 'A' && u[i] <= 'Z') u[i] |= 0x20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_copy_buffer(con->physical.basedir, b);
|
|
|
|
buffer_copy_buffer(con->physical.path, b);
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
* (docroot should only set the docroot/server name, phyiscal should only
|
|
|
|
* change the physical.path;
|
|
|
|
* the exception mod_secdownload doesn't work with userdir anyway)
|
|
|
|
*/
|
|
|
|
buffer_append_slash(con->physical.path);
|
|
|
|
/* 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;
|
|
|
|
if (NULL != (rel_url = strchr(con->physical.rel_path->ptr + 2, '/'))) {
|
|
|
|
buffer_append_string(con->physical.path, rel_url + 1); /* skip the / */
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON;
|
|
|
|
|
|
|
|
if (con->uri.path->ptr[0] != '/' ||
|
|
|
|
con->uri.path->ptr[1] != '~') return HANDLER_GO_ON;
|
|
|
|
|
|
|
|
plugin_data * const p = p_d;
|
|
|
|
mod_userdir_patch_connection(srv, con, p);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
const char * const uptr = con->uri.path->ptr + 2;
|
|
|
|
const char * const rel_url = strchr(uptr, '/');
|
|
|
|
if (NULL == rel_url) {
|
|
|
|
/* / is missing -> redirect to .../ as we are a user - DIRECTORY ! :) */
|
|
|
|
http_response_redirect_to_directory(srv, con, 301);
|
|
|
|
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
|
|
|
|
* if con->conf.force_lowercase_filenames */
|
|
|
|
if (!con->conf.force_lowercase_filenames
|
|
|
|
? 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 */
|
|
|
|
}
|
|
|
|
|
|
|
|
return mod_userdir_docroot_construct(con, 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;
|
|
|
|
p->cleanup = mod_userdir_free;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|