mod_socache_dbm.c revision 0797faae937515a5225a36db4a1ec79480d2555c
0797faae937515a5225a36db4a1ec79480d2555cjorton/* Licensed to the Apache Software Foundation (ASF) under one or more
0797faae937515a5225a36db4a1ec79480d2555cjorton * contributor license agreements. See the NOTICE file distributed with
0797faae937515a5225a36db4a1ec79480d2555cjorton * this work for additional information regarding copyright ownership.
0797faae937515a5225a36db4a1ec79480d2555cjorton * The ASF licenses this file to You under the Apache License, Version 2.0
0797faae937515a5225a36db4a1ec79480d2555cjorton * (the "License"); you may not use this file except in compliance with
0797faae937515a5225a36db4a1ec79480d2555cjorton * the License. You may obtain a copy of the License at
0797faae937515a5225a36db4a1ec79480d2555cjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * http://www.apache.org/licenses/LICENSE-2.0
0797faae937515a5225a36db4a1ec79480d2555cjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * Unless required by applicable law or agreed to in writing, software
0797faae937515a5225a36db4a1ec79480d2555cjorton * distributed under the License is distributed on an "AS IS" BASIS,
0797faae937515a5225a36db4a1ec79480d2555cjorton * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0797faae937515a5225a36db4a1ec79480d2555cjorton * See the License for the specific language governing permissions and
0797faae937515a5225a36db4a1ec79480d2555cjorton * limitations under the License.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "httpd.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "http_log.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "http_request.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "http_protocol.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "http_config.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "mpm_common.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "apr.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "apr_strings.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "apr_time.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#define APR_WANT_STRFUNC
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "apr_want.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "apr_dbm.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#if APR_HAVE_UNISTD_H
0797faae937515a5225a36db4a1ec79480d2555cjorton#include <unistd.h>
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "ap_socache.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton/* Use of the context structure must be thread-safe after the initial
0797faae937515a5225a36db4a1ec79480d2555cjorton * create/init; callers must hold the mutex. */
0797faae937515a5225a36db4a1ec79480d2555cjortonstruct context {
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *data_file;
0797faae937515a5225a36db4a1ec79480d2555cjorton /* Pool must only be used with the mutex held. */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_t *pool;
0797faae937515a5225a36db4a1ec79480d2555cjorton time_t last_expiry;
0797faae937515a5225a36db4a1ec79480d2555cjorton time_t timeout;
0797faae937515a5225a36db4a1ec79480d2555cjorton};
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton/**
0797faae937515a5225a36db4a1ec79480d2555cjorton * Support for DBM library
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton#define SSL_DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton/* ### this should use apr_dbm_usednames. */
0797faae937515a5225a36db4a1ec79480d2555cjorton#if !defined(SSL_DBM_FILE_SUFFIX_DIR) && !defined(SSL_DBM_FILE_SUFFIX_PAG)
0797faae937515a5225a36db4a1ec79480d2555cjorton#if defined(DBM_SUFFIX)
0797faae937515a5225a36db4a1ec79480d2555cjorton#define SSL_DBM_FILE_SUFFIX_DIR DBM_SUFFIX
0797faae937515a5225a36db4a1ec79480d2555cjorton#define SSL_DBM_FILE_SUFFIX_PAG DBM_SUFFIX
0797faae937515a5225a36db4a1ec79480d2555cjorton#elif defined(__FreeBSD__) || (defined(DB_LOCK) && defined(DB_SHMEM))
0797faae937515a5225a36db4a1ec79480d2555cjorton#define SSL_DBM_FILE_SUFFIX_DIR ".db"
0797faae937515a5225a36db4a1ec79480d2555cjorton#define SSL_DBM_FILE_SUFFIX_PAG ".db"
0797faae937515a5225a36db4a1ec79480d2555cjorton#else
0797faae937515a5225a36db4a1ec79480d2555cjorton#define SSL_DBM_FILE_SUFFIX_DIR ".dir"
0797faae937515a5225a36db4a1ec79480d2555cjorton#define SSL_DBM_FILE_SUFFIX_PAG ".pag"
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void socache_dbm_expire(struct context *ctx, server_rec *s);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void socache_dbm_remove(void *context, server_rec *s,
0797faae937515a5225a36db4a1ec79480d2555cjorton const unsigned char *id, unsigned int idlen,
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_t *p);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic const char *socache_dbm_create(void **context, const char *arg,
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_t *tmp, apr_pool_t *p)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton struct context *ctx;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton *context = ctx = apr_pcalloc(p, sizeof *ctx);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file = ap_server_root_relative(p, arg);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (!ctx->data_file) {
0797faae937515a5225a36db4a1ec79480d2555cjorton return apr_psprintf(tmp, "Invalid cache file path %s", arg);
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->timeout = 30; /* ### take as hint in _init */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_create(&ctx->pool, p);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return NULL;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic apr_status_t socache_dbm_init(void *context, server_rec *s, apr_pool_t *p)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton struct context *ctx = context;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_t *dbm;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_status_t rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* for the DBM we need the data file */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (ctx->data_file == NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
0797faae937515a5225a36db4a1ec79480d2555cjorton "SSLSessionCache required");
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_EINVAL;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* open it once to create it and to make sure it _can_ be created */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_clear(ctx->pool);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_open(&dbm, ctx->data_file,
0797faae937515a5225a36db4a1ec79480d2555cjorton APR_DBM_RWCREATE, SSL_DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot create SSLSessionCache DBM file `%s'",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton return rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * We have to make sure the Apache child processes have access to
0797faae937515a5225a36db4a1ec79480d2555cjorton * the DBM file. But because there are brain-dead platforms where we
0797faae937515a5225a36db4a1ec79480d2555cjorton * cannot exactly determine the suffixes we try all possibilities.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (geteuid() == 0 /* is superuser */) {
0797faae937515a5225a36db4a1ec79480d2555cjorton chown(ctx->data_file, unixd_config.user_id, -1 /* no gid change */);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (chown(apr_pstrcat(p, ctx->data_file, SSL_DBM_FILE_SUFFIX_DIR, NULL),
0797faae937515a5225a36db4a1ec79480d2555cjorton unixd_config.user_id, -1) == -1) {
0797faae937515a5225a36db4a1ec79480d2555cjorton if (chown(apr_pstrcat(p, ctx->data_file, ".db", NULL),
0797faae937515a5225a36db4a1ec79480d2555cjorton unixd_config.user_id, -1) == -1)
0797faae937515a5225a36db4a1ec79480d2555cjorton chown(apr_pstrcat(p, ctx->data_file, ".dir", NULL),
0797faae937515a5225a36db4a1ec79480d2555cjorton unixd_config.user_id, -1);
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton if (chown(apr_pstrcat(p, ctx->data_file, SSL_DBM_FILE_SUFFIX_PAG, NULL),
0797faae937515a5225a36db4a1ec79480d2555cjorton unixd_config.user_id, -1) == -1) {
0797faae937515a5225a36db4a1ec79480d2555cjorton if (chown(apr_pstrcat(p, ctx->data_file, ".db", NULL),
0797faae937515a5225a36db4a1ec79480d2555cjorton unixd_config.user_id, -1) == -1)
0797faae937515a5225a36db4a1ec79480d2555cjorton chown(apr_pstrcat(p, ctx->data_file, ".pag", NULL),
0797faae937515a5225a36db4a1ec79480d2555cjorton unixd_config.user_id, -1);
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_expire(ctx, s);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_SUCCESS;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void socache_dbm_kill(void *context, server_rec *s)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton struct context *ctx = context;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* the correct way */
0797faae937515a5225a36db4a1ec79480d2555cjorton unlink(apr_pstrcat(ctx->pool, ctx->data_file, SSL_DBM_FILE_SUFFIX_DIR, NULL));
0797faae937515a5225a36db4a1ec79480d2555cjorton unlink(apr_pstrcat(ctx->pool, ctx->data_file, SSL_DBM_FILE_SUFFIX_PAG, NULL));
0797faae937515a5225a36db4a1ec79480d2555cjorton /* the additional ways to be sure */
0797faae937515a5225a36db4a1ec79480d2555cjorton unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".dir", NULL));
0797faae937515a5225a36db4a1ec79480d2555cjorton unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".pag", NULL));
0797faae937515a5225a36db4a1ec79480d2555cjorton unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".db", NULL));
0797faae937515a5225a36db4a1ec79480d2555cjorton unlink(ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic apr_status_t socache_dbm_store(void *context, server_rec *s,
0797faae937515a5225a36db4a1ec79480d2555cjorton const unsigned char *id, unsigned int idlen,
0797faae937515a5225a36db4a1ec79480d2555cjorton time_t expiry,
0797faae937515a5225a36db4a1ec79480d2555cjorton unsigned char *ucaData, unsigned int nData)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton struct context *ctx = context;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_t *dbm;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmkey;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmval;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_status_t rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* be careful: do not try to store too much bytes in a DBM file! */
0797faae937515a5225a36db4a1ec79480d2555cjorton#ifdef PAIRMAX
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((idlen + nData) >= PAIRMAX) {
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
0797faae937515a5225a36db4a1ec79480d2555cjorton "data size too large for DBM session cache: %d >= %d",
0797faae937515a5225a36db4a1ec79480d2555cjorton (idlen + nData), PAIRMAX);
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_ENOSPC;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton#else
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((idlen + nData) >= 950 /* at least less than approx. 1KB */) {
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
0797faae937515a5225a36db4a1ec79480d2555cjorton "data size too large for DBM session cache: %d >= %d",
0797faae937515a5225a36db4a1ec79480d2555cjorton (idlen + nData), 950);
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_ENOSPC;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM key */
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmkey.dptr = (char *)id;
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmkey.dsize = idlen;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM value */
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmval.dsize = sizeof(time_t) + nData;
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmval.dptr = (char *)malloc(dbmval.dsize);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (dbmval.dptr == NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
0797faae937515a5225a36db4a1ec79480d2555cjorton "malloc error creating DBM value");
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_ENOMEM;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton memcpy((char *)dbmval.dptr, &expiry, sizeof(time_t));
0797faae937515a5225a36db4a1ec79480d2555cjorton memcpy((char *)dbmval.dptr+sizeof(time_t), ucaData, nData);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* and store it to the DBM file */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_clear(ctx->pool);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_open(&dbm, ctx->data_file,
0797faae937515a5225a36db4a1ec79480d2555cjorton APR_DBM_RWCREATE, SSL_DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot open SSLSessionCache DBM file `%s' for writing "
0797faae937515a5225a36db4a1ec79480d2555cjorton "(store)",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton free(dbmval.dptr);
0797faae937515a5225a36db4a1ec79480d2555cjorton return rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_store(dbm, dbmkey, dbmval)) != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot store SSL session to DBM file `%s'",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton free(dbmval.dptr);
0797faae937515a5225a36db4a1ec79480d2555cjorton return rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* free temporary buffers */
0797faae937515a5225a36db4a1ec79480d2555cjorton free(dbmval.dptr);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* allow the regular expiring to occur */
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_expire(ctx, s);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_SUCCESS;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic apr_status_t socache_dbm_retrieve(void *context, server_rec *s,
0797faae937515a5225a36db4a1ec79480d2555cjorton const unsigned char *id, unsigned int idlen,
0797faae937515a5225a36db4a1ec79480d2555cjorton unsigned char *dest, unsigned int *destlen,
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_t *p)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton struct context *ctx = context;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_t *dbm;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmkey;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmval;
0797faae937515a5225a36db4a1ec79480d2555cjorton unsigned int nData;
0797faae937515a5225a36db4a1ec79480d2555cjorton time_t expiry;
0797faae937515a5225a36db4a1ec79480d2555cjorton time_t now;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_status_t rc;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* allow the regular expiring to occur */
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_expire(ctx, s);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM key and values */
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmkey.dptr = (char *)id;
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmkey.dsize = idlen;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* and fetch it from the DBM file
0797faae937515a5225a36db4a1ec79480d2555cjorton * XXX: Should we open the dbm against r->pool so the cleanup will
0797faae937515a5225a36db4a1ec79480d2555cjorton * do the apr_dbm_close? This would make the code a bit cleaner.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_clear(ctx->pool);
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rc = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
0797faae937515a5225a36db4a1ec79480d2555cjorton SSL_DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_error(APLOG_MARK, APLOG_ERR, rc, s,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot open SSLSessionCache DBM file `%s' for reading "
0797faae937515a5225a36db4a1ec79480d2555cjorton "(fetch)",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton return rc;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton rc = apr_dbm_fetch(dbm, dbmkey, &dbmval);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (rc != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton return rc;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton if (dbmval.dptr == NULL || dbmval.dsize <= sizeof(time_t)) {
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_EGENERAL;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* parse resulting data */
0797faae937515a5225a36db4a1ec79480d2555cjorton nData = dbmval.dsize-sizeof(time_t);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (nData > *destlen) {
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_ENOSPC;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton *destlen = nData;
0797faae937515a5225a36db4a1ec79480d2555cjorton memcpy(&expiry, dbmval.dptr, sizeof(time_t));
0797faae937515a5225a36db4a1ec79480d2555cjorton memcpy(dest, (char *)dbmval.dptr + sizeof(time_t), nData);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* make sure the stuff is still not expired */
0797faae937515a5225a36db4a1ec79480d2555cjorton now = time(NULL);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (expiry <= now) {
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_remove(ctx, s, id, idlen, p);
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_NOTFOUND;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return APR_SUCCESS;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void socache_dbm_remove(void *context, server_rec *s,
0797faae937515a5225a36db4a1ec79480d2555cjorton const unsigned char *id, unsigned int idlen,
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_t *p)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton struct context *ctx = context;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_t *dbm;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmkey;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_status_t rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM key and values */
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmkey.dptr = (char *)id;
0797faae937515a5225a36db4a1ec79480d2555cjorton dbmkey.dsize = idlen;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* and delete it from the DBM file */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_clear(ctx->pool);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
0797faae937515a5225a36db4a1ec79480d2555cjorton SSL_DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot open SSLSessionCache DBM file `%s' for writing "
0797faae937515a5225a36db4a1ec79480d2555cjorton "(delete)",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton return;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_delete(dbm, dbmkey);
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void socache_dbm_expire(struct context *ctx, server_rec *s)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_t *dbm;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmkey;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmval;
0797faae937515a5225a36db4a1ec79480d2555cjorton time_t tExpiresAt;
0797faae937515a5225a36db4a1ec79480d2555cjorton int nElements = 0;
0797faae937515a5225a36db4a1ec79480d2555cjorton int nDeleted = 0;
0797faae937515a5225a36db4a1ec79480d2555cjorton int bDelete;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t *keylist;
0797faae937515a5225a36db4a1ec79480d2555cjorton int keyidx;
0797faae937515a5225a36db4a1ec79480d2555cjorton int i;
0797faae937515a5225a36db4a1ec79480d2555cjorton time_t tNow;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_status_t rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * make sure the expiration for still not-accessed session
0797faae937515a5225a36db4a1ec79480d2555cjorton * cache entries is done only from time to time
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton tNow = time(NULL);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if (tNow < ctx->last_expiry + ctx->timeout) {
0797faae937515a5225a36db4a1ec79480d2555cjorton return;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->last_expiry = tNow;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * Here we have to be very carefully: Not all DBM libraries are
0797faae937515a5225a36db4a1ec79480d2555cjorton * smart enough to allow one to iterate over the elements and at the
0797faae937515a5225a36db4a1ec79480d2555cjorton * same time delete expired ones. Some of them get totally crazy
0797faae937515a5225a36db4a1ec79480d2555cjorton * while others have no problems. So we have to do it the slower but
0797faae937515a5225a36db4a1ec79480d2555cjorton * more safe way: we first iterate over all elements and remember
0797faae937515a5225a36db4a1ec79480d2555cjorton * those which have to be expired. Then in a second pass we delete
0797faae937515a5225a36db4a1ec79480d2555cjorton * all those expired elements. Additionally we reopen the DBM file
0797faae937515a5225a36db4a1ec79480d2555cjorton * to be really safe in state.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#define KEYMAX 1024
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton for (;;) {
0797faae937515a5225a36db4a1ec79480d2555cjorton /* allocate the key array in a memory sub pool */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_clear(ctx->pool);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((keylist = apr_palloc(ctx->pool, sizeof(dbmkey)*KEYMAX)) == NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton break;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* pass 1: scan DBM database */
0797faae937515a5225a36db4a1ec79480d2555cjorton keyidx = 0;
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
0797faae937515a5225a36db4a1ec79480d2555cjorton SSL_DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot open SSLSessionCache DBM file `%s' for "
0797faae937515a5225a36db4a1ec79480d2555cjorton "scanning",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton break;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_firstkey(dbm, &dbmkey);
0797faae937515a5225a36db4a1ec79480d2555cjorton while (dbmkey.dptr != NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton nElements++;
0797faae937515a5225a36db4a1ec79480d2555cjorton bDelete = FALSE;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_fetch(dbm, dbmkey, &dbmval);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (dbmval.dsize <= sizeof(time_t) || dbmval.dptr == NULL)
0797faae937515a5225a36db4a1ec79480d2555cjorton bDelete = TRUE;
0797faae937515a5225a36db4a1ec79480d2555cjorton else {
0797faae937515a5225a36db4a1ec79480d2555cjorton memcpy(&tExpiresAt, dbmval.dptr, sizeof(time_t));
0797faae937515a5225a36db4a1ec79480d2555cjorton if (tExpiresAt <= tNow)
0797faae937515a5225a36db4a1ec79480d2555cjorton bDelete = TRUE;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton if (bDelete) {
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((keylist[keyidx].dptr = apr_pmemdup(ctx->pool, dbmkey.dptr, dbmkey.dsize)) != NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton keylist[keyidx].dsize = dbmkey.dsize;
0797faae937515a5225a36db4a1ec79480d2555cjorton keyidx++;
0797faae937515a5225a36db4a1ec79480d2555cjorton if (keyidx == KEYMAX)
0797faae937515a5225a36db4a1ec79480d2555cjorton break;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_nextkey(dbm, &dbmkey);
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* pass 2: delete expired elements */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
0797faae937515a5225a36db4a1ec79480d2555cjorton SSL_DBM_FILE_MODE, ctx->pool) != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot re-open SSLSessionCache DBM file `%s' for "
0797faae937515a5225a36db4a1ec79480d2555cjorton "expiring",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton break;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton for (i = 0; i < keyidx; i++) {
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_delete(dbm, keylist[i]);
0797faae937515a5225a36db4a1ec79480d2555cjorton nDeleted++;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if (keyidx < KEYMAX)
0797faae937515a5225a36db4a1ec79480d2555cjorton break;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Inter-Process Session Cache (DBM) Expiry: "
0797faae937515a5225a36db4a1ec79480d2555cjorton "old: %d, new: %d, removed: %d",
0797faae937515a5225a36db4a1ec79480d2555cjorton nElements, nElements-nDeleted, nDeleted);
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void socache_dbm_status(void *context, request_rec *r, int flags)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton struct context *ctx = context;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_t *dbm;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmkey;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_datum_t dbmval;
0797faae937515a5225a36db4a1ec79480d2555cjorton int nElem;
0797faae937515a5225a36db4a1ec79480d2555cjorton int nSize;
0797faae937515a5225a36db4a1ec79480d2555cjorton int nAverage;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_status_t rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton nElem = 0;
0797faae937515a5225a36db4a1ec79480d2555cjorton nSize = 0;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_pool_clear(ctx->pool);
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
0797faae937515a5225a36db4a1ec79480d2555cjorton SSL_DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot open SSLSessionCache DBM file `%s' for status "
0797faae937515a5225a36db4a1ec79480d2555cjorton "retrival",
0797faae937515a5225a36db4a1ec79480d2555cjorton ctx->data_file);
0797faae937515a5225a36db4a1ec79480d2555cjorton return;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * XXX - Check the return value of apr_dbm_firstkey, apr_dbm_fetch - TBD
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_firstkey(dbm, &dbmkey);
0797faae937515a5225a36db4a1ec79480d2555cjorton for ( ; dbmkey.dptr != NULL; apr_dbm_nextkey(dbm, &dbmkey)) {
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_fetch(dbm, dbmkey, &dbmval);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (dbmval.dptr == NULL)
0797faae937515a5225a36db4a1ec79480d2555cjorton continue;
0797faae937515a5225a36db4a1ec79480d2555cjorton nElem += 1;
0797faae937515a5225a36db4a1ec79480d2555cjorton nSize += dbmval.dsize;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_dbm_close(dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (nSize > 0 && nElem > 0)
0797faae937515a5225a36db4a1ec79480d2555cjorton nAverage = nSize / nElem;
0797faae937515a5225a36db4a1ec79480d2555cjorton else
0797faae937515a5225a36db4a1ec79480d2555cjorton nAverage = 0;
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_rprintf(r, "cache type: <b>DBM</b>, maximum size: <b>unlimited</b><br>");
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_rprintf(r, "current sessions: <b>%d</b>, current size: <b>%d</b> bytes<br>", nElem, nSize);
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_rprintf(r, "average session size: <b>%d</b> bytes<br>", nAverage);
0797faae937515a5225a36db4a1ec79480d2555cjorton return;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic const ap_socache_provider_t socache_dbm = {
0797faae937515a5225a36db4a1ec79480d2555cjorton "dbm",
0797faae937515a5225a36db4a1ec79480d2555cjorton AP_SOCACHE_FLAG_NOTMPSAFE,
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_create,
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_init,
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_kill,
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_store,
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_retrieve,
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_remove,
0797faae937515a5225a36db4a1ec79480d2555cjorton socache_dbm_status
0797faae937515a5225a36db4a1ec79480d2555cjorton};
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void register_hooks(apr_pool_t *p)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_register_provider(p, AP_SOCACHE_PROVIDER_GROUP, "dbm",
0797faae937515a5225a36db4a1ec79480d2555cjorton AP_SOCACHE_PROVIDER_VERSION,
0797faae937515a5225a36db4a1ec79480d2555cjorton &socache_dbm);
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonconst module AP_MODULE_DECLARE_DATA socache_dbm_module = {
0797faae937515a5225a36db4a1ec79480d2555cjorton STANDARD20_MODULE_STUFF,
0797faae937515a5225a36db4a1ec79480d2555cjorton NULL, NULL, NULL, NULL, NULL,
0797faae937515a5225a36db4a1ec79480d2555cjorton register_hooks
0797faae937515a5225a36db4a1ec79480d2555cjorton};