]> git.samba.org - samba.git/blob - source4/libnet/groupman.c
Make sure prototypes are always included, make some functions static and
[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
178         /* send the request */
179         lookup_req = dcerpc_samr_LookupNames_send(p, c, &s->lookupname);
180         if (composite_nomem(lookup_req, c)) return c;
181
182         composite_continue_rpc(c, lookup_req, continue_groupdel_name_found, c);
183         return c;
184 }
185
186
187 static void continue_groupdel_name_found(struct rpc_request *req)
188 {
189         struct composite_context *c;
190         struct groupdel_state *s;
191         struct rpc_request *opengroup_req;
192
193         c = talloc_get_type(req->async.private_data, struct composite_context);
194         s = talloc_get_type(c->private_data, struct groupdel_state);
195
196         /* receive samr_LookupNames result */
197         c->status = dcerpc_ndr_request_recv(req);
198         if (!composite_is_ok(c)) return;
199
200         c->status = s->lookupname.out.result;
201         if (!NT_STATUS_IS_OK(c->status)) {
202                 composite_error(c, c->status);
203                 return;
204         }
205
206         /* what to do when there's no group account to delete
207            and what if there's more than one rid resolved */
208         if (!s->lookupname.out.rids.count) {
209                 c->status = NT_STATUS_NO_SUCH_GROUP;
210                 composite_error(c, c->status);
211                 return;
212
213         } else if (!s->lookupname.out.rids.count > 1) {
214                 c->status = NT_STATUS_INVALID_ACCOUNT_NAME;
215                 composite_error(c, c->status);
216                 return;
217         }
218
219         /* prepare the arguments for rpc call */
220         s->opengroup.in.domain_handle = &s->domain_handle;
221         s->opengroup.in.rid           = s->lookupname.out.rids.ids[0];
222         s->opengroup.in.access_mask   = SEC_FLAG_MAXIMUM_ALLOWED;
223         s->opengroup.out.group_handle  = &s->group_handle;
224
225         /* send rpc request */
226         opengroup_req = dcerpc_samr_OpenGroup_send(s->pipe, c, &s->opengroup);
227         if (composite_nomem(opengroup_req, c)) return;
228
229         composite_continue_rpc(c, opengroup_req, continue_groupdel_group_opened, c);
230 }
231
232
233 static void continue_groupdel_group_opened(struct rpc_request *req)
234 {
235         struct composite_context *c;
236         struct groupdel_state *s;
237         struct rpc_request *delgroup_req;
238
239         c = talloc_get_type(req->async.private_data, struct composite_context);
240         s = talloc_get_type(c->private_data, struct groupdel_state);
241
242         /* receive samr_OpenGroup result */
243         c->status = dcerpc_ndr_request_recv(req);
244         if (!composite_is_ok(c)) return;
245
246         c->status = s->opengroup.out.result;
247         if (!NT_STATUS_IS_OK(c->status)) {
248                 composite_error(c, c->status);
249                 return;
250         }
251         
252         /* prepare the final rpc call arguments */
253         s->deletegroup.in.group_handle   = &s->group_handle;
254         s->deletegroup.out.group_handle  = &s->group_handle;
255         
256         /* send rpc request */
257         delgroup_req = dcerpc_samr_DeleteDomainGroup_send(s->pipe, c, &s->deletegroup);
258         if (composite_nomem(delgroup_req, c)) return;
259
260         /* callback handler setup */
261         composite_continue_rpc(c, delgroup_req, continue_groupdel_deleted, c);
262 }
263
264
265 static void continue_groupdel_deleted(struct rpc_request *req)
266 {
267         struct composite_context *c;
268         struct groupdel_state *s;
269
270         c = talloc_get_type(req->async.private_data, struct composite_context);
271         s = talloc_get_type(c->private_data, struct groupdel_state);
272
273         /* receive samr_DeleteGroup result */
274         c->status = dcerpc_ndr_request_recv(req);
275         if (!composite_is_ok(c)) return;
276
277         /* return the actual function call status */
278         c->status = s->deletegroup.out.result;
279         if (!NT_STATUS_IS_OK(c->status)) {
280                 composite_error(c, c->status);
281                 return;
282         }
283         
284         composite_done(c);
285 }
286
287
288 NTSTATUS libnet_rpc_groupdel_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
289                                   struct libnet_rpc_groupdel *io)
290 {
291         NTSTATUS status;
292         struct groupdel_state *s;
293
294         status = composite_wait(c);
295         if (NT_STATUS_IS_OK(status) && io) {
296                 s = talloc_get_type(c->private_data, struct groupdel_state);
297                 io->out.group_handle = s->group_handle;
298         }
299
300         talloc_free(c);
301         return status;
302 }
303
304
305 NTSTATUS libnet_rpc_groupdel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
306                              struct libnet_rpc_groupdel *io)
307 {
308         struct composite_context *c;
309
310         c = libnet_rpc_groupdel_send(p, io, NULL);
311         return libnet_rpc_groupdel_recv(c, mem_ctx, io);
312 }