Repository navigation
Expand file tree
/
Copy pathRuntimeDataAclRepairService.cs
More file actions
253 lines (225 loc) · 9.14 KB
/
Copy pathRuntimeDataAclRepairService.cs
File metadata and controls
253 lines (225 loc) · 9.14 KB
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
using System.IO;
using System.Security;
using System.Security.AccessControl;
using System.Security.Principal;
namespace ContextMenuMgr.Backend.Services;
internal sealed record RuntimeDataAclRepairResult(bool Success, string Code, string Detail);
internal static class RuntimeDataAclRepairService
{
private const int MaxFailureDetails = 5;
private static readonly SecurityIdentifier BuiltinUsersSid = new(WellKnownSidType.BuiltinUsersSid, null);
private static readonly SecurityIdentifier AuthenticatedUsersSid = new(WellKnownSidType.AuthenticatedUserSid, null);
private static readonly SecurityIdentifier WorldSid = new(WellKnownSidType.WorldSid, null);
public static RuntimeDataAclRepairResult RepairRuntimeDataDirectory(
string rootDirectory,
CancellationToken cancellationToken = default)
{
if (!OperatingSystem.IsWindows())
{
return new RuntimeDataAclRepairResult(false, "UNSUPPORTED_PLATFORM", "Runtime data ACL repair is only supported on Windows.");
}
if (string.IsNullOrWhiteSpace(rootDirectory))
{
return new RuntimeDataAclRepairResult(false, "INVALID_RUNTIME_DATA_DIRECTORY", "Runtime data directory path is empty.");
}
var repairedCount = 0;
var failedCount = 0;
var failures = new List<string>();
try
{
cancellationToken.ThrowIfCancellationRequested();
Directory.CreateDirectory(rootDirectory);
RepairDirectoryAcl(rootDirectory);
repairedCount++;
}
catch (OperationCanceledException)
{
return new RuntimeDataAclRepairResult(false, "CANCELLED", "Runtime data ACL repair was cancelled before the root directory was repaired.");
}
catch (Exception ex) when (IsRepairException(ex))
{
return new RuntimeDataAclRepairResult(
false,
"ROOT_REPAIR_FAILED",
$"Failed to repair runtime data root '{rootDirectory}': {ex.GetType().Name}: {ex.Message}");
}
var pendingDirectories = new Stack<string>();
// Materialize both siblings before logger/DI initialization. Existing protected child
// ACLs are repaired by the same traversal below; new files inherit Users Modify.
foreach (var name in new[] { "Logs", "Locks" })
{
var directory = Path.Combine(rootDirectory, name);
try { Directory.CreateDirectory(directory); }
catch (Exception ex) when (IsRepairException(ex))
{
AddFailure(ref failedCount, failures, directory, ex);
}
}
pendingDirectories.Push(rootDirectory);
while (pendingDirectories.Count > 0)
{
cancellationToken.ThrowIfCancellationRequested();
var currentDirectory = pendingDirectories.Pop();
foreach (var filePath in EnumerateFileSystemEntries(
() => Directory.EnumerateFiles(currentDirectory, "*", SearchOption.TopDirectoryOnly),
currentDirectory,
"files",
ref failedCount,
failures))
{
cancellationToken.ThrowIfCancellationRequested();
if (TryRepairFile(filePath, ref repairedCount, ref failedCount, failures))
{
continue;
}
}
foreach (var childDirectory in EnumerateFileSystemEntries(
() => Directory.EnumerateDirectories(currentDirectory, "*", SearchOption.TopDirectoryOnly),
currentDirectory,
"directories",
ref failedCount,
failures))
{
cancellationToken.ThrowIfCancellationRequested();
if (TryRepairDirectory(childDirectory, ref repairedCount, ref failedCount, failures))
{
pendingDirectories.Push(childDirectory);
}
}
}
var detail = failedCount == 0
? $"Runtime data ACL repaired. Root={rootDirectory}, RepairedItems={repairedCount}, FailedItems=0."
: $"Runtime data ACL repaired with warnings. Root={rootDirectory}, RepairedItems={repairedCount}, FailedItems={failedCount}, FirstFailures={string.Join(" || ", failures)}.";
return new RuntimeDataAclRepairResult(
true,
failedCount == 0 ? "OK" : "OK_WITH_WARNINGS",
detail);
}
private static bool TryRepairDirectory(
string directoryPath,
ref int repairedCount,
ref int failedCount,
List<string> failures)
{
try
{
RepairDirectoryAcl(directoryPath);
repairedCount++;
return true;
}
catch (Exception ex) when (IsRepairException(ex))
{
AddFailure(ref failedCount, failures, directoryPath, ex);
return false;
}
}
private static bool TryRepairFile(
string filePath,
ref int repairedCount,
ref int failedCount,
List<string> failures)
{
try
{
RepairFileAcl(filePath);
repairedCount++;
return true;
}
catch (Exception ex) when (IsRepairException(ex))
{
AddFailure(ref failedCount, failures, filePath, ex);
return false;
}
}
private static void RepairDirectoryAcl(string directoryPath)
{
var directoryInfo = new DirectoryInfo(directoryPath);
var security = directoryInfo.GetAccessControl(AccessControlSections.Access);
security.SetAccessRuleProtection(isProtected: false, preserveInheritance: true);
RemoveBroadUserWriteDenies(security);
security.SetAccessRule(CreateDirectoryUsersModifyRule());
directoryInfo.SetAccessControl(security);
}
private static void RepairFileAcl(string filePath)
{
var fileInfo = new FileInfo(filePath);
var security = fileInfo.GetAccessControl(AccessControlSections.Access);
security.SetAccessRuleProtection(isProtected: false, preserveInheritance: true);
RemoveBroadUserWriteDenies(security);
security.SetAccessRule(CreateFileUsersModifyRule());
fileInfo.SetAccessControl(security);
}
private static FileSystemAccessRule CreateDirectoryUsersModifyRule()
=> new(
BuiltinUsersSid,
FileSystemRights.Modify | FileSystemRights.Synchronize,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
private static FileSystemAccessRule CreateFileUsersModifyRule()
=> new(
BuiltinUsersSid,
FileSystemRights.Modify | FileSystemRights.Synchronize,
InheritanceFlags.None,
PropagationFlags.None,
AccessControlType.Allow);
private static void RemoveBroadUserWriteDenies(FileSystemSecurity security)
{
foreach (FileSystemAccessRule rule in security.GetAccessRules(includeExplicit: true, includeInherited: false, typeof(SecurityIdentifier)))
{
if (rule.AccessControlType != AccessControlType.Deny
|| !IsBroadUserSid((SecurityIdentifier)rule.IdentityReference)
|| !DeniesWrite(rule.FileSystemRights))
{
continue;
}
security.RemoveAccessRuleSpecific(rule);
}
}
private static bool IsBroadUserSid(SecurityIdentifier sid)
=> sid.Equals(BuiltinUsersSid)
|| sid.Equals(AuthenticatedUsersSid)
|| sid.Equals(WorldSid);
private static bool DeniesWrite(FileSystemRights rights)
{
const FileSystemRights writeRights =
FileSystemRights.Write
| FileSystemRights.Modify
| FileSystemRights.FullControl
| FileSystemRights.Delete
| FileSystemRights.DeleteSubdirectoriesAndFiles
| FileSystemRights.ChangePermissions
| FileSystemRights.TakeOwnership;
return (rights & writeRights) != 0;
}
private static IEnumerable<string> EnumerateFileSystemEntries(
Func<IEnumerable<string>> enumerate,
string directoryPath,
string entryKind,
ref int failedCount,
List<string> failures)
{
try
{
return enumerate().ToArray();
}
catch (Exception ex) when (IsRepairException(ex))
{
AddFailure(ref failedCount, failures, $"{directoryPath} ({entryKind})", ex);
return [];
}
}
private static void AddFailure(ref int failedCount, List<string> failures, string path, Exception exception)
{
failedCount++;
if (failures.Count >= MaxFailureDetails)
{
return;
}
failures.Add($"{path}: {exception.GetType().Name}: {exception.Message}");
}
private static bool IsRepairException(Exception exception)
=> exception is UnauthorizedAccessException
or IOException
or SecurityException;
}