The Labs \ Source Viewer \ SSCLI \ System.Security.AccessControl \ SemaphoreSecurity

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*============================================================
  16. **
  17. ** Class:  SemaphoreSecurity
  18. **
  19. ** Purpose: Managed ACL wrapper for Win32 semaphores.
  20. **
  21. ** Date:  November 26, 2003
  22. **
  23. ===========================================================*/
  24. using System;
  25. using System.Collections;
  26. using System.Security.Permissions;
  27. using System.Security.Principal;
  28. using Microsoft.Win32;
  29. using Microsoft.Win32.SafeHandles;
  30. using System.Runtime.InteropServices;
  31. using System.IO;
  32. using System.Threading;
  33. namespace System.Security.AccessControl
  34. {
  35.     // Derive this list of values from winnt.h and MSDN docs:
  36.     // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/synchronization_object_security_and_access_rights.asp
  37.    
  38.     // Win32's interesting values are SEMAPHORE_MODIFY_STATE (0x2) and
  39.     // SEMAPHORE_ALL_ACCESS (0x1F0003). I don't know what 0x1 is.
  40.     [Flags(), ComVisible(false)]
  41.     public enum SemaphoreRights
  42.     {
  43.         Modify = 2,
  44.         Delete = 65536,
  45.         ReadPermissions = 131072,
  46.         ChangePermissions = 262144,
  47.         TakeOwnership = 524288,
  48.         Synchronize = 1048576,
  49.         // SYNCHRONIZE
  50.         FullControl = 2031619
  51.     }
  52.    
  53.     [ComVisible(false)]
  54.     public sealed class SemaphoreAccessRule : AccessRule
  55.     {
  56.         // Constructor for creating access rules for registry objects
  57.        
  58.         public SemaphoreAccessRule(IdentityReference identity, SemaphoreRights eventRights, AccessControlType type) : this(identity, (int)eventRights, false, InheritanceFlags.None, PropagationFlags.None, type)
  59.         {
  60.         }
  61.        
  62.         public SemaphoreAccessRule(string identity, SemaphoreRights eventRights, AccessControlType type) : this(new NTAccount(identity), (int)eventRights, false, InheritanceFlags.None, PropagationFlags.None, type)
  63.         {
  64.         }
  65.        
  66.         //
  67.         // Internal constructor to be called by public constructors
  68.         // and the access rule factory methods of {File|Folder}Security
  69.         //
  70.         internal SemaphoreAccessRule(IdentityReference identity, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type) : base(identity, accessMask, isInherited, inheritanceFlags, propagationFlags, type)
  71.         {
  72.         }
  73.        
  74.         public SemaphoreRights SemaphoreRights {
  75.             get { return (SemaphoreRights)base.AccessMask; }
  76.         }
  77.     }
  78.    
  79.     [ComVisible(false)]
  80.     public sealed class SemaphoreAuditRule : AuditRule
  81.     {
  82.         public SemaphoreAuditRule(IdentityReference identity, SemaphoreRights eventRights, AuditFlags flags) : this(identity, (int)eventRights, false, InheritanceFlags.None, PropagationFlags.None, flags)
  83.         {
  84.         }
  85.        
  86. /*  // Not in the spec
  87.         public SemaphoreAuditRule(string identity, SemaphoreRights eventRights, AuditFlags flags)
  88.             : this(new NTAccount(identity), (int) eventRights, false, InheritanceFlags.None, PropagationFlags.None, flags)
  89.         {
  90.         }
  91.         */       
  92.        
  93.         internal SemaphoreAuditRule(IdentityReference identity, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags) : base(identity, accessMask, isInherited, inheritanceFlags, propagationFlags, flags)
  94.         {
  95.         }
  96.        
  97.         public SemaphoreRights SemaphoreRights {
  98.             get { return (SemaphoreRights)base.AccessMask; }
  99.         }
  100.     }
  101.    
  102.     [ComVisible(false)]
  103.     public sealed class SemaphoreSecurity : NativeObjectSecurity
  104.     {
  105.         public SemaphoreSecurity() : base(true, ResourceType.KernelObject)
  106.         {
  107.         }
  108.        
  109.         public SemaphoreSecurity(string name, AccessControlSections includeSections) : base(true, ResourceType.KernelObject, name, includeSections, _HandleErrorCode, null)
  110.         {
  111.             // Let the underlying ACL API's demand unmanaged code permission.
  112.         }
  113.        
  114.         internal SemaphoreSecurity(SafeWaitHandle handle, AccessControlSections includeSections) : base(true, ResourceType.KernelObject, handle, includeSections, _HandleErrorCode, null)
  115.         {
  116.             // Let the underlying ACL API's demand unmanaged code permission.
  117.         }
  118.        
  119.         private static Exception _HandleErrorCode(int errorCode, string name, SafeHandle handle, object context)
  120.         {
  121.             System.Exception exception = null;
  122.            
  123.             switch (errorCode) {
  124.                 case NativeMethods.ERROR_INVALID_NAME:
  125.                 case NativeMethods.ERROR_INVALID_HANDLE:
  126.                 case NativeMethods.ERROR_FILE_NOT_FOUND:
  127.                     if ((name != null) && (name.Length != 0))
  128.                         exception = new WaitHandleCannotBeOpenedException(SR.GetString(SR.WaitHandleCannotBeOpenedException_InvalidHandle, name));
  129.                     else
  130.                         exception = new WaitHandleCannotBeOpenedException();
  131.                     break;
  132.                 default:
  133.                    
  134.                     break;
  135.             }
  136.            
  137.             return exception;
  138.         }
  139.        
  140.         public override AccessRule AccessRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
  141.         {
  142.             return new SemaphoreAccessRule(identityReference, accessMask, isInherited, inheritanceFlags, propagationFlags, type);
  143.         }
  144.        
  145.         public override AuditRule AuditRuleFactory(IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AuditFlags flags)
  146.         {
  147.             return new SemaphoreAuditRule(identityReference, accessMask, isInherited, inheritanceFlags, propagationFlags, flags);
  148.         }
  149.        
  150.         internal AccessControlSections GetAccessControlSectionsFromChanges()
  151.         {
  152.             AccessControlSections persistRules = AccessControlSections.None;
  153.             if (AccessRulesModified)
  154.                 persistRules = AccessControlSections.Access;
  155.             if (AuditRulesModified)
  156.                 persistRules |= AccessControlSections.Audit;
  157.             if (OwnerModified)
  158.                 persistRules |= AccessControlSections.Owner;
  159.             if (GroupModified)
  160.                 persistRules |= AccessControlSections.Group;
  161.             return persistRules;
  162.         }
  163.        
  164.         internal void Persist(SafeWaitHandle handle)
  165.         {
  166.             // Let the underlying ACL API's demand unmanaged code.
  167.            
  168.             WriteLock();
  169.            
  170.             try {
  171.                 AccessControlSections persistSections = GetAccessControlSectionsFromChanges();
  172.                 if (persistSections == AccessControlSections.None)
  173.                     return;
  174.                 // Don't need to persist anything.
  175.                 base.Persist(handle, persistSections);
  176.                 OwnerModified = GroupModified = AuditRulesModified = AccessRulesModified = false;
  177.             }
  178.             finally {
  179.                 WriteUnlock();
  180.             }
  181.         }
  182.        
  183.         public void AddAccessRule(SemaphoreAccessRule rule)
  184.         {
  185.             base.AddAccessRule(rule);
  186.         }
  187.        
  188.         public void SetAccessRule(SemaphoreAccessRule rule)
  189.         {
  190.             base.SetAccessRule(rule);
  191.         }
  192.        
  193.         public void ResetAccessRule(SemaphoreAccessRule rule)
  194.         {
  195.             base.ResetAccessRule(rule);
  196.         }
  197.        
  198.         public bool RemoveAccessRule(SemaphoreAccessRule rule)
  199.         {
  200.             return base.RemoveAccessRule(rule);
  201.         }
  202.        
  203.         public void RemoveAccessRuleAll(SemaphoreAccessRule rule)
  204.         {
  205.             base.RemoveAccessRuleAll(rule);
  206.         }
  207.        
  208.         public void RemoveAccessRuleSpecific(SemaphoreAccessRule rule)
  209.         {
  210.             base.RemoveAccessRuleSpecific(rule);
  211.         }
  212.        
  213.         public void AddAuditRule(SemaphoreAuditRule rule)
  214.         {
  215.             base.AddAuditRule(rule);
  216.         }
  217.        
  218.         public void SetAuditRule(SemaphoreAuditRule rule)
  219.         {
  220.             base.SetAuditRule(rule);
  221.         }
  222.        
  223.         public bool RemoveAuditRule(SemaphoreAuditRule rule)
  224.         {
  225.             return base.RemoveAuditRule(rule);
  226.         }
  227.        
  228.         public void RemoveAuditRuleAll(SemaphoreAuditRule rule)
  229.         {
  230.             base.RemoveAuditRuleAll(rule);
  231.         }
  232.        
  233.         public void RemoveAuditRuleSpecific(SemaphoreAuditRule rule)
  234.         {
  235.             base.RemoveAuditRuleSpecific(rule);
  236.         }
  237.        
  238.         public override Type AccessRightType {
  239.             get { return typeof(SemaphoreRights); }
  240.         }
  241.        
  242.         public override Type AccessRuleType {
  243.             get { return typeof(SemaphoreAccessRule); }
  244.         }
  245.        
  246.         public override Type AuditRuleType {
  247.             get { return typeof(SemaphoreAuditRule); }
  248.         }
  249.     }
  250. }

Developer Fusion