2 // System.Security.Policy.NetCodeGroup.cs
5 // Jackson Harper (Jackson@LatitudeGeo.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2002 Jackson Harper, All rights reserved
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Globalization;
33 using System.Runtime.InteropServices;
34 using System.Security.Permissions;
36 namespace System.Security.Policy {
40 public sealed class NetCodeGroup : CodeGroup {
42 public static readonly string AbsentOriginScheme = String.Empty;
43 public static readonly string AnyOtherOriginScheme = "*";
45 private Hashtable _rules = new Hashtable ();
46 private int _hashcode;
48 public NetCodeGroup (IMembershipCondition membershipCondition)
49 : base (membershipCondition, null)
53 // for PolicyLevel (to avoid validation duplication)
54 internal NetCodeGroup (SecurityElement e, PolicyLevel level)
63 public override string AttributeString {
67 public override string MergeLogic {
68 get { return "Union"; }
71 public override string PermissionSetName {
72 get { return "Same site Web"; }
80 [MonoTODO ("(2.0) missing validations")]
81 public void AddConnectAccess (string originScheme, CodeConnectAccess connectAccess)
83 if (originScheme == null)
84 throw new ArgumentException ("originScheme");
86 // TODO (2.0) - invalid characters in originScheme
87 if ((originScheme == AbsentOriginScheme) && (connectAccess.Scheme == CodeConnectAccess.OriginScheme)) {
88 throw new ArgumentOutOfRangeException ("connectAccess", Locale.GetText (
89 "Schema == CodeConnectAccess.OriginScheme"));
92 if (_rules.ContainsKey (originScheme)) {
94 if (connectAccess != null) {
95 CodeConnectAccess[] existing = (CodeConnectAccess[]) _rules [originScheme];
96 CodeConnectAccess[] array = new CodeConnectAccess [existing.Length + 1];
97 Array.Copy (existing, 0, array, 0, existing.Length);
98 array [existing.Length] = connectAccess;
99 _rules [originScheme] = array;
103 CodeConnectAccess[] array = new CodeConnectAccess [1];
104 array [0] = connectAccess;
105 _rules.Add (originScheme, array);
106 // add null to prevent access
110 public override CodeGroup Copy ()
112 NetCodeGroup copy = new NetCodeGroup (MembershipCondition);
114 copy.Description = Description;
115 copy.PolicyStatement = PolicyStatement;
117 foreach (CodeGroup child in Children) {
118 copy.AddChild (child.Copy ()); // deep copy
123 private bool Equals (CodeConnectAccess[] rules1, CodeConnectAccess[] rules2)
125 for (int i=0; i < rules1.Length; i++) {
127 for (int j=0; j < rules2.Length; j++) {
128 if (rules1 [i].Equals (rules2 [j])) {
139 public override bool Equals (object o)
141 if (!base.Equals (o))
143 NetCodeGroup ncg = (o as NetCodeGroup);
148 foreach (DictionaryEntry de in _rules) {
150 CodeConnectAccess[] ccas = (CodeConnectAccess[]) ncg._rules [de.Key];
152 found = Equals ((CodeConnectAccess[]) de.Value, ccas);
154 found = (de.Value == null);
162 public DictionaryEntry[] GetConnectAccessRules ()
164 DictionaryEntry[] result = new DictionaryEntry [_rules.Count];
165 _rules.CopyTo (result, 0);
169 public override int GetHashCode ()
171 if (_hashcode == 0) {
172 _hashcode = base.GetHashCode ();
173 foreach (DictionaryEntry de in _rules) {
174 CodeConnectAccess[] ccas = (CodeConnectAccess[]) de.Value;
176 foreach (CodeConnectAccess cca in ccas) {
177 _hashcode ^= cca.GetHashCode ();
185 public override PolicyStatement Resolve (Evidence evidence)
187 if (evidence == null)
188 throw new ArgumentNullException ("evidence");
190 if (!MembershipCondition.Check (evidence))
193 PermissionSet ps = null;
194 if (this.PolicyStatement == null)
195 ps = new PermissionSet (PermissionState.None);
197 ps = this.PolicyStatement.PermissionSet.Copy ();
199 if (this.Children.Count > 0) {
200 foreach (CodeGroup child_cg in this.Children) {
201 PolicyStatement child_pst = child_cg.Resolve (evidence);
202 if (child_pst != null) {
203 ps = ps.Union (child_pst.PermissionSet);
208 PolicyStatement pst = this.PolicyStatement.Copy ();
209 pst.PermissionSet = ps;
213 public void ResetConnectAccess ()
218 public override CodeGroup ResolveMatchingCodeGroups (Evidence evidence)
220 if (evidence == null)
221 throw new ArgumentNullException ("evidence");
223 CodeGroup return_group = null;
224 if (MembershipCondition.Check (evidence)) {
225 return_group = Copy ();
227 foreach (CodeGroup child_group in Children) {
229 child_group.ResolveMatchingCodeGroups (evidence);
230 if (matching == null)
232 return_group.AddChild (matching);
239 [MonoTODO ("(2.0) Add new stuff (CodeConnectAccess) into XML")]
240 protected override void CreateXml (SecurityElement element, PolicyLevel level)
242 base.CreateXml (element, level);
245 [MonoTODO ("(2.0) Parse new stuff (CodeConnectAccess) from XML")]
246 protected override void ParseXml (SecurityElement e, PolicyLevel level)
248 base.ParseXml (e, level);