2 // System.Security.CodeAccessPermission.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Nick Drochak, ndrochak@gol.com
7 // Sebastien Pouliot <sebastien@ximian.com>
9 // (C) Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2001 Nick Drochak, All Rights Reserved
11 // Portions (C) 2004 Motus Technologies Inc. (http://www.motus.com)
12 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Diagnostics;
35 using System.Globalization;
36 using System.Reflection;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39 using System.Security.Permissions;
40 using System.Threading;
42 namespace System.Security {
45 [SecurityPermission (SecurityAction.InheritanceDemand, ControlEvidence = true, ControlPolicy = true)]
47 [MonoTODO ("CAS support is experimental (and unsupported).")]
48 public abstract class CodeAccessPermission : IPermission, ISecurityEncodable, IStackWalk {
51 protected CodeAccessPermission ()
55 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
58 new PermissionSet (this).Assert ();
61 public abstract IPermission Copy ();
65 // note: here we're sure it's a CAS demand
66 if (!SecurityManager.SecurityEnabled)
69 // skip frames until we get the caller (of our caller)
70 new PermissionSet (this).CasOnlyDemand (3);
73 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
76 new PermissionSet (this).Deny ();
80 public override bool Equals (object obj)
84 if (obj.GetType () != this.GetType ())
86 CodeAccessPermission cap = (obj as CodeAccessPermission);
87 return (IsSubsetOf (cap) && cap.IsSubsetOf (this));
90 public abstract void FromXml (SecurityElement elem);
93 public override int GetHashCode ()
95 return base.GetHashCode ();
98 public abstract IPermission Intersect (IPermission target);
100 public abstract bool IsSubsetOf (IPermission target);
102 public override string ToString ()
104 SecurityElement elem = ToXml ();
105 return elem.ToString ();
108 public abstract SecurityElement ToXml ();
110 public virtual IPermission Union (IPermission other)
113 throw new System.NotSupportedException (); // other is not null.
117 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
118 public void PermitOnly ()
120 new PermissionSet (this).PermitOnly ();
123 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
124 public static void RevertAll ()
126 if (!SecurityManager.SecurityEnabled)
128 throw new NotImplementedException ();
131 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
132 public static void RevertAssert ()
134 if (!SecurityManager.SecurityEnabled)
136 throw new NotImplementedException ();
139 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
140 public static void RevertDeny ()
142 if (!SecurityManager.SecurityEnabled)
144 throw new NotImplementedException ();
147 [MonoTODO ("CAS support is experimental (and unsupported). Imperative mode is not implemented.")]
148 public static void RevertPermitOnly ()
150 if (!SecurityManager.SecurityEnabled)
152 throw new NotImplementedException ();
155 // Internal helpers methods
157 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
158 internal SecurityElement Element (int version)
160 SecurityElement se = new SecurityElement ("IPermission");
161 Type type = this.GetType ();
162 se.AddAttribute ("class", type.FullName + ", " + type.Assembly.ToString ().Replace ('\"', '\''));
163 se.AddAttribute ("version", version.ToString ());
167 internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
171 case PermissionState.None:
173 case PermissionState.Unrestricted:
174 // unrestricted permissions are possible for identiy permissions
177 msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
178 throw new ArgumentException (msg, "state");
183 internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion)
186 throw new ArgumentNullException (parameterName);
188 // Tag is case-sensitive
189 if (se.Tag != "IPermission") {
190 string msg = String.Format (Locale.GetText ("Invalid tag {0}"), se.Tag);
191 throw new ArgumentException (msg, parameterName);
194 // Note: we do not care about the class attribute at
195 // this stage (in fact we don't even if the class
196 // attribute is present or not). Anyway the object has
197 // already be created, with success, if we're loading it
199 // we assume minimum version if no version number is supplied
200 int version = minimumVersion;
201 string v = se.Attribute ("version");
204 version = Int32.Parse (v);
206 catch (Exception e) {
207 string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
208 msg = String.Format (msg, v);
209 throw new ArgumentException (msg, parameterName, e);
213 if ((version < minimumVersion) || (version > maximumVersion)) {
214 string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
215 msg = String.Format (msg, version, minimumVersion, maximumVersion);
216 throw new ArgumentException (msg, parameterName);
221 // must be called after CheckSecurityElement (i.e. se != null)
222 internal static bool IsUnrestricted (SecurityElement se)
224 string value = se.Attribute ("Unrestricted");
227 return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
230 internal static void ThrowInvalidPermission (IPermission target, Type expected)
232 string msg = Locale.GetText ("Invalid permission type '{0}', expected type '{1}'.");
233 msg = String.Format (msg, target.GetType (), expected);
234 throw new ArgumentException (msg, "target");