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;
45 static void *authn_dbd_cr_conf(apr_pool_t *pool, char *dummy)
47 authn_dbd_conf *ret = apr_pcalloc(pool, sizeof(authn_dbd_conf));
50 static void *authn_dbd_merge_conf(apr_pool_t *pool, void *BASE, void *ADD)
52 authn_dbd_conf *add = ADD;
53 authn_dbd_conf *base = BASE;
54 authn_dbd_conf *ret = apr_palloc(pool, sizeof(authn_dbd_conf));
55 ret->user = (add->user == NULL) ? base->user : add->user;
56 ret->realm = (add->realm == NULL) ? base->realm : add->realm;
59 static const char *authn_dbd_prepare(cmd_parms *cmd, void *cfg, const char *query)
61 static unsigned int label_num = 0;
64 if (authn_dbd_prepare_fn == NULL) {
65 authn_dbd_prepare_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_prepare);
66 if (authn_dbd_prepare_fn == NULL) {
67 return "You must load mod_dbd to enable AuthDBD functions";
69 authn_dbd_acquire_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_acquire);
71 label = apr_psprintf(cmd->pool, "authn_dbd_%d", ++label_num);
73 authn_dbd_prepare_fn(cmd->server, query, label);
75 /* save the label here for our own use */
76 return ap_set_string_slot(cmd, cfg, label);
78 static const command_rec authn_dbd_cmds[] =
80 AP_INIT_TAKE1("AuthDBDUserPWQuery", authn_dbd_prepare,
81 (void *)APR_OFFSETOF(authn_dbd_conf, user), ACCESS_CONF,
82 "Query used to fetch password for user"),
83 AP_INIT_TAKE1("AuthDBDUserRealmQuery", authn_dbd_prepare,
84 (void *)APR_OFFSETOF(authn_dbd_conf, realm), ACCESS_CONF,
85 "Query used to fetch password for user+realm"),
88 static authn_status authn_dbd_password(request_rec *r, const char *user,
92 const char *dbd_password = NULL;
93 apr_dbd_prepared_t *statement;
94 apr_dbd_results_t *res = NULL;
95 apr_dbd_row_t *row = NULL;
97 authn_dbd_conf *conf = ap_get_module_config(r->per_dir_config,
99 ap_dbd_t *dbd = authn_dbd_acquire_fn(r);
101 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
102 "Failed to acquire database connection to look up "
104 return AUTH_GENERAL_ERROR;
107 if (conf->user == NULL) {
108 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
109 "No AuthDBDUserPWQuery has been specified");
110 return AUTH_GENERAL_ERROR;
113 statement = apr_hash_get(dbd->prepared, conf->user, APR_HASH_KEY_STRING);
114 if (statement == NULL) {
115 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
116 "A prepared statement could not be found for "
117 "AuthDBDUserPWQuery with the key '%s'", conf->user);
118 return AUTH_GENERAL_ERROR;
120 if (apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle, &res, statement,
121 0, user, NULL) != 0) {
122 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
123 "Query execution error looking up '%s' "
124 "in database", user);
125 return AUTH_GENERAL_ERROR;
127 for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
129 rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
131 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
132 "Error retrieving results while looking up '%s' "
133 "in database", user);
134 return AUTH_GENERAL_ERROR;
136 if (dbd_password == NULL) {
137 #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 3)
138 /* add the rest of the columns to the environment */
141 for (name = apr_dbd_get_name(dbd->driver, res, i);
143 name = apr_dbd_get_name(dbd->driver, res, i)) {
145 char *str = apr_pstrcat(r->pool, AUTHN_PREFIX,
148 int j = sizeof(AUTHN_PREFIX)-1; /* string length of "AUTHENTICATE_", excluding the trailing NIL */
150 if (!apr_isalnum(str[j])) {
154 str[j] = apr_toupper(str[j]);
158 apr_table_set(r->subprocess_env, str,
159 apr_dbd_get_entry(dbd->driver, row, i));
163 dbd_password = apr_dbd_get_entry(dbd->driver, row, 0);
165 /* we can't break out here or row won't get cleaned up */
169 return AUTH_USER_NOT_FOUND;
172 rv = apr_password_validate(password, dbd_password);
174 if (rv != APR_SUCCESS) {
180 static authn_status authn_dbd_realm(request_rec *r, const char *user,
181 const char *realm, char **rethash)
184 const char *dbd_hash = NULL;
185 apr_dbd_prepared_t *statement;
186 apr_dbd_results_t *res = NULL;
187 apr_dbd_row_t *row = NULL;
189 authn_dbd_conf *conf = ap_get_module_config(r->per_dir_config,
191 ap_dbd_t *dbd = authn_dbd_acquire_fn(r);
193 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
194 "Failed to acquire database connection to look up "
195 "user '%s:%s'", user, realm);
196 return AUTH_GENERAL_ERROR;
198 if (conf->realm == NULL) {
199 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
200 "No AuthDBDUserRealmQuery has been specified");
201 return AUTH_GENERAL_ERROR;
203 statement = apr_hash_get(dbd->prepared, conf->realm, APR_HASH_KEY_STRING);
204 if (statement == NULL) {
205 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
206 "A prepared statement could not be found for "
207 "AuthDBDUserRealmQuery with the key '%s'", conf->realm);
208 return AUTH_GENERAL_ERROR;
210 if (apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle, &res, statement,
211 0, user, realm, NULL) != 0) {
212 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
213 "Query execution error looking up '%s:%s' "
214 "in database", user, realm);
215 return AUTH_GENERAL_ERROR;
217 for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
219 rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
221 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
222 "Error retrieving results while looking up '%s:%s' "
223 "in database", user, realm);
224 return AUTH_GENERAL_ERROR;
226 if (dbd_hash == NULL) {
227 #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 3)
228 /* add the rest of the columns to the environment */
231 for (name = apr_dbd_get_name(dbd->driver, res, i);
233 name = apr_dbd_get_name(dbd->driver, res, i)) {
235 char *str = apr_pstrcat(r->pool, AUTHN_PREFIX,
238 int j = sizeof(AUTHN_PREFIX)-1; /* string length of "AUTHENTICATE_", excluding the trailing NIL */
240 if (!apr_isalnum(str[j])) {
244 str[j] = apr_toupper(str[j]);
248 apr_table_set(r->subprocess_env, str,
249 apr_dbd_get_entry(dbd->driver, row, i));
253 dbd_hash = apr_dbd_get_entry(dbd->driver, row, 0);
255 /* we can't break out here or row won't get cleaned up */
259 return AUTH_USER_NOT_FOUND;
262 *rethash = apr_pstrdup(r->pool, dbd_hash);
263 return AUTH_USER_FOUND;
265 static void authn_dbd_hooks(apr_pool_t *p)
267 static const authn_provider authn_dbd_provider = {
272 ap_register_auth_provider(p, AUTHN_PROVIDER_GROUP, "dbd", "0",
273 &authn_dbd_provider, AP_AUTH_INTERNAL_PER_CONF);
275 module AP_MODULE_DECLARE_DATA authn_dbd_module =
277 STANDARD20_MODULE_STUFF,
279 authn_dbd_merge_conf,