mod_socache_dbm.c revision 0b310e2cb91c7ca69a95637c05fae2fb124d7fcf
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 * 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/* Use of the context structure must be thread-safe after the initial
0797faae937515a5225a36db4a1ec79480d2555cjorton * create/init; callers must hold the mutex. */
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *data_file;
0797faae937515a5225a36db4a1ec79480d2555cjorton /* Pool must only be used with the mutex held. */
0797faae937515a5225a36db4a1ec79480d2555cjorton * Support for DBM library
0797faae937515a5225a36db4a1ec79480d2555cjorton#define SSL_DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
0797faae937515a5225a36db4a1ec79480d2555cjorton/* ### this should use apr_dbm_usednames. */
0797faae937515a5225a36db4a1ec79480d2555cjorton#if !defined(SSL_DBM_FILE_SUFFIX_DIR) && !defined(SSL_DBM_FILE_SUFFIX_PAG)
0797faae937515a5225a36db4a1ec79480d2555cjorton#elif defined(__FreeBSD__) || (defined(DB_LOCK) && defined(DB_SHMEM))
2685f3814b77577ef7b2523442dab1ca88df1e41jortonstatic void socache_dbm_expire(ap_socache_instance_t *ctx, server_rec *s);
2685f3814b77577ef7b2523442dab1ca88df1e41jortonstatic void socache_dbm_remove(ap_socache_instance_t *ctx, server_rec *s,
2685f3814b77577ef7b2523442dab1ca88df1e41jortonstatic const char *socache_dbm_create(ap_socache_instance_t **context,
2685f3814b77577ef7b2523442dab1ca88df1e41jorton const char *arg,
0797faae937515a5225a36db4a1ec79480d2555cjorton return apr_psprintf(tmp, "Invalid cache file path %s", arg);
2685f3814b77577ef7b2523442dab1ca88df1e41jortonstatic apr_status_t socache_dbm_init(ap_socache_instance_t *ctx,
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton const char *namespace,
0797faae937515a5225a36db4a1ec79480d2555cjorton /* for the DBM we need the data file */
0797faae937515a5225a36db4a1ec79480d2555cjorton "SSLSessionCache required");
0797faae937515a5225a36db4a1ec79480d2555cjorton /* open it once to create it and to make sure it _can_ be created */
0797faae937515a5225a36db4a1ec79480d2555cjorton APR_DBM_RWCREATE, SSL_DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot create SSLSessionCache DBM file `%s'",
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton ctx->expiry_interval = (hints && hints->expiry_interval
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 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 if (chown(apr_pstrcat(p, ctx->data_file, ".db", NULL),
0797faae937515a5225a36db4a1ec79480d2555cjorton if (chown(apr_pstrcat(p, ctx->data_file, SSL_DBM_FILE_SUFFIX_PAG, NULL),
0797faae937515a5225a36db4a1ec79480d2555cjorton if (chown(apr_pstrcat(p, ctx->data_file, ".db", NULL),
2685f3814b77577ef7b2523442dab1ca88df1e41jortonstatic void socache_dbm_kill(ap_socache_instance_t *ctx, server_rec *s)
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));
072f7e449a76d28b580de6e89a1723713ab9adb1jortonstatic apr_status_t socache_dbm_store(ap_socache_instance_t *ctx, server_rec *s,
072f7e449a76d28b580de6e89a1723713ab9adb1jorton unsigned int nData)
0797faae937515a5225a36db4a1ec79480d2555cjorton /* be careful: do not try to store too much bytes in a DBM file! */
0797faae937515a5225a36db4a1ec79480d2555cjorton "data size too large for DBM session cache: %d >= %d",
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((idlen + nData) >= 950 /* at least less than approx. 1KB */) {
0797faae937515a5225a36db4a1ec79480d2555cjorton "data size too large for DBM session cache: %d >= %d",
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM key */
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM value */
0797faae937515a5225a36db4a1ec79480d2555cjorton "malloc error creating DBM value");
0797faae937515a5225a36db4a1ec79480d2555cjorton memcpy((char *)dbmval.dptr, &expiry, sizeof(time_t));
0797faae937515a5225a36db4a1ec79480d2555cjorton memcpy((char *)dbmval.dptr+sizeof(time_t), ucaData, nData);
0797faae937515a5225a36db4a1ec79480d2555cjorton /* and store it to the DBM file */
0797faae937515a5225a36db4a1ec79480d2555cjorton APR_DBM_RWCREATE, SSL_DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot open SSLSessionCache DBM file `%s' for writing "
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_store(dbm, dbmkey, dbmval)) != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot store SSL session to DBM file `%s'",
0797faae937515a5225a36db4a1ec79480d2555cjorton /* free temporary buffers */
0797faae937515a5225a36db4a1ec79480d2555cjorton /* allow the regular expiring to occur */
072f7e449a76d28b580de6e89a1723713ab9adb1jortonstatic apr_status_t socache_dbm_retrieve(ap_socache_instance_t *ctx, server_rec *s,
0797faae937515a5225a36db4a1ec79480d2555cjorton unsigned int nData;
0797faae937515a5225a36db4a1ec79480d2555cjorton /* allow the regular expiring to occur */
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM key and values */
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 if ((rc = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot open SSLSessionCache DBM file `%s' for reading "
0797faae937515a5225a36db4a1ec79480d2555cjorton if (dbmval.dptr == NULL || dbmval.dsize <= sizeof(time_t)) {
0797faae937515a5225a36db4a1ec79480d2555cjorton /* parse resulting data */
0797faae937515a5225a36db4a1ec79480d2555cjorton memcpy(dest, (char *)dbmval.dptr + sizeof(time_t), nData);
0797faae937515a5225a36db4a1ec79480d2555cjorton /* make sure the stuff is still not expired */
2685f3814b77577ef7b2523442dab1ca88df1e41jortonstatic void socache_dbm_remove(ap_socache_instance_t *ctx, server_rec *s,
0797faae937515a5225a36db4a1ec79480d2555cjorton /* create DBM key and values */
0797faae937515a5225a36db4a1ec79480d2555cjorton /* and delete it from the DBM file */
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot open SSLSessionCache DBM file `%s' for writing "
0797faae937515a5225a36db4a1ec79480d2555cjorton "(delete)",
2685f3814b77577ef7b2523442dab1ca88df1e41jortonstatic void socache_dbm_expire(ap_socache_instance_t *ctx, server_rec *s)
0797faae937515a5225a36db4a1ec79480d2555cjorton * make sure the expiration for still not-accessed session
0797faae937515a5225a36db4a1ec79480d2555cjorton * cache entries is done only from time to time
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton if (tNow < ctx->last_expiry + ctx->expiry_interval) {
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 /* allocate the key array in a memory sub pool */
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((keylist = apr_palloc(ctx->pool, sizeof(dbmkey)*KEYMAX)) == NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton /* pass 1: scan DBM database */
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot open SSLSessionCache DBM file `%s' for "
0797faae937515a5225a36db4a1ec79480d2555cjorton "scanning",
0797faae937515a5225a36db4a1ec79480d2555cjorton if (dbmval.dsize <= sizeof(time_t) || dbmval.dptr == NULL)
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((keylist[keyidx].dptr = apr_pmemdup(ctx->pool, dbmkey.dptr, dbmkey.dsize)) != NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton /* pass 2: delete expired elements */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot re-open SSLSessionCache DBM file `%s' for "
0797faae937515a5225a36db4a1ec79480d2555cjorton "expiring",
0797faae937515a5225a36db4a1ec79480d2555cjorton for (i = 0; i < keyidx; i++) {
0797faae937515a5225a36db4a1ec79480d2555cjorton "Inter-Process Session Cache (DBM) Expiry: "
0797faae937515a5225a36db4a1ec79480d2555cjorton "old: %d, new: %d, removed: %d",
2685f3814b77577ef7b2523442dab1ca88df1e41jortonstatic void socache_dbm_status(ap_socache_instance_t *ctx, request_rec *r,
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
0797faae937515a5225a36db4a1ec79480d2555cjorton "Cannot open SSLSessionCache DBM file `%s' for status "
0797faae937515a5225a36db4a1ec79480d2555cjorton "retrival",
0797faae937515a5225a36db4a1ec79480d2555cjorton * XXX - Check the return value of apr_dbm_firstkey, apr_dbm_fetch - TBD
0797faae937515a5225a36db4a1ec79480d2555cjorton for ( ; dbmkey.dptr != NULL; apr_dbm_nextkey(dbm, &dbmkey)) {
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);