diff --git a/changes-entries/authn_socache_htaccess.txt b/changes-entries/authn_socache_htaccess.txt new file mode 100644 index 00000000000..e48a50b0e66 --- /dev/null +++ b/changes-entries/authn_socache_htaccess.txt @@ -0,0 +1,3 @@ + *) mod_authn_socache: Fix a crash if AuthnCacheProvideFor is used in + an htaccess file while caching is not otherwise enabled. + [Joe Orton] diff --git a/changes-entries/socache_dbm_notfound.txt b/changes-entries/socache_dbm_notfound.txt new file mode 100644 index 00000000000..447e98f6e5c --- /dev/null +++ b/changes-entries/socache_dbm_notfound.txt @@ -0,0 +1,3 @@ + *) mod_socache_dbm: Stop mod_authn_socache logging "Error accessing + authentication cache" for every user not yet in the cache. + [Joe Orton] diff --git a/docs/manual/mod/mod_authn_socache.xml b/docs/manual/mod/mod_authn_socache.xml index aa9e59804ef..d00779b9c5d 100644 --- a/docs/manual/mod/mod_authn_socache.xml +++ b/docs/manual/mod/mod_authn_socache.xml @@ -90,10 +90,58 @@ AuthnCacheSOCache dbm

Module developers should note that their modules must be enabled for caching with mod_authn_socache. A single optional API function ap_authn_cache_store is provided to cache credentials - a provider has just looked up or generated. Usage examples are - available in r957072, in which three authn providers are enabled for caching.

+ a provider has just looked up or generated. It is declared in + mod_auth.h:

+ + +void ap_authn_cache_store(request_rec *r, const char *module, + const char *user, const char *realm, + const char *data); + + +

Here module is the name by which the provider is + listed in AuthnCacheProvideFor, realm is + NULL for basic authentication, and data is + the password hash, or the digest authentication hash, found for + user.

+ +

A provider retrieves the function once the configuration has been + loaded, since mod_authn_socache may not be present, + and calls it whenever it has found a user's credentials:

+ + +static APR_OPTIONAL_FN_TYPE(ap_authn_cache_store) *authn_cache_store = NULL; + +static void opt_retr(void) +{ + authn_cache_store = APR_RETRIEVE_OPTIONAL_FN(ap_authn_cache_store); +} + +static authn_status check_password(request_rec *r, const char *user, + const char *password) +{ + const char *hash; + + /* ... look up the password hash for user ... */ + + if (authn_cache_store != NULL) { + authn_cache_store(r, "example", user, NULL, hash); + } + + /* ... validate password against hash ... */ +} + +static void register_hooks(apr_pool_t *p) +{ + /* ... */ + ap_hook_optional_fn_retrieve(opt_retr, NULL, NULL, APR_HOOK_MIDDLE); +} + + +

The providers in mod_authn_file, + mod_authn_dbm, and mod_authn_dbd + are enabled for caching in this way.

diff --git a/modules/aaa/mod_authn_socache.c b/modules/aaa/mod_authn_socache.c index 757083e40fe..53eb3b9af77 100644 --- a/modules/aaa/mod_authn_socache.c +++ b/modules/aaa/mod_authn_socache.c @@ -231,7 +231,11 @@ static const char *authn_cache_setprovider(cmd_parms *cmd, void *CFG, cfg->providers = apr_array_make(cmd->pool, 4, sizeof(const char*)); } APR_ARRAY_PUSH(cfg->providers, const char*) = arg; - configured = 1; + /* The cache is only set up at startup, so cannot be brought into + * use by an htaccess file. */ + if (ap_check_cmd_context(cmd, NOT_IN_HTACCESS) == NULL) { + configured = 1; + } return NULL; } diff --git a/modules/cache/mod_socache_dbm.c b/modules/cache/mod_socache_dbm.c index a81b6c67596..055163a9aa7 100644 --- a/modules/cache/mod_socache_dbm.c +++ b/modules/cache/mod_socache_dbm.c @@ -28,6 +28,10 @@ #define APR_WANT_STRFUNC #include "apr_want.h" #include "apr_dbm.h" +#include "apr_version.h" +#if !APR_VERSION_AT_LEAST(2,0,0) +#include "apu_version.h" +#endif #if APR_HAVE_UNISTD_H #include @@ -148,7 +152,7 @@ static apr_status_t socache_dbm_init(ap_socache_instance_t *ctx, #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 7) if ((rv = apr_dbm_get_driver(&driver, NULL, &err, - ctx->pool) != APR_SUCCESS) { + ctx->pool)) != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(10277) "Cannot load socache DBM library '%s': %s", err->reason, err->msg); @@ -255,7 +259,7 @@ static apr_status_t socache_dbm_store(ap_socache_instance_t *ctx, #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 7) if ((rv = apr_dbm_get_driver(&driver, NULL, &err, - ctx->pool) != APR_SUCCESS) { + ctx->pool)) != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(10278) "Cannot load socache DBM library '%s' (store): %s", err->reason, err->msg); @@ -331,14 +335,14 @@ static apr_status_t socache_dbm_retrieve(ap_socache_instance_t *ctx, server_rec */ apr_pool_clear(ctx->pool); #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 7) - if ((rv = apr_dbm_get_driver(&driver, NULL, &err, - ctx->pool) != APR_SUCCESS) { - ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(10279) + if ((rc = apr_dbm_get_driver(&driver, NULL, &err, + ctx->pool)) != APR_SUCCESS) { + ap_log_error(APLOG_MARK, APLOG_ERR, rc, s, APLOGNO(10279) "Cannot load socache DBM library '%s' (fetch): %s", err->reason, err->msg); return rc; } - if ((rv = apr_dbm_open2(&dbm, driver, ctx->data_file, + if ((rc = apr_dbm_open2(&dbm, driver, ctx->data_file, APR_DBM_RWCREATE, DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_ERR, rc, s, APLOGNO(00809) "Cannot open socache DBM file `%s' for reading " @@ -361,7 +365,12 @@ static apr_status_t socache_dbm_retrieve(ap_socache_instance_t *ctx, server_rec apr_dbm_close(dbm); return APR_NOTFOUND; } - if (dbmval.dptr == NULL || dbmval.dsize <= sizeof(apr_time_t)) { + if (dbmval.dptr == NULL) { + /* apr_dbm_fetch() succeeds for a key which is not there. */ + apr_dbm_close(dbm); + return APR_NOTFOUND; + } + if (dbmval.dsize <= sizeof(apr_time_t)) { apr_dbm_close(dbm); return APR_EGENERAL; } @@ -410,7 +419,7 @@ static apr_status_t socache_dbm_remove(ap_socache_instance_t *ctx, #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 7) if ((rv = apr_dbm_get_driver(&driver, NULL, &err, - ctx->pool) != APR_SUCCESS) { + ctx->pool)) != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(10280) "Cannot load socache DBM library '%s' (delete): %s", err->reason, err->msg); @@ -473,11 +482,11 @@ static void socache_dbm_expire(ap_socache_instance_t *ctx, server_rec *s) #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 7) if ((rv = apr_dbm_get_driver(&driver, NULL, &err, - ctx->pool) != APR_SUCCESS) { + ctx->pool)) != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(10281) "Cannot load socache DBM library '%s' (expire): %s", err->reason, err->msg); - return rv; + return; } #endif @@ -604,7 +613,7 @@ static void socache_dbm_status(ap_socache_instance_t *ctx, request_rec *r, apr_pool_clear(ctx->pool); #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 7) if ((rv = apr_dbm_get_driver(&driver, NULL, &err, - ctx->pool) != APR_SUCCESS) { + ctx->pool)) != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(10282) "Cannot load socache DBM library '%s' (status retrieval): %s", err->reason, err->msg); @@ -681,7 +690,7 @@ static apr_status_t socache_dbm_iterate(ap_socache_instance_t *ctx, now = apr_time_now(); #if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 7) if ((rv = apr_dbm_get_driver(&driver, NULL, &err, - ctx->pool) != APR_SUCCESS) { + ctx->pool)) != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(10283) "Cannot load socache DBM library '%s' (iterating): %s", err->reason, err->msg); diff --git a/test/modules/aaa/env.py b/test/modules/aaa/env.py index 87fcb667762..24fc431424e 100644 --- a/test/modules/aaa/env.py +++ b/test/modules/aaa/env.py @@ -1,8 +1,10 @@ +import base64 +import contextlib import hashlib import inspect import logging import os -from typing import List, Optional +from typing import Dict, List, Optional from pyhttpd.env import HttpdTestEnv, HttpdTestSetup from pyhttpd.result import ExecResult @@ -16,7 +18,8 @@ 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", - "authz_core", "authz_user", "proxy", "proxy_http"]) + "authz_core", "authz_user", "proxy", "proxy_http", + "authn_socache"]) class AAATestEnv(HttpdTestEnv): @@ -32,7 +35,8 @@ class AAATestEnv(HttpdTestEnv): def __init__(self, pytestconfig=None): super().__init__(pytestconfig=pytestconfig) - self.add_httpd_log_modules(["auth_digest", "authn_file", "authz_core"]) + self.add_httpd_log_modules(["auth_digest", "authn_file", "authz_core", + "authn_socache"]) self._digest_pwfile = os.path.join(self.server_dir, "digest.passwd") self._digest_pwfile2 = os.path.join(self.server_dir, "digest2.passwd") @@ -62,6 +66,42 @@ def digest_pwfile(self) -> str: def digest_pwfile2(self) -> str: return self._digest_pwfile2 + @staticmethod + def write_basic_pwfile(path: str, users: Dict[str, str]): + """Write a Basic auth password file holding `users`, a dict of + user name to password, replacing the file atomically. The + passwords are stored as {SHA} hashes, which need no htpasswd.""" + with open(path + ".tmp", 'w') as fd: + for user, password in users.items(): + sha = base64.b64encode( + hashlib.sha1(password.encode()).digest()).decode() + fd.write(f"{user}:{{SHA}}{sha}\n") + os.replace(path + ".tmp", path) + + def write_digest_pwfile(self, path: str, users: Dict[str, str]): + """As write_basic_pwfile(), for a Digest password file in REALM.""" + with open(path + ".tmp", 'w') as fd: + for user, password in users.items(): + ha1 = hashlib.md5( + f"{user}:{self.REALM}:{password}".encode()).hexdigest() + fd.write(f"{user}:{self.REALM}:{ha1}\n") + os.replace(path + ".tmp", path) + + @contextlib.contextmanager + def preserved_test_conf(self): + """Put back the installed test configuration on exit and restart + the server with it, for a test which installs its own while + sharing the package's server.""" + with open(self._test_conf) as fd: + saved = fd.read() + try: + yield + finally: + self.apache_stop() + with open(self._test_conf, 'w') as fd: + fd.write(saved) + assert self.apache_restart() == 0 + def configtest(self, directory_lines: List[str], extra_top_lines: Optional[List[str]] = None ) -> ExecResult: """Run `httpd -t` against a minimal, standalone config built from the diff --git a/test/modules/aaa/test_016_authn_socache.py b/test/modules/aaa/test_016_authn_socache.py new file mode 100644 index 00000000000..3f093c1c8ce --- /dev/null +++ b/test/modules/aaa/test_016_authn_socache.py @@ -0,0 +1,437 @@ +"""mod_authn_socache: caching of credentials found by another provider. + +The "socache" authn provider only ever answers from its cache, and it never +fills the cache itself: a backing provider (mod_authn_file here) hands over +what it found once it has located a user, if AuthnCacheProvideFor names it. +So socache is listed first and the backing provider after it; the first +request for a user falls through to the file, later ones are answered from +the cache until the entry expires. + +Whether an answer came from the cache is not visible in the response, so +these tests make the two sources disagree. A request with the password P1 +fills the cache; the password file is then rewritten to hold P2 instead. +From then on an answer from the cache accepts P1 and rejects P2, and an +answer from the file does the opposite. + +The cache outlives a test, so each test has a user name of its own. +""" + +import os +import time + +import pytest + +from pyhttpd.conf import HttpdConf +from . import digest_client as dc +from .env import AAATestEnv + +P1 = "firstpass2617" +P2 = "secondpass2617" + +BODY = "authn-socache-secret\n" + +# A second document in each directory, for a section to give a +# password file of its own. +OTHER = "other.txt" + +# Seconds, for the location whose entries are to expire under test. +SHORT_TIMEOUT = 2 + +# What mod_auth_basic and mod_auth_digest log for a wrong password and +# for an unknown user. +AUTH_FAILURES = ["AH01617", "AH01618", "AH01794", "AH01790"] + + +def _docdir(env, name): + return os.path.join(env.server_docs_dir, "socache", name) + + +def _make_docdir(env, name): + d = _docdir(env, name) + os.makedirs(d, exist_ok=True) + for fname in "secret.txt", OTHER: + with open(os.path.join(d, fname), 'wb') as fd: + fd.write(BODY.encode()) + return d + + +def _basic_lines(pwfile, cache_lines): + return [ + 'AuthType Basic', + 'AuthName "AAA Basic Realm"', + 'AuthBasicProvider socache file', + f'AuthUserFile "{pwfile}"', + *cache_lines, + 'Require valid-user', + ] + + +def _digest_lines(pwfile, cache_lines): + return [ + 'AuthType Digest', + f'AuthName "{AAATestEnv.REALM}"', + 'AuthDigestProvider socache file', + f'AuthUserFile "{pwfile}"', + *cache_lines, + 'Require valid-user', + ] + + +def _directory(env, name, lines): + return [f'', + *(f" {l}" for l in lines), + ''] + + +# Class scoped, to outlast the error log check made after each test. +@pytest.fixture(scope='class') +def _expect_auth_failures(env): + env.httpd_error_log.add_ignored_lognos(AUTH_FAILURES) + yield + env.httpd_error_log.remove_ignored_lognos(AUTH_FAILURES) + + +@pytest.mark.usefixtures("_expect_auth_failures") +class _SocacheBase: + + @pytest.fixture(scope='class') + @classmethod + def basic_pwfile(cls, env): + return os.path.join(env.server_dir, "socache-basic.passwd") + + @pytest.fixture(scope='class') + @classmethod + def basic_pwfile2(cls, env): + return os.path.join(env.server_dir, "socache-basic2.passwd") + + @pytest.fixture(scope='class') + @classmethod + def digest_pwfile(cls, env): + return os.path.join(env.server_dir, "socache-digest.passwd") + + def url(self, env, name, fname="secret.txt"): + return env.mkurl("http", "aaa", f"/socache/{name}/{fname}") + + def basic(self, env, name, user, password, fname="secret.txt"): + """The status of a Basic authenticated request.""" + r = env.curl_get(self.url(env, name, fname), + options=["-u", f"{user}:{password}"]) + assert r.exit_code == 0, r.stderr + if r.response["status"] == 200: + assert r.response["body"].decode() == BODY + return r.response["status"] + + def digest(self, env, name, user, password): + """The status of a Digest authenticated request, answering a + fresh challenge.""" + url = self.url(env, name) + r = env.curl_get(url) + assert r.response["status"] == 401 + challenge = dc.DigestChallenge.parse( + r.response["header"]["www-authenticate"]) + auth = dc.build_authorization( + user, challenge, password, method="GET", + uri=f"/socache/{name}/secret.txt") + r = env.curl_get(url, options=["-H", f"Authorization: {auth}"]) + assert r.exit_code == 0, r.stderr + if r.response["status"] == 200: + assert r.response["body"].decode() == BODY + return r.response["status"] + + +class TestAuthnSocache(_SocacheBase): + """Run for the default socache provider, when no AuthnCacheSOCache + is given, and for shmcb by name.""" + + @pytest.fixture(autouse=True, scope='class', params=[None, "shmcb"], + ids=["default", "shmcb"]) + @classmethod + def _class_scope(cls, env, request, basic_pwfile, basic_pwfile2, + digest_pwfile): + AAATestEnv.write_basic_pwfile(basic_pwfile, {}) + AAATestEnv.write_basic_pwfile(basic_pwfile2, {}) + env.write_digest_pwfile(digest_pwfile, {}) + cached = ['AuthnCacheProvideFor file'] + conf = HttpdConf(env) + if request.param: + conf.add(f'AuthnCacheSOCache {request.param}') + for name, cache_lines in [ + ("basic", cached), + # Caches for a provider which is not the one in use. + ("other", ['AuthnCacheProvideFor dbm']), + ("none", []), + ("dir1", cached), + ("dir2", cached), + ("literal1", cached + ['AuthnCacheContext shared']), + ("literal2", cached + ['AuthnCacheContext shared']), + ("literal3", cached + ['AuthnCacheContext another']), + ("server1", cached + ['AuthnCacheContext server']), + ("server2", cached + ['AuthnCacheContext server']), + ("timeout", cached + [f'AuthnCacheTimeout {SHORT_TIMEOUT}']), + ]: + conf.add(_directory(env, name, + _basic_lines(basic_pwfile, cache_lines))) + # One directory with two password files: OTHER has its own. + for name, other_lines in [ + ("files", []), + ("files-context", ['AuthnCacheContext other-file']), + ]: + conf.add(_directory(env, name, [ + *_basic_lines(basic_pwfile, cached), + f'', + f' AuthUserFile "{basic_pwfile2}"', + *(f" {l}" for l in other_lines), + '', + ])) + for name, cache_lines in [ + ("digest", cached), + ("digest-none", []), + ]: + conf.add(_directory(env, name, + _digest_lines(digest_pwfile, cache_lines))) + with env.preserved_test_conf(): + conf.install() + assert env.apache_restart() == 0 + yield + + def test_authn_socache_160_cached_entry_is_used(self, env, basic_pwfile): + """A user the backing provider has found is authenticated from the + cache afterwards, whatever the backing provider would now say.""" + user = "user160" + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P1}) + assert self.basic(env, "basic", user, P1) == 200 + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P2}) + assert self.basic(env, "basic", user, P1) == 200 + AAATestEnv.write_basic_pwfile(basic_pwfile, {}) + assert self.basic(env, "basic", user, P1) == 200 + + def test_authn_socache_161_cached_entry_rejects_wrong_password( + self, env, basic_pwfile): + """A password which does not match the cached entry is refused + outright, not passed on to the backing provider.""" + user = "user161" + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P1}) + assert self.basic(env, "basic", user, P1) == 200 + assert self.basic(env, "basic", user, "wrong") == 401 + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P2}) + assert self.basic(env, "basic", user, P2) == 401 + assert self.basic(env, "basic", user, P1) == 200 + + def test_authn_socache_162_wrong_password_first(self, env, basic_pwfile): + """A wrong password is refused when the backing provider answers, + and that failed attempt caches the user's entry all the same.""" + user = "user162" + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P1}) + assert self.basic(env, "basic", user, "wrong") == 401 + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P2}) + assert self.basic(env, "basic", user, "wrong") == 401 + assert self.basic(env, "basic", user, P1) == 200 + + def test_authn_socache_163_unknown_user_not_cached( + self, env, basic_pwfile): + """A user the backing provider does not know is refused and + nothing is remembered about it, so it can log in once created.""" + user = "user163" + AAATestEnv.write_basic_pwfile(basic_pwfile, {"someone": P1}) + assert self.basic(env, "basic", user, P1) == 401 + assert self.basic(env, "basic", user, P1) == 401 + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P1}) + assert self.basic(env, "basic", user, P1) == 200 + + @pytest.mark.parametrize("name", ["other", "none"]) + def test_authn_socache_164_provide_for_gates_caching( + self, env, basic_pwfile, name): + """Nothing is cached for a backing provider AuthnCacheProvideFor + does not name, nor where the directive is absent.""" + user = f"user164{name}" + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P1}) + assert self.basic(env, name, user, P1) == 200 + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P2}) + assert self.basic(env, name, user, P1) == 401 + assert self.basic(env, name, user, P2) == 200 + + def test_authn_socache_165_directory_context(self, env, basic_pwfile): + """With the default "directory" AuthnCacheContext an entry is only + good for the directory of the request which cached it.""" + user = "user165" + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P1}) + assert self.basic(env, "dir1", user, P1) == 200 + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P2}) + assert self.basic(env, "dir2", user, P1) == 401 + assert self.basic(env, "dir2", user, P2) == 200 + # and the two entries stay apart + AAATestEnv.write_basic_pwfile(basic_pwfile, {}) + assert self.basic(env, "dir1", user, P1) == 200 + assert self.basic(env, "dir1", user, P2) == 401 + assert self.basic(env, "dir2", user, P2) == 200 + assert self.basic(env, "dir2", user, P1) == 401 + + def test_authn_socache_166_literal_context(self, env, basic_pwfile): + """Locations with the same literal AuthnCacheContext share their + entries, and not with one which has a different context.""" + user = "user166" + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P1}) + assert self.basic(env, "literal1", user, P1) == 200 + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P2}) + assert self.basic(env, "literal2", user, P1) == 200 + assert self.basic(env, "literal2", user, P2) == 401 + assert self.basic(env, "literal3", user, P1) == 401 + assert self.basic(env, "literal3", user, P2) == 200 + + def test_authn_socache_167_server_context(self, env, basic_pwfile): + """Locations with the "server" AuthnCacheContext share their + entries across the server, and not with other contexts.""" + user = "user167" + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P1}) + assert self.basic(env, "server1", user, P1) == 200 + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P2}) + assert self.basic(env, "server2", user, P1) == 200 + assert self.basic(env, "server2", user, P2) == 401 + assert self.basic(env, "basic", user, P1) == 401 + + def test_authn_socache_168_timeout(self, env, basic_pwfile): + """An entry is dropped once AuthnCacheTimeout has passed, and the + backing provider is asked again.""" + user = "user168" + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P1}) + assert self.basic(env, "timeout", user, P1) == 200 + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P2}) + assert self.basic(env, "timeout", user, P1) == 200 + time.sleep(SHORT_TIMEOUT + 1) + assert self.basic(env, "timeout", user, P1) == 401 + assert self.basic(env, "timeout", user, P2) == 200 + + def test_authn_socache_169_restart_empties_cache(self, env, basic_pwfile): + """The cache does not survive a restart.""" + user = "user169" + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P1}) + assert self.basic(env, "basic", user, P1) == 200 + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P2}) + assert self.basic(env, "basic", user, P1) == 200 + assert env.apache_restart() == 0 + assert self.basic(env, "basic", user, P1) == 401 + assert self.basic(env, "basic", user, P2) == 200 + + def test_authn_socache_170_digest_cached_entry_is_used( + self, env, digest_pwfile): + """The hash the backing provider found for a Digest user is served + from the cache afterwards.""" + user = "user170" + env.write_digest_pwfile(digest_pwfile, {user: P1}) + assert self.digest(env, "digest", user, P1) == 200 + env.write_digest_pwfile(digest_pwfile, {user: P2}) + assert self.digest(env, "digest", user, P1) == 200 + assert self.digest(env, "digest", user, P2) == 401 + env.write_digest_pwfile(digest_pwfile, {}) + assert self.digest(env, "digest", user, P1) == 200 + + def test_authn_socache_171_digest_not_cached_without_provide_for( + self, env, digest_pwfile): + """No Digest hash is cached where AuthnCacheProvideFor is absent.""" + user = "user171" + env.write_digest_pwfile(digest_pwfile, {user: P1}) + assert self.digest(env, "digest-none", user, P1) == 200 + env.write_digest_pwfile(digest_pwfile, {user: P2}) + assert self.digest(env, "digest-none", user, P1) == 401 + assert self.digest(env, "digest-none", user, P2) == 200 + + def test_authn_socache_172_digest_unknown_user_not_cached( + self, env, digest_pwfile): + """A Digest user the backing provider does not know is refused and + can log in once created.""" + user = "user172" + env.write_digest_pwfile(digest_pwfile, {}) + assert self.digest(env, "digest", user, P1) == 401 + env.write_digest_pwfile(digest_pwfile, {user: P1}) + assert self.digest(env, "digest", user, P1) == 200 + + def test_authn_socache_173_files_share_directory_context( + self, env, basic_pwfile, basic_pwfile2): + """With the default "directory" AuthnCacheContext, documents in + one directory share their entries even where they use different + password files.""" + user = "user173" + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P1}) + AAATestEnv.write_basic_pwfile(basic_pwfile2, {user: P2}) + assert self.basic(env, "files", user, P1) == 200 + assert self.basic(env, "files", user, P1, OTHER) == 200 + assert self.basic(env, "files", user, P2, OTHER) == 401 + + def test_authn_socache_174_files_context( + self, env, basic_pwfile, basic_pwfile2): + """An AuthnCacheContext of its own keeps the entries for a + document with a different password file apart from those of the + rest of the directory.""" + user = "user174" + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P1}) + AAATestEnv.write_basic_pwfile(basic_pwfile2, {user: P2}) + for _ in range(2): + # first from the password files, then from the cache + assert self.basic(env, "files-context", user, P1) == 200 + assert self.basic(env, "files-context", user, P1, OTHER) == 401 + assert self.basic(env, "files-context", user, P2, OTHER) == 200 + assert self.basic(env, "files-context", user, P2) == 401 + AAATestEnv.write_basic_pwfile(basic_pwfile, {}) + AAATestEnv.write_basic_pwfile(basic_pwfile2, {}) + assert self.basic(env, "files-context", user, P1) == 200 + assert self.basic(env, "files-context", user, P2, OTHER) == 200 + + +class TestAuthnSocacheHtaccess(_SocacheBase): + """AuthnCacheProvideFor given only in .htaccess files.""" + + @pytest.fixture(autouse=True, scope='class', params=[True, False], + ids=["enabled", "not-enabled"]) + @classmethod + def enabled(cls, env, request, basic_pwfile): + AAATestEnv.write_basic_pwfile(basic_pwfile, {}) + d = _make_docdir(env, "htaccess") + with open(os.path.join(d, ".htaccess"), 'w') as fd: + fd.write('\n'.join(_basic_lines( + basic_pwfile, ['AuthnCacheProvideFor file']))) + fd.write('\n') + conf = HttpdConf(env) + if request.param: + conf.add('AuthnCacheEnable') + conf.add([f'', + ' AllowOverride AuthConfig', + '']) + with env.preserved_test_conf(): + conf.install() + assert env.apache_restart() == 0 + yield request.param + + def test_authn_socache_180_htaccess(self, env, basic_pwfile, enabled): + """AuthnCacheEnable sets the cache up for .htaccess files to use + where the configuration itself has no AuthnCacheProvideFor. + Without it authentication works, uncached.""" + user = "user180" + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P1}) + assert self.basic(env, "htaccess", user, P1) == 200 + AAATestEnv.write_basic_pwfile(basic_pwfile, {user: P2}) + if enabled: + assert self.basic(env, "htaccess", user, P1) == 200 + assert self.basic(env, "htaccess", user, P2) == 401 + else: + assert self.basic(env, "htaccess", user, P1) == 401 + assert self.basic(env, "htaccess", user, P2) == 200 + + +class TestAuthnSocacheConfig: + + def test_authn_socache_190_unknown_socache_provider(self, env): + """AuthnCacheSOCache naming a provider which is not loaded is a + configuration error.""" + r = env.configtest(['Require all granted'], + extra_top_lines=['AuthnCacheSOCache no-such']) + assert r.exit_code != 0 + assert "Unknown socache provider 'no-such'" in r.stderr + + def test_authn_socache_191_socache_global_only(self, env): + """AuthnCacheSOCache and AuthnCacheEnable are refused inside a + virtual host.""" + for directive in ['AuthnCacheSOCache shmcb', 'AuthnCacheEnable']: + r = env.configtest(['Require all granted'], extra_top_lines=[ + '', directive, '']) + assert r.exit_code != 0, directive + assert directive.split()[0] in r.stderr