Apache HTTPD
mod_authz_groupfile.c
Go to the documentation of this file.
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17/* This module is triggered by an
18 *
19 * AuthGroupFile standard /path/to/file
20 *
21 * and the presence of a
22 *
23 * require group <list-of-groups>
24 *
25 * In an applicable limit/directory block for that method.
26 *
27 * If there are no AuthGroupFile directives valid for
28 * the request; we DECLINED.
29 *
30 * If the AuthGroupFile is defined; but somehow not
31 * accessible: we SERVER_ERROR (was DECLINED).
32 *
33 * If there are no 'require ' directives defined for
34 * this request then we DECLINED (was OK).
35 *
36 * If there are no 'require ' directives valid for
37 * this request method then we DECLINED. (was OK)
38 *
39 * If there are any 'require group' blocks and we
40 * are not in any group - we HTTP_UNAUTHORIZE
41 *
42 */
43
44#include "apr_strings.h"
45#include "apr_lib.h" /* apr_isspace */
46
47#include "ap_config.h"
48#include "ap_provider.h"
49#include "httpd.h"
50#include "http_config.h"
51#include "http_core.h"
52#include "http_log.h"
53#include "http_protocol.h"
54#include "http_request.h"
55#include "util_varbuf.h"
56
57#include "mod_auth.h"
58#include "mod_authz_owner.h"
59
60typedef struct {
61 char *groupfile;
63
65{
66 authz_groupfile_config_rec *conf = apr_palloc(p, sizeof(*conf));
67
68 conf->groupfile = NULL;
69 return conf;
70}
71
73{
74 AP_INIT_TAKE1("AuthGroupFile", ap_set_file_slot,
75 (void *)APR_OFFSETOF(authz_groupfile_config_rec, groupfile),
77 "text file containing group names and member user IDs"),
78 {NULL}
79};
80
81module AP_MODULE_DECLARE_DATA authz_groupfile_module;
82
83#define VARBUF_INIT_LEN 512
84#define VARBUF_MAX_LEN (16*1024*1024)
85static apr_status_t groups_for_user(apr_pool_t *p, char *user, char *grpfile,
87{
91 struct ap_varbuf vb;
92 const char *group_name, *ll, *w;
95
96 if ((status = ap_pcfg_openfile(&f, p, grpfile)) != APR_SUCCESS) {
97 return status ;
98 }
99
101 apr_pool_tag(sp, "authz_groupfile (groups_for_user)");
102
104
105 while (!(ap_varbuf_cfg_getline(&vb, f, VARBUF_MAX_LEN))) {
106 if ((vb.buf[0] == '#') || (!vb.buf[0])) {
107 continue;
108 }
109 ll = vb.buf;
111
112 group_name = ap_getword(sp, &ll, ':');
114
115 while (group_len && apr_isspace(*(group_name + group_len - 1))) {
116 --group_len;
117 }
118
119 while (ll[0]) {
120 w = ap_getword_conf(sp, &ll);
121 if (!strcmp(w, user)) {
123 "in");
124 break;
125 }
126 }
127 }
130 ap_varbuf_free(&vb);
131
132 *out = grps;
133 return APR_SUCCESS;
134}
135
137 const char *require_args,
138 const void *parsed_require_args)
139{
141 &authz_groupfile_module);
142 char *user = r->user;
143
144 const char *err = NULL;
146 const char *require;
147
148 const char *t, *w;
151
152 if (!user) {
154 }
155
156 /* If there is no group file - then we are not
157 * configured. So decline.
158 */
159 if (!(conf->groupfile)) {
161 "No group file was specified in the configuration");
162 return AUTHZ_DENIED;
163 }
164
165 status = groups_for_user(r->pool, user, conf->groupfile,
166 &grpstatus);
167
168 if (status != APR_SUCCESS) {
170 "Could not open group file: %s",
171 conf->groupfile);
172 return AUTHZ_DENIED;
173 }
174
176 /* no groups available, so exit immediately */
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);
181 return AUTHZ_DENIED;
182 }
183
184 require = ap_expr_str_exec(r, expr, &err);
185 if (err) {
187 "authz_groupfile authorize: require group: Can't "
188 "evaluate require expression: %s", err);
189 return AUTHZ_DENIED;
190 }
191
192 t = require;
193 while ((w = ap_getword_conf(r->pool, &t)) && w[0]) {
194 if (apr_table_get(grpstatus, w)) {
195 return AUTHZ_GRANTED;
196 }
197 }
198
200 "Authorization of user %s to access %s failed, reason: "
201 "user is not part of the 'require'ed group(s).",
202 r->user, r->uri);
203
204 return AUTHZ_DENIED;
205}
206
208
210 const char *require_args,
211 const void *parsed_require_args)
212{
214 &authz_groupfile_module);
215 char *user = r->user;
218 const char *filegroup = NULL;
219
220 if (!user) {
222 }
223
224 /* If there is no group file - then we are not
225 * configured. So decline.
226 */
227 if (!(conf->groupfile)) {
229 "No group file was specified in the configuration");
230 return AUTHZ_DENIED;
231 }
232
233 status = groups_for_user(r->pool, user, conf->groupfile,
234 &grpstatus);
235 if (status != APR_SUCCESS) {
237 "Could not open group file: %s",
238 conf->groupfile);
239 return AUTHZ_DENIED;
240 }
241
243 /* no groups available, so exit immediately */
245 "Authorization of user %s to access %s failed, reason: "
246 "user doesn't appear in group file (%s).",
247 r->user, r->uri, conf->groupfile);
248 return AUTHZ_DENIED;
249 }
250
252
253 if (filegroup) {
255 return AUTHZ_GRANTED;
256 }
257 }
258 else {
259 /* No need to emit a error log entry because the call
260 to authz_owner_get_file_group already did it
261 for us.
262 */
263 return AUTHZ_DENIED;
264 }
265
267 "Authorization of user %s to access %s failed, reason: "
268 "user is not part of the 'require'ed file group.",
269 r->user, r->uri);
270
271 return AUTHZ_DENIED;
272}
273
274static const char *groupfile_parse_config(cmd_parms *cmd, const char *require_line,
275 const void **parsed_require_line)
276{
277 const char *expr_err = NULL;
278 ap_expr_info_t *expr;
279
281 &expr_err, NULL);
282
283 if (expr_err)
284 return apr_pstrcat(cmd->temp_pool,
285 "Cannot parse expression in require line: ",
286 expr_err, NULL);
287
288 *parsed_require_line = expr;
289
290 return NULL;
291}
292
298
304
305
310
323
325{
327 create_authz_groupfile_dir_config,/* dir config creater */
328 NULL, /* dir merger -- default is to override */
329 NULL, /* server config */
330 NULL, /* merge server config */
331 authz_groupfile_cmds, /* command apr_table_t */
332 register_hooks /* register hooks */
333};
Symbol export macros and hook functions.
Apache Provider API.
APR general purpose library routines.
apr_size_t const unsigned char unsigned int unsigned int d
Definition apr_siphash.h:72
APR Strings library.
#define AP_INIT_TAKE1(directive, func, mconfig, where, help)
#define ap_get_module_config(v, m)
int ap_cfg_closefile(ap_configfile_t *cfp)
Definition util.c:931
apr_status_t ap_pcfg_openfile(ap_configfile_t **ret_cfg, apr_pool_t *p, const char *name)
Definition util.c:957
#define AP_DECLARE_MODULE(foo)
request_rec * r
void ap_hook_optional_fn_retrieve(ap_HOOK_optional_fn_retrieve_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition config.c:195
const char * ap_set_file_slot(cmd_parms *cmd, void *struct_ptr, const char *arg)
Definition config.c:1535
#define APLOGNO(n)
Definition http_log.h:117
#define APLOG_INFO
Definition http_log.h:70
#define ap_log_rerror
Definition http_log.h:454
#define APLOG_ERR
Definition http_log.h:67
#define APLOG_MARK
Definition http_log.h:283
#define APLOG_DEBUG
Definition http_log.h:71
apr_status_t ap_register_auth_provider(apr_pool_t *pool, const char *provider_group, const char *provider_name, const char *provider_version, const void *provider, int type)
Definition request.c:2179
#define AP_AUTH_INTERNAL_PER_CONF
apr_status_t ap_varbuf_cfg_getline(struct ap_varbuf *vb, ap_configfile_t *cfp, apr_size_t max_len)
Definition util.c:1207
void ap_varbuf_free(struct ap_varbuf *vb)
Definition util.c:3081
void ap_varbuf_init(apr_pool_t *pool, struct ap_varbuf *vb, apr_size_t init_size)
Definition util.c:2959
apr_file_t * f
#define APR_HOOK_MIDDLE
Definition apr_hooks.h:303
#define APR_RETRIEVE_OPTIONAL_FN(name)
#define APR_OPTIONAL_FN_TYPE(name)
#define AP_EXPR_FLAG_STRING_RESULT
Definition ap_expr.h:68
#define ap_expr_parse_cmd(cmd, expr, flags, err, lookup_fn)
Definition ap_expr.h:340
const char * ap_expr_str_exec(request_rec *r, const ap_expr_info_t *expr, const char **err)
#define OR_AUTHCFG
#define STANDARD20_MODULE_STUFF
char * ap_getword(apr_pool_t *p, const char **line, char stop)
Definition util.c:723
char * ap_getword_conf(apr_pool_t *p, const char **line)
Definition util.c:833
apr_size_t size
#define apr_isspace(c)
Definition apr_lib.h:225
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
apr_interval_time_t t
#define apr_pool_create(newpool, parent)
Definition apr_pools.h:322
apr_int32_t apr_int32_t apr_int32_t err
apr_cmdtype_e cmd
int int status
Apache Configuration.
CORE HTTP Daemon.
Apache Logging library.
HTTP protocol handling.
Apache Request library.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
Authentication and Authorization Extension for Apache.
authz_status
Definition mod_auth.h:72
@ AUTHZ_DENIED
Definition mod_auth.h:73
@ AUTHZ_DENIED_NO_USER
Definition mod_auth.h:77
@ AUTHZ_GRANTED
Definition mod_auth.h:74
#define AUTHZ_PROVIDER_VERSION
Definition mod_auth.h:42
#define AUTHZ_PROVIDER_GROUP
Definition mod_auth.h:40
static apr_status_t groups_for_user(apr_pool_t *p, char *user, char *grpfile, apr_table_t **out)
static apr_OFN_authz_owner_get_file_group_t * authz_owner_get_file_group
static const command_rec authz_groupfile_cmds[]
#define VARBUF_INIT_LEN
#define VARBUF_MAX_LEN
static void authz_groupfile_getfns(void)
static void register_hooks(apr_pool_t *p)
static void * create_authz_groupfile_dir_config(apr_pool_t *p, char *d)
static const authz_provider authz_group_provider
static authz_status group_check_authorization(request_rec *r, const char *require_args, const void *parsed_require_args)
static const authz_provider authz_filegroup_provider
static authz_status filegroup_check_authorization(request_rec *r, const char *require_args, const void *parsed_require_args)
static const char * groupfile_parse_config(cmd_parms *cmd, const char *require_line, const void **parsed_require_line)
static apr_file_t * out
Definition mod_info.c:85
return NULL
Definition mod_so.c:359
apr_size_t strlen
Definition util_varbuf.h:59
char * buf
Definition util_varbuf.h:50
A structure that represents the current request.
Definition httpd.h:845
char * user
Definition httpd.h:1005
char * uri
Definition httpd.h:1016
apr_pool_t * pool
Definition httpd.h:847
struct ap_conf_vector_t * per_dir_config
Definition httpd.h:1047
A structure to keep track of authorization requirements.
Definition http_core.h:316
Apache resizable variable length buffer library.