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. */
47 apr_time_t last_expiry;
48 apr_interval_time_t expiry_interval;
52 * Support for DBM library
54 #define DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
56 #define DEFAULT_DBM_PREFIX DEFAULT_REL_RUNTIMEDIR "/socache-dbm-"
58 /* ### this should use apr_dbm_usednames. */
59 #if !defined(DBM_FILE_SUFFIX_DIR) && !defined(DBM_FILE_SUFFIX_PAG)
60 #if defined(DBM_SUFFIX)
61 #define DBM_FILE_SUFFIX_DIR DBM_SUFFIX
62 #define DBM_FILE_SUFFIX_PAG DBM_SUFFIX
63 #elif defined(__FreeBSD__) || (defined(DB_LOCK) && defined(DB_SHMEM))
64 #define DBM_FILE_SUFFIX_DIR ".db"
65 #define DBM_FILE_SUFFIX_PAG ".db"
67 #define DBM_FILE_SUFFIX_DIR ".dir"
68 #define DBM_FILE_SUFFIX_PAG ".pag"
72 static void socache_dbm_expire(ap_socache_instance_t *ctx, server_rec *s);
74 static apr_status_t socache_dbm_remove(ap_socache_instance_t *ctx,
75 server_rec *s, const unsigned char *id,
76 unsigned int idlen, apr_pool_t *p);
78 static const char *socache_dbm_create(ap_socache_instance_t **context,
80 apr_pool_t *tmp, apr_pool_t *p)
82 ap_socache_instance_t *ctx;
84 *context = ctx = apr_pcalloc(p, sizeof *ctx);
87 ctx->data_file = ap_server_root_relative(p, arg);
88 if (!ctx->data_file) {
89 return apr_psprintf(tmp, "Invalid cache file path %s", arg);
93 apr_pool_create(&ctx->pool, p);
98 #if AP_NEED_SET_MUTEX_PERMS
99 static int try_chown(apr_pool_t *p, server_rec *s,
100 const char *name, const char *suffix)
103 name = apr_pstrcat(p, name, suffix, NULL);
104 if (-1 == chown(name, ap_unixd_config.user_id,
105 (gid_t)-1 /* no gid change */ ))
108 ap_log_error(APLOG_MARK, APLOG_ERR, APR_FROM_OS_ERROR(errno), s, APLOGNO(00802)
109 "Can't change owner of %s", name);
117 static apr_status_t socache_dbm_init(ap_socache_instance_t *ctx,
118 const char *namespace,
119 const struct ap_socache_hints *hints,
120 server_rec *s, apr_pool_t *p)
125 /* for the DBM we need the data file */
126 if (ctx->data_file == NULL) {
127 const char *path = apr_pstrcat(p, DEFAULT_DBM_PREFIX, namespace,
130 ctx->data_file = ap_server_root_relative(p, path);
132 if (ctx->data_file == NULL) {
133 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00803)
134 "could not use default path '%s' for DBM socache",
140 /* open it once to create it and to make sure it _can_ be created */
141 apr_pool_clear(ctx->pool);
143 if ((rv = apr_dbm_open(&dbm, ctx->data_file,
144 APR_DBM_RWCREATE, DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
145 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00804)
146 "Cannot create socache DBM file `%s'",
152 ctx->expiry_interval = (hints && hints->expiry_interval
153 ? hints->expiry_interval : apr_time_from_sec(30));
155 #if AP_NEED_SET_MUTEX_PERMS
157 * We have to make sure the Apache child processes have access to
158 * the DBM file. But because there are brain-dead platforms where we
159 * cannot exactly determine the suffixes we try all possibilities.
161 if (geteuid() == 0 /* is superuser */) {
162 try_chown(p, s, ctx->data_file, NULL);
163 if (try_chown(p, s, ctx->data_file, DBM_FILE_SUFFIX_DIR))
164 if (try_chown(p, s, ctx->data_file, ".db"))
165 try_chown(p, s, ctx->data_file, ".dir");
166 if (try_chown(p, s, ctx->data_file, DBM_FILE_SUFFIX_PAG))
167 if (try_chown(p, s, ctx->data_file, ".db"))
168 try_chown(p, s, ctx->data_file, ".pag");
171 socache_dbm_expire(ctx, s);
176 static void socache_dbm_destroy(ap_socache_instance_t *ctx, server_rec *s)
178 /* the correct way */
179 unlink(apr_pstrcat(ctx->pool, ctx->data_file, DBM_FILE_SUFFIX_DIR, NULL));
180 unlink(apr_pstrcat(ctx->pool, ctx->data_file, DBM_FILE_SUFFIX_PAG, NULL));
181 /* the additional ways to be sure */
182 unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".dir", NULL));
183 unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".pag", NULL));
184 unlink(apr_pstrcat(ctx->pool, ctx->data_file, ".db", NULL));
185 unlink(ctx->data_file);
190 static apr_status_t socache_dbm_store(ap_socache_instance_t *ctx,
191 server_rec *s, const unsigned char *id,
192 unsigned int idlen, apr_time_t expiry,
193 unsigned char *ucaData,
194 unsigned int nData, apr_pool_t *pool)
201 /* be careful: do not try to store too much bytes in a DBM file! */
203 if ((idlen + nData) >= PAIRMAX) {
204 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00805)
205 "data size too large for DBM socache: %d >= %d",
206 (idlen + nData), PAIRMAX);
210 if ((idlen + nData) >= 950 /* at least less than approx. 1KB */) {
211 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00806)
212 "data size too large for DBM socache: %d >= %d",
213 (idlen + nData), 950);
219 dbmkey.dptr = (char *)id;
220 dbmkey.dsize = idlen;
222 /* create DBM value */
223 dbmval.dsize = sizeof(apr_time_t) + nData;
224 dbmval.dptr = (char *)ap_malloc(dbmval.dsize);
225 memcpy((char *)dbmval.dptr, &expiry, sizeof(apr_time_t));
226 memcpy((char *)dbmval.dptr+sizeof(apr_time_t), ucaData, nData);
228 /* and store it to the DBM file */
229 apr_pool_clear(ctx->pool);
231 if ((rv = apr_dbm_open(&dbm, ctx->data_file,
232 APR_DBM_RWCREATE, DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
233 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00807)
234 "Cannot open socache DBM file `%s' for writing "
240 if ((rv = apr_dbm_store(dbm, dbmkey, dbmval)) != APR_SUCCESS) {
241 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00808)
242 "Cannot store socache object to DBM file `%s'",
250 /* free temporary buffers */
253 /* allow the regular expiring to occur */
254 socache_dbm_expire(ctx, s);
259 static apr_status_t socache_dbm_retrieve(ap_socache_instance_t *ctx, server_rec *s,
260 const unsigned char *id, unsigned int idlen,
261 unsigned char *dest, unsigned int *destlen,
272 /* allow the regular expiring to occur */
273 socache_dbm_expire(ctx, s);
275 /* create DBM key and values */
276 dbmkey.dptr = (char *)id;
277 dbmkey.dsize = idlen;
279 /* and fetch it from the DBM file
280 * XXX: Should we open the dbm against r->pool so the cleanup will
281 * do the apr_dbm_close? This would make the code a bit cleaner.
283 apr_pool_clear(ctx->pool);
284 if ((rc = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
285 DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
286 ap_log_error(APLOG_MARK, APLOG_ERR, rc, s, APLOGNO(00809)
287 "Cannot open socache DBM file `%s' for reading "
292 rc = apr_dbm_fetch(dbm, dbmkey, &dbmval);
293 if (rc != APR_SUCCESS) {
297 if (dbmval.dptr == NULL || dbmval.dsize <= sizeof(apr_time_t)) {
302 /* parse resulting data */
303 nData = dbmval.dsize-sizeof(apr_time_t);
304 if (nData > *destlen) {
310 memcpy(&expiry, dbmval.dptr, sizeof(apr_time_t));
311 memcpy(dest, (char *)dbmval.dptr + sizeof(apr_time_t), nData);
315 /* make sure the stuff is still not expired */
316 now = apr_time_now();
318 socache_dbm_remove(ctx, s, id, idlen, p);
325 static apr_status_t socache_dbm_remove(ap_socache_instance_t *ctx,
326 server_rec *s, const unsigned char *id,
327 unsigned int idlen, apr_pool_t *p)
333 /* create DBM key and values */
334 dbmkey.dptr = (char *)id;
335 dbmkey.dsize = idlen;
337 /* and delete it from the DBM file */
338 apr_pool_clear(ctx->pool);
340 if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
341 DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
342 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00810)
343 "Cannot open socache DBM file `%s' for writing "
348 apr_dbm_delete(dbm, dbmkey);
354 static void socache_dbm_expire(ap_socache_instance_t *ctx, server_rec *s)
363 apr_datum_t *keylist;
370 * make sure the expiration for still not-accessed
371 * socache entries is done only from time to time
373 now = apr_time_now();
375 if (now < ctx->last_expiry + ctx->expiry_interval) {
379 ctx->last_expiry = now;
382 * Here we have to be very carefully: Not all DBM libraries are
383 * smart enough to allow one to iterate over the elements and at the
384 * same time delete expired ones. Some of them get totally crazy
385 * while others have no problems. So we have to do it the slower but
386 * more safe way: we first iterate over all elements and remember
387 * those which have to be expired. Then in a second pass we delete
388 * all those expired elements. Additionally we reopen the DBM file
389 * to be really safe in state.
395 /* allocate the key array in a memory sub pool */
396 apr_pool_clear(ctx->pool);
398 if ((keylist = apr_palloc(ctx->pool, sizeof(dbmkey)*KEYMAX)) == NULL) {
402 /* pass 1: scan DBM database */
404 if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
405 DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
406 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00811)
407 "Cannot open socache DBM file `%s' for "
412 apr_dbm_firstkey(dbm, &dbmkey);
413 while (dbmkey.dptr != NULL) {
416 apr_dbm_fetch(dbm, dbmkey, &dbmval);
417 if (dbmval.dsize <= sizeof(apr_time_t) || dbmval.dptr == NULL)
420 memcpy(&expiry, dbmval.dptr, sizeof(apr_time_t));
425 if ((keylist[keyidx].dptr = apr_pmemdup(ctx->pool, dbmkey.dptr, dbmkey.dsize)) != NULL) {
426 keylist[keyidx].dsize = dbmkey.dsize;
428 if (keyidx == KEYMAX)
432 apr_dbm_nextkey(dbm, &dbmkey);
436 /* pass 2: delete expired elements */
437 if (apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
438 DBM_FILE_MODE, ctx->pool) != APR_SUCCESS) {
439 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00812)
440 "Cannot re-open socache DBM file `%s' for "
445 for (i = 0; i < keyidx; i++) {
446 apr_dbm_delete(dbm, keylist[i]);
455 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00813)
456 "DBM socache expiry: "
457 "old: %d, new: %d, removed: %d",
458 elts, elts-deleted, deleted);
461 static void socache_dbm_status(ap_socache_instance_t *ctx, request_rec *r,
475 apr_pool_clear(ctx->pool);
476 if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
477 DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
478 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00814)
479 "Cannot open socache DBM file `%s' for status "
485 * XXX - Check the return value of apr_dbm_firstkey, apr_dbm_fetch - TBD
487 apr_dbm_firstkey(dbm, &dbmkey);
488 for ( ; dbmkey.dptr != NULL; apr_dbm_nextkey(dbm, &dbmkey)) {
489 apr_dbm_fetch(dbm, dbmkey, &dbmval);
490 if (dbmval.dptr == NULL)
493 size += dbmval.dsize;
496 if (size > 0 && elts > 0)
497 avg = (int)(size / (long)elts);
500 ap_rprintf(r, "cache type: <b>DBM</b>, maximum size: <b>unlimited</b><br>");
501 ap_rprintf(r, "current entries: <b>%d</b>, current size: <b>%ld</b> bytes<br>", elts, size);
502 ap_rprintf(r, "average entry size: <b>%d</b> bytes<br>", avg);
506 static apr_status_t socache_dbm_iterate(ap_socache_instance_t *ctx,
507 server_rec *s, void *userctx,
508 ap_socache_iterator_t *iterator,
520 * make sure the expired records are omitted
522 now = apr_time_now();
523 if ((rv = apr_dbm_open(&dbm, ctx->data_file, APR_DBM_RWCREATE,
524 DBM_FILE_MODE, ctx->pool)) != APR_SUCCESS) {
525 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00815)
526 "Cannot open socache DBM file `%s' for "
527 "iterating", ctx->data_file);
530 rv = apr_dbm_firstkey(dbm, &dbmkey);
531 while (rv == APR_SUCCESS && dbmkey.dptr != NULL) {
533 apr_dbm_fetch(dbm, dbmkey, &dbmval);
534 if (dbmval.dsize <= sizeof(apr_time_t) || dbmval.dptr == NULL)
537 memcpy(&expiry, dbmval.dptr, sizeof(apr_time_t));
542 rv = iterator(ctx, s, userctx,
543 (unsigned char *)dbmkey.dptr, dbmkey.dsize,
544 (unsigned char *)dbmval.dptr + sizeof(apr_time_t),
545 dbmval.dsize - sizeof(apr_time_t), pool);
546 ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, s, APLOGNO(00816)
547 "dbm `%s' entry iterated", ctx->data_file);
548 if (rv != APR_SUCCESS)
551 rv = apr_dbm_nextkey(dbm, &dbmkey);
555 if (rv != APR_SUCCESS && rv != APR_EOF) {
556 ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00817)
557 "Failure reading first/next socache DBM file `%s' record",
564 static const ap_socache_provider_t socache_dbm = {
566 AP_SOCACHE_FLAG_NOTMPSAFE,
571 socache_dbm_retrieve,
577 static void register_hooks(apr_pool_t *p)
579 ap_register_provider(p, AP_SOCACHE_PROVIDER_GROUP, "dbm",
580 AP_SOCACHE_PROVIDER_VERSION,
584 AP_DECLARE_MODULE(socache_dbm) = {
585 STANDARD20_MODULE_STUFF,
586 NULL, NULL, NULL, NULL, NULL,