r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation
[samba.git] / source3 / nsswitch / winbindd_cache.c
index a38a0fb16d4a40742c48bac6c9f2d45996967eb6..ba274ec8e7060f1bec04086fbc38881b333e4dec 100644 (file)
@@ -4,6 +4,8 @@
    Winbind cache backend functions
 
    Copyright (C) Andrew Tridgell 2001
+   Copyright (C) Gerald Carter   2003
+   
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -20,6 +22,7 @@
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
+#include "includes.h"
 #include "winbindd.h"
 
 #undef DBGC_CLASS
@@ -103,11 +106,29 @@ static struct winbind_cache *get_cache(struct winbindd_domain *domain)
 #ifdef HAVE_ADS
                case SEC_ADS: {
                        extern struct winbindd_methods ads_methods;
-                       domain->backend = &ads_methods;
-                       break;
-               }
+                       /* always obey the lp_security parameter for our domain */
+                       if (domain->primary) {
+                               domain->backend = &ads_methods;
+                               break;
+                       }
+
+                       /* only use ADS for native modes at the momment.
+                          The problem is the correct detection of mixed 
+                          mode domains from NT4 BDC's    --jerry */
+                       
+                       if ( domain->native_mode ) {
+                               DEBUG(5,("get_cache: Setting ADS methods for domain %s\n",
+                                       domain->name));
+                               domain->backend = &ads_methods;
+                               break;
+                       }
+
+                       /* fall through */
+               }       
 #endif
                default:
+                       DEBUG(5,("get_cache: Setting MS-RPC methods for domain %s\n",
+                               domain->name));
                        domain->backend = &msrpc_methods;
                }
        }
@@ -115,7 +136,7 @@ static struct winbind_cache *get_cache(struct winbindd_domain *domain)
        if (ret)
                return ret;
        
-       ret = smb_xmalloc(sizeof(*ret));
+       ret = SMB_XMALLOC_P(struct winbind_cache);
        ZERO_STRUCTP(ret);
 
        wcache = ret;
@@ -188,7 +209,7 @@ static char *centry_string(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
                smb_panic("centry_string");
        }
 
-       ret = talloc(mem_ctx, len+1);
+       ret = TALLOC(mem_ctx, len+1);
        if (!ret) {
                smb_panic("centry_string out of memory\n");
        }
@@ -206,7 +227,7 @@ static DOM_SID *centry_sid(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
        DOM_SID *sid;
        char *sid_string;
 
-       sid = talloc(mem_ctx, sizeof(*sid));
+       sid = TALLOC_P(mem_ctx, DOM_SID);
        if (!sid)
                return NULL;
        
@@ -244,9 +265,9 @@ static NTSTATUS fetch_cache_seqnum( struct winbindd_domain *domain, time_t now )
                return NT_STATUS_UNSUCCESSFUL;
        }
                
-       snprintf( key, sizeof(key), "SEQNUM/%s", domain->name );
+       fstr_sprintf( key, "SEQNUM/%s", domain->name );
        
-       data = tdb_fetch_by_string( wcache->tdb, key );
+       data = tdb_fetch_bystring( wcache->tdb, key );
        if ( !data.dptr || data.dsize!=8 ) {
                DEBUG(10,("fetch_cache_seqnum: invalid data size key [%s]\n", key ));
                return NT_STATUS_UNSUCCESSFUL;
@@ -255,6 +276,8 @@ static NTSTATUS fetch_cache_seqnum( struct winbindd_domain *domain, time_t now )
        domain->sequence_number = IVAL(data.dptr, 0);
        domain->last_seq_check  = IVAL(data.dptr, 4);
        
+       SAFE_FREE(data.dptr);
+
        /* have we expired? */
        
        time_diff = now - domain->last_seq_check;
@@ -283,7 +306,7 @@ static NTSTATUS store_cache_seqnum( struct winbindd_domain *domain )
                return NT_STATUS_UNSUCCESSFUL;
        }
                
-       snprintf( key_str, sizeof(key_str), "SEQNUM/%s", domain->name );
+       fstr_sprintf( key_str, "SEQNUM/%s", domain->name );
        key.dptr = key_str;
        key.dsize = strlen(key_str)+1;
        
@@ -316,17 +339,21 @@ static void refresh_sequence_number(struct winbindd_domain *domain, BOOL force)
        time_t t = time(NULL);
        unsigned cache_time = lp_winbind_cache_time();
 
+       get_cache( domain );
+
+#if 0  /* JERRY -- disable as the default cache time is now 5 minutes */
        /* trying to reconnect is expensive, don't do it too often */
        if (domain->sequence_number == DOM_SEQUENCE_NONE) {
                cache_time *= 8;
        }
+#endif
 
        time_diff = t - domain->last_seq_check;
 
        /* see if we have to refetch the domain sequence number */
        if (!force && (time_diff < cache_time)) {
                DEBUG(10, ("refresh_sequence_number: %s time ok\n", domain->name));
-               return;
+               goto done;
        }
        
        /* try to get the sequence number from the tdb cache first */
@@ -336,12 +363,19 @@ static void refresh_sequence_number(struct winbindd_domain *domain, BOOL force)
        if ( NT_STATUS_IS_OK(status) )
                goto done;      
 
+       /* important! make sure that we know if this is a native 
+          mode domain or not */
+
+       if ( !domain->initialized )
+               set_dc_type_and_flags( domain );
+
        status = domain->backend->sequence_number(domain, &domain->sequence_number);
 
        if (!NT_STATUS_IS_OK(status)) {
                domain->sequence_number = DOM_SEQUENCE_NONE;
        }
        
+       domain->last_status = status;
        domain->last_seq_check = time(NULL);
        
        /* save the new sequence number ni the cache */
@@ -416,8 +450,8 @@ static struct cache_entry *wcache_fetch(struct winbind_cache *cache,
                return NULL;
        }
 
-       centry = smb_xmalloc(sizeof(*centry));
-       centry->data = data.dptr;
+       centry = SMB_XMALLOC_P(struct cache_entry);
+       centry->data = (unsigned char *)data.dptr;
        centry->len = data.dsize;
        centry->ofs = 0;
 
@@ -467,7 +501,7 @@ static void centry_expand(struct cache_entry *centry, uint32 len)
        if (centry->len - centry->ofs >= len)
                return;
        centry->len *= 2;
-       p = realloc(centry->data, centry->len);
+       p = SMB_REALLOC(centry->data, centry->len);
        if (!p) {
                DEBUG(0,("out of memory: needed %d bytes in centry_expand\n", centry->len));
                smb_panic("out of memory in centry_expand");
@@ -534,10 +568,10 @@ struct cache_entry *centry_start(struct winbindd_domain *domain, NTSTATUS status
        if (!wcache->tdb)
                return NULL;
 
-       centry = smb_xmalloc(sizeof(*centry));
+       centry = SMB_XMALLOC_P(struct cache_entry);
 
        centry->len = 8192; /* reasonable default */
-       centry->data = smb_xmalloc(centry->len);
+       centry->data = SMB_XMALLOC_ARRAY(char, centry->len);
        centry->ofs = 0;
        centry->sequence_number = domain->sequence_number;
        centry_put_uint32(centry, NT_STATUS_V(status));
@@ -561,7 +595,7 @@ static void centry_end(struct cache_entry *centry, const char *format, ...)
 
        key.dptr = kstr;
        key.dsize = strlen(kstr);
-       data.dptr = centry->data;
+       data.dptr = (char *)centry->data;
        data.dsize = centry->ofs;
 
        tdb_store(wcache->tdb, key, data, TDB_REPLACE);
@@ -569,27 +603,28 @@ static void centry_end(struct cache_entry *centry, const char *format, ...)
 }
 
 static void wcache_save_name_to_sid(struct winbindd_domain *domain, 
-                                   NTSTATUS status, 
-                                   const char *name, DOM_SID *sid, 
+                                   NTSTATUS status, const char *domain_name,
+                                   const char *name, const DOM_SID *sid, 
                                    enum SID_NAME_USE type)
 {
        struct cache_entry *centry;
        fstring uname;
-       fstring sid_string;
 
        centry = centry_start(domain, status);
        if (!centry)
                return;
+       centry_put_uint32(centry, type);
        centry_put_sid(centry, sid);
        fstrcpy(uname, name);
-       strupper(uname);
-       centry_end(centry, "NS/%s", sid_to_string(sid_string, sid));
-       DEBUG(10,("wcache_save_name_to_sid: %s -> %s\n", uname, sid_string));
+       strupper_m(uname);
+       centry_end(centry, "NS/%s/%s", domain_name, uname);
+       DEBUG(10,("wcache_save_name_to_sid: %s -> %s\n", uname,
+                 sid_string_static(sid)));
        centry_free(centry);
 }
 
 static void wcache_save_sid_to_name(struct winbindd_domain *domain, NTSTATUS status, 
-                                   DOM_SID *sid, const char *name, enum SID_NAME_USE type)
+                                   const DOM_SID *sid, const char *domain_name, const char *name, enum SID_NAME_USE type)
 {
        struct cache_entry *centry;
        fstring sid_string;
@@ -599,6 +634,7 @@ static void wcache_save_sid_to_name(struct winbindd_domain *domain, NTSTATUS sta
                return;
        if (NT_STATUS_IS_OK(status)) {
                centry_put_uint32(centry, type);
+               centry_put_string(centry, domain_name);
                centry_put_string(centry, name);
        }
        centry_end(centry, "SN/%s", sid_to_string(sid_string, sid));
@@ -634,7 +670,7 @@ static NTSTATUS query_user_list(struct winbindd_domain *domain,
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry = NULL;
        NTSTATUS status;
-       unsigned int i;
+       unsigned int i, retry;
 
        if (!cache->tdb)
                goto do_query;
@@ -648,7 +684,7 @@ static NTSTATUS query_user_list(struct winbindd_domain *domain,
        if (*num_entries == 0)
                goto do_cached;
 
-       (*info) = talloc(mem_ctx, sizeof(**info) * (*num_entries));
+       (*info) = TALLOC_ARRAY(mem_ctx, WINBIND_USERINFO, *num_entries);
        if (! (*info))
                smb_panic("query_user_list out of memory");
        for (i=0; i<(*num_entries); i++) {
@@ -671,14 +707,35 @@ do_query:
        *num_entries = 0;
        *info = NULL;
 
-       if (wcache_server_down(domain)) {
-               return NT_STATUS_SERVER_DISABLED;
-       }
+       /* Return status value returned by seq number check */
 
-       DEBUG(10,("query_user_list: [Cached] - doing backend query for list for domain %s\n",
-               domain->name ));
+       if (!NT_STATUS_IS_OK(domain->last_status))
+               return domain->last_status;
+
+       /* Put the query_user_list() in a retry loop.  There appears to be
+        * some bug either with Windows 2000 or Samba's handling of large
+        * rpc replies.  This manifests itself as sudden disconnection
+        * at a random point in the enumeration of a large (60k) user list.
+        * The retry loop simply tries the operation again. )-:  It's not
+        * pretty but an acceptable workaround until we work out what the
+        * real problem is. */
+
+       retry = 0;
+       do {
+
+               DEBUG(10,("query_user_list: [Cached] - doing backend query for list for domain %s\n",
+                       domain->name ));
+
+               status = domain->backend->query_user_list(domain, mem_ctx, num_entries, info);
+               if (!NT_STATUS_IS_OK(status))
+                       DEBUG(3, ("query_user_list: returned 0x%08x, retrying\n", NT_STATUS_V(status)));
+                       if (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_UNSUCCESSFUL)) {
+                               DEBUG(3, ("query_user_list: flushing connection cache\n"));
+                               winbindd_cm_flush();
+                       }
 
-       status = domain->backend->query_user_list(domain, mem_ctx, num_entries, info);
+       } while (NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_UNSUCCESSFUL) && 
+                (retry++ < 5));
 
        /* and save it */
        refresh_sequence_number(domain, False);
@@ -694,11 +751,13 @@ do_query:
                if (domain->backend->consistent) {
                        /* when the backend is consistent we can pre-prime some mappings */
                        wcache_save_name_to_sid(domain, NT_STATUS_OK, 
+                                               domain->name,
                                                (*info)[i].acct_name, 
                                                (*info)[i].user_sid,
                                                SID_NAME_USER);
                        wcache_save_sid_to_name(domain, NT_STATUS_OK, 
                                                (*info)[i].user_sid,
+                                               domain->name,
                                                (*info)[i].acct_name, 
                                                SID_NAME_USER);
                        wcache_save_user(domain, NT_STATUS_OK, &(*info)[i]);
@@ -734,7 +793,7 @@ static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
        if (*num_entries == 0)
                goto do_cached;
 
-       (*info) = talloc(mem_ctx, sizeof(**info) * (*num_entries));
+       (*info) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_entries);
        if (! (*info))
                smb_panic("enum_dom_groups out of memory");
        for (i=0; i<(*num_entries); i++) {
@@ -756,9 +815,10 @@ do_query:
        *num_entries = 0;
        *info = NULL;
 
-       if (wcache_server_down(domain)) {
-               return NT_STATUS_SERVER_DISABLED;
-       }
+       /* Return status value returned by seq number check */
+
+       if (!NT_STATUS_IS_OK(domain->last_status))
+               return domain->last_status;
 
        DEBUG(10,("enum_dom_groups: [Cached] - doing backend query for list for domain %s\n",
                domain->name ));
@@ -806,7 +866,7 @@ static NTSTATUS enum_local_groups(struct winbindd_domain *domain,
        if (*num_entries == 0)
                goto do_cached;
 
-       (*info) = talloc(mem_ctx, sizeof(**info) * (*num_entries));
+       (*info) = TALLOC_ARRAY(mem_ctx, struct acct_info, *num_entries);
        if (! (*info))
                smb_panic("enum_dom_groups out of memory");
        for (i=0; i<(*num_entries); i++) {
@@ -838,9 +898,10 @@ do_query:
        *num_entries = 0;
        *info = NULL;
 
-       if (wcache_server_down(domain)) {
-               return NT_STATUS_SERVER_DISABLED;
-       }
+       /* Return status value returned by seq number check */
+
+       if (!NT_STATUS_IS_OK(domain->last_status))
+               return domain->last_status;
 
        DEBUG(10,("enum_local_groups: [Cached] - doing backend query for list for domain %s\n",
                domain->name ));
@@ -868,6 +929,7 @@ skip_save:
 /* convert a single name to a sid in a domain */
 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
                            TALLOC_CTX *mem_ctx,
+                           const char *domain_name,
                            const char *name,
                            DOM_SID *sid,
                            enum SID_NAME_USE *type)
@@ -878,14 +940,15 @@ static NTSTATUS name_to_sid(struct winbindd_domain *domain,
        fstring uname;
        DOM_SID *sid2;
 
-       if (!cache->tdb) goto do_query;
+       if (!cache->tdb)
+               goto do_query;
 
        fstrcpy(uname, name);
-       strupper(uname);
-       centry = wcache_fetch(cache, domain, "NS/%s/%s", domain->name, uname);
+       strupper_m(uname);
+       centry = wcache_fetch(cache, domain, "NS/%s/%s", domain_name, uname);
        if (!centry)
                goto do_query;
-       *type = centry_uint32(centry);
+       *type = (enum SID_NAME_USE)centry_uint32(centry);
        sid2 = centry_sid(centry, mem_ctx);
        if (!sid2) {
                ZERO_STRUCTP(sid);
@@ -904,17 +967,24 @@ static NTSTATUS name_to_sid(struct winbindd_domain *domain,
 do_query:
        ZERO_STRUCTP(sid);
 
-       if (wcache_server_down(domain)) {
-               return NT_STATUS_SERVER_DISABLED;
-       }
+       /* If the seq number check indicated that there is a problem
+        * with this DC, then return that status... except for
+        * access_denied.  This is special because the dc may be in
+        * "restrict anonymous = 1" mode, in which case it will deny
+        * most unauthenticated operations, but *will* allow the LSA
+        * name-to-sid that we try as a fallback. */
+
+       if (!(NT_STATUS_IS_OK(domain->last_status)
+             || NT_STATUS_EQUAL(domain->last_status, NT_STATUS_ACCESS_DENIED)))
+               return domain->last_status;
 
        DEBUG(10,("name_to_sid: [Cached] - doing backend query for name for domain %s\n",
                domain->name ));
 
-       status = domain->backend->name_to_sid(domain, mem_ctx, name, sid, type);
+       status = domain->backend->name_to_sid(domain, mem_ctx, domain_name, name, sid, type);
 
        /* and save it */
-       wcache_save_name_to_sid(domain, status, name, sid, *type);
+       wcache_save_name_to_sid(domain, status, domain_name, name, sid, *type);
 
        /* We can't save the sid to name mapping as we don't know the
           correct case of the name without looking it up */
@@ -926,7 +996,8 @@ do_query:
    given */
 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
                            TALLOC_CTX *mem_ctx,
-                           DOM_SID *sid,
+                           const DOM_SID *sid,
+                           char **domain_name,
                            char **name,
                            enum SID_NAME_USE *type)
 {
@@ -942,7 +1013,8 @@ static NTSTATUS sid_to_name(struct winbindd_domain *domain,
        if (!centry)
                goto do_query;
        if (NT_STATUS_IS_OK(centry->status)) {
-               *type = centry_uint32(centry);
+               *type = (enum SID_NAME_USE)centry_uint32(centry);
+               *domain_name = centry_string(centry, mem_ctx);
                *name = centry_string(centry, mem_ctx);
        }
        status = centry->status;
@@ -955,20 +1027,30 @@ static NTSTATUS sid_to_name(struct winbindd_domain *domain,
 
 do_query:
        *name = NULL;
+       *domain_name = NULL;
 
-       if (wcache_server_down(domain)) {
-               return NT_STATUS_SERVER_DISABLED;
-       }
+       /* If the seq number check indicated that there is a problem
+        * with this DC, then return that status... except for
+        * access_denied.  This is special because the dc may be in
+        * "restrict anonymous = 1" mode, in which case it will deny
+        * most unauthenticated operations, but *will* allow the LSA
+        * sid-to-name that we try as a fallback. */
+
+       if (!(NT_STATUS_IS_OK(domain->last_status)
+             || NT_STATUS_EQUAL(domain->last_status, NT_STATUS_ACCESS_DENIED)))
+               return domain->last_status;
 
        DEBUG(10,("sid_to_name: [Cached] - doing backend query for name for domain %s\n",
                domain->name ));
 
-       status = domain->backend->sid_to_name(domain, mem_ctx, sid, name, type);
+       status = domain->backend->sid_to_name(domain, mem_ctx, sid, domain_name, name, type);
 
        /* and save it */
        refresh_sequence_number(domain, False);
-       wcache_save_sid_to_name(domain, status, sid, *name, *type);
-       wcache_save_name_to_sid(domain, status, *name, sid, *type);
+       wcache_save_sid_to_name(domain, status, sid, *domain_name, *name, *type);
+
+       /* We can't save the name to sid mapping here, as with sid history a
+        * later name2sid would give the wrong sid. */
 
        return status;
 }
@@ -977,18 +1059,30 @@ do_query:
 /* Lookup user information from a rid */
 static NTSTATUS query_user(struct winbindd_domain *domain, 
                           TALLOC_CTX *mem_ctx, 
-                          DOM_SID *user_sid, 
+                          const DOM_SID *user_sid, 
                           WINBIND_USERINFO *info)
 {
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry = NULL;
        NTSTATUS status;
-       fstring sid_string;
 
        if (!cache->tdb)
                goto do_query;
 
-       centry = wcache_fetch(cache, domain, "U/%s", sid_to_string(sid_string, user_sid));
+       centry = wcache_fetch(cache, domain, "U/%s", sid_string_static(user_sid));
+       
+       /* If we have an access denied cache entry and a cached info3 in the
+           samlogon cache then do a query.  This will force the rpc back end
+           to return the info3 data. */
+
+       if (NT_STATUS_V(domain->last_status) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED) &&
+           netsamlogon_cache_have(user_sid)) {
+               DEBUG(10, ("query_user: cached access denied and have cached info3\n"));
+               domain->last_status = NT_STATUS_OK;
+               centry_free(centry);
+               goto do_query;
+       }
+       
        if (!centry)
                goto do_query;
 
@@ -1007,9 +1101,10 @@ static NTSTATUS query_user(struct winbindd_domain *domain,
 do_query:
        ZERO_STRUCTP(info);
 
-       if (wcache_server_down(domain)) {
-               return NT_STATUS_SERVER_DISABLED;
-       }
+       /* Return status value returned by seq number check */
+
+       if (!NT_STATUS_IS_OK(domain->last_status))
+               return domain->last_status;
        
        DEBUG(10,("sid_to_name: [Cached] - doing backend query for info for domain %s\n",
                domain->name ));
@@ -1027,7 +1122,7 @@ do_query:
 /* Lookup groups a user is a member of. */
 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
                                  TALLOC_CTX *mem_ctx,
-                                 DOM_SID *user_sid, 
+                                 const DOM_SID *user_sid, 
                                  uint32 *num_groups, DOM_SID ***user_gids)
 {
        struct winbind_cache *cache = get_cache(domain);
@@ -1040,6 +1135,19 @@ static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
                goto do_query;
 
        centry = wcache_fetch(cache, domain, "UG/%s", sid_to_string(sid_string, user_sid));
+       
+       /* If we have an access denied cache entry and a cached info3 in the
+           samlogon cache then do a query.  This will force the rpc back end
+           to return the info3 data. */
+
+       if (NT_STATUS_V(domain->last_status) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED) &&
+           netsamlogon_cache_have(user_sid)) {
+               DEBUG(10, ("query_user: cached access denied and have cached info3\n"));
+               domain->last_status = NT_STATUS_OK;
+               centry_free(centry);
+               goto do_query;
+       }
+       
        if (!centry)
                goto do_query;
 
@@ -1048,7 +1156,7 @@ static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
        if (*num_groups == 0)
                goto do_cached;
 
-       (*user_gids) = talloc(mem_ctx, sizeof(**user_gids) * (*num_groups));
+       (*user_gids) = TALLOC_ARRAY(mem_ctx, DOM_SID *, *num_groups);
        if (! (*user_gids))
                smb_panic("lookup_usergroups out of memory");
        for (i=0; i<(*num_groups); i++) {
@@ -1068,9 +1176,10 @@ do_query:
        (*num_groups) = 0;
        (*user_gids) = NULL;
 
-       if (wcache_server_down(domain)) {
-               return NT_STATUS_SERVER_DISABLED;
-       }
+       /* Return status value returned by seq number check */
+
+       if (!NT_STATUS_IS_OK(domain->last_status))
+               return domain->last_status;
 
        DEBUG(10,("lookup_usergroups: [Cached] - doing backend query for info for domain %s\n",
                domain->name ));
@@ -1096,7 +1205,7 @@ skip_save:
 
 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
                                TALLOC_CTX *mem_ctx,
-                               DOM_SID *group_sid, uint32 *num_names, 
+                               const DOM_SID *group_sid, uint32 *num_names, 
                                DOM_SID ***sid_mem, char ***names, 
                                uint32 **name_types)
 {
@@ -1118,9 +1227,9 @@ static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
        if (*num_names == 0)
                goto do_cached;
 
-       (*sid_mem) = talloc(mem_ctx, sizeof(**sid_mem) * (*num_names));
-       (*names) = talloc(mem_ctx, sizeof(**names) * (*num_names));
-       (*name_types) = talloc(mem_ctx, sizeof(**name_types) * (*num_names));
+       (*sid_mem) = TALLOC_ARRAY(mem_ctx, DOM_SID *, *num_names);
+       (*names) = TALLOC_ARRAY(mem_ctx, char *, *num_names);
+       (*name_types) = TALLOC_ARRAY(mem_ctx, uint32, *num_names);
 
        if (! (*sid_mem) || ! (*names) || ! (*name_types)) {
                smb_panic("lookup_groupmem out of memory");
@@ -1147,10 +1256,10 @@ do_query:
        (*names) = NULL;
        (*name_types) = NULL;
        
+       /* Return status value returned by seq number check */
 
-       if (wcache_server_down(domain)) {
-               return NT_STATUS_SERVER_DISABLED;
-       }
+       if (!NT_STATUS_IS_OK(domain->last_status))
+               return domain->last_status;
 
        DEBUG(10,("lookup_groupmem: [Cached] - doing backend query for info for domain %s\n",
                domain->name ));
@@ -1228,6 +1337,46 @@ static NTSTATUS alternate_name(struct winbindd_domain *domain)
        return domain->backend->alternate_name(domain);
 }
 
+/* Invalidate cached user and group lists coherently */
+
+static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, 
+                      void *state)
+{
+       if (strncmp(kbuf.dptr, "UL/", 3) == 0 ||
+           strncmp(kbuf.dptr, "GL/", 3) == 0)
+               tdb_delete(the_tdb, kbuf);
+
+       return 0;
+}
+
+/* Invalidate the getpwnam and getgroups entries for a winbindd domain */
+
+void wcache_invalidate_samlogon(struct winbindd_domain *domain, 
+                               NET_USER_INFO_3 *info3)
+{
+       struct winbind_cache *cache;
+       
+       if (!domain)
+               return;
+
+       cache = get_cache(domain);
+       netsamlogon_clear_cached_user(cache->tdb, info3);
+}
+
+void wcache_invalidate_cache(void)
+{
+       struct winbindd_domain *domain;
+
+       for (domain = domain_list(); domain; domain = domain->next) {
+               struct winbind_cache *cache = get_cache(domain);
+
+               DEBUG(10, ("wcache_invalidate_cache: invalidating cache "
+                          "entries for %s\n", domain->name));
+               if (cache)
+                       tdb_traverse(cache->tdb, traverse_fn, NULL);
+       }
+}
+
 /* the ADS backend methods are exposed via this structure */
 struct winbindd_methods cache_methods = {
        True,