r15053: fix portabilities issues between 32-bit winbind clients and a 64-bit winbindd...
[tprouty/samba.git] / source / nsswitch / winbindd_cache.c
index 8e694ed85061c2358d9305d54f6382f877a46338..69a4c8eda3bc20f958f844b955adf61276fae107 100644 (file)
@@ -83,7 +83,9 @@ void winbindd_check_cache_size(time_t t)
 static struct winbind_cache *get_cache(struct winbindd_domain *domain)
 {
        struct winbind_cache *ret = wcache;
+#ifdef HAVE_ADS
        struct winbindd_domain *our_domain = domain;
+#endif
 
        /* we have to know what type of domain we are dealing with first */
 
@@ -257,10 +259,7 @@ static char *centry_string(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
                smb_panic("centry_string");
        }
 
-       if (mem_ctx != NULL)
-               ret = TALLOC(mem_ctx, len+1);
-       else
-               ret = SMB_MALLOC(len+1);
+       ret = TALLOC(mem_ctx, len+1);
        if (!ret) {
                smb_panic("centry_string out of memory\n");
        }
@@ -273,14 +272,13 @@ static char *centry_string(struct cache_entry *centry, TALLOC_CTX *mem_ctx)
 /* pull a string from a cache entry, using the supplied
    talloc context 
 */
-static BOOL centry_sid(struct cache_entry *centry, 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, NULL);
+       sid_string = centry_string(centry, mem_ctx);
        if (!string_to_sid(sid, sid_string)) {
                return False;
        }
-       SAFE_FREE(sid_string);
        return True;
 }
 
@@ -802,11 +800,20 @@ 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;
+       uint32 rid;
 
        if (!cache->tdb) {
                return NT_STATUS_INTERNAL_DB_ERROR;
        }
 
+       if (is_null_sid(sid)) {
+               return NT_STATUS_INVALID_SID;
+       }
+
+       if (!(sid_peek_rid(sid, &rid)) || (rid == 0)) {
+               return NT_STATUS_INVALID_SID;
+       }
+
        fstr_sprintf(key_str, "CRED/%s", sid_string_static(sid));
 
        data = tdb_fetch(cache->tdb, make_tdb_data(key_str, strlen(key_str)));
@@ -827,11 +834,20 @@ NTSTATUS wcache_get_creds(struct winbindd_domain *domain,
        struct cache_entry *centry = NULL;
        NTSTATUS status;
        time_t t;
+       uint32 rid;
 
        if (!cache->tdb) {
                return NT_STATUS_INTERNAL_DB_ERROR;
        }
 
+       if (is_null_sid(sid)) {
+               return NT_STATUS_INVALID_SID;
+       }
+
+       if (!(sid_peek_rid(sid, &rid)) || (rid == 0)) {
+               return NT_STATUS_INVALID_SID;
+       }
+
        centry = wcache_fetch(cache, domain, "CRED/%s", sid_string_static(sid));
        
        if (!centry) {
@@ -843,7 +859,9 @@ NTSTATUS wcache_get_creds(struct winbindd_domain *domain,
        t = centry_time(centry);
        *cached_nt_pass = (const uint8 *)centry_string(centry, mem_ctx);
 
-       dump_data(10, (const char *)cached_nt_pass, NT_HASH_LEN);
+#if DEBUG_PASSWORD
+       dump_data(100, (const char *)cached_nt_pass, NT_HASH_LEN);
+#endif
        status = centry->status;
 
        DEBUG(10,("wcache_get_creds: [Cached] - cached creds for user %s status %s\n",
@@ -860,14 +878,24 @@ NTSTATUS wcache_save_creds(struct winbindd_domain *domain,
 {
        struct cache_entry *centry;
        fstring sid_string;
-       NTSTATUS status = NT_STATUS_OK; /* ??? */
+       uint32 rid;
 
-       centry = centry_start(domain, status);
+       if (is_null_sid(sid)) {
+               return NT_STATUS_INVALID_SID;
+       }
+
+       if (!(sid_peek_rid(sid, &rid)) || (rid == 0)) {
+               return NT_STATUS_INVALID_SID;
+       }
+
+       centry = centry_start(domain, NT_STATUS_OK);
        if (!centry) {
                return NT_STATUS_INTERNAL_DB_ERROR;
        }
 
+#if DEBUG_PASSWORD
        dump_data(100, (const char *)nt_pass, NT_HASH_LEN);
+#endif
 
        centry_put_time(centry, time(NULL));
        centry_put_string(centry, (const char *)nt_pass);
@@ -912,8 +940,8 @@ static NTSTATUS query_user_list(struct winbindd_domain *domain,
                (*info)[i].full_name = centry_string(centry, mem_ctx);
                (*info)[i].homedir = centry_string(centry, mem_ctx);
                (*info)[i].shell = centry_string(centry, mem_ctx);
-               centry_sid(centry, &(*info)[i].user_sid);
-               centry_sid(centry, &(*info)[i].group_sid);
+               centry_sid(centry, mem_ctx, &(*info)[i].user_sid);
+               centry_sid(centry, mem_ctx, &(*info)[i].group_sid);
        }
 
 do_cached:     
@@ -1176,7 +1204,7 @@ static NTSTATUS name_to_sid(struct winbindd_domain *domain,
        *type = (enum SID_NAME_USE)centry_uint32(centry);
        status = centry->status;
        if (NT_STATUS_IS_OK(status)) {
-               centry_sid(centry, sid);
+               centry_sid(centry, mem_ctx, sid);
        }
 
        DEBUG(10,("name_to_sid: [Cached] - cached name for domain %s status %s\n",
@@ -1315,8 +1343,8 @@ static NTSTATUS query_user(struct winbindd_domain *domain,
        info->full_name = centry_string(centry, mem_ctx);
        info->homedir = centry_string(centry, mem_ctx);
        info->shell = centry_string(centry, mem_ctx);
-       centry_sid(centry, &info->user_sid);
-       centry_sid(centry, &info->group_sid);
+       centry_sid(centry, mem_ctx, &info->user_sid);
+       centry_sid(centry, mem_ctx, &info->group_sid);
        status = centry->status;
 
        DEBUG(10,("query_user: [Cached] - cached info for domain %s status %s\n",
@@ -1387,7 +1415,7 @@ static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
        if (! (*user_gids))
                smb_panic("lookup_usergroups out of memory");
        for (i=0; i<(*num_groups); i++) {
-               centry_sid(centry, &(*user_gids)[i]);
+               centry_sid(centry, mem_ctx, &(*user_gids)[i]);
        }
 
 do_cached:     
@@ -1549,7 +1577,7 @@ static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
        }
 
        for (i=0; i<(*num_names); i++) {
-               centry_sid(centry, &(*sid_mem)[i]);
+               centry_sid(centry, mem_ctx, &(*sid_mem)[i]);
                (*names)[i] = centry_string(centry, mem_ctx);
                (*name_types)[i] = centry_uint32(centry);
        }
@@ -1645,7 +1673,7 @@ static NTSTATUS trusted_domains(struct winbindd_domain *domain,
        for (i=0; i<(*num_domains); i++) {
                (*names)[i] = centry_string(centry, mem_ctx);
                (*alt_names)[i] = centry_string(centry, mem_ctx);
-               centry_sid(centry, &(*dom_sids)[i]);
+               centry_sid(centry, mem_ctx, &(*dom_sids)[i]);
        }
 
        status = centry->status;
@@ -1672,7 +1700,15 @@ do_query:
  
        status = domain->backend->trusted_domains(domain, mem_ctx, num_domains,
                                                names, alt_names, dom_sids);
+
+       /* no trusts gives NT_STATUS_NO_MORE_ENTRIES resetting to NT_STATUS_OK
+        * so that the generic centry handling still applies correctly -
+        * Guenther*/
+
+       if (!NT_STATUS_IS_ERR(status)) {
+               status = NT_STATUS_OK;
+       }
+
        /* and save it */
        refresh_sequence_number(domain, False);
  
@@ -1885,7 +1921,7 @@ void cache_store_response(pid_t pid, struct winbindd_response *response)
 
        fstr_sprintf(key_str, "DE/%d", pid);
        if (tdb_store(wcache->tdb, string_tdb_data(key_str),
-                     make_tdb_data(response->extra_data,
+                     make_tdb_data(response->extra_data.data,
                                    response->length - sizeof(*response)),
                      TDB_REPLACE) == 0)
                return;
@@ -1922,7 +1958,7 @@ BOOL cache_retrieve_response(pid_t pid, struct winbindd_response * response)
        SAFE_FREE(data.dptr);
 
        if (response->length == sizeof(*response)) {
-               response->extra_data = NULL;
+               response->extra_data.data = NULL;
                return True;
        }
 
@@ -1947,7 +1983,7 @@ BOOL cache_retrieve_response(pid_t pid, struct winbindd_response * response)
 
        dump_data(11, data.dptr, data.dsize);
 
-       response->extra_data = data.dptr;
+       response->extra_data.data = data.dptr;
        return True;
 }
 
@@ -2037,7 +2073,7 @@ BOOL lookup_cached_name(TALLOC_CTX *mem_ctx,
 
        if (NT_STATUS_IS_OK(centry->status)) {
                *type = (enum SID_NAME_USE)centry_uint32(centry);
-               centry_sid(centry, sid);
+               centry_sid(centry, mem_ctx, sid);
        }
 
        status = centry->status;