Merge pull request #2377 from joelmartinez/docs-multiassembly-extension-fix
[mono.git] / mcs / class / referencesource / mscorlib / system / security / permissions / environmentpermission.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 //  EnvironmentPermission.cs
7 // 
8 // <OWNER>[....]</OWNER>
9 //
10
11 namespace System.Security.Permissions {
12     using System.Security;
13     using System;
14     using SecurityElement = System.Security.SecurityElement;
15     using System.Security.Util;
16     using System.IO;
17     using System.Globalization;
18     using System.Diagnostics.Contracts;
19
20     [Serializable]
21     [Flags]
22     [System.Runtime.InteropServices.ComVisible(true)]
23     public enum EnvironmentPermissionAccess
24     {
25         NoAccess = 0x00,
26         Read = 0x01,
27         Write = 0x02,
28         AllAccess = 0x03,
29     }
30     
31     [Serializable]
32     internal class EnvironmentStringExpressionSet : StringExpressionSet
33     {
34         public EnvironmentStringExpressionSet()
35             : base( true, null, false )
36         {
37         }
38         
39         public EnvironmentStringExpressionSet( String str )
40             : base( true, str, false )
41         {
42         }
43         
44         protected override StringExpressionSet CreateNewEmpty()
45         {
46             return new EnvironmentStringExpressionSet();
47         }
48
49         protected override bool StringSubsetString( String left, String right, bool ignoreCase )
50         {
51             return (ignoreCase?(String.Compare( left, right, StringComparison.OrdinalIgnoreCase) == 0):
52                 (String.Compare( left, right, StringComparison.Ordinal) == 0));
53         }
54
55         protected override String ProcessWholeString( String str )
56         {
57             return str;
58         }
59
60         protected override String ProcessSingleString( String str )
61         {
62             return str;
63         }
64
65         [SecuritySafeCritical]
66         public override string ToString()
67         {
68             // SafeCritical: we're not storing path information in the strings, so exposing them out is fine ...
69             // they're just the same strings that came in to the .ctor.
70             return base.UnsafeToString();
71         }
72     }
73     
74 [System.Runtime.InteropServices.ComVisible(true)]
75     [Serializable]
76     sealed public class EnvironmentPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission
77     {
78         private StringExpressionSet m_read;
79         private StringExpressionSet m_write;
80         private bool m_unrestricted;
81     
82         public EnvironmentPermission(PermissionState state)
83         {
84             if (state == PermissionState.Unrestricted)
85                 m_unrestricted = true;
86             else if (state == PermissionState.None)
87                 m_unrestricted = false;
88             else
89                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
90         }
91
92         public EnvironmentPermission( EnvironmentPermissionAccess flag, String pathList )
93         {
94             SetPathList( flag, pathList );
95         }
96         
97         public void SetPathList( EnvironmentPermissionAccess flag, String pathList )
98         {
99             VerifyFlag( flag );
100             
101             m_unrestricted = false;
102
103             if ((flag & EnvironmentPermissionAccess.Read) != 0)
104                 m_read = null;
105             
106             if ((flag & EnvironmentPermissionAccess.Write) != 0)
107                 m_write = null;
108             
109             AddPathList( flag, pathList );
110         }
111         
112         [System.Security.SecuritySafeCritical]  // auto-generated
113         public void AddPathList( EnvironmentPermissionAccess flag, String pathList )
114         {
115             VerifyFlag( flag );
116             
117             if (FlagIsSet( flag, EnvironmentPermissionAccess.Read ))
118             {
119                 if (m_read == null)
120                     m_read = new EnvironmentStringExpressionSet();
121                 m_read.AddExpressions( pathList );
122             }
123             
124             if (FlagIsSet( flag, EnvironmentPermissionAccess.Write ))
125             {
126                 if (m_write == null)
127                     m_write = new EnvironmentStringExpressionSet();
128                 m_write.AddExpressions( pathList );
129             }
130     
131         }
132     
133         public String GetPathList( EnvironmentPermissionAccess flag )
134         {
135             VerifyFlag( flag );
136             ExclusiveFlag( flag );
137     
138             if (FlagIsSet( flag, EnvironmentPermissionAccess.Read ))
139             {
140                 if (m_read == null)
141                 {
142                     return "";
143                 }
144                 return m_read.ToString();
145             }
146             
147             if (FlagIsSet( flag, EnvironmentPermissionAccess.Write ))
148             {
149                 if (m_write == null)
150                 {
151                     return "";
152                 }
153                 return m_write.ToString();
154             }
155     
156             /* not reached */
157             
158             return "";
159         }     
160         
161             
162         private void VerifyFlag( EnvironmentPermissionAccess flag )
163         {
164             if ((flag & ~EnvironmentPermissionAccess.AllAccess) != 0)
165                 throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)flag));
166             Contract.EndContractBlock();
167         }
168     
169         private void ExclusiveFlag( EnvironmentPermissionAccess flag )
170         {
171             if (flag == EnvironmentPermissionAccess.NoAccess)
172             {
173                 throw new ArgumentException( Environment.GetResourceString("Arg_EnumNotSingleFlag") ); 
174             }
175     
176             if (((int)flag & ((int)flag-1)) != 0)
177             {
178                 throw new ArgumentException( Environment.GetResourceString("Arg_EnumNotSingleFlag") );
179             }
180             Contract.EndContractBlock();
181         }
182         
183         
184         private bool FlagIsSet( EnvironmentPermissionAccess flag, EnvironmentPermissionAccess question )
185         {
186             return (flag & question) != 0;
187         }
188         
189         private bool IsEmpty()
190         {
191             return (!m_unrestricted &&
192                     (this.m_read == null || this.m_read.IsEmpty()) &&
193                     (this.m_write == null || this.m_write.IsEmpty()));
194         }
195         
196         //------------------------------------------------------
197         //
198         // CODEACCESSPERMISSION IMPLEMENTATION
199         //
200         //------------------------------------------------------
201         
202         public bool IsUnrestricted()
203         {
204             return m_unrestricted;
205         }
206         
207         //------------------------------------------------------
208         //
209         // IPERMISSION IMPLEMENTATION
210         //
211         //------------------------------------------------------
212         
213         [System.Security.SecuritySafeCritical]  // auto-generated
214         public override bool IsSubsetOf(IPermission target)
215         {
216             if (target == null)
217             {
218                 return this.IsEmpty();
219             }
220
221             try
222             {
223                 EnvironmentPermission operand = (EnvironmentPermission)target;
224                 if (operand.IsUnrestricted())
225                     return true;
226                 else if (this.IsUnrestricted())
227                     return false;
228                 else
229                     return ((this.m_read == null || this.m_read.IsSubsetOf( operand.m_read )) &&
230                             (this.m_write == null || this.m_write.IsSubsetOf( operand.m_write )));
231             }
232             catch (InvalidCastException)
233             {
234                 throw new 
235                     ArgumentException(
236                                     Environment.GetResourceString("Argument_WrongType", this.GetType().FullName)
237                                      );
238             }
239         }
240         
241         [System.Security.SecuritySafeCritical]  // auto-generated
242         public override IPermission Intersect(IPermission target)
243         {
244             if (target == null)
245             {
246                 return null;
247             }
248             else if (!VerifyType(target))
249             {
250                 throw new 
251                     ArgumentException(
252                                     Environment.GetResourceString("Argument_WrongType", this.GetType().FullName)
253                                      );
254             }
255             else if (this.IsUnrestricted())
256             {
257                 return target.Copy();
258             }
259     
260             EnvironmentPermission operand = (EnvironmentPermission)target;
261     
262             if (operand.IsUnrestricted())
263             {
264                 return this.Copy();
265             }
266             
267             StringExpressionSet intersectRead = this.m_read == null ? null : this.m_read.Intersect( operand.m_read );
268             StringExpressionSet intersectWrite = this.m_write == null ? null : this.m_write.Intersect( operand.m_write );
269             
270             if ((intersectRead == null || intersectRead.IsEmpty()) &&
271                 (intersectWrite == null || intersectWrite.IsEmpty()))
272             {
273                 return null;
274             }
275             
276             EnvironmentPermission intersectPermission = new EnvironmentPermission(PermissionState.None);
277             intersectPermission.m_unrestricted = false;
278             intersectPermission.m_read = intersectRead;
279             intersectPermission.m_write = intersectWrite;
280             
281             return intersectPermission;
282         }
283         
284         [System.Security.SecuritySafeCritical]  // auto-generated
285         public override IPermission Union(IPermission other)
286         {
287             if (other == null)
288             {
289                 return this.Copy();
290             }
291             else if (!VerifyType(other))
292             {
293                 throw new 
294                     ArgumentException(
295                                     Environment.GetResourceString("Argument_WrongType", this.GetType().FullName)
296                                      );
297             }
298     
299             EnvironmentPermission operand = (EnvironmentPermission)other;
300            
301             if (this.IsUnrestricted() || operand.IsUnrestricted())
302             {
303                 return new EnvironmentPermission( PermissionState.Unrestricted );
304             }
305     
306             StringExpressionSet unionRead = this.m_read == null ? operand.m_read : this.m_read.Union( operand.m_read );
307             StringExpressionSet unionWrite = this.m_write == null ? operand.m_write : this.m_write.Union( operand.m_write );
308             
309             if ((unionRead == null || unionRead.IsEmpty()) &&
310                 (unionWrite == null || unionWrite.IsEmpty()))
311             {
312                 return null;
313             }
314             
315             EnvironmentPermission unionPermission = new EnvironmentPermission(PermissionState.None);
316             unionPermission.m_unrestricted = false;
317             unionPermission.m_read = unionRead;
318             unionPermission.m_write = unionWrite;
319             
320             return unionPermission;
321         }    
322         
323         public override IPermission Copy()
324         {
325             EnvironmentPermission copy = new EnvironmentPermission(PermissionState.None);
326             if (this.m_unrestricted)
327             {
328                 copy.m_unrestricted = true;
329             }
330             else
331             {
332                 copy.m_unrestricted = false;
333                 if (this.m_read != null)
334                 {
335                     copy.m_read = this.m_read.Copy();
336                 }
337                 if (this.m_write != null)
338                 {
339                     copy.m_write = this.m_write.Copy();
340                 }
341     
342             }
343             return copy;   
344         }
345        
346 #if FEATURE_CAS_POLICY
347         public override SecurityElement ToXml()
348         {
349             SecurityElement esd = CodeAccessPermission.CreatePermissionElement( this, "System.Security.Permissions.EnvironmentPermission" );
350             if (!IsUnrestricted())
351             {
352                 if (this.m_read != null && !this.m_read.IsEmpty())
353                 {
354                     esd.AddAttribute( "Read", SecurityElement.Escape( m_read.ToString() ) );
355                 }
356                 if (this.m_write != null && !this.m_write.IsEmpty())
357                 {
358                     esd.AddAttribute( "Write", SecurityElement.Escape( m_write.ToString() ) );
359                 }
360             }
361             else
362             {
363                 esd.AddAttribute( "Unrestricted", "true" );
364             }
365             return esd;
366         }
367
368         public override void FromXml(SecurityElement esd)
369         {
370             CodeAccessPermission.ValidateElement( esd, this );
371
372             String et;
373             
374             if (XMLUtil.IsUnrestricted(esd))
375             {
376                 m_unrestricted = true;
377                 return;
378             }
379
380             m_unrestricted = false;
381             m_read = null;
382             m_write = null;
383
384             et = esd.Attribute( "Read" );
385             if (et != null)
386             {
387                 m_read = new EnvironmentStringExpressionSet( et );
388             }
389             
390             et = esd.Attribute( "Write" );
391             if (et != null)
392             {
393                 m_write = new EnvironmentStringExpressionSet( et );
394             }
395     
396         }
397 #endif // FEATURE_CAS_POLICY
398
399         /// <internalonly/>
400         int IBuiltInPermission.GetTokenIndex()
401         {
402             return EnvironmentPermission.GetTokenIndex();
403         }
404
405         internal static int GetTokenIndex()
406         {
407             return BuiltInPermissionIndex.EnvironmentPermissionIndex;
408         }
409     }
410
411 }