842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
6c47a7d40935c5508868904cba1e87ca783ec386niq * Unless required by applicable law or agreed to in writing, software
6c47a7d40935c5508868904cba1e87ca783ec386niq * distributed under the License is distributed on an "AS IS" BASIS,
6c47a7d40935c5508868904cba1e87ca783ec386niq * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6c47a7d40935c5508868904cba1e87ca783ec386niq * See the License for the specific language governing permissions and
6c47a7d40935c5508868904cba1e87ca783ec386niq * limitations under the License.
6c47a7d40935c5508868904cba1e87ca783ec386niq/* Export a hook for modules that manage clientside sessions
6c47a7d40935c5508868904cba1e87ca783ec386niq * (e.g. mod_auth_cookie)
6c47a7d40935c5508868904cba1e87ca783ec386niq * to deal with those when we successfully login/logout at the server
e2b6e4d548316b37b2763c7ca7c2b54f5e210ac4wrowe * XXX: WHY would this be specific to dbd_authz? Why wouldn't we track
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim * this across all authz user providers in a lower level mod, such as
e2b6e4d548316b37b2763c7ca7c2b54f5e210ac4wroweAPR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(authz_dbd, AUTHZ_DBD, int, client_login,
6c47a7d40935c5508868904cba1e87ca783ec386niqtypedef struct {
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic void (*dbd_prepare)(server_rec*, const char*, const char*) = NULL;
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic void *authz_dbd_cr_cfg(apr_pool_t *pool, char *dummy)
6c47a7d40935c5508868904cba1e87ca783ec386niq authz_dbd_cfg *ret = apr_pcalloc(pool, sizeof(authz_dbd_cfg));
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic void *authz_dbd_merge_cfg(apr_pool_t *pool, void *BASE, void *ADD)
6c47a7d40935c5508868904cba1e87ca783ec386niq authz_dbd_cfg *ret = apr_palloc(pool, sizeof(authz_dbd_cfg));
6c47a7d40935c5508868904cba1e87ca783ec386niq ret->query = (add->query == NULL) ? base->query : add->query;
6c47a7d40935c5508868904cba1e87ca783ec386niq ret->redirect = (add->redirect == -1) ? base->redirect : add->redirect;
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic const char *authz_dbd_prepare(cmd_parms *cmd, void *cfg,
6c47a7d40935c5508868904cba1e87ca783ec386niq const char *query)
7c2df38555e33d4c30de9973722128cc9646a1b3sf const char *err = ap_check_cmd_context(cmd, NOT_IN_HTACCESS);
6c47a7d40935c5508868904cba1e87ca783ec386niq return "You must load mod_dbd to enable AuthzDBD functions";
6c47a7d40935c5508868904cba1e87ca783ec386niq label = apr_psprintf(cmd->pool, "authz_dbd_%d", ++label_num);
6c47a7d40935c5508868904cba1e87ca783ec386niq /* save the label here for our own use */
6c47a7d40935c5508868904cba1e87ca783ec386niq AP_INIT_FLAG("AuthzDBDLoginToReferer", ap_set_flag_slot,
6c47a7d40935c5508868904cba1e87ca783ec386niq (void*)APR_OFFSETOF(authz_dbd_cfg, redirect), ACCESS_CONF,
6c47a7d40935c5508868904cba1e87ca783ec386niq "Whether to redirect to referer on successful login"),
6c47a7d40935c5508868904cba1e87ca783ec386niq "SQL query for DBD Authz or login"),
6c47a7d40935c5508868904cba1e87ca783ec386niq AP_INIT_TAKE1("AuthzDBDRedirectQuery", authz_dbd_prepare,
6c47a7d40935c5508868904cba1e87ca783ec386niq (void*)APR_OFFSETOF(authz_dbd_cfg, redir_query), ACCESS_CONF,
6c47a7d40935c5508868904cba1e87ca783ec386niq "SQL query to get per-user redirect URL after login"),
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic int authz_dbd_login(request_rec *r, authz_dbd_cfg *cfg,
6c47a7d40935c5508868904cba1e87ca783ec386niq const char *action)
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01642)
6c47a7d40935c5508868904cba1e87ca783ec386niq query = apr_hash_get(dbd->prepared, cfg->query, APR_HASH_KEY_STRING);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01643)
6c47a7d40935c5508868904cba1e87ca783ec386niq rv = apr_dbd_pvquery(dbd->driver, r->pool, dbd->handle, &nrows,
6c47a7d40935c5508868904cba1e87ca783ec386niq if (rv == 0) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01644)
6c47a7d40935c5508868904cba1e87ca783ec386niq "authz_dbd: %s of user %s updated %d rows",
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01645)
6c47a7d40935c5508868904cba1e87ca783ec386niq "authz_dbd: query for %s failed; user %s [%s]",
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01646)
6c47a7d40935c5508868904cba1e87ca783ec386niq "authz_dbd: no redirect query!");
6c47a7d40935c5508868904cba1e87ca783ec386niq /* OK, this is non-critical; we can just not-redirect */
14e0c468a75ddee46ced5c55ea2287642d907ca2sf else if ((rv = apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle,
6c47a7d40935c5508868904cba1e87ca783ec386niq for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
6c47a7d40935c5508868904cba1e87ca783ec386niq rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
d56bacbfefa5aa883ce5162a115747372fc38d13chrisd if (rv != 0) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01647)
6c47a7d40935c5508868904cba1e87ca783ec386niq "authz_dbd in get_row; action=%s user=%s [%s]",
d56bacbfefa5aa883ce5162a115747372fc38d13chrisd /* we can't break out here or row won't get cleaned up */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01648)
6c47a7d40935c5508868904cba1e87ca783ec386niq "authz_dbd/redirect for %s of %s [%s]",
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic int authz_dbd_group_query(request_rec *r, authz_dbd_cfg *cfg,
6c47a7d40935c5508868904cba1e87ca783ec386niq /* SELECT group FROM authz WHERE user = %s */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01649)
6c47a7d40935c5508868904cba1e87ca783ec386niq "No query configured for dbd-group!");
6c47a7d40935c5508868904cba1e87ca783ec386niq query = apr_hash_get(dbd->prepared, cfg->query, APR_HASH_KEY_STRING);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01650)
6c47a7d40935c5508868904cba1e87ca783ec386niq "Error retrieving query for dbd-group!");
6c47a7d40935c5508868904cba1e87ca783ec386niq rv = apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle, &res,
6c47a7d40935c5508868904cba1e87ca783ec386niq if (rv == 0) {
6c47a7d40935c5508868904cba1e87ca783ec386niq for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
6c47a7d40935c5508868904cba1e87ca783ec386niq rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
6c47a7d40935c5508868904cba1e87ca783ec386niq if (rv == 0) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01651)
6c47a7d40935c5508868904cba1e87ca783ec386niq "authz_dbd in get_row; group query for user=%s [%s]",
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01652)
6c47a7d40935c5508868904cba1e87ca783ec386niq "authz_dbd, in groups query for %s [%s]",
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholesstatic authz_status dbdgroup_check_authorization(request_rec *r,
97cc46935ec496b83fef9d6feb094d706c895b3bsf const char *require_args,
6c47a7d40935c5508868904cba1e87ca783ec386niq const char *w;
6c47a7d40935c5508868904cba1e87ca783ec386niq const char *t;
6c47a7d40935c5508868904cba1e87ca783ec386niq authz_dbd_cfg *cfg = ap_get_module_config(r->per_dir_config,
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes groups = apr_array_make(r->pool, 4, sizeof(const char*));
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02590)
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin "authz_dbd authorize: require dbd-group: Can't "
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes while (t[0]) {
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes if (!strcmp(w, ((const char**)groups->elts)[i])) {
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholesstatic authz_status dbdlogin_check_authorization(request_rec *r,
97cc46935ec496b83fef9d6feb094d706c895b3bsf const char *require_args,
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes authz_dbd_cfg *cfg = ap_get_module_config(r->per_dir_config,
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes return (authz_dbd_login(r, cfg, "login") == OK ? AUTHZ_GRANTED : AUTHZ_DENIED);
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholesstatic authz_status dbdlogout_check_authorization(request_rec *r,
97cc46935ec496b83fef9d6feb094d706c895b3bsf const char *require_args,
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes authz_dbd_cfg *cfg = ap_get_module_config(r->per_dir_config,
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes return (authz_dbd_login(r, cfg, "logout") == OK ? AUTHZ_GRANTED : AUTHZ_DENIED);
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrinstatic const char *dbd_parse_config(cmd_parms *cmd, const char *require_line,
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin expr = ap_expr_parse_cmd(cmd, require_line, AP_EXPR_FLAG_STRING_RESULT,
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin "Cannot parse expression in require line: ",
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholesstatic const authz_provider authz_dbdgroup_provider =
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholesstatic const authz_provider authz_dbdlogin_provider =
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholesstatic const authz_provider authz_dbdlogout_provider =
2e242dca7111f99d54dd144b7b8418d88d560032chrisd ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbd-group",
2e242dca7111f99d54dd144b7b8418d88d560032chrisd ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbd-login",