
    Vh-                        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Zd dlZdZ	 d dlZdZd dlmZmZ d d	lmZ d d
lmZ d dlmZmZ d Zd Zd Zd Zd Z d Z!d Z"d Z#e$dk(  r e#        yy# e$ r  ej"                         ZdZY \w xY w)    )absolute_importdivisionprint_functionai  
---
module: selinux
short_description: Change policy and state of SELinux
description:
  - Configures the SELinux mode and policy.
  - A reboot may be required after usage.
  - Ansible will not issue this reboot but will let you know when it is required.
version_added: "1.0.0"
options:
  policy:
    description:
      - The name of the SELinux policy to use (e.g. C(targeted)) will be required unless O(state=disabled).
    type: str
  state:
    description:
      - The SELinux mode.
    required: true
    choices: [ disabled, enforcing, permissive ]
    type: str
  update_kernel_param:
    description:
      - If set to V(true), will update also the kernel boot parameters when disabling/enabling SELinux.
      - The C(grubby) tool must be present on the target system for this to work.
    default: false
    type: bool
    version_added: '1.4.0'
  configfile:
    description:
      - The path to the SELinux configuration file, if non-standard.
    default: /etc/selinux/config
    aliases: [ conf, file ]
    type: str
requirements: [ libselinux-python ]
author:
- Derek Carter (@goozbach) <goozbach@friocorte.com>
a5  
- name: Enable SELinux
  ansible.posix.selinux:
    policy: targeted
    state: enforcing

- name: Put SELinux in permissive mode, logging actions that would be blocked.
  ansible.posix.selinux:
    policy: targeted
    state: permissive

- name: Disable SELinux
  ansible.posix.selinux:
    state: disabled
a  
msg:
    description: Messages that describe changes that were made.
    returned: always
    type: str
    sample: Config SELinux state changed from 'disabled' to 'permissive'
configfile:
    description: Path to SELinux configuration file.
    returned: always
    type: str
    sample: /etc/selinux/config
policy:
    description: Name of the SELinux policy.
    returned: always
    type: str
    sample: targeted
state:
    description: SELinux mode.
    returned: always
    type: str
    sample: enforcing
reboot_required:
    description: Whether or not an reboot is required for the changes to take effect.
    returned: always
    type: bool
    sample: true
NTF)AnsibleModulemissing_required_lib)get_bin_path)get_file_lines)respawn_moduleHAS_RESPAWN_UTILc                     t        | d      }|D ]>  }t        j                  d|      }|s|j                  d      d   j	                         c S  y )NFstrip^SELINUX=.*$=   r	   rematchsplitr   
configfilelinesline	statelines       i/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/ansible/posix/plugins/modules/selinux.pyget_config_stater   r   sL    :U3E .HH_d3	::c?1%++--.    c                     t        | d      }|D ]>  }t        j                  d|      }|s|j                  d      d   j	                         c S  y )NFr   ^SELINUXTYPE=.*$r   r   r   r   s       r   get_config_policyr    {   sM    :U3E .HH0$7	::c?1%++--.r   c                 P   || j                  dd       | j                  |dg      \  }}}|dk7  r| j                  d       d}d}|j                  d	      D ]D  }t        j                  d
|      }||j                  d      j                  d      }	d|	v rd}Cd}F ||k(  ry |S )Nz"'grubby' command not found on hostzxIn order to update the kernel command lineenabled/disabled setting, the grubby packageneeds to be present on the system.msgdetailsz
--info=ALLr   zunable to run grubbyr#   T
z^args="(.*)"$r    	selinux=0F)	fail_jsonrun_commandr   r   r   group)
module
grubby_binrcstdoutstderrall_enabledall_disabledr   r   argss
             r   get_kernel_enabledr4      s    A"F 	 	G
  ++Z,FGB	Qw34KLT" !$/={{1~##C($K L! {"r   c           	      |   d|z  }t        |d      }t        j                         \  }}t        |d      5 }d}|D ]C  }	t	        j
                  d|	      rd}|j                  t	        j                  d||	      dz          E |s|j                  d	|z         d d d        | j                  ||       y # 1 sw Y   xY w)
Nz
SELINUX=%sFr   wr   Tz^SELINUX=.*r&   zSELINUX=%s
)	r	   tempfilemkstempopenr   r   writesubatomic_move)
r,   stater   r   r   tmpfdtmpfile
write_file
line_foundr   s
             r   set_config_staterB      s     u$I:U3E%%'NE7	gs	 5z
 	MDxx.!
RVVNItDtKL	M
 ^e345 w
+5 5s   A!B22B;c                     |dk(  rt        j                  d       y |dk(  rt        j                  d       y |dk(  ry d|z  }| j                  |       y )N	enforcingr   
permissiver   disabledz&trying to set invalid runtime state %sr%   )selinuxsecurity_setenforcer)   )r,   r=   r#   s      r   	set_staterI      sT    ##A&	,	##A&	*	6>S!r   c                     | j                  |d|rdnddg      \  }}}|dk7  r(|r| j                  d       y | j                  d       y y )	Nz--update-kernel=ALLz--remove-argsz--argsr(   r   z-unable to remove selinux=0 from kernel configr%   z(unable to add selinux=0 to kernel config)r*   r)   )r,   r-   valuer.   r/   r0   s         r   set_kernel_enabledrL      sb    ++Z9N@E_8-8-: ;B 
Qw!PQ!KL	 r   c           	         t         j                  j                  d|z        s| j                  d|z         d|z  }t	        |d      }t        j                         \  }}t        |d      5 }d}|D ]C  }	t        j                  d|	      rd	}|j                  t        j                  d
||	      dz          E |s|j                  d|z         d d d        | j                  ||       y # 1 sw Y   xY w)Nz/etc/selinux/%s/policyz)Policy %s does not exist in /etc/selinux/r%   zSELINUXTYPE=%sFr   r6   r   Tz^SELINUXTYPE=.*r&   zSELINUXTYPE=%s
)ospathexistsr)   r	   r7   r8   r9   r   r   r:   r;   r<   )
r,   policyr   
policyliner   r>   r?   r@   rA   r   s
             r   set_config_policyrS      s    77>>2V;<H6QR "F*J:U3E%%'NE7	gs	 :z
 	RDxx+T2!
RVV$6
DIDPQ	R
 /&89: w
+: :s   -A!C))C2c                      t        t        t        d      t        ddg d      t        ddddg	      t        d
d            d      } t        s1t        rt	        d       | j                  t        d      t               d}g }| j                  d   }| j                  d   }| j                  d   }| j                  d   }t        j                         }t        j                         d   }d}	d }
d}|rt        j                         rd}	nd}	t        j                  j                  |      s"| j                  dj!                  |      d       t#        |      }t%        |      }|r	 t'        d      }t+        | |      }
|dk7  r|s| j                  d       n|s|}||k7  r8| j,                  r| j/                  d        |j1                  d!|d"|d#       d}||k7  rH| j,                  r| j/                  d        t3        | ||       |j1                  d$|d%|d"|d#       d}||	k7  r|r|dk(  rH|	dk7  r/| j,                  st5        | d       | j7                  d&|	z         d}n| j7                  d'       d}nI| j,                  st5        | |       |j1                  d(|	d"|d#       d}n| j7                  d)|z         d}||k7  r3| j,                  st9        | ||       |j1                  d*|d"|d#       d}|d+v }|rD|
|k7  r?| j,                  st;        | |       |rd,}nd-}|
d.|d   f}|j1                  d/|z         d}| j/                  |d0j=                  |      ||||1       y # t(        $ r d }Y w xY w)2Nstr)typeT)rD   rE   rF   )rV   requiredchoicesz/etc/selinux/configconffile)rV   defaultaliasesboolF)rV   r[   )rQ   r=   r   update_kernel_param)argument_specsupports_check_moderG   zlibselinux-python)r#   	exceptionr   rQ   r=   r^   r   rF   rD   rE   zUnable to find file {0}zSPlease install SELinux-policy package, if this package is not installed previously.r"   grubbyz-Policy is required if state is not 'disabled'r%   )changedz%Running SELinux policy changed from 'z' to ''z!SELinux policy configuration in 'z' changed from 'zgSELinux state temporarily changed from '%s' to 'permissive'. State change will take effect next reboot.z1SELinux state change will take effect next rebootzSELinux state changed from 'z/Reboot is required to set SELinux state to '%s'z#Config SELinux state changed from ')rD   rE   )rF   enabled)re   rF   z<inconsistent>z.Kernel SELinux state changed from '%s' to '%s'z, )rc   r#   r   rQ   r=   reboot_required)r   dictHAS_SELINUXr   r
   r)   r   SELINUX_IMP_ERRparamsrG   is_selinux_enabledselinux_getpolicytypesecurity_getenforcerN   rO   isfileformatr    r   r   
ValueErrorr4   
check_mode	exit_jsonappendrS   rI   warnrB   rL   join)r,   rc   msgsr   rQ   r=   r^   runtime_enabledruntime_policyruntime_statekernel_enabledrf   config_policyconfig_stater-   requested_kernel_enabledstatess                    r   mainr      s   U#ED:ab0EPVX^O_` $&% @	
 !F 9%12EFRab GD|,J]]8$FMM'"E --(=>002O224Q7NMNO&&('M(M77>>*%6==jI"P 	 	Q &j1M#J/L	%h/J ,FJ? 
!PQ"F T*.Z`abT*&&*5Xbdqsyz{
" L0!,,!&,7KK  !J  N[  !\  ]"GKK ST"&((fe,X]^_ KKIEQR"O  VUJ7V[\]$(CC ~1II  vz3KL#,F,F!&q	2FDvMN
W$))D/jY_gl  N  OQ  	J	s   )M. .M=<M=__main__)%
__future__r   r   r   rV   __metaclass__DOCUMENTATIONEXAMPLESRETURNrN   r   r7   	tracebackri   rG   rh   ImportError
format_excansible.module_utils.basicr   r   #ansible.module_utils.common.processr    ansible.module_utils.facts.utilsr	   ?ansible_collections.ansible.posix.plugins.module_utils._respawnr
   r   r   r    r4   rB   rI   rL   rS   r   __name__ r   r   <module>r      s    A @$L 
8 
 	  K
 K < ; l..:,*	"M,0vOr zF q  *i**,OKs   A. .B	B	