
    Vh5                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ d dlmZ d Zdd	Zdd
ZddZddZd Zedk(  r e        yy)    )absolute_importdivisionprint_functionu  
---
module: acl
short_description: Set and retrieve file ACL information.
description:
- Set and retrieve file ACL information.
version_added: "1.0.0"
options:
  path:
    description:
    - The full path of the file or object.
    type: path
    required: true
    aliases: [ name ]
  state:
    description:
    - Define whether the ACL should be present or not.
    - The V(query) state gets the current ACL without changing it, for use in C(register) operations.
    choices: [ absent, present, query ]
    default: query
    type: str
  follow:
    description:
    - Whether to follow symlinks on the path if a symlink is encountered.
    type: bool
    default: true
  default:
    description:
    - If O(path) is a directory, setting this to V(true) will make it the default ACL for entities created inside the directory.
    - Setting O(default=true) causes an error if O(path) is a file.
    type: bool
    default: false
  entity:
    description:
    - The actual user or group that the ACL applies to when matching entity types user or group are selected.
    type: str
    default: ""
  etype:
    description:
    - The entity type of the ACL to apply, see C(setfacl) documentation for more info.
    choices: [ group, mask, other, user ]
    type: str
  permissions:
    description:
    - The permissions to apply/remove can be any combination of C(r), C(w), C(x)
      (read, write and execute respectively), and C(X) (execute permission if the file is a directory or already has execute permission for some user)
    type: str
  entry:
    description:
    - DEPRECATED.
    - The ACL to set or remove.
    - This must always be quoted in the form of C(<etype>:<qualifier>:<perms>).
    - The qualifier may be empty for some types, but the type and perms are always required.
    - C(-) can be used as placeholder when you do not care about permissions.
    - This is now superseded by entity, type and permissions fields.
    type: str
  recursive:
    description:
    - Recursively sets the specified ACL.
    - Incompatible with O(state=query).
    - Alias O(recurse) added in version 1.3.0.
    type: bool
    default: false
    aliases: [ recurse ]
  use_nfsv4_acls:
    description:
    - Use NFSv4 ACLs instead of POSIX ACLs.
    - This feature uses C(nfs4_setfacl) and C(nfs4_getfacl). The behavior depends on those implementation.
      And currently it only supports C(A) in ACE, so C(D) must be replaced with the appropriate C(A).
    - Permission is set as optimised ACLs by the system. You can check the actual ACLs that has been set using the return value.
    - More info C(man nfs4_setfacl)
    type: bool
    default: false
  recalculate_mask:
    description:
    - Select if and when to recalculate the effective right masks of the files.
    - See C(setfacl) documentation for more info.
    - Incompatible with O(state=query).
    choices: [ default, mask, no_mask ]
    default: default
    type: str
author:
- Brian Coca (@bcoca)
- Jérémie Astori (@astorije)
notes:
- The M(ansible.posix.acl) module requires that ACLs are enabled on the target filesystem and that the C(setfacl) and C(getfacl) binaries are installed.
- As of Ansible 2.0, this module only supports Linux distributions.
- As of Ansible 2.3, the O(name) option has been changed to O(path) as default, but O(name) still works as well.
a  
- name: Grant user Joe read access to a file
  ansible.posix.acl:
    path: /etc/foo.conf
    entity: joe
    etype: user
    permissions: r
    state: present

- name: Removes the ACL for Joe on a specific file
  ansible.posix.acl:
    path: /etc/foo.conf
    entity: joe
    etype: user
    state: absent

- name: Sets default ACL for joe on /etc/foo.d/
  ansible.posix.acl:
    path: /etc/foo.d/
    entity: joe
    etype: user
    permissions: rw
    default: true
    state: present

- name: Same as previous but using entry shorthand
  ansible.posix.acl:
    path: /etc/foo.d/
    entry: default:user:joe:rw-
    state: present

- name: Obtain the ACL for a specific file
  ansible.posix.acl:
    path: /etc/foo.conf
  register: acl_info
z
acl:
    description: Current ACL on provided path (after changes, if any)
    returned: success
    type: list
    sample: [ "user::rwx", "group::rwx", "other::rwx" ]
N)AnsibleModule)	to_nativec                    | j                  d      }d}| j                         j                  d      rd}|j                  d       t	        |      dk(  r|j                  d       |\  }}}|j                         }|j                  d      rd}n>|j                  d	      rd
}n*|j                  d      rd}n|j                  d      rd}nd}||||gS )z+ splits entry and ensures normalized return:NdTr      uuserggroupmmaskoother)splitlower
startswithpoplenappend)entryar
   teps         e/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/ansible/posix/plugins/modules/acl.pysplit_entryr       s     	CAA{{}$	a
1v{	GAq!		A||C	
c		
c		
c	q!Q<    c                 t    |rdj                  d| dk(  rdnd||dz   g      S |r| dz   |z   dz   |z   S | dz   |z   S )zbBuilds and returns an entry string. Does not include the permissions bit if they are not provided.r	   Ar   r    tcy)join)etypeentitypermissionsuse_nfsv4_aclss       r   build_entryr+      sW    xxUg%5c2v{UZGZ[\\s{V#c)K773;r!   c	                    |dk(  r/| j                  |rdndd      g}	|	j                  |rdnd|g       n|dk(  r+| j                  |rdndd      g}	|	j                  d|g       n| j                  d	d      g}	t        j                         j	                         d
k(  rK|r| j                  dd      g}	n$| j                  d	d      g}	|	j                  d       |	j                  d       |r|s|	j                  d       |dk(  r|dv r|	j                  d       n|dk(  r|dv r|	j                  d       |so|smt        j                         j	                         d
k(  r|	j                  d       n6t        j                         j	                         dk(  r|	j                  d       |r|	j                  dd       |	j                  |       |	S )z-Builds and returns a getfacl/setfacl command.setnfs4_setfaclsetfaclTz-az-mrmz-xgetfacllinuxnfs4_getfaclz--absolute-namesz--omit-headerz--recursiver   )r-   r0   z--maskno_maskz	--no-maskz
--physicalfreebsdz-h   z-d)get_bin_pathextendplatformsystemr   r   insert)
modulemodepathfollowdefault	recursiverecalculate_maskr*   r   cmds
             r   build_commandrD      s   u}""^>TXYZ

NDe<=	""^>TXYZ

D%=!""9d34??""$/**>4@A**9d;<

-.JJ'

=!6!dm&;

8	Y	&4=+@

;.??""$/JJ|$__$$&)3JJt

1dJJtJr!   c                     t        j                         j                         dk(  ry|dd }|j                  dd       t	        | |      }d}|D ]%  }|j                  d      r|s y|s||k(  s!|dz  }' |d	k(  ryy)
zPReturns true if the provided command affects the existing ACLs, false otherwise.r5   TNr6   z--testr   z*,*Fr   )r9   r:   r   r;   run_aclendswith)r<   rC   r   r*   linescounterlines          r   acl_changedrK      s      I-
a&CJJq(FC EG ==etmqLG !|r!   c                 \   	 | j                  ||      \  }}}g }j	                         D ]3  }|j                  d      r|j                  |j                                5 |r|d   j                         s|dd S |S # t        $ r%}| j                  t        |             Y d}~d}~ww xY w)zDRuns the provided command and returns the output as a list of lines.)check_rcmsgN#)	run_command	Exception	fail_jsonr   
splitlinesr   r   stripr   )	r<   rC   rM   rcouterrr   rH   ls	            r   rF   rF     s    +++C(+CS# E^^ $||C LL#$ U2Y__&SbzL  +Yq\**+s   A= =	B+B&&B+c                     t        t        t        dddg      t        d      t        dd      t        dg d	
      t        d      t        ddg d      t        dd      t        dd      t        dddg      t        ddg d      t        dd            d      } t        j                         j	                         dvr| j                  d       | j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }	| j                  j                  d       }
| j                  j                  d!      }t        j                  j                  |      s| j                  d"       |dk(  r*|	r| j                  d#       |
d$v r| j                  d%       |sO|d&k(  r|r|s| j                  d'       |d&k(  r|s| j                  d(       |d)v r|s| j                  d*|z         |r|s|s|r| j                  d+       |d,k(  r%|j                  d-      d.vr| j                  d/       |d&k(  r%|j                  d-      d0vr| j                  d1       |dk(  r| j                  d2       t        |      \  }}}}||}t        j                         j	                         d3k(  r|	r| j                  d4       d}d}|d,k(  rOt        ||||      }t        | d5||||	|
||	      }t        | |||      }|r| j                   st#        | |       d6|z  }nl|d&k(  r`|rt        ||||      }nt        |||      }t        | d7||||	|
||	      }t        | |||      }|r| j                   st#        | |d       d8|z  }n|dk(  rd9}t#        | t        | d:||||	|
|            }| j%                  |||;       y )<Nr>   Tname)typerequiredaliasesstr)r]   r$   )r]   r@   )r   r   r   r   )r]   choicesquery)absentpresentrb   )r]   r@   ra   boolFrecurse)r]   r@   r_   r@   )r@   r   r4   )r>   r   r(   r'   r)   stater?   r@   rA   rB   r*   )argument_specsupports_check_mode)r2   r5   z/The acl module is not available on this system.rN   r   r(   r'   r)   rg   r?   rA   rB   r*   z!Path not found or not accessible.z/'recursive' MUST NOT be set when 'state=query'.)r   r4   zM'recalculate_mask' MUST NOT be set to 'mask' or 'no_mask' when 'state=query'.rc   z2'permissions' MUST NOT be set when 'state=absent'.z)'entity' MUST be set when 'state=absent'.)rd   rc   z$'etype' MUST be set when 'state=%s'.zH'entry' MUST NOT be set when 'entity', 'etype' or 'permissions' are set.rd   r	   )r      zF'entry' MUST have 3 or 4 sections divided by ':' when 'state=present'.)r6   r   zE'entry' MUST have 2 or 3 sections divided by ':' when 'state=absent'.z+'entry' MUST NOT be set when 'state=query'.r5   z,recursive is not supported on that platform.r-   z%s is presentr0   z%s is absentzcurrent aclget)changedrO   acl)r   dictr9   r:   r   rT   paramsrk   osr>   existscountr    r+   rD   rK   
check_moderF   	exit_json)r<   r>   r   r(   r'   r)   rg   r?   r@   rA   rB   r*   default_flagrl   rO   commandrm   s                    r   mainrw     su   6D6(CE"UB/: %(6
 VT2fe4	{K!!6
  VU;-
0 !3F8  (<<NO==V$DMMg&E]]x(FMMg&E--##M2KMMg&E]]x(Fmm	*G!!+.I}}(();<]]&&'78N77>>$@A!RS22!pqH^!UVHV!LM))%!G%!OPFk!klIekk#&6&&@!ijHU[[%5%?!hiG!NO3>u3E0eV[#"G I-!OPG
C	E6;GE4Y 0.%
 fgunE6,,FG$%	(	v{NKEv~>ED$Y 0.%
 fgunE6,,FGU+u$	'	
E4)n	
C W#37r!   __main__)NF)r$   )F)T)
__future__r   r   r   r]   __metaclass__DOCUMENTATIONEXAMPLESRETURNrp   r9   ansible.module_utils.basicr   ansible.module_utils._textr   r    r+   rD   rK   rF   rw   __name__ r!   r   <module>r      sn    A @Xt#J
 
  4 0< &R4&~8B zF r!   