]> granicus.if.org Git - apache/blob - modules/aaa/mod_authz_groupfile.c
apply Apache License, Version 2.0
[apache] / modules / aaa / mod_authz_groupfile.c
1 /* Copyright 2000-2004 Apache Software Foundation
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 /* This module is triggered by an
17  *
18  *          AuthGroupFile standard /path/to/file
19  *
20  * and the presense of a
21  *
22  *         require group <list-of-groups>
23  * 
24  * In an applicable limit/directory block for that method.
25  *
26  * If there are no AuthGroupFile directives valid for 
27  * the request; we DECLINED.
28  * 
29  * If the AuthGroupFile is defined; but somehow not
30  * accessible: we SERVER_ERROR (was DECLINED).
31  *
32  * If there are no 'require ' directives defined for
33  * this request then we DECLINED (was OK).
34  * 
35  * If there are no 'require ' directives valid for
36  * this request method then we DECLINED. (was OK)
37  *
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  
41  * case we DECLINED.
42  *
43  */
44
45 #include "apr_strings.h"
46 #include "apr_lib.h" /* apr_isspace */
47
48 #include "ap_config.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
56 #include "mod_auth.h"
57
58 typedef struct {
59     char *groupfile;
60     int authoritative;
61 } authz_groupfile_config_rec;
62
63 static void *create_authz_groupfile_dir_config(apr_pool_t *p, char *d)
64 {
65     authz_groupfile_config_rec *conf = apr_palloc(p, sizeof(*conf));
66
67     conf->groupfile = NULL;    
68     conf->authoritative = 1; /* keep the fortress secure by default */
69     return conf;
70 }
71
72 static const char *set_authz_groupfile_slot(cmd_parms *cmd, void *offset, const char *f, 
73                                  const char *t)
74 {
75     if (t && strcmp(t, "standard")) {
76         return apr_pstrcat(cmd->pool, "Invalid auth file type: ", t, NULL);
77     }
78
79     return ap_set_file_slot(cmd, offset, f);
80 }
81
82 static const command_rec authz_groupfile_cmds[] =
83 {
84     AP_INIT_TAKE12("AuthGroupFile", set_authz_groupfile_slot,
85                    (void *)APR_OFFSETOF(authz_groupfile_config_rec, groupfile),
86                    OR_AUTHCFG,
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,
90                                       authoritative),
91                  OR_AUTHCFG,
92                  "Set to 'Off' to allow access control to be passed along to "
93                  "lower modules if the 'require group' fails. (default is "
94                  "On)."),
95     {NULL}
96 };
97
98 module AP_MODULE_DECLARE_DATA authz_groupfile_module;
99
100 static apr_status_t groups_for_user(apr_pool_t *p, char *user, char *grpfile,
101                                     apr_table_t ** out)
102 {
103     ap_configfile_t *f;
104     apr_table_t *grps = apr_table_make(p, 15);
105     apr_pool_t *sp;
106     char l[MAX_STRING_LEN];
107     const char *group_name, *ll, *w;
108     apr_status_t status;
109     apr_size_t group_len;
110
111     if ((status = ap_pcfg_openfile(&f, p, grpfile)) != APR_SUCCESS) {
112         return status ;
113     }
114
115     apr_pool_create(&sp, p);
116
117     while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
118         if ((l[0] == '#') || (!l[0])) {
119             continue;
120         }
121         ll = l;
122         apr_pool_clear(sp);
123
124         group_name = ap_getword(sp, &ll, ':');
125         group_len = strlen(group_name);
126
127         while (group_len && apr_isspace(*(group_name + group_len - 1))) {
128             --group_len;
129         }
130
131         while (ll[0]) {
132             w = ap_getword_conf(sp, &ll);
133             if (!strcmp(w, user)) {
134                 apr_table_setn(grps, apr_pstrmemdup(p, group_name, group_len),
135                                "in");
136                 break;
137             }
138         }
139     }
140     ap_cfg_closefile(f);
141     apr_pool_destroy(sp);
142
143     *out = grps;
144     return APR_SUCCESS;
145 }
146
147 /* Checking ID */
148
149 static int check_user_access(request_rec *r)
150 {
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;
156     register int x;
157     const char *t, *w;
158     apr_table_t *grpstatus = NULL;
159     const apr_array_header_t *reqs_arr = ap_requires(r);
160     require_line *reqs;
161     const char *filegroup = NULL;
162     char *reason = NULL;
163
164     /* If there is no group file - then we are not
165      * configured. So decline. 
166      */
167     if (!(conf->groupfile)) {
168         return DECLINED;
169     }
170
171     if (!reqs_arr) {
172         return DECLINED; /* XXX change from legacy */
173     } 
174     
175     reqs = (require_line *)reqs_arr->elts;
176
177     for (x = 0; x < reqs_arr->nelts; x++) {
178
179         if (!(reqs[x].method_mask & (AP_METHOD_BIT << m))) {
180             continue;
181         }
182
183         t = reqs[x].requirement;
184         w = ap_getword_white(r->pool, &t);
185
186         /* needs mod_authz_owner to be present */
187         if (!strcmp(w, "file-group")) {
188             filegroup = apr_table_get(r->notes, AUTHZ_GROUP_NOTE);
189             
190             if (!filegroup) {
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.
194                  */
195                 continue;
196             }
197         }
198
199         if (!strcmp(w, "group") || filegroup) {
200             required_group = 1; /* remember the requirement */
201
202             /* create group table only if actually needed. */
203             if (!grpstatus) {
204                 apr_status_t status;
205
206                 status = groups_for_user(r->pool, user, conf->groupfile,
207                                          &grpstatus);
208
209                 if (status != APR_SUCCESS) {
210                     ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
211                                   "Could not open group file: %s",
212                                   conf->groupfile);
213                     return HTTP_INTERNAL_SERVER_ERROR;
214                 }
215
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);
221                     break;
222                 }
223             }
224
225             if (filegroup) {
226                 if (apr_table_get(grpstatus, filegroup)) {
227                     return OK;
228                 }
229
230                 if (conf->authoritative) {
231                     reason = apr_psprintf(r->pool,
232                                           "file group '%s' does not match.",
233                                           filegroup);
234                     break;
235                 }
236
237                 /* now forget the filegroup, thus alternatively require'd
238                    groups get a real chance */
239                 filegroup = NULL;
240             }
241             else {
242                 while (t[0]) {
243                     w = ap_getword_conf(r->pool, &t);
244                     if (apr_table_get(grpstatus, w)) {
245                         return OK;
246                     }
247                 }
248             }
249         }
250     }
251
252     /* No applicable "require group" for this method seen */
253     if (!required_group || !conf->authoritative) {
254         return DECLINED;
255     }
256
257     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
258                   "Authorization of user %s to access %s failed, reason: %s",
259                   r->user, r->uri,
260                   reason ? reason : "user is not part of the "
261                                     "'require'ed group(s).");
262
263     ap_note_auth_failure(r);
264     return HTTP_UNAUTHORIZED;
265 }
266
267 static void register_hooks(apr_pool_t *p)
268 {
269     static const char * const aszPre[]={ "mod_authz_owner.c", NULL };
270
271     ap_hook_auth_checker(check_user_access, aszPre, NULL, APR_HOOK_MIDDLE);
272 }
273
274 module AP_MODULE_DECLARE_DATA authz_groupfile_module =
275 {
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 */
283 };