-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from webmd-health-services/feature/test-cprincipal
Initial version, migrated from Carbon
- Loading branch information
Showing
21 changed files
with
793 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
# Carbon.Accounts PowerShell Module Changelog | ||
|
||
## 1.0.0 | ||
|
||
### Upgrade Instructions | ||
|
||
If switching to Carbon.Accounts from Carbon, do the following: | ||
|
||
* Remove usages of the `ConnectedServer` property on `System.DirectoryServices.AccountManagement.Principal` objects. | ||
That was an extended type property added by Carbon and it no longer exists. | ||
* Remove usages of the `Carbon.Identity` and `Carbon.IdentityType` types. `Carbon.Accounts` now uses and returns native | ||
PowerShell classes and enums instead. The new native/classes enums are identical to the old compiled types, so no need | ||
to update object usages. | ||
|
||
### Added | ||
|
||
* `ConvertTo-CSecurityIdentifier` (from Carbon). | ||
* `Resolve-CIdentity` (from Carbon). | ||
* `Test-CIdentity` (from Carbon). | ||
* `Resolve-CIdentityName` (from Carbon). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
Carbon.Accounts/Functions/ConvertTo-CSecurityIdentifier.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
|
||
function ConvertTo-CSecurityIdentifier | ||
{ | ||
<# | ||
.SYNOPSIS | ||
Converts a string or byte array security identifier into a `System.Security.Principal.SecurityIdentifier` object. | ||
.DESCRIPTION | ||
`ConvertTo-CSecurityIdentifier` converts a SID in SDDL form (as a string), in binary form (as a byte array) into a | ||
`System.Security.Principal.SecurityIdentifier` object. It also accepts | ||
`System.Security.Principal.SecurityIdentifier` objects, and returns them back to you. | ||
If the string or byte array don't represent a SID, an error is written and nothing is returned. | ||
.LINK | ||
Resolve-CIdentity | ||
.LINK | ||
Resolve-CIdentityName | ||
.EXAMPLE | ||
ConvertTo-CSecurityIdentifier -SID 'S-1-5-21-2678556459-1010642102-471947008-1017' | ||
Demonstrates how to convert a a SID in SDDL into a `System.Security.Principal.SecurityIdentifier` object. | ||
.EXAMPLE | ||
ConvertTo-CSecurityIdentifier -SID (New-Object 'Security.Principal.SecurityIdentifier' 'S-1-5-21-2678556459-1010642102-471947008-1017') | ||
Demonstrates that you can pass a `SecurityIdentifier` object as the value of the SID parameter. The SID you passed | ||
in will be returned to you unchanged. | ||
.EXAMPLE | ||
ConvertTo-CSecurityIdentifier -SID $sidBytes | ||
Demonstrates that you can use a byte array that represents a SID as the value of the `SID` parameter. | ||
#> | ||
[CmdletBinding()] | ||
param( | ||
# The SID to convert to a `System.Security.Principal.SecurityIdentifier`. Accepts a SID in SDDL form as a | ||
# `string`, a `System.Security.Principal.SecurityIdentifier` object, or a SID in binary form as an array of | ||
# bytes. | ||
[Parameter(Mandatory)] | ||
[Object] $SID | ||
) | ||
|
||
Set-StrictMode -Version 'Latest' | ||
Use-CallerPreference -Cmdlet $PSCmdlet -Session $ExecutionContext.SessionState | ||
|
||
try | ||
{ | ||
if ( $SID -is [string]) | ||
{ | ||
New-Object 'Security.Principal.SecurityIdentifier' $SID | ||
} | ||
elseif ($SID -is [byte[]]) | ||
{ | ||
New-Object 'Security.Principal.SecurityIdentifier' $SID,0 | ||
} | ||
elseif ($SID -is [Security.Principal.SecurityIdentifier]) | ||
{ | ||
$SID | ||
} | ||
else | ||
{ | ||
$msg = "Invalid SID parameter value [$($SID.GetType().FullName)]${SID}. Only " + | ||
'[System.Security.Principal.SecurityIdentifier] objects, SIDs in SDDL form as a [String], or SIDs ' + | ||
'in binary form as a byte array are allowed.' | ||
return | ||
} | ||
} | ||
catch | ||
{ | ||
$sidDisplayMsg = '' | ||
if ($SID -is [String]) | ||
{ | ||
$sidDisplayMsg = " ""${SID}""" | ||
} | ||
elseif ($SID -is [byte[]]) | ||
{ | ||
$sidDisplayMsg = " [$($SID -join ', ')]" | ||
} | ||
$msg = "Exception converting SID${sidDisplayMsg} to a [System.Security.Principal.SecurityIdentifier] " + | ||
'object. This usually means you passed an invalid SID in SDDL form (as a string) or an invalid SID ' + | ||
"in binary form (as a byte array): ${_}" | ||
Write-Error $msg -ErrorAction $ErrorActionPreference | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
|
||
function Resolve-CIdentity | ||
{ | ||
<# | ||
.SYNOPSIS | ||
Gets domain, name, type, and SID information about a user or group. | ||
.DESCRIPTION | ||
The `Resolve-CIdentity` function takes an identity name or security identifier (SID) and gets its canonical | ||
representation. It returns a `Carbon_Accounts_Identity` object, which contains the following information about the | ||
identity: | ||
* Domain - the domain the user was found in | ||
* FullName - the users full name, e.g. Domain\Name | ||
* Name - the user's username or the group's name | ||
* Type - the Sid type. | ||
* Sid - the account's security identifier as a `System.Security.Principal.SecurityIdentifier` object. | ||
The common name for an account is not always the canonical name used by the operating system. For example, the | ||
local Administrators group is actually called BUILTIN\Administrators. This function uses the `LookupAccountName` | ||
and `LookupAccountSid` Windows functions to resolve an account name or security identifier into its domain, name, | ||
full name, SID, and SID type. | ||
You may pass a `System.Security.Principal.SecurityIdentifer`, a SID in SDDL form (as a string), or a SID in binary | ||
form (a byte array) as the value to the `SID` parameter. You'll get an error and nothing returned if the SDDL or | ||
byte array SID are invalid. | ||
If the name or security identifier doesn't represent an actual user or group, an error is written and nothing is | ||
returned. | ||
.LINK | ||
Test-CIdentity | ||
.LINK | ||
Resolve-CIdentityName | ||
.LINK | ||
http://msdn.microsoft.com/en-us/library/system.security.principal.securityidentifier.aspx | ||
.LINK | ||
http://msdn.microsoft.com/en-us/library/windows/desktop/aa379601.aspx | ||
.LINK | ||
ConvertTo-CSecurityIdentifier | ||
.LINK | ||
Resolve-CIdentityName | ||
.LINK | ||
Test-CIdentity | ||
.OUTPUTS | ||
Carbon_Accounts_Identity. | ||
.EXAMPLE | ||
Resolve-CIdentity -Name 'Administrators' | ||
Returns an object representing the `Administrators` group. | ||
.EXAMPLE | ||
Resolve-CIdentity -SID 'S-1-5-21-2678556459-1010642102-471947008-1017' | ||
Demonstrates how to use a SID in SDDL form to convert a SID into an identity. | ||
.EXAMPLE | ||
Resolve-CIdentity -SID ([Security.Principal.SecurityIdentifier]::New()'S-1-5-21-2678556459-1010642102-471947008-1017') | ||
Demonstrates that you can pass a `SecurityIdentifier` object as the value of the SID parameter. | ||
.EXAMPLE | ||
Resolve-CIdentity -SID $sidBytes | ||
Demonstrates that you can use a byte array that represents a SID as the value of the `SID` parameter. | ||
#> | ||
[CmdletBinding()] | ||
param( | ||
# The name of the identity to return. | ||
[Parameter(Mandatory, ParameterSetName='ByName', Position=0)] | ||
[string] $Name, | ||
|
||
# The SID of the identity to return. Accepts a SID in SDDL form as a `string`, a | ||
# `System.Security.Principal.SecurityIdentifier` object, or a SID in binary form as an array of bytes. | ||
[Parameter(Mandatory , ParameterSetName='BySid')] | ||
[Object] $SID | ||
) | ||
|
||
Set-StrictMode -Version 'Latest' | ||
Use-CallerPreference -Cmdlet $PSCmdlet -Session $ExecutionContext.SessionState | ||
|
||
if ($PSCmdlet.ParameterSetName -eq 'BySid') | ||
{ | ||
$SID = ConvertTo-CSecurityIdentifier -SID $SID | ||
if (-not $SID) | ||
{ | ||
return | ||
} | ||
|
||
$sidBytes = [byte[]]::New($SID.BinaryLength) | ||
$SID.GetBinaryForm($sidBytes, 0) | ||
$account = Invoke-AdvapiLookupAccountSid -Sid $sidBytes | ||
if (-not $account) | ||
{ | ||
Write-Error -Message "SID ""${SID}"" not found." -ErrorAction $ErrorActionPreference | ||
return | ||
} | ||
return [Carbon_Accounts_Identity]::New($account.ReferencedDomainName, $account.Name, $SID, $account.Use) | ||
} | ||
|
||
if ($Name.StartsWith('.\')) | ||
{ | ||
$username = $Name.Substring(2) | ||
$Name = "$([Environment]::MachineName)\${username}" | ||
$identity = Resolve-CIdentity -Name $Name | ||
if (-not $identity) | ||
{ | ||
$Name = "BUILTIN\${username}" | ||
$identity = Resolve-CIdentity -Name $Name | ||
} | ||
return $identity | ||
} | ||
|
||
if ($Name.Equals("LocalSystem", [StringComparison]::InvariantCultureIgnoreCase)) | ||
{ | ||
$Name = "NT AUTHORITY\SYSTEM" | ||
} | ||
|
||
$account = Invoke-AdvapiLookupAccountName -AccountName $Name | ||
if (-not $account) | ||
{ | ||
Write-Error -Message "Identity ""${Name}"" not found." -ErrorAction $ErrorActionPreference | ||
return | ||
} | ||
|
||
$sid = [SecurityIdentifier]::New($account.Sid, 0) | ||
$ntAccount = $sid.Translate([NTAccount]) | ||
$domainName,$accountName = $ntAccount.Value.Split('\', 2) | ||
if (-not $accountName) | ||
{ | ||
$accountName = $domainName | ||
$domainName = '' | ||
} | ||
return [Carbon_Accounts_Identity]::New($domainName, $accountName, $sid, $account.Use) | ||
|
||
} |
Oops, something went wrong.