gensec: Fix CID 242642 Unchecked return value
[Samba.git] / source3 / winbindd / wb_next_grent.c
blobfd925b609e5773fcca2b6b2d380944219eaad43a
1 /*
2 Unix SMB/CIFS implementation.
3 async next_grent
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/ndr_winbind_c.h"
23 #include "passdb/machine_sid.h"
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 talloc_dict *members;
33 static void wb_next_grent_fetch_done(struct tevent_req *subreq);
34 static void wb_next_grent_getgrsid_done(struct tevent_req *subreq);
36 static void wb_next_grent_send_do(struct tevent_req *req,
37 struct wb_next_grent_state *state)
39 struct tevent_req *subreq;
41 if (state->gstate->next_group >= state->gstate->num_groups) {
42 TALLOC_FREE(state->gstate->groups);
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;
50 subreq = wb_query_group_list_send(state, state->ev,
51 state->gstate->domain);
52 if (tevent_req_nomem(subreq, req)) {
53 return;
55 tevent_req_set_callback(subreq, wb_next_grent_fetch_done, req);
56 return;
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;
66 tevent_req_set_callback(subreq, wb_next_grent_getgrsid_done, req);
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)
75 struct tevent_req *req;
76 struct wb_next_grent_state *state;
78 req = tevent_req_create(mem_ctx, &state, struct wb_next_grent_state);
79 if (req == NULL) {
80 return NULL;
82 state->ev = ev;
83 state->gstate = gstate;
84 state->gr = gr;
85 state->max_nesting = max_nesting;
87 wb_next_grent_send_do(req, state);
88 if (!tevent_req_is_in_progress(req)) {
89 return tevent_req_post(req, ev);
92 return req;
95 static void wb_next_grent_fetch_done(struct tevent_req *subreq)
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;
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;
114 state->gstate->next_group = 0;
116 wb_next_grent_send_do(req, state);
119 static void wb_next_grent_getgrsid_done(struct tevent_req *subreq)
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;
128 status = wb_getgrsid_recv(subreq, talloc_tos(), &domname, &name,
129 &state->gr->gr_gid, &state->members);
130 TALLOC_FREE(subreq);
132 if (NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
133 state->gstate->next_group += 1;
135 wb_next_grent_send_do(req, state);
137 return;
138 } else if (tevent_req_nterror(req, status)) {
139 return;
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;
148 state->gstate->next_group += 1;
149 tevent_req_done(req);
152 NTSTATUS wb_next_grent_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
153 struct talloc_dict **members)
155 struct wb_next_grent_state *state = tevent_req_data(
156 req, struct wb_next_grent_state);
157 NTSTATUS status;
159 if (tevent_req_is_nterror(req, &status)) {
160 return status;
162 *members = talloc_move(mem_ctx, &state->members);
163 return NT_STATUS_OK;