home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / mod_user.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-21  |  7.2 KB  |  207 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54.  
  55. /*
  56.  * mod_userdir... implement the UserDir command.  Broken away from the
  57.  * Alias stuff for a couple of good and not-so-good reasons:
  58.  *
  59.  * 1) It shows a real minimal working example of how to do something like
  60.  *    this.
  61.  * 2) I know people who are actually interested in changing this *particular*
  62.  *    aspect of server functionality without changing the rest of it.  That's
  63.  *    what this whole modular arrangement is supposed to be good at...
  64.  *
  65.  * Modified by Alexei Kosut to support the following constructs
  66.  * (server running at www.foo.com, request for /~bar/one/two.html)
  67.  *
  68.  * UserDir public_html      -> ~bar/public_html/one/two.html
  69.  * UserDir /usr/web         -> /usr/web/bar/one/two.html
  70.  * UserDir /home/ * /www     -> /home/bar/www/one/two.html
  71.  *  NOTE: theses ^ ^ space only added allow it to work in a comment, ignore
  72.  * UserDir http://x/users   -> (302) http://x/users/bar/one/two.html
  73.  * UserDir http://x/ * /y     -> (302) http://x/bar/y/one/two.html
  74.  *  NOTE: here also ^ ^
  75.  *
  76.  * In addition, you can use multiple entries, to specify alternate
  77.  * user directories (a la Directory Index). For example:
  78.  *
  79.  * UserDir public_html /usr/web http://www.xyz.com/users
  80.  *
  81.  */
  82.  
  83. #include "httpd.h"
  84. #include "http_config.h"
  85.  
  86. module userdir_module;
  87.  
  88. /*
  89.  * Sever config for this module is a little unconventional...
  90.  * It's just one string anyway, so why pretend?
  91.  */
  92.  
  93. void *create_userdir_config (pool *dummy, server_rec *s) { 
  94.     return (void*)DEFAULT_USER_DIR; 
  95. }
  96.  
  97. char *set_user_dir (cmd_parms *cmd, void *dummy, char *arg)
  98. {
  99.     void *server_conf = cmd->server->module_config;
  100.     
  101.     set_module_config (server_conf, &userdir_module, pstrdup (cmd->pool, arg));
  102.     return NULL;
  103. }
  104.  
  105. command_rec userdir_cmds[] = {
  106. { "UserDir", set_user_dir, NULL, RSRC_CONF, RAW_ARGS,
  107.     "the public subdirectory in users' home directories, or 'disabled'" },
  108. { NULL }
  109. };
  110.  
  111. int translate_userdir (request_rec *r)
  112. {
  113.     void *server_conf = r->server->module_config;
  114.     char *userdirs = (char *)get_module_config(server_conf, &userdir_module);
  115.     char *name = r->uri;
  116.     char *w, *dname, *redirect;
  117.     char *x = NULL;
  118.  
  119.     if (userdirs == NULL || !strcasecmp(userdirs, "disabled") ||
  120.         (name[0] != '/') || (name[1] != '~')) {
  121.       return DECLINED;
  122.     }
  123.  
  124.     while (*userdirs) {
  125.       char *userdir = getword_conf (r->pool, &userdirs);
  126.       char *filename = NULL;
  127.  
  128.       dname = name + 2;
  129.       w = getword(r->pool, &dname, '/');
  130.  
  131.       if (!strcmp(w, ""))
  132.     return DECLINED;
  133.  
  134.       /* The 'dname' funny business involves backing it up to capture
  135.        * the '/' delimiting the "/~user" part from the rest of the URL,
  136.        * in case there was one (the case where there wasn't being just
  137.        * "GET /~user HTTP/1.0", for which we don't want to tack on a
  138.        * '/' onto the filename).
  139.        */
  140.     
  141.       if (dname[-1] == '/') --dname;
  142.  
  143.       if (strchr(userdir, '*'))
  144.     x = getword(r->pool, &userdir, '*');
  145.  
  146.       if (userdir[0] == '/') {
  147.     if (x) {
  148.       if (strchr(x, ':')) {
  149.         redirect = pstrcat(r->pool, x, w, userdir, dname, NULL);
  150.         table_set (r->headers_out, "Location", redirect);
  151.         return REDIRECT;
  152.       }
  153.       else
  154.         filename = pstrcat (r->pool, x, w, userdir, NULL);
  155.     }
  156.     else
  157.       filename = pstrcat (r->pool, userdir, "/", w, NULL);
  158.       }
  159.       else if (strchr(userdir, ':')) {
  160.     redirect = pstrcat(r->pool, userdir, "/", w, dname, NULL);
  161.     table_set (r->headers_out, "Location", redirect);
  162.     return REDIRECT;
  163.       }
  164.       else {
  165.     struct passwd *pw;
  166.     if((pw=getpwnam(w)))
  167. #ifdef __EMX__
  168.       /* Need to manually add user name for OS/2 */
  169.       filename = pstrcat (r->pool, pw->pw_dir, w, "/", userdir, NULL);
  170. #else
  171.       filename = pstrcat (r->pool, pw->pw_dir, "/", userdir, NULL);
  172. #endif
  173.  
  174.       }
  175.  
  176.       /* Now see if it exists, or we're at the last entry. If we are at the
  177.        last entry, then use the filename generated (if there is one) anyway,
  178.        in the hope that some handler might handle it. This can be used, for
  179.        example, to run a CGI script for the user. 
  180.        */
  181.       if (filename && (!*userdirs || stat(filename, &r->finfo) != -1)) {
  182.     r->filename = pstrcat(r->pool, filename, dname, NULL);
  183.     return OK;
  184.       }
  185.     }
  186.  
  187.   return DECLINED;    
  188. }
  189.     
  190. module userdir_module = {
  191.    STANDARD_MODULE_STUFF,
  192.    NULL,            /* initializer */
  193.    NULL,            /* dir config creater */
  194.    NULL,            /* dir merger --- default is to override */
  195.    create_userdir_config,    /* server config */
  196.    NULL,            /* merge server config */
  197.    userdir_cmds,        /* command table */
  198.    NULL,            /* handlers */
  199.    translate_userdir,        /*filename translation */
  200.    NULL,            /* check_user_id */
  201.    NULL,            /* check auth */
  202.    NULL,            /* check access */
  203.    NULL,            /* type_checker */
  204.    NULL,            /* fixups */
  205.    NULL                /* logger */
  206. };
  207.