Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NativeObjectSecurity.cs
Go to the documentation of this file.
1using System.IO;
4
6
8{
9 protected internal delegate Exception? ExceptionFromErrorCode(int errorCode, string? name, SafeHandle? handle, object? context);
10
11 private readonly ResourceType _resourceType;
12
14
15 private readonly object _exceptionContext;
16
17 private readonly uint ProtectedDiscretionaryAcl = 2147483648u;
18
19 private readonly uint ProtectedSystemAcl = 1073741824u;
20
21 private readonly uint UnprotectedDiscretionaryAcl = 536870912u;
22
23 private readonly uint UnprotectedSystemAcl = 268435456u;
24
25 protected NativeObjectSecurity(bool isContainer, ResourceType resourceType)
26 : base(isContainer)
27 {
28 _resourceType = resourceType;
29 }
30
31 protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, ExceptionFromErrorCode? exceptionFromErrorCode, object? exceptionContext)
32 : this(isContainer, resourceType)
33 {
34 _exceptionContext = exceptionContext;
35 _exceptionFromErrorCode = exceptionFromErrorCode;
36 }
37
38 internal NativeObjectSecurity(ResourceType resourceType, CommonSecurityDescriptor securityDescriptor, ExceptionFromErrorCode exceptionFromErrorCode)
39 : base(securityDescriptor)
40 {
41 _resourceType = resourceType;
42 _exceptionFromErrorCode = exceptionFromErrorCode;
43 }
44
45 protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, string? name, AccessControlSections includeSections, ExceptionFromErrorCode? exceptionFromErrorCode, object? exceptionContext)
46 : this(resourceType, CreateInternal(resourceType, isContainer, name, null, includeSections, createByName: true, exceptionFromErrorCode, exceptionContext), exceptionFromErrorCode)
47 {
48 }
49
50 protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, string? name, AccessControlSections includeSections)
51 : this(isContainer, resourceType, name, includeSections, null, null)
52 {
53 }
54
55 protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle? handle, AccessControlSections includeSections, ExceptionFromErrorCode? exceptionFromErrorCode, object? exceptionContext)
56 : this(resourceType, CreateInternal(resourceType, isContainer, null, handle, includeSections, createByName: false, exceptionFromErrorCode, exceptionContext), exceptionFromErrorCode)
57 {
58 }
59
60 protected NativeObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle? handle, AccessControlSections includeSections)
61 : this(isContainer, resourceType, handle, includeSections, null, null)
62 {
63 }
64
65 private static CommonSecurityDescriptor CreateInternal(ResourceType resourceType, bool isContainer, string name, SafeHandle handle, AccessControlSections includeSections, bool createByName, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext)
66 {
67 if (createByName && name == null)
68 {
69 throw new ArgumentNullException("name");
70 }
71 if (!createByName && handle == null)
72 {
73 throw new ArgumentNullException("handle");
74 }
75 RawSecurityDescriptor resultSd;
76 int securityInfo = Win32.GetSecurityInfo(resourceType, name, handle, includeSections, out resultSd);
77 if (securityInfo != 0)
78 {
79 Exception ex = null;
80 if (exceptionFromErrorCode != null)
81 {
82 ex = exceptionFromErrorCode(securityInfo, name, handle, exceptionContext);
83 }
84 if (ex == null)
85 {
86 switch (securityInfo)
87 {
88 case 5:
90 break;
91 case 1307:
93 break;
94 case 1308:
96 break;
97 case 87:
99 break;
100 case 123:
102 break;
103 case 2:
104 ex = new FileNotFoundException(name);
105 break;
106 case 3:
107 {
108 Exception ex2 = (isContainer ? ((IOException)new DirectoryNotFoundException(name)) : ((IOException)new FileNotFoundException(name)));
109 ex = ex2;
110 break;
111 }
112 case 1350:
114 break;
115 case 233:
117 break;
118 default:
120 break;
121 }
122 }
123 throw ex;
124 }
125 return new CommonSecurityDescriptor(isContainer, isDS: false, resultSd, trusted: true);
126 }
127
128 private void Persist(string name, SafeHandle handle, AccessControlSections includeSections, object exceptionContext)
129 {
130 WriteLock();
131 try
132 {
133 SecurityInfos securityInfos = (SecurityInfos)0;
134 SecurityIdentifier owner = null;
135 SecurityIdentifier group = null;
136 SystemAcl sacl = null;
137 DiscretionaryAcl dacl = null;
138 if ((includeSections & AccessControlSections.Owner) != 0 && _securityDescriptor.Owner != null)
139 {
140 securityInfos |= SecurityInfos.Owner;
142 }
143 if ((includeSections & AccessControlSections.Group) != 0 && _securityDescriptor.Group != null)
144 {
145 securityInfos |= SecurityInfos.Group;
147 }
148 if ((includeSections & AccessControlSections.Audit) != 0)
149 {
150 securityInfos |= SecurityInfos.SystemAcl;
151 sacl = ((!_securityDescriptor.IsSystemAclPresent || _securityDescriptor.SystemAcl == null || _securityDescriptor.SystemAcl.Count <= 0) ? null : _securityDescriptor.SystemAcl);
152 securityInfos = (SecurityInfos)(((_securityDescriptor.ControlFlags & ControlFlags.SystemAclProtected) == 0) ? ((int)securityInfos | (int)UnprotectedSystemAcl) : ((int)securityInfos | (int)ProtectedSystemAcl));
153 }
154 if ((includeSections & AccessControlSections.Access) != 0 && _securityDescriptor.IsDiscretionaryAclPresent)
155 {
156 securityInfos |= SecurityInfos.DiscretionaryAcl;
158 securityInfos = (SecurityInfos)(((_securityDescriptor.ControlFlags & ControlFlags.DiscretionaryAclProtected) == 0) ? ((int)securityInfos | (int)UnprotectedDiscretionaryAcl) : ((int)securityInfos | (int)ProtectedDiscretionaryAcl));
159 }
160 if (securityInfos == (SecurityInfos)0)
161 {
162 return;
163 }
164 int num = Win32.SetSecurityInfo(_resourceType, name, handle, securityInfos, owner, group, sacl, dacl);
165 if (num != 0)
166 {
167 Exception ex = null;
168 if (_exceptionFromErrorCode != null)
169 {
170 ex = _exceptionFromErrorCode(num, name, handle, exceptionContext);
171 }
172 if (ex == null)
173 {
174 ex = num switch
175 {
176 5 => new UnauthorizedAccessException(),
179 123 => new ArgumentException(System.SR.Argument_InvalidName, "name"),
181 2 => new FileNotFoundException(),
184 };
185 }
186 throw ex;
187 }
188 base.OwnerModified = false;
189 base.GroupModified = false;
190 base.AccessRulesModified = false;
191 base.AuditRulesModified = false;
192 }
193 finally
194 {
195 WriteUnlock();
196 }
197 }
198
199 protected sealed override void Persist(string name, AccessControlSections includeSections)
200 {
201 Persist(name, includeSections, _exceptionContext);
202 }
203
204 protected void Persist(string name, AccessControlSections includeSections, object? exceptionContext)
205 {
206 if (name == null)
207 {
208 throw new ArgumentNullException("name");
209 }
210 Persist(name, null, includeSections, exceptionContext);
211 }
212
213 protected sealed override void Persist(SafeHandle handle, AccessControlSections includeSections)
214 {
215 Persist(handle, includeSections, _exceptionContext);
216 }
217
218 protected void Persist(SafeHandle handle, AccessControlSections includeSections, object? exceptionContext)
219 {
220 if (handle == null)
221 {
222 throw new ArgumentNullException("handle");
223 }
224 Persist(null, handle, includeSections, exceptionContext);
225 }
226}
static string AccessControl_UnexpectedError
Definition SR.cs:42
static string AccessControl_InvalidOwner
Definition SR.cs:20
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_InvalidName
Definition SR.cs:20
static string AccessControl_InvalidHandle
Definition SR.cs:14
static string InvalidOperation_DisconnectedPipe
Definition SR.cs:96
static string AccessControl_InvalidGroup
Definition SR.cs:22
static string AccessControl_NoAssociatedSecurity
Definition SR.cs:40
Definition SR.cs:7
NativeObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle? handle, AccessControlSections includeSections)
void Persist(string name, SafeHandle handle, AccessControlSections includeSections, object exceptionContext)
NativeObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle? handle, AccessControlSections includeSections, ExceptionFromErrorCode? exceptionFromErrorCode, object? exceptionContext)
NativeObjectSecurity(bool isContainer, ResourceType resourceType, ExceptionFromErrorCode? exceptionFromErrorCode, object? exceptionContext)
NativeObjectSecurity(bool isContainer, ResourceType resourceType, string? name, AccessControlSections includeSections)
void Persist(string name, AccessControlSections includeSections, object? exceptionContext)
static CommonSecurityDescriptor CreateInternal(ResourceType resourceType, bool isContainer, string name, SafeHandle handle, AccessControlSections includeSections, bool createByName, ExceptionFromErrorCode exceptionFromErrorCode, object exceptionContext)
void Persist(SafeHandle handle, AccessControlSections includeSections, object? exceptionContext)
NativeObjectSecurity(ResourceType resourceType, CommonSecurityDescriptor securityDescriptor, ExceptionFromErrorCode exceptionFromErrorCode)
override void Persist(string name, AccessControlSections includeSections)
delegate? Exception ExceptionFromErrorCode(int errorCode, string? name, SafeHandle? handle, object? context)
NativeObjectSecurity(bool isContainer, ResourceType resourceType)
override void Persist(SafeHandle handle, AccessControlSections includeSections)
NativeObjectSecurity(bool isContainer, ResourceType resourceType, string? name, AccessControlSections includeSections, ExceptionFromErrorCode? exceptionFromErrorCode, object? exceptionContext)
readonly CommonSecurityDescriptor _securityDescriptor
static int GetSecurityInfo(ResourceType resourceType, string name, SafeHandle handle, AccessControlSections accessControlSections, out RawSecurityDescriptor resultSd)
Definition Win32.cs:26
static int SetSecurityInfo(ResourceType type, string name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dacl)
Definition Win32.cs:123