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

  1.  
  2. /*-
  3.  * Copyright (c) 1995 The Apache Group. All rights reserved.
  4.  * 
  5.  *
  6.  * Apache httpd license
  7.  * ====================
  8.  * 
  9.  *
  10.  * This is the license for the Apache Server. It covers all the
  11.  * files which come in this distribution, and should never be removed.
  12.  * 
  13.  * The "Apache Group" has based this server, called "Apache", on
  14.  * public domain code distributed under the name "NCSA httpd 1.3".
  15.  * 
  16.  * NCSA httpd 1.3 was placed in the public domain by the National Center 
  17.  * for Supercomputing Applications at the University of Illinois 
  18.  * at Urbana-Champaign.
  19.  * 
  20.  * As requested by NCSA we acknowledge,
  21.  * 
  22.  *  "Portions developed at the National Center for Supercomputing
  23.  *   Applications at the University of Illinois at Urbana-Champaign."
  24.  *
  25.  * Copyright on the sections of code added by the "Apache Group" belong
  26.  * to the "Apache Group" and/or the original authors. The "Apache Group" and
  27.  * authors hereby grant permission for their code, along with the
  28.  * public domain NCSA code, to be distributed under the "Apache" name.
  29.  * 
  30.  * Reuse of "Apache Group" code outside of the Apache distribution should
  31.  * be acknowledged with the following quoted text, to be included with any new
  32.  * work;
  33.  * 
  34.  * "Portions developed by the "Apache Group", taken with permission 
  35.  *  from the Apache Server   http://www.apache.org/apache/   "
  36.  *
  37.  *
  38.  * Permission is hereby granted to anyone to redistribute Apache under
  39.  * the "Apache" name. We do not grant permission for the resale of Apache, but
  40.  * we do grant permission for vendors to bundle Apache free with other software,
  41.  * or to charge a reasonable price for redistribution, provided it is made
  42.  * clear that Apache is free. Permission is also granted for vendors to 
  43.  * sell support for for Apache. We explicitly forbid the redistribution of 
  44.  * Apache under any other name.
  45.  * 
  46.  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  47.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  48.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  49.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  50.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  51.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  52.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  53.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  54.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  55.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  56.  * SUCH DAMAGE.
  57.  * 
  58.  */
  59.  
  60.  
  61.  
  62. /*
  63.  * http_auth_msql: authentication
  64.  * 
  65.  * Rob McCool & Brian Behlendorf.
  66.  * 
  67.  * Adapted to Shambhala by rst.
  68.  */
  69.  
  70. /*
  71.  * converted to use mSQL by Vivek Khera <khera@kciLink.com>
  72.  * only has user/passwords in mSQL database.  A suitable table would be:
  73.  *
  74.  * CREATE TABLE user_info (
  75.  *   user CHAR(30) PRIMARY KEY,
  76.  *   password CHAR(20) NOT NULL,
  77.  *     [ any other fields if needed ]
  78.  * )
  79.  *
  80.  * User must be a unique, non-empty field.  Length is however long you
  81.  * want it to be.  Password length of 20 follows new-style crypt() usage;
  82.  * the older crypt uses shorter encrypted passwords.  Any other fields in
  83.  * the named table will be ignored.  The actual field names are configurable
  84.  * using the parameters listed below.  The defaults are "user" and "password"
  85.  * respectively, for the user ID and the password.  If you like to store
  86.  * passwords in clear text, set AuthMSQLCryptedPasswords to Off.  I think this
  87.  * is a bad idea, but people have requested it.
  88.  *
  89.  * Usage in per-directory access conf file:
  90.  *
  91.  *  AuthName mSQL Testing
  92.  *  AuthType Basic
  93.  *  AuthGroupFile /dev/null
  94.  *  AuthMSQLHost localhost
  95.  *  AuthMSQLDB www_data
  96.  *  AuthMSQLUserTable user_info
  97.  *
  98.  *  <Limit GET POST>
  99.  *  require valid-user
  100.  *  </Limit>
  101.  *
  102.  * The following parameters are optional in the config file.  The defaults
  103.  * values are shown here.
  104.  *
  105.  *  AuthMSQLNameField user
  106.  *  AuthMSQLPasswordField password
  107.  *  AuthMSQLCryptedPasswords On
  108.  * 
  109.  * the Host of "localhost" means use the mSQL socket instead of a TCP
  110.  * connection to the database.  DB is the database name on the server,
  111.  * and UserTable is the actual table name within that database.
  112.  *
  113.  * Groups are not implemented in mSQL.  Use the original flat file or
  114.  * the Apache DBM version.
  115.  *
  116.  * $Id: mod_auth_msql.c,v 1.7 1995/12/02 18:47:30 khera Exp $
  117.  */
  118.  
  119. #include "httpd.h"
  120. #include "http_config.h"
  121. #include "http_core.h"
  122. #include "http_log.h"
  123. #include "http_protocol.h"
  124. #include <msql.h>
  125.  
  126. /*
  127.  * msqlhost is host name. localhost means use Unix Domain socket for mSQL.
  128.  * msqlDB is the database name on that host.
  129.  * msqlpwtable is the table name for passwords.  uses fields "user","password".
  130.  * The "user" field must be "not null" and unique.  "password" is encrypted.
  131.  * the user field must not have a ' (single quote) character in it.
  132.  */
  133. typedef struct  {
  134.     char *msqlhost;
  135.     char *msqlDB;
  136.     char *msqlpwtable;
  137.     char *msqlNameField;
  138.     char *msqlPasswordField;
  139.     int  msqlCrypted;
  140. } msql_auth_config_rec;
  141.  
  142. static
  143. void *create_msql_auth_dir_config (pool *p, char *d)
  144. {
  145.   msql_auth_config_rec *m = pcalloc (p, sizeof(msql_auth_config_rec));
  146.   if (!m) return NULL;        /* failure to get memory is a bad thing */
  147.  
  148.   /* need these defaults for compatibility with prior versions */
  149.   m->msqlNameField = "user";
  150.   m->msqlPasswordField = "password";
  151.   m->msqlCrypted = 1;
  152.   return (void *)m;
  153. }
  154.  
  155. static
  156. char *set_crypted_password (cmd_parms *cmd, void *mrec, int arg) {
  157.   ((msql_auth_config_rec *)mrec)->msqlCrypted = arg;
  158.   return NULL;
  159. }
  160.  
  161. static
  162. command_rec msql_auth_cmds[] = {
  163. { "AuthMSQLHost", set_string_slot,
  164.     (void*)XtOffsetOf(msql_auth_config_rec, msqlhost),
  165.     OR_AUTHCFG, TAKE1, "mSQL server hostname" },
  166. { "AuthMSQLDB", set_string_slot,
  167.     (void*)XtOffsetOf(msql_auth_config_rec, msqlDB),
  168.     OR_AUTHCFG, TAKE1, "mSQL database name" },
  169. { "AuthMSQLUserTable", set_string_slot,
  170.     (void*)XtOffsetOf(msql_auth_config_rec, msqlpwtable),
  171.     OR_AUTHCFG, TAKE1, "mSQL table name" },
  172. { "AuthMSQLNameField", set_string_slot,
  173.     (void*)XtOffsetOf(msql_auth_config_rec, msqlNameField),
  174.     OR_AUTHCFG, TAKE1, "mSQL User ID field name within table" },
  175. { "AuthMSQLPasswordField", set_string_slot,
  176.     (void*)XtOffsetOf(msql_auth_config_rec, msqlPasswordField),
  177.     OR_AUTHCFG, TAKE1, "mSQL Password field name within table" },
  178. { "AuthMSQLCryptedPasswords", set_crypted_password,
  179.     NULL, OR_AUTHCFG, FLAG, "mSQL passwords are stored encrypted if On" },
  180. { NULL }
  181. };
  182.  
  183. module msql_auth_module;
  184.  
  185. /*
  186.  * get password from database
  187.  */
  188. static
  189. char *get_msql_pw(request_rec *r, char *user, msql_auth_config_rec *m) {
  190.     int msqlSock;
  191.     m_result *result;
  192.     m_row data;
  193.     char *pw = NULL;
  194.     char *host;
  195.     char query[MAX_STRING_LEN];
  196.  
  197.     if (!m->msqlhost || strcmp(m->msqlhost,"localhost") == 0) {
  198.       host = NULL;
  199.     } else {
  200.       host = m->msqlhost;
  201.     }
  202.     
  203.     if((msqlSock=msqlConnect(host)) < 0) {
  204.         log_reason (msqlErrMsg, r->uri, r);
  205.     return NULL;
  206.     }
  207.  
  208.     if (msqlSelectDB(msqlSock,m->msqlDB) < 0) {
  209.         log_reason (msqlErrMsg, r->uri, r);
  210.     return NULL;
  211.     }
  212.  
  213.     sprintf(query,"SELECT %s FROM %s WHERE %s = '%s'",
  214.         m->msqlPasswordField, m->msqlpwtable,
  215.         m->msqlNameField, user);
  216.     if (msqlQuery(msqlSock, query) < 0) {
  217.         log_reason (msqlErrMsg, r->uri, r);
  218.     return NULL;
  219.     }
  220.  
  221.     result = msqlStoreResult();
  222.     if (msqlNumRows(result) == 1) {
  223.         data = msqlFetchRow(result);
  224.     if (data[0]) {
  225.       pw = palloc (r->pool, strlen(data[0]) + 1);
  226.       strcpy(pw,data[0]);
  227.     } else {        /* no password in mSQL table -- returns NULL */
  228.       log_reason ("mSQL user has no valid password", r->uri, r);
  229.       return NULL;
  230.     }
  231.     }
  232.  
  233.     msqlFreeResult(result);
  234.     msqlClose(msqlSock);
  235.  
  236.     return pw; 
  237. }
  238.  
  239. static
  240. int msql_authenticate_basic_user (request_rec *r)
  241. {
  242.     msql_auth_config_rec *sec =
  243.       (msql_auth_config_rec *)get_module_config (r->per_dir_config,
  244.                         &msql_auth_module);
  245.     conn_rec *c = r->connection;
  246.     char *sent_pw, *real_pw;
  247.     char errstr[MAX_STRING_LEN];
  248.     int res;
  249.     
  250.     if ((res = get_basic_auth_pw (r, &sent_pw)))
  251.         return res;
  252.     
  253.     if(!sec->msqlpwtable)
  254.         return DECLINED;
  255.     
  256.     if(!(real_pw = get_msql_pw(r, c->user, sec))) {
  257.         sprintf(errstr,"mSQL user `%s' not found", c->user);
  258.     log_reason (errstr, r->uri, r);
  259.     note_basic_auth_failure (r);
  260.     return AUTH_REQUIRED;
  261.     }    
  262.  
  263.     if(strcmp(real_pw, sec->msqlCrypted ? crypt(sent_pw,real_pw) : sent_pw)) {
  264.       sprintf(errstr,"user %s: password mismatch",c->user);
  265.       log_reason (errstr, r->uri, r);
  266.       note_basic_auth_failure (r);
  267.       return AUTH_REQUIRED;
  268.     }
  269.     return OK;
  270. }
  271.     
  272.  
  273. module msql_auth_module = {
  274.    STANDARD_MODULE_STUFF,
  275.    NULL,            /* initializer */
  276.    create_msql_auth_dir_config,    /* dir config creater */
  277.    NULL,            /* dir merger --- default is to override */
  278.    NULL,            /* server config */
  279.    NULL,            /* merge server config */
  280.    msql_auth_cmds,        /* command table */
  281.    NULL,            /* handlers */
  282.    NULL,            /* filename translation */
  283.    msql_authenticate_basic_user,    /* check_user_id */
  284.    NULL,            /* check auth */
  285.    NULL,            /* check access */
  286.    NULL,            /* type_checker */
  287.    NULL,            /* fixups */
  288.    NULL                /* logger */
  289. };
  290.