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 #include "apr_strings.h"
19 #include "ap_config.h"
20 #include "ap_provider.h"
22 #include "http_config.h"
23 #include "http_core.h"
25 #include "http_protocol.h"
26 #include "http_request.h"
32 } authn_file_config_rec;
34 static APR_OPTIONAL_FN_TYPE(ap_authn_cache_store) *authn_cache_store = NULL;
35 #define AUTHN_CACHE_STORE(r,user,realm,data) \
36 if (authn_cache_store != NULL) \
37 authn_cache_store((r), "file", (user), (realm), (data))
39 static void *create_authn_file_dir_config(apr_pool_t *p, char *d)
41 authn_file_config_rec *conf = apr_palloc(p, sizeof(*conf));
43 conf->pwfile = NULL; /* just to illustrate the default really */
47 static const command_rec authn_file_cmds[] =
49 AP_INIT_TAKE1("AuthUserFile", ap_set_file_slot,
50 (void *)APR_OFFSETOF(authn_file_config_rec, pwfile),
51 OR_AUTHCFG, "text file containing user IDs and passwords"),
55 module AP_MODULE_DECLARE_DATA authn_file_module;
57 static authn_status check_password(request_rec *r, const char *user,
60 authn_file_config_rec *conf = ap_get_module_config(r->per_dir_config,
63 char l[MAX_STRING_LEN];
65 char *file_password = NULL;
68 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01619)
69 "AuthUserFile not specified in the configuration");
70 return AUTH_GENERAL_ERROR;
73 status = ap_pcfg_openfile(&f, r->pool, conf->pwfile);
75 if (status != APR_SUCCESS) {
76 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01620)
77 "Could not open password file: %s", conf->pwfile);
78 return AUTH_GENERAL_ERROR;
81 while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
84 /* Skip # or blank lines. */
85 if ((l[0] == '#') || (!l[0])) {
90 w = ap_getword(r->pool, &rpw, ':');
92 if (!strcmp(user, w)) {
93 file_password = ap_getword(r->pool, &rpw, ':');
100 return AUTH_USER_NOT_FOUND;
102 AUTHN_CACHE_STORE(r, user, NULL, file_password);
104 status = ap_password_validate(r, user, password, file_password);
105 if (status != APR_SUCCESS) {
112 static authn_status get_realm_hash(request_rec *r, const char *user,
113 const char *realm, char **rethash)
115 authn_file_config_rec *conf = ap_get_module_config(r->per_dir_config,
118 char l[MAX_STRING_LEN];
120 char *file_hash = NULL;
123 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01621)
124 "AuthUserFile not specified in the configuration");
125 return AUTH_GENERAL_ERROR;
128 status = ap_pcfg_openfile(&f, r->pool, conf->pwfile);
130 if (status != APR_SUCCESS) {
131 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01622)
132 "Could not open password file: %s", conf->pwfile);
133 return AUTH_GENERAL_ERROR;
136 while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
137 const char *rpw, *w, *x;
139 /* Skip # or blank lines. */
140 if ((l[0] == '#') || (!l[0])) {
145 w = ap_getword(r->pool, &rpw, ':');
146 x = ap_getword(r->pool, &rpw, ':');
148 if (x && w && !strcmp(user, w) && !strcmp(realm, x)) {
149 /* Remember that this is a md5 hash of user:realm:password. */
150 file_hash = ap_getword(r->pool, &rpw, ':');
157 return AUTH_USER_NOT_FOUND;
160 *rethash = file_hash;
161 AUTHN_CACHE_STORE(r, user, realm, file_hash);
163 return AUTH_USER_FOUND;
166 static const authn_provider authn_file_provider =
172 static void opt_retr(void)
174 authn_cache_store = APR_RETRIEVE_OPTIONAL_FN(ap_authn_cache_store);
176 static void register_hooks(apr_pool_t *p)
178 ap_register_auth_provider(p, AUTHN_PROVIDER_GROUP, "file",
179 AUTHN_PROVIDER_VERSION,
180 &authn_file_provider, AP_AUTH_INTERNAL_PER_CONF);
181 ap_hook_optional_fn_retrieve(opt_retr, NULL, NULL, APR_HOOK_MIDDLE);
184 AP_DECLARE_MODULE(authn_file) =
186 STANDARD20_MODULE_STUFF,
187 create_authn_file_dir_config, /* dir config creater */
188 NULL, /* dir merger --- default is to override */
189 NULL, /* server config */
190 NULL, /* merge server config */
191 authn_file_cmds, /* command apr_table_t */
192 register_hooks /* register hooks */