summaryrefslogtreecommitdiff
path: root/source4/libnet/libnet_group.c
blob: 7679b426107fa24763d31d22c494d82f00668807 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/* 
   Unix SMB/CIFS implementation.
   
   Copyright (C) Rafal Szczesniak  2007
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "includes.h"
#include "libnet/libnet.h"
#include "libcli/composite/composite.h"
#include "librpc/gen_ndr/lsa.h"
#include "librpc/gen_ndr/ndr_lsa_c.h"
#include "librpc/gen_ndr/samr.h"
#include "librpc/gen_ndr/ndr_samr_c.h"
#include "libcli/security/security.h"


struct create_group_state {
	struct libnet_context *ctx;
	struct libnet_CreateGroup r;
	struct libnet_DomainOpen domain_open;
	struct libnet_rpc_groupadd group_add;

	/* information about the progress */
	void (*monitor_fn)(struct monitor_msg *);
};


static void continue_domain_opened(struct composite_context *ctx);
static void continue_rpc_group_added(struct composite_context *ctx);


struct composite_context* libnet_CreateGroup_send(struct libnet_context *ctx,
						  TALLOC_CTX *mem_ctx,
						  struct libnet_CreateGroup *r,
						  void (*monitor)(struct monitor_msg*))
{
	struct composite_context *c;
	struct create_group_state *s;
	struct composite_context *create_req;
	bool prereq_met = false;

	/* composite context allocation and setup */
	c = composite_create(mem_ctx, ctx->event_ctx);
	if (c == NULL) return NULL;

	s = talloc_zero(c, struct create_group_state);
	if (composite_nomem(s, c)) return c;

	c->private_data = s;

	s->ctx = ctx;
	s->r   = *r;
	ZERO_STRUCT(s->r.out);

	/* prerequisite: make sure we have a valid samr domain handle */
	prereq_met = samr_domain_opened(ctx, s->r.in.domain_name, &c, &s->domain_open,
					continue_domain_opened, monitor);
	if (!prereq_met) return c;

	/* prepare arguments of rpc group add call */
	s->group_add.in.groupname     = r->in.group_name;
	s->group_add.in.domain_handle = ctx->samr.handle;

	/* send the request */
	create_req = libnet_rpc_groupadd_send(ctx->samr.pipe, &s->group_add, monitor);
	if (composite_nomem(create_req, c)) return c;

	composite_continue(c, create_req, continue_rpc_group_added, c);
	return c;
}


static void continue_domain_opened(struct composite_context *ctx)
{
	struct composite_context *c;
	struct create_group_state *s;
	struct composite_context *create_req;
	
	c = talloc_get_type(ctx->async.private_data, struct composite_context);
	s = talloc_get_type(c->private_data, struct create_group_state);

	c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
	if (!composite_is_ok(c)) return;

	/* prepare arguments of groupadd call */
	s->group_add.in.groupname     = s->r.in.group_name;
	s->group_add.in.domain_handle = s->ctx->samr.handle;

	/* send the request */
	create_req = libnet_rpc_groupadd_send(s->ctx->samr.pipe, &s->group_add,
					      s->monitor_fn);
	if (composite_nomem(create_req, c)) return;

	composite_continue(c, create_req, continue_rpc_group_added, c);
}


static void continue_rpc_group_added(struct composite_context *ctx)
{
	struct composite_context *c;
	struct create_group_state *s;

	c = talloc_get_type(ctx->async.private_data, struct composite_context);
	s = talloc_get_type(c->private_data, struct create_group_state);

	/* receive result of group add call */
	c->status = libnet_rpc_groupadd_recv(ctx, c, &s->group_add);
	if (!composite_is_ok(c)) return;

	/* we're done */
	composite_done(c);
}


/**
 * Receive result of CreateGroup call
 *
 * @param c composite context returned by send request routine
 * @param mem_ctx memory context of this call
 * @param r pointer to a structure containing arguments and result of this call
 * @return nt status
 */
NTSTATUS libnet_CreateGroup_recv(struct composite_context *c,
				 TALLOC_CTX *mem_ctx,
				 struct libnet_CreateGroup *r)
{
	NTSTATUS status;
	struct create_group_state *s;

	status = composite_wait(c);
	if (!NT_STATUS_IS_OK(status)) {
		s = talloc_get_type(c->private_data, struct create_group_state);
		r->out.error_string = talloc_strdup(mem_ctx, nt_errstr(status));
	}

	talloc_free(c);
	return status;
}


/**
 * Create domain group
 *
 * @param ctx initialised libnet context
 * @param mem_ctx memory context of this call
 * @param io pointer to structure containing arguments and result of this call
 * @return nt status
 */
NTSTATUS libnet_CreateGroup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
			    struct libnet_CreateGroup *io)
{
	struct composite_context *c;

	c = libnet_CreateGroup_send(ctx, mem_ctx, io, NULL);
	return libnet_CreateGroup_recv(c, mem_ctx, io);
}


struct group_info_state {
	struct libnet_context *ctx;
	const char *domain_name;
	enum libnet_GroupInfo_level level;
	const char *group_name;
	const char *sid_string;
	struct libnet_LookupName lookup;
	struct libnet_DomainOpen domopen;
	struct libnet_rpc_groupinfo info;
	
	/* information about the progress */
	void (*monitor_fn)(struct monitor_msg *);
};


static void continue_domain_open_info(struct composite_context *ctx);
static void continue_name_found(struct composite_context *ctx);
static void continue_group_info(struct composite_context *ctx);

/**
 * Sends request to get group information
 *
 * @param ctx initialised libnet context
 * @param mem_ctx memory context of this call
 * @param io pointer to structure containing arguments the call
 * @param monitor function pointer for receiving monitor messages
 * @return composite context of this request
 */
struct composite_context* libnet_GroupInfo_send(struct libnet_context *ctx,
						TALLOC_CTX *mem_ctx,
						struct libnet_GroupInfo *io,
						void (*monitor)(struct monitor_msg*))
{
	struct composite_context *c;
	struct group_info_state *s;
	bool prereq_met = false;
	struct composite_context *lookup_req, *info_req;

	/* composite context allocation and setup */
	c = composite_create(mem_ctx, ctx->event_ctx);
	if (c == NULL) return NULL;

	s = talloc_zero(c, struct group_info_state);
	if (composite_nomem(s, c)) return c;

	c->private_data = s;

	/* store arguments in the state structure */
	s->monitor_fn = monitor;
	s->ctx = ctx;
	s->domain_name = talloc_strdup(c, io->in.domain_name);
	s->level = io->in.level;
	switch(s->level) {
	case GROUP_INFO_BY_NAME:
		s->group_name = talloc_strdup(c, io->in.data.group_name);
		s->sid_string = NULL;
		break;
	case GROUP_INFO_BY_SID:
		s->group_name = NULL;
		s->sid_string = dom_sid_string(c, io->in.data.group_sid);
		break;
	}

	/* prerequisite: make sure the domain is opened */
	prereq_met = samr_domain_opened(ctx, s->domain_name, &c, &s->domopen,
					continue_domain_open_info, monitor);
	if (!prereq_met) return c;

	switch(s->level) {
	case GROUP_INFO_BY_NAME:
		/* prepare arguments for LookupName call */
		s->lookup.in.name        = s->group_name;
		s->lookup.in.domain_name = s->domain_name;

		/* send the request */
		lookup_req = libnet_LookupName_send(s->ctx, c, &s->lookup, s->monitor_fn);
		if (composite_nomem(lookup_req, c)) return c;

		/* set the next stage */
		composite_continue(c, lookup_req, continue_name_found, c);
		break;
	case GROUP_INFO_BY_SID:
		/* prepare arguments for groupinfo call */
		s->info.in.domain_handle = s->ctx->samr.handle;
		s->info.in.sid           = s->sid_string;
		/* we're looking for all information available */
		s->info.in.level         = GROUPINFOALL;

		/* send the request */
		info_req = libnet_rpc_groupinfo_send(s->ctx->samr.pipe, &s->info, s->monitor_fn);
		if (composite_nomem(info_req, c)) return c;

		/* set the next stage */
		composite_continue(c, info_req, continue_group_info, c);
		break;
	}

	return c;
}


/*
 * Stage 0.5 (optional): receive opened domain and send lookup name request
 */
static void continue_domain_open_info(struct composite_context *ctx)
{
	struct composite_context *c;
	struct group_info_state *s;
	struct composite_context *lookup_req, *info_req;
	
	c = talloc_get_type(ctx->async.private_data, struct composite_context);
	s = talloc_get_type(c->private_data, struct group_info_state);
	
	/* receive domain handle */
	c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
	if (!composite_is_ok(c)) return;

	switch(s->level) {
	case GROUP_INFO_BY_NAME:
		/* prepare arguments for LookupName call */
		s->lookup.in.name        = s->group_name;
		s->lookup.in.domain_name = s->domain_name;

		/* send the request */
		lookup_req = libnet_LookupName_send(s->ctx, c, &s->lookup, s->monitor_fn);
		if (composite_nomem(lookup_req, c)) return;

		/* set the next stage */
		composite_continue(c, lookup_req, continue_name_found, c);
		break;
	case GROUP_INFO_BY_SID:
		/* prepare arguments for groupinfo call */
		s->info.in.domain_handle = s->ctx->samr.handle;
		s->info.in.sid           = s->sid_string;
		/* we're looking for all information available */
		s->info.in.level         = GROUPINFOALL;

		/* send the request */
		info_req = libnet_rpc_groupinfo_send(s->ctx->samr.pipe, &s->info, s->monitor_fn);
		if (composite_nomem(info_req, c)) return;

		/* set the next stage */
		composite_continue(c, info_req, continue_group_info, c);
		break;

	}
}


/*
 * Stage 1: Receive SID found and send request for group info
 */
static void continue_name_found(struct composite_context *ctx)
{
	struct composite_context *c;
	struct group_info_state *s;
	struct composite_context *info_req;

	c = talloc_get_type(ctx->async.private_data, struct composite_context);
	s = talloc_get_type(c->private_data, struct group_info_state);

	/* receive SID assiociated with name found */
	c->status = libnet_LookupName_recv(ctx, c, &s->lookup);
	if (!composite_is_ok(c)) return;

	/* Is is a group SID actually ? */
	if (s->lookup.out.sid_type != SID_NAME_DOM_GRP &&
	    s->lookup.out.sid_type != SID_NAME_ALIAS) {
		composite_error(c, NT_STATUS_NO_SUCH_GROUP);
	}

	/* prepare arguments for groupinfo call */
	s->info.in.domain_handle = s->ctx->samr.handle;
	s->info.in.groupname     = s->group_name;
	s->info.in.sid           = s->lookup.out.sidstr;
	/* we're looking for all information available */
	s->info.in.level         = GROUPINFOALL;

	/* send the request */
	info_req = libnet_rpc_groupinfo_send(s->ctx->samr.pipe, &s->info, s->monitor_fn);
	if (composite_nomem(info_req, c)) return;

	/* set the next stage */
	composite_continue(c, info_req, continue_group_info, c);
}


/*
 * Stage 2: Receive group information
 */
static void continue_group_info(struct composite_context *ctx)
{
	struct composite_context *c;
	struct group_info_state *s;

	c = talloc_get_type(ctx->async.private_data, struct composite_context);
	s = talloc_get_type(c->private_data, struct group_info_state);

	/* receive group information */
	c->status = libnet_rpc_groupinfo_recv(ctx, c, &s->info);
	if (!composite_is_ok(c)) return;

	/* we're done */
	composite_done(c);
}


/*
 * Receive group information
 *
 * @param c composite context returned by libnet_GroupInfo_send
 * @param mem_ctx memory context of this call
 * @param io pointer to structure receiving results of the call
 * @result nt status
 */
NTSTATUS libnet_GroupInfo_recv(struct composite_context* c, TALLOC_CTX *mem_ctx,
			       struct libnet_GroupInfo *io)
{
	NTSTATUS status;
	struct group_info_state *s;
	
	status = composite_wait(c);
	if (NT_STATUS_IS_OK(status)) {
		/* put the results into io structure if everything went fine */
		s = talloc_get_type(c->private_data, struct group_info_state);

		io->out.group_name = talloc_steal(mem_ctx,
					s->info.out.info.all.name.string);
		io->out.group_sid = talloc_steal(mem_ctx, s->lookup.out.sid);
		io->out.num_members = s->info.out.info.all.num_members;
		io->out.description = talloc_steal(mem_ctx, s->info.out.info.all.description.string);

		io->out.error_string = talloc_strdup(mem_ctx, "Success");

	} else {
		io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
	}

	talloc_free(c);
	return status;
}


/**
 * Obtains specified group information
 * 
 * @param ctx initialised libnet context
 * @param mem_ctx memory context of the call
 * @param io pointer to a structure containing arguments and results of the call
 */
NTSTATUS libnet_GroupInfo(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
			  struct libnet_GroupInfo *io)
{
	struct composite_context *c = libnet_GroupInfo_send(ctx, mem_ctx,
							    io, NULL);
	return libnet_GroupInfo_recv(c, mem_ctx, io);
}


struct grouplist_state {
	struct libnet_context *ctx;
	const char *domain_name;
	struct lsa_DomainInfo dominfo;
	int page_size;
	uint32_t resume_index;
	struct grouplist *groups;
	uint32_t count;

	struct libnet_DomainOpen domain_open;
	struct lsa_QueryInfoPolicy query_domain;
	struct samr_EnumDomainGroups group_list;

	void (*monitor_fn)(struct monitor_msg*);
};


static void continue_lsa_domain_opened(struct composite_context *ctx);
static void continue_domain_queried(struct tevent_req *subreq);
static void continue_samr_domain_opened(struct composite_context *ctx);
static void continue_groups_enumerated(struct tevent_req *subreq);


/**
 * Sends request to list (enumerate) group accounts
 *
 * @param ctx initialised libnet context
 * @param mem_ctx memory context of this call
 * @param io pointer to structure containing arguments and results of this call
 * @param monitor function pointer for receiving monitor messages
 * @return compostite context of this request
 */
struct composite_context *libnet_GroupList_send(struct libnet_context *ctx,
						TALLOC_CTX *mem_ctx,
						struct libnet_GroupList *io,
						void (*monitor)(struct monitor_msg*))
{
	struct composite_context *c;
	struct grouplist_state *s;
	struct tevent_req *subreq;
	bool prereq_met = false;

	/* composite context allocation and setup */
	c = composite_create(mem_ctx, ctx->event_ctx);
	if (c == NULL) return NULL;

	s = talloc_zero(c, struct grouplist_state);
	if (composite_nomem(s, c)) return c;
	
	c->private_data = s;

	/* store the arguments in the state structure */
	s->ctx          = ctx;
	s->page_size    = io->in.page_size;
	s->resume_index = io->in.resume_index;
	s->domain_name  = talloc_strdup(c, io->in.domain_name);
	s->monitor_fn   = monitor;

	/* make sure we have lsa domain handle before doing anything */
	prereq_met = lsa_domain_opened(ctx, s->domain_name, &c, &s->domain_open,
				       continue_lsa_domain_opened, monitor);
	if (!prereq_met) return c;

	/* prepare arguments of QueryDomainInfo call */
	s->query_domain.in.handle = &ctx->lsa.handle;
	s->query_domain.in.level  = LSA_POLICY_INFO_DOMAIN;
	s->query_domain.out.info  = talloc_zero(c, union lsa_PolicyInformation *);
	if (composite_nomem(s->query_domain.out.info, c)) return c;

	/* send the request */
	subreq = dcerpc_lsa_QueryInfoPolicy_r_send(s, c->event_ctx,
						   ctx->lsa.pipe->binding_handle,
						   &s->query_domain);
	if (composite_nomem(subreq, c)) return c;
	
	tevent_req_set_callback(subreq, continue_domain_queried, c);
	return c;
}


/*
 * Stage 0.5 (optional): receive lsa domain handle and send
 * request to query domain info
 */
static void continue_lsa_domain_opened(struct composite_context *ctx)
{
	struct composite_context *c;
	struct grouplist_state *s;
	struct tevent_req *subreq;
	
	c = talloc_get_type(ctx->async.private_data, struct composite_context);
	s = talloc_get_type(c->private_data, struct grouplist_state);

	/* receive lsa domain handle */
	c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
	if (!composite_is_ok(c)) return;

	/* prepare arguments of QueryDomainInfo call */
	s->query_domain.in.handle = &s->ctx->lsa.handle;
	s->query_domain.in.level  = LSA_POLICY_INFO_DOMAIN;
	s->query_domain.out.info  = talloc_zero(c, union lsa_PolicyInformation *);
	if (composite_nomem(s->query_domain.out.info, c)) return;

	/* send the request */
	subreq = dcerpc_lsa_QueryInfoPolicy_r_send(s, c->event_ctx,
						   s->ctx->lsa.pipe->binding_handle,
						   &s->query_domain);
	if (composite_nomem(subreq, c)) return;

	tevent_req_set_callback(subreq, continue_domain_queried, c);
}


/*
 * Stage 1: receive domain info and request to enum groups
 * provided a valid samr handle is opened
 */
static void continue_domain_queried(struct tevent_req *subreq)
{
	struct composite_context *c;
	struct grouplist_state *s;
	bool prereq_met = false;
	
	c = tevent_req_callback_data(subreq, struct composite_context);
	s = talloc_get_type(c->private_data, struct grouplist_state);

	/* receive result of rpc request */
	c->status = dcerpc_lsa_QueryInfoPolicy_r_recv(subreq, s);
	TALLOC_FREE(subreq);
	if (!composite_is_ok(c)) return;

	/* get the returned domain info */
	s->dominfo = (*s->query_domain.out.info)->domain;

	/* make sure we have samr domain handle before continuing */
	prereq_met = samr_domain_opened(s->ctx, s->domain_name, &c, &s->domain_open,
					continue_samr_domain_opened, s->monitor_fn);
	if (!prereq_met) return;

	/* prepare arguments od EnumDomainGroups call */
	s->group_list.in.domain_handle  = &s->ctx->samr.handle;
	s->group_list.in.max_size       = s->page_size;
	s->group_list.in.resume_handle  = &s->resume_index;
	s->group_list.out.resume_handle = &s->resume_index;
	s->group_list.out.num_entries   = talloc(s, uint32_t);
	if (composite_nomem(s->group_list.out.num_entries, c)) return;
	s->group_list.out.sam           = talloc(s, struct samr_SamArray *);
	if (composite_nomem(s->group_list.out.sam, c)) return;

	/* send the request */
	subreq = dcerpc_samr_EnumDomainGroups_r_send(s, c->event_ctx,
						     s->ctx->samr.pipe->binding_handle,
						     &s->group_list);
	if (composite_nomem(subreq, c)) return;

	tevent_req_set_callback(subreq, continue_groups_enumerated, c);
}


/*
 * Stage 1.5 (optional): receive samr domain handle
 * and request to enumerate accounts
 */
static void continue_samr_domain_opened(struct composite_context *ctx)
{
	struct composite_context *c;
	struct grouplist_state *s;
	struct tevent_req *subreq;

	c = talloc_get_type(ctx->async.private_data, struct composite_context);
	s = talloc_get_type(c->private_data, struct grouplist_state);

	/* receive samr domain handle */
	c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
	if (!composite_is_ok(c)) return;

	/* prepare arguments of EnumDomainGroups call */
	s->group_list.in.domain_handle  = &s->ctx->samr.handle;
	s->group_list.in.max_size       = s->page_size;
	s->group_list.in.resume_handle  = &s->resume_index;
	s->group_list.out.resume_handle = &s->resume_index;
	s->group_list.out.num_entries   = talloc(s, uint32_t);
	if (composite_nomem(s->group_list.out.num_entries, c)) return;
	s->group_list.out.sam           = talloc(s, struct samr_SamArray *);
	if (composite_nomem(s->group_list.out.sam, c)) return;

	/* send the request */
	subreq = dcerpc_samr_EnumDomainGroups_r_send(s, c->event_ctx,
						     s->ctx->samr.pipe->binding_handle,
						     &s->group_list);
	if (composite_nomem(subreq, c)) return;

	tevent_req_set_callback(subreq, continue_groups_enumerated, c);
}


/*
 * Stage 2: receive enumerated groups and their rids
 */
static void continue_groups_enumerated(struct tevent_req *subreq)
{
	struct composite_context *c;
	struct grouplist_state *s;
	uint32_t i;

	c = tevent_req_callback_data(subreq, struct composite_context);
	s = talloc_get_type(c->private_data, struct grouplist_state);

	/* receive result of rpc request */
	c->status = dcerpc_samr_EnumDomainGroups_r_recv(subreq, s);
	TALLOC_FREE(subreq);
	if (!composite_is_ok(c)) return;

	/* get the actual status of the rpc call result
	   (instead of rpc layer) */
	c->status = s->group_list.out.result;

	/* we're interested in status "ok" as well as two
	   enum-specific status codes */
	if (NT_STATUS_IS_OK(c->status) ||
	    NT_STATUS_EQUAL(c->status, STATUS_MORE_ENTRIES) ||
	    NT_STATUS_EQUAL(c->status, NT_STATUS_NO_MORE_ENTRIES)) {
		
		/* get enumerated accounts counter and resume handle (the latter allows
		   making subsequent call to continue enumeration) */
		s->resume_index = *s->group_list.out.resume_handle;
		s->count        = *s->group_list.out.num_entries;

		/* prepare returned group accounts array */
		s->groups       = talloc_array(c, struct grouplist, (*s->group_list.out.sam)->count);
		if (composite_nomem(s->groups, c)) return;

		for (i = 0; i < (*s->group_list.out.sam)->count; i++) {
			struct dom_sid *group_sid;
			struct samr_SamEntry *entry = &(*s->group_list.out.sam)->entries[i];
			struct dom_sid *domain_sid = (*s->query_domain.out.info)->domain.sid;
			
			/* construct group sid from returned rid and queried domain sid */
			group_sid = dom_sid_add_rid(c, domain_sid, entry->idx);
			if (composite_nomem(group_sid, c)) return;

			/* groupname */
			s->groups[i].groupname = talloc_strdup(s->groups, entry->name.string);
			if (composite_nomem(s->groups[i].groupname, c)) return;

			/* sid string */
			s->groups[i].sid = dom_sid_string(s->groups, group_sid);
			if (composite_nomem(s->groups[i].sid, c)) return;
		}

		/* that's it */
		composite_done(c);

	} else {
		/* something went wrong */
		composite_error(c, c->status);
	}
}


/**
 * Receive result of GroupList call
 *
 * @param c composite context returned by send request routine
 * @param mem_ctx memory context of this call
 * @param io pointer to structure containing arguments and result of this call
 * @param nt status
 */
NTSTATUS libnet_GroupList_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
			       struct libnet_GroupList *io)
{
	NTSTATUS status;
	struct grouplist_state *s;

	if (c == NULL || mem_ctx == NULL || io == NULL) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	status = composite_wait(c);
	if (NT_STATUS_IS_OK(status) ||
	    NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) ||
	    NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
		
		s = talloc_get_type(c->private_data, struct grouplist_state);
		
		/* get results from composite context */
		io->out.count        = s->count;
		io->out.resume_index = s->resume_index;
		io->out.groups       = talloc_steal(mem_ctx, s->groups);

		if (NT_STATUS_IS_OK(status)) {
			io->out.error_string = talloc_asprintf(mem_ctx, "Success");
		} else {
			/* success, but we're not done yet */
			io->out.error_string = talloc_asprintf(mem_ctx, "Success (status: %s)",
							       nt_errstr(status));
		}

	} else {
		io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
	}

	talloc_free(c);
	return status;
}


/**
 * Enumerate domain groups
 *
 * @param ctx initialised libnet context
 * @param mem_ctx memory context of this call
 * @param io pointer to structure containing arguments and result of this call
 * @return nt status
 */
NTSTATUS libnet_GroupList(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
			  struct libnet_GroupList *io)
{
	struct composite_context *c;

	c = libnet_GroupList_send(ctx, mem_ctx, io, NULL);
	return libnet_GroupList_recv(c, mem_ctx, io);
}