Add check for invalid data size.
[kai/samba.git] / source4 / libnet / libnet_group.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 #include "includes.h"
22 #include "libnet/libnet.h"
23 #include "libcli/composite/composite.h"
24 #include "librpc/gen_ndr/lsa.h"
25 #include "librpc/gen_ndr/ndr_lsa_c.h"
26 #include "librpc/gen_ndr/samr.h"
27 #include "librpc/gen_ndr/ndr_samr_c.h"
28 #include "libcli/security/security.h"
29
30
31 struct create_group_state {
32         struct libnet_context *ctx;
33         struct libnet_CreateGroup r;
34         struct libnet_DomainOpen domain_open;
35         struct libnet_rpc_groupadd group_add;
36
37         /* information about the progress */
38         void (*monitor_fn)(struct monitor_msg *);
39 };
40
41
42 static void continue_domain_opened(struct composite_context *ctx);
43 static void continue_rpc_group_added(struct composite_context *ctx);
44
45
46 struct composite_context* libnet_CreateGroup_send(struct libnet_context *ctx,
47                                                   TALLOC_CTX *mem_ctx,
48                                                   struct libnet_CreateGroup *r,
49                                                   void (*monitor)(struct monitor_msg*))
50 {
51         struct composite_context *c;
52         struct create_group_state *s;
53         struct composite_context *create_req;
54         bool prereq_met = false;
55
56         /* composite context allocation and setup */
57         c = composite_create(mem_ctx, ctx->event_ctx);
58         if (c == NULL) return NULL;
59
60         s = talloc_zero(c, struct create_group_state);
61         if (composite_nomem(s, c)) return c;
62
63         c->private_data = s;
64
65         s->ctx = ctx;
66         s->r   = *r;
67         ZERO_STRUCT(s->r.out);
68
69         /* prerequisite: make sure we have a valid samr domain handle */
70         prereq_met = samr_domain_opened(ctx, s->r.in.domain_name, &c, &s->domain_open,
71                                         continue_domain_opened, monitor);
72         if (!prereq_met) return c;
73
74         /* prepare arguments of rpc group add call */
75         s->group_add.in.groupname     = r->in.group_name;
76         s->group_add.in.domain_handle = ctx->samr.handle;
77
78         /* send the request */
79         create_req = libnet_rpc_groupadd_send(ctx->samr.pipe, &s->group_add, monitor);
80         if (composite_nomem(create_req, c)) return c;
81
82         composite_continue(c, create_req, continue_rpc_group_added, c);
83         return c;
84 }
85
86
87 static void continue_domain_opened(struct composite_context *ctx)
88 {
89         struct composite_context *c;
90         struct create_group_state *s;
91         struct composite_context *create_req;
92         
93         c = talloc_get_type(ctx->async.private_data, struct composite_context);
94         s = talloc_get_type(c->private_data, struct create_group_state);
95
96         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
97         if (!composite_is_ok(c)) return;
98
99         /* prepare arguments of groupadd call */
100         s->group_add.in.groupname     = s->r.in.group_name;
101         s->group_add.in.domain_handle = s->ctx->samr.handle;
102
103         /* send the request */
104         create_req = libnet_rpc_groupadd_send(s->ctx->samr.pipe, &s->group_add,
105                                               s->monitor_fn);
106         if (composite_nomem(create_req, c)) return;
107
108         composite_continue(c, create_req, continue_rpc_group_added, c);
109 }
110
111
112 static void continue_rpc_group_added(struct composite_context *ctx)
113 {
114         struct composite_context *c;
115         struct create_group_state *s;
116
117         c = talloc_get_type(ctx->async.private_data, struct composite_context);
118         s = talloc_get_type(c->private_data, struct create_group_state);
119
120         /* receive result of group add call */
121         c->status = libnet_rpc_groupadd_recv(ctx, c, &s->group_add);
122         if (!composite_is_ok(c)) return;
123
124         /* we're done */
125         composite_done(c);
126 }
127
128
129 /**
130  * Receive result of CreateGroup call
131  *
132  * @param c composite context returned by send request routine
133  * @param mem_ctx memory context of this call
134  * @param r pointer to a structure containing arguments and result of this call
135  * @return nt status
136  */
137 NTSTATUS libnet_CreateGroup_recv(struct composite_context *c,
138                                  TALLOC_CTX *mem_ctx,
139                                  struct libnet_CreateGroup *r)
140 {
141         NTSTATUS status;
142         struct create_group_state *s;
143
144         status = composite_wait(c);
145         if (!NT_STATUS_IS_OK(status)) {
146                 s = talloc_get_type(c->private_data, struct create_group_state);
147                 r->out.error_string = talloc_strdup(mem_ctx, nt_errstr(status));
148         }
149
150         talloc_free(c);
151         return status;
152 }
153
154
155 /**
156  * Create domain group
157  *
158  * @param ctx initialised libnet context
159  * @param mem_ctx memory context of this call
160  * @param io pointer to structure containing arguments and result of this call
161  * @return nt status
162  */
163 NTSTATUS libnet_CreateGroup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
164                             struct libnet_CreateGroup *io)
165 {
166         struct composite_context *c;
167
168         c = libnet_CreateGroup_send(ctx, mem_ctx, io, NULL);
169         return libnet_CreateGroup_recv(c, mem_ctx, io);
170 }
171
172
173 struct group_info_state {
174         struct libnet_context *ctx;
175         const char *domain_name;
176         enum libnet_GroupInfo_level level;
177         const char *group_name;
178         const char *sid_string;
179         struct libnet_LookupName lookup;
180         struct libnet_DomainOpen domopen;
181         struct libnet_rpc_groupinfo info;
182         
183         /* information about the progress */
184         void (*monitor_fn)(struct monitor_msg *);
185 };
186
187
188 static void continue_domain_open_info(struct composite_context *ctx);
189 static void continue_name_found(struct composite_context *ctx);
190 static void continue_group_info(struct composite_context *ctx);
191
192 /**
193  * Sends request to get group information
194  *
195  * @param ctx initialised libnet context
196  * @param mem_ctx memory context of this call
197  * @param io pointer to structure containing arguments the call
198  * @param monitor function pointer for receiving monitor messages
199  * @return composite context of this request
200  */
201 struct composite_context* libnet_GroupInfo_send(struct libnet_context *ctx,
202                                                 TALLOC_CTX *mem_ctx,
203                                                 struct libnet_GroupInfo *io,
204                                                 void (*monitor)(struct monitor_msg*))
205 {
206         struct composite_context *c;
207         struct group_info_state *s;
208         bool prereq_met = false;
209         struct composite_context *lookup_req, *info_req;
210
211         /* composite context allocation and setup */
212         c = composite_create(mem_ctx, ctx->event_ctx);
213         if (c == NULL) return NULL;
214
215         s = talloc_zero(c, struct group_info_state);
216         if (composite_nomem(s, c)) return c;
217
218         c->private_data = s;
219
220         /* store arguments in the state structure */
221         s->monitor_fn = monitor;
222         s->ctx = ctx;
223         s->domain_name = talloc_strdup(c, io->in.domain_name);
224         s->level = io->in.level;
225         switch(s->level) {
226         case GROUP_INFO_BY_NAME:
227                 s->group_name = talloc_strdup(c, io->in.data.group_name);
228                 s->sid_string = NULL;
229                 break;
230         case GROUP_INFO_BY_SID:
231                 s->group_name = NULL;
232                 s->sid_string = dom_sid_string(c, io->in.data.group_sid);
233                 break;
234         }
235
236         /* prerequisite: make sure the domain is opened */
237         prereq_met = samr_domain_opened(ctx, s->domain_name, &c, &s->domopen,
238                                         continue_domain_open_info, monitor);
239         if (!prereq_met) return c;
240
241         switch(s->level) {
242         case GROUP_INFO_BY_NAME:
243                 /* prepare arguments for LookupName call */
244                 s->lookup.in.name        = s->group_name;
245                 s->lookup.in.domain_name = s->domain_name;
246
247                 /* send the request */
248                 lookup_req = libnet_LookupName_send(s->ctx, c, &s->lookup, s->monitor_fn);
249                 if (composite_nomem(lookup_req, c)) return c;
250
251                 /* set the next stage */
252                 composite_continue(c, lookup_req, continue_name_found, c);
253                 break;
254         case GROUP_INFO_BY_SID:
255                 /* prepare arguments for groupinfo call */
256                 s->info.in.domain_handle = s->ctx->samr.handle;
257                 s->info.in.sid           = s->sid_string;
258                 /* we're looking for all information available */
259                 s->info.in.level         = GROUPINFOALL;
260
261                 /* send the request */
262                 info_req = libnet_rpc_groupinfo_send(s->ctx->samr.pipe, &s->info, s->monitor_fn);
263                 if (composite_nomem(info_req, c)) return c;
264
265                 /* set the next stage */
266                 composite_continue(c, info_req, continue_group_info, c);
267                 break;
268         }
269
270         return c;
271 }
272
273
274 /*
275  * Stage 0.5 (optional): receive opened domain and send lookup name request
276  */
277 static void continue_domain_open_info(struct composite_context *ctx)
278 {
279         struct composite_context *c;
280         struct group_info_state *s;
281         struct composite_context *lookup_req, *info_req;
282         
283         c = talloc_get_type(ctx->async.private_data, struct composite_context);
284         s = talloc_get_type(c->private_data, struct group_info_state);
285         
286         /* receive domain handle */
287         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
288         if (!composite_is_ok(c)) return;
289
290         switch(s->level) {
291         case GROUP_INFO_BY_NAME:
292                 /* prepare arguments for LookupName call */
293                 s->lookup.in.name        = s->group_name;
294                 s->lookup.in.domain_name = s->domain_name;
295
296                 /* send the request */
297                 lookup_req = libnet_LookupName_send(s->ctx, c, &s->lookup, s->monitor_fn);
298                 if (composite_nomem(lookup_req, c)) return;
299
300                 /* set the next stage */
301                 composite_continue(c, lookup_req, continue_name_found, c);
302                 break;
303         case GROUP_INFO_BY_SID:
304                 /* prepare arguments for groupinfo call */
305                 s->info.in.domain_handle = s->ctx->samr.handle;
306                 s->info.in.sid           = s->sid_string;
307                 /* we're looking for all information available */
308                 s->info.in.level         = GROUPINFOALL;
309
310                 /* send the request */
311                 info_req = libnet_rpc_groupinfo_send(s->ctx->samr.pipe, &s->info, s->monitor_fn);
312                 if (composite_nomem(info_req, c)) return;
313
314                 /* set the next stage */
315                 composite_continue(c, info_req, continue_group_info, c);
316                 break;
317
318         }
319 }
320
321
322 /*
323  * Stage 1: Receive SID found and send request for group info
324  */
325 static void continue_name_found(struct composite_context *ctx)
326 {
327         struct composite_context *c;
328         struct group_info_state *s;
329         struct composite_context *info_req;
330
331         c = talloc_get_type(ctx->async.private_data, struct composite_context);
332         s = talloc_get_type(c->private_data, struct group_info_state);
333
334         /* receive SID assiociated with name found */
335         c->status = libnet_LookupName_recv(ctx, c, &s->lookup);
336         if (!composite_is_ok(c)) return;
337
338         /* Is is a group SID actually ? */
339         if (s->lookup.out.sid_type != SID_NAME_DOM_GRP &&
340             s->lookup.out.sid_type != SID_NAME_ALIAS) {
341                 composite_error(c, NT_STATUS_NO_SUCH_GROUP);
342         }
343
344         /* prepare arguments for groupinfo call */
345         s->info.in.domain_handle = s->ctx->samr.handle;
346         s->info.in.groupname     = s->group_name;
347         s->info.in.sid           = s->lookup.out.sidstr;
348         /* we're looking for all information available */
349         s->info.in.level         = GROUPINFOALL;
350
351         /* send the request */
352         info_req = libnet_rpc_groupinfo_send(s->ctx->samr.pipe, &s->info, s->monitor_fn);
353         if (composite_nomem(info_req, c)) return;
354
355         /* set the next stage */
356         composite_continue(c, info_req, continue_group_info, c);
357 }
358
359
360 /*
361  * Stage 2: Receive group information
362  */
363 static void continue_group_info(struct composite_context *ctx)
364 {
365         struct composite_context *c;
366         struct group_info_state *s;
367
368         c = talloc_get_type(ctx->async.private_data, struct composite_context);
369         s = talloc_get_type(c->private_data, struct group_info_state);
370
371         /* receive group information */
372         c->status = libnet_rpc_groupinfo_recv(ctx, c, &s->info);
373         if (!composite_is_ok(c)) return;
374
375         /* we're done */
376         composite_done(c);
377 }
378
379
380 /*
381  * Receive group information
382  *
383  * @param c composite context returned by libnet_GroupInfo_send
384  * @param mem_ctx memory context of this call
385  * @param io pointer to structure receiving results of the call
386  * @result nt status
387  */
388 NTSTATUS libnet_GroupInfo_recv(struct composite_context* c, TALLOC_CTX *mem_ctx,
389                                struct libnet_GroupInfo *io)
390 {
391         NTSTATUS status;
392         struct group_info_state *s;
393         
394         status = composite_wait(c);
395         if (NT_STATUS_IS_OK(status)) {
396                 /* put the results into io structure if everything went fine */
397                 s = talloc_get_type(c->private_data, struct group_info_state);
398
399                 io->out.group_name = talloc_steal(mem_ctx,
400                                         s->info.out.info.all.name.string);
401                 io->out.group_sid = talloc_steal(mem_ctx, s->lookup.out.sid);
402                 io->out.num_members = s->info.out.info.all.num_members;
403                 io->out.description = talloc_steal(mem_ctx, s->info.out.info.all.description.string);
404
405                 io->out.error_string = talloc_strdup(mem_ctx, "Success");
406
407         } else {
408                 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
409         }
410
411         talloc_free(c);
412         return status;
413 }
414
415
416 /**
417  * Obtains specified group information
418  * 
419  * @param ctx initialised libnet context
420  * @param mem_ctx memory context of the call
421  * @param io pointer to a structure containing arguments and results of the call
422  */
423 NTSTATUS libnet_GroupInfo(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
424                           struct libnet_GroupInfo *io)
425 {
426         struct composite_context *c = libnet_GroupInfo_send(ctx, mem_ctx,
427                                                             io, NULL);
428         return libnet_GroupInfo_recv(c, mem_ctx, io);
429 }
430
431
432 struct grouplist_state {
433         struct libnet_context *ctx;
434         const char *domain_name;
435         struct lsa_DomainInfo dominfo;
436         int page_size;
437         uint32_t resume_index;
438         struct grouplist *groups;
439         uint32_t count;
440
441         struct libnet_DomainOpen domain_open;
442         struct lsa_QueryInfoPolicy query_domain;
443         struct samr_EnumDomainGroups group_list;
444
445         void (*monitor_fn)(struct monitor_msg*);
446 };
447
448
449 static void continue_lsa_domain_opened(struct composite_context *ctx);
450 static void continue_domain_queried(struct tevent_req *subreq);
451 static void continue_samr_domain_opened(struct composite_context *ctx);
452 static void continue_groups_enumerated(struct tevent_req *subreq);
453
454
455 /**
456  * Sends request to list (enumerate) group accounts
457  *
458  * @param ctx initialised libnet context
459  * @param mem_ctx memory context of this call
460  * @param io pointer to structure containing arguments and results of this call
461  * @param monitor function pointer for receiving monitor messages
462  * @return compostite context of this request
463  */
464 struct composite_context *libnet_GroupList_send(struct libnet_context *ctx,
465                                                 TALLOC_CTX *mem_ctx,
466                                                 struct libnet_GroupList *io,
467                                                 void (*monitor)(struct monitor_msg*))
468 {
469         struct composite_context *c;
470         struct grouplist_state *s;
471         struct tevent_req *subreq;
472         bool prereq_met = false;
473
474         /* composite context allocation and setup */
475         c = composite_create(mem_ctx, ctx->event_ctx);
476         if (c == NULL) return NULL;
477
478         s = talloc_zero(c, struct grouplist_state);
479         if (composite_nomem(s, c)) return c;
480         
481         c->private_data = s;
482
483         /* store the arguments in the state structure */
484         s->ctx          = ctx;
485         s->page_size    = io->in.page_size;
486         s->resume_index = io->in.resume_index;
487         s->domain_name  = talloc_strdup(c, io->in.domain_name);
488         s->monitor_fn   = monitor;
489
490         /* make sure we have lsa domain handle before doing anything */
491         prereq_met = lsa_domain_opened(ctx, s->domain_name, &c, &s->domain_open,
492                                        continue_lsa_domain_opened, monitor);
493         if (!prereq_met) return c;
494
495         /* prepare arguments of QueryDomainInfo call */
496         s->query_domain.in.handle = &ctx->lsa.handle;
497         s->query_domain.in.level  = LSA_POLICY_INFO_DOMAIN;
498         s->query_domain.out.info  = talloc_zero(c, union lsa_PolicyInformation *);
499         if (composite_nomem(s->query_domain.out.info, c)) return c;
500
501         /* send the request */
502         subreq = dcerpc_lsa_QueryInfoPolicy_r_send(s, c->event_ctx,
503                                                    ctx->lsa.pipe->binding_handle,
504                                                    &s->query_domain);
505         if (composite_nomem(subreq, c)) return c;
506         
507         tevent_req_set_callback(subreq, continue_domain_queried, c);
508         return c;
509 }
510
511
512 /*
513  * Stage 0.5 (optional): receive lsa domain handle and send
514  * request to query domain info
515  */
516 static void continue_lsa_domain_opened(struct composite_context *ctx)
517 {
518         struct composite_context *c;
519         struct grouplist_state *s;
520         struct tevent_req *subreq;
521         
522         c = talloc_get_type(ctx->async.private_data, struct composite_context);
523         s = talloc_get_type(c->private_data, struct grouplist_state);
524
525         /* receive lsa domain handle */
526         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
527         if (!composite_is_ok(c)) return;
528
529         /* prepare arguments of QueryDomainInfo call */
530         s->query_domain.in.handle = &s->ctx->lsa.handle;
531         s->query_domain.in.level  = LSA_POLICY_INFO_DOMAIN;
532         s->query_domain.out.info  = talloc_zero(c, union lsa_PolicyInformation *);
533         if (composite_nomem(s->query_domain.out.info, c)) return;
534
535         /* send the request */
536         subreq = dcerpc_lsa_QueryInfoPolicy_r_send(s, c->event_ctx,
537                                                    s->ctx->lsa.pipe->binding_handle,
538                                                    &s->query_domain);
539         if (composite_nomem(subreq, c)) return;
540
541         tevent_req_set_callback(subreq, continue_domain_queried, c);
542 }
543
544
545 /*
546  * Stage 1: receive domain info and request to enum groups
547  * provided a valid samr handle is opened
548  */
549 static void continue_domain_queried(struct tevent_req *subreq)
550 {
551         struct composite_context *c;
552         struct grouplist_state *s;
553         bool prereq_met = false;
554         
555         c = tevent_req_callback_data(subreq, struct composite_context);
556         s = talloc_get_type(c->private_data, struct grouplist_state);
557
558         /* receive result of rpc request */
559         c->status = dcerpc_lsa_QueryInfoPolicy_r_recv(subreq, s);
560         TALLOC_FREE(subreq);
561         if (!composite_is_ok(c)) return;
562
563         /* get the returned domain info */
564         s->dominfo = (*s->query_domain.out.info)->domain;
565
566         /* make sure we have samr domain handle before continuing */
567         prereq_met = samr_domain_opened(s->ctx, s->domain_name, &c, &s->domain_open,
568                                         continue_samr_domain_opened, s->monitor_fn);
569         if (!prereq_met) return;
570
571         /* prepare arguments od EnumDomainGroups call */
572         s->group_list.in.domain_handle  = &s->ctx->samr.handle;
573         s->group_list.in.max_size       = s->page_size;
574         s->group_list.in.resume_handle  = &s->resume_index;
575         s->group_list.out.resume_handle = &s->resume_index;
576         s->group_list.out.num_entries   = talloc(s, uint32_t);
577         if (composite_nomem(s->group_list.out.num_entries, c)) return;
578         s->group_list.out.sam           = talloc(s, struct samr_SamArray *);
579         if (composite_nomem(s->group_list.out.sam, c)) return;
580
581         /* send the request */
582         subreq = dcerpc_samr_EnumDomainGroups_r_send(s, c->event_ctx,
583                                                      s->ctx->samr.pipe->binding_handle,
584                                                      &s->group_list);
585         if (composite_nomem(subreq, c)) return;
586
587         tevent_req_set_callback(subreq, continue_groups_enumerated, c);
588 }
589
590
591 /*
592  * Stage 1.5 (optional): receive samr domain handle
593  * and request to enumerate accounts
594  */
595 static void continue_samr_domain_opened(struct composite_context *ctx)
596 {
597         struct composite_context *c;
598         struct grouplist_state *s;
599         struct tevent_req *subreq;
600
601         c = talloc_get_type(ctx->async.private_data, struct composite_context);
602         s = talloc_get_type(c->private_data, struct grouplist_state);
603
604         /* receive samr domain handle */
605         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
606         if (!composite_is_ok(c)) return;
607
608         /* prepare arguments of EnumDomainGroups call */
609         s->group_list.in.domain_handle  = &s->ctx->samr.handle;
610         s->group_list.in.max_size       = s->page_size;
611         s->group_list.in.resume_handle  = &s->resume_index;
612         s->group_list.out.resume_handle = &s->resume_index;
613         s->group_list.out.num_entries   = talloc(s, uint32_t);
614         if (composite_nomem(s->group_list.out.num_entries, c)) return;
615         s->group_list.out.sam           = talloc(s, struct samr_SamArray *);
616         if (composite_nomem(s->group_list.out.sam, c)) return;
617
618         /* send the request */
619         subreq = dcerpc_samr_EnumDomainGroups_r_send(s, c->event_ctx,
620                                                      s->ctx->samr.pipe->binding_handle,
621                                                      &s->group_list);
622         if (composite_nomem(subreq, c)) return;
623
624         tevent_req_set_callback(subreq, continue_groups_enumerated, c);
625 }
626
627
628 /*
629  * Stage 2: receive enumerated groups and their rids
630  */
631 static void continue_groups_enumerated(struct tevent_req *subreq)
632 {
633         struct composite_context *c;
634         struct grouplist_state *s;
635         uint32_t i;
636
637         c = tevent_req_callback_data(subreq, struct composite_context);
638         s = talloc_get_type(c->private_data, struct grouplist_state);
639
640         /* receive result of rpc request */
641         c->status = dcerpc_samr_EnumDomainGroups_r_recv(subreq, s);
642         TALLOC_FREE(subreq);
643         if (!composite_is_ok(c)) return;
644
645         /* get the actual status of the rpc call result
646            (instead of rpc layer) */
647         c->status = s->group_list.out.result;
648
649         /* we're interested in status "ok" as well as two
650            enum-specific status codes */
651         if (NT_STATUS_IS_OK(c->status) ||
652             NT_STATUS_EQUAL(c->status, STATUS_MORE_ENTRIES) ||
653             NT_STATUS_EQUAL(c->status, NT_STATUS_NO_MORE_ENTRIES)) {
654                 
655                 /* get enumerated accounts counter and resume handle (the latter allows
656                    making subsequent call to continue enumeration) */
657                 s->resume_index = *s->group_list.out.resume_handle;
658                 s->count        = *s->group_list.out.num_entries;
659
660                 /* prepare returned group accounts array */
661                 s->groups       = talloc_array(c, struct grouplist, (*s->group_list.out.sam)->count);
662                 if (composite_nomem(s->groups, c)) return;
663
664                 for (i = 0; i < (*s->group_list.out.sam)->count; i++) {
665                         struct dom_sid *group_sid;
666                         struct samr_SamEntry *entry = &(*s->group_list.out.sam)->entries[i];
667                         struct dom_sid *domain_sid = (*s->query_domain.out.info)->domain.sid;
668                         
669                         /* construct group sid from returned rid and queried domain sid */
670                         group_sid = dom_sid_add_rid(c, domain_sid, entry->idx);
671                         if (composite_nomem(group_sid, c)) return;
672
673                         /* groupname */
674                         s->groups[i].groupname = talloc_strdup(s->groups, entry->name.string);
675                         if (composite_nomem(s->groups[i].groupname, c)) return;
676
677                         /* sid string */
678                         s->groups[i].sid = dom_sid_string(s->groups, group_sid);
679                         if (composite_nomem(s->groups[i].sid, c)) return;
680                 }
681
682                 /* that's it */
683                 composite_done(c);
684
685         } else {
686                 /* something went wrong */
687                 composite_error(c, c->status);
688         }
689 }
690
691
692 /**
693  * Receive result of GroupList call
694  *
695  * @param c composite context returned by send request routine
696  * @param mem_ctx memory context of this call
697  * @param io pointer to structure containing arguments and result of this call
698  * @param nt status
699  */
700 NTSTATUS libnet_GroupList_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
701                                struct libnet_GroupList *io)
702 {
703         NTSTATUS status;
704         struct grouplist_state *s;
705
706         if (c == NULL || mem_ctx == NULL || io == NULL) {
707                 talloc_free(c);
708                 return NT_STATUS_INVALID_PARAMETER;
709         }
710
711         status = composite_wait(c);
712         if (NT_STATUS_IS_OK(status) ||
713             NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) ||
714             NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
715                 
716                 s = talloc_get_type(c->private_data, struct grouplist_state);
717                 
718                 /* get results from composite context */
719                 io->out.count        = s->count;
720                 io->out.resume_index = s->resume_index;
721                 io->out.groups       = talloc_steal(mem_ctx, s->groups);
722
723                 if (NT_STATUS_IS_OK(status)) {
724                         io->out.error_string = talloc_asprintf(mem_ctx, "Success");
725                 } else {
726                         /* success, but we're not done yet */
727                         io->out.error_string = talloc_asprintf(mem_ctx, "Success (status: %s)",
728                                                                nt_errstr(status));
729                 }
730
731         } else {
732                 io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
733         }
734
735         talloc_free(c);
736         return status;
737 }
738
739
740 /**
741  * Enumerate domain groups
742  *
743  * @param ctx initialised libnet context
744  * @param mem_ctx memory context of this call
745  * @param io pointer to structure containing arguments and result of this call
746  * @return nt status
747  */
748 NTSTATUS libnet_GroupList(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
749                           struct libnet_GroupList *io)
750 {
751         struct composite_context *c;
752
753         c = libnet_GroupList_send(ctx, mem_ctx, io, NULL);
754         return libnet_GroupList_recv(c, mem_ctx, io);
755 }