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 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
;
35 namespace System
.Security
.Permissions
{
38 public sealed class EnvironmentPermission
: CodeAccessPermission
, IUnrestrictedPermission
, IBuiltInPermission
{
42 private const int version
= 1;
44 EnvironmentPermissionAccess flags
;
45 PermissionState _state
;
53 public EnvironmentPermission (PermissionState state
) : base ()
55 _state
= CheckPermissionState (state
, true);
56 readList
= new ArrayList ();
57 writeList
= new ArrayList ();
60 public EnvironmentPermission (EnvironmentPermissionAccess flag
, string pathList
) : base ()
62 readList
= new ArrayList ();
63 writeList
= new ArrayList ();
64 SetPathList (flag
, pathList
);
67 #endregion // Constructors
71 public void AddPathList (EnvironmentPermissionAccess flag
, string pathList
)
74 throw new ArgumentNullException ("pathList");
78 case EnvironmentPermissionAccess
.AllAccess
:
79 paths
= pathList
.Split (';');
80 foreach (string path
in paths
) {
81 if (!readList
.Contains (path
))
83 if (!writeList
.Contains (path
))
87 case EnvironmentPermissionAccess
.NoAccess
:
88 // ??? unit tests doesn't show removal using NoAccess ???
90 case EnvironmentPermissionAccess
.Read
:
91 paths
= pathList
.Split (';');
92 foreach (string path
in paths
) {
93 if (!readList
.Contains (path
))
97 case EnvironmentPermissionAccess
.Write
:
98 paths
= pathList
.Split (';');
99 foreach (string path
in paths
) {
100 if (!writeList
.Contains (path
))
101 writeList
.Add (path
);
105 ThrowInvalidFlag (flag
, false);
110 public override IPermission
Copy ()
112 EnvironmentPermission ep
= new EnvironmentPermission (_state
);
113 string path
= GetPathList (EnvironmentPermissionAccess
.Read
);
115 ep
.SetPathList (EnvironmentPermissionAccess
.Read
, path
);
116 path
= GetPathList (EnvironmentPermissionAccess
.Write
);
118 ep
.SetPathList (EnvironmentPermissionAccess
.Write
, path
);
122 public override void FromXml (SecurityElement esd
)
124 // General validation in CodeAccessPermission
125 CheckSecurityElement (esd
, "esd", version
, version
);
126 // Note: we do not (yet) care about the return value
127 // as we only accept version 1 (min/max values)
129 if (IsUnrestricted (esd
))
130 _state
= PermissionState
.Unrestricted
;
132 string read
= esd
.Attribute ("Read");
133 if ((read
!= null) && (read
.Length
> 0))
134 SetPathList (EnvironmentPermissionAccess
.Read
, read
);
136 string write
= esd
.Attribute ("Write");
137 if ((write
!= null) && (write
.Length
> 0))
138 SetPathList (EnvironmentPermissionAccess
.Write
, write
);
141 public string GetPathList (EnvironmentPermissionAccess flag
)
144 case EnvironmentPermissionAccess
.AllAccess
:
145 case EnvironmentPermissionAccess
.NoAccess
:
146 ThrowInvalidFlag (flag
, true);
148 case EnvironmentPermissionAccess
.Read
:
149 return GetPathList (readList
);
150 case EnvironmentPermissionAccess
.Write
:
151 return GetPathList (writeList
);
153 ThrowInvalidFlag (flag
, false);
156 return null; // never reached
159 public override IPermission
Intersect (IPermission target
)
161 EnvironmentPermission ep
= Cast (target
);
165 if (IsUnrestricted ())
167 if (ep
.IsUnrestricted ())
171 EnvironmentPermission result
= new EnvironmentPermission (PermissionState
.None
);
172 string readTarget
= ep
.GetPathList (EnvironmentPermissionAccess
.Read
);
173 if (readTarget
!= null) {
174 string[] targets
= readTarget
.Split (';');
175 foreach (string t
in targets
) {
176 if (readList
.Contains (t
)) {
177 result
.AddPathList (EnvironmentPermissionAccess
.Read
, t
);
183 string writeTarget
= ep
.GetPathList (EnvironmentPermissionAccess
.Write
);
184 if (writeTarget
!= null) {
185 string[] targets
= writeTarget
.Split (';');
186 foreach (string t
in targets
) {
187 if (writeList
.Contains (t
)) {
188 result
.AddPathList (EnvironmentPermissionAccess
.Write
, t
);
193 return ((n
> 0) ? result
: null);
196 public override bool IsSubsetOf (IPermission target
)
198 EnvironmentPermission ep
= Cast (target
);
202 if (IsUnrestricted ())
203 return ep
.IsUnrestricted ();
204 else if (ep
.IsUnrestricted ())
207 foreach (string s
in readList
) {
208 if (!ep
.readList
.Contains (s
))
212 foreach (string s
in writeList
) {
213 if (!ep
.writeList
.Contains (s
))
220 public bool IsUnrestricted ()
222 return (_state
== PermissionState
.Unrestricted
);
225 public void SetPathList (EnvironmentPermissionAccess flag
, string pathList
)
227 if (pathList
== null)
228 throw new ArgumentNullException ("pathList");
231 case EnvironmentPermissionAccess
.AllAccess
:
234 paths
= pathList
.Split (';');
235 foreach (string path
in paths
) {
237 writeList
.Add (path
);
240 case EnvironmentPermissionAccess
.NoAccess
:
241 // ??? unit tests doesn't show removal using NoAccess ???
243 case EnvironmentPermissionAccess
.Read
:
245 paths
= pathList
.Split (';');
246 foreach (string path
in paths
) {
250 case EnvironmentPermissionAccess
.Write
:
252 paths
= pathList
.Split (';');
253 foreach (string path
in paths
) {
254 writeList
.Add (path
);
258 ThrowInvalidFlag (flag
, false);
263 public override SecurityElement
ToXml ()
265 SecurityElement se
= Element (version
);
267 if (_state
== PermissionState
.Unrestricted
) {
268 se
.AddAttribute ("Unrestricted", "true");
271 string path
= GetPathList (EnvironmentPermissionAccess
.Read
);
273 se
.AddAttribute ("Read", path
);
274 path
= GetPathList (EnvironmentPermissionAccess
.Write
);
276 se
.AddAttribute ("Write", path
);
281 public override IPermission
Union (IPermission other
)
283 EnvironmentPermission ep
= Cast (other
);
287 if (IsUnrestricted () || ep
.IsUnrestricted ())
288 return new EnvironmentPermission (PermissionState
.Unrestricted
);
290 if (IsEmpty () && ep
.IsEmpty ())
293 EnvironmentPermission result
= (EnvironmentPermission
) Copy ();
294 string path
= ep
.GetPathList (EnvironmentPermissionAccess
.Read
);
296 result
.AddPathList (EnvironmentPermissionAccess
.Read
, path
);
297 path
= ep
.GetPathList (EnvironmentPermissionAccess
.Write
);
299 result
.AddPathList (EnvironmentPermissionAccess
.Write
, path
);
303 // IBuiltInPermission
304 int IBuiltInPermission
.GetTokenIndex ()
306 return (int) BuiltInToken
.Environment
;
311 private bool IsEmpty ()
313 return ((_state
== PermissionState
.None
) && (readList
.Count
== 0) && (writeList
.Count
== 0));
316 private EnvironmentPermission
Cast (IPermission target
)
321 EnvironmentPermission ep
= (target
as EnvironmentPermission
);
323 ThrowInvalidPermission (target
, typeof (EnvironmentPermission
));
329 internal void ThrowInvalidFlag (EnvironmentPermissionAccess flag
, bool context
)
333 msg
= Locale
.GetText ("Unknown flag '{0}'.");
335 msg
= Locale
.GetText ("Invalid flag '{0}' in this context.");
336 throw new ArgumentException (String
.Format (msg
, flag
), "flag");
339 private string GetPathList (ArrayList list
)
341 if (IsUnrestricted ())
350 StringBuilder sb
= new StringBuilder ();
351 foreach (string path
in list
) {
356 string result
= sb
.ToString ();
358 int n
= result
.Length
;
360 return result
.Substring (0, n
- 1);
364 return ((_state
== PermissionState
.Unrestricted
) ? String
.Empty
: null);
368 #endregion // Methods