-
Notifications
You must be signed in to change notification settings - Fork 36
/
NtUtils.Security.Acl.pas
826 lines (666 loc) · 20.3 KB
/
NtUtils.Security.Acl.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
unit NtUtils.Security.Acl;
{
This module adds support for Access Control List construction, modification,
and canonicalization.
}
interface
uses
Ntapi.WinNt, NtUtils, DelphiUtils.AutoObjects;
type
TAceData = record
AceType: TAceType;
AceFlags: TAceFlags;
Mask: TAccessMask;
SID: ISid; // aka. Client SID for compound ACEs
ServerSID: ISid; // Compound ACEs only
CompoundAceType: TCompoundAceType; // Compound ACEs only
ObjectFlags: TObjectAceFlags; // Object ACEs only
ObjectType: TGuid; // Object ACEs only
InheritedObjectType: TGuid; // Object ACEs only
ExtraData: IMemory;
class function New(
AceType: TAceType;
AceFlags: TAceFlags;
Mask: TAccessMask;
const SID: ISid
): TAceData; static;
end;
// Define the canonical order
TAceCategory = (
acExplicitDeny,
acExplicitDenyInheritOnly,
acExplicit,
acExplicitInheritOnly,
acInherited
);
// Retrieve size information of an ACL
function RtlxSizeAcl(
[opt] const Acl: IAcl
): TAclSizeInformation;
{ Creation }
// Create a new ACL
function RtlxCreateAcl(
out Acl: IAcl;
Size: Cardinal = 256
): TNtxStatus;
// Create a copy of an ACL.
// The function returns a NULL ACL when the input is NULL
function RtlxCopyAcl(
[MayReturnNil] out NewAcl: IAcl;
[in, opt] const SourceAcl: IAcl
): TNtxStatus;
// Relocate the ACL if necessary to satisfy the size requirements
function RtlxEnsureFreeBytesAcl(
var Acl: IAcl;
RequiredFreeBytes: Cardinal
): TNtxStatus;
// Append all ACEs from one ACL to another.
// The function returns a NULL ACL if both inputs are NULL
function RtlxAppendAcl(
[MayReturnNil] var TargetAcl: IAcl;
[in, opt] const SourceAcl: IAcl
): TNtxStatus;
// Capture an ACL from a raw pointer
// The function returns a NULL ACL when the input is NULL
function RtlxCaptureAcl(
[MayReturnNil] out Acl: IAcl;
[in, opt] Buffer: PAcl
): TNtxStatus;
// Create an ACL from a collection of ACEs
function RtlxBuildAcl(
out Acl: IAcl;
const AceData: TArray<TAceData>
): TNtxStatus;
{ Operations }
// Export all ACEs from an ACL
function RtlxDumpAcl(
[opt] const Acl: IAcl;
out Aces: TArray<TAceData>
): TNtxStatus;
// Map a generic mapping for each ACE in the ACL
function RtlxMapGenericMaskAcl(
[in, out] const Acl: IAcl;
const GenericMapping: TGenericMapping
): TNtxStatus;
{ Ordering }
// Determine which canonical category an ACE belongs to
function RtlxGetCategoryAce(
AceType: TAceType;
AceFlags: TAceFlags
): TAceCategory;
// Check if an ACL matches requirements for being canonical
function RtlxIsCanonicalAcl(
[opt] const Acl: IAcl;
out IsCanonical: Boolean
): TNtxStatus;
// Determine appropriate location for insertion of an ACE
function RtlxChooseIndexAce(
[opt] const Acl: IAcl;
Category: TAceCategory
): Integer;
// Reorder ACEs to make a canonical ACL
// The function returns a NULL ACL when the input is NULL
function RtlxCanonicalizeAcl(
[MayReturnNil] var Acl: IAcl
): TNtxStatus;
{ ACE manipulation }
// Allocate an ACE from a prototype
function RtlxAllocateAce(
const AceData: TAceData;
out Buffer: IMemory<PAce>
): TNtxStatus;
// Save an ACE into a prototype
function RtlxCaptureAce(
[in] Buffer: PAce;
out AceData: TAceData
): TNtxStatus;
// Insert an ACE preserving the canonical order of an ACL
function RtlxAddAce(
var Acl: IAcl;
const Ace: TAceData
): TNtxStatus;
// Insert an ACE into a particular location
function RtlxInsertAce(
var Acl: IAcl;
const Ace: TAceData;
Index: Integer
): TNtxStatus;
// Remove an ACE by index
function RtlxDeleteAce(
var Acl: IAcl;
Index: Integer
): TNtxStatus;
// Obtain a copy of an ACE
function RtlxGetAce(
const Acl: IAcl;
Index: Integer;
out AceData: TAceData
): TNtxStatus;
implementation
uses
Ntapi.ntdef, Ntapi.ntrtl, Ntapi.ntstatus, NtUtils.Security.Sid, NtUtils.Errors;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ ACL information }
function RtlxSizeAcl;
begin
if not Assigned(Acl) or not RtlQueryInformationAcl(Acl.Data, @Result,
SizeOf(Result), AclSizeInformation).IsSuccess then
Result := Default(TAclSizeInformation);
end;
{ ACL creation }
function RtlxCreateAcl;
var
Buffer: IMemory absolute Acl;
begin
// Align the size up to the next DWORD
Size := AlignUp(Size, SizeOf(Cardinal));
if Size < SizeOf(TAcl) then
Size := SizeOf(TAcl)
else if Size > MAX_ACL_SIZE then
Size := MAX_ACL_SIZE;
Buffer := Auto.AllocateDynamic(Size);
Result.Location := 'RtlCreateAcl';
Result.Status := RtlCreateAcl(Buffer.Data, Buffer.Size, ACL_REVISION);
end;
function AddExtraSpace(Size: Cardinal): Cardinal;
begin
// + 12.5% + 256 B
Result := Size + Size shr 3 + 256;
end;
function RtlxCopyAcl;
begin
// Duplicate NULL ACLs
if not Assigned(SourceAcl) then
begin
NewAcl := nil;
Exit(NtxSuccess);
end;
// Create a new ACL
Result := RtlxCreateAcl(NewAcl, AddExtraSpace(SizeOf(TAcl) +
RtlxSizeAcl(SourceAcl).AclBytesInUseByAces));
if not Result.IsSuccess then
Exit;
// Copy all ACEs from the source
Result := RtlxAppendAcl(NewAcl, SourceAcl);
end;
function RtlxEnsureFreeBytesAcl;
var
SizeInfo: TAclSizeInformation;
RequiredSize: Cardinal;
ExpandedAcl: IAcl;
begin
SizeInfo := RtlxSizeAcl(Acl);
RequiredSize := SizeOf(TAcl) + SizeInfo.AclBytesInUseByAces +
RequiredFreeBytes;
// Can't grow enough
if RequiredSize > MAX_ACL_SIZE then
begin
Result.Location := 'RtlxEnsureFreeBytesAcl';
Result.Status := STATUS_BUFFER_TOO_SMALL;
Exit;
end;
// Reserve some extra space
RequiredSize := AddExtraSpace(RequiredSize);
// Already enough?
if Assigned(Acl) and (SizeInfo.AclBytesTotal >= RequiredSize) then
Exit(NtxSuccess);
// Allocate a new ACL
Result := RtlxCreateAcl(ExpandedAcl, RequiredSize);
if not Result.IsSuccess then
Exit;
// Copy existing ACEs
Result := RtlxAppendAcl(ExpandedAcl, Acl);
// Swap the reference
if Result.IsSuccess then
Acl := ExpandedAcl;
end;
function RtlxAppendAcl;
var
SourceSize, TargetSize: TAclSizeInformation;
FirstNewAce: PAce;
begin
// NULL + NULL = NULL; the rest gives non-NULL output
if not Assigned(TargetAcl) and not Assigned(SourceAcl) then
Exit(NtxSuccess);
SourceSize := RtlxSizeAcl(SourceAcl);
TargetSize := RtlxSizeAcl(TargetAcl);
// Make sure the new ACEs fit
Result := RtlxEnsureFreeBytesAcl(TargetAcl, SourceSize.AclBytesInUseByAces);
if SourceSize.AceCount > 0 then
begin
// Copy ACEs
Result.Location := 'RtlGetAce';
Result.Status := RtlGetAce(SourceAcl.Data, 0, FirstNewAce);
if not Result.IsSuccess then
Exit;
Result.Location := 'RtlAddAce';
Result.Status := RtlAddAce(TargetAcl.Data, SourceAcl.Data.AclRevision, -1,
FirstNewAce, SourceSize.AclBytesInUseByAces);
end;
end;
function RtlxCaptureAcl;
var
SizeInfo: TAclSizeInformation;
pAces: PAce;
begin
// NULL ACL remains NULL
if not Assigned(Buffer) then
begin
Acl := nil;
Exit(NtxSuccess);
end;
if not RtlValidAcl(Buffer) then
begin
Result.Location := 'RtlValidAcl';
Result.Status := STATUS_INVALID_ACL;
Exit;
end;
// Determine input's size
Result.Location := 'RtlQueryInformationAcl';
Result.LastCall.UsesInfoClass(AclSizeInformation, icQuery);
Result.Status := RtlQueryInformationAcl(Buffer, @SizeInfo, SizeOf(SizeInfo),
AclSizeInformation);
if not Result.IsSuccess then
Exit;
// Make a new ACL of sufficient size
Result := RtlxCreateAcl(Acl, SizeInfo.AclBytesInUse);
if not Result.IsSuccess then
Exit;
if SizeInfo.AceCount > 0 then
begin
// Copy ACEs
Result.Location := 'RtlGetAce';
Result.Status := RtlGetAce(Buffer, 0, pAces);
if not Result.IsSuccess then
Exit;
Result.Location := 'RtlAddAce';
Result.Status := RtlAddAce(Acl.Data, Buffer.AclRevision, -1,
pAces, SizeInfo.AclBytesInUseByAces);
end;
end;
function RtlxBuildAcl;
var
AceBuffers: TArray<IMemory<PAce>>;
RequiredSize: Cardinal;
i: Integer;
begin
RequiredSize := SizeOf(TAcl);
SetLength(AceBuffers, Length(AceData));
// Allocate all ACEs and compute the required ACL size
for i := 0 to High(AceBuffers) do
begin
Result := RtlxAllocateAce(AceData[i], AceBuffers[i]);
if not Result.IsSuccess then
Exit;
Inc(RequiredSize, AceBuffers[i].Size);
end;
Result := RtlxCreateAcl(Acl, RequiredSize);
if not Result.IsSuccess then
Exit;
for i := 0 to High(AceBuffers) do
begin
Result.Location := 'RtlAddAce';
Result.Status := RtlAddAce(Acl.Data, AceBuffers[i].Data.Header.Revision, -1,
AceBuffers[i].Data, AceBuffers[i].Size);
if not Result.IsSuccess then
Exit;
end;
end;
{ ACL operations }
function RtlxDumpAcl;
var
i: Integer;
Ace: PAce;
begin
Result := NtxSuccess;
SetLength(Aces, RtlxSizeAcl(Acl).AceCount);
for i := 0 to High(Aces) do
begin
Result.Location := 'RtlGetAce';
Result.Status := RtlGetAce(Acl.Data, i, Ace);
if not Result.IsSuccess then
Exit;
Result := RtlxCaptureAce(Ace, Aces[i]);
end;
end;
function RtlxMapGenericMaskAcl;
var
i: Integer;
Ace: PAce;
begin
Result := NtxSuccess;
for i := 0 to Pred(RtlxSizeAcl(Acl).AceCount) do
begin
Result.Location := 'RtlGetAce';
Result.Status := RtlGetAce(Acl.Data, i, Ace);
if not Result.IsSuccess then
Exit;
if Ace.Header.AceType in NonObjectAces then
RtlMapGenericMask(Ace.NonObjectAce.Mask, GenericMapping)
else if Ace.Header.AceType = ACCESS_ALLOWED_COMPOUND_ACE_TYPE then
RtlMapGenericMask(Ace.CompoundAce.Mask, GenericMapping)
else if Ace.Header.AceType in ObjectAces then
RtlMapGenericMask(Ace.ObjectAce.Mask, GenericMapping)
else
begin
// Unrecognized ACE type
Result.Location := 'RtlxMapGenericMaskAcl';
Result.LastCall.UsesInfoClass(Ace.Header.AceType, icParse);
Result.Status := STATUS_UNKNOWN_REVISION;
Exit;
end;
end;
end;
{ Ordering }
function RtlxGetCategoryAce;
begin
// Inherited ACEs always come after explicitly defined
if BitTest(AceFlags and INHERITED_ACE) then
Result := acInherited
// Explicit denying ACEs come before explicit allowing ACEs
else if AceType in AccessDeniedAces then
begin
// ACEs affecting the object come before inherit-only ACEs
if BitTest(AceFlags and INHERIT_ONLY_ACE) then
Result := acExplicitDenyInheritOnly
else
Result := acExplicitDeny;
end
else
begin
// ACEs affecting the object come before inherit-only ACEs
if BitTest(AceFlags and INHERIT_ONLY_ACE) then
Result := acExplicitInheritOnly
else
Result := acExplicit;
end;
end;
function RtlxIsCanonicalAcl;
var
AceRef: PAce;
LastCategory, CurrentCategory: TAceCategory;
i: Integer;
begin
Result := NtxSuccess;
// The elements of the enumeration follow the required order
LastCategory := Low(TAceCategory);
for i := 0 to Pred(RtlxSizeAcl(Acl).AceCount) do
begin
Result.Location := 'RtlGetAce';
Result.Status := RtlGetAce(Acl.Data, i, AceRef);
if not Result.IsSuccess then
Exit;
// Determine which category the ACE belongs to
CurrentCategory := RtlxGetCategoryAce(AceRef.Header.AceType,
AceRef.Header.AceFlags);
// Categories should always grow
if not (CurrentCategory >= LastCategory) then
begin
IsCanonical := False;
Exit;
end;
LastCategory := CurrentCategory;
end;
IsCanonical := True;
end;
function RtlxChooseIndexAce;
var
AceRef: PAce;
CurrentCategory: TAceCategory;
i: Integer;
begin
// Insert as the last by default
Result := -1;
for i := 0 to Pred(RtlxSizeAcl(Acl).AceCount) do
begin
if not RtlGetAce(Acl.Data, i, AceRef).IsSuccess then
Exit;
// Determine which category the ACE belongs to
CurrentCategory := RtlxGetCategoryAce(AceRef.Header.AceType,
AceRef.Header.AceFlags);
// Insert right before the next category
if CurrentCategory > Category then
Exit(i);
end;
end;
function RtlxCanonicalizeAcl;
var
Ace: PAce;
Aces: array [TAceCategory] of TArray<PAce>;
i: Integer;
Category: TAceCategory;
NewAcl: IAcl;
begin
// No need to order NULL ACLs
if not Assigned(Acl) then
begin
Result := NtxSuccess;
Exit;
end;
// Each category is empty by default
for Category := Low(TAceCategory) to High(TAceCategory) do
Aces[Category] := nil;
// Save each ACE under the corresponding category
for i := 0 to Pred(RtlxSizeAcl(Acl).AceCount) do
begin
Result.Location := 'RtlGetAce';
Result.Status := RtlGetAce(Acl.Data, i, Ace);
if not Result.IsSuccess then
Exit;
Category := RtlxGetCategoryAce(Ace.Header.AceType, Ace.Header.AceFlags);
SetLength(Aces[Category], Length(Aces[Category]) + 1);
Aces[Category][High(Aces[Category])] := Ace;
end;
// Allocate a new ACL
Result := RtlxCreateAcl(NewAcl, AddExtraSpace(RtlxSizeAcl(Acl).AclBytesInUse));
// Add ACEs category-by-category preserving their order within each
for Category := Low(TAceCategory) to High(TAceCategory) do
for i := 0 to High(Aces[Category]) do
begin
Result.Location := 'RtlAddAce';
Result.Status := RtlAddAce(NewAcl.Data, Aces[Category][i].Header.Revision,
-1, Aces[Category][i], Aces[Category][i].Header.AceSize);
if not Result.IsSuccess then
Exit;
end;
// Swap the reference
Acl := NewAcl;
end;
{ ACE manipulation }
class function TAceData.New;
begin
Result := Default(TAceData);
Result.AceType := AceType;
Result.AceFlags := AceFlags;
Result.Mask := Mask;
Result.SID := SID;
end;
function RtlxAllocateAce;
var
Size: Cardinal;
begin
Result := NtxSuccess;
if AceData.AceType in NonObjectAces then
begin
// Non-object ACE
Size := SizeOf(TKnownAce) + RtlLengthSid(AceData.SID.Data);
if Assigned(AceData.ExtraData) then
Inc(Size, AceData.ExtraData.Size);
// ACE size should be multiple of DWORD size
Size := AlignUp(Size, SizeOf(Cardinal));
IMemory(Buffer) := Auto.AllocateDynamic(Size);
Buffer.Data.Header.AceType := AceData.AceType;
Buffer.Data.Header.AceFlags := AceData.AceFlags;
Buffer.Data.Header.AceSize := Size;
Buffer.Data.NonObjectAce.Mask := AceData.Mask;
Move(AceData.SID.Data^, Buffer.Data.NonObjectAce.Sid^,
RtlLengthSid(AceData.SID.Data));
if Assigned(AceData.ExtraData) then
Move(AceData.ExtraData.Data^, Buffer.Data.NonObjectAce.ExtraData^,
AceData.ExtraData.Size);
end
else if AceData.AceType = ACCESS_ALLOWED_COMPOUND_ACE_TYPE then
begin
if not Assigned(AceData.ServerSID) then
begin
Result.Location := 'RtlxAllocateAce';
Result.Status := STATUS_INVALID_SID;
Exit;
end;
Size := SizeOf(TKnownCompoundAce) +
RtlLengthSid(AceData.SID.Data) +
RtlLengthSid(AceData.ServerSID.Data);
if Assigned(AceData.ExtraData) then
Inc(Size, AceData.ExtraData.Size);
// ACE size should be multiple of DWORD size
Size := AlignUp(Size, SizeOf(Cardinal));
IMemory(Buffer) := Auto.AllocateDynamic(Size);
Buffer.Data.Header.AceType := AceData.AceType;
Buffer.Data.Header.AceFlags := AceData.AceFlags;
Buffer.Data.Header.AceSize := Size;
Buffer.Data.CompoundAce.Mask := AceData.Mask;
Buffer.Data.CompoundAce.CompoundAceType := AceData.CompoundAceType;
// Marshal the server SID first due to layout
Move(AceData.ServerSID.Data^, Buffer.Data.CompoundAce.ServerSid^,
RtlLengthSid(AceData.ServerSID.Data));
// Follow by the client SID
Move(AceData.SID.Data^, Buffer.Data.CompoundAce.ClientSid^,
RtlLengthSid(AceData.SID.Data));
if Assigned(AceData.ExtraData) then
Move(AceData.ExtraData.Data^, Buffer.Data.CompoundAce.ExtraData^,
AceData.ExtraData.Size);
end
else if AceData.AceType in ObjectAces then
begin
// Object ACE
Size := SizeOf(TKnownObjectAce) + RtlLengthSid(AceData.SID.Data);
// GUIDs are optional
if BitTest(AceData.ObjectFlags and ACE_OBJECT_TYPE_PRESENT) then
Inc(Size, SizeOf(TGuid));
if BitTest(AceData.ObjectFlags and ACE_INHERITED_OBJECT_TYPE_PRESENT) then
Inc(Size, SizeOf(TGuid));
if Assigned(AceData.ExtraData) then
Inc(Size, AceData.ExtraData.Size);
// ACE size should be multiple of DWORD size
Size := AlignUp(Size, SizeOf(Cardinal));
IMemory(Buffer) := Auto.AllocateDynamic(Size);
Buffer.Data.Header.AceType := AceData.AceType;
Buffer.Data.Header.AceFlags := AceData.AceFlags;
Buffer.Data.Header.AceSize := Size;
Buffer.Data.ObjectAce.Mask := AceData.Mask;
Buffer.Data.ObjectAce.Flags := AceData.ObjectFlags;
// Marshal GUIDs in the right order before the SID
if BitTest(AceData.ObjectFlags and ACE_OBJECT_TYPE_PRESENT) then
Buffer.Data.ObjectAce.ObjectType^ := AceData.ObjectType;
if BitTest(AceData.ObjectFlags and ACE_INHERITED_OBJECT_TYPE_PRESENT) then
Buffer.Data.ObjectAce.InheritedObjectType^ := AceData.InheritedObjectType;
Move(AceData.SID.Data^, Buffer.Data.ObjectAce.Sid^,
RtlLengthSid(AceData.SID.Data));
if Assigned(AceData.ExtraData) then
Move(AceData.ExtraData.Data^, Buffer.Data.ObjectAce.ExtraData^,
AceData.ExtraData.Size);
end
else
begin
Result.Location := 'RtlxAllocateAce';
Result.LastCall.UsesInfoClass(AceData.AceType, icMarshal);
Result.Status := STATUS_UNKNOWN_REVISION;
end;
end;
function RtlxCaptureAce;
begin
Result := NtxSuccess;
AceData := Default(TAceData);
AceData.AceType := Buffer.Header.AceType;
AceData.AceFlags := Buffer.Header.AceFlags;
if AceData.AceType in NonObjectAces then
begin
// Non-object ACE
AceData.Mask := Buffer.NonObjectAce.Mask;
Result := RtlxCopySid(Buffer.NonObjectAce.Sid, AceData.Sid);
if Buffer.NonObjectAce.ExtraDataSize > 0 then
AceData.ExtraData := Auto.CopyDynamic(Buffer.NonObjectAce.ExtraData,
Buffer.NonObjectAce.ExtraDataSize);
end
else if AceData.AceType = ACCESS_ALLOWED_COMPOUND_ACE_TYPE then
begin
// Compound ACE
AceData.Mask := Buffer.CompoundAce.Mask;
AceData.CompoundAceType := Buffer.CompoundAce.CompoundAceType;
Result := RtlxCopySid(Buffer.CompoundAce.ClientSid, AceData.Sid);
if not Result.IsSuccess then
Exit;
Result := RtlxCopySid(Buffer.CompoundAce.ServerSid, AceData.ServerSID);
if Buffer.CompoundAce.ExtraDataSize > 0 then
AceData.ExtraData := Auto.CopyDynamic(Buffer.CompoundAce.ExtraData,
Buffer.CompoundAce.ExtraDataSize);
end
else if AceData.AceType in ObjectAces then
begin
// Object ACE
AceData.Mask := Buffer.ObjectAce.Mask;
AceData.ObjectFlags := Buffer.ObjectAce.Flags;
if BitTest(AceData.ObjectFlags and ACE_OBJECT_TYPE_PRESENT) then
AceData.ObjectType := Buffer.ObjectAce.ObjectType^;
if BitTest(AceData.ObjectFlags and ACE_INHERITED_OBJECT_TYPE_PRESENT) then
AceData.InheritedObjectType := Buffer.ObjectAce.InheritedObjectType^;
Result := RtlxCopySid(Buffer.ObjectAce.Sid, AceData.Sid);
if Buffer.ObjectAce.ExtraDataSize > 0 then
AceData.ExtraData := Auto.CopyDynamic(Buffer.ObjectAce.ExtraData,
Buffer.ObjectAce.ExtraDataSize);
end
else
begin
Result.Location := 'RtlxCaptureAce';
Result.LastCall.UsesInfoClass(Buffer.Header.AceType, icParse);
Result.Status := STATUS_UNKNOWN_REVISION;
end;
end;
function RtlxAddAce;
begin
Result := RtlxInsertAce(Acl, Ace, RtlxChooseIndexAce(Acl,
RtlxGetCategoryAce(Ace.AceType, Ace.AceFlags)));
end;
function RtlxInsertAce;
var
AceBuffer: IMemory<PAce>;
begin
Result := RtlxAllocateAce(Ace, AceBuffer);
if not Result.IsSuccess then
Exit;
Result := RtlxEnsureFreeBytesAcl(Acl, AceBuffer.Size);
if not Result.IsSuccess then
Exit;
Result.Location := 'RtlAddAce';
Result.Status := RtlAddAce(Acl.Data, AceBuffer.Data.Header.Revision,
Index, AceBuffer.Data, AceBuffer.Size);
end;
function RtlxDeleteAce;
begin
if not Assigned(Acl) then
begin
Result.Location := 'RtlxDeleteAce';
Result.Status := STATUS_INVALID_PARAMETER;
Exit;
end;
Result.Location := 'RtlDeleteAce';
Result.Status := RtlDeleteAce(Acl.Data, Index);
end;
function RtlxGetAce;
var
AceRef: PAce;
begin
if not Assigned(Acl) then
begin
Result.Location := 'RtlxGetAce';
Result.Status := STATUS_INVALID_PARAMETER;
Exit;
end;
Result.Location := 'RtlGetAce';
Result.Status := RtlGetAce(Acl.Data, Index, AceRef);
if Result.IsSuccess then
Result := RtlxCaptureAce(AceRef, AceData);
end;
end.