WHATSNEW: Add release notes for Samba 4.9.17.
[Samba.git] / source3 / winbindd / winbindd_getgrent.c
blob15a35f7044e7224c92dbf6b4068dc607bb033a61
1 /*
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/>.
20 #include "includes.h"
21 #include "winbindd.h"
23 struct winbindd_getgrent_state {
24 struct tevent_context *ev;
25 struct winbindd_cli_state *cli;
26 int max_groups;
27 int num_groups;
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);
44 if (req == NULL) {
45 return NULL;
47 state->ev = ev;
48 state->num_groups = 0;
49 state->cli = cli;
51 DEBUG(3, ("[%5lu]: getgrent\n", (unsigned long)cli->pid));
53 if (cli->grent_state == NULL) {
54 tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
55 return tevent_req_post(req, ev);
58 state->max_groups = MIN(500, request->data.num_entries);
59 if (state->max_groups == 0) {
60 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
61 return tevent_req_post(req, ev);
64 state->groups = talloc_zero_array(state, struct winbindd_gr,
65 state->max_groups);
66 if (tevent_req_nomem(state->groups, req)) {
67 return tevent_req_post(req, ev);
70 state->members = talloc_array(state, struct db_context *,
71 state->max_groups);
72 if (tevent_req_nomem(state->members, req)) {
73 TALLOC_FREE(state->groups);
74 return tevent_req_post(req, ev);
77 subreq = wb_next_grent_send(state, ev, lp_winbind_expand_groups(),
78 cli->grent_state,
79 &state->groups[state->num_groups]);
80 if (tevent_req_nomem(subreq, req)) {
81 return tevent_req_post(req, ev);
83 tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
84 return req;
87 static void winbindd_getgrent_done(struct tevent_req *subreq)
89 struct tevent_req *req = tevent_req_callback_data(
90 subreq, struct tevent_req);
91 struct winbindd_getgrent_state *state = tevent_req_data(
92 req, struct winbindd_getgrent_state);
93 NTSTATUS status;
95 status = wb_next_grent_recv(subreq, state,
96 &state->members[state->num_groups]);
97 TALLOC_FREE(subreq);
98 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
99 DEBUG(10, ("winbindd_getgrent_done: done with %d groups\n",
100 (int)state->num_groups));
101 TALLOC_FREE(state->cli->grent_state);
102 tevent_req_done(req);
103 return;
105 if (!NT_STATUS_IS_OK(status)) {
106 tevent_req_nterror(req, status);
107 return;
109 state->num_groups += 1;
110 if (state->num_groups >= state->max_groups) {
111 DEBUG(10, ("winbindd_getgrent_done: Got enough groups: %d\n",
112 (int)state->num_groups));
113 tevent_req_done(req);
114 return;
116 if (state->cli->grent_state == NULL) {
117 DEBUG(10, ("winbindd_getgrent_done: endgrent called in "
118 "between\n"));
119 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
120 return;
122 subreq = wb_next_grent_send(state, state->ev,
123 lp_winbind_expand_groups(),
124 state->cli->grent_state,
125 &state->groups[state->num_groups]);
126 if (tevent_req_nomem(subreq, req)) {
127 return;
129 tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
132 NTSTATUS winbindd_getgrent_recv(struct tevent_req *req,
133 struct winbindd_response *response)
135 struct winbindd_getgrent_state *state = tevent_req_data(
136 req, struct winbindd_getgrent_state);
137 NTSTATUS status;
138 char **memberstrings;
139 char *result;
140 size_t base_memberofs, total_memberlen;
141 int i;
143 if (tevent_req_is_nterror(req, &status)) {
144 TALLOC_FREE(state->cli->grent_state);
145 DEBUG(5, ("getgrent failed: %s\n", nt_errstr(status)));
146 return status;
149 if (state->num_groups == 0) {
150 return NT_STATUS_NO_MORE_ENTRIES;
153 memberstrings = talloc_array(talloc_tos(), char *, state->num_groups);
154 if (memberstrings == NULL) {
155 TALLOC_FREE(state->cli->grent_state);
156 return NT_STATUS_NO_MEMORY;
159 total_memberlen = 0;
161 for (i=0; i<state->num_groups; i++) {
162 int num_members;
164 status = winbindd_print_groupmembers(
165 state->members[i], memberstrings, &num_members,
166 &memberstrings[i]);
168 if (!NT_STATUS_IS_OK(status)) {
169 TALLOC_FREE(memberstrings);
170 TALLOC_FREE(state->cli->grent_state);
171 return status;
173 TALLOC_FREE(state->members[i]);
175 state->groups[i].num_gr_mem = num_members;
176 state->groups[i].gr_mem_ofs = total_memberlen;
178 total_memberlen += talloc_get_size(memberstrings[i]);
181 base_memberofs = state->num_groups * sizeof(struct winbindd_gr);
183 result = talloc_realloc(state, state->groups, char,
184 base_memberofs + total_memberlen);
185 if (result == NULL) {
186 TALLOC_FREE(state->cli->grent_state);
187 return NT_STATUS_NO_MEMORY;
189 state->groups = (struct winbindd_gr *)result;
191 for (i=0; i<state->num_groups; i++) {
192 memcpy(result + base_memberofs + state->groups[i].gr_mem_ofs,
193 memberstrings[i], talloc_get_size(memberstrings[i]));
194 TALLOC_FREE(memberstrings[i]);
197 TALLOC_FREE(memberstrings);
199 response->data.num_entries = state->num_groups;
200 response->length += talloc_get_size(result);
201 response->extra_data.data = talloc_move(response, &result);
202 return NT_STATUS_OK;