s3:winbind: Convert WINBINDD_GETGRGID to the new API
[metze/samba/wip.git] / source3 / winbindd / winbindd_group.c
index 6ad93adf4a2049c33c2bc97ac440acd26d3f995a..f33b52f83fe15115622b1a056db9585463d9eb65 100644 (file)
@@ -157,8 +157,8 @@ static bool fill_passdb_alias_grmem(struct winbindd_domain *domain,
        *gr_mem = NULL;
        *gr_mem_len = 0;
 
-       if (!NT_STATUS_IS_OK(pdb_enum_aliasmem(group_sid, &members,
-                                              &num_members)))
+       if (!NT_STATUS_IS_OK(pdb_enum_aliasmem(group_sid, talloc_tos(),
+                                              &members, &num_members)))
                return True;
 
        for (i=0; i<num_members; i++) {
@@ -181,9 +181,8 @@ static bool fill_passdb_alias_grmem(struct winbindd_domain *domain,
 
 /* Fill a grent structure from various other information */
 
-static bool fill_grent(TALLOC_CTX *mem_ctx, struct winbindd_gr *gr,
-                      const char *dom_name,
-                      char *gr_name, gid_t unix_gid)
+bool fill_grent(TALLOC_CTX *mem_ctx, struct winbindd_gr *gr,
+               const char *dom_name, const char *gr_name, gid_t unix_gid)
 {
        fstring full_group_name;
        char *mapped_name = NULL;
@@ -460,17 +459,36 @@ static NTSTATUS expand_groups( TALLOC_CTX *ctx,
        *new_glist = NULL;
        *n_new_glist = 0;
 
+       DEBUG(10,("expand_groups:\n"));
+
        for ( i=0; i<n_glist; i++ ) {
+
+               NTSTATUS lookup_status;
+
                tmp_ctx = talloc_new( ctx );
 
                /* Lookup the group membership */
 
-               status = d->methods->lookup_groupmem(d, tmp_ctx,
+               lookup_status = d->methods->lookup_groupmem(d, tmp_ctx,
                                                     &glist[i], &num_names,
                                                     &sid_mem, &names,
                                                     &name_types);
-               if ( !NT_STATUS_IS_OK(status) )
+               if (!NT_STATUS_IS_OK(lookup_status)) {
+                       DEBUG(10,("expand_groups: lookup_groupmem for "
+                               "sid %s failed with: %s\n",
+                               sid_string_dbg(&glist[i]),
+                               nt_errstr(lookup_status)));
+
+                       /* we might have hit a logic error when called for an
+                        * alias, in that case just continue with group
+                        * expansion - Guenther */
+
+                       if (NT_STATUS_EQUAL(lookup_status, NT_STATUS_NO_SUCH_GROUP)) {
+                               continue;
+                       }
+                       status = lookup_status;
                        goto out;
+               }
 
                /* Separate users and groups into two lists */
 
@@ -514,6 +532,11 @@ static NTSTATUS expand_groups( TALLOC_CTX *ctx,
  out:
        TALLOC_FREE( tmp_ctx );
 
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(10,("expand_groups: returning with %s\n",
+                       nt_errstr(status)));
+       }
+
        return status;
 }
 
@@ -726,7 +749,8 @@ done:
 
        talloc_destroy(mem_ctx);
 
-       DEBUG(10, ("fill_grent_mem returning %d\n", result));
+       DEBUG(10,("fill_grent_mem returning %s\n",
+               result == true ? "true" : "false"));
 
        return result;
 }
@@ -764,20 +788,20 @@ void winbindd_getgrnam(struct winbindd_cli_state *state)
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
 
        /* Ensure null termination */
-       state->request.data.groupname[sizeof(state->request.data.groupname)-1]='\0';
+       state->request->data.groupname[sizeof(state->request->data.groupname)-1]='\0';
 
        DEBUG(3, ("[%5lu]: getgrnam %s\n", (unsigned long)state->pid,
-                 state->request.data.groupname));
+                 state->request->data.groupname));
 
        nt_status = normalize_name_unmap(state->mem_ctx,
-                                        state->request.data.groupname,
+                                        state->request->data.groupname,
                                         &tmp);
        /* If we didn't map anything in the above call, just reset the
           tmp pointer to the original string */
        if (!NT_STATUS_IS_OK(nt_status) &&
            !NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED))
        {
-               tmp = state->request.data.groupname;
+               tmp = state->request->data.groupname;
        }
 
        /* Parse domain and groupname */
@@ -863,7 +887,7 @@ static void getgrsid_sid2gid_recv(void *private_data, bool success, gid_t gid)
                return;
        }
 
-       if (!fill_grent(s->state->mem_ctx, &s->state->response.data.gr,
+       if (!fill_grent(s->state->mem_ctx, &s->state->response->data.gr,
                        dom_name, group_name, gid) ||
            !fill_grent_mem(domain, s->state, &s->group_sid, s->group_type,
                            &num_gr_mem, &gr_mem, &gr_mem_len))
@@ -872,14 +896,19 @@ static void getgrsid_sid2gid_recv(void *private_data, bool success, gid_t gid)
                return;
        }
 
-       s->state->response.data.gr.num_gr_mem = (uint32)num_gr_mem;
+       s->state->response->data.gr.num_gr_mem = (uint32)num_gr_mem;
 
        /* Group membership lives at start of extra data */
 
-       s->state->response.data.gr.gr_mem_ofs = 0;
+       s->state->response->data.gr.gr_mem_ofs = 0;
 
-       s->state->response.length += gr_mem_len;
-       s->state->response.extra_data.data = gr_mem;
+       s->state->response->length += gr_mem_len;
+       s->state->response->extra_data.data = talloc_memdup(
+               s->state->mem_ctx, gr_mem, gr_mem_len);
+       if (s->state->response->extra_data.data == NULL) {
+               request_error(s->state);
+               return;
+       }
 
        request_ok(s->state);
 }
@@ -978,57 +1007,6 @@ static void winbindd_getgrsid( struct winbindd_cli_state *state, const DOM_SID g
                                  getgrsid_lookupsid_recv, s );
 }
 
-
-static void getgrgid_recv(void *private_data, bool success, const char *sid)
-{
-       struct winbindd_cli_state *state = talloc_get_type_abort(private_data, struct winbindd_cli_state);
-       enum lsa_SidType name_type;
-       DOM_SID group_sid;
-
-       if (success) {
-               DEBUG(10,("getgrgid_recv: gid %lu has sid %s\n",
-                         (unsigned long)(state->request.data.gid), sid));
-
-               if (!string_to_sid(&group_sid, sid)) {
-                       DEBUG(1,("getgrgid_recv: Could not convert sid %s "
-                               "from string\n", sid));
-                       request_error(state);
-                       return;
-               }
-
-               winbindd_getgrsid(state, group_sid);
-               return;
-       }
-
-       /* Ok, this might be "ours", i.e. an alias */
-       if (pdb_gid_to_sid(state->request.data.gid, &group_sid) &&
-           lookup_sid(state->mem_ctx, &group_sid, NULL, NULL, &name_type) &&
-           (name_type == SID_NAME_ALIAS)) {
-               /* Hey, got an alias */
-               DEBUG(10,("getgrgid_recv: we have an alias with gid %lu and sid %s\n",
-                         (unsigned long)(state->request.data.gid), sid));
-               winbindd_getgrsid(state, group_sid);
-               return;
-       }
-
-       DEBUG(1, ("could not convert gid %lu to sid\n",
-                 (unsigned long)state->request.data.gid));
-       request_error(state);
-}
-
-/* Return a group structure from a gid number */
-void winbindd_getgrgid(struct winbindd_cli_state *state)
-{
-       gid_t gid = state->request.data.gid;
-
-       DEBUG(3, ("[%5lu]: getgrgid %lu\n",
-                 (unsigned long)state->pid,
-                 (unsigned long)gid));
-
-       /* always use the async interface */
-       winbindd_gid2sid_async(state->mem_ctx, gid, getgrgid_recv, state);
-}
-
 /*
  * set/get/endgrent functions
  */
@@ -1265,25 +1243,22 @@ void winbindd_getgrent(struct winbindd_cli_state *state)
                return;
        }
 
-       num_groups = MIN(MAX_GETGRENT_GROUPS, state->request.data.num_entries);
+       num_groups = MIN(MAX_GETGRENT_GROUPS, state->request->data.num_entries);
 
        if (num_groups == 0) {
                request_error(state);
                return;
        }
 
-       group_list = SMB_MALLOC_ARRAY(struct winbindd_gr, num_groups);
+       group_list = talloc_zero_array(state->mem_ctx, struct winbindd_gr,
+                                      num_groups);
        if (!group_list) {
                request_error(state);
                return;
        }
-       /* will be freed by process_request() */
-       state->response.extra_data.data = group_list;
-
-       memset(state->response.extra_data.data, '\0',
-               num_groups * sizeof(struct winbindd_gr) );
+       state->response->extra_data.data = group_list;
 
-       state->response.data.num_entries = 0;
+       state->response->data.num_entries = 0;
 
        if (!state->getgrent_initialized)
                winbindd_setgrent_internal(state);
@@ -1404,7 +1379,7 @@ void winbindd_getgrent(struct winbindd_cli_state *state)
                        gr_mem_len = 0;
 
                        /* Get group membership */
-                       if (state->request.cmd == WINBINDD_GETGRLST) {
+                       if (state->request->cmd == WINBINDD_GETGRLST) {
                                result = True;
                        } else {
                                sid_copy(&member_sid, &domain->sid);
@@ -1454,12 +1429,12 @@ void winbindd_getgrent(struct winbindd_cli_state *state)
                if (result) {
 
                        DEBUG(10, ("adding group num_entries = %d\n",
-                                  state->response.data.num_entries));
+                                  state->response->data.num_entries));
 
                        group_list_ndx++;
-                       state->response.data.num_entries++;
+                       state->response->data.num_entries++;
 
-                       state->response.length +=
+                       state->response->length +=
                                sizeof(struct winbindd_gr);
 
                } else {
@@ -1473,11 +1448,11 @@ void winbindd_getgrent(struct winbindd_cli_state *state)
        if (group_list_ndx == 0)
                goto done;
 
-       state->response.extra_data.data = SMB_REALLOC(
-               state->response.extra_data.data,
+       state->response->extra_data.data = talloc_realloc_size(
+               state->mem_ctx, state->response->extra_data.data,
                group_list_ndx * sizeof(struct winbindd_gr) + gr_mem_list_len);
 
-       if (!state->response.extra_data.data) {
+       if (!state->response->extra_data.data) {
                DEBUG(0, ("out of memory\n"));
                group_list_ndx = 0;
                SAFE_FREE(gr_mem_list);
@@ -1485,11 +1460,11 @@ void winbindd_getgrent(struct winbindd_cli_state *state)
                return;
        }
 
-       memcpy(&((char *)state->response.extra_data.data)
+       memcpy(&((char *)state->response->extra_data.data)
               [group_list_ndx * sizeof(struct winbindd_gr)],
               gr_mem_list, gr_mem_list_len);
 
-       state->response.length += gr_mem_list_len;
+       state->response->length += gr_mem_list_len;
 
        DEBUG(10, ("returning %d groups, length = %d\n",
                   group_list_ndx, gr_mem_list_len));
@@ -1532,181 +1507,6 @@ struct getgroups_state {
        size_t num_token_gids;
 };
 
-static void getgroups_usersid_recv(void *private_data, bool success,
-                                  const DOM_SID *sid, enum lsa_SidType type);
-static void getgroups_tokensids_recv(void *private_data, bool success,
-                                    DOM_SID *token_sids, size_t num_token_sids);
-static void getgroups_sid2gid_recv(void *private_data, bool success, gid_t gid);
-
-void winbindd_getgroups(struct winbindd_cli_state *state)
-{
-       struct getgroups_state *s;
-       char *real_name = NULL;
-       NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
-
-       /* Ensure null termination */
-       state->request.data.username
-               [sizeof(state->request.data.username)-1]='\0';
-
-       DEBUG(3, ("[%5lu]: getgroups %s\n", (unsigned long)state->pid,
-                 state->request.data.username));
-
-       /* Parse domain and username */
-
-       s = TALLOC_P(state->mem_ctx, struct getgroups_state);
-       if (s == NULL) {
-               DEBUG(0, ("talloc failed\n"));
-               request_error(state);
-               return;
-       }
-
-       s->state = state;
-
-       nt_status = normalize_name_unmap(state->mem_ctx,
-                                        state->request.data.username,
-                                        &real_name);
-
-       /* Reset the real_name pointer if we didn't do anything
-          productive in the above call */
-       if (!NT_STATUS_IS_OK(nt_status) &&
-           !NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED))
-       {
-               real_name = state->request.data.username;
-       }
-
-       if (!parse_domain_user_talloc(state->mem_ctx, real_name,
-                                     &s->domname, &s->username)) {
-               DEBUG(5, ("Could not parse domain user: %s\n",
-                         real_name));
-
-               /* error out if we do not have nested group support */
-
-               if ( !lp_winbind_nested_groups() ) {
-                       request_error(state);
-                       return;
-               }
-
-               s->domname = talloc_strdup(state->mem_ctx,
-                                          get_global_sam_name());
-               s->username = talloc_strdup(state->mem_ctx,
-                                           state->request.data.username);
-       }
-
-       /* Get info for the domain (either by short domain name or
-          DNS name in the case of a UPN) */
-
-       s->domain = find_domain_from_name_noinit(s->domname);
-       if (!s->domain) {
-               char *p = strchr(s->username, '@');
-
-               if (p) {
-                       s->domain = find_domain_from_name_noinit(p+1);
-               }
-
-       }
-
-       if (s->domain == NULL) {
-               DEBUG(7, ("could not find domain entry for domain %s\n",
-                         s->domname));
-               request_error(state);
-               return;
-       }
-
-       if ( s->domain->primary && lp_winbind_trusted_domains_only()) {
-               DEBUG(7,("winbindd_getgroups: My domain -- rejecting "
-                        "getgroups() for %s\\%s.\n", s->domname,
-                        s->username));
-               request_error(state);
-               return;
-       }
-
-       /* Get rid and name type from name.  The following costs 1 packet */
-
-       winbindd_lookupname_async(state->mem_ctx,
-                                 s->domname, s->username,
-                                 getgroups_usersid_recv,
-                                 WINBINDD_GETGROUPS, s);
-}
-
-static void getgroups_usersid_recv(void *private_data, bool success,
-                                  const DOM_SID *sid, enum lsa_SidType type)
-{
-       struct getgroups_state *s =
-               (struct getgroups_state *)private_data;
-
-       if ((!success) ||
-           ((type != SID_NAME_USER) && (type != SID_NAME_COMPUTER))) {
-               request_error(s->state);
-               return;
-       }
-
-       sid_copy(&s->user_sid, sid);
-
-       winbindd_gettoken_async(s->state->mem_ctx, &s->user_sid,
-                               getgroups_tokensids_recv, s);
-}
-
-static void getgroups_tokensids_recv(void *private_data, bool success,
-                                    DOM_SID *token_sids, size_t num_token_sids)
-{
-       struct getgroups_state *s =
-               (struct getgroups_state *)private_data;
-
-       /* We need at least the user sid and the primary group in the token,
-        * otherwise it's an error */
-
-       if ((!success) || (num_token_sids < 2)) {
-               request_error(s->state);
-               return;
-       }
-
-       s->token_sids = token_sids;
-       s->num_token_sids = num_token_sids;
-       s->i = 0;
-
-       s->token_gids = NULL;
-       s->num_token_gids = 0;
-
-       getgroups_sid2gid_recv(s, False, 0);
-}
-
-static void getgroups_sid2gid_recv(void *private_data, bool success, gid_t gid)
-{
-       struct getgroups_state *s =
-               (struct getgroups_state *)private_data;
-
-       if (success) {
-               if (!add_gid_to_array_unique(s->state->mem_ctx, gid,
-                                       &s->token_gids,
-                                       &s->num_token_gids)) {
-                       return;
-               }
-       }
-
-       if (s->i < s->num_token_sids) {
-               const DOM_SID *sid = &s->token_sids[s->i];
-               s->i += 1;
-
-               if (sid_equal(sid, &s->user_sid)) {
-                       getgroups_sid2gid_recv(s, False, 0);
-                       return;
-               }
-
-               winbindd_sid2gid_async(s->state->mem_ctx, sid,
-                                      getgroups_sid2gid_recv, s);
-               return;
-       }
-
-       s->state->response.data.num_entries = s->num_token_gids;
-       if (s->num_token_gids) {
-               /* s->token_gids are talloced */
-               s->state->response.extra_data.data =
-                       smb_xmemdup(s->token_gids,
-                                       s->num_token_gids * sizeof(gid_t));
-               s->state->response.length += s->num_token_gids * sizeof(gid_t);
-       }
-       request_ok(s->state);
-}
 
 /* Get user supplementary sids. This is equivalent to the
    winbindd_getgroups() function but it involves a SID->SIDs mapping
@@ -1728,7 +1528,7 @@ void winbindd_getusersids(struct winbindd_cli_state *state)
        DOM_SID *user_sid;
 
        /* Ensure null termination */
-       state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
+       state->request->data.sid[sizeof(state->request->data.sid)-1]='\0';
 
        user_sid = TALLOC_P(state->mem_ctx, DOM_SID);
        if (user_sid == NULL) {
@@ -1737,9 +1537,9 @@ void winbindd_getusersids(struct winbindd_cli_state *state)
                return;
        }
 
-       if (!string_to_sid(user_sid, state->request.data.sid)) {
+       if (!string_to_sid(user_sid, state->request->data.sid)) {
                DEBUG(1, ("Could not get convert sid %s from string\n",
-                         state->request.data.sid));
+                         state->request->data.sid));
                request_error(state);
                return;
        }
@@ -1770,7 +1570,7 @@ static void getusersids_recv(void *private_data, bool success, DOM_SID *sids,
        }
 
        /* build the reply */
-       ret = (char *)SMB_MALLOC(ret_size);
+       ret = talloc_array(state->mem_ctx, char, ret_size);
        if (!ret) {
                DEBUG(0, ("malloc failed\n"));
                request_error(state);
@@ -1785,38 +1585,12 @@ static void getusersids_recv(void *private_data, bool success, DOM_SID *sids,
        }
 
        /* Send data back to client */
-       state->response.data.num_entries = num_sids;
-       state->response.extra_data.data = ret;
-       state->response.length += ret_size;
+       state->response->data.num_entries = num_sids;
+       state->response->extra_data.data = ret;
+       state->response->length += ret_size;
        request_ok(state);
 }
 
-void winbindd_getuserdomgroups(struct winbindd_cli_state *state)
-{
-       DOM_SID user_sid;
-       struct winbindd_domain *domain;
-
-       /* Ensure null termination */
-       state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
-
-       if (!string_to_sid(&user_sid, state->request.data.sid)) {
-               DEBUG(1, ("Could not get convert sid %s from string\n",
-                         state->request.data.sid));
-               request_error(state);
-               return;
-       }
-
-       /* Get info for the domain */
-       if ((domain = find_domain_from_sid_noinit(&user_sid)) == NULL) {
-               DEBUG(0,("could not find domain entry for sid %s\n",
-                        sid_string_dbg(&user_sid)));
-               request_error(state);
-               return;
-       }
-
-       sendto_domain(state, domain);
-}
-
 enum winbindd_result winbindd_dual_getuserdomgroups(struct winbindd_domain *domain,
                                                    struct winbindd_cli_state *state)
 {
@@ -1829,11 +1603,11 @@ enum winbindd_result winbindd_dual_getuserdomgroups(struct winbindd_domain *doma
        uint32 num_groups;
 
        /* Ensure null termination */
-       state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
+       state->request->data.sid[sizeof(state->request->data.sid)-1]='\0';
 
-       if (!string_to_sid(&user_sid, state->request.data.sid)) {
+       if (!string_to_sid(&user_sid, state->request->data.sid)) {
                DEBUG(1, ("Could not get convert sid %s from string\n",
-                         state->request.data.sid));
+                         state->request->data.sid));
                return WINBINDD_ERROR;
        }
 
@@ -1844,8 +1618,8 @@ enum winbindd_result winbindd_dual_getuserdomgroups(struct winbindd_domain *doma
                return WINBINDD_ERROR;
 
        if (num_groups == 0) {
-               state->response.data.num_entries = 0;
-               state->response.extra_data.data = NULL;
+               state->response->data.num_entries = 0;
+               state->response->extra_data.data = NULL;
                return WINBINDD_OK;
        }
 
@@ -1856,42 +1630,13 @@ enum winbindd_result winbindd_dual_getuserdomgroups(struct winbindd_domain *doma
                return WINBINDD_ERROR;
        }
 
-       state->response.extra_data.data = SMB_STRDUP(sidstring);
-       if (!state->response.extra_data.data) {
-               return WINBINDD_ERROR;
-       }
-       state->response.length += len+1;
-       state->response.data.num_entries = num_groups;
+       state->response->extra_data.data = sidstring;
+       state->response->length += len+1;
+       state->response->data.num_entries = num_groups;
 
        return WINBINDD_OK;
 }
 
-void winbindd_getsidaliases(struct winbindd_cli_state *state)
-{
-       DOM_SID domain_sid;
-       struct winbindd_domain *domain;
-
-       /* Ensure null termination */
-       state->request.data.sid[sizeof(state->request.data.sid)-1]='\0';
-
-       if (!string_to_sid(&domain_sid, state->request.data.sid)) {
-               DEBUG(1, ("Could not get convert sid %s from string\n",
-                         state->request.data.sid));
-               request_error(state);
-               return;
-       }
-
-       /* Get info for the domain */
-       if ((domain = find_domain_from_sid_noinit(&domain_sid)) == NULL) {
-               DEBUG(0,("could not find domain entry for sid %s\n",
-                        sid_string_dbg(&domain_sid)));
-               request_error(state);
-               return;
-       }
-
-       sendto_domain(state, domain);
-}
-
 enum winbindd_result winbindd_dual_getsidaliases(struct winbindd_domain *domain,
                                                 struct winbindd_cli_state *state)
 {
@@ -1906,7 +1651,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.data;
+       sidstr = state->request->extra_data.data;
        if (sidstr == NULL) {
                sidstr = talloc_strdup(state->mem_ctx, "\n"); /* No SID */
                if (!sidstr) {
@@ -1958,25 +1703,93 @@ enum winbindd_result winbindd_dual_getsidaliases(struct winbindd_domain *domain,
 
        if (!print_sidlist(state->mem_ctx, sids, num_sids, &sidstr, &len)) {
                DEBUG(0, ("Could not print_sidlist\n"));
-               state->response.extra_data.data = NULL;
+               state->response->extra_data.data = NULL;
                return WINBINDD_ERROR;
        }
 
-       state->response.extra_data.data = NULL;
+       state->response->extra_data.data = NULL;
 
        if (sidstr) {
-               state->response.extra_data.data = SMB_STRDUP(sidstr);
-               if (!state->response.extra_data.data) {
-                       DEBUG(0, ("Out of memory\n"));
-                       return WINBINDD_ERROR;
-               }
+               state->response->extra_data.data = sidstr;
                DEBUG(10, ("aliases_list: %s\n",
-                          (char *)state->response.extra_data.data));
-               state->response.length += len+1;
-               state->response.data.num_entries = num_sids;
+                          (char *)state->response->extra_data.data));
+               state->response->length += len+1;
+               state->response->data.num_entries = num_sids;
        }
 
        return WINBINDD_OK;
 }
 
+struct getgr_countmem {
+       int num;
+       size_t len;
+};
+
+static int getgr_calc_memberlen(DATA_BLOB key, void *data, void *priv)
+{
+       struct wbint_GroupMember *m = talloc_get_type_abort(
+               data, struct wbint_GroupMember);
+       struct getgr_countmem *buf = (struct getgr_countmem *)priv;
 
+       buf->num += 1;
+       buf->len += strlen(m->name) + 1;
+       return 0;
+}
+
+struct getgr_stringmem {
+       size_t ofs;
+       char *buf;
+};
+
+static int getgr_unparse_members(DATA_BLOB key, void *data, void *priv)
+{
+       struct wbint_GroupMember *m = talloc_get_type_abort(
+               data, struct wbint_GroupMember);
+       struct getgr_stringmem *buf = (struct getgr_stringmem *)priv;
+       int len;
+
+       len = strlen(m->name);
+
+       memcpy(buf->buf + buf->ofs, m->name, len);
+       buf->ofs += len;
+       buf->buf[buf->ofs] = ',';
+       buf->ofs += 1;
+       return 0;
+}
+
+NTSTATUS winbindd_print_groupmembers(struct talloc_dict *members,
+                                    TALLOC_CTX *mem_ctx,
+                                    int *num_members, char **result)
+{
+       struct getgr_countmem c;
+       struct getgr_stringmem m;
+       int res;
+
+       c.num = 0;
+       c.len = 0;
+
+       res = talloc_dict_traverse(members, getgr_calc_memberlen, &c);
+       if (res != 0) {
+               DEBUG(5, ("talloc_dict_traverse failed\n"));
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       m.ofs = 0;
+       m.buf = talloc_array(mem_ctx, char, c.len);
+       if (m.buf == NULL) {
+               DEBUG(5, ("talloc failed\n"));
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       res = talloc_dict_traverse(members, getgr_unparse_members, &m);
+       if (res != 0) {
+               DEBUG(5, ("talloc_dict_traverse failed\n"));
+               TALLOC_FREE(m.buf);
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+       m.buf[c.len-1] = '\0';
+
+       *num_members = c.num;
+       *result = m.buf;
+       return NT_STATUS_OK;
+}