
    Vh6              
       6   d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZmZ d dlmZ dZ	 d dlZdZdZ	 d dlZdZersej.                  j1                  ej2                  ej4                  ej6                  ej8                  ej:                  ej<                  ej>                  ej@                  
        e!dddddddd
      Z"ddZ#d Z$d Z%ddZ&ddZ'd Z(e)dk(  r e(        yy# e$ r  e	j&                         Zd	ZY w xY w# e$ r  e	j&                         Zd	ZY w xY w)    )absolute_importdivisionprint_functional  
module: sefcontext
short_description: Manages SELinux file context mapping definitions
description:
  - Manages SELinux file context mapping definitions.
  - Similar to the C(semanage fcontext) command.
extends_documentation_fragment:
  - community.general.attributes
  - community.general.attributes.platform
attributes:
  check_mode:
    support: full
  diff_mode:
    support: full
  platform:
    platforms: linux
options:
  target:
    description:
      - Target path (expression).
    type: str
    required: true
    aliases: [path]
  ftype:
    description:
      - The file type that should have SELinux contexts applied.
      - 'The following file type options are available:'
      - V(a) for all files,
      - V(b) for block devices,
      - V(c) for character devices,
      - V(d) for directories,
      - V(f) for regular files,
      - V(l) for symbolic links,
      - V(p) for named pipes,
      - V(s) for socket files.
    type: str
    choices: [a, b, c, d, f, l, p, s]
    default: a
  setype:
    description:
      - SELinux type for the specified O(target).
    type: str
  substitute:
    description:
      - Path to use to substitute file context(s) for the specified O(target). The context labeling for the O(target) subtree
        is made equivalent to this path.
      - This is also referred to as SELinux file context equivalence and it implements the C(equal) functionality of the SELinux
        management tools.
    version_added: 6.4.0
    type: str
    aliases: [equal]
  seuser:
    description:
      - SELinux user for the specified O(target).
      - Defaults to V(system_u) for new file contexts and to existing value when modifying file contexts.
    type: str
  selevel:
    description:
      - SELinux range for the specified O(target).
      - Defaults to V(s0) for new file contexts and to existing value when modifying file contexts.
    type: str
    aliases: [serange]
  state:
    description:
      - Whether the SELinux file context must be V(absent) or V(present).
      - Specifying V(absent) without either O(setype) or O(substitute) deletes both SELinux type or path substitution mappings
        that match O(target).
    type: str
    choices: [absent, present]
    default: present
  reload:
    description:
      - Reload SELinux policy after commit.
      - Note that this does not apply SELinux file contexts to existing files.
    type: bool
    default: true
  ignore_selinux_state:
    description:
      - Useful for scenarios (chrooted environment) that you cannot get the real SELinux state.
    type: bool
    default: false
notes:
  - The changes are persistent across reboots.
  - O(setype) and O(substitute) are mutually exclusive.
  - If O(state=present) then one of O(setype) or O(substitute) is mandatory.
  - The M(community.general.sefcontext) module does not modify existing files to the new SELinux context(s), so it is advisable
    to first create the SELinux file contexts before creating files, or run C(restorecon) manually for the existing files
    that require the new SELinux file contexts.
  - Not applying SELinux fcontexts to existing files is a deliberate decision as it would be unclear what reported changes
    would entail to, and there is no guarantee that applying SELinux fcontext does not pick up other unrelated prior changes.
requirements:
  - libselinux-python
  - policycoreutils-python
author:
  - Dag Wieers (@dagwieers)
a   
- name: Allow apache to modify files in /srv/git_repos
  community.general.sefcontext:
    target: '/srv/git_repos(/.*)?'
    setype: httpd_sys_rw_content_t
    state: present

- name: Substitute file contexts for path /srv/containers with /var/lib/containers
  community.general.sefcontext:
    target: /srv/containers
    substitute: /var/lib/containers
    state: present

- name: Delete file context path substitution for /srv/containers
  community.general.sefcontext:
    target: /srv/containers
    substitute: /var/lib/containers
    state: absent

- name: Delete any file context mappings for path /srv/git
  community.general.sefcontext:
    target: /srv/git
    state: absent

- name: Apply new SELinux file context to filesystem
  ansible.builtin.command: restorecon -irv /srv/git_repos
z
# Default return values
N)AnsibleModulemissing_required_lib)	to_nativeTF)abcdflpsz	all fileszblock devicezcharacter device	directoryzregular filezsymbolic linkz
named pipesocketc                 6    | du rdS t        j                         S )NT)selinuxis_selinux_enabled)ignore_selinux_states    p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/sefcontext.pyget_runtime_statusr      s    '4/4QW5O5O5QQ    c                 b    |t         |   f}| j                         }	 ||   S # t        $ r Y yw xY w)z` Get the SELinux file context mapping definition from policy. Return None if it does not exist. N)option_to_file_type_strget_allKeyError)
sefcontexttargetftyperecordrecordss        r   semanage_fcontext_existsr#      sC     -e45F  "Gv s   " 	..c                 l    | j                   j                  || j                  j                  |            S )zj Get the SELinux file context path substitution definition from policy. Return None if it does not exist. )
equiv_distgetequiv)r   r   s     r   #semanage_fcontext_substitute_existsr(      s.       $$VZ-=-=-A-A&-IJJr   c
                 d   d}
d}	 t        j                  |	      }|j                  |       |t        |||      }|r|\  }}}}||}||}||k7  s||k7  s||k7  rW| j                  s|j                  |||||       d}
| j                  r'|dz  }|d|d|d|d|d|d|d	z  }|d
|d|d|d|d|d|d	z  }n|d}|d}| j                  s|j                  |||||       d}
| j                  r|dz  }|d
|d|d|ddd|d|d	z  }nt        ||      }|rQ|}||k7  r| j                  s|j                  ||       d}
| j                  r[|dz  }|d|d|d	z  }|d
|d|d	z  }n=| j                  s|j                  ||       d}
| j                  r|dz  }|d
|d|d	z  }| j                  r|rt!        |      |d<    | j"                  d|
||d| y# t        $ r?}| j                  |j                  j                  dt        |      d	       Y d}~wd}~ww xY w)zF Add or modify SELinux file context mapping definition to the policy. F NTz+# Change to semanage file context mappings
-      :
+system_us0z-# Addition to semanage file context mappings
object_rz5# Change to semanage file context path substitutions
 = z7# Addition to semanage file context path substitutions
: msgprepareddiff)changedseuserserange )seobjectfcontextRecords
set_reloadr#   
check_modemodify_diffaddr(   modify_equal	add_equal	Exception	fail_json	__class____name__r   dict	exit_json)moduleresultr   r    setype
substitute	do_reloadr<   r;   sestorer:   prepared_diffr   existsorig_seuserorig_seroleorig_setypeorig_serangeorig_substitutees                       r   semanage_fcontext_modifyr[      s    GM>P--g6
i(-j&%HFFLC[+|>(F?*G[(Fk,AWP\E\!,,"))&&%&Q"G||%)WW%VUZ\git  wB  DP  *Q  Q%VUZ\bdoqw  zA  *B  B >'F?"G((NN665'6J<<!%UUM!QVX^`jlrt{%||M8VLF"(0!,,"//
C"G||%)aa%)QQ%)LL ((((<<<!%__M!VZ%HHM
 ||}5vFOWVWOO  P1;;+?+?1NOOPs   F-G' '	H/05H**H/c                    d}d}		 t        j                  |      }
|
j                  |       t        |
||      }t	        |
|      }|r^|\|\  }}}}| j
                  s|
j                  ||       d}| j                  r)|	dz  }	|	d|d|d|d   d	|d
   d	|d   d	|d   dz  }	|rJ|H|||k(  s|?|}| j
                  s|
j                  ||       d}| j                  r|	dz  }	|	d|d|dz  }	| j                  r|	rt        |	      |d<    | j                  dd|i| y# t        $ r?}| j                  |j                  j                  dt        |      d       Y d}~ud}~ww xY w)zA Delete SELinux file context mapping definition from the policy. Fr*   NTz-# Deletion to semanage file context mappings
r+   r,   r   r-            r.   z7# Deletion to semanage file context path substitutions
r3   r4   r5   r7   r9   r:   r=   )r>   r?   r@   r#   r(   rA   deleterC   rG   rH   rI   rJ   r   rK   rL   )rM   rN   r   r    rO   rP   rQ   rR   r:   rS   r   rT   substitute_existsrU   rV   rW   rX   rY   rZ   s                      r   semanage_fcontext_deleterb     s    GMP--g6
i()*feD?
FSj(BH?Kk<$$!!&%0G||!QQVUTZ[\T]_efg_hjpqrjsu{|}u~!j6LQbfpQpu  vH/O$$!!&/:G||![[!II
 ||}5vF/W//  P1;;+?+?1NOOPs   C+D# #	E+,5E&&E+c                     t        t        t        dd      t        dddg      t        ddt        t        j	                               	      t        d
      t        ddg      t        d
      t        ddg      t        ddddg	      t        dd      	      g ddgd      } t
        s | j                  t        d      t               t        s | j                  t        d      t               | j                  d   }t        |      s| j                  d       | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d    }| j                  d!   }	t        |||||"      }
|dk(  rt        | |
|||||	||	       y |dk(  rt        | |
|||||	       y | j                  d#j                  |             y )$NboolF)typedefaultstrTpath)re   requiredaliasesr	   )re   rf   choices)re   equal)re   rj   r<   presentabsent)	r   r   r    rO   rP   r;   selevelstatereload)rO   rP   )rP   r    )rP   r;   )rP   ro   )rp   rm   rr   T)argument_specmutually_exclusiverequired_ifsupports_check_modezlibselinux-python)r6   	exceptionzpolicycoreutils-pythonr   z!SELinux is disabled on this host.r5   r   r    rO   rP   r;   ro   rp   rq   )r   r    rO   rP   rp   z&Invalid value of argument "state": {0})r   rK   listr   keysHAVE_SELINUXrH   r   SELINUX_IMP_ERRHAVE_SEOBJECTSEOBJECT_IMP_ERRparamsr   r[   rb   format)rM   r   r   r    rO   rP   r;   r<   rp   rQ   rN   s              r   mainr   H  s   !%65!AUTF8DE3=T=Y=Y=[8\]U#	:U#ei[9E9x>STVT2


 A
 !-F0 12EFRab12JKWgh!==)?@23@A]]8$FMM'"E]]8$F|,J]]8$FmmI&GMM'"Eh'IuV
Z_`F	 
T]_fhno	(	 
T]^ELLUSTr   __main__)F)r*   )*
__future__r   r   r   re   __metaclass__DOCUMENTATIONEXAMPLESRETURN	tracebackansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr   r{   r   rz   ImportError
format_excr}   r>   r|   
file_typesupdateSEMANAGE_FCONTEXT_ALLSEMANAGE_FCONTEXT_BLOCKSEMANAGE_FCONTEXT_CHARSEMANAGE_FCONTEXT_DIRSEMANAGE_FCONTEXT_REGSEMANAGE_FCONTEXT_LINKSEMANAGE_FCONTEXT_PIPESEMANAGE_FCONTEXT_SOCKrK   r   r   r#   r(   r[   rb   r   rJ   r=   r   r   <module>r      sn   A @_B8
  J AL
  M 

(
(

*
*

)
)

(
(

(
(

)
)

)
)

)
)  	 	 R	KIPX(0V4Un zF U  *i**,OL  +y++-Ms"   C C= C:9C:=DD