packaging(RHEL-CTDB): don't pass CFLAGS to make.
[Samba/fernandojvsilva.git] / source4 / libnet / groupman.c
blob0f54db9705b6b931ce7bfea42c73335527f405c9
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 rpc_request *req);
44 struct composite_context* libnet_rpc_groupadd_send(struct dcerpc_pipe *p,
45 struct libnet_rpc_groupadd *io,
46 void (*monitor)(struct monitor_msg*))
48 struct composite_context *c;
49 struct groupadd_state *s;
50 struct rpc_request *create_req;
52 if (!p || !io) return NULL;
54 c = composite_create(p, dcerpc_event_context(p));
55 if (c == NULL) return NULL;
57 s = talloc_zero(c, struct groupadd_state);
58 if (composite_nomem(s, c)) return c;
60 c->private_data = s;
62 s->domain_handle = io->in.domain_handle;
63 s->pipe = p;
64 s->monitor_fn = monitor;
66 s->creategroup.in.domain_handle = &s->domain_handle;
68 s->creategroup.in.name = talloc_zero(c, struct lsa_String);
69 if (composite_nomem(s->creategroup.in.name, c)) return c;
71 s->creategroup.in.name->string = talloc_strdup(c, io->in.groupname);
72 if (composite_nomem(s->creategroup.in.name->string, c)) return c;
74 s->creategroup.in.access_mask = 0;
76 s->creategroup.out.group_handle = &s->group_handle;
77 s->creategroup.out.rid = &s->group_rid;
79 create_req = dcerpc_samr_CreateDomainGroup_send(s->pipe, c, &s->creategroup);
80 if (composite_nomem(create_req, c)) return c;
82 composite_continue_rpc(c, create_req, continue_groupadd_created, c);
83 return c;
87 NTSTATUS libnet_rpc_groupadd_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
88 struct libnet_rpc_groupadd *io)
90 NTSTATUS status;
91 struct groupadd_state *s;
93 status = composite_wait(c);
94 if (NT_STATUS_IS_OK(status)) {
95 s = talloc_get_type(c, struct groupadd_state);
98 return status;
102 static void continue_groupadd_created(struct rpc_request *req)
104 struct composite_context *c;
105 struct groupadd_state *s;
107 c = talloc_get_type(req->async.private_data, struct composite_context);
108 s = talloc_get_type(c->private_data, struct groupadd_state);
110 c->status = dcerpc_ndr_request_recv(req);
111 if (!composite_is_ok(c)) return;
113 c->status = s->creategroup.out.result;
114 if (!composite_is_ok(c)) return;
116 composite_done(c);
120 NTSTATUS libnet_rpc_groupadd(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
121 struct libnet_rpc_groupadd *io)
123 struct composite_context *c;
125 c = libnet_rpc_groupadd_send(p, io, NULL);
126 return libnet_rpc_groupadd_recv(c, mem_ctx, io);
130 struct groupdel_state {
131 struct dcerpc_pipe *pipe;
132 struct policy_handle domain_handle;
133 struct policy_handle group_handle;
134 struct samr_LookupNames lookupname;
135 struct samr_OpenGroup opengroup;
136 struct samr_DeleteDomainGroup deletegroup;
138 /* information about the progress */
139 void (*monitor_fn)(struct monitor_msg *);
143 static void continue_groupdel_name_found(struct rpc_request *req);
144 static void continue_groupdel_group_opened(struct rpc_request *req);
145 static void continue_groupdel_deleted(struct rpc_request *req);
148 struct composite_context* libnet_rpc_groupdel_send(struct dcerpc_pipe *p,
149 struct libnet_rpc_groupdel *io,
150 void (*monitor)(struct monitor_msg*))
152 struct composite_context *c;
153 struct groupdel_state *s;
154 struct rpc_request *lookup_req;
156 /* composite context allocation and setup */
157 c = composite_create(p, dcerpc_event_context(p));
158 if (c == NULL) return NULL;
160 s = talloc_zero(c, struct groupdel_state);
161 if (composite_nomem(s, c)) return c;
163 c->private_data = s;
165 /* store function parameters in the state structure */
166 s->pipe = p;
167 s->domain_handle = io->in.domain_handle;
168 s->monitor_fn = monitor;
170 /* prepare parameters to send rpc request */
171 s->lookupname.in.domain_handle = &io->in.domain_handle;
172 s->lookupname.in.num_names = 1;
173 s->lookupname.in.names = talloc_zero(s, struct lsa_String);
174 s->lookupname.in.names->string = io->in.groupname;
175 s->lookupname.out.rids = talloc_zero(s, struct samr_Ids);
176 s->lookupname.out.types = talloc_zero(s, struct samr_Ids);
177 if (composite_nomem(s->lookupname.out.rids, c)) return c;
178 if (composite_nomem(s->lookupname.out.types, c)) return c;
180 /* send the request */
181 lookup_req = dcerpc_samr_LookupNames_send(p, c, &s->lookupname);
182 if (composite_nomem(lookup_req, c)) return c;
184 composite_continue_rpc(c, lookup_req, continue_groupdel_name_found, c);
185 return c;
189 static void continue_groupdel_name_found(struct rpc_request *req)
191 struct composite_context *c;
192 struct groupdel_state *s;
193 struct rpc_request *opengroup_req;
195 c = talloc_get_type(req->async.private_data, struct composite_context);
196 s = talloc_get_type(c->private_data, struct groupdel_state);
198 /* receive samr_LookupNames result */
199 c->status = dcerpc_ndr_request_recv(req);
200 if (!composite_is_ok(c)) return;
202 c->status = s->lookupname.out.result;
203 if (!NT_STATUS_IS_OK(c->status)) {
204 composite_error(c, c->status);
205 return;
208 /* what to do when there's no group account to delete
209 and what if there's more than one rid resolved */
210 if (!s->lookupname.out.rids->count) {
211 c->status = NT_STATUS_NO_SUCH_GROUP;
212 composite_error(c, c->status);
213 return;
215 } else if (!s->lookupname.out.rids->count > 1) {
216 c->status = NT_STATUS_INVALID_ACCOUNT_NAME;
217 composite_error(c, c->status);
218 return;
221 /* prepare the arguments for rpc call */
222 s->opengroup.in.domain_handle = &s->domain_handle;
223 s->opengroup.in.rid = s->lookupname.out.rids->ids[0];
224 s->opengroup.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
225 s->opengroup.out.group_handle = &s->group_handle;
227 /* send rpc request */
228 opengroup_req = dcerpc_samr_OpenGroup_send(s->pipe, c, &s->opengroup);
229 if (composite_nomem(opengroup_req, c)) return;
231 composite_continue_rpc(c, opengroup_req, continue_groupdel_group_opened, c);
235 static void continue_groupdel_group_opened(struct rpc_request *req)
237 struct composite_context *c;
238 struct groupdel_state *s;
239 struct rpc_request *delgroup_req;
241 c = talloc_get_type(req->async.private_data, struct composite_context);
242 s = talloc_get_type(c->private_data, struct groupdel_state);
244 /* receive samr_OpenGroup result */
245 c->status = dcerpc_ndr_request_recv(req);
246 if (!composite_is_ok(c)) return;
248 c->status = s->opengroup.out.result;
249 if (!NT_STATUS_IS_OK(c->status)) {
250 composite_error(c, c->status);
251 return;
254 /* prepare the final rpc call arguments */
255 s->deletegroup.in.group_handle = &s->group_handle;
256 s->deletegroup.out.group_handle = &s->group_handle;
258 /* send rpc request */
259 delgroup_req = dcerpc_samr_DeleteDomainGroup_send(s->pipe, c, &s->deletegroup);
260 if (composite_nomem(delgroup_req, c)) return;
262 /* callback handler setup */
263 composite_continue_rpc(c, delgroup_req, continue_groupdel_deleted, c);
267 static void continue_groupdel_deleted(struct rpc_request *req)
269 struct composite_context *c;
270 struct groupdel_state *s;
272 c = talloc_get_type(req->async.private_data, struct composite_context);
273 s = talloc_get_type(c->private_data, struct groupdel_state);
275 /* receive samr_DeleteGroup result */
276 c->status = dcerpc_ndr_request_recv(req);
277 if (!composite_is_ok(c)) return;
279 /* return the actual function call status */
280 c->status = s->deletegroup.out.result;
281 if (!NT_STATUS_IS_OK(c->status)) {
282 composite_error(c, c->status);
283 return;
286 composite_done(c);
290 NTSTATUS libnet_rpc_groupdel_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
291 struct libnet_rpc_groupdel *io)
293 NTSTATUS status;
294 struct groupdel_state *s;
296 status = composite_wait(c);
297 if (NT_STATUS_IS_OK(status) && io) {
298 s = talloc_get_type(c->private_data, struct groupdel_state);
299 io->out.group_handle = s->group_handle;
302 talloc_free(c);
303 return status;
307 NTSTATUS libnet_rpc_groupdel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
308 struct libnet_rpc_groupdel *io)
310 struct composite_context *c;
312 c = libnet_rpc_groupdel_send(p, io, NULL);
313 return libnet_rpc_groupdel_recv(c, mem_ctx, io);