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 *
6c47a7d40935c5508868904cba1e87ca783ec386niq * http://www.apache.org/licenses/LICENSE-2.0
6c47a7d40935c5508868904cba1e87ca783ec386niq *
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 */
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niq#include "httpd.h"
6c47a7d40935c5508868904cba1e87ca783ec386niq#include "http_log.h"
6c47a7d40935c5508868904cba1e87ca783ec386niq#include "http_config.h"
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes#include "ap_provider.h"
6c47a7d40935c5508868904cba1e87ca783ec386niq#include "http_request.h"
6c47a7d40935c5508868904cba1e87ca783ec386niq#include "http_protocol.h"
6c47a7d40935c5508868904cba1e87ca783ec386niq#include "http_core.h"
6c47a7d40935c5508868904cba1e87ca783ec386niq#include "apr_dbd.h"
6c47a7d40935c5508868904cba1e87ca783ec386niq#include "mod_dbd.h"
6c47a7d40935c5508868904cba1e87ca783ec386niq#include "apr_strings.h"
6c47a7d40935c5508868904cba1e87ca783ec386niq#include "mod_authz_dbd.h"
6c47a7d40935c5508868904cba1e87ca783ec386niq
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes#include "mod_auth.h"
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
6c47a7d40935c5508868904cba1e87ca783ec386niqmodule AP_MODULE_DECLARE_DATA authz_dbd_module;
6c47a7d40935c5508868904cba1e87ca783ec386niq
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 *
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
e2b6e4d548316b37b2763c7ca7c2b54f5e210ac4wrowe * mod_auth_basic/digest?
6c47a7d40935c5508868904cba1e87ca783ec386niq */
e2b6e4d548316b37b2763c7ca7c2b54f5e210ac4wroweAPR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(authz_dbd, AUTHZ_DBD, int, client_login,
6c47a7d40935c5508868904cba1e87ca783ec386niq (request_rec *r, int code, const char *action),
6c47a7d40935c5508868904cba1e87ca783ec386niq (r, code, action), OK, DECLINED)
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niqtypedef struct {
6c47a7d40935c5508868904cba1e87ca783ec386niq const char *query;
6c47a7d40935c5508868904cba1e87ca783ec386niq const char *redir_query;
6c47a7d40935c5508868904cba1e87ca783ec386niq int redirect;
6c47a7d40935c5508868904cba1e87ca783ec386niq} authz_dbd_cfg ;
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic ap_dbd_t *(*dbd_handle)(request_rec*) = NULL;
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic void (*dbd_prepare)(server_rec*, const char*, const char*) = NULL;
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic const char *const noerror = "???";
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic void *authz_dbd_cr_cfg(apr_pool_t *pool, char *dummy)
6c47a7d40935c5508868904cba1e87ca783ec386niq{
6c47a7d40935c5508868904cba1e87ca783ec386niq authz_dbd_cfg *ret = apr_pcalloc(pool, sizeof(authz_dbd_cfg));
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes ret->redirect = -1;
6c47a7d40935c5508868904cba1e87ca783ec386niq return ret;
6c47a7d40935c5508868904cba1e87ca783ec386niq}
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic void *authz_dbd_merge_cfg(apr_pool_t *pool, void *BASE, void *ADD)
6c47a7d40935c5508868904cba1e87ca783ec386niq{
6c47a7d40935c5508868904cba1e87ca783ec386niq authz_dbd_cfg *base = BASE;
6c47a7d40935c5508868904cba1e87ca783ec386niq authz_dbd_cfg *add = ADD;
6c47a7d40935c5508868904cba1e87ca783ec386niq authz_dbd_cfg *ret = apr_palloc(pool, sizeof(authz_dbd_cfg));
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niq ret->query = (add->query == NULL) ? base->query : add->query;
6c47a7d40935c5508868904cba1e87ca783ec386niq ret->redir_query = (add->redir_query == NULL)
6c47a7d40935c5508868904cba1e87ca783ec386niq ? base->redir_query : add->redir_query;
6c47a7d40935c5508868904cba1e87ca783ec386niq ret->redirect = (add->redirect == -1) ? base->redirect : add->redirect;
6c47a7d40935c5508868904cba1e87ca783ec386niq return ret;
6c47a7d40935c5508868904cba1e87ca783ec386niq}
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic const char *authz_dbd_prepare(cmd_parms *cmd, void *cfg,
6c47a7d40935c5508868904cba1e87ca783ec386niq const char *query)
6c47a7d40935c5508868904cba1e87ca783ec386niq{
6c47a7d40935c5508868904cba1e87ca783ec386niq static unsigned int label_num = 0;
6c47a7d40935c5508868904cba1e87ca783ec386niq char *label;
7c2df38555e33d4c30de9973722128cc9646a1b3sf const char *err = ap_check_cmd_context(cmd, NOT_IN_HTACCESS);
7c2df38555e33d4c30de9973722128cc9646a1b3sf if (err)
7c2df38555e33d4c30de9973722128cc9646a1b3sf return err;
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niq if (dbd_prepare == NULL) {
6c47a7d40935c5508868904cba1e87ca783ec386niq dbd_prepare = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_prepare);
6c47a7d40935c5508868904cba1e87ca783ec386niq if (dbd_prepare == NULL) {
6c47a7d40935c5508868904cba1e87ca783ec386niq return "You must load mod_dbd to enable AuthzDBD functions";
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq dbd_handle = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_acquire);
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq label = apr_psprintf(cmd->pool, "authz_dbd_%d", ++label_num);
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niq dbd_prepare(cmd->server, query, label);
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niq /* save the label here for our own use */
6c47a7d40935c5508868904cba1e87ca783ec386niq return ap_set_string_slot(cmd, cfg, label);
6c47a7d40935c5508868904cba1e87ca783ec386niq}
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic const command_rec authz_dbd_cmds[] = {
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 AP_INIT_TAKE1("AuthzDBDQuery", authz_dbd_prepare,
6c47a7d40935c5508868904cba1e87ca783ec386niq (void*)APR_OFFSETOF(authz_dbd_cfg, query), ACCESS_CONF,
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"),
6c47a7d40935c5508868904cba1e87ca783ec386niq {NULL}
6c47a7d40935c5508868904cba1e87ca783ec386niq};
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic int authz_dbd_login(request_rec *r, authz_dbd_cfg *cfg,
6c47a7d40935c5508868904cba1e87ca783ec386niq const char *action)
6c47a7d40935c5508868904cba1e87ca783ec386niq{
6c47a7d40935c5508868904cba1e87ca783ec386niq int rv;
6c47a7d40935c5508868904cba1e87ca783ec386niq const char *newuri = NULL;
6c47a7d40935c5508868904cba1e87ca783ec386niq int nrows;
6c47a7d40935c5508868904cba1e87ca783ec386niq const char *message;
6c47a7d40935c5508868904cba1e87ca783ec386niq ap_dbd_t *dbd = dbd_handle(r);
6c47a7d40935c5508868904cba1e87ca783ec386niq apr_dbd_prepared_t *query;
6c47a7d40935c5508868904cba1e87ca783ec386niq apr_dbd_results_t *res = NULL;
6c47a7d40935c5508868904cba1e87ca783ec386niq apr_dbd_row_t *row = NULL;
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niq if (cfg->query == NULL) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01642)
6c47a7d40935c5508868904cba1e87ca783ec386niq "No query configured for %s!", action);
6c47a7d40935c5508868904cba1e87ca783ec386niq return HTTP_INTERNAL_SERVER_ERROR;
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq query = apr_hash_get(dbd->prepared, cfg->query, APR_HASH_KEY_STRING);
6c47a7d40935c5508868904cba1e87ca783ec386niq if (query == NULL) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01643)
6c47a7d40935c5508868904cba1e87ca783ec386niq "Error retrieving Query for %s!", action);
6c47a7d40935c5508868904cba1e87ca783ec386niq return HTTP_INTERNAL_SERVER_ERROR;
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niq rv = apr_dbd_pvquery(dbd->driver, r->pool, dbd->handle, &nrows,
6c47a7d40935c5508868904cba1e87ca783ec386niq query, r->user, NULL);
6c47a7d40935c5508868904cba1e87ca783ec386niq if (rv == 0) {
6c47a7d40935c5508868904cba1e87ca783ec386niq if (nrows != 1) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01644)
6c47a7d40935c5508868904cba1e87ca783ec386niq "authz_dbd: %s of user %s updated %d rows",
6c47a7d40935c5508868904cba1e87ca783ec386niq action, r->user, nrows);
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq else {
6c47a7d40935c5508868904cba1e87ca783ec386niq message = apr_dbd_error(dbd->driver, dbd->handle, rv);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01645)
6c47a7d40935c5508868904cba1e87ca783ec386niq "authz_dbd: query for %s failed; user %s [%s]",
6c47a7d40935c5508868904cba1e87ca783ec386niq action, r->user, message?message:noerror);
6c47a7d40935c5508868904cba1e87ca783ec386niq return HTTP_INTERNAL_SERVER_ERROR;
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niq if (cfg->redirect == 1) {
6c47a7d40935c5508868904cba1e87ca783ec386niq newuri = apr_table_get(r->headers_in, "Referer");
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niq if (!newuri && cfg->redir_query) {
6c47a7d40935c5508868904cba1e87ca783ec386niq query = apr_hash_get(dbd->prepared, cfg->redir_query,
6c47a7d40935c5508868904cba1e87ca783ec386niq APR_HASH_KEY_STRING);
6c47a7d40935c5508868904cba1e87ca783ec386niq if (query == NULL) {
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 */
6c47a7d40935c5508868904cba1e87ca783ec386niq }
14e0c468a75ddee46ced5c55ea2287642d907ca2sf else if ((rv = apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle,
02f90098143fb719f54c9ab6d3964582a36a6967jailletc &res, query, 0, r->user, NULL)) == 0) {
6c47a7d40935c5508868904cba1e87ca783ec386niq for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
6c47a7d40935c5508868904cba1e87ca783ec386niq rv != -1;
6c47a7d40935c5508868904cba1e87ca783ec386niq rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
d56bacbfefa5aa883ce5162a115747372fc38d13chrisd if (rv != 0) {
6c47a7d40935c5508868904cba1e87ca783ec386niq message = apr_dbd_error(dbd->driver, dbd->handle, rv);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01647)
6c47a7d40935c5508868904cba1e87ca783ec386niq "authz_dbd in get_row; action=%s user=%s [%s]",
6c47a7d40935c5508868904cba1e87ca783ec386niq action, r->user, message?message:noerror);
6c47a7d40935c5508868904cba1e87ca783ec386niq }
d56bacbfefa5aa883ce5162a115747372fc38d13chrisd else if (newuri == NULL) {
d56bacbfefa5aa883ce5162a115747372fc38d13chrisd newuri = apr_dbd_get_entry(dbd->driver, row, 0);
d56bacbfefa5aa883ce5162a115747372fc38d13chrisd }
d56bacbfefa5aa883ce5162a115747372fc38d13chrisd /* we can't break out here or row won't get cleaned up */
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq else {
6c47a7d40935c5508868904cba1e87ca783ec386niq message = apr_dbd_error(dbd->driver, dbd->handle, rv);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01648)
6c47a7d40935c5508868904cba1e87ca783ec386niq "authz_dbd/redirect for %s of %s [%s]",
6c47a7d40935c5508868904cba1e87ca783ec386niq action, r->user, message?message:noerror);
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq if (newuri != NULL) {
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes r->status = HTTP_MOVED_TEMPORARILY;
6c47a7d40935c5508868904cba1e87ca783ec386niq apr_table_set(r->err_headers_out, "Location", newuri);
6c47a7d40935c5508868904cba1e87ca783ec386niq }
d56bacbfefa5aa883ce5162a115747372fc38d13chrisd authz_dbd_run_client_login(r, OK, action);
d56bacbfefa5aa883ce5162a115747372fc38d13chrisd return OK;
6c47a7d40935c5508868904cba1e87ca783ec386niq}
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic int authz_dbd_group_query(request_rec *r, authz_dbd_cfg *cfg,
6c47a7d40935c5508868904cba1e87ca783ec386niq apr_array_header_t *groups)
6c47a7d40935c5508868904cba1e87ca783ec386niq{
6c47a7d40935c5508868904cba1e87ca783ec386niq /* SELECT group FROM authz WHERE user = %s */
6c47a7d40935c5508868904cba1e87ca783ec386niq int rv;
6c47a7d40935c5508868904cba1e87ca783ec386niq const char *message;
6c47a7d40935c5508868904cba1e87ca783ec386niq ap_dbd_t *dbd = dbd_handle(r);
6c47a7d40935c5508868904cba1e87ca783ec386niq apr_dbd_prepared_t *query;
6c47a7d40935c5508868904cba1e87ca783ec386niq apr_dbd_results_t *res = NULL;
6c47a7d40935c5508868904cba1e87ca783ec386niq apr_dbd_row_t *row = NULL;
6c47a7d40935c5508868904cba1e87ca783ec386niq const char **group;
6c47a7d40935c5508868904cba1e87ca783ec386niq
6c47a7d40935c5508868904cba1e87ca783ec386niq if (cfg->query == NULL) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01649)
6c47a7d40935c5508868904cba1e87ca783ec386niq "No query configured for dbd-group!");
6c47a7d40935c5508868904cba1e87ca783ec386niq return HTTP_INTERNAL_SERVER_ERROR;
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq query = apr_hash_get(dbd->prepared, cfg->query, APR_HASH_KEY_STRING);
6c47a7d40935c5508868904cba1e87ca783ec386niq if (query == NULL) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01650)
6c47a7d40935c5508868904cba1e87ca783ec386niq "Error retrieving query for dbd-group!");
6c47a7d40935c5508868904cba1e87ca783ec386niq return HTTP_INTERNAL_SERVER_ERROR;
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq rv = apr_dbd_pvselect(dbd->driver, r->pool, dbd->handle, &res,
6c47a7d40935c5508868904cba1e87ca783ec386niq query, 0, r->user, NULL);
6c47a7d40935c5508868904cba1e87ca783ec386niq if (rv == 0) {
6c47a7d40935c5508868904cba1e87ca783ec386niq for (rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1);
6c47a7d40935c5508868904cba1e87ca783ec386niq rv != -1;
6c47a7d40935c5508868904cba1e87ca783ec386niq rv = apr_dbd_get_row(dbd->driver, r->pool, res, &row, -1)) {
6c47a7d40935c5508868904cba1e87ca783ec386niq if (rv == 0) {
6c47a7d40935c5508868904cba1e87ca783ec386niq group = apr_array_push(groups);
6c47a7d40935c5508868904cba1e87ca783ec386niq *group = apr_dbd_get_entry(dbd->driver, row, 0);
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq else {
6c47a7d40935c5508868904cba1e87ca783ec386niq message = apr_dbd_error(dbd->driver, dbd->handle, rv);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01651)
6c47a7d40935c5508868904cba1e87ca783ec386niq "authz_dbd in get_row; group query for user=%s [%s]",
6c47a7d40935c5508868904cba1e87ca783ec386niq r->user, message?message:noerror);
6c47a7d40935c5508868904cba1e87ca783ec386niq return HTTP_INTERNAL_SERVER_ERROR;
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq else {
6c47a7d40935c5508868904cba1e87ca783ec386niq message = apr_dbd_error(dbd->driver, dbd->handle, rv);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01652)
6c47a7d40935c5508868904cba1e87ca783ec386niq "authz_dbd, in groups query for %s [%s]",
6c47a7d40935c5508868904cba1e87ca783ec386niq r->user, message?message:noerror);
6c47a7d40935c5508868904cba1e87ca783ec386niq return HTTP_INTERNAL_SERVER_ERROR;
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq return OK;
6c47a7d40935c5508868904cba1e87ca783ec386niq}
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholesstatic authz_status dbdgroup_check_authorization(request_rec *r,
97cc46935ec496b83fef9d6feb094d706c895b3bsf const char *require_args,
97cc46935ec496b83fef9d6feb094d706c895b3bsf const void *parsed_require_args)
6c47a7d40935c5508868904cba1e87ca783ec386niq{
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes int i, rv;
6c47a7d40935c5508868904cba1e87ca783ec386niq const char *w;
6c47a7d40935c5508868904cba1e87ca783ec386niq apr_array_header_t *groups = NULL;
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin const char *err = NULL;
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin const ap_expr_info_t *expr = parsed_require_args;
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin const char *require;
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin
6c47a7d40935c5508868904cba1e87ca783ec386niq const char *t;
6c47a7d40935c5508868904cba1e87ca783ec386niq authz_dbd_cfg *cfg = ap_get_module_config(r->per_dir_config,
6c47a7d40935c5508868904cba1e87ca783ec386niq &authz_dbd_module);
6c47a7d40935c5508868904cba1e87ca783ec386niq
db027b0af55a7fa31f2e1659368d606a8b860bb0chrisd if (!r->user) {
18b5268e013574026b2503b1641baf3299045f45sf return AUTHZ_DENIED_NO_USER;
db027b0af55a7fa31f2e1659368d606a8b860bb0chrisd }
db027b0af55a7fa31f2e1659368d606a8b860bb0chrisd
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes if (groups == NULL) {
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes groups = apr_array_make(r->pool, 4, sizeof(const char*));
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes rv = authz_dbd_group_query(r, cfg, groups);
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes if (rv != OK) {
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes return AUTHZ_GENERAL_ERROR;
6c47a7d40935c5508868904cba1e87ca783ec386niq }
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes }
6c47a7d40935c5508868904cba1e87ca783ec386niq
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin require = ap_expr_str_exec(r, expr, &err);
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin if (err) {
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02590)
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin "authz_dbd authorize: require dbd-group: Can't "
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin "evaluate require expression: %s", err);
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin return AUTHZ_DENIED;
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin }
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin t = require;
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes while (t[0]) {
6c47a7d40935c5508868904cba1e87ca783ec386niq w = ap_getword_white(r->pool, &t);
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes for (i=0; i < groups->nelts; ++i) {
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes if (!strcmp(w, ((const char**)groups->elts)[i])) {
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes return AUTHZ_GRANTED;
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq }
6c47a7d40935c5508868904cba1e87ca783ec386niq
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes return AUTHZ_DENIED;
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes}
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholesstatic authz_status dbdlogin_check_authorization(request_rec *r,
97cc46935ec496b83fef9d6feb094d706c895b3bsf const char *require_args,
97cc46935ec496b83fef9d6feb094d706c895b3bsf const void *parsed_require_args)
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes{
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes authz_dbd_cfg *cfg = ap_get_module_config(r->per_dir_config,
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes &authz_dbd_module);
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
db027b0af55a7fa31f2e1659368d606a8b860bb0chrisd if (!r->user) {
18b5268e013574026b2503b1641baf3299045f45sf return AUTHZ_DENIED_NO_USER;
db027b0af55a7fa31f2e1659368d606a8b860bb0chrisd }
db027b0af55a7fa31f2e1659368d606a8b860bb0chrisd
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes return (authz_dbd_login(r, cfg, "login") == OK ? AUTHZ_GRANTED : AUTHZ_DENIED);
6c47a7d40935c5508868904cba1e87ca783ec386niq}
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholesstatic authz_status dbdlogout_check_authorization(request_rec *r,
97cc46935ec496b83fef9d6feb094d706c895b3bsf const char *require_args,
97cc46935ec496b83fef9d6feb094d706c895b3bsf const void *parsed_require_args)
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes{
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes authz_dbd_cfg *cfg = ap_get_module_config(r->per_dir_config,
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes &authz_dbd_module);
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
db027b0af55a7fa31f2e1659368d606a8b860bb0chrisd if (!r->user) {
18b5268e013574026b2503b1641baf3299045f45sf return AUTHZ_DENIED_NO_USER;
db027b0af55a7fa31f2e1659368d606a8b860bb0chrisd }
db027b0af55a7fa31f2e1659368d606a8b860bb0chrisd
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes return (authz_dbd_login(r, cfg, "logout") == OK ? AUTHZ_GRANTED : AUTHZ_DENIED);
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes}
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrinstatic const char *dbd_parse_config(cmd_parms *cmd, const char *require_line,
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin const void **parsed_require_line)
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin{
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin const char *expr_err = NULL;
f6098228fada9d4d6dcdc0cd321f4f5904040d1etrawick ap_expr_info_t *expr;
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin expr = ap_expr_parse_cmd(cmd, require_line, AP_EXPR_FLAG_STRING_RESULT,
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin &expr_err, NULL);
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin if (expr_err)
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin return apr_pstrcat(cmd->temp_pool,
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin "Cannot parse expression in require line: ",
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin expr_err, NULL);
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin *parsed_require_line = expr;
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin return NULL;
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin}
0cf25f370efc889c01f7028bdfc8204b17aa4ce4minfrin
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholesstatic const authz_provider authz_dbdgroup_provider =
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes{
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes &dbdgroup_check_authorization,
c391425e78669ebefa01c928dea44d4dd4ffe1bfminfrin &dbd_parse_config,
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes};
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholesstatic const authz_provider authz_dbdlogin_provider =
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes{
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes &dbdlogin_check_authorization,
97cc46935ec496b83fef9d6feb094d706c895b3bsf NULL,
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes};
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholesstatic const authz_provider authz_dbdlogout_provider =
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes{
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes &dbdlogout_check_authorization,
97cc46935ec496b83fef9d6feb094d706c895b3bsf NULL,
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes};
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
6c47a7d40935c5508868904cba1e87ca783ec386niqstatic void authz_dbd_hooks(apr_pool_t *p)
6c47a7d40935c5508868904cba1e87ca783ec386niq{
2e242dca7111f99d54dd144b7b8418d88d560032chrisd ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbd-group",
2e242dca7111f99d54dd144b7b8418d88d560032chrisd AUTHZ_PROVIDER_VERSION,
a72211e92bab814bfa28ee086ca9b2a1a6095c92chrisd &authz_dbdgroup_provider,
a72211e92bab814bfa28ee086ca9b2a1a6095c92chrisd AP_AUTH_INTERNAL_PER_CONF);
2e242dca7111f99d54dd144b7b8418d88d560032chrisd ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbd-login",
2e242dca7111f99d54dd144b7b8418d88d560032chrisd AUTHZ_PROVIDER_VERSION,
a72211e92bab814bfa28ee086ca9b2a1a6095c92chrisd &authz_dbdlogin_provider,
a72211e92bab814bfa28ee086ca9b2a1a6095c92chrisd AP_AUTH_INTERNAL_PER_CONF);
2e242dca7111f99d54dd144b7b8418d88d560032chrisd ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbd-logout",
2e242dca7111f99d54dd144b7b8418d88d560032chrisd AUTHZ_PROVIDER_VERSION,
a72211e92bab814bfa28ee086ca9b2a1a6095c92chrisd &authz_dbdlogout_provider,
a72211e92bab814bfa28ee086ca9b2a1a6095c92chrisd AP_AUTH_INTERNAL_PER_CONF);
6c47a7d40935c5508868904cba1e87ca783ec386niq}
367d146f245f3b1c9f77c18e6ec591b52e0b344cbnicholes
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asfAP_DECLARE_MODULE(authz_dbd) =
6c47a7d40935c5508868904cba1e87ca783ec386niq{
6c47a7d40935c5508868904cba1e87ca783ec386niq STANDARD20_MODULE_STUFF,
6c47a7d40935c5508868904cba1e87ca783ec386niq authz_dbd_cr_cfg,
6c47a7d40935c5508868904cba1e87ca783ec386niq authz_dbd_merge_cfg,
6c47a7d40935c5508868904cba1e87ca783ec386niq NULL,
6c47a7d40935c5508868904cba1e87ca783ec386niq NULL,
6c47a7d40935c5508868904cba1e87ca783ec386niq authz_dbd_cmds,
6c47a7d40935c5508868904cba1e87ca783ec386niq authz_dbd_hooks
6c47a7d40935c5508868904cba1e87ca783ec386niq};
6c47a7d40935c5508868904cba1e87ca783ec386niq