static NTSTATUS pdb_samba_dsdb_enum_group_members(struct pdb_methods *m,
					   TALLOC_CTX *mem_ctx,
					   const struct dom_sid *group,
					   uint32_t **pmembers,
					   size_t *pnum_members)
{
	unsigned int i, num_sids, num_members;
	struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
		m->private_data, struct pdb_samba_dsdb_state);
	struct dom_sid *members_as_sids;
	struct dom_sid *dom_sid;
	uint32_t *members;
	struct ldb_dn *dn;
	NTSTATUS status;
	struct dom_sid_buf buf;

	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
	NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);

	dn = ldb_dn_new_fmt(
		tmp_ctx,
		state->ldb,
		"<SID=%s>",
		dom_sid_str_buf(group, &buf));
	if (!dn || !ldb_dn_validate(dn)) {
		return NT_STATUS_NO_MEMORY;
	}

	status = dsdb_enum_group_mem(state->ldb, tmp_ctx, dn, &members_as_sids, &num_sids);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(tmp_ctx);
		return status;
	}
	status = dom_sid_split_rid(tmp_ctx, group, &dom_sid, NULL);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(tmp_ctx);
		return status;
	}

	*pmembers = members = talloc_array(mem_ctx, uint32_t, num_sids);
	if (*pmembers == NULL) {
		TALLOC_FREE(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}
	num_members = 0;

	for (i = 0; i < num_sids; i++) {
		if (!dom_sid_in_domain(dom_sid, &members_as_sids[i])) {
			continue;
		}
		status = dom_sid_split_rid(NULL, &members_as_sids[i],
					   NULL, &members[num_members]);
		if (!NT_STATUS_IS_OK(status)) {
			talloc_free(tmp_ctx);
			return status;
		}
		num_members++;
	}
	*pnum_members = num_members;
	return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_enum_group_members(struct pdb_methods *m,
					   TALLOC_CTX *mem_ctx,
					   const struct dom_sid *group,
					   uint32_t **pmembers,
					   size_t *pnum_members)
{
	unsigned int i, num_sids, num_members;
	struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
		m->private_data, struct pdb_samba_dsdb_state);
	struct dom_sid *members_as_sids;
	struct dom_sid *dom_sid;
	uint32_t *members;
	struct ldb_dn *dn;
	NTSTATUS status;
	struct dom_sid_buf buf;

	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
	NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);

	dn = ldb_dn_new_fmt(
		tmp_ctx,
		state->ldb,
		"<SID=%s>",
		dom_sid_str_buf(group, &buf));
	if (!dn || !ldb_dn_validate(dn)) {
		return NT_STATUS_NO_MEMORY;
	}

	status = dsdb_enum_group_mem(state->ldb, tmp_ctx, dn, &members_as_sids, &num_sids);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(tmp_ctx);
		return status;
	}
	status = dom_sid_split_rid(tmp_ctx, group, &dom_sid, NULL);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(tmp_ctx);
		return status;
	}

	*pmembers = members = talloc_array(mem_ctx, uint32_t, num_sids);
	if (*pmembers == NULL) {
		TALLOC_FREE(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}
	num_members = 0;

	for (i = 0; i < num_sids; i++) {
		if (!dom_sid_in_domain(dom_sid, &members_as_sids[i])) {
			continue;
		}
		status = dom_sid_split_rid(NULL, &members_as_sids[i],
					   NULL, &members[num_members]);
		if (!NT_STATUS_IS_OK(status)) {
			talloc_free(tmp_ctx);
			return status;
		}
		num_members++;
	}
	*pnum_members = num_members;
	return NT_STATUS_OK;
}