s3/s4 - Adapt the IDL changes on various locations
[samba.git] / source3 / winbindd / wb_next_pwent.c
1 /*
2    Unix SMB/CIFS implementation.
3    async next_pwent
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_pwent_state {
25         struct tevent_context *ev;
26         struct getpwent_state *gstate;
27         struct winbindd_pw *pw;
28 };
29
30 static void wb_next_pwent_fetch_done(struct tevent_req *subreq);
31 static void wb_next_pwent_fill_done(struct tevent_req *subreq);
32
33 struct tevent_req *wb_next_pwent_send(TALLOC_CTX *mem_ctx,
34                                       struct tevent_context *ev,
35                                       struct getpwent_state *gstate,
36                                       struct winbindd_pw *pw)
37 {
38         struct tevent_req *req, *subreq;
39         struct wb_next_pwent_state *state;
40
41         req = tevent_req_create(mem_ctx, &state, struct wb_next_pwent_state);
42         if (req == NULL) {
43                 return NULL;
44         }
45         state->ev = ev;
46         state->gstate = gstate;
47         state->pw = pw;
48
49         if (state->gstate->next_user >= state->gstate->num_users) {
50                 TALLOC_FREE(state->gstate->users);
51
52                 if (state->gstate->domain == NULL) {
53                         state->gstate->domain = domain_list();
54                 } else {
55                         state->gstate->domain = state->gstate->domain->next;
56                 }
57
58                 if (state->gstate->domain == NULL) {
59                         tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
60                         return tevent_req_post(req, ev);
61                 }
62                 subreq = wb_query_user_list_send(state, state->ev,
63                                                  state->gstate->domain);
64                 if (tevent_req_nomem(subreq, req)) {
65                         return tevent_req_post(req, ev);
66                 }
67                 tevent_req_set_callback(subreq, wb_next_pwent_fetch_done, req);
68                 return req;
69         }
70
71         subreq = wb_fill_pwent_send(
72                 state, state->ev,
73                 &state->gstate->users[state->gstate->next_user],
74                 state->pw);
75         if (tevent_req_nomem(subreq, req)) {
76                 return tevent_req_post(req, ev);
77         }
78         tevent_req_set_callback(subreq, wb_next_pwent_fill_done, req);
79         return req;
80 }
81
82 static void wb_next_pwent_fetch_done(struct tevent_req *subreq)
83 {
84         struct tevent_req *req = tevent_req_callback_data(
85                 subreq, struct tevent_req);
86         struct wb_next_pwent_state *state = tevent_req_data(
87                 req, struct wb_next_pwent_state);
88         NTSTATUS status;
89
90         status = wb_query_user_list_recv(subreq, state->gstate,
91                                          &state->gstate->num_users,
92                                          &state->gstate->users);
93         TALLOC_FREE(subreq);
94         if (!NT_STATUS_IS_OK(status)) {
95                 /* Ignore errors here, just log it */
96                 DEBUG(10, ("query_user_list for domain %s returned %s\n",
97                            state->gstate->domain->name,
98                            nt_errstr(status)));
99                 state->gstate->num_users = 0;
100         }
101
102         if (state->gstate->num_users == 0) {
103                 state->gstate->domain = state->gstate->domain->next;
104                 if (state->gstate->domain == NULL) {
105                         tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
106                         return;
107                 }
108                 subreq = wb_query_user_list_send(state, state->ev,
109                                                  state->gstate->domain);
110                 if (tevent_req_nomem(subreq, req)) {
111                         return;
112                 }
113                 tevent_req_set_callback(subreq, wb_next_pwent_fetch_done, req);
114                 return;
115         }
116
117         state->gstate->next_user = 0;
118
119         subreq = wb_fill_pwent_send(
120                 state, state->ev,
121                 &state->gstate->users[state->gstate->next_user],
122                 state->pw);
123         if (tevent_req_nomem(subreq, req)) {
124                 return;
125         }
126         tevent_req_set_callback(subreq, wb_next_pwent_fill_done, req);
127 }
128
129 static void wb_next_pwent_fill_done(struct tevent_req *subreq)
130 {
131         struct tevent_req *req = tevent_req_callback_data(
132                 subreq, struct tevent_req);
133         struct wb_next_pwent_state *state = tevent_req_data(
134                 req, struct wb_next_pwent_state);
135         NTSTATUS status;
136
137         status = wb_fill_pwent_recv(subreq);
138         TALLOC_FREE(subreq);
139         if (!NT_STATUS_IS_OK(status)) {
140                 tevent_req_nterror(req, status);
141                 return;
142         }
143         state->gstate->next_user += 1;
144         tevent_req_done(req);
145 }
146
147 NTSTATUS wb_next_pwent_recv(struct tevent_req *req)
148 {
149         return tevent_req_simple_recv_ntstatus(req);
150 }