File size: 7,290 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#if !UNIX
using System.Management.Automation;
using System.Management.Automation.Provider;
using System.Security.AccessControl;
using Microsoft.PowerShell.Commands.Internal;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Provider that provides access to Registry through cmdlets. This provider
/// implements <see cref="System.Management.Automation.Provider.NavigationCmdletProvider"/>,
/// <see cref="System.Management.Automation.Provider.IPropertyCmdletProvider"/>,
/// <see cref="System.Management.Automation.Provider.IDynamicPropertyCmdletProvider"/>,
/// <see cref="System.Management.Automation.Provider.ISecurityDescriptorCmdletProvider"/>
/// interfaces.
/// </summary>
public sealed partial class RegistryProvider :
NavigationCmdletProvider,
IPropertyCmdletProvider,
IDynamicPropertyCmdletProvider,
ISecurityDescriptorCmdletProvider
{
#region ISecurityDescriptorCmdletProvider members
/// <summary>
/// Gets the security descriptor for the item specified by <paramref name="path"/>.
/// </summary>
/// <param name="path">
/// The path to the item.
/// </param>
/// <param name="sections">
/// Specifies the parts of a security descriptor to retrieve.
/// </param>
/// <returns>
/// Nothing. An object that represents the security descriptor for the item
/// specified by path is written to the WriteSecurityDescriptorObject method.
/// </returns>
public void GetSecurityDescriptor(string path,
AccessControlSections sections)
{
ObjectSecurity sd = null;
IRegistryWrapper key = null;
// Validate input first.
if (string.IsNullOrEmpty(path))
{
throw PSTraceSource.NewArgumentNullException(nameof(path));
}
if ((sections & ~AccessControlSections.All) != 0)
{
throw PSTraceSource.NewArgumentException(nameof(sections));
}
path = NormalizePath(path);
key = GetRegkeyForPathWriteIfError(path, false);
if (key != null)
{
try
{
sd = key.GetAccessControl(sections);
}
catch (System.Security.SecurityException e)
{
WriteError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.PermissionDenied, path));
return;
}
WriteSecurityDescriptorObject(sd, path);
}
}
/// <summary>
/// Sets the security descriptor for the item specified by <paramref name="path"/>
/// </summary>
/// <param name="path">
/// The path to the item to set the security descriptor on.
/// </param>
/// <param name="securityDescriptor">
/// The new security descriptor for the item.
/// </param>
public void SetSecurityDescriptor(
string path,
ObjectSecurity securityDescriptor)
{
IRegistryWrapper key = null;
if (string.IsNullOrEmpty(path))
{
throw PSTraceSource.NewArgumentException(nameof(path));
}
if (securityDescriptor == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(securityDescriptor));
}
path = NormalizePath(path);
ObjectSecurity sd;
if (TransactionAvailable())
{
sd = securityDescriptor as TransactedRegistrySecurity;
if (sd == null)
{
throw PSTraceSource.NewArgumentException(nameof(securityDescriptor));
}
}
else
{
sd = securityDescriptor as RegistrySecurity;
if (sd == null)
{
throw PSTraceSource.NewArgumentException(nameof(securityDescriptor));
}
}
key = GetRegkeyForPathWriteIfError(path, true);
if (key != null)
{
//
// the caller already checks for the following exceptions:
// -- UnauthorizedAccessException
// -- PrivilegeNotHeldException
// -- NotSupportedException
// -- SystemException
//
try
{
key.SetAccessControl(sd);
}
catch (System.Security.SecurityException e)
{
WriteError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.PermissionDenied, path));
return;
}
catch (System.UnauthorizedAccessException e)
{
WriteError(new ErrorRecord(e, e.GetType().FullName, ErrorCategory.PermissionDenied, path));
return;
}
WriteSecurityDescriptorObject(sd, path);
}
}
/// <summary>
/// Creates a new empty security descriptor.
/// </summary>
/// <param name="path">
/// The path to the item whose type is to be used when
/// creating a new descriptor.
/// </param>
/// <param name="sections">
/// Specifies the parts of a security descriptor to create.
/// </param>
/// <returns>
/// An instance of <see cref="System.Security.AccessControl.ObjectSecurity"/> object.
/// </returns>
/// <remarks><paramref name="path"/> and <paramref name="sections"/> are not used by this method.</remarks>
public ObjectSecurity NewSecurityDescriptorFromPath(
string path,
AccessControlSections sections)
{
if (TransactionAvailable())
{
return new TransactedRegistrySecurity();
}
else
{
return new RegistrySecurity(); // sections);
}
}
/// <summary>
/// Creates a new empty security descriptor.
/// </summary>
/// <param name="type">
/// The type of item associated with this security descriptor
/// </param>
/// <param name="sections">
/// Specifies the parts of a security descriptor to create.
/// </param>
/// <returns>
/// An instance of <see cref="System.Security.AccessControl.ObjectSecurity"/> object.
/// </returns>
public ObjectSecurity NewSecurityDescriptorOfType(
string type,
AccessControlSections sections)
{
if (TransactionAvailable())
{
return new TransactedRegistrySecurity();
}
else
{
return new RegistrySecurity(); // sections);
}
}
#endregion ISecurityDescriptorCmdletProvider members
}
}
#endif // !UNIX
|