Debian Codegraph @

lib/ansible/module_utils/powershell/Ansible.ModuleUtils.SID.psm1

raw
1 # Copyright (c) 2017 Ansible Project
2 # Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
3
4 Function Convert-FromSID($sid) {
5 # Converts a SID to a Down-Level Logon name in the form of DOMAIN\UserName
6 # If the SID is for a local user or group then DOMAIN would be the server
7 # name.
8
9 $account_object = New-Object System.Security.Principal.SecurityIdentifier($sid)
10 try {
11 $nt_account = $account_object.Translate([System.Security.Principal.NTAccount])
12 }
13 catch {
14 Fail-Json -obj @{} -message "failed to convert sid '$sid' to a logon name: $($_.Exception.Message)"
15 }
16
17 return $nt_account.Value
18 }
19
20 Function Convert-ToSID {
21 [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingEmptyCatchBlock", "",
22 Justification = "We don't care if converting to a SID fails, just that it failed or not")]
23 param($account_name)
24 # Converts an account name to a SID, it can take in the following forms
25 # SID: Will just return the SID value that was passed in
26 # UPN:
27 # principal@domain (Domain users only)
28 # Down-Level Login Name
29 # DOMAIN\principal (Domain)
30 # SERVERNAME\principal (Local)
31 # .\principal (Local)
32 # NT AUTHORITY\SYSTEM (Local Service Accounts)
33 # Login Name
34 # principal (Local/Local Service Accounts)
35
36 try {
37 $sid = New-Object -TypeName System.Security.Principal.SecurityIdentifier -ArgumentList $account_name
38 return $sid.Value
39 }
40 catch {}
41
42 if ($account_name -like "*\*") {
43 $account_name_split = $account_name -split "\\"
44 if ($account_name_split[0] -eq ".") {
45 $domain = $env:COMPUTERNAME
46 }
47 else {
48 $domain = $account_name_split[0]
49 }
50 $username = $account_name_split[1]
51 }
52 else {
53 $domain = $null
54 $username = $account_name
55 }
56
57 if ($domain) {
58 # searching for a local group with the servername prefixed will fail,
59 # need to check for this situation and only use NTAccount(String)
60 if ($domain -eq $env:COMPUTERNAME) {
61 $adsi = [ADSI]("WinNT://$env:COMPUTERNAME,computer")
62 $group = $adsi.psbase.children | Where-Object { $_.schemaClassName -eq "group" -and $_.Name -eq $username }
63 }
64 else {
65 $group = $null
66 }
67 if ($group) {
68 $account = New-Object System.Security.Principal.NTAccount($username)
69 }
70 else {
71 $account = New-Object System.Security.Principal.NTAccount($domain, $username)
72 }
73 }
74 else {
75 # when in a domain NTAccount(String) will favour domain lookups check
76 # if username is a local user and explicitly search on the localhost for
77 # that account
78 $adsi = [ADSI]("WinNT://$env:COMPUTERNAME,computer")
79 $user = $adsi.psbase.children | Where-Object { $_.schemaClassName -eq "user" -and $_.Name -eq $username }
80 if ($user) {
81 $account = New-Object System.Security.Principal.NTAccount($env:COMPUTERNAME, $username)
82 }
83 else {
84 $account = New-Object System.Security.Principal.NTAccount($username)
85 }
86 }
87
88 try {
89 $account_sid = $account.Translate([System.Security.Principal.SecurityIdentifier])
90 }
91 catch {
92 Fail-Json @{} "account_name $account_name is not a valid account, cannot get SID: $($_.Exception.Message)"
93 }
94
95 return $account_sid.Value
96 }
97
98 # this line must stay at the bottom to ensure all defined module parts are exported
99 Export-ModuleMember -Alias * -Function * -Cmdlet *