mscorlib(4.0.0.0) API with additions
RegistryPermission.cs
4 using System.Security.Util;
5 
7 {
10  [ComVisible(true)]
11  public sealed class RegistryPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission
12  {
13  private StringExpressionSet m_read;
14 
15  private StringExpressionSet m_write;
16 
17  private StringExpressionSet m_create;
18 
19  [OptionalField(VersionAdded = 2)]
20  private StringExpressionSet m_viewAcl;
21 
22  [OptionalField(VersionAdded = 2)]
23  private StringExpressionSet m_changeAcl;
24 
25  private bool m_unrestricted;
26 
31  {
32  switch (state)
33  {
34  case PermissionState.Unrestricted:
35  m_unrestricted = true;
36  break;
37  case PermissionState.None:
38  m_unrestricted = false;
39  break;
40  default:
41  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
42  }
43  }
44 
49  public RegistryPermission(RegistryPermissionAccess access, string pathList)
50  {
51  SetPathList(access, pathList);
52  }
53 
59  public RegistryPermission(RegistryPermissionAccess access, AccessControlActions control, string pathList)
60  {
61  m_unrestricted = false;
62  AddPathList(access, control, pathList);
63  }
64 
69  public void SetPathList(RegistryPermissionAccess access, string pathList)
70  {
71  VerifyAccess(access);
72  m_unrestricted = false;
73  if ((access & RegistryPermissionAccess.Read) != 0)
74  {
75  m_read = null;
76  }
77  if ((access & RegistryPermissionAccess.Write) != 0)
78  {
79  m_write = null;
80  }
81  if ((access & RegistryPermissionAccess.Create) != 0)
82  {
83  m_create = null;
84  }
85  AddPathList(access, pathList);
86  }
87 
88  internal void SetPathList(AccessControlActions control, string pathList)
89  {
90  m_unrestricted = false;
91  if ((control & AccessControlActions.View) != 0)
92  {
93  m_viewAcl = null;
94  }
95  if ((control & AccessControlActions.Change) != 0)
96  {
97  m_changeAcl = null;
98  }
99  AddPathList(RegistryPermissionAccess.NoAccess, control, pathList);
100  }
101 
106  public void AddPathList(RegistryPermissionAccess access, string pathList)
107  {
108  AddPathList(access, AccessControlActions.None, pathList);
109  }
110 
116  [SecuritySafeCritical]
117  public void AddPathList(RegistryPermissionAccess access, AccessControlActions control, string pathList)
118  {
119  VerifyAccess(access);
120  if ((access & RegistryPermissionAccess.Read) != 0)
121  {
122  if (m_read == null)
123  {
124  m_read = new StringExpressionSet();
125  }
126  m_read.AddExpressions(pathList);
127  }
128  if ((access & RegistryPermissionAccess.Write) != 0)
129  {
130  if (m_write == null)
131  {
132  m_write = new StringExpressionSet();
133  }
134  m_write.AddExpressions(pathList);
135  }
136  if ((access & RegistryPermissionAccess.Create) != 0)
137  {
138  if (m_create == null)
139  {
140  m_create = new StringExpressionSet();
141  }
142  m_create.AddExpressions(pathList);
143  }
144  if ((control & AccessControlActions.View) != 0)
145  {
146  if (m_viewAcl == null)
147  {
148  m_viewAcl = new StringExpressionSet();
149  }
150  m_viewAcl.AddExpressions(pathList);
151  }
152  if ((control & AccessControlActions.Change) != 0)
153  {
154  if (m_changeAcl == null)
155  {
156  m_changeAcl = new StringExpressionSet();
157  }
158  m_changeAcl.AddExpressions(pathList);
159  }
160  }
161 
168  [SecuritySafeCritical]
169  public string GetPathList(RegistryPermissionAccess access)
170  {
171  VerifyAccess(access);
172  ExclusiveAccess(access);
173  if ((access & RegistryPermissionAccess.Read) != 0)
174  {
175  if (m_read == null)
176  {
177  return "";
178  }
179  return m_read.UnsafeToString();
180  }
181  if ((access & RegistryPermissionAccess.Write) != 0)
182  {
183  if (m_write == null)
184  {
185  return "";
186  }
187  return m_write.UnsafeToString();
188  }
189  if ((access & RegistryPermissionAccess.Create) != 0)
190  {
191  if (m_create == null)
192  {
193  return "";
194  }
195  return m_create.UnsafeToString();
196  }
197  return "";
198  }
199 
200  private void VerifyAccess(RegistryPermissionAccess access)
201  {
202  if ((access & ~(RegistryPermissionAccess.Read | RegistryPermissionAccess.Write | RegistryPermissionAccess.Create)) != 0)
203  {
204  throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (int)access));
205  }
206  }
207 
208  private void ExclusiveAccess(RegistryPermissionAccess access)
209  {
210  if (access == RegistryPermissionAccess.NoAccess)
211  {
212  throw new ArgumentException(Environment.GetResourceString("Arg_EnumNotSingleFlag"));
213  }
214  if ((access & (access - 1)) != 0)
215  {
216  throw new ArgumentException(Environment.GetResourceString("Arg_EnumNotSingleFlag"));
217  }
218  }
219 
220  private bool IsEmpty()
221  {
222  if (!m_unrestricted && (m_read == null || m_read.IsEmpty()) && (m_write == null || m_write.IsEmpty()) && (m_create == null || m_create.IsEmpty()) && (m_viewAcl == null || m_viewAcl.IsEmpty()))
223  {
224  if (m_changeAcl != null)
225  {
226  return m_changeAcl.IsEmpty();
227  }
228  return true;
229  }
230  return false;
231  }
232 
236  public bool IsUnrestricted()
237  {
238  return m_unrestricted;
239  }
240 
246  [SecuritySafeCritical]
247  public override bool IsSubsetOf(IPermission target)
248  {
249  if (target == null)
250  {
251  return IsEmpty();
252  }
253  RegistryPermission registryPermission = target as RegistryPermission;
254  if (registryPermission == null)
255  {
256  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
257  }
258  if (registryPermission.IsUnrestricted())
259  {
260  return true;
261  }
262  if (IsUnrestricted())
263  {
264  return false;
265  }
266  if ((m_read == null || m_read.IsSubsetOf(registryPermission.m_read)) && (m_write == null || m_write.IsSubsetOf(registryPermission.m_write)) && (m_create == null || m_create.IsSubsetOf(registryPermission.m_create)) && (m_viewAcl == null || m_viewAcl.IsSubsetOf(registryPermission.m_viewAcl)))
267  {
268  if (m_changeAcl != null)
269  {
270  return m_changeAcl.IsSubsetOf(registryPermission.m_changeAcl);
271  }
272  return true;
273  }
274  return false;
275  }
276 
281  [SecuritySafeCritical]
282  public override IPermission Intersect(IPermission target)
283  {
284  if (target == null)
285  {
286  return null;
287  }
288  if (!VerifyType(target))
289  {
290  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
291  }
292  if (IsUnrestricted())
293  {
294  return target.Copy();
295  }
296  RegistryPermission registryPermission = (RegistryPermission)target;
297  if (registryPermission.IsUnrestricted())
298  {
299  return Copy();
300  }
301  StringExpressionSet stringExpressionSet = (m_read == null) ? null : m_read.Intersect(registryPermission.m_read);
302  StringExpressionSet stringExpressionSet2 = (m_write == null) ? null : m_write.Intersect(registryPermission.m_write);
303  StringExpressionSet stringExpressionSet3 = (m_create == null) ? null : m_create.Intersect(registryPermission.m_create);
304  StringExpressionSet stringExpressionSet4 = (m_viewAcl == null) ? null : m_viewAcl.Intersect(registryPermission.m_viewAcl);
305  StringExpressionSet stringExpressionSet5 = (m_changeAcl == null) ? null : m_changeAcl.Intersect(registryPermission.m_changeAcl);
306  if ((stringExpressionSet == null || stringExpressionSet.IsEmpty()) && (stringExpressionSet2 == null || stringExpressionSet2.IsEmpty()) && (stringExpressionSet3 == null || stringExpressionSet3.IsEmpty()) && (stringExpressionSet4 == null || stringExpressionSet4.IsEmpty()) && (stringExpressionSet5 == null || stringExpressionSet5.IsEmpty()))
307  {
308  return null;
309  }
310  RegistryPermission registryPermission2 = new RegistryPermission(PermissionState.None);
311  registryPermission2.m_unrestricted = false;
312  registryPermission2.m_read = stringExpressionSet;
313  registryPermission2.m_write = stringExpressionSet2;
314  registryPermission2.m_create = stringExpressionSet3;
315  registryPermission2.m_viewAcl = stringExpressionSet4;
316  registryPermission2.m_changeAcl = stringExpressionSet5;
317  return registryPermission2;
318  }
319 
324  [SecuritySafeCritical]
325  public override IPermission Union(IPermission other)
326  {
327  if (other == null)
328  {
329  return Copy();
330  }
331  if (!VerifyType(other))
332  {
333  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
334  }
335  RegistryPermission registryPermission = (RegistryPermission)other;
336  if (IsUnrestricted() || registryPermission.IsUnrestricted())
337  {
338  return new RegistryPermission(PermissionState.Unrestricted);
339  }
340  StringExpressionSet stringExpressionSet = (m_read == null) ? registryPermission.m_read : m_read.Union(registryPermission.m_read);
341  StringExpressionSet stringExpressionSet2 = (m_write == null) ? registryPermission.m_write : m_write.Union(registryPermission.m_write);
342  StringExpressionSet stringExpressionSet3 = (m_create == null) ? registryPermission.m_create : m_create.Union(registryPermission.m_create);
343  StringExpressionSet stringExpressionSet4 = (m_viewAcl == null) ? registryPermission.m_viewAcl : m_viewAcl.Union(registryPermission.m_viewAcl);
344  StringExpressionSet stringExpressionSet5 = (m_changeAcl == null) ? registryPermission.m_changeAcl : m_changeAcl.Union(registryPermission.m_changeAcl);
345  if ((stringExpressionSet == null || stringExpressionSet.IsEmpty()) && (stringExpressionSet2 == null || stringExpressionSet2.IsEmpty()) && (stringExpressionSet3 == null || stringExpressionSet3.IsEmpty()) && (stringExpressionSet4 == null || stringExpressionSet4.IsEmpty()) && (stringExpressionSet5 == null || stringExpressionSet5.IsEmpty()))
346  {
347  return null;
348  }
349  RegistryPermission registryPermission2 = new RegistryPermission(PermissionState.None);
350  registryPermission2.m_unrestricted = false;
351  registryPermission2.m_read = stringExpressionSet;
352  registryPermission2.m_write = stringExpressionSet2;
353  registryPermission2.m_create = stringExpressionSet3;
354  registryPermission2.m_viewAcl = stringExpressionSet4;
355  registryPermission2.m_changeAcl = stringExpressionSet5;
356  return registryPermission2;
357  }
358 
361  public override IPermission Copy()
362  {
363  RegistryPermission registryPermission = new RegistryPermission(PermissionState.None);
364  if (m_unrestricted)
365  {
366  registryPermission.m_unrestricted = true;
367  }
368  else
369  {
370  registryPermission.m_unrestricted = false;
371  if (m_read != null)
372  {
373  registryPermission.m_read = m_read.Copy();
374  }
375  if (m_write != null)
376  {
377  registryPermission.m_write = m_write.Copy();
378  }
379  if (m_create != null)
380  {
381  registryPermission.m_create = m_create.Copy();
382  }
383  if (m_viewAcl != null)
384  {
385  registryPermission.m_viewAcl = m_viewAcl.Copy();
386  }
387  if (m_changeAcl != null)
388  {
389  registryPermission.m_changeAcl = m_changeAcl.Copy();
390  }
391  }
392  return registryPermission;
393  }
394 
397  [SecuritySafeCritical]
398  public override SecurityElement ToXml()
399  {
400  SecurityElement securityElement = CodeAccessPermission.CreatePermissionElement(this, "System.Security.Permissions.RegistryPermission");
401  if (!IsUnrestricted())
402  {
403  if (m_read != null && !m_read.IsEmpty())
404  {
405  securityElement.AddAttribute("Read", SecurityElement.Escape(m_read.UnsafeToString()));
406  }
407  if (m_write != null && !m_write.IsEmpty())
408  {
409  securityElement.AddAttribute("Write", SecurityElement.Escape(m_write.UnsafeToString()));
410  }
411  if (m_create != null && !m_create.IsEmpty())
412  {
413  securityElement.AddAttribute("Create", SecurityElement.Escape(m_create.UnsafeToString()));
414  }
415  if (m_viewAcl != null && !m_viewAcl.IsEmpty())
416  {
417  securityElement.AddAttribute("ViewAccessControl", SecurityElement.Escape(m_viewAcl.UnsafeToString()));
418  }
419  if (m_changeAcl != null && !m_changeAcl.IsEmpty())
420  {
421  securityElement.AddAttribute("ChangeAccessControl", SecurityElement.Escape(m_changeAcl.UnsafeToString()));
422  }
423  }
424  else
425  {
426  securityElement.AddAttribute("Unrestricted", "true");
427  }
428  return securityElement;
429  }
430 
435  public override void FromXml(SecurityElement esd)
436  {
437  CodeAccessPermission.ValidateElement(esd, this);
438  if (XMLUtil.IsUnrestricted(esd))
439  {
440  m_unrestricted = true;
441  return;
442  }
443  m_unrestricted = false;
444  m_read = null;
445  m_write = null;
446  m_create = null;
447  m_viewAcl = null;
448  m_changeAcl = null;
449  string text = esd.Attribute("Read");
450  if (text != null)
451  {
452  m_read = new StringExpressionSet(text);
453  }
454  text = esd.Attribute("Write");
455  if (text != null)
456  {
457  m_write = new StringExpressionSet(text);
458  }
459  text = esd.Attribute("Create");
460  if (text != null)
461  {
462  m_create = new StringExpressionSet(text);
463  }
464  text = esd.Attribute("ViewAccessControl");
465  if (text != null)
466  {
467  m_viewAcl = new StringExpressionSet(text);
468  }
469  text = esd.Attribute("ChangeAccessControl");
470  if (text != null)
471  {
472  m_changeAcl = new StringExpressionSet(text);
473  }
474  }
475 
476  int IBuiltInPermission.GetTokenIndex()
477  {
478  return GetTokenIndex();
479  }
480 
481  internal static int GetTokenIndex()
482  {
483  return 5;
484  }
485  }
486 }
Allows a permission to expose an unrestricted state.
override IPermission Intersect(IPermission target)
Creates and returns a permission that is the intersection of the current permission and the specified...
RegistryPermission(RegistryPermissionAccess access, AccessControlActions control, string pathList)
Initializes a new instance of the T:System.Security.Permissions.RegistryPermission class with the spe...
override void FromXml(SecurityElement esd)
Reconstructs a permission with a specified state from an XML encoding.
RegistryPermission(RegistryPermissionAccess access, string pathList)
Initializes a new instance of the T:System.Security.Permissions.RegistryPermission class with the spe...
Controls the ability to access registry variables. This class cannot be inherited.
void AddPathList(RegistryPermissionAccess access, AccessControlActions control, string pathList)
Adds access for the specified registry variables to the existing state of the permission,...
override SecurityElement ToXml()
Creates an XML encoding of the permission and its current state.
Definition: __Canon.cs:3
IPermission Copy()
Creates and returns an identical copy of the current permission.
static string Escape(string str)
Replaces invalid XML characters in a string with their valid XML equivalent.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Represents the XML object model for encoding security objects. This class cannot be inherited.
override IPermission Union(IPermission other)
Creates a permission that is the union of the current permission and the specified permission.
Defines the underlying structure of all code access permissions.
override bool IsSubsetOf(IPermission target)
Determines whether the current permission is a subset of the specified permission.
RegistryPermission(PermissionState state)
Initializes a new instance of the T:System.Security.Permissions.RegistryPermission class with either ...
Defines methods implemented by permission types.
Definition: IPermission.cs:7
AccessControlActions
Specifies the actions that are permitted for securable objects.
bool IsUnrestricted()
Returns a value indicating whether the current permission is unrestricted.
string GetPathList(RegistryPermissionAccess access)
Gets paths for all registry variables with the specified T:System.Security.Permissions....
The exception that is thrown when one of the arguments provided to a method is not valid.
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
void SetPathList(RegistryPermissionAccess access, string pathList)
Sets new access for the specified registry variable names to the existing state of the permission.
void AddPathList(RegistryPermissionAccess access, string pathList)
Adds access for the specified registry variables to the existing state of the permission.
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
Specifies that the class can be serialized.
override IPermission Copy()
Creates and returns an identical copy of the current permission.
RegistryPermissionAccess
Specifies the permitted access to registry keys and values.