s4-netapi: Initialize group_handle of NetGroupGetUsers_r().
[Samba.git] / source4 / libnet / groupman.c
blob97236bd7ad7ca121e34606deb9092354cdc3946c
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Rafal Szczesniak 2007
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/>.
21 a composite function for manipulating (add/edit/del) groups via samr pipe
24 #include "includes.h"
25 #include "libcli/composite/composite.h"
26 #include "libnet/libnet.h"
27 #include "librpc/gen_ndr/ndr_samr_c.h"
30 struct groupadd_state {
31 struct dcerpc_pipe *pipe;
32 struct policy_handle domain_handle;
33 struct samr_CreateDomainGroup creategroup;
34 struct policy_handle group_handle;
35 uint32_t group_rid;
37 void (*monitor_fn)(struct monitor_msg*);
41 static void continue_groupadd_created(struct tevent_req *subreq);
44 struct composite_context* libnet_rpc_groupadd_send(struct dcerpc_pipe *p,
45 TALLOC_CTX *mem_ctx,
46 struct libnet_rpc_groupadd *io,
47 void (*monitor)(struct monitor_msg*))
49 struct composite_context *c;
50 struct groupadd_state *s;
51 struct tevent_req *subreq;
53 if (!p || !io) return NULL;
55 c = composite_create(mem_ctx, dcerpc_event_context(p));
56 if (c == NULL) return NULL;
58 s = talloc_zero(c, struct groupadd_state);
59 if (composite_nomem(s, c)) return c;
61 c->private_data = s;
63 s->domain_handle = io->in.domain_handle;
64 s->pipe = p;
65 s->monitor_fn = monitor;
67 s->creategroup.in.domain_handle = &s->domain_handle;
69 s->creategroup.in.name = talloc_zero(c, struct lsa_String);
70 if (composite_nomem(s->creategroup.in.name, c)) return c;
72 s->creategroup.in.name->string = talloc_strdup(c, io->in.groupname);
73 if (composite_nomem(s->creategroup.in.name->string, c)) return c;
75 s->creategroup.in.access_mask = 0;
77 s->creategroup.out.group_handle = &s->group_handle;
78 s->creategroup.out.rid = &s->group_rid;
80 subreq = dcerpc_samr_CreateDomainGroup_r_send(s, c->event_ctx,
81 s->pipe->binding_handle,
82 &s->creategroup);
83 if (composite_nomem(subreq, c)) return c;
85 tevent_req_set_callback(subreq, continue_groupadd_created, c);
86 return c;
90 NTSTATUS libnet_rpc_groupadd_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
91 struct libnet_rpc_groupadd *io)
93 NTSTATUS status;
94 struct groupadd_state *s;
96 status = composite_wait(c);
97 if (NT_STATUS_IS_OK(status)) {
98 s = talloc_get_type(c, struct groupadd_state);
101 return status;
105 static void continue_groupadd_created(struct tevent_req *subreq)
107 struct composite_context *c;
108 struct groupadd_state *s;
110 c = tevent_req_callback_data(subreq, struct composite_context);
111 s = talloc_get_type(c->private_data, struct groupadd_state);
113 c->status = dcerpc_samr_CreateDomainGroup_r_recv(subreq, s);
114 TALLOC_FREE(subreq);
115 if (!composite_is_ok(c)) return;
117 c->status = s->creategroup.out.result;
118 if (!composite_is_ok(c)) return;
120 composite_done(c);
124 NTSTATUS libnet_rpc_groupadd(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
125 struct libnet_rpc_groupadd *io)
127 struct composite_context *c;
129 c = libnet_rpc_groupadd_send(p, mem_ctx, io, NULL);
130 return libnet_rpc_groupadd_recv(c, mem_ctx, io);
134 struct groupdel_state {
135 struct dcerpc_pipe *pipe;
136 struct policy_handle domain_handle;
137 struct policy_handle group_handle;
138 struct samr_LookupNames lookupname;
139 struct samr_OpenGroup opengroup;
140 struct samr_DeleteDomainGroup deletegroup;
142 /* information about the progress */
143 void (*monitor_fn)(struct monitor_msg *);
147 static void continue_groupdel_name_found(struct tevent_req *subreq);
148 static void continue_groupdel_group_opened(struct tevent_req *subreq);
149 static void continue_groupdel_deleted(struct tevent_req *subreq);
152 struct composite_context* libnet_rpc_groupdel_send(struct dcerpc_pipe *p,
153 TALLOC_CTX *mem_ctx,
154 struct libnet_rpc_groupdel *io,
155 void (*monitor)(struct monitor_msg*))
157 struct composite_context *c;
158 struct groupdel_state *s;
159 struct tevent_req *subreq;
161 /* composite context allocation and setup */
162 c = composite_create(mem_ctx, dcerpc_event_context(p));
163 if (c == NULL) return NULL;
165 s = talloc_zero(c, struct groupdel_state);
166 if (composite_nomem(s, c)) return c;
168 c->private_data = s;
170 /* store function parameters in the state structure */
171 s->pipe = p;
172 s->domain_handle = io->in.domain_handle;
173 s->monitor_fn = monitor;
175 /* prepare parameters to send rpc request */
176 s->lookupname.in.domain_handle = &io->in.domain_handle;
177 s->lookupname.in.num_names = 1;
178 s->lookupname.in.names = talloc_zero(s, struct lsa_String);
179 s->lookupname.in.names->string = io->in.groupname;
180 s->lookupname.out.rids = talloc_zero(s, struct samr_Ids);
181 s->lookupname.out.types = talloc_zero(s, struct samr_Ids);
182 if (composite_nomem(s->lookupname.out.rids, c)) return c;
183 if (composite_nomem(s->lookupname.out.types, c)) return c;
185 /* send the request */
186 subreq = dcerpc_samr_LookupNames_r_send(s, c->event_ctx,
187 p->binding_handle,
188 &s->lookupname);
189 if (composite_nomem(subreq, c)) return c;
191 tevent_req_set_callback(subreq, continue_groupdel_name_found, c);
192 return c;
196 static void continue_groupdel_name_found(struct tevent_req *subreq)
198 struct composite_context *c;
199 struct groupdel_state *s;
201 c = tevent_req_callback_data(subreq, struct composite_context);
202 s = talloc_get_type(c->private_data, struct groupdel_state);
204 /* receive samr_LookupNames result */
205 c->status = dcerpc_samr_LookupNames_r_recv(subreq, s);
206 TALLOC_FREE(subreq);
207 if (!composite_is_ok(c)) return;
209 c->status = s->lookupname.out.result;
210 if (!NT_STATUS_IS_OK(c->status)) {
211 composite_error(c, c->status);
212 return;
215 /* what to do when there's no group account to delete
216 and what if there's more than one rid resolved */
217 if (!s->lookupname.out.rids->count) {
218 c->status = NT_STATUS_NO_SUCH_GROUP;
219 composite_error(c, c->status);
220 return;
222 } else if (!s->lookupname.out.rids->count > 1) {
223 c->status = NT_STATUS_INVALID_ACCOUNT_NAME;
224 composite_error(c, c->status);
225 return;
228 /* prepare the arguments for rpc call */
229 s->opengroup.in.domain_handle = &s->domain_handle;
230 s->opengroup.in.rid = s->lookupname.out.rids->ids[0];
231 s->opengroup.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
232 s->opengroup.out.group_handle = &s->group_handle;
234 /* send rpc request */
235 subreq = dcerpc_samr_OpenGroup_r_send(s, c->event_ctx,
236 s->pipe->binding_handle,
237 &s->opengroup);
238 if (composite_nomem(subreq, c)) return;
240 tevent_req_set_callback(subreq, continue_groupdel_group_opened, c);
244 static void continue_groupdel_group_opened(struct tevent_req *subreq)
246 struct composite_context *c;
247 struct groupdel_state *s;
249 c = tevent_req_callback_data(subreq, struct composite_context);
250 s = talloc_get_type(c->private_data, struct groupdel_state);
252 /* receive samr_OpenGroup result */
253 c->status = dcerpc_samr_OpenGroup_r_recv(subreq, s);
254 TALLOC_FREE(subreq);
255 if (!composite_is_ok(c)) return;
257 c->status = s->opengroup.out.result;
258 if (!NT_STATUS_IS_OK(c->status)) {
259 composite_error(c, c->status);
260 return;
263 /* prepare the final rpc call arguments */
264 s->deletegroup.in.group_handle = &s->group_handle;
265 s->deletegroup.out.group_handle = &s->group_handle;
267 /* send rpc request */
268 subreq = dcerpc_samr_DeleteDomainGroup_r_send(s, c->event_ctx,
269 s->pipe->binding_handle,
270 &s->deletegroup);
271 if (composite_nomem(subreq, c)) return;
273 /* callback handler setup */
274 tevent_req_set_callback(subreq, continue_groupdel_deleted, c);
278 static void continue_groupdel_deleted(struct tevent_req *subreq)
280 struct composite_context *c;
281 struct groupdel_state *s;
283 c = tevent_req_callback_data(subreq, struct composite_context);
284 s = talloc_get_type(c->private_data, struct groupdel_state);
286 /* receive samr_DeleteGroup result */
287 c->status = dcerpc_samr_DeleteDomainGroup_r_recv(subreq, s);
288 TALLOC_FREE(subreq);
289 if (!composite_is_ok(c)) return;
291 /* return the actual function call status */
292 c->status = s->deletegroup.out.result;
293 if (!NT_STATUS_IS_OK(c->status)) {
294 composite_error(c, c->status);
295 return;
298 composite_done(c);
302 NTSTATUS libnet_rpc_groupdel_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
303 struct libnet_rpc_groupdel *io)
305 NTSTATUS status;
306 struct groupdel_state *s;
308 status = composite_wait(c);
309 if (NT_STATUS_IS_OK(status) && io) {
310 s = talloc_get_type(c->private_data, struct groupdel_state);
311 io->out.group_handle = s->group_handle;
314 talloc_free(c);
315 return status;
319 NTSTATUS libnet_rpc_groupdel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
320 struct libnet_rpc_groupdel *io)
322 struct composite_context *c;
324 c = libnet_rpc_groupdel_send(p, mem_ctx, io, NULL);
325 return libnet_rpc_groupdel_recv(c, mem_ctx, io);