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