2 // System.Security.SecurityException.cs
5 // Nick Drochak(ndrochak@gol.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
9 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Globalization;
33 using System.Reflection;
34 using System.Runtime.InteropServices;
35 using System.Runtime.Serialization;
36 using System.Security.Permissions;
37 using System.Security.Policy;
40 namespace System.Security {
46 public class SecurityException : SystemException {
48 string permissionState;
50 private string _granted;
51 private string _refused;
52 private object _demanded;
53 private IPermission _firstperm;
54 // private IPermission _permfailed;
55 private MethodInfo _method;
56 private Evidence _evidence;
59 #pragma warning disable 169
61 private SecurityAction _action;
62 private object _denyset;
63 private object _permitset;
64 private AssemblyName _assembly;
66 private SecurityZone _zone;
69 #pragma warning restore 169
76 public SecurityAction Action {
77 get { return _action; }
78 set { _action = value; }
82 public object DenySetInstance {
83 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
84 get { return _denyset; }
85 set { _denyset = value; }
89 public AssemblyName FailedAssemblyInfo {
90 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
91 get { return _assembly; }
92 set { _assembly = value; }
96 public MethodInfo Method {
97 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
98 get { return _method; }
99 set { _method = value; }
103 public object PermitOnlySetInstance {
104 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
105 get { return _permitset; }
106 set { _permitset = value; }
110 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
112 set { _url = value; }
115 public SecurityZone Zone {
116 get { return _zone; }
117 set { _zone = value; }
128 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
129 get { return _demanded; }
130 set { _demanded = value; }
138 IPermission FirstPermissionThatFailed {
139 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
140 get { return _firstperm; }
141 set { _firstperm = value; }
144 public string PermissionState {
145 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
146 get { return permissionState; }
148 set { permissionState = value; }
152 public Type PermissionType {
153 get { return permissionType; }
155 set { permissionType = value; }
160 public string GrantedSet {
161 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
162 get { return _granted; }
164 set { _granted = value; }
168 public string RefusedSet {
169 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
170 get { return _refused; }
172 set { _refused = value; }
178 public SecurityException ()
179 : base (Locale.GetText ("A security error has been detected."))
181 base.HResult = unchecked ((int)0x8013150A);
184 public SecurityException (string message)
187 base.HResult = unchecked ((int)0x8013150A);
190 protected SecurityException (SerializationInfo info, StreamingContext context)
191 : base (info, context)
193 base.HResult = unchecked ((int)0x8013150A);
194 // depending on the security policy the info about PermissionState may
195 // not be available (but the serializable must work)
196 SerializationInfoEnumerator e = info.GetEnumerator ();
197 while (e.MoveNext ()) {
198 if (e.Name == "PermissionState") {
199 permissionState = (string) e.Value;
205 public SecurityException (string message, Exception inner)
206 : base (message, inner)
208 base.HResult = unchecked ((int)0x8013150A);
211 public SecurityException (string message, Type type)
214 base.HResult = unchecked ((int)0x8013150A);
215 permissionType = type;
218 public SecurityException (string message, Type type, string state)
221 base.HResult = unchecked ((int)0x8013150A);
222 permissionType = type;
223 permissionState = state;
226 internal SecurityException (string message, PermissionSet granted, PermissionSet refused)
229 base.HResult = unchecked ((int)0x8013150A);
230 _granted = granted.ToString ();
231 _refused = refused.ToString ();
239 SecurityException (string message, object deny, object permitOnly, MethodInfo method,
240 object demanded, IPermission permThatFailed)
243 base.HResult = unchecked ((int)0x8013150A);
245 _permitset = permitOnly;
247 _demanded = demanded;
248 _firstperm = permThatFailed;
256 SecurityException (string message, AssemblyName assemblyName, PermissionSet grant,
257 PermissionSet refused, MethodInfo method, SecurityAction action, object demanded,
258 IPermission permThatFailed, Evidence evidence)
261 base.HResult = unchecked ((int)0x8013150A);
262 _assembly = assemblyName;
263 _granted = (grant == null) ? String.Empty : grant.ToString ();
264 _refused = (refused == null) ? String.Empty : refused.ToString ();
267 _demanded = demanded;
268 _firstperm = permThatFailed;
269 if (_firstperm != null)
270 permissionType = _firstperm.GetType ();
271 _evidence = evidence;
275 public override void GetObjectData (SerializationInfo info, StreamingContext context)
277 base.GetObjectData (info, context);
279 info.AddValue ("PermissionState", PermissionState);
281 catch (SecurityException) {
282 // serialize only if permitted to do so
286 public override string ToString ()
288 StringBuilder sb = new StringBuilder (base.ToString ());
290 if (permissionType != null) {
291 sb.AppendFormat ("{0}Type: {1}", Environment.NewLine, PermissionType);
293 if (_method != null) {
294 // method string representation doesn't include the type
295 string m = _method.ToString ();
296 int ret = m.IndexOf (" ") + 1;
297 sb.AppendFormat ("{0}Method: {1} {2}.{3}", Environment.NewLine,
298 _method.ReturnType.Name, _method.ReflectedType, m.Substring (ret));
300 if (permissionState != null) {
301 sb.AppendFormat ("{0}State: {1}", Environment.NewLine, PermissionState);
303 if ((_granted != null) && (_granted.Length > 0)) {
304 sb.AppendFormat ("{0}Granted: {1}", Environment.NewLine, GrantedSet);
306 if ((_refused != null) && (_refused.Length > 0)) {
307 sb.AppendFormat ("{0}Refused: {1}", Environment.NewLine, RefusedSet);
309 if (_demanded != null) {
310 sb.AppendFormat ("{0}Demanded: {1}", Environment.NewLine, Demanded);
312 if (_firstperm != null) {
313 sb.AppendFormat ("{0}Failed Permission: {1}", Environment.NewLine, FirstPermissionThatFailed);
315 if (_evidence != null) {
316 sb.AppendFormat ("{0}Evidences:", Environment.NewLine);
317 foreach (object o in _evidence) {
318 // Hash evidence is way much too verbose to be useful to anyone
320 sb.AppendFormat ("{0}\t{1}", Environment.NewLine, o);
324 catch (SecurityException) {
325 // some informations can't be displayed
327 return sb.ToString ();