Line data Source code
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 1 : 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 1 : struct composite_context *c;
52 1 : struct create_group_state *s;
53 1 : struct composite_context *create_req;
54 1 : bool prereq_met = false;
55 :
56 : /* composite context allocation and setup */
57 1 : c = composite_create(mem_ctx, ctx->event_ctx);
58 1 : if (c == NULL) return NULL;
59 :
60 1 : s = talloc_zero(c, struct create_group_state);
61 1 : if (composite_nomem(s, c)) return c;
62 :
63 1 : c->private_data = s;
64 :
65 1 : s->ctx = ctx;
66 1 : s->r = *r;
67 1 : ZERO_STRUCT(s->r.out);
68 :
69 : /* prerequisite: make sure we have a valid samr domain handle */
70 1 : prereq_met = samr_domain_opened(ctx, c, s->r.in.domain_name, &c, &s->domain_open,
71 : continue_domain_opened, monitor);
72 1 : if (!prereq_met) return c;
73 :
74 : /* prepare arguments of rpc group add call */
75 0 : s->group_add.in.groupname = r->in.group_name;
76 0 : s->group_add.in.domain_handle = ctx->samr.handle;
77 :
78 : /* send the request */
79 0 : create_req = libnet_rpc_groupadd_send(s, s->ctx->event_ctx,
80 : ctx->samr.samr_handle,
81 : &s->group_add, monitor);
82 0 : if (composite_nomem(create_req, c)) return c;
83 :
84 0 : composite_continue(c, create_req, continue_rpc_group_added, c);
85 0 : return c;
86 : }
87 :
88 :
89 1 : static void continue_domain_opened(struct composite_context *ctx)
90 : {
91 1 : struct composite_context *c;
92 1 : struct create_group_state *s;
93 1 : struct composite_context *create_req;
94 :
95 1 : c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
96 1 : s = talloc_get_type_abort(c->private_data, struct create_group_state);
97 :
98 1 : c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
99 1 : if (!composite_is_ok(c)) return;
100 :
101 : /* prepare arguments of groupadd call */
102 1 : s->group_add.in.groupname = s->r.in.group_name;
103 1 : s->group_add.in.domain_handle = s->ctx->samr.handle;
104 :
105 : /* send the request */
106 1 : create_req = libnet_rpc_groupadd_send(s, s->ctx->event_ctx,
107 0 : s->ctx->samr.samr_handle,
108 : &s->group_add, s->monitor_fn);
109 1 : if (composite_nomem(create_req, c)) return;
110 :
111 1 : composite_continue(c, create_req, continue_rpc_group_added, c);
112 : }
113 :
114 :
115 1 : static void continue_rpc_group_added(struct composite_context *ctx)
116 : {
117 1 : struct composite_context *c;
118 1 : struct create_group_state *s;
119 :
120 1 : c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
121 1 : s = talloc_get_type_abort(c->private_data, struct create_group_state);
122 :
123 : /* receive result of group add call */
124 1 : c->status = libnet_rpc_groupadd_recv(ctx, c, &s->group_add);
125 1 : if (!composite_is_ok(c)) return;
126 :
127 : /* we're done */
128 1 : 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 1 : NTSTATUS libnet_CreateGroup_recv(struct composite_context *c,
141 : TALLOC_CTX *mem_ctx,
142 : struct libnet_CreateGroup *r)
143 : {
144 1 : NTSTATUS status;
145 :
146 1 : status = composite_wait(c);
147 1 : if (!NT_STATUS_IS_OK(status)) {
148 0 : r->out.error_string = talloc_strdup(mem_ctx, nt_errstr(status));
149 : }
150 :
151 1 : talloc_free(c);
152 1 : 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 1 : NTSTATUS libnet_CreateGroup(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
165 : struct libnet_CreateGroup *io)
166 : {
167 1 : struct composite_context *c;
168 :
169 1 : c = libnet_CreateGroup_send(ctx, mem_ctx, io, NULL);
170 1 : 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 1 : 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 1 : struct composite_context *c;
208 1 : struct group_info_state *s;
209 1 : bool prereq_met = false;
210 1 : struct composite_context *lookup_req, *info_req;
211 :
212 : /* composite context allocation and setup */
213 1 : c = composite_create(mem_ctx, ctx->event_ctx);
214 1 : if (c == NULL) return NULL;
215 :
216 1 : s = talloc_zero(c, struct group_info_state);
217 1 : if (composite_nomem(s, c)) return c;
218 :
219 1 : c->private_data = s;
220 :
221 : /* store arguments in the state structure */
222 1 : s->monitor_fn = monitor;
223 1 : s->ctx = ctx;
224 1 : s->domain_name = talloc_strdup(c, io->in.domain_name);
225 1 : s->level = io->in.level;
226 1 : switch(s->level) {
227 1 : case GROUP_INFO_BY_NAME:
228 1 : s->group_name = talloc_strdup(c, io->in.data.group_name);
229 1 : s->sid_string = NULL;
230 1 : break;
231 0 : case GROUP_INFO_BY_SID:
232 0 : s->group_name = NULL;
233 0 : s->sid_string = dom_sid_string(c, io->in.data.group_sid);
234 0 : break;
235 : }
236 :
237 : /* prerequisite: make sure the domain is opened */
238 1 : prereq_met = samr_domain_opened(ctx, c, s->domain_name, &c, &s->domopen,
239 : continue_domain_open_info, monitor);
240 1 : if (!prereq_met) return c;
241 :
242 0 : switch(s->level) {
243 0 : case GROUP_INFO_BY_NAME:
244 : /* prepare arguments for LookupName call */
245 0 : s->lookup.in.name = s->group_name;
246 0 : s->lookup.in.domain_name = s->domain_name;
247 :
248 : /* send the request */
249 0 : lookup_req = libnet_LookupName_send(s->ctx, c, &s->lookup, s->monitor_fn);
250 0 : if (composite_nomem(lookup_req, c)) return c;
251 :
252 : /* set the next stage */
253 0 : composite_continue(c, lookup_req, continue_name_found, c);
254 0 : break;
255 0 : case GROUP_INFO_BY_SID:
256 : /* prepare arguments for groupinfo call */
257 0 : s->info.in.domain_handle = s->ctx->samr.handle;
258 0 : s->info.in.sid = s->sid_string;
259 : /* we're looking for all information available */
260 0 : s->info.in.level = GROUPINFOALL;
261 :
262 : /* send the request */
263 0 : info_req = libnet_rpc_groupinfo_send(s, s->ctx->event_ctx,
264 0 : s->ctx->samr.samr_handle,
265 : &s->info, s->monitor_fn);
266 0 : if (composite_nomem(info_req, c)) return c;
267 :
268 : /* set the next stage */
269 0 : composite_continue(c, info_req, continue_group_info, c);
270 0 : break;
271 : }
272 :
273 0 : return c;
274 : }
275 :
276 :
277 : /*
278 : * Stage 0.5 (optional): receive opened domain and send lookup name request
279 : */
280 1 : static void continue_domain_open_info(struct composite_context *ctx)
281 : {
282 1 : struct composite_context *c;
283 1 : struct group_info_state *s;
284 1 : struct composite_context *lookup_req, *info_req;
285 :
286 1 : c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
287 1 : s = talloc_get_type_abort(c->private_data, struct group_info_state);
288 :
289 : /* receive domain handle */
290 1 : c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
291 1 : if (!composite_is_ok(c)) return;
292 :
293 1 : switch(s->level) {
294 1 : case GROUP_INFO_BY_NAME:
295 : /* prepare arguments for LookupName call */
296 1 : s->lookup.in.name = s->group_name;
297 1 : s->lookup.in.domain_name = s->domain_name;
298 :
299 : /* send the request */
300 1 : lookup_req = libnet_LookupName_send(s->ctx, c, &s->lookup, s->monitor_fn);
301 1 : if (composite_nomem(lookup_req, c)) return;
302 :
303 : /* set the next stage */
304 1 : composite_continue(c, lookup_req, continue_name_found, c);
305 1 : break;
306 0 : case GROUP_INFO_BY_SID:
307 : /* prepare arguments for groupinfo call */
308 0 : s->info.in.domain_handle = s->ctx->samr.handle;
309 0 : s->info.in.sid = s->sid_string;
310 : /* we're looking for all information available */
311 0 : s->info.in.level = GROUPINFOALL;
312 :
313 : /* send the request */
314 0 : info_req = libnet_rpc_groupinfo_send(s, s->ctx->event_ctx,
315 0 : s->ctx->samr.samr_handle,
316 : &s->info, s->monitor_fn);
317 0 : if (composite_nomem(info_req, c)) return;
318 :
319 : /* set the next stage */
320 0 : composite_continue(c, info_req, continue_group_info, c);
321 0 : break;
322 :
323 : }
324 : }
325 :
326 :
327 : /*
328 : * Stage 1: Receive SID found and send request for group info
329 : */
330 1 : static void continue_name_found(struct composite_context *ctx)
331 : {
332 1 : struct composite_context *c;
333 1 : struct group_info_state *s;
334 1 : struct composite_context *info_req;
335 :
336 1 : c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
337 1 : s = talloc_get_type_abort(c->private_data, struct group_info_state);
338 :
339 : /* receive SID associated with name found */
340 1 : c->status = libnet_LookupName_recv(ctx, c, &s->lookup);
341 1 : if (!composite_is_ok(c)) return;
342 :
343 : /* Is it a group SID actually ? */
344 1 : if (s->lookup.out.sid_type != SID_NAME_DOM_GRP &&
345 0 : s->lookup.out.sid_type != SID_NAME_ALIAS) {
346 0 : composite_error(c, NT_STATUS_NO_SUCH_GROUP);
347 0 : return;
348 : }
349 :
350 : /* prepare arguments for groupinfo call */
351 1 : s->info.in.domain_handle = s->ctx->samr.handle;
352 1 : s->info.in.groupname = s->group_name;
353 1 : s->info.in.sid = s->lookup.out.sidstr;
354 : /* we're looking for all information available */
355 1 : s->info.in.level = GROUPINFOALL;
356 :
357 : /* send the request */
358 1 : info_req = libnet_rpc_groupinfo_send(s, s->ctx->event_ctx,
359 0 : s->ctx->samr.samr_handle,
360 : &s->info, s->monitor_fn);
361 1 : if (composite_nomem(info_req, c)) return;
362 :
363 : /* set the next stage */
364 1 : composite_continue(c, info_req, continue_group_info, c);
365 : }
366 :
367 :
368 : /*
369 : * Stage 2: Receive group information
370 : */
371 1 : static void continue_group_info(struct composite_context *ctx)
372 : {
373 1 : struct composite_context *c;
374 1 : struct group_info_state *s;
375 :
376 1 : c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
377 1 : s = talloc_get_type_abort(c->private_data, struct group_info_state);
378 :
379 : /* receive group information */
380 1 : c->status = libnet_rpc_groupinfo_recv(ctx, c, &s->info);
381 1 : if (!composite_is_ok(c)) return;
382 :
383 : /* we're done */
384 1 : 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 1 : NTSTATUS libnet_GroupInfo_recv(struct composite_context* c, TALLOC_CTX *mem_ctx,
397 : struct libnet_GroupInfo *io)
398 : {
399 1 : NTSTATUS status;
400 1 : struct group_info_state *s;
401 :
402 1 : status = composite_wait(c);
403 1 : if (NT_STATUS_IS_OK(status)) {
404 : /* put the results into io structure if everything went fine */
405 1 : s = talloc_get_type_abort(c->private_data, struct group_info_state);
406 :
407 1 : io->out.group_name = talloc_steal(mem_ctx,
408 : s->info.out.info.all.name.string);
409 1 : io->out.group_sid = talloc_steal(mem_ctx, s->lookup.out.sid);
410 1 : io->out.num_members = s->info.out.info.all.num_members;
411 1 : io->out.description = talloc_steal(mem_ctx, s->info.out.info.all.description.string);
412 :
413 1 : io->out.error_string = talloc_strdup(mem_ctx, "Success");
414 :
415 : } else {
416 0 : io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
417 : }
418 :
419 1 : talloc_free(c);
420 1 : 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 1 : NTSTATUS libnet_GroupInfo(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
432 : struct libnet_GroupInfo *io)
433 : {
434 1 : struct composite_context *c = libnet_GroupInfo_send(ctx, mem_ctx,
435 : io, NULL);
436 1 : 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 5 : 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 5 : struct composite_context *c;
478 5 : struct grouplist_state *s;
479 5 : struct tevent_req *subreq;
480 5 : bool prereq_met = false;
481 :
482 : /* composite context allocation and setup */
483 5 : c = composite_create(mem_ctx, ctx->event_ctx);
484 5 : if (c == NULL) return NULL;
485 :
486 5 : s = talloc_zero(c, struct grouplist_state);
487 5 : if (composite_nomem(s, c)) return c;
488 :
489 5 : c->private_data = s;
490 :
491 : /* store the arguments in the state structure */
492 5 : s->ctx = ctx;
493 5 : s->page_size = io->in.page_size;
494 5 : s->resume_index = io->in.resume_index;
495 5 : s->domain_name = talloc_strdup(c, io->in.domain_name);
496 5 : s->monitor_fn = monitor;
497 :
498 : /* make sure we have lsa domain handle before doing anything */
499 5 : prereq_met = lsa_domain_opened(ctx, c, s->domain_name, &c, &s->domain_open,
500 : continue_lsa_domain_opened, monitor);
501 5 : if (!prereq_met) return c;
502 :
503 : /* prepare arguments of QueryDomainInfo call */
504 4 : s->query_domain.in.handle = &ctx->lsa.handle;
505 4 : s->query_domain.in.level = LSA_POLICY_INFO_DOMAIN;
506 4 : s->query_domain.out.info = talloc_zero(c, union lsa_PolicyInformation *);
507 4 : if (composite_nomem(s->query_domain.out.info, c)) return c;
508 :
509 : /* send the request */
510 8 : subreq = dcerpc_lsa_QueryInfoPolicy_r_send(s, c->event_ctx,
511 4 : ctx->lsa.pipe->binding_handle,
512 : &s->query_domain);
513 4 : if (composite_nomem(subreq, c)) return c;
514 :
515 4 : tevent_req_set_callback(subreq, continue_domain_queried, c);
516 4 : 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 1 : static void continue_lsa_domain_opened(struct composite_context *ctx)
525 : {
526 1 : struct composite_context *c;
527 1 : struct grouplist_state *s;
528 1 : struct tevent_req *subreq;
529 :
530 1 : c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
531 1 : s = talloc_get_type_abort(c->private_data, struct grouplist_state);
532 :
533 : /* receive lsa domain handle */
534 1 : c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
535 1 : if (!composite_is_ok(c)) return;
536 :
537 : /* prepare arguments of QueryDomainInfo call */
538 1 : s->query_domain.in.handle = &s->ctx->lsa.handle;
539 1 : s->query_domain.in.level = LSA_POLICY_INFO_DOMAIN;
540 1 : s->query_domain.out.info = talloc_zero(c, union lsa_PolicyInformation *);
541 1 : if (composite_nomem(s->query_domain.out.info, c)) return;
542 :
543 : /* send the request */
544 2 : subreq = dcerpc_lsa_QueryInfoPolicy_r_send(s, c->event_ctx,
545 1 : s->ctx->lsa.pipe->binding_handle,
546 : &s->query_domain);
547 1 : if (composite_nomem(subreq, c)) return;
548 :
549 1 : 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 5 : static void continue_domain_queried(struct tevent_req *subreq)
558 : {
559 5 : struct composite_context *c;
560 5 : struct grouplist_state *s;
561 5 : bool prereq_met = false;
562 :
563 5 : c = tevent_req_callback_data(subreq, struct composite_context);
564 5 : s = talloc_get_type_abort(c->private_data, struct grouplist_state);
565 :
566 : /* receive result of rpc request */
567 5 : c->status = dcerpc_lsa_QueryInfoPolicy_r_recv(subreq, s);
568 5 : TALLOC_FREE(subreq);
569 6 : if (!composite_is_ok(c)) return;
570 :
571 : /* get the returned domain info */
572 5 : s->dominfo = (*s->query_domain.out.info)->domain;
573 :
574 : /* make sure we have samr domain handle before continuing */
575 5 : prereq_met = samr_domain_opened(s->ctx, c, s->domain_name, &c, &s->domain_open,
576 : continue_samr_domain_opened, s->monitor_fn);
577 5 : if (!prereq_met) return;
578 :
579 : /* prepare arguments od EnumDomainGroups call */
580 4 : s->group_list.in.domain_handle = &s->ctx->samr.handle;
581 4 : s->group_list.in.max_size = s->page_size;
582 4 : s->group_list.in.resume_handle = &s->resume_index;
583 4 : s->group_list.out.resume_handle = &s->resume_index;
584 4 : s->group_list.out.num_entries = talloc(s, uint32_t);
585 4 : if (composite_nomem(s->group_list.out.num_entries, c)) return;
586 4 : s->group_list.out.sam = talloc(s, struct samr_SamArray *);
587 4 : if (composite_nomem(s->group_list.out.sam, c)) return;
588 :
589 : /* send the request */
590 8 : subreq = dcerpc_samr_EnumDomainGroups_r_send(s, c->event_ctx,
591 4 : s->ctx->samr.pipe->binding_handle,
592 : &s->group_list);
593 4 : if (composite_nomem(subreq, c)) return;
594 :
595 4 : 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 1 : static void continue_samr_domain_opened(struct composite_context *ctx)
604 : {
605 1 : struct composite_context *c;
606 1 : struct grouplist_state *s;
607 1 : struct tevent_req *subreq;
608 :
609 1 : c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
610 1 : s = talloc_get_type_abort(c->private_data, struct grouplist_state);
611 :
612 : /* receive samr domain handle */
613 1 : c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
614 1 : if (!composite_is_ok(c)) return;
615 :
616 : /* prepare arguments of EnumDomainGroups call */
617 1 : s->group_list.in.domain_handle = &s->ctx->samr.handle;
618 1 : s->group_list.in.max_size = s->page_size;
619 1 : s->group_list.in.resume_handle = &s->resume_index;
620 1 : s->group_list.out.resume_handle = &s->resume_index;
621 1 : s->group_list.out.num_entries = talloc(s, uint32_t);
622 1 : if (composite_nomem(s->group_list.out.num_entries, c)) return;
623 1 : s->group_list.out.sam = talloc(s, struct samr_SamArray *);
624 1 : if (composite_nomem(s->group_list.out.sam, c)) return;
625 :
626 : /* send the request */
627 2 : subreq = dcerpc_samr_EnumDomainGroups_r_send(s, c->event_ctx,
628 1 : s->ctx->samr.pipe->binding_handle,
629 : &s->group_list);
630 1 : if (composite_nomem(subreq, c)) return;
631 :
632 1 : tevent_req_set_callback(subreq, continue_groups_enumerated, c);
633 : }
634 :
635 :
636 : /*
637 : * Stage 2: receive enumerated groups and their rids
638 : */
639 5 : static void continue_groups_enumerated(struct tevent_req *subreq)
640 : {
641 5 : struct composite_context *c;
642 5 : struct grouplist_state *s;
643 5 : uint32_t i;
644 :
645 5 : c = tevent_req_callback_data(subreq, struct composite_context);
646 5 : s = talloc_get_type_abort(c->private_data, struct grouplist_state);
647 :
648 : /* receive result of rpc request */
649 5 : c->status = dcerpc_samr_EnumDomainGroups_r_recv(subreq, s);
650 5 : TALLOC_FREE(subreq);
651 5 : if (!composite_is_ok(c)) return;
652 :
653 : /* get the actual status of the rpc call result
654 : (instead of rpc layer) */
655 5 : 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 5 : if (NT_STATUS_IS_OK(c->status) ||
660 0 : NT_STATUS_EQUAL(c->status, STATUS_MORE_ENTRIES) ||
661 0 : 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 5 : s->resume_index = *s->group_list.out.resume_handle;
666 5 : s->count = *s->group_list.out.num_entries;
667 :
668 : /* prepare returned group accounts array */
669 5 : s->groups = talloc_array(c, struct grouplist, (*s->group_list.out.sam)->count);
670 5 : if (composite_nomem(s->groups, c)) return;
671 :
672 18 : for (i = 0; i < (*s->group_list.out.sam)->count; i++) {
673 13 : struct dom_sid *group_sid;
674 13 : struct samr_SamEntry *entry = &(*s->group_list.out.sam)->entries[i];
675 13 : 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 13 : group_sid = dom_sid_add_rid(c, domain_sid, entry->idx);
679 13 : if (composite_nomem(group_sid, c)) return;
680 :
681 : /* groupname */
682 13 : s->groups[i].groupname = talloc_strdup(s->groups, entry->name.string);
683 13 : if (composite_nomem(s->groups[i].groupname, c)) return;
684 :
685 : /* sid string */
686 13 : s->groups[i].sid = dom_sid_string(s->groups, group_sid);
687 13 : if (composite_nomem(s->groups[i].sid, c)) return;
688 : }
689 :
690 : /* that's it */
691 5 : composite_done(c);
692 5 : return;
693 : } else {
694 : /* something went wrong */
695 0 : composite_error(c, c->status);
696 0 : 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 5 : NTSTATUS libnet_GroupList_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
710 : struct libnet_GroupList *io)
711 : {
712 5 : NTSTATUS status;
713 5 : struct grouplist_state *s;
714 :
715 5 : if (c == NULL || mem_ctx == NULL || io == NULL) {
716 0 : talloc_free(c);
717 0 : return NT_STATUS_INVALID_PARAMETER;
718 : }
719 :
720 5 : status = composite_wait(c);
721 5 : if (NT_STATUS_IS_OK(status) ||
722 0 : NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) ||
723 0 : NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
724 :
725 5 : s = talloc_get_type_abort(c->private_data, struct grouplist_state);
726 :
727 : /* get results from composite context */
728 5 : io->out.count = s->count;
729 5 : io->out.resume_index = s->resume_index;
730 5 : io->out.groups = talloc_steal(mem_ctx, s->groups);
731 :
732 5 : if (NT_STATUS_IS_OK(status)) {
733 1 : io->out.error_string = talloc_asprintf(mem_ctx, "Success");
734 : } else {
735 : /* success, but we're not done yet */
736 4 : io->out.error_string = talloc_asprintf(mem_ctx, "Success (status: %s)",
737 : nt_errstr(status));
738 : }
739 :
740 : } else {
741 0 : io->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
742 : }
743 :
744 5 : talloc_free(c);
745 5 : 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 5 : NTSTATUS libnet_GroupList(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
758 : struct libnet_GroupList *io)
759 : {
760 5 : struct composite_context *c;
761 :
762 5 : c = libnet_GroupList_send(ctx, mem_ctx, io, NULL);
763 5 : return libnet_GroupList_recv(c, mem_ctx, io);
764 : }
|