2009-06-30 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / System.Security / SecurityException.cs
index c63a04a4263cc118223ce1c6244c8c9a2db1eca6..e87fad90e5e7399022024afed7a676ba47569765 100644 (file)
@@ -7,7 +7,7 @@
 //
 // (C) Nick Drochak
 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -41,25 +41,33 @@ namespace System.Security {
 
        [Serializable]
 #if NET_2_0
-       public class SecurityException : SystemException, _Exception {
-#else
-       public class SecurityException : SystemException {
+       [ComVisible (true)]
 #endif
+       public class SecurityException : SystemException {
                // Fields
                string permissionState;
                Type permissionType;
                private string _granted;
                private string _refused;
-               private SecurityAction _action;
                private object _demanded;
+               private IPermission _firstperm;
+//             private IPermission _permfailed;
+               private MethodInfo _method;
+               private Evidence _evidence;
+
+#if !NET_2_0
+       #pragma warning disable 169
+#endif
+               private SecurityAction _action;
                private object _denyset;
                private object _permitset;
                private AssemblyName _assembly;
-               private IPermission _firstperm;
-               private IPermission _permfailed;
-               private MethodInfo _method;
                private string _url;
                private SecurityZone _zone;
+               
+#if !NET_2_0
+       #pragma warning restore 169
+#endif 
 
                // Properties
 
@@ -70,49 +78,36 @@ namespace System.Security {
                        set { _action = value; }
                }
 
-               [ComVisible (false)]
-               public object Demanded {
-                       get { return _demanded; }
-                       set { _demanded = value; }
-               }
-
                [ComVisible (false)]
                public object DenySetInstance {
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
                        get { return _denyset; }
                        set { _denyset = value; }
                }
 
                [ComVisible (false)]
                public AssemblyName FailedAssemblyInfo {
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
                        get { return _assembly; }
                        set { _assembly = value; }
                }
 
-               public IPermission FirstPermissionThatFailed {
-                       get { return _firstperm; }
-                       set { _firstperm = value; }
-               }
-
                [ComVisible (false)]
                public MethodInfo Method {
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
                        get { return _method; }
                        set { _method = value; }
                }
 
-               [Obsolete]
-               [ComVisible (false)]
-               public IPermission PermissionThatFailed {
-                       get { return _permfailed; }
-                       set { _permfailed = value; }
-               }
-
                [ComVisible (false)]
                public object PermitOnlySetInstance {
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
                        get { return _permitset; }
                        set { _permitset = value; }
                }
 
                public string Url {
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
                        get { return _url; }
                        set { _url = value; }
                }
@@ -123,7 +118,31 @@ namespace System.Security {
                }
 #endif
 
+#if NET_2_0
+               [ComVisible (false)]
+               public 
+#else
+               internal
+#endif
+               object Demanded {
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
+                       get { return _demanded; }
+                       set { _demanded = value; }
+               }
+
+#if NET_2_0
+               public 
+#else
+               internal
+#endif
+               IPermission FirstPermissionThatFailed {
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
+                       get { return _firstperm; }
+                       set { _firstperm = value; }
+               }
+
                public string PermissionState {
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
                        get { return permissionState; }
 #if NET_2_0
                        set { permissionState = value; }
@@ -139,6 +158,7 @@ namespace System.Security {
 
 #if NET_1_1
                public string GrantedSet {
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
                        get { return _granted; }
 #if NET_2_0
                        set { _granted = value; }
@@ -146,6 +166,7 @@ namespace System.Security {
                }
 
                public string RefusedSet {
+                       [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
                        get { return _refused; }
 #if NET_2_0
                        set { _refused = value; }
@@ -170,7 +191,15 @@ namespace System.Security {
                        : base (info, context)
                {
                        base.HResult = unchecked ((int)0x8013150A);
-                       permissionState = info.GetString ("PermissionState");
+                       // depending on the security policy the info about PermissionState may
+                       // not be available (but the serializable must work)
+                       SerializationInfoEnumerator e = info.GetEnumerator ();
+                       while (e.MoveNext ()) {
+                               if (e.Name == "PermissionState") {
+                                       permissionState = (string) e.Value;
+                                       break;
+                               }
+                       }
                }
                
                public SecurityException (string message, Exception inner) 
@@ -211,11 +240,12 @@ namespace System.Security {
                        object demanded, IPermission permThatFailed)
                        : base (message)
                {
+                       base.HResult = unchecked ((int)0x8013150A);
                        _denyset = deny;
                        _permitset = permitOnly;
                        _method = method;
                        _demanded = demanded;
-                       _permfailed = permThatFailed;
+                       _firstperm = permThatFailed;
                }
 
 #if NET_2_0
@@ -228,47 +258,77 @@ namespace System.Security {
                        IPermission permThatFailed, Evidence evidence)
                        : base (message)
                {
+                       base.HResult = unchecked ((int)0x8013150A);
                        _assembly = assemblyName;
                        _granted = (grant == null) ? String.Empty : grant.ToString ();
                        _refused = (refused == null) ? String.Empty : refused.ToString ();
                        _method = method;
                        _action = action;
                        _demanded = demanded;
-                       _permfailed = permThatFailed;
-                       if (_permfailed != null)
-                               permissionType = _permfailed.GetType ();
-                       // FIXME ? evidence ?
+                       _firstperm = permThatFailed;
+                       if (_firstperm != null)
+                               permissionType = _firstperm.GetType ();
+                       _evidence = evidence;
                }
 
                // Methods
                public override void GetObjectData (SerializationInfo info, StreamingContext context)
                {
                        base.GetObjectData (info, context);
-                       info.AddValue ("PermissionState", permissionState);
+                       try {
+                               info.AddValue ("PermissionState", PermissionState);
+                       }
+                       catch (SecurityException) {
+                               // serialize only if permitted to do so
+                       }
                }
 
                public override string ToString ()
                {
+#if NET_2_1
+                       return base.ToString ();
+#else
                        StringBuilder sb = new StringBuilder (base.ToString ());
-                       if (permissionState != null) {
-                               sb.AppendFormat ("{0}State: {1}", Environment.NewLine, permissionState);
-                       }
-                       if (permissionType != null) {
-                               sb.AppendFormat ("{0}Type: {1}", Environment.NewLine, permissionType);
-                       }
-                       if ((_granted != null) && (_granted.Length > 0)) {
-                               sb.AppendFormat ("{0}Granted: {1}", Environment.NewLine, _granted);
+                       try {
+                               if (permissionType != null) {
+                                       sb.AppendFormat ("{0}Type: {1}", Environment.NewLine, PermissionType);
+                               }
+                               if (_method != null) {
+                                       // method string representation doesn't include the type
+                                       string m = _method.ToString ();
+                                       int ret = m.IndexOf (" ") + 1;
+                                       sb.AppendFormat ("{0}Method: {1} {2}.{3}", Environment.NewLine, 
+                                               _method.ReturnType.Name, _method.ReflectedType, m.Substring (ret));
+                               }
+                               if (permissionState != null) {
+                                       sb.AppendFormat ("{0}State: {1}", Environment.NewLine, PermissionState);
+                               }
+                               if ((_granted != null) && (_granted.Length > 0)) {
+                                       sb.AppendFormat ("{0}Granted: {1}", Environment.NewLine, GrantedSet);
+                               }
+                               if ((_refused != null) && (_refused.Length > 0)) {
+                                       sb.AppendFormat ("{0}Refused: {1}", Environment.NewLine, RefusedSet);
+                               }
+                               if (_demanded != null) {
+                                       sb.AppendFormat ("{0}Demanded: {1}", Environment.NewLine, Demanded);
+                               }
+                               if (_firstperm != null) {
+                                       sb.AppendFormat ("{0}Failed Permission: {1}", Environment.NewLine, FirstPermissionThatFailed);
+                               }
+                               if (_evidence != null) {
+                                       sb.AppendFormat ("{0}Evidences:", Environment.NewLine);
+                                       foreach (object o in _evidence) {
+                                               // Hash evidence is way much too verbose to be useful to anyone
+                                               if (!(o is Hash))
+                                                       sb.AppendFormat ("{0}\t{1}", Environment.NewLine, o);
+                                       }
+                               }
                        }
-                       if ((_refused != null) && (_refused.Length > 0)) {
-                               sb.AppendFormat ("{0}Refused: {1}", Environment.NewLine, _refused);
-                       }
-                       if (_demanded != null) {
-                               sb.AppendFormat ("{0}Demanded: {1}", Environment.NewLine, _demanded);
-                       }
-                       if (_permfailed != null) {
-                               sb.AppendFormat ("{0}Failed Permission: {1}", Environment.NewLine, _permfailed);
+                       catch (SecurityException) {
+                               // some informations can't be displayed
                        }
                        return sb.ToString ();
+#endif
                }
        }
 }