This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
+ the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
#define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
static TDB_CONTEXT *cache;
+static BOOL cache_readonly;
/**
* @file gencache.c
/* skip file open if it's already opened */
if (cache) return True;
- asprintf(&cache_fname, "%s/%s", lp_lockdir(), "gencache.tdb");
- if (cache_fname)
- DEBUG(5, ("Opening cache file at %s\n", cache_fname));
- else {
- DEBUG(0, ("Filename allocation failed.\n"));
- return False;
- }
+ cache_fname = lock_path("gencache.tdb");
+
+ DEBUG(5, ("Opening cache file at %s\n", cache_fname));
cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
O_RDWR|O_CREAT, 0644);
- SAFE_FREE(cache_fname);
+ if (!cache && (errno == EACCES)) {
+ cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDONLY, 0644);
+ if (cache) {
+ cache_readonly = True;
+ DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname));
+ }
+ }
+
if (!cache) {
DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
return False;
DEBUG(5, ("Closing cache file\n"));
ret = tdb_close(cache);
cache = NULL;
+ cache_readonly = False;
return ret != -1;
}
BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
{
int ret;
- TDB_DATA keybuf, databuf;
+ TDB_DATA databuf;
char* valstr = NULL;
/* fail completely if get null pointers passed */
if (!gencache_init()) return False;
+ if (cache_readonly) {
+ return False;
+ }
+
asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value);
if (!valstr)
return False;
- keybuf.dptr = SMB_STRDUP(keystr);
- keybuf.dsize = strlen(keystr)+1;
- databuf.dptr = SMB_STRDUP(valstr);
- databuf.dsize = strlen(valstr)+1;
+ databuf = string_term_tdb_data(valstr);
DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
- " %s (%d seconds %s)\n", keybuf.dptr, value,ctime(&timeout),
+ " %s (%d seconds %s)\n", keystr, value,ctime(&timeout),
(int)(timeout - time(NULL)),
timeout > time(NULL) ? "ahead" : "in the past"));
- ret = tdb_store(cache, keybuf, databuf, 0);
+ ret = tdb_store_bystring(cache, keystr, databuf, 0);
SAFE_FREE(valstr);
- SAFE_FREE(keybuf.dptr);
- SAFE_FREE(databuf.dptr);
-
- return ret == 0;
-}
-
-
-/**
- * Set existing entry to the cache file.
- *
- * @param keystr string that represents a key of this entry
- * @param valstr text representation value being cached
- * @param timeout time when the value is expired
- *
- * @retval true when entry is successfuly set
- * @retval false on failure
- **/
-
-BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout)
-{
- int ret = -1;
- TDB_DATA keybuf, databuf;
- char *old_valstr, *datastr;
- time_t old_timeout;
-
- /* fail completely if get null pointers passed */
- SMB_ASSERT(keystr && valstr);
-
- if (!gencache_init()) return False;
-
- /*
- * Check whether entry exists in the cache
- * Don't verify gencache_get exit code, since the entry may be expired
- */
- gencache_get(keystr, &old_valstr, &old_timeout);
-
- if (!(old_valstr && old_timeout)) return False;
-
- DEBUG(10, ("Setting cache entry with key = %s; old value = %s and old timeout \
- = %s\n", keystr, old_valstr, ctime(&old_timeout)));
-
- asprintf(&datastr, CACHE_DATA_FMT, (int)timeout, valstr);
- keybuf.dptr = SMB_STRDUP(keystr);
- keybuf.dsize = strlen(keystr)+1;
- databuf.dptr = SMB_STRDUP(datastr);
- databuf.dsize = strlen(datastr)+1;
- DEBUGADD(10, ("New value = %s, new timeout = %s (%d seconds %s)", valstr,
- ctime(&timeout), (int)(timeout - time(NULL)),
- timeout > time(NULL) ? "ahead" : "in the past"));
-
-
- ret = tdb_store(cache, keybuf, databuf, TDB_REPLACE);
-
- SAFE_FREE(datastr);
- SAFE_FREE(old_valstr);
- SAFE_FREE(keybuf.dptr);
- SAFE_FREE(databuf.dptr);
return ret == 0;
}
-
/**
* Delete one entry from the cache file.
BOOL gencache_del(const char *keystr)
{
int ret;
- TDB_DATA keybuf;
/* fail completely if get null pointers passed */
SMB_ASSERT(keystr);
if (!gencache_init()) return False;
- keybuf.dptr = SMB_STRDUP(keystr);
- keybuf.dsize = strlen(keystr)+1;
+ if (cache_readonly) {
+ return False;
+ }
+
DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
- ret = tdb_delete(cache, keybuf);
+ ret = tdb_delete_bystring(cache, keystr);
- SAFE_FREE(keybuf.dptr);
return ret == 0;
}
BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout)
{
- TDB_DATA keybuf, databuf;
+ TDB_DATA databuf;
+ time_t t;
+ char *endptr;
/* fail completely if get null pointers passed */
SMB_ASSERT(keystr);
if (!gencache_init()) {
return False;
}
-
- keybuf.dptr = SMB_STRDUP(keystr);
- keybuf.dsize = strlen(keystr)+1;
- databuf = tdb_fetch(cache, keybuf);
- SAFE_FREE(keybuf.dptr);
-
- if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) {
- char* entry_buf = SMB_STRNDUP(databuf.dptr, databuf.dsize);
- char *v;
- time_t t;
- unsigned u;
- int status;
- char *fmt;
- v = (char *)SMB_MALLOC(databuf.dsize + 1 - TIMEOUT_LEN);
- if (!v) {
- return False;
- }
+ databuf = tdb_fetch_bystring(cache, keystr);
- SAFE_FREE(databuf.dptr);
-
- asprintf(&fmt, READ_CACHE_DATA_FMT_TEMPLATE, (unsigned int)databuf.dsize - TIMEOUT_LEN);
- if (!fmt) {
- SAFE_FREE(v);
- return False;
- }
-
- status = sscanf(entry_buf, fmt, &u, v);
- SAFE_FREE(fmt);
-
- if ( status != 2 ) {
- DEBUG(0, ("gencache_get: Invalid return %d from sscanf\n", status ));
- }
- t = u;
- SAFE_FREE(entry_buf);
+ if (databuf.dptr == NULL) {
+ DEBUG(10, ("Cache entry with key = %s couldn't be found\n",
+ keystr));
+ return False;
+ }
- DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
- "timeout = %s", t > time(NULL) ? "valid" :
- "expired", keystr, v, ctime(&t)));
+ t = strtol((const char *)databuf.dptr, &endptr, 10);
- if (valstr) {
- *valstr = v;
- } else {
- SAFE_FREE(v);
- }
+ if ((endptr == NULL) || (*endptr != '/')) {
+ DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr));
+ SAFE_FREE(databuf.dptr);
+ return False;
+ }
- if (timeout) {
- *timeout = t;
- }
+ DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
+ "timeout = %s", t > time(NULL) ? "valid" :
+ "expired", keystr, endptr+1, ctime(&t)));
- return t > time(NULL);
+ if (t <= time(NULL)) {
- }
+ /* We're expired, delete the entry */
+ tdb_delete_bystring(cache, keystr);
- SAFE_FREE(databuf.dptr);
+ SAFE_FREE(databuf.dptr);
+ return False;
+ }
if (valstr) {
- *valstr = NULL;
+ *valstr = SMB_STRDUP(endptr+1);
+ if (*valstr == NULL) {
+ SAFE_FREE(databuf.dptr);
+ DEBUG(0, ("strdup failed\n"));
+ return False;
+ }
}
+
+ SAFE_FREE(databuf.dptr);
+
if (timeout) {
- timeout = NULL;
+ *timeout = t;
}
- DEBUG(10, ("Cache entry with key = %s couldn't be found\n", keystr));
- return False;
-}
+ return True;
+}
+
/**
* Iterate through all entries which key matches to specified pattern
char *fmt;
/* ensure null termination of the key string */
- keystr = SMB_STRNDUP(node->node_key.dptr, node->node_key.dsize);
+ keystr = SMB_STRNDUP((const char *)node->node_key.dptr, node->node_key.dsize);
if (!keystr) {
break;
}
node = node->next;
continue;
}
- entry = SMB_STRNDUP(databuf.dptr, databuf.dsize);
+ entry = SMB_STRNDUP((const char *)databuf.dptr, databuf.dsize);
if (!entry) {
SAFE_FREE(databuf.dptr);
SAFE_FREE(keystr);