s4-pynet: pynet depends on pyrpc_util
[kai/samba.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, 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->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(ctx->async.private_data, struct composite_context);
112         s = talloc_get_type(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->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(ctx->async.private_data, struct composite_context);
144         s = talloc_get_type(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         struct create_user_state *s;
171
172         r->out.error_string = NULL;
173
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));
179         }
180
181         talloc_free(c);
182         return status;
183 }
184
185
186 /**
187  * Synchronous version of CreateUser call
188  *
189  * @param ctx initialised libnet context
190  * @param mem_ctx memory context of this call
191  * @param r pointer to a structure containing arguments and result of this call
192  * @return nt status
193  */
194 NTSTATUS libnet_CreateUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
195                            struct libnet_CreateUser *r)
196 {
197         struct composite_context *c;
198
199         c = libnet_CreateUser_send(ctx, mem_ctx, r, NULL);
200         return libnet_CreateUser_recv(c, mem_ctx, r);
201 }
202
203
204 struct delete_user_state {
205         struct libnet_DeleteUser r;
206         struct libnet_context *ctx;
207         struct libnet_DomainOpen domain_open;
208         struct libnet_rpc_userdel user_del;
209
210         /* information about the progress */
211         void (*monitor_fn)(struct monitor_msg *);
212 };
213
214
215 static void continue_rpc_userdel(struct composite_context *ctx);
216 static void continue_domain_open_delete(struct composite_context *ctx);
217
218
219 /**
220  * Sends request to delete user account
221  *
222  * @param ctx initialised libnet context
223  * @param mem_ctx memory context of this call
224  * @param r pointer to structure containing arguments and result of this call
225  * @param monitor function pointer for receiving monitor messages
226  */
227 struct composite_context *libnet_DeleteUser_send(struct libnet_context *ctx,
228                                                  TALLOC_CTX *mem_ctx,
229                                                  struct libnet_DeleteUser *r,
230                                                  void (*monitor)(struct monitor_msg*))
231 {
232         struct composite_context *c;
233         struct delete_user_state *s;
234         struct composite_context *delete_req;
235         bool prereq_met = false;
236
237         /* composite context allocation and setup */
238         c = composite_create(mem_ctx, ctx->event_ctx);
239         if (c == NULL) return NULL;
240
241         s = talloc_zero(c, struct delete_user_state);
242         if (composite_nomem(s, c)) return c;
243
244         c->private_data = s;
245
246         /* store arguments in state structure */
247         s->ctx = ctx;
248         s->r   = *r;
249         ZERO_STRUCT(s->r.out);
250         
251         /* prerequisite: make sure the domain is opened before proceeding */
252         prereq_met = samr_domain_opened(ctx, s->r.in.domain_name, &c, &s->domain_open,
253                                         continue_domain_open_delete, monitor);
254         if (!prereq_met) return c;
255
256         /* prepare arguments for userdel call */
257         s->user_del.in.username       = r->in.user_name;
258         s->user_del.in.domain_handle  = ctx->samr.handle;
259
260         /* send request */
261         delete_req = libnet_rpc_userdel_send(ctx->samr.pipe, &s->user_del, monitor);
262         if (composite_nomem(delete_req, c)) return c;
263         
264         /* set the next stage */
265         composite_continue(c, delete_req, continue_rpc_userdel, c);
266         return c;
267 }
268
269
270 /*
271  * Stage 0.5 (optional): receive result of domain open request
272  * and send useradd request
273  */
274 static void continue_domain_open_delete(struct composite_context *ctx)
275 {
276         struct composite_context *c;
277         struct delete_user_state *s;
278         struct composite_context *delete_req;
279         struct monitor_msg msg;
280
281         c = talloc_get_type(ctx->async.private_data, struct composite_context);
282         s = talloc_get_type(c->private_data, struct delete_user_state);
283
284         /* receive result of DomainOpen call */
285         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
286         if (!composite_is_ok(c)) return;
287         
288         /* send monitor message */
289         if (s->monitor_fn) s->monitor_fn(&msg);
290
291         /* prepare arguments for userdel call */
292         s->user_del.in.username       = s->r.in.user_name;
293         s->user_del.in.domain_handle  = s->ctx->samr.handle;
294
295         /* send request */
296         delete_req = libnet_rpc_userdel_send(s->ctx->samr.pipe, &s->user_del, s->monitor_fn);
297         if (composite_nomem(delete_req, c)) return;
298
299         /* set the next stage */
300         composite_continue(c, delete_req, continue_rpc_userdel, c);
301 }
302
303
304 /*
305  * Stage 1: receive result of userdel call and finish the composite function
306  */
307 static void continue_rpc_userdel(struct composite_context *ctx)
308 {
309         struct composite_context *c;
310         struct delete_user_state *s;
311         struct monitor_msg msg;
312
313         c = talloc_get_type(ctx->async.private_data, struct composite_context);
314         s = talloc_get_type(c->private_data, struct delete_user_state);
315
316         /* receive result of userdel call */
317         c->status = libnet_rpc_userdel_recv(ctx, c, &s->user_del);
318         if (!composite_is_ok(c)) return;
319
320         /* send monitor message */
321         if (s->monitor_fn) s->monitor_fn(&msg);
322
323         /* we're done */
324         composite_done(c);
325 }
326
327
328 /**
329  * Receives result of asynchronous DeleteUser call
330  *
331  * @param c composite context returned by async DeleteUser call
332  * @param mem_ctx memory context of this call
333  * @param r pointer to structure containing arguments and result
334  */
335 NTSTATUS libnet_DeleteUser_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
336                                 struct libnet_DeleteUser *r)
337 {
338         NTSTATUS status;
339         struct delete_user_state *s;
340
341         r->out.error_string = NULL;
342
343         /* wait for result of async request and check status code */
344         status = composite_wait(c);
345         if (!NT_STATUS_IS_OK(status)) {
346                 s = talloc_get_type(c->private_data, struct delete_user_state);
347                 r->out.error_string = talloc_steal(mem_ctx, s->r.out.error_string);
348         }
349         
350         talloc_free(c);
351         return status;
352 }
353
354
355 /**
356  * Synchronous version of DeleteUser call
357  *
358  * @param ctx initialised libnet context
359  * @param mem_ctx memory context of this call
360  * @param r pointer to structure containing arguments and result
361  */
362 NTSTATUS libnet_DeleteUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
363                            struct libnet_DeleteUser *r)
364 {
365         struct composite_context *c;
366         
367         c = libnet_DeleteUser_send(ctx, mem_ctx, r, NULL);
368         return libnet_DeleteUser_recv(c, mem_ctx, r);
369 }
370
371
372 struct modify_user_state {
373         struct libnet_ModifyUser r;
374         struct libnet_context *ctx;
375         struct libnet_DomainOpen domain_open;
376         struct libnet_rpc_userinfo user_info;
377         struct libnet_rpc_usermod user_mod;
378
379         void (*monitor_fn)(struct monitor_msg *);
380 };
381
382
383 static void continue_rpc_usermod(struct composite_context *ctx);
384 static void continue_domain_open_modify(struct composite_context *ctx);
385 static NTSTATUS set_user_changes(TALLOC_CTX *mem_ctx, struct usermod_change *mod,
386                                  struct libnet_rpc_userinfo *info, struct libnet_ModifyUser *r);
387 static void continue_rpc_userinfo(struct composite_context *ctx);
388
389
390 /**
391  * Sends request to modify user account
392  *
393  * @param ctx initialised libnet context
394  * @param mem_ctx memory context of this call
395  * @param r pointer to structure containing arguments and result of this call
396  * @param monitor function pointer for receiving monitor messages
397  */
398 struct composite_context *libnet_ModifyUser_send(struct libnet_context *ctx,
399                                                  TALLOC_CTX *mem_ctx,
400                                                  struct libnet_ModifyUser *r,
401                                                  void (*monitor)(struct monitor_msg*))
402 {
403         const uint16_t level = 21;
404         struct composite_context *c;
405         struct modify_user_state *s;
406         struct composite_context *userinfo_req;
407         bool prereq_met = false;
408
409         c = composite_create(mem_ctx, ctx->event_ctx);
410         if (c == NULL) return NULL;
411
412         s = talloc_zero(c, struct modify_user_state);
413         if (composite_nomem(s, c)) return c;
414
415         c->private_data = s;
416
417         s->ctx = ctx;
418         s->r = *r;
419
420         prereq_met = samr_domain_opened(ctx, s->r.in.domain_name, &c, &s->domain_open,
421                                         continue_domain_open_modify, monitor);
422         if (!prereq_met) return c;
423
424         s->user_info.in.username      = r->in.user_name;
425         s->user_info.in.domain_handle = ctx->samr.handle;
426         s->user_info.in.level         = level;
427
428         userinfo_req = libnet_rpc_userinfo_send(ctx->samr.pipe, &s->user_info, monitor);
429         if (composite_nomem(userinfo_req, c)) return c;
430
431         composite_continue(c, userinfo_req, continue_rpc_userinfo, c);
432         return c;
433 }
434
435
436 /*
437  * Stage 0.5 (optional): receive result of domain open request
438  * and send userinfo request
439  */
440 static void continue_domain_open_modify(struct composite_context *ctx)
441 {
442         const uint16_t level = 21;
443         struct composite_context *c;
444         struct modify_user_state *s;
445         struct composite_context *userinfo_req;
446         struct monitor_msg msg;
447
448         c = talloc_get_type(ctx->async.private_data, struct composite_context);
449         s = talloc_get_type(c->private_data, struct modify_user_state);
450
451         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domain_open);
452         if (!composite_is_ok(c)) return;
453
454         if (s->monitor_fn) s->monitor_fn(&msg);
455         
456         s->user_info.in.domain_handle  = s->ctx->samr.handle;
457         s->user_info.in.username       = s->r.in.user_name;
458         s->user_info.in.level          = level;
459
460         userinfo_req = libnet_rpc_userinfo_send(s->ctx->samr.pipe, &s->user_info, s->monitor_fn);
461         if (composite_nomem(userinfo_req, c)) return;
462         
463         composite_continue(c, userinfo_req, continue_rpc_userinfo, c);
464 }
465
466
467 /*
468  * Stage 1: receive result of userinfo call, prepare user changes
469  * (set the fields a caller required to change) and send usermod request
470  */
471 static void continue_rpc_userinfo(struct composite_context *ctx)
472 {
473         struct composite_context *c;
474         struct modify_user_state *s;
475         struct composite_context *usermod_req;
476
477         c = talloc_get_type(ctx->async.private_data, struct composite_context);
478         s = talloc_get_type(c->private_data, struct modify_user_state);
479
480         c->status = libnet_rpc_userinfo_recv(ctx, c, &s->user_info);
481         if (!composite_is_ok(c)) return;
482
483         s->user_mod.in.domain_handle = s->ctx->samr.handle;
484         s->user_mod.in.username      = s->r.in.user_name;
485
486         c->status = set_user_changes(c, &s->user_mod.in.change, &s->user_info, &s->r);
487
488         usermod_req = libnet_rpc_usermod_send(s->ctx->samr.pipe, &s->user_mod, s->monitor_fn);
489         if (composite_nomem(usermod_req, c)) return;
490
491         composite_continue(c, usermod_req, continue_rpc_usermod, c);
492 }
493
494
495 /*
496  * Prepare user changes: compare userinfo result to requested changes and
497  * set the field values and flags accordingly for user modify call
498  */
499 static NTSTATUS set_user_changes(TALLOC_CTX *mem_ctx, struct usermod_change *mod,
500                                  struct libnet_rpc_userinfo *info, struct libnet_ModifyUser *r)
501 {
502         struct samr_UserInfo21 *user;
503
504         if (mod == NULL || info == NULL || r == NULL || info->in.level != 21) {
505                 return NT_STATUS_INVALID_PARAMETER;
506         }
507
508         user = &info->out.info.info21;
509         mod->fields = 0;        /* reset flag field before setting individual flags */
510
511         /* account name change */
512         SET_FIELD_LSA_STRING(r->in, user, mod, account_name, USERMOD_FIELD_ACCOUNT_NAME);
513
514         /* full name change */
515         SET_FIELD_LSA_STRING(r->in, user, mod, full_name, USERMOD_FIELD_FULL_NAME);
516
517         /* description change */
518         SET_FIELD_LSA_STRING(r->in, user, mod, description, USERMOD_FIELD_DESCRIPTION);
519
520         /* comment change */
521         SET_FIELD_LSA_STRING(r->in, user, mod, comment, USERMOD_FIELD_COMMENT);
522
523         /* home directory change */
524         SET_FIELD_LSA_STRING(r->in, user, mod, home_directory, USERMOD_FIELD_HOME_DIRECTORY);
525
526         /* home drive change */
527         SET_FIELD_LSA_STRING(r->in, user, mod, home_drive, USERMOD_FIELD_HOME_DRIVE);
528
529         /* logon script change */
530         SET_FIELD_LSA_STRING(r->in, user, mod, logon_script, USERMOD_FIELD_LOGON_SCRIPT);
531
532         /* profile path change */
533         SET_FIELD_LSA_STRING(r->in, user, mod, profile_path, USERMOD_FIELD_PROFILE_PATH);
534
535         /* account expiry change */
536         SET_FIELD_NTTIME(r->in, user, mod, acct_expiry, USERMOD_FIELD_ACCT_EXPIRY);
537
538         /* account flags change */
539         SET_FIELD_ACCT_FLAGS(r->in, user, mod, acct_flags, USERMOD_FIELD_ACCT_FLAGS);
540
541         return NT_STATUS_OK;
542 }
543
544
545 /*
546  * Stage 2: receive result of usermod request and finish the composite function
547  */
548 static void continue_rpc_usermod(struct composite_context *ctx)
549 {
550         struct composite_context *c;
551         struct modify_user_state *s;
552         struct monitor_msg msg;
553
554         c = talloc_get_type(ctx->async.private_data, struct composite_context);
555         s = talloc_get_type(c->private_data, struct modify_user_state);
556         
557         c->status = libnet_rpc_usermod_recv(ctx, c, &s->user_mod);
558         if (!composite_is_ok(c)) return;
559         
560         if (s->monitor_fn) s->monitor_fn(&msg);
561         composite_done(c);
562 }
563
564
565 /**
566  * Receive result of ModifyUser call
567  *
568  * @param c composite context returned by send request routine
569  * @param mem_ctx memory context of this call
570  * @param r pointer to a structure containing arguments and result of this call
571  * @return nt status
572  */
573 NTSTATUS libnet_ModifyUser_recv(struct composite_context *c, TALLOC_CTX *mem_ctx,
574                                 struct libnet_ModifyUser *r)
575 {
576         NTSTATUS status = composite_wait(c);
577
578         talloc_free(c);
579         return status;
580 }
581
582
583 /**
584  * Synchronous version of ModifyUser call
585  *
586  * @param ctx initialised libnet context
587  * @param mem_ctx memory context of this call
588  * @param r pointer to a structure containing arguments and result of this call
589  * @return nt status
590  */
591 NTSTATUS libnet_ModifyUser(struct libnet_context *ctx, TALLOC_CTX *mem_ctx,
592                            struct libnet_ModifyUser *r)
593 {
594         struct composite_context *c;
595
596         c = libnet_ModifyUser_send(ctx, mem_ctx, r, NULL);
597         return libnet_ModifyUser_recv(c, mem_ctx, r);
598 }
599
600
601 struct user_info_state {
602         struct libnet_context *ctx;
603         const char *domain_name;
604         enum libnet_UserInfo_level level;
605         const char *user_name;
606         const char *sid_string;
607         struct libnet_LookupName lookup;
608         struct libnet_DomainOpen domopen;
609         struct libnet_rpc_userinfo userinfo;
610
611         /* information about the progress */
612         void (*monitor_fn)(struct monitor_msg *);
613 };
614
615
616 static void continue_name_found(struct composite_context *ctx);
617 static void continue_domain_open_info(struct composite_context *ctx);
618 static void continue_info_received(struct composite_context *ctx);
619
620
621 /**
622  * Sends request to get user account information
623  *
624  * @param ctx initialised libnet context
625  * @param mem_ctx memory context of this call
626  * @param r pointer to a structure containing arguments and results of this call
627  * @param monitor function pointer for receiving monitor messages
628  * @return compostite context of this request
629  */
630 struct composite_context* libnet_UserInfo_send(struct libnet_context *ctx,
631                                                TALLOC_CTX *mem_ctx,
632                                                struct libnet_UserInfo *r,
633                                                void (*monitor)(struct monitor_msg*))
634 {
635         struct composite_context *c;
636         struct user_info_state *s;
637         struct composite_context *lookup_req, *info_req;
638         bool prereq_met = false;
639
640         /* composite context allocation and setup */
641         c = composite_create(mem_ctx, ctx->event_ctx);
642         if (c == NULL) return NULL;
643
644         s = talloc_zero(c, struct user_info_state);
645         if (composite_nomem(s, c)) return c;
646
647         c->private_data = s;
648
649         /* store arguments in the state structure */
650         s->monitor_fn = monitor;
651         s->ctx = ctx;
652         s->domain_name = talloc_strdup(c, r->in.domain_name);
653         s->level = r->in.level;
654         switch (s->level) {
655         case USER_INFO_BY_NAME:
656                 s->user_name = talloc_strdup(c, r->in.data.user_name);
657                 s->sid_string = NULL;
658                 break;
659         case USER_INFO_BY_SID:
660                 s->user_name = NULL;
661                 s->sid_string = dom_sid_string(c, r->in.data.user_sid);
662                 break;
663         }
664
665         /* prerequisite: make sure the domain is opened */
666         prereq_met = samr_domain_opened(ctx, s->domain_name, &c, &s->domopen,
667                                         continue_domain_open_info, monitor);
668         if (!prereq_met) return c;
669
670         switch (s->level) {
671         case USER_INFO_BY_NAME:
672                 /* prepare arguments for LookupName call */
673                 s->lookup.in.domain_name = s->domain_name;
674                 s->lookup.in.name        = s->user_name;
675
676                 /* send the request */
677                 lookup_req = libnet_LookupName_send(ctx, c, &s->lookup,
678                                                     s->monitor_fn);
679                 if (composite_nomem(lookup_req, c)) return c;
680
681                 /* set the next stage */
682                 composite_continue(c, lookup_req, continue_name_found, c);
683                 break;
684         case USER_INFO_BY_SID:
685                 /* prepare arguments for UserInfo call */
686                 s->userinfo.in.domain_handle = s->ctx->samr.handle;
687                 s->userinfo.in.sid = s->sid_string;
688                 s->userinfo.in.level = 21;
689
690                 /* send the request */
691                 info_req = libnet_rpc_userinfo_send(s->ctx->samr.pipe,
692                                                     &s->userinfo,
693                                                     s->monitor_fn);
694                 if (composite_nomem(info_req, c)) return c;
695
696                 /* set the next stage */
697                 composite_continue(c, info_req, continue_info_received, c);
698                 break;
699         }
700
701         return c;
702 }
703
704
705 /*
706  * Stage 0.5 (optional): receive result of domain open request
707  * and send LookupName request
708  */
709 static void continue_domain_open_info(struct composite_context *ctx)
710 {
711         struct composite_context *c;
712         struct user_info_state *s;
713         struct composite_context *lookup_req, *info_req;
714         struct monitor_msg msg;
715
716         c = talloc_get_type(ctx->async.private_data, struct composite_context);
717         s = talloc_get_type(c->private_data, struct user_info_state);
718
719         /* receive result of DomainOpen call */
720         c->status = libnet_DomainOpen_recv(ctx, s->ctx, c, &s->domopen);
721         if (!composite_is_ok(c)) return;
722
723         /* send monitor message */
724         if (s->monitor_fn) s->monitor_fn(&msg);
725
726         switch (s->level) {
727         case USER_INFO_BY_NAME:
728                 /* prepare arguments for LookupName call */
729                 s->lookup.in.domain_name = s->domain_name;
730                 s->lookup.in.name        = s->user_name;
731
732                 /* send the request */
733                 lookup_req = libnet_LookupName_send(s->ctx, c, &s->lookup, s->monitor_fn);
734                 if (composite_nomem(lookup_req, c)) return;
735
736                 /* set the next stage */
737                 composite_continue(c, lookup_req, continue_name_found, c);
738                 break;
739
740         case USER_INFO_BY_SID:
741                 /* prepare arguments for UserInfo call */
742                 s->userinfo.in.domain_handle = s->ctx->samr.handle;
743                 s->userinfo.in.sid = s->sid_string;
744                 s->userinfo.in.level = 21;
745
746                 /* send the request */
747                 info_req = libnet_rpc_userinfo_send(s->ctx->samr.pipe,
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(ctx->async.private_data, struct composite_context);
769         s = talloc_get_type(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->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(ctx->async.private_data, struct composite_context);
804         s = talloc_get_type(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(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, 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(ctx->async.private_data, struct composite_context);
988         s = talloc_get_type(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(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, 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(ctx->async.private_data, struct composite_context);
1068         s = talloc_get_type(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(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
1152         } else {
1153                 /* something went wrong */
1154                 composite_error(c, c->status);
1155         }
1156 }
1157
1158
1159 /**
1160  * Receive result of UserList call
1161  *
1162  * @param c composite context returned by send request routine
1163  * @param mem_ctx memory context of this call
1164  * @param r pointer to structure containing arguments and result of this call
1165  * @return nt status
1166  */
1167 NTSTATUS libnet_UserList_recv(struct composite_context* c, TALLOC_CTX *mem_ctx,
1168                               struct libnet_UserList *r)
1169 {
1170         NTSTATUS status;
1171         struct userlist_state *s;
1172
1173         if (c == NULL || mem_ctx == NULL || r == NULL) {
1174                 talloc_free(c);
1175                 return NT_STATUS_INVALID_PARAMETER;
1176         }
1177
1178         status = composite_wait(c);
1179         if (NT_STATUS_IS_OK(status) ||
1180             NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES) ||
1181             NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
1182                 
1183                 s = talloc_get_type(c->private_data, struct userlist_state);
1184                 
1185                 /* get results from composite context */
1186                 r->out.count = s->count;
1187                 r->out.resume_index = s->resume_index;
1188                 r->out.users = talloc_steal(mem_ctx, s->users);
1189                 
1190                 if (NT_STATUS_IS_OK(status)) {
1191                         r->out.error_string = talloc_strdup(mem_ctx, "Success");
1192                 } else {
1193                         /* success, but we're not done yet */
1194                         r->out.error_string = talloc_asprintf(mem_ctx, "Success (status: %s)",
1195                                                               nt_errstr(status));
1196                 }
1197
1198         } else {
1199                 r->out.error_string = talloc_asprintf(mem_ctx, "Error: %s", nt_errstr(status));
1200         }
1201
1202         talloc_free(c);
1203         return status;
1204 }
1205
1206
1207 /**
1208  * Synchronous version of UserList call
1209  *
1210  * @param ctx initialised libnet context
1211  * @param mem_ctx memory context of this call
1212  * @param r pointer to structure containing arguments and result of this call
1213  * @return nt status
1214  */
1215 NTSTATUS libnet_UserList(struct libnet_context *ctx,
1216                          TALLOC_CTX *mem_ctx,
1217                          struct libnet_UserList *r)
1218 {
1219         struct composite_context *c;
1220
1221         c = libnet_UserList_send(ctx, mem_ctx, r, NULL);
1222         return libnet_UserList_recv(c, mem_ctx, r);
1223 }