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
|
/*
* Unix SMB/CIFS implementation.
* NetApi User Support
* Copyright (C) Guenther Deschner 2008
*
* 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 "librpc/gen_ndr/libnetapi.h"
#include "lib/netapi/netapi.h"
#include "lib/netapi/netapi_private.h"
#include "lib/netapi/libnetapi.h"
/****************************************************************
****************************************************************/
static void convert_USER_INFO_1_to_samr_user_info25(struct USER_INFO_1 *info1,
DATA_BLOB *user_session_key,
struct samr_UserInfo25 *info25)
{
uint32_t fields_present = SAMR_FIELD_ACCT_FLAGS;
struct samr_LogonHours zero_logon_hours;
struct lsa_BinaryString zero_parameters;
uint32_t acct_flags = 0;
NTTIME password_age;
ZERO_STRUCTP(info25);
ZERO_STRUCT(zero_logon_hours);
ZERO_STRUCT(zero_parameters);
if (info1->usri1_name) {
fields_present |= SAMR_FIELD_FULL_NAME;
}
if (info1->usri1_password) {
fields_present |= SAMR_FIELD_PASSWORD;
}
if (info1->usri1_flags) {
fields_present |= SAMR_FIELD_ACCT_FLAGS;
}
if (info1->usri1_name) {
fields_present |= SAMR_FIELD_FULL_NAME;
}
if (info1->usri1_home_dir) {
fields_present |= SAMR_FIELD_HOME_DIRECTORY;
}
if (info1->usri1_script_path) {
fields_present |= SAMR_FIELD_LOGON_SCRIPT;
}
if (info1->usri1_comment) {
fields_present |= SAMR_FIELD_DESCRIPTION;
}
if (info1->usri1_password_age) {
fields_present |= SAMR_FIELD_FORCE_PWD_CHANGE;
}
acct_flags |= info1->usri1_flags | ACB_NORMAL;
unix_to_nt_time_abs(&password_age, info1->usri1_password_age);
/* TODO: info1->usri1_priv */
init_samr_user_info21(&info25->info,
0,
0,
0,
0,
0,
password_age,
NULL,
info1->usri1_name,
info1->usri1_home_dir,
NULL,
info1->usri1_script_path,
NULL,
info1->usri1_comment,
NULL,
NULL,
&zero_parameters,
0,
0,
acct_flags,
fields_present,
zero_logon_hours,
0,
0,
0,
0,
0,
0,
0);
if (info1->usri1_password) {
uchar pwbuf[532];
struct MD5Context ctx;
uint8_t confounder[16];
DATA_BLOB confounded_session_key = data_blob(NULL, 16);
encode_pw_buffer(pwbuf, info1->usri1_password, STR_UNICODE);
generate_random_buffer((uint8_t *)confounder, 16);
MD5Init(&ctx);
MD5Update(&ctx, confounder, 16);
MD5Update(&ctx, user_session_key->data,
user_session_key->length);
MD5Final(confounded_session_key.data, &ctx);
SamOEMhashBlob(pwbuf, 516, &confounded_session_key);
memcpy(&pwbuf[516], confounder, 16);
memcpy(info25->password.data, pwbuf, sizeof(pwbuf));
data_blob_free(&confounded_session_key);
}
}
/****************************************************************
****************************************************************/
WERROR NetUserAdd_r(struct libnetapi_ctx *ctx,
struct NetUserAdd *r)
{
struct cli_state *cli = NULL;
struct rpc_pipe_client *pipe_cli = NULL;
NTSTATUS status;
WERROR werr;
POLICY_HND connect_handle, domain_handle, user_handle;
struct lsa_String lsa_account_name;
struct dom_sid2 *domain_sid = NULL;
struct samr_UserInfo25 info25;
union samr_UserInfo *user_info = NULL;
struct samr_PwInfo pw_info;
uint32_t access_granted = 0;
uint32_t rid = 0;
struct USER_INFO_1 *info1;
ZERO_STRUCT(connect_handle);
ZERO_STRUCT(domain_handle);
ZERO_STRUCT(user_handle);
if (!r->in.buffer) {
return WERR_INVALID_PARAM;
}
switch (r->in.level) {
case 1:
info1 = (struct USER_INFO_1 *)r->in.buffer;
break;
case 2:
case 3:
case 4:
default:
werr = WERR_NOT_SUPPORTED;
goto done;
}
werr = libnetapi_open_ipc_connection(ctx, r->in.server_name, &cli);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
werr = libnetapi_open_pipe(ctx, cli, PI_SAMR, &pipe_cli);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
werr = libnetapi_samr_open_domain(ctx, pipe_cli,
SAMR_ACCESS_ENUM_DOMAINS |
SAMR_ACCESS_OPEN_DOMAIN,
SAMR_DOMAIN_ACCESS_LOOKUP_INFO_1 |
SAMR_DOMAIN_ACCESS_CREATE_USER |
SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
&connect_handle,
&domain_handle,
&domain_sid);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
init_lsa_String(&lsa_account_name, info1->usri1_name);
status = rpccli_samr_CreateUser2(pipe_cli, ctx,
&domain_handle,
&lsa_account_name,
ACB_NORMAL,
SEC_STD_WRITE_DAC |
SEC_STD_DELETE |
SAMR_USER_ACCESS_SET_PASSWORD |
SAMR_USER_ACCESS_SET_ATTRIBUTES |
SAMR_USER_ACCESS_GET_ATTRIBUTES,
&user_handle,
&access_granted,
&rid);
if (!NT_STATUS_IS_OK(status)) {
werr = ntstatus_to_werror(status);
goto done;
}
status = rpccli_samr_QueryUserInfo(pipe_cli, ctx,
&user_handle,
16,
&user_info);
if (!NT_STATUS_IS_OK(status)) {
werr = ntstatus_to_werror(status);
goto done;
}
if (!(user_info->info16.acct_flags & ACB_NORMAL)) {
werr = WERR_INVALID_PARAM;
goto done;
}
status = rpccli_samr_GetUserPwInfo(pipe_cli, ctx,
&user_handle,
&pw_info);
if (!NT_STATUS_IS_OK(status)) {
werr = ntstatus_to_werror(status);
goto done;
}
ZERO_STRUCTP(user_info);
convert_USER_INFO_1_to_samr_user_info25(info1,
&cli->user_session_key,
&info25);
if (info1->usri1_password) {
user_info->info25 = info25;
status = rpccli_samr_SetUserInfo2(pipe_cli, ctx,
&user_handle,
25,
user_info);
if (NT_STATUS_EQUAL(status, NT_STATUS(DCERPC_FAULT_INVALID_TAG))) {
user_info->info23.info = info25.info;
encode_pw_buffer(user_info->info23.password.data,
info1->usri1_password, STR_UNICODE);
SamOEMhashBlob(user_info->info23.password.data, 516,
&cli->user_session_key);
status = rpccli_samr_SetUserInfo2(pipe_cli, ctx,
&user_handle,
23,
user_info);
}
} else {
user_info->info21 = info25.info;
status = rpccli_samr_SetUserInfo(pipe_cli, ctx,
&user_handle,
21,
user_info);
}
if (!NT_STATUS_IS_OK(status)) {
werr = ntstatus_to_werror(status);
goto failed;
}
werr = WERR_OK;
goto done;
failed:
rpccli_samr_DeleteUser(pipe_cli, ctx,
&user_handle);
done:
if (!cli) {
return werr;
}
if (is_valid_policy_hnd(&user_handle)) {
rpccli_samr_Close(pipe_cli, ctx, &user_handle);
}
if (is_valid_policy_hnd(&domain_handle)) {
rpccli_samr_Close(pipe_cli, ctx, &domain_handle);
}
if (is_valid_policy_hnd(&connect_handle)) {
rpccli_samr_Close(pipe_cli, ctx, &connect_handle);
}
return werr;
}
/****************************************************************
****************************************************************/
WERROR NetUserAdd_l(struct libnetapi_ctx *ctx,
struct NetUserAdd *r)
{
/* for now just talk to local RPC server */
if (!r->in.server_name) {
r->in.server_name = "localhost";
}
return NetUserAdd_r(ctx, r);
}
/****************************************************************
****************************************************************/
WERROR NetUserDel_r(struct libnetapi_ctx *ctx,
struct NetUserDel *r)
{
struct cli_state *cli = NULL;
struct rpc_pipe_client *pipe_cli = NULL;
NTSTATUS status;
WERROR werr;
POLICY_HND connect_handle, builtin_handle, domain_handle, user_handle;
struct lsa_String lsa_account_name;
struct samr_Ids user_rids, name_types;
struct dom_sid2 *domain_sid = NULL;
struct dom_sid2 user_sid;
ZERO_STRUCT(connect_handle);
ZERO_STRUCT(builtin_handle);
ZERO_STRUCT(domain_handle);
ZERO_STRUCT(user_handle);
werr = libnetapi_open_ipc_connection(ctx, r->in.server_name, &cli);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
werr = libnetapi_open_pipe(ctx, cli, PI_SAMR, &pipe_cli);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
werr = libnetapi_samr_open_domain(ctx, pipe_cli,
SAMR_ACCESS_ENUM_DOMAINS |
SAMR_ACCESS_OPEN_DOMAIN,
SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
&connect_handle,
&domain_handle,
&domain_sid);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
status = rpccli_samr_OpenDomain(pipe_cli, ctx,
&connect_handle,
SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
CONST_DISCARD(DOM_SID *, &global_sid_Builtin),
&builtin_handle);
if (!NT_STATUS_IS_OK(status)) {
werr = ntstatus_to_werror(status);
goto done;
}
init_lsa_String(&lsa_account_name, r->in.user_name);
status = rpccli_samr_LookupNames(pipe_cli, ctx,
&domain_handle,
1,
&lsa_account_name,
&user_rids,
&name_types);
if (!NT_STATUS_IS_OK(status)) {
werr = ntstatus_to_werror(status);
goto done;
}
status = rpccli_samr_OpenUser(pipe_cli, ctx,
&domain_handle,
STD_RIGHT_DELETE_ACCESS,
user_rids.ids[0],
&user_handle);
if (!NT_STATUS_IS_OK(status)) {
werr = ntstatus_to_werror(status);
goto done;
}
sid_compose(&user_sid, domain_sid, user_rids.ids[0]);
status = rpccli_samr_RemoveMemberFromForeignDomain(pipe_cli, ctx,
&builtin_handle,
&user_sid);
if (!NT_STATUS_IS_OK(status)) {
werr = ntstatus_to_werror(status);
goto done;
}
status = rpccli_samr_DeleteUser(pipe_cli, ctx,
&user_handle);
if (!NT_STATUS_IS_OK(status)) {
werr = ntstatus_to_werror(status);
goto done;
}
werr = WERR_OK;
done:
if (!cli) {
return werr;
}
if (is_valid_policy_hnd(&user_handle)) {
rpccli_samr_Close(pipe_cli, ctx, &user_handle);
}
if (is_valid_policy_hnd(&builtin_handle)) {
rpccli_samr_Close(pipe_cli, ctx, &builtin_handle);
}
if (is_valid_policy_hnd(&domain_handle)) {
rpccli_samr_Close(pipe_cli, ctx, &domain_handle);
}
if (is_valid_policy_hnd(&connect_handle)) {
rpccli_samr_Close(pipe_cli, ctx, &connect_handle);
}
return werr;
}
/****************************************************************
****************************************************************/
WERROR NetUserDel_l(struct libnetapi_ctx *ctx,
struct NetUserDel *r)
{
/* for now just talk to local RPC server */
if (!r->in.server_name) {
r->in.server_name = "localhost";
}
return NetUserDel_r(ctx, r);
}
/****************************************************************
****************************************************************/
static NTSTATUS libnetapi_samr_lookup_user(TALLOC_CTX *mem_ctx,
struct rpc_pipe_client *pipe_cli,
struct policy_handle *domain_handle,
struct policy_handle *builtin_handle,
const char *user_name,
uint32_t rid,
uint32_t level,
struct samr_UserInfo21 **info21,
struct sec_desc_buf **sec_desc)
{
NTSTATUS status;
struct policy_handle user_handle;
union samr_UserInfo *user_info = NULL;
struct samr_RidWithAttributeArray *rid_array = NULL;
uint32_t access_mask = SEC_STD_READ_CONTROL |
SAMR_USER_ACCESS_GET_ATTRIBUTES |
SAMR_USER_ACCESS_GET_NAME_ETC;
ZERO_STRUCT(user_handle);
switch (level) {
case 0:
case 1:
case 2:
case 3:
case 10:
case 11:
case 20:
case 23:
break;
default:
return NT_STATUS_INVALID_LEVEL;
}
if (level == 0) {
return NT_STATUS_OK;
}
status = rpccli_samr_OpenUser(pipe_cli, mem_ctx,
domain_handle,
access_mask,
rid,
&user_handle);
if (!NT_STATUS_IS_OK(status)) {
goto done;
}
status = rpccli_samr_QueryUserInfo(pipe_cli, mem_ctx,
&user_handle,
21,
&user_info);
if (!NT_STATUS_IS_OK(status)) {
goto done;
}
status = rpccli_samr_QuerySecurity(pipe_cli, mem_ctx,
&user_handle,
SECINFO_DACL,
sec_desc);
if (!NT_STATUS_IS_OK(status)) {
goto done;
}
if (level == 1) {
status = rpccli_samr_GetGroupsForUser(pipe_cli, mem_ctx,
&user_handle,
&rid_array);
if (!NT_STATUS_IS_OK(status)) {
goto done;
}
#if 0
status = rpccli_samr_GetAliasMembership(pipe_cli, ctx,
&builtin_handle,
&sids,
&rids);
if (!NT_STATUS_IS_OK(status)) {
goto done;
}
#endif
}
*info21 = &user_info->info21;
done:
if (is_valid_policy_hnd(&user_handle)) {
rpccli_samr_Close(pipe_cli, mem_ctx, &user_handle);
}
return status;
}
/****************************************************************
****************************************************************/
static NTSTATUS libnetapi_samr_lookup_user_map_USER_INFO(TALLOC_CTX *mem_ctx,
struct rpc_pipe_client *pipe_cli,
struct dom_sid *domain_sid,
struct policy_handle *domain_handle,
struct policy_handle *builtin_handle,
const char *user_name,
uint32_t rid,
uint32_t level,
uint8_t **buffer,
uint32_t *num_entries)
{
NTSTATUS status;
struct samr_UserInfo21 *info21 = NULL;
struct sec_desc_buf *sec_desc = NULL;
struct dom_sid sid;
struct USER_INFO_0 *info0 = NULL;
struct USER_INFO_10 *info10 = NULL;
struct USER_INFO_20 *info20 = NULL;
struct USER_INFO_23 *info23 = NULL;
switch (level) {
case 0:
case 1:
case 2:
case 3:
case 10:
case 11:
case 20:
case 23:
break;
default:
return NT_STATUS_INVALID_LEVEL;
}
if (level == 0) {
info0 = TALLOC_P(mem_ctx, struct USER_INFO_0);
NT_STATUS_HAVE_NO_MEMORY(info0);
info0->usri0_name = talloc_strdup(mem_ctx, user_name);
NT_STATUS_HAVE_NO_MEMORY(info0->usri0_name);
ADD_TO_ARRAY(mem_ctx, struct USER_INFO_0, *info0,
(struct USER_INFO_0 **)buffer, num_entries);
return NT_STATUS_OK;
}
status = libnetapi_samr_lookup_user(mem_ctx, pipe_cli,
domain_handle,
builtin_handle,
user_name,
rid,
level,
&info21,
&sec_desc);
if (!NT_STATUS_IS_OK(status)) {
goto done;
}
switch (level) {
case 10:
info10 = TALLOC_P(mem_ctx, struct USER_INFO_10);
NT_STATUS_HAVE_NO_MEMORY(info10);
info10->usri10_name = talloc_strdup(mem_ctx, user_name);
NT_STATUS_HAVE_NO_MEMORY(info10->usri10_name);
info10->usri10_comment = talloc_strdup(mem_ctx,
info21->description.string);
info10->usri10_full_name = talloc_strdup(mem_ctx,
info21->full_name.string);
info10->usri10_usr_comment = talloc_strdup(mem_ctx,
info21->comment.string);
ADD_TO_ARRAY(mem_ctx, struct USER_INFO_10, *info10,
(struct USER_INFO_10 **)buffer, num_entries);
break;
case 20:
info20 = TALLOC_P(mem_ctx, struct USER_INFO_20);
NT_STATUS_HAVE_NO_MEMORY(info20);
info20->usri20_name = talloc_strdup(mem_ctx, user_name);
NT_STATUS_HAVE_NO_MEMORY(info20->usri20_name);
info20->usri20_comment = talloc_strdup(mem_ctx,
info21->description.string);
info20->usri20_flags = info21->acct_flags;
info20->usri20_user_id = rid;
ADD_TO_ARRAY(mem_ctx, struct USER_INFO_20, *info20,
(struct USER_INFO_20 **)buffer, num_entries);
break;
case 23:
info23 = TALLOC_P(mem_ctx, struct USER_INFO_23);
NT_STATUS_HAVE_NO_MEMORY(info23);
info23->usri23_name = talloc_strdup(mem_ctx, user_name);
NT_STATUS_HAVE_NO_MEMORY(info23->usri23_name);
info23->usri23_comment = talloc_strdup(mem_ctx,
info21->description.string);
info23->usri23_flags = info21->acct_flags;
if (!sid_compose(&sid, domain_sid, rid)) {
return NT_STATUS_NO_MEMORY;
}
info23->usri23_user_sid =
(struct domsid *)sid_dup_talloc(mem_ctx, &sid);
ADD_TO_ARRAY(mem_ctx, struct USER_INFO_23, *info23,
(struct USER_INFO_23 **)buffer, num_entries);
break;
}
done:
return status;
}
/****************************************************************
****************************************************************/
WERROR NetUserEnum_r(struct libnetapi_ctx *ctx,
struct NetUserEnum *r)
{
struct cli_state *cli = NULL;
struct rpc_pipe_client *pipe_cli = NULL;
struct policy_handle connect_handle;
struct dom_sid2 *domain_sid = NULL;
struct policy_handle domain_handle;
struct samr_SamArray *sam = NULL;
uint32_t filter = ACB_NORMAL;
int i;
uint32_t entries_read = 0;
NTSTATUS status = NT_STATUS_OK;
WERROR werr;
ZERO_STRUCT(connect_handle);
ZERO_STRUCT(domain_handle);
if (!r->out.buffer) {
return WERR_INVALID_PARAM;
}
*r->out.buffer = NULL;
*r->out.entries_read = 0;
switch (r->in.level) {
case 0:
case 10:
case 20:
case 23:
break;
case 1:
case 2:
case 3:
case 11:
default:
return WERR_NOT_SUPPORTED;
}
werr = libnetapi_open_ipc_connection(ctx, r->in.server_name, &cli);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
werr = libnetapi_open_pipe(ctx, cli, PI_SAMR, &pipe_cli);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
werr = libnetapi_samr_open_domain(ctx, pipe_cli,
SAMR_ACCESS_ENUM_DOMAINS |
SAMR_ACCESS_OPEN_DOMAIN,
SAMR_DOMAIN_ACCESS_LOOKUP_INFO_2 |
SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS |
SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
&connect_handle,
&domain_handle,
&domain_sid);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
switch (r->in.filter) {
case FILTER_NORMAL_ACCOUNT:
filter = ACB_NORMAL;
break;
case FILTER_TEMP_DUPLICATE_ACCOUNT:
filter = ACB_TEMPDUP;
break;
case FILTER_INTERDOMAIN_TRUST_ACCOUNT:
filter = ACB_DOMTRUST;
break;
case FILTER_WORKSTATION_TRUST_ACCOUNT:
filter = ACB_WSTRUST;
break;
case FILTER_SERVER_TRUST_ACCOUNT:
filter = ACB_SVRTRUST;
break;
default:
break;
}
status = rpccli_samr_EnumDomainUsers(pipe_cli,
ctx,
&domain_handle,
r->in.resume_handle,
filter,
&sam,
r->in.prefmaxlen,
&entries_read);
werr = ntstatus_to_werror(status);
if (NT_STATUS_IS_ERR(status)) {
goto done;
}
for (i=0; i < sam->count; i++) {
status = libnetapi_samr_lookup_user_map_USER_INFO(ctx, pipe_cli,
domain_sid,
&domain_handle,
NULL, /*&builtin_handle, */
sam->entries[i].name.string,
sam->entries[i].idx,
r->in.level,
r->out.buffer,
r->out.entries_read);
if (!NT_STATUS_IS_OK(status)) {
werr = ntstatus_to_werror(status);
goto done;
}
}
done:
if (!cli) {
return werr;
}
if (is_valid_policy_hnd(&domain_handle)) {
rpccli_samr_Close(pipe_cli, ctx, &domain_handle);
}
if (is_valid_policy_hnd(&connect_handle)) {
rpccli_samr_Close(pipe_cli, ctx, &connect_handle);
}
return werr;
}
/****************************************************************
****************************************************************/
WERROR NetUserEnum_l(struct libnetapi_ctx *ctx,
struct NetUserEnum *r)
{
return WERR_NOT_SUPPORTED;
}
/****************************************************************
****************************************************************/
static WERROR convert_samr_dispinfo_to_NET_DISPLAY_USER(TALLOC_CTX *mem_ctx,
struct samr_DispInfoGeneral *info,
uint32_t *entries_read,
void **buffer)
{
struct NET_DISPLAY_USER *user = NULL;
int i;
user = TALLOC_ZERO_ARRAY(mem_ctx,
struct NET_DISPLAY_USER,
info->count);
W_ERROR_HAVE_NO_MEMORY(user);
for (i = 0; i < info->count; i++) {
user[i].usri1_name = talloc_strdup(mem_ctx,
info->entries[i].account_name.string);
user[i].usri1_comment = talloc_strdup(mem_ctx,
info->entries[i].description.string);
user[i].usri1_flags =
info->entries[i].acct_flags;
user[i].usri1_full_name = talloc_strdup(mem_ctx,
info->entries[i].full_name.string);
user[i].usri1_user_id =
info->entries[i].rid;
user[i].usri1_next_index =
info->entries[i].idx;
if (!user[i].usri1_name) {
return WERR_NOMEM;
}
}
*buffer = talloc_memdup(mem_ctx, user,
sizeof(struct NET_DISPLAY_USER) * info->count);
W_ERROR_HAVE_NO_MEMORY(*buffer);
*entries_read = info->count;
return WERR_OK;
}
/****************************************************************
****************************************************************/
static WERROR convert_samr_dispinfo_to_NET_DISPLAY_MACHINE(TALLOC_CTX *mem_ctx,
struct samr_DispInfoFull *info,
uint32_t *entries_read,
void **buffer)
{
struct NET_DISPLAY_MACHINE *machine = NULL;
int i;
machine = TALLOC_ZERO_ARRAY(mem_ctx,
struct NET_DISPLAY_MACHINE,
info->count);
W_ERROR_HAVE_NO_MEMORY(machine);
for (i = 0; i < info->count; i++) {
machine[i].usri2_name = talloc_strdup(mem_ctx,
info->entries[i].account_name.string);
machine[i].usri2_comment = talloc_strdup(mem_ctx,
info->entries[i].description.string);
machine[i].usri2_flags =
info->entries[i].acct_flags;
machine[i].usri2_user_id =
info->entries[i].rid;
machine[i].usri2_next_index =
info->entries[i].idx;
if (!machine[i].usri2_name) {
return WERR_NOMEM;
}
}
*buffer = talloc_memdup(mem_ctx, machine,
sizeof(struct NET_DISPLAY_MACHINE) * info->count);
W_ERROR_HAVE_NO_MEMORY(*buffer);
*entries_read = info->count;
return WERR_OK;
}
/****************************************************************
****************************************************************/
static WERROR convert_samr_dispinfo_to_NET_DISPLAY_GROUP(TALLOC_CTX *mem_ctx,
struct samr_DispInfoFullGroups *info,
uint32_t *entries_read,
void **buffer)
{
struct NET_DISPLAY_GROUP *group = NULL;
int i;
group = TALLOC_ZERO_ARRAY(mem_ctx,
struct NET_DISPLAY_GROUP,
info->count);
W_ERROR_HAVE_NO_MEMORY(group);
for (i = 0; i < info->count; i++) {
group[i].grpi3_name = talloc_strdup(mem_ctx,
info->entries[i].account_name.string);
group[i].grpi3_comment = talloc_strdup(mem_ctx,
info->entries[i].description.string);
group[i].grpi3_group_id =
info->entries[i].rid;
group[i].grpi3_attributes =
info->entries[i].acct_flags;
group[i].grpi3_next_index =
info->entries[i].idx;
if (!group[i].grpi3_name) {
return WERR_NOMEM;
}
}
*buffer = talloc_memdup(mem_ctx, group,
sizeof(struct NET_DISPLAY_GROUP) * info->count);
W_ERROR_HAVE_NO_MEMORY(*buffer);
*entries_read = info->count;
return WERR_OK;
}
/****************************************************************
****************************************************************/
static WERROR convert_samr_dispinfo_to_NET_DISPLAY(TALLOC_CTX *mem_ctx,
union samr_DispInfo *info,
uint32_t level,
uint32_t *entries_read,
void **buffer)
{
switch (level) {
case 1:
return convert_samr_dispinfo_to_NET_DISPLAY_USER(mem_ctx,
&info->info1,
entries_read,
buffer);
case 2:
return convert_samr_dispinfo_to_NET_DISPLAY_MACHINE(mem_ctx,
&info->info2,
entries_read,
buffer);
case 3:
return convert_samr_dispinfo_to_NET_DISPLAY_GROUP(mem_ctx,
&info->info3,
entries_read,
buffer);
default:
return WERR_UNKNOWN_LEVEL;
}
return WERR_OK;
}
/****************************************************************
****************************************************************/
WERROR NetQueryDisplayInformation_r(struct libnetapi_ctx *ctx,
struct NetQueryDisplayInformation *r)
{
struct cli_state *cli = NULL;
struct rpc_pipe_client *pipe_cli = NULL;
struct policy_handle connect_handle;
struct dom_sid2 *domain_sid = NULL;
struct policy_handle domain_handle;
union samr_DispInfo info;
uint32_t total_size = 0;
uint32_t returned_size = 0;
NTSTATUS status;
WERROR werr;
ZERO_STRUCT(connect_handle);
ZERO_STRUCT(domain_handle);
switch (r->in.level) {
case 1:
case 2:
case 3:
break;
default:
return WERR_UNKNOWN_LEVEL;
}
werr = libnetapi_open_ipc_connection(ctx, r->in.server_name, &cli);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
werr = libnetapi_open_pipe(ctx, cli, PI_SAMR, &pipe_cli);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
werr = libnetapi_samr_open_domain(ctx, pipe_cli,
SAMR_ACCESS_ENUM_DOMAINS |
SAMR_ACCESS_OPEN_DOMAIN,
SAMR_DOMAIN_ACCESS_LOOKUP_INFO_2 |
SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS |
SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
&connect_handle,
&domain_handle,
&domain_sid);
if (!W_ERROR_IS_OK(werr)) {
goto done;
}
status = rpccli_samr_QueryDisplayInfo2(pipe_cli,
ctx,
&domain_handle,
r->in.level,
r->in.idx,
r->in.entries_requested,
r->in.prefmaxlen,
&total_size,
&returned_size,
&info);
if (!NT_STATUS_IS_OK(status)) {
werr = ntstatus_to_werror(status);
goto done;
}
werr = convert_samr_dispinfo_to_NET_DISPLAY(ctx, &info,
r->in.level,
r->out.entries_read,
r->out.buffer);
done:
if (!cli) {
return werr;
}
if (is_valid_policy_hnd(&domain_handle)) {
rpccli_samr_Close(pipe_cli, ctx, &domain_handle);
}
if (is_valid_policy_hnd(&connect_handle)) {
rpccli_samr_Close(pipe_cli, ctx, &connect_handle);
}
return werr;
}
/****************************************************************
****************************************************************/
WERROR NetQueryDisplayInformation_l(struct libnetapi_ctx *ctx,
struct NetQueryDisplayInformation *r)
{
return WERR_NOT_SUPPORTED;
}
/****************************************************************
****************************************************************/
WERROR NetUserChangePassword_r(struct libnetapi_ctx *ctx,
struct NetUserChangePassword *r)
{
return WERR_NOT_SUPPORTED;
}
/****************************************************************
****************************************************************/
WERROR NetUserChangePassword_l(struct libnetapi_ctx *ctx,
struct NetUserChangePassword *r)
{
return WERR_NOT_SUPPORTED;
}
/****************************************************************
****************************************************************/
WERROR NetUserGetInfo_r(struct libnetapi_ctx *ctx,
struct NetUserGetInfo *r)
{
return WERR_NOT_SUPPORTED;
}
/****************************************************************
****************************************************************/
WERROR NetUserGetInfo_l(struct libnetapi_ctx *ctx,
struct NetUserGetInfo *r)
{
return WERR_NOT_SUPPORTED;
}
|