| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2017, Ansible Project |
| 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 |
$params = Parse-Args $args -supports_check_mode $true |
| 12 |
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false |
| 13 |
$diff_mode = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -default $false |
| 14 |
$_remote_tmp = Get-AnsibleParam $params "_ansible_remote_tmp" -type "path" -default $env:TMP |
| 15 |
|
| 16 |
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true |
| 17 |
$users = Get-AnsibleParam -obj $params -name "users" -type "list" -failifempty $true |
| 18 |
$action = Get-AnsibleParam -obj $params -name "action" -type "str" -default "set" -validateset "add","remove","set" |
| 19 |
|
| 20 |
$result = @{ |
| 21 |
changed = $false |
| 22 |
added = @() |
| 23 |
removed = @() |
| 24 |
} |
| 25 |
|
| 26 |
if ($diff_mode) { |
| 27 |
$result.diff = @{} |
| 28 |
} |
| 29 |
|
| 30 |
$sec_helper_util = @" |
| 31 |
using System; |
| 32 |
using System.ComponentModel; |
| 33 |
using System.Runtime.InteropServices; |
| 34 |
using System.Security.Principal; |
| 35 |
|
| 36 |
namespace Ansible |
| 37 |
{ |
| 38 |
public class LsaRightHelper : IDisposable |
| 39 |
{ |
| 40 |
// Code modified from https://gallery.technet.microsoft.com/scriptcenter/Grant-Revoke-Query-user-26e259b0 |
| 41 |
|
| 42 |
enum Access : int |
| 43 |
{ |
| 44 |
POLICY_READ = 0x20006, |
| 45 |
POLICY_ALL_ACCESS = 0x00F0FFF, |
| 46 |
POLICY_EXECUTE = 0X20801, |
| 47 |
POLICY_WRITE = 0X207F8 |
| 48 |
} |
| 49 |
|
| 50 |
IntPtr lsaHandle; |
| 51 |
|
| 52 |
const string LSA_DLL = "advapi32.dll"; |
| 53 |
const CharSet DEFAULT_CHAR_SET = CharSet.Unicode; |
| 54 |
|
| 55 |
const uint STATUS_NO_MORE_ENTRIES = 0x8000001a; |
| 56 |
const uint STATUS_NO_SUCH_PRIVILEGE = 0xc0000060; |
| 57 |
|
| 58 |
internal sealed class Sid : IDisposable |
| 59 |
{ |
| 60 |
public IntPtr pSid = IntPtr.Zero; |
| 61 |
public SecurityIdentifier sid = null; |
| 62 |
|
| 63 |
public Sid(string sidString) |
| 64 |
{ |
| 65 |
try |
| 66 |
{ |
| 67 |
sid = new SecurityIdentifier(sidString); |
| 68 |
} catch |
| 69 |
{ |
| 70 |
throw new ArgumentException(String.Format("SID string {0} could not be converted to SecurityIdentifier", sidString)); |
| 71 |
} |
| 72 |
|
| 73 |
Byte[] buffer = new Byte[sid.BinaryLength]; |
| 74 |
sid.GetBinaryForm(buffer, 0); |
| 75 |
|
| 76 |
pSid = Marshal.AllocHGlobal(sid.BinaryLength); |
| 77 |
Marshal.Copy(buffer, 0, pSid, sid.BinaryLength); |
| 78 |
} |
| 79 |
|
| 80 |
public void Dispose() |
| 81 |
{ |
| 82 |
if (pSid != IntPtr.Zero) |
| 83 |
{ |
| 84 |
Marshal.FreeHGlobal(pSid); |
| 85 |
pSid = IntPtr.Zero; |
| 86 |
} |
| 87 |
GC.SuppressFinalize(this); |
| 88 |
} |
| 89 |
~Sid() { Dispose(); } |
| 90 |
} |
| 91 |
|
| 92 |
[StructLayout(LayoutKind.Sequential)] |
| 93 |
private struct LSA_OBJECT_ATTRIBUTES |
| 94 |
{ |
| 95 |
public int Length; |
| 96 |
public IntPtr RootDirectory; |
| 97 |
public IntPtr ObjectName; |
| 98 |
public int Attributes; |
| 99 |
public IntPtr SecurityDescriptor; |
| 100 |
public IntPtr SecurityQualityOfService; |
| 101 |
} |
| 102 |
|
| 103 |
[StructLayout(LayoutKind.Sequential, CharSet = DEFAULT_CHAR_SET)] |
| 104 |
private struct LSA_UNICODE_STRING |
| 105 |
{ |
| 106 |
public ushort Length; |
| 107 |
public ushort MaximumLength; |
| 108 |
[MarshalAs(UnmanagedType.LPWStr)] |
| 109 |
public string Buffer; |
| 110 |
} |
| 111 |
|
| 112 |
[StructLayout(LayoutKind.Sequential)] |
| 113 |
private struct LSA_ENUMERATION_INFORMATION |
| 114 |
{ |
| 115 |
public IntPtr Sid; |
| 116 |
} |
| 117 |
|
| 118 |
[DllImport(LSA_DLL, CharSet = DEFAULT_CHAR_SET, SetLastError = true)] |
| 119 |
private static extern uint LsaOpenPolicy( |
| 120 |
LSA_UNICODE_STRING[] SystemName, |
| 121 |
ref LSA_OBJECT_ATTRIBUTES ObjectAttributes, |
| 122 |
int AccessMask, |
| 123 |
out IntPtr PolicyHandle |
| 124 |
); |
| 125 |
|
| 126 |
[DllImport(LSA_DLL, CharSet = DEFAULT_CHAR_SET, SetLastError = true)] |
| 127 |
private static extern uint LsaAddAccountRights( |
| 128 |
IntPtr PolicyHandle, |
| 129 |
IntPtr pSID, |
| 130 |
LSA_UNICODE_STRING[] UserRights, |
| 131 |
int CountOfRights |
| 132 |
); |
| 133 |
|
| 134 |
[DllImport(LSA_DLL, CharSet = DEFAULT_CHAR_SET, SetLastError = true)] |
| 135 |
private static extern uint LsaRemoveAccountRights( |
| 136 |
IntPtr PolicyHandle, |
| 137 |
IntPtr pSID, |
| 138 |
bool AllRights, |
| 139 |
LSA_UNICODE_STRING[] UserRights, |
| 140 |
int CountOfRights |
| 141 |
); |
| 142 |
|
| 143 |
[DllImport(LSA_DLL, CharSet = DEFAULT_CHAR_SET, SetLastError = true)] |
| 144 |
private static extern uint LsaEnumerateAccountsWithUserRight( |
| 145 |
IntPtr PolicyHandle, |
| 146 |
LSA_UNICODE_STRING[] UserRights, |
| 147 |
out IntPtr EnumerationBuffer, |
| 148 |
out ulong CountReturned |
| 149 |
); |
| 150 |
|
| 151 |
[DllImport(LSA_DLL)] |
| 152 |
private static extern int LsaNtStatusToWinError(int NTSTATUS); |
| 153 |
|
| 154 |
[DllImport(LSA_DLL)] |
| 155 |
private static extern int LsaClose(IntPtr PolicyHandle); |
| 156 |
|
| 157 |
[DllImport(LSA_DLL)] |
| 158 |
private static extern int LsaFreeMemory(IntPtr Buffer); |
| 159 |
|
| 160 |
public LsaRightHelper() |
| 161 |
{ |
| 162 |
LSA_OBJECT_ATTRIBUTES lsaAttr; |
| 163 |
lsaAttr.RootDirectory = IntPtr.Zero; |
| 164 |
lsaAttr.ObjectName = IntPtr.Zero; |
| 165 |
lsaAttr.Attributes = 0; |
| 166 |
lsaAttr.SecurityDescriptor = IntPtr.Zero; |
| 167 |
lsaAttr.SecurityQualityOfService = IntPtr.Zero; |
| 168 |
lsaAttr.Length = Marshal.SizeOf(typeof(LSA_OBJECT_ATTRIBUTES)); |
| 169 |
|
| 170 |
lsaHandle = IntPtr.Zero; |
| 171 |
|
| 172 |
LSA_UNICODE_STRING[] system = new LSA_UNICODE_STRING[1]; |
| 173 |
system[0] = InitLsaString(""); |
| 174 |
|
| 175 |
uint ret = LsaOpenPolicy(system, ref lsaAttr, (int)Access.POLICY_ALL_ACCESS, out lsaHandle); |
| 176 |
if (ret != 0) |
| 177 |
throw new Win32Exception(LsaNtStatusToWinError((int)ret)); |
| 178 |
} |
| 179 |
|
| 180 |
public void AddPrivilege(string sidString, string privilege) |
| 181 |
{ |
| 182 |
uint ret = 0; |
| 183 |
using (Sid sid = new Sid(sidString)) |
| 184 |
{ |
| 185 |
LSA_UNICODE_STRING[] privileges = new LSA_UNICODE_STRING[1]; |
| 186 |
privileges[0] = InitLsaString(privilege); |
| 187 |
ret = LsaAddAccountRights(lsaHandle, sid.pSid, privileges, 1); |
| 188 |
} |
| 189 |
if (ret != 0) |
| 190 |
throw new Win32Exception(LsaNtStatusToWinError((int)ret)); |
| 191 |
} |
| 192 |
|
| 193 |
public void RemovePrivilege(string sidString, string privilege) |
| 194 |
{ |
| 195 |
uint ret = 0; |
| 196 |
using (Sid sid = new Sid(sidString)) |
| 197 |
{ |
| 198 |
LSA_UNICODE_STRING[] privileges = new LSA_UNICODE_STRING[1]; |
| 199 |
privileges[0] = InitLsaString(privilege); |
| 200 |
ret = LsaRemoveAccountRights(lsaHandle, sid.pSid, false, privileges, 1); |
| 201 |
} |
| 202 |
if (ret != 0) |
| 203 |
throw new Win32Exception(LsaNtStatusToWinError((int)ret)); |
| 204 |
} |
| 205 |
|
| 206 |
public string[] EnumerateAccountsWithUserRight(string privilege) |
| 207 |
{ |
| 208 |
uint ret = 0; |
| 209 |
ulong count = 0; |
| 210 |
LSA_UNICODE_STRING[] rights = new LSA_UNICODE_STRING[1]; |
| 211 |
rights[0] = InitLsaString(privilege); |
| 212 |
IntPtr buffer = IntPtr.Zero; |
| 213 |
|
| 214 |
ret = LsaEnumerateAccountsWithUserRight(lsaHandle, rights, out buffer, out count); |
| 215 |
switch (ret) |
| 216 |
{ |
| 217 |
case 0: |
| 218 |
string[] accounts = new string[count]; |
| 219 |
for (int i = 0; i < (int)count; i++) |
| 220 |
{ |
| 221 |
LSA_ENUMERATION_INFORMATION LsaInfo = (LSA_ENUMERATION_INFORMATION)Marshal.PtrToStructure( |
| 222 |
IntPtr.Add(buffer, i * Marshal.SizeOf(typeof(LSA_ENUMERATION_INFORMATION))), |
| 223 |
typeof(LSA_ENUMERATION_INFORMATION)); |
| 224 |
|
| 225 |
accounts[i] = new SecurityIdentifier(LsaInfo.Sid).ToString(); |
| 226 |
} |
| 227 |
LsaFreeMemory(buffer); |
| 228 |
return accounts; |
| 229 |
|
| 230 |
case STATUS_NO_MORE_ENTRIES: |
| 231 |
return new string[0]; |
| 232 |
|
| 233 |
case STATUS_NO_SUCH_PRIVILEGE: |
| 234 |
throw new ArgumentException(String.Format("Invalid privilege {0} not found in LSA database", privilege)); |
| 235 |
|
| 236 |
default: |
| 237 |
throw new Win32Exception(LsaNtStatusToWinError((int)ret)); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
static LSA_UNICODE_STRING InitLsaString(string s) |
| 242 |
{ |
| 243 |
// Unicode strings max. 32KB |
| 244 |
if (s.Length > 0x7ffe) |
| 245 |
throw new ArgumentException("String too long"); |
| 246 |
|
| 247 |
LSA_UNICODE_STRING lus = new LSA_UNICODE_STRING(); |
| 248 |
lus.Buffer = s; |
| 249 |
lus.Length = (ushort)(s.Length * sizeof(char)); |
| 250 |
lus.MaximumLength = (ushort)(lus.Length + sizeof(char)); |
| 251 |
|
| 252 |
return lus; |
| 253 |
} |
| 254 |
|
| 255 |
public void Dispose() |
| 256 |
{ |
| 257 |
if (lsaHandle != IntPtr.Zero) |
| 258 |
{ |
| 259 |
LsaClose(lsaHandle); |
| 260 |
lsaHandle = IntPtr.Zero; |
| 261 |
} |
| 262 |
GC.SuppressFinalize(this); |
| 263 |
} |
| 264 |
~LsaRightHelper() { Dispose(); } |
| 265 |
} |
| 266 |
} |
| 267 |
"@ |
| 268 |
|
| 269 |
$original_tmp = $env:TMP |
| 270 |
$env:TMP = $_remote_tmp |
| 271 |
Add-Type -TypeDefinition $sec_helper_util |
| 272 |
$env:TMP = $original_tmp |
| 273 |
|
| 274 |
Function Compare-UserList($existing_users, $new_users) { |
| 275 |
$added_users = [String[]]@() |
| 276 |
$removed_users = [String[]]@() |
| 277 |
if ($action -eq "add") { |
| 278 |
$added_users = [Linq.Enumerable]::Except($new_users, $existing_users) |
| 279 |
} elseif ($action -eq "remove") { |
| 280 |
$removed_users = [Linq.Enumerable]::Intersect($new_users, $existing_users) |
| 281 |
} else { |
| 282 |
$added_users = [Linq.Enumerable]::Except($new_users, $existing_users) |
| 283 |
$removed_users = [Linq.Enumerable]::Except($existing_users, $new_users) |
| 284 |
} |
| 285 |
|
| 286 |
$change_result = @{ |
| 287 |
added = $added_users |
| 288 |
removed = $removed_users |
| 289 |
} |
| 290 |
|
| 291 |
return $change_result |
| 292 |
} |
| 293 |
|
| 294 |
# C# class we can use to enumerate/add/remove rights |
| 295 |
$lsa_helper = New-Object -TypeName Ansible.LsaRightHelper |
| 296 |
|
| 297 |
$new_users = [System.Collections.ArrayList]@() |
| 298 |
foreach ($user in $users) { |
| 299 |
$user_sid = Convert-ToSID -account_name $user |
| 300 |
$new_users.Add($user_sid) > $null |
| 301 |
} |
| 302 |
$new_users = [String[]]$new_users.ToArray() |
| 303 |
try { |
| 304 |
$existing_users = $lsa_helper.EnumerateAccountsWithUserRight($name) |
| 305 |
} catch [ArgumentException] { |
| 306 |
Fail-Json -obj $result -message "the specified right $name is not a valid right" |
| 307 |
} catch { |
| 308 |
Fail-Json -obj $result -message "failed to enumerate existing accounts with right: $($_.Exception.Message)" |
| 309 |
} |
| 310 |
|
| 311 |
$change_result = Compare-UserList -existing_users $existing_users -new_user $new_users |
| 312 |
if (($change_result.added.Length -gt 0) -or ($change_result.removed.Length -gt 0)) { |
| 313 |
$result.changed = $true |
| 314 |
$diff_text = "[$name]`n" |
| 315 |
|
| 316 |
# used in diff mode calculation |
| 317 |
$new_user_list = [System.Collections.ArrayList]$existing_users |
| 318 |
foreach ($user in $change_result.removed) { |
| 319 |
if (-not $check_mode) { |
| 320 |
$lsa_helper.RemovePrivilege($user, $name) |
| 321 |
} |
| 322 |
$user_name = Convert-FromSID -sid $user |
| 323 |
$result.removed += $user_name |
| 324 |
$diff_text += "-$user_name`n" |
| 325 |
$new_user_list.Remove($user) > $null |
| 326 |
} |
| 327 |
foreach ($user in $change_result.added) { |
| 328 |
if (-not $check_mode) { |
| 329 |
$lsa_helper.AddPrivilege($user, $name) |
| 330 |
} |
| 331 |
$user_name = Convert-FromSID -sid $user |
| 332 |
$result.added += $user_name |
| 333 |
$diff_text += "+$user_name`n" |
| 334 |
$new_user_list.Add($user) > $null |
| 335 |
} |
| 336 |
|
| 337 |
if ($diff_mode) { |
| 338 |
if ($new_user_list.Count -eq 0) { |
| 339 |
$diff_text = "-$diff_text" |
| 340 |
} else { |
| 341 |
if ($existing_users.Count -eq 0) { |
| 342 |
$diff_text = "+$diff_text" |
| 343 |
} |
| 344 |
} |
| 345 |
$result.diff.prepared = $diff_text |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
Exit-Json $result |