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

« back to all changes in this revision

Viewing changes to modules/aaa/mod_authz_dbm.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
#define APR_WANT_STRFUNC
 
18
#include "apr_want.h"
 
19
#include "apr_strings.h"
 
20
#include "apr_dbm.h"
 
21
#include "apr_md5.h"
 
22
 
 
23
#include "httpd.h"
 
24
#include "http_config.h"
 
25
#include "http_core.h"
 
26
#include "http_log.h"
 
27
#include "http_protocol.h"
 
28
#include "http_request.h"   /* for ap_hook_(check_user_id | auth_checker)*/
 
29
 
 
30
#include "mod_auth.h"
 
31
 
 
32
typedef struct {
 
33
    char *grpfile;
 
34
    char *dbmtype;
 
35
    int authoritative;
 
36
} authz_dbm_config_rec;
 
37
 
 
38
/* This should go into APR; perhaps with some nice
 
39
 * caching/locking/flocking of the open dbm file.
 
40
 */
 
41
static char *get_dbm_entry_as_str(apr_pool_t *pool, apr_dbm_t *f, char *key)
 
42
{
 
43
    apr_datum_t d, q;
 
44
    q.dptr = key;
 
45
 
 
46
#ifndef NETSCAPE_DBM_COMPAT
 
47
    q.dsize = strlen(q.dptr);
 
48
#else
 
49
    q.dsize = strlen(q.dptr) + 1;
 
50
#endif
 
51
 
 
52
    if (apr_dbm_fetch(f, q, &d) == APR_SUCCESS && d.dptr) {
 
53
        return apr_pstrmemdup(pool, d.dptr, d.dsize);
 
54
    }
 
55
 
 
56
    return NULL;
 
57
}
 
58
 
 
59
static void *create_authz_dbm_dir_config(apr_pool_t *p, char *d)
 
60
{
 
61
    authz_dbm_config_rec *conf = apr_palloc(p, sizeof(*conf));
 
62
 
 
63
    conf->grpfile = NULL;
 
64
    conf->dbmtype = "default";
 
65
    conf->authoritative = 1;  /* fortress is secure by default */
 
66
 
 
67
    return conf;
 
68
}
 
69
 
 
70
static const command_rec authz_dbm_cmds[] =
 
71
{
 
72
    AP_INIT_TAKE1("AuthDBMGroupFile", ap_set_file_slot,
 
73
     (void *)APR_OFFSETOF(authz_dbm_config_rec, grpfile),
 
74
     OR_AUTHCFG, "database file containing group names and member user IDs"),
 
75
    AP_INIT_TAKE1("AuthzDBMType", ap_set_string_slot,
 
76
     (void *)APR_OFFSETOF(authz_dbm_config_rec, dbmtype),
 
77
     OR_AUTHCFG, "what type of DBM file the group file is"),
 
78
    AP_INIT_FLAG("AuthzDBMAuthoritative", ap_set_flag_slot,
 
79
     (void *)APR_OFFSETOF(authz_dbm_config_rec, authoritative),
 
80
     OR_AUTHCFG, "Set to 'Off' to allow access control to be passed along to "
 
81
     "lower modules, if the group required is not found or empty, or the user "
 
82
     " is not in the required groups. (default is On.)"),
 
83
    {NULL}
 
84
};
 
85
 
 
86
module AP_MODULE_DECLARE_DATA authz_dbm_module;
 
87
 
 
88
/* We do something strange with the group file.  If the group file
 
89
 * contains any : we assume the format is
 
90
 *      key=username value=":"groupname [":"anything here is ignored]
 
91
 * otherwise we now (0.8.14+) assume that the format is
 
92
 *      key=username value=groupname
 
93
 * The first allows the password and group files to be the same
 
94
 * physical DBM file;   key=username value=password":"groupname[":"anything]
 
95
 *
 
96
 * mark@telescope.org, 22Sep95
 
97
 */
 
98
 
 
99
static apr_status_t get_dbm_grp(request_rec *r, char *key1, char *key2,
 
100
                                char *dbmgrpfile, char *dbtype,
 
101
                                const char ** out)
 
102
{
 
103
    char *grp_colon, *val;
 
104
    apr_status_t retval;
 
105
    apr_dbm_t *f;
 
106
 
 
107
    retval = apr_dbm_open_ex(&f, dbtype, dbmgrpfile, APR_DBM_READONLY,
 
108
                             APR_OS_DEFAULT, r->pool);
 
109
 
 
110
    if (retval != APR_SUCCESS) {
 
111
        return retval;
 
112
    }
 
113
 
 
114
    /* Try key2 only if key1 failed */
 
115
    if (!(val = get_dbm_entry_as_str(r->pool, f, key1))) {
 
116
        val = get_dbm_entry_as_str(r->pool, f, key2);
 
117
    }
 
118
 
 
119
    apr_dbm_close(f);
 
120
 
 
121
    if (val && (grp_colon = ap_strchr(val, ':')) != NULL) {
 
122
        char *grp_colon2 = ap_strchr(++grp_colon, ':');
 
123
 
 
124
        if (grp_colon2) {
 
125
            *grp_colon2 = '\0';
 
126
        }
 
127
        *out = grp_colon;
 
128
    }
 
129
    else {
 
130
        *out = val;
 
131
    }
 
132
 
 
133
    return retval;
 
134
}
 
135
 
 
136
/* Checking ID */
 
137
static int dbm_check_auth(request_rec *r)
 
138
{
 
139
    authz_dbm_config_rec *conf = ap_get_module_config(r->per_dir_config,
 
140
                                                      &authz_dbm_module);
 
141
    char *user = r->user;
 
142
    int m = r->method_number;
 
143
    const apr_array_header_t *reqs_arr = ap_requires(r);
 
144
    require_line *reqs = reqs_arr ? (require_line *) reqs_arr->elts : NULL;
 
145
    register int x;
 
146
    const char *t;
 
147
    char *w;
 
148
    int required_group = 0;
 
149
    const char *filegroup = NULL;
 
150
    const char *orig_groups = NULL;
 
151
    char *reason = NULL;
 
152
 
 
153
    if (!conf->grpfile) {
 
154
        return DECLINED;
 
155
    }
 
156
 
 
157
    if (!reqs_arr) {
 
158
        return DECLINED;
 
159
    }
 
160
 
 
161
    for (x = 0; x < reqs_arr->nelts; x++) {
 
162
 
 
163
        if (!(reqs[x].method_mask & (AP_METHOD_BIT << m))) {
 
164
            continue;
 
165
        }
 
166
 
 
167
        t = reqs[x].requirement;
 
168
        w = ap_getword_white(r->pool, &t);
 
169
 
 
170
        if (!strcasecmp(w, "file-group")) {
 
171
            filegroup = apr_table_get(r->notes, AUTHZ_GROUP_NOTE);
 
172
 
 
173
            if (!filegroup) {
 
174
                /* mod_authz_owner is not present or not
 
175
                 * authoritative. We are just a helper module for testing
 
176
                 * group membership, so we don't care and decline.
 
177
                 */
 
178
                continue;
 
179
            }
 
180
        }
 
181
 
 
182
        if (!strcasecmp(w, "group") || filegroup) {
 
183
            const char *realm = ap_auth_name(r);
 
184
            const char *groups;
 
185
            char *v;
 
186
 
 
187
            /* remember that actually a group is required */
 
188
            required_group = 1;
 
189
 
 
190
            /* fetch group data from dbm file only once. */
 
191
            if (!orig_groups) {
 
192
                apr_status_t status;
 
193
 
 
194
                status = get_dbm_grp(r, apr_pstrcat(r->pool, user, ":", realm,
 
195
                                                    NULL),
 
196
                                     user,
 
197
                                     conf->grpfile, conf->dbmtype, &groups);
 
198
 
 
199
                if (status != APR_SUCCESS) {
 
200
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
 
201
                                  "could not open dbm (type %s) group access "
 
202
                                  "file: %s", conf->dbmtype, conf->grpfile);
 
203
                    return HTTP_INTERNAL_SERVER_ERROR;
 
204
                }
 
205
 
 
206
                if (groups == NULL) {
 
207
                    /* no groups available, so exit immediately */
 
208
                    reason = apr_psprintf(r->pool,
 
209
                                          "user doesn't appear in DBM group "
 
210
                                          "file (%s).", conf->grpfile);
 
211
                    break;
 
212
                }
 
213
 
 
214
                orig_groups = groups;
 
215
            }
 
216
 
 
217
            if (filegroup) {
 
218
                groups = orig_groups;
 
219
                while (groups[0]) {
 
220
                    v = ap_getword(r->pool, &groups, ',');
 
221
                    if (!strcmp(v, filegroup)) {
 
222
                        return OK;
 
223
                    }
 
224
                }
 
225
 
 
226
                if (conf->authoritative) {
 
227
                    reason = apr_psprintf(r->pool,
 
228
                                          "file group '%s' does not match.",
 
229
                                          filegroup);
 
230
                    break;
 
231
                }
 
232
 
 
233
                /* now forget the filegroup, thus alternatively require'd
 
234
                   groups get a real chance */
 
235
                filegroup = NULL;
 
236
            }
 
237
            else {
 
238
                while (t[0]) {
 
239
                    w = ap_getword_white(r->pool, &t);
 
240
                    groups = orig_groups;
 
241
                    while (groups[0]) {
 
242
                        v = ap_getword(r->pool, &groups, ',');
 
243
                        if (!strcmp(v, w)) {
 
244
                            return OK;
 
245
                        }
 
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(dbm_check_auth, aszPre, NULL, APR_HOOK_MIDDLE);
 
272
}
 
273
 
 
274
module AP_MODULE_DECLARE_DATA authz_dbm_module =
 
275
{
 
276
    STANDARD20_MODULE_STUFF,
 
277
    create_authz_dbm_dir_config, /* dir config creater */
 
278
    NULL,                        /* dir merger --- default is to override */
 
279
    NULL,                        /* server config */
 
280
    NULL,                        /* merge server config */
 
281
    authz_dbm_cmds,              /* command apr_table_t */
 
282
    register_hooks               /* register hooks */
 
283
};