~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to modules/aaa/mod_authz_groupfile.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * unless we are non-authoritative; in which
 
42
 * case we DECLINED.
 
43
 *
 
44
 */
 
45
 
 
46
#include "apr_strings.h"
 
47
#include "apr_lib.h" /* apr_isspace */
 
48
 
 
49
#include "ap_config.h"
 
50
#include "httpd.h"
 
51
#include "http_config.h"
 
52
#include "http_core.h"
 
53
#include "http_log.h"
 
54
#include "http_protocol.h"
 
55
#include "http_request.h"
 
56
 
 
57
#include "mod_auth.h"
 
58
 
 
59
typedef struct {
 
60
    char *groupfile;
 
61
    int authoritative;
 
62
} authz_groupfile_config_rec;
 
63
 
 
64
static void *create_authz_groupfile_dir_config(apr_pool_t *p, char *d)
 
65
{
 
66
    authz_groupfile_config_rec *conf = apr_palloc(p, sizeof(*conf));
 
67
 
 
68
    conf->groupfile = NULL;
 
69
    conf->authoritative = 1; /* keep the fortress secure by default */
 
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
    AP_INIT_FLAG("AuthzGroupFileAuthoritative", ap_set_flag_slot,
 
90
                 (void *)APR_OFFSETOF(authz_groupfile_config_rec,
 
91
                                      authoritative),
 
92
                 OR_AUTHCFG,
 
93
                 "Set to 'Off' to allow access control to be passed along to "
 
94
                 "lower modules if the 'require group' fails. (default is "
 
95
                 "On)."),
 
96
    {NULL}
 
97
};
 
98
 
 
99
module AP_MODULE_DECLARE_DATA authz_groupfile_module;
 
100
 
 
101
static apr_status_t groups_for_user(apr_pool_t *p, char *user, char *grpfile,
 
102
                                    apr_table_t ** out)
 
103
{
 
104
    ap_configfile_t *f;
 
105
    apr_table_t *grps = apr_table_make(p, 15);
 
106
    apr_pool_t *sp;
 
107
    char l[MAX_STRING_LEN];
 
108
    const char *group_name, *ll, *w;
 
109
    apr_status_t status;
 
110
    apr_size_t group_len;
 
111
 
 
112
    if ((status = ap_pcfg_openfile(&f, p, grpfile)) != APR_SUCCESS) {
 
113
        return status ;
 
114
    }
 
115
 
 
116
    apr_pool_create(&sp, p);
 
117
 
 
118
    while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
 
119
        if ((l[0] == '#') || (!l[0])) {
 
120
            continue;
 
121
        }
 
122
        ll = l;
 
123
        apr_pool_clear(sp);
 
124
 
 
125
        group_name = ap_getword(sp, &ll, ':');
 
126
        group_len = strlen(group_name);
 
127
 
 
128
        while (group_len && apr_isspace(*(group_name + group_len - 1))) {
 
129
            --group_len;
 
130
        }
 
131
 
 
132
        while (ll[0]) {
 
133
            w = ap_getword_conf(sp, &ll);
 
134
            if (!strcmp(w, user)) {
 
135
                apr_table_setn(grps, apr_pstrmemdup(p, group_name, group_len),
 
136
                               "in");
 
137
                break;
 
138
            }
 
139
        }
 
140
    }
 
141
    ap_cfg_closefile(f);
 
142
    apr_pool_destroy(sp);
 
143
 
 
144
    *out = grps;
 
145
    return APR_SUCCESS;
 
146
}
 
147
 
 
148
/* Checking ID */
 
149
 
 
150
static int check_user_access(request_rec *r)
 
151
{
 
152
    authz_groupfile_config_rec *conf = ap_get_module_config(r->per_dir_config,
 
153
                                                      &authz_groupfile_module);
 
154
    char *user = r->user;
 
155
    int m = r->method_number;
 
156
    int required_group = 0;
 
157
    register int x;
 
158
    const char *t, *w;
 
159
    apr_table_t *grpstatus = NULL;
 
160
    const apr_array_header_t *reqs_arr = ap_requires(r);
 
161
    require_line *reqs;
 
162
    const char *filegroup = NULL;
 
163
    char *reason = NULL;
 
164
 
 
165
    /* If there is no group file - then we are not
 
166
     * configured. So decline.
 
167
     */
 
168
    if (!(conf->groupfile)) {
 
169
        return DECLINED;
 
170
    }
 
171
 
 
172
    if (!reqs_arr) {
 
173
        return DECLINED; /* XXX change from legacy */
 
174
    }
 
175
 
 
176
    reqs = (require_line *)reqs_arr->elts;
 
177
 
 
178
    for (x = 0; x < reqs_arr->nelts; x++) {
 
179
 
 
180
        if (!(reqs[x].method_mask & (AP_METHOD_BIT << m))) {
 
181
            continue;
 
182
        }
 
183
 
 
184
        t = reqs[x].requirement;
 
185
        w = ap_getword_white(r->pool, &t);
 
186
 
 
187
        /* needs mod_authz_owner to be present */
 
188
        if (!strcasecmp(w, "file-group")) {
 
189
            filegroup = apr_table_get(r->notes, AUTHZ_GROUP_NOTE);
 
190
 
 
191
            if (!filegroup) {
 
192
                /* mod_authz_owner is not present or not
 
193
                 * authoritative. We are just a helper module for testing
 
194
                 * group membership, so we don't care and decline.
 
195
                 */
 
196
                continue;
 
197
            }
 
198
        }
 
199
 
 
200
        if (!strcasecmp(w, "group") || filegroup) {
 
201
            required_group = 1; /* remember the requirement */
 
202
 
 
203
            /* create group table only if actually needed. */
 
204
            if (!grpstatus) {
 
205
                apr_status_t status;
 
206
 
 
207
                status = groups_for_user(r->pool, user, conf->groupfile,
 
208
                                         &grpstatus);
 
209
 
 
210
                if (status != APR_SUCCESS) {
 
211
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
 
212
                                  "Could not open group file: %s",
 
213
                                  conf->groupfile);
 
214
                    return HTTP_INTERNAL_SERVER_ERROR;
 
215
                }
 
216
 
 
217
                if (apr_table_elts(grpstatus)->nelts == 0) {
 
218
                    /* no groups available, so exit immediately */
 
219
                    reason = apr_psprintf(r->pool,
 
220
                                          "user doesn't appear in group file "
 
221
                                          "(%s).", conf->groupfile);
 
222
                    break;
 
223
                }
 
224
            }
 
225
 
 
226
            if (filegroup) {
 
227
                if (apr_table_get(grpstatus, filegroup)) {
 
228
                    return OK;
 
229
                }
 
230
 
 
231
                if (conf->authoritative) {
 
232
                    reason = apr_psprintf(r->pool,
 
233
                                          "file group '%s' does not match.",
 
234
                                          filegroup);
 
235
                    break;
 
236
                }
 
237
 
 
238
                /* now forget the filegroup, thus alternatively require'd
 
239
                   groups get a real chance */
 
240
                filegroup = NULL;
 
241
            }
 
242
            else {
 
243
                while (t[0]) {
 
244
                    w = ap_getword_conf(r->pool, &t);
 
245
                    if (apr_table_get(grpstatus, w)) {
 
246
                        return OK;
 
247
                    }
 
248
                }
 
249
            }
 
250
        }
 
251
    }
 
252
 
 
253
    /* No applicable "require group" for this method seen */
 
254
    if (!required_group || !conf->authoritative) {
 
255
        return DECLINED;
 
256
    }
 
257
 
 
258
    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
 
259
                  "Authorization of user %s to access %s failed, reason: %s",
 
260
                  r->user, r->uri,
 
261
                  reason ? reason : "user is not part of the "
 
262
                                    "'require'ed group(s).");
 
263
 
 
264
    ap_note_auth_failure(r);
 
265
    return HTTP_UNAUTHORIZED;
 
266
}
 
267
 
 
268
static void register_hooks(apr_pool_t *p)
 
269
{
 
270
    static const char * const aszPre[]={ "mod_authz_owner.c", NULL };
 
271
 
 
272
    ap_hook_auth_checker(check_user_access, aszPre, NULL, APR_HOOK_MIDDLE);
 
273
}
 
274
 
 
275
module AP_MODULE_DECLARE_DATA authz_groupfile_module =
 
276
{
 
277
    STANDARD20_MODULE_STUFF,
 
278
    create_authz_groupfile_dir_config,/* dir config creater */
 
279
    NULL,                             /* dir merger -- default is to override */
 
280
    NULL,                             /* server config */
 
281
    NULL,                             /* merge server config */
 
282
    authz_groupfile_cmds,             /* command apr_table_t */
 
283
    register_hooks                    /* register hooks */
 
284
};