The Labs \ Source Viewer \ SSCLI \ System.Security.Policy \ FileCodeGroup

  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. // FileCodeGroup.cs
  17. //
  18. // Representation for code groups used for the policy mechanism.
  19. //
  20. namespace System.Security.Policy
  21. {
  22.     using System;
  23.     using System.Collections;
  24.     using System.Globalization;
  25.     using System.Security.Permissions;
  26.     using System.Security.Util;
  27.     using System.Runtime.Serialization;
  28.     using System.Runtime.Versioning;
  29.    
  30.     [Serializable()]
  31.     [System.Runtime.InteropServices.ComVisible(true)]
  32.     public sealed class FileCodeGroup : CodeGroup, IUnionSemanticCodeGroup
  33.     {
  34.         private FileIOPermissionAccess m_access;
  35.        
  36.         internal FileCodeGroup() : base()
  37.         {
  38.         }
  39.        
  40.         public FileCodeGroup(IMembershipCondition membershipCondition, FileIOPermissionAccess access) : base(membershipCondition, (PolicyStatement)null)
  41.         {
  42.             m_access = access;
  43.         }
  44.        
  45.         [ResourceExposure(ResourceScope.Machine)]
  46.         [ResourceConsumption(ResourceScope.Machine)]
  47.         public override PolicyStatement Resolve(Evidence evidence)
  48.         {
  49.             if (evidence == null)
  50.                 throw new ArgumentNullException("evidence");
  51.            
  52.             if (this.MembershipCondition.Check(evidence)) {
  53.                 PolicyStatement thisPolicy = CalculateAssemblyPolicy(evidence);
  54.                
  55.                 IEnumerator enumerator = this.Children.GetEnumerator();
  56.                 while (enumerator.MoveNext()) {
  57.                     PolicyStatement childPolicy = ((CodeGroup)enumerator.Current).Resolve(evidence);
  58.                    
  59.                     if (childPolicy != null) {
  60.                         if (((thisPolicy.Attributes & childPolicy.Attributes) & PolicyStatementAttribute.Exclusive) == PolicyStatementAttribute.Exclusive)
  61.                             throw new PolicyException(Environment.GetResourceString("Policy_MultipleExclusive"));
  62.                        
  63.                         thisPolicy.GetPermissionSetNoCopy().InplaceUnion(childPolicy.GetPermissionSetNoCopy());
  64.                         thisPolicy.Attributes = thisPolicy.Attributes | childPolicy.Attributes;
  65.                     }
  66.                 }
  67.                
  68.                 return thisPolicy;
  69.             }
  70.             else {
  71.                 return null;
  72.             }
  73.         }
  74.        
  75.         /// <internalonly/>
  76.         [ResourceExposure(ResourceScope.Machine)]
  77.         [ResourceConsumption(ResourceScope.Machine)]
  78.         PolicyStatement IUnionSemanticCodeGroup.InternalResolve(Evidence evidence)
  79.         {
  80.             if (evidence == null)
  81.                 throw new ArgumentNullException("evidence");
  82.            
  83.             if (this.MembershipCondition.Check(evidence)) {
  84.                 return CalculateAssemblyPolicy(evidence);
  85.             }
  86.            
  87.             return null;
  88.         }
  89.        
  90.         public override CodeGroup ResolveMatchingCodeGroups(Evidence evidence)
  91.         {
  92.             if (evidence == null)
  93.                 throw new ArgumentNullException("evidence");
  94.            
  95.             if (this.MembershipCondition.Check(evidence)) {
  96.                 CodeGroup retGroup = this.Copy();
  97.                 retGroup.Children = new ArrayList();
  98.                 IEnumerator enumerator = this.Children.GetEnumerator();
  99.                 while (enumerator.MoveNext()) {
  100.                     CodeGroup matchingGroups = ((CodeGroup)enumerator.Current).ResolveMatchingCodeGroups(evidence);
  101.                     // If the child has a policy, we are done.
  102.                     if (matchingGroups != null)
  103.                         retGroup.AddChild(matchingGroups);
  104.                 }
  105.                 return retGroup;
  106.             }
  107.             else {
  108.                 return null;
  109.             }
  110.         }
  111.        
  112.         [ResourceExposure(ResourceScope.Machine)]
  113.         [ResourceConsumption(ResourceScope.Machine)]
  114.         internal PolicyStatement CalculatePolicy(Url url)
  115.         {
  116.             URLString urlString = url.GetURLString();
  117.             if (String.Compare(urlString.Scheme, "file", StringComparison.OrdinalIgnoreCase) != 0)
  118.                 return null;
  119.            
  120.             string directory = urlString.GetDirectoryName();
  121.             PermissionSet permSet = new PermissionSet(PermissionState.None);
  122.             permSet.SetPermission(new FileIOPermission(m_access, System.IO.Path.GetFullPath(directory)));
  123.            
  124.             return new PolicyStatement(permSet, PolicyStatementAttribute.Nothing);
  125.         }
  126.        
  127.         [ResourceExposure(ResourceScope.Machine)]
  128.         [ResourceConsumption(ResourceScope.Machine)]
  129.         private PolicyStatement CalculateAssemblyPolicy(Evidence evidence)
  130.         {
  131.             PolicyStatement thisPolicy = null;
  132.            
  133.             IEnumerator evidenceEnumerator = evidence.GetHostEnumerator();
  134.             while (evidenceEnumerator.MoveNext()) {
  135.                 Url url = evidenceEnumerator.Current as Url;
  136.                 if (url != null)
  137.                     thisPolicy = CalculatePolicy(url);
  138.             }
  139.            
  140.             if (thisPolicy == null)
  141.                 thisPolicy = new PolicyStatement(new PermissionSet(false), PolicyStatementAttribute.Nothing);
  142.             return thisPolicy;
  143.         }
  144.        
  145.         public override CodeGroup Copy()
  146.         {
  147.             FileCodeGroup group = new FileCodeGroup(this.MembershipCondition, this.m_access);
  148.             group.Name = this.Name;
  149.             group.Description = this.Description;
  150.            
  151.             IEnumerator enumerator = this.Children.GetEnumerator();
  152.             while (enumerator.MoveNext()) {
  153.                 group.AddChild((CodeGroup)enumerator.Current);
  154.             }
  155.             return group;
  156.         }
  157.        
  158.         public override string MergeLogic {
  159.             get { return Environment.GetResourceString("MergeLogic_Union"); }
  160.         }
  161.        
  162.         public override string PermissionSetName {
  163.             get { return String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("FileCodeGroup_PermissionSet"), XMLUtil.BitFieldEnumToString(typeof(FileIOPermissionAccess), m_access)); }
  164.         }
  165.        
  166.         public override string AttributeString {
  167.             get { return null; }
  168.         }
  169.        
  170.         protected override void CreateXml(SecurityElement element, PolicyLevel level)
  171.         {
  172.             element.AddAttribute("Access", XMLUtil.BitFieldEnumToString(typeof(FileIOPermissionAccess), m_access));
  173.         }
  174.        
  175.         protected override void ParseXml(SecurityElement e, PolicyLevel level)
  176.         {
  177.             string access = e.Attribute("Access");
  178.             if (access != null)
  179.                 m_access = (FileIOPermissionAccess)Enum.Parse(typeof(FileIOPermissionAccess), access);
  180.             else
  181.                 m_access = FileIOPermissionAccess.NoAccess;
  182.         }
  183.        
  184.         public override bool Equals(object o)
  185.         {
  186.             FileCodeGroup that = (o as FileCodeGroup);
  187.             if (that != null && base.Equals(that)) {
  188.                 if (this.m_access == that.m_access)
  189.                     return true;
  190.             }
  191.             return false;
  192.         }
  193.        
  194.         public override int GetHashCode()
  195.         {
  196.             return base.GetHashCode() + m_access.GetHashCode();
  197.         }
  198.        
  199.         internal override string GetTypeName()
  200.         {
  201.             return "System.Security.Policy.FileCodeGroup";
  202.         }
  203.     }
  204. }

Developer Fusion