2 Unix SMB/CIFS implementation.
4 Copyright (C) Rafal Szczesniak <mimir@samba.org> 2005
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.
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.
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/>.
22 #include "libnet/libnet.h"
23 #include "libcli/composite/composite.h"
24 #include "auth/credentials/credentials.h"
25 #include "librpc/ndr/libndr.h"
26 #include "librpc/gen_ndr/samr.h"
27 #include "librpc/gen_ndr/ndr_samr_c.h"
28 #include "librpc/gen_ndr/lsa.h"
29 #include "librpc/gen_ndr/ndr_lsa_c.h"
30 #include "libcli/security/security.h"
33 struct create_user_state {
34 struct libnet_CreateUser r;
35 struct libnet_DomainOpen domain_open;
36 struct libnet_rpc_useradd user_add;
37 struct libnet_context *ctx;
39 /* information about the progress */
40 void (*monitor_fn)(struct monitor_msg *);
44 static void continue_rpc_useradd(struct composite_context *ctx);
45 static void continue_domain_open_create(struct composite_context *ctx);
49 * Sends request to create user account
51 * @param ctx initialised libnet context
52 * @param mem_ctx memory context of this call
53 * @param r pointer to a structure containing arguments and results of this call
54 * @param monitor function pointer for receiving monitor messages
55 * @return compostite context of this request
57 struct composite_context* libnet_CreateUser_send(struct libnet_context *ctx,
59 struct libnet_CreateUser *r,
60 void (*monitor)(struct monitor_msg*))
62 struct composite_context *c;
63 struct create_user_state *s;
64 struct composite_context *create_req;
65 BOOL prereq_met = False;
67 /* composite context allocation and setup */
68 c = composite_create(mem_ctx, ctx->event_ctx);
69 if (c == NULL) return NULL;
71 s = talloc_zero(c, struct create_user_state);
72 if (composite_nomem(s, c)) return c;
76 /* store arguments in the state structure */
79 ZERO_STRUCT(s->r.out);
81 /* prerequisite: make sure the domain is opened */
82 prereq_met = samr_domain_opened(ctx, s->r.in.domain_name, &c, &s->domain_open,
83 continue_domain_open_create, monitor);
84 if (!prereq_met) return c;
86 /* prepare arguments for useradd call */
87 s->user_add.in.username = r->in.user_name;
88 s->user_add.in.domain_handle = ctx->samr.handle;
90 /* send the request */
91 create_req = libnet_rpc_useradd_send(ctx->samr.pipe, &s->user_add, monitor);
92 if (composite_nomem(create_req, c)) return c;
94 /* set the next stage */
95 composite_continue(c, create_req, continue_rpc_useradd, c);
101 * Stage 0.5 (optional): receive result of domain open request
102 * and send useradd request
104 static void continue_domain_open_create(struct composite_context *ctx)
106 struct composite_context *c;
107 struct create_user_state *s;
108 struct composite_context *create_req;
109 struct monitor_msg msg;
111 c = talloc_get_type(ctx->async.private_data, struct composite_context);
112 s = talloc_get_type(c->private_data, struct create_user_state);
114 /* receive result of DomainOpen call */
115 c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
116 if (!composite_is_ok(c)) return;
118 /* send monitor message */
119 if (s->monitor_fn) s->monitor_fn(&msg);
121 /* prepare arguments for useradd call */
122 s->user_add.in.username = s->r.in.user_name;
123 s->user_add.in.domain_handle = s->ctx->samr.handle;
125 /* send the request */
126 create_req = libnet_rpc_useradd_send(s->ctx->samr.pipe, &s->user_add, s->monitor_fn);
127 if (composite_nomem(create_req, c)) return;
129 /* set the next stage */
130 composite_continue(c, create_req, continue_rpc_useradd, c);
135 * Stage 1: receive result of useradd call
137 static void continue_rpc_useradd(struct composite_context *ctx)
139 struct composite_context *c;
140 struct create_user_state *s;
141 struct monitor_msg msg;
143 c = talloc_get_type(ctx->async.private_data, struct composite_context);
144 s = talloc_get_type(c->private_data, struct create_user_state);
146 /* receive result of the call */
147 c->status = libnet_rpc_useradd_recv(ctx, c, &s->user_add);
148 if (!composite_is_ok(c)) return;
150 /* send monitor message */
151 if (s->monitor_fn) s->monitor_fn(&msg);
159 * Receive result of CreateUser call
161 * @param c composite context returned by send request routine
162 * @param mem_ctx memory context of this call
163 * @param r pointer to a structure containing arguments and result of this call
166 NTSTATUS libnet_CreateUser_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
167 struct libnet_CreateUser *r)
170 struct create_user_state *s;
172 r->out.error_string = NULL;
174 /* wait for result of async request and check status code */
175 status = composite_wait(c);
176 if (!NT_STATUS_IS_OK(status)) {
177 s = talloc_get_type(c->private_data, struct create_user_state);
178 r->out.error_string = talloc_strdup(mem_ctx, nt_errstr(status));
186 * Synchronous version of CreateUser call
188 * @param ctx initialised libnet context
189 * @param mem_ctx memory context of this call
190 * @param r pointer to a structure containing arguments and result of this call
193 NTSTATUS libnet_CreateUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
194 struct libnet_CreateUser *r)
196 struct composite_context *c;
198 c = libnet_CreateUser_send(ctx, mem_ctx, r, NULL);
199 return libnet_CreateUser_recv(c, mem_ctx, r);
203 struct delete_user_state {
204 struct libnet_DeleteUser r;
205 struct libnet_context *ctx;
206 struct libnet_DomainOpen domain_open;
207 struct libnet_rpc_userdel user_del;
209 /* information about the progress */
210 void (*monitor_fn)(struct monitor_msg *);
214 static void continue_rpc_userdel(struct composite_context *ctx);
215 static void continue_domain_open_delete(struct composite_context *ctx);
219 * Sends request to delete user account
221 * @param ctx initialised libnet context
222 * @param mem_ctx memory context of this call
223 * @param r pointer to structure containing arguments and result of this call
224 * @param monitor function pointer for receiving monitor messages
226 struct composite_context *libnet_DeleteUser_send(struct libnet_context *ctx,
228 struct libnet_DeleteUser *r,
229 void (*monitor)(struct monitor_msg*))
231 struct composite_context *c;
232 struct delete_user_state *s;
233 struct composite_context *delete_req;
234 BOOL prereq_met = False;
236 /* composite context allocation and setup */
237 c = composite_create(mem_ctx, ctx->event_ctx);
238 if (c == NULL) return NULL;
240 s = talloc_zero(c, struct delete_user_state);
241 if (composite_nomem(s, c)) return c;
245 /* store arguments in state structure */
248 ZERO_STRUCT(s->r.out);
250 /* prerequisite: make sure the domain is opened before proceeding */
251 prereq_met = samr_domain_opened(ctx, s->r.in.domain_name, &c, &s->domain_open,
252 continue_domain_open_delete, monitor);
253 if (!prereq_met) return c;
255 /* prepare arguments for userdel call */
256 s->user_del.in.username = r->in.user_name;
257 s->user_del.in.domain_handle = ctx->samr.handle;
260 delete_req = libnet_rpc_userdel_send(ctx->samr.pipe, &s->user_del, monitor);
261 if (composite_nomem(delete_req, c)) return c;
263 /* set the next stage */
264 composite_continue(c, delete_req, continue_rpc_userdel, c);
270 * Stage 0.5 (optional): receive result of domain open request
271 * and send useradd request
273 static void continue_domain_open_delete(struct composite_context *ctx)
275 struct composite_context *c;
276 struct delete_user_state *s;
277 struct composite_context *delete_req;
278 struct monitor_msg msg;
280 c = talloc_get_type(ctx->async.private_data, struct composite_context);
281 s = talloc_get_type(c->private_data, struct delete_user_state);
283 /* receive result of DomainOpen call */
284 c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
285 if (!composite_is_ok(c)) return;
287 /* send monitor message */
288 if (s->monitor_fn) s->monitor_fn(&msg);
290 /* prepare arguments for userdel call */
291 s->user_del.in.username = s->r.in.user_name;
292 s->user_del.in.domain_handle = s->ctx->samr.handle;
295 delete_req = libnet_rpc_userdel_send(s->ctx->samr.pipe, &s->user_del, s->monitor_fn);
296 if (composite_nomem(delete_req, c)) return;
298 /* set the next stage */
299 composite_continue(c, delete_req, continue_rpc_userdel, c);
304 * Stage 1: receive result of userdel call and finish the composite function
306 static void continue_rpc_userdel(struct composite_context *ctx)
308 struct composite_context *c;
309 struct delete_user_state *s;
310 struct monitor_msg msg;
312 c = talloc_get_type(ctx->async.private_data, struct composite_context);
313 s = talloc_get_type(c->private_data, struct delete_user_state);
315 /* receive result of userdel call */
316 c->status = libnet_rpc_userdel_recv(ctx, c, &s->user_del);
317 if (!composite_is_ok(c)) return;
319 /* send monitor message */
320 if (s->monitor_fn) s->monitor_fn(&msg);
328 * Receives result of asynchronous DeleteUser call
330 * @param c composite context returned by async DeleteUser call
331 * @param mem_ctx memory context of this call
332 * @param r pointer to structure containing arguments and result
334 NTSTATUS libnet_DeleteUser_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
335 struct libnet_DeleteUser *r)
338 struct delete_user_state *s;
340 r->out.error_string = NULL;
342 /* wait for result of async request and check status code */
343 status = composite_wait(c);
344 if (!NT_STATUS_IS_OK(status)) {
345 s = talloc_get_type(c->private_data, struct delete_user_state);
346 r->out.error_string = talloc_steal(mem_ctx, s->r.out.error_string);
354 * Synchronous version of DeleteUser call
356 * @param ctx initialised libnet context
357 * @param mem_ctx memory context of this call
358 * @param r pointer to structure containing arguments and result
360 NTSTATUS libnet_DeleteUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
361 struct libnet_DeleteUser *r)
363 struct composite_context *c;
365 c = libnet_DeleteUser_send(ctx, mem_ctx, r, NULL);
366 return libnet_DeleteUser_recv(c, mem_ctx, r);
370 struct modify_user_state {
371 struct libnet_ModifyUser r;
372 struct libnet_context *ctx;
373 struct libnet_DomainOpen domain_open;
374 struct libnet_rpc_userinfo user_info;
375 struct libnet_rpc_usermod user_mod;
377 void (*monitor_fn)(struct monitor_msg *);
381 static void continue_rpc_usermod(struct composite_context *ctx);
382 static void continue_domain_open_modify(struct composite_context *ctx);
383 static NTSTATUS set_user_changes(TALLOC_CTX *mem_ctx, struct usermod_change *mod,
384 struct libnet_rpc_userinfo *info, struct libnet_ModifyUser *r);
385 static void continue_rpc_userinfo(struct composite_context *ctx);
389 * Sends request to modify user account
391 * @param ctx initialised libnet context
392 * @param mem_ctx memory context of this call
393 * @param r pointer to structure containing arguments and result of this call
394 * @param monitor function pointer for receiving monitor messages
396 struct composite_context *libnet_ModifyUser_send(struct libnet_context *ctx,
398 struct libnet_ModifyUser *r,
399 void (*monitor)(struct monitor_msg*))
401 const uint16_t level = 21;
402 struct composite_context *c;
403 struct modify_user_state *s;
404 struct composite_context *userinfo_req;
405 BOOL prereq_met = False;
407 c = composite_create(mem_ctx, ctx->event_ctx);
408 if (c == NULL) return NULL;
410 s = talloc_zero(c, struct modify_user_state);
411 if (composite_nomem(s, c)) return c;
418 prereq_met = samr_domain_opened(ctx, s->r.in.domain_name, &c, &s->domain_open,
419 continue_domain_open_modify, monitor);
420 if (!prereq_met) return c;
422 s->user_info.in.username = r->in.user_name;
423 s->user_info.in.domain_handle = ctx->samr.handle;
424 s->user_info.in.level = level;
426 userinfo_req = libnet_rpc_userinfo_send(ctx->samr.pipe, &s->user_info, monitor);
427 if (composite_nomem(userinfo_req, c)) return c;
429 composite_continue(c, userinfo_req, continue_rpc_userinfo, c);
435 * Stage 0.5 (optional): receive result of domain open request
436 * and send userinfo request
438 static void continue_domain_open_modify(struct composite_context *ctx)
440 const uint16_t level = 21;
441 struct composite_context *c;
442 struct modify_user_state *s;
443 struct composite_context *userinfo_req;
444 struct monitor_msg msg;
446 c = talloc_get_type(ctx->async.private_data, struct composite_context);
447 s = talloc_get_type(c->private_data, struct modify_user_state);
449 c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
450 if (!composite_is_ok(c)) return;
452 if (s->monitor_fn) s->monitor_fn(&msg);
454 s->user_info.in.domain_handle = s->ctx->samr.handle;
455 s->user_info.in.username = s->r.in.user_name;
456 s->user_info.in.level = level;
458 userinfo_req = libnet_rpc_userinfo_send(s->ctx->samr.pipe, &s->user_info, s->monitor_fn);
459 if (composite_nomem(userinfo_req, c)) return;
461 composite_continue(c, userinfo_req, continue_rpc_userinfo, c);
466 * Stage 1: receive result of userinfo call, prepare user changes
467 * (set the fields a caller required to change) and send usermod request
469 static void continue_rpc_userinfo(struct composite_context *ctx)
471 struct composite_context *c;
472 struct modify_user_state *s;
473 struct composite_context *usermod_req;
475 c = talloc_get_type(ctx->async.private_data, struct composite_context);
476 s = talloc_get_type(c->private_data, struct modify_user_state);
478 c->status = libnet_rpc_userinfo_recv(ctx, c, &s->user_info);
479 if (!composite_is_ok(c)) return;
481 s->user_mod.in.domain_handle = s->ctx->samr.handle;
482 s->user_mod.in.username = s->r.in.user_name;
484 c->status = set_user_changes(c, &s->user_mod.in.change, &s->user_info, &s->r);
486 usermod_req = libnet_rpc_usermod_send(s->ctx->samr.pipe, &s->user_mod, s->monitor_fn);
487 if (composite_nomem(usermod_req, c)) return;
489 composite_continue(c, usermod_req, continue_rpc_usermod, c);
494 * Prepare user changes: compare userinfo result to requested changes and
495 * set the field values and flags accordingly for user modify call
497 static NTSTATUS set_user_changes(TALLOC_CTX *mem_ctx, struct usermod_change *mod,
498 struct libnet_rpc_userinfo *info, struct libnet_ModifyUser *r)
500 struct samr_UserInfo21 *user;
502 if (mod == NULL || info == NULL || r == NULL || info->in.level != 21) {
503 return NT_STATUS_INVALID_PARAMETER;
506 user = &info->out.info.info21;
507 mod->fields = 0; /* reset flag field before setting individual flags */
509 /* account name change */
510 SET_FIELD_LSA_STRING(r->in, user, mod, account_name, USERMOD_FIELD_ACCOUNT_NAME);
512 /* full name change */
513 SET_FIELD_LSA_STRING(r->in, user, mod, full_name, USERMOD_FIELD_FULL_NAME);
515 /* description change */
516 SET_FIELD_LSA_STRING(r->in, user, mod, description, USERMOD_FIELD_DESCRIPTION);
519 SET_FIELD_LSA_STRING(r->in, user, mod, comment, USERMOD_FIELD_COMMENT);
521 /* home directory change */
522 SET_FIELD_LSA_STRING(r->in, user, mod, home_directory, USERMOD_FIELD_HOME_DIRECTORY);
524 /* home drive change */
525 SET_FIELD_LSA_STRING(r->in, user, mod, home_drive, USERMOD_FIELD_HOME_DRIVE);
527 /* logon script change */
528 SET_FIELD_LSA_STRING(r->in, user, mod, logon_script, USERMOD_FIELD_LOGON_SCRIPT);
530 /* profile path change */
531 SET_FIELD_LSA_STRING(r->in, user, mod, profile_path, USERMOD_FIELD_PROFILE_PATH);
533 /* account expiry change */
534 SET_FIELD_NTTIME(r->in, user, mod, acct_expiry, USERMOD_FIELD_ACCT_EXPIRY);
541 * Stage 2: receive result of usermod request and finish the composite function
543 static void continue_rpc_usermod(struct composite_context *ctx)
545 struct composite_context *c;
546 struct modify_user_state *s;
547 struct monitor_msg msg;
549 c = talloc_get_type(ctx->async.private_data, struct composite_context);
550 s = talloc_get_type(c->private_data, struct modify_user_state);
552 c->status = libnet_rpc_usermod_recv(ctx, c, &s->user_mod);
553 if (!composite_is_ok(c)) return;
555 if (s->monitor_fn) s->monitor_fn(&msg);
561 * Receive result of ModifyUser call
563 * @param c composite context returned by send request routine
564 * @param mem_ctx memory context of this call
565 * @param r pointer to a structure containing arguments and result of this call
568 NTSTATUS libnet_ModifyUser_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
569 struct libnet_ModifyUser *r)
571 NTSTATUS status = composite_wait(c);
577 * Synchronous version of ModifyUser call
579 * @param ctx initialised libnet context
580 * @param mem_ctx memory context of this call
581 * @param r pointer to a structure containing arguments and result of this call
584 NTSTATUS libnet_ModifyUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
585 struct libnet_ModifyUser *r)
587 struct composite_context *c;
589 c = libnet_ModifyUser_send(ctx, mem_ctx, r, NULL);
590 return libnet_ModifyUser_recv(c, mem_ctx, r);
594 struct user_info_state {
595 struct libnet_context *ctx;
596 const char *domain_name;
597 const char *user_name;
598 struct libnet_LookupName lookup;
599 struct libnet_DomainOpen domopen;
600 struct libnet_rpc_userinfo userinfo;
602 /* information about the progress */
603 void (*monitor_fn)(struct monitor_msg *);
607 static void continue_name_found(struct composite_context *ctx);
608 static void continue_domain_open_info(struct composite_context *ctx);
609 static void continue_info_received(struct composite_context *ctx);
613 * Sends request to get user account information
615 * @param ctx initialised libnet context
616 * @param mem_ctx memory context of this call
617 * @param r pointer to a structure containing arguments and results of this call
618 * @param monitor function pointer for receiving monitor messages
619 * @return compostite context of this request
621 struct composite_context* libnet_UserInfo_send(struct libnet_context *ctx,
623 struct libnet_UserInfo *r,
624 void (*monitor)(struct monitor_msg*))
626 struct composite_context *c;
627 struct user_info_state *s;
628 struct composite_context *lookup_req;
629 BOOL prereq_met = False;
631 /* composite context allocation and setup */
632 c = composite_create(mem_ctx, ctx->event_ctx);
633 if (c == NULL) return NULL;
635 s = talloc_zero(c, struct user_info_state);
636 if (composite_nomem(s, c)) return c;
640 /* store arguments in the state structure */
641 s->monitor_fn = monitor;
643 s->domain_name = talloc_strdup(c, r->in.domain_name);
644 s->user_name = talloc_strdup(c, r->in.user_name);
646 /* prerequisite: make sure the domain is opened */
647 prereq_met = samr_domain_opened(ctx, s->domain_name, &c, &s->domopen,
648 continue_domain_open_info, monitor);
649 if (!prereq_met) return c;
651 /* prepare arguments for LookupName call */
652 s->lookup.in.domain_name = s->domain_name;
653 s->lookup.in.name = s->user_name;
655 /* send the request */
656 lookup_req = libnet_LookupName_send(ctx, c, &s->lookup, s->monitor_fn);
657 if (composite_nomem(lookup_req, c)) return c;
659 /* set the next stage */
660 composite_continue(c, lookup_req, continue_name_found, c);
666 * Stage 0.5 (optional): receive result of domain open request
667 * and send LookupName request
669 static void continue_domain_open_info(struct composite_context *ctx)
671 struct composite_context *c;
672 struct user_info_state *s;
673 struct composite_context *lookup_req;
674 struct monitor_msg msg;
676 c = talloc_get_type(ctx->async.private_data, struct composite_context);
677 s = talloc_get_type(c->private_data, struct user_info_state);
679 /* receive result of DomainOpen call */
680 c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
681 if (!composite_is_ok(c)) return;
683 /* send monitor message */
684 if (s->monitor_fn) s->monitor_fn(&msg);
686 /* prepare arguments for LookupName call */
687 s->lookup.in.domain_name = s->domain_name;
688 s->lookup.in.name = s->user_name;
690 /* send the request */
691 lookup_req = libnet_LookupName_send(s->ctx, c, &s->lookup, s->monitor_fn);
692 if (composite_nomem(lookup_req, c)) return;
694 /* set the next stage */
695 composite_continue(c, lookup_req, continue_name_found, c);
700 * Stage 1: receive the name (if found) and send userinfo request
702 static void continue_name_found(struct composite_context *ctx)
704 struct composite_context *c;
705 struct user_info_state *s;
706 struct composite_context *info_req;
708 c = talloc_get_type(ctx->async.private_data, struct composite_context);
709 s = talloc_get_type(c->private_data, struct user_info_state);
711 /* receive result of LookupName call */
712 c->status = libnet_LookupName_recv(ctx, c, &s->lookup);
713 if (!composite_is_ok(c)) return;
715 /* we're only interested in user accounts this time */
716 if (s->lookup.out.sid_type != SID_NAME_USER) {
717 composite_error(c, NT_STATUS_NO_SUCH_USER);
721 /* prepare arguments for UserInfo call */
722 s->userinfo.in.domain_handle = s->ctx->samr.handle;
723 s->userinfo.in.sid = s->lookup.out.sidstr;
724 s->userinfo.in.level = 21;
726 /* send the request */
727 info_req = libnet_rpc_userinfo_send(s->ctx->samr.pipe, &s->userinfo, s->monitor_fn);
728 if (composite_nomem(info_req, c)) return;
730 /* set the next stage */
731 composite_continue(c, info_req, continue_info_received, c);
736 * Stage 2: receive user account information and finish the composite function
738 static void continue_info_received(struct composite_context *ctx)
740 struct composite_context *c;
741 struct user_info_state *s;
743 c = talloc_get_type(ctx->async.private_data, struct composite_context);
744 s = talloc_get_type(c->private_data, struct user_info_state);
746 /* receive result of userinfo call */
747 c->status = libnet_rpc_userinfo_recv(ctx, c, &s->userinfo);
748 if (!composite_is_ok(c)) return;
755 * Receive result of UserInfo call
757 * @param c composite context returned by send request routine
758 * @param mem_ctx memory context of this call
759 * @param r pointer to a structure containing arguments and result of this call
762 NTSTATUS libnet_UserInfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
763 struct libnet_UserInfo *r)
766 struct user_info_state *s;
768 status = composite_wait(c);
770 if (NT_STATUS_IS_OK(status) && r != NULL) {
771 struct samr_UserInfo21 *info;
773 s = talloc_get_type(c->private_data, struct user_info_state);
774 info = &s->userinfo.out.info.info21;
777 r->out.account_name = talloc_steal(mem_ctx, info->account_name.string);
778 r->out.full_name = talloc_steal(mem_ctx, info->full_name.string);
779 r->out.description = talloc_steal(mem_ctx, info->description.string);
780 r->out.home_directory = talloc_steal(mem_ctx, info->home_directory.string);
781 r->out.home_drive = talloc_steal(mem_ctx, info->home_drive.string);
782 r->out.comment = talloc_steal(mem_ctx, info->comment.string);
783 r->out.logon_script = talloc_steal(mem_ctx, info->logon_script.string);
784 r->out.profile_path = talloc_steal(mem_ctx, info->profile_path.string);
786 /* time fields (allocation) */
787 r->out.acct_expiry = talloc(mem_ctx, struct timeval);
788 r->out.allow_password_change = talloc(mem_ctx, struct timeval);
789 r->out.force_password_change = talloc(mem_ctx, struct timeval);
790 r->out.last_logon = talloc(mem_ctx, struct timeval);
791 r->out.last_logoff = talloc(mem_ctx, struct timeval);
792 r->out.last_password_change = talloc(mem_ctx, struct timeval);
794 /* time fields (converting) */
795 nttime_to_timeval(r->out.acct_expiry, info->acct_expiry);
796 nttime_to_timeval(r->out.allow_password_change, info->allow_password_change);
797 nttime_to_timeval(r->out.force_password_change, info->force_password_change);
798 nttime_to_timeval(r->out.last_logon, info->last_logon);
799 nttime_to_timeval(r->out.last_logoff, info->last_logoff);
800 nttime_to_timeval(r->out.last_password_change, info->last_password_change);
802 /* flag and number fields */
803 r->out.acct_flags = info->acct_flags;
805 r->out.error_string = talloc_strdup(mem_ctx, "Success");
808 r->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
818 * Synchronous version of UserInfo call
820 * @param ctx initialised libnet context
821 * @param mem_ctx memory context of this call
822 * @param r pointer to a structure containing arguments and result of this call
825 NTSTATUS libnet_UserInfo(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
826 struct libnet_UserInfo *r)
828 struct composite_context *c;
830 c = libnet_UserInfo_send(ctx, mem_ctx, r, NULL);
831 return libnet_UserInfo_recv(c, mem_ctx, r);
835 struct userlist_state {
836 struct libnet_context *ctx;
837 const char *domain_name;
838 struct lsa_DomainInfo dominfo;
840 uint32_t resume_index;
841 struct userlist *users;
844 struct libnet_DomainOpen domain_open;
845 struct lsa_QueryInfoPolicy query_domain;
846 struct samr_EnumDomainUsers user_list;
848 void (*monitor_fn)(struct monitor_msg*);
852 static void continue_lsa_domain_opened(struct composite_context *ctx);
853 static void continue_domain_queried(struct rpc_request *req);
854 static void continue_samr_domain_opened(struct composite_context *ctx);
855 static void continue_users_enumerated(struct rpc_request *req);
859 * Sends request to list (enumerate) user accounts
861 * @param ctx initialised libnet context
862 * @param mem_ctx memory context of this call
863 * @param r pointer to a structure containing arguments and results of this call
864 * @param monitor function pointer for receiving monitor messages
865 * @return compostite context of this request
867 struct composite_context* libnet_UserList_send(struct libnet_context *ctx,
869 struct libnet_UserList *r,
870 void (*monitor)(struct monitor_msg*))
872 struct composite_context *c;
873 struct userlist_state *s;
874 struct rpc_request *query_req;
875 BOOL prereq_met = False;
877 /* composite context allocation and setup */
878 c = composite_create(mem_ctx, ctx->event_ctx);
879 if (c == NULL) return NULL;
881 s = talloc_zero(c, struct userlist_state);
882 if (composite_nomem(s, c)) return c;
886 /* store the arguments in the state structure */
888 s->page_size = r->in.page_size;
889 s->resume_index = (uint32_t)r->in.resume_index;
890 s->domain_name = talloc_strdup(c, r->in.domain_name);
891 s->monitor_fn = monitor;
893 /* make sure we have lsa domain handle before doing anything */
894 prereq_met = lsa_domain_opened(ctx, s->domain_name, &c, &s->domain_open,
895 continue_lsa_domain_opened, monitor);
896 if (!prereq_met) return c;
898 /* prepare arguments of QueryDomainInfo call */
899 s->query_domain.in.handle = &ctx->lsa.handle;
900 s->query_domain.in.level = LSA_POLICY_INFO_DOMAIN;
902 /* send the request */
903 query_req = dcerpc_lsa_QueryInfoPolicy_send(ctx->lsa.pipe, c, &s->query_domain);
904 if (composite_nomem(query_req, c)) return c;
906 composite_continue_rpc(c, query_req, continue_domain_queried, c);
912 * Stage 0.5 (optional): receive lsa domain handle and send
913 * request to query domain info
915 static void continue_lsa_domain_opened(struct composite_context *ctx)
917 struct composite_context *c;
918 struct userlist_state *s;
919 struct rpc_request *query_req;
921 c = talloc_get_type(ctx->async.private_data, struct composite_context);
922 s = talloc_get_type(c->private_data, struct userlist_state);
924 /* receive lsa domain handle */
925 c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
926 if (!composite_is_ok(c)) return;
928 /* prepare arguments of QueryDomainInfo call */
929 s->query_domain.in.handle = &s->ctx->lsa.handle;
930 s->query_domain.in.level = LSA_POLICY_INFO_DOMAIN;
932 /* send the request */
933 query_req = dcerpc_lsa_QueryInfoPolicy_send(s->ctx->lsa.pipe, c, &s->query_domain);
934 if (composite_nomem(query_req, c)) return;
936 composite_continue_rpc(c, query_req, continue_domain_queried, c);
941 * Stage 1: receive domain info and request to enum users,
942 * provided a valid samr handle is opened
944 static void continue_domain_queried(struct rpc_request *req)
946 struct composite_context *c;
947 struct userlist_state *s;
948 struct rpc_request *enum_req;
949 BOOL prereq_met = False;
951 c = talloc_get_type(req->async.private_data, struct composite_context);
952 s = talloc_get_type(c->private_data, struct userlist_state);
954 /* receive result of rpc request */
955 c->status = dcerpc_ndr_request_recv(req);
956 if (!composite_is_ok(c)) return;
958 /* get the returned domain info */
959 s->dominfo = s->query_domain.out.info->domain;
961 /* make sure we have samr domain handle before continuing */
962 prereq_met = samr_domain_opened(s->ctx, s->domain_name, &c, &s->domain_open,
963 continue_samr_domain_opened, s->monitor_fn);
964 if (!prereq_met) return;
966 /* prepare arguments od EnumDomainUsers call */
967 s->user_list.in.domain_handle = &s->ctx->samr.handle;
968 s->user_list.in.max_size = s->page_size;
969 s->user_list.in.resume_handle = &s->resume_index;
970 s->user_list.in.acct_flags = ACB_NORMAL;
971 s->user_list.out.resume_handle = &s->resume_index;
973 /* send the request */
974 enum_req = dcerpc_samr_EnumDomainUsers_send(s->ctx->samr.pipe, c, &s->user_list);
975 if (composite_nomem(enum_req, c)) return;
977 composite_continue_rpc(c, enum_req, continue_users_enumerated, c);
982 * Stage 1.5 (optional): receive samr domain handle
983 * and request to enumerate accounts
985 static void continue_samr_domain_opened(struct composite_context *ctx)
987 struct composite_context *c;
988 struct userlist_state *s;
989 struct rpc_request *enum_req;
991 c = talloc_get_type(ctx->async.private_data, struct composite_context);
992 s = talloc_get_type(c->private_data, struct userlist_state);
994 /* receive samr domain handle */
995 c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
996 if (!composite_is_ok(c)) return;
998 /* prepare arguments od EnumDomainUsers call */
999 s->user_list.in.domain_handle = &s->ctx->samr.handle;
1000 s->user_list.in.max_size = s->page_size;
1001 s->user_list.in.resume_handle = &s->resume_index;
1002 s->user_list.in.acct_flags = ACB_NORMAL;
1003 s->user_list.out.resume_handle = &s->resume_index;
1005 /* send the request */
1006 enum_req = dcerpc_samr_EnumDomainUsers_send(s->ctx->samr.pipe, c, &s->user_list);
1007 if (composite_nomem(enum_req, c)) return;
1009 composite_continue_rpc(c, enum_req, continue_users_enumerated, c);
1014 * Stage 2: receive enumerated users and their rids
1016 static void continue_users_enumerated(struct rpc_request *req)
1018 struct composite_context *c;
1019 struct userlist_state *s;
1022 c = talloc_get_type(req->async.private_data, struct composite_context);
1023 s = talloc_get_type(c->private_data, struct userlist_state);
1025 /* receive result of rpc request */
1026 c->status = dcerpc_ndr_request_recv(req);
1027 if (!composite_is_ok(c)) return;
1029 /* get the actual status of the rpc call result (instead of rpc layer status) */
1030 c->status = s->user_list.out.result;
1032 /* we're interested in status "ok" as well as two enum-specific status codes */
1033 if (NT_STATUS_IS_OK(c->status) ||
1034 NT_STATUS_EQUAL(c->status, STATUS_MORE_ENTRIES) ||
1035 NT_STATUS_EQUAL(c->status, NT_STATUS_NO_MORE_ENTRIES)) {
1037 /* get enumerated accounts counter and resume handle (the latter allows
1038 making subsequent call to continue enumeration) */
1039 s->resume_index = *s->user_list.out.resume_handle;
1040 s->count = s->user_list.out.num_entries;
1042 /* prepare returned user accounts array */
1043 s->users = talloc_array(c, struct userlist, s->user_list.out.sam->count);
1044 if (composite_nomem(s->users, c)) return;
1046 for (i = 0; i < s->user_list.out.sam->count; i++) {
1047 struct dom_sid *user_sid;
1048 struct samr_SamEntry *entry = &s->user_list.out.sam->entries[i];
1049 struct dom_sid *domain_sid = s->query_domain.out.info->domain.sid;
1051 /* construct user sid from returned rid and queried domain sid */
1052 user_sid = dom_sid_add_rid(c, domain_sid, entry->idx);
1053 if (composite_nomem(user_sid, c)) return;
1056 s->users[i].username = talloc_strdup(c, entry->name.string);
1057 if (composite_nomem(s->users[i].username, c)) return;
1060 s->users[i].sid = dom_sid_string(c, user_sid);
1061 if (composite_nomem(s->users[i].sid, c)) return;
1068 /* something went wrong */
1069 composite_error(c, c->status);
1075 * Receive result of UserList call
1077 * @param c composite context returned by send request routine
1078 * @param mem_ctx memory context of this call
1079 * @param r pointer to a structure containing arguments and result of this call
1082 NTSTATUS libnet_UserList_recv(struct composite_context* c, TALLOC_CTX *mem_ctx,
1083 struct libnet_UserList *r)
1086 struct userlist_state *s;
1088 if (c == NULL || mem_ctx == NULL || r == NULL) {
1089 return NT_STATUS_INVALID_PARAMETER;
1092 status = composite_wait(c);
1093 if (NT_STATUS_IS_OK(status) ||
1094 NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) ||
1095 NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1097 s = talloc_get_type(c->private_data, struct userlist_state);
1099 /* get results from composite context */
1100 r->out.count = s->count;
1101 r->out.resume_index = s->resume_index;
1102 r->out.users = talloc_steal(mem_ctx, s->users);
1104 if (NT_STATUS_IS_OK(status)) {
1105 r->out.error_string = talloc_strdup(mem_ctx, "Success");
1107 /* success, but we're not done yet */
1108 r->out.error_string = talloc_asprintf(mem_ctx, "Success (status: %s)",
1113 r->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
1121 * Synchronous version of UserList call
1123 * @param ctx initialised libnet context
1124 * @param mem_ctx memory context of this call
1125 * @param r pointer to a structure containing arguments and result of this call
1128 NTSTATUS libnet_UserList(struct libnet_context *ctx,
1129 TALLOC_CTX *mem_ctx,
1130 struct libnet_UserList *r)
1132 struct composite_context *c;
1134 c = libnet_UserList_send(ctx, mem_ctx, r, NULL);
1135 return libnet_UserList_recv(c, mem_ctx, r);