|
Packit |
90a5c9 |
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
Packit |
90a5c9 |
* contributor license agreements. See the NOTICE file distributed with
|
|
Packit |
90a5c9 |
* this work for additional information regarding copyright ownership.
|
|
Packit |
90a5c9 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
Packit |
90a5c9 |
* (the "License"); you may not use this file except in compliance with
|
|
Packit |
90a5c9 |
* the License. You may obtain a copy of the License at
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
90a5c9 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
90a5c9 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
90a5c9 |
* See the License for the specific language governing permissions and
|
|
Packit |
90a5c9 |
* limitations under the License.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "apr_strings.h"
|
|
Packit |
90a5c9 |
#include "apr_md5.h" /* for apr_password_validate */
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "ap_config.h"
|
|
Packit |
90a5c9 |
#include "ap_provider.h"
|
|
Packit |
90a5c9 |
#include "httpd.h"
|
|
Packit |
90a5c9 |
#include "http_config.h"
|
|
Packit |
90a5c9 |
#include "http_core.h"
|
|
Packit |
90a5c9 |
#include "http_log.h"
|
|
Packit |
90a5c9 |
#include "http_protocol.h"
|
|
Packit |
90a5c9 |
#include "http_request.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "mod_auth.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct {
|
|
Packit |
90a5c9 |
char *pwfile;
|
|
Packit |
90a5c9 |
} authn_file_config_rec;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static APR_OPTIONAL_FN_TYPE(ap_authn_cache_store) *authn_cache_store = NULL;
|
|
Packit |
90a5c9 |
#define AUTHN_CACHE_STORE(r,user,realm,data) \
|
|
Packit |
90a5c9 |
if (authn_cache_store != NULL) \
|
|
Packit |
90a5c9 |
authn_cache_store((r), "file", (user), (realm), (data))
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *create_authn_file_dir_config(apr_pool_t *p, char *d)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authn_file_config_rec *conf = apr_palloc(p, sizeof(*conf));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
conf->pwfile = NULL; /* just to illustrate the default really */
|
|
Packit |
90a5c9 |
return conf;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const command_rec authn_file_cmds[] =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
AP_INIT_TAKE1("AuthUserFile", ap_set_file_slot,
|
|
Packit |
90a5c9 |
(void *)APR_OFFSETOF(authn_file_config_rec, pwfile),
|
|
Packit |
90a5c9 |
OR_AUTHCFG, "text file containing user IDs and passwords"),
|
|
Packit |
90a5c9 |
{NULL}
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
module AP_MODULE_DECLARE_DATA authn_file_module;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static authn_status check_password(request_rec *r, const char *user,
|
|
Packit |
90a5c9 |
const char *password)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authn_file_config_rec *conf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&authn_file_module);
|
|
Packit |
90a5c9 |
ap_configfile_t *f;
|
|
Packit |
90a5c9 |
char l[MAX_STRING_LEN];
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
char *file_password = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!conf->pwfile) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01619)
|
|
Packit |
90a5c9 |
"AuthUserFile not specified in the configuration");
|
|
Packit |
90a5c9 |
return AUTH_GENERAL_ERROR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = ap_pcfg_openfile(&f, r->pool, conf->pwfile);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (status != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01620)
|
|
Packit |
90a5c9 |
"Could not open password file: %s", conf->pwfile);
|
|
Packit |
90a5c9 |
return AUTH_GENERAL_ERROR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
|
|
Packit |
90a5c9 |
const char *rpw, *w;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Skip # or blank lines. */
|
|
Packit |
90a5c9 |
if ((l[0] == '#') || (!l[0])) {
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rpw = l;
|
|
Packit |
90a5c9 |
w = ap_getword(r->pool, &rpw, ':');
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!strcmp(user, w)) {
|
|
Packit |
90a5c9 |
file_password = ap_getword(r->pool, &rpw, ':');
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ap_cfg_closefile(f);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!file_password) {
|
|
Packit |
90a5c9 |
return AUTH_USER_NOT_FOUND;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
AUTHN_CACHE_STORE(r, user, NULL, file_password);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = apr_password_validate(password, file_password);
|
|
Packit |
90a5c9 |
if (status != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return AUTH_DENIED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return AUTH_GRANTED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static authn_status get_realm_hash(request_rec *r, const char *user,
|
|
Packit |
90a5c9 |
const char *realm, char **rethash)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authn_file_config_rec *conf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&authn_file_module);
|
|
Packit |
90a5c9 |
ap_configfile_t *f;
|
|
Packit |
90a5c9 |
char l[MAX_STRING_LEN];
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
char *file_hash = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!conf->pwfile) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01621)
|
|
Packit |
90a5c9 |
"AuthUserFile not specified in the configuration");
|
|
Packit |
90a5c9 |
return AUTH_GENERAL_ERROR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = ap_pcfg_openfile(&f, r->pool, conf->pwfile);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (status != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01622)
|
|
Packit |
90a5c9 |
"Could not open password file: %s", conf->pwfile);
|
|
Packit |
90a5c9 |
return AUTH_GENERAL_ERROR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
|
|
Packit |
90a5c9 |
const char *rpw, *w, *x;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Skip # or blank lines. */
|
|
Packit |
90a5c9 |
if ((l[0] == '#') || (!l[0])) {
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rpw = l;
|
|
Packit |
90a5c9 |
w = ap_getword(r->pool, &rpw, ':');
|
|
Packit |
90a5c9 |
x = ap_getword(r->pool, &rpw, ':');
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (x && w && !strcmp(user, w) && !strcmp(realm, x)) {
|
|
Packit |
90a5c9 |
/* Remember that this is a md5 hash of user:realm:password. */
|
|
Packit |
90a5c9 |
file_hash = ap_getword(r->pool, &rpw, ':');
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ap_cfg_closefile(f);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!file_hash) {
|
|
Packit |
90a5c9 |
return AUTH_USER_NOT_FOUND;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
*rethash = file_hash;
|
|
Packit |
90a5c9 |
AUTHN_CACHE_STORE(r, user, realm, file_hash);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return AUTH_USER_FOUND;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const authn_provider authn_file_provider =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
&check_password,
|
|
Packit |
90a5c9 |
&get_realm_hash,
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void opt_retr(void)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
authn_cache_store = APR_RETRIEVE_OPTIONAL_FN(ap_authn_cache_store);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
static void register_hooks(apr_pool_t *p)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_register_auth_provider(p, AUTHN_PROVIDER_GROUP, "file",
|
|
Packit |
90a5c9 |
AUTHN_PROVIDER_VERSION,
|
|
Packit |
90a5c9 |
&authn_file_provider, AP_AUTH_INTERNAL_PER_CONF);
|
|
Packit |
90a5c9 |
ap_hook_optional_fn_retrieve(opt_retr, NULL, NULL, APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
AP_DECLARE_MODULE(authn_file) =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
STANDARD20_MODULE_STUFF,
|
|
Packit |
90a5c9 |
create_authn_file_dir_config, /* dir config creater */
|
|
Packit |
90a5c9 |
NULL, /* dir merger --- default is to override */
|
|
Packit |
90a5c9 |
NULL, /* server config */
|
|
Packit |
90a5c9 |
NULL, /* merge server config */
|
|
Packit |
90a5c9 |
authn_file_cmds, /* command apr_table_t */
|
|
Packit |
90a5c9 |
register_hooks /* register hooks */
|
|
Packit |
90a5c9 |
};
|