817bc4c2f2bf4cd2e3b5317cf7ff8e4e65a04423
[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 object _demanded;
53                 private IPermission _firstperm;
54 //              private IPermission _permfailed;
55                 private MethodInfo _method;
56                 private Evidence _evidence;
57
58 #if !NET_2_0
59         #pragma warning disable 169
60 #endif
61                 private SecurityAction _action;
62                 private object _denyset;
63                 private object _permitset;
64                 private AssemblyName _assembly;
65                 private string _url;
66                 private SecurityZone _zone;
67                 
68 #if !NET_2_0
69         #pragma warning restore 169
70 #endif  
71
72                 // Properties
73
74 #if NET_2_0
75                 [ComVisible (false)]
76                 public SecurityAction Action {
77                         get { return _action; }
78                         set { _action = value; }
79                 }
80
81                 [ComVisible (false)]
82                 public object DenySetInstance {
83                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
84                         get { return _denyset; }
85                         set { _denyset = value; }
86                 }
87
88                 [ComVisible (false)]
89                 public AssemblyName FailedAssemblyInfo {
90                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
91                         get { return _assembly; }
92                         set { _assembly = value; }
93                 }
94
95                 [ComVisible (false)]
96                 public MethodInfo Method {
97                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
98                         get { return _method; }
99                         set { _method = value; }
100                 }
101
102                 [ComVisible (false)]
103                 public object PermitOnlySetInstance {
104                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
105                         get { return _permitset; }
106                         set { _permitset = value; }
107                 }
108
109                 public string Url {
110                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
111                         get { return _url; }
112                         set { _url = value; }
113                 }
114
115                 public SecurityZone Zone {
116                         get { return _zone; }
117                         set { _zone = value; }
118                 }
119 #endif
120
121 #if NET_2_0
122                 [ComVisible (false)]
123                 public 
124 #else
125                 internal
126 #endif
127                 object Demanded {
128                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
129                         get { return _demanded; }
130                         set { _demanded = value; }
131                 }
132
133 #if NET_2_0
134                 public 
135 #else
136                 internal
137 #endif
138                 IPermission FirstPermissionThatFailed {
139                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
140                         get { return _firstperm; }
141                         set { _firstperm = value; }
142                 }
143
144                 public string PermissionState {
145                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
146                         get { return permissionState; }
147 #if NET_2_0
148                         set { permissionState = value; }
149 #endif
150                 }
151
152                 public Type PermissionType {
153                         get { return permissionType; }
154 #if NET_2_0
155                         set { permissionType = value; }
156 #endif
157                 }
158
159 #if NET_1_1
160                 public string GrantedSet {
161                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
162                         get { return _granted; }
163 #if NET_2_0
164                         set { _granted = value; }
165 #endif
166                 }
167
168                 public string RefusedSet {
169                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
170                         get { return _refused; }
171 #if NET_2_0
172                         set { _refused = value; }
173 #endif
174                 }
175 #endif
176                 // Constructors
177
178                 public SecurityException ()
179                         : base (Locale.GetText ("A security error has been detected."))
180                 {
181                         base.HResult = unchecked ((int)0x8013150A);
182                 }
183
184                 public SecurityException (string message) 
185                         : base (message)
186                 {
187                         base.HResult = unchecked ((int)0x8013150A);
188                 }
189                 
190                 protected SecurityException (SerializationInfo info, StreamingContext context) 
191                         : base (info, context)
192                 {
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;
200                                         break;
201                                 }
202                         }
203                 }
204                 
205                 public SecurityException (string message, Exception inner) 
206                         : base (message, inner)
207                 {
208                         base.HResult = unchecked ((int)0x8013150A);
209                 }
210                 
211                 public SecurityException (string message, Type type) 
212                         :  base (message) 
213                 {
214                         base.HResult = unchecked ((int)0x8013150A);
215                         permissionType = type;
216                 }
217                 
218                 public SecurityException (string message, Type type, string state) 
219                         : base (message) 
220                 {
221                         base.HResult = unchecked ((int)0x8013150A);
222                         permissionType = type;
223                         permissionState = state;
224                 }
225
226                 internal SecurityException (string message, PermissionSet granted, PermissionSet refused) 
227                         : base (message)
228                 {
229                         base.HResult = unchecked ((int)0x8013150A);
230                         _granted = granted.ToString ();
231                         _refused = refused.ToString ();
232                 }
233
234 #if NET_2_0
235                 public
236 #else
237                 internal
238 #endif
239                 SecurityException (string message, object deny, object permitOnly, MethodInfo method, 
240                         object demanded, IPermission permThatFailed)
241                         : base (message)
242                 {
243                         base.HResult = unchecked ((int)0x8013150A);
244                         _denyset = deny;
245                         _permitset = permitOnly;
246                         _method = method;
247                         _demanded = demanded;
248                         _firstperm = permThatFailed;
249                 }
250
251 #if NET_2_0
252                 public
253 #else
254                 internal
255 #endif
256                 SecurityException (string message, AssemblyName assemblyName, PermissionSet grant, 
257                         PermissionSet refused, MethodInfo method, SecurityAction action, object demanded, 
258                         IPermission permThatFailed, Evidence evidence)
259                         : base (message)
260                 {
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 ();
265                         _method = method;
266                         _action = action;
267                         _demanded = demanded;
268                         _firstperm = permThatFailed;
269                         if (_firstperm != null)
270                                 permissionType = _firstperm.GetType ();
271                         _evidence = evidence;
272                 }
273
274                 // Methods
275                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
276                 {
277                         base.GetObjectData (info, context);
278                         try {
279                                 info.AddValue ("PermissionState", PermissionState);
280                         }
281                         catch (SecurityException) {
282                                 // serialize only if permitted to do so
283                         }
284                 }
285
286                 public override string ToString ()
287                 {
288                         StringBuilder sb = new StringBuilder (base.ToString ());
289                         try {
290                                 if (permissionType != null) {
291                                         sb.AppendFormat ("{0}Type: {1}", Environment.NewLine, PermissionType);
292                                 }
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));
299                                 }
300                                 if (permissionState != null) {
301                                         sb.AppendFormat ("{0}State: {1}", Environment.NewLine, PermissionState);
302                                 }
303                                 if ((_granted != null) && (_granted.Length > 0)) {
304                                         sb.AppendFormat ("{0}Granted: {1}", Environment.NewLine, GrantedSet);
305                                 }
306                                 if ((_refused != null) && (_refused.Length > 0)) {
307                                         sb.AppendFormat ("{0}Refused: {1}", Environment.NewLine, RefusedSet);
308                                 }
309                                 if (_demanded != null) {
310                                         sb.AppendFormat ("{0}Demanded: {1}", Environment.NewLine, Demanded);
311                                 }
312                                 if (_firstperm != null) {
313                                         sb.AppendFormat ("{0}Failed Permission: {1}", Environment.NewLine, FirstPermissionThatFailed);
314                                 }
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
319                                                 if (!(o is Hash))
320                                                         sb.AppendFormat ("{0}\t{1}", Environment.NewLine, o);
321                                         }
322                                 }
323                         }
324                         catch (SecurityException) {
325                                 // some informations can't be displayed
326                         }
327                         return sb.ToString ();
328                 }
329         }
330 }