s4-samr: merge samr_LookupNames from s3 idl. (fixme: python)
[tprouty/samba.git] / source4 / libnet / groupman.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Rafal Szczesniak 2007
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 /*
21   a composite function for manipulating (add/edit/del) groups via samr pipe
22 */
23
24 #include "includes.h"
25 #include "libcli/composite/composite.h"
26 #include "libnet/composite.h"
27 #include "libnet/groupman.h"
28 #include "librpc/gen_ndr/ndr_samr_c.h"
29 #include "libnet/libnet_proto.h"
30
31
32 struct groupadd_state {
33         struct dcerpc_pipe *pipe;
34         struct policy_handle domain_handle;
35         struct samr_CreateDomainGroup creategroup;
36         struct policy_handle group_handle;
37         uint32_t group_rid;
38         
39         void (*monitor_fn)(struct monitor_msg*);
40 };
41
42
43 static void continue_groupadd_created(struct rpc_request *req);
44
45
46 struct composite_context* libnet_rpc_groupadd_send(struct dcerpc_pipe *p,
47                                                    struct libnet_rpc_groupadd *io,
48                                                    void (*monitor)(struct monitor_msg*))
49 {
50         struct composite_context *c;
51         struct groupadd_state *s;
52         struct rpc_request *create_req;
53
54         if (!p || !io) return NULL;
55
56         c = composite_create(p, dcerpc_event_context(p));
57         if (c == NULL) return NULL;
58
59         s = talloc_zero(c, struct groupadd_state);
60         if (composite_nomem(s, c)) return c;
61
62         c->private_data = s;
63
64         s->domain_handle = io->in.domain_handle;
65         s->pipe          = p;
66         s->monitor_fn    = monitor;
67
68         s->creategroup.in.domain_handle  = &s->domain_handle;
69
70         s->creategroup.in.name           = talloc_zero(c, struct lsa_String);
71         if (composite_nomem(s->creategroup.in.name, c)) return c;
72
73         s->creategroup.in.name->string   = talloc_strdup(c, io->in.groupname);
74         if (composite_nomem(s->creategroup.in.name->string, c)) return c;
75         
76         s->creategroup.in.access_mask    = 0;
77         
78         s->creategroup.out.group_handle  = &s->group_handle;
79         s->creategroup.out.rid           = &s->group_rid;
80         
81         create_req = dcerpc_samr_CreateDomainGroup_send(s->pipe, c, &s->creategroup);
82         if (composite_nomem(create_req, c)) return c;
83
84         composite_continue_rpc(c, create_req, continue_groupadd_created, c);
85         return c;
86 }
87
88
89 NTSTATUS libnet_rpc_groupadd_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
90                                   struct libnet_rpc_groupadd *io)
91 {
92         NTSTATUS status;
93         struct groupadd_state *s;
94         
95         status = composite_wait(c);
96         if (NT_STATUS_IS_OK(status)) {
97                 s = talloc_get_type(c, struct groupadd_state);
98         }
99
100         return status;
101 }
102
103
104 static void continue_groupadd_created(struct rpc_request *req)
105 {
106         struct composite_context *c;
107         struct groupadd_state *s;
108
109         c = talloc_get_type(req->async.private_data, struct composite_context);
110         s = talloc_get_type(c->private_data, struct groupadd_state);
111
112         c->status = dcerpc_ndr_request_recv(req);
113         if (!composite_is_ok(c)) return;
114
115         c->status = s->creategroup.out.result;
116         if (!composite_is_ok(c)) return;
117         
118         composite_done(c);
119 }
120
121
122 NTSTATUS libnet_rpc_groupadd(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
123                              struct libnet_rpc_groupadd *io)
124 {
125         struct composite_context *c;
126
127         c = libnet_rpc_groupadd_send(p, io, NULL);
128         return libnet_rpc_groupadd_recv(c, mem_ctx, io);
129 }
130
131
132 struct groupdel_state {
133         struct dcerpc_pipe             *pipe;
134         struct policy_handle           domain_handle;
135         struct policy_handle           group_handle;
136         struct samr_LookupNames        lookupname;
137         struct samr_OpenGroup          opengroup;
138         struct samr_DeleteDomainGroup  deletegroup;
139
140         /* information about the progress */
141         void (*monitor_fn)(struct monitor_msg *);
142 };
143
144
145 static void continue_groupdel_name_found(struct rpc_request *req);
146 static void continue_groupdel_group_opened(struct rpc_request *req);
147 static void continue_groupdel_deleted(struct rpc_request *req);
148
149
150 struct composite_context* libnet_rpc_groupdel_send(struct dcerpc_pipe *p,
151                                                    struct libnet_rpc_groupdel *io,
152                                                    void (*monitor)(struct monitor_msg*))
153 {
154         struct composite_context *c;
155         struct groupdel_state *s;
156         struct rpc_request *lookup_req;
157
158         /* composite context allocation and setup */
159         c = composite_create(p, dcerpc_event_context(p));
160         if (c == NULL) return NULL;
161
162         s = talloc_zero(c, struct groupdel_state);
163         if (composite_nomem(s, c)) return c;
164
165         c->private_data  = s;
166
167         /* store function parameters in the state structure */
168         s->pipe          = p;
169         s->domain_handle = io->in.domain_handle;
170         s->monitor_fn    = monitor;
171         
172         /* prepare parameters to send rpc request */
173         s->lookupname.in.domain_handle = &io->in.domain_handle;
174         s->lookupname.in.num_names     = 1;
175         s->lookupname.in.names         = talloc_zero(s, struct lsa_String);
176         s->lookupname.in.names->string = io->in.groupname;
177         s->lookupname.out.rids         = talloc_zero(s, struct samr_Ids);
178         s->lookupname.out.types        = talloc_zero(s, struct samr_Ids);
179         if (composite_nomem(s->lookupname.out.rids, c)) return c;
180         if (composite_nomem(s->lookupname.out.types, c)) return c;
181
182         /* send the request */
183         lookup_req = dcerpc_samr_LookupNames_send(p, c, &s->lookupname);
184         if (composite_nomem(lookup_req, c)) return c;
185
186         composite_continue_rpc(c, lookup_req, continue_groupdel_name_found, c);
187         return c;
188 }
189
190
191 static void continue_groupdel_name_found(struct rpc_request *req)
192 {
193         struct composite_context *c;
194         struct groupdel_state *s;
195         struct rpc_request *opengroup_req;
196
197         c = talloc_get_type(req->async.private_data, struct composite_context);
198         s = talloc_get_type(c->private_data, struct groupdel_state);
199
200         /* receive samr_LookupNames result */
201         c->status = dcerpc_ndr_request_recv(req);
202         if (!composite_is_ok(c)) return;
203
204         c->status = s->lookupname.out.result;
205         if (!NT_STATUS_IS_OK(c->status)) {
206                 composite_error(c, c->status);
207                 return;
208         }
209
210         /* what to do when there's no group account to delete
211            and what if there's more than one rid resolved */
212         if (!s->lookupname.out.rids->count) {
213                 c->status = NT_STATUS_NO_SUCH_GROUP;
214                 composite_error(c, c->status);
215                 return;
216
217         } else if (!s->lookupname.out.rids->count > 1) {
218                 c->status = NT_STATUS_INVALID_ACCOUNT_NAME;
219                 composite_error(c, c->status);
220                 return;
221         }
222
223         /* prepare the arguments for rpc call */
224         s->opengroup.in.domain_handle = &s->domain_handle;
225         s->opengroup.in.rid           = s->lookupname.out.rids->ids[0];
226         s->opengroup.in.access_mask   = SEC_FLAG_MAXIMUM_ALLOWED;
227         s->opengroup.out.group_handle  = &s->group_handle;
228
229         /* send rpc request */
230         opengroup_req = dcerpc_samr_OpenGroup_send(s->pipe, c, &s->opengroup);
231         if (composite_nomem(opengroup_req, c)) return;
232
233         composite_continue_rpc(c, opengroup_req, continue_groupdel_group_opened, c);
234 }
235
236
237 static void continue_groupdel_group_opened(struct rpc_request *req)
238 {
239         struct composite_context *c;
240         struct groupdel_state *s;
241         struct rpc_request *delgroup_req;
242
243         c = talloc_get_type(req->async.private_data, struct composite_context);
244         s = talloc_get_type(c->private_data, struct groupdel_state);
245
246         /* receive samr_OpenGroup result */
247         c->status = dcerpc_ndr_request_recv(req);
248         if (!composite_is_ok(c)) return;
249
250         c->status = s->opengroup.out.result;
251         if (!NT_STATUS_IS_OK(c->status)) {
252                 composite_error(c, c->status);
253                 return;
254         }
255         
256         /* prepare the final rpc call arguments */
257         s->deletegroup.in.group_handle   = &s->group_handle;
258         s->deletegroup.out.group_handle  = &s->group_handle;
259         
260         /* send rpc request */
261         delgroup_req = dcerpc_samr_DeleteDomainGroup_send(s->pipe, c, &s->deletegroup);
262         if (composite_nomem(delgroup_req, c)) return;
263
264         /* callback handler setup */
265         composite_continue_rpc(c, delgroup_req, continue_groupdel_deleted, c);
266 }
267
268
269 static void continue_groupdel_deleted(struct rpc_request *req)
270 {
271         struct composite_context *c;
272         struct groupdel_state *s;
273
274         c = talloc_get_type(req->async.private_data, struct composite_context);
275         s = talloc_get_type(c->private_data, struct groupdel_state);
276
277         /* receive samr_DeleteGroup result */
278         c->status = dcerpc_ndr_request_recv(req);
279         if (!composite_is_ok(c)) return;
280
281         /* return the actual function call status */
282         c->status = s->deletegroup.out.result;
283         if (!NT_STATUS_IS_OK(c->status)) {
284                 composite_error(c, c->status);
285                 return;
286         }
287         
288         composite_done(c);
289 }
290
291
292 NTSTATUS libnet_rpc_groupdel_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
293                                   struct libnet_rpc_groupdel *io)
294 {
295         NTSTATUS status;
296         struct groupdel_state *s;
297
298         status = composite_wait(c);
299         if (NT_STATUS_IS_OK(status) && io) {
300                 s = talloc_get_type(c->private_data, struct groupdel_state);
301                 io->out.group_handle = s->group_handle;
302         }
303
304         talloc_free(c);
305         return status;
306 }
307
308
309 NTSTATUS libnet_rpc_groupdel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
310                              struct libnet_rpc_groupdel *io)
311 {
312         struct composite_context *c;
313
314         c = libnet_rpc_groupdel_send(p, io, NULL);
315         return libnet_rpc_groupdel_recv(c, mem_ctx, io);
316 }