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
44 #include "apr_strings.h"
45 #include "apr_lib.h" /* apr_isspace */
47 #include "ap_config.h"
48 #include "ap_provider.h"
50 #include "http_config.h"
51 #include "http_core.h"
53 #include "http_protocol.h"
54 #include "http_request.h"
60 } authz_groupfile_config_rec
;
62 APR_DECLARE_OPTIONAL_FN(char*, authz_owner_get_file_group
, (request_rec
*r
));
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
;
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"),
91 module AP_MODULE_DECLARE_DATA authz_groupfile_module
;
93 static apr_status_t
groups_for_user(apr_pool_t
*p
, char *user
, char *grpfile
,
97 apr_table_t
*grps
= apr_table_make(p
, 15);
99 char l
[MAX_STRING_LEN
];
100 const char *group_name
, *ll
, *w
;
102 apr_size_t group_len
;
104 if ((status
= ap_pcfg_openfile(&f
, p
, grpfile
)) != APR_SUCCESS
) {
108 apr_pool_create(&sp
, p
);
110 while (!(ap_cfg_getline(l
, MAX_STRING_LEN
, f
))) {
111 if ((l
[0] == '#') || (!l
[0])) {
117 group_name
= ap_getword(sp
, &ll
, ':');
118 group_len
= strlen(group_name
);
120 while (group_len
&& apr_isspace(*(group_name
+ group_len
- 1))) {
125 w
= ap_getword_conf(sp
, &ll
);
126 if (!strcmp(w
, user
)) {
127 apr_table_setn(grps
, apr_pstrmemdup(p
, group_name
, group_len
),
134 apr_pool_destroy(sp
);
140 static authz_status
group_check_authorization(request_rec
*r
,
141 const char *require_args
)
143 authz_groupfile_config_rec
*conf
= ap_get_module_config(r
->per_dir_config
,
144 &authz_groupfile_module
);
145 char *user
= r
->user
;
147 apr_table_t
*grpstatus
= NULL
;
151 ap_log_rerror(APLOG_MARK
, APLOG_ERR
, 0, r
,
152 "access to %s failed, reason: no authenticated user", r
->uri
);
156 /* If there is no group file - then we are not
157 * configured. So decline.
159 if (!(conf
->groupfile
)) {
160 ap_log_rerror(APLOG_MARK
, APLOG_ERR
, 0, r
,
161 "No group file was specified in the configuration");
165 status
= groups_for_user(r
->pool
, user
, conf
->groupfile
,
168 if (status
!= APR_SUCCESS
) {
169 ap_log_rerror(APLOG_MARK
, APLOG_ERR
, status
, r
,
170 "Could not open group file: %s",
175 if (apr_table_elts(grpstatus
)->nelts
== 0) {
176 /* no groups available, so exit immediately */
177 ap_log_rerror(APLOG_MARK
, APLOG_ERR
, 0, r
,
178 "Authorization of user %s to access %s failed, reason: "
179 "user doesn't appear in group file (%s).",
180 r
->user
, r
->uri
, conf
->groupfile
);
185 while ((w
= ap_getword_conf(r
->pool
, &t
)) && w
[0]) {
186 if (apr_table_get(grpstatus
, w
)) {
187 return AUTHZ_GRANTED
;
191 ap_log_rerror(APLOG_MARK
, APLOG_ERR
, 0, r
,
192 "Authorization of user %s to access %s failed, reason: "
193 "user is not part of the 'require'ed group(s).",
199 APR_OPTIONAL_FN_TYPE(authz_owner_get_file_group
) *authz_owner_get_file_group
;
201 static authz_status
filegroup_check_authorization(request_rec
*r
,
202 const char *require_args
)
204 authz_groupfile_config_rec
*conf
= ap_get_module_config(r
->per_dir_config
,
205 &authz_groupfile_module
);
206 char *user
= r
->user
;
207 apr_table_t
*grpstatus
= NULL
;
209 const char *filegroup
= NULL
;
212 ap_log_rerror(APLOG_MARK
, APLOG_ERR
, 0, r
,
213 "access to %s failed, reason: no authenticated user", r
->uri
);
217 /* If there is no group file - then we are not
218 * configured. So decline.
220 if (!(conf
->groupfile
)) {
221 ap_log_rerror(APLOG_MARK
, APLOG_ERR
, 0, r
,
222 "No group file was specified in the configuration");
226 status
= groups_for_user(r
->pool
, user
, conf
->groupfile
,
228 if (status
!= APR_SUCCESS
) {
229 ap_log_rerror(APLOG_MARK
, APLOG_ERR
, status
, r
,
230 "Could not open group file: %s",
235 if (apr_table_elts(grpstatus
)->nelts
== 0) {
236 /* no groups available, so exit immediately */
237 ap_log_rerror(APLOG_MARK
, APLOG_ERR
, 0, r
,
238 "Authorization of user %s to access %s failed, reason: "
239 "user doesn't appear in group file (%s).",
240 r
->user
, r
->uri
, conf
->groupfile
);
244 filegroup
= authz_owner_get_file_group(r
);
247 if (apr_table_get(grpstatus
, filegroup
)) {
248 return AUTHZ_GRANTED
;
252 /* No need to emit a error log entry because the call
253 to authz_owner_get_file_group already did it
259 ap_log_rerror(APLOG_MARK
, APLOG_ERR
, 0, r
,
260 "Authorization of user %s to access %s failed, reason: "
261 "user is not part of the 'require'ed file group.",
267 static const authz_provider authz_group_provider
=
269 &group_check_authorization
,
272 static const authz_provider authz_filegroup_provider
=
274 &filegroup_check_authorization
,
277 static void register_hooks(apr_pool_t
*p
)
279 authz_owner_get_file_group
= APR_RETRIEVE_OPTIONAL_FN(authz_owner_get_file_group
);
281 ap_register_auth_provider(p
, AUTHZ_PROVIDER_GROUP
, "group",
282 AUTHZ_PROVIDER_VERSION
,
283 &authz_group_provider
,
284 AP_AUTH_INTERNAL_PER_CONF
);
285 ap_register_auth_provider(p
, AUTHZ_PROVIDER_GROUP
, "file-group",
286 AUTHZ_PROVIDER_VERSION
,
287 &authz_filegroup_provider
,
288 AP_AUTH_INTERNAL_PER_CONF
);
291 module AP_MODULE_DECLARE_DATA authz_groupfile_module
=
293 STANDARD20_MODULE_STUFF
,
294 create_authz_groupfile_dir_config
,/* dir config creater */
295 NULL
, /* dir merger -- default is to override */
296 NULL
, /* server config */
297 NULL
, /* merge server config */
298 authz_groupfile_cmds
, /* command apr_table_t */
299 register_hooks
/* register hooks */