|
Packit |
90a5c9 |
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
Packit |
90a5c9 |
* contributor license agreements. See the NOTICE file distributed with
|
|
Packit |
90a5c9 |
* this work for additional information regarding copyright ownership.
|
|
Packit |
90a5c9 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
Packit |
90a5c9 |
* (the "License"); you may not use this file except in compliance with
|
|
Packit |
90a5c9 |
* the License. You may obtain a copy of the License at
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
90a5c9 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
90a5c9 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
90a5c9 |
* See the License for the specific language governing permissions and
|
|
Packit |
90a5c9 |
* limitations under the License.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* http_auth: authentication
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Rob McCool & Brian Behlendorf.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Adapted to Apache by rst.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#define APR_WANT_STRFUNC
|
|
Packit |
90a5c9 |
#include "apr_want.h"
|
|
Packit |
90a5c9 |
#include "apr_strings.h"
|
|
Packit |
90a5c9 |
#include "apr_dbm.h"
|
|
Packit |
90a5c9 |
#include "apr_md5.h" /* for apr_password_validate */
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "ap_provider.h"
|
|
Packit |
90a5c9 |
#include "httpd.h"
|
|
Packit |
90a5c9 |
#include "http_config.h"
|
|
Packit |
90a5c9 |
#include "http_core.h"
|
|
Packit |
90a5c9 |
#include "http_log.h"
|
|
Packit |
90a5c9 |
#include "http_protocol.h"
|
|
Packit |
90a5c9 |
#include "http_request.h" /* for ap_hook_(check_user_id | auth_checker)*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "mod_auth.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static APR_OPTIONAL_FN_TYPE(ap_authn_cache_store) *authn_cache_store = NULL;
|
|
Packit |
90a5c9 |
#define AUTHN_CACHE_STORE(r,user,realm,data) \
|
|
Packit |
90a5c9 |
if (authn_cache_store != NULL) \
|
|
Packit |
90a5c9 |
authn_cache_store((r), "dbm", (user), (realm), (data))
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct {
|
|
Packit |
90a5c9 |
const char *pwfile;
|
|
Packit |
90a5c9 |
const char *dbmtype;
|
|
Packit |
90a5c9 |
} authn_dbm_config_rec;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *create_authn_dbm_dir_config(apr_pool_t *p, char *d)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authn_dbm_config_rec *conf = apr_palloc(p, sizeof(*conf));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
conf->pwfile = NULL;
|
|
Packit |
90a5c9 |
conf->dbmtype = "default";
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return conf;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const command_rec authn_dbm_cmds[] =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
AP_INIT_TAKE1("AuthDBMUserFile", ap_set_file_slot,
|
|
Packit |
90a5c9 |
(void *)APR_OFFSETOF(authn_dbm_config_rec, pwfile),
|
|
Packit |
90a5c9 |
OR_AUTHCFG, "dbm database file containing user IDs and passwords"),
|
|
Packit |
90a5c9 |
AP_INIT_TAKE1("AuthDBMType", ap_set_string_slot,
|
|
Packit |
90a5c9 |
(void *)APR_OFFSETOF(authn_dbm_config_rec, dbmtype),
|
|
Packit |
90a5c9 |
OR_AUTHCFG, "what type of DBM file the user file is"),
|
|
Packit |
90a5c9 |
{NULL}
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
module AP_MODULE_DECLARE_DATA authn_dbm_module;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t fetch_dbm_value(const char *dbmtype, const char *dbmfile,
|
|
Packit |
90a5c9 |
const char *user, char **value,
|
|
Packit |
90a5c9 |
apr_pool_t *pool)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_dbm_t *f;
|
|
Packit |
90a5c9 |
apr_datum_t key, val;
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = apr_dbm_open_ex(&f, dbmtype, dbmfile, APR_DBM_READONLY,
|
|
Packit |
90a5c9 |
APR_OS_DEFAULT, pool);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
key.dptr = (char*)user;
|
|
Packit |
90a5c9 |
#ifndef NETSCAPE_DBM_COMPAT
|
|
Packit |
90a5c9 |
key.dsize = strlen(key.dptr);
|
|
Packit |
90a5c9 |
#else
|
|
Packit |
90a5c9 |
key.dsize = strlen(key.dptr) + 1;
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
*value = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (apr_dbm_fetch(f, key, &val) == APR_SUCCESS && val.dptr) {
|
|
Packit |
90a5c9 |
*value = apr_pstrmemdup(pool, val.dptr, val.dsize);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_dbm_close(f);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return rv;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static authn_status check_dbm_pw(request_rec *r, const char *user,
|
|
Packit |
90a5c9 |
const char *password)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authn_dbm_config_rec *conf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&authn_dbm_module);
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
char *dbm_password;
|
|
Packit |
90a5c9 |
char *colon_pw;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = fetch_dbm_value(conf->dbmtype, conf->pwfile, user, &dbm_password,
|
|
Packit |
90a5c9 |
r->pool);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01754)
|
|
Packit |
90a5c9 |
"could not open dbm (type %s) auth file: %s",
|
|
Packit |
90a5c9 |
conf->dbmtype, conf->pwfile);
|
|
Packit |
90a5c9 |
return AUTH_GENERAL_ERROR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!dbm_password) {
|
|
Packit |
90a5c9 |
return AUTH_USER_NOT_FOUND;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
colon_pw = ap_strchr(dbm_password, ':');
|
|
Packit |
90a5c9 |
if (colon_pw) {
|
|
Packit |
90a5c9 |
*colon_pw = '\0';
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
AUTHN_CACHE_STORE(r, user, NULL, dbm_password);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = apr_password_validate(password, dbm_password);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return AUTH_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return AUTH_GRANTED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static authn_status get_dbm_realm_hash(request_rec *r, const char *user,
|
|
Packit |
90a5c9 |
const char *realm, char **rethash)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authn_dbm_config_rec *conf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&authn_dbm_module);
|
|
Packit |
90a5c9 |
apr_status_t rv;
|
|
Packit |
90a5c9 |
char *dbm_hash;
|
|
Packit |
90a5c9 |
char *colon_hash;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = fetch_dbm_value(conf->dbmtype, conf->pwfile,
|
|
Packit |
90a5c9 |
apr_pstrcat(r->pool, user, ":", realm, NULL),
|
|
Packit |
90a5c9 |
&dbm_hash, r->pool);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (rv != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01755)
|
|
Packit |
90a5c9 |
"Could not open dbm (type %s) hash file: %s",
|
|
Packit |
90a5c9 |
conf->dbmtype, conf->pwfile);
|
|
Packit |
90a5c9 |
return AUTH_GENERAL_ERROR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!dbm_hash) {
|
|
Packit |
90a5c9 |
return AUTH_USER_NOT_FOUND;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
colon_hash = ap_strchr(dbm_hash, ':');
|
|
Packit |
90a5c9 |
if (colon_hash) {
|
|
Packit |
90a5c9 |
*colon_hash = '\0';
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
*rethash = dbm_hash;
|
|
Packit |
90a5c9 |
AUTHN_CACHE_STORE(r, user, realm, dbm_hash);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return AUTH_USER_FOUND;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const authn_provider authn_dbm_provider =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
&check_dbm_pw,
|
|
Packit |
90a5c9 |
&get_dbm_realm_hash,
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void opt_retr(void)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authn_cache_store = APR_RETRIEVE_OPTIONAL_FN(ap_authn_cache_store);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
static void register_hooks(apr_pool_t *p)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_register_auth_provider(p, AUTHN_PROVIDER_GROUP, "dbm",
|
|
Packit |
90a5c9 |
AUTHN_PROVIDER_VERSION,
|
|
Packit |
90a5c9 |
&authn_dbm_provider, AP_AUTH_INTERNAL_PER_CONF);
|
|
Packit |
90a5c9 |
ap_hook_optional_fn_retrieve(opt_retr, NULL, NULL, APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
AP_DECLARE_MODULE(authn_dbm) =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
STANDARD20_MODULE_STUFF,
|
|
Packit |
90a5c9 |
create_authn_dbm_dir_config, /* dir config creater */
|
|
Packit |
90a5c9 |
NULL, /* dir merger --- default is to override */
|
|
Packit |
90a5c9 |
NULL, /* server config */
|
|
Packit |
90a5c9 |
NULL, /* merge server config */
|
|
Packit |
90a5c9 |
authn_dbm_cmds, /* command apr_table_t */
|
|
Packit |
90a5c9 |
register_hooks /* register hooks */
|
|
Packit |
90a5c9 |
};
|