Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : async implementation of WINBINDD_GETGRENT
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 :
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;
30 : };
31 :
32 : static void winbindd_getgrent_done(struct tevent_req *subreq);
33 :
34 50 : 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)
38 : {
39 0 : struct tevent_req *req, *subreq;
40 0 : struct winbindd_getgrent_state *state;
41 :
42 50 : req = tevent_req_create(mem_ctx, &state,
43 : struct winbindd_getgrent_state);
44 50 : if (req == NULL) {
45 0 : return NULL;
46 : }
47 50 : state->ev = ev;
48 50 : state->num_groups = 0;
49 50 : state->cli = cli;
50 :
51 50 : 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);
57 :
58 50 : if (cli->grent_state == NULL) {
59 20 : D_NOTICE("The grent state from winbindd client state is NULL.\n");
60 20 : tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
61 20 : return tevent_req_post(req, ev);
62 : }
63 :
64 30 : state->max_groups = MIN(500, request->data.num_entries);
65 30 : if (state->max_groups == 0) {
66 0 : tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
67 0 : return tevent_req_post(req, ev);
68 : }
69 :
70 30 : state->groups = talloc_zero_array(state, struct winbindd_gr,
71 : state->max_groups);
72 30 : if (tevent_req_nomem(state->groups, req)) {
73 0 : return tevent_req_post(req, ev);
74 : }
75 :
76 30 : state->members = talloc_array(state, struct db_context *,
77 : state->max_groups);
78 30 : if (tevent_req_nomem(state->members, req)) {
79 0 : TALLOC_FREE(state->groups);
80 0 : return tevent_req_post(req, ev);
81 : }
82 :
83 30 : subreq = wb_next_grent_send(state, ev, lp_winbind_expand_groups(),
84 : cli->grent_state,
85 30 : &state->groups[state->num_groups]);
86 30 : if (tevent_req_nomem(subreq, req)) {
87 0 : return tevent_req_post(req, ev);
88 : }
89 30 : tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
90 30 : return req;
91 : }
92 :
93 1790 : static void winbindd_getgrent_done(struct tevent_req *subreq)
94 : {
95 1790 : struct tevent_req *req = tevent_req_callback_data(
96 : subreq, struct tevent_req);
97 1790 : struct winbindd_getgrent_state *state = tevent_req_data(
98 : req, struct winbindd_getgrent_state);
99 0 : NTSTATUS status;
100 :
101 1790 : status = wb_next_grent_recv(subreq, state,
102 1790 : &state->members[state->num_groups]);
103 1790 : TALLOC_FREE(subreq);
104 1790 : if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
105 30 : D_WARNING("winbindd_getgrent_done: done with %"PRIu32" groups\n",
106 : state->num_groups);
107 30 : TALLOC_FREE(state->cli->grent_state);
108 30 : tevent_req_done(req);
109 30 : return;
110 : }
111 1760 : if (!NT_STATUS_IS_OK(status)) {
112 0 : tevent_req_nterror(req, status);
113 0 : return;
114 : }
115 1760 : state->num_groups += 1;
116 1760 : if (state->num_groups >= state->max_groups) {
117 0 : D_DEBUG("winbindd_getgrent_done: Got enough groups: %"PRIu32"\n",
118 : state->num_groups);
119 0 : tevent_req_done(req);
120 0 : return;
121 : }
122 1760 : if (state->cli->grent_state == NULL) {
123 0 : D_DEBUG("winbindd_getgrent_done: endgrent called in between\n");
124 0 : tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
125 0 : return;
126 : }
127 1760 : subreq = wb_next_grent_send(state, state->ev,
128 : lp_winbind_expand_groups(),
129 1760 : state->cli->grent_state,
130 1760 : &state->groups[state->num_groups]);
131 1760 : if (tevent_req_nomem(subreq, req)) {
132 0 : return;
133 : }
134 1760 : tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
135 : }
136 :
137 50 : NTSTATUS winbindd_getgrent_recv(struct tevent_req *req,
138 : struct winbindd_response *response)
139 : {
140 50 : struct winbindd_getgrent_state *state = tevent_req_data(
141 : req, struct winbindd_getgrent_state);
142 0 : NTSTATUS status;
143 0 : char **memberstrings;
144 0 : char *result;
145 0 : size_t base_memberofs, total_memberlen;
146 0 : uint32_t i;
147 :
148 50 : if (tevent_req_is_nterror(req, &status)) {
149 20 : TALLOC_FREE(state->cli->grent_state);
150 20 : D_WARNING("getgrent failed: %s\n", nt_errstr(status));
151 20 : return status;
152 : }
153 :
154 30 : if (state->num_groups == 0) {
155 10 : return NT_STATUS_NO_MORE_ENTRIES;
156 : }
157 :
158 20 : memberstrings = talloc_array(talloc_tos(), char *, state->num_groups);
159 20 : if (memberstrings == NULL) {
160 0 : TALLOC_FREE(state->cli->grent_state);
161 0 : return NT_STATUS_NO_MEMORY;
162 : }
163 :
164 20 : total_memberlen = 0;
165 :
166 1780 : for (i=0; i<state->num_groups; i++) {
167 0 : int num_members;
168 :
169 1760 : status = winbindd_print_groupmembers(
170 1760 : state->members[i], memberstrings, &num_members,
171 1760 : &memberstrings[i]);
172 :
173 1760 : if (!NT_STATUS_IS_OK(status)) {
174 0 : TALLOC_FREE(memberstrings);
175 0 : TALLOC_FREE(state->cli->grent_state);
176 0 : return status;
177 : }
178 1760 : TALLOC_FREE(state->members[i]);
179 :
180 1760 : state->groups[i].num_gr_mem = num_members;
181 1760 : state->groups[i].gr_mem_ofs = total_memberlen;
182 :
183 1760 : total_memberlen += talloc_get_size(memberstrings[i]);
184 : }
185 :
186 20 : base_memberofs = state->num_groups * sizeof(struct winbindd_gr);
187 :
188 20 : result = talloc_realloc(state, state->groups, char,
189 : base_memberofs + total_memberlen);
190 20 : if (result == NULL) {
191 0 : TALLOC_FREE(state->cli->grent_state);
192 0 : return NT_STATUS_NO_MEMORY;
193 : }
194 20 : state->groups = (struct winbindd_gr *)result;
195 :
196 1780 : for (i=0; i<state->num_groups; i++) {
197 3520 : memcpy(result + base_memberofs + state->groups[i].gr_mem_ofs,
198 1760 : memberstrings[i], talloc_get_size(memberstrings[i]));
199 1760 : TALLOC_FREE(memberstrings[i]);
200 : }
201 :
202 20 : TALLOC_FREE(memberstrings);
203 :
204 20 : response->data.num_entries = state->num_groups;
205 20 : response->length += talloc_get_size(result);
206 20 : response->extra_data.data = talloc_move(response, &result);
207 :
208 20 : D_NOTICE("Winbind external command GETGRENT end.\n"
209 : "Received %"PRIu32" entries.\n",
210 : response->data.num_entries);
211 :
212 20 : return NT_STATUS_OK;
213 : }
|