]> granicus.if.org Git - apache/blob - modules/aaa/mod_authz_dbd.c
Added many log numbers to log statements that
[apache] / modules / aaa / mod_authz_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 "httpd.h"
18 #include "http_log.h"
19 #include "http_config.h"
20 #include "ap_provider.h"
21 #include "http_request.h"
22 #include "http_protocol.h"
23 #include "http_core.h"
24 #include "apr_dbd.h"
25 #include "mod_dbd.h"
26 #include "apr_strings.h"
27 #include "mod_authz_dbd.h"
28
29 #include "mod_auth.h"
30
31
32 module AP_MODULE_DECLARE_DATA authz_dbd_module;
33
34 /* Export a hook for modules that manage clientside sessions
35  * (e.g. mod_auth_cookie)
36  * to deal with those when we successfully login/logout at the server
37  *
38  * XXX: WHY would this be specific to dbd_authz?  Why wouldn't we track
39  * this across all authz user providers in a lower level mod, such as
40  * mod_auth_basic/digest?
41  */
42 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(authz_dbd, AUTHZ_DBD, int, client_login,
43                             (request_rec *r, int code, const char *action),
44                             (r, code, action), OK, DECLINED)
45
46
47 typedef struct {
48     const char *query;
49     const char *redir_query;
50     int redirect;
51 } authz_dbd_cfg ;
52
53 static ap_dbd_t *(*dbd_handle)(request_rec*) = NULL;
54 static void (*dbd_prepare)(server_rec*, const char*, const char*) = NULL;
55
56 static const char *const noerror = "???";
57
58 static void *authz_dbd_cr_cfg(apr_pool_t *pool, char *dummy)
59 {
60     authz_dbd_cfg *ret = apr_pcalloc(pool, sizeof(authz_dbd_cfg));
61     ret->redirect = -1;
62     return ret;
63 }
64 static void *authz_dbd_merge_cfg(apr_pool_t *pool, void *BASE, void *ADD)
65 {
66     authz_dbd_cfg *base = BASE;
67     authz_dbd_cfg *add = ADD;
68     authz_dbd_cfg *ret = apr_palloc(pool, sizeof(authz_dbd_cfg));
69
70     ret->query = (add->query == NULL) ? base->query : add->query;
71     ret->redir_query = (add->redir_query == NULL)
72                             ? base->redir_query : add->redir_query;
73     ret->redirect = (add->redirect == -1) ? base->redirect : add->redirect;
74     return ret;
75 }
76 static const char *authz_dbd_prepare(cmd_parms *cmd, void *cfg,
77                                      const char *query)
78 {
79     static unsigned int label_num = 0;
80     char *label;
81     const char *err = ap_check_cmd_context(cmd, NOT_IN_HTACCESS);
82     if (err)
83         return err;
84
85     if (dbd_prepare == NULL) {
86         dbd_prepare = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_prepare);
87         if (dbd_prepare == NULL) {
88             return "You must load mod_dbd to enable AuthzDBD functions";
89         }
90         dbd_handle = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_acquire);
91     }
92     label = apr_psprintf(cmd->pool, "authz_dbd_%d", ++label_num);
93
94     dbd_prepare(cmd->server, query, label);
95
96     /* save the label here for our own use */
97     return ap_set_string_slot(cmd, cfg, label);
98 }
99 static const command_rec authz_dbd_cmds[] = {
100     AP_INIT_FLAG("AuthzDBDLoginToReferer", ap_set_flag_slot,
101                  (void*)APR_OFFSETOF(authz_dbd_cfg, redirect), ACCESS_CONF,
102                  "Whether to redirect to referer on successful login"),
103     AP_INIT_TAKE1("AuthzDBDQuery", authz_dbd_prepare,
104                   (void*)APR_OFFSETOF(authz_dbd_cfg, query), ACCESS_CONF,
105                   "SQL query for DBD Authz or login"),
106     AP_INIT_TAKE1("AuthzDBDRedirectQuery", authz_dbd_prepare,
107                   (void*)APR_OFFSETOF(authz_dbd_cfg, redir_query), ACCESS_CONF,
108                   "SQL query to get per-user redirect URL after login"),
109     {NULL}
110 };
111
112 static int authz_dbd_login(request_rec *r, authz_dbd_cfg *cfg,
113                            const char *action)
114 {
115     int rv;
116     const char *newuri = NULL;
117     int nrows;
118     const char *message;
119     ap_dbd_t *dbd = dbd_handle(r);
120     apr_dbd_prepared_t *query;
121     apr_dbd_results_t *res = NULL;
122     apr_dbd_row_t *row = NULL;
123
124     if (cfg->query == NULL) {
125         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01642)
126                       "No query configured for %s!", action);
127         return HTTP_INTERNAL_SERVER_ERROR;
128     }
129     if (dbd == NULL) {
130         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02902)
131                       "No db handle available for %s! "
132                       "Check your database access",
133                       action);
134         return HTTP_INTERNAL_SERVER_ERROR;
135     }
136     query = apr_hash_get(dbd->prepared, cfg->query, APR_HASH_KEY_STRING);
137     if (query == NULL) {
138         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01643)
139                       "Error retrieving Query for %s!", action);
140         return HTTP_INTERNAL_SERVER_ERROR;
141     }
142
143     rv = apr_dbd_pvquery(dbd->driver, r->pool, dbd->handle, &nrows,
144                          query, r->user, NULL);
145     if (rv == 0) {
146         if (nrows != 1) {
147             ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01644)
148                           "authz_dbd: %s of user %s updated %d rows",
149                           action, r->user, nrows);
150         }
151     }
152     else {
153         message = apr_dbd_error(dbd->driver, dbd->handle, rv);
154         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01645)
155                       "authz_dbd: query for %s failed; user %s [%s]",
156                       action, r->user, message?message:noerror);
157         return HTTP_INTERNAL_SERVER_ERROR;
158     }
159
160     if (cfg->redirect == 1) {
161         newuri = apr_table_get(r->headers_in, "Referer");
162     }
163
164     if (!newuri && cfg->redir_query) {
165         query = apr_hash_get(dbd->prepared, cfg->redir_query,
166                              APR_HASH_KEY_STRING);
167         if (query == NULL) {
168             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01646)
169                           "authz_dbd: no redirect query!");
170             /* OK, this is non-critical; we can just not-redirect */
171         }
172         else if ((rv = apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle,
173                                         &res, query, 0, r->user, NULL)) == 0) {
174             for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
175                  rv != -1;
176                  rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
177                 if (rv != 0) {
178                     message = apr_dbd_error(dbd->driver, dbd->handle, rv);
179                     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01647)
180                           "authz_dbd in get_row; action=%s user=%s [%s]",
181                           action, r->user, message?message:noerror);
182                 }
183                 else if (newuri == NULL) {
184                     newuri =
185                         apr_pstrdup(r->pool,
186                                     apr_dbd_get_entry(dbd->driver, row, 0));
187                 }
188                 /* we can't break out here or row won't get cleaned up */
189             }
190         }
191         else {
192             message = apr_dbd_error(dbd->driver, dbd->handle, rv);
193             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01648)
194                           "authz_dbd/redirect for %s of %s [%s]",
195                           action, r->user, message?message:noerror);
196         }
197     }
198     if (newuri != NULL) {
199         r->status = HTTP_MOVED_TEMPORARILY;
200         apr_table_set(r->err_headers_out, "Location", newuri);
201     }
202     authz_dbd_run_client_login(r, OK, action);
203     return OK;
204 }
205
206 static int authz_dbd_group_query(request_rec *r, authz_dbd_cfg *cfg,
207                                  apr_array_header_t *groups)
208 {
209     /* SELECT group FROM authz WHERE user = %s */
210     int rv;
211     const char *message;
212     ap_dbd_t *dbd = dbd_handle(r);
213     apr_dbd_prepared_t *query;
214     apr_dbd_results_t *res = NULL;
215     apr_dbd_row_t *row = NULL;
216
217     if (cfg->query == NULL) {
218         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01649)
219                       "No query configured for dbd-group!");
220         return HTTP_INTERNAL_SERVER_ERROR;
221     }
222     if (dbd == NULL) {
223         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02903)
224                       "No db handle available for dbd-query! "
225                       "Check your database access");
226         return HTTP_INTERNAL_SERVER_ERROR;
227     }
228     query = apr_hash_get(dbd->prepared, cfg->query, APR_HASH_KEY_STRING);
229     if (query == NULL) {
230         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01650)
231                       "Error retrieving query for dbd-group!");
232         return HTTP_INTERNAL_SERVER_ERROR;
233     }
234     rv = apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle, &res,
235                           query, 0, r->user, NULL);
236     if (rv == 0) {
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                 APR_ARRAY_PUSH(groups, const char *) =
242                     apr_pstrdup(r->pool,
243                                 apr_dbd_get_entry(dbd->driver, row, 0));
244             }
245             else {
246                 message = apr_dbd_error(dbd->driver, dbd->handle, rv);
247                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01651)
248                         "authz_dbd in get_row; group query for user=%s [%s]",
249                         r->user, message?message:noerror);
250                 return HTTP_INTERNAL_SERVER_ERROR;
251             }
252         }
253     }
254     else {
255         message = apr_dbd_error(dbd->driver, dbd->handle, rv);
256         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01652)
257                       "authz_dbd, in groups query for %s [%s]",
258                       r->user, message?message:noerror);
259         return HTTP_INTERNAL_SERVER_ERROR;
260     }
261     return OK;
262 }
263
264 static authz_status dbdgroup_check_authorization(request_rec *r,
265                                                  const char *require_args,
266                                                  const void *parsed_require_args)
267 {
268     int i, rv;
269     const char *w;
270     apr_array_header_t *groups = NULL;
271
272     const char *err = NULL;
273     const ap_expr_info_t *expr = parsed_require_args;
274     const char *require;
275
276     const char *t;
277     authz_dbd_cfg *cfg = ap_get_module_config(r->per_dir_config,
278                                               &authz_dbd_module);
279
280     if (!r->user) {
281         return AUTHZ_DENIED_NO_USER;
282     }
283
284     if (groups == NULL) {
285         groups = apr_array_make(r->pool, 4, sizeof(const char*));
286         rv = authz_dbd_group_query(r, cfg, groups);
287         if (rv != OK) {
288             return AUTHZ_GENERAL_ERROR;
289         }
290     }
291
292     require = ap_expr_str_exec(r, expr, &err);
293     if (err) {
294         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02590)
295                       "authz_dbd authorize: require dbd-group: Can't "
296                       "evaluate require expression: %s", err);
297         return AUTHZ_DENIED;
298     }
299
300     t = require;
301     while (t[0]) {
302         w = ap_getword_white(r->pool, &t);
303         for (i=0; i < groups->nelts; ++i) {
304             if (!strcmp(w, ((const char**)groups->elts)[i])) {
305                 return AUTHZ_GRANTED;
306             }
307         }
308     }
309
310     return AUTHZ_DENIED;
311 }
312
313 static authz_status dbdlogin_check_authorization(request_rec *r,
314                                                  const char *require_args,
315                                                  const void *parsed_require_args)
316 {
317     authz_dbd_cfg *cfg = ap_get_module_config(r->per_dir_config,
318                                               &authz_dbd_module);
319
320     if (!r->user) {
321         return AUTHZ_DENIED_NO_USER;
322     }
323
324     return (authz_dbd_login(r, cfg, "login") == OK ? AUTHZ_GRANTED : AUTHZ_DENIED);
325 }
326
327 static authz_status dbdlogout_check_authorization(request_rec *r,
328                                                   const char *require_args,
329                                                   const void *parsed_require_args)
330 {
331     authz_dbd_cfg *cfg = ap_get_module_config(r->per_dir_config,
332                                               &authz_dbd_module);
333
334     if (!r->user) {
335         return AUTHZ_DENIED_NO_USER;
336     }
337
338     return (authz_dbd_login(r, cfg, "logout") == OK ? AUTHZ_GRANTED : AUTHZ_DENIED);
339 }
340
341 static const char *dbd_parse_config(cmd_parms *cmd, const char *require_line,
342                                      const void **parsed_require_line)
343 {
344     const char *expr_err = NULL;
345     ap_expr_info_t *expr;
346
347     expr = ap_expr_parse_cmd(cmd, require_line, AP_EXPR_FLAG_STRING_RESULT,
348             &expr_err, NULL);
349
350     if (expr_err)
351         return apr_pstrcat(cmd->temp_pool,
352                            "Cannot parse expression in require line: ",
353                            expr_err, NULL);
354
355     *parsed_require_line = expr;
356
357     return NULL;
358 }
359
360 static const authz_provider authz_dbdgroup_provider =
361 {
362     &dbdgroup_check_authorization,
363     &dbd_parse_config,
364 };
365
366 static const authz_provider authz_dbdlogin_provider =
367 {
368     &dbdlogin_check_authorization,
369     NULL,
370 };
371
372
373 static const authz_provider authz_dbdlogout_provider =
374 {
375     &dbdlogout_check_authorization,
376     NULL,
377 };
378
379 static void authz_dbd_hooks(apr_pool_t *p)
380 {
381     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbd-group",
382                               AUTHZ_PROVIDER_VERSION,
383                               &authz_dbdgroup_provider,
384                               AP_AUTH_INTERNAL_PER_CONF);
385     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbd-login",
386                               AUTHZ_PROVIDER_VERSION,
387                               &authz_dbdlogin_provider,
388                               AP_AUTH_INTERNAL_PER_CONF);
389     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbd-logout",
390                               AUTHZ_PROVIDER_VERSION,
391                               &authz_dbdlogout_provider,
392                               AP_AUTH_INTERNAL_PER_CONF);
393 }
394
395 AP_DECLARE_MODULE(authz_dbd) =
396 {
397     STANDARD20_MODULE_STUFF,
398     authz_dbd_cr_cfg,
399     authz_dbd_merge_cfg,
400     NULL,
401     NULL,
402     authz_dbd_cmds,
403     authz_dbd_hooks
404 };
405