s3: winbind: refresh_sequence_number is only ever called with 'false'.
[metze/samba/wip.git] / source3 / winbindd / winbindd_cache.c
index 72529276c8e6450c46f473353d825089136cf876..2bce12da9a7063cdd4e9b3ce87b6cc89335d597c 100644 (file)
@@ -28,7 +28,7 @@
 #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"
@@ -46,7 +46,7 @@
 
 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;
@@ -99,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;
@@ -111,6 +111,15 @@ void (*smb_panic_fn)(const char *const why) = smb_panic;
 
 static struct winbind_cache *wcache;
 
+static char *wcache_path(void)
+{
+       /*
+        * Data needs to be kept persistent in state directory for
+        * running with "winbindd offline logon".
+        */
+       return state_path("winbindd_cache.tdb");
+}
+
 /* get the winbind_cache structure */
 static struct winbind_cache *get_cache(struct winbindd_domain *domain)
 {
@@ -120,21 +129,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_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
@@ -165,7 +177,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));
@@ -225,11 +237,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");
@@ -240,11 +252,11 @@ 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");
        }
@@ -254,11 +266,11 @@ static uint16 centry_uint16(struct cache_entry *centry)
 }
 
 /*
-  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");
        }
@@ -278,7 +290,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;
 }
@@ -296,7 +308,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);
@@ -325,7 +337,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);
@@ -395,48 +407,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)) {
@@ -450,13 +467,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;
 }
@@ -464,7 +481,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;
 
@@ -473,22 +492,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);
+       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_compat(wcache->tdb)));
-               TALLOC_FREE(key_str);
+                          tdb_errorstr(wcache->tdb)));
                return false;
        }
 
@@ -505,11 +518,10 @@ static bool store_cache_seqnum( struct winbindd_domain *domain )
 }
 
 /*
-  refresh the domain sequence number. If force is true
-  then always refresh it, no matter how recently we fetched it
+  refresh the domain sequence number on timeout.
 */
 
-static void refresh_sequence_number(struct winbindd_domain *domain, bool force)
+static void refresh_sequence_number(struct winbindd_domain *domain)
 {
        NTSTATUS status;
        unsigned time_diff;
@@ -532,7 +544,7 @@ static void refresh_sequence_number(struct winbindd_domain *domain, bool force)
        time_diff = t - domain->last_seq_check;
 
        /* see if we have to refetch the domain sequence number */
-       if (!force && (time_diff < cache_time) &&
+       if ((time_diff < cache_time) &&
                        (domain->sequence_number != DOM_SEQUENCE_NONE) &&
                        NT_STATUS_IS_OK(domain->last_status)) {
                DEBUG(10, ("refresh_sequence_number: %s time ok\n", domain->name));
@@ -639,7 +651,7 @@ static struct cache_entry *wcache_fetch_raw(char *kstr)
        TDB_DATA key;
 
        key = string_tdb_data(kstr);
-       data = tdb_fetch_compat(wcache->tdb, key);
+       data = tdb_fetch(wcache->tdb, key);
        if (!data.dptr) {
                /* a cache miss */
                return NULL;
@@ -706,7 +718,7 @@ static struct cache_entry *wcache_fetch(struct winbind_cache *cache,
                return NULL;
        }
 
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
 
        va_start(ap, format);
        smb_xvasprintf(&kstr, format, ap);
@@ -755,7 +767,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;
@@ -779,9 +791,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);
@@ -789,9 +801,9 @@ 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);
        SSVAL(centry->data, centry->ofs, v);
@@ -799,9 +811,9 @@ static void centry_put_uint16(struct cache_entry *centry, uint16 v)
 }
 
 /*
-  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);
@@ -836,7 +848,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);
@@ -856,7 +868,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);
 }
 
@@ -886,7 +898,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;
 
@@ -896,7 +909,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);
@@ -1283,7 +1296,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;
@@ -1299,7 +1312,7 @@ NTSTATUS wcache_cached_creds_exist(struct winbindd_domain *domain, const struct
 
        fstr_sprintf(key_str, "CRED/%s", sid_to_fstring(tmp, sid));
 
-       data = tdb_fetch_compat(cache->tdb, string_tdb_data(key_str));
+       data = tdb_fetch(cache->tdb, string_tdb_data(key_str));
        if (!data.dptr) {
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
@@ -1314,13 +1327,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;
-       uint32 rid;
+       uint32_t rid;
        fstring tmp;
 
        if (!cache->tdb) {
@@ -1358,7 +1371,7 @@ NTSTATUS wcache_get_creds(struct winbindd_domain *domain,
           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;
 
@@ -1375,7 +1388,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;
        }
@@ -1398,13 +1411,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;
@@ -1442,7 +1455,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);
@@ -1517,7 +1530,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)) {
@@ -1553,7 +1566,7 @@ do_query:
                 (retry++ < 5));
 
        /* and save it */
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -1593,7 +1606,7 @@ skip_save:
 /* list all domain groups */
 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
                                TALLOC_CTX *mem_ctx,
-                               uint32 *num_entries, 
+                               uint32_t *num_entries,
                                struct wb_acct_info **info)
 {
        struct winbind_cache *cache = get_cache(domain);
@@ -1665,7 +1678,7 @@ do_query:
                }
        }
        /* and save it */
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -1688,7 +1701,7 @@ skip_save:
 /* list all domain groups */
 static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
                                TALLOC_CTX *mem_ctx,
-                               uint32 *num_entries, 
+                               uint32_t *num_entries,
                                struct wb_acct_info **info)
 {
        struct winbind_cache *cache = get_cache(domain);
@@ -1770,7 +1783,7 @@ do_query:
                }
        }
        /* and save it */
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -1885,7 +1898,7 @@ static NTSTATUS name_to_sid(struct winbindd_domain *domain,
                }
        }
        /* and save it */
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
 
        if (domain->online &&
            (NT_STATUS_IS_OK(status) || NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED))) {
@@ -1904,12 +1917,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;
@@ -1999,7 +2012,7 @@ static NTSTATUS sid_to_name(struct winbindd_domain *domain,
                }
        }
        /* and save it */
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -2014,7 +2027,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,
@@ -2090,6 +2103,7 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
                } else {
                        /* something's definitely wrong */
                        result = centry->status;
+                       centry_free(centry);
                        goto error;
                }
 
@@ -2124,6 +2138,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;
@@ -2210,7 +2237,7 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
                return result;
        }
 
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
 
        for (i=0; i<num_rids; i++) {
                struct dom_sid sid;
@@ -2296,6 +2323,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,
@@ -2337,7 +2398,7 @@ static NTSTATUS query_user(struct winbindd_domain *domain,
                }
        }
        /* and save it */
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -2409,7 +2470,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;
@@ -2455,7 +2516,7 @@ static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
                goto skip_save;
 
        /* and save it */
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -2559,8 +2620,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;
@@ -2608,7 +2669,7 @@ static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
                }
        }
        /* and save it */
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -2665,7 +2726,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);
@@ -2694,9 +2755,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;
@@ -2744,7 +2805,7 @@ static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
                }
        }
        /* and save it */
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -2765,9 +2826,9 @@ 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);
+       refresh_sequence_number(domain);
 
        *seq = domain->sequence_number;
 
@@ -2945,7 +3006,7 @@ do_query:
                }
        }
        /* and save it */
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -3017,7 +3078,7 @@ do_query:
                }
        }
        /* and save it */
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -3047,7 +3108,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()) {
@@ -3132,6 +3193,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);
@@ -3140,13 +3203,18 @@ bool init_wcache(void)
        if (wcache->tdb != NULL)
                return true;
 
+       db_path = wcache_path();
+       if (db_path == NULL) {
+               return false;
+       }
+
        /* when working offline we must not clear the cache on restart */
-       wcache->tdb = tdb_open_log(state_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;
@@ -3164,7 +3232,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"));
@@ -3178,6 +3246,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 ));
@@ -3185,12 +3255,19 @@ bool initialize_winbindd_cache(void)
                tdb_close(wcache->tdb);
                wcache->tdb = NULL;
 
-               if (unlink(state_path("winbindd_cache.tdb")) == -1) {
+               db_path = wcache_path();
+               if (db_path == NULL) {
+                       return false;
+               }
+
+               if (unlink(db_path) == -1) {
                        DEBUG(0,("initialize_winbindd_cache: unlink %s failed %s ",
-                               state_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"));
@@ -3200,7 +3277,7 @@ bool initialize_winbindd_cache(void)
                /* Write the version. */
                if (!tdb_store_uint32(wcache->tdb, WINBINDD_CACHE_VERSION_KEYSTR, WINBINDD_CACHE_VERSION)) {
                        DEBUG(0,("initialize_winbindd_cache: version number store failed %s\n",
-                               tdb_errorstr_compat(wcache->tdb) ));
+                               tdb_errorstr(wcache->tdb) ));
                        return false;
                }
        }
@@ -3266,7 +3343,7 @@ void cache_name2sid(struct winbindd_domain *domain,
                    const char *domain_name, const char *name,
                    enum lsa_SidType type, const struct dom_sid *sid)
 {
-       refresh_sequence_number(domain, false);
+       refresh_sequence_number(domain);
        wcache_save_name_to_sid(domain, NT_STATUS_OK, domain_name, name,
                                sid, type);
 }
@@ -3303,6 +3380,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) {
@@ -3313,13 +3392,18 @@ void wcache_flush_cache(void)
                return;
        }
 
+       db_path = wcache_path();
+       if (db_path == NULL) {
+               return;
+       }
+
        /* when working offline we must not clear the cache on restart */
-       wcache->tdb = tdb_open_log(state_path("winbindd_cache.tdb"),
-                               WINBINDD_CACHE_TDB_DEFAULT_HASH_SIZE, 
+       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;
@@ -3395,7 +3479,7 @@ NTSTATUS wcache_remove_oldest_cached_creds(struct winbindd_domain *domain, const
        struct winbind_cache *cache = get_cache(domain);
        NTSTATUS status;
        int ret;
-       struct cred_list *cred, *oldest = NULL;
+       struct cred_list *cred, *next, *oldest = NULL;
 
        if (!cache->tdb) {
                return NT_STATUS_INTERNAL_DB_ERROR;
@@ -3429,7 +3513,7 @@ NTSTATUS wcache_remove_oldest_cached_creds(struct winbindd_domain *domain, const
                TDB_DATA data;
                time_t t;
 
-               data = tdb_fetch_compat(cache->tdb, string_tdb_data(cred->name));
+               data = tdb_fetch(cache->tdb, string_tdb_data(cred->name));
                if (!data.dptr) {
                        DEBUG(10,("wcache_remove_oldest_cached_creds: entry for [%s] not found\n", 
                                cred->name));
@@ -3464,7 +3548,11 @@ NTSTATUS wcache_remove_oldest_cached_creds(struct winbindd_domain *domain, const
                status = NT_STATUS_UNSUCCESSFUL;
        }
 done:
-       SAFE_FREE(wcache_cred_list);
+       for (cred = wcache_cred_list; cred; cred = next) {
+               next = cred->next;
+               DLIST_REMOVE(wcache_cred_list, cred);
+               SAFE_FREE(cred);
+       }
        SAFE_FREE(oldest);
 
        return status;
@@ -3547,7 +3635,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;
@@ -3732,13 +3820,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;
@@ -3763,7 +3851,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;
@@ -3790,7 +3878,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;
@@ -3816,7 +3904,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;
@@ -3841,7 +3929,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;
@@ -4064,7 +4152,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) {
@@ -4105,9 +4194,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. */
@@ -4192,7 +4281,7 @@ static bool wbcache_upgrade_v1_to_v2(TDB_CONTEXT *tdb)
 int winbindd_validate_cache(void)
 {
        int ret = -1;
-       const char *tdb_path = state_path("winbindd_cache.tdb");
+       char *tdb_path = NULL;
        TDB_CONTEXT *tdb = NULL;
        uint32_t vers_id;
        bool ok;
@@ -4200,13 +4289,18 @@ int winbindd_validate_cache(void)
        DEBUG(10, ("winbindd_validate_cache: replacing panic function\n"));
        smb_panic_fn = validate_panic;
 
-       tdb = tdb_open_log(tdb_path, 
+       tdb_path = wcache_path();
+       if (tdb_path == NULL) {
+               goto done;
+       }
+
+       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: "
@@ -4248,6 +4342,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;
@@ -4260,11 +4355,15 @@ done:
 int winbindd_validate_cache_nobackup(void)
 {
        int ret = -1;
-       const char *tdb_path = state_path("winbindd_cache.tdb");
+       char *tdb_path;
 
        DEBUG(10, ("winbindd_validate_cache: replacing panic function\n"));
        smb_panic_fn = validate_panic;
 
+       tdb_path = wcache_path();
+       if (tdb_path == NULL) {
+               goto err_panic_restore;
+       }
 
        if (wcache == NULL || wcache->tdb == NULL) {
                ret = tdb_validate_open(tdb_path, cache_traverse_validate_fn);
@@ -4277,6 +4376,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;
@@ -4465,7 +4566,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;
@@ -4575,7 +4676,7 @@ bool wcache_tdc_fetch_list( struct winbindd_tdc_domain **domains, size_t *num_do
        if ( !key.dptr )
                return false;
 
-       data = tdb_fetch_compat( wcache->tdb, key );
+       data = tdb_fetch( wcache->tdb, key );
 
        SAFE_FREE( key.dptr );
 
@@ -4771,7 +4872,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;
@@ -4930,7 +5031,7 @@ bool wcache_fetch_ndr(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
        if (!wcache_ndr_key(talloc_tos(), domain->name, opnum, req, &key)) {
                return false;
        }
-       data = tdb_fetch_compat(wcache->tdb, key);
+       data = tdb_fetch(wcache->tdb, key);
        TALLOC_FREE(key.dptr);
 
        if (data.dptr == NULL) {