]> granicus.if.org Git - apache/blob - modules/aaa/mod_authn_dbd.c
Added many log numbers to log statements that
[apache] / modules / aaa / mod_authn_dbd.c
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 #include "ap_provider.h"
18 #include "httpd.h"
19 #include "http_config.h"
20 #include "http_log.h"
21 #include "http_request.h"
22 #include "apr_lib.h"
23 #include "apr_dbd.h"
24 #include "mod_dbd.h"
25 #include "apr_strings.h"
26 #include "mod_auth.h"
27 #include "apr_md5.h"
28 #include "apu_version.h"
29
30 module AP_MODULE_DECLARE_DATA authn_dbd_module;
31
32 typedef struct {
33     const char *user;
34     const char *realm;
35 } authn_dbd_conf;
36
37 typedef struct {
38     const char *label;
39     const char *query;
40 } authn_dbd_rec;
41
42 /* optional function - look it up once in post_config */
43 static ap_dbd_t *(*authn_dbd_acquire_fn)(request_rec*) = NULL;
44 static void (*authn_dbd_prepare_fn)(server_rec*, const char*, const char*) = NULL;
45 static APR_OPTIONAL_FN_TYPE(ap_authn_cache_store) *authn_cache_store = NULL;
46 #define AUTHN_CACHE_STORE(r,user,realm,data) \
47     if (authn_cache_store != NULL) \
48         authn_cache_store((r), "dbd", (user), (realm), (data))
49
50 static void *authn_dbd_cr_conf(apr_pool_t *pool, char *dummy)
51 {
52     authn_dbd_conf *ret = apr_pcalloc(pool, sizeof(authn_dbd_conf));
53     return ret;
54 }
55
56 static void *authn_dbd_merge_conf(apr_pool_t *pool, void *BASE, void *ADD)
57 {
58     authn_dbd_conf *add = ADD;
59     authn_dbd_conf *base = BASE;
60     authn_dbd_conf *ret = apr_palloc(pool, sizeof(authn_dbd_conf));
61     ret->user = (add->user == NULL) ? base->user : add->user;
62     ret->realm = (add->realm == NULL) ? base->realm : add->realm;
63     return ret;
64 }
65
66 static const char *authn_dbd_prepare(cmd_parms *cmd, void *cfg, const char *query)
67 {
68     static unsigned int label_num = 0;
69     char *label;
70     const char *err = ap_check_cmd_context(cmd, NOT_IN_HTACCESS);
71     if (err)
72         return err;
73
74     if (authn_dbd_prepare_fn == NULL) {
75         authn_dbd_prepare_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_prepare);
76         if (authn_dbd_prepare_fn == NULL) {
77             return "You must load mod_dbd to enable AuthDBD functions";
78         }
79         authn_dbd_acquire_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_acquire);
80     }
81     label = apr_psprintf(cmd->pool, "authn_dbd_%d", ++label_num);
82
83     authn_dbd_prepare_fn(cmd->server, query, label);
84
85     /* save the label here for our own use */
86     return ap_set_string_slot(cmd, cfg, label);
87 }
88
89 static const command_rec authn_dbd_cmds[] =
90 {
91     AP_INIT_TAKE1("AuthDBDUserPWQuery", authn_dbd_prepare,
92                   (void *)APR_OFFSETOF(authn_dbd_conf, user), ACCESS_CONF,
93                   "Query used to fetch password for user"),
94     AP_INIT_TAKE1("AuthDBDUserRealmQuery", authn_dbd_prepare,
95                   (void *)APR_OFFSETOF(authn_dbd_conf, realm), ACCESS_CONF,
96                   "Query used to fetch password for user+realm"),
97     {NULL}
98 };
99
100 static authn_status authn_dbd_password(request_rec *r, const char *user,
101                                        const char *password)
102 {
103     apr_status_t rv;
104     const char *dbd_password = NULL;
105     apr_dbd_prepared_t *statement;
106     apr_dbd_results_t *res = NULL;
107     apr_dbd_row_t *row = NULL;
108     int ret;
109
110     authn_dbd_conf *conf = ap_get_module_config(r->per_dir_config,
111                                                 &authn_dbd_module);
112     ap_dbd_t *dbd = authn_dbd_acquire_fn(r);
113     if (dbd == NULL) {
114         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01653)
115                       "Failed to acquire database connection to look up "
116                       "user '%s'", user);
117         return AUTH_GENERAL_ERROR;
118     }
119
120     if (conf->user == NULL) {
121         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01654)
122                       "No AuthDBDUserPWQuery has been specified");
123         return AUTH_GENERAL_ERROR;
124     }
125
126     statement = apr_hash_get(dbd->prepared, conf->user, APR_HASH_KEY_STRING);
127     if (statement == NULL) {
128         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01655)
129                       "A prepared statement could not be found for "
130                       "AuthDBDUserPWQuery with the key '%s'", conf->user);
131         return AUTH_GENERAL_ERROR;
132     }
133     if ((ret = apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle, &res,
134                                 statement, 0, user, NULL)) != 0) {
135         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01656)
136                       "Query execution error looking up '%s' "
137                       "in database [%s]",
138                       user, apr_dbd_error(dbd->driver, dbd->handle, ret));
139         return AUTH_GENERAL_ERROR;
140     }
141     for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
142          rv != -1;
143          rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
144         if (rv != 0) {
145             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01657)
146                           "Error retrieving results while looking up '%s' "
147                           "in database", user);
148             return AUTH_GENERAL_ERROR;
149         }
150         if (dbd_password == NULL) {
151 #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 3)
152             /* add the rest of the columns to the environment */
153             int i = 1;
154             const char *name;
155             for (name = apr_dbd_get_name(dbd->driver, res, i);
156                  name != NULL;
157                  name = apr_dbd_get_name(dbd->driver, res, i)) {
158
159                 char *str = apr_pstrcat(r->pool, AUTHN_PREFIX,
160                                         name,
161                                         NULL);
162                 int j = sizeof(AUTHN_PREFIX)-1; /* string length of "AUTHENTICATE_", excluding the trailing NIL */
163                 while (str[j]) {
164                     if (!apr_isalnum(str[j])) {
165                         str[j] = '_';
166                     }
167                     else {
168                         str[j] = apr_toupper(str[j]);
169                     }
170                     j++;
171                 }
172                 apr_table_set(r->subprocess_env, str,
173                               apr_dbd_get_entry(dbd->driver, row, i));
174                 i++;
175             }
176 #endif
177             dbd_password = apr_pstrdup(r->pool,
178                                        apr_dbd_get_entry(dbd->driver, row, 0));
179         }
180         /* we can't break out here or row won't get cleaned up */
181     }
182
183     if (!dbd_password) {
184         return AUTH_USER_NOT_FOUND;
185     }
186     AUTHN_CACHE_STORE(r, user, NULL, dbd_password);
187
188     rv = ap_password_validate(r, user, password, dbd_password);
189
190     if (rv != APR_SUCCESS) {
191         return AUTH_DENIED;
192     }
193
194     return AUTH_GRANTED;
195 }
196
197 static authn_status authn_dbd_realm(request_rec *r, const char *user,
198                                     const char *realm, char **rethash)
199 {
200     apr_status_t rv;
201     const char *dbd_hash = NULL;
202     apr_dbd_prepared_t *statement;
203     apr_dbd_results_t *res = NULL;
204     apr_dbd_row_t *row = NULL;
205     int ret;
206
207     authn_dbd_conf *conf = ap_get_module_config(r->per_dir_config,
208                                                 &authn_dbd_module);
209     ap_dbd_t *dbd = authn_dbd_acquire_fn(r);
210     if (dbd == NULL) {
211         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01658)
212                       "Failed to acquire database connection to look up "
213                       "user '%s:%s'", user, realm);
214         return AUTH_GENERAL_ERROR;
215     }
216     if (conf->realm == NULL) {
217         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01659)
218                       "No AuthDBDUserRealmQuery has been specified");
219         return AUTH_GENERAL_ERROR;
220     }
221     statement = apr_hash_get(dbd->prepared, conf->realm, APR_HASH_KEY_STRING);
222     if (statement == NULL) {
223         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01660)
224                       "A prepared statement could not be found for "
225                       "AuthDBDUserRealmQuery with the key '%s'", conf->realm);
226         return AUTH_GENERAL_ERROR;
227     }
228     if ((ret = apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle, &res,
229                                 statement, 0, user, realm, NULL)) != 0) {
230         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01661)
231                       "Query execution error looking up '%s:%s' "
232                       "in database [%s]",
233                       user, realm,
234                       apr_dbd_error(dbd->driver, dbd->handle, ret));
235         return AUTH_GENERAL_ERROR;
236     }
237     for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
238          rv != -1;
239          rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
240         if (rv != 0) {
241             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01662)
242                           "Error retrieving results while looking up '%s:%s' "
243                           "in database", user, realm);
244             return AUTH_GENERAL_ERROR;
245         }
246         if (dbd_hash == NULL) {
247 #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 3)
248             /* add the rest of the columns to the environment */
249             int i = 1;
250             const char *name;
251             for (name = apr_dbd_get_name(dbd->driver, res, i);
252                  name != NULL;
253                  name = apr_dbd_get_name(dbd->driver, res, i)) {
254
255                 char *str = apr_pstrcat(r->pool, AUTHN_PREFIX,
256                                         name,
257                                         NULL);
258                 int j = sizeof(AUTHN_PREFIX)-1; /* string length of "AUTHENTICATE_", excluding the trailing NIL */
259                 while (str[j]) {
260                     if (!apr_isalnum(str[j])) {
261                         str[j] = '_';
262                     }
263                     else {
264                         str[j] = apr_toupper(str[j]);
265                     }
266                     j++;
267                 }
268                 apr_table_set(r->subprocess_env, str,
269                               apr_dbd_get_entry(dbd->driver, row, i));
270                 i++;
271             }
272 #endif
273             dbd_hash = apr_pstrdup(r->pool,
274                                    apr_dbd_get_entry(dbd->driver, row, 0));
275         }
276         /* we can't break out here or row won't get cleaned up */
277     }
278
279     if (!dbd_hash) {
280         return AUTH_USER_NOT_FOUND;
281     }
282     AUTHN_CACHE_STORE(r, user, realm, dbd_hash);
283     *rethash = apr_pstrdup(r->pool, dbd_hash);
284     return AUTH_USER_FOUND;
285 }
286
287 static void opt_retr(void)
288 {
289     authn_cache_store = APR_RETRIEVE_OPTIONAL_FN(ap_authn_cache_store);
290 }
291
292 static void authn_dbd_hooks(apr_pool_t *p)
293 {
294     static const authn_provider authn_dbd_provider = {
295         &authn_dbd_password,
296         &authn_dbd_realm
297     };
298
299     ap_register_auth_provider(p, AUTHN_PROVIDER_GROUP, "dbd",
300                               AUTHN_PROVIDER_VERSION,
301                               &authn_dbd_provider, AP_AUTH_INTERNAL_PER_CONF);
302     ap_hook_optional_fn_retrieve(opt_retr, NULL, NULL, APR_HOOK_MIDDLE);
303 }
304
305 AP_DECLARE_MODULE(authn_dbd) =
306 {
307     STANDARD20_MODULE_STUFF,
308     authn_dbd_cr_conf,
309     authn_dbd_merge_conf,
310     NULL,
311     NULL,
312     authn_dbd_cmds,
313     authn_dbd_hooks
314 };