switch to a 60 bit hash
[httpd-crcsyncproxy.git] / modules / aaa / mod_authz_dbm.c
blob9979e1e9cadeb4efe8082d50659e76e262e8638f
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
23 #include "httpd.h"
24 #include "http_config.h"
25 #include "ap_provider.h"
26 #include "http_core.h"
27 #include "http_log.h"
28 #include "http_protocol.h"
29 #include "http_request.h" /* for ap_hook_(check_user_id | auth_checker)*/
31 #include "mod_auth.h"
33 typedef struct {
34 char *grpfile;
35 char *dbmtype;
36 } authz_dbm_config_rec;
38 APR_DECLARE_OPTIONAL_FN(char*, authz_owner_get_file_group, (request_rec *r));
41 /* This should go into APR; perhaps with some nice
42 * caching/locking/flocking of the open dbm file.
44 static char *get_dbm_entry_as_str(apr_pool_t *pool, apr_dbm_t *f, char *key)
46 apr_datum_t d, q;
47 q.dptr = key;
49 #ifndef NETSCAPE_DBM_COMPAT
50 q.dsize = strlen(q.dptr);
51 #else
52 q.dsize = strlen(q.dptr) + 1;
53 #endif
55 if (apr_dbm_fetch(f, q, &d) == APR_SUCCESS && d.dptr) {
56 return apr_pstrmemdup(pool, d.dptr, d.dsize);
59 return NULL;
62 static void *create_authz_dbm_dir_config(apr_pool_t *p, char *d)
64 authz_dbm_config_rec *conf = apr_palloc(p, sizeof(*conf));
66 conf->grpfile = NULL;
67 conf->dbmtype = "default";
69 return conf;
72 static const command_rec authz_dbm_cmds[] =
74 AP_INIT_TAKE1("AuthDBMGroupFile", ap_set_file_slot,
75 (void *)APR_OFFSETOF(authz_dbm_config_rec, grpfile),
76 OR_AUTHCFG, "database file containing group names and member user IDs"),
77 AP_INIT_TAKE1("AuthzDBMType", ap_set_string_slot,
78 (void *)APR_OFFSETOF(authz_dbm_config_rec, dbmtype),
79 OR_AUTHCFG, "what type of DBM file the group file is"),
80 {NULL}
83 module AP_MODULE_DECLARE_DATA authz_dbm_module;
85 /* We do something strange with the group file. If the group file
86 * contains any : we assume the format is
87 * key=username value=":"groupname [":"anything here is ignored]
88 * otherwise we now (0.8.14+) assume that the format is
89 * key=username value=groupname
90 * The first allows the password and group files to be the same
91 * physical DBM file; key=username value=password":"groupname[":"anything]
93 * mark@telescope.org, 22Sep95
96 static apr_status_t get_dbm_grp(request_rec *r, char *key1, char *key2,
97 char *dbmgrpfile, char *dbtype,
98 const char ** out)
100 char *grp_colon, *val;
101 apr_status_t retval;
102 apr_dbm_t *f;
104 retval = apr_dbm_open_ex(&f, dbtype, dbmgrpfile, APR_DBM_READONLY,
105 APR_OS_DEFAULT, r->pool);
107 if (retval != APR_SUCCESS) {
108 return retval;
111 /* Try key2 only if key1 failed */
112 if (!(val = get_dbm_entry_as_str(r->pool, f, key1))) {
113 val = get_dbm_entry_as_str(r->pool, f, key2);
116 apr_dbm_close(f);
118 if (val && (grp_colon = ap_strchr(val, ':')) != NULL) {
119 char *grp_colon2 = ap_strchr(++grp_colon, ':');
121 if (grp_colon2) {
122 *grp_colon2 = '\0';
124 *out = grp_colon;
126 else {
127 *out = val;
130 return retval;
133 static authz_status dbmgroup_check_authorization(request_rec *r,
134 const char *require_args)
136 authz_dbm_config_rec *conf = ap_get_module_config(r->per_dir_config,
137 &authz_dbm_module);
138 char *user = r->user;
139 const char *t;
140 char *w;
141 const char *orig_groups = NULL;
142 const char *realm = ap_auth_name(r);
143 const char *groups;
144 char *v;
146 if (!user) {
147 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
148 "access to %s failed, reason: no authenticated user", r->uri);
149 return AUTHZ_DENIED;
152 if (!conf->grpfile) {
153 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
154 "No group file was specified in the configuration");
155 return AUTHZ_DENIED;
158 /* fetch group data from dbm file only once. */
159 if (!orig_groups) {
160 apr_status_t status;
162 status = get_dbm_grp(r, apr_pstrcat(r->pool, user, ":", realm, NULL),
163 user, conf->grpfile, conf->dbmtype, &groups);
165 if (status != APR_SUCCESS) {
166 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
167 "could not open dbm (type %s) group access "
168 "file: %s", conf->dbmtype, conf->grpfile);
169 return AUTHZ_GENERAL_ERROR;
172 if (groups == NULL) {
173 /* no groups available, so exit immediately */
174 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
175 "Authorization of user %s to access %s failed, reason: "
176 "user doesn't appear in DBM group file (%s).",
177 r->user, r->uri, conf->grpfile);
178 return AUTHZ_DENIED;
181 orig_groups = groups;
184 t = require_args;
185 while ((w = ap_getword_white(r->pool, &t)) && w[0]) {
186 groups = orig_groups;
187 while (groups[0]) {
188 v = ap_getword(r->pool, &groups, ',');
189 if (!strcmp(v, w)) {
190 return AUTHZ_GRANTED;
195 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
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);
200 return AUTHZ_DENIED;
203 APR_OPTIONAL_FN_TYPE(authz_owner_get_file_group) *authz_owner_get_file_group;
205 static authz_status dbmfilegroup_check_authorization(request_rec *r,
206 const char *require_args)
208 authz_dbm_config_rec *conf = ap_get_module_config(r->per_dir_config,
209 &authz_dbm_module);
210 char *user = r->user;
211 const char *realm = ap_auth_name(r);
212 const char *filegroup = NULL;
213 const char *orig_groups = NULL;
214 apr_status_t status;
215 const char *groups;
216 char *v;
218 if (!user) {
219 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
220 "access to %s failed, reason: no authenticated user", r->uri);
221 return AUTHZ_DENIED;
224 if (!conf->grpfile) {
225 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
226 "No group file was specified in the configuration");
227 return AUTHZ_DENIED;
230 /* fetch group data from dbm file. */
231 status = get_dbm_grp(r, apr_pstrcat(r->pool, user, ":", realm, NULL),
232 user, conf->grpfile, conf->dbmtype, &groups);
234 if (status != APR_SUCCESS) {
235 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
236 "could not open dbm (type %s) group access "
237 "file: %s", conf->dbmtype, conf->grpfile);
238 return AUTHZ_DENIED;
241 if (groups == NULL) {
242 /* no groups available, so exit immediately */
243 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
244 "Authorization of user %s to access %s failed, reason: "
245 "user doesn't appear in DBM group file (%s).",
246 r->user, r->uri, conf->grpfile);
247 return AUTHZ_DENIED;
250 orig_groups = groups;
252 filegroup = authz_owner_get_file_group(r);
254 if (filegroup) {
255 groups = orig_groups;
256 while (groups[0]) {
257 v = ap_getword(r->pool, &groups, ',');
258 if (!strcmp(v, filegroup)) {
259 return AUTHZ_GRANTED;
264 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
265 "Authorization of user %s to access %s failed, reason: "
266 "user is not part of the 'require'ed group(s).",
267 r->user, r->uri);
269 return AUTHZ_DENIED;
272 static const authz_provider authz_dbmgroup_provider =
274 &dbmgroup_check_authorization,
277 static const authz_provider authz_dbmfilegroup_provider =
279 &dbmfilegroup_check_authorization,
283 static void register_hooks(apr_pool_t *p)
285 authz_owner_get_file_group = APR_RETRIEVE_OPTIONAL_FN(authz_owner_get_file_group);
287 ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbm-group",
288 AUTHZ_PROVIDER_VERSION,
289 &authz_dbmgroup_provider,
290 AP_AUTH_INTERNAL_PER_CONF);
291 ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbm-file-group",
292 AUTHZ_PROVIDER_VERSION,
293 &authz_dbmfilegroup_provider,
294 AP_AUTH_INTERNAL_PER_CONF);
297 module AP_MODULE_DECLARE_DATA authz_dbm_module =
299 STANDARD20_MODULE_STUFF,
300 create_authz_dbm_dir_config, /* dir config creater */
301 NULL, /* dir merger --- default is to override */
302 NULL, /* server config */
303 NULL, /* merge server config */
304 authz_dbm_cmds, /* command apr_table_t */
305 register_hooks /* register hooks */