s3: Fix but 7145 -- duplicate sam and unix accounts
[amitay/samba.git] / source3 / winbindd / wb_next_grent.c
1 /*
2    Unix SMB/CIFS implementation.
3    async next_grent
4    Copyright (C) Volker Lendecke 2009
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "librpc/gen_ndr/cli_wbint.h"
23
24 struct wb_next_grent_state {
25         struct tevent_context *ev;
26         int max_nesting;
27         struct getgrent_state *gstate;
28         struct wbint_Principals next_groups;
29         struct winbindd_gr *gr;
30         struct talloc_dict *members;
31 };
32
33 static void wb_next_grent_fetch_done(struct tevent_req *subreq);
34 static void wb_next_grent_getgrsid_done(struct tevent_req *subreq);
35
36 struct tevent_req *wb_next_grent_send(TALLOC_CTX *mem_ctx,
37                                       struct tevent_context *ev,
38                                       int max_nesting,
39                                       struct getgrent_state *gstate,
40                                       struct winbindd_gr *gr)
41 {
42         struct tevent_req *req, *subreq;
43         struct wb_next_grent_state *state;
44
45         req = tevent_req_create(mem_ctx, &state, struct wb_next_grent_state);
46         if (req == NULL) {
47                 return NULL;
48         }
49         state->ev = ev;
50         state->gstate = gstate;
51         state->gr = gr;
52
53         if (state->gstate->next_group >= state->gstate->num_groups) {
54                 TALLOC_FREE(state->gstate->groups);
55
56                 if (state->gstate->domain == NULL) {
57                         state->gstate->domain = domain_list();
58                 } else {
59                         state->gstate->domain = state->gstate->domain->next;
60                 }
61
62                 if ((state->gstate->domain != NULL)
63                     && sid_check_is_domain(&state->gstate->domain->sid)) {
64                         state->gstate->domain = state->gstate->domain->next;
65                 }
66
67                 if (state->gstate->domain == NULL) {
68                         tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
69                         return tevent_req_post(req, ev);
70                 }
71                 subreq = rpccli_wbint_QueryGroupList_send(
72                         state, state->ev, state->gstate->domain->child.rpccli,
73                         &state->next_groups);
74                 if (tevent_req_nomem(subreq, req)) {
75                         return tevent_req_post(req, ev);
76                 }
77                 tevent_req_set_callback(subreq, wb_next_grent_fetch_done, req);
78                 return req;
79         }
80
81         subreq = wb_getgrsid_send(
82                 state, state->ev,
83                 &state->gstate->groups[state->gstate->next_group].sid,
84                 state->max_nesting);
85         if (tevent_req_nomem(subreq, req)) {
86                 return tevent_req_post(req, ev);
87         }
88         tevent_req_set_callback(subreq, wb_next_grent_getgrsid_done, req);
89         return req;
90 }
91
92 static void wb_next_grent_fetch_done(struct tevent_req *subreq)
93 {
94         struct tevent_req *req = tevent_req_callback_data(
95                 subreq, struct tevent_req);
96         struct wb_next_grent_state *state = tevent_req_data(
97                 req, struct wb_next_grent_state);
98         NTSTATUS status, result;
99
100         status = rpccli_wbint_QueryGroupList_recv(subreq, state, &result);
101         TALLOC_FREE(subreq);
102         if (!NT_STATUS_IS_OK(status) || !NT_STATUS_IS_OK(result)) {
103                 /* Ignore errors here, just log it */
104                 DEBUG(10, ("query_user_list for domain %s returned %s/%s\n",
105                            state->gstate->domain->name,
106                            nt_errstr(status), nt_errstr(result)));
107                 tevent_req_nterror(req, result);
108                 return;
109         }
110
111         state->gstate->num_groups = state->next_groups.num_principals;
112         state->gstate->groups = talloc_move(
113                 state->gstate, &state->next_groups.principals);
114
115         if (state->gstate->num_groups == 0) {
116                 state->gstate->domain = state->gstate->domain->next;
117
118                 if ((state->gstate->domain != NULL)
119                     && sid_check_is_domain(&state->gstate->domain->sid)) {
120                         state->gstate->domain = state->gstate->domain->next;
121                 }
122
123                 if (state->gstate->domain == NULL) {
124                         tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
125                         return;
126                 }
127                 subreq = rpccli_wbint_QueryGroupList_send(
128                         state, state->ev, state->gstate->domain->child.rpccli,
129                         &state->next_groups);
130                 if (tevent_req_nomem(subreq, req)) {
131                         return;
132                 }
133                 tevent_req_set_callback(subreq, wb_next_grent_fetch_done, req);
134                 return;
135         }
136
137         state->gstate->next_group = 0;
138
139         subreq = wb_getgrsid_send(
140                 state, state->ev,
141                 &state->gstate->groups[state->gstate->next_group].sid,
142                 state->max_nesting);
143         if (tevent_req_nomem(subreq, req)) {
144                 return;
145         }
146         tevent_req_set_callback(subreq, wb_next_grent_getgrsid_done, req);
147         return;
148 }
149
150 static void wb_next_grent_getgrsid_done(struct tevent_req *subreq)
151 {
152         struct tevent_req *req = tevent_req_callback_data(
153                 subreq, struct tevent_req);
154         struct wb_next_grent_state *state = tevent_req_data(
155                 req, struct wb_next_grent_state);
156         const char *domname, *name;
157         NTSTATUS status;
158
159         status = wb_getgrsid_recv(subreq, talloc_tos(), &domname, &name,
160                                   &state->gr->gr_gid, &state->members);
161         TALLOC_FREE(subreq);
162         if (!NT_STATUS_IS_OK(status)) {
163                 tevent_req_nterror(req, status);
164                 return;
165         }
166         if (!fill_grent(talloc_tos(), state->gr, domname, name,
167                         state->gr->gr_gid)) {
168                 DEBUG(5, ("fill_grent failed\n"));
169                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
170                 return;
171         }
172         state->gstate->next_group += 1;
173         tevent_req_done(req);
174 }
175
176 NTSTATUS wb_next_grent_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
177                             struct talloc_dict **members)
178 {
179         struct wb_next_grent_state *state = tevent_req_data(
180                 req, struct wb_next_grent_state);
181         NTSTATUS status;
182
183         if (tevent_req_is_nterror(req, &status)) {
184                 return status;
185         }
186         *members = talloc_move(mem_ctx, &state->members);
187         return NT_STATUS_OK;
188 }