af173e993d10e102fbe37916eb0e8a080a837625
[kai/samba-autobuild/.git] / source4 / libnet / libnet_user.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Copyright (C) Rafal Szczesniak  2005
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 "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"
31
32
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;
38
39         /* information about the progress */
40         void (*monitor_fn)(struct monitor_msg *);
41 };
42
43
44 static void continue_rpc_useradd(struct composite_context *ctx);
45 static void continue_domain_open_create(struct composite_context *ctx);
46
47
48 /**
49  * Sends request to create user account
50  *
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
56  */
57 struct composite_context* libnet_CreateUser_send(struct libnet_context *ctx,
58                                                  TALLOC_CTX *mem_ctx,
59                                                  struct libnet_CreateUser *r,
60                                                  void (*monitor)(struct monitor_msg*))
61 {
62         struct composite_context *c;
63         struct create_user_state *s;
64         struct composite_context *create_req;
65         bool prereq_met = false;
66
67         /* composite context allocation and setup */
68         c = composite_create(mem_ctx, ctx->event_ctx);
69         if (c == NULL) return NULL;
70
71         s = talloc_zero(c, struct create_user_state);
72         if (composite_nomem(s, c)) return c;
73
74         c->private_data = s;
75
76         /* store arguments in the state structure */
77         s->ctx = ctx;
78         s->r   = *r;
79         ZERO_STRUCT(s->r.out);
80
81         /* prerequisite: make sure the domain is opened */
82         prereq_met = samr_domain_opened(ctx, c, s->r.in.domain_name, &c, &s->domain_open,
83                                         continue_domain_open_create, monitor);
84         if (!prereq_met) return c;
85
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;
89
90         /* send the request */
91         create_req = libnet_rpc_useradd_send(ctx->samr.pipe, s, &s->user_add, monitor);
92         if (composite_nomem(create_req, c)) return c;
93
94         /* set the next stage */
95         composite_continue(c, create_req, continue_rpc_useradd, c);
96         return c;
97 }
98
99
100 /*
101  * Stage 0.5 (optional): receive result of domain open request
102  * and send useradd request
103  */
104 static void continue_domain_open_create(struct composite_context *ctx)
105 {
106         struct composite_context *c;
107         struct create_user_state *s;
108         struct composite_context *create_req;
109         struct monitor_msg msg;
110
111         c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
112         s = talloc_get_type_abort(c->private_data, struct create_user_state);
113
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;
117
118         /* send monitor message */
119         if (s->monitor_fn) s->monitor_fn(&msg);
120         
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;
124
125         /* send the request */
126         create_req = libnet_rpc_useradd_send(s->ctx->samr.pipe, s, &s->user_add, s->monitor_fn);
127         if (composite_nomem(create_req, c)) return;
128
129         /* set the next stage */
130         composite_continue(c, create_req, continue_rpc_useradd, c);
131 }
132
133
134 /*
135  * Stage 1: receive result of useradd call
136  */
137 static void continue_rpc_useradd(struct composite_context *ctx)
138 {
139         struct composite_context *c;
140         struct create_user_state *s;
141         struct monitor_msg msg;
142
143         c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
144         s = talloc_get_type_abort(c->private_data, struct create_user_state);
145         
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;
149
150         /* send monitor message */
151         if (s->monitor_fn) s->monitor_fn(&msg);
152
153         /* we're done */
154         composite_done(c);
155 }
156
157
158 /**
159  * Receive result of CreateUser call
160  *
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
164  * @return nt status
165  */
166 NTSTATUS libnet_CreateUser_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
167                                 struct libnet_CreateUser *r)
168 {
169         NTSTATUS status;
170
171         r->out.error_string = NULL;
172
173         /* wait for result of async request and check status code */
174         status = composite_wait(c);
175         if (!NT_STATUS_IS_OK(status)) {
176                 r->out.error_string = talloc_strdup(mem_ctx, nt_errstr(status));
177         }
178
179         talloc_free(c);
180         return status;
181 }
182
183
184 /**
185  * Synchronous version of CreateUser call
186  *
187  * @param ctx initialised libnet context
188  * @param mem_ctx memory context of this call
189  * @param r pointer to a structure containing arguments and result of this call
190  * @return nt status
191  */
192 NTSTATUS libnet_CreateUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
193                            struct libnet_CreateUser *r)
194 {
195         struct composite_context *c;
196
197         c = libnet_CreateUser_send(ctx, mem_ctx, r, NULL);
198         return libnet_CreateUser_recv(c, mem_ctx, r);
199 }
200
201
202 struct delete_user_state {
203         struct libnet_DeleteUser r;
204         struct libnet_context *ctx;
205         struct libnet_DomainOpen domain_open;
206         struct libnet_rpc_userdel user_del;
207
208         /* information about the progress */
209         void (*monitor_fn)(struct monitor_msg *);
210 };
211
212
213 static void continue_rpc_userdel(struct composite_context *ctx);
214 static void continue_domain_open_delete(struct composite_context *ctx);
215
216
217 /**
218  * Sends request to delete user account
219  *
220  * @param ctx initialised libnet context
221  * @param mem_ctx memory context of this call
222  * @param r pointer to structure containing arguments and result of this call
223  * @param monitor function pointer for receiving monitor messages
224  */
225 struct composite_context *libnet_DeleteUser_send(struct libnet_context *ctx,
226                                                  TALLOC_CTX *mem_ctx,
227                                                  struct libnet_DeleteUser *r,
228                                                  void (*monitor)(struct monitor_msg*))
229 {
230         struct composite_context *c;
231         struct delete_user_state *s;
232         struct composite_context *delete_req;
233         bool prereq_met = false;
234
235         /* composite context allocation and setup */
236         c = composite_create(mem_ctx, ctx->event_ctx);
237         if (c == NULL) return NULL;
238
239         s = talloc_zero(c, struct delete_user_state);
240         if (composite_nomem(s, c)) return c;
241
242         c->private_data = s;
243
244         /* store arguments in state structure */
245         s->ctx = ctx;
246         s->r   = *r;
247         ZERO_STRUCT(s->r.out);
248         
249         /* prerequisite: make sure the domain is opened before proceeding */
250         prereq_met = samr_domain_opened(ctx, c, s->r.in.domain_name, &c, &s->domain_open,
251                                         continue_domain_open_delete, monitor);
252         if (!prereq_met) return c;
253
254         /* prepare arguments for userdel call */
255         s->user_del.in.username       = r->in.user_name;
256         s->user_del.in.domain_handle  = ctx->samr.handle;
257
258         /* send request */
259         delete_req = libnet_rpc_userdel_send(ctx->samr.pipe, s, &s->user_del, monitor);
260         if (composite_nomem(delete_req, c)) return c;
261         
262         /* set the next stage */
263         composite_continue(c, delete_req, continue_rpc_userdel, c);
264         return c;
265 }
266
267
268 /*
269  * Stage 0.5 (optional): receive result of domain open request
270  * and send useradd request
271  */
272 static void continue_domain_open_delete(struct composite_context *ctx)
273 {
274         struct composite_context *c;
275         struct delete_user_state *s;
276         struct composite_context *delete_req;
277         struct monitor_msg msg;
278
279         c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
280         s = talloc_get_type_abort(c->private_data, struct delete_user_state);
281
282         /* receive result of DomainOpen call */
283         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
284         if (!composite_is_ok(c)) return;
285         
286         /* send monitor message */
287         if (s->monitor_fn) s->monitor_fn(&msg);
288
289         /* prepare arguments for userdel call */
290         s->user_del.in.username       = s->r.in.user_name;
291         s->user_del.in.domain_handle  = s->ctx->samr.handle;
292
293         /* send request */
294         delete_req = libnet_rpc_userdel_send(s->ctx->samr.pipe, s, &s->user_del, s->monitor_fn);
295         if (composite_nomem(delete_req, c)) return;
296
297         /* set the next stage */
298         composite_continue(c, delete_req, continue_rpc_userdel, c);
299 }
300
301
302 /*
303  * Stage 1: receive result of userdel call and finish the composite function
304  */
305 static void continue_rpc_userdel(struct composite_context *ctx)
306 {
307         struct composite_context *c;
308         struct delete_user_state *s;
309         struct monitor_msg msg;
310
311         c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
312         s = talloc_get_type_abort(c->private_data, struct delete_user_state);
313
314         /* receive result of userdel call */
315         c->status = libnet_rpc_userdel_recv(ctx, c, &s->user_del);
316         if (!composite_is_ok(c)) return;
317
318         /* send monitor message */
319         if (s->monitor_fn) s->monitor_fn(&msg);
320
321         /* we're done */
322         composite_done(c);
323 }
324
325
326 /**
327  * Receives result of asynchronous DeleteUser call
328  *
329  * @param c composite context returned by async DeleteUser call
330  * @param mem_ctx memory context of this call
331  * @param r pointer to structure containing arguments and result
332  */
333 NTSTATUS libnet_DeleteUser_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
334                                 struct libnet_DeleteUser *r)
335 {
336         NTSTATUS status;
337         struct delete_user_state *s;
338
339         r->out.error_string = NULL;
340
341         /* wait for result of async request and check status code */
342         status = composite_wait(c);
343         if (!NT_STATUS_IS_OK(status)) {
344                 s = talloc_get_type_abort(c->private_data, struct delete_user_state);
345                 r->out.error_string = talloc_steal(mem_ctx, s->r.out.error_string);
346         }
347         
348         talloc_free(c);
349         return status;
350 }
351
352
353 /**
354  * Synchronous version of DeleteUser call
355  *
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
359  */
360 NTSTATUS libnet_DeleteUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
361                            struct libnet_DeleteUser *r)
362 {
363         struct composite_context *c;
364         
365         c = libnet_DeleteUser_send(ctx, mem_ctx, r, NULL);
366         return libnet_DeleteUser_recv(c, mem_ctx, r);
367 }
368
369
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;
376
377         void (*monitor_fn)(struct monitor_msg *);
378 };
379
380
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);
386
387
388 /**
389  * Sends request to modify user account
390  *
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
395  */
396 struct composite_context *libnet_ModifyUser_send(struct libnet_context *ctx,
397                                                  TALLOC_CTX *mem_ctx,
398                                                  struct libnet_ModifyUser *r,
399                                                  void (*monitor)(struct monitor_msg*))
400 {
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;
406
407         c = composite_create(mem_ctx, ctx->event_ctx);
408         if (c == NULL) return NULL;
409
410         s = talloc_zero(c, struct modify_user_state);
411         if (composite_nomem(s, c)) return c;
412
413         c->private_data = s;
414
415         s->ctx = ctx;
416         s->r = *r;
417
418         prereq_met = samr_domain_opened(ctx, c, s->r.in.domain_name, &c, &s->domain_open,
419                                         continue_domain_open_modify, monitor);
420         if (!prereq_met) return c;
421
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;
425
426         userinfo_req = libnet_rpc_userinfo_send(ctx->samr.pipe, s, &s->user_info, monitor);
427         if (composite_nomem(userinfo_req, c)) return c;
428
429         composite_continue(c, userinfo_req, continue_rpc_userinfo, c);
430         return c;
431 }
432
433
434 /*
435  * Stage 0.5 (optional): receive result of domain open request
436  * and send userinfo request
437  */
438 static void continue_domain_open_modify(struct composite_context *ctx)
439 {
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;
445
446         c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
447         s = talloc_get_type_abort(c->private_data, struct modify_user_state);
448
449         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
450         if (!composite_is_ok(c)) return;
451
452         if (s->monitor_fn) s->monitor_fn(&msg);
453         
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;
457
458         userinfo_req = libnet_rpc_userinfo_send(s->ctx->samr.pipe, s, &s->user_info, s->monitor_fn);
459         if (composite_nomem(userinfo_req, c)) return;
460         
461         composite_continue(c, userinfo_req, continue_rpc_userinfo, c);
462 }
463
464
465 /*
466  * Stage 1: receive result of userinfo call, prepare user changes
467  * (set the fields a caller required to change) and send usermod request
468  */
469 static void continue_rpc_userinfo(struct composite_context *ctx)
470 {
471         struct composite_context *c;
472         struct modify_user_state *s;
473         struct composite_context *usermod_req;
474
475         c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
476         s = talloc_get_type_abort(c->private_data, struct modify_user_state);
477
478         c->status = libnet_rpc_userinfo_recv(ctx, c, &s->user_info);
479         if (!composite_is_ok(c)) return;
480
481         s->user_mod.in.domain_handle = s->ctx->samr.handle;
482         s->user_mod.in.username      = s->r.in.user_name;
483
484         c->status = set_user_changes(c, &s->user_mod.in.change, &s->user_info, &s->r);
485
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;
488
489         composite_continue(c, usermod_req, continue_rpc_usermod, c);
490 }
491
492
493 /*
494  * Prepare user changes: compare userinfo result to requested changes and
495  * set the field values and flags accordingly for user modify call
496  */
497 static NTSTATUS set_user_changes(TALLOC_CTX *mem_ctx, struct usermod_change *mod,
498                                  struct libnet_rpc_userinfo *info, struct libnet_ModifyUser *r)
499 {
500         struct samr_UserInfo21 *user;
501
502         if (mod == NULL || info == NULL || r == NULL || info->in.level != 21) {
503                 return NT_STATUS_INVALID_PARAMETER;
504         }
505
506         user = &info->out.info.info21;
507         mod->fields = 0;        /* reset flag field before setting individual flags */
508
509         /* account name change */
510         SET_FIELD_LSA_STRING(r->in, user, mod, account_name, USERMOD_FIELD_ACCOUNT_NAME);
511
512         /* full name change */
513         SET_FIELD_LSA_STRING(r->in, user, mod, full_name, USERMOD_FIELD_FULL_NAME);
514
515         /* description change */
516         SET_FIELD_LSA_STRING(r->in, user, mod, description, USERMOD_FIELD_DESCRIPTION);
517
518         /* comment change */
519         SET_FIELD_LSA_STRING(r->in, user, mod, comment, USERMOD_FIELD_COMMENT);
520
521         /* home directory change */
522         SET_FIELD_LSA_STRING(r->in, user, mod, home_directory, USERMOD_FIELD_HOME_DIRECTORY);
523
524         /* home drive change */
525         SET_FIELD_LSA_STRING(r->in, user, mod, home_drive, USERMOD_FIELD_HOME_DRIVE);
526
527         /* logon script change */
528         SET_FIELD_LSA_STRING(r->in, user, mod, logon_script, USERMOD_FIELD_LOGON_SCRIPT);
529
530         /* profile path change */
531         SET_FIELD_LSA_STRING(r->in, user, mod, profile_path, USERMOD_FIELD_PROFILE_PATH);
532
533         /* account expiry change */
534         SET_FIELD_NTTIME(r->in, user, mod, acct_expiry, USERMOD_FIELD_ACCT_EXPIRY);
535
536         /* account flags change */
537         SET_FIELD_ACCT_FLAGS(r->in, user, mod, acct_flags, USERMOD_FIELD_ACCT_FLAGS);
538
539         return NT_STATUS_OK;
540 }
541
542
543 /*
544  * Stage 2: receive result of usermod request and finish the composite function
545  */
546 static void continue_rpc_usermod(struct composite_context *ctx)
547 {
548         struct composite_context *c;
549         struct modify_user_state *s;
550         struct monitor_msg msg;
551
552         c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
553         s = talloc_get_type_abort(c->private_data, struct modify_user_state);
554         
555         c->status = libnet_rpc_usermod_recv(ctx, c, &s->user_mod);
556         if (!composite_is_ok(c)) return;
557         
558         if (s->monitor_fn) s->monitor_fn(&msg);
559         composite_done(c);
560 }
561
562
563 /**
564  * Receive result of ModifyUser call
565  *
566  * @param c composite context returned by send request routine
567  * @param mem_ctx memory context of this call
568  * @param r pointer to a structure containing arguments and result of this call
569  * @return nt status
570  */
571 NTSTATUS libnet_ModifyUser_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
572                                 struct libnet_ModifyUser *r)
573 {
574         NTSTATUS status = composite_wait(c);
575
576         talloc_free(c);
577         return status;
578 }
579
580
581 /**
582  * Synchronous version of ModifyUser call
583  *
584  * @param ctx initialised libnet context
585  * @param mem_ctx memory context of this call
586  * @param r pointer to a structure containing arguments and result of this call
587  * @return nt status
588  */
589 NTSTATUS libnet_ModifyUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
590                            struct libnet_ModifyUser *r)
591 {
592         struct composite_context *c;
593
594         c = libnet_ModifyUser_send(ctx, mem_ctx, r, NULL);
595         return libnet_ModifyUser_recv(c, mem_ctx, r);
596 }
597
598
599 struct user_info_state {
600         struct libnet_context *ctx;
601         const char *domain_name;
602         enum libnet_UserInfo_level level;
603         const char *user_name;
604         const char *sid_string;
605         struct libnet_LookupName lookup;
606         struct libnet_DomainOpen domopen;
607         struct libnet_rpc_userinfo userinfo;
608
609         /* information about the progress */
610         void (*monitor_fn)(struct monitor_msg *);
611 };
612
613
614 static void continue_name_found(struct composite_context *ctx);
615 static void continue_domain_open_info(struct composite_context *ctx);
616 static void continue_info_received(struct composite_context *ctx);
617
618
619 /**
620  * Sends request to get user account information
621  *
622  * @param ctx initialised libnet context
623  * @param mem_ctx memory context of this call
624  * @param r pointer to a structure containing arguments and results of this call
625  * @param monitor function pointer for receiving monitor messages
626  * @return compostite context of this request
627  */
628 struct composite_context* libnet_UserInfo_send(struct libnet_context *ctx,
629                                                TALLOC_CTX *mem_ctx,
630                                                struct libnet_UserInfo *r,
631                                                void (*monitor)(struct monitor_msg*))
632 {
633         struct composite_context *c;
634         struct user_info_state *s;
635         struct composite_context *lookup_req, *info_req;
636         bool prereq_met = false;
637
638         /* composite context allocation and setup */
639         c = composite_create(mem_ctx, ctx->event_ctx);
640         if (c == NULL) return NULL;
641
642         s = talloc_zero(c, struct user_info_state);
643         if (composite_nomem(s, c)) return c;
644
645         c->private_data = s;
646
647         /* store arguments in the state structure */
648         s->monitor_fn = monitor;
649         s->ctx = ctx;
650         s->domain_name = talloc_strdup(c, r->in.domain_name);
651         s->level = r->in.level;
652         switch (s->level) {
653         case USER_INFO_BY_NAME:
654                 s->user_name = talloc_strdup(c, r->in.data.user_name);
655                 s->sid_string = NULL;
656                 break;
657         case USER_INFO_BY_SID:
658                 s->user_name = NULL;
659                 s->sid_string = dom_sid_string(c, r->in.data.user_sid);
660                 break;
661         }
662
663         /* prerequisite: make sure the domain is opened */
664         prereq_met = samr_domain_opened(ctx, c, s->domain_name, &c, &s->domopen,
665                                         continue_domain_open_info, monitor);
666         if (!prereq_met) return c;
667
668         switch (s->level) {
669         case USER_INFO_BY_NAME:
670                 /* prepare arguments for LookupName call */
671                 s->lookup.in.domain_name = s->domain_name;
672                 s->lookup.in.name        = s->user_name;
673
674                 /* send the request */
675                 lookup_req = libnet_LookupName_send(ctx, c, &s->lookup,
676                                                     s->monitor_fn);
677                 if (composite_nomem(lookup_req, c)) return c;
678
679                 /* set the next stage */
680                 composite_continue(c, lookup_req, continue_name_found, c);
681                 break;
682         case USER_INFO_BY_SID:
683                 /* prepare arguments for UserInfo call */
684                 s->userinfo.in.domain_handle = s->ctx->samr.handle;
685                 s->userinfo.in.sid = s->sid_string;
686                 s->userinfo.in.level = 21;
687
688                 /* send the request */
689                 info_req = libnet_rpc_userinfo_send(s->ctx->samr.pipe,
690                                                     s,
691                                                     &s->userinfo,
692                                                     s->monitor_fn);
693                 if (composite_nomem(info_req, c)) return c;
694
695                 /* set the next stage */
696                 composite_continue(c, info_req, continue_info_received, c);
697                 break;
698         }
699
700         return c;
701 }
702
703
704 /*
705  * Stage 0.5 (optional): receive result of domain open request
706  * and send LookupName request
707  */
708 static void continue_domain_open_info(struct composite_context *ctx)
709 {
710         struct composite_context *c;
711         struct user_info_state *s;
712         struct composite_context *lookup_req, *info_req;
713         struct monitor_msg msg;
714
715         c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
716         s = talloc_get_type_abort(c->private_data, struct user_info_state);
717
718         /* receive result of DomainOpen call */
719         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
720         if (!composite_is_ok(c)) return;
721
722         /* send monitor message */
723         if (s->monitor_fn) s->monitor_fn(&msg);
724
725         switch (s->level) {
726         case USER_INFO_BY_NAME:
727                 /* prepare arguments for LookupName call */
728                 s->lookup.in.domain_name = s->domain_name;
729                 s->lookup.in.name        = s->user_name;
730
731                 /* send the request */
732                 lookup_req = libnet_LookupName_send(s->ctx, c, &s->lookup, s->monitor_fn);
733                 if (composite_nomem(lookup_req, c)) return;
734
735                 /* set the next stage */
736                 composite_continue(c, lookup_req, continue_name_found, c);
737                 break;
738
739         case USER_INFO_BY_SID:
740                 /* prepare arguments for UserInfo call */
741                 s->userinfo.in.domain_handle = s->ctx->samr.handle;
742                 s->userinfo.in.sid = s->sid_string;
743                 s->userinfo.in.level = 21;
744
745                 /* send the request */
746                 info_req = libnet_rpc_userinfo_send(s->ctx->samr.pipe,
747                                                     s,
748                                                     &s->userinfo,
749                                                     s->monitor_fn);
750                 if (composite_nomem(info_req, c)) return;
751
752                 /* set the next stage */
753                 composite_continue(c, info_req, continue_info_received, c);
754                 break;
755         }
756 }
757
758
759 /*
760  * Stage 1: receive the name (if found) and send userinfo request
761  */
762 static void continue_name_found(struct composite_context *ctx)
763 {
764         struct composite_context *c;
765         struct user_info_state *s;
766         struct composite_context *info_req;
767
768         c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
769         s = talloc_get_type_abort(c->private_data, struct user_info_state);
770
771         /* receive result of LookupName call */
772         c->status = libnet_LookupName_recv(ctx, c, &s->lookup);
773         if (!composite_is_ok(c)) return;
774
775         /* we're only interested in user accounts this time */
776         if (s->lookup.out.sid_type != SID_NAME_USER) {
777                 composite_error(c, NT_STATUS_NO_SUCH_USER);
778                 return;
779         }
780
781         /* prepare arguments for UserInfo call */
782         s->userinfo.in.domain_handle = s->ctx->samr.handle;
783         s->userinfo.in.sid = s->lookup.out.sidstr;
784         s->userinfo.in.level = 21;
785
786         /* send the request */
787         info_req = libnet_rpc_userinfo_send(s->ctx->samr.pipe, s, &s->userinfo, s->monitor_fn);
788         if (composite_nomem(info_req, c)) return;
789
790         /* set the next stage */
791         composite_continue(c, info_req, continue_info_received, c);
792 }
793
794
795 /*
796  * Stage 2: receive user account information and finish the composite function
797  */
798 static void continue_info_received(struct composite_context *ctx)
799 {
800         struct composite_context *c;
801         struct user_info_state *s;
802
803         c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
804         s = talloc_get_type_abort(c->private_data, struct user_info_state);
805         
806         /* receive result of userinfo call */
807         c->status = libnet_rpc_userinfo_recv(ctx, c, &s->userinfo);
808         if (!composite_is_ok(c)) return;
809
810         composite_done(c);
811 }
812
813
814 /**
815  * Receive result of UserInfo call
816  *
817  * @param c composite context returned by send request routine
818  * @param mem_ctx memory context of this call
819  * @param r pointer to a structure containing arguments and result of this call
820  * @return nt status
821  */
822 NTSTATUS libnet_UserInfo_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
823                               struct libnet_UserInfo *r)
824 {
825         NTSTATUS status;
826         struct user_info_state *s;
827
828         status = composite_wait(c);
829
830         if (NT_STATUS_IS_OK(status) && r != NULL) {
831                 struct samr_UserInfo21 *info;
832
833                 s = talloc_get_type_abort(c->private_data, struct user_info_state);
834                 info = &s->userinfo.out.info.info21;
835
836                 r->out.user_sid = dom_sid_add_rid(mem_ctx, s->ctx->samr.sid, info->rid);
837                 r->out.primary_group_sid = dom_sid_add_rid(mem_ctx, s->ctx->samr.sid, info->primary_gid);
838
839                 /* string fields */
840                 r->out.account_name   = talloc_steal(mem_ctx, info->account_name.string);
841                 r->out.full_name      = talloc_steal(mem_ctx, info->full_name.string);
842                 r->out.description    = talloc_steal(mem_ctx, info->description.string);
843                 r->out.home_directory = talloc_steal(mem_ctx, info->home_directory.string);
844                 r->out.home_drive     = talloc_steal(mem_ctx, info->home_drive.string);
845                 r->out.comment        = talloc_steal(mem_ctx, info->comment.string);
846                 r->out.logon_script   = talloc_steal(mem_ctx, info->logon_script.string);
847                 r->out.profile_path   = talloc_steal(mem_ctx, info->profile_path.string);
848
849                 /* time fields (allocation) */
850                 r->out.acct_expiry           = talloc(mem_ctx, struct timeval);
851                 r->out.allow_password_change = talloc(mem_ctx, struct timeval);
852                 r->out.force_password_change = talloc(mem_ctx, struct timeval);
853                 r->out.last_logon            = talloc(mem_ctx, struct timeval);
854                 r->out.last_logoff           = talloc(mem_ctx, struct timeval);
855                 r->out.last_password_change  = talloc(mem_ctx, struct timeval);
856                 
857                 /* time fields (converting) */
858                 nttime_to_timeval(r->out.acct_expiry, info->acct_expiry);
859                 nttime_to_timeval(r->out.allow_password_change, info->allow_password_change);
860                 nttime_to_timeval(r->out.force_password_change, info->force_password_change);
861                 nttime_to_timeval(r->out.last_logon, info->last_logon);
862                 nttime_to_timeval(r->out.last_logoff, info->last_logoff);
863                 nttime_to_timeval(r->out.last_password_change, info->last_password_change);
864
865                 /* flag and number fields */
866                 r->out.acct_flags = info->acct_flags;
867
868                 r->out.error_string = talloc_strdup(mem_ctx, "Success");
869
870         } else {
871                 r->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
872         }
873
874         talloc_free(c);
875         return status;
876 }
877
878
879 /**
880  * Synchronous version of UserInfo call
881  *
882  * @param ctx initialised libnet context
883  * @param mem_ctx memory context of this call
884  * @param r pointer to a structure containing arguments and result of this call
885  * @return nt status
886  */
887 NTSTATUS libnet_UserInfo(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
888                          struct libnet_UserInfo *r)
889 {
890         struct composite_context *c;
891         
892         c = libnet_UserInfo_send(ctx, mem_ctx, r, NULL);
893         return libnet_UserInfo_recv(c, mem_ctx, r);
894 }
895
896
897 struct userlist_state {
898         struct libnet_context *ctx;
899         const char *domain_name;
900         struct lsa_DomainInfo dominfo;
901         int page_size;
902         uint32_t resume_index;
903         struct userlist *users;
904         uint32_t count;
905
906         struct libnet_DomainOpen domain_open;
907         struct lsa_QueryInfoPolicy query_domain;
908         struct samr_EnumDomainUsers user_list;
909
910         void (*monitor_fn)(struct monitor_msg*);
911 };
912
913
914 static void continue_lsa_domain_opened(struct composite_context *ctx);
915 static void continue_domain_queried(struct tevent_req *subreq);
916 static void continue_samr_domain_opened(struct composite_context *ctx);
917 static void continue_users_enumerated(struct tevent_req *subreq);
918
919
920 /**
921  * Sends request to list (enumerate) user accounts
922  *
923  * @param ctx initialised libnet context
924  * @param mem_ctx memory context of this call
925  * @param r pointer to structure containing arguments and results of this call
926  * @param monitor function pointer for receiving monitor messages
927  * @return compostite context of this request
928  */
929 struct composite_context* libnet_UserList_send(struct libnet_context *ctx,
930                                                TALLOC_CTX *mem_ctx,
931                                                struct libnet_UserList *r,
932                                                void (*monitor)(struct monitor_msg*))
933 {
934         struct composite_context *c;
935         struct userlist_state *s;
936         struct tevent_req *subreq;
937         bool prereq_met = false;
938
939         /* composite context allocation and setup */
940         c = composite_create(mem_ctx, ctx->event_ctx);
941         if (c == NULL) return NULL;
942
943         s = talloc_zero(c, struct userlist_state);
944         if (composite_nomem(s, c)) return c;
945
946         c->private_data = s;
947
948         /* store the arguments in the state structure */
949         s->ctx          = ctx;
950         s->page_size    = r->in.page_size;
951         s->resume_index = r->in.resume_index;
952         s->domain_name  = talloc_strdup(c, r->in.domain_name);
953         s->monitor_fn   = monitor;
954
955         /* make sure we have lsa domain handle before doing anything */
956         prereq_met = lsa_domain_opened(ctx, c, s->domain_name, &c, &s->domain_open,
957                                        continue_lsa_domain_opened, monitor);
958         if (!prereq_met) return c;
959
960         /* prepare arguments of QueryDomainInfo call */
961         s->query_domain.in.handle = &ctx->lsa.handle;
962         s->query_domain.in.level  = LSA_POLICY_INFO_DOMAIN;
963         s->query_domain.out.info  = talloc_zero(c, union lsa_PolicyInformation *);
964         if (composite_nomem(s->query_domain.out.info, c)) return c;
965
966         /* send the request */
967         subreq = dcerpc_lsa_QueryInfoPolicy_r_send(s, c->event_ctx,
968                                                    ctx->lsa.pipe->binding_handle,
969                                                    &s->query_domain);
970         if (composite_nomem(subreq, c)) return c;
971
972         tevent_req_set_callback(subreq, continue_domain_queried, c);
973         return c;
974 }
975
976
977 /*
978  * Stage 0.5 (optional): receive lsa domain handle and send
979  * request to query domain info
980  */
981 static void continue_lsa_domain_opened(struct composite_context *ctx)
982 {
983         struct composite_context *c;
984         struct userlist_state *s;
985         struct tevent_req *subreq;
986         
987         c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
988         s = talloc_get_type_abort(c->private_data, struct userlist_state);
989         
990         /* receive lsa domain handle */
991         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
992         if (!composite_is_ok(c)) return;
993
994         /* prepare arguments of QueryDomainInfo call */
995         s->query_domain.in.handle = &s->ctx->lsa.handle;
996         s->query_domain.in.level  = LSA_POLICY_INFO_DOMAIN;
997         s->query_domain.out.info  = talloc_zero(c, union lsa_PolicyInformation *);
998         if (composite_nomem(s->query_domain.out.info, c)) return;
999
1000         /* send the request */
1001         subreq = dcerpc_lsa_QueryInfoPolicy_r_send(s, c->event_ctx,
1002                                                    s->ctx->lsa.pipe->binding_handle,
1003                                                    &s->query_domain);
1004         if (composite_nomem(subreq, c)) return;
1005
1006         tevent_req_set_callback(subreq, continue_domain_queried, c);
1007 }
1008
1009
1010 /*
1011  * Stage 1: receive domain info and request to enum users,
1012  * provided a valid samr handle is opened
1013  */
1014 static void continue_domain_queried(struct tevent_req *subreq)
1015 {
1016         struct composite_context *c;
1017         struct userlist_state *s;
1018         bool prereq_met = false;
1019         
1020         c = tevent_req_callback_data(subreq, struct composite_context);
1021         s = talloc_get_type_abort(c->private_data, struct userlist_state);
1022
1023         /* receive result of rpc request */
1024         c->status = dcerpc_lsa_QueryInfoPolicy_r_recv(subreq, s);
1025         TALLOC_FREE(subreq);
1026         if (!composite_is_ok(c)) return;
1027
1028         /* get the returned domain info */
1029         s->dominfo = (*s->query_domain.out.info)->domain;
1030
1031         /* make sure we have samr domain handle before continuing */
1032         prereq_met = samr_domain_opened(s->ctx, c, s->domain_name, &c, &s->domain_open,
1033                                         continue_samr_domain_opened, s->monitor_fn);
1034         if (!prereq_met) return;
1035
1036         /* prepare arguments of EnumDomainUsers call */
1037         s->user_list.in.domain_handle = &s->ctx->samr.handle;
1038         s->user_list.in.max_size = s->page_size;
1039         s->user_list.in.resume_handle = &s->resume_index;
1040         s->user_list.in.acct_flags = ACB_NORMAL;
1041         s->user_list.out.resume_handle = &s->resume_index;
1042         s->user_list.out.num_entries = talloc(s, uint32_t);
1043         if (composite_nomem(s->user_list.out.num_entries, c)) return;
1044         s->user_list.out.sam = talloc(s, struct samr_SamArray *);
1045         if (composite_nomem(s->user_list.out.sam, c)) return;
1046
1047         /* send the request */
1048         subreq = dcerpc_samr_EnumDomainUsers_r_send(s, c->event_ctx,
1049                                                     s->ctx->samr.pipe->binding_handle,
1050                                                     &s->user_list);
1051         if (composite_nomem(subreq, c)) return;
1052
1053         tevent_req_set_callback(subreq, continue_users_enumerated, c);
1054 }
1055
1056
1057 /*
1058  * Stage 1.5 (optional): receive samr domain handle
1059  * and request to enumerate accounts
1060  */
1061 static void continue_samr_domain_opened(struct composite_context *ctx)
1062 {
1063         struct composite_context *c;
1064         struct userlist_state *s;
1065         struct tevent_req *subreq;
1066
1067         c = talloc_get_type_abort(ctx->async.private_data, struct composite_context);
1068         s = talloc_get_type_abort(c->private_data, struct userlist_state);
1069
1070         /* receive samr domain handle */
1071         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
1072         if (!composite_is_ok(c)) return;
1073
1074         /* prepare arguments of EnumDomainUsers call */
1075         s->user_list.in.domain_handle = &s->ctx->samr.handle;
1076         s->user_list.in.max_size = s->page_size;
1077         s->user_list.in.resume_handle = &s->resume_index;
1078         s->user_list.in.acct_flags = ACB_NORMAL;
1079         s->user_list.out.resume_handle = &s->resume_index;
1080         s->user_list.out.sam = talloc(s, struct samr_SamArray *);
1081         if (composite_nomem(s->user_list.out.sam, c)) return;
1082         s->user_list.out.num_entries = talloc(s, uint32_t);
1083         if (composite_nomem(s->user_list.out.num_entries, c)) return;
1084         
1085         /* send the request */
1086         subreq = dcerpc_samr_EnumDomainUsers_r_send(s, c->event_ctx,
1087                                                     s->ctx->samr.pipe->binding_handle,
1088                                                     &s->user_list);
1089         if (composite_nomem(subreq, c)) return;
1090
1091         tevent_req_set_callback(subreq, continue_users_enumerated, c);
1092 }
1093
1094
1095 /*
1096  * Stage 2: receive enumerated users and their rids
1097  */
1098 static void continue_users_enumerated(struct tevent_req *subreq)
1099 {
1100         struct composite_context *c;
1101         struct userlist_state *s;
1102         uint32_t i;
1103
1104         c = tevent_req_callback_data(subreq, struct composite_context);
1105         s = talloc_get_type_abort(c->private_data, struct userlist_state);
1106
1107         /* receive result of rpc request */
1108         c->status = dcerpc_samr_EnumDomainUsers_r_recv(subreq, s);
1109         TALLOC_FREE(subreq);
1110         if (!composite_is_ok(c)) return;
1111
1112         /* get the actual status of the rpc call result
1113            (instead of rpc layer status) */
1114         c->status = s->user_list.out.result;
1115
1116         /* we're interested in status "ok" as well as two
1117            enum-specific status codes */
1118         if (NT_STATUS_IS_OK(c->status) ||
1119             NT_STATUS_EQUAL(c->status, STATUS_MORE_ENTRIES) ||
1120             NT_STATUS_EQUAL(c->status, NT_STATUS_NO_MORE_ENTRIES)) {
1121
1122                 /* get enumerated accounts counter and resume handle (the latter allows
1123                    making subsequent call to continue enumeration) */
1124                 s->resume_index = *s->user_list.out.resume_handle;
1125                 s->count        = *s->user_list.out.num_entries;
1126                 
1127                 /* prepare returned user accounts array */
1128                 s->users        = talloc_array(c, struct userlist, (*s->user_list.out.sam)->count);
1129                 if (composite_nomem(s->users, c)) return;
1130
1131                 for (i = 0; i < (*s->user_list.out.sam)->count; i++) {
1132                         struct dom_sid *user_sid;
1133                         struct samr_SamEntry *entry = &(*s->user_list.out.sam)->entries[i];
1134                         struct dom_sid *domain_sid = (*s->query_domain.out.info)->domain.sid;
1135                         
1136                         /* construct user sid from returned rid and queried domain sid */
1137                         user_sid = dom_sid_add_rid(c, domain_sid, entry->idx);
1138                         if (composite_nomem(user_sid, c)) return;
1139                         
1140                         /* username */
1141                         s->users[i].username = talloc_strdup(s->users, entry->name.string);
1142                         if (composite_nomem(s->users[i].username, c)) return;
1143
1144                         /* sid string */
1145                         s->users[i].sid = dom_sid_string(s->users, user_sid);
1146                         if (composite_nomem(s->users[i].sid, c)) return;
1147                 }
1148                 
1149                 /* that's it */
1150                 composite_done(c);
1151                 return;
1152
1153         } else {
1154                 /* something went wrong */
1155                 composite_error(c, c->status);
1156                 return;
1157         }
1158 }
1159
1160
1161 /**
1162  * Receive result of UserList call
1163  *
1164  * @param c composite context returned by send request routine
1165  * @param mem_ctx memory context of this call
1166  * @param r pointer to structure containing arguments and result of this call
1167  * @return nt status
1168  */
1169 NTSTATUS libnet_UserList_recv(struct composite_context* c, TALLOC_CTX *mem_ctx,
1170                               struct libnet_UserList *r)
1171 {
1172         NTSTATUS status;
1173         struct userlist_state *s;
1174
1175         if (c == NULL || mem_ctx == NULL || r == NULL) {
1176                 talloc_free(c);
1177                 return NT_STATUS_INVALID_PARAMETER;
1178         }
1179
1180         status = composite_wait(c);
1181         if (NT_STATUS_IS_OK(status) ||
1182             NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) ||
1183             NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1184                 
1185                 s = talloc_get_type_abort(c->private_data, struct userlist_state);
1186                 
1187                 /* get results from composite context */
1188                 r->out.count = s->count;
1189                 r->out.resume_index = s->resume_index;
1190                 r->out.users = talloc_steal(mem_ctx, s->users);
1191                 
1192                 if (NT_STATUS_IS_OK(status)) {
1193                         r->out.error_string = talloc_strdup(mem_ctx, "Success");
1194                 } else {
1195                         /* success, but we're not done yet */
1196                         r->out.error_string = talloc_asprintf(mem_ctx, "Success (status: %s)",
1197                                                               nt_errstr(status));
1198                 }
1199
1200         } else {
1201                 r->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
1202         }
1203
1204         talloc_free(c);
1205         return status;
1206 }
1207
1208
1209 /**
1210  * Synchronous version of UserList call
1211  *
1212  * @param ctx initialised libnet context
1213  * @param mem_ctx memory context of this call
1214  * @param r pointer to structure containing arguments and result of this call
1215  * @return nt status
1216  */
1217 NTSTATUS libnet_UserList(struct libnet_context *ctx,
1218                          TALLOC_CTX *mem_ctx,
1219                          struct libnet_UserList *r)
1220 {
1221         struct composite_context *c;
1222
1223         c = libnet_UserList_send(ctx, mem_ctx, r, NULL);
1224         return libnet_UserList_recv(c, mem_ctx, r);
1225 }