s3:winbind:pwent: move wb_next_domain() to winbindd_util.c for re-use
[kai/samba-autobuild/.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/ndr_winbind_c.h"
23 #include "passdb/machine_sid.h"
24
25 struct wb_next_pwent_state {
26         struct tevent_context *ev;
27         struct getpwent_state *gstate;
28         struct winbindd_pw *pw;
29 };
30
31 static void wb_next_pwent_fetch_done(struct tevent_req *subreq);
32 static void wb_next_pwent_fill_done(struct tevent_req *subreq);
33
34 struct tevent_req *wb_next_pwent_send(TALLOC_CTX *mem_ctx,
35                                       struct tevent_context *ev,
36                                       struct getpwent_state *gstate,
37                                       struct winbindd_pw *pw)
38 {
39         struct tevent_req *req, *subreq;
40         struct wb_next_pwent_state *state;
41
42         req = tevent_req_create(mem_ctx, &state, struct wb_next_pwent_state);
43         if (req == NULL) {
44                 return NULL;
45         }
46         state->ev = ev;
47         state->gstate = gstate;
48         state->pw = pw;
49
50         if (state->gstate->next_user >= state->gstate->num_users) {
51                 TALLOC_FREE(state->gstate->users);
52
53                 state->gstate->domain = wb_next_domain(state->gstate->domain);
54                 if (state->gstate->domain == NULL) {
55                         tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
56                         return tevent_req_post(req, ev);
57                 }
58                 subreq = wb_query_user_list_send(state, state->ev,
59                                                  state->gstate->domain);
60                 if (tevent_req_nomem(subreq, req)) {
61                         return tevent_req_post(req, ev);
62                 }
63                 tevent_req_set_callback(subreq, wb_next_pwent_fetch_done, req);
64                 return req;
65         }
66
67         subreq = wb_fill_pwent_send(
68                 state, state->ev,
69                 &state->gstate->users[state->gstate->next_user],
70                 state->pw);
71         if (tevent_req_nomem(subreq, req)) {
72                 return tevent_req_post(req, ev);
73         }
74         tevent_req_set_callback(subreq, wb_next_pwent_fill_done, req);
75         return req;
76 }
77
78 static void wb_next_pwent_fetch_done(struct tevent_req *subreq)
79 {
80         struct tevent_req *req = tevent_req_callback_data(
81                 subreq, struct tevent_req);
82         struct wb_next_pwent_state *state = tevent_req_data(
83                 req, struct wb_next_pwent_state);
84         NTSTATUS status;
85
86         status = wb_query_user_list_recv(subreq, state->gstate,
87                                          &state->gstate->num_users,
88                                          &state->gstate->users);
89         TALLOC_FREE(subreq);
90         if (!NT_STATUS_IS_OK(status)) {
91                 /* Ignore errors here, just log it */
92                 DEBUG(10, ("query_user_list for domain %s returned %s\n",
93                            state->gstate->domain->name,
94                            nt_errstr(status)));
95                 state->gstate->num_users = 0;
96         }
97
98         if (state->gstate->num_users == 0) {
99                 state->gstate->domain = wb_next_domain(state->gstate->domain);
100                 if (state->gstate->domain == NULL) {
101                         tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
102                         return;
103                 }
104                 subreq = wb_query_user_list_send(state, state->ev,
105                                                  state->gstate->domain);
106                 if (tevent_req_nomem(subreq, req)) {
107                         return;
108                 }
109                 tevent_req_set_callback(subreq, wb_next_pwent_fetch_done, req);
110                 return;
111         }
112
113         state->gstate->next_user = 0;
114
115         subreq = wb_fill_pwent_send(
116                 state, state->ev,
117                 &state->gstate->users[state->gstate->next_user],
118                 state->pw);
119         if (tevent_req_nomem(subreq, req)) {
120                 return;
121         }
122         tevent_req_set_callback(subreq, wb_next_pwent_fill_done, req);
123 }
124
125 static void wb_next_pwent_fill_done(struct tevent_req *subreq)
126 {
127         struct tevent_req *req = tevent_req_callback_data(
128                 subreq, struct tevent_req);
129         struct wb_next_pwent_state *state = tevent_req_data(
130                 req, struct wb_next_pwent_state);
131         NTSTATUS status;
132
133         status = wb_fill_pwent_recv(subreq);
134         TALLOC_FREE(subreq);
135         /*
136          * When you try to enumerate users with 'getent passwd' and the user
137          * doesn't have a uid set we should just move on.
138          */
139         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
140                 state->gstate->next_user += 1;
141
142                 if (state->gstate->next_user >= state->gstate->num_users) {
143                         TALLOC_FREE(state->gstate->users);
144
145                         state->gstate->domain = wb_next_domain(state->gstate->domain);
146                         if (state->gstate->domain == NULL) {
147                                 tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
148                                 return;
149                         }
150
151                         subreq = wb_query_user_list_send(state, state->ev,
152                                         state->gstate->domain);
153                         if (tevent_req_nomem(subreq, req)) {
154                                 return;
155                         }
156                         tevent_req_set_callback(subreq, wb_next_pwent_fetch_done, req);
157                         return;
158                 }
159
160                 subreq = wb_fill_pwent_send(state,
161                                             state->ev,
162                                             &state->gstate->users[state->gstate->next_user],
163                                             state->pw);
164                 if (tevent_req_nomem(subreq, req)) {
165                         return;
166                 }
167                 tevent_req_set_callback(subreq, wb_next_pwent_fill_done, req);
168
169                 return;
170         } else if (tevent_req_nterror(req, status)) {
171                 return;
172         }
173         state->gstate->next_user += 1;
174         tevent_req_done(req);
175 }
176
177 NTSTATUS wb_next_pwent_recv(struct tevent_req *req)
178 {
179         return tevent_req_simple_recv_ntstatus(req);
180 }