s3:winbind: Make wcache_lookup_useraliases available publically
authorVolker Lendecke <vl@samba.org>
Sun, 2 Aug 2009 14:52:19 +0000 (16:52 +0200)
committerVolker Lendecke <vl@samba.org>
Wed, 5 Aug 2009 07:21:24 +0000 (03:21 -0400)
source3/winbindd/winbindd_cache.c
source3/winbindd/winbindd_proto.h

index 270b9f310d03f80ff6d18671a44c9674d5e6288f..ea3f00e6d8a3cca7ccdd018d13ef224a88d21ec8 100644 (file)
@@ -2122,58 +2122,74 @@ skip_save:
        return status;
 }
 
-static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
-                                  TALLOC_CTX *mem_ctx,
-                                  uint32 num_sids, const DOM_SID *sids,
-                                  uint32 *num_aliases, uint32 **alias_rids)
+static char *wcache_make_sidlist(TALLOC_CTX *mem_ctx, uint32_t num_sids,
+                                const struct dom_sid *sids)
+{
+       uint32_t i;
+       char *sidlist;
+
+       sidlist = talloc_strdup(mem_ctx, "");
+       if (sidlist == NULL) {
+               return NULL;
+       }
+       for (i=0; i<num_sids; i++) {
+               fstring tmp;
+               sidlist = talloc_asprintf_append_buffer(
+                       sidlist, "/%s", sid_to_fstring(tmp, &sids[i]));
+               if (sidlist == NULL) {
+                       return NULL;
+               }
+       }
+       return sidlist;
+}
+
+NTSTATUS wcache_lookup_useraliases(struct winbindd_domain *domain,
+                                  TALLOC_CTX *mem_ctx, uint32_t num_sids,
+                                  const struct dom_sid *sids,
+                                  uint32_t *pnum_aliases, uint32_t **paliases)
 {
        struct winbind_cache *cache = get_cache(domain);
        struct cache_entry *centry = NULL;
+       uint32_t num_aliases;
+       uint32_t *aliases;
        NTSTATUS status;
-       char *sidlist = talloc_strdup(mem_ctx, "");
+       char *sidlist;
        int i;
 
-       if (!cache->tdb)
-               goto do_query;
+       if (cache->tdb == NULL) {
+               return NT_STATUS_NOT_FOUND;
+       }
 
        if (num_sids == 0) {
-               *num_aliases = 0;
-               *alias_rids = NULL;
+               *pnum_aliases = 0;
+               *paliases = NULL;
                return NT_STATUS_OK;
        }
 
        /* We need to cache indexed by the whole list of SIDs, the aliases
         * 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_to_fstring(tmp, &sids[i]));
-               if (sidlist == NULL)
-                       return NT_STATUS_NO_MEMORY;
+       sidlist = wcache_make_sidlist(talloc_tos(), num_sids, sids);
+       if (sidlist == NULL) {
+               return NT_STATUS_NO_MEMORY;
        }
 
        centry = wcache_fetch(cache, domain, "UA%s", sidlist);
+       TALLOC_FREE(sidlist);
+       if (centry == NULL) {
+               return NT_STATUS_NOT_FOUND;
+       }
 
-       if (!centry)
-               goto do_query;
-
-       *num_aliases = centry_uint32(centry);
-       *alias_rids = NULL;
-
-       if (*num_aliases) {
-               (*alias_rids) = TALLOC_ARRAY(mem_ctx, uint32, *num_aliases);
-
-               if ((*alias_rids) == NULL) {
-                       centry_free(centry);
-                       return NT_STATUS_NO_MEMORY;
-               }
-       } else {
-               (*alias_rids) = NULL;
+       num_aliases = centry_uint32(centry);
+       aliases = talloc_array(mem_ctx, uint32_t, num_aliases);
+       if (aliases == NULL) {
+               centry_free(centry);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       for (i=0; i<(*num_aliases); i++)
-               (*alias_rids)[i] = centry_uint32(centry);
+       for (i=0; i<num_aliases; i++) {
+               aliases[i] = centry_uint32(centry);
+       }
 
        status = centry->status;
 
@@ -2181,9 +2197,29 @@ static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
                  "status %s\n", domain->name, nt_errstr(status)));
 
        centry_free(centry);
+
+       *pnum_aliases = num_aliases;
+       *paliases = aliases;
+
        return status;
+}
+
+static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
+                                  TALLOC_CTX *mem_ctx,
+                                  uint32 num_sids, const DOM_SID *sids,
+                                  uint32 *num_aliases, uint32 **alias_rids)
+{
+       struct cache_entry *centry = NULL;
+       NTSTATUS status;
+       char *sidlist;
+       int i;
+
+       status = wcache_lookup_useraliases(domain, mem_ctx, num_sids, sids,
+                                          num_aliases, alias_rids);
+       if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
+               return status;
+       }
 
- do_query:
        (*num_aliases) = 0;
        (*alias_rids) = NULL;
 
@@ -2193,6 +2229,11 @@ static NTSTATUS lookup_useraliases(struct winbindd_domain *domain,
        DEBUG(10,("lookup_usergroups: [Cached] - doing backend query for info "
                  "for domain %s\n", domain->name ));
 
+       sidlist = wcache_make_sidlist(talloc_tos(), num_sids, sids);
+       if (sidlist == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
        status = domain->backend->lookup_useraliases(domain, mem_ctx,
                                                     num_sids, sids,
                                                     num_aliases, alias_rids);
index c155589954634f2ffa9176420f1dc10707236155..08c08222a65a74f7764f65dd3ed7a32d4803b27c 100644 (file)
@@ -175,6 +175,10 @@ NTSTATUS wcache_query_user(struct winbindd_domain *domain,
                           TALLOC_CTX *mem_ctx,
                           const struct dom_sid *user_sid,
                           struct winbind_userinfo *info);
+NTSTATUS wcache_lookup_useraliases(struct winbindd_domain *domain,
+                                  TALLOC_CTX *mem_ctx,
+                                  uint32 num_sids, const DOM_SID *sids,
+                                  uint32 *pnum_aliases, uint32 **paliases);
 void wcache_flush_cache(void);
 NTSTATUS wcache_count_cached_creds(struct winbindd_domain *domain, int *count);
 NTSTATUS wcache_remove_oldest_cached_creds(struct winbindd_domain *domain, const DOM_SID *sid) ;