Merge pull request #137 from eisnerd/bug-xpath-reverse-axes-497017
[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         [ComVisible (true)]
44         public class SecurityException : SystemException {
45                 // Fields
46                 string permissionState;
47                 Type permissionType;
48                 private string _granted;
49                 private string _refused;
50                 private object _demanded;
51                 private IPermission _firstperm;
52 //              private IPermission _permfailed;
53                 private MethodInfo _method;
54                 private Evidence _evidence;
55
56                 private SecurityAction _action;
57                 private object _denyset;
58                 private object _permitset;
59                 private AssemblyName _assembly;
60                 private string _url;
61                 private SecurityZone _zone;
62                 
63                 // Properties
64
65                 [ComVisible (false)]
66                 public SecurityAction Action {
67                         get { return _action; }
68                         set { _action = value; }
69                 }
70
71                 [ComVisible (false)]
72                 public object DenySetInstance {
73                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
74                         get { return _denyset; }
75                         set { _denyset = value; }
76                 }
77
78                 [ComVisible (false)]
79                 public AssemblyName FailedAssemblyInfo {
80                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
81                         get { return _assembly; }
82                         set { _assembly = value; }
83                 }
84
85                 [ComVisible (false)]
86                 public MethodInfo Method {
87                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
88                         get { return _method; }
89                         set { _method = value; }
90                 }
91
92                 [ComVisible (false)]
93                 public object PermitOnlySetInstance {
94                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
95                         get { return _permitset; }
96                         set { _permitset = value; }
97                 }
98
99                 public string Url {
100                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
101                         get { return _url; }
102                         set { _url = value; }
103                 }
104
105                 public SecurityZone Zone {
106                         get { return _zone; }
107                         set { _zone = value; }
108                 }
109
110                 [ComVisible (false)]
111                 public object Demanded {
112                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
113                         get { return _demanded; }
114                         set { _demanded = value; }
115                 }
116
117                 public IPermission FirstPermissionThatFailed {
118                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
119                         get { return _firstperm; }
120                         set { _firstperm = value; }
121                 }
122
123                 public string PermissionState {
124                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
125                         get { return permissionState; }
126                         set { permissionState = value; }
127                 }
128
129                 public Type PermissionType {
130                         get { return permissionType; }
131                         set { permissionType = value; }
132                 }
133
134                 public string GrantedSet {
135                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
136                         get { return _granted; }
137                         set { _granted = value; }
138                 }
139
140                 public string RefusedSet {
141                         [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
142                         get { return _refused; }
143                         set { _refused = value; }
144                 }
145
146                 // Constructors
147
148                 public SecurityException ()
149                         : base (Locale.GetText ("A security error has been detected."))
150                 {
151                         base.HResult = unchecked ((int)0x8013150A);
152                 }
153
154                 public SecurityException (string message) 
155                         : base (message)
156                 {
157                         base.HResult = unchecked ((int)0x8013150A);
158                 }
159                 
160                 protected SecurityException (SerializationInfo info, StreamingContext context) 
161                         : base (info, context)
162                 {
163                         base.HResult = unchecked ((int)0x8013150A);
164                         // depending on the security policy the info about PermissionState may
165                         // not be available (but the serializable must work)
166                         SerializationInfoEnumerator e = info.GetEnumerator ();
167                         while (e.MoveNext ()) {
168                                 if (e.Name == "PermissionState") {
169                                         permissionState = (string) e.Value;
170                                         break;
171                                 }
172                         }
173                 }
174                 
175                 public SecurityException (string message, Exception inner) 
176                         : base (message, inner)
177                 {
178                         base.HResult = unchecked ((int)0x8013150A);
179                 }
180                 
181                 public SecurityException (string message, Type type) 
182                         :  base (message) 
183                 {
184                         base.HResult = unchecked ((int)0x8013150A);
185                         permissionType = type;
186                 }
187                 
188                 public SecurityException (string message, Type type, string state) 
189                         : base (message) 
190                 {
191                         base.HResult = unchecked ((int)0x8013150A);
192                         permissionType = type;
193                         permissionState = state;
194                 }
195
196                 internal SecurityException (string message, PermissionSet granted, PermissionSet refused) 
197                         : base (message)
198                 {
199                         base.HResult = unchecked ((int)0x8013150A);
200                         _granted = granted.ToString ();
201                         _refused = refused.ToString ();
202                 }
203
204                 public SecurityException (string message, object deny, object permitOnly, MethodInfo method, 
205                         object demanded, IPermission permThatFailed)
206                         : base (message)
207                 {
208                         base.HResult = unchecked ((int)0x8013150A);
209                         _denyset = deny;
210                         _permitset = permitOnly;
211                         _method = method;
212                         _demanded = demanded;
213                         _firstperm = permThatFailed;
214                 }
215
216                 public SecurityException (string message, AssemblyName assemblyName, PermissionSet grant, 
217                         PermissionSet refused, MethodInfo method, SecurityAction action, object demanded, 
218                         IPermission permThatFailed, Evidence evidence)
219                         : base (message)
220                 {
221                         base.HResult = unchecked ((int)0x8013150A);
222                         _assembly = assemblyName;
223                         _granted = (grant == null) ? String.Empty : grant.ToString ();
224                         _refused = (refused == null) ? String.Empty : refused.ToString ();
225                         _method = method;
226                         _action = action;
227                         _demanded = demanded;
228                         _firstperm = permThatFailed;
229                         if (_firstperm != null)
230                                 permissionType = _firstperm.GetType ();
231                         _evidence = evidence;
232                 }
233
234                 // Methods
235                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
236                 {
237                         base.GetObjectData (info, context);
238                         try {
239                                 info.AddValue ("PermissionState", PermissionState);
240                         }
241                         catch (SecurityException) {
242                                 // serialize only if permitted to do so
243                         }
244                 }
245
246                 public override string ToString ()
247                 {
248 #if MOBILE
249                         return base.ToString ();
250 #else
251                         StringBuilder sb = new StringBuilder (base.ToString ());
252                         try {
253                                 if (permissionType != null) {
254                                         sb.AppendFormat ("{0}Type: {1}", Environment.NewLine, PermissionType);
255                                 }
256                                 if (_method != null) {
257                                         // method string representation doesn't include the type
258                                         string m = _method.ToString ();
259                                         int ret = m.IndexOf (" ") + 1;
260                                         sb.AppendFormat ("{0}Method: {1} {2}.{3}", Environment.NewLine, 
261                                                 _method.ReturnType.Name, _method.ReflectedType, m.Substring (ret));
262                                 }
263                                 if (permissionState != null) {
264                                         sb.AppendFormat ("{0}State: {1}", Environment.NewLine, PermissionState);
265                                 }
266                                 if ((_granted != null) && (_granted.Length > 0)) {
267                                         sb.AppendFormat ("{0}Granted: {1}", Environment.NewLine, GrantedSet);
268                                 }
269                                 if ((_refused != null) && (_refused.Length > 0)) {
270                                         sb.AppendFormat ("{0}Refused: {1}", Environment.NewLine, RefusedSet);
271                                 }
272                                 if (_demanded != null) {
273                                         sb.AppendFormat ("{0}Demanded: {1}", Environment.NewLine, Demanded);
274                                 }
275                                 if (_firstperm != null) {
276                                         sb.AppendFormat ("{0}Failed Permission: {1}", Environment.NewLine, FirstPermissionThatFailed);
277                                 }
278                                 if (_evidence != null) {
279                                         sb.AppendFormat ("{0}Evidences:", Environment.NewLine);
280                                         foreach (object o in _evidence) {
281                                                 // Hash evidence is way much too verbose to be useful to anyone
282                                                 if (!(o is Hash))
283                                                         sb.AppendFormat ("{0}\t{1}", Environment.NewLine, o);
284                                         }
285                                 }
286                         }
287                         catch (SecurityException) {
288                                 // some informations can't be displayed
289                         }
290                         return sb.ToString ();
291 #endif
292                 }
293         }
294 }