-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUtils.Sam.pas
1994 lines (1578 loc) · 50.3 KB
/
NtUtils.Sam.pas
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
unit NtUtils.Sam;
{
This module provides functions for interacting with Security Account Manager.
}
interface
uses
Ntapi.WinNt, Ntapi.ntdef, Ntapi.ntsam, Ntapi.ntseapi, NtUtils,
DelphiUtils.AutoObjects;
type
ISamHandle = NtUtils.IHandle;
TRidAndName = record
RelativeID: Cardinal;
Name: String;
end;
TRidAndUse = record
RelativeID: Cardinal;
NameUse: TSidNameUse;
end;
TSidAndUse = record
SID: ISid;
NameUse: TSidNameUse;
end;
TNameAndUse = record
Name: String;
NameUse: TSidNameUse;
end;
TGroupMembership = Ntapi.ntsam.TGroupMembership;
TSamxLocalizableAccount = record
Rid: Cardinal;
NameUse: TSidNameUse;
Name: String;
AdminComment: String;
end;
// Connect to Security Account Manager
function SamxConnect(
out hxServer: ISamHandle;
DesiredAccess: TSamAccessMask;
[opt] const ServerName: String = ''
): TNtxStatus;
// Subscribe for notifications about changes in SAM/LSA; requires SYSTEM
function SamxNotifyChanges(
ObjectType: TSecurityDbObjectType;
const hxEvent: IHandle;
out Registration: IAutoReleasable
): TNtxStatus;
{ --------------------------------- Domains -------------------------------- }
// Enumerate domains on a server
function SamxEnumerateDomains(
out Names: TArray<String>;
[opt, Access(SAM_SERVER_ENUMERATE_DOMAINS)] hxServer: ISamHandle = nil
): TNtxStatus;
// Lookup a domain by name
function SamxLookupDomain(
const Name: String;
out DomainSid: ISid;
[opt, Access(SAM_SERVER_LOOKUP_DOMAIN)] hxServer: ISamHandle = nil
): TNtxStatus;
// Open a domain by SID
function SamxOpenDomain(
out hxDomain: ISamHandle;
const DomainId: ISid;
DesiredAccess: TDomainAccessMask;
[opt, Access(SAM_SERVER_LOOKUP_DOMAIN)] hxServer: ISamHandle = nil
): TNtxStatus;
// Open a domain by name
function SamxOpenDomainByName(
out hxDomain: ISamHandle;
const Name: String;
DesiredAccess: TDomainAccessMask;
[opt, Access(SAM_SERVER_LOOKUP_DOMAIN)] hxServer: ISamHandle = nil
): TNtxStatus;
// Open a domain by an SID of an account within this domain
function SamxOpenParentDomain(
out hxDomain: ISamHandle;
const Sid: ISid;
DesiredAccess: TDomainAccessMask;
[opt, Access(SAM_SERVER_LOOKUP_DOMAIN)] const hxServer: ISamHandle = nil
): TNtxStatus;
// Query domain information
function SamxQueryDomain(
[Access(DOMAIN_READ_OTHER_PARAMETERS or
DOMAIN_READ_PASSWORD_PARAMETERS)] const hxDomain: ISamHandle;
InfoClass: TDomainInformationClass;
out xBuffer: IAutoPointer
): TNtxStatus;
// Set domain information
function SamxSetDomain(
[Access(DOMAIN_WRITE_OTHER_PARAMETERS or
DOMAIN_WRITE_PASSWORD_PARAMS)] const hxDomain: ISamHandle;
InfoClass: TDomainInformationClass;
[in] Buffer: Pointer
): TNtxStatus;
type
SamxDomain = class abstract
// Query fixed-size domain information
class function Query<T>(
[Access(DOMAIN_READ_OTHER_PARAMETERS or
DOMAIN_READ_PASSWORD_PARAMETERS)] const hxDomain: ISamHandle;
InfoClass: TDomainInformationClass;
out Buffer: T
): TNtxStatus; static;
// Set fixed-size domain information
class function &Set<T>(
[Access(DOMAIN_WRITE_OTHER_PARAMETERS or
DOMAIN_WRITE_PASSWORD_PARAMS)] const hxDomain: ISamHandle;
InfoClass: TDomainInformationClass;
const Buffer: T
): TNtxStatus; static;
end;
// Query display information for accounts in a domain.
// The output contains an array of structures defined by the info class
// (i.e. IMemory<^TAnysizeArray<TDomainDisplayUser>> for DomainDisplayUser)
function SamxQueryDisplayDomain(
[Access(DOMAIN_LIST_ACCOUNTS)] const hxDomain: ISamHandle;
InfoClass: TDomainDisplayInformation;
out xMemory: IMemory;
out ReturnedEntryCount: Integer;
StartIndex: Cardinal = 0;
EntryCount: Cardinal = MAX_UINT;
[out, opt] pTotalAvailable: PCardinal = nil
): TNtxStatus;
// Find an index of an account for use with SamxQueryDisplayDomain
function SamxGetDisplayIndex(
[Access(DOMAIN_LIST_ACCOUNTS)] const hxDomain: ISamHandle;
DisplayInformation: TDomainDisplayInformation;
const Prefix: String;
out Index: Cardinal
): TNtxStatus;
// Enumerate domain accounts that support localization
function SamxQueryLocalizableAccountsDomain(
out Accounts: TArray<TSamxLocalizableAccount>;
[Access(DOMAIN_READ_OTHER_PARAMETERS)] const hxDomain: ISamHandle;
LanguageId: Cardinal = LANG_NEUTRAL
): TNtxStatus;
{ --------------------------------- Lookup --------------------------------- }
// Construct an SID with a specific RID within the same domain
function SamxRidToSid(
[Access(0)] const hxAccountOrDomainHandle: ISamHandle;
Rid: Cardinal;
out Sid: ISid
): TNtxStatus;
// Construct multiple SIDs with a specific RIDs within the same domain
function SamxRidsToSids(
[Access(0)] const hxAccountOrDomainHandle: ISamHandle;
const Rids: TArray<Cardinal>;
out Sids: TArray<ISid>
): TNtxStatus;
// Find an account by name and return its RID
function SamxNameToRid(
[Access(DOMAIN_LOOKUP)] const hxDomain: ISamHandle;
const Name: String;
out Lookup: TRidAndUse
): TNtxStatus;
// Find many accounts by names and return their RIDs
function SamxNamesToRids(
[Access(DOMAIN_LOOKUP)] const hxDomain: ISamHandle;
const Names: TArray<String>;
out Lookup: TArray<TRidAndUse>
): TNtxStatus;
// Find an account by name and return its SID
function SamxNameToSid(
[Access(DOMAIN_LOOKUP)] const hxDomain: ISamHandle;
const Name: String;
out Lookup: TSidAndUse
): TNtxStatus;
// Find many accounts by names and return their SIDs
function SamxNamesToSids(
[Access(DOMAIN_LOOKUP)] const hxDomain: ISamHandle;
const Names: TArray<String>;
out Lookup: TArray<TSidAndUse>
): TNtxStatus;
// Find an account by ID and return its name
function SamxRidToName(
[Access(DOMAIN_LOOKUP)] const hxDomain: ISamHandle;
RelativeId: Cardinal;
out Lookup: TNameAndUse
): TNtxStatus;
// Find many accounts by IDs and return their names
function SamxRidsToNames(
[Access(DOMAIN_LOOKUP)] const hxDomain: ISamHandle;
RelativeIds: TArray<Cardinal>;
out Lookup: TArray<TNameAndUse>
): TNtxStatus;
{ --------------------------------- Groups ---------------------------------- }
// Enumerate groups in a domain
function SamxEnumerateGroups(
[Access(DOMAIN_LIST_ACCOUNTS)] const hxDomain: ISamHandle;
out Groups: TArray<TRidAndName>
): TNtxStatus;
// Create a new group in a domain
function SamxCreateGroup(
[Access(DOMAIN_CREATE_GROUP)] const hxDomain: ISamHandle;
const Name: String;
out hxGroup: ISamHandle;
[out, opt] pRelativeId: PCardinal = nil;
DesiredAccess: TGroupAccessMask = GROUP_ALL_ACCESS
): TNtxStatus;
// Open a group
function SamxOpenGroup(
out hxGroup: ISamHandle;
[Access(DOMAIN_LOOKUP)] const hxDomain: ISamHandle;
GroupId: Cardinal;
DesiredAccess: TGroupAccessMask
): TNtxStatus;
// Open a group by name
function SamxOpenGroupByName(
out hxGroup: ISamHandle;
[Access(DOMAIN_LOOKUP)] const hxDomain: ISamHandle;
const Name: String;
DesiredAccess: TGroupAccessMask
): TNtxStatus;
// Open a group by full name
function SamxOpenGroupByFullName(
out hxGroup: ISamHandle;
const DomainName: String;
const GroupName: String;
DesiredAccess: TGroupAccessMask;
[opt, Access(SAM_SERVER_LOOKUP_DOMAIN)] const hxServer: ISamHandle = nil
): TNtxStatus;
// Open a group by a SID
function SamxOpenGroupBySid(
out hxGroup: ISamHandle;
const Sid: ISid;
DesiredAccess: TGroupAccessMask;
[opt, Access(SAM_SERVER_LOOKUP_DOMAIN)] const hxServer: ISamHandle = nil
): TNtxStatus;
// Enumerate group's members
function SamxEnumerateMembersGroup(
[Access(GROUP_LIST_MEMBERS)] const hxGroup: ISamHandle;
out Members: TArray<TGroupMembership>
): TNtxStatus;
// Add a member to a group
function SamxAddMemberGroup(
[Access(GROUP_ADD_MEMBER)] const hxGroup: ISamHandle;
MemberId: Cardinal;
Attributes: TGroupAttributes
): TNtxStatus;
// Remove a member from a group
function SamxRemoveMemberGroup(
[Access(GROUP_REMOVE_MEMBER)] const hxGroup: ISamHandle;
MemberId: Cardinal
): TNtxStatus;
// Adjust attributes of a group member
function SamxSetAttributesGroup(
[Access(GROUP_ADD_MEMBER)] const hxGroup: ISamHandle;
MemberId: Cardinal;
Attributes: TGroupAttributes
): TNtxStatus;
// Query group information
function SamxQueryGroup(
[Access(GROUP_READ_INFORMATION)] const hxGroup: ISamHandle;
InfoClass: TGroupInformationClass;
out xBuffer: IAutoPointer
): TNtxStatus;
// Set group information
function SamxSetGroup(
[Access(GROUP_WRITE_ACCOUNT)] const hxGroup: ISamHandle;
InfoClass: TGroupInformationClass;
[in] Buffer: Pointer
): TNtxStatus;
type
SamxGroup = class abstract
// Query fixed-size group information
class function Query<T>(
[Access(GROUP_READ_INFORMATION)] const hxGroup: ISamHandle;
InfoClass: TGroupInformationClass;
out Buffer: T
): TNtxStatus; static;
// Set fixed-size group information
class function &Set<T>(
[Access(GROUP_WRITE_ACCOUNT)] const hxGroup: ISamHandle;
InfoClass: TGroupInformationClass;
const Buffer: T
): TNtxStatus; static;
end;
// Delete a group from a domain
function SamxDeleteGroup(
[Access(_DELETE)] const hxGroup: ISamHandle
): TNtxStatus;
{ --------------------------------- Aliases --------------------------------- }
// Enumerate aliases in a domain
function SamxEnumerateAliases(
[Access(DOMAIN_LIST_ACCOUNTS)] const hxDomain: ISamHandle;
out Aliases: TArray<TRidAndName>
): TNtxStatus;
// Create a new alias in a domain
function SamxCreateAlias(
[Access(DOMAIN_CREATE_ALIAS)] const hxDomain: ISamHandle;
const Name: String;
out hxAlias: ISamHandle;
[out, opt] pRelativeId: PCardinal = nil;
DesiredAccess: TAliasAccessMask = ALIAS_ALL_ACCESS
): TNtxStatus;
// Open an alias
function SamxOpenAlias(
out hxAlias: ISamHandle;
[Access(DOMAIN_LOOKUP)] const hxDomain: ISamHandle;
AliasId: Cardinal;
DesiredAccess: TAliasAccessMask
): TNtxStatus;
// Open an alias
function SamxOpenAliasByName(
out hxAlias: ISamHandle;
[Access(DOMAIN_LOOKUP)] const hxDomain: ISamHandle;
const Name: String;
DesiredAccess: TAliasAccessMask
): TNtxStatus;
// Open an alias by full name
function SamxOpenAliasByFullName(
out hxAlias: ISamHandle;
const DomainName: String;
const AliasName: String;
DesiredAccess: TAliasAccessMask;
[opt, Access(SAM_SERVER_LOOKUP_DOMAIN)] const hxServer: ISamHandle = nil
): TNtxStatus;
// Open an alias by a SID
function SamxOpenAliasBySid(
out hxAlias: ISamHandle;
const Sid: ISid;
DesiredAccess: TAliasAccessMask;
[opt, Access(SAM_SERVER_LOOKUP_DOMAIN)] const hxServer: ISamHandle = nil
): TNtxStatus;
// Get alias members
function SamxEnumerateMembersAlias(
[Access(ALIAS_LIST_MEMBERS)] const hxAlias: ISamHandle;
out Members: TArray<ISid>
): TNtxStatus;
// Add a member to an alias
function SamxAddMemberAlias(
[Access(ALIAS_ADD_MEMBER)] const hxAlias: ISamHandle;
const MemberId: ISid
): TNtxStatus;
// Add multiple members to an alias
function SamxAddMembersAlias(
[Access(ALIAS_ADD_MEMBER)] const hxAlias: ISamHandle;
const MemberIds: TArray<ISid>
): TNtxStatus;
// Remove a member from an alias
function SamxRemoveMemberAlias(
[Access(ALIAS_ADD_MEMBER)] const hxAlias: ISamHandle;
const MemberId: ISid
): TNtxStatus;
// Remove multiple members from an alias
function SamxRemoveMembersAlias(
[Access(ALIAS_ADD_MEMBER)] const hxAlias: ISamHandle;
const MemberIds: TArray<ISid>
): TNtxStatus;
// Query alias information
function SamxQueryAlias(
[Access(ALIAS_READ_INFORMATION)] const hxAlias: ISamHandle;
InfoClass: TAliasInformationClass;
out xBuffer: IAutoPointer
): TNtxStatus;
// Set alias information
function SamxSetAlias(
[Access(ALIAS_WRITE_ACCOUNT)] const hxAlias: ISamHandle;
InfoClass: TAliasInformationClass;
[in] Buffer: Pointer
): TNtxStatus;
type
SamxAlias = class abstract
// Query fixed-size alias information
class function Query<T>(
[Access(ALIAS_READ_INFORMATION)] const hxAlias: ISamHandle;
InfoClass: TAliasInformationClass;
out Buffer: T
): TNtxStatus; static;
// Set fixed-size alias information
class function &Set<T>(
[Access(ALIAS_WRITE_ACCOUNT)] const hxAlias: ISamHandle;
InfoClass: TAliasInformationClass;
const Buffer: T
): TNtxStatus; static;
end;
// Delete an alias from a domain
function SamxDeleteAlias(
[Access(_DELETE)] const hxAlias: ISamHandle
): TNtxStatus;
// Find a union of all aliases in a domain that given SIDs are members of
function SamxGetAliasMembership(
[Access(DOMAIN_GET_ALIAS_MEMBERSHIP)] const hxDomain: ISamHandle;
const Sids: TArray<ISid>;
out AliasIds: TArray<Cardinal>
): TNtxStatus;
// Remove an SID from all aliases in a domain
function SamxRemoveMemberFromForeignDomain(
[Access(DOMAIN_LOOKUP)] const hxDomain: ISamHandle;
const MemberId: ISid
): TNtxStatus;
{ ---------------------------------- Users ---------------------------------- }
// Enumerate users in domain
function SamxEnumerateUsers(
[Access(DOMAIN_LIST_ACCOUNTS)] const hxDomain: ISamHandle;
out Users: TArray<TRidAndName>;
UserType: TUserAccountFlags = 0
): TNtxStatus;
// Create a new user in a domain
function SamxCreateUser(
[Access(DOMAIN_CREATE_USER)] const hxDomain: ISamHandle;
const Name: String;
out hxUser: ISamHandle;
[out, opt] pRelativeId: PCardinal = nil;
AccountType: TUserAccountFlags = USER_NORMAL_ACCOUNT;
DesiredAccess: TUserAccessMask = USER_ALL_ACCESS;
[out, opt] pGrantedAccess: PUserAccessMask = nil
): TNtxStatus;
// Open a user
function SamxOpenUser(
out hxUser: ISamHandle;
[Access(DOMAIN_LOOKUP)] const hxDomain: ISamHandle;
UserId: Cardinal;
DesiredAccess: TUserAccessMask
): TNtxStatus;
// Open a user by name
function SamxOpenUserByName(
out hxUser: ISamHandle;
[Access(DOMAIN_LOOKUP)] const hxDomain: ISamHandle;
const Name: String;
DesiredAccess: TUserAccessMask
): TNtxStatus;
// Open a user by full name
function SamxOpenUserByFullName(
out hxUser: ISamHandle;
const DomainName: String;
const UserName: String;
DesiredAccess: TUserAccessMask;
[opt, Access(SAM_SERVER_LOOKUP_DOMAIN)] const hxServer: ISamHandle = nil
): TNtxStatus;
// Open a user by a SID
function SamxOpenUserBySid(
out hxUser: ISamHandle;
const Sid: ISid;
DesiredAccess: TUserAccessMask;
[opt, Access(SAM_SERVER_LOOKUP_DOMAIN)] const hxServer: ISamHandle = nil
): TNtxStatus;
// Get groups for a user
function SamxEnumerateGroupsForUser(
[Access(USER_LIST_GROUPS)] const hxUser: ISamHandle;
out Groups: TArray<TGroupMembership>
): TNtxStatus;
// Query user information
function SamxQueryUser(
[Access(USER_READ_GENERAL or USER_READ_PREFERENCES or
USER_READ_LOGON or USER_READ_ACCOUNT)] const hxUser: ISamHandle;
InfoClass: TUserInformationClass;
out xBuffer: IAutoPointer
): TNtxStatus;
// Set user information
function SamxSetUser(
[Access(USER_WRITE_ACCOUNT)] const hxUser: ISamHandle;
InfoClass: TUserInformationClass;
[in] Buffer: Pointer
): TNtxStatus;
type
SamxUser = class abstract
// Query fixed-size user information
class function Query<T>(
[Access(USER_READ_GENERAL or USER_READ_PREFERENCES or
USER_READ_LOGON or USER_READ_ACCOUNT)] const hxUser: ISamHandle;
InfoClass: TUserInformationClass;
out Buffer: T
): TNtxStatus; static;
// Set fixed-size user information
class function &Set<T>(
[Access(USER_WRITE_ACCOUNT)] const hxUser: ISamHandle;
InfoClass: TUserInformationClass;
const Buffer: T
): TNtxStatus; static;
end;
// Delete a user from a domain
function SamxDeleteUser(
[Access(_DELETE)] const hxUser: ISamHandle
): TNtxStatus;
{ --------------------------------- Security ------------------------------- }
// Query security descriptor of a SAM object
function SamxQuerySecurityObject(
[Access(OBJECT_READ_SECURITY)] const SamHandle: ISamHandle;
Info: TSecurityInformation;
out SD: ISecurityDescriptor
): TNtxStatus;
// Set security descriptor on a SAM object
function SamxSetSecurityObject(
[Access(OBJECT_WRITE_SECURITY)] const SamHandle: ISamHandle;
Info: TSecurityInformation;
[in] SD: PSecurityDescriptor
): TNtxStatus;
implementation
uses
Ntapi.ntstatus, Ntapi.ntrtl, NtUtils.Security.Sid, NtUtils.Ldr;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
type
TSamAutoHandle = class(TCustomAutoHandle, ISamHandle, IAutoReleasable)
procedure Release; override;
end;
TSamAutoPointer = class(TCustomAutoPointer, IAutoPointer, IAutoReleasable)
procedure Release; override;
end;
TSamAutoMemory = class(TCustomAutoMemory, IMemory, IAutoPointer, IAutoReleasable)
procedure Release; override;
end;
TSamAutoNotification = class(TCustomAutoReleasable, IAutoReleasable)
FType: TSecurityDbObjectType;
FEvent: IHandle;
procedure Release; override;
constructor Create(
OperationType: TSecurityDbObjectType;
const hxEvent: IHandle
);
end;
{ Common & Server }
procedure TSamAutoHandle.Release;
begin
if (FHandle <> 0) and LdrxCheckDelayedImport(
delayed_SamCloseHandle).IsSuccess then
SamCloseHandle(FHandle);
FHandle := 0;
inherited;
end;
procedure TSamAutoPointer.Release;
begin
if Assigned(FData) and LdrxCheckDelayedImport(
delayed_SamFreeMemory).IsSuccess then
SamFreeMemory(FData);
FData := nil;
inherited;
end;
procedure TSamAutoMemory.Release;
begin
if Assigned(FData) and LdrxCheckDelayedImport(
delayed_SamFreeMemory).IsSuccess then
SamFreeMemory(FData);
FData := nil;
inherited;
end;
constructor TSamAutoNotification.Create;
begin
inherited Create;
FEvent := hxEvent;
FType := OperationType;
end;
procedure TSamAutoNotification.Release;
begin
if Assigned(FEvent) and LdrxCheckDelayedImport(
delayed_SamUnregisterObjectChangeNotification).IsSuccess then
SamUnregisterObjectChangeNotification(FType, FEvent.Handle);
FEvent := nil;
inherited;
end;
function SamxDelayAutoFree(
[in] Buffer: Pointer
): IAutoReleasable;
begin
Result := Auto.Delay(
procedure
begin
if LdrxCheckDelayedImport(
delayed_SamFreeMemory).IsSuccess then
SamFreeMemory(Buffer);
end
);
end;
function SamxConnect;
var
ObjAttr: TObjectAttributes;
ServerNameStr: TNtUnicodeString;
hServer: TSamHandle;
begin
Result := LdrxCheckDelayedImport(delayed_SamConnect);
if not Result.IsSuccess then
Exit;
Result := RtlxInitUnicodeString(ServerNameStr, ServerName);
if not Result.IsSuccess then
Exit;
InitializeObjectAttributes(ObjAttr);
Result.Location := 'SamConnect';
Result.LastCall.OpensForAccess(DesiredAccess);
Result.Status := SamConnect(ServerNameStr.RefOrNil, hServer, DesiredAccess,
ObjAttr);
if Result.IsSuccess then
hxServer := TSamAutoHandle.Capture(hServer);
end;
function SamxpEnsureConnected(
var hxServer: ISamHandle;
DesiredAccess: TSamAccessMask
): TNtxStatus;
begin
if not Assigned(hxServer) then
Result := SamxConnect(hxServer, DesiredAccess)
else
Result := NtxSuccess;
end;
function SamxNotifyChanges;
begin
Result := LdrxCheckDelayedImport(delayed_SamRegisterObjectChangeNotification);
if not Result.IsSuccess then
Exit;
Result.Location := 'SamRegisterObjectChangeNotification';
Result.Status := SamRegisterObjectChangeNotification(ObjectType,
hxEvent.Handle);
if Result.IsSuccess then
Registration := TSamAutoNotification.Create(ObjectType, hxEvent);
end;
{ Domains }
function SamxEnumerateDomains;
var
EnumContext: TSamEnumerationHandle;
Buffer: PSamRidEnumerationArray;
BufferDeallocator: IAutoReleasable;
Count: Cardinal;
i: Integer;
begin
Result := LdrxCheckDelayedImport(delayed_SamEnumerateDomainsInSamServer);
if not Result.IsSuccess then
Exit;
Result := SamxpEnsureConnected(hxServer, SAM_SERVER_ENUMERATE_DOMAINS);
if not Result.IsSuccess then
Exit;
EnumContext := 0;
Result.Location := 'SamEnumerateDomainsInSamServer';
Result.LastCall.Expects<TSamAccessMask>(SAM_SERVER_ENUMERATE_DOMAINS);
Result.Status := SamEnumerateDomainsInSamServer(hxServer.Handle, EnumContext,
Buffer, MAX_UINT, Count);
if not Result.IsSuccess then
Exit;
BufferDeallocator := SamxDelayAutoFree(Buffer);
SetLength(Names, Count);
// RelativeId is always zero for domains, but names are available
for i := 0 to High(Names) do
Names[i] := Buffer{$R-}[i]{$IFDEF R+}{$R+}{$ENDIF}.Name.ToString;
end;
function SamxLookupDomain;
var
Buffer: PSid;
BufferDeallocator: IAutoReleasable;
NameStr: TNtUnicodeString;
begin
Result := LdrxCheckDelayedImport(delayed_SamLookupDomainInSamServer);
if not Result.IsSuccess then
Exit;
Result := SamxpEnsureConnected(hxServer, SAM_SERVER_LOOKUP_DOMAIN);
if not Result.IsSuccess then
Exit;
Result := RtlxInitUnicodeString(NameStr, Name);
if not Result.IsSuccess then
Exit;
Result.Location := 'SamLookupDomainInSamServer';
Result.LastCall.Expects<TSamAccessMask>(SAM_SERVER_LOOKUP_DOMAIN);
Result.Status := SamLookupDomainInSamServer(hxServer.Handle, NameStr, Buffer);
if not Result.IsSuccess then
Exit;
BufferDeallocator := SamxDelayAutoFree(Buffer);
Result := RtlxCopySid(Buffer, DomainSid);
end;
function SamxOpenDomain;
var
hDomain: TSamHandle;
begin
Result := LdrxCheckDelayedImport(delayed_SamOpenDomain);
if not Result.IsSuccess then
Exit;
Result := SamxpEnsureConnected(hxServer, SAM_SERVER_LOOKUP_DOMAIN);
if not Result.IsSuccess then
Exit;
Result.Location := 'SamOpenDomain';
Result.LastCall.OpensForAccess(DesiredAccess);
Result.LastCall.Expects<TSamAccessMask>(SAM_SERVER_LOOKUP_DOMAIN);
Result.Status := SamOpenDomain(hxServer.Handle, DesiredAccess, DomainId.Data,
hDomain);
if Result.IsSuccess then
hxDomain := TSamAutoHandle.Capture(hDomain);
end;
function SamxOpenDomainByName;
var
DomainSid: ISid;
begin
Result := SamxpEnsureConnected(hxServer, SAM_SERVER_LOOKUP_DOMAIN);
if not Result.IsSuccess then
Exit;
Result := SamxLookupDomain(Name, DomainSid, hxServer);
if not Result.IsSuccess then
Exit;
Result := SamxOpenDomain(hxDomain, DomainSid, DesiredAccess, hxServer);
end;
function SamxOpenParentDomain;
var
ParentSid: ISid;
begin
Result := RtlxMakeParentSid(ParentSid, SID);
if Result.IsSuccess then
Result := SamxOpenDomain(hxDomain, ParentSid, DOMAIN_LOOKUP, hxServer);
end;
function SamxQueryDomain;
var
Buffer: Pointer;
begin
Result := LdrxCheckDelayedImport(delayed_SamQueryInformationDomain);
if not Result.IsSuccess then
Exit;
Result.Location := 'SamQueryInformationDomain';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.LastCall.Expects(ExpectedDomainQueryAccess(InfoClass));
Result.Status := SamQueryInformationDomain(HandleOrDefault(hxDomain),
InfoClass, Buffer);
if Result.IsSuccess then
xBuffer := TSamAutoPointer.Capture(Buffer);
end;
function SamxSetDomain;
begin
Result := LdrxCheckDelayedImport(delayed_SamSetInformationDomain);
if not Result.IsSuccess then
Exit;
Result.Location := 'SamSetInformationDomain';
Result.LastCall.UsesInfoClass(InfoClass, icSet);
Result.LastCall.Expects(ExpectedDomainSetAccess(InfoClass));
Result.Status := SamSetInformationDomain(HandleOrDefault(hxDomain), InfoClass, Buffer);
end;
class function SamxDomain.Query<T>;
var
xBuffer: IAutoPointer;
begin
Result := SamxQueryDomain(hxDomain, InfoClass, xBuffer);
if Result.IsSuccess then
Buffer := T(xBuffer.Data^);
end;
class function SamxDomain.&Set<T>;
begin
Result := SamxSetDomain(hxDomain, InfoClass, @Buffer);
end;
function SamxQueryDisplayDomain;
var
TotalAvailable, TotalReturned: Cardinal;
Buffer: Pointer;
begin
Result := LdrxCheckDelayedImport(delayed_SamQueryDisplayInformation);
if not Result.IsSuccess then
Exit;
Result.Location := 'SamQueryDisplayInformation';
Result.LastCall.Expects<TDomainAccessMask>(DOMAIN_LIST_ACCOUNTS);
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.Status := SamQueryDisplayInformation(HandleOrDefault(hxDomain),
InfoClass, StartIndex, EntryCount, MAX_UINT, TotalAvailable, TotalReturned,
Cardinal(ReturnedEntryCount), Buffer);
if not Result.IsSuccess then
Exit;
xMemory := TSamAutoMemory.Capture(Buffer, TotalReturned);
if Assigned(pTotalAvailable) then
pTotalAvailable^ := TotalAvailable;
end;
function SamxGetDisplayIndex;
var
PrefixStr: TNtUnicodeString;
begin
Result := LdrxCheckDelayedImport(delayed_SamGetDisplayEnumerationIndex);
if not Result.IsSuccess then
Exit;
Result := RtlxInitUnicodeString(PrefixStr, Prefix);
if not Result.IsSuccess then
Exit;
Result.Location := 'SamGetDisplayEnumerationIndex';
Result.LastCall.Expects<TDomainAccessMask>(DOMAIN_LIST_ACCOUNTS);
Result.Status := SamGetDisplayEnumerationIndex(HandleOrDefault(hxDomain),
DisplayInformation, PrefixStr, Index);
end;
function SamxQueryLocalizableAccountsDomain;
var
Buffer: PDomainLocalizableAccounts;
BufferDeallocator: IAutoReleasable;
Entry: PDomainLocalizableAccountsEntry;
i: Integer;
begin
Result := LdrxCheckDelayedImport(delayed_SamQueryLocalizableAccountsInDomain);
if not Result.IsSuccess then
Exit;
Result.Location := 'SamQueryLocalizableAccountsInDomain';
Result.LastCall.Expects<TDomainAccessMask>(DOMAIN_READ_OTHER_PARAMETERS);
Result.LastCall.UsesInfoClass(DomainLocalizableAccountsBasic, icQuery);
Result.Status := SamQueryLocalizableAccountsInDomain(
HandleOrDefault(hxDomain), 0, LanguageId, DomainLocalizableAccountsBasic,
Pointer(Buffer));
if not Result.IsSuccess then
Exit;
BufferDeallocator := SamxDelayAutoFree(Buffer);
Entry := @Buffer.Entries[0];
SetLength(Accounts, Buffer.Count);
for i := 0 to High(Accounts) do
begin
Accounts[i].Rid := Entry.Rid;
Accounts[i].NameUse := Entry.NameUse;
Accounts[i].Name := Entry.Name.ToString;
Accounts[i].AdminComment := Entry.AdminComment.ToString;
Inc(Entry);
end;
end;
{ Accounts }
function SamxRidToSid;
var
Buffer: PSid;
BufferDeallocator: IAutoReleasable;
begin
Result := LdrxCheckDelayedImport(delayed_SamRidToSid);
if not Result.IsSuccess then
Exit;
Result.Location := 'SamRidToSid';
Result.Status := SamRidToSid(HandleOrDefault(hxAccountOrDomainHandle), Rid,
Buffer);
if not Result.IsSuccess then
Exit;
BufferDeallocator := SamxDelayAutoFree(Buffer);
Result := RtlxCopySid(Buffer, Sid);
end;
function SamxRidsToSids;
var
i: Integer;
Sid0: ISid;