1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "http_request.h"
20 #include "http_protocol.h"
21 #include "http_config.h"
22 #include "mpm_common.h"
25 #include "apr_strings.h"
27 #define APR_WANT_STRFUNC
35 #include "ap_socache.h"
37 #if AP_NEED_SET_MUTEX_PERMS
41 /* Use of the context structure must be thread-safe after the initial
42 * create/init; callers must hold the mutex. */
43 struct ap_socache_instance_t
{
44 const char *data_file
;
45 /* Pool must only be used with the mutex held. */
48 time_t expiry_interval
;
52 * Support for DBM library
54 #define SSL_DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
56 /* Check for definition of DEFAULT_REL_RUNTIMEDIR */
57 #ifndef DEFAULT_REL_RUNTIMEDIR
58 #define DEFAULT_DBM_PREFIX "logs/socache-dbm-"
60 #define DEFAULT_DBM_PREFIX DEFAULT_REL_RUNTIMEDIR "/socache-dbm-"
63 /* ### this should use apr_dbm_usednames. */
64 #if !defined(SSL_DBM_FILE_SUFFIX_DIR) && !defined(SSL_DBM_FILE_SUFFIX_PAG)
65 #if defined(DBM_SUFFIX)
66 #define SSL_DBM_FILE_SUFFIX_DIR DBM_SUFFIX
67 #define SSL_DBM_FILE_SUFFIX_PAG DBM_SUFFIX
68 #elif defined(__FreeBSD__) || (defined(DB_LOCK) && defined(DB_SHMEM))
69 #define SSL_DBM_FILE_SUFFIX_DIR ".db"
70 #define SSL_DBM_FILE_SUFFIX_PAG ".db"
72 #define SSL_DBM_FILE_SUFFIX_DIR ".dir"
73 #define SSL_DBM_FILE_SUFFIX_PAG ".pag"
77 static void socache_dbm_expire(ap_socache_instance_t
*ctx
, server_rec
*s
);
79 static apr_status_t
socache_dbm_remove(ap_socache_instance_t
*ctx
,
80 server_rec
*s
, const unsigned char *id
,
81 unsigned int idlen
, apr_pool_t
*p
);
83 static const char *socache_dbm_create(ap_socache_instance_t
**context
,
85 apr_pool_t
*tmp
, apr_pool_t
*p
)
87 ap_socache_instance_t
*ctx
;
89 *context
= ctx
= apr_pcalloc(p
, sizeof *ctx
);
92 ctx
->data_file
= ap_server_root_relative(p
, arg
);
93 if (!ctx
->data_file
) {
94 return apr_psprintf(tmp
, "Invalid cache file path %s", arg
);
98 apr_pool_create(&ctx
->pool
, p
);
103 static apr_status_t
socache_dbm_init(ap_socache_instance_t
*ctx
,
104 const char *namespace,
105 const struct ap_socache_hints
*hints
,
106 server_rec
*s
, apr_pool_t
*p
)
111 /* for the DBM we need the data file */
112 if (ctx
->data_file
== NULL
) {
113 const char *path
= apr_pstrcat(p
, DEFAULT_DBM_PREFIX
, namespace,
116 ctx
->data_file
= ap_server_root_relative(p
, path
);
118 if (ctx
->data_file
== NULL
) {
119 ap_log_error(APLOG_MARK
, APLOG_ERR
, 0, s
,
120 "could not use default path '%s' for DBM socache",
126 /* open it once to create it and to make sure it _can_ be created */
127 apr_pool_clear(ctx
->pool
);
129 if ((rv
= apr_dbm_open(&dbm
, ctx
->data_file
,
130 APR_DBM_RWCREATE
, SSL_DBM_FILE_MODE
, ctx
->pool
)) != APR_SUCCESS
) {
131 ap_log_error(APLOG_MARK
, APLOG_ERR
, rv
, s
,
132 "Cannot create SSLSessionCache DBM file `%s'",
138 ctx
->expiry_interval
= (hints
&& hints
->expiry_interval
139 ? hints
->expiry_interval
: 30);
141 #if AP_NEED_SET_MUTEX_PERMS
143 * We have to make sure the Apache child processes have access to
144 * the DBM file. But because there are brain-dead platforms where we
145 * cannot exactly determine the suffixes we try all possibilities.
147 if (geteuid() == 0 /* is superuser */) {
148 chown(ctx
->data_file
, ap_unixd_config
.user_id
, -1 /* no gid change */);
149 if (chown(apr_pstrcat(p
, ctx
->data_file
, SSL_DBM_FILE_SUFFIX_DIR
, NULL
),
150 ap_unixd_config
.user_id
, -1) == -1) {
151 if (chown(apr_pstrcat(p
, ctx
->data_file
, ".db", NULL
),
152 ap_unixd_config
.user_id
, -1) == -1)
153 chown(apr_pstrcat(p
, ctx
->data_file
, ".dir", NULL
),
154 ap_unixd_config
.user_id
, -1);
156 if (chown(apr_pstrcat(p
, ctx
->data_file
, SSL_DBM_FILE_SUFFIX_PAG
, NULL
),
157 ap_unixd_config
.user_id
, -1) == -1) {
158 if (chown(apr_pstrcat(p
, ctx
->data_file
, ".db", NULL
),
159 ap_unixd_config
.user_id
, -1) == -1)
160 chown(apr_pstrcat(p
, ctx
->data_file
, ".pag", NULL
),
161 ap_unixd_config
.user_id
, -1);
165 socache_dbm_expire(ctx
, s
);
170 static void socache_dbm_kill(ap_socache_instance_t
*ctx
, server_rec
*s
)
172 /* the correct way */
173 unlink(apr_pstrcat(ctx
->pool
, ctx
->data_file
, SSL_DBM_FILE_SUFFIX_DIR
, NULL
));
174 unlink(apr_pstrcat(ctx
->pool
, ctx
->data_file
, SSL_DBM_FILE_SUFFIX_PAG
, NULL
));
175 /* the additional ways to be sure */
176 unlink(apr_pstrcat(ctx
->pool
, ctx
->data_file
, ".dir", NULL
));
177 unlink(apr_pstrcat(ctx
->pool
, ctx
->data_file
, ".pag", NULL
));
178 unlink(apr_pstrcat(ctx
->pool
, ctx
->data_file
, ".db", NULL
));
179 unlink(ctx
->data_file
);
184 static apr_status_t
socache_dbm_store(ap_socache_instance_t
*ctx
,
185 server_rec
*s
, const unsigned char *id
,
186 unsigned int idlen
, time_t expiry
,
187 unsigned char *ucaData
,
188 unsigned int nData
, apr_pool_t
*pool
)
195 /* be careful: do not try to store too much bytes in a DBM file! */
197 if ((idlen
+ nData
) >= PAIRMAX
) {
198 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, 0, s
,
199 "data size too large for DBM session cache: %d >= %d",
200 (idlen
+ nData
), PAIRMAX
);
204 if ((idlen
+ nData
) >= 950 /* at least less than approx. 1KB */) {
205 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, 0, s
,
206 "data size too large for DBM session cache: %d >= %d",
207 (idlen
+ nData
), 950);
213 dbmkey
.dptr
= (char *)id
;
214 dbmkey
.dsize
= idlen
;
216 /* create DBM value */
217 dbmval
.dsize
= sizeof(time_t) + nData
;
218 dbmval
.dptr
= (char *)malloc(dbmval
.dsize
);
219 if (dbmval
.dptr
== NULL
) {
220 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, 0, s
,
221 "malloc error creating DBM value");
224 memcpy((char *)dbmval
.dptr
, &expiry
, sizeof(time_t));
225 memcpy((char *)dbmval
.dptr
+sizeof(time_t), ucaData
, nData
);
227 /* and store it to the DBM file */
228 apr_pool_clear(ctx
->pool
);
230 if ((rv
= apr_dbm_open(&dbm
, ctx
->data_file
,
231 APR_DBM_RWCREATE
, SSL_DBM_FILE_MODE
, ctx
->pool
)) != APR_SUCCESS
) {
232 ap_log_error(APLOG_MARK
, APLOG_ERR
, rv
, s
,
233 "Cannot open SSLSessionCache DBM file `%s' for writing "
239 if ((rv
= apr_dbm_store(dbm
, dbmkey
, dbmval
)) != APR_SUCCESS
) {
240 ap_log_error(APLOG_MARK
, APLOG_ERR
, rv
, s
,
241 "Cannot store SSL session to DBM file `%s'",
249 /* free temporary buffers */
252 /* allow the regular expiring to occur */
253 socache_dbm_expire(ctx
, s
);
258 static apr_status_t
socache_dbm_retrieve(ap_socache_instance_t
*ctx
, server_rec
*s
,
259 const unsigned char *id
, unsigned int idlen
,
260 unsigned char *dest
, unsigned int *destlen
,
271 /* allow the regular expiring to occur */
272 socache_dbm_expire(ctx
, s
);
274 /* create DBM key and values */
275 dbmkey
.dptr
= (char *)id
;
276 dbmkey
.dsize
= idlen
;
278 /* and fetch it from the DBM file
279 * XXX: Should we open the dbm against r->pool so the cleanup will
280 * do the apr_dbm_close? This would make the code a bit cleaner.
282 apr_pool_clear(ctx
->pool
);
283 if ((rc
= apr_dbm_open(&dbm
, ctx
->data_file
, APR_DBM_RWCREATE
,
284 SSL_DBM_FILE_MODE
, ctx
->pool
)) != APR_SUCCESS
) {
285 ap_log_error(APLOG_MARK
, APLOG_ERR
, rc
, s
,
286 "Cannot open SSLSessionCache DBM file `%s' for reading "
291 rc
= apr_dbm_fetch(dbm
, dbmkey
, &dbmval
);
292 if (rc
!= APR_SUCCESS
) {
296 if (dbmval
.dptr
== NULL
|| dbmval
.dsize
<= sizeof(time_t)) {
301 /* parse resulting data */
302 nData
= dbmval
.dsize
-sizeof(time_t);
303 if (nData
> *destlen
) {
309 memcpy(&expiry
, dbmval
.dptr
, sizeof(time_t));
310 memcpy(dest
, (char *)dbmval
.dptr
+ sizeof(time_t), nData
);
314 /* make sure the stuff is still not expired */
317 socache_dbm_remove(ctx
, s
, id
, idlen
, p
);
324 static apr_status_t
socache_dbm_remove(ap_socache_instance_t
*ctx
,
325 server_rec
*s
, const unsigned char *id
,
326 unsigned int idlen
, apr_pool_t
*p
)
332 /* create DBM key and values */
333 dbmkey
.dptr
= (char *)id
;
334 dbmkey
.dsize
= idlen
;
336 /* and delete it from the DBM file */
337 apr_pool_clear(ctx
->pool
);
339 if ((rv
= apr_dbm_open(&dbm
, ctx
->data_file
, APR_DBM_RWCREATE
,
340 SSL_DBM_FILE_MODE
, ctx
->pool
)) != APR_SUCCESS
) {
341 ap_log_error(APLOG_MARK
, APLOG_ERR
, rv
, s
,
342 "Cannot open SSLSessionCache DBM file `%s' for writing "
347 apr_dbm_delete(dbm
, dbmkey
);
353 static void socache_dbm_expire(ap_socache_instance_t
*ctx
, server_rec
*s
)
362 apr_datum_t
*keylist
;
369 * make sure the expiration for still not-accessed session
370 * cache entries is done only from time to time
374 if (tNow
< ctx
->last_expiry
+ ctx
->expiry_interval
) {
378 ctx
->last_expiry
= tNow
;
381 * Here we have to be very carefully: Not all DBM libraries are
382 * smart enough to allow one to iterate over the elements and at the
383 * same time delete expired ones. Some of them get totally crazy
384 * while others have no problems. So we have to do it the slower but
385 * more safe way: we first iterate over all elements and remember
386 * those which have to be expired. Then in a second pass we delete
387 * all those expired elements. Additionally we reopen the DBM file
388 * to be really safe in state.
394 /* allocate the key array in a memory sub pool */
395 apr_pool_clear(ctx
->pool
);
397 if ((keylist
= apr_palloc(ctx
->pool
, sizeof(dbmkey
)*KEYMAX
)) == NULL
) {
401 /* pass 1: scan DBM database */
403 if ((rv
= apr_dbm_open(&dbm
, ctx
->data_file
, APR_DBM_RWCREATE
,
404 SSL_DBM_FILE_MODE
, ctx
->pool
)) != APR_SUCCESS
) {
405 ap_log_error(APLOG_MARK
, APLOG_ERR
, rv
, s
,
406 "Cannot open SSLSessionCache DBM file `%s' for "
411 apr_dbm_firstkey(dbm
, &dbmkey
);
412 while (dbmkey
.dptr
!= NULL
) {
415 apr_dbm_fetch(dbm
, dbmkey
, &dbmval
);
416 if (dbmval
.dsize
<= sizeof(time_t) || dbmval
.dptr
== NULL
)
419 memcpy(&tExpiresAt
, dbmval
.dptr
, sizeof(time_t));
420 if (tExpiresAt
<= tNow
)
424 if ((keylist
[keyidx
].dptr
= apr_pmemdup(ctx
->pool
, dbmkey
.dptr
, dbmkey
.dsize
)) != NULL
) {
425 keylist
[keyidx
].dsize
= dbmkey
.dsize
;
427 if (keyidx
== KEYMAX
)
431 apr_dbm_nextkey(dbm
, &dbmkey
);
435 /* pass 2: delete expired elements */
436 if (apr_dbm_open(&dbm
, ctx
->data_file
, APR_DBM_RWCREATE
,
437 SSL_DBM_FILE_MODE
, ctx
->pool
) != APR_SUCCESS
) {
438 ap_log_error(APLOG_MARK
, APLOG_ERR
, rv
, s
,
439 "Cannot re-open SSLSessionCache DBM file `%s' for "
444 for (i
= 0; i
< keyidx
; i
++) {
445 apr_dbm_delete(dbm
, keylist
[i
]);
454 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, 0, s
,
455 "Inter-Process Session Cache (DBM) Expiry: "
456 "old: %d, new: %d, removed: %d",
457 nElements
, nElements
-nDeleted
, nDeleted
);
460 static void socache_dbm_status(ap_socache_instance_t
*ctx
, request_rec
*r
,
474 apr_pool_clear(ctx
->pool
);
475 if ((rv
= apr_dbm_open(&dbm
, ctx
->data_file
, APR_DBM_RWCREATE
,
476 SSL_DBM_FILE_MODE
, ctx
->pool
)) != APR_SUCCESS
) {
477 ap_log_rerror(APLOG_MARK
, APLOG_ERR
, rv
, r
,
478 "Cannot open SSLSessionCache DBM file `%s' for status "
484 * XXX - Check the return value of apr_dbm_firstkey, apr_dbm_fetch - TBD
486 apr_dbm_firstkey(dbm
, &dbmkey
);
487 for ( ; dbmkey
.dptr
!= NULL
; apr_dbm_nextkey(dbm
, &dbmkey
)) {
488 apr_dbm_fetch(dbm
, dbmkey
, &dbmval
);
489 if (dbmval
.dptr
== NULL
)
492 nSize
+= dbmval
.dsize
;
495 if (nSize
> 0 && nElem
> 0)
496 nAverage
= nSize
/ nElem
;
499 ap_rprintf(r
, "cache type: <b>DBM</b>, maximum size: <b>unlimited</b><br>");
500 ap_rprintf(r
, "current sessions: <b>%d</b>, current size: <b>%d</b> bytes<br>", nElem
, nSize
);
501 ap_rprintf(r
, "average session size: <b>%d</b> bytes<br>", nAverage
);
505 static const ap_socache_provider_t socache_dbm
= {
507 AP_SOCACHE_FLAG_NOTMPSAFE
,
512 socache_dbm_retrieve
,
517 static void register_hooks(apr_pool_t
*p
)
519 ap_register_provider(p
, AP_SOCACHE_PROVIDER_GROUP
, "dbm",
520 AP_SOCACHE_PROVIDER_VERSION
,
524 module AP_MODULE_DECLARE_DATA socache_dbm_module
= {
525 STANDARD20_MODULE_STUFF
,
526 NULL
, NULL
, NULL
, NULL
, NULL
,