updates user search verification for local/domain

- Thanks to @trondhindenes for implementing this strategy
- also updated documentation
reviewable/pr18780/r1
Phil 9 years ago
parent dc84f05fed
commit 05e01bd3b5

@ -15,21 +15,82 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# WANT_JSON # WANT_JSON
# POWERSHELL_COMMON # POWERSHELL_COMMON
# win_acl module (File/Resources Permission Additions/Removal) # win_acl module (File/Resources Permission Additions/Removal)
#Functions
Function UserSearch
{
Param ([string]$AccountName)
#Check if there's a realm specified
if ($AccountName.Split("\").count -gt 1)
{
if ($AccountName.Split("\")[0] -eq $env:COMPUTERNAME)
{
$IsLocalAccount = $true
}
Else
{
$IsDomainAccount = $true
$IsUpn = $false
}
}
Elseif ($AccountName -contains "@")
{
$IsDomainAccount = $true
$IsUpn = $true
}
Else
{
#Default to local user account
$accountname = $env:COMPUTERNAME + "\" + $AccountName
$IsLocalAccount = $true
}
if ($IsLocalAccount -eq $true)
{
$localaccount = get-wmiobject -class "Win32_UserAccount" -namespace "root\CIMV2" -filter "(LocalAccount = True)" | where {$_.Caption -eq $AccountName}
if ($localaccount)
{
return $localaccount.Caption
}
$LocalGroup = get-wmiobject -class "Win32_Group" -namespace "root\CIMV2" -filter "LocalAccount = True"| where {$_.Caption -eq $AccountName}
if ($LocalGroup)
{
return $LocalGroup.Caption
}
}
ElseIf (($IsDomainAccount -eq $true) -and ($IsUpn -eq $false))
{
#Search by samaccountname
$Searcher = [adsisearcher]""
$Searcher.Filter = "sAMAccountName=$($accountname.split("\")[1])"
$result = $Searcher.FindOne()
if ($result)
{
return $accountname
}
}
}
$params = Parse-Args $args; $params = Parse-Args $args;
$result = New-Object psobject @{ $result = New-Object psobject @{
win_acl = New-Object psobject win_acl = New-Object psobject
changed = $false changed = $false
} }
If ($params.src) { If ($params.src) {
$src = $params.src.toString() $src = $params.src.toString()
If (-Not (Test-Path -Path $src)) { If (-Not (Test-Path -Path $src)) {
Fail-Json $result "$src file or directory does not exist on the host" Fail-Json $result "$src file or directory does not exist on the host"
} }
@ -37,21 +98,20 @@ If ($params.src) {
Else { Else {
Fail-Json $result "missing required argument: src" Fail-Json $result "missing required argument: src"
} }
If ($params.user) { If ($params.user) {
$user = $params.user.toString() $user = UserSearch -AccountName ($Params.User)
# Test that the user/group exists on the local machine # Test that the user/group is resolvable on the local machine
$localComputer = [ADSI]("WinNT://"+[System.Net.Dns]::GetHostName()) if (!$user)
$list = ($localComputer.psbase.children | Where-Object { (($_.psBase.schemaClassName -eq "User") -Or ($_.psBase.schemaClassName -eq "Group"))} | Select-Object -expand Name) {
If (-Not ($list -contains "$user")) { Fail-Json $result "$($Params.User) is not a valid user or group on the host machine or domain"
Fail-Json $result "$user is not a valid user or group on the host machine" }
}
} }
Else { Else {
Fail-Json $result "missing required argument: user. specify the user or group to apply permission changes." Fail-Json $result "missing required argument: user. specify the user or group to apply permission changes."
} }
If ($params.type -eq "allow") { If ($params.type -eq "allow") {
$type = $true $type = $true
} }
@ -61,7 +121,7 @@ ElseIf ($params.type -eq "deny") {
Else { Else {
Fail-Json $result "missing required argument: type. specify whether to allow or deny the specified rights." Fail-Json $result "missing required argument: type. specify whether to allow or deny the specified rights."
} }
If ($params.inherit) { If ($params.inherit) {
# If it's a file then no flags can be set or an exception will be thrown # If it's a file then no flags can be set or an exception will be thrown
If (Test-Path -Path $src -PathType Leaf) { If (Test-Path -Path $src -PathType Leaf) {
@ -80,44 +140,44 @@ Else {
$inherit = "ContainerInherit, ObjectInherit" $inherit = "ContainerInherit, ObjectInherit"
} }
} }
If ($params.propagation) { If ($params.propagation) {
$propagation = $params.propagation.toString() $propagation = $params.propagation.toString()
} }
Else { Else {
$propagation = "None" $propagation = "None"
} }
If ($params.rights) { If ($params.rights) {
$rights = $params.rights.toString() $rights = $params.rights.toString()
} }
Else { Else {
Fail-Json $result "missing required argument: rights" Fail-Json $result "missing required argument: rights"
} }
If ($params.state -eq "absent") { If ($params.state -eq "absent") {
$state = "remove" $state = "remove"
} }
Else { Else {
$state = "add" $state = "add"
} }
Try { Try {
$colRights = [System.Security.AccessControl.FileSystemRights]$rights $colRights = [System.Security.AccessControl.FileSystemRights]$rights
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]$inherit $InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]$inherit
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]$propagation $PropagationFlag = [System.Security.AccessControl.PropagationFlags]$propagation
If ($type) { If ($type) {
$objType =[System.Security.AccessControl.AccessControlType]::Allow $objType =[System.Security.AccessControl.AccessControlType]::Allow
} }
Else { Else {
$objType =[System.Security.AccessControl.AccessControlType]::Deny $objType =[System.Security.AccessControl.AccessControlType]::Deny
} }
$objUser = New-Object System.Security.Principal.NTAccount($user) $objUser = New-Object System.Security.Principal.NTAccount($user)
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType) $objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType)
$objACL = Get-ACL $src $objACL = Get-ACL $src
# Check if the ACE exists already in the objects ACL list # Check if the ACE exists already in the objects ACL list
$match = $false $match = $false
ForEach($rule in $objACL.Access){ ForEach($rule in $objACL.Access){
@ -126,7 +186,7 @@ Try {
Break Break
} }
} }
If ($state -eq "add" -And $match -eq $false) { If ($state -eq "add" -And $match -eq $false) {
Try { Try {
$objACL.AddAccessRule($objACE) $objACL.AddAccessRule($objACE)
@ -161,5 +221,5 @@ Try {
Catch { Catch {
Fail-Json $result "an error occured when attempting to $state $rights permission(s) on $src for $user" Fail-Json $result "an error occured when attempting to $state $rights permission(s) on $src for $user"
} }
Exit-Json $result Exit-Json $result

@ -24,7 +24,7 @@
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
module: win_acl module: win_acl
version_added: "" version_added: "2.0"
short_description: Set file/directory permissions for a system user or group. short_description: Set file/directory permissions for a system user or group.
description: description:
- Add or remove rights/permissions for a given user or group for the specified src file or folder. - Add or remove rights/permissions for a given user or group for the specified src file or folder.
@ -107,7 +107,7 @@ options:
- InheritOnly - InheritOnly
default: "None" default: "None"
aliases: [] aliases: []
author: Phil Schwartz author: Phil Schwartz, Trond Hindenes
''' '''
EXAMPLES = ''' EXAMPLES = '''

Loading…
Cancel
Save