source: branches/samba-3.5.x/source3/winbindd/wb_next_pwent.c

Last change on this file was 733, checked in by Silvan Scherrer, 13 years ago

Samba Server 3.5: update branche to 3.5.13

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