|
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 |
/* This module is triggered by an
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* AuthGroupFile standard /path/to/file
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* and the presence of a
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* require group <list-of-groups>
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* In an applicable limit/directory block for that method.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* If there are no AuthGroupFile directives valid for
|
|
Packit |
90a5c9 |
* the request; we DECLINED.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* If the AuthGroupFile is defined; but somehow not
|
|
Packit |
90a5c9 |
* accessible: we SERVER_ERROR (was DECLINED).
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* If there are no 'require ' directives defined for
|
|
Packit |
90a5c9 |
* this request then we DECLINED (was OK).
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* If there are no 'require ' directives valid for
|
|
Packit |
90a5c9 |
* this request method then we DECLINED. (was OK)
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* If there are any 'require group' blocks and we
|
|
Packit |
90a5c9 |
* are not in any group - we HTTP_UNAUTHORIZE
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "apr_strings.h"
|
|
Packit |
90a5c9 |
#include "apr_lib.h" /* apr_isspace */
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "ap_config.h"
|
|
Packit |
90a5c9 |
#include "ap_provider.h"
|
|
Packit |
90a5c9 |
#include "httpd.h"
|
|
Packit |
90a5c9 |
#include "http_config.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"
|
|
Packit |
90a5c9 |
#include "util_varbuf.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "mod_auth.h"
|
|
Packit |
90a5c9 |
#include "mod_authz_owner.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct {
|
|
Packit |
90a5c9 |
char *groupfile;
|
|
Packit |
90a5c9 |
} authz_groupfile_config_rec;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *create_authz_groupfile_dir_config(apr_pool_t *p, char *d)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authz_groupfile_config_rec *conf = apr_palloc(p, sizeof(*conf));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
conf->groupfile = NULL;
|
|
Packit |
90a5c9 |
return conf;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const command_rec authz_groupfile_cmds[] =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
AP_INIT_TAKE1("AuthGroupFile", ap_set_file_slot,
|
|
Packit |
90a5c9 |
(void *)APR_OFFSETOF(authz_groupfile_config_rec, groupfile),
|
|
Packit |
90a5c9 |
OR_AUTHCFG,
|
|
Packit |
90a5c9 |
"text file containing group names and member user IDs"),
|
|
Packit |
90a5c9 |
{NULL}
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
module AP_MODULE_DECLARE_DATA authz_groupfile_module;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#define VARBUF_INIT_LEN 512
|
|
Packit |
90a5c9 |
#define VARBUF_MAX_LEN (16*1024*1024)
|
|
Packit |
90a5c9 |
static apr_status_t groups_for_user(apr_pool_t *p, char *user, char *grpfile,
|
|
Packit |
90a5c9 |
apr_table_t ** out)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_configfile_t *f;
|
|
Packit |
90a5c9 |
apr_table_t *grps = apr_table_make(p, 15);
|
|
Packit |
90a5c9 |
apr_pool_t *sp;
|
|
Packit |
90a5c9 |
struct ap_varbuf vb;
|
|
Packit |
90a5c9 |
const char *group_name, *ll, *w;
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
apr_size_t group_len;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if ((status = ap_pcfg_openfile(&f, p, grpfile)) != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return status ;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_pool_create(&sp, p);
|
|
Packit |
90a5c9 |
ap_varbuf_init(p, &vb, VARBUF_INIT_LEN);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
while (!(ap_varbuf_cfg_getline(&vb, f, VARBUF_MAX_LEN))) {
|
|
Packit |
90a5c9 |
if ((vb.buf[0] == '#') || (!vb.buf[0])) {
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ll = vb.buf;
|
|
Packit |
90a5c9 |
apr_pool_clear(sp);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
group_name = ap_getword(sp, &ll, ':');
|
|
Packit |
90a5c9 |
group_len = strlen(group_name);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
while (group_len && apr_isspace(*(group_name + group_len - 1))) {
|
|
Packit |
90a5c9 |
--group_len;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
while (ll[0]) {
|
|
Packit |
90a5c9 |
w = ap_getword_conf(sp, &ll);
|
|
Packit |
90a5c9 |
if (!strcmp(w, user)) {
|
|
Packit |
90a5c9 |
apr_table_setn(grps, apr_pstrmemdup(p, group_name, group_len),
|
|
Packit |
90a5c9 |
"in");
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ap_cfg_closefile(f);
|
|
Packit |
90a5c9 |
apr_pool_destroy(sp);
|
|
Packit |
90a5c9 |
ap_varbuf_free(&vb;;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
*out = grps;
|
|
Packit |
90a5c9 |
return APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static authz_status group_check_authorization(request_rec *r,
|
|
Packit |
90a5c9 |
const char *require_args,
|
|
Packit |
90a5c9 |
const void *parsed_require_args)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authz_groupfile_config_rec *conf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&authz_groupfile_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, *w;
|
|
Packit |
90a5c9 |
apr_table_t *grpstatus = NULL;
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!user) {
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED_NO_USER;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* If there is no group file - then we are not
|
|
Packit |
90a5c9 |
* configured. So decline.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (!(conf->groupfile)) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01664)
|
|
Packit |
90a5c9 |
"No group file was specified in the configuration");
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = groups_for_user(r->pool, user, conf->groupfile,
|
|
Packit |
90a5c9 |
&grpstatus);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (status != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01665)
|
|
Packit |
90a5c9 |
"Could not open group file: %s",
|
|
Packit |
90a5c9 |
conf->groupfile);
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (apr_is_empty_table(grpstatus)) {
|
|
Packit |
90a5c9 |
/* no groups available, so exit immediately */
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01666)
|
|
Packit |
90a5c9 |
"Authorization of user %s to access %s failed, reason: "
|
|
Packit |
90a5c9 |
"user doesn't appear in group file (%s).",
|
|
Packit |
90a5c9 |
r->user, r->uri, conf->groupfile);
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
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(02592)
|
|
Packit |
90a5c9 |
"authz_groupfile authorize: require 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_conf(r->pool, &t)) && w[0]) {
|
|
Packit |
90a5c9 |
if (apr_table_get(grpstatus, w)) {
|
|
Packit |
90a5c9 |
return AUTHZ_GRANTED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01667)
|
|
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 filegroup_check_authorization(request_rec *r,
|
|
Packit |
90a5c9 |
const char *require_args,
|
|
Packit |
90a5c9 |
const void *parsed_require_args)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authz_groupfile_config_rec *conf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&authz_groupfile_module);
|
|
Packit |
90a5c9 |
char *user = r->user;
|
|
Packit |
90a5c9 |
apr_table_t *grpstatus = NULL;
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
const char *filegroup = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!user) {
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED_NO_USER;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* If there is no group file - then we are not
|
|
Packit |
90a5c9 |
* configured. So decline.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (!(conf->groupfile)) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01668)
|
|
Packit |
90a5c9 |
"No group file was specified in the configuration");
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = groups_for_user(r->pool, user, conf->groupfile,
|
|
Packit |
90a5c9 |
&grpstatus);
|
|
Packit |
90a5c9 |
if (status != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01669)
|
|
Packit |
90a5c9 |
"Could not open group file: %s",
|
|
Packit |
90a5c9 |
conf->groupfile);
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (apr_is_empty_table(grpstatus)) {
|
|
Packit |
90a5c9 |
/* no groups available, so exit immediately */
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01670)
|
|
Packit |
90a5c9 |
"Authorization of user %s to access %s failed, reason: "
|
|
Packit |
90a5c9 |
"user doesn't appear in group file (%s).",
|
|
Packit |
90a5c9 |
r->user, r->uri, conf->groupfile);
|
|
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 |
if (apr_table_get(grpstatus, filegroup)) {
|
|
Packit |
90a5c9 |
return AUTHZ_GRANTED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
/* No need to emit a error log entry because the call
|
|
Packit |
90a5c9 |
to authz_owner_get_file_group already did it
|
|
Packit |
90a5c9 |
for us.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01671)
|
|
Packit |
90a5c9 |
"Authorization of user %s to access %s failed, reason: "
|
|
Packit |
90a5c9 |
"user is not part of the 'require'ed file group.",
|
|
Packit |
90a5c9 |
r->user, r->uri);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return AUTHZ_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *groupfile_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_group_provider =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
&group_check_authorization,
|
|
Packit |
90a5c9 |
&groupfile_parse_config,
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const authz_provider authz_filegroup_provider =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
&filegroup_check_authorization,
|
|
Packit |
90a5c9 |
NULL,
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void authz_groupfile_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, "group",
|
|
Packit |
90a5c9 |
AUTHZ_PROVIDER_VERSION,
|
|
Packit |
90a5c9 |
&authz_group_provider,
|
|
Packit |
90a5c9 |
AP_AUTH_INTERNAL_PER_CONF);
|
|
Packit |
90a5c9 |
ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "file-group",
|
|
Packit |
90a5c9 |
AUTHZ_PROVIDER_VERSION,
|
|
Packit |
90a5c9 |
&authz_filegroup_provider,
|
|
Packit |
90a5c9 |
AP_AUTH_INTERNAL_PER_CONF);
|
|
Packit |
90a5c9 |
ap_hook_optional_fn_retrieve(authz_groupfile_getfns, NULL, NULL, APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
AP_DECLARE_MODULE(authz_groupfile) =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
STANDARD20_MODULE_STUFF,
|
|
Packit |
90a5c9 |
create_authz_groupfile_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_groupfile_cmds, /* command apr_table_t */
|
|
Packit |
90a5c9 |
register_hooks /* register hooks */
|
|
Packit |
90a5c9 |
};
|