winbind: Fix a typo in a wrong comment...
[kai/samba-autobuild/.git] / source3 / winbindd / winbindd_cache.c
index 5b1757b1dd3ecdbc4fba72089dde90a33fcacaa6..6123e96170053e6e09df0805878a9654629881e5 100644 (file)
 #include "winbindd.h"
 #include "tdb_validate.h"
 #include "../libcli/auth/libcli_auth.h"
-#include "../librpc/gen_ndr/ndr_wbint.h"
+#include "../librpc/gen_ndr/ndr_winbind.h"
 #include "ads.h"
 #include "nss_info.h"
 #include "../libcli/security/security.h"
+#include "passdb/machine_sid.h"
+#include "util_tdb.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
 
-#define WINBINDD_CACHE_VERSION 2
+#define WINBINDD_CACHE_VER1 1 /* initial db version */
+#define WINBINDD_CACHE_VER2 2 /* second version with timeouts for NDR entries */
+
+#define WINBINDD_CACHE_VERSION WINBINDD_CACHE_VER2
 #define WINBINDD_CACHE_VERSION_KEYSTR "WINBINDD_CACHE_VERSION"
 
 extern struct winbindd_methods reconnect_methods;
 #ifdef HAVE_ADS
-extern struct winbindd_methods ads_methods;
+extern struct winbindd_methods reconnect_ads_methods;
 #endif
 extern struct winbindd_methods builtin_passdb_methods;
 extern struct winbindd_methods sam_passdb_methods;
@@ -54,8 +59,6 @@ extern struct winbindd_methods sam_passdb_methods;
 
 static const char *non_centry_keys[] = {
        "SEQNUM/",
-       "DR/",
-       "DE/",
        "WINBINDD_OFFLINE",
        WINBINDD_CACHE_VERSION_KEYSTR,
        NULL
@@ -96,10 +99,10 @@ struct winbind_cache {
 
 struct cache_entry {
        NTSTATUS status;
-       uint32 sequence_number;
+       uint32_t sequence_number;
        uint64_t timeout;
-       uint8 *data;
-       uint32 len, ofs;
+       uint8_t *data;
+       uint32_t len, ofs;
 };
 
 void (*smb_panic_fn)(const char *const why) = smb_panic;
@@ -117,21 +120,24 @@ static struct winbind_cache *get_cache(struct winbindd_domain *domain)
 
        if (domain->internal) {
                domain->backend = &builtin_passdb_methods;
-               domain->initialized = True;
+       }
+
+       if (dom_sid_equal(&domain->sid, &global_sid_Builtin)) {
+               domain->initialized = true;
        }
 
        if (strequal(domain->name, get_global_sam_name()) &&
-           sid_check_is_domain(&domain->sid)) {
+           sid_check_is_our_sam(&domain->sid)) {
                domain->backend = &sam_passdb_methods;
-               domain->initialized = True;
        }
 
        if ( !domain->initialized ) {
-               init_dc_connection( domain );
+               /* We do not need a connection to an RW DC for cache operation */
+               init_dc_connection(domain, false);
        }
 
        /* 
-          OK.  listen up becasue I'm only going to say this once.
+          OK.  Listen up because I'm only going to say this once.
           We have the following scenarios to consider
           (a) trusted AD domains on a Samba DC,
           (b) trusted AD domains and we are joined to a non-kerberos domain
@@ -162,7 +168,7 @@ static struct winbind_cache *get_cache(struct winbindd_domain *domain)
                    && domain->active_directory
                    && !lp_winbind_rpc_only()) {
                        DEBUG(5,("get_cache: Setting ADS methods for domain %s\n", domain->name));
-                       domain->backend = &ads_methods;
+                       domain->backend = &reconnect_ads_methods;
                } else {
 #endif /* HAVE_ADS */
                        DEBUG(5,("get_cache: Setting MS-RPC methods for domain %s\n", domain->name));
@@ -222,11 +228,11 @@ static uint64_t centry_uint64_t(struct cache_entry *centry)
 }
 
 /*
-  pull a uint32 from a cache entry 
+  pull a uint32_t from a cache entry
 */
-static uint32 centry_uint32(struct cache_entry *centry)
+static uint32_t centry_uint32(struct cache_entry *centry)
 {
-       uint32 ret;
+       uint32_t ret;
 
        if (!centry_check_bytes(centry, 4)) {
                smb_panic_fn("centry_uint32");
@@ -237,25 +243,25 @@ static uint32 centry_uint32(struct cache_entry *centry)
 }
 
 /*
-  pull a uint16 from a cache entry 
+  pull a uint16_t from a cache entry
 */
-static uint16 centry_uint16(struct cache_entry *centry)
+static uint16_t centry_uint16(struct cache_entry *centry)
 {
-       uint16 ret;
+       uint16_t ret;
        if (!centry_check_bytes(centry, 2)) {
                smb_panic_fn("centry_uint16");
        }
-       ret = CVAL(centry->data, centry->ofs);
+       ret = SVAL(centry->data, centry->ofs);
        centry->ofs += 2;
        return ret;
 }
 
 /*
-  pull a uint8 from a cache entry 
+  pull a uint8_t from a cache entry
 */
-static uint8 centry_uint8(struct cache_entry *centry)
+static uint8_t centry_uint8(struct cache_entry *centry)
 {
-       uint8 ret;
+       uint8_t ret;
        if (!centry_check_bytes(centry, 1)) {
                smb_panic_fn("centry_uint8");
        }
@@ -275,7 +281,7 @@ static NTTIME centry_nttime(struct cache_entry *centry)
        }
        ret = IVAL(centry->data, centry->ofs);
        centry->ofs += 4;
-       ret += (uint64)IVAL(centry->data, centry->ofs) << 32;
+       ret += (uint64_t)IVAL(centry->data, centry->ofs) << 32;
        centry->ofs += 4;
        return ret;
 }
@@ -293,7 +299,7 @@ static time_t centry_time(struct cache_entry *centry)
 */
 static char *centry_string(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
 {
-       uint32 len;
+       uint32_t len;
        char *ret;
 
        len = centry_uint8(centry);
@@ -307,7 +313,7 @@ static char *centry_string(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
                smb_panic_fn("centry_string");
        }
 
-       ret = TALLOC_ARRAY(mem_ctx, char, len+1);
+       ret = talloc_array(mem_ctx, char, len+1);
        if (!ret) {
                smb_panic_fn("centry_string out of memory\n");
        }
@@ -322,7 +328,7 @@ static char *centry_string(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
 */
 static char *centry_hash16(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
 {
-       uint32 len;
+       uint32_t len;
        char *ret;
 
        len = centry_uint8(centry);
@@ -337,7 +343,7 @@ static char *centry_hash16(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
                return NULL;
        }
 
-       ret = TALLOC_ARRAY(mem_ctx, char, 16);
+       ret = talloc_array(mem_ctx, char, 16);
        if (!ret) {
                smb_panic_fn("centry_hash out of memory\n");
        }
@@ -392,48 +398,53 @@ static bool wcache_server_down(struct winbindd_domain *domain)
        return ret;
 }
 
-static bool wcache_fetch_seqnum(const char *domain_name, uint32_t *seqnum,
-                               uint32_t *last_seq_check)
+struct wcache_seqnum_state {
+       uint32_t *seqnum;
+       uint32_t *last_seq_check;
+};
+
+static int wcache_seqnum_parser(TDB_DATA key, TDB_DATA data,
+                               void *private_data)
 {
-       char *key;
-       TDB_DATA data;
+       struct wcache_seqnum_state *state = private_data;
 
-       if (wcache->tdb == NULL) {
-               DEBUG(10,("wcache_fetch_seqnum: tdb == NULL\n"));
-               return false;
+       if (data.dsize != 8) {
+               DEBUG(10, ("wcache_fetch_seqnum: invalid data size %d\n",
+                          (int)data.dsize));
+               return -1;
        }
 
-       key = talloc_asprintf(talloc_tos(), "SEQNUM/%s", domain_name);
-       if (key == NULL) {
-               DEBUG(10, ("talloc failed\n"));
-               return false;
-       }
+       *state->seqnum = IVAL(data.dptr, 0);
+       *state->last_seq_check = IVAL(data.dptr, 4);
+       return 0;
+}
 
-       data = tdb_fetch_bystring(wcache->tdb, key);
-       TALLOC_FREE(key);
+static bool wcache_fetch_seqnum(const char *domain_name, uint32_t *seqnum,
+                               uint32_t *last_seq_check)
+{
+       struct wcache_seqnum_state state = {
+               .seqnum = seqnum, .last_seq_check = last_seq_check
+       };
+       size_t len = strlen(domain_name);
+       char keystr[len+8];
+       TDB_DATA key = { .dptr = (uint8_t *)keystr, .dsize = sizeof(keystr) };
+       int ret;
 
-       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);
+       if (wcache->tdb == NULL) {
+               DEBUG(10,("wcache_fetch_seqnum: tdb == NULL\n"));
                return false;
        }
 
-       *seqnum = IVAL(data.dptr, 0);
-       *last_seq_check = IVAL(data.dptr, 4);
-       SAFE_FREE(data.dptr);
+       snprintf(keystr, sizeof(keystr),  "SEQNUM/%s", domain_name);
 
-       return true;
+       ret = tdb_parse_record(wcache->tdb, key, wcache_seqnum_parser,
+                              &state);
+       return (ret == 0);
 }
 
 static NTSTATUS fetch_cache_seqnum( struct winbindd_domain *domain, time_t now )
 {
-       uint32 last_check, time_diff;
+       uint32_t last_check, time_diff;
 
        if (!wcache_fetch_seqnum(domain->name, &domain->sequence_number,
                                 &last_check)) {
@@ -447,13 +458,13 @@ static NTSTATUS fetch_cache_seqnum( struct winbindd_domain *domain, time_t now )
        if ( time_diff > lp_winbind_cache_time() ) {
                DEBUG(10,("fetch_cache_seqnum: timeout [%s][%u @ %u]\n",
                        domain->name, domain->sequence_number,
-                       (uint32)domain->last_seq_check));
+                       (uint32_t)domain->last_seq_check));
                return NT_STATUS_UNSUCCESSFUL;
        }
 
        DEBUG(10,("fetch_cache_seqnum: success [%s][%u @ %u]\n", 
                domain->name, domain->sequence_number, 
-               (uint32)domain->last_seq_check));
+               (uint32_t)domain->last_seq_check));
 
        return NT_STATUS_OK;
 }
@@ -461,7 +472,9 @@ static NTSTATUS fetch_cache_seqnum( struct winbindd_domain *domain, time_t now )
 bool wcache_store_seqnum(const char *domain_name, uint32_t seqnum,
                         time_t last_seq_check)
 {
-       char *key_str;
+       size_t len = strlen(domain_name);
+       char keystr[len+8];
+       TDB_DATA key = { .dptr = (uint8_t *)keystr, .dsize = sizeof(keystr) };
        uint8_t buf[8];
        int ret;
 
@@ -470,22 +483,16 @@ bool wcache_store_seqnum(const char *domain_name, uint32_t seqnum,
                return false;
        }
 
-       key_str = talloc_asprintf(talloc_tos(), "SEQNUM/%s", domain_name);
-       if (key_str == NULL) {
-               DEBUG(10, ("talloc_asprintf failed\n"));
-               return false;
-       }
+       snprintf(keystr, sizeof(keystr),  "SEQNUM/%s", domain_name);
 
        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);
-       TALLOC_FREE(key_str);
-       if (ret == -1) {
+       ret = tdb_store(wcache->tdb, key, make_tdb_data(buf, sizeof(buf)),
+                       TDB_REPLACE);
+       if (ret != 0) {
                DEBUG(10, ("tdb_store_bystring failed: %s\n",
                           tdb_errorstr(wcache->tdb)));
-               TALLOC_FREE(key_str);
                return false;
        }
 
@@ -665,7 +672,7 @@ static struct cache_entry *wcache_fetch_raw(char *kstr)
 static bool is_my_own_sam_domain(struct winbindd_domain *domain)
 {
        if (strequal(domain->name, get_global_sam_name()) &&
-           sid_check_is_domain(&domain->sid)) {
+           sid_check_is_our_sam(&domain->sid)) {
                return true;
        }
 
@@ -752,7 +759,7 @@ static void wcache_delete(const char *format, ...)
 /*
   make sure we have at least len bytes available in a centry 
 */
-static void centry_expand(struct cache_entry *centry, uint32 len)
+static void centry_expand(struct cache_entry *centry, uint32_t len)
 {
        if (centry->len - centry->ofs >= len)
                return;
@@ -776,9 +783,9 @@ static void centry_put_uint64_t(struct cache_entry *centry, uint64_t v)
 }
 
 /*
-  push a uint32 into a centry 
+  push a uint32_t into a centry
 */
-static void centry_put_uint32(struct cache_entry *centry, uint32 v)
+static void centry_put_uint32(struct cache_entry *centry, uint32_t v)
 {
        centry_expand(centry, 4);
        SIVAL(centry->data, centry->ofs, v);
@@ -786,19 +793,19 @@ static void centry_put_uint32(struct cache_entry *centry, uint32 v)
 }
 
 /*
-  push a uint16 into a centry 
+  push a uint16_t into a centry
 */
-static void centry_put_uint16(struct cache_entry *centry, uint16 v)
+static void centry_put_uint16(struct cache_entry *centry, uint16_t v)
 {
        centry_expand(centry, 2);
-       SIVAL(centry->data, centry->ofs, v);
+       SSVAL(centry->data, centry->ofs, v);
        centry->ofs += 2;
 }
 
 /*
-  push a uint8 into a centry 
+  push a uint8_t into a centry
 */
-static void centry_put_uint8(struct cache_entry *centry, uint8 v)
+static void centry_put_uint8(struct cache_entry *centry, uint8_t v)
 {
        centry_expand(centry, 1);
        SCVAL(centry->data, centry->ofs, v);
@@ -833,7 +840,7 @@ static void centry_put_string(struct cache_entry *centry, const char *s)
 /* 
    push a 16 byte hash into a centry - treat as 16 byte string.
  */
-static void centry_put_hash16(struct cache_entry *centry, const uint8 val[16])
+static void centry_put_hash16(struct cache_entry *centry, const uint8_t val[16])
 {
        centry_put_uint8(centry, 16);
        centry_expand(centry, 16);
@@ -853,7 +860,7 @@ static void centry_put_sid(struct cache_entry *centry, const struct dom_sid *sid
 */
 static void centry_put_ntstatus(struct cache_entry *centry, NTSTATUS status)
 {
-       uint32 status_value = NT_STATUS_V(status);
+       uint32_t status_value = NT_STATUS_V(status);
        centry_put_uint32(centry, status_value);
 }
 
@@ -883,7 +890,8 @@ static void centry_put_time(struct cache_entry *centry, time_t t)
 /*
   start a centry for output. When finished, call centry_end()
 */
-struct cache_entry *centry_start(struct winbindd_domain *domain, NTSTATUS status)
+static struct cache_entry *centry_start(struct winbindd_domain *domain,
+                                       NTSTATUS status)
 {
        struct cache_entry *centry;
 
@@ -893,7 +901,7 @@ struct cache_entry *centry_start(struct winbindd_domain *domain, NTSTATUS status
        centry = SMB_XMALLOC_P(struct cache_entry);
 
        centry->len = 8192; /* reasonable default */
-       centry->data = SMB_XMALLOC_ARRAY(uint8, centry->len);
+       centry->data = SMB_XMALLOC_ARRAY(uint8_t, centry->len);
        centry->ofs = 0;
        centry->sequence_number = domain->sequence_number;
        centry->timeout = lp_winbind_cache_time() + time(NULL);
@@ -940,10 +948,19 @@ static void wcache_save_name_to_sid(struct winbindd_domain *domain,
        centry = centry_start(domain, status);
        if (!centry)
                return;
+
+       if ((domain_name == NULL) || (domain_name[0] == '\0')) {
+               struct winbindd_domain *mydomain =
+                       find_domain_from_sid_noinit(sid);
+               if (mydomain != NULL) {
+                       domain_name = mydomain->name;
+               }
+       }
+
        centry_put_uint32(centry, type);
        centry_put_sid(centry, sid);
        fstrcpy(uname, name);
-       strupper_m(uname);
+       (void)strupper_m(uname);
        centry_end(centry, "NS/%s/%s", domain_name, uname);
        DEBUG(10,("wcache_save_name_to_sid: %s\\%s -> %s (%s)\n", domain_name,
                  uname, sid_string_dbg(sid), nt_errstr(status)));
@@ -960,6 +977,14 @@ static void wcache_save_sid_to_name(struct winbindd_domain *domain, NTSTATUS sta
        if (!centry)
                return;
 
+       if ((domain_name == NULL) || (domain_name[0] == '\0')) {
+               struct winbindd_domain *mydomain =
+                       find_domain_from_sid_noinit(sid);
+               if (mydomain != NULL) {
+                       domain_name = mydomain->name;
+               }
+       }
+
        if (NT_STATUS_IS_OK(status)) {
                centry_put_uint32(centry, type);
                centry_put_string(centry, domain_name);
@@ -967,8 +992,8 @@ static void wcache_save_sid_to_name(struct winbindd_domain *domain, NTSTATUS sta
        }
 
        centry_end(centry, "SN/%s", sid_to_fstring(sid_string, sid));
-       DEBUG(10,("wcache_save_sid_to_name: %s -> %s (%s)\n", sid_string, 
-                 name, nt_errstr(status)));
+       DEBUG(10,("wcache_save_sid_to_name: %s -> %s\\%s (%s)\n", sid_string,
+                 domain_name, name, nt_errstr(status)));
        centry_free(centry);
 }
 
@@ -1061,7 +1086,7 @@ static void wcache_save_username_alias(struct winbindd_domain *domain,
        centry_put_string( centry, alias );
 
        fstrcpy(uname, name);
-       strupper_m(uname);
+       (void)strupper_m(uname);
        centry_end(centry, "NSS/NA/%s", uname);
 
        DEBUG(10,("wcache_save_username_alias: %s -> %s\n", name, alias ));
@@ -1082,7 +1107,7 @@ static void wcache_save_alias_username(struct winbindd_domain *domain,
        centry_put_string( centry, name );
 
        fstrcpy(uname, alias);
-       strupper_m(uname);
+       (void)strupper_m(uname);
        centry_end(centry, "NSS/AN/%s", uname);
 
        DEBUG(10,("wcache_save_alias_username: %s -> %s\n", alias, name ));
@@ -1108,13 +1133,18 @@ NTSTATUS resolve_username_to_alias( TALLOC_CTX *mem_ctx,
        if (!cache->tdb)
                goto do_query;
 
-       if ( (upper_name = SMB_STRDUP(name)) == NULL )
+       upper_name = talloc_strdup(mem_ctx, name);
+       if (upper_name == NULL) {
                return NT_STATUS_NO_MEMORY;
-       strupper_m(upper_name);
+       }
+       if (!strupper_m(upper_name)) {
+               talloc_free(upper_name);
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
        centry = wcache_fetch(cache, domain, "NSS/NA/%s", upper_name);
 
-       SAFE_FREE( upper_name );
+       talloc_free(upper_name);
 
        if (!centry)
                goto do_query;
@@ -1183,13 +1213,18 @@ NTSTATUS resolve_alias_to_username( TALLOC_CTX *mem_ctx,
        if (!cache->tdb)
                goto do_query;
 
-       if ( (upper_name = SMB_STRDUP(alias)) == NULL )
+       upper_name = talloc_strdup(mem_ctx, alias);
+       if (upper_name == NULL) {
                return NT_STATUS_NO_MEMORY;
-       strupper_m(upper_name);
+       }
+       if (!strupper_m(upper_name)) {
+               talloc_free(upper_name);
+               return NT_STATUS_INVALID_PARAMETER;
+       }
 
        centry = wcache_fetch(cache, domain, "NSS/AN/%s", upper_name);
 
-       SAFE_FREE( upper_name );
+       talloc_free(upper_name);
 
        if (!centry)
                goto do_query;
@@ -1253,7 +1288,7 @@ NTSTATUS wcache_cached_creds_exist(struct winbindd_domain *domain, const struct
        struct winbind_cache *cache = get_cache(domain);
        TDB_DATA data;
        fstring key_str, tmp;
-       uint32 rid;
+       uint32_t rid;
 
        if (!cache->tdb) {
                return NT_STATUS_INTERNAL_DB_ERROR;
@@ -1284,14 +1319,13 @@ NTSTATUS wcache_cached_creds_exist(struct winbindd_domain *domain, const struct
 NTSTATUS wcache_get_creds(struct winbindd_domain *domain, 
                          TALLOC_CTX *mem_ctx, 
                          const struct dom_sid *sid,
-                         const uint8 **cached_nt_pass,
-                         const uint8 **cached_salt)
+                         const uint8_t **cached_nt_pass,
+                         const uint8_t **cached_salt)
 {
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry = NULL;
        NTSTATUS status;
-       time_t t;
-       uint32 rid;
+       uint32_t rid;
        fstring tmp;
 
        if (!cache->tdb) {
@@ -1317,14 +1351,19 @@ NTSTATUS wcache_get_creds(struct winbindd_domain *domain,
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
-       t = centry_time(centry);
+       /*
+        * We don't use the time element at this moment,
+        * but we have to consume it, so that we don't
+        * neet to change the disk format of the cache.
+        */
+       (void)centry_time(centry);
 
        /* In the salted case this isn't actually the nt_hash itself,
           but the MD5 of the salt + nt_hash. Let the caller
           sort this out. It can tell as we only return the cached_salt
           if we are returning a salted cred. */
 
-       *cached_nt_pass = (const uint8 *)centry_hash16(centry, mem_ctx);
+       *cached_nt_pass = (const uint8_t *)centry_hash16(centry, mem_ctx);
        if (*cached_nt_pass == NULL) {
                fstring sidstr;
 
@@ -1341,7 +1380,7 @@ NTSTATUS wcache_get_creds(struct winbindd_domain *domain,
 
        /* We only have 17 bytes more data in the salted cred case. */
        if (centry->len - centry->ofs == 17) {
-               *cached_salt = (const uint8 *)centry_hash16(centry, mem_ctx);
+               *cached_salt = (const uint8_t *)centry_hash16(centry, mem_ctx);
        } else {
                *cached_salt = NULL;
        }
@@ -1364,13 +1403,13 @@ NTSTATUS wcache_get_creds(struct winbindd_domain *domain,
 
 NTSTATUS wcache_save_creds(struct winbindd_domain *domain, 
                           const struct dom_sid *sid,
-                          const uint8 nt_pass[NT_HASH_LEN])
+                          const uint8_t nt_pass[NT_HASH_LEN])
 {
        struct cache_entry *centry;
        fstring sid_string;
-       uint32 rid;
-       uint8 cred_salt[NT_HASH_LEN];
-       uint8 salted_hash[NT_HASH_LEN];
+       uint32_t rid;
+       uint8_t cred_salt[NT_HASH_LEN];
+       uint8_t salted_hash[NT_HASH_LEN];
 
        if (is_null_sid(sid)) {
                return NT_STATUS_INVALID_SID;
@@ -1408,7 +1447,7 @@ NTSTATUS wcache_save_creds(struct winbindd_domain *domain,
 /* Query display info. This is the basic user list fn */
 static NTSTATUS query_user_list(struct winbindd_domain *domain,
                                TALLOC_CTX *mem_ctx,
-                               uint32 *num_entries, 
+                               uint32_t *num_entries,
                                struct wbint_userinfo **info)
 {
        struct winbind_cache *cache = get_cache(domain);
@@ -1430,7 +1469,7 @@ do_fetch_cache:
        if (*num_entries == 0)
                goto do_cached;
 
-       (*info) = TALLOC_ARRAY(mem_ctx, struct wbint_userinfo, *num_entries);
+       (*info) = talloc_array(mem_ctx, struct wbint_userinfo, *num_entries);
        if (! (*info)) {
                smb_panic_fn("query_user_list out of memory");
        }
@@ -1483,7 +1522,7 @@ do_query:
                if (NT_STATUS_EQUAL(status, NT_STATUS_UNSUCCESSFUL)) {
                        DEBUG(3, ("query_user_list: flushing "
                                  "connection cache\n"));
-                       invalidate_cm_connection(&domain->conn);
+                       invalidate_cm_connection(domain);
                }
                if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
                    NT_STATUS_EQUAL(status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
@@ -1559,8 +1598,8 @@ skip_save:
 /* list all domain groups */
 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
                                TALLOC_CTX *mem_ctx,
-                               uint32 *num_entries, 
-                               struct acct_info **info)
+                               uint32_t *num_entries,
+                               struct wb_acct_info **info)
 {
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry = NULL;
@@ -1582,7 +1621,7 @@ do_fetch_cache:
        if (*num_entries == 0)
                goto do_cached;
 
-       (*info) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_entries);
+       (*info) = talloc_array(mem_ctx, struct wb_acct_info, *num_entries);
        if (! (*info)) {
                smb_panic_fn("enum_dom_groups out of memory");
        }
@@ -1654,8 +1693,8 @@ skip_save:
 /* list all domain groups */
 static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
                                TALLOC_CTX *mem_ctx,
-                               uint32 *num_entries, 
-                               struct acct_info **info)
+                               uint32_t *num_entries,
+                               struct wb_acct_info **info)
 {
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry = NULL;
@@ -1677,7 +1716,7 @@ do_fetch_cache:
        if (*num_entries == 0)
                goto do_cached;
 
-       (*info) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_entries);
+       (*info) = talloc_array(mem_ctx, struct wb_acct_info, *num_entries);
        if (! (*info)) {
                smb_panic_fn("enum_dom_groups out of memory");
        }
@@ -1776,6 +1815,10 @@ NTSTATUS wcache_name_to_sid(struct winbindd_domain *domain,
                return NT_STATUS_NO_MEMORY;
        }
 
+       if ((domain_name == NULL) || (domain_name[0] == '\0')) {
+               domain_name = domain->name;
+       }
+
        centry = wcache_fetch(cache, domain, "NS/%s/%s", domain_name, uname);
        TALLOC_FREE(uname);
        if (centry == NULL) {
@@ -1855,8 +1898,10 @@ static NTSTATUS name_to_sid(struct winbindd_domain *domain,
 
                /* Only save the reverse mapping if this was not a UPN */
                if (!strchr(name, '@')) {
-                       strupper_m(CONST_DISCARD(char *,domain_name));
-                       strlower_m(CONST_DISCARD(char *,name));
+                       if (!strupper_m(discard_const_p(char, domain_name))) {
+                               return NT_STATUS_INVALID_PARAMETER;
+                       }
+                       (void)strlower_m(discard_const_p(char, name));
                        wcache_save_sid_to_name(domain, status, sid, domain_name, name, *type);
                }
        }
@@ -1864,12 +1909,12 @@ static NTSTATUS name_to_sid(struct winbindd_domain *domain,
        return status;
 }
 
-NTSTATUS wcache_sid_to_name(struct winbindd_domain *domain,
-                           const struct dom_sid *sid,
-                           TALLOC_CTX *mem_ctx,
-                           char **domain_name,
-                           char **name,
-                           enum lsa_SidType *type)
+static NTSTATUS wcache_sid_to_name(struct winbindd_domain *domain,
+                                  const struct dom_sid *sid,
+                                  TALLOC_CTX *mem_ctx,
+                                  char **domain_name,
+                                  char **name,
+                                  enum lsa_SidType *type)
 {
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry;
@@ -1974,7 +2019,7 @@ static NTSTATUS sid_to_name(struct winbindd_domain *domain,
 static NTSTATUS rids_to_names(struct winbindd_domain *domain,
                              TALLOC_CTX *mem_ctx,
                              const struct dom_sid *domain_sid,
-                             uint32 *rids,
+                             uint32_t *rids,
                              size_t num_rids,
                              char **domain_name,
                              char ***names,
@@ -2000,8 +2045,8 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
                return NT_STATUS_OK;
        }
 
-       *names = TALLOC_ARRAY(mem_ctx, char *, num_rids);
-       *types = TALLOC_ARRAY(mem_ctx, enum lsa_SidType, num_rids);
+       *names = talloc_array(mem_ctx, char *, num_rids);
+       *types = talloc_array(mem_ctx, enum lsa_SidType, num_rids);
 
        if ((*names == NULL) || (*types == NULL)) {
                result = NT_STATUS_NO_MEMORY;
@@ -2050,6 +2095,7 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
                } else {
                        /* something's definitely wrong */
                        result = centry->status;
+                       centry_free(centry);
                        goto error;
                }
 
@@ -2084,6 +2130,19 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
                        old_status) {
                        have_mapped = have_unmapped = false;
 
+                       *names = talloc_array(mem_ctx, char *, num_rids);
+                       if (*names == NULL) {
+                               result = NT_STATUS_NO_MEMORY;
+                               goto error;
+                       }
+
+                       *types = talloc_array(mem_ctx, enum lsa_SidType,
+                                             num_rids);
+                       if (*types == NULL) {
+                               result = NT_STATUS_NO_MEMORY;
+                               goto error;
+                       }
+
                        for (i=0; i<num_rids; i++) {
                                struct dom_sid sid;
                                struct cache_entry *centry;
@@ -2125,6 +2184,7 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
                                } else {
                                        /* something's definitely wrong */
                                        result = centry->status;
+                                       centry_free(centry);
                                        goto error;
                                }
 
@@ -2255,6 +2315,40 @@ NTSTATUS wcache_query_user(struct winbindd_domain *domain,
        return status;
 }
 
+
+/**
+* @brief Query a fullname from the username cache (for further gecos processing)
+*
+* @param domain                A pointer to the winbindd_domain struct.
+* @param mem_ctx       The talloc context.
+* @param user_sid      The user sid.
+* @param full_name     A pointer to the full_name string.
+*
+* @return NTSTATUS code
+*/
+NTSTATUS wcache_query_user_fullname(struct winbindd_domain *domain,
+                                   TALLOC_CTX *mem_ctx,
+                                   const struct dom_sid *user_sid,
+                                   const char **full_name)
+{
+       NTSTATUS status;
+       struct wbint_userinfo info;
+
+       status = wcache_query_user(domain, mem_ctx, user_sid, &info);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       if (info.full_name != NULL) {
+               *full_name = talloc_strdup(mem_ctx, info.full_name);
+               if (*full_name == NULL) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
+
+       return NT_STATUS_OK;
+}
+
 /* Lookup user information from a rid */
 static NTSTATUS query_user(struct winbindd_domain *domain,
                           TALLOC_CTX *mem_ctx,
@@ -2368,7 +2462,7 @@ NTSTATUS wcache_lookup_usergroups(struct winbindd_domain *domain,
 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
                                  TALLOC_CTX *mem_ctx,
                                  const struct dom_sid *user_sid,
-                                 uint32 *num_groups, struct dom_sid **user_gids)
+                                 uint32_t *num_groups, struct dom_sid **user_gids)
 {
        struct cache_entry *centry = NULL;
        NTSTATUS status;
@@ -2518,8 +2612,8 @@ NTSTATUS wcache_lookup_useraliases(struct winbindd_domain *domain,
 
 static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
                                   TALLOC_CTX *mem_ctx,
-                                  uint32 num_sids, const struct dom_sid *sids,
-                                  uint32 *num_aliases, uint32 **alias_rids)
+                                  uint32_t num_sids, const struct dom_sid *sids,
+                                  uint32_t *num_aliases, uint32_t **alias_rids)
 {
        struct cache_entry *centry = NULL;
        NTSTATUS status;
@@ -2624,7 +2718,7 @@ NTSTATUS wcache_lookup_groupmem(struct winbindd_domain *domain,
 
        *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);
+       *name_types = talloc_array(mem_ctx, uint32_t, *num_names);
 
        if ((*sid_mem == NULL) || (*names == NULL) || (*name_types == NULL)) {
                TALLOC_FREE(*sid_mem);
@@ -2653,9 +2747,9 @@ 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,
+                               uint32_t *num_names,
                                struct dom_sid **sid_mem, char ***names,
-                               uint32 **name_types)
+                               uint32_t **name_types)
 {
        struct cache_entry *centry = NULL;
        NTSTATUS status;
@@ -2724,7 +2818,7 @@ skip_save:
 }
 
 /* find the sequence number for a domain */
-static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
+static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32_t *seq)
 {
        refresh_sequence_number(domain, false);
 
@@ -2768,7 +2862,7 @@ static NTSTATUS trusted_domains(struct winbindd_domain *domain,
        }
 
 do_fetch_cache:
-       trusts->array = TALLOC_ZERO_ARRAY(mem_ctx, struct netr_DomainTrust, num_domains);
+       trusts->array = talloc_zero_array(mem_ctx, struct netr_DomainTrust, num_domains);
        if (!trusts->array) {
                TALLOC_FREE(dom_list);
                return NT_STATUS_NO_MEMORY;
@@ -3006,7 +3100,7 @@ void wcache_invalidate_samlogon(struct winbindd_domain *domain,
         fstring key_str, sid_string;
        struct winbind_cache *cache;
 
-       /* dont clear cached U/SID and UG/SID entries when we want to logon
+       /* don't clear cached U/SID and UG/SID entries when we want to logon
         * offline - gd */
 
        if (lp_winbind_offline_logon()) {
@@ -3091,6 +3185,8 @@ bool wcache_invalidate_cache_noinit(void)
 
 bool init_wcache(void)
 {
+       char *db_path;
+
        if (wcache == NULL) {
                wcache = SMB_XMALLOC_P(struct winbind_cache);
                ZERO_STRUCTP(wcache);
@@ -3099,13 +3195,18 @@ bool init_wcache(void)
        if (wcache->tdb != NULL)
                return true;
 
+       db_path = state_path("winbindd_cache.tdb");
+       if (db_path == NULL) {
+               return false;
+       }
+
        /* when working offline we must not clear the cache on restart */
-       wcache->tdb = tdb_open_log(cache_path("winbindd_cache.tdb"),
+       wcache->tdb = tdb_open_log(db_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);
-
+       TALLOC_FREE(db_path);
        if (wcache->tdb == NULL) {
                DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
                return false;
@@ -3123,7 +3224,7 @@ bool init_wcache(void)
 bool initialize_winbindd_cache(void)
 {
        bool cache_bad = true;
-       uint32 vers;
+       uint32_t vers;
 
        if (!init_wcache()) {
                DEBUG(0,("initialize_winbindd_cache: init_wcache failed.\n"));
@@ -3137,6 +3238,8 @@ bool initialize_winbindd_cache(void)
        }
 
        if (cache_bad) {
+               char *db_path;
+
                DEBUG(0,("initialize_winbindd_cache: clearing cache "
                        "and re-creating with version number %d\n",
                        WINBINDD_CACHE_VERSION ));
@@ -3144,12 +3247,19 @@ bool initialize_winbindd_cache(void)
                tdb_close(wcache->tdb);
                wcache->tdb = NULL;
 
-               if (unlink(cache_path("winbindd_cache.tdb")) == -1) {
+               db_path = state_path("winbindd_cache.tdb");
+               if (db_path == NULL) {
+                       return false;
+               }
+
+               if (unlink(db_path) == -1) {
                        DEBUG(0,("initialize_winbindd_cache: unlink %s failed %s ",
-                               cache_path("winbindd_cache.tdb"),
+                               db_path,
                                strerror(errno) ));
+                       TALLOC_FREE(db_path);
                        return false;
                }
+               TALLOC_FREE(db_path);
                if (!init_wcache()) {
                        DEBUG(0,("initialize_winbindd_cache: re-initialization "
                                        "init_wcache failed.\n"));
@@ -3262,6 +3372,8 @@ static int traverse_fn_cleanup(TDB_CONTEXT *the_tdb, TDB_DATA kbuf,
 /* flush the cache */
 void wcache_flush_cache(void)
 {
+       char *db_path;
+
        if (!wcache)
                return;
        if (wcache->tdb) {
@@ -3272,13 +3384,18 @@ void wcache_flush_cache(void)
                return;
        }
 
+       db_path = state_path("winbindd_cache.tdb");
+       if (db_path == NULL) {
+               return;
+       }
+
        /* when working offline we must not clear the cache on restart */
-       wcache->tdb = tdb_open_log(cache_path("winbindd_cache.tdb"),
-                               WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE, 
+       wcache->tdb = tdb_open_log(db_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);
-
+       TALLOC_FREE(db_path);
        if (!wcache->tdb) {
                DEBUG(0,("Failed to open winbindd_cache.tdb!\n"));
                return;
@@ -3377,7 +3494,7 @@ NTSTATUS wcache_remove_oldest_cached_creds(struct winbindd_domain *domain, const
        ret = tdb_traverse(cache->tdb, traverse_fn_get_credlist, NULL);
        if (ret == 0) {
                return NT_STATUS_OK;
-       } else if ((ret == -1) || (wcache_cred_list == NULL)) {
+       } else if ((ret < 0) || (wcache_cred_list == NULL)) {
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
@@ -3506,7 +3623,7 @@ static struct cache_entry *create_centry_validate(const char *kstr, TDB_DATA dat
        struct cache_entry *centry;
 
        centry = SMB_XMALLOC_P(struct cache_entry);
-       centry->data = (unsigned char *)memdup(data.dptr, data.dsize);
+       centry->data = (unsigned char *)smb_memdup(data.dptr, data.dsize);
        if (!centry->data) {
                SAFE_FREE(centry);
                return NULL;
@@ -3691,13 +3808,13 @@ static int validate_ul(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_entries, i;
+       int32_t num_entries, i;
 
        if (!centry) {
                return 1;
        }
 
-       num_entries = (int32)centry_uint32(centry);
+       num_entries = (int32_t)centry_uint32(centry);
 
        for (i=0; i< num_entries; i++) {
                struct dom_sid sid;
@@ -3722,7 +3839,7 @@ static int validate_gl(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_entries, i;
+       int32_t num_entries, i;
 
        if (!centry) {
                return 1;
@@ -3749,7 +3866,7 @@ static int validate_ug(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_groups, i;
+       int32_t num_groups, i;
 
        if (!centry) {
                return 1;
@@ -3775,7 +3892,7 @@ static int validate_ua(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_aliases, i;
+       int32_t num_aliases, i;
 
        if (!centry) {
                return 1;
@@ -3800,7 +3917,7 @@ static int validate_gm(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_names, i;
+       int32_t num_names, i;
 
        if (!centry) {
                return 1;
@@ -4023,7 +4140,8 @@ static int cache_traverse_validate_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_D
        struct tdb_validation_status *v_state = (struct tdb_validation_status *)state;
 
        /* Paranoia check. */
-       if (strncmp("UA/", (const char *)kbuf.dptr, 3) == 0) {
+       if (strncmp("UA/", (const char *)kbuf.dptr, 3) == 0 ||
+           strncmp("NDR/", (const char *)kbuf.dptr, 4) == 0) {
                max_key_len = 1024 * 1024;
        }
        if (kbuf.dsize > max_key_len) {
@@ -4064,9 +4182,9 @@ static int cache_traverse_validate_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_D
        }
 
        DEBUG(0,("cache_traverse_validate_fn: unknown cache entry\nkey :\n"));
-       dump_data(0, (uint8 *)kbuf.dptr, kbuf.dsize);
+       dump_data(0, (uint8_t *)kbuf.dptr, kbuf.dsize);
        DEBUG(0,("data :\n"));
-       dump_data(0, (uint8 *)dbuf.dptr, dbuf.dsize);
+       dump_data(0, (uint8_t *)dbuf.dptr, dbuf.dsize);
        v_state->unknown_key = true;
        v_state->success = false;
        return 1; /* terminate. */
@@ -4079,6 +4197,70 @@ 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)
+{
+       uint64_t ctimeout;
+       TDB_DATA blob;
+
+       if (is_non_centry_key(key)) {
+               return 0;
+       }
+
+       if (data.dptr == NULL || data.dsize == 0) {
+               if (tdb_delete(tdb, key) < 0) {
+                       DEBUG(0, ("tdb_delete for [%s] failed!\n",
+                                 key.dptr));
+                       return 1;
+               }
+       }
+
+       /* add timeout to blob (uint64_t) */
+       blob.dsize = data.dsize + 8;
+
+       blob.dptr = SMB_XMALLOC_ARRAY(uint8_t, blob.dsize);
+       if (blob.dptr == NULL) {
+               return 1;
+       }
+       memset(blob.dptr, 0, blob.dsize);
+
+       /* copy status and seqnum */
+       memcpy(blob.dptr, data.dptr, 8);
+
+       /* add timeout */
+       ctimeout = lp_winbind_cache_time() + time(NULL);
+       SBVAL(blob.dptr, 8, ctimeout);
+
+       /* 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",
+                         key.dptr));
+               SAFE_FREE(blob.dptr);
+               return 1;
+       }
+
+       SAFE_FREE(blob.dptr);
+       return 0;
+}
+
+static bool wbcache_upgrade_v1_to_v2(TDB_CONTEXT *tdb)
+{
+       int rc;
+
+       DEBUG(1, ("Upgrade to version 2 of the winbindd_cache.tdb\n"));
+
+       rc = tdb_traverse(tdb, wbcache_update_centry_fn, NULL);
+       if (rc < 0) {
+               return false;
+       }
+
+       return true;
+}
+
 /***********************************************************************
  Try and validate every entry in the winbindd cache. If we fail here,
  delete the cache tdb and return non-zero.
@@ -4087,26 +4269,56 @@ static void validate_panic(const char *const why)
 int winbindd_validate_cache(void)
 {
        int ret = -1;
-       const char *tdb_path = cache_path("winbindd_cache.tdb");
+       char *tdb_path = NULL;
        TDB_CONTEXT *tdb = NULL;
+       uint32_t vers_id;
+       bool ok;
 
        DEBUG(10, ("winbindd_validate_cache: replacing panic function\n"));
        smb_panic_fn = validate_panic;
 
+       tdb_path = state_path("winbindd_cache.tdb");
+       if (tdb_path == NULL) {
+               goto done;
+       }
 
-       tdb = tdb_open_log(tdb_path, 
+       tdb = tdb_open_log(tdb_path,
                           WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE,
                           TDB_INCOMPATIBLE_HASH |
-                          ( lp_winbind_offline_logon() 
-                            ? TDB_DEFAULT 
+                          ( lp_winbind_offline_logon()
+                            ? TDB_DEFAULT
                             : TDB_DEFAULT | TDB_CLEAR_IF_FIRST ),
-                          O_RDWR|O_CREAT, 
+                          O_RDWR|O_CREAT,
                           0600);
        if (!tdb) {
                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)) {
+               DEBUG(10, ("Fresh database\n"));
+               tdb_store_uint32(tdb, 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);
+                       if (!ok) {
+                               DEBUG(10, ("winbindd_validate_cache: upgrade to version 2 failed.\n"));
+                               unlink(tdb_path);
+                               goto done;
+                       }
+
+                       tdb_store_uint32(tdb,
+                                        WINBINDD_CACHE_VERSION_KEYSTR,
+                                        WINBINDD_CACHE_VERSION);
+                       vers_id = WINBINDD_CACHE_VER2;
+               }
+       }
+
        tdb_close(tdb);
 
        ret = tdb_validate_and_backup(tdb_path, cache_traverse_validate_fn);
@@ -4118,6 +4330,7 @@ int winbindd_validate_cache(void)
        }
 
 done:
+       TALLOC_FREE(tdb_path);
        DEBUG(10, ("winbindd_validate_cache: restoring panic function\n"));
        smb_panic_fn = smb_panic;
        return ret;
@@ -4130,11 +4343,15 @@ done:
 int winbindd_validate_cache_nobackup(void)
 {
        int ret = -1;
-       const char *tdb_path = cache_path("winbindd_cache.tdb");
+       char *tdb_path;
 
        DEBUG(10, ("winbindd_validate_cache: replacing panic function\n"));
        smb_panic_fn = validate_panic;
 
+       tdb_path = state_path("winbindd_cache.tdb");
+       if (tdb_path == NULL) {
+               goto err_panic_restore;
+       }
 
        if (wcache == NULL || wcache->tdb == NULL) {
                ret = tdb_validate_open(tdb_path, cache_traverse_validate_fn);
@@ -4147,6 +4364,8 @@ int winbindd_validate_cache_nobackup(void)
                           "successful.\n"));
        }
 
+       TALLOC_FREE(tdb_path);
+err_panic_restore:
        DEBUG(10, ("winbindd_validate_cache_nobackup: restoring panic "
                   "function\n"));
        smb_panic_fn = smb_panic;
@@ -4197,10 +4416,10 @@ static bool add_wbdomain_to_tdc_array( struct winbindd_domain *new_dom,
 
        if ( !set_only ) {
                if ( !*domains ) {
-                       list = TALLOC_ARRAY( NULL, struct winbindd_tdc_domain, 1 );
+                       list = talloc_array( NULL, struct winbindd_tdc_domain, 1 );
                        idx = 0;
                } else {
-                       list = TALLOC_REALLOC_ARRAY( *domains, *domains, 
+                       list = talloc_realloc( *domains, *domains, 
                                                     struct winbindd_tdc_domain,  
                                                     (*num_domains)+1);
                        idx = *num_domains;             
@@ -4212,8 +4431,16 @@ static bool add_wbdomain_to_tdc_array( struct winbindd_domain *new_dom,
        if ( !list )
                return false;
 
-       list[idx].domain_name = talloc_strdup( list, new_dom->name );
-       list[idx].dns_name = talloc_strdup( list, new_dom->alt_name );
+       list[idx].domain_name = talloc_strdup(list, new_dom->name);
+       if (list[idx].domain_name == NULL) {
+               return false;
+       }
+       if (new_dom->alt_name != NULL) {
+               list[idx].dns_name = talloc_strdup(list, new_dom->alt_name);
+               if (list[idx].dns_name == NULL) {
+                       return false;
+               }
+       }
 
        if ( !is_null_sid( &new_dom->sid ) ) {
                sid_copy( &list[idx].sid, &new_dom->sid );
@@ -4296,7 +4523,7 @@ static int pack_tdc_domains( struct winbindd_tdc_domain *domains,
 
                len += tdb_pack( buffer+len, buflen-len, "fffddd",
                                 domains[i].domain_name,
-                                domains[i].dns_name,
+                                domains[i].dns_name ? domains[i].dns_name : "",
                                 sid_to_fstring(tmp, &domains[i].sid),
                                 domains[i].trust_flags,
                                 domains[i].trust_attribs,
@@ -4327,7 +4554,7 @@ static size_t unpack_tdc_domains( unsigned char *buf, int buflen,
                                  struct winbindd_tdc_domain **domains )
 {
        fstring domain_name, dns_name, sid_string;      
-       uint32 type, attribs, flags;
+       uint32_t type, attribs, flags;
        int num_domains;
        int len = 0;
        int i;
@@ -4340,14 +4567,16 @@ static size_t unpack_tdc_domains( unsigned char *buf, int buflen,
                return 0;
        }
 
-       list = TALLOC_ARRAY( NULL, struct winbindd_tdc_domain, num_domains );
+       list = talloc_array( NULL, struct winbindd_tdc_domain, num_domains );
        if ( !list ) {
                DEBUG(0,("unpack_tdc_domains: Failed to talloc() domain list!\n"));
                return 0;               
        }
 
        for ( i=0; i<num_domains; i++ ) {
-               len += tdb_unpack( buf+len, buflen-len, "fffddd",
+               int this_len;
+
+               this_len = tdb_unpack( buf+len, buflen-len, "fffddd",
                                   domain_name,
                                   dns_name,
                                   sid_string,
@@ -4355,11 +4584,12 @@ static size_t unpack_tdc_domains( unsigned char *buf, int buflen,
                                   &attribs,
                                   &type );
 
-               if ( len == -1 ) {
+               if ( this_len == -1 ) {
                        DEBUG(5,("unpack_tdc_domains: Failed to unpack domain array\n"));
                        TALLOC_FREE( list );                    
                        return 0;
                }
+               len += this_len;
 
                DEBUG(11,("unpack_tdc_domains: Unpacking domain %s (%s) "
                          "SID %s, flags = 0x%x, attribs = 0x%x, type = 0x%x\n",
@@ -4367,7 +4597,10 @@ static size_t unpack_tdc_domains( unsigned char *buf, int buflen,
                          flags, attribs, type));
 
                list[i].domain_name = talloc_strdup( list, domain_name );
-               list[i].dns_name = talloc_strdup( list, dns_name );
+               list[i].dns_name = NULL;
+               if (dns_name[0] != '\0') {
+                       list[i].dns_name = talloc_strdup(list, dns_name);
+               }
                if ( !string_to_sid( &(list[i].sid), sid_string ) ) {                   
                        DEBUG(10,("unpack_tdc_domains: no SID for domain %s\n",
                                  domain_name));
@@ -4414,7 +4647,7 @@ static bool wcache_tdc_store_list( struct winbindd_tdc_domain *domains, size_t n
        SAFE_FREE( data.dptr );
        SAFE_FREE( key.dptr );
 
-       return ( ret != -1 );   
+       return ( ret == 0 );
 }
 
 /*********************************************************************
@@ -4494,6 +4727,38 @@ bool wcache_tdc_add_domain( struct winbindd_domain *domain )
        return ret;     
 }
 
+static struct winbindd_tdc_domain *wcache_tdc_dup_domain(
+       TALLOC_CTX *mem_ctx, const struct winbindd_tdc_domain *src)
+{
+       struct winbindd_tdc_domain *dst;
+
+       dst = talloc(mem_ctx, struct winbindd_tdc_domain);
+       if (dst == NULL) {
+               goto fail;
+       }
+       dst->domain_name = talloc_strdup(dst, src->domain_name);
+       if (dst->domain_name == NULL) {
+               goto fail;
+       }
+
+       dst->dns_name = NULL;
+       if (src->dns_name != NULL) {
+               dst->dns_name = talloc_strdup(dst, src->dns_name);
+               if (dst->dns_name == NULL) {
+                       goto fail;
+               }
+       }
+
+       sid_copy(&dst->sid, &src->sid);
+       dst->trust_flags = src->trust_flags;
+       dst->trust_type = src->trust_type;
+       dst->trust_attribs = src->trust_attribs;
+       return dst;
+fail:
+       TALLOC_FREE(dst);
+       return NULL;
+}
+
 /*********************************************************************
  ********************************************************************/
 
@@ -4507,7 +4772,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() ) {
-               return false;
+               return NULL;
        }
 
        /* fetch the list */
@@ -4521,17 +4786,7 @@ struct winbindd_tdc_domain * wcache_tdc_fetch_domain( TALLOC_CTX *ctx, const cha
                        DEBUG(10,("wcache_tdc_fetch_domain: Found domain %s\n",
                                  name));
 
-                       d = TALLOC_P( ctx, struct winbindd_tdc_domain );
-                       if ( !d )
-                               break;                  
-
-                       d->domain_name = talloc_strdup( d, dom_list[i].domain_name );
-                       d->dns_name = talloc_strdup( d, dom_list[i].dns_name );
-                       sid_copy( &d->sid, &dom_list[i].sid );
-                       d->trust_flags   = dom_list[i].trust_flags;
-                       d->trust_type    = dom_list[i].trust_type;
-                       d->trust_attribs = dom_list[i].trust_attribs;
-
+                       d = wcache_tdc_dup_domain(ctx, &dom_list[i]);
                        break;
                }
        }
@@ -4557,7 +4812,7 @@ struct winbindd_tdc_domain*
                  sid_string_dbg(sid)));
 
        if (!init_wcache()) {
-               return false;
+               return NULL;
        }
 
        /* fetch the list */
@@ -4565,25 +4820,13 @@ struct winbindd_tdc_domain*
        wcache_tdc_fetch_list(&dom_list, &num_domains);
 
        for (i = 0; i<num_domains; i++) {
-               if (sid_equal(sid, &(dom_list[i].sid))) {
+               if (dom_sid_equal(sid, &(dom_list[i].sid))) {
                        DEBUG(10, ("wcache_tdc_fetch_domainbysid: "
                                   "Found domain %s for SID %s\n",
                                   dom_list[i].domain_name,
                                   sid_string_dbg(sid)));
 
-                       d = TALLOC_P(ctx, struct winbindd_tdc_domain);
-                       if (!d)
-                               break;
-
-                       d->domain_name = talloc_strdup(d,
-                                                      dom_list[i].domain_name);
-
-                       d->dns_name = talloc_strdup(d, dom_list[i].dns_name);
-                       sid_copy(&d->sid, &dom_list[i].sid);
-                       d->trust_flags = dom_list[i].trust_flags;
-                       d->trust_type = dom_list[i].trust_type;
-                       d->trust_attribs = dom_list[i].trust_attribs;
-
+                       d = wcache_tdc_dup_domain(ctx, &dom_list[i]);
                        break;
                }
        }
@@ -4617,7 +4860,7 @@ static void wcache_save_user_pwinfo(struct winbindd_domain *domain,
                                    const char *homedir,
                                    const char *shell,
                                    const char *gecos,
-                                   uint32 gid)
+                                   uint32_t gid)
 {
        struct cache_entry *centry;
        fstring tmp;
@@ -4718,7 +4961,7 @@ struct winbindd_methods cache_methods = {
        trusted_domains
 };
 
-static bool wcache_ndr_key(TALLOC_CTX *mem_ctx, char *domain_name,
+static bool wcache_ndr_key(TALLOC_CTX *mem_ctx, const char *domain_name,
                           uint32_t opnum, const DATA_BLOB *req,
                           TDB_DATA *pkey)
 {
@@ -4801,7 +5044,7 @@ bool wcache_fetch_ndr(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
                        goto fail;
                }
                entry_timeout = BVAL(data.dptr, 4);
-               if (entry_timeout > time(NULL)) {
+               if (time(NULL) > entry_timeout) {
                        DEBUG(10, ("Entry has timed out\n"));
                        goto fail;
                }