| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 |
|
|---|
| 4 | Generic, persistent and shared between processes cache mechanism for use
|
|---|
| 5 | by various parts of the Samba code
|
|---|
| 6 |
|
|---|
| 7 | Copyright (C) Rafal Szczesniak 2002
|
|---|
| 8 |
|
|---|
| 9 | This program is free software; you can redistribute it and/or modify
|
|---|
| 10 | it under the terms of the GNU General Public License as published by
|
|---|
| 11 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 12 | (at your option) any later version.
|
|---|
| 13 |
|
|---|
| 14 | This program is distributed in the hope that it will be useful,
|
|---|
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 17 | GNU General Public License for more details.
|
|---|
| 18 |
|
|---|
| 19 | You should have received a copy of the GNU General Public License
|
|---|
| 20 | along with this program; if not, write to the Free Software
|
|---|
| 21 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 22 | */
|
|---|
| 23 |
|
|---|
| 24 | #include "includes.h"
|
|---|
| 25 |
|
|---|
| 26 | #undef DBGC_CLASS
|
|---|
| 27 | #define DBGC_CLASS DBGC_TDB
|
|---|
| 28 |
|
|---|
| 29 | #define TIMEOUT_LEN 12
|
|---|
| 30 | #define CACHE_DATA_FMT "%12u/%s"
|
|---|
| 31 | #define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
|
|---|
| 32 |
|
|---|
| 33 | static TDB_CONTEXT *cache;
|
|---|
| 34 | static BOOL cache_readonly;
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * @file gencache.c
|
|---|
| 38 | * @brief Generic, persistent and shared between processes cache mechanism
|
|---|
| 39 | * for use by various parts of the Samba code
|
|---|
| 40 | *
|
|---|
| 41 | **/
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Cache initialisation function. Opens cache tdb file or creates
|
|---|
| 46 | * it if does not exist.
|
|---|
| 47 | *
|
|---|
| 48 | * @return true on successful initialisation of the cache or
|
|---|
| 49 | * false on failure
|
|---|
| 50 | **/
|
|---|
| 51 |
|
|---|
| 52 | BOOL gencache_init(void)
|
|---|
| 53 | {
|
|---|
| 54 | char* cache_fname = NULL;
|
|---|
| 55 |
|
|---|
| 56 | /* skip file open if it's already opened */
|
|---|
| 57 | if (cache) return True;
|
|---|
| 58 |
|
|---|
| 59 | cache_fname = lock_path("gencache.tdb");
|
|---|
| 60 |
|
|---|
| 61 | DEBUG(5, ("Opening cache file at %s\n", cache_fname));
|
|---|
| 62 |
|
|---|
| 63 | cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
|
|---|
| 64 | O_RDWR|O_CREAT, 0644);
|
|---|
| 65 |
|
|---|
| 66 | if (!cache && (errno == EACCES)) {
|
|---|
| 67 | cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDONLY, 0644);
|
|---|
| 68 | if (cache) {
|
|---|
| 69 | cache_readonly = True;
|
|---|
| 70 | DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname));
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | if (!cache) {
|
|---|
| 75 | DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
|
|---|
| 76 | return False;
|
|---|
| 77 | }
|
|---|
| 78 | return True;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Cache shutdown function. Closes opened cache tdb file.
|
|---|
| 84 | *
|
|---|
| 85 | * @return true on successful closing the cache or
|
|---|
| 86 | * false on failure during cache shutdown
|
|---|
| 87 | **/
|
|---|
| 88 |
|
|---|
| 89 | BOOL gencache_shutdown(void)
|
|---|
| 90 | {
|
|---|
| 91 | int ret;
|
|---|
| 92 | /* tdb_close routine returns -1 on error */
|
|---|
| 93 | if (!cache) return False;
|
|---|
| 94 | DEBUG(5, ("Closing cache file\n"));
|
|---|
| 95 | ret = tdb_close(cache);
|
|---|
| 96 | cache = NULL;
|
|---|
| 97 | cache_readonly = False;
|
|---|
| 98 | return ret != -1;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 | /**
|
|---|
| 103 | * Set an entry in the cache file. If there's no such
|
|---|
| 104 | * one, then add it.
|
|---|
| 105 | *
|
|---|
| 106 | * @param keystr string that represents a key of this entry
|
|---|
| 107 | * @param value text representation value being cached
|
|---|
| 108 | * @param timeout time when the value is expired
|
|---|
| 109 | *
|
|---|
| 110 | * @retval true when entry is successfuly stored
|
|---|
| 111 | * @retval false on failure
|
|---|
| 112 | **/
|
|---|
| 113 |
|
|---|
| 114 | BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
|
|---|
| 115 | {
|
|---|
| 116 | int ret;
|
|---|
| 117 | TDB_DATA keybuf, databuf;
|
|---|
| 118 | char* valstr = NULL;
|
|---|
| 119 |
|
|---|
| 120 | /* fail completely if get null pointers passed */
|
|---|
| 121 | SMB_ASSERT(keystr && value);
|
|---|
| 122 |
|
|---|
| 123 | if (!gencache_init()) return False;
|
|---|
| 124 |
|
|---|
| 125 | if (cache_readonly) {
|
|---|
| 126 | return False;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value);
|
|---|
| 130 | if (!valstr)
|
|---|
| 131 | return False;
|
|---|
| 132 |
|
|---|
| 133 | keybuf.dptr = CONST_DISCARD(char *, keystr);
|
|---|
| 134 | keybuf.dsize = strlen(keystr)+1;
|
|---|
| 135 | databuf.dptr = valstr;
|
|---|
| 136 | databuf.dsize = strlen(valstr)+1;
|
|---|
| 137 | DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
|
|---|
| 138 | " %s (%d seconds %s)\n", keybuf.dptr, value,ctime(&timeout),
|
|---|
| 139 | (int)(timeout - time(NULL)),
|
|---|
| 140 | timeout > time(NULL) ? "ahead" : "in the past"));
|
|---|
| 141 |
|
|---|
| 142 | ret = tdb_store(cache, keybuf, databuf, 0);
|
|---|
| 143 | SAFE_FREE(valstr);
|
|---|
| 144 |
|
|---|
| 145 | return ret == 0;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * Delete one entry from the cache file.
|
|---|
| 150 | *
|
|---|
| 151 | * @param keystr string that represents a key of this entry
|
|---|
| 152 | *
|
|---|
| 153 | * @retval true upon successful deletion
|
|---|
| 154 | * @retval false in case of failure
|
|---|
| 155 | **/
|
|---|
| 156 |
|
|---|
| 157 | BOOL gencache_del(const char *keystr)
|
|---|
| 158 | {
|
|---|
| 159 | int ret;
|
|---|
| 160 | TDB_DATA keybuf;
|
|---|
| 161 |
|
|---|
| 162 | /* fail completely if get null pointers passed */
|
|---|
| 163 | SMB_ASSERT(keystr);
|
|---|
| 164 |
|
|---|
| 165 | if (!gencache_init()) return False;
|
|---|
| 166 |
|
|---|
| 167 | if (cache_readonly) {
|
|---|
| 168 | return False;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | keybuf.dptr = CONST_DISCARD(char *, keystr);
|
|---|
| 172 | keybuf.dsize = strlen(keystr)+1;
|
|---|
| 173 | DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
|
|---|
| 174 | ret = tdb_delete(cache, keybuf);
|
|---|
| 175 |
|
|---|
| 176 | return ret == 0;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 | /**
|
|---|
| 181 | * Get existing entry from the cache file.
|
|---|
| 182 | *
|
|---|
| 183 | * @param keystr string that represents a key of this entry
|
|---|
| 184 | * @param valstr buffer that is allocated and filled with the entry value
|
|---|
| 185 | * buffer's disposing must be done outside
|
|---|
| 186 | * @param timeout pointer to a time_t that is filled with entry's
|
|---|
| 187 | * timeout
|
|---|
| 188 | *
|
|---|
| 189 | * @retval true when entry is successfuly fetched
|
|---|
| 190 | * @retval False for failure
|
|---|
| 191 | **/
|
|---|
| 192 |
|
|---|
| 193 | BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout)
|
|---|
| 194 | {
|
|---|
| 195 | TDB_DATA keybuf, databuf;
|
|---|
| 196 | time_t t;
|
|---|
| 197 | char *endptr;
|
|---|
| 198 |
|
|---|
| 199 | /* fail completely if get null pointers passed */
|
|---|
| 200 | SMB_ASSERT(keystr);
|
|---|
| 201 |
|
|---|
| 202 | if (!gencache_init()) {
|
|---|
| 203 | return False;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | keybuf.dptr = CONST_DISCARD(char *, keystr);
|
|---|
| 207 | keybuf.dsize = strlen(keystr)+1;
|
|---|
| 208 | databuf = tdb_fetch(cache, keybuf);
|
|---|
| 209 |
|
|---|
| 210 | if (databuf.dptr == NULL) {
|
|---|
| 211 | DEBUG(10, ("Cache entry with key = %s couldn't be found\n",
|
|---|
| 212 | keystr));
|
|---|
| 213 | return False;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | t = strtol(databuf.dptr, &endptr, 10);
|
|---|
| 217 |
|
|---|
| 218 | if ((endptr == NULL) || (*endptr != '/')) {
|
|---|
| 219 | DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr));
|
|---|
| 220 | SAFE_FREE(databuf.dptr);
|
|---|
| 221 | return False;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
|
|---|
| 225 | "timeout = %s", t > time(NULL) ? "valid" :
|
|---|
| 226 | "expired", keystr, endptr+1, ctime(&t)));
|
|---|
| 227 |
|
|---|
| 228 | if (t <= time(NULL)) {
|
|---|
| 229 |
|
|---|
| 230 | /* We're expired, delete the entry */
|
|---|
| 231 | tdb_delete(cache, keybuf);
|
|---|
| 232 |
|
|---|
| 233 | SAFE_FREE(databuf.dptr);
|
|---|
| 234 | return False;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | if (valstr) {
|
|---|
| 238 | *valstr = SMB_STRDUP(endptr+1);
|
|---|
| 239 | if (*valstr == NULL) {
|
|---|
| 240 | SAFE_FREE(databuf.dptr);
|
|---|
| 241 | DEBUG(0, ("strdup failed\n"));
|
|---|
| 242 | return False;
|
|---|
| 243 | }
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | SAFE_FREE(databuf.dptr);
|
|---|
| 247 |
|
|---|
| 248 | if (timeout) {
|
|---|
| 249 | *timeout = t;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | return True;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 | /**
|
|---|
| 257 | * Iterate through all entries which key matches to specified pattern
|
|---|
| 258 | *
|
|---|
| 259 | * @param fn pointer to the function that will be supplied with each single
|
|---|
| 260 | * matching cache entry (key, value and timeout) as an arguments
|
|---|
| 261 | * @param data void pointer to an arbitrary data that is passed directly to the fn
|
|---|
| 262 | * function on each call
|
|---|
| 263 | * @param keystr_pattern pattern the existing entries' keys are matched to
|
|---|
| 264 | *
|
|---|
| 265 | **/
|
|---|
| 266 |
|
|---|
| 267 | void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr),
|
|---|
| 268 | void* data, const char* keystr_pattern)
|
|---|
| 269 | {
|
|---|
| 270 | TDB_LIST_NODE *node, *first_node;
|
|---|
| 271 | TDB_DATA databuf;
|
|---|
| 272 | char *keystr = NULL, *valstr = NULL, *entry = NULL;
|
|---|
| 273 | time_t timeout = 0;
|
|---|
| 274 | int status;
|
|---|
| 275 | unsigned u;
|
|---|
| 276 |
|
|---|
| 277 | /* fail completely if get null pointers passed */
|
|---|
| 278 | SMB_ASSERT(fn && keystr_pattern);
|
|---|
| 279 |
|
|---|
| 280 | if (!gencache_init()) return;
|
|---|
| 281 |
|
|---|
| 282 | DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
|
|---|
| 283 | node = tdb_search_keys(cache, keystr_pattern);
|
|---|
| 284 | first_node = node;
|
|---|
| 285 |
|
|---|
| 286 | while (node) {
|
|---|
| 287 | char *fmt;
|
|---|
| 288 |
|
|---|
| 289 | /* ensure null termination of the key string */
|
|---|
| 290 | keystr = SMB_STRNDUP(node->node_key.dptr, node->node_key.dsize);
|
|---|
| 291 | if (!keystr) {
|
|---|
| 292 | break;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | /*
|
|---|
| 296 | * We don't use gencache_get function, because we need to iterate through
|
|---|
| 297 | * all of the entries. Validity verification is up to fn routine.
|
|---|
| 298 | */
|
|---|
| 299 | databuf = tdb_fetch(cache, node->node_key);
|
|---|
| 300 | if (!databuf.dptr || databuf.dsize <= TIMEOUT_LEN) {
|
|---|
| 301 | SAFE_FREE(databuf.dptr);
|
|---|
| 302 | SAFE_FREE(keystr);
|
|---|
| 303 | node = node->next;
|
|---|
| 304 | continue;
|
|---|
| 305 | }
|
|---|
| 306 | entry = SMB_STRNDUP(databuf.dptr, databuf.dsize);
|
|---|
| 307 | if (!entry) {
|
|---|
| 308 | SAFE_FREE(databuf.dptr);
|
|---|
| 309 | SAFE_FREE(keystr);
|
|---|
| 310 | break;
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | SAFE_FREE(databuf.dptr);
|
|---|
| 314 |
|
|---|
| 315 | valstr = (char *)SMB_MALLOC(databuf.dsize + 1 - TIMEOUT_LEN);
|
|---|
| 316 | if (!valstr) {
|
|---|
| 317 | SAFE_FREE(entry);
|
|---|
| 318 | SAFE_FREE(keystr);
|
|---|
| 319 | break;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | asprintf(&fmt, READ_CACHE_DATA_FMT_TEMPLATE, (unsigned int)databuf.dsize - TIMEOUT_LEN);
|
|---|
| 323 | if (!fmt) {
|
|---|
| 324 | SAFE_FREE(valstr);
|
|---|
| 325 | SAFE_FREE(entry);
|
|---|
| 326 | SAFE_FREE(keystr);
|
|---|
| 327 | break;
|
|---|
| 328 | }
|
|---|
| 329 | status = sscanf(entry, fmt, &u, valstr);
|
|---|
| 330 | SAFE_FREE(fmt);
|
|---|
| 331 |
|
|---|
| 332 | if ( status != 2 ) {
|
|---|
| 333 | DEBUG(0,("gencache_iterate: invalid return from sscanf %d\n",status));
|
|---|
| 334 | }
|
|---|
| 335 | timeout = u;
|
|---|
| 336 |
|
|---|
| 337 | DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n",
|
|---|
| 338 | keystr, valstr, ctime(&timeout)));
|
|---|
| 339 | fn(keystr, valstr, timeout, data);
|
|---|
| 340 |
|
|---|
| 341 | SAFE_FREE(valstr);
|
|---|
| 342 | SAFE_FREE(entry);
|
|---|
| 343 | SAFE_FREE(keystr);
|
|---|
| 344 | node = node->next;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | tdb_search_list_free(first_node);
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | /********************************************************************
|
|---|
| 351 | lock a key
|
|---|
| 352 | ********************************************************************/
|
|---|
| 353 |
|
|---|
| 354 | int gencache_lock_entry( const char *key )
|
|---|
| 355 | {
|
|---|
| 356 | if (!gencache_init())
|
|---|
| 357 | return -1;
|
|---|
| 358 |
|
|---|
| 359 | return tdb_lock_bystring(cache, key);
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | /********************************************************************
|
|---|
| 363 | unlock a key
|
|---|
| 364 | ********************************************************************/
|
|---|
| 365 |
|
|---|
| 366 | void gencache_unlock_entry( const char *key )
|
|---|
| 367 | {
|
|---|
| 368 | if (!gencache_init())
|
|---|
| 369 | return;
|
|---|
| 370 |
|
|---|
| 371 | tdb_unlock_bystring(cache, key);
|
|---|
| 372 | return;
|
|---|
| 373 | }
|
|---|