Skip to content
Merged
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
3 changes: 3 additions & 0 deletions changes-entries/authn_socache_htaccess.txt
Original file line number Diff line number Diff line change
@@ -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]
3 changes: 3 additions & 0 deletions changes-entries/socache_dbm_notfound.txt
Original file line number Diff line number Diff line change
@@ -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]
56 changes: 52 additions & 4 deletions docs/manual/mod/mod_authn_socache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,58 @@ AuthnCacheSOCache dbm
<p>Module developers should note that their modules must be enabled
for caching with <module>mod_authn_socache</module>. A single optional API function
<var>ap_authn_cache_store</var> is provided to cache credentials
a provider has just looked up or generated. Usage examples are
available in <a
href="http://svn.eu.apache.org/viewvc?view=revision&amp;revision=957072"
>r957072</a>, in which three authn providers are enabled for caching.</p>
a provider has just looked up or generated. It is declared in
<code>mod_auth.h</code>:</p>

<highlight language="c">
void ap_authn_cache_store(request_rec *r, const char *module,
const char *user, const char *realm,
const char *data);
</highlight>

<p>Here <var>module</var> is the name by which the provider is
listed in <directive module="mod_authn_socache"
>AuthnCacheProvideFor</directive>, <var>realm</var> is
<code>NULL</code> for basic authentication, and <var>data</var> is
the password hash, or the digest authentication hash, found for
<var>user</var>.</p>

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

<highlight language="c">
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);
}
</highlight>

<p>The providers in <module>mod_authn_file</module>,
<module>mod_authn_dbm</module>, and <module>mod_authn_dbd</module>
are enabled for caching in this way.</p>
</section>

<directivesynopsis>
Expand Down
6 changes: 5 additions & 1 deletion modules/aaa/mod_authn_socache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
33 changes: 21 additions & 12 deletions modules/cache/mod_socache_dbm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <unistd.h>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 "
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
46 changes: 43 additions & 3 deletions test/modules/aaa/env.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand All @@ -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")

Expand Down Expand Up @@ -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
Expand Down
Loading
Loading