| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2022, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 7 |
#AnsibleRequires -PowerShell Ansible.ModuleUtils.AddType |
| 8 |
|
| 9 |
$spec = @{ |
| 10 |
options = @{ |
| 11 |
domain_password = @{ type = 'str'; no_log = $true } |
| 12 |
domain_server = @{ type = 'str' } |
| 13 |
domain_username = @{ type = 'str' } |
| 14 |
filter = @{ type = 'str' } |
| 15 |
identity = @{ type = 'str' } |
| 16 |
include_deleted = @{ type = 'bool'; default = $false } |
| 17 |
ldap_filter = @{ type = 'str' } |
| 18 |
properties = @{ type = 'list'; elements = 'str' } |
| 19 |
search_base = @{ type = 'str' } |
| 20 |
search_scope = @{ type = 'str'; choices = @('base', 'one_level', 'subtree') } |
| 21 |
} |
| 22 |
supports_check_mode = $true |
| 23 |
mutually_exclusive = @( |
| 24 |
@('filter', 'identity', 'ldap_filter'), |
| 25 |
@('identity', 'search_base'), |
| 26 |
@('identity', 'search_scope') |
| 27 |
) |
| 28 |
required_one_of = @( |
| 29 |
, @('filter', 'identity', 'ldap_filter') |
| 30 |
) |
| 31 |
required_together = @(, @('domain_username', 'domain_password')) |
| 32 |
} |
| 33 |
|
| 34 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 35 |
|
| 36 |
$module.Result.objects = @() # Always ensure this is returned even in a failure. |
| 37 |
|
| 38 |
$domainServer = $module.Params.domain_server |
| 39 |
$domainPassword = $module.Params.domain_password |
| 40 |
$domainUsername = $module.Params.domain_username |
| 41 |
$filter = $module.Params.filter |
| 42 |
$identity = $module.Params.identity |
| 43 |
$includeDeleted = $module.Params.include_deleted |
| 44 |
$ldapFilter = $module.Params.ldap_filter |
| 45 |
$properties = $module.Params.properties |
| 46 |
$searchBase = $module.Params.search_base |
| 47 |
$searchScope = $module.Params.search_scope |
| 48 |
|
| 49 |
# Attempt import of ActiveDirectory module |
| 50 |
try { |
| 51 |
Import-Module -Name ActiveDirectory |
| 52 |
} |
| 53 |
catch { |
| 54 |
$module.FailJson("The ActiveDirectory module failed to load properly: $($_.Exception.Message)", $_) |
| 55 |
} |
| 56 |
|
| 57 |
$credential = $null |
| 58 |
if ($domainUsername) { |
| 59 |
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @( |
| 60 |
$domainUsername, |
| 61 |
(ConvertTo-SecureString -AsPlainText -Force -String $domainPassword) |
| 62 |
) |
| 63 |
} |
| 64 |
|
| 65 |
Add-CSharpType -AnsibleModule $module -References @' |
| 66 |
using System; |
| 67 |
|
| 68 |
namespace Ansible.WinDomainObjectInfo |
| 69 |
{ |
| 70 |
[Flags] |
| 71 |
public enum GroupType : uint |
| 72 |
{ |
| 73 |
GROUP_TYPE_BUILTIN_LOCAL_GROUP = 0x00000001, |
| 74 |
GROUP_TYPE_ACCOUNT_GROUP = 0x00000002, |
| 75 |
GROUP_TYPE_RESOURCE_GROUP = 0x00000004, |
| 76 |
GROUP_TYPE_UNIVERSAL_GROUP = 0x00000008, |
| 77 |
GROUP_TYPE_APP_BASIC_GROUP = 0x00000010, |
| 78 |
GROUP_TYPE_APP_QUERY_GROUP = 0x00000020, |
| 79 |
GROUP_TYPE_SECURITY_ENABLED = 0x80000000, |
| 80 |
} |
| 81 |
|
| 82 |
[Flags] |
| 83 |
public enum UserAccountControl : int |
| 84 |
{ |
| 85 |
ADS_UF_SCRIPT = 0x00000001, |
| 86 |
ADS_UF_ACCOUNTDISABLE = 0x00000002, |
| 87 |
ADS_UF_HOMEDIR_REQUIRED = 0x00000008, |
| 88 |
ADS_UF_LOCKOUT = 0x00000010, |
| 89 |
ADS_UF_PASSWD_NOTREQD = 0x00000020, |
| 90 |
ADS_UF_PASSWD_CANT_CHANGE = 0x00000040, |
| 91 |
ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 0x00000080, |
| 92 |
ADS_UF_TEMP_DUPLICATE_ACCOUNT = 0x00000100, |
| 93 |
ADS_UF_NORMAL_ACCOUNT = 0x00000200, |
| 94 |
ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = 0x00000800, |
| 95 |
ADS_UF_WORKSTATION_TRUST_ACCOUNT = 0x00001000, |
| 96 |
ADS_UF_SERVER_TRUST_ACCOUNT = 0x00002000, |
| 97 |
ADS_UF_DONT_EXPIRE_PASSWD = 0x00010000, |
| 98 |
ADS_UF_MNS_LOGON_ACCOUNT = 0x00020000, |
| 99 |
ADS_UF_SMARTCARD_REQUIRED = 0x00040000, |
| 100 |
ADS_UF_TRUSTED_FOR_DELEGATION = 0x00080000, |
| 101 |
ADS_UF_NOT_DELEGATED = 0x00100000, |
| 102 |
ADS_UF_USE_DES_KEY_ONLY = 0x00200000, |
| 103 |
ADS_UF_DONT_REQUIRE_PREAUTH = 0x00400000, |
| 104 |
ADS_UF_PASSWORD_EXPIRED = 0x00800000, |
| 105 |
ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 0x01000000, |
| 106 |
} |
| 107 |
|
| 108 |
[Flags] |
| 109 |
public enum SupportedEncryptionTypes : int |
| 110 |
{ |
| 111 |
None = 0x00, |
| 112 |
DES_CBC_CRC = 0x01, |
| 113 |
DES_CBC_MD5 = 0x02, |
| 114 |
RC4_HMAC = 0x04, |
| 115 |
AES128_CTS_HMAC_SHA1_96 = 0x08, |
| 116 |
AES256_CTS_HMAC_SHA1_96 = 0x10, |
| 117 |
} |
| 118 |
|
| 119 |
public enum sAMAccountType : int |
| 120 |
{ |
| 121 |
SAM_DOMAIN_OBJECT = 0x00000000, |
| 122 |
SAM_GROUP_OBJECT = 0x10000000, |
| 123 |
SAM_NON_SECURITY_GROUP_OBJECT = 0x10000001, |
| 124 |
SAM_ALIAS_OBJECT = 0x20000000, |
| 125 |
SAM_NON_SECURITY_ALIAS_OBJECT = 0x20000001, |
| 126 |
SAM_USER_OBJECT = 0x30000000, |
| 127 |
SAM_NORMAL_USER_ACCOUNT = 0x30000000, |
| 128 |
SAM_MACHINE_ACCOUNT = 0x30000001, |
| 129 |
SAM_TRUST_ACCOUNT = 0x30000002, |
| 130 |
SAM_APP_BASIC_GROUP = 0x40000000, |
| 131 |
SAM_APP_QUERY_GROUP = 0x40000001, |
| 132 |
SAM_ACCOUNT_TYPE_MAX = 0x7fffffff, |
| 133 |
} |
| 134 |
} |
| 135 |
'@ |
| 136 |
|
| 137 |
Function ConvertTo-OutputValue { |
| 138 |
[CmdletBinding()] |
| 139 |
Param ( |
| 140 |
[Parameter(Mandatory = $true)] |
| 141 |
[AllowNull()] |
| 142 |
[Object] |
| 143 |
$InputObject |
| 144 |
) |
| 145 |
|
| 146 |
if ($InputObject -is [System.Security.Principal.SecurityIdentifier]) { |
| 147 |
# Syntax: SID - Only serialize the SID as a string and not the other metadata properties. |
| 148 |
$sidInfo = @{ |
| 149 |
Sid = $InputObject.Value |
| 150 |
} |
| 151 |
|
| 152 |
# Try and map the SID to the account name, this may fail if the SID is invalid or not mappable. |
| 153 |
try { |
| 154 |
$sidInfo.Name = $InputObject.Translate([System.Security.Principal.NTAccount]).Value |
| 155 |
} |
| 156 |
catch [System.Security.Principal.IdentityNotMappedException] { |
| 157 |
$sidInfo.Name = $null |
| 158 |
} |
| 159 |
|
| 160 |
$sidInfo |
| 161 |
} |
| 162 |
elseif ($InputObject -is [Byte[]]) { |
| 163 |
# Syntax: Octet String - By default will serialize as a list of decimal values per byte, instead return a |
| 164 |
# Base64 string as Ansible can easily parse that. |
| 165 |
[System.Convert]::ToBase64String($InputObject) |
| 166 |
} |
| 167 |
elseif ($InputObject -is [DateTime]) { |
| 168 |
# Syntax: UTC Coded Time - .NET DateTimes serialized as in the form "Date(FILETIME)" which isn't easily |
| 169 |
# parsable by Ansible, instead return as an ISO 8601 string in the UTC timezone. |
| 170 |
[TimeZoneInfo]::ConvertTimeToUtc($InputObject).ToString("o") |
| 171 |
} |
| 172 |
elseif ($InputObject -is [System.Security.AccessControl.ObjectSecurity]) { |
| 173 |
# Complex object which isn't easily serializable. Instead we should just return the SDDL string. If a user |
| 174 |
# needs to parse this then they really need to reprocess the SDDL string and process their results on another |
| 175 |
# win_shell task. |
| 176 |
$InputObject.GetSecurityDescriptorSddlForm(([System.Security.AccessControl.AccessControlSections]::All)) |
| 177 |
} |
| 178 |
else { |
| 179 |
# Syntax: (All Others) - The default serialization handling of other syntaxes are fine, don't do anything. |
| 180 |
$InputObject |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
<# |
| 185 |
Calling Get-ADObject that returns multiple objects with -Properties * will only return the properties that were set on |
| 186 |
the first found object. To counter this problem we will first call Get-ADObject to list all the objects that match the |
| 187 |
filter specified then get the properties on each object. |
| 188 |
#> |
| 189 |
|
| 190 |
$commonParams = @{ |
| 191 |
IncludeDeletedObjects = $includeDeleted |
| 192 |
} |
| 193 |
|
| 194 |
if ($credential) { |
| 195 |
$commonParams.Credential = $credential |
| 196 |
} |
| 197 |
|
| 198 |
if ($domainServer) { |
| 199 |
$commonParams.Server = $domainServer |
| 200 |
} |
| 201 |
|
| 202 |
# First get the IDs for all the AD objects that match the filter specified. |
| 203 |
$getParams = @{ |
| 204 |
Properties = @('DistinguishedName', 'ObjectGUID') |
| 205 |
} |
| 206 |
|
| 207 |
if ($filter) { |
| 208 |
$getParams.Filter = $filter |
| 209 |
} |
| 210 |
elseif ($identity) { |
| 211 |
$getParams.Identity = $identity |
| 212 |
} |
| 213 |
elseif ($ldapFilter) { |
| 214 |
$getParams.LDAPFilter = $ldapFilter |
| 215 |
} |
| 216 |
|
| 217 |
# Explicit check on $null as an empty string is different from not being set. |
| 218 |
if ($null -ne $searchBase) { |
| 219 |
$getParams.SearchBase = $searchbase |
| 220 |
} |
| 221 |
|
| 222 |
if ($searchScope) { |
| 223 |
$getParams.SearchScope = switch ($searchScope) { |
| 224 |
base { 'Base' } |
| 225 |
one_level { 'OneLevel' } |
| 226 |
subtree { 'Subtree' } |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
try { |
| 231 |
# We run this in a custom PowerShell pipeline so that users of this module can't use any of the variables defined |
| 232 |
# above in their filter. While the cmdlet won't execute sub expressions we don't want anyone implicitly relying on |
| 233 |
# a defined variable in this module in case we ever change the name or remove it. |
| 234 |
$iss = [InitialSessionState]::CreateDefault() |
| 235 |
$iss.ImportPSModule("ActiveDirectory") |
| 236 |
$ps = [PowerShell]::Create($iss) |
| 237 |
$null = $ps.AddCommand('Get-ADObject').AddParameters($commonParams).AddParameters($getParams) |
| 238 |
$null = $ps.AddCommand('Select-Object').AddParameter('Property', @('DistinguishedName', 'ObjectGUID')) |
| 239 |
|
| 240 |
$foundGuids = @($ps.Invoke()) |
| 241 |
} |
| 242 |
catch { |
| 243 |
# Because we ran in a pipeline we can't catch ADIdentityNotFoundException. Instead this scans each InnerException |
| 244 |
# to see if it contains ADIdentityNotFoundException. |
| 245 |
$exp = $_.Exception |
| 246 |
$foundGuids = $null |
| 247 |
while ($exp) { |
| 248 |
$exp = $exp.InnerException |
| 249 |
if ($exp -is [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]) { |
| 250 |
$foundGuids = @() |
| 251 |
break |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
if ($null -eq $foundGuids) { |
| 256 |
# The exception is from the .Invoke() call, compare on the InnerException which was what was actually raised by |
| 257 |
# the pipeline. |
| 258 |
$innerException = $_.Exception.InnerException.InnerException |
| 259 |
if ($innerException -is [Microsoft.ActiveDirectory.Management.ADServerDownException]) { |
| 260 |
# Point users in the direction of the double hop problem as that is what is typically the cause of this. |
| 261 |
$msg = "Failed to contact the AD server, this could be caused by the double hop problem over WinRM. " |
| 262 |
$msg += "Try using the module with auth as Kerberos with credential delegation or CredSSP, become, or " |
| 263 |
$msg += "defining the domain_username and domain_password module parameters." |
| 264 |
$module.FailJson($msg, $innerException) |
| 265 |
} |
| 266 |
else { |
| 267 |
throw $innerException |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
$getParams = @{} |
| 273 |
if ($properties) { |
| 274 |
$getParams.Properties = $properties |
| 275 |
} |
| 276 |
$module.Result.objects = @(foreach ($adId in $foundGuids) { |
| 277 |
try { |
| 278 |
$adObject = Get-ADObject @commonParams @getParams -Identity $adId.ObjectGUID |
| 279 |
} |
| 280 |
catch { |
| 281 |
$msg = "Failed to retrieve properties for AD Object '$($adId.DistinguishedName)': $($_.Exception.Message)" |
| 282 |
$module.Warn($msg) |
| 283 |
continue |
| 284 |
} |
| 285 |
|
| 286 |
$propertyNames = $adObject.PropertyNames |
| 287 |
$propertyNames += ($properties | Where-Object { $_ -ne '*' }) |
| 288 |
|
| 289 |
# Now process each property to an easy to represent string |
| 290 |
$filteredObject = [Ordered]@{} |
| 291 |
foreach ($name in ($propertyNames | Sort-Object)) { |
| 292 |
# In the case of explicit properties that were asked for but weren't set, Get-ADObject won't actually return |
| 293 |
# the property so this is a defensive check against that scenario. |
| 294 |
if (-not $adObject.PSObject.Properties.Name.Contains($name)) { |
| 295 |
$filteredObject.$name = $null |
| 296 |
continue |
| 297 |
} |
| 298 |
|
| 299 |
$value = $adObject.$name |
| 300 |
if ($value -is [Microsoft.ActiveDirectory.Management.ADPropertyValueCollection]) { |
| 301 |
$value = @( |
| 302 |
foreach ($v in $value) { |
| 303 |
ConvertTo-OutputValue -InputObject $v |
| 304 |
} |
| 305 |
) |
| 306 |
} |
| 307 |
else { |
| 308 |
$value = ConvertTo-OutputValue -InputObject $value |
| 309 |
} |
| 310 |
$filteredObject.$name = $value |
| 311 |
|
| 312 |
# For these 3 properties, add an _AnsibleFlags attribute which contains the enum strings that are set. |
| 313 |
$enumValue = switch ($name) { |
| 314 |
groupType { |
| 315 |
[Ansible.WinDomainObjectInfo.GroupType]$value |
| 316 |
} |
| 317 |
'msDS-SupportedEncryptionTypes' { |
| 318 |
[Ansible.WinDomainObjectInfo.SupportedEncryptionTypes]$value |
| 319 |
} |
| 320 |
sAMAccountType { |
| 321 |
[Ansible.WinDomainObjectInfo.sAMAccountType]$value |
| 322 |
} |
| 323 |
userAccountControl { |
| 324 |
[Ansible.WinDomainObjectInfo.UserAccountControl]$value |
| 325 |
} |
| 326 |
} |
| 327 |
if ($null -ne $enumValue) { |
| 328 |
$filteredObject."$($name)_AnsibleFlags" = $enumValue.ToString() -split ', ' |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
$filteredObject |
| 333 |
}) |
| 334 |
|
| 335 |
$module.ExitJson() |