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"
55 #include "util_varbuf.h"
61 } authz_groupfile_config_rec;
63 APR_DECLARE_OPTIONAL_FN(char*, authz_owner_get_file_group, (request_rec *r));
65 static void *create_authz_groupfile_dir_config(apr_pool_t *p, char *d)
67 authz_groupfile_config_rec *conf = apr_palloc(p, sizeof(*conf));
69 conf->groupfile = NULL;
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"),
92 module AP_MODULE_DECLARE_DATA authz_groupfile_module;
94 #define VARBUF_INIT_LEN 512
95 #define VARBUF_MAX_LEN (16*1024*1024)
96 static apr_status_t groups_for_user(apr_pool_t *p, char *user, char *grpfile,
100 apr_table_t *grps = apr_table_make(p, 15);
103 const char *group_name, *ll, *w;
105 apr_size_t group_len;
107 if ((status = ap_pcfg_openfile(&f, p, grpfile)) != APR_SUCCESS) {
111 apr_pool_create(&sp, p);
112 ap_varbuf_init(p, &vb, VARBUF_INIT_LEN);
114 while (!(ap_varbuf_cfg_getline(&vb, f, VARBUF_MAX_LEN))) {
115 if ((vb.buf[0] == '#') || (!vb.buf[0])) {
121 group_name = ap_getword(sp, &ll, ':');
122 group_len = strlen(group_name);
124 while (group_len && apr_isspace(*(group_name + group_len - 1))) {
129 w = ap_getword_conf(sp, &ll);
130 if (!strcmp(w, user)) {
131 apr_table_setn(grps, apr_pstrmemdup(p, group_name, group_len),
138 apr_pool_destroy(sp);
145 static authz_status group_check_authorization(request_rec *r,
146 const char *require_args,
147 const void *parsed_require_args)
149 authz_groupfile_config_rec *conf = ap_get_module_config(r->per_dir_config,
150 &authz_groupfile_module);
151 char *user = r->user;
153 apr_table_t *grpstatus = NULL;
157 return AUTHZ_DENIED_NO_USER;
160 /* If there is no group file - then we are not
161 * configured. So decline.
163 if (!(conf->groupfile)) {
164 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
165 "No group file was specified in the configuration");
169 status = groups_for_user(r->pool, user, conf->groupfile,
172 if (status != APR_SUCCESS) {
173 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
174 "Could not open group file: %s",
179 if (apr_table_elts(grpstatus)->nelts == 0) {
180 /* no groups available, so exit immediately */
181 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
182 "Authorization of user %s to access %s failed, reason: "
183 "user doesn't appear in group file (%s).",
184 r->user, r->uri, conf->groupfile);
189 while ((w = ap_getword_conf(r->pool, &t)) && w[0]) {
190 if (apr_table_get(grpstatus, w)) {
191 return AUTHZ_GRANTED;
195 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
196 "Authorization of user %s to access %s failed, reason: "
197 "user is not part of the 'require'ed group(s).",
203 APR_OPTIONAL_FN_TYPE(authz_owner_get_file_group) *authz_owner_get_file_group;
205 static authz_status filegroup_check_authorization(request_rec *r,
206 const char *require_args,
207 const void *parsed_require_args)
209 authz_groupfile_config_rec *conf = ap_get_module_config(r->per_dir_config,
210 &authz_groupfile_module);
211 char *user = r->user;
212 apr_table_t *grpstatus = NULL;
214 const char *filegroup = NULL;
217 return AUTHZ_DENIED_NO_USER;
220 /* If there is no group file - then we are not
221 * configured. So decline.
223 if (!(conf->groupfile)) {
224 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
225 "No group file was specified in the configuration");
229 status = groups_for_user(r->pool, user, conf->groupfile,
231 if (status != APR_SUCCESS) {
232 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
233 "Could not open group file: %s",
238 if (apr_table_elts(grpstatus)->nelts == 0) {
239 /* no groups available, so exit immediately */
240 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
241 "Authorization of user %s to access %s failed, reason: "
242 "user doesn't appear in group file (%s).",
243 r->user, r->uri, conf->groupfile);
247 filegroup = authz_owner_get_file_group(r);
250 if (apr_table_get(grpstatus, filegroup)) {
251 return AUTHZ_GRANTED;
255 /* No need to emit a error log entry because the call
256 to authz_owner_get_file_group already did it
262 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
263 "Authorization of user %s to access %s failed, reason: "
264 "user is not part of the 'require'ed file group.",
270 static const authz_provider authz_group_provider =
272 &group_check_authorization,
276 static const authz_provider authz_filegroup_provider =
278 &filegroup_check_authorization,
282 static void register_hooks(apr_pool_t *p)
284 authz_owner_get_file_group = APR_RETRIEVE_OPTIONAL_FN(authz_owner_get_file_group);
286 ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "group",
287 AUTHZ_PROVIDER_VERSION,
288 &authz_group_provider,
289 AP_AUTH_INTERNAL_PER_CONF);
290 ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "file-group",
291 AUTHZ_PROVIDER_VERSION,
292 &authz_filegroup_provider,
293 AP_AUTH_INTERNAL_PER_CONF);
296 AP_DECLARE_MODULE(authz_groupfile) =
298 STANDARD20_MODULE_STUFF,
299 create_authz_groupfile_dir_config,/* dir config creater */
300 NULL, /* dir merger -- default is to override */
301 NULL, /* server config */
302 NULL, /* merge server config */
303 authz_groupfile_cmds, /* command apr_table_t */
304 register_hooks /* register hooks */