2 Unix SMB/CIFS implementation.
4 Generic, persistent and shared between processes cache mechanism for use
5 by various parts of the Samba code
7 Copyright (C) Rafal Szczesniak 2002
8 Copyright (C) Volker Lendecke 2009
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "lib/gencache.h"
26 #include "system/filesys.h"
27 #include "system/glob.h"
29 #include "tdb_wrap/tdb_wrap.h"
31 #include "lib/util/strv.h"
32 #include "lib/util/util_paths.h"
35 #define DBGC_CLASS DBGC_TDB
37 #define GENCACHE_USER_PATH "~/.cache/samba/gencache.tdb"
39 static struct tdb_wrap
*cache
;
43 * @brief Generic, persistent and shared between processes cache mechanism
44 * for use by various parts of the Samba code
48 static bool gencache_pull_timeout(TDB_DATA key
,
53 struct gencache_timeout
{
57 bool gencache_timeout_expired(const struct gencache_timeout
*t
)
59 return t
->timeout
<= time(NULL
);
63 * Cache initialisation function. Opens cache tdb file or creates
64 * it if does not exist.
66 * @return true on successful initialisation of the cache or
70 static bool gencache_init(void)
72 char* cache_fname
= NULL
;
73 int open_flags
= O_RDWR
|O_CREAT
;
74 int tdb_flags
= TDB_INCOMPATIBLE_HASH
|TDB_NOSYNC
|TDB_MUTEX_LOCKING
;
77 /* skip file open if it's already opened */
82 hash_size
= lp_parm_int(-1, "gencache", "hash_size", 10000);
84 cache_fname
= lock_path(talloc_tos(), "gencache.tdb");
85 if (cache_fname
== NULL
) {
89 DEBUG(5, ("Opening cache file at %s\n", cache_fname
));
91 cache
= tdb_wrap_open(NULL
, cache_fname
, hash_size
,
95 * Allow client tools to create a gencache in the home directory
98 if (cache
== NULL
&& errno
== EACCES
&& geteuid() != 0) {
99 char *cache_dname
= NULL
, *tmp
= NULL
;
102 TALLOC_FREE(cache_fname
);
104 cache_fname
= path_expand_tilde(talloc_tos(),
106 if (cache_fname
== NULL
) {
107 DBG_ERR("Failed to expand path: %s\n",
112 tmp
= talloc_strdup(talloc_tos(), cache_fname
);
114 DBG_ERR("No memory!\n");
115 TALLOC_FREE(cache_fname
);
119 cache_dname
= dirname(tmp
);
120 if (cache_dname
== NULL
) {
121 DBG_ERR("Invalid path: %s\n", cache_fname
);
123 TALLOC_FREE(cache_fname
);
127 ok
= directory_create_or_exists_recursive(cache_dname
, 0700);
129 DBG_ERR("Failed to create directory: %s - %s\n",
130 cache_dname
, strerror(errno
));
132 TALLOC_FREE(cache_fname
);
137 cache
= tdb_wrap_open(NULL
,
144 DBG_INFO("Opening user cache file %s.\n",
150 DEBUG(5, ("Opening %s failed: %s\n", cache_fname
,
152 TALLOC_FREE(cache_fname
);
155 TALLOC_FREE(cache_fname
);
161 * Walk the hash chain for "key", deleting all expired entries for
164 struct gencache_prune_expired_state
{
169 static int gencache_prune_expired_fn(struct tdb_context
*tdb
,
174 struct gencache_prune_expired_state
*state
= private_data
;
175 struct gencache_timeout t
;
177 bool expired
= false;
179 if ((key
.dsize
== 0) || (key
.dptr
[key
.dsize
-1] != '\0')) {
180 /* not a valid record, should never happen */
184 ok
= gencache_pull_timeout(key
, data
, &t
.timeout
, NULL
);
186 expired
= gencache_timeout_expired(&t
);
189 if (!ok
|| expired
) {
192 ret
= strv_add(state
->mem_ctx
, &state
->keys
, (char *)key
.dptr
);
195 * Exit the loop. It's unlikely that it will
205 static void gencache_prune_expired(struct tdb_context
*tdb
,
208 struct gencache_prune_expired_state state
= {
209 .mem_ctx
= talloc_tos(),
214 ret
= tdb_traverse_key_chain(
215 tdb
, chain_key
, gencache_prune_expired_fn
, &state
);
217 DBG_DEBUG("tdb_traverse_key_chain failed: %s\n",
222 while ((keystr
= strv_next(state
.keys
, keystr
)) != NULL
) {
223 TDB_DATA key
= string_term_tdb_data(keystr
);
226 * We expect the hash chain of "chain_key" to be
227 * locked. So between gencache_prune_expired_fn
228 * figuring out "keystr" is expired and the
229 * tdb_delete, nobody can have reset the timeout.
231 tdb_delete(tdb
, key
);
234 TALLOC_FREE(state
.keys
);
238 * Set an entry in the cache file. If there's no such
241 * @param keystr string that represents a key of this entry
242 * @param blob DATA_BLOB value being cached
243 * @param timeout time when the value is expired
245 * @retval true when entry is successfully stored
246 * @retval false on failure
249 bool gencache_set_data_blob(const char *keystr
, DATA_BLOB blob
,
257 if ((keystr
== NULL
) || (blob
.data
== NULL
)) {
261 key
= string_term_tdb_data(keystr
);
263 if (!gencache_init()) {
267 dbufs
[0] = (TDB_DATA
) { .dptr
= (uint8_t *)&timeout
,
268 .dsize
= sizeof(time_t) };
269 dbufs
[1] = (TDB_DATA
) { .dptr
= blob
.data
, .dsize
= blob
.length
};
271 crc
= crc32(0, Z_NULL
, 0);
272 crc
= crc32(crc
, key
.dptr
, key
.dsize
);
273 crc
= crc32(crc
, dbufs
[0].dptr
, dbufs
[0].dsize
);
274 crc
= crc32(crc
, dbufs
[1].dptr
, dbufs
[1].dsize
);
276 dbufs
[2] = (TDB_DATA
) { .dptr
= (uint8_t *)&crc
,
277 .dsize
= sizeof(crc
) };
279 DBG_DEBUG("Adding cache entry with key=[%s] and timeout="
280 "[%s] (%ld seconds %s)\n", keystr
,
281 timestring(talloc_tos(), timeout
),
282 ((long int)timeout
) - time(NULL
),
283 timeout
> time(NULL
) ? "ahead" : "in the past");
285 ret
= tdb_chainlock(cache
->tdb
, key
);
287 DBG_WARNING("tdb_chainlock for key [%s] failed: %s\n",
288 keystr
, tdb_errorstr(cache
->tdb
));
292 gencache_prune_expired(cache
->tdb
, key
);
294 ret
= tdb_storev(cache
->tdb
, key
, dbufs
, ARRAY_SIZE(dbufs
), 0);
296 tdb_chainunlock(cache
->tdb
, key
);
301 if (tdb_error(cache
->tdb
) != TDB_ERR_CORRUPT
) {
305 ret
= tdb_wipe_all(cache
->tdb
);
306 SMB_ASSERT(ret
== 0);
312 * Delete one entry from the cache file.
314 * @param keystr string that represents a key of this entry
316 * @retval true upon successful deletion
317 * @retval false in case of failure
320 bool gencache_del(const char *keystr
)
322 TDB_DATA key
= string_term_tdb_data(keystr
);
325 if (keystr
== NULL
) {
329 if (!gencache_init()) {
333 DEBUG(10, ("Deleting cache entry (key=[%s])\n", keystr
));
335 ret
= tdb_delete(cache
->tdb
, key
);
340 if (tdb_error(cache
->tdb
) != TDB_ERR_CORRUPT
) {
344 ret
= tdb_wipe_all(cache
->tdb
);
345 SMB_ASSERT(ret
== 0);
347 return true; /* We've deleted a bit more... */
350 static bool gencache_pull_timeout(TDB_DATA key
,
356 uint32_t crc
, stored_crc
;
358 if ((data
.dptr
== NULL
) ||
359 (data
.dsize
< (sizeof(time_t) + sizeof(uint32_t)))) {
363 crc_ofs
= data
.dsize
- sizeof(uint32_t);
365 crc
= crc32(0, Z_NULL
, 0);
366 crc
= crc32(crc
, key
.dptr
, key
.dsize
);
367 crc
= crc32(crc
, data
.dptr
, crc_ofs
);
369 memcpy(&stored_crc
, data
.dptr
+ crc_ofs
, sizeof(uint32_t));
371 if (stored_crc
!= crc
) {
376 memcpy(pres
, data
.dptr
, sizeof(time_t));
378 if (payload
!= NULL
) {
379 *payload
= (DATA_BLOB
) {
380 .data
= data
.dptr
+sizeof(time_t),
381 .length
= data
.dsize
-sizeof(time_t)-sizeof(uint32_t),
387 struct gencache_parse_state
{
388 void (*parser
)(const struct gencache_timeout
*timeout
,
395 static int gencache_parse_fn(TDB_DATA key
, TDB_DATA data
, void *private_data
)
397 struct gencache_parse_state
*state
= private_data
;
398 struct gencache_timeout t
;
402 ret
= gencache_pull_timeout(key
, data
, &t
.timeout
, &payload
);
404 state
->format_error
= true;
407 state
->parser(&t
, payload
, state
->private_data
);
412 bool gencache_parse(const char *keystr
,
413 void (*parser
)(const struct gencache_timeout
*timeout
,
418 struct gencache_parse_state state
= {
419 .parser
= parser
, .private_data
= private_data
421 TDB_DATA key
= string_term_tdb_data(keystr
);
424 if (keystr
== NULL
) {
427 if (!gencache_init()) {
431 ret
= tdb_parse_record(cache
->tdb
, key
,
432 gencache_parse_fn
, &state
);
433 if ((ret
== -1) && (tdb_error(cache
->tdb
) == TDB_ERR_CORRUPT
)) {
439 if (state
.format_error
) {
440 ret
= tdb_delete(cache
->tdb
, key
);
449 ret
= tdb_wipe_all(cache
->tdb
);
450 SMB_ASSERT(ret
== 0);
454 struct gencache_get_data_blob_state
{
461 static void gencache_get_data_blob_parser(const struct gencache_timeout
*t
,
465 struct gencache_get_data_blob_state
*state
=
466 (struct gencache_get_data_blob_state
*)private_data
;
468 if (t
->timeout
== 0) {
469 state
->result
= false;
472 state
->timeout
= t
->timeout
;
474 if (state
->blob
== NULL
) {
475 state
->result
= true;
479 *state
->blob
= data_blob_talloc(state
->mem_ctx
, blob
.data
,
481 if (state
->blob
->data
== NULL
) {
482 state
->result
= false;
485 state
->result
= true;
489 * Get existing entry from the cache file.
491 * @param keystr string that represents a key of this entry
492 * @param blob DATA_BLOB that is filled with entry's blob
493 * @param timeout pointer to a time_t that is filled with entry's
496 * @retval true when entry is successfully fetched
497 * @retval false for failure
500 bool gencache_get_data_blob(const char *keystr
, TALLOC_CTX
*mem_ctx
,
502 time_t *timeout
, bool *was_expired
)
504 struct gencache_get_data_blob_state state
;
505 bool expired
= false;
507 state
.result
= false;
508 state
.mem_ctx
= mem_ctx
;
511 if (!gencache_parse(keystr
, gencache_get_data_blob_parser
, &state
)) {
517 if (state
.timeout
<= time(NULL
)) {
519 * We're expired, delete the entry. We can't use gencache_del
520 * here, because that uses gencache_get_data_blob for checking
521 * the existence of a record. We know the thing exists and
522 * directly store an empty value with 0 timeout.
524 gencache_set(keystr
, "", 0);
529 *timeout
= state
.timeout
;
535 if (was_expired
!= NULL
) {
536 *was_expired
= expired
;
538 if (state
.result
&& state
.blob
) {
539 data_blob_free(state
.blob
);
545 * Get existing entry from the cache file.
547 * @param keystr string that represents a key of this entry
548 * @param valstr buffer that is allocated and filled with the entry value
549 * buffer's disposing must be done outside
550 * @param timeout pointer to a time_t that is filled with entry's
553 * @retval true when entry is successfully fetched
554 * @retval false for failure
557 bool gencache_get(const char *keystr
, TALLOC_CTX
*mem_ctx
, char **value
,
563 ret
= gencache_get_data_blob(keystr
, mem_ctx
, &blob
, ptimeout
, NULL
);
567 if ((blob
.data
== NULL
) || (blob
.length
== 0)) {
568 data_blob_free(&blob
);
571 if (blob
.data
[blob
.length
-1] != '\0') {
572 /* Not NULL terminated, can't be a string */
573 data_blob_free(&blob
);
578 * talloc_move generates a type-punned warning here. As we
579 * leave the function immediately, do a simple talloc_steal.
581 *value
= (char *)talloc_steal(mem_ctx
, blob
.data
);
584 data_blob_free(&blob
);
589 * Set an entry in the cache file. If there's no such
592 * @param keystr string that represents a key of this entry
593 * @param value text representation value being cached
594 * @param timeout time when the value is expired
596 * @retval true when entry is successfully stored
597 * @retval false on failure
600 bool gencache_set(const char *keystr
, const char *value
, time_t timeout
)
602 DATA_BLOB blob
= data_blob_const(value
, strlen(value
)+1);
603 return gencache_set_data_blob(keystr
, blob
, timeout
);
606 struct gencache_iterate_blobs_state
{
607 void (*fn
)(const char *key
, DATA_BLOB value
,
608 time_t timeout
, void *private_data
);
613 static int gencache_iterate_blobs_fn(struct tdb_context
*tdb
, TDB_DATA key
,
614 TDB_DATA data
, void *priv
)
616 struct gencache_iterate_blobs_state
*state
=
617 (struct gencache_iterate_blobs_state
*)priv
;
619 char *free_key
= NULL
;
623 if (key
.dptr
[key
.dsize
-1] == '\0') {
624 keystr
= (char *)key
.dptr
;
626 /* ensure 0-termination */
627 keystr
= talloc_strndup(talloc_tos(), (char *)key
.dptr
, key
.dsize
);
629 if (keystr
== NULL
) {
634 if (!gencache_pull_timeout(key
, data
, &timeout
, &payload
)) {
643 if (fnmatch(state
->pattern
, keystr
, 0) != 0) {
647 DEBUG(10, ("Calling function with arguments "
648 "(key=[%s], timeout=[%s])\n",
649 keystr
, timestring(talloc_tos(), timeout
)));
651 state
->fn(keystr
, payload
, timeout
, state
->private_data
);
654 TALLOC_FREE(free_key
);
658 void gencache_iterate_blobs(void (*fn
)(const char *key
, DATA_BLOB value
,
659 time_t timeout
, void *private_data
),
660 void *private_data
, const char *pattern
)
662 struct gencache_iterate_blobs_state state
;
665 if ((fn
== NULL
) || (pattern
== NULL
) || !gencache_init()) {
669 DEBUG(5, ("Searching cache keys with pattern %s\n", pattern
));
672 state
.pattern
= pattern
;
673 state
.private_data
= private_data
;
675 ret
= tdb_traverse(cache
->tdb
, gencache_iterate_blobs_fn
, &state
);
677 if ((ret
== -1) && (tdb_error(cache
->tdb
) == TDB_ERR_CORRUPT
)) {
678 ret
= tdb_wipe_all(cache
->tdb
);
679 SMB_ASSERT(ret
== 0);
684 * Iterate through all entries which key matches to specified pattern
686 * @param fn pointer to the function that will be supplied with each single
687 * matching cache entry (key, value and timeout) as an arguments
688 * @param data void pointer to an arbitrary data that is passed directly to the fn
689 * function on each call
690 * @param keystr_pattern pattern the existing entries' keys are matched to
694 struct gencache_iterate_state
{
695 void (*fn
)(const char *key
, const char *value
, time_t timeout
,
700 static void gencache_iterate_fn(const char *key
, DATA_BLOB value
,
701 time_t timeout
, void *private_data
)
703 struct gencache_iterate_state
*state
=
704 (struct gencache_iterate_state
*)private_data
;
706 char *free_val
= NULL
;
708 if (value
.data
[value
.length
-1] == '\0') {
709 valstr
= (char *)value
.data
;
711 /* ensure 0-termination */
712 valstr
= talloc_strndup(talloc_tos(), (char *)value
.data
, value
.length
);
714 if (valstr
== NULL
) {
719 DEBUG(10, ("Calling function with arguments "
720 "(key=[%s], value=[%s], timeout=[%s])\n",
721 key
, valstr
, timestring(talloc_tos(), timeout
)));
723 state
->fn(key
, valstr
, timeout
, state
->private_data
);
727 TALLOC_FREE(free_val
);
730 void gencache_iterate(void (*fn
)(const char *key
, const char *value
,
731 time_t timeout
, void *dptr
),
732 void *private_data
, const char *pattern
)
734 struct gencache_iterate_state state
;
740 state
.private_data
= private_data
;
741 gencache_iterate_blobs(gencache_iterate_fn
, &state
, pattern
);