mod_socache_dbm.c revision 8ba562720ad913bc899d15deba8b83dfe1065c0e
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher/* Licensed to the Apache Software Foundation (ASF) under one or more
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * contributor license agreements. See the NOTICE file distributed with
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * this work for additional information regarding copyright ownership.
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * The ASF licenses this file to You under the Apache License, Version 2.0
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * (the "License"); you may not use this file except in compliance with
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * the License. You may obtain a copy of the License at
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher *
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * http://www.apache.org/licenses/LICENSE-2.0
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher *
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * Unless required by applicable law or agreed to in writing, software
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * distributed under the License is distributed on an "AS IS" BASIS,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * See the License for the specific language governing permissions and
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * limitations under the License.
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include "httpd.h"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include "http_log.h"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include "http_request.h"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include "http_protocol.h"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include "http_config.h"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include "mpm_common.h"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include "apr.h"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include "apr_strings.h"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include "apr_time.h"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#define APR_WANT_STRFUNC
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include "apr_want.h"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include "apr_dbm.h"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#if APR_HAVE_UNISTD_H
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include <unistd.h>
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#endif
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include "ap_socache.h"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#if AP_NEED_SET_MUTEX_PERMS
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#include "unixd.h"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#endif
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher/* Use of the context structure must be thread-safe after the initial
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * create/init; callers must hold the mutex. */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstruct ap_socache_instance_t {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher const char *data_file;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* Pool must only be used with the mutex held. */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_pool_t *pool;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_time_t last_expiry;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_interval_time_t expiry_interval;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher};
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher/**
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * Support for DBM library
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#define DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#define DEFAULT_DBM_PREFIX "socache-dbm-"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher/* ### this should use apr_dbm_usednames. */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#if !defined(DBM_FILE_SUFFIX_DIR) && !defined(DBM_FILE_SUFFIX_PAG)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#if defined(DBM_SUFFIX)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#define DBM_FILE_SUFFIX_DIR DBM_SUFFIX
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#define DBM_FILE_SUFFIX_PAG DBM_SUFFIX
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#elif defined(__FreeBSD__) || (defined(DB_LOCK) && defined(DB_SHMEM))
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#define DBM_FILE_SUFFIX_DIR ".db"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#define DBM_FILE_SUFFIX_PAG ".db"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#else
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#define DBM_FILE_SUFFIX_DIR ".dir"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#define DBM_FILE_SUFFIX_PAG ".pag"
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#endif
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#endif
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic void socache_dbm_expire(ap_socache_instance_t *ctx, server_rec *s);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic apr_status_t socache_dbm_remove(ap_socache_instance_t *ctx,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher server_rec *s, const unsigned char *id,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher unsigned int idlen, apr_pool_t *p);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic const char *socache_dbm_create(ap_socache_instance_t **context,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher const char *arg,
8a1738f9379a1b8fb5c95c3df649e014ff5a1434Jan Zeleny apr_pool_t *tmp, apr_pool_t *p)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher{
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_socache_instance_t *ctx;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher *context = ctx = apr_pcalloc(p, sizeof *ctx);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (arg && *arg) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ctx->data_file = ap_server_root_relative(p, arg);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (!ctx->data_file) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return apr_psprintf(tmp, "Invalid cache file path %s", arg);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_pool_create(&ctx->pool, p);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return NULL;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher}
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#if AP_NEED_SET_MUTEX_PERMS
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic int try_chown(apr_pool_t *p, server_rec *s,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher const char *name, const char *suffix)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher{
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (suffix)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher name = apr_pstrcat(p, name, suffix, NULL);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (-1 == chown(name, ap_unixd_config.user_id,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher (gid_t)-1 /* no gid change */ ))
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (errno != ENOENT)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(errno), s, APLOGNO(00802)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "Can't change owner of %s", name);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return -1;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return 0;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher}
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#endif
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic apr_status_t socache_dbm_init(ap_socache_instance_t *ctx,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher const char *namespace,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher const struct ap_socache_hints *hints,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher server_rec *s, apr_pool_t *p)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher{
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_t *dbm;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_status_t rv;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* for the DBM we need the data file */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (ctx->data_file == NULL) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher const char *path = apr_pstrcat(p, DEFAULT_DBM_PREFIX, namespace,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher NULL);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ctx->data_file = ap_runtime_dir_relative(p, path);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (ctx->data_file == NULL) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00803)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "could not use default path '%s' for DBM socache",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher path);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return APR_EINVAL;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* open it once to create it and to make sure it _can_ be created */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_pool_clear(ctx->pool);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if ((rv = apr_dbm_open(&dbm, ctx->data_file,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher APR_DBM_RWCREATE, DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00804)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "Cannot create socache DBM file `%s'",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ctx->data_file);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return rv;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_close(dbm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ctx->expiry_interval = (hints && hints->expiry_interval
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ? hints->expiry_interval : apr_time_from_sec(30));
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#if AP_NEED_SET_MUTEX_PERMS
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /*
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * We have to make sure the Apache child processes have access to
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * the DBM file. But because there are brain-dead platforms where we
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * cannot exactly determine the suffixes we try all possibilities.
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (geteuid() == 0 /* is superuser */) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher try_chown(p, s, ctx->data_file, NULL);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (try_chown(p, s, ctx->data_file, DBM_FILE_SUFFIX_DIR))
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (try_chown(p, s, ctx->data_file, ".db"))
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher try_chown(p, s, ctx->data_file, ".dir");
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (try_chown(p, s, ctx->data_file, DBM_FILE_SUFFIX_PAG))
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (try_chown(p, s, ctx->data_file, ".db"))
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher try_chown(p, s, ctx->data_file, ".pag");
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#endif
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher socache_dbm_expire(ctx, s);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return APR_SUCCESS;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher}
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic void socache_dbm_destroy(ap_socache_instance_t *ctx, server_rec *s)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher{
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* the correct way */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher unlink(apr_pstrcat(ctx->pool, ctx->data_file, DBM_FILE_SUFFIX_DIR, NULL));
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher unlink(apr_pstrcat(ctx->pool, ctx->data_file, DBM_FILE_SUFFIX_PAG, NULL));
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* the additional ways to be sure */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".dir", NULL));
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".pag", NULL));
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".db", NULL));
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher unlink(ctx->data_file);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher}
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic apr_status_t socache_dbm_store(ap_socache_instance_t *ctx,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher server_rec *s, const unsigned char *id,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher unsigned int idlen, apr_time_t expiry,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher unsigned char *ucaData,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher unsigned int nData, apr_pool_t *pool)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher{
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_t *dbm;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_datum_t dbmkey;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_datum_t dbmval;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_status_t rv;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* be careful: do not try to store too much bytes in a DBM file! */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#ifdef PAIRMAX
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if ((idlen + nData) >= PAIRMAX) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00805)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "data size too large for DBM socache: %d >= %d",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher (idlen + nData), PAIRMAX);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return APR_ENOSPC;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#else
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if ((idlen + nData) >= 950 /* at least less than approx. 1KB */) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00806)
8a1738f9379a1b8fb5c95c3df649e014ff5a1434Jan Zeleny "data size too large for DBM socache: %d >= %d",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher (idlen + nData), 950);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return APR_ENOSPC;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#endif
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* create DBM key */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher dbmkey.dptr = (char *)id;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher dbmkey.dsize = idlen;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* create DBM value */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher dbmval.dsize = sizeof(apr_time_t) + nData;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher dbmval.dptr = (char *)ap_malloc(dbmval.dsize);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher memcpy((char *)dbmval.dptr, &expiry, sizeof(apr_time_t));
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher memcpy((char *)dbmval.dptr+sizeof(apr_time_t), ucaData, nData);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* and store it to the DBM file */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_pool_clear(ctx->pool);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if ((rv = apr_dbm_open(&dbm, ctx->data_file,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher APR_DBM_RWCREATE, DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00807)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "Cannot open socache DBM file `%s' for writing "
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "(store)",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ctx->data_file);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher free(dbmval.dptr);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return rv;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if ((rv = apr_dbm_store(dbm, dbmkey, dbmval)) != APR_SUCCESS) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00808)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "Cannot store socache object to DBM file `%s'",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ctx->data_file);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_close(dbm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher free(dbmval.dptr);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return rv;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_close(dbm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* free temporary buffers */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher free(dbmval.dptr);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* allow the regular expiring to occur */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher socache_dbm_expire(ctx, s);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return APR_SUCCESS;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher}
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic apr_status_t socache_dbm_retrieve(ap_socache_instance_t *ctx, server_rec *s,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher const unsigned char *id, unsigned int idlen,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher unsigned char *dest, unsigned int *destlen,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_pool_t *p)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher{
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_t *dbm;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_datum_t dbmkey;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_datum_t dbmval;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher unsigned int nData;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_time_t expiry;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_time_t now;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_status_t rc;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* allow the regular expiring to occur */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher socache_dbm_expire(ctx, s);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* create DBM key and values */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher dbmkey.dptr = (char *)id;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher dbmkey.dsize = idlen;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* and fetch it from the DBM file
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * XXX: Should we open the dbm against r->pool so the cleanup will
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * do the apr_dbm_close? This would make the code a bit cleaner.
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_pool_clear(ctx->pool);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if ((rc = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, rc, s, APLOGNO(00809)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "Cannot open socache DBM file `%s' for reading "
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "(fetch)",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ctx->data_file);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return rc;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher rc = apr_dbm_fetch(dbm, dbmkey, &dbmval);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (rc != APR_SUCCESS) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_close(dbm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return APR_NOTFOUND;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (dbmval.dptr == NULL || dbmval.dsize <= sizeof(apr_time_t)) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_close(dbm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return APR_EGENERAL;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* parse resulting data */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher nData = dbmval.dsize-sizeof(apr_time_t);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (nData > *destlen) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_close(dbm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return APR_ENOSPC;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher *destlen = nData;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher memcpy(&expiry, dbmval.dptr, sizeof(apr_time_t));
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher memcpy(dest, (char *)dbmval.dptr + sizeof(apr_time_t), nData);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_close(dbm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* make sure the stuff is still not expired */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher now = apr_time_now();
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (expiry <= now) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher socache_dbm_remove(ctx, s, id, idlen, p);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return APR_NOTFOUND;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return APR_SUCCESS;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher}
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic apr_status_t socache_dbm_remove(ap_socache_instance_t *ctx,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher server_rec *s, const unsigned char *id,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher unsigned int idlen, apr_pool_t *p)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher{
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_t *dbm;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_datum_t dbmkey;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_status_t rv;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* create DBM key and values */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher dbmkey.dptr = (char *)id;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher dbmkey.dsize = idlen;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* and delete it from the DBM file */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_pool_clear(ctx->pool);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00810)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "Cannot open socache DBM file `%s' for writing "
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "(delete)",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ctx->data_file);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return rv;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_delete(dbm, dbmkey);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_close(dbm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return APR_SUCCESS;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher}
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic void socache_dbm_expire(ap_socache_instance_t *ctx, server_rec *s)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher{
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_t *dbm;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_datum_t dbmkey;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_datum_t dbmval;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_time_t expiry;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher int elts = 0;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher int deleted = 0;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher int expired;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_datum_t *keylist;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher int keyidx;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher int i;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_time_t now;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_status_t rv;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /*
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * make sure the expiration for still not-accessed
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * socache entries is done only from time to time
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher now = apr_time_now();
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (now < ctx->last_expiry + ctx->expiry_interval) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ctx->last_expiry = now;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /*
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * Here we have to be very carefully: Not all DBM libraries are
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * smart enough to allow one to iterate over the elements and at the
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * same time delete expired ones. Some of them get totally crazy
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * while others have no problems. So we have to do it the slower but
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * more safe way: we first iterate over all elements and remember
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * those which have to be expired. Then in a second pass we delete
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * all those expired elements. Additionally we reopen the DBM file
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * to be really safe in state.
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher#define KEYMAX 1024
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher for (;;) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* allocate the key array in a memory sub pool */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_pool_clear(ctx->pool);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if ((keylist = apr_palloc(ctx->pool, sizeof(dbmkey)*KEYMAX)) == NULL) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher break;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* pass 1: scan DBM database */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher keyidx = 0;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00811)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "Cannot open socache DBM file `%s' for "
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "scanning",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ctx->data_file);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher break;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_firstkey(dbm, &dbmkey);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher while (dbmkey.dptr != NULL) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher elts++;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher expired = FALSE;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_fetch(dbm, dbmkey, &dbmval);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (dbmval.dsize <= sizeof(apr_time_t) || dbmval.dptr == NULL)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher expired = TRUE;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher else {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher memcpy(&expiry, dbmval.dptr, sizeof(apr_time_t));
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (expiry <= now)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher expired = TRUE;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (expired) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if ((keylist[keyidx].dptr = apr_pmemdup(ctx->pool, dbmkey.dptr, dbmkey.dsize)) != NULL) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher keylist[keyidx].dsize = dbmkey.dsize;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher keyidx++;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (keyidx == KEYMAX)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher break;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_nextkey(dbm, &dbmkey);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_close(dbm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /* pass 2: delete expired elements */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher DBM_FILE_MODE, ctx->pool) != APR_SUCCESS) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00812)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "Cannot re-open socache DBM file `%s' for "
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "expiring",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ctx->data_file);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher break;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher for (i = 0; i < keyidx; i++) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_delete(dbm, keylist[i]);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher deleted++;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_close(dbm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (keyidx < KEYMAX)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher break;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00813)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "DBM socache expiry: "
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "old: %d, new: %d, removed: %d",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher elts, elts-deleted, deleted);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher}
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic void socache_dbm_status(ap_socache_instance_t *ctx, request_rec *r,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher int flags)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher{
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_t *dbm;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_datum_t dbmkey;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_datum_t dbmval;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher int elts;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher long size;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher int avg;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_status_t rv;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher elts = 0;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher size = 0;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_pool_clear(ctx->pool);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00814)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "Cannot open socache DBM file `%s' for status "
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "retrival",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ctx->data_file);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /*
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * XXX - Check the return value of apr_dbm_firstkey, apr_dbm_fetch - TBD
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_firstkey(dbm, &dbmkey);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher for ( ; dbmkey.dptr != NULL; apr_dbm_nextkey(dbm, &dbmkey)) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_fetch(dbm, dbmkey, &dbmval);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (dbmval.dptr == NULL)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher continue;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher elts += 1;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher size += dbmval.dsize;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_close(dbm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (size > 0 && elts > 0)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher avg = (int)(size / (long)elts);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher else
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher avg = 0;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_rprintf(r, "cache type: <b>DBM</b>, maximum size: <b>unlimited</b><br>");
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_rprintf(r, "current entries: <b>%d</b>, current size: <b>%ld</b> bytes<br>", elts, size);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_rprintf(r, "average entry size: <b>%d</b> bytes<br>", avg);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher}
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic apr_status_t socache_dbm_iterate(ap_socache_instance_t *ctx,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher server_rec *s, void *userctx,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_socache_iterator_t *iterator,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_pool_t *pool)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher{
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_t *dbm;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_datum_t dbmkey;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_datum_t dbmval;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_time_t expiry;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher int expired;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_time_t now;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_status_t rv;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher /*
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher * make sure the expired records are omitted
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher */
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher now = apr_time_now();
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00815)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "Cannot open socache DBM file `%s' for "
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "iterating", ctx->data_file);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return rv;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher rv = apr_dbm_firstkey(dbm, &dbmkey);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher while (rv == APR_SUCCESS && dbmkey.dptr != NULL) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher expired = FALSE;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_fetch(dbm, dbmkey, &dbmval);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (dbmval.dsize <= sizeof(apr_time_t) || dbmval.dptr == NULL)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher expired = TRUE;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher else {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher memcpy(&expiry, dbmval.dptr, sizeof(apr_time_t));
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (expiry <= now)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher expired = TRUE;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
1ea2e8bd370e0dc2f2c3fa09232cf67082ef748dStephen Gallagher if (!expired) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher rv = iterator(ctx, s, userctx,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher (unsigned char *)dbmkey.dptr, dbmkey.dsize,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher (unsigned char *)dbmval.dptr + sizeof(apr_time_t),
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher dbmval.dsize - sizeof(apr_time_t), pool);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, s, APLOGNO(00816)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "dbm `%s' entry iterated", ctx->data_file);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (rv != APR_SUCCESS)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return rv;
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher rv = apr_dbm_nextkey(dbm, &dbmkey);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher }
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher apr_dbm_close(dbm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher if (rv != APR_SUCCESS && rv != APR_EOF) {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00817)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher "Failure reading first/next socache DBM file `%s' record",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ctx->data_file);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher return rv;
9b72b00ebcfd6225a4e139619c8e18d44a448f87Stephen Gallagher }
9b72b00ebcfd6225a4e139619c8e18d44a448f87Stephen Gallagher return APR_SUCCESS;
9b72b00ebcfd6225a4e139619c8e18d44a448f87Stephen Gallagher}
9b72b00ebcfd6225a4e139619c8e18d44a448f87Stephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic const ap_socache_provider_t socache_dbm = {
9b72b00ebcfd6225a4e139619c8e18d44a448f87Stephen Gallagher "dbm",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher AP_SOCACHE_FLAG_NOTMPSAFE,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher socache_dbm_create,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher socache_dbm_init,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher socache_dbm_destroy,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher socache_dbm_store,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher socache_dbm_retrieve,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher socache_dbm_remove,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher socache_dbm_status,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher socache_dbm_iterate
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher};
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagherstatic void register_hooks(apr_pool_t *p)
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher{
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher ap_register_provider(p, AP_SOCACHE_PROVIDER_GROUP, "dbm",
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher AP_SOCACHE_PROVIDER_VERSION,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher &socache_dbm);
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher}
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen GallagherAP_DECLARE_MODULE(socache_dbm) = {
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher STANDARD20_MODULE_STUFF,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher NULL, NULL, NULL, NULL, NULL,
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher register_hooks
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher};
e134a6af42102c8d865e82bf89e0b8c5a40fb5faStephen Gallagher