Add some braces to if statement.
[ira/wip.git] / source3 / winbindd / winbindd_cache.c
index e5090dfacf5841f8cfd42b872abe6570a177a309..9602a128a6a1fb6200319583dd42a37d9f5a7c8d 100644 (file)
@@ -33,7 +33,7 @@
 #define WINBINDD_CACHE_VERSION_KEYSTR "WINBINDD_CACHE_VERSION"
 
 extern struct winbindd_methods reconnect_methods;
-extern BOOL opt_nocache;
+extern bool opt_nocache;
 #ifdef HAVE_ADS
 extern struct winbindd_methods ads_methods;
 #endif
@@ -57,7 +57,7 @@ static const char *non_centry_keys[] = {
  Is this key a non-centry type ?
 ************************************************************************/
 
-static BOOL is_non_centry_key(TDB_DATA kbuf)
+static bool is_non_centry_key(TDB_DATA kbuf)
 {
        int i;
 
@@ -80,7 +80,7 @@ static BOOL is_non_centry_key(TDB_DATA kbuf)
    and sets this to true if the first query fails and there's an entry in
    the cache tdb telling us to stay offline. */
 
-static BOOL global_winbindd_offline_state;
+static bool global_winbindd_offline_state;
 
 struct winbind_cache {
        TDB_CONTEXT *tdb;
@@ -204,7 +204,7 @@ static void centry_free(struct cache_entry *centry)
        free(centry);
 }
 
-static BOOL centry_check_bytes(struct cache_entry *centry, size_t nbytes)
+static bool centry_check_bytes(struct cache_entry *centry, size_t nbytes)
 {
        if (centry->len - centry->ofs < nbytes) {
                DEBUG(0,("centry corruption? needed %u bytes, have %d\n", 
@@ -343,7 +343,7 @@ static char *centry_hash16(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
 /* pull a sid from a cache entry, using the supplied
    talloc context 
 */
-static BOOL centry_sid(struct cache_entry *centry, TALLOC_CTX *mem_ctx, DOM_SID *sid)
+static bool centry_sid(struct cache_entry *centry, TALLOC_CTX *mem_ctx, DOM_SID *sid)
 {
        char *sid_string;
        sid_string = centry_string(centry, mem_ctx);
@@ -367,9 +367,9 @@ static NTSTATUS centry_ntstatus(struct cache_entry *centry)
 
 
 /* the server is considered down if it can't give us a sequence number */
-static BOOL wcache_server_down(struct winbindd_domain *domain)
+static bool wcache_server_down(struct winbindd_domain *domain)
 {
-       BOOL ret;
+       bool ret;
 
        if (!wcache->tdb)
                return False;
@@ -458,7 +458,7 @@ static NTSTATUS store_cache_seqnum( struct winbindd_domain *domain )
   then always refresh it, no matter how recently we fetched it
 */
 
-static void refresh_sequence_number(struct winbindd_domain *domain, BOOL force)
+static void refresh_sequence_number(struct winbindd_domain *domain, bool force)
 {
        NTSTATUS status;
        unsigned time_diff;
@@ -532,7 +532,7 @@ done:
 /*
   decide if a cache entry has expired
 */
-static BOOL centry_expired(struct winbindd_domain *domain, const char *keystr, struct cache_entry *centry)
+static bool centry_expired(struct winbindd_domain *domain, const char *keystr, struct cache_entry *centry)
 {
        /* If we've been told to be offline - stay in that state... */
        if (lp_winbind_offline_logon() && global_winbindd_offline_state) {
@@ -756,7 +756,7 @@ static void centry_put_hash16(struct cache_entry *centry, const uint8 val[16])
 static void centry_put_sid(struct cache_entry *centry, const DOM_SID *sid) 
 {
        fstring sid_string;
-       centry_put_string(centry, sid_to_string(sid_string, sid));
+       centry_put_string(centry, sid_to_fstring(sid_string, sid));
 }
 
 
@@ -855,8 +855,8 @@ static void wcache_save_name_to_sid(struct winbindd_domain *domain,
        fstrcpy(uname, name);
        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_static(sid), nt_errstr(status)));
+       DEBUG(10,("wcache_save_name_to_sid: %s\\%s -> %s (%s)\n", domain_name,
+                 uname, sid_string_dbg(sid), nt_errstr(status)));
        centry_free(centry);
 }
 
@@ -876,7 +876,7 @@ static void wcache_save_sid_to_name(struct winbindd_domain *domain, NTSTATUS sta
                centry_put_string(centry, name);
        }
 
-       centry_end(centry, "SN/%s", sid_to_string(sid_string, sid));
+       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)));
        centry_free(centry);
@@ -902,7 +902,8 @@ static void wcache_save_user(struct winbindd_domain *domain, NTSTATUS status, WI
        centry_put_uint32(centry, info->primary_gid);
        centry_put_sid(centry, &info->user_sid);
        centry_put_sid(centry, &info->group_sid);
-       centry_end(centry, "U/%s", sid_to_string(sid_string, &info->user_sid));
+       centry_end(centry, "U/%s", sid_to_fstring(sid_string,
+                                                 &info->user_sid));
        DEBUG(10,("wcache_save_user: %s (acct_name %s)\n", sid_string, info->acct_name));
        centry_free(centry);
 }
@@ -951,7 +952,7 @@ NTSTATUS wcache_cached_creds_exist(struct winbindd_domain *domain, const DOM_SID
 {
        struct winbind_cache *cache = get_cache(domain);
        TDB_DATA data;
-       fstring key_str;
+       fstring key_str, tmp;
        uint32 rid;
 
        if (!cache->tdb) {
@@ -966,7 +967,7 @@ NTSTATUS wcache_cached_creds_exist(struct winbindd_domain *domain, const DOM_SID
                return NT_STATUS_INVALID_SID;
        }
 
-       fstr_sprintf(key_str, "CRED/%s", sid_string_static(sid));
+       fstr_sprintf(key_str, "CRED/%s", sid_to_fstring(tmp, sid));
 
        data = tdb_fetch(cache->tdb, string_tdb_data(key_str));
        if (!data.dptr) {
@@ -991,6 +992,7 @@ NTSTATUS wcache_get_creds(struct winbindd_domain *domain,
        NTSTATUS status;
        time_t t;
        uint32 rid;
+       fstring tmp;
 
        if (!cache->tdb) {
                return NT_STATUS_INTERNAL_DB_ERROR;
@@ -1007,10 +1009,11 @@ NTSTATUS wcache_get_creds(struct winbindd_domain *domain,
        /* Try and get a salted cred first. If we can't
           fall back to an unsalted cred. */
 
-       centry = wcache_fetch(cache, domain, "CRED/%s", sid_string_static(sid));
+       centry = wcache_fetch(cache, domain, "CRED/%s",
+                             sid_to_fstring(tmp, sid));
        if (!centry) {
                DEBUG(10,("wcache_get_creds: entry for [CRED/%s] not found\n", 
-                               sid_string_static(sid)));
+                         sid_string_dbg(sid)));
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
@@ -1023,7 +1026,9 @@ NTSTATUS wcache_get_creds(struct winbindd_domain *domain,
 
        *cached_nt_pass = (const uint8 *)centry_hash16(centry, mem_ctx);
        if (*cached_nt_pass == NULL) {
-               const char *sidstr = sid_string_static(sid);
+               fstring sidstr;
+
+               sid_to_fstring(sidstr, sid);
 
                /* Bad (old) cred cache. Delete and pretend we
                   don't have it. */
@@ -1049,7 +1054,7 @@ NTSTATUS wcache_get_creds(struct winbindd_domain *domain,
        status = centry->status;
 
        DEBUG(10,("wcache_get_creds: [Cached] - cached creds for user %s status: %s\n",
-               sid_string_static(sid), nt_errstr(status) ));
+                 sid_string_dbg(sid), nt_errstr(status) ));
 
        centry_free(centry);
        return status;
@@ -1091,7 +1096,7 @@ NTSTATUS wcache_save_creds(struct winbindd_domain *domain,
 
        centry_put_hash16(centry, salted_hash);
        centry_put_hash16(centry, cred_salt);
-       centry_end(centry, "CRED/%s", sid_to_string(sid_string, sid));
+       centry_end(centry, "CRED/%s", sid_to_fstring(sid_string, sid));
 
        DEBUG(10,("wcache_save_creds: %s\n", sid_string));
 
@@ -1466,7 +1471,8 @@ static NTSTATUS sid_to_name(struct winbindd_domain *domain,
        if (!cache->tdb)
                goto do_query;
 
-       centry = wcache_fetch(cache, domain, "SN/%s", sid_to_string(sid_string, sid));
+       centry = wcache_fetch(cache, domain, "SN/%s",
+                             sid_to_fstring(sid_string, sid));
        if (!centry)
                goto do_query;
 
@@ -1525,8 +1531,8 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
        struct winbind_cache *cache = get_cache(domain);
        size_t i;
        NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
-       BOOL have_mapped;
-       BOOL have_unmapped;
+       bool have_mapped;
+       bool have_unmapped;
 
        *domain_name = NULL;
        *names = NULL;
@@ -1553,6 +1559,7 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
        for (i=0; i<num_rids; i++) {
                DOM_SID sid;
                struct cache_entry *centry;
+               fstring tmp;
 
                if (!sid_compose(&sid, domain_sid, rids[i])) {
                        result = NT_STATUS_INTERNAL_ERROR;
@@ -1560,7 +1567,7 @@ static NTSTATUS rids_to_names(struct winbindd_domain *domain,
                }
 
                centry = wcache_fetch(cache, domain, "SN/%s",
-                                     sid_string_static(&sid));
+                                     sid_to_fstring(tmp, &sid));
                if (!centry) {
                        goto do_query;
                }
@@ -1676,11 +1683,13 @@ static NTSTATUS query_user(struct winbindd_domain *domain,
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry = NULL;
        NTSTATUS status;
+       fstring tmp;
 
        if (!cache->tdb)
                goto do_query;
 
-       centry = wcache_fetch(cache, domain, "U/%s", sid_string_static(user_sid));
+       centry = wcache_fetch(cache, domain, "U/%s",
+                             sid_to_fstring(tmp, 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
@@ -1752,7 +1761,8 @@ static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
        if (!cache->tdb)
                goto do_query;
 
-       centry = wcache_fetch(cache, domain, "UG/%s", sid_to_string(sid_string, user_sid));
+       centry = wcache_fetch(cache, domain, "UG/%s",
+                             sid_to_fstring(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
@@ -1819,7 +1829,7 @@ do_query:
                centry_put_sid(centry, &(*user_gids)[i]);
        }       
 
-       centry_end(centry, "UG/%s", sid_to_string(sid_string, user_sid));
+       centry_end(centry, "UG/%s", sid_to_fstring(sid_string, user_sid));
        centry_free(centry);
 
 skip_save:
@@ -1850,8 +1860,9 @@ static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
         * resulting might come from any of the SIDs. */
 
        for (i=0; i<num_sids; i++) {
+               fstring tmp;
                sidlist = talloc_asprintf(mem_ctx, "%s/%s", sidlist,
-                                         sid_string_static(&sids[i]));
+                                         sid_to_fstring(tmp, &sids[i]));
                if (sidlist == NULL)
                        return NT_STATUS_NO_MEMORY;
        }
@@ -1931,7 +1942,8 @@ static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
        if (!cache->tdb)
                goto do_query;
 
-       centry = wcache_fetch(cache, domain, "GM/%s", sid_to_string(sid_string, group_sid));
+       centry = wcache_fetch(cache, domain, "GM/%s",
+                             sid_to_fstring(sid_string, group_sid));
        if (!centry)
                goto do_query;
 
@@ -1991,7 +2003,7 @@ do_query:
                centry_put_string(centry, (*names)[i]);
                centry_put_uint32(centry, (*name_types)[i]);
        }       
-       centry_end(centry, "GM/%s", sid_to_string(sid_string, group_sid));
+       centry_end(centry, "GM/%s", sid_to_fstring(sid_string, group_sid));
        centry_free(centry);
 
 skip_save:
@@ -2249,7 +2261,7 @@ void wcache_invalidate_samlogon(struct winbindd_domain *domain,
        netsamlogon_clear_cached_user(cache->tdb, info3);
 }
 
-void wcache_invalidate_cache(void)
+int wcache_invalidate_cache(void)
 {
        struct winbindd_domain *domain;
 
@@ -2258,12 +2270,18 @@ void wcache_invalidate_cache(void)
 
                DEBUG(10, ("wcache_invalidate_cache: invalidating cache "
                           "entries for %s\n", domain->name));
-               if (cache)
-                       tdb_traverse(cache->tdb, traverse_fn, NULL);
+               if (cache) {
+                       if (cache->tdb) {
+                               tdb_traverse(cache->tdb, traverse_fn, NULL);
+                       } else {
+                               return -1;
+                       }
+               }
        }
+       return 0;
 }
 
-BOOL init_wcache(void)
+bool init_wcache(void)
 {
        if (wcache == NULL) {
                wcache = SMB_XMALLOC_P(struct winbind_cache);
@@ -2293,9 +2311,9 @@ BOOL init_wcache(void)
  only opener.
 ************************************************************************/
 
-BOOL initialize_winbindd_cache(void)
+bool initialize_winbindd_cache(void)
 {
-       BOOL cache_bad = True;
+       bool cache_bad = True;
        uint32 vers;
 
        if (!init_wcache()) {
@@ -2342,6 +2360,17 @@ BOOL initialize_winbindd_cache(void)
        return True;
 }
 
+void close_winbindd_cache()
+{
+       if (!wcache) {
+               return;
+       }
+       if (wcache->tdb) {
+               tdb_close(wcache->tdb);
+               wcache->tdb = NULL;
+       }
+}
+
 void cache_store_response(pid_t pid, struct winbindd_response *response)
 {
        fstring key_str;
@@ -2382,7 +2411,7 @@ void cache_store_response(pid_t pid, struct winbindd_response *response)
        return;
 }
 
-BOOL cache_retrieve_response(pid_t pid, struct winbindd_response * response)
+bool cache_retrieve_response(pid_t pid, struct winbindd_response * response)
 {
        TDB_DATA data;
        fstring key_str;
@@ -2451,7 +2480,7 @@ void cache_cleanup_response(pid_t pid)
 }
 
 
-BOOL lookup_cached_sid(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
+bool lookup_cached_sid(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
                       char **domain_name, char **name,
                       enum lsa_SidType *type)
 {
@@ -2459,6 +2488,7 @@ BOOL lookup_cached_sid(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
        struct winbind_cache *cache;
        struct cache_entry *centry = NULL;
        NTSTATUS status;
+       fstring tmp;
 
        domain = find_lookup_domain_from_sid(sid);
        if (domain == NULL) {
@@ -2471,7 +2501,8 @@ BOOL lookup_cached_sid(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
                return False;
        }
 
-       centry = wcache_fetch(cache, domain, "SN/%s", sid_string_static(sid));
+       centry = wcache_fetch(cache, domain, "SN/%s",
+                             sid_to_fstring(tmp, sid));
        if (centry == NULL) {
                return False;
        }
@@ -2487,7 +2518,7 @@ BOOL lookup_cached_sid(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
        return NT_STATUS_IS_OK(status);
 }
 
-BOOL lookup_cached_name(TALLOC_CTX *mem_ctx,
+bool lookup_cached_name(TALLOC_CTX *mem_ctx,
                        const char *domain_name,
                        const char *name,
                        DOM_SID *sid,
@@ -2498,7 +2529,7 @@ BOOL lookup_cached_name(TALLOC_CTX *mem_ctx,
        struct cache_entry *centry = NULL;
        NTSTATUS status;
        fstring uname;
-       BOOL original_online_state;     
+       bool original_online_state;     
 
        domain = find_lookup_domain_from_name(domain_name);
        if (domain == NULL) {
@@ -2677,11 +2708,11 @@ NTSTATUS wcache_remove_oldest_cached_creds(struct winbindd_domain *domain, const
        /* we possibly already have an entry */
        if (sid && NT_STATUS_IS_OK(wcache_cached_creds_exist(domain, sid))) {
        
-               fstring key_str;
+               fstring key_str, tmp;
 
                DEBUG(11,("we already have an entry, deleting that\n"));
 
-               fstr_sprintf(key_str, "CRED/%s", sid_string_static(sid));
+               fstr_sprintf(key_str, "CRED/%s", sid_to_fstring(tmp, sid));
 
                tdb_delete(cache->tdb, string_tdb_data(key_str));
 
@@ -2744,7 +2775,7 @@ done:
 }
 
 /* Change the global online/offline state. */
-BOOL set_global_winbindd_state_offline(void)
+bool set_global_winbindd_state_offline(void)
 {
        TDB_DATA data;
 
@@ -2805,7 +2836,7 @@ void set_global_winbindd_state_online(void)
        tdb_delete_bystring(wcache->tdb, "WINBINDD_OFFLINE");
 }
 
-BOOL get_global_winbindd_state_offline(void)
+bool get_global_winbindd_state_offline(void)
 {
        return global_winbindd_offline_state;
 }
@@ -3338,8 +3369,7 @@ static void validate_panic(const char *const why)
 
 /***********************************************************************
  Try and validate every entry in the winbindd cache. If we fail here,
- delete the cache tdb and return non-zero - the caller (main winbindd
- function) will restart us as we don't know if we crashed or not.
+ delete the cache tdb and return non-zero.
 ***********************************************************************/
 
 int winbindd_validate_cache(void)
@@ -3413,14 +3443,14 @@ int winbindd_validate_cache_nobackup(void)
 /*********************************************************************
  ********************************************************************/
 
-static BOOL add_wbdomain_to_tdc_array( struct winbindd_domain *new_dom,
+static bool add_wbdomain_to_tdc_array( struct winbindd_domain *new_dom,
                                       struct winbindd_tdc_domain **domains, 
                                       size_t *num_domains )
 {
        struct winbindd_tdc_domain *list = NULL;
        size_t idx;
        int i;
-       BOOL set_only = False;  
+       bool set_only = False;  
        
        /* don't allow duplicates */
 
@@ -3525,6 +3555,8 @@ static int pack_tdc_domains( struct winbindd_tdc_domain *domains,
        /* now pack each domain trust record */
        for ( i=0; i<num_domains; i++ ) {
 
+               fstring tmp;
+
                if ( buflen > 0 ) {
                        DEBUG(10,("pack_tdc_domains: Packing domain %s (%s)\n",
                                  domains[i].domain_name,
@@ -3534,7 +3566,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,
-                                sid_string_static(&domains[i].sid),
+                                sid_to_fstring(tmp, &domains[i].sid),
                                 domains[i].trust_flags,
                                 domains[i].trust_attribs,
                                 domains[i].trust_type );
@@ -3622,7 +3654,7 @@ static size_t unpack_tdc_domains( unsigned char *buf, int buflen,
 /*********************************************************************
  ********************************************************************/
 
-static BOOL wcache_tdc_store_list( struct winbindd_tdc_domain *domains, size_t num_domains )
+static bool wcache_tdc_store_list( struct winbindd_tdc_domain *domains, size_t num_domains )
 {
        TDB_DATA key = make_tdc_key( lp_workgroup() );   
        TDB_DATA data = { NULL, 0 };
@@ -3657,7 +3689,7 @@ static BOOL wcache_tdc_store_list( struct winbindd_tdc_domain *domains, size_t n
 /*********************************************************************
  ********************************************************************/
 
-BOOL wcache_tdc_fetch_list( struct winbindd_tdc_domain **domains, size_t *num_domains )
+bool wcache_tdc_fetch_list( struct winbindd_tdc_domain **domains, size_t *num_domains )
 {
        TDB_DATA key = make_tdc_key( lp_workgroup() );
        TDB_DATA data = { NULL, 0 };
@@ -3688,16 +3720,16 @@ BOOL wcache_tdc_fetch_list( struct winbindd_tdc_domain **domains, size_t *num_do
 /*********************************************************************
  ********************************************************************/
 
-BOOL wcache_tdc_add_domain( struct winbindd_domain *domain )
+bool wcache_tdc_add_domain( struct winbindd_domain *domain )
 {
        struct winbindd_tdc_domain *dom_list = NULL;
        size_t num_domains = 0;
-       BOOL ret = False;       
+       bool ret = False;       
 
        DEBUG(10,("wcache_tdc_add_domain: Adding domain %s (%s), SID %s, "
                  "flags = 0x%x, attributes = 0x%x, type = 0x%x\n",
                  domain->name, domain->alt_name, 
-                 sid_string_static(&domain->sid),
+                 sid_string_dbg(&domain->sid),
                  domain->domain_flags,
                  domain->domain_trust_attribs,
                  domain->domain_type));        
@@ -3805,6 +3837,7 @@ static void wcache_save_user_pwinfo(struct winbindd_domain *domain,
                                    uint32 gid)
 {
        struct cache_entry *centry;
+       fstring tmp;
 
        if ( (centry = centry_start(domain, status)) == NULL )
                return;
@@ -3814,9 +3847,9 @@ static void wcache_save_user_pwinfo(struct winbindd_domain *domain,
        centry_put_string( centry, gecos );
        centry_put_uint32( centry, gid );
        
-       centry_end(centry, "NSS/PWINFO/%s", sid_string_static(user_sid) );
+       centry_end(centry, "NSS/PWINFO/%s", sid_to_fstring(tmp, user_sid) );
 
-       DEBUG(10,("wcache_save_user_pwinfo: %s\n", sid_string_static(user_sid) ));
+       DEBUG(10,("wcache_save_user_pwinfo: %s\n", sid_string_dbg(user_sid) ));
 
        centry_free(centry);
 }
@@ -3831,11 +3864,13 @@ NTSTATUS nss_get_info_cached( struct winbindd_domain *domain,
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry = NULL;
        NTSTATUS nt_status;
+       fstring tmp;
 
        if (!cache->tdb)
                goto do_query;
 
-       centry = wcache_fetch(cache, domain, "NSS/PWINFO/%s", sid_string_static(user_sid));     
+       centry = wcache_fetch(cache, domain, "NSS/PWINFO/%s",
+                             sid_to_fstring(tmp, user_sid));
        
        if (!centry)
                goto do_query;
@@ -3848,7 +3883,7 @@ NTSTATUS nss_get_info_cached( struct winbindd_domain *domain,
        centry_free(centry);
 
        DEBUG(10,("nss_get_info_cached: [Cached] - user_sid %s\n",
-                 sid_string_static(user_sid)));
+                 sid_string_dbg(user_sid)));
 
        return NT_STATUS_OK;