Files
2026-06-04 11:42:34 +02:00

536 lines
21 KiB
C#

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Text;
namespace System.Security.Permissions
{
/// <summary>Controls the ability to access registry variables. This class cannot be inherited.</summary>
// Token: 0x02000564 RID: 1380
[ComVisible(true)]
[Serializable]
public sealed class RegistryPermission : CodeAccessPermission, IBuiltInPermission, IUnrestrictedPermission
{
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Permissions.RegistryPermission" /> class with either fully restricted or unrestricted permission as specified.</summary>
/// <param name="state">One of the <see cref="T:System.Security.Permissions.PermissionState" /> values. </param>
/// <exception cref="T:System.ArgumentException">The <paramref name="state" /> parameter is not a valid value of <see cref="T:System.Security.Permissions.PermissionState" />. </exception>
// Token: 0x06003264 RID: 12900 RVA: 0x000AD0A8 File Offset: 0x000AB2A8
public RegistryPermission(PermissionState state)
{
this._state = CodeAccessPermission.CheckPermissionState(state, true);
this.createList = new ArrayList();
this.readList = new ArrayList();
this.writeList = new ArrayList();
}
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Permissions.RegistryPermission" /> class with the specified access to the specified registry variables.</summary>
/// <param name="access">One of the <see cref="T:System.Security.Permissions.RegistryPermissionAccess" /> values. </param>
/// <param name="pathList">A list of registry variables (semicolon-separated) to which access is granted. </param>
/// <exception cref="T:System.ArgumentException">The <paramref name="access" /> parameter is not a valid value of <see cref="T:System.Security.Permissions.RegistryPermissionAccess" />.-or- The <paramref name="pathList" /> parameter is not a valid string. </exception>
// Token: 0x06003265 RID: 12901 RVA: 0x000AD0EC File Offset: 0x000AB2EC
public RegistryPermission(RegistryPermissionAccess access, string pathList)
{
this._state = PermissionState.None;
this.createList = new ArrayList();
this.readList = new ArrayList();
this.writeList = new ArrayList();
this.AddPathList(access, pathList);
}
/// <summary>Initializes a new instance of the <see cref="T:System.Security.Permissions.RegistryPermission" /> class with the specified access to the specified registry variables and the specified access rights to registry control information.</summary>
/// <param name="access">One of the <see cref="T:System.Security.Permissions.RegistryPermissionAccess" /> values.</param>
/// <param name="control">A bitwise combination of the <see cref="T:System.Security.AccessControl.AccessControlActions" /> values.</param>
/// <param name="pathList">A list of registry variables (semicolon-separated) to which access is granted.</param>
/// <exception cref="T:System.ArgumentException">The <paramref name="access" /> parameter is not a valid value of <see cref="T:System.Security.Permissions.RegistryPermissionAccess" />.-or- The <paramref name="pathList" /> parameter is not a valid string. </exception>
// Token: 0x06003266 RID: 12902 RVA: 0x000AD130 File Offset: 0x000AB330
public RegistryPermission(RegistryPermissionAccess access, AccessControlActions control, string pathList)
{
if (!Enum.IsDefined(typeof(AccessControlActions), control))
{
string text = string.Format(Locale.GetText("Invalid enum {0}"), control);
throw new ArgumentException(text, "AccessControlActions");
}
this._state = PermissionState.None;
this.AddPathList(access, control, pathList);
}
// Token: 0x06003267 RID: 12903 RVA: 0x000AD190 File Offset: 0x000AB390
int IBuiltInPermission.GetTokenIndex()
{
return 5;
}
/// <summary>Adds access for the specified registry variables to the existing state of the permission.</summary>
/// <param name="access">One of the <see cref="T:System.Security.Permissions.RegistryPermissionAccess" /> values. </param>
/// <param name="pathList">A list of registry variables (semicolon-separated). </param>
/// <exception cref="T:System.ArgumentException">The <paramref name="access" /> parameter is not a valid value of <see cref="T:System.Security.Permissions.RegistryPermissionAccess" />.-or- The <paramref name="pathList" /> parameter is not a valid string. </exception>
// Token: 0x06003268 RID: 12904 RVA: 0x000AD194 File Offset: 0x000AB394
public void AddPathList(RegistryPermissionAccess access, string pathList)
{
if (pathList == null)
{
throw new ArgumentNullException("pathList");
}
switch (access)
{
case RegistryPermissionAccess.NoAccess:
return;
case RegistryPermissionAccess.Read:
this.AddWithUnionKey(this.readList, pathList);
return;
case RegistryPermissionAccess.Write:
this.AddWithUnionKey(this.writeList, pathList);
return;
case RegistryPermissionAccess.Create:
this.AddWithUnionKey(this.createList, pathList);
return;
case RegistryPermissionAccess.AllAccess:
this.AddWithUnionKey(this.createList, pathList);
this.AddWithUnionKey(this.readList, pathList);
this.AddWithUnionKey(this.writeList, pathList);
return;
}
this.ThrowInvalidFlag(access, false);
}
/// <summary>Adds access for the specified registry variables to the existing state of the permission, specifying registry permission access and access control actions.</summary>
/// <param name="access">One of the <see cref="T:System.Security.Permissions.RegistryPermissionAccess" /> values. </param>
/// <param name="control">One of the <see cref="T:System.Security.AccessControl.AccessControlActions" /> values. </param>
/// <param name="pathList">A list of registry variables (separated by semicolons).</param>
/// <exception cref="T:System.ArgumentException">The <paramref name="access" /> parameter is not a valid value of <see cref="T:System.Security.Permissions.RegistryPermissionAccess" />.-or- The <paramref name="pathList" /> parameter is not a valid string. </exception>
// Token: 0x06003269 RID: 12905 RVA: 0x000AD254 File Offset: 0x000AB454
[MonoTODO("(2.0) Access Control isn't implemented")]
public void AddPathList(RegistryPermissionAccess access, AccessControlActions control, string pathList)
{
throw new NotImplementedException();
}
/// <summary>Gets paths for all registry variables with the specified <see cref="T:System.Security.Permissions.RegistryPermissionAccess" />.</summary>
/// <returns>A list of the registry variables (semicolon-separated) with the specified <see cref="T:System.Security.Permissions.RegistryPermissionAccess" />.</returns>
/// <param name="access">One of the <see cref="T:System.Security.Permissions.RegistryPermissionAccess" /> values that represents a single type of registry variable access. </param>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="access" /> is not a valid value of <see cref="T:System.Security.Permissions.RegistryPermissionAccess" />.-or- <paramref name="access" /> is <see cref="F:System.Security.Permissions.RegistryPermissionAccess.AllAccess" />, which represents more than one type of registry variable access, or <see cref="F:System.Security.Permissions.RegistryPermissionAccess.NoAccess" />, which does not represent any type of registry variable access. </exception>
// Token: 0x0600326A RID: 12906 RVA: 0x000AD25C File Offset: 0x000AB45C
public string GetPathList(RegistryPermissionAccess access)
{
switch (access)
{
case RegistryPermissionAccess.NoAccess:
case RegistryPermissionAccess.AllAccess:
this.ThrowInvalidFlag(access, true);
goto IL_6E;
case RegistryPermissionAccess.Read:
return this.GetPathList(this.readList);
case RegistryPermissionAccess.Write:
return this.GetPathList(this.writeList);
case RegistryPermissionAccess.Create:
return this.GetPathList(this.createList);
}
this.ThrowInvalidFlag(access, false);
IL_6E:
return null;
}
/// <summary>Sets new access for the specified registry variable names to the existing state of the permission.</summary>
/// <param name="access">One of the <see cref="T:System.Security.Permissions.RegistryPermissionAccess" /> values. </param>
/// <param name="pathList">A list of registry variables (semicolon-separated). </param>
/// <exception cref="T:System.ArgumentException">The <paramref name="access" /> parameter is not a valid value of <see cref="T:System.Security.Permissions.RegistryPermissionAccess" />.-or- The <paramref name="pathList" /> parameter is not a valid string. </exception>
// Token: 0x0600326B RID: 12907 RVA: 0x000AD2D8 File Offset: 0x000AB4D8
public void SetPathList(RegistryPermissionAccess access, string pathList)
{
if (pathList == null)
{
throw new ArgumentNullException("pathList");
}
switch (access)
{
case RegistryPermissionAccess.NoAccess:
return;
case RegistryPermissionAccess.Read:
{
this.readList.Clear();
string[] array = pathList.Split(new char[] { ';' });
foreach (string text in array)
{
this.readList.Add(text);
}
return;
}
case RegistryPermissionAccess.Write:
{
this.writeList.Clear();
string[] array = pathList.Split(new char[] { ';' });
foreach (string text2 in array)
{
this.writeList.Add(text2);
}
return;
}
case RegistryPermissionAccess.Create:
{
this.createList.Clear();
string[] array = pathList.Split(new char[] { ';' });
foreach (string text3 in array)
{
this.createList.Add(text3);
}
return;
}
case RegistryPermissionAccess.AllAccess:
{
this.createList.Clear();
this.readList.Clear();
this.writeList.Clear();
string[] array = pathList.Split(new char[] { ';' });
foreach (string text4 in array)
{
this.createList.Add(text4);
this.readList.Add(text4);
this.writeList.Add(text4);
}
return;
}
}
this.ThrowInvalidFlag(access, false);
}
/// <summary>Creates and returns an identical copy of the current permission.</summary>
/// <returns>A copy of the current permission.</returns>
// Token: 0x0600326C RID: 12908 RVA: 0x000AD4AC File Offset: 0x000AB6AC
public override IPermission Copy()
{
RegistryPermission registryPermission = new RegistryPermission(this._state);
string text = this.GetPathList(RegistryPermissionAccess.Create);
if (text != null)
{
registryPermission.SetPathList(RegistryPermissionAccess.Create, text);
}
text = this.GetPathList(RegistryPermissionAccess.Read);
if (text != null)
{
registryPermission.SetPathList(RegistryPermissionAccess.Read, text);
}
text = this.GetPathList(RegistryPermissionAccess.Write);
if (text != null)
{
registryPermission.SetPathList(RegistryPermissionAccess.Write, text);
}
return registryPermission;
}
/// <summary>Reconstructs a permission with a specified state from an XML encoding.</summary>
/// <param name="esd">The XML encoding to use to reconstruct the permission. </param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="esd" /> parameter is null. </exception>
/// <exception cref="T:System.ArgumentException">The <paramref name="esd" /> parameter is not a valid permission element.-or- The <paramref name="esd" /> parameter's version number is not valid. </exception>
// Token: 0x0600326D RID: 12909 RVA: 0x000AD508 File Offset: 0x000AB708
public override void FromXml(SecurityElement esd)
{
CodeAccessPermission.CheckSecurityElement(esd, "esd", 1, 1);
CodeAccessPermission.CheckSecurityElement(esd, "esd", 1, 1);
if (CodeAccessPermission.IsUnrestricted(esd))
{
this._state = PermissionState.Unrestricted;
}
string text = esd.Attribute("Create");
if (text != null && text.Length > 0)
{
this.SetPathList(RegistryPermissionAccess.Create, text);
}
string text2 = esd.Attribute("Read");
if (text2 != null && text2.Length > 0)
{
this.SetPathList(RegistryPermissionAccess.Read, text2);
}
string text3 = esd.Attribute("Write");
if (text3 != null && text3.Length > 0)
{
this.SetPathList(RegistryPermissionAccess.Write, text3);
}
}
/// <summary>Creates and returns a permission that is the intersection of the current permission and the specified permission.</summary>
/// <returns>A new permission that represents the intersection of the current permission and the specified permission. This new permission is null if the intersection is empty.</returns>
/// <param name="target">A permission to intersect with the current permission. It must be of the same type as the current permission. </param>
/// <exception cref="T:System.ArgumentException">The <paramref name="target" /> parameter is not null and is not of the same type as the current permission. </exception>
// Token: 0x0600326E RID: 12910 RVA: 0x000AD5B8 File Offset: 0x000AB7B8
public override IPermission Intersect(IPermission target)
{
RegistryPermission registryPermission = this.Cast(target);
if (registryPermission == null)
{
return null;
}
if (this.IsUnrestricted())
{
return registryPermission.Copy();
}
if (registryPermission.IsUnrestricted())
{
return this.Copy();
}
RegistryPermission registryPermission2 = new RegistryPermission(PermissionState.None);
this.IntersectKeys(this.createList, registryPermission.createList, registryPermission2.createList);
this.IntersectKeys(this.readList, registryPermission.readList, registryPermission2.readList);
this.IntersectKeys(this.writeList, registryPermission.writeList, registryPermission2.writeList);
return (!registryPermission2.IsEmpty()) ? registryPermission2 : null;
}
/// <summary>Determines whether the current permission is a subset of the specified permission.</summary>
/// <returns>true if the current permission is a subset of the specified permission; otherwise, false.</returns>
/// <param name="target">A permission that is to be tested for the subset relationship. This permission must be of the same type as the current permission. </param>
/// <exception cref="T:System.ArgumentException">The <paramref name="target" /> parameter is not null and is not of the same type as the current permission. </exception>
// Token: 0x0600326F RID: 12911 RVA: 0x000AD65C File Offset: 0x000AB85C
public override bool IsSubsetOf(IPermission target)
{
RegistryPermission registryPermission = this.Cast(target);
if (registryPermission == null)
{
return false;
}
if (registryPermission.IsEmpty())
{
return this.IsEmpty();
}
if (this.IsUnrestricted())
{
return registryPermission.IsUnrestricted();
}
return registryPermission.IsUnrestricted() || (this.KeyIsSubsetOf(this.createList, registryPermission.createList) && this.KeyIsSubsetOf(this.readList, registryPermission.readList) && this.KeyIsSubsetOf(this.writeList, registryPermission.writeList));
}
/// <summary>Returns a value indicating whether the current permission is unrestricted.</summary>
/// <returns>true if the current permission is unrestricted; otherwise, false.</returns>
// Token: 0x06003270 RID: 12912 RVA: 0x000AD6F8 File Offset: 0x000AB8F8
public bool IsUnrestricted()
{
return this._state == PermissionState.Unrestricted;
}
/// <summary>Creates an XML encoding of the permission and its current state.</summary>
/// <returns>An XML encoding of the permission, including any state information.</returns>
// Token: 0x06003271 RID: 12913 RVA: 0x000AD704 File Offset: 0x000AB904
public override SecurityElement ToXml()
{
SecurityElement securityElement = base.Element(1);
if (this._state == PermissionState.Unrestricted)
{
securityElement.AddAttribute("Unrestricted", "true");
}
else
{
string text = this.GetPathList(RegistryPermissionAccess.Create);
if (text != null)
{
securityElement.AddAttribute("Create", text);
}
text = this.GetPathList(RegistryPermissionAccess.Read);
if (text != null)
{
securityElement.AddAttribute("Read", text);
}
text = this.GetPathList(RegistryPermissionAccess.Write);
if (text != null)
{
securityElement.AddAttribute("Write", text);
}
}
return securityElement;
}
/// <summary>Creates a permission that is the union of the current permission and the specified permission.</summary>
/// <returns>A new permission that represents the union of the current permission and the specified permission.</returns>
/// <param name="other">A permission to combine with the current permission. It must be of the same type as the current permission. </param>
/// <exception cref="T:System.ArgumentException">The <paramref name="other" /> parameter is not null and is not of the same type as the current permission. </exception>
// Token: 0x06003272 RID: 12914 RVA: 0x000AD78C File Offset: 0x000AB98C
public override IPermission Union(IPermission other)
{
RegistryPermission registryPermission = this.Cast(other);
if (registryPermission == null)
{
return this.Copy();
}
if (this.IsUnrestricted() || registryPermission.IsUnrestricted())
{
return new RegistryPermission(PermissionState.Unrestricted);
}
if (this.IsEmpty() && registryPermission.IsEmpty())
{
return null;
}
RegistryPermission registryPermission2 = (RegistryPermission)this.Copy();
string text = registryPermission.GetPathList(RegistryPermissionAccess.Create);
if (text != null)
{
registryPermission2.AddPathList(RegistryPermissionAccess.Create, text);
}
text = registryPermission.GetPathList(RegistryPermissionAccess.Read);
if (text != null)
{
registryPermission2.AddPathList(RegistryPermissionAccess.Read, text);
}
text = registryPermission.GetPathList(RegistryPermissionAccess.Write);
if (text != null)
{
registryPermission2.AddPathList(RegistryPermissionAccess.Write, text);
}
return registryPermission2;
}
// Token: 0x06003273 RID: 12915 RVA: 0x000AD834 File Offset: 0x000ABA34
private bool IsEmpty()
{
return this._state == PermissionState.None && this.createList.Count == 0 && this.readList.Count == 0 && this.writeList.Count == 0;
}
// Token: 0x06003274 RID: 12916 RVA: 0x000AD880 File Offset: 0x000ABA80
private RegistryPermission Cast(IPermission target)
{
if (target == null)
{
return null;
}
RegistryPermission registryPermission = target as RegistryPermission;
if (registryPermission == null)
{
CodeAccessPermission.ThrowInvalidPermission(target, typeof(RegistryPermission));
}
return registryPermission;
}
// Token: 0x06003275 RID: 12917 RVA: 0x000AD8B4 File Offset: 0x000ABAB4
internal void ThrowInvalidFlag(RegistryPermissionAccess flag, bool context)
{
string text;
if (context)
{
text = Locale.GetText("Unknown flag '{0}'.");
}
else
{
text = Locale.GetText("Invalid flag '{0}' in this context.");
}
throw new ArgumentException(string.Format(text, flag), "flag");
}
// Token: 0x06003276 RID: 12918 RVA: 0x000AD8FC File Offset: 0x000ABAFC
private string GetPathList(ArrayList list)
{
if (this.IsUnrestricted())
{
return string.Empty;
}
if (list.Count == 0)
{
return string.Empty;
}
StringBuilder stringBuilder = new StringBuilder();
foreach (object obj in list)
{
string text = (string)obj;
stringBuilder.Append(text);
stringBuilder.Append(";");
}
string text2 = stringBuilder.ToString();
int length = text2.Length;
if (length > 0)
{
return text2.Substring(0, length - 1);
}
return string.Empty;
}
// Token: 0x06003277 RID: 12919 RVA: 0x000AD9C8 File Offset: 0x000ABBC8
internal bool KeyIsSubsetOf(IList local, IList target)
{
bool flag = false;
foreach (object obj in local)
{
string text = (string)obj;
foreach (object obj2 in target)
{
string text2 = (string)obj2;
if (text.StartsWith(text2))
{
flag = true;
break;
}
}
if (!flag)
{
return false;
}
}
return true;
}
// Token: 0x06003278 RID: 12920 RVA: 0x000ADAB0 File Offset: 0x000ABCB0
internal void AddWithUnionKey(IList list, string pathList)
{
string[] array = pathList.Split(new char[] { ';' });
foreach (string text in array)
{
int count = list.Count;
if (count == 0)
{
list.Add(text);
}
else
{
for (int j = 0; j < count; j++)
{
string text2 = (string)list[j];
if (text2.StartsWith(text))
{
list[j] = text;
}
else if (!text.StartsWith(text2))
{
list.Add(text);
}
}
}
}
}
// Token: 0x06003279 RID: 12921 RVA: 0x000ADB60 File Offset: 0x000ABD60
internal void IntersectKeys(IList local, IList target, IList result)
{
foreach (object obj in local)
{
string text = (string)obj;
foreach (object obj2 in target)
{
string text2 = (string)obj2;
if (text2.Length > text.Length)
{
if (text2.StartsWith(text))
{
result.Add(text2);
}
}
else if (text.StartsWith(text2))
{
result.Add(text);
}
}
}
}
// Token: 0x040014B0 RID: 5296
private const int version = 1;
// Token: 0x040014B1 RID: 5297
private PermissionState _state;
// Token: 0x040014B2 RID: 5298
private ArrayList createList;
// Token: 0x040014B3 RID: 5299
private ArrayList readList;
// Token: 0x040014B4 RID: 5300
private ArrayList writeList;
}
}