s4-libnet_vampire use a linked list to handle schema objects pending conversion
[kai/samba.git] / source4 / libnet / groupinfo.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 getting group information via samr pipe
22 */
23
24
25 #include "includes.h"
26 #include "libcli/composite/composite.h"
27 #include "librpc/gen_ndr/security.h"
28 #include "libcli/security/security.h"
29 #include "libnet/libnet.h"
30 #include "librpc/gen_ndr/ndr_samr_c.h"
31
32
33 struct groupinfo_state {
34         struct dcerpc_pipe         *pipe;
35         struct policy_handle       domain_handle;
36         struct policy_handle       group_handle;
37         uint16_t                   level;
38         struct samr_LookupNames    lookup;
39         struct samr_OpenGroup      opengroup;
40         struct samr_QueryGroupInfo querygroupinfo;
41         struct samr_Close          samrclose;
42         union  samr_GroupInfo      *info;
43
44         /* information about the progress */
45         void (*monitor_fn)(struct monitor_msg *);
46 };
47
48
49 static void continue_groupinfo_lookup(struct tevent_req *subreq);
50 static void continue_groupinfo_opengroup(struct tevent_req *subreq);
51 static void continue_groupinfo_getgroup(struct tevent_req *subreq);
52 static void continue_groupinfo_closegroup(struct tevent_req *subreq);
53
54
55 /**
56  * Stage 1 (optional): Look for a group name in SAM server.
57  */
58 static void continue_groupinfo_lookup(struct tevent_req *subreq)
59 {
60         struct composite_context *c;
61         struct groupinfo_state *s;
62         struct monitor_msg msg;
63         struct msg_rpc_lookup_name *msg_lookup;
64
65         c = tevent_req_callback_data(subreq, struct composite_context);
66         s = talloc_get_type(c->private_data, struct groupinfo_state);
67
68         /* receive samr_Lookup reply */
69         c->status = dcerpc_samr_LookupNames_r_recv(subreq, s);
70         TALLOC_FREE(subreq);
71         if (!composite_is_ok(c)) return;
72         
73         /* there could be a problem with name resolving itself */
74         if (!NT_STATUS_IS_OK(s->lookup.out.result)) {
75                 composite_error(c, s->lookup.out.result);
76                 return;
77         }
78
79         /* issue a monitor message */
80         if (s->monitor_fn) {
81                 msg.type = mon_SamrLookupName;
82                 msg_lookup = talloc(s, struct msg_rpc_lookup_name);
83                 msg_lookup->rid = s->lookup.out.rids->ids;
84                 msg_lookup->count = s->lookup.out.rids->count;
85                 msg.data = (void*)msg_lookup;
86                 msg.data_size = sizeof(*msg_lookup);
87                 
88                 s->monitor_fn(&msg);
89         }
90         
91
92         /* have we actually got name resolved
93            - we're looking for only one at the moment */
94         if (s->lookup.out.rids->count == 0) {
95                 composite_error(c, NT_STATUS_NO_SUCH_USER);
96         }
97
98         /* TODO: find proper status code for more than one rid found */
99
100         /* prepare parameters for LookupNames */
101         s->opengroup.in.domain_handle   = &s->domain_handle;
102         s->opengroup.in.access_mask     = SEC_FLAG_MAXIMUM_ALLOWED;
103         s->opengroup.in.rid             = s->lookup.out.rids->ids[0];
104         s->opengroup.out.group_handle   = &s->group_handle;
105
106         /* send request */
107         subreq = dcerpc_samr_OpenGroup_r_send(s, c->event_ctx,
108                                               s->pipe->binding_handle,
109                                               &s->opengroup);
110         if (composite_nomem(subreq, c)) return;
111
112         tevent_req_set_callback(subreq, continue_groupinfo_opengroup, c);
113 }
114
115
116 /**
117  * Stage 2: Open group policy handle.
118  */
119 static void continue_groupinfo_opengroup(struct tevent_req *subreq)
120 {
121         struct composite_context *c;
122         struct groupinfo_state *s;
123         struct monitor_msg msg;
124         struct msg_rpc_open_group *msg_open;
125
126         c = tevent_req_callback_data(subreq, struct composite_context);
127         s = talloc_get_type(c->private_data, struct groupinfo_state);
128
129         /* receive samr_OpenGroup reply */
130         c->status = dcerpc_samr_OpenGroup_r_recv(subreq, s);
131         TALLOC_FREE(subreq);
132         if (!composite_is_ok(c)) return;
133
134         if (!NT_STATUS_IS_OK(s->querygroupinfo.out.result)) {
135                 composite_error(c, s->querygroupinfo.out.result);
136                 return;
137         }
138
139         /* issue a monitor message */
140         if (s->monitor_fn) {
141                 msg.type = mon_SamrOpenGroup;
142                 msg_open = talloc(s, struct msg_rpc_open_group);
143                 msg_open->rid = s->opengroup.in.rid;
144                 msg_open->access_mask = s->opengroup.in.access_mask;
145                 msg.data = (void*)msg_open;
146                 msg.data_size = sizeof(*msg_open);
147                 
148                 s->monitor_fn(&msg);
149         }
150         
151         /* prepare parameters for QueryGroupInfo call */
152         s->querygroupinfo.in.group_handle = &s->group_handle;
153         s->querygroupinfo.in.level        = s->level;
154         s->querygroupinfo.out.info        = talloc(s, union samr_GroupInfo *);
155         if (composite_nomem(s->querygroupinfo.out.info, c)) return;
156         
157         /* queue rpc call, set event handling and new state */
158         subreq = dcerpc_samr_QueryGroupInfo_r_send(s,
159                                                    c->event_ctx,
160                                                    s->pipe->binding_handle,
161                                                    &s->querygroupinfo);
162         if (composite_nomem(subreq, c)) return;
163         
164         tevent_req_set_callback(subreq, continue_groupinfo_getgroup, c);
165 }
166
167
168 /**
169  * Stage 3: Get requested group information.
170  */
171 static void continue_groupinfo_getgroup(struct tevent_req *subreq)
172 {
173         struct composite_context *c;
174         struct groupinfo_state *s;
175         struct monitor_msg msg;
176         struct msg_rpc_query_group *msg_query;
177
178         c = tevent_req_callback_data(subreq, struct composite_context);
179         s = talloc_get_type(c->private_data, struct groupinfo_state);
180
181         /* receive samr_QueryGroupInfo reply */
182         c->status = dcerpc_samr_QueryGroupInfo_r_recv(subreq, s);
183         TALLOC_FREE(subreq);
184         if (!composite_is_ok(c)) return;
185
186         /* check if querygroup itself went ok */
187         if (!NT_STATUS_IS_OK(s->querygroupinfo.out.result)) {
188                 composite_error(c, s->querygroupinfo.out.result);
189                 return;
190         }
191
192         s->info = talloc_steal(s, *s->querygroupinfo.out.info);
193
194         /* issue a monitor message */
195         if (s->monitor_fn) {
196                 msg.type = mon_SamrQueryGroup;
197                 msg_query = talloc(s, struct msg_rpc_query_group);
198                 msg_query->level = s->querygroupinfo.in.level;
199                 msg.data = (void*)msg_query;
200                 msg.data_size = sizeof(*msg_query);
201                 
202                 s->monitor_fn(&msg);
203         }
204         
205         /* prepare arguments for Close call */
206         s->samrclose.in.handle  = &s->group_handle;
207         s->samrclose.out.handle = &s->group_handle;
208         
209         /* queue rpc call, set event handling and new state */
210         subreq = dcerpc_samr_Close_r_send(s, c->event_ctx,
211                                           s->pipe->binding_handle,
212                                           &s->samrclose);
213         if (composite_nomem(subreq, c)) return;
214         
215         tevent_req_set_callback(subreq, continue_groupinfo_closegroup, c);
216 }
217
218
219 /**
220  * Stage 4: Close policy handle associated with opened group.
221  */
222 static void continue_groupinfo_closegroup(struct tevent_req *subreq)
223 {
224         struct composite_context *c;
225         struct groupinfo_state *s;
226         struct monitor_msg msg;
227         struct msg_rpc_close_group *msg_close;
228
229         c = tevent_req_callback_data(subreq, struct composite_context);
230         s = talloc_get_type(c->private_data, struct groupinfo_state);
231
232         /* receive samr_Close reply */
233         c->status = dcerpc_samr_Close_r_recv(subreq, s);
234         TALLOC_FREE(subreq);
235         if (!composite_is_ok(c)) return;
236
237         if (!NT_STATUS_IS_OK(s->samrclose.out.result)) {
238                 composite_error(c, s->samrclose.out.result);
239                 return;
240         }
241
242         /* issue a monitor message */
243         if (s->monitor_fn) {
244                 msg.type = mon_SamrClose;
245                 msg_close = talloc(s, struct msg_rpc_close_group);
246                 msg_close->rid = s->opengroup.in.rid;
247                 msg.data = (void*)msg_close;
248                 msg.data_size = sizeof(*msg_close);
249
250                 s->monitor_fn(&msg);
251         }
252
253         composite_done(c);
254 }
255
256
257 /**
258  * Sends asynchronous groupinfo request
259  *
260  * @param p dce/rpc call pipe 
261  * @param io arguments and results of the call
262  */
263 struct composite_context *libnet_rpc_groupinfo_send(struct dcerpc_pipe *p,
264                                                     struct libnet_rpc_groupinfo *io,
265                                                     void (*monitor)(struct monitor_msg*))
266 {
267         struct composite_context *c;
268         struct groupinfo_state *s;
269         struct dom_sid *sid;
270         struct tevent_req *subreq;
271
272         if (!p || !io) return NULL;
273         
274         c = composite_create(p, dcerpc_event_context(p));
275         if (c == NULL) return c;
276         
277         s = talloc_zero(c, struct groupinfo_state);
278         if (composite_nomem(s, c)) return c;
279
280         c->private_data = s;
281
282         s->level         = io->in.level;
283         s->pipe          = p;
284         s->domain_handle = io->in.domain_handle;
285         s->monitor_fn    = monitor;
286
287         if (io->in.sid) {
288                 sid = dom_sid_parse_talloc(s, io->in.sid);
289                 if (composite_nomem(sid, c)) return c;
290
291                 s->opengroup.in.domain_handle  = &s->domain_handle;
292                 s->opengroup.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
293                 s->opengroup.in.rid            = sid->sub_auths[sid->num_auths - 1];
294                 s->opengroup.out.group_handle  = &s->group_handle;
295                 
296                 /* send request */
297                 subreq = dcerpc_samr_OpenGroup_r_send(s, c->event_ctx,
298                                                       p->binding_handle,
299                                                       &s->opengroup);
300                 if (composite_nomem(subreq, c)) return c;
301
302                 tevent_req_set_callback(subreq, continue_groupinfo_opengroup, c);
303
304         } else {
305                 /* preparing parameters to send rpc request */
306                 s->lookup.in.domain_handle    = &s->domain_handle;
307                 s->lookup.in.num_names        = 1;
308                 s->lookup.in.names            = talloc_array(s, struct lsa_String, 1);
309                 if (composite_nomem(s->lookup.in.names, c)) return c;
310
311                 s->lookup.in.names[0].string  = talloc_strdup(s, io->in.groupname);
312                 if (composite_nomem(s->lookup.in.names[0].string, c)) return c;
313                 s->lookup.out.rids         = talloc_zero(s, struct samr_Ids);
314                 s->lookup.out.types        = talloc_zero(s, struct samr_Ids);
315                 if (composite_nomem(s->lookup.out.rids, c)) return c;
316                 if (composite_nomem(s->lookup.out.types, c)) return c;
317
318                 /* send request */
319                 subreq = dcerpc_samr_LookupNames_r_send(s, c->event_ctx,
320                                                         p->binding_handle,
321                                                         &s->lookup);
322                 if (composite_nomem(subreq, c)) return c;
323                 
324                 tevent_req_set_callback(subreq, continue_groupinfo_lookup, c);
325         }
326
327         return c;
328 }
329
330
331 /**
332  * Waits for and receives result of asynchronous groupinfo call
333  * 
334  * @param c composite context returned by asynchronous groupinfo call
335  * @param mem_ctx memory context of the call
336  * @param io pointer to results (and arguments) of the call
337  * @return nt status code of execution
338  */
339
340 NTSTATUS libnet_rpc_groupinfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
341                                    struct libnet_rpc_groupinfo *io)
342 {
343         NTSTATUS status;
344         struct groupinfo_state *s;
345         
346         /* wait for results of sending request */
347         status = composite_wait(c);
348         
349         if (NT_STATUS_IS_OK(status) && io) {
350                 s = talloc_get_type(c->private_data, struct groupinfo_state);
351                 talloc_steal(mem_ctx, s->info);
352                 io->out.info = *s->info;
353         }
354         
355         /* memory context associated to composite context is no longer needed */
356         talloc_free(c);
357         return status;
358 }
359
360
361 /**
362  * Synchronous version of groupinfo call
363  *
364  * @param pipe dce/rpc call pipe
365  * @param mem_ctx memory context for the call
366  * @param io arguments and results of the call
367  * @return nt status code of execution
368  */
369
370 NTSTATUS libnet_rpc_groupinfo(struct dcerpc_pipe *p,
371                               TALLOC_CTX *mem_ctx,
372                               struct libnet_rpc_groupinfo *io)
373 {
374         struct composite_context *c = libnet_rpc_groupinfo_send(p, io, NULL);
375         return libnet_rpc_groupinfo_recv(c, mem_ctx, io);
376 }