Blame modules/mappers/mod_userdir.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * mod_userdir... implement the UserDir command.  Broken away from the
Packit 90a5c9
 * Alias stuff for a couple of good and not-so-good reasons:
Packit 90a5c9
 *
Packit 90a5c9
 * 1) It shows a real minimal working example of how to do something like
Packit 90a5c9
 *    this.
Packit 90a5c9
 * 2) I know people who are actually interested in changing this *particular*
Packit 90a5c9
 *    aspect of server functionality without changing the rest of it.  That's
Packit 90a5c9
 *    what this whole modular arrangement is supposed to be good at...
Packit 90a5c9
 *
Packit 90a5c9
 * Modified by Alexei Kosut to support the following constructs
Packit 90a5c9
 * (server running at www.foo.com, request for /~bar/one/two.html)
Packit 90a5c9
 *
Packit 90a5c9
 * UserDir public_html      -> ~bar/public_html/one/two.html
Packit 90a5c9
 * UserDir /usr/web         -> /usr/web/bar/one/two.html
Packit 90a5c9
 * UserDir /home/ * /www     -> /home/bar/www/one/two.html
Packit 90a5c9
 *  NOTE: theses ^ ^ space only added allow it to work in a comment, ignore
Packit 90a5c9
 * UserDir http://x/users   -> (302) http://x/users/bar/one/two.html
Packit 90a5c9
 * UserDir http://x/ * /y     -> (302) http://x/bar/y/one/two.html
Packit 90a5c9
 *  NOTE: here also ^ ^
Packit 90a5c9
 *
Packit 90a5c9
 * In addition, you can use multiple entries, to specify alternate
Packit 90a5c9
 * user directories (a la Directory Index). For example:
Packit 90a5c9
 *
Packit 90a5c9
 * UserDir public_html /usr/web http://www.xyz.com/users
Packit 90a5c9
 *
Packit 90a5c9
 * Modified by Ken Coar to provide for the following:
Packit 90a5c9
 *
Packit 90a5c9
 * UserDir disable[d] username ...
Packit 90a5c9
 * UserDir enable[d] username ...
Packit 90a5c9
 *
Packit 90a5c9
 * If "disabled" has no other arguments, *all* ~<username> references are
Packit 90a5c9
 * disabled, except those explicitly turned on with the "enabled" keyword.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "apr_user.h"
Packit 90a5c9
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
Packit 90a5c9
#if APR_HAVE_UNISTD_H
Packit 90a5c9
#include <unistd.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
Packit 90a5c9
#if !defined(WIN32) && !defined(OS2) && !defined(NETWARE)
Packit 90a5c9
#define HAVE_UNIX_SUEXEC
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#ifdef HAVE_UNIX_SUEXEC
Packit 90a5c9
#include "unixd.h"        /* Contains the suexec_identity hook used on Unix */
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * The default directory in user's home dir
Packit 90a5c9
 * In the default install, the module is disabled
Packit 90a5c9
 */
Packit 90a5c9
#ifndef DEFAULT_USER_DIR
Packit 90a5c9
#define DEFAULT_USER_DIR NULL
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#define O_DEFAULT 0
Packit 90a5c9
#define O_ENABLE 1
Packit 90a5c9
#define O_DISABLE 2
Packit 90a5c9
Packit 90a5c9
module AP_MODULE_DECLARE_DATA userdir_module;
Packit 90a5c9
Packit 90a5c9
typedef struct {
Packit 90a5c9
    int globally_disabled;
Packit 90a5c9
    const char *userdir;
Packit 90a5c9
    apr_table_t *enabled_users;
Packit 90a5c9
    apr_table_t *disabled_users;
Packit 90a5c9
} userdir_config;
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Server config for this module: global disablement flag, a list of usernames
Packit 90a5c9
 * ineligible for UserDir access, a list of those immune to global (but not
Packit 90a5c9
 * explicit) disablement, and the replacement string for all others.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
static void *create_userdir_config(apr_pool_t *p, server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    userdir_config *newcfg = apr_pcalloc(p, sizeof(*newcfg));
Packit 90a5c9
Packit 90a5c9
    newcfg->globally_disabled = O_DEFAULT;
Packit 90a5c9
    newcfg->userdir = DEFAULT_USER_DIR;
Packit 90a5c9
    newcfg->enabled_users = apr_table_make(p, 4);
Packit 90a5c9
    newcfg->disabled_users = apr_table_make(p, 4);
Packit 90a5c9
Packit 90a5c9
    return newcfg;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *merge_userdir_config(apr_pool_t *p, void *basev, void *overridesv)
Packit 90a5c9
{
Packit 90a5c9
    userdir_config *cfg = apr_pcalloc(p, sizeof(userdir_config));
Packit 90a5c9
    userdir_config *base = basev, *overrides = overridesv;
Packit 90a5c9
Packit 90a5c9
    cfg->globally_disabled = (overrides->globally_disabled != O_DEFAULT) ?
Packit 90a5c9
                             overrides->globally_disabled :
Packit 90a5c9
                             base->globally_disabled;
Packit 90a5c9
    cfg->userdir = (overrides->userdir != DEFAULT_USER_DIR) ?
Packit 90a5c9
                   overrides->userdir : base->userdir;
Packit 90a5c9
Packit 90a5c9
    /* not merged */
Packit 90a5c9
    cfg->enabled_users = overrides->enabled_users;
Packit 90a5c9
    cfg->disabled_users = overrides->disabled_users;
Packit 90a5c9
Packit 90a5c9
    return cfg;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static const char *set_user_dir(cmd_parms *cmd, void *dummy, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    userdir_config *s_cfg = ap_get_module_config(cmd->server->module_config,
Packit 90a5c9
                                                 &userdir_module);
Packit 90a5c9
    char *username;
Packit 90a5c9
    const char *usernames = arg;
Packit 90a5c9
    char *kw = ap_getword_conf(cmd->temp_pool, &usernames);
Packit 90a5c9
    apr_table_t *usertable;
Packit 90a5c9
Packit 90a5c9
    /* Since we are a raw argument, it is possible for us to be called with
Packit 90a5c9
     * zero arguments.  So that we aren't ambiguous, flat out reject this.
Packit 90a5c9
     */
Packit 90a5c9
    if (*kw == '\0') {
Packit 90a5c9
        return "UserDir requires an argument.";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Let's do the comparisons once.
Packit 90a5c9
     */
Packit 90a5c9
    if ((!strcasecmp(kw, "disable")) || (!strcasecmp(kw, "disabled"))) {
Packit 90a5c9
        /*
Packit 90a5c9
         * If there are no usernames specified, this is a global disable - we
Packit 90a5c9
         * need do no more at this point than record the fact.
Packit 90a5c9
         */
Packit 90a5c9
        if (!*usernames) {
Packit 90a5c9
            s_cfg->globally_disabled = O_DISABLE;
Packit 90a5c9
            return NULL;
Packit 90a5c9
        }
Packit 90a5c9
        usertable = s_cfg->disabled_users;
Packit 90a5c9
    }
Packit 90a5c9
    else if ((!strcasecmp(kw, "enable")) || (!strcasecmp(kw, "enabled"))) {
Packit 90a5c9
        if (!*usernames) {
Packit 90a5c9
            s_cfg->globally_disabled = O_ENABLE;
Packit 90a5c9
            return NULL;
Packit 90a5c9
        }
Packit 90a5c9
        usertable = s_cfg->enabled_users;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        /*
Packit 90a5c9
         * If the first (only?) value isn't one of our keywords, just copy
Packit 90a5c9
         * the string to the userdir string.
Packit 90a5c9
         */
Packit 90a5c9
        s_cfg->userdir = arg;
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
    /*
Packit 90a5c9
     * Now we just take each word in turn from the command line and add it to
Packit 90a5c9
     * the appropriate table.
Packit 90a5c9
     */
Packit 90a5c9
    while (*usernames) {
Packit 90a5c9
        username = ap_getword_conf(cmd->pool, &usernames);
Packit 90a5c9
        apr_table_setn(usertable, username, "1");
Packit 90a5c9
    }
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const command_rec userdir_cmds[] = {
Packit 90a5c9
    AP_INIT_RAW_ARGS("UserDir", set_user_dir, NULL, RSRC_CONF,
Packit 90a5c9
                     "the public subdirectory in users' home directories, or "
Packit 90a5c9
                     "'disabled', or 'disabled username username...', or "
Packit 90a5c9
                     "'enabled username username...'"),
Packit 90a5c9
    {NULL}
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
static int translate_userdir(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    ap_conf_vector_t *server_conf;
Packit 90a5c9
    const userdir_config *s_cfg;
Packit 90a5c9
    const char *userdirs;
Packit 90a5c9
    const char *user, *dname;
Packit 90a5c9
    char *redirect;
Packit 90a5c9
    apr_finfo_t statbuf;
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * If the URI doesn't match our basic pattern, we've nothing to do with
Packit 90a5c9
     * it.
Packit 90a5c9
     */
Packit 90a5c9
    if (r->uri[0] != '/' || r->uri[1] != '~') {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
    server_conf = r->server->module_config;
Packit 90a5c9
    s_cfg = ap_get_module_config(server_conf, &userdir_module);
Packit 90a5c9
    userdirs = s_cfg->userdir;
Packit 90a5c9
    if (userdirs == NULL) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    dname = r->uri + 2;
Packit 90a5c9
    user = ap_getword(r->pool, &dname, '/');
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * The 'dname' funny business involves backing it up to capture the '/'
Packit 90a5c9
     * delimiting the "/~user" part from the rest of the URL, in case there
Packit 90a5c9
     * was one (the case where there wasn't being just "GET /~user HTTP/1.0",
Packit 90a5c9
     * for which we don't want to tack on a '/' onto the filename).
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    if (dname[-1] == '/') {
Packit 90a5c9
        --dname;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * If there's no username, it's not for us.  Ignore . and .. as well.
Packit 90a5c9
     */
Packit 90a5c9
    if (user[0] == '\0' ||
Packit 90a5c9
        (user[1] == '.' && (user[2] == '\0' ||
Packit 90a5c9
                            (user[2] == '.' && user[3] == '\0')))) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
    /*
Packit 90a5c9
     * Nor if there's an username but it's in the disabled list.
Packit 90a5c9
     */
Packit 90a5c9
    if (apr_table_get(s_cfg->disabled_users, user) != NULL) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
    /*
Packit 90a5c9
     * If there's a global interdiction on UserDirs, check to see if this
Packit 90a5c9
     * name is one of the Blessed.
Packit 90a5c9
     */
Packit 90a5c9
    if (s_cfg->globally_disabled == O_DISABLE
Packit 90a5c9
        && apr_table_get(s_cfg->enabled_users, user) == NULL) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Special cases all checked, onward to normal substitution processing.
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    while (*userdirs) {
Packit 90a5c9
        const char *userdir = ap_getword_conf(r->pool, &userdirs);
Packit 90a5c9
        char *filename = NULL, *prefix = NULL;
Packit 90a5c9
        apr_status_t rv;
Packit 90a5c9
        int is_absolute = ap_os_is_path_absolute(r->pool, userdir);
Packit 90a5c9
Packit 90a5c9
        if (ap_strchr_c(userdir, '*'))
Packit 90a5c9
            prefix = ap_getword(r->pool, &userdir, '*');
Packit 90a5c9
Packit 90a5c9
        if (userdir[0] == '\0' || is_absolute) {
Packit 90a5c9
            if (prefix) {
Packit 90a5c9
#ifdef HAVE_DRIVE_LETTERS
Packit 90a5c9
                /*
Packit 90a5c9
                 * Crummy hack. Need to figure out whether we have been
Packit 90a5c9
                 * redirected to a URL or to a file on some drive. Since I
Packit 90a5c9
                 * know of no protocols that are a single letter, ignore
Packit 90a5c9
                 * a : as the first or second character, and assume a file
Packit 90a5c9
                 * was specified
Packit 90a5c9
                 */
Packit 90a5c9
                if (strchr(prefix + 2, ':'))
Packit 90a5c9
#else
Packit 90a5c9
                if (strchr(prefix, ':') && !is_absolute)
Packit 90a5c9
#endif /* HAVE_DRIVE_LETTERS */
Packit 90a5c9
                {
Packit 90a5c9
                    redirect = apr_pstrcat(r->pool, prefix, user, userdir,
Packit 90a5c9
                                           dname, NULL);
Packit 90a5c9
                    apr_table_setn(r->headers_out, "Location", redirect);
Packit 90a5c9
                    return HTTP_MOVED_TEMPORARILY;
Packit 90a5c9
                }
Packit 90a5c9
                else
Packit 90a5c9
                    filename = apr_pstrcat(r->pool, prefix, user, userdir,
Packit 90a5c9
                                           NULL);
Packit 90a5c9
            }
Packit 90a5c9
            else
Packit 90a5c9
                filename = apr_pstrcat(r->pool, userdir, "/", user, NULL);
Packit 90a5c9
        }
Packit 90a5c9
        else if (prefix && ap_strchr_c(prefix, ':')) {
Packit 90a5c9
            redirect = apr_pstrcat(r->pool, prefix, user, dname, NULL);
Packit 90a5c9
            apr_table_setn(r->headers_out, "Location", redirect);
Packit 90a5c9
            return HTTP_MOVED_TEMPORARILY;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
#if APR_HAS_USER
Packit 90a5c9
            char *homedir;
Packit 90a5c9
Packit 90a5c9
            if (apr_uid_homepath_get(&homedir, user, r->pool) == APR_SUCCESS) {
Packit 90a5c9
                filename = apr_pstrcat(r->pool, homedir, "/", userdir, NULL);
Packit 90a5c9
            }
Packit 90a5c9
#else
Packit 90a5c9
            return DECLINED;
Packit 90a5c9
#endif
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /*
Packit 90a5c9
         * Now see if it exists, or we're at the last entry. If we are at the
Packit 90a5c9
         * last entry, then use the filename generated (if there is one)
Packit 90a5c9
         * anyway, in the hope that some handler might handle it. This can be
Packit 90a5c9
         * used, for example, to run a CGI script for the user.
Packit 90a5c9
         */
Packit 90a5c9
        if (filename && (!*userdirs
Packit 90a5c9
                      || ((rv = apr_stat(&statbuf, filename, APR_FINFO_MIN,
Packit 90a5c9
                                         r->pool)) == APR_SUCCESS
Packit 90a5c9
                                             || rv == APR_INCOMPLETE))) {
Packit 90a5c9
            r->filename = apr_pstrcat(r->pool, filename, dname, NULL);
Packit 90a5c9
            ap_set_context_info(r, apr_pstrmemdup(r->pool, r->uri,
Packit 90a5c9
                                                  dname - r->uri),
Packit 90a5c9
                                filename);
Packit 90a5c9
            /* XXX: Does this walk us around FollowSymLink rules?
Packit 90a5c9
             * When statbuf contains info on r->filename we can save a syscall
Packit 90a5c9
             * by copying it to r->finfo
Packit 90a5c9
             */
Packit 90a5c9
            if (*userdirs && dname[0] == 0)
Packit 90a5c9
                r->finfo = statbuf;
Packit 90a5c9
Packit 90a5c9
            /* For use in the get_suexec_identity phase */
Packit 90a5c9
            apr_table_setn(r->notes, "mod_userdir_user", user);
Packit 90a5c9
Packit 90a5c9
            return OK;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return DECLINED;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
#ifdef HAVE_UNIX_SUEXEC
Packit 90a5c9
static ap_unix_identity_t *get_suexec_id_doer(const request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    ap_unix_identity_t *ugid = NULL;
Packit 90a5c9
#if APR_HAS_USER
Packit 90a5c9
    const char *username = apr_table_get(r->notes, "mod_userdir_user");
Packit 90a5c9
Packit 90a5c9
    if (username == NULL) {
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if ((ugid = apr_palloc(r->pool, sizeof(*ugid))) == NULL) {
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (apr_uid_get(&ugid->uid, &ugid->gid, username, r->pool) != APR_SUCCESS) {
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ugid->userdir = 1;
Packit 90a5c9
#endif
Packit 90a5c9
    return ugid;
Packit 90a5c9
}
Packit 90a5c9
#endif /* HAVE_UNIX_SUEXEC */
Packit 90a5c9
Packit 90a5c9
static void register_hooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    static const char * const aszPre[]={ "mod_alias.c",NULL };
Packit 90a5c9
    static const char * const aszSucc[]={ "mod_vhost_alias.c",NULL };
Packit 90a5c9
Packit 90a5c9
    ap_hook_translate_name(translate_userdir,aszPre,aszSucc,APR_HOOK_MIDDLE);
Packit 90a5c9
#ifdef HAVE_UNIX_SUEXEC
Packit 90a5c9
    ap_hook_get_suexec_identity(get_suexec_id_doer,NULL,NULL,APR_HOOK_FIRST);
Packit 90a5c9
#endif
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(userdir) = {
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    NULL,                       /* dir config creater */
Packit 90a5c9
    NULL,                       /* dir merger --- default is to override */
Packit 90a5c9
    create_userdir_config,      /* server config */
Packit 90a5c9
    merge_userdir_config,       /* merge server config */
Packit 90a5c9
    userdir_cmds,               /* command apr_table_t */
Packit 90a5c9
    register_hooks              /* register hooks */
Packit 90a5c9
};