| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2018, Kevin Subileau (@ksubileau) |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#Requires -Module Ansible.ModuleUtils.Legacy |
| 7 |
#Requires -Module Ansible.ModuleUtils.SID |
| 8 |
|
| 9 |
$ErrorActionPreference = "Stop" |
| 10 |
|
| 11 |
# List of authentication methods as string. Used for parameter validation and conversion to integer flag, so order is important! |
| 12 |
$computer_group_types = @("rdg_group", "ad_network_resource_group", "allow_any") |
| 13 |
$computer_group_types_wmi = @{rdg_group = "RG"; ad_network_resource_group = "CG"; allow_any = "ALL" } |
| 14 |
|
| 15 |
$params = Parse-Args -arguments $args -supports_check_mode $true |
| 16 |
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false |
| 17 |
$diff_mode = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -default $false |
| 18 |
|
| 19 |
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true |
| 20 |
$description = Get-AnsibleParam -obj $params -name "description" -type "str" |
| 21 |
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "absent", "present", "enabled", "disabled" |
| 22 |
$computer_group_type = Get-AnsibleParam -obj $params -name "computer_group_type" -type "str" -validateset $computer_group_types |
| 23 |
$computer_group_failure = ($computer_group_type -eq "ad_network_resource_group" -or $computer_group_type -eq "rdg_group") |
| 24 |
$computer_group = Get-AnsibleParam -obj $params -name "computer_group" -type "str" -failifempty $computer_group_failure |
| 25 |
$user_groups = Get-AnsibleParam -obj $params -name "user_groups" -type "list" |
| 26 |
$allowed_ports = Get-AnsibleParam -obj $params -name "allowed_ports" -type "list" |
| 27 |
|
| 28 |
|
| 29 |
Function ConvertTo-Sid { |
| 30 |
[OutputType([string])] |
| 31 |
[CmdletBinding()] |
| 32 |
param ( |
| 33 |
[Parameter(Mandatory, ValueFromPipeline)] |
| 34 |
[string[]] |
| 35 |
$InputObject |
| 36 |
) |
| 37 |
|
| 38 |
process { |
| 39 |
foreach ($user in $InputObject) { |
| 40 |
# RDS uses the UPN format with the builtin domain but Convert-ToSID tries to look this up as a domain. |
| 41 |
# Ensure the input value is in the Netlogon format to ensure BUILTIN is resolved properly |
| 42 |
if ($user.EndsWith("@builtin", [System.StringComparison]::OrdinalIgnoreCase)) { |
| 43 |
$user = "BUILTIN\$($user.Substring(0, $user.Length - 8))" |
| 44 |
} |
| 45 |
|
| 46 |
Convert-ToSID -account_name $user |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
function Get-RAP([string] $name) { |
| 53 |
$rap_path = "RDS:\GatewayServer\RAP\$name" |
| 54 |
$rap = @{ |
| 55 |
Name = $name |
| 56 |
} |
| 57 |
|
| 58 |
# Fetch RAP properties |
| 59 |
Get-ChildItem -LiteralPath $rap_path | ForEach-Object { $rap.Add($_.Name, $_.CurrentValue) } |
| 60 |
# Convert boolean values |
| 61 |
$rap.Enabled = $rap.Status -eq 1 |
| 62 |
$rap.Remove("Status") |
| 63 |
|
| 64 |
# Convert computer group name from UPN to Down-Level Logon format |
| 65 |
if ($rap.ComputerGroupType -ne 2) { |
| 66 |
$rap.ComputerGroup = Convert-FromSID -sid (ConvertTo-SID -InputObject $rap.ComputerGroup) |
| 67 |
} |
| 68 |
|
| 69 |
# Convert multiple choices values |
| 70 |
$rap.ComputerGroupType = $computer_group_types[$rap.ComputerGroupType] |
| 71 |
|
| 72 |
# Convert allowed ports from string to list |
| 73 |
if ($rap.PortNumbers -eq '*') { |
| 74 |
$rap.PortNumbers = @("any") |
| 75 |
} |
| 76 |
else { |
| 77 |
$rap.PortNumbers = @($rap.PortNumbers -split ',') |
| 78 |
} |
| 79 |
|
| 80 |
# Fetch RAP user groups in Down-Level Logon format |
| 81 |
$rap.UserGroups = @( |
| 82 |
Get-ChildItem -LiteralPath "$rap_path\UserGroups" | |
| 83 |
Select-Object -ExpandProperty Name | |
| 84 |
ForEach-Object { Convert-FromSID -sid (ConvertTo-Sid -InputObject $_) } |
| 85 |
) |
| 86 |
|
| 87 |
return $rap |
| 88 |
} |
| 89 |
|
| 90 |
function Set-RAPPropertyValue { |
| 91 |
[CmdletBinding(SupportsShouldProcess = $true)] |
| 92 |
param ( |
| 93 |
[Parameter(Mandatory = $true)] |
| 94 |
[string] $name, |
| 95 |
[Parameter(Mandatory = $true)] |
| 96 |
[string] $property, |
| 97 |
[Parameter(Mandatory = $true)] |
| 98 |
$value, |
| 99 |
[Parameter()] |
| 100 |
$resultobj = @{} |
| 101 |
) |
| 102 |
|
| 103 |
$rap_path = "RDS:\GatewayServer\RAP\$name" |
| 104 |
|
| 105 |
try { |
| 106 |
Set-Item -LiteralPath "$rap_path\$property" -Value $value -ErrorAction stop |
| 107 |
} |
| 108 |
catch { |
| 109 |
Fail-Json -obj $resultobj -message "Failed to set property $property of RAP ${name}: $($_.Exception.Message)" |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
$result = @{ |
| 114 |
changed = $false |
| 115 |
} |
| 116 |
$diff_text = $null |
| 117 |
|
| 118 |
# Validate RAP name |
| 119 |
if ($name -match "[*/\\;:?`"<>|\t]+") { |
| 120 |
Fail-Json -obj $result -message "Invalid character in RAP name." |
| 121 |
} |
| 122 |
|
| 123 |
# Validate user groups |
| 124 |
if ($null -ne $user_groups) { |
| 125 |
if ($user_groups.Count -lt 1) { |
| 126 |
Fail-Json -obj $result -message "Parameter 'user_groups' cannot be an empty list." |
| 127 |
} |
| 128 |
|
| 129 |
$user_groups = $user_groups | ForEach-Object { |
| 130 |
$group = $_ |
| 131 |
# Test that the group is resolvable on the local machine |
| 132 |
$sid = ConvertTo-Sid -InputObject $group |
| 133 |
if (!$sid) { |
| 134 |
Fail-Json -obj $result -message "$group is not a valid user group on the host machine or domain." |
| 135 |
} |
| 136 |
|
| 137 |
# Return the normalized group name in Down-Level Logon format |
| 138 |
Convert-FromSID -sid $sid |
| 139 |
} |
| 140 |
$user_groups = @($user_groups) |
| 141 |
} |
| 142 |
|
| 143 |
# Ensure RemoteDesktopServices module is loaded |
| 144 |
if ($null -eq (Get-Module -Name RemoteDesktopServices -ErrorAction SilentlyContinue)) { |
| 145 |
Import-Module -Name RemoteDesktopServices |
| 146 |
} |
| 147 |
|
| 148 |
# Validate computer group parameter |
| 149 |
if ($computer_group_type -eq "allow_any" -and $null -ne $computer_group) { |
| 150 |
Add-Warning -obj $result -message "Parameter 'computer_group' ignored because the computer_group_type is set to allow_any." |
| 151 |
} |
| 152 |
elseif ($computer_group_type -eq "rdg_group" -and -not (Test-Path -LiteralPath "RDS:\GatewayServer\GatewayManagedComputerGroups\$computer_group")) { |
| 153 |
Fail-Json -obj $result -message "$computer_group is not a valid gateway managed computer group" |
| 154 |
} |
| 155 |
elseif ($computer_group_type -eq "ad_network_resource_group") { |
| 156 |
$sid = ConvertTo-Sid -InputObject $computer_group |
| 157 |
if (!$sid) { |
| 158 |
Fail-Json -obj $result -message "$computer_group is not a valid computer group on the host machine or domain." |
| 159 |
} |
| 160 |
# Ensure the group name is in Down-Level Logon format |
| 161 |
$computer_group = Convert-FromSID -sid $sid |
| 162 |
} |
| 163 |
|
| 164 |
# Validate port numbers |
| 165 |
if ($null -ne $allowed_ports) { |
| 166 |
foreach ($port in $allowed_ports) { |
| 167 |
if (-not ($port -eq "any" -or ($port -is [int] -and $port -ge 1 -and $port -le 65535))) { |
| 168 |
Fail-Json -obj $result -message "$port is not a valid port number." |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
# Check if a RAP with the given name already exists |
| 174 |
$rap_exist = Test-Path -LiteralPath "RDS:\GatewayServer\RAP\$name" |
| 175 |
|
| 176 |
if ($state -eq 'absent') { |
| 177 |
if ($rap_exist) { |
| 178 |
Remove-Item -LiteralPath "RDS:\GatewayServer\RAP\$name" -Recurse -WhatIf:$check_mode |
| 179 |
$diff_text += "-[$name]" |
| 180 |
$result.changed = $true |
| 181 |
} |
| 182 |
} |
| 183 |
else { |
| 184 |
$diff_text_added_prefix = '' |
| 185 |
if (-not $rap_exist) { |
| 186 |
if ($null -eq $user_groups) { |
| 187 |
Fail-Json -obj $result -message "User groups must be defined to create a new RAP." |
| 188 |
} |
| 189 |
|
| 190 |
# Computer group type is required when creating a new RAP. Set it to allow connect to any resource by default. |
| 191 |
if ($null -eq $computer_group_type) { |
| 192 |
$computer_group_type = "allow_any" |
| 193 |
} |
| 194 |
|
| 195 |
# Create a new RAP |
| 196 |
if (-not $check_mode) { |
| 197 |
$RapArgs = @{ |
| 198 |
Name = $name |
| 199 |
ResourceGroupType = 'ALL' |
| 200 |
UserGroupNames = $user_groups -join ';' |
| 201 |
ProtocolNames = 'RDP' |
| 202 |
PortNumbers = '*' |
| 203 |
} |
| 204 |
$cimParams = @{ |
| 205 |
Namespace = "Root\CIMV2\TerminalServices" |
| 206 |
ClassName = "Win32_TSGatewayResourceAuthorizationPolicy" |
| 207 |
MethodName = "Create" |
| 208 |
Arguments = $RapArgs |
| 209 |
} |
| 210 |
$return = Invoke-CimMethod @cimParams |
| 211 |
if ($return.ReturnValue -ne 0) { |
| 212 |
Fail-Json -obj $result -message "Failed to create RAP $name (code: $($return.ReturnValue))" |
| 213 |
} |
| 214 |
} |
| 215 |
$rap_exist = -not $check_mode |
| 216 |
|
| 217 |
$diff_text_added_prefix = '+' |
| 218 |
$result.changed = $true |
| 219 |
} |
| 220 |
|
| 221 |
$diff_text += "$diff_text_added_prefix[$name]`n" |
| 222 |
|
| 223 |
# We cannot configure a RAP that was created above in check mode as it won't actually exist |
| 224 |
if ($rap_exist) { |
| 225 |
$rap = Get-RAP -Name $name |
| 226 |
$wmi_rap = Get-CimInstance -ClassName Win32_TSGatewayResourceAuthorizationPolicy -Namespace Root\CIMv2\TerminalServices -Filter "name='$($name)'" |
| 227 |
|
| 228 |
if ($state -in @('disabled', 'enabled')) { |
| 229 |
$rap_enabled = $state -ne 'disabled' |
| 230 |
if ($rap.Enabled -ne $rap_enabled) { |
| 231 |
$diff_text += "-State = $(@('disabled', 'enabled')[[int]$rap.Enabled])`n+State = $state`n" |
| 232 |
Set-RAPPropertyValue -Name $name -Property Status -Value ([int]$rap_enabled) -ResultObj $result -WhatIf:$check_mode |
| 233 |
$result.changed = $true |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if ($null -ne $description -and $description -ne $rap.Description) { |
| 238 |
Set-RAPPropertyValue -Name $name -Property Description -Value $description -ResultObj $result -WhatIf:$check_mode |
| 239 |
$diff_text += "-Description = $($rap.Description)`n+Description = $description`n" |
| 240 |
$result.changed = $true |
| 241 |
} |
| 242 |
|
| 243 |
if ($null -ne $allowed_ports -and @(Compare-Object $rap.PortNumbers $allowed_ports -SyncWindow 0).Count -ne 0) { |
| 244 |
$diff_text += "-AllowedPorts = [$($rap.PortNumbers -join ',')]`n+AllowedPorts = [$($allowed_ports -join ',')]`n" |
| 245 |
if ($allowed_ports -contains 'any') { $allowed_ports = '*' } |
| 246 |
Set-RAPPropertyValue -Name $name -Property PortNumbers -Value $allowed_ports -ResultObj $result -WhatIf:$check_mode |
| 247 |
$result.changed = $true |
| 248 |
} |
| 249 |
|
| 250 |
if ($null -ne $computer_group_type -and $computer_group_type -ne $rap.ComputerGroupType) { |
| 251 |
$diff_text += "-ComputerGroupType = $($rap.ComputerGroupType)`n+ComputerGroupType = $computer_group_type`n" |
| 252 |
if ($computer_group_type -ne "allow_any") { |
| 253 |
$diff_text += "+ComputerGroup = $computer_group`n" |
| 254 |
} |
| 255 |
$return = $wmi_rap | Invoke-CimMethod -MethodName SetResourceGroup -Arguments @{ |
| 256 |
ResourceGroupName = $computer_group |
| 257 |
ResourceGroupType = $computer_group_types_wmi.$($computer_group_type) |
| 258 |
} |
| 259 |
if ($return.ReturnValue -ne 0) { |
| 260 |
Fail-Json -obj $result -message "Failed to set computer group type to $($computer_group_type) (code: $($return.ReturnValue))" |
| 261 |
} |
| 262 |
|
| 263 |
$result.changed = $true |
| 264 |
|
| 265 |
} |
| 266 |
elseif ($null -ne $computer_group -and $computer_group -ne $rap.ComputerGroup) { |
| 267 |
$diff_text += "-ComputerGroup = $($rap.ComputerGroup)`n+ComputerGroup = $computer_group`n" |
| 268 |
$return = $wmi_rap | Invoke-CimMethod -MethodName SetResourceGroup -Arguments @{ |
| 269 |
ResourceGroupName = $computer_group |
| 270 |
ResourceGroupType = $computer_group_types_wmi.$($rap.ComputerGroupType) |
| 271 |
} |
| 272 |
if ($return.ReturnValue -ne 0) { |
| 273 |
Fail-Json -obj $result -message "Failed to set computer group name to $($computer_group) (code: $($return.ReturnValue))" |
| 274 |
} |
| 275 |
$result.changed = $true |
| 276 |
} |
| 277 |
|
| 278 |
if ($null -ne $user_groups) { |
| 279 |
$groups_to_remove = @($rap.UserGroups | Where-Object { $user_groups -notcontains $_ }) |
| 280 |
$groups_to_add = @($user_groups | Where-Object { $rap.UserGroups -notcontains $_ }) |
| 281 |
|
| 282 |
$user_groups_diff = $null |
| 283 |
foreach ($group in $groups_to_add) { |
| 284 |
if (-not $check_mode) { |
| 285 |
$return = $wmi_rap | Invoke-CimMethod -MethodName AddUserGroupNames -Arguments @{ UserGroupNames = $group } |
| 286 |
if ($return.ReturnValue -ne 0) { |
| 287 |
Fail-Json -obj $result -message "Failed to add user group $($group) (code: $($return.ReturnValue))" |
| 288 |
} |
| 289 |
} |
| 290 |
$user_groups_diff += " +$group`n" |
| 291 |
$result.changed = $true |
| 292 |
} |
| 293 |
|
| 294 |
foreach ($group in $groups_to_remove) { |
| 295 |
if (-not $check_mode) { |
| 296 |
$return = $wmi_rap | Invoke-CimMethod -MethodName RemoveUserGroupNames -Arguments @{ UserGroupNames = $group } |
| 297 |
if ($return.ReturnValue -ne 0) { |
| 298 |
Fail-Json -obj $result -message "Failed to remove user group $($group) (code: $($return.ReturnValue))" |
| 299 |
} |
| 300 |
} |
| 301 |
$user_groups_diff += " -$group`n" |
| 302 |
$result.changed = $true |
| 303 |
} |
| 304 |
|
| 305 |
if ($user_groups_diff) { |
| 306 |
$diff_text += "~UserGroups`n$user_groups_diff" |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
if ($diff_mode -and $result.changed -eq $true) { |
| 313 |
$result.diff = @{ |
| 314 |
prepared = $diff_text |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
Exit-Json $result |