s3-winbind: Implemented samr backend function common_enum_local_groups.
[Samba/gebeck_regimport.git] / source3 / winbindd / wb_next_pwent.c
blobfbaaa48b6b89536f5ab1a3559bb8a7a53a220dcf
1 /*
2 Unix SMB/CIFS implementation.
3 async next_pwent
4 Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "librpc/gen_ndr/cli_wbint.h"
24 struct wb_next_pwent_state {
25 struct tevent_context *ev;
26 struct getpwent_state *gstate;
27 struct winbindd_pw *pw;
30 static void wb_next_pwent_fetch_done(struct tevent_req *subreq);
31 static void wb_next_pwent_fill_done(struct tevent_req *subreq);
33 struct tevent_req *wb_next_pwent_send(TALLOC_CTX *mem_ctx,
34 struct tevent_context *ev,
35 struct getpwent_state *gstate,
36 struct winbindd_pw *pw)
38 struct tevent_req *req, *subreq;
39 struct wb_next_pwent_state *state;
41 req = tevent_req_create(mem_ctx, &state, struct wb_next_pwent_state);
42 if (req == NULL) {
43 return NULL;
45 state->ev = ev;
46 state->gstate = gstate;
47 state->pw = pw;
49 if (state->gstate->next_user >= state->gstate->num_users) {
50 TALLOC_FREE(state->gstate->users);
52 if (state->gstate->domain == NULL) {
53 state->gstate->domain = domain_list();
54 } else {
55 state->gstate->domain = state->gstate->domain->next;
58 if ((state->gstate->domain != NULL)
59 && sid_check_is_domain(&state->gstate->domain->sid)) {
60 state->gstate->domain = state->gstate->domain->next;
63 if (state->gstate->domain == NULL) {
64 tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
65 return tevent_req_post(req, ev);
67 subreq = wb_query_user_list_send(state, state->ev,
68 state->gstate->domain);
69 if (tevent_req_nomem(subreq, req)) {
70 return tevent_req_post(req, ev);
72 tevent_req_set_callback(subreq, wb_next_pwent_fetch_done, req);
73 return req;
76 subreq = wb_fill_pwent_send(
77 state, state->ev,
78 &state->gstate->users[state->gstate->next_user],
79 state->pw);
80 if (tevent_req_nomem(subreq, req)) {
81 return tevent_req_post(req, ev);
83 tevent_req_set_callback(subreq, wb_next_pwent_fill_done, req);
84 return req;
87 static void wb_next_pwent_fetch_done(struct tevent_req *subreq)
89 struct tevent_req *req = tevent_req_callback_data(
90 subreq, struct tevent_req);
91 struct wb_next_pwent_state *state = tevent_req_data(
92 req, struct wb_next_pwent_state);
93 NTSTATUS status;
95 status = wb_query_user_list_recv(subreq, state->gstate,
96 &state->gstate->num_users,
97 &state->gstate->users);
98 TALLOC_FREE(subreq);
99 if (!NT_STATUS_IS_OK(status)) {
100 /* Ignore errors here, just log it */
101 DEBUG(10, ("query_user_list for domain %s returned %s\n",
102 state->gstate->domain->name,
103 nt_errstr(status)));
104 state->gstate->num_users = 0;
107 if (state->gstate->num_users == 0) {
108 state->gstate->domain = state->gstate->domain->next;
110 if ((state->gstate->domain != NULL)
111 && sid_check_is_domain(&state->gstate->domain->sid)) {
112 state->gstate->domain = state->gstate->domain->next;
115 if (state->gstate->domain == NULL) {
116 tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
117 return;
119 subreq = wb_query_user_list_send(state, state->ev,
120 state->gstate->domain);
121 if (tevent_req_nomem(subreq, req)) {
122 return;
124 tevent_req_set_callback(subreq, wb_next_pwent_fetch_done, req);
125 return;
128 state->gstate->next_user = 0;
130 subreq = wb_fill_pwent_send(
131 state, state->ev,
132 &state->gstate->users[state->gstate->next_user],
133 state->pw);
134 if (tevent_req_nomem(subreq, req)) {
135 return;
137 tevent_req_set_callback(subreq, wb_next_pwent_fill_done, req);
140 static void wb_next_pwent_fill_done(struct tevent_req *subreq)
142 struct tevent_req *req = tevent_req_callback_data(
143 subreq, struct tevent_req);
144 struct wb_next_pwent_state *state = tevent_req_data(
145 req, struct wb_next_pwent_state);
146 NTSTATUS status;
148 status = wb_fill_pwent_recv(subreq);
149 TALLOC_FREE(subreq);
150 if (!NT_STATUS_IS_OK(status)) {
151 tevent_req_nterror(req, status);
152 return;
154 state->gstate->next_user += 1;
155 tevent_req_done(req);
158 NTSTATUS wb_next_pwent_recv(struct tevent_req *req)
160 return tevent_req_simple_recv_ntstatus(req);