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