This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / System.Security.Permissions / EnvironmentPermission.cs
1 //
2 // System.Security.Permissions.EnvironmentPermission.cs
3 //
4 // Authors:
5 //      Tim Coleman <tim@timcoleman.com>
6 //      Sebastien Pouliot <spouliot@motus.com>
7 //
8 // Copyright (C) 2002, Tim Coleman
9 // Portions Copyright (C) 2003 Motus Technologies (http://www.motus.com)
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Collections;
37 using System.Security.Permissions;
38 using System.Text;
39
40 namespace System.Security.Permissions {
41
42         [Serializable]
43         public sealed class EnvironmentPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
44
45                 #region Fields
46
47                 EnvironmentPermissionAccess flags;
48                 PermissionState _state;
49                 ArrayList readList;
50                 ArrayList writeList;
51
52                 #endregion // Fields
53
54                 #region Constructors
55
56                 public EnvironmentPermission (PermissionState state) : base ()
57                 {
58                         switch (state) {
59                                 case PermissionState.None:
60                                 case PermissionState.Unrestricted:
61                                         _state = state;
62                                         readList = new ArrayList ();
63                                         writeList = new ArrayList ();
64                                         break;
65                                 default:
66                                         throw new ArgumentException ("Invalid PermissionState", "state");
67                         }
68                 }
69
70                 public EnvironmentPermission (EnvironmentPermissionAccess flag, string pathList) : base ()
71                 {
72                         readList = new ArrayList ();
73                         writeList = new ArrayList ();
74                         SetPathList (flag, pathList);
75                 }
76
77                 #endregion // Constructors
78
79                 #region Methods
80
81                 public void AddPathList (EnvironmentPermissionAccess flag, string pathList)
82                 {
83                         if (pathList == null)
84                                 throw new ArgumentNullException ("pathList");
85                         string[] paths;
86                         switch (flag) {
87                                 case EnvironmentPermissionAccess.AllAccess:
88                                         paths = pathList.Split (';');
89                                         foreach (string path in paths) {
90                                                 if (!readList.Contains (path))
91                                                         readList.Add (path);
92                                                 if (!writeList.Contains (path))
93                                                         writeList.Add (path);
94                                         }
95                                         break;
96                                 case EnvironmentPermissionAccess.NoAccess:
97                                         // ??? unit tests doesn't show removal using NoAccess ???
98                                         break;
99                                 case EnvironmentPermissionAccess.Read:
100                                         paths = pathList.Split (';');
101                                         foreach (string path in paths) {
102                                                 if (!readList.Contains (path))
103                                                         readList.Add (path);
104                                         }
105                                         break;
106                                 case EnvironmentPermissionAccess.Write:
107                                         paths = pathList.Split (';');
108                                         foreach (string path in paths) {
109                                                 if (!writeList.Contains (path))
110                                                         writeList.Add (path);
111                                         }
112                                         break;
113                                 default:
114                                         throw new ArgumentException ("Invalid EnvironmentPermissionAccess", "flag");
115                         }
116                 }
117
118                 public override IPermission Copy ()
119                 {
120                         EnvironmentPermission ep = new EnvironmentPermission (_state);
121                         string path = GetPathList (EnvironmentPermissionAccess.Read);
122                         if (path != null)
123                                 ep.SetPathList (EnvironmentPermissionAccess.Read, path);
124                         path = GetPathList (EnvironmentPermissionAccess.Write);
125                         if (path != null)
126                                 ep.SetPathList (EnvironmentPermissionAccess.Write, path);
127                         return ep;
128                 }
129
130                 public override void FromXml (SecurityElement esd)
131                 {
132                         if (esd == null)
133                                 throw new ArgumentNullException ("esd");
134                         if (esd.Tag != "IPermission")
135                                 throw new ArgumentException ("not IPermission");
136                         if (!(esd.Attributes ["class"] as string).StartsWith ("System.Security.Permissions.EnvironmentPermission"))
137                                 throw new ArgumentException ("not EnvironmentPermission");
138                         if ((esd.Attributes ["version"] as string) != "1")
139                                 throw new ArgumentException ("wrong version");
140
141                         string read = (esd.Attributes ["Read"] as string);
142                         if ((read != null) && (read.Length > 0))
143                                 SetPathList (EnvironmentPermissionAccess.Read, read);
144                         string write = (esd.Attributes ["Write"] as string);
145                         if ((write != null) && (write.Length > 0))
146                                 SetPathList (EnvironmentPermissionAccess.Write, write);
147
148                         // Unrestricted ???
149                 }
150
151                 public string GetPathList (EnvironmentPermissionAccess flag)
152                 {
153                         StringBuilder sb = new StringBuilder ();
154                         switch (flag) {
155                                 case EnvironmentPermissionAccess.AllAccess:
156                                 case EnvironmentPermissionAccess.NoAccess:
157                                         throw new ArgumentException ("Invalid EnvironmentPermissionAccess in context", "flag");
158                                 case EnvironmentPermissionAccess.Read:
159                                         foreach (string path in readList) {
160                                                 sb.Append (path);
161                                                 sb.Append (";");
162                                         }
163                                         break;
164                                 case EnvironmentPermissionAccess.Write:
165                                         foreach (string path in writeList) {
166                                                 sb.Append (path);
167                                                 sb.Append (";");
168                                         }
169                                         break;
170                                 default:
171                                         throw new ArgumentException ("Unknown EnvironmentPermissionAccess", "flag");
172                         }
173                         string result = sb.ToString ();
174                         // remove last ';'
175                         int n = result.Length;
176                         if (n > 0)
177                                 return result.Substring (0, n - 1);
178                         return ((_state == PermissionState.Unrestricted) ? String.Empty : null);
179                 }
180
181                 public override IPermission Intersect (IPermission target)
182                 {
183                         if (target == null)
184                                 return null;
185                         if (! (target is EnvironmentPermission))
186                                 throw new ArgumentException ("wrong type");
187
188                         EnvironmentPermission o = (EnvironmentPermission) target;
189                         int n = 0;
190                         if (IsUnrestricted ())
191                                 return o.Copy ();
192                         if (o.IsUnrestricted ())
193                                 return Copy ();
194
195                         EnvironmentPermission ep = new EnvironmentPermission (PermissionState.None);
196                         string readTarget = o.GetPathList (EnvironmentPermissionAccess.Read);
197                         if (readTarget != null) {
198                                 string[] targets = readTarget.Split (';');
199                                 foreach (string t in targets) {
200                                         if (readList.Contains (t)) {
201                                                 ep.AddPathList (EnvironmentPermissionAccess.Read, t);
202                                                 n++;
203                                         }
204                                 }
205                         }
206
207                         string writeTarget = o.GetPathList (EnvironmentPermissionAccess.Write);
208                         if (writeTarget != null) {
209                                 string[] targets = writeTarget.Split (';');
210                                 foreach (string t in targets) {
211                                         if (writeList.Contains (t)) {
212                                                 ep.AddPathList (EnvironmentPermissionAccess.Write, t);
213                                                 n++;
214                                         }
215                                 }
216                         }
217                         return ((n > 0) ? ep : null);
218                 }
219
220                 public override bool IsSubsetOf (IPermission target)
221                 {
222                         if (target == null)
223                                 return false;
224
225                         if (! (target is EnvironmentPermission))
226                                 throw new ArgumentException ("wrong type");
227
228                         EnvironmentPermission o = (EnvironmentPermission) target;
229                         if (IsUnrestricted ())
230                                 return o.IsUnrestricted ();
231                         else if (o.IsUnrestricted ())
232                                 return true;
233
234                         foreach (string s in readList) {
235                                 if (!o.readList.Contains (s))
236                                         return false;
237                         }
238
239                         foreach (string s in writeList) {
240                                 if (!o.writeList.Contains (s))
241                                         return false;
242                         }
243
244                         return true;
245                 }
246
247                 public bool IsUnrestricted ()
248                 {
249                         return (_state == PermissionState.Unrestricted);
250                 }
251
252                 public void SetPathList (EnvironmentPermissionAccess flag, string pathList)
253                 {
254                         if (pathList == null)
255                                 throw new ArgumentNullException ("pathList");
256                         string[] paths;
257                         switch (flag) {
258                                 case EnvironmentPermissionAccess.AllAccess:
259                                         readList.Clear ();
260                                         writeList.Clear ();
261                                         paths = pathList.Split (';');
262                                         foreach (string path in paths) {
263                                                 readList.Add (path);
264                                                 writeList.Add (path);
265                                         }
266                                         break;
267                                 case EnvironmentPermissionAccess.NoAccess:
268                                         // ??? unit tests doesn't show removal using NoAccess ???
269                                         break;
270                                 case EnvironmentPermissionAccess.Read:
271                                         readList.Clear ();
272                                         paths = pathList.Split (';');
273                                         foreach (string path in paths) {
274                                                 readList.Add (path);
275                                         }
276                                         break;
277                                 case EnvironmentPermissionAccess.Write:
278                                         writeList.Clear ();
279                                         paths = pathList.Split (';');
280                                         foreach (string path in paths) {
281                                                 writeList.Add (path);
282                                         }
283                                         break;
284                                 default:
285                                         throw new ArgumentException ("Invalid EnvironmentPermissionAccess", "flag");
286                         }
287                 }
288
289                 public override SecurityElement ToXml ()
290                 {
291                         SecurityElement se = Element (this, 1);
292                         if (_state == PermissionState.Unrestricted) {
293                                 se.AddAttribute ("Unrestricted", "true");
294                         }
295                         else {
296                                 string path = GetPathList (EnvironmentPermissionAccess.Read);
297                                 if (path != null)
298                                         se.AddAttribute ("Read", path);
299                                 path = GetPathList (EnvironmentPermissionAccess.Write);
300                                 if (path != null)
301                                         se.AddAttribute ("Write", path);
302                         }
303                         return se;
304                 }
305
306                 public override IPermission Union (IPermission other)
307                 {
308                         if (other == null)
309                                 return Copy ();
310                         if (! (other is EnvironmentPermission))
311                                 throw new ArgumentException ("wrong type");
312
313                         EnvironmentPermission o = (EnvironmentPermission) other;
314                         if (IsUnrestricted () || o.IsUnrestricted ())
315                                 return new EnvironmentPermission (PermissionState.Unrestricted);
316
317                         EnvironmentPermission ep = (EnvironmentPermission) Copy ();
318                         string path = o.GetPathList (EnvironmentPermissionAccess.Read);
319                         if (path != null) 
320                                 ep.AddPathList (EnvironmentPermissionAccess.Read, path);
321                         path = o.GetPathList (EnvironmentPermissionAccess.Write);
322                         if (path != null)
323                                 ep.AddPathList (EnvironmentPermissionAccess.Write, path);
324                         return ep;
325                 }
326
327                 // IBuiltInPermission
328                 int IBuiltInPermission.GetTokenIndex ()
329                 {
330                         return 0;
331                 }
332
333                 #endregion // Methods
334         }
335 }