s3:libads: move ads->auth.time_offset to ads->config.time_offset
[Samba.git] / source3 / winbindd / winbindd_getgrent.c
blobe8e2b66f038eaa913965ef9700ff513ba7354ee5
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 uint32_t max_groups;
27 uint32_t 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 D_NOTICE("[%s (%u)] Winbind external command GETGRENT start.\n"
52 "The caller (%s) provided room for %"PRIu32" entries.\n",
53 cli->client_name,
54 (unsigned int)cli->pid,
55 cli->client_name,
56 request->data.num_entries);
58 if (cli->grent_state == NULL) {
59 D_NOTICE("The grent state from winbindd client state is NULL.\n");
60 tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
61 return tevent_req_post(req, ev);
64 state->max_groups = MIN(500, request->data.num_entries);
65 if (state->max_groups == 0) {
66 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
67 return tevent_req_post(req, ev);
70 state->groups = talloc_zero_array(state, struct winbindd_gr,
71 state->max_groups);
72 if (tevent_req_nomem(state->groups, req)) {
73 return tevent_req_post(req, ev);
76 state->members = talloc_array(state, struct db_context *,
77 state->max_groups);
78 if (tevent_req_nomem(state->members, req)) {
79 TALLOC_FREE(state->groups);
80 return tevent_req_post(req, ev);
83 subreq = wb_next_grent_send(state, ev, lp_winbind_expand_groups(),
84 cli->grent_state,
85 &state->groups[state->num_groups]);
86 if (tevent_req_nomem(subreq, req)) {
87 return tevent_req_post(req, ev);
89 tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
90 return req;
93 static void winbindd_getgrent_done(struct tevent_req *subreq)
95 struct tevent_req *req = tevent_req_callback_data(
96 subreq, struct tevent_req);
97 struct winbindd_getgrent_state *state = tevent_req_data(
98 req, struct winbindd_getgrent_state);
99 NTSTATUS status;
101 status = wb_next_grent_recv(subreq, state,
102 &state->members[state->num_groups]);
103 TALLOC_FREE(subreq);
104 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
105 D_WARNING("winbindd_getgrent_done: done with %"PRIu32" groups\n",
106 state->num_groups);
107 TALLOC_FREE(state->cli->grent_state);
108 tevent_req_done(req);
109 return;
111 if (!NT_STATUS_IS_OK(status)) {
112 tevent_req_nterror(req, status);
113 return;
115 state->num_groups += 1;
116 if (state->num_groups >= state->max_groups) {
117 D_DEBUG("winbindd_getgrent_done: Got enough groups: %"PRIu32"\n",
118 state->num_groups);
119 tevent_req_done(req);
120 return;
122 if (state->cli->grent_state == NULL) {
123 D_DEBUG("winbindd_getgrent_done: endgrent called in between\n");
124 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
125 return;
127 subreq = wb_next_grent_send(state, state->ev,
128 lp_winbind_expand_groups(),
129 state->cli->grent_state,
130 &state->groups[state->num_groups]);
131 if (tevent_req_nomem(subreq, req)) {
132 return;
134 tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
137 NTSTATUS winbindd_getgrent_recv(struct tevent_req *req,
138 struct winbindd_response *response)
140 struct winbindd_getgrent_state *state = tevent_req_data(
141 req, struct winbindd_getgrent_state);
142 NTSTATUS status;
143 char **memberstrings;
144 char *result;
145 size_t base_memberofs, total_memberlen;
146 uint32_t i;
148 if (tevent_req_is_nterror(req, &status)) {
149 TALLOC_FREE(state->cli->grent_state);
150 D_WARNING("getgrent failed: %s\n", nt_errstr(status));
151 return status;
154 if (state->num_groups == 0) {
155 return NT_STATUS_NO_MORE_ENTRIES;
158 memberstrings = talloc_array(talloc_tos(), char *, state->num_groups);
159 if (memberstrings == NULL) {
160 TALLOC_FREE(state->cli->grent_state);
161 return NT_STATUS_NO_MEMORY;
164 total_memberlen = 0;
166 for (i=0; i<state->num_groups; i++) {
167 int num_members;
169 status = winbindd_print_groupmembers(
170 state->members[i], memberstrings, &num_members,
171 &memberstrings[i]);
173 if (!NT_STATUS_IS_OK(status)) {
174 TALLOC_FREE(memberstrings);
175 TALLOC_FREE(state->cli->grent_state);
176 return status;
178 TALLOC_FREE(state->members[i]);
180 state->groups[i].num_gr_mem = num_members;
181 state->groups[i].gr_mem_ofs = total_memberlen;
183 total_memberlen += talloc_get_size(memberstrings[i]);
186 base_memberofs = state->num_groups * sizeof(struct winbindd_gr);
188 result = talloc_realloc(state, state->groups, char,
189 base_memberofs + total_memberlen);
190 if (result == NULL) {
191 TALLOC_FREE(state->cli->grent_state);
192 return NT_STATUS_NO_MEMORY;
194 state->groups = (struct winbindd_gr *)result;
196 for (i=0; i<state->num_groups; i++) {
197 memcpy(result + base_memberofs + state->groups[i].gr_mem_ofs,
198 memberstrings[i], talloc_get_size(memberstrings[i]));
199 TALLOC_FREE(memberstrings[i]);
202 TALLOC_FREE(memberstrings);
204 response->data.num_entries = state->num_groups;
205 response->length += talloc_get_size(result);
206 response->extra_data.data = talloc_move(response, &result);
208 D_NOTICE("Winbind external command GETGRENT end.\n"
209 "Received %"PRIu32" entries.\n",
210 response->data.num_entries);
212 return NT_STATUS_OK;