mirror of https://github.com/ansible/ansible.git
Merge pull request #200 from schwartzmx/module/win_acl
win_acl Module talked to @cchurch, merging this for now, need to open ticket to address the 2008 issuereviewable/pr18780/r1
commit
05f53f2b95
@ -0,0 +1,225 @@
|
|||||||
|
#!powershell
|
||||||
|
# This file is part of Ansible
|
||||||
|
#
|
||||||
|
# Copyright 2015, Phil Schwartz <schwartzmx@gmail.com>
|
||||||
|
#
|
||||||
|
# Ansible is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# Ansible is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# WANT_JSON
|
||||||
|
# POWERSHELL_COMMON
|
||||||
|
|
||||||
|
# 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;
|
||||||
|
|
||||||
|
$result = New-Object psobject @{
|
||||||
|
win_acl = New-Object psobject
|
||||||
|
changed = $false
|
||||||
|
}
|
||||||
|
|
||||||
|
If ($params.path) {
|
||||||
|
$path = $params.path.toString()
|
||||||
|
|
||||||
|
If (-Not (Test-Path -Path $path)) {
|
||||||
|
Fail-Json $result "$path file or directory does not exist on the host"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Else {
|
||||||
|
Fail-Json $result "missing required argument: path"
|
||||||
|
}
|
||||||
|
|
||||||
|
If ($params.user) {
|
||||||
|
$user = UserSearch -AccountName ($Params.User)
|
||||||
|
|
||||||
|
# Test that the user/group is resolvable on the local machine
|
||||||
|
if (!$user)
|
||||||
|
{
|
||||||
|
Fail-Json $result "$($Params.User) is not a valid user or group on the host machine or domain"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Else {
|
||||||
|
Fail-Json $result "missing required argument: user. specify the user or group to apply permission changes."
|
||||||
|
}
|
||||||
|
|
||||||
|
If ($params.type -eq "allow") {
|
||||||
|
$type = $true
|
||||||
|
}
|
||||||
|
ElseIf ($params.type -eq "deny") {
|
||||||
|
$type = $false
|
||||||
|
}
|
||||||
|
Else {
|
||||||
|
Fail-Json $result "missing required argument: type. specify whether to allow or deny the specified rights."
|
||||||
|
}
|
||||||
|
|
||||||
|
If ($params.inherit) {
|
||||||
|
# If it's a file then no flags can be set or an exception will be thrown
|
||||||
|
If (Test-Path -Path $path -PathType Leaf) {
|
||||||
|
$inherit = "None"
|
||||||
|
}
|
||||||
|
Else {
|
||||||
|
$inherit = $params.inherit.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Else {
|
||||||
|
# If it's a file then no flags can be set or an exception will be thrown
|
||||||
|
If (Test-Path -Path $path -PathType Leaf) {
|
||||||
|
$inherit = "None"
|
||||||
|
}
|
||||||
|
Else {
|
||||||
|
$inherit = "ContainerInherit, ObjectInherit"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
If ($params.propagation) {
|
||||||
|
$propagation = $params.propagation.toString()
|
||||||
|
}
|
||||||
|
Else {
|
||||||
|
$propagation = "None"
|
||||||
|
}
|
||||||
|
|
||||||
|
If ($params.rights) {
|
||||||
|
$rights = $params.rights.toString()
|
||||||
|
}
|
||||||
|
Else {
|
||||||
|
Fail-Json $result "missing required argument: rights"
|
||||||
|
}
|
||||||
|
|
||||||
|
If ($params.state -eq "absent") {
|
||||||
|
$state = "remove"
|
||||||
|
}
|
||||||
|
Else {
|
||||||
|
$state = "add"
|
||||||
|
}
|
||||||
|
|
||||||
|
Try {
|
||||||
|
$colRights = [System.Security.AccessControl.FileSystemRights]$rights
|
||||||
|
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]$inherit
|
||||||
|
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]$propagation
|
||||||
|
|
||||||
|
If ($type) {
|
||||||
|
$objType =[System.Security.AccessControl.AccessControlType]::Allow
|
||||||
|
}
|
||||||
|
Else {
|
||||||
|
$objType =[System.Security.AccessControl.AccessControlType]::Deny
|
||||||
|
}
|
||||||
|
|
||||||
|
$objUser = New-Object System.Security.Principal.NTAccount($user)
|
||||||
|
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType)
|
||||||
|
$objACL = Get-ACL $path
|
||||||
|
|
||||||
|
# Check if the ACE exists already in the objects ACL list
|
||||||
|
$match = $false
|
||||||
|
ForEach($rule in $objACL.Access){
|
||||||
|
If (($rule.FileSystemRights -eq $objACE.FileSystemRights) -And ($rule.AccessControlType -eq $objACE.AccessControlType) -And ($rule.IdentityReference -eq $objACE.IdentityReference) -And ($rule.IsInherited -eq $objACE.IsInherited) -And ($rule.InheritanceFlags -eq $objACE.InheritanceFlags) -And ($rule.PropagationFlags -eq $objACE.PropagationFlags)) {
|
||||||
|
$match = $true
|
||||||
|
Break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
If ($state -eq "add" -And $match -eq $false) {
|
||||||
|
Try {
|
||||||
|
$objACL.AddAccessRule($objACE)
|
||||||
|
Set-ACL $path $objACL
|
||||||
|
$result.changed = $true
|
||||||
|
}
|
||||||
|
Catch {
|
||||||
|
Fail-Json $result "an exception occured when adding the specified rule"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ElseIf ($state -eq "remove" -And $match -eq $true) {
|
||||||
|
Try {
|
||||||
|
$objACL.RemoveAccessRule($objACE)
|
||||||
|
Set-ACL $path $objACL
|
||||||
|
$result.changed = $true
|
||||||
|
}
|
||||||
|
Catch {
|
||||||
|
Fail-Json $result "an exception occured when removing the specified rule"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Else {
|
||||||
|
# A rule was attempting to be added but already exists
|
||||||
|
If ($match -eq $true) {
|
||||||
|
Exit-Json $result "the specified rule already exists"
|
||||||
|
}
|
||||||
|
# A rule didn't exist that was trying to be removed
|
||||||
|
Else {
|
||||||
|
Exit-Json $result "the specified rule does not exist"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Catch {
|
||||||
|
Fail-Json $result "an error occured when attempting to $state $rights permission(s) on $path for $user"
|
||||||
|
}
|
||||||
|
|
||||||
|
Exit-Json $result
|
@ -0,0 +1,139 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# (c) 2015, Phil Schwartz <schwartzmx@gmail.com>
|
||||||
|
#
|
||||||
|
# This file is part of Ansible
|
||||||
|
#
|
||||||
|
# Ansible is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# Ansible is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# this is a windows documentation stub. actual code lives in the .ps1
|
||||||
|
# file of the same name
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
module: win_acl
|
||||||
|
version_added: "2.0"
|
||||||
|
short_description: Set file/directory permissions for a system user or group.
|
||||||
|
description:
|
||||||
|
- Add or remove rights/permissions for a given user or group for the specified src file or folder.
|
||||||
|
options:
|
||||||
|
path:
|
||||||
|
description:
|
||||||
|
- File or Directory
|
||||||
|
required: yes
|
||||||
|
user:
|
||||||
|
description:
|
||||||
|
- User or Group to add specified rights to act on src file/folder
|
||||||
|
required: yes
|
||||||
|
default: none
|
||||||
|
state:
|
||||||
|
description:
|
||||||
|
- Specify whether to add (present) or remove (absent) the specified access rule
|
||||||
|
required: no
|
||||||
|
choices:
|
||||||
|
- present
|
||||||
|
- absent
|
||||||
|
default: present
|
||||||
|
type:
|
||||||
|
description:
|
||||||
|
- Specify whether to allow or deny the rights specified
|
||||||
|
required: yes
|
||||||
|
choices:
|
||||||
|
- allow
|
||||||
|
- deny
|
||||||
|
default: none
|
||||||
|
rights:
|
||||||
|
description:
|
||||||
|
- The rights/permissions that are to be allowed/denyed for the specified user or group for the given src file or directory. Can be entered as a comma separated list (Ex. "Modify, Delete, ExecuteFile"). For more information on the choices see MSDN FileSystemRights Enumeration.
|
||||||
|
required: yes
|
||||||
|
choices:
|
||||||
|
- AppendData
|
||||||
|
- ChangePermissions
|
||||||
|
- Delete
|
||||||
|
- DeleteSubdirectoriesAndFiles
|
||||||
|
- ExecuteFile
|
||||||
|
- FullControl
|
||||||
|
- ListDirectory
|
||||||
|
- Modify
|
||||||
|
- Read
|
||||||
|
- ReadAndExecute
|
||||||
|
- ReadAttributes
|
||||||
|
- ReadData
|
||||||
|
- ReadExtendedAttributes
|
||||||
|
- ReadPermissions
|
||||||
|
- Synchronize
|
||||||
|
- TakeOwnership
|
||||||
|
- Traverse
|
||||||
|
- Write
|
||||||
|
- WriteAttributes
|
||||||
|
- WriteData
|
||||||
|
- WriteExtendedAttributes
|
||||||
|
default: none
|
||||||
|
inherit:
|
||||||
|
description:
|
||||||
|
- Inherit flags on the ACL rules. Can be specified as a comma separated list (Ex. "ContainerInherit, ObjectInherit"). For more information on the choices see MSDN InheritanceFlags Enumeration.
|
||||||
|
required: no
|
||||||
|
choices:
|
||||||
|
- ContainerInherit
|
||||||
|
- ObjectInherit
|
||||||
|
- None
|
||||||
|
default: For Leaf File: None; For Directory: ContainerInherit, ObjectInherit;
|
||||||
|
propagation:
|
||||||
|
description:
|
||||||
|
- Propagation flag on the ACL rules. For more information on the choices see MSDN PropagationFlags Enumeration.
|
||||||
|
required: no
|
||||||
|
choices:
|
||||||
|
- None
|
||||||
|
- NoPropagateInherit
|
||||||
|
- InheritOnly
|
||||||
|
default: "None"
|
||||||
|
author: Phil Schwartz (@schwartzmx), Trond Hindenes (@trondhindenes)
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
# Restrict write,execute access to User Fed-Phil
|
||||||
|
$ ansible -i hosts -m win_acl -a "user=Fed-Phil path=C:\Important\Executable.exe type=deny rights='ExecuteFile,Write'" all
|
||||||
|
|
||||||
|
# Playbook example
|
||||||
|
# Add access rule to allow IIS_IUSRS FullControl to MySite
|
||||||
|
---
|
||||||
|
- name: Add IIS_IUSRS allow rights
|
||||||
|
win_acl:
|
||||||
|
path: 'C:\inetpub\wwwroot\MySite'
|
||||||
|
user: 'IIS_IUSRS'
|
||||||
|
rights: 'FullControl'
|
||||||
|
type: 'allow'
|
||||||
|
state: 'present'
|
||||||
|
inherit: 'ContainerInherit, ObjectInherit'
|
||||||
|
propagation: 'None'
|
||||||
|
|
||||||
|
# Remove previously added rule for IIS_IUSRS
|
||||||
|
- name: Remove FullControl AccessRule for IIS_IUSRS
|
||||||
|
path: 'C:\inetpub\wwwroot\MySite'
|
||||||
|
user: 'IIS_IUSRS'
|
||||||
|
rights: 'FullControl'
|
||||||
|
type: 'allow'
|
||||||
|
state: 'absent'
|
||||||
|
inherit: 'ContainerInherit, ObjectInherit'
|
||||||
|
propagation: 'None'
|
||||||
|
|
||||||
|
# Deny Intern
|
||||||
|
- name: Deny Deny
|
||||||
|
path: 'C:\Administrator\Documents'
|
||||||
|
user: 'Intern'
|
||||||
|
rights: 'Read,Write,Modify,FullControl,Delete'
|
||||||
|
type: 'deny'
|
||||||
|
state: 'present'
|
||||||
|
'''
|
Loading…
Reference in New Issue