home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / modules / standard / mod_auth_anon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-01  |  9.8 KB  |  316 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.  * IT'S 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.  * http_auth: authentication
  60.  * 
  61.  * Rob McCool & Brian Behlendorf.
  62.  * 
  63.  * Adapted to Apache by rst.
  64.  *
  65.  * Version 0.5 May 1996
  66.  *
  67.  * Modified by Dirk.vanGulik@jrc.it to
  68.  * 
  69.  * Adapted to allow anonymous logins, just like with Anon-FTP, when
  70.  * one gives the magic user name 'anonymous' and ones email address
  71.  * as the password.
  72.  *
  73.  * Just add the following tokes to your <directory> setup:
  74.  * 
  75.  * Anonymous                    magic-user-id [magic-user-id]...
  76.  *
  77.  * Anonymous_MustGiveEmail      [ on | off ] default = off
  78.  * Anonymous_LogEmail           [ on | off ] default = on
  79.  * Anonymous_VerifyEmail        [ on | off ] default = off
  80.  * Anonymous_NoUserId           [ on | off ] default = off
  81.  * Anonymous_Authoritative      [ on | off ] default = off
  82.  *
  83.  * The magic user id is something like 'anonymous', it is NOT case sensitive. 
  84.  * 
  85.  * The MustGiveEmail flag can be used to force users to enter something
  86.  * in the password field (like an email address). Default is off.
  87.  *
  88.  * Furthermore the 'NoUserID' flag can be set to allow completely empty
  89.  * usernames in as well; this can be is convenient as a single return
  90.  * in broken GUIs like W95 is often given by the user. The Default is off.
  91.  *
  92.  * Dirk.vanGulik@jrc.it; http://ewse.ceo.org; http://me-www.jrc.it/~dirkx
  93.  * 
  94.  */
  95.  
  96. #include "httpd.h"
  97. #include "http_config.h"
  98. #include "http_core.h"
  99. #include "http_log.h"
  100. #include "http_protocol.h"
  101. #include "http_request.h"
  102.  
  103. typedef struct auth_anon {
  104.     char *password;
  105.     struct auth_anon *next;
  106. } auth_anon;
  107.  
  108. typedef struct {
  109.  
  110.     auth_anon *auth_anon_passwords;
  111.     int auth_anon_nouserid;
  112.     int auth_anon_logemail;
  113.     int auth_anon_verifyemail;
  114.     int auth_anon_mustemail;
  115.     int auth_anon_authoritative;
  116.  
  117. } anon_auth_config_rec;
  118.  
  119. static void *create_anon_auth_dir_config(pool *p, char *d)
  120. {
  121.     anon_auth_config_rec *sec = (anon_auth_config_rec *)
  122.     ap_pcalloc(p, sizeof(anon_auth_config_rec));
  123.  
  124.     if (!sec)
  125.     return NULL;        /* no memory... */
  126.  
  127.     /* just to illustrate the defaults really. */
  128.     sec->auth_anon_passwords = NULL;
  129.  
  130.     sec->auth_anon_nouserid = 0;
  131.     sec->auth_anon_logemail = 1;
  132.     sec->auth_anon_verifyemail = 0;
  133.     sec->auth_anon_mustemail = 1;
  134.     sec->auth_anon_authoritative = 0;
  135.     return sec;
  136. }
  137.  
  138. static const char *anon_set_passwd_flag(cmd_parms *cmd,
  139.                  anon_auth_config_rec * sec, int arg)
  140. {
  141.     sec->auth_anon_mustemail = arg;
  142.     return NULL;
  143. }
  144.  
  145. static const char *anon_set_userid_flag(cmd_parms *cmd,
  146.                  anon_auth_config_rec * sec, int arg)
  147. {
  148.     sec->auth_anon_nouserid = arg;
  149.     return NULL;
  150. }
  151. static const char *anon_set_logemail_flag(cmd_parms *cmd,
  152.                    anon_auth_config_rec * sec, int arg)
  153. {
  154.     sec->auth_anon_logemail = arg;
  155.     return NULL;
  156. }
  157. static const char *anon_set_verifyemail_flag(cmd_parms *cmd,
  158.                       anon_auth_config_rec * sec, int arg)
  159. {
  160.     sec->auth_anon_verifyemail = arg;
  161.     return NULL;
  162. }
  163. static const char *anon_set_authoritative_flag(cmd_parms *cmd,
  164.                     anon_auth_config_rec * sec, int arg)
  165. {
  166.     sec->auth_anon_authoritative = arg;
  167.     return NULL;
  168. }
  169.  
  170. static const char *anon_set_string_slots(cmd_parms *cmd,
  171.                   anon_auth_config_rec * sec, char *arg)
  172. {
  173.  
  174.     auth_anon *first;
  175.  
  176.     if (!(*arg))
  177.     return "Anonymous string cannot be empty, use Anonymous_NoUserId instead";
  178.  
  179.     /* squeeze in a record */
  180.     first = sec->auth_anon_passwords;
  181.  
  182.     if (
  183.        (!(sec->auth_anon_passwords = (auth_anon *) ap_palloc(cmd->pool, sizeof(auth_anon)))) ||
  184.            (!(sec->auth_anon_passwords->password = arg))
  185.     )
  186.          return "Failed to claim memory for an anonymous password...";
  187.  
  188.     /* and repair the next */
  189.     sec->auth_anon_passwords->next = first;
  190.  
  191.     return NULL;
  192. }
  193.  
  194. static const command_rec anon_auth_cmds[] =
  195. {
  196.     {"Anonymous", anon_set_string_slots, NULL, OR_AUTHCFG, ITERATE,
  197.      "a space-separated list of user IDs"},
  198.     {"Anonymous_MustGiveEmail", anon_set_passwd_flag, NULL, OR_AUTHCFG, FLAG,
  199.      "Limited to 'on' or 'off'"},
  200.     {"Anonymous_NoUserId", anon_set_userid_flag, NULL, OR_AUTHCFG, FLAG,
  201.      "Limited to 'on' or 'off'"},
  202. {"Anonymous_VerifyEmail", anon_set_verifyemail_flag, NULL, OR_AUTHCFG, FLAG,
  203.  "Limited to 'on' or 'off'"},
  204.     {"Anonymous_LogEmail", anon_set_logemail_flag, NULL, OR_AUTHCFG, FLAG,
  205.      "Limited to 'on' or 'off'"},
  206.     {"Anonymous_Authoritative", anon_set_authoritative_flag, NULL, OR_AUTHCFG, FLAG,
  207.      "Limited to 'on' or 'off'"},
  208.  
  209.     {NULL}
  210. };
  211.  
  212. module MODULE_VAR_EXPORT anon_auth_module;
  213.  
  214. static int anon_authenticate_basic_user(request_rec *r)
  215. {
  216.     anon_auth_config_rec *sec =
  217.     (anon_auth_config_rec *) ap_get_module_config(r->per_dir_config,
  218.                            &anon_auth_module);
  219.     conn_rec *c = r->connection;
  220.     const char *sent_pw;
  221.     int res = DECLINED;
  222.  
  223.     if ((res = ap_get_basic_auth_pw(r, &sent_pw)))
  224.     return res;
  225.  
  226.     /* Ignore if we are not configured */
  227.     if (!sec->auth_anon_passwords)
  228.     return DECLINED;
  229.  
  230.     /* Do we allow an empty userID and/or is it the magic one
  231.      */
  232.  
  233.     if ((!(c->user[0])) && (sec->auth_anon_nouserid)) {
  234.     res = OK;
  235.     }
  236.     else {
  237.     auth_anon *p = sec->auth_anon_passwords;
  238.     res = DECLINED;
  239.     while ((res == DECLINED) && (p != NULL)) {
  240.         if (!(strcasecmp(c->user, p->password)))
  241.         res = OK;
  242.         p = p->next;
  243.     }
  244.     }
  245.     if (
  246.     /* username is OK */
  247.        (res == OK)
  248.     /* password been filled out ? */
  249.        && ((!sec->auth_anon_mustemail) || strlen(sent_pw))
  250.     /* does the password look like an email address ? */
  251.        && ((!sec->auth_anon_verifyemail)
  252.            || ((strpbrk("@", sent_pw) != NULL)
  253.            && (strpbrk(".", sent_pw) != NULL)))) {
  254.     if (sec->auth_anon_logemail && ap_is_initial_req(r)) {
  255.         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, r,
  256.             "Anonymous: Passwd <%s> Accepted",
  257.             sent_pw ? sent_pw : "\'none\'");
  258.     }
  259.     return OK;
  260.     }
  261.     else {
  262.     if (sec->auth_anon_authoritative) {
  263.         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
  264.             "Anonymous: Authoritative, Passwd <%s> not accepted",
  265.             sent_pw ? sent_pw : "\'none\'");
  266.         return AUTH_REQUIRED;
  267.     }
  268.     /* Drop out the bottom to return DECLINED */
  269.     }
  270.  
  271.     return DECLINED;
  272. }
  273.  
  274. static int check_anon_access(request_rec *r)
  275. {
  276. #ifdef NOTYET
  277.     conn_rec *c = r->connection;
  278.     anon_auth_config_rec *sec =
  279.     (anon_auth_config_rec *) ap_get_module_config(r->per_dir_config,
  280.                            &anon_auth_module);
  281.  
  282.     if (!sec->auth_anon)
  283.     return DECLINED;
  284.  
  285.     if (strcasecmp(r->connection->user, sec->auth_anon))
  286.     return DECLINED;
  287.  
  288.     return OK;
  289. #endif
  290.     return DECLINED;
  291. }
  292.  
  293.  
  294. module MODULE_VAR_EXPORT anon_auth_module =
  295. {
  296.     STANDARD_MODULE_STUFF,
  297.     NULL,            /* initializer */
  298.     create_anon_auth_dir_config,    /* dir config creater */
  299.     NULL,            /* dir merger ensure strictness */
  300.     NULL,            /* server config */
  301.     NULL,            /* merge server config */
  302.     anon_auth_cmds,        /* command table */
  303.     NULL,            /* handlers */
  304.     NULL,            /* filename translation */
  305.     anon_authenticate_basic_user,    /* check_user_id */
  306.     check_anon_access,        /* check auth */
  307.     NULL,            /* check access */
  308.     NULL,            /* type_checker */
  309.     NULL,            /* fixups */
  310.     NULL,            /* logger */
  311.     NULL,            /* header parser */
  312.     NULL,            /* child_init */
  313.     NULL,            /* child_exit */
  314.     NULL            /* post read-request */
  315. };
  316.