home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / modules / standard / mod_userdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-01  |  12.4 KB  |  350 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in
  13.  *    the documentation and/or other materials provided with the
  14.  *    distribution.
  15.  *
  16.  * 3. All advertising materials mentioning features or use of this
  17.  *    software must display the following acknowledgment:
  18.  *    "This product includes software developed by the Apache Group
  19.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  20.  *
  21.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  22.  *    endorse or promote products derived from this software without
  23.  *    prior written permission. For written permission, please contact
  24.  *    apache@apache.org.
  25.  *
  26.  * 5. Products derived from this software may not be called "Apache"
  27.  *    nor may "Apache" appear in their names without prior written
  28.  *    permission of the Apache Group.
  29.  *
  30.  * 6. Redistributions of any form whatsoever must retain the following
  31.  *    acknowledgment:
  32.  *    "This product includes software developed by the Apache Group
  33.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  36.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Group and was originally based
  51.  * on public domain software written at the National Center for
  52.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  53.  * For more information on the Apache Group and the Apache HTTP server
  54.  * project, please see <http://www.apache.org/>.
  55.  *
  56.  */
  57.  
  58. /*
  59.  * mod_userdir... implement the UserDir command.  Broken away from the
  60.  * Alias stuff for a couple of good and not-so-good reasons:
  61.  *
  62.  * 1) It shows a real minimal working example of how to do something like
  63.  *    this.
  64.  * 2) I know people who are actually interested in changing this *particular*
  65.  *    aspect of server functionality without changing the rest of it.  That's
  66.  *    what this whole modular arrangement is supposed to be good at...
  67.  *
  68.  * Modified by Alexei Kosut to support the following constructs
  69.  * (server running at www.foo.com, request for /~bar/one/two.html)
  70.  *
  71.  * UserDir public_html      -> ~bar/public_html/one/two.html
  72.  * UserDir /usr/web         -> /usr/web/bar/one/two.html
  73.  * UserDir /home/ * /www     -> /home/bar/www/one/two.html
  74.  *  NOTE: theses ^ ^ space only added allow it to work in a comment, ignore
  75.  * UserDir http://x/users   -> (302) http://x/users/bar/one/two.html
  76.  * UserDir http://x/ * /y     -> (302) http://x/bar/y/one/two.html
  77.  *  NOTE: here also ^ ^
  78.  *
  79.  * In addition, you can use multiple entries, to specify alternate
  80.  * user directories (a la Directory Index). For example:
  81.  *
  82.  * UserDir public_html /usr/web http://www.xyz.com/users
  83.  *
  84.  * Modified by Ken Coar to provide for the following:
  85.  *
  86.  * UserDir disable[d] username ...
  87.  * UserDir enable[d] username ...
  88.  *
  89.  * If "disabled" has no other arguments, *all* ~<username> references are
  90.  * disabled, except those explicitly turned on with the "enabled" keyword.
  91.  */
  92.  
  93. #include "httpd.h"
  94. #include "http_config.h"
  95.  
  96. module userdir_module;
  97.  
  98. typedef struct userdir_config {
  99.     int globally_disabled;
  100.     char *userdir;
  101.     table *enabled_users;
  102.     table *disabled_users;
  103. }              userdir_config;
  104.  
  105. /*
  106.  * Server config for this module: global disablement flag, a list of usernames
  107.  * ineligible for UserDir access, a list of those immune to global (but not
  108.  * explicit) disablement, and the replacement string for all others.
  109.  */
  110.  
  111. static void *create_userdir_config(pool *p, server_rec *s)
  112. {
  113.     userdir_config
  114.     * newcfg = (userdir_config *) ap_pcalloc(p, sizeof(userdir_config));
  115.  
  116.     newcfg->globally_disabled = 0;
  117.     newcfg->userdir = DEFAULT_USER_DIR;
  118.     newcfg->enabled_users = ap_make_table(p, 4);
  119.     newcfg->disabled_users = ap_make_table(p, 4);
  120.     return (void *) newcfg;
  121. }
  122.  
  123. #define O_DEFAULT 0
  124. #define O_ENABLE 1
  125. #define O_DISABLE 2
  126.  
  127. static const char *set_user_dir(cmd_parms *cmd, void *dummy, char *arg)
  128. {
  129.     userdir_config
  130.     * s_cfg = (userdir_config *) ap_get_module_config
  131.     (
  132.      cmd->server->module_config,
  133.      &userdir_module
  134.     );
  135.     char *username;
  136.     const char
  137.         *usernames = arg;
  138.     char *kw = ap_getword_conf(cmd->pool, &usernames);
  139.     table *usertable;
  140.  
  141.     /*
  142.      * Let's do the comparisons once.
  143.      */
  144.     if ((!strcasecmp(kw, "disable")) || (!strcasecmp(kw, "disabled"))) {
  145.         /*
  146.          * If there are no usernames specified, this is a global disable - we
  147.          * need do no more at this point than record the fact.
  148.          */
  149.         if (strlen(usernames) == 0) {
  150.             s_cfg->globally_disabled = 1;
  151.             return NULL;
  152.         }
  153.         usertable = s_cfg->disabled_users;
  154.     }
  155.     else if ((!strcasecmp(kw, "enable")) || (!strcasecmp(kw, "enabled"))) {
  156.         /*
  157.          * The "disable" keyword can stand alone or take a list of names, but
  158.          * the "enable" keyword requires the list.  Whinge if it doesn't have
  159.          * it.
  160.          */
  161.         if (strlen(usernames) == 0) {
  162.             return "UserDir \"enable\" keyword requires a list of usernames";
  163.         }
  164.         usertable = s_cfg->enabled_users;
  165.     }
  166.     else {
  167.         /*
  168.          * If the first (only?) value isn't one of our keywords, just copy
  169.          * the string to the userdir string.
  170.          */
  171.         s_cfg->userdir = ap_pstrdup(cmd->pool, arg);
  172.         return NULL;
  173.     }
  174.     /*
  175.      * Now we just take each word in turn from the command line and add it to
  176.      * the appropriate table.
  177.      */
  178.     while (*usernames) {
  179.         username = ap_getword_conf(cmd->pool, &usernames);
  180.         ap_table_setn(usertable, username, kw);
  181.     }
  182.     return NULL;
  183. }
  184.  
  185. static const command_rec userdir_cmds[] = {
  186.     {"UserDir", set_user_dir, NULL, RSRC_CONF, RAW_ARGS,
  187.     "the public subdirectory in users' home directories, or 'disabled', or 'disabled username username...', or 'enabled username username...'"},
  188.     {NULL}
  189. };
  190.  
  191. static int translate_userdir(request_rec *r)
  192. {
  193.     void *server_conf = r->server->module_config;
  194.     const userdir_config *s_cfg =
  195.     (userdir_config *) ap_get_module_config(server_conf, &userdir_module);
  196.     char *name = r->uri;
  197.     const char *userdirs = s_cfg->userdir;
  198.     const char *w, *dname;
  199.     char *redirect;
  200.     char *x = NULL;
  201.     struct stat statbuf;
  202.  
  203.     /*
  204.      * If the URI doesn't match our basic pattern, we've nothing to do with
  205.      * it.
  206.      */
  207.     if (
  208.         (s_cfg->userdir == NULL) ||
  209.         (name[0] != '/') ||
  210.         (name[1] != '~')
  211.         ) {
  212.         return DECLINED;
  213.     }
  214.  
  215.     dname = name + 2;
  216.     w = ap_getword(r->pool, &dname, '/');
  217.  
  218.     /*
  219.      * The 'dname' funny business involves backing it up to capture the '/'
  220.      * delimiting the "/~user" part from the rest of the URL, in case there
  221.      * was one (the case where there wasn't being just "GET /~user HTTP/1.0",
  222.      * for which we don't want to tack on a '/' onto the filename).
  223.      */
  224.  
  225.     if (dname[-1] == '/') {
  226.         --dname;
  227.     }
  228.  
  229.     /*
  230.      * If there's no username, it's not for us.  Ignore . and .. as well.
  231.      */
  232.     if (w[0] == '\0' || (w[1] == '.' && (w[2] == '\0' || (w[2] == '.' && w[3] == '\0')))) {
  233.         return DECLINED;
  234.     }
  235.     /*
  236.      * Nor if there's an username but it's in the disabled list.
  237.      */
  238.     if (ap_table_get(s_cfg->disabled_users, w) != NULL) {
  239.         return DECLINED;
  240.     }
  241.     /*
  242.      * If there's a global interdiction on UserDirs, check to see if this
  243.      * name is one of the Blessed.
  244.      */
  245.     if (
  246.         s_cfg->globally_disabled &&
  247.         (ap_table_get(s_cfg->enabled_users, w) == NULL)
  248.         ) {
  249.         return DECLINED;
  250.     }
  251.  
  252.     /*
  253.      * Special cases all checked, onward to normal substitution processing.
  254.      */
  255.  
  256.     while (*userdirs) {
  257.         const char *userdir = ap_getword_conf(r->pool, &userdirs);
  258.         char *filename = NULL;
  259.  
  260.         if (strchr(userdir, '*'))
  261.             x = ap_getword(r->pool, &userdir, '*');
  262.  
  263.     if (userdir[0] == '\0' || ap_os_is_path_absolute(userdir)) {
  264.             if (x) {
  265. #ifdef WIN32
  266.                 /*
  267.                  * Crummy hack. Need to figure out whether we have been
  268.                  * redirected to a URL or to a file on some drive. Since I
  269.                  * know of no protocols that are a single letter, if the : is
  270.                  * the second character, I will assume a file was specified
  271.                  */
  272.                 if (strchr(x + 2, ':'))
  273. #else
  274.                 if (strchr(x, ':'))
  275. #endif                          /* WIN32 */
  276.         {
  277.                     redirect = ap_pstrcat(r->pool, x, w, userdir, dname, NULL);
  278.                     ap_table_setn(r->headers_out, "Location", redirect);
  279.                     return REDIRECT;
  280.                 }
  281.                 else
  282.                     filename = ap_pstrcat(r->pool, x, w, userdir, NULL);
  283.             }
  284.             else
  285.                 filename = ap_pstrcat(r->pool, userdir, "/", w, NULL);
  286.         }
  287.         else if (strchr(userdir, ':')) {
  288.             redirect = ap_pstrcat(r->pool, userdir, "/", w, dname, NULL);
  289.             ap_table_setn(r->headers_out, "Location", redirect);
  290.             return REDIRECT;
  291.         }
  292.         else {
  293. #ifdef WIN32
  294.             /* Need to figure out home dirs on NT */
  295.             return DECLINED;
  296. #else                           /* WIN32 */
  297.             struct passwd *pw;
  298.             if ((pw = getpwnam(w))) {
  299. #ifdef OS2
  300.                 /* Need to manually add user name for OS/2 */
  301.                 filename = ap_pstrcat(r->pool, pw->pw_dir, w, "/", userdir, NULL);
  302. #else
  303.                 filename = ap_pstrcat(r->pool, pw->pw_dir, "/", userdir, NULL);
  304. #endif
  305.             }
  306. #endif                          /* WIN32 */
  307.         }
  308.  
  309.         /*
  310.          * Now see if it exists, or we're at the last entry. If we are at the
  311.          * last entry, then use the filename generated (if there is one)
  312.          * anyway, in the hope that some handler might handle it. This can be
  313.          * used, for example, to run a CGI script for the user.
  314.          */
  315.         if (filename && (!*userdirs || stat(filename, &statbuf) != -1)) {
  316.             r->filename = ap_pstrcat(r->pool, filename, dname, NULL);
  317.         /* when statbuf contains info on r->filename we can save a syscall
  318.          * by copying it to r->finfo
  319.          */
  320.         if (*userdirs && dname[0] == 0)
  321.         r->finfo = statbuf;
  322.             return OK;
  323.         }
  324.     }
  325.  
  326.     return DECLINED;
  327. }
  328.  
  329. module userdir_module = {
  330.     STANDARD_MODULE_STUFF,
  331.     NULL,                       /* initializer */
  332.     NULL,                       /* dir config creater */
  333.     NULL,                       /* dir merger --- default is to override */
  334.     create_userdir_config,      /* server config */
  335.     NULL,                       /* merge server config */
  336.     userdir_cmds,               /* command table */
  337.     NULL,                       /* handlers */
  338.     translate_userdir,          /* filename translation */
  339.     NULL,                       /* check_user_id */
  340.     NULL,                       /* check auth */
  341.     NULL,                       /* check access */
  342.     NULL,                       /* type_checker */
  343.     NULL,                       /* fixups */
  344.     NULL,                       /* logger */
  345.     NULL,                       /* header parser */
  346.     NULL,                       /* child_init */
  347.     NULL,                       /* child_exit */
  348.     NULL                        /* post read-request */
  349. };
  350.