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 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;
61 private SecurityZone _zone;
67 public SecurityAction Action {
68 get { return _action; }
69 set { _action = value; }
73 public object DenySetInstance {
74 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
75 get { return _denyset; }
76 set { _denyset = value; }
80 public AssemblyName FailedAssemblyInfo {
81 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
82 get { return _assembly; }
83 set { _assembly = value; }
87 public MethodInfo Method {
88 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
89 get { return _method; }
90 set { _method = value; }
94 public object PermitOnlySetInstance {
95 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
96 get { return _permitset; }
97 set { _permitset = value; }
101 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
103 set { _url = value; }
106 public SecurityZone Zone {
107 get { return _zone; }
108 set { _zone = value; }
119 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
120 get { return _demanded; }
121 set { _demanded = value; }
129 IPermission FirstPermissionThatFailed {
130 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
131 get { return _firstperm; }
132 set { _firstperm = value; }
135 public string PermissionState {
136 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
137 get { return permissionState; }
139 set { permissionState = value; }
143 public Type PermissionType {
144 get { return permissionType; }
146 set { permissionType = value; }
151 public string GrantedSet {
152 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
153 get { return _granted; }
155 set { _granted = value; }
159 public string RefusedSet {
160 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
161 get { return _refused; }
163 set { _refused = value; }
169 public SecurityException ()
170 : base (Locale.GetText ("A security error has been detected."))
172 base.HResult = unchecked ((int)0x8013150A);
175 public SecurityException (string message)
178 base.HResult = unchecked ((int)0x8013150A);
181 protected SecurityException (SerializationInfo info, StreamingContext context)
182 : base (info, context)
184 base.HResult = unchecked ((int)0x8013150A);
185 // depending on the security policy the info about PermissionState may
186 // not be available (but the serializable must work)
187 SerializationInfoEnumerator e = info.GetEnumerator ();
188 while (e.MoveNext ()) {
189 if (e.Name == "PermissionState") {
190 permissionState = (string) e.Value;
196 public SecurityException (string message, Exception inner)
197 : base (message, inner)
199 base.HResult = unchecked ((int)0x8013150A);
202 public SecurityException (string message, Type type)
205 base.HResult = unchecked ((int)0x8013150A);
206 permissionType = type;
209 public SecurityException (string message, Type type, string state)
212 base.HResult = unchecked ((int)0x8013150A);
213 permissionType = type;
214 permissionState = state;
217 internal SecurityException (string message, PermissionSet granted, PermissionSet refused)
220 base.HResult = unchecked ((int)0x8013150A);
221 _granted = granted.ToString ();
222 _refused = refused.ToString ();
230 SecurityException (string message, object deny, object permitOnly, MethodInfo method,
231 object demanded, IPermission permThatFailed)
234 base.HResult = unchecked ((int)0x8013150A);
236 _permitset = permitOnly;
238 _demanded = demanded;
239 _firstperm = permThatFailed;
247 SecurityException (string message, AssemblyName assemblyName, PermissionSet grant,
248 PermissionSet refused, MethodInfo method, SecurityAction action, object demanded,
249 IPermission permThatFailed, Evidence evidence)
252 base.HResult = unchecked ((int)0x8013150A);
253 _assembly = assemblyName;
254 _granted = (grant == null) ? String.Empty : grant.ToString ();
255 _refused = (refused == null) ? String.Empty : refused.ToString ();
258 _demanded = demanded;
259 _firstperm = permThatFailed;
260 if (_firstperm != null)
261 permissionType = _firstperm.GetType ();
262 // FIXME ? evidence ?
266 public override void GetObjectData (SerializationInfo info, StreamingContext context)
268 base.GetObjectData (info, context);
270 info.AddValue ("PermissionState", PermissionState);
272 catch (SecurityException) {
273 // serialize only if permitted to do so
277 public override string ToString ()
279 StringBuilder sb = new StringBuilder (base.ToString ());
281 if (permissionType != null) {
282 sb.AppendFormat ("{0}Type: {1}", Environment.NewLine, PermissionType);
284 if (_method != null) {
285 // method string representation doesn't include the type
286 string m = _method.ToString ();
287 int ret = m.IndexOf (" ") + 1;
288 sb.AppendFormat ("{0}Method: {1} {2}.{3}", Environment.NewLine,
289 _method.ReturnType.Name, _method.ReflectedType, m.Substring (ret));
291 if (permissionState != null) {
292 sb.AppendFormat ("{0}State: {1}", Environment.NewLine, PermissionState);
294 if ((_granted != null) && (_granted.Length > 0)) {
295 sb.AppendFormat ("{0}Granted: {1}", Environment.NewLine, GrantedSet);
297 if ((_refused != null) && (_refused.Length > 0)) {
298 sb.AppendFormat ("{0}Refused: {1}", Environment.NewLine, RefusedSet);
300 if (_demanded != null) {
301 sb.AppendFormat ("{0}Demanded: {1}", Environment.NewLine, Demanded);
303 if (_firstperm != null) {
304 sb.AppendFormat ("{0}Failed Permission: {1}", Environment.NewLine, FirstPermissionThatFailed);
307 catch (SecurityException) {
308 // some informations can't be displayed
310 return sb.ToString ();