Update 4.2 Roadmap file
[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_winbind_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 void wb_next_pwent_send_do(struct tevent_req *req,
35                                   struct wb_next_pwent_state *state)
36 {
37         struct tevent_req *subreq;
38
39         if (state->gstate->next_user >= state->gstate->num_users) {
40                 TALLOC_FREE(state->gstate->users);
41
42                 state->gstate->domain = wb_next_domain(state->gstate->domain);
43                 if (state->gstate->domain == NULL) {
44                         tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
45                         return;
46                 }
47
48                 subreq = wb_query_user_list_send(state, state->ev,
49                                                  state->gstate->domain);
50                 if (tevent_req_nomem(subreq, req)) {
51                         return;
52                 }
53
54                 tevent_req_set_callback(subreq, wb_next_pwent_fetch_done, req);
55                 return;
56         }
57
58         subreq = wb_fill_pwent_send(state, state->ev,
59                                 &state->gstate->users[state->gstate->next_user],
60                                 state->pw);
61         if (tevent_req_nomem(subreq, req)) {
62                 return;
63         }
64
65         tevent_req_set_callback(subreq, wb_next_pwent_fill_done, req);
66 }
67
68 struct tevent_req *wb_next_pwent_send(TALLOC_CTX *mem_ctx,
69                                       struct tevent_context *ev,
70                                       struct getpwent_state *gstate,
71                                       struct winbindd_pw *pw)
72 {
73         struct tevent_req *req;
74         struct wb_next_pwent_state *state;
75
76         req = tevent_req_create(mem_ctx, &state, struct wb_next_pwent_state);
77         if (req == NULL) {
78                 return NULL;
79         }
80         state->ev = ev;
81         state->gstate = gstate;
82         state->pw = pw;
83
84         wb_next_pwent_send_do(req, state);
85         if (!tevent_req_is_in_progress(req)) {
86                 return tevent_req_post(req, ev);
87         }
88
89         return req;
90 }
91
92 static void wb_next_pwent_fetch_done(struct tevent_req *subreq)
93 {
94         struct tevent_req *req = tevent_req_callback_data(
95                 subreq, struct tevent_req);
96         struct wb_next_pwent_state *state = tevent_req_data(
97                 req, struct wb_next_pwent_state);
98         NTSTATUS status;
99
100         status = wb_query_user_list_recv(subreq, state->gstate,
101                                          &state->gstate->num_users,
102                                          &state->gstate->users);
103         TALLOC_FREE(subreq);
104         if (!NT_STATUS_IS_OK(status)) {
105                 /* Ignore errors here, just log it */
106                 DEBUG(10, ("query_user_list for domain %s returned %s\n",
107                            state->gstate->domain->name,
108                            nt_errstr(status)));
109                 state->gstate->num_users = 0;
110         }
111
112         state->gstate->next_user = 0;
113
114         wb_next_pwent_send_do(req, state);
115 }
116
117 static void wb_next_pwent_fill_done(struct tevent_req *subreq)
118 {
119         struct tevent_req *req = tevent_req_callback_data(
120                 subreq, struct tevent_req);
121         struct wb_next_pwent_state *state = tevent_req_data(
122                 req, struct wb_next_pwent_state);
123         NTSTATUS status;
124
125         status = wb_fill_pwent_recv(subreq);
126         TALLOC_FREE(subreq);
127         /*
128          * When you try to enumerate users with 'getent passwd' and the user
129          * doesn't have a uid set we should just move on.
130          */
131         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
132                 state->gstate->next_user += 1;
133
134                 wb_next_pwent_send_do(req, state);
135
136                 return;
137         } else if (tevent_req_nterror(req, status)) {
138                 return;
139         }
140         state->gstate->next_user += 1;
141         tevent_req_done(req);
142 }
143
144 NTSTATUS wb_next_pwent_recv(struct tevent_req *req)
145 {
146         return tevent_req_simple_recv_ntstatus(req);
147 }