| 1 |
# Copyright (c) 2018 Ansible Project |
| 2 |
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 3 |
|
| 4 |
#AnsibleRequires -CSharpUtil Ansible.Privilege |
| 5 |
|
| 6 |
Function Get-AnsiblePrivilege { |
| 7 |
<# |
| 8 |
.SYNOPSIS |
| 9 |
Get the status of a privilege for the current process. This returns |
| 10 |
$true - the privilege is enabled |
| 11 |
$false - the privilege is disabled |
| 12 |
$null - the privilege is removed from the token |
| 13 |
|
| 14 |
If Name is not a valid privilege name, this will throw an |
| 15 |
ArgumentException. |
| 16 |
|
| 17 |
.EXAMPLE |
| 18 |
Get-AnsiblePrivilege -Name SeDebugPrivilege |
| 19 |
#> |
| 20 |
[CmdletBinding()] |
| 21 |
param( |
| 22 |
[Parameter(Mandatory = $true)][String]$Name |
| 23 |
) |
| 24 |
|
| 25 |
if (-not [Ansible.Privilege.PrivilegeUtil]::CheckPrivilegeName($Name)) { |
| 26 |
throw [System.ArgumentException] "Invalid privilege name '$Name'" |
| 27 |
} |
| 28 |
|
| 29 |
$process_token = [Ansible.Privilege.PrivilegeUtil]::GetCurrentProcess() |
| 30 |
$privilege_info = [Ansible.Privilege.PrivilegeUtil]::GetAllPrivilegeInfo($process_token) |
| 31 |
if ($privilege_info.ContainsKey($Name)) { |
| 32 |
$status = $privilege_info.$Name |
| 33 |
return $status.HasFlag([Ansible.Privilege.PrivilegeAttributes]::Enabled) |
| 34 |
} |
| 35 |
else { |
| 36 |
return $null |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
Function Set-AnsiblePrivilege { |
| 41 |
<# |
| 42 |
.SYNOPSIS |
| 43 |
Enables/Disables a privilege on the current process' token. If a privilege |
| 44 |
has been removed from the process token, this will throw an |
| 45 |
InvalidOperationException. |
| 46 |
|
| 47 |
.EXAMPLE |
| 48 |
# enable a privilege |
| 49 |
Set-AnsiblePrivilege -Name SeCreateSymbolicLinkPrivilege -Value $true |
| 50 |
|
| 51 |
# disable a privilege |
| 52 |
Set-AnsiblePrivilege -Name SeCreateSymbolicLinkPrivilege -Value $false |
| 53 |
#> |
| 54 |
[CmdletBinding(SupportsShouldProcess)] |
| 55 |
param( |
| 56 |
[Parameter(Mandatory = $true)][String]$Name, |
| 57 |
[Parameter(Mandatory = $true)][bool]$Value |
| 58 |
) |
| 59 |
|
| 60 |
$action = switch ($Value) { |
| 61 |
$true { "Enable" } |
| 62 |
$false { "Disable" } |
| 63 |
} |
| 64 |
|
| 65 |
$current_state = Get-AnsiblePrivilege -Name $Name |
| 66 |
if ($current_state -eq $Value) { |
| 67 |
return # no change needs to occur |
| 68 |
} |
| 69 |
elseif ($null -eq $current_state) { |
| 70 |
# once a privilege is removed from a token we cannot do anything with it |
| 71 |
throw [System.InvalidOperationException] "Cannot $($action.ToLower()) the privilege '$Name' as it has been removed from the token" |
| 72 |
} |
| 73 |
|
| 74 |
$process_token = [Ansible.Privilege.PrivilegeUtil]::GetCurrentProcess() |
| 75 |
if ($PSCmdlet.ShouldProcess($Name, "$action the privilege $Name")) { |
| 76 |
$new_state = New-Object -TypeName 'System.Collections.Generic.Dictionary`2[[System.String], [System.Nullable`1[System.Boolean]]]' |
| 77 |
$new_state.Add($Name, $Value) |
| 78 |
[Ansible.Privilege.PrivilegeUtil]::SetTokenPrivileges($process_token, $new_state) > $null |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
Export-ModuleMember -Function Get-AnsiblePrivilege, Set-AnsiblePrivilege |
| 83 |
|