winbindd_cache.c: move some some notice messages from ERR to NOTICE level
[samba.git] / source3 / winbindd / winbindd_getgrent.c
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 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         struct tevent_req *req, *subreq;
40         struct winbindd_getgrent_state *state;
41
42         req = tevent_req_create(mem_ctx, &state,
43                                 struct winbindd_getgrent_state);
44         if (req == NULL) {
45                 return NULL;
46         }
47         state->ev = ev;
48         state->num_groups = 0;
49         state->cli = cli;
50
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);
57
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);
62         }
63
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);
68         }
69
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);
74         }
75
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);
81         }
82
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);
88         }
89         tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
90         return req;
91 }
92
93 static void winbindd_getgrent_done(struct tevent_req *subreq)
94 {
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;
100
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;
110         }
111         if (!NT_STATUS_IS_OK(status)) {
112                 tevent_req_nterror(req, status);
113                 return;
114         }
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;
121         }
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;
126         }
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;
133         }
134         tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
135 }
136
137 NTSTATUS winbindd_getgrent_recv(struct tevent_req *req,
138                                 struct winbindd_response *response)
139 {
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;
147
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;
152         }
153
154         if (state->num_groups == 0) {
155                 return NT_STATUS_NO_MORE_ENTRIES;
156         }
157
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;
162         }
163
164         total_memberlen = 0;
165
166         for (i=0; i<state->num_groups; i++) {
167                 int num_members;
168
169                 status = winbindd_print_groupmembers(
170                         state->members[i], memberstrings, &num_members,
171                         &memberstrings[i]);
172
173                 if (!NT_STATUS_IS_OK(status)) {
174                         TALLOC_FREE(memberstrings);
175                         TALLOC_FREE(state->cli->grent_state);
176                         return status;
177                 }
178                 TALLOC_FREE(state->members[i]);
179
180                 state->groups[i].num_gr_mem = num_members;
181                 state->groups[i].gr_mem_ofs = total_memberlen;
182
183                 total_memberlen += talloc_get_size(memberstrings[i]);
184         }
185
186         base_memberofs = state->num_groups * sizeof(struct winbindd_gr);
187
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;
193         }
194         state->groups = (struct winbindd_gr *)result;
195
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]);
200         }
201
202         TALLOC_FREE(memberstrings);
203
204         response->data.num_entries = state->num_groups;
205         response->length += talloc_get_size(result);
206         response->extra_data.data = talloc_move(response, &result);
207
208         D_NOTICE("Winbind external command GETGRENT end.\n"
209                  "Received %"PRIu32" entries.\n",
210                  response->data.num_entries);
211
212         return NT_STATUS_OK;
213 }