2 // System.Security.Permissions.EnvironmentPermission.cs
5 // Tim Coleman <tim@timcoleman.com>
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2002, Tim Coleman
9 // Portions Copyright (C) 2003 Motus Technologies (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
.Collections
;
33 using System
.Runtime
.InteropServices
;
36 namespace System
.Security
.Permissions
{
40 public sealed class EnvironmentPermission
: CodeAccessPermission
, IUnrestrictedPermission
, IBuiltInPermission
{
44 private const int version
= 1;
46 // EnvironmentPermissionAccess flags;
47 PermissionState _state
;
55 public EnvironmentPermission (PermissionState state
) : base ()
57 _state
= CheckPermissionState (state
, true);
58 readList
= new ArrayList ();
59 writeList
= new ArrayList ();
62 public EnvironmentPermission (EnvironmentPermissionAccess flag
, string pathList
) : base ()
64 readList
= new ArrayList ();
65 writeList
= new ArrayList ();
66 SetPathList (flag
, pathList
);
69 #endregion // Constructors
73 public void AddPathList (EnvironmentPermissionAccess flag
, string pathList
)
76 throw new ArgumentNullException ("pathList");
80 case EnvironmentPermissionAccess
.AllAccess
:
81 paths
= pathList
.Split (';');
82 foreach (string path
in paths
) {
83 if (!readList
.Contains (path
))
85 if (!writeList
.Contains (path
))
89 case EnvironmentPermissionAccess
.NoAccess
:
90 // ??? unit tests doesn't show removal using NoAccess ???
92 case EnvironmentPermissionAccess
.Read
:
93 paths
= pathList
.Split (';');
94 foreach (string path
in paths
) {
95 if (!readList
.Contains (path
))
99 case EnvironmentPermissionAccess
.Write
:
100 paths
= pathList
.Split (';');
101 foreach (string path
in paths
) {
102 if (!writeList
.Contains (path
))
103 writeList
.Add (path
);
107 ThrowInvalidFlag (flag
, false);
112 public override IPermission
Copy ()
114 EnvironmentPermission ep
= new EnvironmentPermission (_state
);
115 string path
= GetPathList (EnvironmentPermissionAccess
.Read
);
117 ep
.SetPathList (EnvironmentPermissionAccess
.Read
, path
);
118 path
= GetPathList (EnvironmentPermissionAccess
.Write
);
120 ep
.SetPathList (EnvironmentPermissionAccess
.Write
, path
);
124 public override void FromXml (SecurityElement esd
)
126 // General validation in CodeAccessPermission
127 CheckSecurityElement (esd
, "esd", version
, version
);
128 // Note: we do not (yet) care about the return value
129 // as we only accept version 1 (min/max values)
131 if (IsUnrestricted (esd
))
132 _state
= PermissionState
.Unrestricted
;
134 string read
= esd
.Attribute ("Read");
135 if ((read
!= null) && (read
.Length
> 0))
136 SetPathList (EnvironmentPermissionAccess
.Read
, read
);
138 string write
= esd
.Attribute ("Write");
139 if ((write
!= null) && (write
.Length
> 0))
140 SetPathList (EnvironmentPermissionAccess
.Write
, write
);
143 public string GetPathList (EnvironmentPermissionAccess flag
)
146 case EnvironmentPermissionAccess
.AllAccess
:
147 case EnvironmentPermissionAccess
.NoAccess
:
148 ThrowInvalidFlag (flag
, true);
150 case EnvironmentPermissionAccess
.Read
:
151 return GetPathList (readList
);
152 case EnvironmentPermissionAccess
.Write
:
153 return GetPathList (writeList
);
155 ThrowInvalidFlag (flag
, false);
158 return null; // never reached
161 public override IPermission
Intersect (IPermission target
)
163 EnvironmentPermission ep
= Cast (target
);
167 if (IsUnrestricted ())
169 if (ep
.IsUnrestricted ())
173 EnvironmentPermission result
= new EnvironmentPermission (PermissionState
.None
);
174 string readTarget
= ep
.GetPathList (EnvironmentPermissionAccess
.Read
);
175 if (readTarget
!= null) {
176 string[] targets
= readTarget
.Split (';');
177 foreach (string t
in targets
) {
178 if (readList
.Contains (t
)) {
179 result
.AddPathList (EnvironmentPermissionAccess
.Read
, t
);
185 string writeTarget
= ep
.GetPathList (EnvironmentPermissionAccess
.Write
);
186 if (writeTarget
!= null) {
187 string[] targets
= writeTarget
.Split (';');
188 foreach (string t
in targets
) {
189 if (writeList
.Contains (t
)) {
190 result
.AddPathList (EnvironmentPermissionAccess
.Write
, t
);
195 return ((n
> 0) ? result
: null);
198 public override bool IsSubsetOf (IPermission target
)
200 EnvironmentPermission ep
= Cast (target
);
204 if (IsUnrestricted ())
205 return ep
.IsUnrestricted ();
206 else if (ep
.IsUnrestricted ())
209 foreach (string s
in readList
) {
210 if (!ep
.readList
.Contains (s
))
214 foreach (string s
in writeList
) {
215 if (!ep
.writeList
.Contains (s
))
222 public bool IsUnrestricted ()
224 return (_state
== PermissionState
.Unrestricted
);
227 public void SetPathList (EnvironmentPermissionAccess flag
, string pathList
)
229 if (pathList
== null)
230 throw new ArgumentNullException ("pathList");
233 case EnvironmentPermissionAccess
.AllAccess
:
236 paths
= pathList
.Split (';');
237 foreach (string path
in paths
) {
239 writeList
.Add (path
);
242 case EnvironmentPermissionAccess
.NoAccess
:
243 // ??? unit tests doesn't show removal using NoAccess ???
245 case EnvironmentPermissionAccess
.Read
:
247 paths
= pathList
.Split (';');
248 foreach (string path
in paths
) {
252 case EnvironmentPermissionAccess
.Write
:
254 paths
= pathList
.Split (';');
255 foreach (string path
in paths
) {
256 writeList
.Add (path
);
260 ThrowInvalidFlag (flag
, false);
265 public override SecurityElement
ToXml ()
267 SecurityElement se
= Element (version
);
269 if (_state
== PermissionState
.Unrestricted
) {
270 se
.AddAttribute ("Unrestricted", "true");
273 string path
= GetPathList (EnvironmentPermissionAccess
.Read
);
275 se
.AddAttribute ("Read", path
);
276 path
= GetPathList (EnvironmentPermissionAccess
.Write
);
278 se
.AddAttribute ("Write", path
);
283 public override IPermission
Union (IPermission other
)
285 EnvironmentPermission ep
= Cast (other
);
289 if (IsUnrestricted () || ep
.IsUnrestricted ())
290 return new EnvironmentPermission (PermissionState
.Unrestricted
);
292 if (IsEmpty () && ep
.IsEmpty ())
295 EnvironmentPermission result
= (EnvironmentPermission
) Copy ();
296 string path
= ep
.GetPathList (EnvironmentPermissionAccess
.Read
);
298 result
.AddPathList (EnvironmentPermissionAccess
.Read
, path
);
299 path
= ep
.GetPathList (EnvironmentPermissionAccess
.Write
);
301 result
.AddPathList (EnvironmentPermissionAccess
.Write
, path
);
305 // IBuiltInPermission
306 int IBuiltInPermission
.GetTokenIndex ()
308 return (int) BuiltInToken
.Environment
;
313 private bool IsEmpty ()
315 return ((_state
== PermissionState
.None
) && (readList
.Count
== 0) && (writeList
.Count
== 0));
318 private EnvironmentPermission
Cast (IPermission target
)
323 EnvironmentPermission ep
= (target
as EnvironmentPermission
);
325 ThrowInvalidPermission (target
, typeof (EnvironmentPermission
));
331 internal void ThrowInvalidFlag (EnvironmentPermissionAccess flag
, bool context
)
335 msg
= Locale
.GetText ("Unknown flag '{0}'.");
337 msg
= Locale
.GetText ("Invalid flag '{0}' in this context.");
338 throw new ArgumentException (String
.Format (msg
, flag
), "flag");
341 private string GetPathList (ArrayList list
)
343 if (IsUnrestricted ())
347 StringBuilder sb
= new StringBuilder ();
348 foreach (string path
in list
) {
353 string result
= sb
.ToString ();
355 int n
= result
.Length
;
357 return result
.Substring (0, n
- 1);
362 #endregion // Methods