s3-passdb: use passdb headers where needed.
[Samba.git] / source3 / winbindd / wb_next_grent.c
blobc7af6416c49a5a78d8f513a070a13a9d14913a42
1 /*
2 Unix SMB/CIFS implementation.
3 async next_grent
4 Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "librpc/gen_ndr/ndr_wbint_c.h"
23 #include "passdb/machine_sid.h"
25 struct wb_next_grent_state {
26 struct tevent_context *ev;
27 int max_nesting;
28 struct getgrent_state *gstate;
29 struct wbint_Principals next_groups;
30 struct winbindd_gr *gr;
31 struct talloc_dict *members;
34 static void wb_next_grent_fetch_done(struct tevent_req *subreq);
35 static void wb_next_grent_getgrsid_done(struct tevent_req *subreq);
37 struct tevent_req *wb_next_grent_send(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev,
39 int max_nesting,
40 struct getgrent_state *gstate,
41 struct winbindd_gr *gr)
43 struct tevent_req *req, *subreq;
44 struct wb_next_grent_state *state;
46 req = tevent_req_create(mem_ctx, &state, struct wb_next_grent_state);
47 if (req == NULL) {
48 return NULL;
50 state->ev = ev;
51 state->gstate = gstate;
52 state->gr = gr;
53 state->max_nesting = max_nesting;
55 if (state->gstate->next_group >= state->gstate->num_groups) {
56 TALLOC_FREE(state->gstate->groups);
58 if (state->gstate->domain == NULL) {
59 state->gstate->domain = domain_list();
60 } else {
61 state->gstate->domain = state->gstate->domain->next;
64 if ((state->gstate->domain != NULL)
65 && sid_check_is_domain(&state->gstate->domain->sid)) {
66 state->gstate->domain = state->gstate->domain->next;
69 if (state->gstate->domain == NULL) {
70 tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
71 return tevent_req_post(req, ev);
73 subreq = dcerpc_wbint_QueryGroupList_send(
74 state, state->ev, dom_child_handle(state->gstate->domain),
75 &state->next_groups);
76 if (tevent_req_nomem(subreq, req)) {
77 return tevent_req_post(req, ev);
79 tevent_req_set_callback(subreq, wb_next_grent_fetch_done, req);
80 return req;
83 subreq = wb_getgrsid_send(
84 state, state->ev,
85 &state->gstate->groups[state->gstate->next_group].sid,
86 state->max_nesting);
87 if (tevent_req_nomem(subreq, req)) {
88 return tevent_req_post(req, ev);
90 tevent_req_set_callback(subreq, wb_next_grent_getgrsid_done, req);
91 return req;
94 static void wb_next_grent_fetch_done(struct tevent_req *subreq)
96 struct tevent_req *req = tevent_req_callback_data(
97 subreq, struct tevent_req);
98 struct wb_next_grent_state *state = tevent_req_data(
99 req, struct wb_next_grent_state);
100 NTSTATUS status, result;
102 status = dcerpc_wbint_QueryGroupList_recv(subreq, state, &result);
103 TALLOC_FREE(subreq);
104 if (!NT_STATUS_IS_OK(status)) {
105 /* Ignore errors here, just log it */
106 DEBUG(10, ("query_user_list for domain %s returned %s\n",
107 state->gstate->domain->name,
108 nt_errstr(status)));
109 tevent_req_nterror(req, status);
110 return;
112 if (!NT_STATUS_IS_OK(result)) {
113 /* Ignore errors here, just log it */
114 DEBUG(10, ("query_user_list for domain %s returned %s/%s\n",
115 state->gstate->domain->name,
116 nt_errstr(status), nt_errstr(result)));
117 tevent_req_nterror(req, result);
118 return;
121 state->gstate->num_groups = state->next_groups.num_principals;
122 state->gstate->groups = talloc_move(
123 state->gstate, &state->next_groups.principals);
125 if (state->gstate->num_groups == 0) {
126 state->gstate->domain = state->gstate->domain->next;
128 if ((state->gstate->domain != NULL)
129 && sid_check_is_domain(&state->gstate->domain->sid)) {
130 state->gstate->domain = state->gstate->domain->next;
133 if (state->gstate->domain == NULL) {
134 tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
135 return;
137 subreq = dcerpc_wbint_QueryGroupList_send(
138 state, state->ev, dom_child_handle(state->gstate->domain),
139 &state->next_groups);
140 if (tevent_req_nomem(subreq, req)) {
141 return;
143 tevent_req_set_callback(subreq, wb_next_grent_fetch_done, req);
144 return;
147 state->gstate->next_group = 0;
149 subreq = wb_getgrsid_send(
150 state, state->ev,
151 &state->gstate->groups[state->gstate->next_group].sid,
152 state->max_nesting);
153 if (tevent_req_nomem(subreq, req)) {
154 return;
156 tevent_req_set_callback(subreq, wb_next_grent_getgrsid_done, req);
157 return;
160 static void wb_next_grent_getgrsid_done(struct tevent_req *subreq)
162 struct tevent_req *req = tevent_req_callback_data(
163 subreq, struct tevent_req);
164 struct wb_next_grent_state *state = tevent_req_data(
165 req, struct wb_next_grent_state);
166 const char *domname, *name;
167 NTSTATUS status;
169 status = wb_getgrsid_recv(subreq, talloc_tos(), &domname, &name,
170 &state->gr->gr_gid, &state->members);
171 TALLOC_FREE(subreq);
172 if (!NT_STATUS_IS_OK(status)) {
173 tevent_req_nterror(req, status);
174 return;
176 if (!fill_grent(talloc_tos(), state->gr, domname, name,
177 state->gr->gr_gid)) {
178 DEBUG(5, ("fill_grent failed\n"));
179 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
180 return;
182 state->gstate->next_group += 1;
183 tevent_req_done(req);
186 NTSTATUS wb_next_grent_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
187 struct talloc_dict **members)
189 struct wb_next_grent_state *state = tevent_req_data(
190 req, struct wb_next_grent_state);
191 NTSTATUS status;
193 if (tevent_req_is_nterror(req, &status)) {
194 return status;
196 *members = talloc_move(mem_ctx, &state->members);
197 return NT_STATUS_OK;