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.
17
/* This module is triggered by an
19
* AuthGroupFile standard /path/to/file
21
* and the presense of a
23
* require group <list-of-groups>
25
* In an applicable limit/directory block for that method.
27
* If there are no AuthGroupFile directives valid for
28
* the request; we DECLINED.
30
* If the AuthGroupFile is defined; but somehow not
31
* accessible: we SERVER_ERROR (was DECLINED).
33
* If there are no 'require ' directives defined for
34
* this request then we DECLINED (was OK).
36
* If there are no 'require ' directives valid for
37
* this request method then we DECLINED. (was OK)
39
* If there are any 'require group' blocks and we
40
* are not in any group - we HTTP_UNAUTHORIZE
41
* unless we are non-authoritative; in which
46
#include "apr_strings.h"
47
#include "apr_lib.h" /* apr_isspace */
49
#include "ap_config.h"
51
#include "http_config.h"
52
#include "http_core.h"
54
#include "http_protocol.h"
55
#include "http_request.h"
62
} authz_groupfile_config_rec;
64
static void *create_authz_groupfile_dir_config(apr_pool_t *p, char *d)
66
authz_groupfile_config_rec *conf = apr_palloc(p, sizeof(*conf));
68
conf->groupfile = NULL;
69
conf->authoritative = 1; /* keep the fortress secure by default */
73
static const char *set_authz_groupfile_slot(cmd_parms *cmd, void *offset, const char *f,
76
if (t && strcmp(t, "standard")) {
77
return apr_pstrcat(cmd->pool, "Invalid auth file type: ", t, NULL);
80
return ap_set_file_slot(cmd, offset, f);
83
static const command_rec authz_groupfile_cmds[] =
85
AP_INIT_TAKE12("AuthGroupFile", set_authz_groupfile_slot,
86
(void *)APR_OFFSETOF(authz_groupfile_config_rec, groupfile),
88
"text file containing group names and member user IDs"),
89
AP_INIT_FLAG("AuthzGroupFileAuthoritative", ap_set_flag_slot,
90
(void *)APR_OFFSETOF(authz_groupfile_config_rec,
93
"Set to 'Off' to allow access control to be passed along to "
94
"lower modules if the 'require group' fails. (default is "
99
module AP_MODULE_DECLARE_DATA authz_groupfile_module;
101
static apr_status_t groups_for_user(apr_pool_t *p, char *user, char *grpfile,
105
apr_table_t *grps = apr_table_make(p, 15);
107
char l[MAX_STRING_LEN];
108
const char *group_name, *ll, *w;
110
apr_size_t group_len;
112
if ((status = ap_pcfg_openfile(&f, p, grpfile)) != APR_SUCCESS) {
116
apr_pool_create(&sp, p);
118
while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
119
if ((l[0] == '#') || (!l[0])) {
125
group_name = ap_getword(sp, &ll, ':');
126
group_len = strlen(group_name);
128
while (group_len && apr_isspace(*(group_name + group_len - 1))) {
133
w = ap_getword_conf(sp, &ll);
134
if (!strcmp(w, user)) {
135
apr_table_setn(grps, apr_pstrmemdup(p, group_name, group_len),
142
apr_pool_destroy(sp);
150
static int check_user_access(request_rec *r)
152
authz_groupfile_config_rec *conf = ap_get_module_config(r->per_dir_config,
153
&authz_groupfile_module);
154
char *user = r->user;
155
int m = r->method_number;
156
int required_group = 0;
159
apr_table_t *grpstatus = NULL;
160
const apr_array_header_t *reqs_arr = ap_requires(r);
162
const char *filegroup = NULL;
165
/* If there is no group file - then we are not
166
* configured. So decline.
168
if (!(conf->groupfile)) {
173
return DECLINED; /* XXX change from legacy */
176
reqs = (require_line *)reqs_arr->elts;
178
for (x = 0; x < reqs_arr->nelts; x++) {
180
if (!(reqs[x].method_mask & (AP_METHOD_BIT << m))) {
184
t = reqs[x].requirement;
185
w = ap_getword_white(r->pool, &t);
187
/* needs mod_authz_owner to be present */
188
if (!strcasecmp(w, "file-group")) {
189
filegroup = apr_table_get(r->notes, AUTHZ_GROUP_NOTE);
192
/* mod_authz_owner is not present or not
193
* authoritative. We are just a helper module for testing
194
* group membership, so we don't care and decline.
200
if (!strcasecmp(w, "group") || filegroup) {
201
required_group = 1; /* remember the requirement */
203
/* create group table only if actually needed. */
207
status = groups_for_user(r->pool, user, conf->groupfile,
210
if (status != APR_SUCCESS) {
211
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
212
"Could not open group file: %s",
214
return HTTP_INTERNAL_SERVER_ERROR;
217
if (apr_table_elts(grpstatus)->nelts == 0) {
218
/* no groups available, so exit immediately */
219
reason = apr_psprintf(r->pool,
220
"user doesn't appear in group file "
221
"(%s).", conf->groupfile);
227
if (apr_table_get(grpstatus, filegroup)) {
231
if (conf->authoritative) {
232
reason = apr_psprintf(r->pool,
233
"file group '%s' does not match.",
238
/* now forget the filegroup, thus alternatively require'd
239
groups get a real chance */
244
w = ap_getword_conf(r->pool, &t);
245
if (apr_table_get(grpstatus, w)) {
253
/* No applicable "require group" for this method seen */
254
if (!required_group || !conf->authoritative) {
258
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
259
"Authorization of user %s to access %s failed, reason: %s",
261
reason ? reason : "user is not part of the "
262
"'require'ed group(s).");
264
ap_note_auth_failure(r);
265
return HTTP_UNAUTHORIZED;
268
static void register_hooks(apr_pool_t *p)
270
static const char * const aszPre[]={ "mod_authz_owner.c", NULL };
272
ap_hook_auth_checker(check_user_access, aszPre, NULL, APR_HOOK_MIDDLE);
275
module AP_MODULE_DECLARE_DATA authz_groupfile_module =
277
STANDARD20_MODULE_STUFF,
278
create_authz_groupfile_dir_config,/* dir config creater */
279
NULL, /* dir merger -- default is to override */
280
NULL, /* server config */
281
NULL, /* merge server config */
282
authz_groupfile_cmds, /* command apr_table_t */
283
register_hooks /* register hooks */