home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / linux / apache / contrib / modules / probably_obsolete / mod_auth_db.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-11  |  8.5 KB  |  268 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1995 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.
  24.  *
  25.  * 5. Redistributions of any form whatsoever must retain the following
  26.  *    acknowledgment:
  27.  *    "This product includes software developed by the Apache Group
  28.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  29.  *
  30.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  31.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  34.  * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  42.  * ====================================================================
  43.  *
  44.  * This software consists of voluntary contributions made by many
  45.  * individuals on behalf of the Apache Group and was originally based
  46.  * on public domain software written at the National Center for
  47.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  48.  * For more information on the Apache Group and the Apache HTTP server
  49.  * project, please see <http://www.apache.org/>.
  50.  *
  51.  */
  52.  
  53.  
  54. /*
  55.  * mod_auth_db: authentication
  56.  * 
  57.  * Original work by Rob McCool & Brian Behlendorf.
  58.  * 
  59.  * Adapted to Apache by rst (mod_auth_dbm)
  60.  *
  61.  * Adapted for Berkeley DB by Andrew Cohen 
  62.  *
  63.  * mod_auth_db was based on mod_auth_dbm.
  64.  * 
  65.  * Warning, this is not a drop in replacement for mod_auth_dbm, 
  66.  * for people wanting to switch from dbm to Berkeley DB.
  67.  * It requires the use of AuthDBUserFile and AuthDBGroupFile
  68.  *           instead of   AuthDBMUserFile    AuthDBMGroupFile
  69.  *
  70.  * Also, in the configuration file you need to specify
  71.  *  db_auth_module rather than dbm_auth_module
  72.  *
  73.  * On some BSD systems (e.g. FreeBSD and NetBSD) dbm is automatically
  74.  * mapped to Berkeley DB. You can use either mod_auth_dbm or
  75.  * mod_auth_db. The latter makes it more obvious that it's Berkeley.
  76.  */
  77.  
  78. #include "httpd.h"
  79. #include "http_config.h"
  80. #include "http_core.h"
  81. #include "http_log.h"
  82. #include "http_protocol.h"
  83. #include <db.h>
  84.  
  85. typedef struct  {
  86.  
  87.     char *auth_dbpwfile;
  88.     char *auth_dbgrpfile;
  89.  
  90. } db_auth_config_rec;
  91.  
  92. void *create_db_auth_dir_config (pool *p, char *d)
  93. {
  94.     return pcalloc (p, sizeof(db_auth_config_rec));
  95. }
  96.  
  97. command_rec db_auth_cmds[] = {
  98. { "AuthDBUserFile", set_string_slot,
  99.     (void*)XtOffsetOf(db_auth_config_rec, auth_dbpwfile),
  100.     OR_AUTHCFG, TAKE1, NULL },
  101. { "AuthDBGroupFile", set_string_slot,
  102.     (void*)XtOffsetOf(db_auth_config_rec, auth_dbgrpfile),
  103.     OR_AUTHCFG, TAKE1, NULL },
  104. { NULL }
  105. };
  106.  
  107. module db_auth_module;
  108.  
  109. char *get_db_pw(request_rec *r, char *user, const char *auth_dbpwfile) {
  110.     DB *f; 
  111.     DBT d, q; 
  112.     char *pw = NULL;
  113.  
  114.     q.data = user; 
  115.     q.size = strlen(q.data); 
  116.     
  117.     if(!(f=dbopen(auth_dbpwfile,O_RDONLY,0664,DB_HASH,NULL))) {
  118.         log_reason ("could not open db auth file", (char *) auth_dbpwfile, r);
  119.     return NULL;
  120.     }
  121.  
  122.     if (!((f->get)(f,&q,&d,0))) {
  123.         pw = palloc (r->pool, d.size + 1);
  124.     strncpy(pw,d.data,d.size);
  125.     pw[d.size] = '\0';         /* Terminate the string */
  126.     }
  127.  
  128.     (f->close)(f);
  129.     return pw; 
  130. }
  131.  
  132. /* We do something strange with the group file.  If the group file
  133.  * contains any : we assume the format is
  134.  *      key=username value=":"groupname [":"anything here is ignored]
  135.  * otherwise we now (0.8.14+) assume that the format is
  136.  *      key=username value=groupname
  137.  * The first allows the password and group files to be the same 
  138.  * physical DB file;   key=username value=password":"groupname[":"anything]
  139.  *
  140.  * mark@telescope.org, 22Sep95
  141.  */
  142.  
  143. char  *get_db_grp(request_rec *r, char *user, const char *auth_dbgrpfile) {
  144.     char *grp_data = get_db_pw (r, user, auth_dbgrpfile);
  145.     char *grp_colon; char *grp_colon2;
  146.  
  147.     if (grp_data == NULL) return NULL;
  148.     
  149.     if ((grp_colon = strchr(grp_data, ':'))!=NULL) {
  150.         grp_colon2 = strchr(++grp_colon, ':');
  151.         if (grp_colon2) *grp_colon2='\0';
  152.         return grp_colon;
  153.     }
  154.     return grp_data;
  155. }
  156.  
  157. int db_authenticate_basic_user (request_rec *r)
  158. {
  159.     db_auth_config_rec *sec =
  160.       (db_auth_config_rec *)get_module_config (r->per_dir_config,
  161.                         &db_auth_module);
  162.     conn_rec *c = r->connection;
  163.     char *sent_pw, *real_pw, *colon_pw;
  164.     char errstr[MAX_STRING_LEN];
  165.     int res;
  166.     
  167.     if ((res = get_basic_auth_pw (r, &sent_pw)))
  168.         return res;
  169.     
  170.     if(!sec->auth_dbpwfile)
  171.         return DECLINED;
  172.     
  173.     if(!(real_pw = get_db_pw(r, c->user, sec->auth_dbpwfile))) {
  174.         sprintf(errstr,"DB user %s not found", c->user);
  175.     log_reason (errstr, r->filename, r);
  176.     note_basic_auth_failure (r);
  177.     return AUTH_REQUIRED;
  178.     }    
  179.     /* Password is up to first : if exists */
  180.     colon_pw = strchr(real_pw,':');
  181.     if (colon_pw) *colon_pw='\0';   
  182.     /* anyone know where the prototype for crypt is? */
  183.     if(strcmp(real_pw,(char *)crypt(sent_pw,real_pw))) {
  184.         sprintf(errstr,"user %s: password mismatch",c->user);
  185.     log_reason (errstr, r->uri, r);
  186.     note_basic_auth_failure (r);
  187.     return AUTH_REQUIRED;
  188.     }
  189.     return OK;
  190. }
  191.     
  192. /* Checking ID */
  193.     
  194. int db_check_auth(request_rec *r) {
  195.     db_auth_config_rec *sec =
  196.       (db_auth_config_rec *)get_module_config (r->per_dir_config,
  197.                         &db_auth_module);
  198.     char *user = r->connection->user;
  199.     int m = r->method_number;
  200.     char errstr[MAX_STRING_LEN];
  201.     
  202.     array_header *reqs_arr = requires (r);
  203.     require_line *reqs = reqs_arr ? (require_line *)reqs_arr->elts : NULL;
  204.  
  205.     register int x;
  206.     char *t, *w;
  207.  
  208.     if (!sec->auth_dbgrpfile) return DECLINED;
  209.     if (!reqs_arr) return DECLINED;
  210.     
  211.     for(x=0; x < reqs_arr->nelts; x++) {
  212.       
  213.     if (! (reqs[x].method_mask & (1 << m))) continue;
  214.     
  215.         t = reqs[x].requirement;
  216.         w = getword(r->pool, &t, ' ');
  217.     
  218.         if(!strcmp(w,"group") && sec->auth_dbgrpfile) {
  219.            char *orig_groups,*groups,*v;
  220.  
  221.            if (!(groups = get_db_grp(r, user, sec->auth_dbgrpfile))) {
  222.                sprintf(errstr,"user %s not in DB group file %s",
  223.                user, sec->auth_dbgrpfile);
  224.            log_reason (errstr, r->filename, r);
  225.            note_basic_auth_failure (r);
  226.            return AUTH_REQUIRED;
  227.            }
  228.            orig_groups = groups;
  229.            while(t[0]) {
  230.                w = getword(r->pool, &t, ' ');
  231.                groups = orig_groups;
  232.                while(groups[0]) {
  233.                    v = getword(r->pool, &groups,',');
  234.                    if(!strcmp(v,w))
  235.                        return OK;
  236.                }
  237.            }
  238.            sprintf(errstr,"user %s not in right group",user);
  239.        log_reason (errstr, r->filename, r);
  240.            note_basic_auth_failure(r);
  241.        return AUTH_REQUIRED;
  242.        }
  243.     }
  244.     
  245.     return DECLINED;
  246. }
  247.  
  248.  
  249. module db_auth_module = {
  250.    STANDARD_MODULE_STUFF,
  251.    NULL,            /* initializer */
  252.    create_db_auth_dir_config,    /* dir config creater */
  253.    NULL,            /* dir merger --- default is to override */
  254.    NULL,            /* server config */
  255.    NULL,            /* merge server config */
  256.    db_auth_cmds,        /* command table */
  257.    NULL,            /* handlers */
  258.    NULL,            /* filename translation */
  259.    db_authenticate_basic_user,    /* check_user_id */
  260.    db_check_auth,        /* check auth */
  261.    NULL,            /* check access */
  262.    NULL,            /* type_checker */
  263.    NULL,            /* fixups */
  264.    NULL                /* logger */
  265. };
  266.  
  267.  
  268.