|
Packit |
90a5c9 |
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
Packit |
90a5c9 |
* contributor license agreements. See the NOTICE file distributed with
|
|
Packit |
90a5c9 |
* this work for additional information regarding copyright ownership.
|
|
Packit |
90a5c9 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
Packit |
90a5c9 |
* (the "License"); you may not use this file except in compliance with
|
|
Packit |
90a5c9 |
* the License. You may obtain a copy of the License at
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
90a5c9 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
90a5c9 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
90a5c9 |
* See the License for the specific language governing permissions and
|
|
Packit |
90a5c9 |
* limitations under the License.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#define APR_WANT_STRFUNC
|
|
Packit |
90a5c9 |
#include "apr_want.h"
|
|
Packit |
90a5c9 |
#include "apr_strings.h"
|
|
Packit |
90a5c9 |
#include "apr_dbm.h"
|
|
Packit |
90a5c9 |
#include "apr_md5.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "httpd.h"
|
|
Packit |
90a5c9 |
#include "http_config.h"
|
|
Packit |
90a5c9 |
#include "ap_provider.h"
|
|
Packit |
90a5c9 |
#include "http_core.h"
|
|
Packit |
90a5c9 |
#include "http_log.h"
|
|
Packit |
90a5c9 |
#include "http_protocol.h"
|
|
Packit |
90a5c9 |
#include "http_request.h" /* for ap_hook_(check_user_id | auth_checker)*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "mod_auth.h"
|
|
Packit |
90a5c9 |
#include "mod_authz_owner.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct {
|
|
Packit |
90a5c9 |
const char *grpfile;
|
|
Packit |
90a5c9 |
const char *dbmtype;
|
|
Packit |
90a5c9 |
} authz_dbm_config_rec;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* This should go into APR; perhaps with some nice
|
|
Packit |
90a5c9 |
* caching/locking/flocking of the open dbm file.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static char *get_dbm_entry_as_str(apr_pool_t *pool, apr_dbm_t *f, char *key)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_datum_t d, q;
|
|
Packit |
90a5c9 |
q.dptr = key;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#ifndef NETSCAPE_DBM_COMPAT
|
|
Packit |
90a5c9 |
q.dsize = strlen(q.dptr);
|
|
Packit |
90a5c9 |
#else
|
|
Packit |
90a5c9 |
q.dsize = strlen(q.dptr) + 1;
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (apr_dbm_fetch(f, q, &d) == APR_SUCCESS && d.dptr) {
|
|
Packit |
90a5c9 |
return apr_pstrmemdup(pool, d.dptr, d.dsize);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *create_authz_dbm_dir_config(apr_pool_t *p, char *d)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authz_dbm_config_rec *conf = apr_palloc(p, sizeof(*conf));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
conf->grpfile = NULL;
|
|
Packit |
90a5c9 |
conf->dbmtype = "default";
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return conf;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const command_rec authz_dbm_cmds[] =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
AP_INIT_TAKE1("AuthDBMGroupFile", ap_set_file_slot,
|
|
Packit |
90a5c9 |
(void *)APR_OFFSETOF(authz_dbm_config_rec, grpfile),
|
|
Packit |
90a5c9 |
OR_AUTHCFG, "database file containing group names and member user IDs"),
|
|
Packit |
90a5c9 |
AP_INIT_TAKE1("AuthzDBMType", ap_set_string_slot,
|
|
Packit |
90a5c9 |
(void *)APR_OFFSETOF(authz_dbm_config_rec, dbmtype),
|
|
Packit |
90a5c9 |
OR_AUTHCFG, "what type of DBM file the group file is"),
|
|
Packit |
90a5c9 |
{NULL}
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
module AP_MODULE_DECLARE_DATA authz_dbm_module;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* We do something strange with the group file. If the group file
|
|
Packit |
90a5c9 |
* contains any : we assume the format is
|
|
Packit |
90a5c9 |
* key=username value=":"groupname [":"anything here is ignored]
|
|
Packit |
90a5c9 |
* otherwise we now (0.8.14+) assume that the format is
|
|
Packit |
90a5c9 |
* key=username value=groupname
|
|
Packit |
90a5c9 |
* The first allows the password and group files to be the same
|
|
Packit |
90a5c9 |
* physical DBM file; key=username value=password":"groupname[":"anything]
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* mark@telescope.org, 22Sep95
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t get_dbm_grp(request_rec *r, char *key1, char *key2,
|
|
Packit |
90a5c9 |
const char *dbmgrpfile, const char *dbtype,
|
|
Packit |
90a5c9 |
const char ** out)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
char *grp_colon, *val;
|
|
Packit |
90a5c9 |
apr_status_t retval;
|
|
Packit |
90a5c9 |
apr_dbm_t *f;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
retval = apr_dbm_open_ex(&f, dbtype, dbmgrpfile, APR_DBM_READONLY,
|
|
Packit |
90a5c9 |
APR_OS_DEFAULT, r->pool);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (retval != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return retval;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Try key2 only if key1 failed */
|
|
Packit |
90a5c9 |
if (!(val = get_dbm_entry_as_str(r->pool, f, key1))) {
|
|
Packit |
90a5c9 |
val = get_dbm_entry_as_str(r->pool, f, key2);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_dbm_close(f);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (val && (grp_colon = ap_strchr(val, ':')) != NULL) {
|
|
Packit |
90a5c9 |
char *grp_colon2 = ap_strchr(++grp_colon, ':');
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (grp_colon2) {
|
|
Packit |
90a5c9 |
*grp_colon2 = '\0';
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
*out = grp_colon;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
*out = val;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return retval;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static authz_status dbmgroup_check_authorization(request_rec *r,
|
|
Packit |
90a5c9 |
const char *require_args,
|
|
Packit |
90a5c9 |
const void *parsed_require_args)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authz_dbm_config_rec *conf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&authz_dbm_module);
|
|
Packit |
90a5c9 |
char *user = r->user;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
const char *err = NULL;
|
|
Packit |
90a5c9 |
const ap_expr_info_t *expr = parsed_require_args;
|
|
Packit |
90a5c9 |
const char *require;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
const char *t;
|
|
Packit |
90a5c9 |
char *w;
|
|
Packit |
90a5c9 |
const char *orig_groups = NULL;
|
|
Packit |
90a5c9 |
const char *realm = ap_auth_name(r);
|
|
Packit |
90a5c9 |
const char *groups;
|
|
Packit |
90a5c9 |
char *v;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!user) {
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED_NO_USER;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!conf->grpfile) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01798)
|
|
Packit |
90a5c9 |
"No group file was specified in the configuration");
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* fetch group data from dbm file only once. */
|
|
Packit |
90a5c9 |
if (!orig_groups) {
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = get_dbm_grp(r, apr_pstrcat(r->pool, user, ":", realm, NULL),
|
|
Packit |
90a5c9 |
user, conf->grpfile, conf->dbmtype, &groups);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (status != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01799)
|
|
Packit |
90a5c9 |
"could not open dbm (type %s) group access "
|
|
Packit |
90a5c9 |
"file: %s", conf->dbmtype, conf->grpfile);
|
|
Packit |
90a5c9 |
return AUTHZ_GENERAL_ERROR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (groups == NULL) {
|
|
Packit |
90a5c9 |
/* no groups available, so exit immediately */
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01800)
|
|
Packit |
90a5c9 |
"Authorization of user %s to access %s failed, reason: "
|
|
Packit |
90a5c9 |
"user doesn't appear in DBM group file (%s).",
|
|
Packit |
90a5c9 |
r->user, r->uri, conf->grpfile);
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
orig_groups = groups;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
require = ap_expr_str_exec(r, expr, &err;;
|
|
Packit |
90a5c9 |
if (err) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02591)
|
|
Packit |
90a5c9 |
"authz_dbm authorize: require dbm-group: Can't "
|
|
Packit |
90a5c9 |
"evaluate require expression: %s", err);
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
t = require;
|
|
Packit |
90a5c9 |
while ((w = ap_getword_white(r->pool, &t)) && w[0]) {
|
|
Packit |
90a5c9 |
groups = orig_groups;
|
|
Packit |
90a5c9 |
while (groups[0]) {
|
|
Packit |
90a5c9 |
v = ap_getword(r->pool, &groups, ',');
|
|
Packit |
90a5c9 |
if (!strcmp(v, w)) {
|
|
Packit |
90a5c9 |
return AUTHZ_GRANTED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01801)
|
|
Packit |
90a5c9 |
"Authorization of user %s to access %s failed, reason: "
|
|
Packit |
90a5c9 |
"user is not part of the 'require'ed group(s).",
|
|
Packit |
90a5c9 |
r->user, r->uri);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static APR_OPTIONAL_FN_TYPE(authz_owner_get_file_group) *authz_owner_get_file_group;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static authz_status dbmfilegroup_check_authorization(request_rec *r,
|
|
Packit |
90a5c9 |
const char *require_args,
|
|
Packit |
90a5c9 |
const void *parsed_require_args)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authz_dbm_config_rec *conf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&authz_dbm_module);
|
|
Packit |
90a5c9 |
char *user = r->user;
|
|
Packit |
90a5c9 |
const char *realm = ap_auth_name(r);
|
|
Packit |
90a5c9 |
const char *filegroup = NULL;
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
const char *groups;
|
|
Packit |
90a5c9 |
char *v;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!user) {
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED_NO_USER;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!conf->grpfile) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01802)
|
|
Packit |
90a5c9 |
"No group file was specified in the configuration");
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* fetch group data from dbm file. */
|
|
Packit |
90a5c9 |
status = get_dbm_grp(r, apr_pstrcat(r->pool, user, ":", realm, NULL),
|
|
Packit |
90a5c9 |
user, conf->grpfile, conf->dbmtype, &groups);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (status != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01803)
|
|
Packit |
90a5c9 |
"could not open dbm (type %s) group access "
|
|
Packit |
90a5c9 |
"file: %s", conf->dbmtype, conf->grpfile);
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (groups == NULL) {
|
|
Packit |
90a5c9 |
/* no groups available, so exit immediately */
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01804)
|
|
Packit |
90a5c9 |
"Authorization of user %s to access %s failed, reason: "
|
|
Packit |
90a5c9 |
"user doesn't appear in DBM group file (%s).",
|
|
Packit |
90a5c9 |
r->user, r->uri, conf->grpfile);
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
filegroup = authz_owner_get_file_group(r);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (filegroup) {
|
|
Packit |
90a5c9 |
while (groups[0]) {
|
|
Packit |
90a5c9 |
v = ap_getword(r->pool, &groups, ',');
|
|
Packit |
90a5c9 |
if (!strcmp(v, filegroup)) {
|
|
Packit |
90a5c9 |
return AUTHZ_GRANTED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01805)
|
|
Packit |
90a5c9 |
"Authorization of user %s to access %s failed, reason: "
|
|
Packit |
90a5c9 |
"user is not part of the 'require'ed group(s).",
|
|
Packit |
90a5c9 |
r->user, r->uri);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *dbm_parse_config(cmd_parms *cmd, const char *require_line,
|
|
Packit |
90a5c9 |
const void **parsed_require_line)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
const char *expr_err = NULL;
|
|
Packit |
90a5c9 |
ap_expr_info_t *expr;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
expr = ap_expr_parse_cmd(cmd, require_line, AP_EXPR_FLAG_STRING_RESULT,
|
|
Packit |
90a5c9 |
&expr_err, NULL);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (expr_err)
|
|
Packit |
90a5c9 |
return apr_pstrcat(cmd->temp_pool,
|
|
Packit |
90a5c9 |
"Cannot parse expression in require line: ",
|
|
Packit |
90a5c9 |
expr_err, NULL);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
*parsed_require_line = expr;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const authz_provider authz_dbmgroup_provider =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
&dbmgroup_check_authorization,
|
|
Packit |
90a5c9 |
&dbm_parse_config,
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const authz_provider authz_dbmfilegroup_provider =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
&dbmfilegroup_check_authorization,
|
|
Packit |
90a5c9 |
NULL,
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void authz_dbm_getfns(void)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authz_owner_get_file_group = APR_RETRIEVE_OPTIONAL_FN(authz_owner_get_file_group);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void register_hooks(apr_pool_t *p)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbm-group",
|
|
Packit |
90a5c9 |
AUTHZ_PROVIDER_VERSION,
|
|
Packit |
90a5c9 |
&authz_dbmgroup_provider,
|
|
Packit |
90a5c9 |
AP_AUTH_INTERNAL_PER_CONF);
|
|
Packit |
90a5c9 |
ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbm-file-group",
|
|
Packit |
90a5c9 |
AUTHZ_PROVIDER_VERSION,
|
|
Packit |
90a5c9 |
&authz_dbmfilegroup_provider,
|
|
Packit |
90a5c9 |
AP_AUTH_INTERNAL_PER_CONF);
|
|
Packit |
90a5c9 |
ap_hook_optional_fn_retrieve(authz_dbm_getfns, NULL, NULL, APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
AP_DECLARE_MODULE(authz_dbm) =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
STANDARD20_MODULE_STUFF,
|
|
Packit |
90a5c9 |
create_authz_dbm_dir_config, /* dir config creater */
|
|
Packit |
90a5c9 |
NULL, /* dir merger --- default is to override */
|
|
Packit |
90a5c9 |
NULL, /* server config */
|
|
Packit |
90a5c9 |
NULL, /* merge server config */
|
|
Packit |
90a5c9 |
authz_dbm_cmds, /* command apr_table_t */
|
|
Packit |
90a5c9 |
register_hooks /* register hooks */
|
|
Packit |
90a5c9 |
};
|