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