1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * http_auth: authentication
20 * Rob McCool & Brian Behlendorf.
22 * Adapted to Apache by rst.
26 #define APR_WANT_STRFUNC
28 #include "apr_strings.h"
31 #include "ap_provider.h"
33 #include "http_config.h"
34 #include "http_core.h"
36 #include "http_protocol.h"
37 #include "http_request.h" /* for ap_hook_(check_user_id | auth_checker)*/
41 static APR_OPTIONAL_FN_TYPE(ap_authn_cache_store) *authn_cache_store = NULL;
42 #define AUTHN_CACHE_STORE(r,user,realm,data) \
43 if (authn_cache_store != NULL) \
44 authn_cache_store((r), "dbm", (user), (realm), (data))
49 } authn_dbm_config_rec;
51 static void *create_authn_dbm_dir_config(apr_pool_t *p, char *d)
53 authn_dbm_config_rec *conf = apr_palloc(p, sizeof(*conf));
56 conf->dbmtype = "default";
61 static const command_rec authn_dbm_cmds[] =
63 AP_INIT_TAKE1("AuthDBMUserFile", ap_set_file_slot,
64 (void *)APR_OFFSETOF(authn_dbm_config_rec, pwfile),
65 OR_AUTHCFG, "dbm database file containing user IDs and passwords"),
66 AP_INIT_TAKE1("AuthDBMType", ap_set_string_slot,
67 (void *)APR_OFFSETOF(authn_dbm_config_rec, dbmtype),
68 OR_AUTHCFG, "what type of DBM file the user file is"),
72 module AP_MODULE_DECLARE_DATA authn_dbm_module;
74 static apr_status_t fetch_dbm_value(const char *dbmtype, const char *dbmfile,
75 const char *user, char **value,
82 rv = apr_dbm_open_ex(&f, dbmtype, dbmfile, APR_DBM_READONLY,
83 APR_OS_DEFAULT, pool);
85 if (rv != APR_SUCCESS) {
89 key.dptr = (char*)user;
90 #ifndef NETSCAPE_DBM_COMPAT
91 key.dsize = strlen(key.dptr);
93 key.dsize = strlen(key.dptr) + 1;
98 if (apr_dbm_fetch(f, key, &val) == APR_SUCCESS && val.dptr) {
99 *value = apr_pstrmemdup(pool, val.dptr, val.dsize);
107 static authn_status check_dbm_pw(request_rec *r, const char *user,
108 const char *password)
110 authn_dbm_config_rec *conf = ap_get_module_config(r->per_dir_config,
116 rv = fetch_dbm_value(conf->dbmtype, conf->pwfile, user, &dbm_password,
119 if (rv != APR_SUCCESS) {
120 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01754)
121 "could not open dbm (type %s) auth file: %s",
122 conf->dbmtype, conf->pwfile);
123 return AUTH_GENERAL_ERROR;
127 return AUTH_USER_NOT_FOUND;
130 colon_pw = ap_strchr(dbm_password, ':');
134 AUTHN_CACHE_STORE(r, user, NULL, dbm_password);
136 rv = ap_password_validate(r, user, password, dbm_password);
138 if (rv != APR_SUCCESS) {
145 static authn_status get_dbm_realm_hash(request_rec *r, const char *user,
146 const char *realm, char **rethash)
148 authn_dbm_config_rec *conf = ap_get_module_config(r->per_dir_config,
154 rv = fetch_dbm_value(conf->dbmtype, conf->pwfile,
155 apr_pstrcat(r->pool, user, ":", realm, NULL),
158 if (rv != APR_SUCCESS) {
159 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01755)
160 "Could not open dbm (type %s) hash file: %s",
161 conf->dbmtype, conf->pwfile);
162 return AUTH_GENERAL_ERROR;
166 return AUTH_USER_NOT_FOUND;
169 colon_hash = ap_strchr(dbm_hash, ':');
175 AUTHN_CACHE_STORE(r, user, realm, dbm_hash);
177 return AUTH_USER_FOUND;
180 static const authn_provider authn_dbm_provider =
186 static void opt_retr(void)
188 authn_cache_store = APR_RETRIEVE_OPTIONAL_FN(ap_authn_cache_store);
190 static void register_hooks(apr_pool_t *p)
192 ap_register_auth_provider(p, AUTHN_PROVIDER_GROUP, "dbm",
193 AUTHN_PROVIDER_VERSION,
194 &authn_dbm_provider, AP_AUTH_INTERNAL_PER_CONF);
195 ap_hook_optional_fn_retrieve(opt_retr, NULL, NULL, APR_HOOK_MIDDLE);
198 AP_DECLARE_MODULE(authn_dbm) =
200 STANDARD20_MODULE_STUFF,
201 create_authn_dbm_dir_config, /* dir config creater */
202 NULL, /* dir merger --- default is to override */
203 NULL, /* server config */
204 NULL, /* merge server config */
205 authn_dbm_cmds, /* command apr_table_t */
206 register_hooks /* register hooks */