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.
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"
26 #include "apr_strings.h"
27 #include "mod_authz_dbd.h"
32 module AP_MODULE_DECLARE_DATA authz_dbd_module;
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
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?
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)
49 const char *redir_query;
53 static ap_dbd_t *(*dbd_handle)(request_rec*) = NULL;
54 static void (*dbd_prepare)(server_rec*, const char*, const char*) = NULL;
56 static const char *const noerror = "???";
58 static void *authz_dbd_cr_cfg(apr_pool_t *pool, char *dummy)
60 authz_dbd_cfg *ret = apr_pcalloc(pool, sizeof(authz_dbd_cfg));
64 static void *authz_dbd_merge_cfg(apr_pool_t *pool, void *BASE, void *ADD)
66 authz_dbd_cfg *base = BASE;
67 authz_dbd_cfg *add = ADD;
68 authz_dbd_cfg *ret = apr_palloc(pool, sizeof(authz_dbd_cfg));
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;
76 static const char *authz_dbd_prepare(cmd_parms *cmd, void *cfg,
79 static unsigned int label_num = 0;
81 const char *err = ap_check_cmd_context(cmd, NOT_IN_HTACCESS);
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";
90 dbd_handle = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_acquire);
92 label = apr_psprintf(cmd->pool, "authz_dbd_%d", ++label_num);
94 dbd_prepare(cmd->server, query, label);
96 /* save the label here for our own use */
97 return ap_set_string_slot(cmd, cfg, label);
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"),
112 static int authz_dbd_login(request_rec *r, authz_dbd_cfg *cfg,
116 const char *newuri = NULL;
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;
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;
129 query = apr_hash_get(dbd->prepared, cfg->query, APR_HASH_KEY_STRING);
131 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01643)
132 "Error retrieving Query for %s!", action);
133 return HTTP_INTERNAL_SERVER_ERROR;
136 rv = apr_dbd_pvquery(dbd->driver, r->pool, dbd->handle, &nrows,
137 query, r->user, NULL);
140 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01644)
141 "authz_dbd: %s of user %s updated %d rows",
142 action, r->user, nrows);
146 message = apr_dbd_error(dbd->driver, dbd->handle, rv);
147 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01645)
148 "authz_dbd: query for %s failed; user %s [%s]",
149 action, r->user, message?message:noerror);
150 return HTTP_INTERNAL_SERVER_ERROR;
153 if (cfg->redirect == 1) {
154 newuri = apr_table_get(r->headers_in, "Referer");
157 if (!newuri && cfg->redir_query) {
158 query = apr_hash_get(dbd->prepared, cfg->redir_query,
159 APR_HASH_KEY_STRING);
161 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01646)
162 "authz_dbd: no redirect query!");
163 /* OK, this is non-critical; we can just not-redirect */
165 else if ((rv = apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle,
166 &res, query, 0, r->user, NULL)) == 0) {
167 for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
169 rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
171 message = apr_dbd_error(dbd->driver, dbd->handle, rv);
172 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01647)
173 "authz_dbd in get_row; action=%s user=%s [%s]",
174 action, r->user, message?message:noerror);
176 else if (newuri == NULL) {
177 newuri = apr_dbd_get_entry(dbd->driver, row, 0);
179 /* we can't break out here or row won't get cleaned up */
183 message = apr_dbd_error(dbd->driver, dbd->handle, rv);
184 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01648)
185 "authz_dbd/redirect for %s of %s [%s]",
186 action, r->user, message?message:noerror);
189 if (newuri != NULL) {
190 r->status = HTTP_MOVED_TEMPORARILY;
191 apr_table_set(r->err_headers_out, "Location", newuri);
193 authz_dbd_run_client_login(r, OK, action);
197 static int authz_dbd_group_query(request_rec *r, authz_dbd_cfg *cfg,
198 apr_array_header_t *groups)
200 /* SELECT group FROM authz WHERE user = %s */
203 ap_dbd_t *dbd = dbd_handle(r);
204 apr_dbd_prepared_t *query;
205 apr_dbd_results_t *res = NULL;
206 apr_dbd_row_t *row = NULL;
209 if (cfg->query == NULL) {
210 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01649)
211 "No query configured for dbd-group!");
212 return HTTP_INTERNAL_SERVER_ERROR;
214 query = apr_hash_get(dbd->prepared, cfg->query, APR_HASH_KEY_STRING);
216 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01650)
217 "Error retrieving query for dbd-group!");
218 return HTTP_INTERNAL_SERVER_ERROR;
220 rv = apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle, &res,
221 query, 0, r->user, NULL);
223 for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
225 rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
227 group = apr_array_push(groups);
228 *group = apr_dbd_get_entry(dbd->driver, row, 0);
231 message = apr_dbd_error(dbd->driver, dbd->handle, rv);
232 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01651)
233 "authz_dbd in get_row; group query for user=%s [%s]",
234 r->user, message?message:noerror);
235 return HTTP_INTERNAL_SERVER_ERROR;
240 message = apr_dbd_error(dbd->driver, dbd->handle, rv);
241 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01652)
242 "authz_dbd, in groups query for %s [%s]",
243 r->user, message?message:noerror);
244 return HTTP_INTERNAL_SERVER_ERROR;
249 static authz_status dbdgroup_check_authorization(request_rec *r,
250 const char *require_args,
251 const void *parsed_require_args)
255 apr_array_header_t *groups = NULL;
257 const char *err = NULL;
258 const ap_expr_info_t *expr = parsed_require_args;
262 authz_dbd_cfg *cfg = ap_get_module_config(r->per_dir_config,
266 return AUTHZ_DENIED_NO_USER;
269 if (groups == NULL) {
270 groups = apr_array_make(r->pool, 4, sizeof(const char*));
271 rv = authz_dbd_group_query(r, cfg, groups);
273 return AUTHZ_GENERAL_ERROR;
277 require = ap_expr_str_exec(r, expr, &err);
279 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02590)
280 "authz_dbd authorize: require dbd-group: Can't "
281 "evaluate require expression: %s", err);
287 w = ap_getword_white(r->pool, &t);
288 for (i=0; i < groups->nelts; ++i) {
289 if (!strcmp(w, ((const char**)groups->elts)[i])) {
290 return AUTHZ_GRANTED;
298 static authz_status dbdlogin_check_authorization(request_rec *r,
299 const char *require_args,
300 const void *parsed_require_args)
302 authz_dbd_cfg *cfg = ap_get_module_config(r->per_dir_config,
306 return AUTHZ_DENIED_NO_USER;
309 return (authz_dbd_login(r, cfg, "login") == OK ? AUTHZ_GRANTED : AUTHZ_DENIED);
312 static authz_status dbdlogout_check_authorization(request_rec *r,
313 const char *require_args,
314 const void *parsed_require_args)
316 authz_dbd_cfg *cfg = ap_get_module_config(r->per_dir_config,
320 return AUTHZ_DENIED_NO_USER;
323 return (authz_dbd_login(r, cfg, "logout") == OK ? AUTHZ_GRANTED : AUTHZ_DENIED);
326 static const char *dbd_parse_config(cmd_parms *cmd, const char *require_line,
327 const void **parsed_require_line)
329 const char *expr_err = NULL;
330 ap_expr_info_t *expr;
332 expr = ap_expr_parse_cmd(cmd, require_line, AP_EXPR_FLAG_STRING_RESULT,
336 return apr_pstrcat(cmd->temp_pool,
337 "Cannot parse expression in require line: ",
340 *parsed_require_line = expr;
345 static const authz_provider authz_dbdgroup_provider =
347 &dbdgroup_check_authorization,
351 static const authz_provider authz_dbdlogin_provider =
353 &dbdlogin_check_authorization,
358 static const authz_provider authz_dbdlogout_provider =
360 &dbdlogout_check_authorization,
364 static void authz_dbd_hooks(apr_pool_t *p)
366 ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbd-group",
367 AUTHZ_PROVIDER_VERSION,
368 &authz_dbdgroup_provider,
369 AP_AUTH_INTERNAL_PER_CONF);
370 ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbd-login",
371 AUTHZ_PROVIDER_VERSION,
372 &authz_dbdlogin_provider,
373 AP_AUTH_INTERNAL_PER_CONF);
374 ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbd-logout",
375 AUTHZ_PROVIDER_VERSION,
376 &authz_dbdlogout_provider,
377 AP_AUTH_INTERNAL_PER_CONF);
380 AP_DECLARE_MODULE(authz_dbd) =
382 STANDARD20_MODULE_STUFF,