Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changes-entries/65172.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*) mod_authn_dbd, mod_authn_socache: Preserve the extra AuthDBDUserPWQuery
and AuthDBDUserRealmQuery columns, exported as AUTHENTICATE_<NAME>
environment variables, on a login answered from the cache
(AuthBasicProvider/AuthDigestProvider "socache dbd"). Previously only
the first, uncached login for a user had them. PR 65172.
[Arturo Bernal]
2 changes: 1 addition & 1 deletion docs/log-message-tags/next-number
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10687
10703
4 changes: 3 additions & 1 deletion include/ap_mmn.h
Original file line number Diff line number Diff line change
Expand Up @@ -744,14 +744,16 @@
* connection_added() and ap_mpm_note_extra_
* connection_removed(), and ap_mpm_wait_for_extra_
* connections(), to mpm_common.h
* 20211221.36 (2.5.1-dev) Add the optional function ap_authn_cache_store_env()
* to mod_auth.h
*/

#define MODULE_MAGIC_COOKIE 0x41503235UL /* "AP25" */

#ifndef MODULE_MAGIC_NUMBER_MAJOR
#define MODULE_MAGIC_NUMBER_MAJOR 20211221
#endif
#define MODULE_MAGIC_NUMBER_MINOR 35 /* 0...n */
#define MODULE_MAGIC_NUMBER_MINOR 36 /* 0...n */

/**
* Determine if the server's current MODULE_MAGIC_NUMBER is at least a
Expand Down
15 changes: 15 additions & 0 deletions include/mod_auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ APR_DECLARE_OPTIONAL_FN(void, ap_authn_cache_store,
(request_rec*, const char*, const char*,
const char*, const char*));

/* ap_authn_cache_store_env: As ap_authn_cache_store, for a provider
* whose lookup also sets variables in r->subprocess_env. The cache keeps
* them with the value and sets them again on a cache hit, as the lookup
* itself would have done.
* @param r The request rec
* @param module Module identifier
* @param user User name to authenticate
* @param realm Digest authn realm (NULL for basic authn)
* @param data The value looked up by the authn provider, to cache
* @param env The variables set by the lookup, or NULL for none
*/
APR_DECLARE_OPTIONAL_FN(void, ap_authn_cache_store_env,
(request_rec*, const char*, const char*,
const char*, const char*, const apr_table_t*));

#ifdef __cplusplus
}
#endif
Expand Down
43 changes: 36 additions & 7 deletions modules/aaa/mod_authn_dbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ typedef struct {
static ap_dbd_t *(*authn_dbd_acquire_fn)(request_rec*) = NULL;
static void (*authn_dbd_prepare_fn)(server_rec*, const char*, const char*) = NULL;
static APR_OPTIONAL_FN_TYPE(ap_authn_cache_store) *authn_cache_store = NULL;
#define AUTHN_CACHE_STORE(r,user,realm,data) \
if (authn_cache_store != NULL) \
authn_cache_store((r), "dbd", (user), (realm), (data))
static APR_OPTIONAL_FN_TYPE(ap_authn_cache_store_env) *authn_cache_store_env = NULL;

static void *authn_dbd_cr_conf(apr_pool_t *pool, char *dummy)
{
Expand Down Expand Up @@ -92,11 +90,28 @@ static const command_rec authn_dbd_cmds[] =
{NULL}
};

/* Cache a lookup, with the extra columns it exported as env. A cache
* which cannot keep them would answer a later login without them, so
* such a lookup is not cached there.
*/
static void authn_dbd_cache_store(request_rec *r, const char *user,
const char *realm, const char *data,
const apr_table_t *env)
{
if (authn_cache_store_env != NULL) {
authn_cache_store_env(r, "dbd", user, realm, data, env);
}
else if (authn_cache_store != NULL && env == NULL) {
authn_cache_store(r, "dbd", user, realm, data);
}
}

static authn_status authn_dbd_password(request_rec *r, const char *user,
const char *password)
{
apr_status_t rv;
const char *dbd_password = NULL;
apr_table_t *env = NULL;
apr_dbd_prepared_t *statement;
apr_dbd_results_t *res = NULL;
apr_dbd_row_t *row = NULL;
Expand Down Expand Up @@ -163,7 +178,10 @@ static authn_status authn_dbd_password(request_rec *r, const char *user,
}
j++;
}
apr_table_set(r->subprocess_env, str,
if (env == NULL) {
env = apr_table_make(r->pool, 2);
}
apr_table_set(env, str,
apr_dbd_get_entry(dbd->driver, row, i));
i++;
}
Expand All @@ -174,10 +192,13 @@ static authn_status authn_dbd_password(request_rec *r, const char *user,
/* we can't break out here or row won't get cleaned up */
}

if (env != NULL) {
apr_table_overlap(r->subprocess_env, env, APR_OVERLAP_TABLES_SET);
}
if (!dbd_password) {
return AUTH_USER_NOT_FOUND;
}
AUTHN_CACHE_STORE(r, user, NULL, dbd_password);
authn_dbd_cache_store(r, user, NULL, dbd_password, env);

rv = ap_password_validate(r, user, password, dbd_password);

Expand All @@ -193,6 +214,7 @@ static authn_status authn_dbd_realm(request_rec *r, const char *user,
{
apr_status_t rv;
const char *dbd_hash = NULL;
apr_table_t *env = NULL;
apr_dbd_prepared_t *statement;
apr_dbd_results_t *res = NULL;
apr_dbd_row_t *row = NULL;
Expand Down Expand Up @@ -258,7 +280,10 @@ static authn_status authn_dbd_realm(request_rec *r, const char *user,
}
j++;
}
apr_table_set(r->subprocess_env, str,
if (env == NULL) {
env = apr_table_make(r->pool, 2);
}
apr_table_set(env, str,
apr_dbd_get_entry(dbd->driver, row, i));
i++;
}
Expand All @@ -269,17 +294,21 @@ static authn_status authn_dbd_realm(request_rec *r, const char *user,
/* we can't break out here or row won't get cleaned up */
}

if (env != NULL) {
apr_table_overlap(r->subprocess_env, env, APR_OVERLAP_TABLES_SET);
}
if (!dbd_hash) {
return AUTH_USER_NOT_FOUND;
}
AUTHN_CACHE_STORE(r, user, realm, dbd_hash);
authn_dbd_cache_store(r, user, realm, dbd_hash, env);
*rethash = apr_pstrdup(r->pool, dbd_hash);
return AUTH_USER_FOUND;
}

static void opt_retr(void)
{
authn_cache_store = APR_RETRIEVE_OPTIONAL_FN(ap_authn_cache_store);
authn_cache_store_env = APR_RETRIEVE_OPTIONAL_FN(ap_authn_cache_store_env);
}

static void authn_dbd_hooks(apr_pool_t *p)
Expand Down
104 changes: 97 additions & 7 deletions modules/aaa/mod_authn_socache.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,86 @@ static const char *construct_key(request_rec *r, const char *context,
}
}

static void ap_authn_cache_store(request_rec *r, const char *module,
const char *user, const char *realm,
const char* data)
/* Size of the buffer the lookups below read a cached value into,
* room for its terminating NUL included.
*/
#define MAX_VAL_LEN 256

/* The cached value is the provider's data followed, for each variable
* in env, by a NUL, its name, a NUL and its value. None of them can hold
* a NUL, and a value without one is the data alone.
*/
static unsigned char *cache_value(apr_pool_t *p, const char *data,
const apr_table_t *env, apr_size_t *len)
{
const apr_array_header_t *arr;
const apr_table_entry_t *elts;
apr_size_t datalen = strlen(data);
unsigned char *val, *pos;
int i;

if (env == NULL || apr_is_empty_table(env)) {
*len = datalen;
return (unsigned char *)data;
}
arr = apr_table_elts(env);
elts = (const apr_table_entry_t *)arr->elts;

*len = datalen;
for (i = 0; i < arr->nelts; i++) {
*len += 1 + strlen(elts[i].key) + 1 + strlen(elts[i].val);
}

pos = val = apr_palloc(p, *len);
memcpy(pos, data, datalen);
pos += datalen;
for (i = 0; i < arr->nelts; i++) {
apr_size_t keylen = strlen(elts[i].key);
apr_size_t vallen = strlen(elts[i].val);

*pos++ = '\0';
memcpy(pos, elts[i].key, keylen);
pos += keylen;
*pos++ = '\0';
memcpy(pos, elts[i].val, vallen);
pos += vallen;
}
return val;
}

/* Terminate the data of a cached value of vallen bytes in place, with
* val[vallen] available for that, and set the variables stored with it.
*/
static void restore_cached_env(request_rec *r, unsigned char *val,
unsigned int vallen)
{
const char *end = (const char *)val + vallen;
const char *pos;

val[vallen] = '\0';
pos = (const char *)val + strlen((const char *)val);
while (pos < end) {
const char *key = pos + 1;
const char *value = key + strlen(key) + 1;

if (value > end) {
break;
}
apr_table_set(r->subprocess_env, key, value);
pos = value + strlen(value);
}
}

static void ap_authn_cache_store_env(request_rec *r, const char *module,
const char *user, const char *realm,
const char *data, const apr_table_t *env)
{
apr_status_t rv;
authn_cache_dircfg *dcfg;
const char *key;
apr_time_t expiry;
unsigned char *val;
apr_size_t vallen;

/* first check whether we're caching for this module */
dcfg = ap_get_module_config(r->per_dir_config, &authn_socache_module);
Expand All @@ -351,6 +423,16 @@ static void ap_authn_cache_store(request_rec *r, const char *module,
return;
}

val = cache_value(r->pool, data, env, &vallen);
if (env != NULL && vallen >= MAX_VAL_LEN) {
/* Cached, it would be read back without its variables */
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(10701)
"authn credentials for %s not cached: %" APR_SIZE_T_FMT
" bytes with their variables, over the cache value "
"limit", user, vallen);
return;
}

/* OK, we're on. Grab mutex to do our business */
rv = apr_global_mutex_trylock(authn_cache_mutex);
if (APR_STATUS_IS_EBUSY(rv)) {
Expand All @@ -374,7 +456,7 @@ static void ap_authn_cache_store(request_rec *r, const char *module,
/* store it */
rv = socache_provider->store(socache_instance, r->server,
(unsigned char*)key, strlen(key), expiry,
(unsigned char*)data, strlen(data), r->pool);
val, vallen, r->pool);
if (rv == APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01681)
"Cached authn credentials for %s in %s",
Expand All @@ -393,7 +475,13 @@ static void ap_authn_cache_store(request_rec *r, const char *module,
}
}

#define MAX_VAL_LEN 256
static void ap_authn_cache_store(request_rec *r, const char *module,
const char *user, const char *realm,
const char* data)
{
ap_authn_cache_store_env(r, module, user, realm, data, NULL);
}

static authn_status check_password(request_rec *r, const char *user,
const char *password)
{
Expand Down Expand Up @@ -444,7 +532,7 @@ static authn_status check_password(request_rec *r, const char *user,
/* OK, we got a value */
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01685)
"Authn cache: found credentials for %s", user);
val[vallen] = 0;
restore_cached_env(r, val, vallen);
}
else {
/* error: give up and pass the buck */
Expand Down Expand Up @@ -499,6 +587,7 @@ static authn_status get_realm_hash(request_rec *r, const char *user,
/* OK, we got a value */
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01688)
"Authn cache: found credentials for %s", user);
restore_cached_env(r, val, vallen);
}
else {
/* error: give up and pass the buck */
Expand All @@ -507,7 +596,7 @@ static authn_status get_realm_hash(request_rec *r, const char *user,
"Error accessing authentication cache");
return AUTH_USER_NOT_FOUND;
}
*rethash = apr_pstrmemdup(r->pool, (char *)val, vallen);
*rethash = apr_pstrdup(r->pool, (char *)val);

return AUTH_USER_FOUND;
}
Expand All @@ -524,6 +613,7 @@ static void register_hooks(apr_pool_t *p)
AUTHN_PROVIDER_VERSION,
&authn_cache_provider, AP_AUTH_INTERNAL_PER_CONF);
APR_REGISTER_OPTIONAL_FN(ap_authn_cache_store);
APR_REGISTER_OPTIONAL_FN(ap_authn_cache_store_env);
ap_hook_pre_config(authn_cache_precfg, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_post_config(authn_cache_post_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_child_init(authn_cache_child_init, NULL, NULL, APR_HOOK_MIDDLE);
Expand Down
39 changes: 37 additions & 2 deletions test/modules/aaa/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import inspect
import logging
import os
import sqlite3
from typing import Dict, List, Optional

from pyhttpd.env import HttpdTestEnv, HttpdTestSetup
Expand All @@ -19,7 +20,9 @@ def __init__(self, env: 'HttpdTestEnv'):
self.add_source_dir(os.path.dirname(inspect.getfile(AAATestSetup)))
self.add_modules(["auth_digest", "authn_file", "authn_core",
"authz_core", "authz_user", "proxy", "proxy_http",
"authn_socache"])
"authn_socache", "dbd", "authn_dbd", "auth_basic",
"socache_shmcb"])
self.add_cgi_module()


class AAATestEnv(HttpdTestEnv):
Expand All @@ -33,16 +36,48 @@ class AAATestEnv(HttpdTestEnv):
# accounts under the same realm.
DIGEST_PASSWORD_VHOST2 = "vhost2pass2617"

DBD_USER = "dbduser"
DBD_PASSWORD = "dbdpass2617"
DBD_ROLE = "editor"
# A role too long to be cached with the password hash.
DBD_LONG_USER = "dbdlonguser"
DBD_LONG_ROLE = "r" * 300

def __init__(self, pytestconfig=None):
super().__init__(pytestconfig=pytestconfig)
self.add_httpd_log_modules(["auth_digest", "authn_file", "authz_core",
"authn_socache"])
"authn_socache", "authn_dbd", "dbd"])
self._digest_pwfile = os.path.join(self.server_dir, "digest.passwd")
self._digest_pwfile2 = os.path.join(self.server_dir, "digest2.passwd")
self._dbd_file = os.path.join(self.server_dir, "authn_dbd.sqlite3")

def setup_httpd(self, setup: HttpdTestSetup = None):
super().setup_httpd(setup=AAATestSetup(env=self))
self._write_digest_pwfile()
self._write_dbd_file()

def _write_dbd_file(self):
if os.path.exists(self._dbd_file):
os.remove(self._dbd_file)
con = sqlite3.connect(self._dbd_file)
try:
con.execute("CREATE TABLE users (user TEXT, password TEXT, role TEXT)")
# "{SHA}" + base64(sha1(password)): the format ap_password_validate()
# checks itself, without depending on which crypt(3) algorithms the
# host libc offers.
digest = hashlib.sha1(self.DBD_PASSWORD.encode()).digest()
pw_hash = "{SHA}" + base64.b64encode(digest).decode()
for user, role in [(self.DBD_USER, self.DBD_ROLE),
(self.DBD_LONG_USER, self.DBD_LONG_ROLE)]:
con.execute("INSERT INTO users (user, password, role) "
"VALUES (?, ?, ?)", (user, pw_hash, role))
con.commit()
finally:
con.close()

@property
def dbd_file(self) -> str:
return self._dbd_file

def _write_digest_pwfile(self):
def ha1(user, password):
Expand Down
Loading