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