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