1 /* Copyright 2000-2004 Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 /* This module is triggered by an
18 * AuthGroupFile standard /path/to/file
20 * and the presense of a
22 * require group <list-of-groups>
24 * In an applicable limit/directory block for that method.
26 * If there are no AuthGroupFile directives valid for
27 * the request; we DECLINED.
29 * If the AuthGroupFile is defined; but somehow not
30 * accessible: we SERVER_ERROR (was DECLINED).
32 * If there are no 'require ' directives defined for
33 * this request then we DECLINED (was OK).
35 * If there are no 'require ' directives valid for
36 * this request method then we DECLINED. (was OK)
38 * If there are any 'require group' blocks and we
39 * are not in any group - we HTTP_UNAUTHORIZE
40 * unless we are non-authoritative; in which
45 #include "apr_strings.h"
46 #include "apr_lib.h" /* apr_isspace */
48 #include "ap_config.h"
50 #include "http_config.h"
51 #include "http_core.h"
53 #include "http_protocol.h"
54 #include "http_request.h"
61 } authz_groupfile_config_rec;
63 static void *create_authz_groupfile_dir_config(apr_pool_t *p, char *d)
65 authz_groupfile_config_rec *conf = apr_palloc(p, sizeof(*conf));
67 conf->groupfile = NULL;
68 conf->authoritative = 1; /* keep the fortress secure by default */
72 static const char *set_authz_groupfile_slot(cmd_parms *cmd, void *offset, const char *f,
75 if (t && strcmp(t, "standard")) {
76 return apr_pstrcat(cmd->pool, "Invalid auth file type: ", t, NULL);
79 return ap_set_file_slot(cmd, offset, f);
82 static const command_rec authz_groupfile_cmds[] =
84 AP_INIT_TAKE12("AuthGroupFile", set_authz_groupfile_slot,
85 (void *)APR_OFFSETOF(authz_groupfile_config_rec, groupfile),
87 "text file containing group names and member user IDs"),
88 AP_INIT_FLAG("AuthzGroupFileAuthoritative", ap_set_flag_slot,
89 (void *)APR_OFFSETOF(authz_groupfile_config_rec,
92 "Set to 'Off' to allow access control to be passed along to "
93 "lower modules if the 'require group' fails. (default is "
98 module AP_MODULE_DECLARE_DATA authz_groupfile_module;
100 static apr_status_t groups_for_user(apr_pool_t *p, char *user, char *grpfile,
104 apr_table_t *grps = apr_table_make(p, 15);
106 char l[MAX_STRING_LEN];
107 const char *group_name, *ll, *w;
109 apr_size_t group_len;
111 if ((status = ap_pcfg_openfile(&f, p, grpfile)) != APR_SUCCESS) {
115 apr_pool_create(&sp, p);
117 while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
118 if ((l[0] == '#') || (!l[0])) {
124 group_name = ap_getword(sp, &ll, ':');
125 group_len = strlen(group_name);
127 while (group_len && apr_isspace(*(group_name + group_len - 1))) {
132 w = ap_getword_conf(sp, &ll);
133 if (!strcmp(w, user)) {
134 apr_table_setn(grps, apr_pstrmemdup(p, group_name, group_len),
141 apr_pool_destroy(sp);
149 static int check_user_access(request_rec *r)
151 authz_groupfile_config_rec *conf = ap_get_module_config(r->per_dir_config,
152 &authz_groupfile_module);
153 char *user = r->user;
154 int m = r->method_number;
155 int required_group = 0;
158 apr_table_t *grpstatus = NULL;
159 const apr_array_header_t *reqs_arr = ap_requires(r);
161 const char *filegroup = NULL;
164 /* If there is no group file - then we are not
165 * configured. So decline.
167 if (!(conf->groupfile)) {
172 return DECLINED; /* XXX change from legacy */
175 reqs = (require_line *)reqs_arr->elts;
177 for (x = 0; x < reqs_arr->nelts; x++) {
179 if (!(reqs[x].method_mask & (AP_METHOD_BIT << m))) {
183 t = reqs[x].requirement;
184 w = ap_getword_white(r->pool, &t);
186 /* needs mod_authz_owner to be present */
187 if (!strcmp(w, "file-group")) {
188 filegroup = apr_table_get(r->notes, AUTHZ_GROUP_NOTE);
191 /* mod_authz_owner is not present or not
192 * authoritative. We are just a helper module for testing
193 * group membership, so we don't care and decline.
199 if (!strcmp(w, "group") || filegroup) {
200 required_group = 1; /* remember the requirement */
202 /* create group table only if actually needed. */
206 status = groups_for_user(r->pool, user, conf->groupfile,
209 if (status != APR_SUCCESS) {
210 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
211 "Could not open group file: %s",
213 return HTTP_INTERNAL_SERVER_ERROR;
216 if (apr_table_elts(grpstatus)->nelts == 0) {
217 /* no groups available, so exit immediately */
218 reason = apr_psprintf(r->pool,
219 "user doesn't appear in group file "
220 "(%s).", conf->groupfile);
226 if (apr_table_get(grpstatus, filegroup)) {
230 if (conf->authoritative) {
231 reason = apr_psprintf(r->pool,
232 "file group '%s' does not match.",
237 /* now forget the filegroup, thus alternatively require'd
238 groups get a real chance */
243 w = ap_getword_conf(r->pool, &t);
244 if (apr_table_get(grpstatus, w)) {
252 /* No applicable "require group" for this method seen */
253 if (!required_group || !conf->authoritative) {
257 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
258 "Authorization of user %s to access %s failed, reason: %s",
260 reason ? reason : "user is not part of the "
261 "'require'ed group(s).");
263 ap_note_auth_failure(r);
264 return HTTP_UNAUTHORIZED;
267 static void register_hooks(apr_pool_t *p)
269 static const char * const aszPre[]={ "mod_authz_owner.c", NULL };
271 ap_hook_auth_checker(check_user_access, aszPre, NULL, APR_HOOK_MIDDLE);
274 module AP_MODULE_DECLARE_DATA authz_groupfile_module =
276 STANDARD20_MODULE_STUFF,
277 create_authz_groupfile_dir_config,/* dir config creater */
278 NULL, /* dir merger -- default is to override */
279 NULL, /* server config */
280 NULL, /* merge server config */
281 authz_groupfile_cmds, /* command apr_table_t */
282 register_hooks /* register hooks */