[System] Added NetworkCredential.SecurePassword.
[mono.git] / mcs / class / System / System.Security.Permissions / ResourcePermissionBase.cs
1 //
2 // System.Security.Permissions.ResourcePermissionBase.cs
3 //
4 // Authors:
5 //      Jonathan Pryor (jonpryor@vt.edu)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2002
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.Globalization;
33
34 namespace System.Security.Permissions {
35
36         [Serializable]
37         public abstract class ResourcePermissionBase : CodeAccessPermission, IUnrestrictedPermission {
38
39                 private const int version = 1;
40
41                 private ArrayList _list;
42                 private bool _unrestricted;
43                 private Type _type;
44                 private string[] _tags;
45
46                 protected ResourcePermissionBase ()
47                 {
48                         _list = new ArrayList ();
49                 }
50
51                 protected ResourcePermissionBase (PermissionState state) : this ()
52                 {
53 #if NET_2_0
54                         PermissionHelper.CheckPermissionState (state, true);
55 #else
56                         // there are no validation of the permission state
57                         // but any invalid value results in a restricted set
58 #endif
59                         _unrestricted = (state == PermissionState.Unrestricted);
60                 }
61
62                 public const string Any = "*";
63                 public const string Local = ".";
64
65                 protected Type PermissionAccessType {
66                         get { return _type; }
67                         set {
68                                 if (value == null)
69                                         throw new ArgumentNullException ("PermissionAccessType");
70                                 if (!value.IsEnum)
71                                         throw new ArgumentException ("!Enum", "PermissionAccessType");
72                                 _type = value;
73                         }
74                 }
75
76                 protected string[] TagNames {
77                         get { return _tags; }
78                         set {
79                                 if (value == null)
80                                         throw new ArgumentNullException ("TagNames");
81                                 if (value.Length == 0)
82                                         throw new ArgumentException ("Length==0", "TagNames");
83                                 _tags = value;
84                         }
85                 }
86
87                 protected void AddPermissionAccess (ResourcePermissionBaseEntry entry)
88                 {
89                         CheckEntry (entry);
90                         if (Exists (entry)) {
91                                 string msg = Locale.GetText ("Entry already exists.");
92                                 throw new InvalidOperationException (msg);
93                         }
94
95                         _list.Add (entry);
96                 }
97
98                 protected void Clear ()
99                 {
100                         _list.Clear ();
101                 }
102
103                 public override IPermission Copy ()
104                 {
105                         ResourcePermissionBase copy = CreateFromType (this.GetType (), _unrestricted);
106                         if (_tags != null)
107                                 copy._tags = (string[]) _tags.Clone ();
108                         copy._type = _type;
109                         // FIXME: shallow or deep copy ?
110                         copy._list.AddRange (_list);
111                         return copy;
112                 }
113
114                 [MonoTODO ("incomplete - need more test")]
115                 public override void FromXml (SecurityElement securityElement)
116                 {
117 #if NET_2_0
118                         if (securityElement == null)
119                                 throw new ArgumentNullException ("securityElement");
120 #else
121                         if (securityElement == null)
122                                 throw new NullReferenceException ("securityElement");
123 #endif
124                         CheckSecurityElement (securityElement, "securityElement", version, version);
125                         // Note: we do not (yet) care about the return value 
126                         // as we only accept version 1 (min/max values)
127
128                         _list.Clear ();
129                         _unrestricted = PermissionHelper.IsUnrestricted (securityElement);
130                         if ((securityElement.Children == null) || (securityElement.Children.Count < 1))
131                                 return;
132
133                         string[] names = new string [1];
134                         foreach (SecurityElement child in securityElement.Children) {
135                                 // TODO: handle multiple names
136                                 names [0] = child.Attribute ("name");
137                                 int access = (int) Enum.Parse (PermissionAccessType, child.Attribute ("access"));
138                                 ResourcePermissionBaseEntry entry = new ResourcePermissionBaseEntry (access, names);
139                                 AddPermissionAccess (entry);
140                         }
141                 }
142
143                 protected ResourcePermissionBaseEntry[] GetPermissionEntries ()
144                 {
145                         ResourcePermissionBaseEntry[] entries = new ResourcePermissionBaseEntry [_list.Count];
146                         _list.CopyTo (entries, 0);
147                         return entries;
148                 }
149
150                 public override IPermission Intersect (IPermission target)
151                 {
152                         ResourcePermissionBase rpb = Cast (target);
153                         if (rpb == null)
154                                 return null;
155
156                         bool su = this.IsUnrestricted ();
157                         bool tu = rpb.IsUnrestricted ();
158
159                         // if one is empty we return null (unless the other one is unrestricted)
160                         if (IsEmpty () && !tu)
161                                 return null;
162                         if (rpb.IsEmpty () && !su)
163                                 return null;
164
165                         ResourcePermissionBase result = CreateFromType (this.GetType (), (su && tu));
166                         foreach (ResourcePermissionBaseEntry entry in _list) {
167                                 if (tu || rpb.Exists (entry))
168                                         result.AddPermissionAccess (entry);
169                         }
170                         foreach (ResourcePermissionBaseEntry entry in rpb._list) {
171                                 // don't add twice
172                                 if ((su || this.Exists (entry)) && !result.Exists (entry))
173                                         result.AddPermissionAccess (entry);
174                         }
175                         return result;
176                 }
177
178                 public override bool IsSubsetOf (IPermission target)
179                 {
180                         if (target == null) {
181 #if NET_2_0
182                                 // do not use Cast - different permissions (and earlier Fx) return false :-/
183                                 return true;
184 #else
185                                 return false;
186 #endif
187                         }
188
189                         ResourcePermissionBase rpb = (target as ResourcePermissionBase);
190                         if (rpb == null)
191                                 return false;
192                         if (rpb.IsUnrestricted ())
193                                 return true;
194                         if (IsUnrestricted ())
195                                 return rpb.IsUnrestricted ();
196                         foreach (ResourcePermissionBaseEntry entry in _list) {
197                                 if (!rpb.Exists (entry))
198                                         return false;
199                         }
200                         return true;
201                 }
202
203                 public bool IsUnrestricted ()
204                 {
205                         return _unrestricted;
206                 }
207
208                 protected void RemovePermissionAccess (ResourcePermissionBaseEntry entry)
209                 {
210                         CheckEntry (entry);
211                         for (int i = 0; i < _list.Count; i++) {
212                                 ResourcePermissionBaseEntry rpbe = (ResourcePermissionBaseEntry) _list [i];
213                                 if (Equals (entry, rpbe)) {
214                                         _list.RemoveAt (i);
215                                         return;
216                                 }
217                         }
218                         string msg = Locale.GetText ("Entry doesn't exists.");
219                         throw new InvalidOperationException (msg);
220                 }
221
222                 public override SecurityElement ToXml ()
223                 {
224                         SecurityElement se = PermissionHelper.Element (this.GetType (), version);
225                         if (IsUnrestricted ()) {
226                                 se.AddAttribute ("Unrestricted", "true");
227                         }
228                         else {
229                                 foreach (ResourcePermissionBaseEntry entry in _list) {
230                                         SecurityElement container = se;
231                                         string access = null;
232                                         if (PermissionAccessType != null)
233                                                 access = Enum.Format (PermissionAccessType, entry.PermissionAccess, "g");
234
235                                         for (int i=0; i < _tags.Length; i++) {
236                                                 SecurityElement child = new SecurityElement (_tags [i]);
237                                                 child.AddAttribute ("name", entry.PermissionAccessPath [i]);
238                                                 if (access != null)
239                                                         child.AddAttribute ("access", access);
240                                                 container.AddChild (child);
241                                                 child = container;
242                                         }
243                                 }
244                         }
245                         return se;
246                 }
247
248                 public override IPermission Union (IPermission target)
249                 {
250                         ResourcePermissionBase rpb = Cast (target);
251                         if (rpb == null)
252                                 return Copy ();
253                         if (IsEmpty () && rpb.IsEmpty ())
254                                 return null;
255                         if (rpb.IsEmpty ())
256                                 return Copy ();
257                         if (IsEmpty ())
258                                 return rpb.Copy ();
259
260                         bool unrestricted = (IsUnrestricted () || rpb.IsUnrestricted ());
261                         ResourcePermissionBase result = CreateFromType (this.GetType (), unrestricted);
262                         // strangely unrestricted union doesn't process the elements (while intersect does)
263                         if (!unrestricted) {
264                                 foreach (ResourcePermissionBaseEntry entry in _list) {
265                                         result.AddPermissionAccess (entry);
266                                 }
267                                 foreach (ResourcePermissionBaseEntry entry in rpb._list) {
268                                         // don't add twice
269                                         if (!result.Exists (entry))
270                                                 result.AddPermissionAccess (entry);
271                                 }
272                         }
273                         return result;
274                 }
275
276                 // helpers
277
278                 private bool IsEmpty ()
279                 {
280                         return (!_unrestricted && (_list.Count == 0));
281                 }
282
283                 private ResourcePermissionBase Cast (IPermission target)
284                 {
285                         if (target == null)
286                                 return null;
287
288                         ResourcePermissionBase rp = (target as ResourcePermissionBase);
289                         if (rp == null) {
290                                 PermissionHelper.ThrowInvalidPermission (target, typeof (ResourcePermissionBase));
291                         }
292
293                         return rp;
294                 }
295
296                 internal void CheckEntry (ResourcePermissionBaseEntry entry)
297                 {
298                         if (entry == null)
299                                 throw new ArgumentNullException ("entry");
300                         if ((entry.PermissionAccessPath == null) || (entry.PermissionAccessPath.Length != _tags.Length)) {
301                                 string msg = Locale.GetText ("Entry doesn't match TagNames");
302                                 throw new InvalidOperationException (msg);
303                         }
304                 }
305
306                 internal bool Equals (ResourcePermissionBaseEntry entry1, ResourcePermissionBaseEntry entry2)
307                 {
308                         if (entry1.PermissionAccess != entry2.PermissionAccess)
309                                 return false;
310                         if (entry1.PermissionAccessPath.Length != entry2.PermissionAccessPath.Length)
311                                 return false;
312                         for (int i=0; i < entry1.PermissionAccessPath.Length; i++) {
313                                 if (entry1.PermissionAccessPath [i] != entry2.PermissionAccessPath [i])
314                                         return false;
315                         }
316                         return true;
317                 }
318
319                 internal bool Exists (ResourcePermissionBaseEntry entry)
320                 {
321                         if (_list.Count == 0)
322                                 return false;
323                         foreach (ResourcePermissionBaseEntry rpbe in _list) {
324                                 if (Equals (rpbe, entry))
325                                         return true;
326                         }
327                         return false;
328                 }
329
330                 // logic isn't identical to PermissionHelper.CheckSecurityElement
331                 // - no throw on version mismatch
332                 internal int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion) 
333                 {
334                         if (se == null)
335                                 throw new ArgumentNullException (parameterName);
336 #if NET_2_0
337                         // Tag is case-sensitive
338                         if (se.Tag != "IPermission") {
339                                 string msg = String.Format (Locale.GetText ("Invalid tag {0}"), se.Tag);
340                                 throw new ArgumentException (msg, parameterName);
341                         }
342 #endif
343                         // Note: we do not care about the class attribute at 
344                         // this stage (in fact we don't even if the class 
345                         // attribute is present or not). Anyway the object has
346                         // already be created, with success, if we're loading it
347
348                         // we assume minimum version if no version number is supplied
349                         int version = minimumVersion;
350                         string v = se.Attribute ("version");
351                         if (v != null) {
352                                 try {
353                                         version = Int32.Parse (v);
354                                 }
355                                 catch (Exception e) {
356                                         string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
357                                         msg = String.Format (msg, v);
358                                         throw new ArgumentException (msg, parameterName, e);
359                                 }
360                         }
361 #if NET_2_0
362                         if ((version < minimumVersion) || (version > maximumVersion)) {
363                                 string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
364                                 msg = String.Format (msg, version, minimumVersion, maximumVersion);
365                                 throw new ArgumentException (msg, parameterName);
366                         }
367 #endif
368                         return version;
369                 }
370
371                 // static helpers
372
373                 private static char[] invalidChars = new char[] { '\t', '\n', '\v', '\f', '\r', ' ', '\\', '\x160' };
374
375                 internal static void ValidateMachineName (string name)
376                 {
377                         // FIXME: maybe other checks are required (but not documented)
378                         if ((name == null) || (name.Length == 0) || (name.IndexOfAny (invalidChars) != -1)) {
379                                 string msg = Locale.GetText ("Invalid machine name '{0}'.");
380                                 if (name == null)
381                                         name = "(null)";
382                                 msg = String.Format (msg, name);
383                                 throw new ArgumentException (msg, "MachineName");
384                         }
385                 }
386
387                 internal static ResourcePermissionBase CreateFromType (Type type, bool unrestricted)
388                 {
389                         object[] parameters = new object [1];
390                         parameters [0] = (object) ((unrestricted) ? PermissionState.Unrestricted : PermissionState.None);
391                         // we must return the derived type - this is why an empty constructor is required ;-)
392                         return (ResourcePermissionBase) Activator.CreateInstance (type, parameters);
393                 }
394         }
395 }