s3:dom_sid Global replace of DOM_SID with struct dom_sid
[idra/samba.git] / source3 / winbindd / winbindd_cache.c
index 5dfdc5ae29cbbbca768142419da173221e2eb4a2..b92ef2fabb43858966ec79ba535385cae92c5f9d 100644 (file)
@@ -27,6 +27,7 @@
 #include "winbindd.h"
 #include "tdb_validate.h"
 #include "../libcli/auth/libcli_auth.h"
+#include "../librpc/gen_ndr/ndr_wbint.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
@@ -392,30 +393,55 @@ static bool wcache_server_down(struct winbindd_domain *domain)
        return ret;
 }
 
-static NTSTATUS fetch_cache_seqnum( struct winbindd_domain *domain, time_t now )
+static bool wcache_fetch_seqnum(const char *domain_name, uint32_t *seqnum,
+                               uint32_t *last_seq_check)
 {
+       char *key;
        TDB_DATA data;
-       fstring key;
-       uint32 time_diff;
 
-       if (!wcache->tdb) {
-               DEBUG(10,("fetch_cache_seqnum: tdb == NULL\n"));
-               return NT_STATUS_UNSUCCESSFUL;
+       if (wcache->tdb == NULL) {
+               DEBUG(10,("wcache_fetch_seqnum: tdb == NULL\n"));
+               return false;
        }
 
-       fstr_sprintf( key, "SEQNUM/%s", domain->name );
-
-       data = tdb_fetch_bystring( wcache->tdb, key );
-       if ( !data.dptr || data.dsize!=8 ) {
-               DEBUG(10,("fetch_cache_seqnum: invalid data size key [%s]\n", key ));
-               return NT_STATUS_UNSUCCESSFUL;
+       key = talloc_asprintf(talloc_tos(), "SEQNUM/%s", domain_name);
+       if (key == NULL) {
+               DEBUG(10, ("talloc failed\n"));
+               return false;
        }
 
-       domain->sequence_number = IVAL(data.dptr, 0);
-       domain->last_seq_check  = IVAL(data.dptr, 4);
+       data = tdb_fetch_bystring(wcache->tdb, key);
+       TALLOC_FREE(key);
+
+       if (data.dptr == NULL) {
+               DEBUG(10, ("wcache_fetch_seqnum: %s not found\n",
+                          domain_name));
+               return false;
+       }
+       if (data.dsize != 8) {
+               DEBUG(10, ("wcache_fetch_seqnum: invalid data size %d\n",
+                          (int)data.dsize));
+               SAFE_FREE(data.dptr);
+               return false;
+       }
 
+       *seqnum = IVAL(data.dptr, 0);
+       *last_seq_check = IVAL(data.dptr, 4);
        SAFE_FREE(data.dptr);
 
+       return true;
+}
+
+static NTSTATUS fetch_cache_seqnum( struct winbindd_domain *domain, time_t now )
+{
+       uint32 last_check, time_diff;
+
+       if (!wcache_fetch_seqnum(domain->name, &domain->sequence_number,
+                                &last_check)) {
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+       domain->last_seq_check = last_check;
+
        /* have we expired? */
 
        time_diff = now - domain->last_seq_check;
@@ -433,34 +459,47 @@ static NTSTATUS fetch_cache_seqnum( struct winbindd_domain *domain, time_t now )
        return NT_STATUS_OK;
 }
 
-static NTSTATUS store_cache_seqnum( struct winbindd_domain *domain )
+bool wcache_store_seqnum(const char *domain_name, uint32_t seqnum,
+                        time_t last_seq_check)
 {
-       TDB_DATA data;
-       fstring key_str;
-       uint8 buf[8];
+       char *key_str;
+       uint8_t buf[8];
+       int ret;
 
-       if (!wcache->tdb) {
-               DEBUG(10,("store_cache_seqnum: tdb == NULL\n"));
-               return NT_STATUS_UNSUCCESSFUL;
+       if (wcache->tdb == NULL) {
+               DEBUG(10, ("wcache_store_seqnum: wcache->tdb == NULL\n"));
+               return false;
        }
 
-       fstr_sprintf( key_str, "SEQNUM/%s", domain->name );
+       key_str = talloc_asprintf(talloc_tos(), "SEQNUM/%s", domain_name);
+       if (key_str == NULL) {
+               DEBUG(10, ("talloc_asprintf failed\n"));
+               return false;
+       }
 
-       SIVAL(buf, 0, domain->sequence_number);
-       SIVAL(buf, 4, domain->last_seq_check);
-       data.dptr = buf;
-       data.dsize = 8;
+       SIVAL(buf, 0, seqnum);
+       SIVAL(buf, 4, last_seq_check);
 
-       if ( tdb_store_bystring( wcache->tdb, key_str, data, TDB_REPLACE) == -1 ) {
-               DEBUG(10,("store_cache_seqnum: tdb_store fail key [%s]\n", key_str ));
-               return NT_STATUS_UNSUCCESSFUL;
+       ret = tdb_store_bystring(wcache->tdb, key_str,
+                                make_tdb_data(buf, sizeof(buf)), TDB_REPLACE);
+       TALLOC_FREE(key_str);
+       if (ret == -1) {
+               DEBUG(10, ("tdb_store_bystring failed: %s\n",
+                          tdb_errorstr(wcache->tdb)));
+               TALLOC_FREE(key_str);
+               return false;
        }
 
-       DEBUG(10,("store_cache_seqnum: success [%s][%u @ %u]\n", 
-               domain->name, domain->sequence_number, 
-               (uint32)domain->last_seq_check));
+       DEBUG(10, ("wcache_store_seqnum: success [%s][%u @ %u]\n",
+                  domain_name, seqnum, (unsigned)last_seq_check));
 
-       return NT_STATUS_OK;
+       return true;
+}
+
+static bool store_cache_seqnum( struct winbindd_domain *domain )
+{
+       return wcache_store_seqnum(domain->name, domain->sequence_number,
+                                  domain->last_seq_check);
 }
 
 /*
@@ -475,7 +514,7 @@ static void refresh_sequence_number(struct winbindd_domain *domain, bool force)
        time_t t = time(NULL);
        unsigned cache_time = lp_winbind_cache_time();
 
-       if ( IS_DOMAIN_OFFLINE(domain) ) {
+       if (is_domain_offline(domain)) {
                return;
        }
 
@@ -768,7 +807,7 @@ static void centry_put_hash16(struct cache_entry *centry, const uint8 val[16])
        centry->ofs += 16;
 }
 
-static void centry_put_sid(struct cache_entry *centry, const DOM_SID *sid) 
+static void centry_put_sid(struct cache_entry *centry, const struct dom_sid *sid)
 {
        fstring sid_string;
        centry_put_string(centry, sid_to_fstring(sid_string, sid));
@@ -856,7 +895,7 @@ static void centry_end(struct cache_entry *centry, const char *format, ...)
 
 static void wcache_save_name_to_sid(struct winbindd_domain *domain, 
                                    NTSTATUS status, const char *domain_name,
-                                   const char *name, const DOM_SID *sid, 
+                                   const char *name, const struct dom_sid *sid,
                                    enum lsa_SidType type)
 {
        struct cache_entry *centry;
@@ -876,7 +915,7 @@ static void wcache_save_name_to_sid(struct winbindd_domain *domain,
 }
 
 static void wcache_save_sid_to_name(struct winbindd_domain *domain, NTSTATUS status, 
-                                   const DOM_SID *sid, const char *domain_name, const char *name, enum lsa_SidType type)
+                                   const struct dom_sid *sid, const char *domain_name, const char *name, enum lsa_SidType type)
 {
        struct cache_entry *centry;
        fstring sid_string;
@@ -898,7 +937,8 @@ static void wcache_save_sid_to_name(struct winbindd_domain *domain, NTSTATUS sta
 }
 
 
-static void wcache_save_user(struct winbindd_domain *domain, NTSTATUS status, WINBIND_USERINFO *info)
+static void wcache_save_user(struct winbindd_domain *domain, NTSTATUS status,
+                            struct wbint_userinfo *info)
 {
        struct cache_entry *centry;
        fstring sid_string;
@@ -1172,7 +1212,7 @@ do_query:
        return status;
 }
 
-NTSTATUS wcache_cached_creds_exist(struct winbindd_domain *domain, const DOM_SID *sid)
+NTSTATUS wcache_cached_creds_exist(struct winbindd_domain *domain, const struct dom_sid *sid)
 {
        struct winbind_cache *cache = get_cache(domain);
        TDB_DATA data;
@@ -1207,7 +1247,7 @@ NTSTATUS wcache_cached_creds_exist(struct winbindd_domain *domain, const DOM_SID
 
 NTSTATUS wcache_get_creds(struct winbindd_domain *domain, 
                          TALLOC_CTX *mem_ctx, 
-                         const DOM_SID *sid,
+                         const struct dom_sid *sid,
                          const uint8 **cached_nt_pass,
                          const uint8 **cached_salt)
 {
@@ -1288,7 +1328,7 @@ NTSTATUS wcache_get_creds(struct winbindd_domain *domain,
 
 NTSTATUS wcache_save_creds(struct winbindd_domain *domain, 
                           TALLOC_CTX *mem_ctx, 
-                          const DOM_SID *sid, 
+                          const struct dom_sid *sid,
                           const uint8 nt_pass[NT_HASH_LEN])
 {
        struct cache_entry *centry;
@@ -1334,12 +1374,13 @@ NTSTATUS wcache_save_creds(struct winbindd_domain *domain,
 static NTSTATUS query_user_list(struct winbindd_domain *domain,
                                TALLOC_CTX *mem_ctx,
                                uint32 *num_entries, 
-                               WINBIND_USERINFO **info)
+                               struct wbint_userinfo **info)
 {
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry = NULL;
        NTSTATUS status;
        unsigned int i, retry;
+       bool old_status = domain->online;
 
        if (!cache->tdb)
                goto do_query;
@@ -1348,12 +1389,13 @@ static NTSTATUS query_user_list(struct winbindd_domain *domain,
        if (!centry)
                goto do_query;
 
+do_fetch_cache:
        *num_entries = centry_uint32(centry);
 
        if (*num_entries == 0)
                goto do_cached;
 
-       (*info) = TALLOC_ARRAY(mem_ctx, WINBIND_USERINFO, *num_entries);
+       (*info) = TALLOC_ARRAY(mem_ctx, struct wbint_userinfo, *num_entries);
        if (! (*info)) {
                smb_panic_fn("query_user_list out of memory");
        }
@@ -1408,12 +1450,44 @@ do_query:
                                  "connection cache\n"));
                        invalidate_cm_connection(&domain->conn);
                }
+               if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
+                   NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
+                       if (!domain->internal && old_status) {
+                               set_domain_offline(domain);
+                       }
+                       /* store partial response. */
+                       if (*num_entries > 0) {
+                               /*
+                                * humm, what about the status used for cache?
+                                * Should it be NT_STATUS_OK?
+                                */
+                               break;
+                       }
+                       /*
+                        * 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) {
+                               centry = wcache_fetch(cache, domain, "UL/%s", domain->name);
+                               /* partial response... */
+                               if (!centry) {
+                                       goto skip_save;
+                               } else {
+                                       goto do_fetch_cache;
+                               }
+                       } else {
+                               goto skip_save;
+                       }
+               }
 
        } while (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_UNSUCCESSFUL) && 
                 (retry++ < 5));
 
        /* and save it */
        refresh_sequence_number(domain, false);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        centry = centry_start(domain, status);
        if (!centry)
                goto skip_save;
@@ -1457,7 +1531,9 @@ static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
        struct cache_entry *centry = NULL;
        NTSTATUS status;
        unsigned int i;
+       bool old_status;
 
+       old_status = domain->online;
        if (!cache->tdb)
                goto do_query;
 
@@ -1465,6 +1541,7 @@ static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
        if (!centry)
                goto do_query;
 
+do_fetch_cache:
        *num_entries = centry_uint32(centry);
 
        if (*num_entries == 0)
@@ -1503,8 +1580,26 @@ do_query:
 
        status = domain->backend->enum_dom_groups(domain, mem_ctx, num_entries, info);
 
+       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
+           NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
+               if (!domain->internal && old_status) {
+                       set_domain_offline(domain);
+               }
+               if (cache->tdb &&
+                       !domain->online &&
+                       !domain->internal &&
+                       old_status) {
+                       centry = wcache_fetch(cache, domain, "GL/%s/domain", domain->name);
+                       if (centry) {
+                               goto do_fetch_cache;
+                       }
+               }
+       }
        /* and save it */
        refresh_sequence_number(domain, false);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        centry = centry_start(domain, status);
        if (!centry)
                goto skip_save;
@@ -1531,7 +1626,9 @@ static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
        struct cache_entry *centry = NULL;
        NTSTATUS status;
        unsigned int i;
+       bool old_status;
 
+       old_status = domain->online;
        if (!cache->tdb)
                goto do_query;
 
@@ -1539,6 +1636,7 @@ static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
        if (!centry)
                goto do_query;
 
+do_fetch_cache:
        *num_entries = centry_uint32(centry);
 
        if (*num_entries == 0)
@@ -1587,8 +1685,26 @@ do_query:
 
        status = domain->backend->enum_local_groups(domain, mem_ctx, num_entries, info);
 
+       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
+               NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
+               if (!domain->internal && old_status) {
+                       set_domain_offline(domain);
+               }
+               if (cache->tdb &&
+                       !domain->internal &&
+                       !domain->online &&
+                       old_status) {
+                       centry = wcache_fetch(cache, domain, "GL/%s/local", domain->name);
+                       if (centry) {
+                               goto do_fetch_cache;
+                       }
+               }
+       }
        /* and save it */
        refresh_sequence_number(domain, false);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        centry = centry_start(domain, status);
        if (!centry)
                goto skip_save;
@@ -1650,10 +1766,13 @@ static NTSTATUS name_to_sid(struct winbindd_domain *domain,
                            const char *domain_name,
                            const char *name,
                            uint32_t flags,
-                           DOM_SID *sid,
+                           struct dom_sid *sid,
                            enum lsa_SidType *type)
 {
        NTSTATUS status;
+       bool old_status;
+
+       old_status = domain->online;
 
        status = wcache_name_to_sid(domain, domain_name, name, sid, type);
        if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
@@ -1679,6 +1798,19 @@ static NTSTATUS name_to_sid(struct winbindd_domain *domain,
        status = domain->backend->name_to_sid(domain, mem_ctx, domain_name,
                                              name, flags, sid, type);
 
+       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
+               NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
+               if (!domain->internal && old_status) {
+                       set_domain_offline(domain);
+               }
+               if (!domain->internal &&
+                       !domain->online &&
+                       old_status) {
+                       NTSTATUS cache_status;
+                       cache_status = wcache_name_to_sid(domain, domain_name, name, sid, type);
+                       return cache_status;
+               }
+       }
        /* and save it */
        refresh_sequence_number(domain, false);
 
@@ -1743,13 +1875,15 @@ NTSTATUS wcache_sid_to_name(struct winbindd_domain *domain,
    given */
 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
                            TALLOC_CTX *mem_ctx,
-                           const DOM_SID *sid,
+                           const struct dom_sid *sid,
                            char **domain_name,
                            char **name,
                            enum lsa_SidType *type)
 {
        NTSTATUS status;
+       bool old_status;
 
+       old_status = domain->online;
        status = wcache_sid_to_name(domain, sid, mem_ctx, domain_name, name,
                                    type);
        if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
@@ -1775,8 +1909,25 @@ static NTSTATUS sid_to_name(struct winbindd_domain *domain,
 
        status = domain->backend->sid_to_name(domain, mem_ctx, sid, domain_name, name, type);
 
+       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
+               NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
+               if (!domain->internal && old_status) {
+                       set_domain_offline(domain);
+               }
+               if (!domain->internal &&
+                       !domain->online &&
+                       old_status) {
+                       NTSTATUS cache_status;
+                       cache_status = wcache_sid_to_name(domain, sid, mem_ctx,
+                                                       domain_name, name, type);
+                       return cache_status;
+               }
+       }
        /* and save it */
        refresh_sequence_number(domain, false);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        wcache_save_sid_to_name(domain, status, sid, *domain_name, *name, *type);
 
        /* We can't save the name to sid mapping here, as with sid history a
@@ -1787,7 +1938,7 @@ static NTSTATUS sid_to_name(struct winbindd_domain *domain,
 
 static NTSTATUS rids_to_names(struct winbindd_domain *domain,
                              TALLOC_CTX *mem_ctx,
-                             const DOM_SID *domain_sid,
+                             const struct dom_sid *domain_sid,
                              uint32 *rids,
                              size_t num_rids,
                              char **domain_name,
@@ -1799,7 +1950,9 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
        bool have_mapped;
        bool have_unmapped;
+       bool old_status;
 
+       old_status = domain->online;
        *domain_name = NULL;
        *names = NULL;
        *types = NULL;
@@ -1823,7 +1976,7 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
        have_mapped = have_unmapped = false;
 
        for (i=0; i<num_rids; i++) {
-               DOM_SID sid;
+               struct dom_sid sid;
                struct cache_entry *centry;
                fstring tmp;
 
@@ -1884,12 +2037,79 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
                                                rids, num_rids, domain_name,
                                                names, types);
 
+       if (NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT) ||
+               NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
+               if (!domain->internal && old_status) {
+                       set_domain_offline(domain);
+               }
+               if (cache->tdb &&
+                       !domain->internal &&
+                       !domain->online &&
+                       old_status) {
+                       have_mapped = have_unmapped = false;
+
+                       for (i=0; i<num_rids; i++) {
+                               struct dom_sid sid;
+                               struct cache_entry *centry;
+                               fstring tmp;
+
+                               if (!sid_compose(&sid, domain_sid, rids[i])) {
+                                       result = NT_STATUS_INTERNAL_ERROR;
+                                       goto error;
+                               }
+
+                               centry = wcache_fetch(cache, domain, "SN/%s",
+                                                     sid_to_fstring(tmp, &sid));
+                               if (!centry) {
+                                       (*types)[i] = SID_NAME_UNKNOWN;
+                                       (*names)[i] = talloc_strdup(*names, "");
+                                       continue;
+                               }
+
+                               (*types)[i] = SID_NAME_UNKNOWN;
+                               (*names)[i] = talloc_strdup(*names, "");
+
+                               if (NT_STATUS_IS_OK(centry->status)) {
+                                       char *dom;
+                                       have_mapped = true;
+                                       (*types)[i] = (enum lsa_SidType)centry_uint32(centry);
+
+                                       dom = centry_string(centry, mem_ctx);
+                                       if (*domain_name == NULL) {
+                                               *domain_name = dom;
+                                       } else {
+                                               talloc_free(dom);
+                                       }
+
+                                       (*names)[i] = centry_string(centry, *names);
+
+                               } else if (NT_STATUS_EQUAL(centry->status, NT_STATUS_NONE_MAPPED)) {
+                                       have_unmapped = true;
+
+                               } else {
+                                       /* something's definitely wrong */
+                                       result = centry->status;
+                                       goto error;
+                               }
+
+                               centry_free(centry);
+                       }
+
+                       if (!have_mapped) {
+                               return NT_STATUS_NONE_MAPPED;
+                       }
+                       if (!have_unmapped) {
+                               return NT_STATUS_OK;
+                       }
+                       return STATUS_SOME_UNMAPPED;
+               }
+       }
        /*
          None of the queried rids has been found so save all negative entries
        */
        if (NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED)) {
                for (i = 0; i < num_rids; i++) {
-                       DOM_SID sid;
+                       struct dom_sid sid;
                        const char *name = "";
                        const enum lsa_SidType type = SID_NAME_UNKNOWN;
                        NTSTATUS status = NT_STATUS_NONE_MAPPED;
@@ -1916,7 +2136,7 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
        refresh_sequence_number(domain, false);
 
        for (i=0; i<num_rids; i++) {
-               DOM_SID sid;
+               struct dom_sid sid;
                NTSTATUS status;
 
                if (!sid_compose(&sid, domain_sid, rids[i])) {
@@ -1942,7 +2162,7 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
 NTSTATUS wcache_query_user(struct winbindd_domain *domain,
                           TALLOC_CTX *mem_ctx,
                           const struct dom_sid *user_sid,
-                          struct winbind_userinfo *info)
+                          struct wbint_userinfo *info)
 {
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry = NULL;
@@ -2002,11 +2222,13 @@ NTSTATUS wcache_query_user(struct winbindd_domain *domain,
 /* Lookup user information from a rid */
 static NTSTATUS query_user(struct winbindd_domain *domain,
                           TALLOC_CTX *mem_ctx,
-                          const DOM_SID *user_sid,
-                          WINBIND_USERINFO *info)
+                          const struct dom_sid *user_sid,
+                          struct wbint_userinfo *info)
 {
        NTSTATUS status;
+       bool old_status;
 
+       old_status = domain->online;
        status = wcache_query_user(domain, mem_ctx, user_sid, info);
        if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
                return status;
@@ -2024,8 +2246,24 @@ static NTSTATUS query_user(struct winbindd_domain *domain,
 
        status = domain->backend->query_user(domain, mem_ctx, user_sid, info);
 
+       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
+               NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
+               if (!domain->internal && old_status) {
+                       set_domain_offline(domain);
+               }
+               if (!domain->internal &&
+                       !domain->online &&
+                       old_status) {
+                       NTSTATUS cache_status;
+                       cache_status = wcache_query_user(domain, mem_ctx, user_sid, info);
+                       return cache_status;
+               }
+       }
        /* and save it */
        refresh_sequence_number(domain, false);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        wcache_save_user(domain, status, info);
 
        return status;
@@ -2070,6 +2308,7 @@ NTSTATUS wcache_lookup_usergroups(struct winbindd_domain *domain,
        num_sids = centry_uint32(centry);
        sids = talloc_array(mem_ctx, struct dom_sid, num_sids);
        if (sids == NULL) {
+               centry_free(centry);
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -2092,14 +2331,16 @@ NTSTATUS wcache_lookup_usergroups(struct winbindd_domain *domain,
 /* Lookup groups a user is a member of. */
 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
                                  TALLOC_CTX *mem_ctx,
-                                 const DOM_SID *user_sid,
-                                 uint32 *num_groups, DOM_SID **user_gids)
+                                 const struct dom_sid *user_sid,
+                                 uint32 *num_groups, struct dom_sid **user_gids)
 {
        struct cache_entry *centry = NULL;
        NTSTATUS status;
        unsigned int i;
        fstring sid_string;
+       bool old_status;
 
+       old_status = domain->online;
        status = wcache_lookup_usergroups(domain, mem_ctx, user_sid,
                                          num_groups, user_gids);
        if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
@@ -2119,11 +2360,28 @@ static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
 
        status = domain->backend->lookup_usergroups(domain, mem_ctx, user_sid, num_groups, user_gids);
 
+       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
+               NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
+               if (!domain->internal && old_status) {
+                       set_domain_offline(domain);
+               }
+               if (!domain->internal &&
+                       !domain->online &&
+                       old_status) {
+                       NTSTATUS cache_status;
+                       cache_status = wcache_lookup_usergroups(domain, mem_ctx, user_sid,
+                                                         num_groups, user_gids);
+                       return cache_status;
+               }
+       }
        if ( NT_STATUS_EQUAL(status, NT_STATUS_SYNCHRONIZATION_REQUIRED) )
                goto skip_save;
 
        /* and save it */
        refresh_sequence_number(domain, false);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        centry = centry_start(domain, status);
        if (!centry)
                goto skip_save;
@@ -2224,14 +2482,16 @@ NTSTATUS wcache_lookup_useraliases(struct winbindd_domain *domain,
 
 static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
                                   TALLOC_CTX *mem_ctx,
-                                  uint32 num_sids, const DOM_SID *sids,
+                                  uint32 num_sids, const struct dom_sid *sids,
                                   uint32 *num_aliases, uint32 **alias_rids)
 {
        struct cache_entry *centry = NULL;
        NTSTATUS status;
        char *sidlist;
        int i;
+       bool old_status;
 
+       old_status = domain->online;
        status = wcache_lookup_useraliases(domain, mem_ctx, num_sids, sids,
                                           num_aliases, alias_rids);
        if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
@@ -2256,8 +2516,25 @@ static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
                                                     num_sids, sids,
                                                     num_aliases, alias_rids);
 
+       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
+               NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
+               if (!domain->internal && old_status) {
+                       set_domain_offline(domain);
+               }
+               if (!domain->internal &&
+                       !domain->online &&
+                       old_status) {
+                       NTSTATUS cache_status;
+                       cache_status = wcache_lookup_useraliases(domain, mem_ctx, num_sids,
+                                                                sids, num_aliases, alias_rids);
+                       return cache_status;
+               }
+       }
        /* and save it */
        refresh_sequence_number(domain, false);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        centry = centry_start(domain, status);
        if (!centry)
                goto skip_save;
@@ -2271,38 +2548,54 @@ static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
        return status;
 }
 
-
-static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
+NTSTATUS wcache_lookup_groupmem(struct winbindd_domain *domain,
                                TALLOC_CTX *mem_ctx,
-                               const DOM_SID *group_sid, uint32 *num_names, 
-                               DOM_SID **sid_mem, char ***names, 
-                               uint32 **name_types)
+                               const struct dom_sid *group_sid,
+                               uint32_t *num_names,
+                               struct dom_sid **sid_mem, char ***names,
+                               uint32_t **name_types)
 {
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry = NULL;
        NTSTATUS status;
        unsigned int i;
-       fstring sid_string;
+       char *sid_string;
 
-       if (!cache->tdb)
-               goto do_query;
+       if (cache->tdb == NULL) {
+               return NT_STATUS_NOT_FOUND;
+       }
 
-       centry = wcache_fetch(cache, domain, "GM/%s",
-                             sid_to_fstring(sid_string, group_sid));
-       if (!centry)
-               goto do_query;
+       sid_string = sid_string_tos(group_sid);
+       if (sid_string == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
-       *num_names = centry_uint32(centry);
+       centry = wcache_fetch(cache, domain, "GM/%s", sid_string);
+       TALLOC_FREE(sid_string);
+       if (centry == NULL) {
+               return NT_STATUS_NOT_FOUND;
+       }
 
-       if (*num_names == 0)
-               goto do_cached;
+       *sid_mem = NULL;
+       *names = NULL;
+       *name_types = NULL;
+
+       *num_names = centry_uint32(centry);
+       if (*num_names == 0) {
+               centry_free(centry);
+               return NT_STATUS_OK;
+       }
 
-       (*sid_mem) = TALLOC_ARRAY(mem_ctx, DOM_SID, *num_names);
-       (*names) = TALLOC_ARRAY(mem_ctx, char *, *num_names);
-       (*name_types) = TALLOC_ARRAY(mem_ctx, uint32, *num_names);
+       *sid_mem = talloc_array(mem_ctx, struct dom_sid, *num_names);
+       *names = talloc_array(mem_ctx, char *, *num_names);
+       *name_types = talloc_array(mem_ctx, uint32, *num_names);
 
-       if (! (*sid_mem) || ! (*names) || ! (*name_types)) {
-               smb_panic_fn("lookup_groupmem out of memory");
+       if ((*sid_mem == NULL) || (*names == NULL) || (*name_types == NULL)) {
+               TALLOC_FREE(*sid_mem);
+               TALLOC_FREE(*names);
+               TALLOC_FREE(*name_types);
+               centry_free(centry);
+               return NT_STATUS_NO_MEMORY;
        }
 
        for (i=0; i<(*num_names); i++) {
@@ -2311,16 +2604,36 @@ static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
                (*name_types)[i] = centry_uint32(centry);
        }
 
-do_cached:     
        status = centry->status;
 
-       DEBUG(10,("lookup_groupmem: [Cached] - cached info for domain %s status: %s\n",
-               domain->name, nt_errstr(status)));
+       DEBUG(10,("lookup_groupmem: [Cached] - cached info for domain %s "
+                 "status: %s\n", domain->name, nt_errstr(status)));
 
        centry_free(centry);
        return status;
+}
+
+static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
+                               TALLOC_CTX *mem_ctx,
+                               const struct dom_sid *group_sid,
+                               enum lsa_SidType type,
+                               uint32 *num_names,
+                               struct dom_sid **sid_mem, char ***names,
+                               uint32 **name_types)
+{
+       struct cache_entry *centry = NULL;
+       NTSTATUS status;
+       unsigned int i;
+       fstring sid_string;
+       bool old_status;
+
+       old_status = domain->online;
+       status = wcache_lookup_groupmem(domain, mem_ctx, group_sid, num_names,
+                                       sid_mem, names, name_types);
+       if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
+               return status;
+       }
 
-do_query:
        (*num_names) = 0;
        (*sid_mem) = NULL;
        (*names) = NULL;
@@ -2334,11 +2647,30 @@ do_query:
        DEBUG(10,("lookup_groupmem: [Cached] - doing backend query for info for domain %s\n",
                domain->name ));
 
-       status = domain->backend->lookup_groupmem(domain, mem_ctx, group_sid, num_names, 
+       status = domain->backend->lookup_groupmem(domain, mem_ctx, group_sid,
+                                                 type, num_names,
                                                  sid_mem, names, name_types);
 
+       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
+               NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
+               if (!domain->internal && old_status) {
+                       set_domain_offline(domain);
+               }
+               if (!domain->internal &&
+                       !domain->online &&
+                       old_status) {
+                       NTSTATUS cache_status;
+                       cache_status = wcache_lookup_groupmem(domain, mem_ctx, group_sid,
+                                                             num_names, sid_mem, names,
+                                                             name_types);
+                       return cache_status;
+               }
+       }
        /* and save it */
        refresh_sequence_number(domain, false);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        centry = centry_start(domain, status);
        if (!centry)
                goto skip_save;
@@ -2370,63 +2702,75 @@ static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
  * Guenther */
 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
                                TALLOC_CTX *mem_ctx,
-                               uint32 *num_domains,
-                               char ***names,
-                               char ***alt_names,
-                               DOM_SID **dom_sids)
+                               struct netr_DomainTrustList *trusts)
 {
-       struct winbind_cache *cache = get_cache(domain);
-       struct cache_entry *centry = NULL;
        NTSTATUS status;
+       struct winbind_cache *cache;
+       struct winbindd_tdc_domain *dom_list = NULL;
+       size_t num_domains = 0;
+       bool retval = false;
        int i;
+       bool old_status;
 
-       if (!cache->tdb)
-               goto do_query;
+       old_status = domain->online;
+       trusts->count = 0;
+       trusts->array = NULL;
 
-       centry = wcache_fetch(cache, domain, "TRUSTDOMS/%s", domain->name);
-
-       if (!centry) {
-               goto do_query;
+       cache = get_cache(domain);
+       if (!cache || !cache->tdb) {
+               goto do_query;
        }
 
-       *num_domains = centry_uint32(centry);
+       if (domain->online) {
+               goto do_query;
+       }
 
-       if (*num_domains) {
-               (*names)        = TALLOC_ARRAY(mem_ctx, char *, *num_domains);
-               (*alt_names)    = TALLOC_ARRAY(mem_ctx, char *, *num_domains);
-               (*dom_sids)     = TALLOC_ARRAY(mem_ctx, DOM_SID, *num_domains);
+       retval = wcache_tdc_fetch_list(&dom_list, &num_domains);
+       if (!retval || !num_domains || !dom_list) {
+               TALLOC_FREE(dom_list);
+               goto do_query;
+       }
 
-               if (! (*dom_sids) || ! (*names) || ! (*alt_names)) {
-                       smb_panic_fn("trusted_domains out of memory");
-               }
-       } else {
-               (*names) = NULL;
-               (*alt_names) = NULL;
-               (*dom_sids) = NULL;
+do_fetch_cache:
+       trusts->array = TALLOC_ZERO_ARRAY(mem_ctx, struct netr_DomainTrust, num_domains);
+       if (!trusts->array) {
+               TALLOC_FREE(dom_list);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       for (i=0; i<(*num_domains); i++) {
-               (*names)[i] = centry_string(centry, mem_ctx);
-               (*alt_names)[i] = centry_string(centry, mem_ctx);
-               if (!centry_sid(centry, &(*dom_sids)[i])) {
-                       sid_copy(&(*dom_sids)[i], &global_sid_NULL);
+       for (i = 0; i < num_domains; i++) {
+               struct netr_DomainTrust *trust;
+               struct dom_sid *sid;
+               struct winbindd_domain *dom;
+
+               dom = find_domain_from_name_noinit(dom_list[i].domain_name);
+               if (dom && dom->internal) {
+                       continue;
                }
-       }
 
-       status = centry->status;
+               trust = &trusts->array[trusts->count];
+               trust->netbios_name = talloc_strdup(trusts->array, dom_list[i].domain_name);
+               trust->dns_name = talloc_strdup(trusts->array, dom_list[i].dns_name);
+               sid = talloc(trusts->array, struct dom_sid);
+               if (!trust->netbios_name || !trust->dns_name ||
+                       !sid) {
+                       TALLOC_FREE(dom_list);
+                       TALLOC_FREE(trusts->array);
+                       return NT_STATUS_NO_MEMORY;
+               }
 
-       DEBUG(10,("trusted_domains: [Cached] - cached info for domain %s (%d trusts) status: %s\n",
-               domain->name, *num_domains, nt_errstr(status) ));
+               trust->trust_flags = dom_list[i].trust_flags;
+               trust->trust_attributes = dom_list[i].trust_attribs;
+               trust->trust_type = dom_list[i].trust_type;
+               sid_copy(sid, &dom_list[i].sid);
+               trust->sid = sid;
+               trusts->count++;
+       }
 
-       centry_free(centry);
-       return status;
+       TALLOC_FREE(dom_list);
+       return NT_STATUS_OK;
 
 do_query:
-       (*num_domains) = 0;
-       (*dom_sids) = NULL;
-       (*names) = NULL;
-       (*alt_names) = NULL;
-
        /* Return status value returned by seq number check */
 
        if (!NT_STATUS_IS_OK(domain->last_status))
@@ -2435,9 +2779,24 @@ do_query:
        DEBUG(10,("trusted_domains: [Cached] - doing backend query for info for domain %s\n",
                domain->name ));
 
-       status = domain->backend->trusted_domains(domain, mem_ctx, num_domains,
-                                               names, alt_names, dom_sids);
+       status = domain->backend->trusted_domains(domain, mem_ctx, trusts);
 
+       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
+               NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
+               if (!domain->internal && old_status) {
+                       set_domain_offline(domain);
+               }
+               if (!domain->internal &&
+                       !domain->online &&
+                       old_status) {
+                       retval = wcache_tdc_fetch_list(&dom_list, &num_domains);
+                       if (retval && num_domains && dom_list) {
+                               TALLOC_FREE(trusts->array);
+                               trusts->count = 0;
+                               goto do_fetch_cache;
+                       }
+               }
+       }
        /* no trusts gives NT_STATUS_NO_MORE_ENTRIES resetting to NT_STATUS_OK
         * so that the generic centry handling still applies correctly -
         * Guenther*/
@@ -2445,33 +2804,6 @@ do_query:
        if (!NT_STATUS_IS_ERR(status)) {
                status = NT_STATUS_OK;
        }
-
-
-#if 0    /* Disabled as we want the trust dom list to be managed by
-           the main parent and always to make the query.  --jerry */
-
-       /* and save it */
-       refresh_sequence_number(domain, false);
-
-       centry = centry_start(domain, status);
-       if (!centry)
-               goto skip_save;
-
-       centry_put_uint32(centry, *num_domains);
-
-       for (i=0; i<(*num_domains); i++) {
-               centry_put_string(centry, (*names)[i]);
-               centry_put_string(centry, (*alt_names)[i]);
-               centry_put_sid(centry, &(*dom_sids)[i]);
-       }
-
-       centry_end(centry, "TRUSTDOMS/%s", domain->name);
-
-       centry_free(centry);
-
-skip_save:
-#endif
-
        return status;
 }      
 
@@ -2483,7 +2815,9 @@ static NTSTATUS lockout_policy(struct winbindd_domain *domain,
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry = NULL;
        NTSTATUS status;
+       bool old_status;
 
+       old_status = domain->online;
        if (!cache->tdb)
                goto do_query;
 
@@ -2492,6 +2826,7 @@ static NTSTATUS lockout_policy(struct winbindd_domain *domain,
        if (!centry)
                goto do_query;
 
+do_fetch_cache:
        policy->lockout_duration = centry_nttime(centry);
        policy->lockout_window = centry_nttime(centry);
        policy->lockout_threshold = centry_uint16(centry);
@@ -2517,8 +2852,26 @@ do_query:
 
        status = domain->backend->lockout_policy(domain, mem_ctx, policy);
 
+       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
+               NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
+               if (!domain->internal && old_status) {
+                       set_domain_offline(domain);
+               }
+               if (cache->tdb &&
+                       !domain->internal &&
+                       !domain->online &&
+                       old_status) {
+                       centry = wcache_fetch(cache, domain, "LOC_POL/%s", domain->name);
+                       if (centry) {
+                               goto do_fetch_cache;
+                       }
+               }
+       }
        /* and save it */
        refresh_sequence_number(domain, false);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
        wcache_save_lockout_policy(domain, status, policy);
 
        return status;
@@ -2532,7 +2885,9 @@ static NTSTATUS password_policy(struct winbindd_domain *domain,
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry = NULL;
        NTSTATUS status;
+       bool old_status;
 
+       old_status = domain->online;
        if (!cache->tdb)
                goto do_query;
 
@@ -2541,6 +2896,7 @@ static NTSTATUS password_policy(struct winbindd_domain *domain,
        if (!centry)
                goto do_query;
 
+do_fetch_cache:
        policy->min_password_length = centry_uint16(centry);
        policy->password_history_length = centry_uint16(centry);
        policy->password_properties = centry_uint32(centry);
@@ -2568,11 +2924,27 @@ do_query:
 
        status = domain->backend->password_policy(domain, mem_ctx, policy);
 
+       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
+               NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
+               if (!domain->internal && old_status) {
+                       set_domain_offline(domain);
+               }
+               if (cache->tdb &&
+                       !domain->internal &&
+                       !domain->online &&
+                       old_status) {
+                       centry = wcache_fetch(cache, domain, "PWD_POL/%s", domain->name);
+                       if (centry) {
+                               goto do_fetch_cache;
+                       }
+               }
+       }
        /* and save it */
        refresh_sequence_number(domain, false);
-       if (NT_STATUS_IS_OK(status)) {
-               wcache_save_password_policy(domain, status, policy);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
+       wcache_save_password_policy(domain, status, policy);
 
        return status;
 }
@@ -2595,7 +2967,7 @@ static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf,
 void wcache_invalidate_samlogon(struct winbindd_domain *domain, 
                                struct netr_SamInfo3 *info3)
 {
-        DOM_SID sid;
+        struct dom_sid sid;
         fstring key_str, sid_string;
        struct winbind_cache *cache;
 
@@ -2615,8 +2987,7 @@ void wcache_invalidate_samlogon(struct winbindd_domain *domain,
                 return;
         }
 
-       sid_copy(&sid, info3->base.domain_sid);
-       sid_append_rid(&sid, info3->base.rid);
+       sid_compose(&sid, info3->base.domain_sid, info3->base.rid);
 
        /* Clear U/SID cache entry */
        fstr_sprintf(key_str, "U/%s", sid_to_fstring(sid_string, &sid));
@@ -2652,6 +3023,39 @@ bool wcache_invalidate_cache(void)
        return true;
 }
 
+bool wcache_invalidate_cache_noinit(void)
+{
+       struct winbindd_domain *domain;
+
+       for (domain = domain_list(); domain; domain = domain->next) {
+               struct winbind_cache *cache;
+
+               /* Skip uninitialized domains. */
+               if (!domain->initialized && !domain->internal) {
+                       continue;
+               }
+
+               cache = get_cache(domain);
+
+               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);
+                               /*
+                                * Flushing cache has nothing to with domains.
+                                * return here if we successfully flushed once.
+                                * To avoid unnecessary traversing the cache.
+                                */
+                               return true;
+                       } else {
+                               return false;
+                       }
+               }
+       }
+       return true;
+}
+
 bool init_wcache(void)
 {
        if (wcache == NULL) {
@@ -2742,7 +3146,7 @@ void close_winbindd_cache(void)
        }
 }
 
-bool lookup_cached_sid(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
+bool lookup_cached_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
                       char **domain_name, char **name,
                       enum lsa_SidType *type)
 {
@@ -2761,7 +3165,7 @@ bool lookup_cached_sid(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
 bool lookup_cached_name(TALLOC_CTX *mem_ctx,
                        const char *domain_name,
                        const char *name,
-                       DOM_SID *sid,
+                       struct dom_sid *sid,
                        enum lsa_SidType *type)
 {
        struct winbindd_domain *domain;
@@ -2786,7 +3190,7 @@ bool lookup_cached_name(TALLOC_CTX *mem_ctx,
 
 void cache_name2sid(struct winbindd_domain *domain, 
                    const char *domain_name, const char *name,
-                   enum lsa_SidType type, const DOM_SID *sid)
+                   enum lsa_SidType type, const struct dom_sid *sid)
 {
        refresh_sequence_number(domain, false);
        wcache_save_name_to_sid(domain, NT_STATUS_OK, domain_name, name,
@@ -2911,7 +3315,7 @@ static int traverse_fn_get_credlist(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DAT
        return 0;
 }
 
-NTSTATUS wcache_remove_oldest_cached_creds(struct winbindd_domain *domain, const DOM_SID *sid) 
+NTSTATUS wcache_remove_oldest_cached_creds(struct winbindd_domain *domain, const struct dom_sid *sid)
 {
        struct winbind_cache *cache = get_cache(domain);
        NTSTATUS status;
@@ -3112,7 +3516,7 @@ static int validate_ns(TALLOC_CTX *mem_ctx, const char *keystr, TDB_DATA dbuf,
 
        (void)centry_uint32(centry);
        if (NT_STATUS_IS_OK(centry->status)) {
-               DOM_SID sid;
+               struct dom_sid sid;
                (void)centry_sid(centry, &sid);
        }
 
@@ -3152,7 +3556,7 @@ static int validate_u(TALLOC_CTX *mem_ctx, const char *keystr, TDB_DATA dbuf,
                      struct tdb_validation_status *state)
 {
        struct cache_entry *centry = create_centry_validate(keystr, dbuf, state);
-       DOM_SID sid;
+       struct dom_sid sid;
 
        if (!centry) {
                return 1;
@@ -3260,7 +3664,7 @@ static int validate_ul(TALLOC_CTX *mem_ctx, const char *keystr, TDB_DATA dbuf,
        num_entries = (int32)centry_uint32(centry);
 
        for (i=0; i< num_entries; i++) {
-               DOM_SID sid;
+               struct dom_sid sid;
                (void)centry_string(centry, mem_ctx);
                (void)centry_string(centry, mem_ctx);
                (void)centry_string(centry, mem_ctx);
@@ -3318,7 +3722,7 @@ static int validate_ug(TALLOC_CTX *mem_ctx, const char *keystr, TDB_DATA dbuf,
        num_groups = centry_uint32(centry);
 
        for (i=0; i< num_groups; i++) {
-               DOM_SID sid;
+               struct dom_sid sid;
                centry_sid(centry, &sid);
        }
 
@@ -3369,7 +3773,7 @@ static int validate_gm(TALLOC_CTX *mem_ctx, const char *keystr, TDB_DATA dbuf,
        num_names = centry_uint32(centry);
 
        for (i=0; i< num_names; i++) {
-               DOM_SID sid;
+               struct dom_sid sid;
                centry_sid(centry, &sid);
                (void)centry_string(centry, mem_ctx);
                (void)centry_uint32(centry);
@@ -3481,34 +3885,6 @@ static int validate_nss_na(TALLOC_CTX *mem_ctx, const char *keystr,
        return 0;
 }
 
-static int validate_trustdoms(TALLOC_CTX *mem_ctx, const char *keystr, TDB_DATA dbuf,
-                             struct tdb_validation_status *state)
-{
-       struct cache_entry *centry = create_centry_validate(keystr, dbuf, state);
-       int32 num_domains, i;
-
-       if (!centry) {
-               return 1;
-       }
-
-       num_domains = centry_uint32(centry);
-
-       for (i=0; i< num_domains; i++) {
-               DOM_SID sid;
-               (void)centry_string(centry, mem_ctx);
-               (void)centry_string(centry, mem_ctx);
-               (void)centry_sid(centry, &sid);
-       }
-
-       centry_free(centry);
-
-       if (!(state->success)) {
-               return 1;
-       }
-       DEBUG(10,("validate_trustdoms: %s ok\n", keystr));
-       return 0;
-}
-
 static int validate_trustdomcache(TALLOC_CTX *mem_ctx, const char *keystr, 
                                  TDB_DATA dbuf,
                                  struct tdb_validation_status *state)
@@ -3540,6 +3916,16 @@ static int validate_offline(TALLOC_CTX *mem_ctx, const char *keystr, TDB_DATA db
        return 0;
 }
 
+static int validate_ndr(TALLOC_CTX *mem_ctx, const char *keystr, TDB_DATA dbuf,
+                       struct tdb_validation_status *state)
+{
+       /*
+        * Ignore validation for now. The proper way to do this is with a
+        * checksum. Just pure parsing does not really catch much.
+        */
+       return 0;
+}
+
 static int validate_cache_version(TALLOC_CTX *mem_ctx, const char *keystr, TDB_DATA dbuf,
                                  struct tdb_validation_status *state)
 {
@@ -3580,11 +3966,11 @@ struct key_val_struct {
        {"DR/", validate_dr},
        {"DE/", validate_de},
        {"NSS/PWINFO/", validate_pwinfo},
-       {"TRUSTDOMS/", validate_trustdoms},
        {"TRUSTDOMCACHE/", validate_trustdomcache},
        {"NSS/NA/", validate_nss_na},
        {"NSS/AN/", validate_nss_an},
        {"WINBINDD_OFFLINE", validate_offline},
+       {"NDR/", validate_ndr},
        {WINBINDD_CACHE_VERSION_KEYSTR, validate_cache_version},
        {NULL, NULL}
 };
@@ -4138,7 +4524,7 @@ void wcache_tdc_clear( void )
 
 static void wcache_save_user_pwinfo(struct winbindd_domain *domain, 
                                    NTSTATUS status,
-                                   const DOM_SID *user_sid,
+                                   const struct dom_sid *user_sid,
                                    const char *homedir,
                                    const char *shell,
                                    const char *gecos,
@@ -4163,7 +4549,7 @@ static void wcache_save_user_pwinfo(struct winbindd_domain *domain,
 }
 
 NTSTATUS nss_get_info_cached( struct winbindd_domain *domain, 
-                             const DOM_SID *user_sid,
+                             const struct dom_sid *user_sid,
                              TALLOC_CTX *ctx,
                              ADS_STRUCT *ads, LDAPMessage *msg,
                              const char **homedir, const char **shell,
@@ -4240,3 +4626,138 @@ struct winbindd_methods cache_methods = {
        password_policy,
        trusted_domains
 };
+
+static bool wcache_ndr_key(TALLOC_CTX *mem_ctx, char *domain_name,
+                          uint32_t opnum, const DATA_BLOB *req,
+                          TDB_DATA *pkey)
+{
+       char *key;
+       size_t keylen;
+
+       key = talloc_asprintf(mem_ctx, "NDR/%s/%d/", domain_name, (int)opnum);
+       if (key == NULL) {
+               return false;
+       }
+       keylen = talloc_get_size(key) - 1;
+
+       key = talloc_realloc(mem_ctx, key, char, keylen + req->length);
+       if (key == NULL) {
+               return false;
+       }
+       memcpy(key + keylen, req->data, req->length);
+
+       pkey->dptr = (uint8_t *)key;
+       pkey->dsize = talloc_get_size(key);
+       return true;
+}
+
+static bool wcache_opnum_cacheable(uint32_t opnum)
+{
+       switch (opnum) {
+       case NDR_WBINT_PING:
+       case NDR_WBINT_QUERYSEQUENCENUMBER:
+       case NDR_WBINT_ALLOCATEUID:
+       case NDR_WBINT_ALLOCATEGID:
+       case NDR_WBINT_CHECKMACHINEACCOUNT:
+       case NDR_WBINT_CHANGEMACHINEACCOUNT:
+       case NDR_WBINT_PINGDC:
+               return false;
+       }
+       return true;
+}
+
+bool wcache_fetch_ndr(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
+                     uint32_t opnum, const DATA_BLOB *req, DATA_BLOB *resp)
+{
+       TDB_DATA key, data;
+       bool ret = false;
+
+       if (!wcache_opnum_cacheable(opnum)) {
+               return false;
+       }
+
+       if (wcache->tdb == NULL) {
+               return false;
+       }
+
+       if (!wcache_ndr_key(talloc_tos(), domain->name, opnum, req, &key)) {
+               return false;
+       }
+       data = tdb_fetch(wcache->tdb, key);
+       TALLOC_FREE(key.dptr);
+
+       if (data.dptr == NULL) {
+               return false;
+       }
+       if (data.dsize < 4) {
+               goto fail;
+       }
+
+       if (!is_domain_offline(domain)) {
+               uint32_t entry_seqnum, dom_seqnum, last_check;
+
+               if (!wcache_fetch_seqnum(domain->name, &dom_seqnum,
+                                        &last_check)) {
+                       goto fail;
+               }
+               entry_seqnum = IVAL(data.dptr, 0);
+               if (entry_seqnum != dom_seqnum) {
+                       DEBUG(10, ("Entry has wrong sequence number: %d\n",
+                                  (int)entry_seqnum));
+                       goto fail;
+               }
+       }
+
+       resp->data = (uint8_t *)talloc_memdup(mem_ctx, data.dptr + 4,
+                                             data.dsize - 4);
+       if (resp->data == NULL) {
+               DEBUG(10, ("talloc failed\n"));
+               goto fail;
+       }
+       resp->length = data.dsize - 4;
+
+       ret = true;
+fail:
+       SAFE_FREE(data.dptr);
+       return ret;
+}
+
+void wcache_store_ndr(struct winbindd_domain *domain, uint32_t opnum,
+                     const DATA_BLOB *req, const DATA_BLOB *resp)
+{
+       TDB_DATA key, data;
+       uint32_t dom_seqnum, last_check;
+
+       if (!wcache_opnum_cacheable(opnum)) {
+               return;
+       }
+
+       if (wcache->tdb == NULL) {
+               return;
+       }
+
+       if (!wcache_fetch_seqnum(domain->name, &dom_seqnum, &last_check)) {
+               DEBUG(10, ("could not fetch seqnum for domain %s\n",
+                          domain->name));
+               return;
+       }
+
+       if (!wcache_ndr_key(talloc_tos(), domain->name, opnum, req, &key)) {
+               return;
+       }
+
+       data.dsize = resp->length + 4;
+       data.dptr = talloc_array(key.dptr, uint8_t, data.dsize);
+       if (data.dptr == NULL) {
+               goto done;
+       }
+
+       SIVAL(data.dptr, 0, dom_seqnum);
+       memcpy(data.dptr+4, resp->data, resp->length);
+
+       tdb_store(wcache->tdb, key, data, 0);
+
+done:
+       TALLOC_FREE(key.dptr);
+       return;
+}