Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ObjectSecurity.cs
Go to the documentation of this file.
4
6
7public abstract class ObjectSecurity
8{
9 private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
10
12
13 private bool _ownerModified;
14
15 private bool _groupModified;
16
17 private bool _saclModified;
18
19 private bool _daclModified;
20
22
23 protected bool OwnerModified
24 {
25 get
26 {
28 {
30 }
31 return _ownerModified;
32 }
33 set
34 {
36 {
38 }
40 }
41 }
42
43 protected bool GroupModified
44 {
45 get
46 {
48 {
50 }
51 return _groupModified;
52 }
53 set
54 {
56 {
58 }
60 }
61 }
62
63 protected bool AuditRulesModified
64 {
65 get
66 {
68 {
70 }
71 return _saclModified;
72 }
73 set
74 {
76 {
78 }
80 }
81 }
82
83 protected bool AccessRulesModified
84 {
85 get
86 {
88 {
90 }
91 return _daclModified;
92 }
93 set
94 {
96 {
98 }
100 }
101 }
102
104
105 protected bool IsDS => _securityDescriptor.IsDS;
106
108 {
109 get
110 {
111 ReadLock();
112 try
113 {
114 return (_securityDescriptor.ControlFlags & ControlFlags.DiscretionaryAclProtected) != 0;
115 }
116 finally
117 {
118 ReadUnlock();
119 }
120 }
121 }
122
124 {
125 get
126 {
127 ReadLock();
128 try
129 {
130 return (_securityDescriptor.ControlFlags & ControlFlags.SystemAclProtected) != 0;
131 }
132 finally
133 {
134 ReadUnlock();
135 }
136 }
137 }
138
140 {
141 get
142 {
143 ReadLock();
144 try
145 {
147 }
148 finally
149 {
150 ReadUnlock();
151 }
152 }
153 }
154
156 {
157 get
158 {
159 ReadLock();
160 try
161 {
163 }
164 finally
165 {
166 ReadUnlock();
167 }
168 }
169 }
170
171 public abstract Type AccessRightType { get; }
172
173 public abstract Type AccessRuleType { get; }
174
175 public abstract Type AuditRuleType { get; }
176
177 protected ObjectSecurity()
178 {
179 }
180
181 protected ObjectSecurity(bool isContainer, bool isDS)
182 : this()
183 {
184 DiscretionaryAcl discretionaryAcl = new DiscretionaryAcl(isContainer, isDS, 5);
185 _securityDescriptor = new CommonSecurityDescriptor(isContainer, isDS, ControlFlags.None, null, null, null, discretionaryAcl);
186 }
187
188 protected ObjectSecurity(CommonSecurityDescriptor securityDescriptor)
189 : this()
190 {
191 if (securityDescriptor == null)
192 {
193 throw new ArgumentNullException("securityDescriptor");
194 }
195 _securityDescriptor = securityDescriptor;
196 }
197
199 {
200 if ((includeSections & AccessControlSections.Owner) != 0)
201 {
202 _ownerModified = true;
203 _securityDescriptor.Owner = newOne.Owner;
204 }
205 if ((includeSections & AccessControlSections.Group) != 0)
206 {
207 _groupModified = true;
208 _securityDescriptor.Group = newOne.Group;
209 }
210 if ((includeSections & AccessControlSections.Audit) != 0)
211 {
212 _saclModified = true;
213 if (newOne.SystemAcl != null)
214 {
215 _securityDescriptor.SystemAcl = new SystemAcl(IsContainer, IsDS, newOne.SystemAcl, trusted: true);
216 }
217 else
218 {
219 _securityDescriptor.SystemAcl = null;
220 }
221 _securityDescriptor.UpdateControlFlags(ControlFlags.SystemAclPresent | ControlFlags.SystemAclAutoInherited | ControlFlags.SystemAclProtected, newOne.ControlFlags & (ControlFlags.SystemAclPresent | ControlFlags.SystemAclAutoInherited | ControlFlags.SystemAclProtected));
222 }
223 if ((includeSections & AccessControlSections.Access) != 0)
224 {
225 _daclModified = true;
226 if (newOne.DiscretionaryAcl != null)
227 {
228 _securityDescriptor.DiscretionaryAcl = new DiscretionaryAcl(IsContainer, IsDS, newOne.DiscretionaryAcl, trusted: true);
229 }
230 else
231 {
232 _securityDescriptor.DiscretionaryAcl = null;
233 }
234 ControlFlags controlFlags = _securityDescriptor.ControlFlags & ControlFlags.DiscretionaryAclPresent;
235 _securityDescriptor.UpdateControlFlags(ControlFlags.DiscretionaryAclPresent | ControlFlags.DiscretionaryAclAutoInherited | ControlFlags.DiscretionaryAclProtected, (newOne.ControlFlags | controlFlags) & (ControlFlags.DiscretionaryAclPresent | ControlFlags.DiscretionaryAclAutoInherited | ControlFlags.DiscretionaryAclProtected));
236 }
237 }
238
239 protected void ReadLock()
240 {
242 }
243
244 protected void ReadUnlock()
245 {
247 }
248
249 protected void WriteLock()
250 {
252 }
253
254 protected void WriteUnlock()
255 {
257 }
258
259 protected virtual void Persist(string name, AccessControlSections includeSections)
260 {
262 }
263
264 protected virtual void Persist(bool enableOwnershipPrivilege, string name, AccessControlSections includeSections)
265 {
266 Privilege privilege = null;
267 try
268 {
269 if (enableOwnershipPrivilege)
270 {
271 privilege = new Privilege("SeTakeOwnershipPrivilege");
272 try
273 {
274 privilege.Enable();
275 }
277 {
278 }
279 }
280 Persist(name, includeSections);
281 }
282 catch
283 {
284 privilege?.Revert();
285 throw;
286 }
287 finally
288 {
289 privilege?.Revert();
290 }
291 }
292
293 protected virtual void Persist(SafeHandle handle, AccessControlSections includeSections)
294 {
296 }
297
298 public IdentityReference? GetOwner(Type targetType)
299 {
300 ReadLock();
301 try
302 {
303 if (_securityDescriptor.Owner == null)
304 {
305 return null;
306 }
307 return _securityDescriptor.Owner.Translate(targetType);
308 }
309 finally
310 {
311 ReadUnlock();
312 }
313 }
314
315 public void SetOwner(IdentityReference identity)
316 {
317 if (identity == null)
318 {
319 throw new ArgumentNullException("identity");
320 }
321 WriteLock();
322 try
323 {
324 _securityDescriptor.Owner = identity.Translate(typeof(SecurityIdentifier)) as SecurityIdentifier;
325 _ownerModified = true;
326 }
327 finally
328 {
329 WriteUnlock();
330 }
331 }
332
333 public IdentityReference? GetGroup(Type targetType)
334 {
335 ReadLock();
336 try
337 {
338 if (_securityDescriptor.Group == null)
339 {
340 return null;
341 }
342 return _securityDescriptor.Group.Translate(targetType);
343 }
344 finally
345 {
346 ReadUnlock();
347 }
348 }
349
350 public void SetGroup(IdentityReference identity)
351 {
352 if (identity == null)
353 {
354 throw new ArgumentNullException("identity");
355 }
356 WriteLock();
357 try
358 {
359 _securityDescriptor.Group = identity.Translate(typeof(SecurityIdentifier)) as SecurityIdentifier;
360 _groupModified = true;
361 }
362 finally
363 {
364 WriteUnlock();
365 }
366 }
367
368 public virtual void PurgeAccessRules(IdentityReference identity)
369 {
370 if (identity == null)
371 {
372 throw new ArgumentNullException("identity");
373 }
374 WriteLock();
375 try
376 {
378 _daclModified = true;
379 }
380 finally
381 {
382 WriteUnlock();
383 }
384 }
385
386 public virtual void PurgeAuditRules(IdentityReference identity)
387 {
388 if (identity == null)
389 {
390 throw new ArgumentNullException("identity");
391 }
392 WriteLock();
393 try
394 {
396 _saclModified = true;
397 }
398 finally
399 {
400 WriteUnlock();
401 }
402 }
403
404 public void SetAccessRuleProtection(bool isProtected, bool preserveInheritance)
405 {
406 WriteLock();
407 try
408 {
409 _securityDescriptor.SetDiscretionaryAclProtection(isProtected, preserveInheritance);
410 _daclModified = true;
411 }
412 finally
413 {
414 WriteUnlock();
415 }
416 }
417
418 public void SetAuditRuleProtection(bool isProtected, bool preserveInheritance)
419 {
420 WriteLock();
421 try
422 {
423 _securityDescriptor.SetSystemAclProtection(isProtected, preserveInheritance);
424 _saclModified = true;
425 }
426 finally
427 {
428 WriteUnlock();
429 }
430 }
431
432 public static bool IsSddlConversionSupported()
433 {
434 return true;
435 }
436
438 {
439 ReadLock();
440 try
441 {
442 return _securityDescriptor.GetSddlForm(includeSections);
443 }
444 finally
445 {
446 ReadUnlock();
447 }
448 }
449
450 public void SetSecurityDescriptorSddlForm(string sddlForm)
451 {
453 }
454
455 public void SetSecurityDescriptorSddlForm(string sddlForm, AccessControlSections includeSections)
456 {
457 if (sddlForm == null)
458 {
459 throw new ArgumentNullException("sddlForm");
460 }
461 if ((includeSections & AccessControlSections.All) == 0)
462 {
463 throw new ArgumentException(System.SR.Arg_EnumAtLeastOneFlag, "includeSections");
464 }
465 WriteLock();
466 try
467 {
468 UpdateWithNewSecurityDescriptor(new RawSecurityDescriptor(sddlForm), includeSections);
469 }
470 finally
471 {
472 WriteUnlock();
473 }
474 }
475
477 {
478 ReadLock();
479 try
480 {
481 byte[] array = new byte[_securityDescriptor.BinaryLength];
483 return array;
484 }
485 finally
486 {
487 ReadUnlock();
488 }
489 }
490
491 public void SetSecurityDescriptorBinaryForm(byte[] binaryForm)
492 {
494 }
495
496 public void SetSecurityDescriptorBinaryForm(byte[] binaryForm, AccessControlSections includeSections)
497 {
498 if (binaryForm == null)
499 {
500 throw new ArgumentNullException("binaryForm");
501 }
502 if ((includeSections & AccessControlSections.All) == 0)
503 {
504 throw new ArgumentException(System.SR.Arg_EnumAtLeastOneFlag, "includeSections");
505 }
506 WriteLock();
507 try
508 {
509 UpdateWithNewSecurityDescriptor(new RawSecurityDescriptor(binaryForm, 0), includeSections);
510 }
511 finally
512 {
513 WriteUnlock();
514 }
515 }
516
517 protected abstract bool ModifyAccess(AccessControlModification modification, AccessRule rule, out bool modified);
518
519 protected abstract bool ModifyAudit(AccessControlModification modification, AuditRule rule, out bool modified);
520
521 public virtual bool ModifyAccessRule(AccessControlModification modification, AccessRule rule, out bool modified)
522 {
523 if (rule == null)
524 {
525 throw new ArgumentNullException("rule");
526 }
527 if (!AccessRuleType.IsAssignableFrom(rule.GetType()))
528 {
530 }
531 WriteLock();
532 try
533 {
534 return ModifyAccess(modification, rule, out modified);
535 }
536 finally
537 {
538 WriteUnlock();
539 }
540 }
541
542 public virtual bool ModifyAuditRule(AccessControlModification modification, AuditRule rule, out bool modified)
543 {
544 if (rule == null)
545 {
546 throw new ArgumentNullException("rule");
547 }
548 if (!AuditRuleType.IsAssignableFrom(rule.GetType()))
549 {
551 }
552 WriteLock();
553 try
554 {
555 return ModifyAudit(modification, rule, out modified);
556 }
557 finally
558 {
559 WriteUnlock();
560 }
561 }
562
563 public abstract AccessRule AccessRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type);
564
565 public abstract AuditRule AuditRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags);
566}
567public abstract class ObjectSecurity<T> : NativeObjectSecurity where T : struct
568{
569 public override Type AccessRightType => typeof(T);
570
571 public override Type AccessRuleType => typeof(AccessRule<T>);
572
573 public override Type AuditRuleType => typeof(AuditRule<T>);
574
575 protected ObjectSecurity(bool isContainer, ResourceType resourceType)
576 : base(isContainer, resourceType, null, null)
577 {
578 }
579
580 protected ObjectSecurity(bool isContainer, ResourceType resourceType, string? name, AccessControlSections includeSections)
581 : base(isContainer, resourceType, name, includeSections, null, null)
582 {
583 }
584
585 protected ObjectSecurity(bool isContainer, ResourceType resourceType, string? name, AccessControlSections includeSections, ExceptionFromErrorCode? exceptionFromErrorCode, object? exceptionContext)
586 : base(isContainer, resourceType, name, includeSections, exceptionFromErrorCode, exceptionContext)
587 {
588 }
589
590 protected ObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle? safeHandle, AccessControlSections includeSections)
591 : base(isContainer, resourceType, safeHandle, includeSections, null, null)
592 {
593 }
594
595 protected ObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle? safeHandle, AccessControlSections includeSections, ExceptionFromErrorCode? exceptionFromErrorCode, object? exceptionContext)
596 : base(isContainer, resourceType, safeHandle, includeSections, exceptionFromErrorCode, exceptionContext)
597 {
598 }
599
600 public override AccessRule AccessRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
601 {
602 return new AccessRule<T>(identityReference, accessMask, isInherited, inheritanceFlags, propagationFlags, type);
603 }
604
605 public override AuditRule AuditRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags)
606 {
607 return new AuditRule<T>(identityReference, accessMask, isInherited, inheritanceFlags, propagationFlags, flags);
608 }
609
611 {
612 AccessControlSections accessControlSections = AccessControlSections.None;
613 if (base.AccessRulesModified)
614 {
615 accessControlSections = AccessControlSections.Access;
616 }
617 if (base.AuditRulesModified)
618 {
619 accessControlSections |= AccessControlSections.Audit;
620 }
621 if (base.OwnerModified)
622 {
623 accessControlSections |= AccessControlSections.Owner;
624 }
625 if (base.GroupModified)
626 {
627 accessControlSections |= AccessControlSections.Group;
628 }
629 return accessControlSections;
630 }
631
632 protected internal void Persist(SafeHandle handle)
633 {
634 WriteLock();
635 try
636 {
637 AccessControlSections accessControlSectionsFromChanges = GetAccessControlSectionsFromChanges();
638 Persist(handle, accessControlSectionsFromChanges);
639 bool flag2 = (base.AccessRulesModified = false);
640 bool flag4 = (base.AuditRulesModified = flag2);
641 bool ownerModified = (base.GroupModified = flag4);
642 base.OwnerModified = ownerModified;
643 }
644 finally
645 {
646 WriteUnlock();
647 }
648 }
649
650 protected internal void Persist(string name)
651 {
652 WriteLock();
653 try
654 {
655 AccessControlSections accessControlSectionsFromChanges = GetAccessControlSectionsFromChanges();
656 Persist(name, accessControlSectionsFromChanges);
657 bool flag2 = (base.AccessRulesModified = false);
658 bool flag4 = (base.AuditRulesModified = flag2);
659 bool ownerModified = (base.GroupModified = flag4);
660 base.OwnerModified = ownerModified;
661 }
662 finally
663 {
664 WriteUnlock();
665 }
666 }
667
668 public virtual void AddAccessRule(AccessRule<T> rule)
669 {
671 }
672
673 public virtual void SetAccessRule(AccessRule<T> rule)
674 {
676 }
677
678 public virtual void ResetAccessRule(AccessRule<T> rule)
679 {
681 }
682
683 public virtual bool RemoveAccessRule(AccessRule<T> rule)
684 {
685 return RemoveAccessRule((AccessRule)rule);
686 }
687
688 public virtual void RemoveAccessRuleAll(AccessRule<T> rule)
689 {
691 }
692
693 public virtual void RemoveAccessRuleSpecific(AccessRule<T> rule)
694 {
696 }
697
698 public virtual void AddAuditRule(AuditRule<T> rule)
699 {
700 AddAuditRule((AuditRule)rule);
701 }
702
703 public virtual void SetAuditRule(AuditRule<T> rule)
704 {
705 SetAuditRule((AuditRule)rule);
706 }
707
708 public virtual bool RemoveAuditRule(AuditRule<T> rule)
709 {
710 return RemoveAuditRule((AuditRule)rule);
711 }
712
713 public virtual void RemoveAuditRuleAll(AuditRule<T> rule)
714 {
716 }
717
718 public virtual void RemoveAuditRuleSpecific(AuditRule<T> rule)
719 {
721 }
722}
static Exception ByDesign
static string AccessControl_InvalidAccessRuleType
Definition SR.cs:16
static string AccessControl_InvalidAuditRuleType
Definition SR.cs:18
static string InvalidOperation_MustLockForReadOrWrite
Definition SR.cs:86
static string InvalidOperation_MustLockForWrite
Definition SR.cs:88
static string Arg_EnumAtLeastOneFlag
Definition SR.cs:44
Definition SR.cs:7
void UpdateControlFlags(ControlFlags flagsToUpdate, ControlFlags newFlags)
void SetDiscretionaryAclProtection(bool isProtected, bool preserveInheritance)
void SetSystemAclProtection(bool isProtected, bool preserveInheritance)
string GetSddlForm(AccessControlSections includeSections)
virtual void AddAuditRule(AuditRule< T > rule)
virtual void ResetAccessRule(AccessRule< T > rule)
override AuditRule AuditRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags)
virtual void RemoveAccessRuleSpecific(AccessRule< T > rule)
virtual bool ModifyAccessRule(AccessControlModification modification, AccessRule rule, out bool modified)
virtual void PurgeAuditRules(IdentityReference identity)
virtual void SetAuditRule(AuditRule< T > rule)
virtual bool ModifyAuditRule(AccessControlModification modification, AuditRule rule, out bool modified)
IdentityReference? GetOwner(Type targetType)
ObjectSecurity(CommonSecurityDescriptor securityDescriptor)
AuditRule AuditRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags)
void SetOwner(IdentityReference identity)
virtual bool RemoveAuditRule(AuditRule< T > rule)
virtual void RemoveAuditRuleAll(AuditRule< T > rule)
bool ModifyAudit(AccessControlModification modification, AuditRule rule, out bool modified)
IdentityReference? GetGroup(Type targetType)
void UpdateWithNewSecurityDescriptor(RawSecurityDescriptor newOne, AccessControlSections includeSections)
ObjectSecurity(bool isContainer, ResourceType resourceType)
AccessRule AccessRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
readonly CommonSecurityDescriptor _securityDescriptor
void SetSecurityDescriptorBinaryForm(byte[] binaryForm)
virtual void PurgeAccessRules(IdentityReference identity)
void SetSecurityDescriptorBinaryForm(byte[] binaryForm, AccessControlSections includeSections)
readonly ReaderWriterLockSlim _lock
ObjectSecurity(bool isContainer, ResourceType resourceType, string? name, AccessControlSections includeSections, ExceptionFromErrorCode? exceptionFromErrorCode, object? exceptionContext)
virtual void Persist(bool enableOwnershipPrivilege, string name, AccessControlSections includeSections)
virtual void AddAccessRule(AccessRule< T > rule)
virtual bool RemoveAccessRule(AccessRule< T > rule)
bool ModifyAccess(AccessControlModification modification, AccessRule rule, out bool modified)
void SetAuditRuleProtection(bool isProtected, bool preserveInheritance)
ObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle? safeHandle, AccessControlSections includeSections, ExceptionFromErrorCode? exceptionFromErrorCode, object? exceptionContext)
void SetAccessRuleProtection(bool isProtected, bool preserveInheritance)
virtual void SetAccessRule(AccessRule< T > rule)
virtual void RemoveAuditRuleSpecific(AuditRule< T > rule)
virtual void Persist(SafeHandle handle, AccessControlSections includeSections)
virtual void Persist(string name, AccessControlSections includeSections)
override AccessRule AccessRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
AccessControlSections GetAccessControlSectionsFromChanges()
void SetGroup(IdentityReference identity)
virtual void RemoveAccessRuleAll(AccessRule< T > rule)
ObjectSecurity(bool isContainer, ResourceType resourceType, SafeHandle? safeHandle, AccessControlSections includeSections)
string GetSecurityDescriptorSddlForm(AccessControlSections includeSections)
ObjectSecurity(bool isContainer, bool isDS)
ObjectSecurity(bool isContainer, ResourceType resourceType, string? name, AccessControlSections includeSections)
void SetSecurityDescriptorSddlForm(string sddlForm, AccessControlSections includeSections)
IdentityReference Translate(Type targetType)
override IdentityReference Translate(Type targetType)
virtual bool IsAssignableFrom([NotNullWhen(true)] Type? c)
Definition Type.cs:1561