2007-08-28 Mark Probst <mark.probst@gmail.com>
[mono.git] / mcs / class / corlib / System.Security / SecurityException.cs
1 //
2 // System.Security.SecurityException.cs
3 //
4 // Authors:
5 //      Nick Drochak(ndrochak@gol.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) Nick Drochak
9 // (C) 2004 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
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;
38 using System.Text;
39
40 namespace System.Security {
41
42         [Serializable]
43 #if NET_2_0
44         [ComVisible (true)]
45 #endif
46         public class SecurityException : SystemException {
47                 // Fields
48                 string permissionState;
49                 Type permissionType;
50                 private string _granted;
51                 private string _refused;
52                 private SecurityAction _action;
53                 private object _demanded;
54                 private object _denyset;
55                 private object _permitset;
56                 private AssemblyName _assembly;
57                 private IPermission _firstperm;
58                 private IPermission _permfailed;
59                 private MethodInfo _method;
60                 private string _url;
61                 private SecurityZone _zone;
62                 private Evidence _evidence;
63
64                 // Properties
65
66 #if NET_2_0
67                 [ComVisible (false)]
68                 public SecurityAction Action {
69                         get { return _action; }
70                         set { _action = value; }
71                 }
72
73                 [ComVisible (false)]
74                 public object DenySetInstance {
75                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
76                         get { return _denyset; }
77                         set { _denyset = value; }
78                 }
79
80                 [ComVisible (false)]
81                 public AssemblyName FailedAssemblyInfo {
82                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
83                         get { return _assembly; }
84                         set { _assembly = value; }
85                 }
86
87                 [ComVisible (false)]
88                 public MethodInfo Method {
89                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
90                         get { return _method; }
91                         set { _method = value; }
92                 }
93
94                 [ComVisible (false)]
95                 public object PermitOnlySetInstance {
96                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
97                         get { return _permitset; }
98                         set { _permitset = value; }
99                 }
100
101                 public string Url {
102                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
103                         get { return _url; }
104                         set { _url = value; }
105                 }
106
107                 public SecurityZone Zone {
108                         get { return _zone; }
109                         set { _zone = value; }
110                 }
111 #endif
112
113 #if NET_2_0
114                 [ComVisible (false)]
115                 public 
116 #else
117                 internal
118 #endif
119                 object Demanded {
120                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
121                         get { return _demanded; }
122                         set { _demanded = value; }
123                 }
124
125 #if NET_2_0
126                 public 
127 #else
128                 internal
129 #endif
130                 IPermission FirstPermissionThatFailed {
131                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
132                         get { return _firstperm; }
133                         set { _firstperm = value; }
134                 }
135
136                 public string PermissionState {
137                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
138                         get { return permissionState; }
139 #if NET_2_0
140                         set { permissionState = value; }
141 #endif
142                 }
143
144                 public Type PermissionType {
145                         get { return permissionType; }
146 #if NET_2_0
147                         set { permissionType = value; }
148 #endif
149                 }
150
151 #if NET_1_1
152                 public string GrantedSet {
153                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
154                         get { return _granted; }
155 #if NET_2_0
156                         set { _granted = value; }
157 #endif
158                 }
159
160                 public string RefusedSet {
161                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
162                         get { return _refused; }
163 #if NET_2_0
164                         set { _refused = value; }
165 #endif
166                 }
167 #endif
168                 // Constructors
169
170                 public SecurityException ()
171                         : base (Locale.GetText ("A security error has been detected."))
172                 {
173                         base.HResult = unchecked ((int)0x8013150A);
174                 }
175
176                 public SecurityException (string message) 
177                         : base (message)
178                 {
179                         base.HResult = unchecked ((int)0x8013150A);
180                 }
181                 
182                 protected SecurityException (SerializationInfo info, StreamingContext context) 
183                         : base (info, context)
184                 {
185                         base.HResult = unchecked ((int)0x8013150A);
186                         // depending on the security policy the info about PermissionState may
187                         // not be available (but the serializable must work)
188                         SerializationInfoEnumerator e = info.GetEnumerator ();
189                         while (e.MoveNext ()) {
190                                 if (e.Name == "PermissionState") {
191                                         permissionState = (string) e.Value;
192                                         break;
193                                 }
194                         }
195                 }
196                 
197                 public SecurityException (string message, Exception inner) 
198                         : base (message, inner)
199                 {
200                         base.HResult = unchecked ((int)0x8013150A);
201                 }
202                 
203                 public SecurityException (string message, Type type) 
204                         :  base (message) 
205                 {
206                         base.HResult = unchecked ((int)0x8013150A);
207                         permissionType = type;
208                 }
209                 
210                 public SecurityException (string message, Type type, string state) 
211                         : base (message) 
212                 {
213                         base.HResult = unchecked ((int)0x8013150A);
214                         permissionType = type;
215                         permissionState = state;
216                 }
217
218                 internal SecurityException (string message, PermissionSet granted, PermissionSet refused) 
219                         : base (message)
220                 {
221                         base.HResult = unchecked ((int)0x8013150A);
222                         _granted = granted.ToString ();
223                         _refused = refused.ToString ();
224                 }
225
226 #if NET_2_0
227                 public
228 #else
229                 internal
230 #endif
231                 SecurityException (string message, object deny, object permitOnly, MethodInfo method, 
232                         object demanded, IPermission permThatFailed)
233                         : base (message)
234                 {
235                         base.HResult = unchecked ((int)0x8013150A);
236                         _denyset = deny;
237                         _permitset = permitOnly;
238                         _method = method;
239                         _demanded = demanded;
240                         _firstperm = permThatFailed;
241                 }
242
243 #if NET_2_0
244                 public
245 #else
246                 internal
247 #endif
248                 SecurityException (string message, AssemblyName assemblyName, PermissionSet grant, 
249                         PermissionSet refused, MethodInfo method, SecurityAction action, object demanded, 
250                         IPermission permThatFailed, Evidence evidence)
251                         : base (message)
252                 {
253                         base.HResult = unchecked ((int)0x8013150A);
254                         _assembly = assemblyName;
255                         _granted = (grant == null) ? String.Empty : grant.ToString ();
256                         _refused = (refused == null) ? String.Empty : refused.ToString ();
257                         _method = method;
258                         _action = action;
259                         _demanded = demanded;
260                         _firstperm = permThatFailed;
261                         if (_firstperm != null)
262                                 permissionType = _firstperm.GetType ();
263                         _evidence = evidence;
264                 }
265
266                 // Methods
267                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
268                 {
269                         base.GetObjectData (info, context);
270                         try {
271                                 info.AddValue ("PermissionState", PermissionState);
272                         }
273                         catch (SecurityException) {
274                                 // serialize only if permitted to do so
275                         }
276                 }
277
278                 public override string ToString ()
279                 {
280                         StringBuilder sb = new StringBuilder (base.ToString ());
281                         try {
282                                 if (permissionType != null) {
283                                         sb.AppendFormat ("{0}Type: {1}", Environment.NewLine, PermissionType);
284                                 }
285                                 if (_method != null) {
286                                         // method string representation doesn't include the type
287                                         string m = _method.ToString ();
288                                         int ret = m.IndexOf (" ") + 1;
289                                         sb.AppendFormat ("{0}Method: {1} {2}.{3}", Environment.NewLine, 
290                                                 _method.ReturnType.Name, _method.ReflectedType, m.Substring (ret));
291                                 }
292                                 if (permissionState != null) {
293                                         sb.AppendFormat ("{0}State: {1}", Environment.NewLine, PermissionState);
294                                 }
295                                 if ((_granted != null) && (_granted.Length > 0)) {
296                                         sb.AppendFormat ("{0}Granted: {1}", Environment.NewLine, GrantedSet);
297                                 }
298                                 if ((_refused != null) && (_refused.Length > 0)) {
299                                         sb.AppendFormat ("{0}Refused: {1}", Environment.NewLine, RefusedSet);
300                                 }
301                                 if (_demanded != null) {
302                                         sb.AppendFormat ("{0}Demanded: {1}", Environment.NewLine, Demanded);
303                                 }
304                                 if (_firstperm != null) {
305                                         sb.AppendFormat ("{0}Failed Permission: {1}", Environment.NewLine, FirstPermissionThatFailed);
306                                 }
307                                 if (_evidence != null) {
308                                         sb.AppendFormat ("{0}Evidences:", Environment.NewLine);
309                                         foreach (object o in _evidence) {
310                                                 // Hash evidence is way much too verbose to be useful to anyone
311                                                 if (!(o is Hash))
312                                                         sb.AppendFormat ("{0}\t{1}", Environment.NewLine, o);
313                                         }
314                                 }
315                         }
316                         catch (SecurityException) {
317                                 // some informations can't be displayed
318                         }
319                         return sb.ToString ();
320                 }
321         }
322 }