-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathNtUiLib.AutoCompletion.Sid.AppContainer.pas
386 lines (320 loc) · 9.83 KB
/
NtUiLib.AutoCompletion.Sid.AppContainer.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
unit NtUiLib.AutoCompletion.Sid.AppContainer;
{
This module adds support for representing and recognizing AppContainer and
Package Family SIDs.
Adding the module as a dependency enhances the following functions:
- RtlxStringToSid
- RtlxLookupSidInCustomProviders
- LsaxLookupSids
}
interface
uses
Ntapi.ObjBase, NtUtils;
const
// Custom domain names for AppContainer and Package Family SIDs
APP_CONTAINER_DOMAIN = 'APP CONTAINER';
APP_PACKAGE_DOMAIN = 'APP PACKAGE';
type
TAppContainerFilter = set of (
afParentAppContainer,
afChildAppContainer,
afPackage
);
// Remember a SID-name mapping for an AppContainer or a Package Family
function RtlxRememberAppContainer(
const FullMoniker: String
): TNtxStatus;
// Enumerate and remember AppContainers of a user
function RtlxCollectAppContainersForUser(
[opt] const ParentMoniker: String = '';
[opt] const UserSid: ISid = nil
): TNtxStatus;
// Enumerate and remember Package Families of a user
[RequiresCOM]
function RtlxCollectPackageFamiliesForUser(
AllUsers: Boolean;
[opt] const UserSid: ISid = nil
): TNtxStatus;
// Enumerate all accessible AppContainer and Package Family SIDs
procedure RtlxCollectAllAppContainersAndPackages(
[opt] const ParentMoniker: String = ''
);
// Retrieve the list of remembered AppContainer names
function RtlxEnumerateRememberedAppContainers(
Filter: TAppContainerFilter;
const AddPrefix: String = ''
): TArray<String>;
implementation
uses
Ntapi.WinNt, Ntapi.ntrtl, Ntapi.Versions, NtUtils.SysUtils,
DelphiUtils.Arrays, NtUtils.Security.Sid, NtUtils.Security.AppContainer,
NtUtils.Packages, NtUtils.Packages.WinRT, NtUtils.Profiles;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ Cache definitions }
type
TAppContainerEntry = record
Sid: ISid;
Name: String;
IsPackage: Boolean;
IsChild: Boolean;
end;
function RtlxCompareAppContainers(const A, B: TAppContainerEntry): Integer;
begin
Result := RtlxCompareSids(A.Sid, B.Sid);
end;
var
// Known AppContainer/Package SIDs
AppContainerCache: TArray<TAppContainerEntry>;
// Whether global collection of parent SIDs already ran
CollectedParents: Boolean;
// Parent SIDs for which child collection already ran
CollectedChildrenOf: TArray<ISid>;
function RtlxpFindCachedSidIndex(
const Sid: ISid
): Integer;
begin
Result := TArray.BinarySearchEx<TAppContainerEntry>(AppContainerCache,
function (const Entry: TAppContainerEntry): Integer
begin
Result := RtlxCompareSids(Entry.Sid, Sid);
end
);
end;
function RtlxpAlreadyCollectedChildrenOf(
const ParentSid: ISid
): Boolean;
begin
Result := TArray.BinarySearchEx<ISid>(CollectedChildrenOf,
function (const Entry: ISid): Integer
begin
Result := RtlxCompareSids(Entry, ParentSid);
end
) >= 0;
end;
{ Deriving }
function RtlxDeriveAndRememberAppContainer(
const FullMoniker: String;
out Entry: TAppContainerEntry
): TNtxStatus;
begin
Entry.Name := FullMoniker;
// Construct the SID
Result := RtlxDeriveFullAppContainerSid(FullMoniker, Entry.Sid,
@Entry.IsChild);
if not Result.IsSuccess then
Exit;
Entry.IsPackage := not Entry.IsChild and PkgxIsValidFamilyName(FullMoniker);
// Remember the mapping
TArray.InsertSorted<TAppContainerEntry>(AppContainerCache, Entry, dhSkip,
RtlxCompareAppContainers);
end;
function RtlxRememberAppContainer;
var
Dummy: TAppContainerEntry;
begin
Result := RtlxDeriveAndRememberAppContainer(FullMoniker, Dummy);
end;
function RtlxCollectAppContainersForUser;
var
Monikers: TArray<String>;
i: Integer;
begin
// Collect known monikers
Result := RtlxEnumerateAppContainerMonikers(Monikers, ParentMoniker, UserSid);
if not Result.IsSuccess then
Exit;
// Remember them
for i := 0 to High(Monikers) do
if ParentMoniker <> '' then
RtlxRememberAppContainer(ParentMoniker + '\' + Monikers[i])
else
RtlxRememberAppContainer(Monikers[i])
end;
function RtlxCollectPackageFamiliesForUser;
var
FamilyNames: TArray<String>;
i: Integer;
begin
// Collect known package families
Result := RoxEnumeratePackageFamilyNames(FamilyNames, AllUsers, UserSid);
if not Result.IsSuccess then
Exit;
// Remember them
for i := 0 to High(FamilyNames) do
RtlxRememberAppContainer(FamilyNames[i]);
end;
procedure RtlxCollectAllAppContainersAndPackages;
var
Users: TArray<ISid>;
ParentSid: ISid;
i: Integer;
begin
// Make sure to remember the parent moniker before processing children
if ParentMoniker <> '' then
RtlxRememberAppContainer(ParentMoniker);
// AppContainer profiles are per-user; collect all users
if not UnvxEnumerateProfiles(Users).IsSuccess then
Users := [nil]; // Or at least the current effective user
// Package SIDs don't have parent/child hierarchy
if ParentMoniker = '' then
begin
// Try enumerating all packages; otherwise, collect them from accessible users
if not RtlxCollectPackageFamiliesForUser(True).IsSuccess then
for i := 0 to High(Users) do
RtlxCollectPackageFamiliesForUser(False, Users[i]);
end;
// Collect AppContainer profiles from accessible users
for i := 0 to High(Users) do
RtlxCollectAppContainersForUser(ParentMoniker, Users[i]);
if ParentMoniker = '' then
// Mark that we enumerated all parents
CollectedParents := True
else
// Mark the parent SID as processed after enumerating its children
if RtlxDeriveParentAppContainerSid(ParentMoniker, ParentSid).IsSuccess then
TArray.InsertSorted<ISid>(CollectedChildrenOf, ParentSid, dhSkip,
RtlxCompareSids);
end;
{ Translation}
function RtlxRecognizeAppContainerSIDs(
const StringSid: String;
out Sid: ISid
): Boolean;
const
APP_CONTAINER_PREFIX = APP_CONTAINER_DOMAIN + '\';
APP_PACKAGE_PREFIX = APP_PACKAGE_DOMAIN + '\';
var
Name: String;
MakePackageSid: Boolean;
Entry: TAppContainerEntry;
begin
Result := False;
Name := StringSid;
// We accept all AppContainer names but only valid package family names
if RtlxPrefixStripString(APP_CONTAINER_PREFIX, Name) then
MakePackageSid := False
else if RtlxPrefixStripString(APP_PACKAGE_PREFIX, Name) and
PkgxIsValidFamilyName(Name) then
MakePackageSid := True
else
Exit;
// Derive the SID and remember the result
Result := RtlxDeriveAndRememberAppContainer(Name, Entry).IsSuccess;
if not Result then
Exit;
if MakePackageSid then
begin
// Make a copy of the SID before modifying it
Result := RtlxCopySid(Entry.Sid.Data, Sid).IsSuccess;
if not Result then
Exit;
// Package SIDs use the same algorithm, but a different sub authority
RtlSubAuthoritySid(Sid.Data, 0)^ := SECURITY_CAPABILITY_BASE_RID;
end
else
Sid := Entry.Sid;
end;
function RtlxProvideAppContainerSIDs(
const Sid: ISid;
out SidType: TSidNameUse;
out SidDomain: String;
out SidUser: String
): Boolean;
var
Index: Integer;
SidCopy, ParentSid: ISid;
RetrySearch: Boolean;
begin
Result := False;
SidType := SidTypeWellKnownGroup;
if RtlxIdentifierAuthoritySid(Sid) <> SECURITY_APP_PACKAGE_AUTHORITY then
Exit;
if (RtlxSubAuthorityCountSid(Sid) in
[SECURITY_PARENT_PACKAGE_RID_COUNT, SECURITY_CHILD_PACKAGE_RID_COUNT])
and (RtlxSubAuthoritySid(Sid, 0) = SECURITY_APP_PACKAGE_BASE_RID) then
begin
// Use AppContainer SIDs as is
SidDomain := APP_CONTAINER_DOMAIN;
SidCopy := Sid;
end
else if (RtlxSubAuthorityCountSid(Sid) = SECURITY_APP_PACKAGE_RID_COUNT) and
(RtlxSubAuthoritySid(Sid, 0) = SECURITY_CAPABILITY_BASE_RID) then
begin
SidDomain := APP_PACKAGE_DOMAIN;
// Duplicate Package SIDs to adjust the structure
Result := RtlxCopySid(Sid.Data, SidCopy).IsSuccess;
if not Result then
Exit;
// Swap the root sub-authority to match the cache structure
RtlSubAuthoritySid(SidCopy.Data, 0)^ := SECURITY_APP_PACKAGE_BASE_RID;
end
else
Exit;
// Check the cache to lookup remembered names
Index := RtlxpFindCachedSidIndex(SidCopy);
Result := Index >= 0;
if Result then
begin
// Found the name; return
SidUser := AppContainerCache[Index].Name;
Exit;
end;
RetrySearch := False;
// If we failed, make sure the cache has completed one-time initialization
if not CollectedParents then
begin
RtlxCollectAllAppContainersAndPackages;
RetrySearch := True;
end;
// For an unknown child SIDs, find its parent and then collect siblings
if (RtlxSubAuthorityCountSid(SidCopy) = SECURITY_CHILD_PACKAGE_RID_COUNT) and
RtlxGetAppContainerParent(SidCopy, ParentSid).IsSuccess and
not RtlxpAlreadyCollectedChildrenOf(ParentSid) then
begin
// Check the cache for the parent's name
Index := RtlxpFindCachedSidIndex(ParentSid);
if Index >= 0 then
begin
// Collect parent's children (i.e., the siblings of the input SID)
RtlxCollectAllAppContainersAndPackages(AppContainerCache[Index].Name);
RetrySearch := True;
end;
end;
if RetrySearch then
begin
// The cache changed; retry the query
Index := RtlxpFindCachedSidIndex(SidCopy);
Result := Index >= 0;
if Result then
SidUser := AppContainerCache[Index].Name;
end;
end;
function RtlxEnumerateRememberedAppContainers;
begin
Result := TArray.Convert<TAppContainerEntry, String>(
AppContainerCache,
function (const Entry: TAppContainerEntry; out Name: String): Boolean
begin
Result :=
((afParentAppContainer in Filter) and not Entry.IsChild) or
((afChildAppContainer in Filter) and Entry.IsChild) or
((afPackage in Filter) and Entry.IsPackage)
;
if not Result then
Exit;
if AddPrefix <> '' then
Name := AddPrefix + Entry.Name
else
Name := Entry.Name;
end
);
end;
initialization
if RtlOsVersionAtLeast(OsWin8) then
begin
RtlxRegisterSidNameRecognizer(RtlxRecognizeAppContainerSIDs);
RtlxRegisterSidNameProvider(RtlxProvideAppContainerSIDs);
end;
end.