Apache HTTPD
mod_authn_dbm.c
Go to the documentation of this file.
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17/*
18 * http_auth: authentication
19 *
20 * Rob McCool & Brian Behlendorf.
21 *
22 * Adapted to Apache by rst.
23 *
24 */
25
26#define APR_WANT_STRFUNC
27#include "apr_want.h"
28#include "apr_strings.h"
29#include "apr_dbm.h"
30#include "apr_md5.h" /* for apr_password_validate */
31
32#include "ap_provider.h"
33#include "httpd.h"
34#include "http_config.h"
35#include "http_core.h"
36#include "http_log.h"
37#include "http_protocol.h"
38#include "http_request.h" /* for ap_hook_(check_user_id | auth_checker)*/
39
40#include "mod_auth.h"
41
42#include "apr_version.h"
43#if !APR_VERSION_AT_LEAST(2,0,0)
44#include "apu_version.h"
45#endif
46
48#define AUTHN_CACHE_STORE(r,user,realm,data) \
49 if (authn_cache_store != NULL) \
50 authn_cache_store((r), "dbm", (user), (realm), (data))
51
52typedef struct {
53 const char *pwfile;
54 const char *dbmtype;
56
58{
59 authn_dbm_config_rec *conf = apr_palloc(p, sizeof(*conf));
60
61 conf->pwfile = NULL;
62 conf->dbmtype = "default";
63
64 return conf;
65}
66
68{
69 AP_INIT_TAKE1("AuthDBMUserFile", ap_set_file_slot,
70 (void *)APR_OFFSETOF(authn_dbm_config_rec, pwfile),
71 OR_AUTHCFG, "dbm database file containing user IDs and passwords"),
72 AP_INIT_TAKE1("AuthDBMType", ap_set_string_slot,
73 (void *)APR_OFFSETOF(authn_dbm_config_rec, dbmtype),
74 OR_AUTHCFG, "what type of DBM file the user file is"),
75 {NULL}
76};
77
78module AP_MODULE_DECLARE_DATA authn_dbm_module;
79
80static apr_status_t fetch_dbm_value(request_rec *r, const char *dbmtype,
81 const char *dbmfile,
82 const char *user, char **value)
83{
84#if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 7)
86 const apu_err_t *err;
87#endif
88 apr_dbm_t *f;
90 apr_status_t rv;
91
92#if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 7)
93 rv = apr_dbm_get_driver(&driver, dbmtype, &err, r->pool);
94
95 if (rv != APR_SUCCESS) {
97 "could not load '%s' dbm library: %s",
98 err->reason, err->msg);
99 return rv;
100 }
101
102 rv = apr_dbm_open2(&f, driver, dbmfile, APR_DBM_READONLY,
104#else
105 rv = apr_dbm_open_ex(&f, dbmtype, dbmfile, APR_DBM_READONLY,
107#endif
108
109 if (rv != APR_SUCCESS) {
111 "could not open dbm (type %s) file: %s",
112 dbmtype, dbmfile);
113 return rv;
114 }
115
116 key.dptr = (char*)user;
117#ifndef NETSCAPE_DBM_COMPAT
118 key.dsize = strlen(key.dptr);
119#else
120 key.dsize = strlen(key.dptr) + 1;
121#endif
122
123 *value = NULL;
124
125 if (apr_dbm_fetch(f, key, &val) == APR_SUCCESS && val.dptr) {
126 *value = apr_pstrmemdup(r->pool, val.dptr, val.dsize);
127 }
128
130
131 /* NOT FOUND is not an error case; this is indicated by a NULL result.
132 * Treat all NULL lookup/error results as success for the simple case
133 * of auth credential lookup, these are DECLINED in both cases.
134 */
135 return APR_SUCCESS;
136}
137
138static authn_status check_dbm_pw(request_rec *r, const char *user,
139 const char *password)
140{
142 &authn_dbm_module);
143 apr_status_t rv;
144 char *dbm_password;
145 char *colon_pw;
146
147 rv = fetch_dbm_value(r, conf->dbmtype, conf->pwfile, user, &dbm_password);
148
149 if (rv != APR_SUCCESS) {
150 return AUTH_GENERAL_ERROR;
151 }
152
153 if (!dbm_password) {
154 return AUTH_USER_NOT_FOUND;
155 }
156
158 if (colon_pw) {
159 *colon_pw = '\0';
160 }
162
164
165 if (rv != APR_SUCCESS) {
166 return AUTH_DENIED;
167 }
168
169 return AUTH_GRANTED;
170}
171
173 const char *realm, char **rethash)
174{
176 &authn_dbm_module);
177 apr_status_t rv;
178 char *dbm_hash;
179 char *colon_hash;
180
181 rv = fetch_dbm_value(r, conf->dbmtype, conf->pwfile,
182 apr_pstrcat(r->pool, user, ":", realm, NULL),
183 &dbm_hash);
184
185 if (rv != APR_SUCCESS) {
186 return AUTH_GENERAL_ERROR;
187 }
188
189 if (!dbm_hash) {
190 return AUTH_USER_NOT_FOUND;
191 }
192
194 if (colon_hash) {
195 *colon_hash = '\0';
196 }
197
198 *rethash = dbm_hash;
199 AUTHN_CACHE_STORE(r, user, realm, dbm_hash);
200
201 return AUTH_USER_FOUND;
202}
203
209
221
223{
225 create_authn_dbm_dir_config, /* dir config creater */
226 NULL, /* dir merger --- default is to override */
227 NULL, /* server config */
228 NULL, /* merge server config */
229 authn_dbm_cmds, /* command apr_table_t */
230 register_hooks /* register hooks */
231};
Apache Provider API.
APR-UTIL DBM library.
APR MD5 Routines.
apr_size_t const unsigned char unsigned int unsigned int d
Definition apr_siphash.h:72
APR Strings library.
APR Versioning Interface.
APR Standard Headers Support.
APR-util Versioning Interface.
#define AP_INIT_TAKE1(directive, func, mconfig, where, help)
#define ap_get_module_config(v, m)
#define AP_DECLARE_MODULE(foo)
const char * ap_set_string_slot(cmd_parms *cmd, void *struct_ptr, const char *arg)
Definition config.c:1469
request_rec * r
void ap_hook_optional_fn_retrieve(ap_HOOK_optional_fn_retrieve_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition config.c:195
const char * ap_set_file_slot(cmd_parms *cmd, void *struct_ptr, const char *arg)
Definition config.c:1535
#define APLOGNO(n)
Definition http_log.h:117
#define ap_log_rerror
Definition http_log.h:454
#define APLOG_ERR
Definition http_log.h:67
#define APLOG_MARK
Definition http_log.h:283
apr_status_t ap_register_auth_provider(apr_pool_t *pool, const char *provider_group, const char *provider_name, const char *provider_version, const void *provider, int type)
Definition request.c:2179
#define AP_AUTH_INTERNAL_PER_CONF
apr_file_t * f
const char const apr_dbd_driver_t ** driver
Definition apr_dbd.h:106
#define APR_DBM_READONLY
Definition apr_dbm.h:56
#define APR_HOOK_MIDDLE
Definition apr_hooks.h:303
#define APR_RETRIEVE_OPTIONAL_FN(name)
#define APR_OPTIONAL_FN_TYPE(name)
#define OR_AUTHCFG
#define STANDARD20_MODULE_STUFF
#define ap_strchr(s, c)
Definition httpd.h:2351
apr_size_t size
apr_uint32_t val
Definition apr_atomic.h:66
const char * value
Definition apr_env.h:51
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
const char * key
#define APR_OS_DEFAULT
const char const char * password
apr_int32_t apr_int32_t apr_int32_t err
Apache Configuration.
CORE HTTP Daemon.
Apache Logging library.
HTTP protocol handling.
Apache Request library.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
Authentication and Authorization Extension for Apache.
#define AUTHN_PROVIDER_VERSION
Definition mod_auth.h:41
#define AUTHN_PROVIDER_GROUP
Definition mod_auth.h:39
authn_status
Definition mod_auth.h:64
@ AUTH_GRANTED
Definition mod_auth.h:66
@ AUTH_DENIED
Definition mod_auth.h:65
@ AUTH_USER_FOUND
Definition mod_auth.h:67
@ AUTH_GENERAL_ERROR
Definition mod_auth.h:69
@ AUTH_USER_NOT_FOUND
Definition mod_auth.h:68
static const authn_provider authn_dbm_provider
static const command_rec authn_dbm_cmds[]
static void opt_retr(void)
static void register_hooks(apr_pool_t *p)
static void * create_authn_dbm_dir_config(apr_pool_t *p, char *d)
#define AUTHN_CACHE_STORE(r, user, realm, data)
static apr_OFN_ap_authn_cache_store_t * authn_cache_store
static authn_status get_dbm_realm_hash(request_rec *r, const char *user, const char *realm, char **rethash)
static apr_status_t fetch_dbm_value(request_rec *r, const char *dbmtype, const char *dbmfile, const char *user, char **value)
static authn_status check_dbm_pw(request_rec *r, const char *user, const char *password)
static void ap_authn_cache_store(request_rec *r, const char *module, const char *user, const char *realm, const char *data)
return NULL
Definition mod_so.c:359
A structure that represents the current request.
Definition httpd.h:845
apr_pool_t * pool
Definition httpd.h:847
struct ap_conf_vector_t * per_dir_config
Definition httpd.h:1047