Skip to content
Closed
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
10 changes: 10 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
-*- coding: utf-8 -*-
Changes with Apache 2.5.1

*) mod_authn_socache: Bind cache keys to the authentication domain.
The server name in the directory and server contexts now includes
the port, the key includes the name of the provider that produced
the entry, and Basic auth entries include the protection space
(AuthName), as Digest entries already included the realm.
Previously, the key separated neither providers nor protection
spaces, so distinct credential sources silently shared cache
entries. The AuthnCacheContext documentation now states which
differences the default context does not separate. [Hafiar]

*) mpm_event, mpm_worker, mpm_winnt, mpm_prefork, mpm_motorz: Added optional
functions to account for connections managed outside the MPM accept loop
(e.g., UDP).
Expand Down
48 changes: 36 additions & 12 deletions docs/manual/mod/mod_authn_socache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,44 @@ AuthnCacheProvideFor dbd myprovider
<contextlist><context>directory</context></contextlist>

<usage>
<p>This directive specifies a string to be used along with the supplied
username (and realm in the case of Digest Authentication) in constructing
a cache key. This serves to disambiguate identical usernames serving
different authentication areas on the server.</p>
<p>This directive specifies a string to be used along with the name of
the provider that produced the entry, the supplied username, and the
realm (for Basic Authentication, the protection space named by
<directive module="mod_authn_core">AuthName</directive>) in
constructing a cache key. This serves to disambiguate identical
usernames serving different authentication areas on the server.</p>
<p>Two special values for this are <code>directory</code>, which uses
the directory context of the request as a string, and <code>server</code>
which uses the virtual host name.</p>
<p>The default is <code>directory</code>, which is also the most
conservative setting. This is likely to be less than optimal, as it
(for example) causes <var>$app-base</var>, <var>$app-base/images</var>,
<var>$app-base/scripts</var> and <var>$app-base/media</var> each to
have its own separate cache key. A better policy is to name the
the server (hostname and port) and the directory of the request URI,
and <code>server</code>, which uses the virtual host name and port.</p>
<p>The default is <code>directory</code>. The default separates
backend provider results only where the directory-like URL path, the
provider, or the <directive
module="mod_authn_core">AuthName</directive> differs. A configuration
that varies the credential source within a single directory and a
single <directive module="mod_authn_core">AuthName</directive> — for
example <directive module="core" type="section">Files</directive>
sections with different <directive
module="mod_authn_file">AuthUserFile</directive> settings — must give
each source its own <directive
module="mod_authn_core">AuthName</directive> or its own custom
<directive>AuthnCacheContext</directive>; otherwise the sources share
cache entries, and a password valid for one source is accepted for
the others.</p>
<p>The <code>directory</code> context causes <var>$app-base</var>,
<var>$app-base/images</var>, <var>$app-base/scripts</var> and
<var>$app-base/media</var> each to have its own separate cache
entries. Where a whole directory tree is a single authentication
area backed by one credential source, name the
<directive>AuthnCacheContext</directive> for the password
provider: for example a <var>htpasswd</var> file or database table.</p>
provider instead: for example a <var>htpasswd</var> file or database
table, and use that one context for the whole area.</p>
<p>A shared custom context is only safe where every request in its
scope is checked against the same credential source. If different
credential sources are used within the scope of one custom context
(different password files, database tables, or queries for
different resources), entries are shared between them, and a
password valid for one source will be accepted for the others.
Give each distinct credential source its own context.</p>
<p>Contexts can be shared across different areas of a server, where
credentials are shared. However, this has potential to become a vector
for cross-site or cross-application security breaches, so this directive
Expand Down
98 changes: 75 additions & 23 deletions modules/aaa/mod_authn_socache.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,28 +263,57 @@ static const command_rec authn_cache_cmds[] =
};

static const char *construct_key(request_rec *r, const char *context,
const char *user, const char *realm)
const char *provider, const char *user,
const char *realm)
{
const char *slash = ap_strrchr_c(r->uri, '/');
/* handle "special" context values */
if (!strcmp(context, directory) && slash) {
char *new_context;
new_context = apr_palloc(r->pool, slash - r->uri +
strlen(r->server->server_hostname) + 1);
strcpy(new_context, r->server->server_hostname);
strncat(new_context, r->uri, slash - r->uri);
context = new_context;
if (!strcmp(context, directory)) {
const char *slash = ap_strrchr_c(r->uri, '/');
/* The directory context identifies an entry by the server and
* the directory of the request URI. Within one directory,
* entries are separated by the provider and realm mixed into
* the key below; a configuration that varies the credential
* source within a single directory and realm (for example
* <Files> sections with different AuthUserFile settings) must
* use distinct AuthName values or a custom AuthnCacheContext
* to keep the sources apart, as the documentation states.
*/
if (slash) {
context = apr_psprintf(r->pool, "%s:%u%.*s",
r->server->server_hostname,
(unsigned)ap_get_server_port(r),
(int)(slash - r->uri), r->uri);
}
else {
context = apr_psprintf(r->pool, "%s:%u%s",
r->server->server_hostname,
(unsigned)ap_get_server_port(r), r->uri);
}
}
else if (!strcmp(context, "server")) {
context = r->server->server_hostname;
context = apr_psprintf(r->pool, "%s:%u",
r->server->server_hostname,
(unsigned)ap_get_server_port(r));
}
/* any other context value is literal */

if (realm == NULL) { /* basic auth */
return apr_pstrcat(r->pool, context, ":", user, NULL);
if (realm == NULL) {
/* Basic auth: the protection space (AuthName) is the realm. */
realm = ap_auth_name(r);
}

/* The key is bound to the provider that produced the entry, so a
* hash stored by one credential source is never validated against
* a request for another, and to the realm, so entries are not
* shared between protection spaces.
*/
if (realm == NULL) {
return apr_pstrcat(r->pool, context, ":", provider, ":", user,
NULL);
}
else { /* digest auth */
return apr_pstrcat(r->pool, context, ":", user, ":", realm, NULL);
else {
return apr_pstrcat(r->pool, context, ":", provider, ":", user,
":", realm, NULL);
}
}

Expand Down Expand Up @@ -323,7 +352,7 @@ static void ap_authn_cache_store(request_rec *r, const char *module,

/* We have the mutex, so go ahead */
/* first build our key and determine expiry time */
key = construct_key(r, dcfg->context, user, realm);
key = construct_key(r, dcfg->context, module, user, realm);
expiry = apr_time_now() + dcfg->timeout;

/* store it */
Expand Down Expand Up @@ -367,14 +396,27 @@ static authn_status check_password(request_rec *r, const char *user,
authn_cache_dircfg *dcfg;
unsigned char val[MAX_VAL_LEN];
unsigned int vallen = MAX_VAL_LEN - 1;
int i;
dcfg = ap_get_module_config(r->per_dir_config, &authn_socache_module);
if (!configured || !dcfg->providers) {
return AUTH_USER_NOT_FOUND;
}
key = construct_key(r, dcfg->context, user, NULL);
rv = socache_provider->retrieve(socache_instance, r->server,
(unsigned char*)key, strlen(key),
val, &vallen, r->pool);
/* Entries are stored under the key of the provider that produced
* them; try each provider this configuration caches for.
*/
rv = APR_NOTFOUND;
for (i = 0; i < dcfg->providers->nelts; i++) {
const char *provider = APR_ARRAY_IDX(dcfg->providers, i,
const char *);
vallen = MAX_VAL_LEN - 1;
key = construct_key(r, dcfg->context, provider, user, NULL);
rv = socache_provider->retrieve(socache_instance, r->server,
(unsigned char*)key, strlen(key),
val, &vallen, r->pool);
if (rv == APR_SUCCESS || !APR_STATUS_IS_NOTFOUND(rv)) {
break;
}
}

if (APR_STATUS_IS_NOTFOUND(rv)) {
/* not found - just return */
Expand Down Expand Up @@ -412,14 +454,24 @@ static authn_status get_realm_hash(request_rec *r, const char *user,
authn_cache_dircfg *dcfg;
unsigned char val[MAX_VAL_LEN];
unsigned int vallen = MAX_VAL_LEN - 1;
int i;
dcfg = ap_get_module_config(r->per_dir_config, &authn_socache_module);
if (!configured || !dcfg->providers) {
return AUTH_USER_NOT_FOUND;
}
key = construct_key(r, dcfg->context, user, realm);
rv = socache_provider->retrieve(socache_instance, r->server,
(unsigned char*)key, strlen(key),
val, &vallen, r->pool);
rv = APR_NOTFOUND;
for (i = 0; i < dcfg->providers->nelts; i++) {
const char *provider = APR_ARRAY_IDX(dcfg->providers, i,
const char *);
vallen = MAX_VAL_LEN - 1;
key = construct_key(r, dcfg->context, provider, user, realm);
rv = socache_provider->retrieve(socache_instance, r->server,
(unsigned char*)key, strlen(key),
val, &vallen, r->pool);
if (rv == APR_SUCCESS || !APR_STATUS_IS_NOTFOUND(rv)) {
break;
}
}

if (APR_STATUS_IS_NOTFOUND(rv)) {
/* not found - just return */
Expand Down
52 changes: 52 additions & 0 deletions test/modules/aaa/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ def pytest_report_header(config, start_path):
return f"mod_auth_digest [apache: {env.get_httpd_version()}, mpm: {env.mpm_module}, {env.prefix}]"


def _socache_dir(docs, path, files_lines):
lines = [
f'<Directory "{docs}/socache/{path}">',
' AuthType Basic',
' AuthName "socache-shared"',
' AuthBasicProvider socache file',
' AuthnCacheProvideFor file',
' AuthnCacheTimeout 600',
]
lines.extend(f" {l}" for l in files_lines)
lines.append(' Require valid-user')
lines.append('</Directory>')
return lines


def _digest_dir(docs, path, extra_lines):
lines = [
f'<Directory "{docs}/digest/{path}">',
Expand Down Expand Up @@ -102,6 +117,43 @@ def env(pytestconfig) -> AAATestEnv:
# AuthDigestProvider intentionally omitted: falls back to "file".
f'AuthUserFile "{pwfile}"',
]))
# mod_authn_socache areas (test_020): one directory whose two files
# are backed by different htpasswd files. In "byname" the files carry
# distinct AuthName values, in "byctx" distinct AuthnCacheContext
# values, and in "samename" neither, which the documentation states
# shares cache entries between the two sources.
conf.add('AuthnCacheSOCache shmcb')
conf.add('AuthnCacheEnable')
low = env.basic_pwfile_low
high = env.basic_pwfile_high
conf.add(_socache_dir(docs, "byname", [
'<Files "low.txt">',
'AuthName "lowarea"',
f'AuthUserFile "{low}"',
'</Files>',
'<Files "high.txt">',
'AuthName "higharea"',
f'AuthUserFile "{high}"',
'</Files>',
]))
conf.add(_socache_dir(docs, "byctx", [
'<Files "low.txt">',
f'AuthUserFile "{low}"',
'AuthnCacheContext lowctx',
'</Files>',
'<Files "high.txt">',
f'AuthUserFile "{high}"',
'AuthnCacheContext highctx',
'</Files>',
]))
conf.add(_socache_dir(docs, "samename", [
'<Files "low.txt">',
f'AuthUserFile "{low}"',
'</Files>',
'<Files "high.txt">',
f'AuthUserFile "{high}"',
'</Files>',
]))
# A request which fails header validation (e.g. no Host on HTTP/1.1)
# is answered before the Digest module's post_read_request runs, so it
# has no per-request Digest record; an ErrorDocument redirecting it into
Expand Down
31 changes: 30 additions & 1 deletion test/modules/aaa/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class AAATestSetup(HttpdTestSetup):
def __init__(self, env: 'HttpdTestEnv'):
super().__init__(env=env)
self.add_source_dir(os.path.dirname(inspect.getfile(AAATestSetup)))
self.add_modules(["auth_digest", "authn_file", "authn_core",
self.add_modules(["auth_digest", "auth_basic", "authn_socache",
"socache_shmcb", "authn_file", "authn_core",
"authz_core", "authz_user", "proxy", "proxy_http"])


Expand All @@ -26,6 +27,11 @@ class AAATestEnv(HttpdTestEnv):
DIGEST_PASSWORD = "digestpass2617"
DIGEST_USER2 = "otheruser"
DIGEST_PASSWORD2 = "otherpass2617"
# Basic auth accounts for the mod_authn_socache tests: the same user
# with a different password in each of two htpasswd files.
SOCACHE_USER = "socacheuser"
SOCACHE_PASSWORD_LOW = "lowpass2617"
SOCACHE_PASSWORD_HIGH = "highpass2617"
# DIGEST_USER's password in the second file, for a vhost with its own
# accounts under the same realm.
DIGEST_PASSWORD_VHOST2 = "vhost2pass2617"
Expand All @@ -35,10 +41,33 @@ def __init__(self, pytestconfig=None):
self.add_httpd_log_modules(["auth_digest", "authn_file", "authz_core"])
self._digest_pwfile = os.path.join(self.server_dir, "digest.passwd")
self._digest_pwfile2 = os.path.join(self.server_dir, "digest2.passwd")
self._basic_pwfile_low = os.path.join(self.server_dir, "basic-low.passwd")
self._basic_pwfile_high = os.path.join(self.server_dir, "basic-high.passwd")

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

def _write_basic_pwfiles(self):
import base64

def sha_entry(user, password):
digest = hashlib.sha1(password.encode()).digest()
return f"{user}:{{SHA}}{base64.b64encode(digest).decode()}\n"

with open(self._basic_pwfile_low, 'w') as fd:
fd.write(sha_entry(self.SOCACHE_USER, self.SOCACHE_PASSWORD_LOW))
with open(self._basic_pwfile_high, 'w') as fd:
fd.write(sha_entry(self.SOCACHE_USER, self.SOCACHE_PASSWORD_HIGH))

@property
def basic_pwfile_low(self) -> str:
return self._basic_pwfile_low

@property
def basic_pwfile_high(self) -> str:
return self._basic_pwfile_high

def _write_digest_pwfile(self):
def ha1(user, password):
Expand Down
1 change: 1 addition & 0 deletions test/modules/aaa/htdocs/socache/byctx/high.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
socache-byctx-high
1 change: 1 addition & 0 deletions test/modules/aaa/htdocs/socache/byctx/low.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
socache-byctx-low
1 change: 1 addition & 0 deletions test/modules/aaa/htdocs/socache/byname/high.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
socache-byname-high
1 change: 1 addition & 0 deletions test/modules/aaa/htdocs/socache/byname/low.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
socache-byname-low
1 change: 1 addition & 0 deletions test/modules/aaa/htdocs/socache/samename/high.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
socache-samename-high
1 change: 1 addition & 0 deletions test/modules/aaa/htdocs/socache/samename/low.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
socache-samename-low
Loading
Loading