winbindd: only use the domain name from lookup sids if the domain matches
[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_winbind_c.h"
23 #include "libcli/security/dom_sid.h"
24 #include "passdb/machine_sid.h"
25
26 struct wb_next_pwent_state {
27         struct tevent_context *ev;
28         struct getpwent_state *gstate;
29         struct dom_sid next_sid;
30         struct winbindd_pw *pw;
31 };
32
33 static void wb_next_pwent_fetch_done(struct tevent_req *subreq);
34 static void wb_next_pwent_fill_done(struct tevent_req *subreq);
35
36 static void wb_next_pwent_send_do(struct tevent_req *req,
37                                   struct wb_next_pwent_state *state)
38 {
39         struct tevent_req *subreq;
40
41         if (state->gstate->next_user >= state->gstate->rids.num_rids) {
42                 TALLOC_FREE(state->gstate->rids.rids);
43                 state->gstate->rids.num_rids = 0;
44
45                 state->gstate->domain = wb_next_domain(state->gstate->domain);
46                 if (state->gstate->domain == NULL) {
47                         tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
48                         return;
49                 }
50
51                 subreq = dcerpc_wbint_QueryUserRidList_send(
52                         state, state->ev,
53                         dom_child_handle(state->gstate->domain),
54                         &state->gstate->rids);
55                 if (tevent_req_nomem(subreq, req)) {
56                         return;
57                 }
58
59                 tevent_req_set_callback(subreq, wb_next_pwent_fetch_done, req);
60                 return;
61         }
62
63         sid_compose(&state->next_sid, &state->gstate->domain->sid,
64                     state->gstate->rids.rids[state->gstate->next_user]);
65
66         subreq = wb_getpwsid_send(state, state->ev, &state->next_sid,
67                                   state->pw);
68         if (tevent_req_nomem(subreq, req)) {
69                 return;
70         }
71
72         tevent_req_set_callback(subreq, wb_next_pwent_fill_done, req);
73 }
74
75 struct tevent_req *wb_next_pwent_send(TALLOC_CTX *mem_ctx,
76                                       struct tevent_context *ev,
77                                       struct getpwent_state *gstate,
78                                       struct winbindd_pw *pw)
79 {
80         struct tevent_req *req;
81         struct wb_next_pwent_state *state;
82
83         req = tevent_req_create(mem_ctx, &state, struct wb_next_pwent_state);
84         if (req == NULL) {
85                 return NULL;
86         }
87         state->ev = ev;
88         state->gstate = gstate;
89         state->pw = pw;
90
91         wb_next_pwent_send_do(req, state);
92         if (!tevent_req_is_in_progress(req)) {
93                 return tevent_req_post(req, ev);
94         }
95
96         return req;
97 }
98
99 static void wb_next_pwent_fetch_done(struct tevent_req *subreq)
100 {
101         struct tevent_req *req = tevent_req_callback_data(
102                 subreq, struct tevent_req);
103         struct wb_next_pwent_state *state = tevent_req_data(
104                 req, struct wb_next_pwent_state);
105         NTSTATUS status, result;
106
107         status = dcerpc_wbint_QueryUserRidList_recv(subreq, state->gstate,
108                                                     &result);
109         TALLOC_FREE(subreq);
110         if (any_nt_status_not_ok(status, result, &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->rids.num_rids = 0;
116         }
117
118         state->gstate->next_user = 0;
119
120         wb_next_pwent_send_do(req, state);
121 }
122
123 static void wb_next_pwent_fill_done(struct tevent_req *subreq)
124 {
125         struct tevent_req *req = tevent_req_callback_data(
126                 subreq, struct tevent_req);
127         struct wb_next_pwent_state *state = tevent_req_data(
128                 req, struct wb_next_pwent_state);
129         NTSTATUS status;
130
131         status = wb_getpwsid_recv(subreq);
132         TALLOC_FREE(subreq);
133         /*
134          * When you try to enumerate users with 'getent passwd' and the user
135          * doesn't have a uid set we should just move on.
136          */
137         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
138                 state->gstate->next_user += 1;
139
140                 wb_next_pwent_send_do(req, state);
141
142                 return;
143         } else if (tevent_req_nterror(req, status)) {
144                 return;
145         }
146         state->gstate->next_user += 1;
147         tevent_req_done(req);
148 }
149
150 NTSTATUS wb_next_pwent_recv(struct tevent_req *req)
151 {
152         return tevent_req_simple_recv_ntstatus(req);
153 }