wscript: Add check for --wrap linker flag
[vlendec/samba-autobuild/.git] / source3 / winbindd / wb_next_grent.c
1 /*
2    Unix SMB/CIFS implementation.
3    async next_grent
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_grent_state {
26         struct tevent_context *ev;
27         int max_nesting;
28         struct getgrent_state *gstate;
29         struct winbindd_gr *gr;
30         struct db_context *members;
31 };
32
33 static void wb_next_grent_fetch_done(struct tevent_req *subreq);
34 static void wb_next_grent_getgrsid_done(struct tevent_req *subreq);
35
36 static void wb_next_grent_send_do(struct tevent_req *req,
37                                   struct wb_next_grent_state *state)
38 {
39         struct tevent_req *subreq;
40
41         if (state->gstate->next_group >= state->gstate->num_groups) {
42                 TALLOC_FREE(state->gstate->groups);
43
44                 state->gstate->domain = wb_next_domain(state->gstate->domain);
45                 if (state->gstate->domain == NULL) {
46                         tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
47                         return;
48                 }
49
50                 subreq = wb_query_group_list_send(state, state->ev,
51                                                   state->gstate->domain);
52                 if (tevent_req_nomem(subreq, req)) {
53                         return;
54                 }
55                 tevent_req_set_callback(subreq, wb_next_grent_fetch_done, req);
56                 return;
57         }
58
59         subreq = wb_getgrsid_send(
60                 state, state->ev,
61                 &state->gstate->groups[state->gstate->next_group].sid,
62                 state->max_nesting);
63         if (tevent_req_nomem(subreq, req)) {
64                 return;
65         }
66         tevent_req_set_callback(subreq, wb_next_grent_getgrsid_done, req);
67 }
68
69 struct tevent_req *wb_next_grent_send(TALLOC_CTX *mem_ctx,
70                                       struct tevent_context *ev,
71                                       int max_nesting,
72                                       struct getgrent_state *gstate,
73                                       struct winbindd_gr *gr)
74 {
75         struct tevent_req *req;
76         struct wb_next_grent_state *state;
77
78         req = tevent_req_create(mem_ctx, &state, struct wb_next_grent_state);
79         if (req == NULL) {
80                 return NULL;
81         }
82         state->ev = ev;
83         state->gstate = gstate;
84         state->gr = gr;
85         state->max_nesting = max_nesting;
86
87         wb_next_grent_send_do(req, state);
88         if (!tevent_req_is_in_progress(req)) {
89                 return tevent_req_post(req, ev);
90         }
91
92         return req;
93 }
94
95 static void wb_next_grent_fetch_done(struct tevent_req *subreq)
96 {
97         struct tevent_req *req = tevent_req_callback_data(
98                 subreq, struct tevent_req);
99         struct wb_next_grent_state *state = tevent_req_data(
100                 req, struct wb_next_grent_state);
101         NTSTATUS status;
102
103         status = wb_query_group_list_recv(subreq, state->gstate,
104                                           &state->gstate->num_groups,
105                                           &state->gstate->groups);
106         TALLOC_FREE(subreq);
107         if (!NT_STATUS_IS_OK(status)) {
108                 /* Ignore errors here, just log it */
109                 DEBUG(10, ("query_group_list for domain %s returned %s\n",
110                            state->gstate->domain->name, nt_errstr(status)));
111                 state->gstate->num_groups = 0;
112         }
113
114         state->gstate->next_group = 0;
115
116         wb_next_grent_send_do(req, state);
117 }
118
119 static void wb_next_grent_getgrsid_done(struct tevent_req *subreq)
120 {
121         struct tevent_req *req = tevent_req_callback_data(
122                 subreq, struct tevent_req);
123         struct wb_next_grent_state *state = tevent_req_data(
124                 req, struct wb_next_grent_state);
125         const char *domname, *name;
126         NTSTATUS status;
127
128         status = wb_getgrsid_recv(subreq, talloc_tos(), &domname, &name,
129                                   &state->gr->gr_gid, &state->members);
130         TALLOC_FREE(subreq);
131
132         if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
133                 state->gstate->next_group += 1;
134
135                 wb_next_grent_send_do(req, state);
136
137                 return;
138         } else if (tevent_req_nterror(req, status)) {
139                 return;
140         }
141
142         if (!fill_grent(talloc_tos(), state->gr, domname, name,
143                         state->gr->gr_gid)) {
144                 DEBUG(5, ("fill_grent failed\n"));
145                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
146                 return;
147         }
148         state->gstate->next_group += 1;
149         tevent_req_done(req);
150 }
151
152 NTSTATUS wb_next_grent_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
153                             struct db_context **members)
154 {
155         struct wb_next_grent_state *state = tevent_req_data(
156                 req, struct wb_next_grent_state);
157         NTSTATUS status;
158
159         if (tevent_req_is_nterror(req, &status)) {
160                 return status;
161         }
162         *members = talloc_move(mem_ctx, &state->members);
163         return NT_STATUS_OK;
164 }