pyldb: avoid segfault when adding an element with no name
[kai/samba-autobuild/.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         int max_groups;
27         int 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         DBG_NOTICE("[%s (%u)] getgrent\n",
52                    cli->client_name,
53                    (unsigned int)cli->pid);
54
55         if (cli->grent_state == NULL) {
56                 tevent_req_nterror(req, NT_STATUS_NO_MORE_ENTRIES);
57                 return tevent_req_post(req, ev);
58         }
59
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);
64         }
65
66         state->groups = talloc_zero_array(state, struct winbindd_gr,
67                                           state->max_groups);
68         if (tevent_req_nomem(state->groups, req)) {
69                 return tevent_req_post(req, ev);
70         }
71
72         state->members = talloc_array(state, struct db_context *,
73                                       state->max_groups);
74         if (tevent_req_nomem(state->members, req)) {
75                 TALLOC_FREE(state->groups);
76                 return tevent_req_post(req, ev);
77         }
78
79         subreq = wb_next_grent_send(state, ev, lp_winbind_expand_groups(),
80                                     cli->grent_state,
81                                     &state->groups[state->num_groups]);
82         if (tevent_req_nomem(subreq, req)) {
83                 return tevent_req_post(req, ev);
84         }
85         tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
86         return req;
87 }
88
89 static void winbindd_getgrent_done(struct tevent_req *subreq)
90 {
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);
95         NTSTATUS status;
96
97         status = wb_next_grent_recv(subreq, state,
98                                     &state->members[state->num_groups]);
99         TALLOC_FREE(subreq);
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);
105                 return;
106         }
107         if (!NT_STATUS_IS_OK(status)) {
108                 tevent_req_nterror(req, status);
109                 return;
110         }
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);
116                 return;
117         }
118         if (state->cli->grent_state == NULL) {
119                 DEBUG(10, ("winbindd_getgrent_done: endgrent called in "
120                            "between\n"));
121                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
122                 return;
123         }
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)) {
129                 return;
130         }
131         tevent_req_set_callback(subreq, winbindd_getgrent_done, req);
132 }
133
134 NTSTATUS winbindd_getgrent_recv(struct tevent_req *req,
135                                 struct winbindd_response *response)
136 {
137         struct winbindd_getgrent_state *state = tevent_req_data(
138                 req, struct winbindd_getgrent_state);
139         NTSTATUS status;
140         char **memberstrings;
141         char *result;
142         size_t base_memberofs, total_memberlen;
143         int i;
144
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)));
148                 return status;
149         }
150
151         if (state->num_groups == 0) {
152                 return NT_STATUS_NO_MORE_ENTRIES;
153         }
154
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;
159         }
160
161         total_memberlen = 0;
162
163         for (i=0; i<state->num_groups; i++) {
164                 int num_members;
165
166                 status = winbindd_print_groupmembers(
167                         state->members[i], memberstrings, &num_members,
168                         &memberstrings[i]);
169
170                 if (!NT_STATUS_IS_OK(status)) {
171                         TALLOC_FREE(memberstrings);
172                         TALLOC_FREE(state->cli->grent_state);
173                         return status;
174                 }
175                 TALLOC_FREE(state->members[i]);
176
177                 state->groups[i].num_gr_mem = num_members;
178                 state->groups[i].gr_mem_ofs = total_memberlen;
179
180                 total_memberlen += talloc_get_size(memberstrings[i]);
181         }
182
183         base_memberofs = state->num_groups * sizeof(struct winbindd_gr);
184
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;
190         }
191         state->groups = (struct winbindd_gr *)result;
192
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]);
197         }
198
199         TALLOC_FREE(memberstrings);
200
201         response->data.num_entries = state->num_groups;
202         response->length += talloc_get_size(result);
203         response->extra_data.data = talloc_move(response, &result);
204         return NT_STATUS_OK;
205 }