r20090: Fix a class of bugs found by James Peach. Ensure
[gd/samba-autobuild/.git] / source3 / nsswitch / winbindd_async.c
index 1581b7c272024e738a2066a968b576051f72ea21..607a9947ea22fb5b5ac0d97b42328b8dc24932aa 100644 (file)
@@ -4,7 +4,7 @@
    Async helpers for blocking functions
 
    Copyright (C) Volker Lendecke 2005
-   Copyright (C) Volker Lendecke 2006
+   Copyright (C) Gerald Carter 2006
    
    The helpers always consist of three functions: 
 
@@ -67,7 +67,7 @@ static void do_async(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
 {
        struct do_async_state *state;
 
-       state = TALLOC_P(mem_ctx, struct do_async_state);
+       state = TALLOC_ZERO_P(mem_ctx, struct do_async_state);
        if (state == NULL) {
                DEBUG(0, ("talloc failed\n"));
                cont(mem_ctx, False, NULL, c, private_data);
@@ -94,7 +94,7 @@ void do_async_domain(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
 {
        struct do_async_state *state;
 
-       state = TALLOC_P(mem_ctx, struct do_async_state);
+       state = TALLOC_ZERO_P(mem_ctx, struct do_async_state);
        if (state == NULL) {
                DEBUG(0, ("talloc failed\n"));
                cont(mem_ctx, False, NULL, c, private_data);
@@ -116,7 +116,7 @@ static void idmap_set_mapping_recv(TALLOC_CTX *mem_ctx, BOOL success,
                                   struct winbindd_response *response,
                                   void *c, void *private_data)
 {
-       void (*cont)(void *priv, BOOL succ) = c;
+       void (*cont)(void *priv, BOOL succ) = (void (*)(void *, BOOL))c;
 
        if (!success) {
                DEBUG(5, ("Could not trigger idmap_set_mapping\n"));
@@ -149,7 +149,7 @@ void idmap_set_mapping_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
        sid_to_string(request.data.dual_idmapset.sid, sid);
 
        do_async(mem_ctx, idmap_child(), &request, idmap_set_mapping_recv,
-                cont, private_data);
+                (void *)cont, private_data);
 }
 
 enum winbindd_result winbindd_dual_idmapset(struct winbindd_domain *domain,
@@ -169,8 +169,9 @@ enum winbindd_result winbindd_dual_idmapset(struct winbindd_domain *domain,
        else
                id.gid = state->request.data.dual_idmapset.gid;
 
-       result = idmap_set_mapping(&sid, id,
-                                  state->request.data.dual_idmapset.type);
+       result = idmap_set_mapping(
+               &sid, id,
+               (enum idmap_type)state->request.data.dual_idmapset.type);
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
 
@@ -188,7 +189,7 @@ void idmap_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid, BOOL alloc,
        sid_to_string(request.data.dual_sid2id.sid, sid);
        request.data.dual_sid2id.alloc = alloc;
        do_async(mem_ctx, idmap_child(), &request, idmap_sid2uid_recv,
-                cont, private_data);
+                (void *)cont, private_data);
 }
 
 enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
@@ -211,7 +212,7 @@ enum winbindd_result winbindd_dual_sid2uid(struct winbindd_domain *domain,
 
        result = idmap_sid_to_uid(&sid, &(state->response.data.uid),
                                  state->request.data.dual_sid2id.alloc ?
-                                 0 : ID_QUERY_ONLY);
+                                 0 : IDMAP_FLAG_QUERY_ONLY);
 
        return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
 }
@@ -220,7 +221,8 @@ static void idmap_sid2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
                               struct winbindd_response *response,
                               void *c, void *private_data)
 {
-       void (*cont)(void *priv, BOOL succ, uid_t uid) = c;
+       void (*cont)(void *priv, BOOL succ, uid_t uid) =
+               (void (*)(void *, BOOL, uid_t))c;
 
        if (!success) {
                DEBUG(5, ("Could not trigger sid2uid\n"));
@@ -251,7 +253,7 @@ void winbindd_uid2name_async(TALLOC_CTX *mem_ctx, uid_t uid,
        request.cmd = WINBINDD_DUAL_UID2NAME;
        request.data.uid = uid;
        do_async(mem_ctx, idmap_child(), &request, uid2name_recv,
-                cont, private_data);
+                (void *)cont, private_data);
 }
 
 enum winbindd_result winbindd_dual_uid2name(struct winbindd_domain *domain,
@@ -277,7 +279,8 @@ static void uid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
                          struct winbindd_response *response,
                          void *c, void *private_data)
 {
-       void (*cont)(void *priv, BOOL succ, const char *name) = c;
+       void (*cont)(void *priv, BOOL succ, const char *name) =
+               (void (*)(void *, BOOL, const char *))c;
 
        if (!success) {
                DEBUG(5, ("Could not trigger uid2name\n"));
@@ -308,7 +311,7 @@ static void winbindd_name2uid_async(TALLOC_CTX *mem_ctx, const char *name,
        request.cmd = WINBINDD_DUAL_NAME2UID;
        fstrcpy(request.data.username, name);
        do_async(mem_ctx, idmap_child(), &request, name2uid_recv,
-                cont, private_data);
+                (void *)cont, private_data);
 }
 
 enum winbindd_result winbindd_dual_name2uid(struct winbindd_domain *domain,
@@ -336,7 +339,8 @@ static void name2uid_recv(TALLOC_CTX *mem_ctx, BOOL success,
                          struct winbindd_response *response,
                          void *c, void *private_data)
 {
-       void (*cont)(void *priv, BOOL succ, uid_t uid) = c;
+       void (*cont)(void *priv, BOOL succ, uid_t uid) =
+               (void (*)(void *, BOOL, uid_t))c;
 
        if (!success) {
                DEBUG(5, ("Could not trigger name2uid\n"));
@@ -371,7 +375,7 @@ void idmap_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid, BOOL alloc,
 
        request.data.dual_sid2id.alloc = alloc;
        do_async(mem_ctx, idmap_child(), &request, idmap_sid2gid_recv,
-                cont, private_data);
+                (void *)cont, private_data);
 }
 
 enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
@@ -392,9 +396,9 @@ enum winbindd_result winbindd_dual_sid2gid(struct winbindd_domain *domain,
        /* Find gid for this sid and return it, possibly ask the slow remote
         * idmap */
 
-       result = idmap_sid_to_gid(&sid, &(state->response.data.gid),
+       result = idmap_sid_to_gid(&sid, &state->response.data.gid,
                                  state->request.data.dual_sid2id.alloc ?
-                                 0 : ID_QUERY_ONLY);
+                                 0 : IDMAP_FLAG_QUERY_ONLY);
 
        /* If the lookup failed, the perhaps we need to look 
           at the passdb for local groups */
@@ -412,7 +416,8 @@ static void idmap_sid2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
                               struct winbindd_response *response,
                               void *c, void *private_data)
 {
-       void (*cont)(void *priv, BOOL succ, gid_t gid) = c;
+       void (*cont)(void *priv, BOOL succ, gid_t gid) =
+               (void (*)(void *, BOOL, gid_t))c;
 
        if (!success) {
                DEBUG(5, ("Could not trigger sid2gid\n"));
@@ -433,7 +438,8 @@ static void gid2name_recv(TALLOC_CTX *mem_ctx, BOOL success,
                          struct winbindd_response *response,
                          void *c, void *private_data)
 {
-       void (*cont)(void *priv, BOOL succ, const char *name) = c;
+       void (*cont)(void *priv, BOOL succ, const char *name) =
+               (void (*)(void *, BOOL, const char *))c;
 
        if (!success) {
                DEBUG(5, ("Could not trigger gid2name\n"));
@@ -460,7 +466,7 @@ void winbindd_gid2name_async(TALLOC_CTX *mem_ctx, gid_t gid,
        request.cmd = WINBINDD_DUAL_GID2NAME;
        request.data.gid = gid;
        do_async(mem_ctx, idmap_child(), &request, gid2name_recv,
-                cont, private_data);
+                (void *)cont, private_data);
 }
 
 enum winbindd_result winbindd_dual_gid2name(struct winbindd_domain *domain,
@@ -493,7 +499,7 @@ static void winbindd_name2gid_async(TALLOC_CTX *mem_ctx, const char *name,
        request.cmd = WINBINDD_DUAL_NAME2GID;
        fstrcpy(request.data.groupname, name);
        do_async(mem_ctx, idmap_child(), &request, name2gid_recv,
-                cont, private_data);
+                (void *)cont, private_data);
 }
 
 enum winbindd_result winbindd_dual_name2gid(struct winbindd_domain *domain,
@@ -521,7 +527,8 @@ static void name2gid_recv(TALLOC_CTX *mem_ctx, BOOL success,
                          struct winbindd_response *response,
                          void *c, void *private_data)
 {
-       void (*cont)(void *priv, BOOL succ, gid_t gid) = c;
+       void (*cont)(void *priv, BOOL succ, gid_t gid) =
+               (void (*)(void *, BOOL, gid_t))c;
 
        if (!success) {
                DEBUG(5, ("Could not trigger name2gid\n"));
@@ -544,7 +551,9 @@ static void lookupsid_recv(TALLOC_CTX *mem_ctx, BOOL success,
                           void *c, void *private_data)
 {
        void (*cont)(void *priv, BOOL succ, const char *dom_name,
-                    const char *name, enum SID_NAME_USE type) = c;
+                    const char *name, enum lsa_SidType type) =
+               (void (*)(void *, BOOL, const char *, const char *,
+                         enum lsa_SidType))c;
 
        if (!success) {
                DEBUG(5, ("Could not trigger lookupsid\n"));
@@ -559,14 +568,15 @@ static void lookupsid_recv(TALLOC_CTX *mem_ctx, BOOL success,
        }
 
        cont(private_data, True, response->data.name.dom_name,
-            response->data.name.name, response->data.name.type);
+            response->data.name.name,
+            (enum lsa_SidType)response->data.name.type);
 }
 
 void winbindd_lookupsid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
                              void (*cont)(void *private_data, BOOL success,
                                           const char *dom_name,
                                           const char *name,
-                                          enum SID_NAME_USE type),
+                                          enum lsa_SidType type),
                              void *private_data)
 {
        struct winbindd_domain *domain;
@@ -585,13 +595,13 @@ void winbindd_lookupsid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
        fstrcpy(request.data.sid, sid_string_static(sid));
 
        do_async_domain(mem_ctx, domain, &request, lookupsid_recv,
-                       cont, private_data);
+                       (void *)cont, private_data);
 }
 
 enum winbindd_result winbindd_dual_lookupsid(struct winbindd_domain *domain,
                                             struct winbindd_cli_state *state)
 {
-       enum SID_NAME_USE type;
+       enum lsa_SidType type;
        DOM_SID sid;
        fstring name;
        fstring dom_name;
@@ -628,7 +638,8 @@ static void lookupname_recv(TALLOC_CTX *mem_ctx, BOOL success,
                            void *c, void *private_data)
 {
        void (*cont)(void *priv, BOOL succ, const DOM_SID *sid,
-                    enum SID_NAME_USE type) = c;
+                    enum lsa_SidType type) =
+               (void (*)(void *, BOOL, const DOM_SID *, enum lsa_SidType))c;
        DOM_SID sid;
 
        if (!success) {
@@ -650,14 +661,15 @@ static void lookupname_recv(TALLOC_CTX *mem_ctx, BOOL success,
                return;
        }
 
-       cont(private_data, True, &sid, response->data.sid.type);
+       cont(private_data, True, &sid,
+            (enum lsa_SidType)response->data.sid.type);
 }
 
 void winbindd_lookupname_async(TALLOC_CTX *mem_ctx, const char *dom_name,
                               const char *name,
                               void (*cont)(void *private_data, BOOL success,
                                            const DOM_SID *sid,
-                                           enum SID_NAME_USE type),
+                                           enum lsa_SidType type),
                               void *private_data)
 {
        struct winbindd_request request;
@@ -677,13 +689,13 @@ void winbindd_lookupname_async(TALLOC_CTX *mem_ctx, const char *dom_name,
        fstrcpy(request.data.name.name, name);
 
        do_async_domain(mem_ctx, domain, &request, lookupname_recv,
-                       cont, private_data);
+                       (void *)cont, private_data);
 }
 
 enum winbindd_result winbindd_dual_lookupname(struct winbindd_domain *domain,
                                              struct winbindd_cli_state *state)
 {
-       enum SID_NAME_USE type;
+       enum lsa_SidType type;
        char *name_domain, *name_user;
        DOM_SID sid;
        char *p;
@@ -740,8 +752,8 @@ BOOL print_sidlist(TALLOC_CTX *mem_ctx, const DOM_SID *sids,
        return True;
 }
 
-BOOL parse_sidlist(TALLOC_CTX *mem_ctx, char *sidstr,
-                  DOM_SID **sids, size_t *num_sids)
+static BOOL parse_sidlist(TALLOC_CTX *mem_ctx, char *sidstr,
+                         DOM_SID **sids, size_t *num_sids)
 {
        char *p, *q;
 
@@ -762,34 +774,16 @@ BOOL parse_sidlist(TALLOC_CTX *mem_ctx, char *sidstr,
                        DEBUG(0, ("Could not parse sid %s\n", p));
                        return False;
                }
-               add_sid_to_array(mem_ctx, &sid, sids, num_sids);
+               if (!add_sid_to_array(mem_ctx, &sid, sids, num_sids)) {
+                       return False;
+               }
                p = q;
        }
        return True;
 }
 
-BOOL print_ridlist(TALLOC_CTX *mem_ctx, uint32 *rids, size_t num_rids,
-                  char **result, ssize_t *len)
-{
-       size_t i;
-       size_t buflen = 0;
-
-       *len = 0;
-       *result = NULL;
-       for (i=0; i<num_rids; i++) {
-               sprintf_append(mem_ctx, result, len, &buflen,
-                              "%ld\n", rids[i]);
-       }
-
-       if ((num_rids != 0) && (*result == NULL)) {
-               return False;
-       }
-
-       return True;
-}
-
-BOOL parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
-                  uint32 **sids, size_t *num_rids)
+static BOOL parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
+                         uint32 **rids, size_t *num_rids)
 {
        char *p;
 
@@ -806,17 +800,76 @@ BOOL parse_ridlist(TALLOC_CTX *mem_ctx, char *ridstr,
                        return False;
                }
                p = q+1;
-               ADD_TO_ARRAY(mem_ctx, uint32, rid, sids, num_rids);
+               ADD_TO_ARRAY(mem_ctx, uint32, rid, rids, num_rids);
        }
        return True;
 }
 
+enum winbindd_result winbindd_dual_lookuprids(struct winbindd_domain *domain,
+                                             struct winbindd_cli_state *state)
+{
+       uint32 *rids = NULL;
+       size_t i, buflen, num_rids = 0;
+       ssize_t len;
+       DOM_SID domain_sid;
+       char *domain_name;
+       char **names;
+       enum lsa_SidType *types;
+       NTSTATUS status;
+       char *result;
+
+       DEBUG(10, ("Looking up RIDs for domain %s (%s)\n",
+                  state->request.domain_name,
+                  state->request.data.sid));
+
+       if (!parse_ridlist(state->mem_ctx, state->request.extra_data.data,
+                          &rids, &num_rids)) {
+               DEBUG(5, ("Could not parse ridlist\n"));
+               return WINBINDD_ERROR;
+       }
+
+       if (!string_to_sid(&domain_sid, state->request.data.sid)) {
+               DEBUG(5, ("Could not parse domain sid %s\n",
+                         state->request.data.sid));
+               return WINBINDD_ERROR;
+       }
+
+       status = domain->methods->rids_to_names(domain, state->mem_ctx,
+                                               &domain_sid, rids, num_rids,
+                                               &domain_name,
+                                               &names, &types);
+
+       if (!NT_STATUS_IS_OK(status) &&
+           !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
+               return WINBINDD_ERROR;
+       }
+
+       len = 0;
+       buflen = 0;
+       result = NULL;
+
+       for (i=0; i<num_rids; i++) {
+               sprintf_append(state->mem_ctx, &result, &len, &buflen,
+                              "%d %s\n", types[i], names[i]);
+       }
+
+       fstrcpy(state->response.data.domain_name, domain_name);
+
+       if (result != NULL) {
+               state->response.extra_data.data = SMB_STRDUP(result);
+               state->response.length += len+1;
+       }
+
+       return WINBINDD_OK;
+}
+
 static void getsidaliases_recv(TALLOC_CTX *mem_ctx, BOOL success,
                               struct winbindd_response *response,
                               void *c, void *private_data)
 {
        void (*cont)(void *priv, BOOL succ,
-                    DOM_SID *aliases, size_t num_aliases) = c;
+                    DOM_SID *aliases, size_t num_aliases) =
+               (void (*)(void *, BOOL, DOM_SID *, size_t))c;
        char *aliases_str;
        DOM_SID *sids = NULL;
        size_t num_sids = 0;
@@ -833,7 +886,7 @@ static void getsidaliases_recv(TALLOC_CTX *mem_ctx, BOOL success,
                return;
        }
 
-       aliases_str = response->extra_data;
+       aliases_str = (char *)response->extra_data.data;
 
        if (aliases_str == NULL) {
                DEBUG(10, ("getsidaliases return 0 SIDs\n"));
@@ -847,7 +900,7 @@ static void getsidaliases_recv(TALLOC_CTX *mem_ctx, BOOL success,
                return;
        }
 
-       SAFE_FREE(response->extra_data);
+       SAFE_FREE(response->extra_data.data);
 
        cont(private_data, True, sids, num_sids);
 }
@@ -878,10 +931,10 @@ void winbindd_getsidaliases_async(struct winbindd_domain *domain,
        ZERO_STRUCT(request);
        request.cmd = WINBINDD_DUAL_GETSIDALIASES;
        request.extra_len = len;
-       request.extra_data = sidstr;
+       request.extra_data.data = sidstr;
 
        do_async_domain(mem_ctx, domain, &request, getsidaliases_recv,
-                       cont, private_data);
+                       (void *)cont, private_data);
 }
 
 enum winbindd_result winbindd_dual_getsidaliases(struct winbindd_domain *domain,
@@ -898,7 +951,7 @@ enum winbindd_result winbindd_dual_getsidaliases(struct winbindd_domain *domain,
 
        DEBUG(3, ("[%5lu]: getsidaliases\n", (unsigned long)state->pid));
 
-       sidstr = state->request.extra_data;
+       sidstr = state->request.extra_data.data;
        if (sidstr == NULL)
                sidstr = talloc_strdup(state->mem_ctx, "\n"); /* No SID */
 
@@ -934,18 +987,23 @@ enum winbindd_result winbindd_dual_getsidaliases(struct winbindd_domain *domain,
                DEBUGADD(10, (" rid %d\n", alias_rids[i]));
                sid_copy(&sid, &domain->sid);
                sid_append_rid(&sid, alias_rids[i]);
-               add_sid_to_array(state->mem_ctx, &sid, &sids, &num_sids);
+               if (!add_sid_to_array(state->mem_ctx, &sid, &sids, &num_sids)) {
+                       return WINBINDD_ERROR;
+               }
        }
 
-       if (!print_sidlist(NULL, sids, num_sids,
-                          (char **)&state->response.extra_data, &len)) {
+
+       if (!print_sidlist(NULL, sids, num_sids, &sidstr, &len)) {
                DEBUG(0, ("Could not print_sidlist\n"));
+               state->response.extra_data.data = NULL;
                return WINBINDD_ERROR;
        }
 
-       if (state->response.extra_data != NULL) {
+       state->response.extra_data.data = sidstr;
+
+       if (state->response.extra_data.data != NULL) {
                DEBUG(10, ("aliases_list: %s\n",
-                          (char *)state->response.extra_data));
+                          (char *)state->response.extra_data.data));
                state->response.length += len+1;
        }
        
@@ -956,6 +1014,7 @@ struct gettoken_state {
        TALLOC_CTX *mem_ctx;
        DOM_SID user_sid;
        struct winbindd_domain *alias_domain;
+       struct winbindd_domain *local_alias_domain;
        struct winbindd_domain *builtin_domain;
        DOM_SID *sids;
        size_t num_sids;
@@ -980,7 +1039,7 @@ void winbindd_gettoken_async(TALLOC_CTX *mem_ctx, const DOM_SID *user_sid,
        struct winbindd_request request;
        struct gettoken_state *state;
 
-       state = TALLOC_P(mem_ctx, struct gettoken_state);
+       state = TALLOC_ZERO_P(mem_ctx, struct gettoken_state);
        if (state == NULL) {
                DEBUG(0, ("talloc failed\n"));
                cont(private_data, False, NULL, 0);
@@ -990,6 +1049,7 @@ void winbindd_gettoken_async(TALLOC_CTX *mem_ctx, const DOM_SID *user_sid,
        state->mem_ctx = mem_ctx;
        sid_copy(&state->user_sid, user_sid);
        state->alias_domain = find_our_domain();
+       state->local_alias_domain = find_domain_from_name( get_global_sam_name() );
        state->builtin_domain = find_builtin_domain();
        state->cont = cont;
        state->private_data = private_data;
@@ -1024,7 +1084,7 @@ static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, BOOL success,
                return;
        }
 
-       sids_str = response->extra_data;
+       sids_str = (char *)response->extra_data.data;
 
        if (sids_str == NULL) {
                /* This could be normal if we are dealing with a
@@ -1040,8 +1100,12 @@ static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, BOOL success,
        state->sids = NULL;
        state->num_sids = 0;
 
-       add_sid_to_array(mem_ctx, &state->user_sid, &state->sids,
-                        &state->num_sids);
+       if (!add_sid_to_array(mem_ctx, &state->user_sid, &state->sids,
+                        &state->num_sids)) {
+               DEBUG(0, ("Out of memory\n"));
+               state->cont(state->private_data, False, NULL, 0);
+               return;
+       }
 
        if (sids_str && !parse_sidlist(mem_ctx, sids_str, &state->sids,
                           &state->num_sids)) {
@@ -1050,7 +1114,7 @@ static void gettoken_recvdomgroups(TALLOC_CTX *mem_ctx, BOOL success,
                return;
        }
 
-       SAFE_FREE(response->extra_data);
+       SAFE_FREE(response->extra_data.data);
 
        if (state->alias_domain == NULL) {
                DEBUG(10, ("Don't expand domain local groups\n"));
@@ -1068,7 +1132,7 @@ static void gettoken_recvaliases(void *private_data, BOOL success,
                                 const DOM_SID *aliases,
                                 size_t num_aliases)
 {
-       struct gettoken_state *state = private_data;
+       struct gettoken_state *state = (struct gettoken_state *)private_data;
        size_t i;
 
        if (!success) {
@@ -1077,13 +1141,28 @@ static void gettoken_recvaliases(void *private_data, BOOL success,
                return;
        }
 
-       for (i=0; i<num_aliases; i++)
-               add_sid_to_array(state->mem_ctx, &aliases[i],
-                                &state->sids, &state->num_sids);
+       for (i=0; i<num_aliases; i++) {
+               if (!add_sid_to_array(state->mem_ctx, &aliases[i],
+                                &state->sids, &state->num_sids)) {
+                       DEBUG(0, ("Out of memory\n"));
+                       state->cont(state->private_data, False, NULL, 0);
+                       return;
+               }
+       }
+
+       if (state->local_alias_domain != NULL) {
+               struct winbindd_domain *local_domain = state->local_alias_domain;
+               DEBUG(10, ("Expanding our own local groups\n"));
+               state->local_alias_domain = NULL;
+               winbindd_getsidaliases_async(local_domain, state->mem_ctx,
+                                            state->sids, state->num_sids,
+                                            gettoken_recvaliases, state);
+               return;
+       }
 
        if (state->builtin_domain != NULL) {
                struct winbindd_domain *builtin_domain = state->builtin_domain;
-               DEBUG(10, ("Expanding our own local groups\n"));
+               DEBUG(10, ("Expanding our own BUILTIN groups\n"));
                state->builtin_domain = NULL;
                winbindd_getsidaliases_async(builtin_domain, state->mem_ctx,
                                             state->sids, state->num_sids,
@@ -1105,7 +1184,7 @@ struct sid2uid_state {
 
 static void sid2uid_lookup_sid_recv(void *private_data, BOOL success,
                                    const char *dom_name, const char *name,
-                                   enum SID_NAME_USE type);
+                                   enum lsa_SidType type);
 static void sid2uid_noalloc_recv(void *private_data, BOOL success, uid_t uid);
 static void sid2uid_alloc_recv(void *private_data, BOOL success, uid_t uid);
 static void sid2uid_name2uid_recv(void *private_data, BOOL success, uid_t uid);
@@ -1128,14 +1207,14 @@ void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
 
        /* Query only the local tdb, everything else might possibly block */
 
-       result = idmap_sid_to_uid(sid, &uid, ID_QUERY_ONLY|ID_CACHE_ONLY);
+       result = idmap_sid_to_uid(sid, &uid, IDMAP_FLAG_QUERY_ONLY|IDMAP_FLAG_CACHE_ONLY);
 
        if (NT_STATUS_IS_OK(result)) {
                cont(private_data, True, uid);
                return;
        }
 
-       state = TALLOC_P(mem_ctx, struct sid2uid_state);
+       state = TALLOC_ZERO_P(mem_ctx, struct sid2uid_state);
        if (state == NULL) {
                DEBUG(0, ("talloc failed\n"));
                cont(private_data, False, 0);
@@ -1154,7 +1233,7 @@ void winbindd_sid2uid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
 
 static void sid2uid_lookup_sid_recv(void *private_data, BOOL success,
                                    const char *dom_name, const char *name,
-                                   enum SID_NAME_USE type)
+                                   enum lsa_SidType type)
 {
        struct sid2uid_state *state =
                talloc_get_type_abort(private_data, struct sid2uid_state);
@@ -1267,7 +1346,7 @@ struct sid2gid_state {
 
 static void sid2gid_lookup_sid_recv(void *private_data, BOOL success,
                                    const char *dom_name, const char *name,
-                                   enum SID_NAME_USE type);
+                                   enum lsa_SidType type);
 static void sid2gid_noalloc_recv(void *private_data, BOOL success, gid_t gid);
 static void sid2gid_alloc_recv(void *private_data, BOOL success, gid_t gid);
 static void sid2gid_name2gid_recv(void *private_data, BOOL success, gid_t gid);
@@ -1290,14 +1369,14 @@ void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
 
        /* Query only the local tdb, everything else might possibly block */
 
-       result = idmap_sid_to_gid(sid, &gid, ID_QUERY_ONLY|ID_CACHE_ONLY);
+       result = idmap_sid_to_gid(sid, &gid, IDMAP_FLAG_QUERY_ONLY|IDMAP_FLAG_CACHE_ONLY);
 
        if (NT_STATUS_IS_OK(result)) {
                cont(private_data, True, gid);
                return;
        }
 
-       state = TALLOC_P(mem_ctx, struct sid2gid_state);
+       state = TALLOC_ZERO_P(mem_ctx, struct sid2gid_state);
        if (state == NULL) {
                DEBUG(0, ("talloc failed\n"));
                cont(private_data, False, 0);
@@ -1316,7 +1395,7 @@ void winbindd_sid2gid_async(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
 
 static void sid2gid_lookup_sid_recv(void *private_data, BOOL success,
                                    const char *dom_name, const char *name,
-                                   enum SID_NAME_USE type)
+                                   enum lsa_SidType type)
 {
        struct sid2gid_state *state =
                talloc_get_type_abort(private_data, struct sid2gid_state);
@@ -1425,7 +1504,9 @@ static void query_user_recv(TALLOC_CTX *mem_ctx, BOOL success,
 {
        void (*cont)(void *priv, BOOL succ, const char *acct_name,
                     const char *full_name, const char *homedir, 
-                    const char *shell, uint32 group_rid) = c;
+                    const char *shell, uint32 group_rid) =
+               (void (*)(void *, BOOL, const char *, const char *,
+                         const char *, const char *, uint32))c;
 
        if (!success) {
                DEBUG(5, ("Could not trigger query_user\n"));
@@ -1455,5 +1536,125 @@ void query_user_async(TALLOC_CTX *mem_ctx, struct winbindd_domain *domain,
        request.cmd = WINBINDD_DUAL_USERINFO;
        sid_to_string(request.data.sid, sid);
        do_async_domain(mem_ctx, domain, &request, query_user_recv,
-                       cont, private_data);
+                       (void *)cont, private_data);
+}
+
+/* The following uid2sid/gid2sid functions has been contributed by
+ * Keith Reynolds <Keith.Reynolds@centrify.com> */
+
+static void winbindd_uid2sid_recv(TALLOC_CTX *mem_ctx, BOOL success,
+                                 struct winbindd_response *response,
+                                 void *c, void *private_data)
+{
+       void (*cont)(void *priv, BOOL succ, const char *sid) =
+               (void (*)(void *, BOOL, const char *))c;
+
+       if (!success) {
+               DEBUG(5, ("Could not trigger uid2sid\n"));
+               cont(private_data, False, NULL);
+               return;
+       }
+
+       if (response->result != WINBINDD_OK) {
+               DEBUG(5, ("uid2sid returned an error\n"));
+               cont(private_data, False, NULL);
+               return;
+       }
+
+       cont(private_data, True, response->data.sid.sid);
+}
+
+void winbindd_uid2sid_async(TALLOC_CTX *mem_ctx, uid_t uid,
+                           void (*cont)(void *private_data, BOOL success, const char *sid),
+                           void *private_data)
+{
+       struct winbindd_request request;
+
+       ZERO_STRUCT(request);
+       request.cmd = WINBINDD_DUAL_UID2SID;
+       request.data.uid = uid;
+       do_async(mem_ctx, idmap_child(), &request, winbindd_uid2sid_recv,
+                (void *)cont, private_data);
+}
+
+enum winbindd_result winbindd_dual_uid2sid(struct winbindd_domain *domain,
+                                          struct winbindd_cli_state *state)
+{
+       DOM_SID sid;
+       NTSTATUS result;
+
+       DEBUG(3,("[%5lu]: uid to sid %lu\n",
+                (unsigned long)state->pid,
+                (unsigned long) state->request.data.uid));
+
+       /* Find sid for this uid and return it, possibly ask the slow remote idmap */
+       result = idmap_uid_to_sid(&sid, state->request.data.uid, IDMAP_FLAG_NONE);
+
+       if (NT_STATUS_IS_OK(result)) {
+               sid_to_string(state->response.data.sid.sid, &sid);
+               state->response.data.sid.type = SID_NAME_USER;
+               return WINBINDD_OK;
+       }
+
+       return WINBINDD_ERROR;
+}
+
+static void winbindd_gid2sid_recv(TALLOC_CTX *mem_ctx, BOOL success,
+                                 struct winbindd_response *response,
+                                 void *c, void *private_data)
+{
+       void (*cont)(void *priv, BOOL succ, const char *sid) =
+               (void (*)(void *, BOOL, const char *))c;
+
+       if (!success) {
+               DEBUG(5, ("Could not trigger gid2sid\n"));
+               cont(private_data, False, NULL);
+               return;
+       }
+
+       if (response->result != WINBINDD_OK) {
+               DEBUG(5, ("gid2sid returned an error\n"));
+               cont(private_data, False, NULL);
+               return;
+       }
+
+       cont(private_data, True, response->data.sid.sid);
+}
+
+void winbindd_gid2sid_async(TALLOC_CTX *mem_ctx, gid_t gid,
+                           void (*cont)(void *private_data, BOOL success, const char *sid),
+                           void *private_data)
+{
+       struct winbindd_request request;
+
+       ZERO_STRUCT(request);
+       request.cmd = WINBINDD_DUAL_GID2SID;
+       request.data.gid = gid;
+       do_async(mem_ctx, idmap_child(), &request, winbindd_gid2sid_recv,
+                (void *)cont, private_data);
+}
+
+enum winbindd_result winbindd_dual_gid2sid(struct winbindd_domain *domain,
+                                          struct winbindd_cli_state *state)
+{
+       DOM_SID sid;
+       NTSTATUS result;
+
+       DEBUG(3,("[%5lu]: gid %lu to sid\n",
+               (unsigned long)state->pid,
+               (unsigned long) state->request.data.gid));
+
+       /* Find sid for this gid and return it, possibly ask the slow remote idmap */
+       result = idmap_gid_to_sid(&sid, state->request.data.gid, IDMAP_FLAG_NONE);
+
+       if (NT_STATUS_IS_OK(result)) {
+               sid_to_string(state->response.data.sid.sid, &sid);
+               DEBUG(10, ("[%5lu]: retrieved sid: %s\n",
+                          (unsigned long)state->pid,
+                          state->response.data.sid.sid));
+               state->response.data.sid.type = SID_NAME_DOM_GRP;
+               return WINBINDD_OK;
+       }
+
+       return WINBINDD_ERROR;
 }