2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_GETGRENT
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/>.
23 struct winbindd_getgrent_state {
24 struct tevent_context *ev;
25 struct winbindd_cli_state *cli;
28 struct winbindd_gr *groups;
29 struct db_context **members;
32 static void winbindd_getgrent_done(struct tevent_req *subreq);
34 struct tevent_req *winbindd_getgrent_send(TALLOC_CTX *mem_ctx,
35 struct tevent_context *ev,
36 struct winbindd_cli_state *cli,
37 struct winbindd_request *request)
39 struct tevent_req *req, *subreq;
40 struct winbindd_getgrent_state *state;
42 req = tevent_req_create(mem_ctx, &state,
43 struct winbindd_getgrent_state);
48 state->num_groups = 0;
51 DBG_NOTICE("[%s (%u)] getgrent\n",
53 (unsigned int)cli->pid);
55 if (cli->grent_state == NULL) {
56 tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
57 return tevent_req_post(req, ev);
60 state->max_groups = MIN(500, request->data.num_entries);
61 if (state->max_groups == 0) {
62 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
63 return tevent_req_post(req, ev);
66 state->groups = talloc_zero_array(state, struct winbindd_gr,
68 if (tevent_req_nomem(state->groups, req)) {
69 return tevent_req_post(req, ev);
72 state->members = talloc_array(state, struct db_context *,
74 if (tevent_req_nomem(state->members, req)) {
75 TALLOC_FREE(state->groups);
76 return tevent_req_post(req, ev);
79 subreq = wb_next_grent_send(state, ev, lp_winbind_expand_groups(),
81 &state->groups[state->num_groups]);
82 if (tevent_req_nomem(subreq, req)) {
83 return tevent_req_post(req, ev);
85 tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
89 static void winbindd_getgrent_done(struct tevent_req *subreq)
91 struct tevent_req *req = tevent_req_callback_data(
92 subreq, struct tevent_req);
93 struct winbindd_getgrent_state *state = tevent_req_data(
94 req, struct winbindd_getgrent_state);
97 status = wb_next_grent_recv(subreq, state,
98 &state->members[state->num_groups]);
100 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
101 DEBUG(10, ("winbindd_getgrent_done: done with %d groups\n",
102 (int)state->num_groups));
103 TALLOC_FREE(state->cli->grent_state);
104 tevent_req_done(req);
107 if (!NT_STATUS_IS_OK(status)) {
108 tevent_req_nterror(req, status);
111 state->num_groups += 1;
112 if (state->num_groups >= state->max_groups) {
113 DEBUG(10, ("winbindd_getgrent_done: Got enough groups: %d\n",
114 (int)state->num_groups));
115 tevent_req_done(req);
118 if (state->cli->grent_state == NULL) {
119 DEBUG(10, ("winbindd_getgrent_done: endgrent called in "
121 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
124 subreq = wb_next_grent_send(state, state->ev,
125 lp_winbind_expand_groups(),
126 state->cli->grent_state,
127 &state->groups[state->num_groups]);
128 if (tevent_req_nomem(subreq, req)) {
131 tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
134 NTSTATUS winbindd_getgrent_recv(struct tevent_req *req,
135 struct winbindd_response *response)
137 struct winbindd_getgrent_state *state = tevent_req_data(
138 req, struct winbindd_getgrent_state);
140 char **memberstrings;
142 size_t base_memberofs, total_memberlen;
145 if (tevent_req_is_nterror(req, &status)) {
146 TALLOC_FREE(state->cli->grent_state);
147 DEBUG(5, ("getgrent failed: %s\n", nt_errstr(status)));
151 if (state->num_groups == 0) {
152 return NT_STATUS_NO_MORE_ENTRIES;
155 memberstrings = talloc_array(talloc_tos(), char *, state->num_groups);
156 if (memberstrings == NULL) {
157 TALLOC_FREE(state->cli->grent_state);
158 return NT_STATUS_NO_MEMORY;
163 for (i=0; i<state->num_groups; i++) {
166 status = winbindd_print_groupmembers(
167 state->members[i], memberstrings, &num_members,
170 if (!NT_STATUS_IS_OK(status)) {
171 TALLOC_FREE(memberstrings);
172 TALLOC_FREE(state->cli->grent_state);
175 TALLOC_FREE(state->members[i]);
177 state->groups[i].num_gr_mem = num_members;
178 state->groups[i].gr_mem_ofs = total_memberlen;
180 total_memberlen += talloc_get_size(memberstrings[i]);
183 base_memberofs = state->num_groups * sizeof(struct winbindd_gr);
185 result = talloc_realloc(state, state->groups, char,
186 base_memberofs + total_memberlen);
187 if (result == NULL) {
188 TALLOC_FREE(state->cli->grent_state);
189 return NT_STATUS_NO_MEMORY;
191 state->groups = (struct winbindd_gr *)result;
193 for (i=0; i<state->num_groups; i++) {
194 memcpy(result + base_memberofs + state->groups[i].gr_mem_ofs,
195 memberstrings[i], talloc_get_size(memberstrings[i]));
196 TALLOC_FREE(memberstrings[i]);
199 TALLOC_FREE(memberstrings);
201 response->data.num_entries = state->num_groups;
202 response->length += talloc_get_size(result);
203 response->extra_data.data = talloc_move(response, &result);