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