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
19 #include "apr_strings.h"
24 #include "http_config.h"
25 #include "ap_provider.h"
26 #include "http_core.h"
28 #include "http_protocol.h"
29 #include "http_request.h" /* for ap_hook_(check_user_id | auth_checker)*/
32 #include "mod_authz_owner.h"
37 } authz_dbm_config_rec;
40 /* This should go into APR; perhaps with some nice
41 * caching/locking/flocking of the open dbm file.
43 static char *get_dbm_entry_as_str(apr_pool_t *pool, apr_dbm_t *f, char *key)
48 #ifndef NETSCAPE_DBM_COMPAT
49 q.dsize = strlen(q.dptr);
51 q.dsize = strlen(q.dptr) + 1;
54 if (apr_dbm_fetch(f, q, &d) == APR_SUCCESS && d.dptr) {
55 return apr_pstrmemdup(pool, d.dptr, d.dsize);
61 static void *create_authz_dbm_dir_config(apr_pool_t *p, char *d)
63 authz_dbm_config_rec *conf = apr_palloc(p, sizeof(*conf));
66 conf->dbmtype = "default";
71 static const command_rec authz_dbm_cmds[] =
73 AP_INIT_TAKE1("AuthDBMGroupFile", ap_set_file_slot,
74 (void *)APR_OFFSETOF(authz_dbm_config_rec, grpfile),
75 OR_AUTHCFG, "database file containing group names and member user IDs"),
76 AP_INIT_TAKE1("AuthzDBMType", ap_set_string_slot,
77 (void *)APR_OFFSETOF(authz_dbm_config_rec, dbmtype),
78 OR_AUTHCFG, "what type of DBM file the group file is"),
82 module AP_MODULE_DECLARE_DATA authz_dbm_module;
84 /* We do something strange with the group file. If the group file
85 * contains any : we assume the format is
86 * key=username value=":"groupname [":"anything here is ignored]
87 * otherwise we now (0.8.14+) assume that the format is
88 * key=username value=groupname
89 * The first allows the password and group files to be the same
90 * physical DBM file; key=username value=password":"groupname[":"anything]
92 * mark@telescope.org, 22Sep95
95 static apr_status_t get_dbm_grp(request_rec *r, char *key1, char *key2,
96 const char *dbmgrpfile, const char *dbtype,
99 char *grp_colon, *val;
103 retval = apr_dbm_open_ex(&f, dbtype, dbmgrpfile, APR_DBM_READONLY,
104 APR_OS_DEFAULT, r->pool);
106 if (retval != APR_SUCCESS) {
110 /* Try key2 only if key1 failed */
111 if (!(val = get_dbm_entry_as_str(r->pool, f, key1))) {
112 val = get_dbm_entry_as_str(r->pool, f, key2);
117 if (val && (grp_colon = ap_strchr(val, ':')) != NULL) {
118 char *grp_colon2 = ap_strchr(++grp_colon, ':');
132 static authz_status dbmgroup_check_authorization(request_rec *r,
133 const char *require_args,
134 const void *parsed_require_args)
136 authz_dbm_config_rec *conf = ap_get_module_config(r->per_dir_config,
138 char *user = r->user;
140 const char *err = NULL;
141 const ap_expr_info_t *expr = parsed_require_args;
146 const char *orig_groups = NULL;
147 const char *realm = ap_auth_name(r);
152 return AUTHZ_DENIED_NO_USER;
155 if (!conf->grpfile) {
156 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01798)
157 "No group file was specified in the configuration");
161 /* fetch group data from dbm file only once. */
165 status = get_dbm_grp(r, apr_pstrcat(r->pool, user, ":", realm, NULL),
166 user, conf->grpfile, conf->dbmtype, &groups);
168 if (status != APR_SUCCESS) {
169 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01799)
170 "could not open dbm (type %s) group access "
171 "file: %s", conf->dbmtype, conf->grpfile);
172 return AUTHZ_GENERAL_ERROR;
175 if (groups == NULL) {
176 /* no groups available, so exit immediately */
177 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01800)
178 "Authorization of user %s to access %s failed, reason: "
179 "user doesn't appear in DBM group file (%s).",
180 r->user, r->uri, conf->grpfile);
184 orig_groups = groups;
187 require = ap_expr_str_exec(r, expr, &err);
189 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02591)
190 "authz_dbm authorize: require dbm-group: Can't "
191 "evaluate require expression: %s", err);
196 while ((w = ap_getword_white(r->pool, &t)) && w[0]) {
197 groups = orig_groups;
199 v = ap_getword(r->pool, &groups, ',');
201 return AUTHZ_GRANTED;
206 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01801)
207 "Authorization of user %s to access %s failed, reason: "
208 "user is not part of the 'require'ed group(s).",
214 static APR_OPTIONAL_FN_TYPE(authz_owner_get_file_group) *authz_owner_get_file_group;
216 static authz_status dbmfilegroup_check_authorization(request_rec *r,
217 const char *require_args,
218 const void *parsed_require_args)
220 authz_dbm_config_rec *conf = ap_get_module_config(r->per_dir_config,
222 char *user = r->user;
223 const char *realm = ap_auth_name(r);
224 const char *filegroup = NULL;
230 return AUTHZ_DENIED_NO_USER;
233 if (!conf->grpfile) {
234 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01802)
235 "No group file was specified in the configuration");
239 /* fetch group data from dbm file. */
240 status = get_dbm_grp(r, apr_pstrcat(r->pool, user, ":", realm, NULL),
241 user, conf->grpfile, conf->dbmtype, &groups);
243 if (status != APR_SUCCESS) {
244 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01803)
245 "could not open dbm (type %s) group access "
246 "file: %s", conf->dbmtype, conf->grpfile);
250 if (groups == NULL) {
251 /* no groups available, so exit immediately */
252 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01804)
253 "Authorization of user %s to access %s failed, reason: "
254 "user doesn't appear in DBM group file (%s).",
255 r->user, r->uri, conf->grpfile);
259 filegroup = authz_owner_get_file_group(r);
263 v = ap_getword(r->pool, &groups, ',');
264 if (!strcmp(v, filegroup)) {
265 return AUTHZ_GRANTED;
270 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01805)
271 "Authorization of user %s to access %s failed, reason: "
272 "user is not part of the 'require'ed group(s).",
278 static const char *dbm_parse_config(cmd_parms *cmd, const char *require_line,
279 const void **parsed_require_line)
281 const char *expr_err = NULL;
282 ap_expr_info_t *expr;
284 expr = ap_expr_parse_cmd(cmd, require_line, AP_EXPR_FLAG_STRING_RESULT,
288 return apr_pstrcat(cmd->temp_pool,
289 "Cannot parse expression in require line: ",
292 *parsed_require_line = expr;
297 static const authz_provider authz_dbmgroup_provider =
299 &dbmgroup_check_authorization,
303 static const authz_provider authz_dbmfilegroup_provider =
305 &dbmfilegroup_check_authorization,
309 static void authz_dbm_getfns(void)
311 authz_owner_get_file_group = APR_RETRIEVE_OPTIONAL_FN(authz_owner_get_file_group);
314 static void register_hooks(apr_pool_t *p)
316 ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbm-group",
317 AUTHZ_PROVIDER_VERSION,
318 &authz_dbmgroup_provider,
319 AP_AUTH_INTERNAL_PER_CONF);
320 ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "dbm-file-group",
321 AUTHZ_PROVIDER_VERSION,
322 &authz_dbmfilegroup_provider,
323 AP_AUTH_INTERNAL_PER_CONF);
324 ap_hook_optional_fn_retrieve(authz_dbm_getfns, NULL, NULL, APR_HOOK_MIDDLE);
327 AP_DECLARE_MODULE(authz_dbm) =
329 STANDARD20_MODULE_STUFF,
330 create_authz_dbm_dir_config, /* dir config creater */
331 NULL, /* dir merger --- default is to override */
332 NULL, /* server config */
333 NULL, /* merge server config */
334 authz_dbm_cmds, /* command apr_table_t */
335 register_hooks /* register hooks */