source3/winbindd: convert to dbwrap
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 15 Jun 2012 04:11:19 +0000 (13:41 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 15 Jun 2012 04:11:19 +0000 (13:41 +0930)
We temporarily comment out the tdb_validate calls, until the next patch
where we change that to db_validate.

There's a subtlety here: rather than unlinking winbindd_cache.tdb, we
open it with O_TRUNC.  That way it works even if it's actually
winbindd_cache.ntdb.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
source3/winbindd/winbindd_cache.c
source3/winbindd/winbindd_proto.h

index 315202d618163b99499b41782c3a60bd2f9719a5..a5a85b880e276b1385f0e9266d6e550bed759d9a 100644 (file)
 #include "includes.h"
 #include "system/filesys.h"
 #include "winbindd.h"
-#include "tdb_validate.h"
+//#include "tdb_validate.h"
 #include "../libcli/auth/libcli_auth.h"
 #include "../librpc/gen_ndr/ndr_wbint.h"
 #include "ads.h"
 #include "nss_info.h"
 #include "../libcli/security/security.h"
 #include "passdb/machine_sid.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_open.h"
 #include "util_tdb.h"
 
 #undef DBGC_CLASS
@@ -94,7 +96,7 @@ static bool is_non_centry_key(TDB_DATA kbuf)
 static bool global_winbindd_offline_state;
 
 struct winbind_cache {
-       TDB_CONTEXT *tdb;
+       struct db_context *db;
 };
 
 struct cache_entry {
@@ -178,10 +180,7 @@ static struct winbind_cache *get_cache(struct winbindd_domain *domain)
        if (ret)
                return ret;
 
-       ret = SMB_XMALLOC_P(struct winbind_cache);
-       ZERO_STRUCTP(ret);
-
-       wcache = ret;
+       wcache = ret = talloc_zero(NULL, struct winbind_cache);
        wcache_flush_cache();
 
        return ret;
@@ -384,7 +383,7 @@ static bool wcache_server_down(struct winbindd_domain *domain)
 {
        bool ret;
 
-       if (!wcache->tdb)
+       if (!wcache->db)
                return false;
 
        ret = (domain->sequence_number == DOM_SEQUENCE_NONE);
@@ -401,8 +400,8 @@ static bool wcache_fetch_seqnum(const char *domain_name, uint32_t *seqnum,
        char *key;
        TDB_DATA data;
 
-       if (wcache->tdb == NULL) {
-               DEBUG(10,("wcache_fetch_seqnum: tdb == NULL\n"));
+       if (wcache->db == NULL) {
+               DEBUG(10,("wcache_fetch_seqnum: db == NULL\n"));
                return false;
        }
 
@@ -412,24 +411,24 @@ static bool wcache_fetch_seqnum(const char *domain_name, uint32_t *seqnum,
                return false;
        }
 
-       data = tdb_fetch_bystring(wcache->tdb, key);
-       TALLOC_FREE(key);
-
-       if (data.dptr == NULL) {
+       if (!NT_STATUS_IS_OK(dbwrap_fetch_bystring(wcache->db, wcache,
+                                                  key, &data))) {
+               TALLOC_FREE(key);
                DEBUG(10, ("wcache_fetch_seqnum: %s not found\n",
                           domain_name));
                return false;
        }
+       TALLOC_FREE(key);
        if (data.dsize != 8) {
                DEBUG(10, ("wcache_fetch_seqnum: invalid data size %d\n",
                           (int)data.dsize));
-               SAFE_FREE(data.dptr);
+               TALLOC_FREE(data.dptr);
                return false;
        }
 
        *seqnum = IVAL(data.dptr, 0);
        *last_seq_check = IVAL(data.dptr, 4);
-       SAFE_FREE(data.dptr);
+       TALLOC_FREE(data.dptr);
 
        return true;
 }
@@ -466,10 +465,10 @@ bool wcache_store_seqnum(const char *domain_name, uint32_t seqnum,
 {
        char *key_str;
        uint8_t buf[8];
-       int ret;
+       NTSTATUS status;
 
-       if (wcache->tdb == NULL) {
-               DEBUG(10, ("wcache_store_seqnum: wcache->tdb == NULL\n"));
+       if (wcache->db == NULL) {
+               DEBUG(10, ("wcache_store_seqnum: wcache->db == NULL\n"));
                return false;
        }
 
@@ -482,12 +481,12 @@ bool wcache_store_seqnum(const char *domain_name, uint32_t seqnum,
        SIVAL(buf, 0, seqnum);
        SIVAL(buf, 4, last_seq_check);
 
-       ret = tdb_store_bystring(wcache->tdb, key_str,
-                                make_tdb_data(buf, sizeof(buf)), TDB_REPLACE);
+       status = dbwrap_store_bystring(wcache->db, key_str,
+                                      make_tdb_data(buf, sizeof(buf)),
+                                      TDB_REPLACE);
        TALLOC_FREE(key_str);
-       if (ret != 0) {
-               DEBUG(10, ("tdb_store_bystring failed: %s\n",
-                          tdb_errorstr_compat(wcache->tdb)));
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(10, ("dbwrap_store_bystring failed\n"));
                TALLOC_FREE(key_str);
                return false;
        }
@@ -639,14 +638,21 @@ static struct cache_entry *wcache_fetch_raw(char *kstr)
        TDB_DATA key;
 
        key = string_tdb_data(kstr);
-       data = tdb_fetch_compat(wcache->tdb, key);
-       if (!data.dptr) {
+       if (!NT_STATUS_IS_OK(dbwrap_fetch(wcache->db, wcache, key, &data))) {
                /* a cache miss */
                return NULL;
        }
 
        centry = SMB_XMALLOC_P(struct cache_entry);
-       centry->data = (unsigned char *)data.dptr;
+       centry->data = (unsigned char *)memdup(data.dptr, data.dsize);
+       TALLOC_FREE(data.dptr);
+       if (!centry->data) {
+               DEBUG(0,("wcache_fetch_raw: alloc of %zu bytes failed\n",
+                        data.dsize));
+               free(centry);
+               return NULL;
+       }
+
        centry->len = data.dsize;
        centry->ofs = 0;
 
@@ -748,7 +754,7 @@ static void wcache_delete(const char *format, ...)
 
        key = string_tdb_data(kstr);
 
-       tdb_delete(wcache->tdb, key);
+       dbwrap_delete(wcache->db, key);
        free(kstr);
 }
 
@@ -890,7 +896,7 @@ struct cache_entry *centry_start(struct winbindd_domain *domain, NTSTATUS status
 {
        struct cache_entry *centry;
 
-       if (!wcache->tdb)
+       if (!wcache->db)
                return NULL;
 
        centry = SMB_XMALLOC_P(struct cache_entry);
@@ -907,7 +913,7 @@ struct cache_entry *centry_start(struct winbindd_domain *domain, NTSTATUS status
 }
 
 /*
-  finish a centry and write it to the tdb
+  finish a centry and write it to the db
 */
 static void centry_end(struct cache_entry *centry, const char *format, ...) PRINTF_ATTRIBUTE(2,3);
 static void centry_end(struct cache_entry *centry, const char *format, ...)
@@ -928,7 +934,7 @@ static void centry_end(struct cache_entry *centry, const char *format, ...)
        data.dptr = centry->data;
        data.dsize = centry->ofs;
 
-       tdb_store(wcache->tdb, key, data, TDB_REPLACE);
+       dbwrap_store(wcache->db, key, data, TDB_REPLACE);
        free(kstr);
 }
 
@@ -1108,7 +1114,7 @@ NTSTATUS resolve_username_to_alias( TALLOC_CTX *mem_ctx,
        if ( domain->internal )
                return NT_STATUS_NOT_SUPPORTED;
 
-       if (!cache->tdb)
+       if (!cache->db)
                goto do_query;
 
        if ( (upper_name = SMB_STRDUP(name)) == NULL )
@@ -1183,7 +1189,7 @@ NTSTATUS resolve_alias_to_username( TALLOC_CTX *mem_ctx,
        if ( domain->internal )
                return  NT_STATUS_NOT_SUPPORTED;
 
-       if (!cache->tdb)
+       if (!cache->db)
                goto do_query;
 
        if ( (upper_name = SMB_STRDUP(alias)) == NULL )
@@ -1257,8 +1263,9 @@ NTSTATUS wcache_cached_creds_exist(struct winbindd_domain *domain, const struct
        TDB_DATA data;
        fstring key_str, tmp;
        uint32 rid;
+       NTSTATUS status;
 
-       if (!cache->tdb) {
+       if (!cache->db) {
                return NT_STATUS_INTERNAL_DB_ERROR;
        }
 
@@ -1272,13 +1279,11 @@ NTSTATUS wcache_cached_creds_exist(struct winbindd_domain *domain, const struct
 
        fstr_sprintf(key_str, "CRED/%s", sid_to_fstring(tmp, sid));
 
-       data = tdb_fetch_compat(cache->tdb, string_tdb_data(key_str));
-       if (!data.dptr) {
-               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       status = dbwrap_fetch(cache->db, NULL, string_tdb_data(key_str), &data);
+       if (NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(data.dptr);
        }
-
-       SAFE_FREE(data.dptr);
-       return NT_STATUS_OK;
+       return status;
 }
 
 /* Lookup creds for a SID - copes with old (unsalted) creds as well
@@ -1300,7 +1305,7 @@ NTSTATUS wcache_get_creds(struct winbindd_domain *domain,
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
-       if (!cache->tdb) {
+       if (!cache->db) {
                return NT_STATUS_INTERNAL_DB_ERROR;
        }
 
@@ -1421,7 +1426,7 @@ static NTSTATUS query_user_list(struct winbindd_domain *domain,
        unsigned int i, retry;
        bool old_status = domain->online;
 
-       if (!cache->tdb)
+       if (!cache->db)
                goto do_query;
 
        centry = wcache_fetch(cache, domain, "UL/%s", domain->name);
@@ -1506,7 +1511,7 @@ do_query:
                         * domain is offline now, and there is no user entries,
                         * try to fetch from cache again.
                         */
-                       if (cache->tdb && !domain->online && !domain->internal && old_status) {
+                       if (cache->db && !domain->online && !domain->internal && old_status) {
                                centry = wcache_fetch(cache, domain, "UL/%s", domain->name);
                                /* partial response... */
                                if (!centry) {
@@ -1573,7 +1578,7 @@ static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
        bool old_status;
 
        old_status = domain->online;
-       if (!cache->tdb)
+       if (!cache->db)
                goto do_query;
 
        centry = wcache_fetch(cache, domain, "GL/%s/domain", domain->name);
@@ -1624,7 +1629,7 @@ do_query:
                if (!domain->internal && old_status) {
                        set_domain_offline(domain);
                }
-               if (cache->tdb &&
+               if (cache->db &&
                        !domain->online &&
                        !domain->internal &&
                        old_status) {
@@ -1668,7 +1673,7 @@ static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
        bool old_status;
 
        old_status = domain->online;
-       if (!cache->tdb)
+       if (!cache->db)
                goto do_query;
 
        centry = wcache_fetch(cache, domain, "GL/%s/local", domain->name);
@@ -1729,7 +1734,7 @@ do_query:
                if (!domain->internal && old_status) {
                        set_domain_offline(domain);
                }
-               if (cache->tdb &&
+               if (cache->db &&
                        !domain->internal &&
                        !domain->online &&
                        old_status) {
@@ -1771,7 +1776,7 @@ NTSTATUS wcache_name_to_sid(struct winbindd_domain *domain,
        NTSTATUS status;
        char *uname;
 
-       if (cache->tdb == NULL) {
+       if (cache->db == NULL) {
                return NT_STATUS_NOT_FOUND;
        }
 
@@ -1880,7 +1885,7 @@ NTSTATUS wcache_sid_to_name(struct winbindd_domain *domain,
        char *sid_string;
        NTSTATUS status;
 
-       if (cache->tdb == NULL) {
+       if (cache->db == NULL) {
                return NT_STATUS_NOT_FOUND;
        }
 
@@ -1996,7 +2001,7 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
        *names = NULL;
        *types = NULL;
 
-       if (!cache->tdb) {
+       if (!cache->db) {
                goto do_query;
        }
 
@@ -2082,7 +2087,7 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
                if (!domain->internal && old_status) {
                        set_domain_offline(domain);
                }
-               if (cache->tdb &&
+               if (cache->db &&
                        !domain->internal &&
                        !domain->online &&
                        old_status) {
@@ -2209,7 +2214,7 @@ NTSTATUS wcache_query_user(struct winbindd_domain *domain,
        NTSTATUS status;
        char *sid_string;
 
-       if (cache->tdb == NULL) {
+       if (cache->db == NULL) {
                return NT_STATUS_NOT_FOUND;
        }
 
@@ -2322,7 +2327,7 @@ NTSTATUS wcache_lookup_usergroups(struct winbindd_domain *domain,
        struct dom_sid *sids;
        fstring sid_string;
 
-       if (cache->tdb == NULL) {
+       if (cache->db == NULL) {
                return NT_STATUS_NOT_FOUND;
        }
 
@@ -2472,7 +2477,7 @@ NTSTATUS wcache_lookup_useraliases(struct winbindd_domain *domain,
        char *sidlist;
        int i;
 
-       if (cache->tdb == NULL) {
+       if (cache->db == NULL) {
                return NT_STATUS_NOT_FOUND;
        }
 
@@ -2601,7 +2606,7 @@ NTSTATUS wcache_lookup_groupmem(struct winbindd_domain *domain,
        unsigned int i;
        char *sid_string;
 
-       if (cache->tdb == NULL) {
+       if (cache->db == NULL) {
                return NT_STATUS_NOT_FOUND;
        }
 
@@ -2757,7 +2762,7 @@ static NTSTATUS trusted_domains(struct winbindd_domain *domain,
        trusts->array = NULL;
 
        cache = get_cache(domain);
-       if (!cache || !cache->tdb) {
+       if (!cache || !cache->db) {
                goto do_query;
        }
 
@@ -2858,7 +2863,7 @@ static NTSTATUS lockout_policy(struct winbindd_domain *domain,
        bool old_status;
 
        old_status = domain->online;
-       if (!cache->tdb)
+       if (!cache->db)
                goto do_query;
 
        centry = wcache_fetch(cache, domain, "LOC_POL/%s", domain->name);
@@ -2897,7 +2902,7 @@ do_query:
                if (!domain->internal && old_status) {
                        set_domain_offline(domain);
                }
-               if (cache->tdb &&
+               if (cache->db &&
                        !domain->internal &&
                        !domain->online &&
                        old_status) {
@@ -2928,7 +2933,7 @@ static NTSTATUS password_policy(struct winbindd_domain *domain,
        bool old_status;
 
        old_status = domain->online;
-       if (!cache->tdb)
+       if (!cache->db)
                goto do_query;
 
        centry = wcache_fetch(cache, domain, "PWD_POL/%s", domain->name);
@@ -2969,7 +2974,7 @@ do_query:
                if (!domain->internal && old_status) {
                        set_domain_offline(domain);
                }
-               if (cache->tdb &&
+               if (cache->db &&
                        !domain->internal &&
                        !domain->online &&
                        old_status) {
@@ -2992,12 +2997,14 @@ do_query:
 
 /* Invalidate cached user and group lists coherently */
 
-static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, 
-                      void *state)
+static int traverse_fn(struct db_record *rec, void *state)
 {
+       TDB_DATA kbuf = dbwrap_record_get_key(rec);
+
        if (strncmp((const char *)kbuf.dptr, "UL/", 3) == 0 ||
-           strncmp((const char *)kbuf.dptr, "GL/", 3) == 0)
-               tdb_delete(the_tdb, kbuf);
+           strncmp((const char *)kbuf.dptr, "GL/", 3) == 0) {
+               dbwrap_record_delete(rec);
+       }
 
        return 0;
 }
@@ -3022,19 +3029,19 @@ void wcache_invalidate_samlogon(struct winbindd_domain *domain,
 
        cache = get_cache(domain);
 
-        if (!cache->tdb) {
+        if (!cache->db) {
                 return;
         }
 
        /* Clear U/SID cache entry */
        fstr_sprintf(key_str, "U/%s", sid_to_fstring(sid_string, sid));
        DEBUG(10, ("wcache_invalidate_samlogon: clearing %s\n", key_str));
-       tdb_delete(cache->tdb, string_tdb_data(key_str));
+       dbwrap_delete(cache->db, string_tdb_data(key_str));
 
        /* Clear UG/SID cache entry */
        fstr_sprintf(key_str, "UG/%s", sid_to_fstring(sid_string, sid));
        DEBUG(10, ("wcache_invalidate_samlogon: clearing %s\n", key_str));
-       tdb_delete(cache->tdb, string_tdb_data(key_str));
+       dbwrap_delete(cache->db, string_tdb_data(key_str));
 
        /* Samba/winbindd never needs this. */
        netsamlogon_clear_cached_user(sid);
@@ -3050,8 +3057,9 @@ bool wcache_invalidate_cache(void)
                DEBUG(10, ("wcache_invalidate_cache: invalidating cache "
                           "entries for %s\n", domain->name));
                if (cache) {
-                       if (cache->tdb) {
-                               tdb_traverse(cache->tdb, traverse_fn, NULL);
+                       if (cache->db) {
+                               dbwrap_traverse(cache->db, traverse_fn, NULL,
+                                               NULL);
                        } else {
                                return false;
                        }
@@ -3077,8 +3085,9 @@ bool wcache_invalidate_cache_noinit(void)
                DEBUG(10, ("wcache_invalidate_cache: invalidating cache "
                           "entries for %s\n", domain->name));
                if (cache) {
-                       if (cache->tdb) {
-                               tdb_traverse(cache->tdb, traverse_fn, NULL);
+                       if (cache->db) {
+                               dbwrap_traverse(cache->db, traverse_fn, NULL,
+                                               NULL);
                                /*
                                 * Flushing cache has nothing to with domains.
                                 * return here if we successfully flushed once.
@@ -3093,24 +3102,32 @@ bool wcache_invalidate_cache_noinit(void)
        return true;
 }
 
-bool init_wcache(void)
+static bool init_wcache(bool do_truncate)
 {
+       int open_flags = O_RDWR|O_CREAT;
+
+       if (do_truncate) {
+               open_flags |= O_TRUNC;
+       }
+
        if (wcache == NULL) {
-               wcache = SMB_XMALLOC_P(struct winbind_cache);
-               ZERO_STRUCTP(wcache);
+               wcache = talloc_zero(NULL, struct winbind_cache);
+               if (!wcache) {
+                       smb_panic("Could not allocate wcache");
+               }
        }
 
-       if (wcache->tdb != NULL)
+       if (wcache->db != NULL)
                return true;
 
        /* when working offline we must not clear the cache on restart */
-       wcache->tdb = tdb_open_log(state_path("winbindd_cache.tdb"),
-                               WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE, 
-                               TDB_INCOMPATIBLE_HASH |
-                                       (lp_winbind_offline_logon() ? TDB_DEFAULT : (TDB_DEFAULT | TDB_CLEAR_IF_FIRST)),
-                               O_RDWR|O_CREAT, 0600);
+       wcache->db = db_s3open(wcache, state_path("winbindd_cache.tdb"),
+                              WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE,
+                              TDB_INCOMPATIBLE_HASH |
+                              (lp_winbind_offline_logon() ? TDB_DEFAULT : (TDB_DEFAULT | TDB_CLEAR_IF_FIRST)),
+                              open_flags, 0600);
 
-       if (wcache->tdb == NULL) {
+       if (wcache->db == NULL) {
                DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
                return false;
        }
@@ -3128,15 +3145,17 @@ bool initialize_winbindd_cache(void)
 {
        bool cache_bad = true;
        uint32 vers;
+       NTSTATUS status;
 
-       if (!init_wcache()) {
+       if (!init_wcache(false)) {
                DEBUG(0,("initialize_winbindd_cache: init_wcache failed.\n"));
                return false;
        }
 
        /* Check version number. */
-       if (tdb_fetch_uint32(wcache->tdb, WINBINDD_CACHE_VERSION_KEYSTR, &vers) &&
-                       vers == WINBINDD_CACHE_VERSION) {
+       status = dbwrap_fetch_uint32(wcache->db, WINBINDD_CACHE_VERSION_KEYSTR,
+                                    &vers);
+       if (NT_STATUS_IS_OK(status) && vers == WINBINDD_CACHE_VERSION) {
                cache_bad = false;
        }
 
@@ -3145,31 +3164,25 @@ bool initialize_winbindd_cache(void)
                        "and re-creating with version number %d\n",
                        WINBINDD_CACHE_VERSION ));
 
-               tdb_close(wcache->tdb);
-               wcache->tdb = NULL;
+               TALLOC_FREE(wcache->db);
 
-               if (unlink(state_path("winbindd_cache.tdb")) == -1) {
-                       DEBUG(0,("initialize_winbindd_cache: unlink %s failed %s ",
-                               state_path("winbindd_cache.tdb"),
-                               strerror(errno) ));
-                       return false;
-               }
-               if (!init_wcache()) {
+               if (!init_wcache(true)) {
                        DEBUG(0,("initialize_winbindd_cache: re-initialization "
                                        "init_wcache failed.\n"));
                        return false;
                }
 
                /* Write the version. */
-               if (!tdb_store_uint32(wcache->tdb, WINBINDD_CACHE_VERSION_KEYSTR, WINBINDD_CACHE_VERSION)) {
-                       DEBUG(0,("initialize_winbindd_cache: version number store failed %s\n",
-                               tdb_errorstr_compat(wcache->tdb) ));
+               status = dbwrap_store_uint32(wcache->db,
+                                            WINBINDD_CACHE_VERSION_KEYSTR,
+                                            WINBINDD_CACHE_VERSION);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0,("initialize_winbindd_cache: version number store failed\n"));
                        return false;
                }
        }
 
-       tdb_close(wcache->tdb);
-       wcache->tdb = NULL;
+       TALLOC_FREE(wcache->db);
        return true;
 }
 
@@ -3178,10 +3191,7 @@ void close_winbindd_cache(void)
        if (!wcache) {
                return;
        }
-       if (wcache->tdb) {
-               tdb_close(wcache->tdb);
-               wcache->tdb = NULL;
-       }
+       TALLOC_FREE(wcache->db);
 }
 
 bool lookup_cached_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
@@ -3240,10 +3250,10 @@ void cache_name2sid(struct winbindd_domain *domain,
  * ignore these things on cleanup.
  */
 
-static int traverse_fn_cleanup(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, 
-                              TDB_DATA dbuf, void *state)
+static int traverse_fn_cleanup(struct db_record *rec, void *state)
 {
        struct cache_entry *centry;
+       TDB_DATA kbuf = dbwrap_record_get_key(rec);
 
        if (is_non_centry_key(kbuf)) {
                return 0;
@@ -3256,7 +3266,7 @@ static int traverse_fn_cleanup(TDB_CONTEXT *the_tdb, TDB_DATA kbuf,
 
        if (!NT_STATUS_IS_OK(centry->status)) {
                DEBUG(10,("deleting centry %s\n", (const char *)kbuf.dptr));
-               tdb_delete(the_tdb, kbuf);
+               dbwrap_record_delete(rec);
        }
 
        centry_free(centry);
@@ -3268,38 +3278,36 @@ void wcache_flush_cache(void)
 {
        if (!wcache)
                return;
-       if (wcache->tdb) {
-               tdb_close(wcache->tdb);
-               wcache->tdb = NULL;
-       }
+       TALLOC_FREE(wcache->db);
        if (!winbindd_use_cache()) {
                return;
        }
 
        /* when working offline we must not clear the cache on restart */
-       wcache->tdb = tdb_open_log(state_path("winbindd_cache.tdb"),
-                               WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE, 
-                               TDB_INCOMPATIBLE_HASH |
-                               (lp_winbind_offline_logon() ? TDB_DEFAULT : (TDB_DEFAULT | TDB_CLEAR_IF_FIRST)),
-                               O_RDWR|O_CREAT, 0600);
-
-       if (!wcache->tdb) {
+       wcache->db = db_s3open(wcache,
+                              state_path("winbindd_cache.tdb"),
+                              WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE,
+                              TDB_INCOMPATIBLE_HASH |
+                              (lp_winbind_offline_logon() ? TDB_DEFAULT : (TDB_DEFAULT | TDB_CLEAR_IF_FIRST)),
+                              O_RDWR|O_CREAT, 0600);
+
+       if (!wcache->db) {
                DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
                return;
        }
 
-       tdb_traverse(wcache->tdb, traverse_fn_cleanup, NULL);
+       dbwrap_traverse(wcache->db, traverse_fn_cleanup, NULL, NULL);
 
        DEBUG(10,("wcache_flush_cache success\n"));
 }
 
 /* Count cached creds */
 
-static int traverse_fn_cached_creds(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, 
-                                   void *state)
+static int traverse_fn_cached_creds(struct db_record *rec, void *state)
 {
        int *cred_count = (int*)state;
+       TDB_DATA kbuf = dbwrap_record_get_key(rec);
+
        if (strncmp((const char *)kbuf.dptr, "CRED/", 5) == 0) {
                (*cred_count)++;
        }
@@ -3312,11 +3320,12 @@ NTSTATUS wcache_count_cached_creds(struct winbindd_domain *domain, int *count)
 
        *count = 0;
 
-       if (!cache->tdb) {
+       if (!cache->db) {
                return NT_STATUS_INTERNAL_DB_ERROR;
        }
  
-       tdb_traverse(cache->tdb, traverse_fn_cached_creds, (void *)count);
+       dbwrap_traverse(cache->db, traverse_fn_cached_creds, (void *)count,
+                       NULL);
 
        return NT_STATUS_OK;
 }
@@ -3329,10 +3338,10 @@ struct cred_list {
 };
 static struct cred_list *wcache_cred_list;
 
-static int traverse_fn_get_credlist(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, 
-                                   void *state)
+static int traverse_fn_get_credlist(struct db_record *rec, void *state)
 {
        struct cred_list *cred;
+       TDB_DATA kbuf = dbwrap_record_get_key(rec);
 
        if (strncmp((const char *)kbuf.dptr, "CRED/", 5) == 0) {
 
@@ -3357,10 +3366,10 @@ NTSTATUS wcache_remove_oldest_cached_creds(struct winbindd_domain *domain, const
 {
        struct winbind_cache *cache = get_cache(domain);
        NTSTATUS status;
-       int ret;
+       int count;
        struct cred_list *cred, *oldest = NULL;
 
-       if (!cache->tdb) {
+       if (!cache->db) {
                return NT_STATUS_INTERNAL_DB_ERROR;
        }
 
@@ -3373,15 +3382,20 @@ NTSTATUS wcache_remove_oldest_cached_creds(struct winbindd_domain *domain, const
 
                fstr_sprintf(key_str, "CRED/%s", sid_to_fstring(tmp, sid));
 
-               tdb_delete(cache->tdb, string_tdb_data(key_str));
+               dbwrap_delete(cache->db, string_tdb_data(key_str));
 
                return NT_STATUS_OK;
        }
 
-       ret = tdb_traverse(cache->tdb, traverse_fn_get_credlist, NULL);
-       if (ret == 0) {
+       status = dbwrap_traverse(cache->db, traverse_fn_get_credlist, NULL,
+                                &count);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+       if (count == 0) {
                return NT_STATUS_OK;
-       } else if ((ret < 0) || (wcache_cred_list == NULL)) {
+       }
+       if (wcache_cred_list == NULL) {
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
@@ -3392,16 +3406,16 @@ NTSTATUS wcache_remove_oldest_cached_creds(struct winbindd_domain *domain, const
                TDB_DATA data;
                time_t t;
 
-               data = tdb_fetch_compat(cache->tdb, string_tdb_data(cred->name));
-               if (!data.dptr) {
+               status = dbwrap_fetch(cache->db, NULL,
+                                     string_tdb_data(cred->name), &data);
+               if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(10,("wcache_remove_oldest_cached_creds: entry for [%s] not found\n", 
                                cred->name));
-                       status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
                        goto done;
                }
 
                t = IVAL(data.dptr, 0);
-               SAFE_FREE(data.dptr);
+               TALLOC_FREE(data.dptr);
 
                if (!oldest) {
                        oldest = SMB_MALLOC_P(struct cred_list);
@@ -3421,11 +3435,7 @@ NTSTATUS wcache_remove_oldest_cached_creds(struct winbindd_domain *domain, const
                }
        }
 
-       if (tdb_delete(cache->tdb, string_tdb_data(oldest->name)) == 0) {
-               status = NT_STATUS_OK;
-       } else {
-               status = NT_STATUS_UNSUCCESSFUL;
-       }
+       status = dbwrap_delete(cache->db, string_tdb_data(oldest->name));
 done:
        SAFE_FREE(wcache_cred_list);
        SAFE_FREE(oldest);
@@ -3437,13 +3447,14 @@ done:
 bool set_global_winbindd_state_offline(void)
 {
        TDB_DATA data;
+       NTSTATUS status;
 
        DEBUG(10,("set_global_winbindd_state_offline: offline requested.\n"));
 
        /* Only go offline if someone has created
           the key "WINBINDD_OFFLINE" in the cache tdb. */
 
-       if (wcache == NULL || wcache->tdb == NULL) {
+       if (wcache == NULL || wcache->db == NULL) {
                DEBUG(10,("set_global_winbindd_state_offline: wcache not open yet.\n"));
                return false;
        }
@@ -3458,16 +3469,19 @@ bool set_global_winbindd_state_offline(void)
                return true;
        }
 
-       data = tdb_fetch_bystring( wcache->tdb, "WINBINDD_OFFLINE" );
+       status = dbwrap_fetch_bystring(wcache->db, NULL,
+                                      "WINBINDD_OFFLINE", &data);
 
-       if (!data.dptr || data.dsize != 4) {
+       if (!NT_STATUS_IS_OK(status) || data.dsize != 4) {
                DEBUG(10,("set_global_winbindd_state_offline: offline state not set.\n"));
-               SAFE_FREE(data.dptr);
+               if (NT_STATUS_IS_OK(status)) {
+                       TALLOC_FREE(data.dptr);
+               }
                return false;
        } else {
                DEBUG(10,("set_global_winbindd_state_offline: offline state set.\n"));
                global_winbindd_offline_state = true;
-               SAFE_FREE(data.dptr);
+               TALLOC_FREE(data.dptr);
                return true;
        }
 }
@@ -3487,12 +3501,12 @@ void set_global_winbindd_state_online(void)
        }
        global_winbindd_offline_state = false;
 
-       if (!wcache->tdb) {
+       if (!wcache->db) {
                return;
        }
 
        /* Ensure there is no key "WINBINDD_OFFLINE" in the cache tdb. */
-       tdb_delete_bystring(wcache->tdb, "WINBINDD_OFFLINE");
+       dbwrap_delete_bystring(wcache->db, "WINBINDD_OFFLINE");
 }
 
 bool get_global_winbindd_state_offline(void)
@@ -3500,8 +3514,9 @@ bool get_global_winbindd_state_offline(void)
        return global_winbindd_offline_state;
 }
 
+#if 0
 /***********************************************************************
- Validate functions for all possible cache tdb keys.
+ Validate functions for all possible cache db keys.
 ***********************************************************************/
 
 static struct cache_entry *create_centry_validate(const char *kstr, TDB_DATA data, 
@@ -4020,10 +4035,12 @@ struct key_val_struct {
  possible.
 ***********************************************************************/
 
-static int cache_traverse_validate_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
+static int cache_traverse_validate_fn(struct db_record *rec, void *state)
 {
        int i;
        unsigned int max_key_len = 1024;
+       TDB_DATA kbuf = dbwrap_record_get_key(rec);
+       TDB_DATA dbuf = dbwrap_record_get_value(rec);
        struct tdb_validation_status *v_state = (struct tdb_validation_status *)state;
 
        /* Paranoia check. */
@@ -4075,6 +4092,7 @@ static int cache_traverse_validate_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_D
        v_state->success = false;
        return 1; /* terminate. */
 }
+#endif
 
 static void validate_panic(const char *const why)
 {
@@ -4083,11 +4101,10 @@ static void validate_panic(const char *const why)
        exit(47);
 }
 
-static int wbcache_update_centry_fn(TDB_CONTEXT *tdb,
-                                   TDB_DATA key,
-                                   TDB_DATA data,
-                                   void *state)
+static int wbcache_update_centry_fn(struct db_record *rec, void *state)
 {
+       TDB_DATA key = dbwrap_record_get_key(rec);
+       TDB_DATA data = dbwrap_record_get_value(rec);
        uint64_t ctimeout;
        TDB_DATA blob;
 
@@ -4096,7 +4113,7 @@ static int wbcache_update_centry_fn(TDB_CONTEXT *tdb,
        }
 
        if (data.dptr == NULL || data.dsize == 0) {
-               if (tdb_delete(tdb, key) < 0) {
+               if (!NT_STATUS_IS_OK(dbwrap_record_delete(rec))) {
                        DEBUG(0, ("tdb_delete for [%s] failed!\n",
                                  key.dptr));
                        return 1;
@@ -4122,8 +4139,8 @@ static int wbcache_update_centry_fn(TDB_CONTEXT *tdb,
        /* copy the rest */
        memcpy(blob.dptr + 16, data.dptr + 8, data.dsize - 8);
 
-       if (tdb_store(tdb, key, blob, TDB_REPLACE) < 0) {
-               DEBUG(0, ("tdb_store to update [%s] failed!\n",
+       if (!NT_STATUS_IS_OK(dbwrap_record_store(rec, blob, TDB_REPLACE))) {
+               DEBUG(0, ("tdbwrap_store to update [%s] failed!\n",
                          key.dptr));
                SAFE_FREE(blob.dptr);
                return 1;
@@ -4133,14 +4150,14 @@ static int wbcache_update_centry_fn(TDB_CONTEXT *tdb,
        return 0;
 }
 
-static bool wbcache_upgrade_v1_to_v2(TDB_CONTEXT *tdb)
+static bool wbcache_upgrade_v1_to_v2(struct db_context *db)
 {
-       int rc;
+       NTSTATUS status;
 
        DEBUG(1, ("Upgrade to version 2 of the winbindd_cache.tdb\n"));
 
-       rc = tdb_traverse(tdb, wbcache_update_centry_fn, NULL);
-       if (rc < 0) {
+       status = dbwrap_traverse(db, wbcache_update_centry_fn, NULL, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
                return false;
        }
 
@@ -4156,59 +4173,68 @@ int winbindd_validate_cache(void)
 {
        int ret = -1;
        const char *tdb_path = state_path("winbindd_cache.tdb");
-       TDB_CONTEXT *tdb = NULL;
+       struct db_context *db = NULL;
        uint32_t vers_id;
+       NTSTATUS status;
        bool ok;
 
        DEBUG(10, ("winbindd_validate_cache: replacing panic function\n"));
        smb_panic_fn = validate_panic;
 
-       tdb = tdb_open_log(tdb_path, 
-                          WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE,
-                          TDB_INCOMPATIBLE_HASH |
-                          ( lp_winbind_offline_logon() 
-                            ? TDB_DEFAULT 
-                            : TDB_DEFAULT | TDB_CLEAR_IF_FIRST ),
-                          O_RDWR|O_CREAT, 
-                          0600);
-       if (!tdb) {
+       db = db_s3open(NULL, tdb_path,
+                      WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE,
+                      TDB_INCOMPATIBLE_HASH |
+                      ( lp_winbind_offline_logon()
+                        ? TDB_DEFAULT
+                        : TDB_DEFAULT | TDB_CLEAR_IF_FIRST ),
+                      O_RDWR|O_CREAT,
+                      0600);
+       if (!db) {
                DEBUG(0, ("winbindd_validate_cache: "
                          "error opening/initializing tdb\n"));
                goto done;
        }
 
        /* Version check and upgrade code. */
-       if (!tdb_fetch_uint32(tdb, WINBINDD_CACHE_VERSION_KEYSTR, &vers_id)) {
+       status = dbwrap_fetch_uint32(db, WINBINDD_CACHE_VERSION_KEYSTR,
+                                    &vers_id);
+       if (!NT_STATUS_IS_OK(status)) {
                DEBUG(10, ("Fresh database\n"));
-               tdb_store_uint32(tdb, WINBINDD_CACHE_VERSION_KEYSTR, WINBINDD_CACHE_VERSION);
+               dbwrap_store_uint32(db, WINBINDD_CACHE_VERSION_KEYSTR,
+                                   WINBINDD_CACHE_VERSION);
                vers_id = WINBINDD_CACHE_VERSION;
        }
 
        if (vers_id != WINBINDD_CACHE_VERSION) {
                if (vers_id == WINBINDD_CACHE_VER1) {
-                       ok = wbcache_upgrade_v1_to_v2(tdb);
+                       ok = wbcache_upgrade_v1_to_v2(db);
                        if (!ok) {
                                DEBUG(10, ("winbindd_validate_cache: upgrade to version 2 failed.\n"));
-                               unlink(tdb_path);
+                               unlink(dbwrap_name(db));
                                goto done;
                        }
 
-                       tdb_store_uint32(tdb,
-                                        WINBINDD_CACHE_VERSION_KEYSTR,
-                                        WINBINDD_CACHE_VERSION);
+                       dbwrap_store_uint32(db,
+                                           WINBINDD_CACHE_VERSION_KEYSTR,
+                                           WINBINDD_CACHE_VERSION);
                        vers_id = WINBINDD_CACHE_VER2;
                }
        }
 
-       tdb_close(tdb);
+       TALLOC_FREE(db);
 
-       ret = tdb_validate_and_backup(tdb_path, cache_traverse_validate_fn);
+#if 0
+       ret = db_validate_and_backup(tdb_path, cache_traverse_validate_fn);
 
        if (ret != 0) {
+               const char *ntdb_path = state_path("winbindd_cache.ntdb");
                DEBUG(10, ("winbindd_validate_cache: validation not successful.\n"));
                DEBUGADD(10, ("removing tdb %s.\n", tdb_path));
+               /* Hard to know which was opened: delete both */
                unlink(tdb_path);
+               unlink(ntdb_path);
        }
+#endif
 
 done:
        DEBUG(10, ("winbindd_validate_cache: restoring panic function\n"));
@@ -4228,17 +4254,18 @@ int winbindd_validate_cache_nobackup(void)
        DEBUG(10, ("winbindd_validate_cache: replacing panic function\n"));
        smb_panic_fn = validate_panic;
 
-
-       if (wcache == NULL || wcache->tdb == NULL) {
-               ret = tdb_validate_open(tdb_path, cache_traverse_validate_fn);
+#if 0
+       if (wcache == NULL || wcache->db == NULL) {
+               ret = db_validate_open(tdb_path, cache_traverse_validate_fn);
        } else {
-               ret = tdb_validate(wcache->tdb, cache_traverse_validate_fn);
+               ret = db_validate(wcache->db, cache_traverse_validate_fn);
        }
 
        if (ret != 0) {
                DEBUG(10, ("winbindd_validate_cache_nobackup: validation not "
                           "successful.\n"));
        }
+#endif
 
        DEBUG(10, ("winbindd_validate_cache_nobackup: restoring panic "
                   "function\n"));
@@ -4482,7 +4509,7 @@ static bool wcache_tdc_store_list( struct winbindd_tdc_domain *domains, size_t n
 {
        TDB_DATA key = make_tdc_key( lp_workgroup() );   
        TDB_DATA data = { NULL, 0 };
-       int ret;
+       NTSTATUS status;
 
        if ( !key.dptr )
                return false;
@@ -4490,24 +4517,24 @@ static bool wcache_tdc_store_list( struct winbindd_tdc_domain *domains, size_t n
        /* See if we were asked to delete the cache entry */
 
        if ( !domains ) {
-               ret = tdb_delete( wcache->tdb, key );
+               status = dbwrap_delete(wcache->db, key);
                goto done;
        }
 
        data.dsize = pack_tdc_domains( domains, num_domains, &data.dptr );
 
        if ( !data.dptr ) {
-               ret = -1;
+               status = NT_STATUS_NO_MEMORY;
                goto done;
        }
 
-       ret = tdb_store( wcache->tdb, key, data, 0 );
+       status = dbwrap_store(wcache->db, key, data, 0);
 
  done:
        SAFE_FREE( data.dptr );
        SAFE_FREE( key.dptr );
 
-       return ( ret == 0 );
+       return NT_STATUS_IS_OK(status);
 }
 
 /*********************************************************************
@@ -4524,16 +4551,15 @@ bool wcache_tdc_fetch_list( struct winbindd_tdc_domain **domains, size_t *num_do
        if ( !key.dptr )
                return false;
 
-       data = tdb_fetch_compat( wcache->tdb, key );
-
-       SAFE_FREE( key.dptr );
-
-       if ( !data.dptr ) 
+       if (!NT_STATUS_IS_OK(dbwrap_fetch(wcache->db, NULL, key, &data))) {
+               SAFE_FREE( key.dptr );
                return false;
+       }
+       SAFE_FREE( key.dptr );
 
        *num_domains = unpack_tdc_domains( data.dptr, data.dsize, domains );
 
-       SAFE_FREE( data.dptr );
+       TALLOC_FREE( data.dptr );
 
        if ( !*domains )
                return false;
@@ -4558,7 +4584,7 @@ bool wcache_tdc_add_domain( struct winbindd_domain *domain )
                  domain->domain_trust_attribs,
                  domain->domain_type));        
 
-       if ( !init_wcache() ) {
+       if ( !init_wcache(false) ) {
                return false;
        }
 
@@ -4599,7 +4625,7 @@ struct winbindd_tdc_domain * wcache_tdc_fetch_domain( TALLOC_CTX *ctx, const cha
 
        DEBUG(10,("wcache_tdc_fetch_domain: Searching for domain %s\n", name));
 
-       if ( !init_wcache() ) {
+       if ( !init_wcache(false) ) {
                return NULL;
        }
 
@@ -4649,7 +4675,7 @@ struct winbindd_tdc_domain*
        DEBUG(10,("wcache_tdc_fetch_domainbysid: Searching for domain %s\n",
                  sid_string_dbg(sid)));
 
-       if (!init_wcache()) {
+       if (!init_wcache(false)) {
                return NULL;
        }
 
@@ -4692,7 +4718,7 @@ struct winbindd_tdc_domain*
 
 void wcache_tdc_clear( void )
 {
-       if ( !init_wcache() )
+       if ( !init_wcache(false) )
                return;
 
        wcache_tdc_store_list( NULL, 0 );
@@ -4743,7 +4769,7 @@ NTSTATUS nss_get_info_cached( struct winbindd_domain *domain,
        NTSTATUS nt_status;
        fstring tmp;
 
-       if (!cache->tdb)
+       if (!cache->db)
                goto do_query;
 
        centry = wcache_fetch(cache, domain, "NSS/PWINFO/%s",
@@ -4862,19 +4888,18 @@ bool wcache_fetch_ndr(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
                return false;
        }
 
-       if (wcache->tdb == NULL) {
+       if (wcache->db == NULL) {
                return false;
        }
 
        if (!wcache_ndr_key(talloc_tos(), domain->name, opnum, req, &key)) {
                return false;
        }
-       data = tdb_fetch_compat(wcache->tdb, key);
-       TALLOC_FREE(key.dptr);
-
-       if (data.dptr == NULL) {
+       if (!NT_STATUS_IS_OK(dbwrap_fetch(wcache->db, NULL, key, &data))) {
+               TALLOC_FREE(key.dptr);
                return false;
        }
+       TALLOC_FREE(key.dptr);
        if (data.dsize < 12) {
                goto fail;
        }
@@ -4910,7 +4935,7 @@ bool wcache_fetch_ndr(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
 
        ret = true;
 fail:
-       SAFE_FREE(data.dptr);
+       TALLOC_FREE(data.dptr);
        return ret;
 }
 
@@ -4927,7 +4952,7 @@ void wcache_store_ndr(struct winbindd_domain *domain, uint32_t opnum,
                return;
        }
 
-       if (wcache->tdb == NULL) {
+       if (wcache->db == NULL) {
                return;
        }
 
@@ -4953,7 +4978,7 @@ void wcache_store_ndr(struct winbindd_domain *domain, uint32_t opnum,
        SBVAL(data.dptr, 4, timeout);
        memcpy(data.dptr + 12, resp->data, resp->length);
 
-       tdb_store(wcache->tdb, key, data, 0);
+       dbwrap_store(wcache->db, key, data, 0);
 
 done:
        TALLOC_FREE(key.dptr);
index 3746fe0268105c2a6d039116d64ea7c1dccc2c99..fd74d6d6ef0b44bf90ed4949f228439c020e011c 100644 (file)
@@ -70,7 +70,6 @@ void wcache_invalidate_samlogon(struct winbindd_domain *domain,
                                const struct dom_sid *user_sid);
 bool wcache_invalidate_cache(void);
 bool wcache_invalidate_cache_noinit(void);
-bool init_wcache(void);
 bool initialize_winbindd_cache(void);
 void close_winbindd_cache(void);
 NTSTATUS wcache_sid_to_name(struct winbindd_domain *domain,