s3:winbind: Update winbind to tevent 0.15.0 API
[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/ndr_winbind_c.h"
23 #include "passdb/machine_sid.h"
24
25 struct wb_next_grent_state {
26         struct tevent_context *ev;
27         int max_nesting;
28         struct getgrent_state *gstate;
29         struct winbindd_gr *gr;
30         struct db_context *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 static void wb_next_grent_send_do(struct tevent_req *req,
37                                   struct wb_next_grent_state *state)
38 {
39         struct tevent_req *subreq;
40
41         if (state->gstate->next_group >= state->gstate->num_groups) {
42                 TALLOC_FREE(state->gstate->groups);
43
44                 state->gstate->domain = wb_next_domain(state->gstate->domain);
45                 if (state->gstate->domain == NULL) {
46                         tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
47                         return;
48                 }
49
50                 subreq = wb_query_group_list_send(state, state->ev,
51                                                   state->gstate->domain);
52                 if (tevent_req_nomem(subreq, req)) {
53                         return;
54                 }
55                 tevent_req_set_callback(subreq, wb_next_grent_fetch_done, req);
56                 return;
57         }
58
59         subreq = wb_getgrsid_send(
60                 state, state->ev,
61                 &state->gstate->groups[state->gstate->next_group].sid,
62                 state->max_nesting);
63         if (tevent_req_nomem(subreq, req)) {
64                 return;
65         }
66         tevent_req_set_callback(subreq, wb_next_grent_getgrsid_done, req);
67 }
68
69 struct tevent_req *wb_next_grent_send(TALLOC_CTX *mem_ctx,
70                                       struct tevent_context *ev,
71                                       int max_nesting,
72                                       struct getgrent_state *gstate,
73                                       struct winbindd_gr *gr)
74 {
75         struct tevent_req *req;
76         struct wb_next_grent_state *state;
77
78         req = tevent_req_create(mem_ctx, &state, struct wb_next_grent_state);
79         if (req == NULL) {
80                 return NULL;
81         }
82
83         D_INFO("WB command next_grent start.\n");
84
85         state->ev = ev;
86         state->gstate = gstate;
87         state->gr = gr;
88         state->max_nesting = max_nesting;
89
90         wb_next_grent_send_do(req, state);
91         if (!tevent_req_is_in_progress(req)) {
92                 return tevent_req_post(req, ev);
93         }
94
95         return req;
96 }
97
98 static void wb_next_grent_fetch_done(struct tevent_req *subreq)
99 {
100         struct tevent_req *req = tevent_req_callback_data(
101                 subreq, struct tevent_req);
102         struct wb_next_grent_state *state = tevent_req_data(
103                 req, struct wb_next_grent_state);
104         NTSTATUS status;
105
106         status = wb_query_group_list_recv(subreq, state->gstate,
107                                           &state->gstate->num_groups,
108                                           &state->gstate->groups);
109         TALLOC_FREE(subreq);
110         if (!NT_STATUS_IS_OK(status)) {
111                 /* Ignore errors here, just log it */
112                 D_DEBUG("query_group_list for domain %s returned %s\n",
113                         state->gstate->domain->name, nt_errstr(status));
114                 state->gstate->num_groups = 0;
115         }
116
117         state->gstate->next_group = 0;
118
119         wb_next_grent_send_do(req, state);
120 }
121
122 static void wb_next_grent_getgrsid_done(struct tevent_req *subreq)
123 {
124         struct tevent_req *req = tevent_req_callback_data(
125                 subreq, struct tevent_req);
126         struct wb_next_grent_state *state = tevent_req_data(
127                 req, struct wb_next_grent_state);
128         const char *domname, *name;
129         NTSTATUS status;
130
131         status = wb_getgrsid_recv(subreq, talloc_tos(), &domname, &name,
132                                   &state->gr->gr_gid, &state->members);
133         TALLOC_FREE(subreq);
134
135         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
136                 state->gstate->next_group += 1;
137
138                 wb_next_grent_send_do(req, state);
139
140                 return;
141         } else if (tevent_req_nterror(req, status)) {
142                 return;
143         }
144
145         if (!fill_grent(talloc_tos(), state->gr, domname, name,
146                         state->gr->gr_gid)) {
147                 D_WARNING("fill_grent failed\n");
148                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
149                 return;
150         }
151         state->gstate->next_group += 1;
152         tevent_req_done(req);
153 }
154
155 NTSTATUS wb_next_grent_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
156                             struct db_context **members)
157 {
158         struct wb_next_grent_state *state = tevent_req_data(
159                 req, struct wb_next_grent_state);
160         NTSTATUS status;
161
162         D_INFO("WB command next_grent end.\n");
163         if (tevent_req_is_nterror(req, &status)) {
164                 D_WARNING("Failed with %s.\n", nt_errstr(status));
165                 return status;
166         }
167         *members = talloc_move(mem_ctx, &state->members);
168         return NT_STATUS_OK;
169 }