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.
18 * http_auth: authentication
20 * Rob McCool & Brian Behlendorf.
22 * Adapted to Apache by rst.
24 * dirkx - Added Authoritative control to allow passing on to lower
25 * modules if and only if the userid is not known to this
26 * module. A known user with a faulty or absent password still
27 * causes an AuthRequired. The default is 'Authoritative', i.e.
28 * no control is passed along.
31 #define APR_WANT_STRFUNC
33 #include "apr_strings.h"
35 #include "apr_md5.h" /* for apr_password_validate */
38 #include "http_config.h"
39 #include "http_core.h"
41 #include "http_protocol.h"
42 #include "http_request.h" /* for ap_hook_(check_user_id | auth_checker)*/
47 char *auth_dbmgrpfile;
49 int auth_dbmauthoritative;
50 } dbm_auth_config_rec;
52 static void *create_dbm_auth_dir_config(apr_pool_t *p, char *d)
54 dbm_auth_config_rec *conf = apr_palloc(p, sizeof(*conf));
56 conf->auth_dbmpwfile = NULL;
57 conf->auth_dbmgrpfile = NULL;
58 conf->auth_dbmtype = "default";
59 conf->auth_dbmauthoritative = 1; /* fortress is secure by default */
64 static const char *set_dbm_slot(cmd_parms *cmd, void *offset,
65 const char *f, const char *t)
67 if (!t || strcmp(t, "dbm"))
70 return ap_set_file_slot(cmd, offset, f);
73 static const char *set_dbm_type(cmd_parms *cmd,
77 dbm_auth_config_rec *conf = dir_config;
79 conf->auth_dbmtype = apr_pstrdup(cmd->pool, arg);
83 static const command_rec dbm_auth_cmds[] =
85 AP_INIT_TAKE1("AuthDBMUserFile", ap_set_file_slot,
86 (void *)APR_OFFSETOF(dbm_auth_config_rec, auth_dbmpwfile),
87 OR_AUTHCFG, "dbm database file containing user IDs and passwords"),
88 AP_INIT_TAKE1("AuthDBMGroupFile", ap_set_file_slot,
89 (void *)APR_OFFSETOF(dbm_auth_config_rec, auth_dbmgrpfile),
90 OR_AUTHCFG, "dbm database file containing group names and member user IDs"),
91 AP_INIT_TAKE12("AuthUserFile", set_dbm_slot,
92 (void *)APR_OFFSETOF(dbm_auth_config_rec, auth_dbmpwfile),
94 AP_INIT_TAKE12("AuthGroupFile", set_dbm_slot,
95 (void *)APR_OFFSETOF(dbm_auth_config_rec, auth_dbmgrpfile),
97 AP_INIT_TAKE1("AuthDBMType", set_dbm_type,
99 OR_AUTHCFG, "what type of DBM file the user file is"),
100 AP_INIT_FLAG("AuthDBMAuthoritative", ap_set_flag_slot,
101 (void *)APR_OFFSETOF(dbm_auth_config_rec, auth_dbmauthoritative),
102 OR_AUTHCFG, "Set to 'no' to allow access control to be passed along to lower modules, if the UserID is not known in this module"),
106 module AP_MODULE_DECLARE_DATA auth_dbm_module;
108 static char *get_dbm_pw(request_rec *r,
110 char *auth_dbmpwfile,
118 #ifndef NETSCAPE_DBM_COMPAT
119 q.dsize = strlen(q.dptr);
121 q.dsize = strlen(q.dptr) + 1;
124 retval = apr_dbm_open_ex(&f, dbtype, auth_dbmpwfile, APR_DBM_READONLY,
125 APR_OS_DEFAULT, r->pool);
126 if (retval != APR_SUCCESS) {
127 ap_log_rerror(APLOG_MARK, APLOG_ERR, retval, r,
128 "could not open dbm (type %s) auth file: %s", dbtype,
132 if (apr_dbm_fetch(f, q, &d) == APR_SUCCESS && d.dptr) {
133 pw = apr_palloc(r->pool, d.dsize + 1);
134 strncpy(pw, d.dptr, d.dsize);
135 pw[d.dsize] = '\0'; /* Terminate the string */
142 /* We do something strange with the group file. If the group file
143 * contains any : we assume the format is
144 * key=username value=":"groupname [":"anything here is ignored]
145 * otherwise we now (0.8.14+) assume that the format is
146 * key=username value=groupname
147 * The first allows the password and group files to be the same
148 * physical DBM file; key=username value=password":"groupname[":"anything]
150 * mark@telescope.org, 22Sep95
153 static char *get_dbm_grp(request_rec *r, char *user, char *auth_dbmgrpfile,
156 char *grp_data = get_dbm_pw(r, user, auth_dbmgrpfile,dbtype);
160 if (grp_data == NULL)
163 if ((grp_colon = strchr(grp_data, ':')) != NULL) {
164 grp_colon2 = strchr(++grp_colon, ':');
172 static int dbm_authenticate_basic_user(request_rec *r)
174 dbm_auth_config_rec *conf = ap_get_module_config(r->per_dir_config,
177 char *real_pw, *colon_pw;
178 apr_status_t invalid_pw;
181 if ((res = ap_get_basic_auth_pw(r, &sent_pw)))
184 if (!conf->auth_dbmpwfile)
187 if (!(real_pw = get_dbm_pw(r, r->user, conf->auth_dbmpwfile,
188 conf->auth_dbmtype))) {
189 if (!(conf->auth_dbmauthoritative))
191 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
192 "DBM user %s not found: %s", r->user, r->filename);
193 ap_note_basic_auth_failure(r);
194 return HTTP_UNAUTHORIZED;
196 /* Password is up to first : if exists */
197 colon_pw = strchr(real_pw, ':');
201 invalid_pw = apr_password_validate(sent_pw, real_pw);
202 if (invalid_pw != APR_SUCCESS) {
203 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
204 "DBM user %s: authentication failure for \"%s\": "
207 ap_note_basic_auth_failure(r);
208 return HTTP_UNAUTHORIZED;
215 static int dbm_check_auth(request_rec *r)
217 dbm_auth_config_rec *conf = ap_get_module_config(r->per_dir_config,
219 char *user = r->user;
220 int m = r->method_number;
222 const apr_array_header_t *reqs_arr = ap_requires(r);
223 require_line *reqs = reqs_arr ? (require_line *) reqs_arr->elts : NULL;
229 if (!conf->auth_dbmgrpfile)
234 for (x = 0; x < reqs_arr->nelts; x++) {
236 if (!(reqs[x].method_mask & (AP_METHOD_BIT << m)))
239 t = reqs[x].requirement;
240 w = ap_getword_white(r->pool, &t);
242 if (!strcmp(w, "group") && conf->auth_dbmgrpfile) {
243 const char *orig_groups, *groups;
246 if (!(groups = get_dbm_grp(r, user, conf->auth_dbmgrpfile,
247 conf->auth_dbmtype))) {
248 if (!(conf->auth_dbmauthoritative))
250 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
251 "user %s not in DBM group file %s: %s",
252 user, conf->auth_dbmgrpfile, r->filename);
253 ap_note_basic_auth_failure(r);
254 return HTTP_UNAUTHORIZED;
256 orig_groups = groups;
258 w = ap_getword_white(r->pool, &t);
259 groups = orig_groups;
261 v = ap_getword(r->pool, &groups, ',');
266 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
267 "user %s not in right group: %s",
269 ap_note_basic_auth_failure(r);
270 return HTTP_UNAUTHORIZED;
277 static void register_hooks(apr_pool_t *p)
279 ap_hook_check_user_id(dbm_authenticate_basic_user, NULL, NULL,
281 ap_hook_auth_checker(dbm_check_auth, NULL, NULL, APR_HOOK_MIDDLE);
284 module AP_MODULE_DECLARE_DATA auth_dbm_module =
286 STANDARD20_MODULE_STUFF,
287 create_dbm_auth_dir_config, /* dir config creater */
288 NULL, /* dir merger --- default is to override */
289 NULL, /* server config */
290 NULL, /* merge server config */
291 dbm_auth_cmds, /* command apr_table_t */
292 register_hooks /* register hooks */