mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 12:45:47 +00:00
Update files from Source Depot changeset [SD:715912]
Corresponds to 77eda782aa78ba8b7a36c51ee619629ce6c990f2 in psl-monad.
This commit is contained in:
1 parent
d9b11669ce
commit
8ee6d6ca43
74 files changed
+1376
-1336
No files matched your search
@@ -294,15 +294,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
#endregion Cmdlet Overrides
|
||||
|
||||
#region Private Methods
|
||||
private LocalUser GetUser()
|
||||
{
|
||||
if (InputObject != null)
|
||||
return InputObject;
|
||||
|
||||
return SID != null ? sam.GetLocalUser(SID)
|
||||
: sam.GetLocalUser(Name);
|
||||
}
|
||||
|
||||
private bool CheckShouldProcess(string target)
|
||||
{
|
||||
return ShouldProcess(target, Strings.ActionSetUser);
|
||||
|
||||
@@ -267,7 +267,11 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </summary>
|
||||
public class InvalidParametersException : LocalAccountsException
|
||||
{
|
||||
internal InvalidParametersException(string message)
|
||||
/// <summary>
|
||||
/// Creates InvalidParametersException using the specified message.
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
public InvalidParametersException(string message)
|
||||
: base(message, null, ErrorCategory.InvalidArgument)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace System.Management.Automation.SecurityAccountsManager.Extensions
|
||||
bool allowSidConstants = false)
|
||||
{
|
||||
if (!allowSidConstants)
|
||||
if (!(s.Length > 2 && s.StartsWith("S-") && Char.IsDigit(s[2])))
|
||||
if (!(s.Length > 2 && s.StartsWith("S-", StringComparison.Ordinal) && Char.IsDigit(s[2])))
|
||||
return null;
|
||||
|
||||
SecurityIdentifier sid = null;
|
||||
@@ -144,21 +144,7 @@ namespace System.Management.Automation.SecurityAccountsManager.Extensions
|
||||
}
|
||||
|
||||
internal static class SecureStringExtensions
|
||||
{
|
||||
private static Regex pwRegex = new Regex(@"^[^\x00-\x1F""/\\\[\]:;\|=,+\*?<>@]{0,127}$",
|
||||
RegexOptions.Compiled | RegexOptions.CultureInvariant);
|
||||
|
||||
internal static bool IsValidPassword(this SecureString str)
|
||||
{
|
||||
return str.Length == 0 || pwRegex.IsMatch(str.AsString());
|
||||
}
|
||||
|
||||
internal static void ValidatePassword(this SecureString str)
|
||||
{
|
||||
if (!str.IsValidPassword())
|
||||
throw new InvalidPasswordException();
|
||||
}
|
||||
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension method to extract clear text from a
|
||||
/// <see cref="System.Security.SecureString"/> object.
|
||||
|
||||
@@ -149,37 +149,7 @@ namespace System.Management.Automation.SecurityAccountsManager.Native
|
||||
public IntPtr SecurityQualityOfService;
|
||||
|
||||
private IntPtr objectName;
|
||||
|
||||
public OBJECT_ATTRIBUTES(string name, uint attrs)
|
||||
{
|
||||
Length = 0;
|
||||
RootDirectory = IntPtr.Zero;
|
||||
objectName = IntPtr.Zero;
|
||||
Attributes = attrs;
|
||||
SecurityDescriptor = IntPtr.Zero;
|
||||
SecurityQualityOfService = IntPtr.Zero;
|
||||
|
||||
Length = ClrFacade.SizeOf<OBJECT_ATTRIBUTES>();
|
||||
ObjectName = new UNICODE_STRING(name);
|
||||
}
|
||||
|
||||
public UNICODE_STRING ObjectName
|
||||
{
|
||||
get
|
||||
{
|
||||
return ClrFacade.PtrToStructure<UNICODE_STRING>(objectName);
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
bool fDeleteOld = objectName != IntPtr.Zero;
|
||||
|
||||
if (!fDeleteOld)
|
||||
objectName = Marshal.AllocHGlobal(Marshal.SizeOf(value));
|
||||
|
||||
Marshal.StructureToPtr(value, objectName, fDeleteOld);
|
||||
}
|
||||
}
|
||||
public UNICODE_STRING ObjectName;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
@@ -463,8 +433,6 @@ namespace System.Management.Automation.SecurityAccountsManager.Native
|
||||
ref uint domainNameLength,
|
||||
out SID_NAME_USE peUse);
|
||||
|
||||
[DllImport(PInvokeDllNames.GetComputerNameDllName, CharSet=CharSet.Unicode, SetLastError=true)]
|
||||
internal static extern bool GetComputerName(StringBuilder name, ref uint size);
|
||||
|
||||
[DllImport(PInvokeDllNames.GetSecurityDescriptorDaclDllName, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
@@ -484,7 +452,7 @@ namespace System.Management.Automation.SecurityAccountsManager.Native
|
||||
[MarshalAs(UnmanagedType.Bool)]
|
||||
bool bDaclDefaulted);
|
||||
|
||||
[DllImport(PInvokeDllNames.FormatMessageDllName, SetLastError = true)]
|
||||
[DllImport(PInvokeDllNames.FormatMessageDllName, CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
internal static extern uint FormatMessage(uint dwFlags,
|
||||
IntPtr lpSource,
|
||||
uint dwMessageId,
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace System.Management.Automation.SecurityAccountsManager.Native
|
||||
{
|
||||
internal static class NtStatus
|
||||
@@ -451,6 +453,7 @@ namespace System.Management.Automation.SecurityAccountsManager.Native
|
||||
return Severity(ntstatus) == STATUS_SEVERITY_ERROR;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Determine if an NTSTATUS value indicates a Warning
|
||||
/// </summary>
|
||||
@@ -458,6 +461,7 @@ namespace System.Management.Automation.SecurityAccountsManager.Native
|
||||
/// <returns>
|
||||
/// True if the NTSTATUS value indicates a warning, false otherwise.
|
||||
/// </returns>
|
||||
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
public static bool IsWarning(UInt32 ntstatus)
|
||||
{
|
||||
return Severity(ntstatus) == STATUS_SEVERITY_WARNING;
|
||||
@@ -470,11 +474,12 @@ namespace System.Management.Automation.SecurityAccountsManager.Native
|
||||
/// <returns>
|
||||
/// True if the NTSTATUS value indicates that it is informational, false otherwise.
|
||||
/// </returns>
|
||||
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
public static bool IsInformational(UInt32 ntstatus)
|
||||
{
|
||||
return Severity(ntstatus) == STATUS_SEVERITY_INFORMATIONAL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Return the Severity part of an NTSTATUS value
|
||||
@@ -488,6 +493,7 @@ namespace System.Management.Automation.SecurityAccountsManager.Native
|
||||
return ntstatus >> 30;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Return the Facility part of an NSTATUS value
|
||||
/// </summary>
|
||||
@@ -495,11 +501,14 @@ namespace System.Management.Automation.SecurityAccountsManager.Native
|
||||
/// <returns>
|
||||
/// The value of the Facility portion of an NTSTATUS value.
|
||||
/// </returns>
|
||||
|
||||
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
public static uint Facility(UInt32 ntstatus)
|
||||
{
|
||||
return (ntstatus >> 16) & 0x0FFF;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Return the Code part of an NTSTATUS value
|
||||
/// </summary>
|
||||
@@ -507,10 +516,12 @@ namespace System.Management.Automation.SecurityAccountsManager.Native
|
||||
/// <returns>
|
||||
/// The value of the Code portion of an NTSTATUS value.
|
||||
/// </returns>
|
||||
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
public static uint Code(UInt32 ntstatus)
|
||||
{
|
||||
return ntstatus & 0xFFFF;
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,9 @@ using System.Management.Automation.SecurityAccountsManager.Native.NtSam;
|
||||
using System.Text;
|
||||
|
||||
using Microsoft.PowerShell.LocalAccounts;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
[module: SuppressMessage("Microsoft.Design", "CA1014:MarkAssembliesWithClsCompliant")]
|
||||
|
||||
namespace System.Management.Automation.SecurityAccountsManager
|
||||
{
|
||||
@@ -262,6 +265,7 @@ namespace System.Management.Automation.SecurityAccountsManager
|
||||
/// <summary>
|
||||
/// Gets a string containing the type of operation under way.
|
||||
/// </summary>
|
||||
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
public string OperationName
|
||||
{
|
||||
get { return operation.ToString(); }
|
||||
@@ -271,6 +275,7 @@ namespace System.Management.Automation.SecurityAccountsManager
|
||||
/// Gets a string containing the type of object ("User" or "Group")
|
||||
/// being used.
|
||||
/// </summary>
|
||||
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
public string TypeNamne
|
||||
{
|
||||
get { return type.ToString(); }
|
||||
@@ -1083,31 +1088,6 @@ namespace System.Management.Automation.SecurityAccountsManager
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a named group and return a <see cref="SamRidEnumeration"/> object
|
||||
/// representing the group.
|
||||
/// </summary>
|
||||
/// <param name="groupName">Name of the group to search for.</param>
|
||||
/// <returns>
|
||||
/// A SamRidEnumeration object representing the group.
|
||||
/// </returns>
|
||||
/// <exception cref="GroupNotFoundException">
|
||||
/// Thrown when the specified group is not found.
|
||||
/// </exception>
|
||||
/// <remarks>
|
||||
/// This method saves some time and effort over the GetGroup method
|
||||
/// because it does not have to open a group to populate a full Group
|
||||
/// object.
|
||||
/// </remarks>
|
||||
private SamRidEnumeration GetGroupSre(string groupName)
|
||||
{
|
||||
foreach (var sre in EnumerateGroups())
|
||||
if (sre.Name.Equals(groupName, StringComparison.CurrentCultureIgnoreCase))
|
||||
return sre;
|
||||
|
||||
throw new GroupNotFoundException(groupName, context.target);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a group by SID and return a <see cref="SamRidEnumeration"/> object
|
||||
/// representing the group.
|
||||
@@ -1134,31 +1114,6 @@ namespace System.Management.Automation.SecurityAccountsManager
|
||||
throw new GroupNotFoundException(sid.ToString(), sid);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a named user and return a <see cref="SamRidEnumeration"/> object
|
||||
/// representing the user.
|
||||
/// </summary>
|
||||
/// <param name="userName">Name of the user to search for.</param>
|
||||
/// <returns>
|
||||
/// A SamRidEnumeration object representing the user.
|
||||
/// </returns>
|
||||
/// <exception cref="UserNotFoundException">
|
||||
/// Thrown when the specified user is not found.
|
||||
/// </exception>
|
||||
/// <remarks>
|
||||
/// This method saves some time and effort over the GetUser method
|
||||
/// because it does not have to open a group to populate a full LocalUser
|
||||
/// object.
|
||||
/// </remarks>
|
||||
private SamRidEnumeration GetUserSre(string userName)
|
||||
{
|
||||
foreach (var sre in EnumerateGroups())
|
||||
if (sre.Name.Equals(userName, StringComparison.CurrentCultureIgnoreCase))
|
||||
return sre;
|
||||
|
||||
throw new GroupNotFoundException(userName, userName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a user by SID and return a <see cref="SamRidEnumeration"/> object
|
||||
/// representing the user.
|
||||
@@ -1205,14 +1160,12 @@ namespace System.Management.Automation.SecurityAccountsManager
|
||||
|
||||
do
|
||||
{
|
||||
status = SamApi.SamEnumerateUsersInDomain2(domainHandle,
|
||||
ref context,
|
||||
0,
|
||||
SamApi.SAM_USER_ENUMERATION_FILTER_LOCAL,
|
||||
//0, // non-filtered
|
||||
out buffer,
|
||||
1,
|
||||
out countReturned);
|
||||
status = SamApi.SamEnumerateUsersInDomain(domainHandle,
|
||||
ref context,
|
||||
0,
|
||||
out buffer,
|
||||
1,
|
||||
out countReturned);
|
||||
|
||||
if (status == NtStatus.STATUS_MORE_ENTRIES && countReturned == 1)
|
||||
{
|
||||
@@ -1350,6 +1303,8 @@ namespace System.Management.Automation.SecurityAccountsManager
|
||||
|
||||
status = SamApi.SamDeleteAlias(aliasHandle);
|
||||
ThrowOnFailure(status);
|
||||
|
||||
aliasHandle = IntPtr.Zero; // The handle is freed internally if SamDeleteAlias succeeds
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -1829,6 +1784,8 @@ namespace System.Management.Automation.SecurityAccountsManager
|
||||
|
||||
status = SamApi.SamDeleteUser(userHandle);
|
||||
ThrowOnFailure(status);
|
||||
|
||||
userHandle = IntPtr.Zero; // The handle is freed internally if SamDeleteUser succeeds
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -2677,7 +2634,7 @@ namespace System.Management.Automation.SecurityAccountsManager
|
||||
|
||||
try
|
||||
{
|
||||
SamApi.SamRidToSid(domainHandle, rid, out sidBytes);
|
||||
status = SamApi.SamRidToSid(domainHandle, rid, out sidBytes);
|
||||
|
||||
if (status == NtStatus.STATUS_NOT_FOUND)
|
||||
throw new InternalException(status,
|
||||
@@ -2808,7 +2765,7 @@ namespace System.Management.Automation.SecurityAccountsManager
|
||||
// Ideally , accountname should be processed to hold only accout name (without domain)
|
||||
// as we are keeping the domain in 'DomainName' variable.
|
||||
|
||||
int index = accountName.IndexOf("\\");
|
||||
int index = accountName.IndexOf("\\", StringComparison.CurrentCultureIgnoreCase);
|
||||
if (index > -1)
|
||||
{
|
||||
accountName = accountName.Substring(index + 1);
|
||||
|
||||
@@ -407,60 +407,16 @@ namespace System.Management.Automation.SecurityAccountsManager.Native.NtSam
|
||||
internal static extern UInt32 SamAddMemberToAlias(IntPtr aliasHandle,
|
||||
byte[] memberId); // PSID
|
||||
|
||||
[DllImport("samlib.dll")]
|
||||
internal static extern UInt32 SamAddMultipleMembersToAlias(IntPtr aliasHandle,
|
||||
IntPtr memberIds, // PSID
|
||||
UInt32 memberCount);
|
||||
|
||||
[DllImport("samlib.dll")]
|
||||
internal static extern UInt32 SamRemoveMemberFromAlias(IntPtr aliasHandle,
|
||||
byte[] memberId); // PSID
|
||||
|
||||
[DllImport("samlib.dll")]
|
||||
internal static extern UInt32 SamRemoveMultipleMembersFromAlias(IntPtr aliasHandle,
|
||||
IntPtr memberIds, // PSID
|
||||
UInt32 memberCount);
|
||||
|
||||
[DllImport("samlib.dll")]
|
||||
internal static extern UInt32 SamGetMembersInAlias(IntPtr aliasHandle,
|
||||
out IntPtr memberIds, // PSID **
|
||||
out UInt32 memberCount);
|
||||
#endregion Alias Functions
|
||||
|
||||
#region Group Functions
|
||||
[DllImport("samlib.dll")]
|
||||
internal static extern UInt32 SamEnumerateGroupsInDomain(IntPtr domainHandle,
|
||||
ref UInt32 enumerationContext,
|
||||
out IntPtr buffer,
|
||||
UInt32 preferredMaximumLength,
|
||||
out UInt32 countReturned);
|
||||
|
||||
[DllImport("samlib.dll")]
|
||||
internal static extern UInt32 SamCreateGroupInDomain(IntPtr domainHandle,
|
||||
ref UNICODE_STRING accountName,
|
||||
UInt32 desiredAccess,
|
||||
out IntPtr groupHandle,
|
||||
out UInt32 relativeId);
|
||||
|
||||
|
||||
[DllImport("samlib.dll")]
|
||||
internal static extern UInt32 SamOpenGroup(IntPtr domainHandle,
|
||||
UInt32 desiredAccess,
|
||||
UInt32 groupId,
|
||||
out IntPtr groupHandle);
|
||||
|
||||
[DllImport("samlib.dll")]
|
||||
internal static extern UInt32 SamQueryInformationGroup(IntPtr groupHandle,
|
||||
GROUP_INFORMATION_CLASS groupInformationClass,
|
||||
out IntPtr buffer);
|
||||
|
||||
|
||||
[DllImport("samlib.dll")]
|
||||
internal static extern UInt32 SamSetInformationGroup(IntPtr groupHandle,
|
||||
GROUP_INFORMATION_CLASS groupInformationClass,
|
||||
IntPtr buffer);
|
||||
#endregion Group Functions
|
||||
|
||||
#region User Functions
|
||||
[DllImport("samlib.dll")]
|
||||
internal static extern UInt32 SamOpenUser(IntPtr domainHandle,
|
||||
@@ -479,22 +435,6 @@ namespace System.Management.Automation.SecurityAccountsManager.Native.NtSam
|
||||
UInt32 preferredMaximumLength,
|
||||
out UInt32 countReturned);
|
||||
|
||||
[DllImport("samlib.dll")]
|
||||
internal static extern UInt32 SamEnumerateUsersInDomain2(IntPtr domainHandle,
|
||||
ref UInt32 enumerationContext,
|
||||
UInt32 userAccountControl,
|
||||
UInt32 filter,
|
||||
out IntPtr buffer,
|
||||
UInt32 preferredMaximumLength,
|
||||
out UInt32 countReturned);
|
||||
|
||||
[DllImport("samlib.dll")]
|
||||
internal static extern UInt32 SamCreateUserInDomain(IntPtr domainHandle,
|
||||
ref UNICODE_STRING accountName,
|
||||
UInt32 desiredAccess,
|
||||
out IntPtr userHandle,
|
||||
out UInt32 relativeId);
|
||||
|
||||
[DllImport("samlib.dll")]
|
||||
internal static extern UInt32 SamCreateUser2InDomain(IntPtr domainHandle,
|
||||
ref UNICODE_STRING accountName,
|
||||
|
||||
@@ -8,6 +8,14 @@ namespace System.Management.Automation.SecurityAccountsManager
|
||||
/// </summary>
|
||||
internal class StringUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// Private constructor to precent auto-generation of a default constructor with greater accessability.
|
||||
/// </summary>
|
||||
private StringUtil()
|
||||
{
|
||||
}
|
||||
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
internal static string Format(string str)
|
||||
{
|
||||
return string.Format(CultureInfo.CurrentCulture, str);
|
||||
|
||||
Reference in new issue
Block a user