]> granicus.if.org Git - apache/blob - modules/aaa/mod_authz_groupfile.c
Add lots of unique tags to error log messages
[apache] / modules / aaa / mod_authz_groupfile.c
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 presense 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
59 typedef struct {
60     char *groupfile;
61 } authz_groupfile_config_rec;
62
63 APR_DECLARE_OPTIONAL_FN(char*, authz_owner_get_file_group, (request_rec *r));
64
65 static void *create_authz_groupfile_dir_config(apr_pool_t *p, char *d)
66 {
67     authz_groupfile_config_rec *conf = apr_palloc(p, sizeof(*conf));
68
69     conf->groupfile = NULL;
70     return conf;
71 }
72
73 static const char *set_authz_groupfile_slot(cmd_parms *cmd, void *offset, const char *f,
74                                  const char *t)
75 {
76     if (t && strcmp(t, "standard")) {
77         return apr_pstrcat(cmd->pool, "Invalid auth file type: ", t, NULL);
78     }
79
80     return ap_set_file_slot(cmd, offset, f);
81 }
82
83 static const command_rec authz_groupfile_cmds[] =
84 {
85     AP_INIT_TAKE12("AuthGroupFile", set_authz_groupfile_slot,
86                    (void *)APR_OFFSETOF(authz_groupfile_config_rec, groupfile),
87                    OR_AUTHCFG,
88                    "text file containing group names and member user IDs"),
89     {NULL}
90 };
91
92 module AP_MODULE_DECLARE_DATA authz_groupfile_module;
93
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,
97                                     apr_table_t ** out)
98 {
99     ap_configfile_t *f;
100     apr_table_t *grps = apr_table_make(p, 15);
101     apr_pool_t *sp;
102     struct ap_varbuf vb;
103     const char *group_name, *ll, *w;
104     apr_status_t status;
105     apr_size_t group_len;
106
107     if ((status = ap_pcfg_openfile(&f, p, grpfile)) != APR_SUCCESS) {
108         return status ;
109     }
110
111     apr_pool_create(&sp, p);
112     ap_varbuf_init(p, &vb, VARBUF_INIT_LEN);
113
114     while (!(ap_varbuf_cfg_getline(&vb, f, VARBUF_MAX_LEN))) {
115         if ((vb.buf[0] == '#') || (!vb.buf[0])) {
116             continue;
117         }
118         ll = vb.buf;
119         apr_pool_clear(sp);
120
121         group_name = ap_getword(sp, &ll, ':');
122         group_len = strlen(group_name);
123
124         while (group_len && apr_isspace(*(group_name + group_len - 1))) {
125             --group_len;
126         }
127
128         while (ll[0]) {
129             w = ap_getword_conf(sp, &ll);
130             if (!strcmp(w, user)) {
131                 apr_table_setn(grps, apr_pstrmemdup(p, group_name, group_len),
132                                "in");
133                 break;
134             }
135         }
136     }
137     ap_cfg_closefile(f);
138     apr_pool_destroy(sp);
139     ap_varbuf_free(&vb);
140
141     *out = grps;
142     return APR_SUCCESS;
143 }
144
145 static authz_status group_check_authorization(request_rec *r,
146                                               const char *require_args,
147                                               const void *parsed_require_args)
148 {
149     authz_groupfile_config_rec *conf = ap_get_module_config(r->per_dir_config,
150             &authz_groupfile_module);
151     char *user = r->user;
152     const char *t, *w;
153     apr_table_t *grpstatus = NULL;
154     apr_status_t status;
155
156     if (!user) {
157         return AUTHZ_DENIED_NO_USER;
158     }
159
160     /* If there is no group file - then we are not
161      * configured. So decline.
162      */
163     if (!(conf->groupfile)) {
164         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01664)
165                         "No group file was specified in the configuration");
166         return AUTHZ_DENIED;
167     }
168
169     status = groups_for_user(r->pool, user, conf->groupfile,
170                                 &grpstatus);
171
172     if (status != APR_SUCCESS) {
173         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01665)
174                         "Could not open group file: %s",
175                         conf->groupfile);
176         return AUTHZ_DENIED;
177     }
178
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, APLOGNO(01666)
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);
185         return AUTHZ_DENIED;
186     }
187
188     t = require_args;
189     while ((w = ap_getword_conf(r->pool, &t)) && w[0]) {
190         if (apr_table_get(grpstatus, w)) {
191             return AUTHZ_GRANTED;
192         }
193     }
194
195     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01667)
196                     "Authorization of user %s to access %s failed, reason: "
197                     "user is not part of the 'require'ed group(s).",
198                     r->user, r->uri);
199
200     return AUTHZ_DENIED;
201 }
202
203 APR_OPTIONAL_FN_TYPE(authz_owner_get_file_group) *authz_owner_get_file_group;
204
205 static authz_status filegroup_check_authorization(request_rec *r,
206                                                   const char *require_args,
207                                                   const void *parsed_require_args)
208 {
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;
213     apr_status_t status;
214     const char *filegroup = NULL;
215
216     if (!user) {
217         return AUTHZ_DENIED_NO_USER;
218     }
219
220     /* If there is no group file - then we are not
221      * configured. So decline.
222      */
223     if (!(conf->groupfile)) {
224         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01668)
225                         "No group file was specified in the configuration");
226         return AUTHZ_DENIED;
227     }
228
229     status = groups_for_user(r->pool, user, conf->groupfile,
230                              &grpstatus);
231     if (status != APR_SUCCESS) {
232         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01669)
233                       "Could not open group file: %s",
234                       conf->groupfile);
235         return AUTHZ_DENIED;
236     }
237
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, APLOGNO(01670)
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);
244         return AUTHZ_DENIED;
245     }
246
247     filegroup = authz_owner_get_file_group(r);
248
249     if (filegroup) {
250         if (apr_table_get(grpstatus, filegroup)) {
251             return AUTHZ_GRANTED;
252         }
253     }
254     else {
255         /* No need to emit a error log entry because the call
256         to authz_owner_get_file_group already did it
257         for us.
258         */
259         return AUTHZ_DENIED;
260     }
261
262     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01671)
263                   "Authorization of user %s to access %s failed, reason: "
264                   "user is not part of the 'require'ed file group.",
265                   r->user, r->uri);
266
267     return AUTHZ_DENIED;
268 }
269
270 static const authz_provider authz_group_provider =
271 {
272     &group_check_authorization,
273     NULL,
274 };
275
276 static const authz_provider authz_filegroup_provider =
277 {
278     &filegroup_check_authorization,
279     NULL,
280 };
281
282 static void register_hooks(apr_pool_t *p)
283 {
284     authz_owner_get_file_group = APR_RETRIEVE_OPTIONAL_FN(authz_owner_get_file_group);
285
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);
294 }
295
296 AP_DECLARE_MODULE(authz_groupfile) =
297 {
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 */
305 };