mod_authn_dbm.c revision 807dcf9d7b1b1e1f34b8274d08a40f5435987110
08cb74ca432a8c24e39f17dedce527e6a47b8001jerenkrantz/* ====================================================================
08cb74ca432a8c24e39f17dedce527e6a47b8001jerenkrantz * The Apache Software License, Version 1.1
ab2c1c1c83ec91415565da5a71fbc15d9685caa6fielding *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * reserved.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Redistribution and use in source and binary forms, with or without
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * modification, are permitted provided that the following conditions
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * are met:
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * 1. Redistributions of source code must retain the above copyright
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * notice, this list of conditions and the following disclaimer.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * 2. Redistributions in binary form must reproduce the above copyright
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * notice, this list of conditions and the following disclaimer in
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * the documentation and/or other materials provided with the
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * distribution.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * 3. The end-user documentation included with the redistribution,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * if any, must include the following acknowledgment:
b6055b7832a0e4d0818416252fff5925aaebae4brbb * "This product includes software developed by the
2d71630471d1c23f0137309e3c3957c633ecbfd6rbb * Apache Software Foundation (http://www.apache.org/)."
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Alternately, this acknowledgment may appear in the software itself,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * if and wherever such third-party acknowledgments normally appear.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * 4. The names "Apache" and "Apache Software Foundation" must
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * not be used to endorse or promote products derived from this
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * software without prior written permission. For written
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * permission, please contact apache@apache.org.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * 5. Products derived from this software may not be called "Apache",
3d96ee83babeec32482c9082c9426340cee8c44dwrowe * nor may "Apache" appear in their name, without prior written
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * permission of the Apache Software Foundation.
2d84861186d70e5396751ba308bb90c8a7db47acnd *
2d84861186d70e5396751ba308bb90c8a7db47acnd * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
2d84861186d70e5396751ba308bb90c8a7db47acnd * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2d84861186d70e5396751ba308bb90c8a7db47acnd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2d84861186d70e5396751ba308bb90c8a7db47acnd * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
2d84861186d70e5396751ba308bb90c8a7db47acnd * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2d84861186d70e5396751ba308bb90c8a7db47acnd * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * SUCH DAMAGE.
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick * ====================================================================
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * This software consists of voluntary contributions made by many
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * individuals on behalf of the Apache Software Foundation. For more
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * information on the Apache Software Foundation, please see
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron * <http://www.apache.org/>.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
066877f1a045103acfdd376d48cdd473c33f409bdougm * Portions of this software are based upon public domain software
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * originally written at the National Center for Supercomputing Applications,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * University of Illinois, Urbana-Champaign.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
2d84861186d70e5396751ba308bb90c8a7db47acnd
2d84861186d70e5396751ba308bb90c8a7db47acnd/*
2d84861186d70e5396751ba308bb90c8a7db47acnd * http_auth: authentication
2d84861186d70e5396751ba308bb90c8a7db47acnd *
2d84861186d70e5396751ba308bb90c8a7db47acnd * Rob McCool & Brian Behlendorf.
2d84861186d70e5396751ba308bb90c8a7db47acnd *
2d84861186d70e5396751ba308bb90c8a7db47acnd * Adapted to Apache by rst.
2d84861186d70e5396751ba308bb90c8a7db47acnd *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick#define APR_WANT_STRFUNC
4f9a74ad7e44b0464f7cf56525a205d788becacbtrawick#include "apr_want.h"
2d84861186d70e5396751ba308bb90c8a7db47acnd#include "apr_strings.h"
2d84861186d70e5396751ba308bb90c8a7db47acnd#include "apr_dbm.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "apr_md5.h" /* for apr_password_validate */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "ap_provider.h"
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm#include "httpd.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "http_config.h"
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron#include "http_core.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "http_log.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "http_protocol.h"
2d84861186d70e5396751ba308bb90c8a7db47acnd#include "http_request.h" /* for ap_hook_(check_user_id | auth_checker)*/
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "mod_auth.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmtypedef struct {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char *pwfile;
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron char *dbmtype;
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron} authn_dbm_config_rec;
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic void *create_authn_dbm_dir_config(apr_pool_t *p, char *d)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
2d84861186d70e5396751ba308bb90c8a7db47acnd authn_dbm_config_rec *conf = apr_palloc(p, sizeof(*conf));
2d84861186d70e5396751ba308bb90c8a7db47acnd
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding conf->pwfile = NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding conf->dbmtype = "default";
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0958c822c32923b9848a73b2f4c94eab07fb416drbb return conf;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowestatic const char *set_dbm_type(cmd_parms *cmd,
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe void *dir_config,
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe const char *arg)
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe{
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe authn_dbm_config_rec *conf = dir_config;
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe conf->dbmtype = apr_pstrdup(cmd->pool, arg);
d54c6537f5f5a7bc54ec37ce8dbe04193605043astoddard return NULL;
957b9b3f5e47c6cc7c2d8d9e2224c7364e966c70rbb}
e8f95a682820a599fe41b22977010636be5c2717jim
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowestatic const command_rec authn_dbm_cmds[] =
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe{
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe AP_INIT_TAKE1("AuthDBMUserFile", ap_set_file_slot,
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe (void *)APR_OFFSETOF(authn_dbm_config_rec, pwfile),
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe OR_AUTHCFG, "dbm database file containing user IDs and passwords"),
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron AP_INIT_TAKE1("AuthDBMType", set_dbm_type,
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe NULL,
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe OR_AUTHCFG, "what type of DBM file the user file is"),
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe {NULL}
e95f5a6ebefb83d0e8a82cbc4db7d354957a817dben};
2d84861186d70e5396751ba308bb90c8a7db47acnd
2d84861186d70e5396751ba308bb90c8a7db47acndmodule AP_MODULE_DECLARE_DATA authn_dbm_module;
2d84861186d70e5396751ba308bb90c8a7db47acnd
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowestatic apr_status_t fetch_dbm(const char *dbmtype, const char *dbmfile,
e8f95a682820a599fe41b22977010636be5c2717jim const char *user, apr_datum_t *val,
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe apr_pool_t *pool)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe apr_dbm_t *f;
2d84861186d70e5396751ba308bb90c8a7db47acnd apr_datum_t key;
2d84861186d70e5396751ba308bb90c8a7db47acnd apr_status_t rv;
2d84861186d70e5396751ba308bb90c8a7db47acnd
2d84861186d70e5396751ba308bb90c8a7db47acnd rv = apr_dbm_open_ex(&f, dbmtype, dbmfile, APR_DBM_READONLY,
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe APR_OS_DEFAULT, pool);
e8f95a682820a599fe41b22977010636be5c2717jim
e8f95a682820a599fe41b22977010636be5c2717jim if (rv != APR_SUCCESS) {
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe return rv;
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron }
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe key.dptr = (char*)user;
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe#ifndef NETSCAPE_DBM_COMPAT
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe key.dsize = strlen(key.dptr);
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron#else
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm key.dsize = strlen(key.dptr) + 1;
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe#endif
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron rv = apr_dbm_fetch(f, key, val);
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe apr_dbm_close(f);
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return rv;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm}
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic authn_status check_dbm_pw(request_rec *r, const char *user,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *password)
c0905aec36b287200b8a255b33fba6c975752bdfgstein{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding authn_dbm_config_rec *conf = ap_get_module_config(r->per_dir_config,
d54c6537f5f5a7bc54ec37ce8dbe04193605043astoddard &authn_dbm_module);
d54c6537f5f5a7bc54ec37ce8dbe04193605043astoddard apr_datum_t dbm_pw;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_status_t rv;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char *dbm_password;
d54c6537f5f5a7bc54ec37ce8dbe04193605043astoddard
d54c6537f5f5a7bc54ec37ce8dbe04193605043astoddard rv = fetch_dbm(conf->dbmtype, conf->pwfile, user, &dbm_pw, r->pool);
d54c6537f5f5a7bc54ec37ce8dbe04193605043astoddard
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (rv != APR_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "could not open dbm (type %s) auth file: %s",
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe conf->dbmtype, conf->pwfile);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return AUTH_GENERAL_ERROR;
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe if (dbm_pw.dptr) {
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe dbm_password = apr_pstrmemdup(r->pool, dbm_pw.dptr, dbm_pw.dsize);
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe }
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe if (!dbm_password) {
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe return AUTH_USER_NOT_FOUND;
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron }
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron rv = apr_password_validate(password, dbm_password);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe if (rv != APR_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return AUTH_DENIED;
a4be7c7d73005dedac190ce6cb68ab4dd38383bbcolm }
a4be7c7d73005dedac190ce6cb68ab4dd38383bbcolm
e8f95a682820a599fe41b22977010636be5c2717jim return AUTH_GRANTED;
a4be7c7d73005dedac190ce6cb68ab4dd38383bbcolm}
a4be7c7d73005dedac190ce6cb68ab4dd38383bbcolm
a4be7c7d73005dedac190ce6cb68ab4dd38383bbcolmstatic const authn_provider authn_dbm_provider =
e8f95a682820a599fe41b22977010636be5c2717jim{
e8f95a682820a599fe41b22977010636be5c2717jim &check_dbm_pw,
a4be7c7d73005dedac190ce6cb68ab4dd38383bbcolm NULL, /* No realm support yet. */
a4be7c7d73005dedac190ce6cb68ab4dd38383bbcolm};
a4be7c7d73005dedac190ce6cb68ab4dd38383bbcolm
eea521297270de3f9ae70d8822f8665c513de574ndstatic void register_hooks(apr_pool_t *p)
eea521297270de3f9ae70d8822f8665c513de574nd{
3f65070bf58882ab06bfa08cc4e04c7c90ac04f8wrowe ap_register_provider(p, AUTHN_PROVIDER_GROUP, "dbm", &authn_dbm_provider);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
e8f95a682820a599fe41b22977010636be5c2717jimmodule AP_MODULE_DECLARE_DATA authn_dbm_module =
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding STANDARD20_MODULE_STUFF,
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron create_authn_dbm_dir_config, /* dir config creater */
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron NULL, /* dir merger --- default is to override */
06a537b50a7a5d4f8543231d2b6067b8f6805dd3aaron NULL, /* server config */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding NULL, /* merge server config */
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe authn_dbm_cmds, /* command apr_table_t */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding register_hooks /* register hooks */
066877f1a045103acfdd376d48cdd473c33f409bdougm};
066877f1a045103acfdd376d48cdd473c33f409bdougm