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.
17 #include "ap_provider.h"
19 #include "http_config.h"
21 #include "http_request.h"
25 #include "apr_strings.h"
28 #include "apu_version.h"
30 module AP_MODULE_DECLARE_DATA authn_dbd_module;
41 /* optional function - look it up once in post_config */
42 static ap_dbd_t *(*authn_dbd_acquire_fn)(request_rec*) = NULL;
43 static void (*authn_dbd_prepare_fn)(server_rec*, const char*, const char*) = NULL;
44 static APR_OPTIONAL_FN_TYPE(ap_authn_cache_store) *authn_cache_store = NULL;
45 #define AUTHN_CACHE_STORE(r,user,realm,data) \
46 if (authn_cache_store != NULL) \
47 authn_cache_store((r), "dbd", (user), (realm), (data))
49 static void *authn_dbd_cr_conf(apr_pool_t *pool, char *dummy)
51 authn_dbd_conf *ret = apr_pcalloc(pool, sizeof(authn_dbd_conf));
54 static void *authn_dbd_merge_conf(apr_pool_t *pool, void *BASE, void *ADD)
56 authn_dbd_conf *add = ADD;
57 authn_dbd_conf *base = BASE;
58 authn_dbd_conf *ret = apr_palloc(pool, sizeof(authn_dbd_conf));
59 ret->user = (add->user == NULL) ? base->user : add->user;
60 ret->realm = (add->realm == NULL) ? base->realm : add->realm;
63 static const char *authn_dbd_prepare(cmd_parms *cmd, void *cfg, const char *query)
65 static unsigned int label_num = 0;
67 const char *err = ap_check_cmd_context(cmd, NOT_IN_HTACCESS);
71 if (authn_dbd_prepare_fn == NULL) {
72 authn_dbd_prepare_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_prepare);
73 if (authn_dbd_prepare_fn == NULL) {
74 return "You must load mod_dbd to enable AuthDBD functions";
76 authn_dbd_acquire_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_acquire);
78 label = apr_psprintf(cmd->pool, "authn_dbd_%d", ++label_num);
80 authn_dbd_prepare_fn(cmd->server, query, label);
82 /* save the label here for our own use */
83 return ap_set_string_slot(cmd, cfg, label);
85 static const command_rec authn_dbd_cmds[] =
87 AP_INIT_TAKE1("AuthDBDUserPWQuery", authn_dbd_prepare,
88 (void *)APR_OFFSETOF(authn_dbd_conf, user), ACCESS_CONF,
89 "Query used to fetch password for user"),
90 AP_INIT_TAKE1("AuthDBDUserRealmQuery", authn_dbd_prepare,
91 (void *)APR_OFFSETOF(authn_dbd_conf, realm), ACCESS_CONF,
92 "Query used to fetch password for user+realm"),
95 static authn_status authn_dbd_password(request_rec *r, const char *user,
99 const char *dbd_password = NULL;
100 apr_dbd_prepared_t *statement;
101 apr_dbd_results_t *res = NULL;
102 apr_dbd_row_t *row = NULL;
105 authn_dbd_conf *conf = ap_get_module_config(r->per_dir_config,
107 ap_dbd_t *dbd = authn_dbd_acquire_fn(r);
109 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01653)
110 "Failed to acquire database connection to look up "
112 return AUTH_GENERAL_ERROR;
115 if (conf->user == NULL) {
116 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01654)
117 "No AuthDBDUserPWQuery has been specified");
118 return AUTH_GENERAL_ERROR;
121 statement = apr_hash_get(dbd->prepared, conf->user, APR_HASH_KEY_STRING);
122 if (statement == NULL) {
123 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01655)
124 "A prepared statement could not be found for "
125 "AuthDBDUserPWQuery with the key '%s'", conf->user);
126 return AUTH_GENERAL_ERROR;
128 if ((ret = apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle, &res,
129 statement, 0, user, NULL) != 0)) {
130 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01656)
131 "Query execution error looking up '%s' "
133 user, apr_dbd_error(dbd->driver, dbd->handle, ret));
134 return AUTH_GENERAL_ERROR;
136 for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
138 rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
140 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01657)
141 "Error retrieving results while looking up '%s' "
142 "in database", user);
143 return AUTH_GENERAL_ERROR;
145 if (dbd_password == NULL) {
146 #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 3)
147 /* add the rest of the columns to the environment */
150 for (name = apr_dbd_get_name(dbd->driver, res, i);
152 name = apr_dbd_get_name(dbd->driver, res, i)) {
154 char *str = apr_pstrcat(r->pool, AUTHN_PREFIX,
157 int j = sizeof(AUTHN_PREFIX)-1; /* string length of "AUTHENTICATE_", excluding the trailing NIL */
159 if (!apr_isalnum(str[j])) {
163 str[j] = apr_toupper(str[j]);
167 apr_table_set(r->subprocess_env, str,
168 apr_dbd_get_entry(dbd->driver, row, i));
172 dbd_password = apr_dbd_get_entry(dbd->driver, row, 0);
174 /* we can't break out here or row won't get cleaned up */
178 return AUTH_USER_NOT_FOUND;
180 AUTHN_CACHE_STORE(r, user, NULL, dbd_password);
182 rv = ap_password_validate(r, user, password, dbd_password);
184 if (rv != APR_SUCCESS) {
190 static authn_status authn_dbd_realm(request_rec *r, const char *user,
191 const char *realm, char **rethash)
194 const char *dbd_hash = NULL;
195 apr_dbd_prepared_t *statement;
196 apr_dbd_results_t *res = NULL;
197 apr_dbd_row_t *row = NULL;
200 authn_dbd_conf *conf = ap_get_module_config(r->per_dir_config,
202 ap_dbd_t *dbd = authn_dbd_acquire_fn(r);
204 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01658)
205 "Failed to acquire database connection to look up "
206 "user '%s:%s'", user, realm);
207 return AUTH_GENERAL_ERROR;
209 if (conf->realm == NULL) {
210 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01659)
211 "No AuthDBDUserRealmQuery has been specified");
212 return AUTH_GENERAL_ERROR;
214 statement = apr_hash_get(dbd->prepared, conf->realm, APR_HASH_KEY_STRING);
215 if (statement == NULL) {
216 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01660)
217 "A prepared statement could not be found for "
218 "AuthDBDUserRealmQuery with the key '%s'", conf->realm);
219 return AUTH_GENERAL_ERROR;
221 if ((ret = apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle, &res,
222 statement, 0, user, realm, NULL) != 0)) {
223 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01661)
224 "Query execution error looking up '%s:%s' "
227 apr_dbd_error(dbd->driver, dbd->handle, ret));
228 return AUTH_GENERAL_ERROR;
230 for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
232 rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
234 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01662)
235 "Error retrieving results while looking up '%s:%s' "
236 "in database", user, realm);
237 return AUTH_GENERAL_ERROR;
239 if (dbd_hash == NULL) {
240 #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 3)
241 /* add the rest of the columns to the environment */
244 for (name = apr_dbd_get_name(dbd->driver, res, i);
246 name = apr_dbd_get_name(dbd->driver, res, i)) {
248 char *str = apr_pstrcat(r->pool, AUTHN_PREFIX,
251 int j = sizeof(AUTHN_PREFIX)-1; /* string length of "AUTHENTICATE_", excluding the trailing NIL */
253 if (!apr_isalnum(str[j])) {
257 str[j] = apr_toupper(str[j]);
261 apr_table_set(r->subprocess_env, str,
262 apr_dbd_get_entry(dbd->driver, row, i));
266 dbd_hash = apr_dbd_get_entry(dbd->driver, row, 0);
268 /* we can't break out here or row won't get cleaned up */
272 return AUTH_USER_NOT_FOUND;
274 AUTHN_CACHE_STORE(r, user, realm, dbd_hash);
275 *rethash = apr_pstrdup(r->pool, dbd_hash);
276 return AUTH_USER_FOUND;
278 static void opt_retr(void)
280 authn_cache_store = APR_RETRIEVE_OPTIONAL_FN(ap_authn_cache_store);
282 static void authn_dbd_hooks(apr_pool_t *p)
284 static const authn_provider authn_dbd_provider = {
289 ap_register_auth_provider(p, AUTHN_PROVIDER_GROUP, "dbd",
290 AUTHN_PROVIDER_VERSION,
291 &authn_dbd_provider, AP_AUTH_INTERNAL_PER_CONF);
292 ap_hook_optional_fn_retrieve(opt_retr, NULL, NULL, APR_HOOK_MIDDLE);
294 AP_DECLARE_MODULE(authn_dbd) =
296 STANDARD20_MODULE_STUFF,
298 authn_dbd_merge_conf,