
    Vh#                     z    d dl mZmZmZ eZdZdZd dlZd dl	m
Z
 d dlmZ  G d de      Zd	 Zed
k(  r e        yy)    )absolute_importdivisionprint_functiona	  
module: sudoers
short_description: Manage sudoers files
version_added: "4.3.0"
description:
  - This module allows for the manipulation of sudoers files.
author:
  - "Jon Ellis (@JonEllis) <ellis.jp@gmail.com>"
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  commands:
    description:
      - The commands allowed by the sudoers rule.
      - Multiple can be added by passing a list of commands.
      - Use V(ALL) for all commands.
    type: list
    elements: str
  group:
    description:
      - The name of the group for the sudoers rule.
      - This option cannot be used in conjunction with O(user).
    type: str
  name:
    required: true
    description:
      - The name of the sudoers rule.
      - This will be used for the filename for the sudoers file managed by this rule.
    type: str
  noexec:
    description:
      - Whether a command is prevented to run further commands itself.
    default: false
    type: bool
    version_added: 8.4.0
  nopassword:
    description:
      - Whether a password is required when command is run with sudo.
    default: true
    type: bool
  setenv:
    description:
      - Whether to allow keeping the environment when command is run with sudo.
    default: false
    type: bool
    version_added: 6.3.0
  host:
    description:
      - Specify the host the rule is for.
    default: ALL
    type: str
    version_added: 6.2.0
  runas:
    description:
      - Specify the target user the command(s) will run as.
    type: str
    version_added: 4.7.0
  sudoers_path:
    description:
      - The path which sudoers config files will be managed in.
    default: /etc/sudoers.d
    type: str
  state:
    default: "present"
    choices:
      - present
      - absent
    description:
      - Whether the rule should exist or not.
    type: str
  user:
    description:
      - The name of the user for the sudoers rule.
      - This option cannot be used in conjunction with O(group).
    type: str
  validation:
    description:
      - If V(absent), the sudoers rule will be added without validation.
      - If V(detect) and visudo is available, then the sudoers rule will be validated by visudo.
      - If V(required), visudo must be available to validate the sudoers rule.
    type: str
    default: detect
    choices: [absent, detect, required]
    version_added: 5.2.0
a9  
- name: Allow the backup user to sudo /usr/local/bin/backup
  community.general.sudoers:
    name: allow-backup
    state: present
    user: backup
    commands: /usr/local/bin/backup

- name: Allow the bob user to run any commands as alice with sudo -u alice
  community.general.sudoers:
    name: bob-do-as-alice
    state: present
    user: bob
    runas: alice
    commands: ALL

- name: >-
    Allow the monitoring group to run sudo /usr/local/bin/gather-app-metrics
    without requiring a password on the host called webserver
  community.general.sudoers:
    name: monitor-app
    group: monitoring
    host: webserver
    commands: /usr/local/bin/gather-app-metrics

- name: >-
    Allow the alice user to run sudo /bin/systemctl restart my-service or
    sudo /bin/systemctl reload my-service, but a password is required
  community.general.sudoers:
    name: alice-service
    user: alice
    commands:
      - /bin/systemctl restart my-service
      - /bin/systemctl reload my-service
    nopassword: false

- name: Revoke the previous sudo grants given to the alice user
  community.general.sudoers:
    name: alice-service
    state: absent

- name: Allow alice to sudo /usr/local/bin/upload and keep env variables
  community.general.sudoers:
    name: allow-alice-upload
    user: alice
    commands: /usr/local/bin/upload
    setenv: true

- name: >-
    Allow alice to sudo /usr/bin/less but prevent less from
    running further commands itself
  community.general.sudoers:
    name: allow-alice-restricted-less
    user: alice
    commands: /usr/bin/less
    noexec: true
N)AnsibleModule)	to_nativec                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)Sudoersi   c                    || _         |j                  | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _	        |j                  d   | _
        |j                  d	   | _        |j                  d
   | _        t        j                  j                  | j                  | j                        | _        |j                  d   | _        |j                  d   | _        y )Nnameusergroupstatenoexec
nopasswordsetenvhostrunassudoers_pathcommands
validation)module
check_modeparamsr   r   r   r   r   r   r   r   r   r   ospathjoinfiler   r   )selfr   s     m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/sudoers.py__init__zSudoers.__init__   s     ++MM&)	MM&)	]]7+
]]7+
mmH- --5mmH-MM&)	]]7+
"MM.9GGLL!2!2DII>	j1 --5    c                    | j                   ry t        | j                  d      5 }|j                  | j	                                d d d        t        j                  | j                  | j                         y # 1 sw Y   4xY w)Nw)r   openr   writecontentr   chmod	FILE_MODE)r   fs     r   r%   zSudoers.write   s[    ??$))S! 	$QGGDLLN#	$ 	DNN+	$ 	$s    A77B c                 \    | j                   ry t        j                  | j                         y N)r   r   remover   r   s    r   deletezSudoers.delete   s    ??
		$))r!   c                 T    t         j                  j                  | j                        S r+   )r   r   existsr   r-   s    r   r0   zSudoers.exists   s    ww~~dii((r!   c                    t        | j                  d      5 }|j                         | j                         k(  }d d d        t	        j
                  | j                        j                  dz  }|| j                  k(  }xr |S # 1 sw Y   JxY w)Nri  )r$   r   readr&   r   statst_moder(   )r   r)   content_matchescurrent_modemode_matchess        r   matcheszSudoers.matches   su    $))S! 	9Qffh$,,.8O	9 wwtyy)11E9#t~~5/</	9 	9s   "BBc           	         | j                   r| j                   }n(| j                  rdj                  | j                        }dj                  | j                        }| j
                  rdnd}| j                  rdnd}| j                  rdnd}| j                  dj                  | j                  	      nd}d
j                  | j                  |||||      S )Nz%{group})r   z, zNOEXEC: z	NOPASSWD:zSETENV:z	({runas}))r   z<{owner} {host}={runas}{noexec}{nopasswd}{setenv} {commands}
)ownerr   r   r   nopasswdr   r   )
r   r   formatr   r   r   r   r   r   r   )r   r<   commands_str
noexec_strnopasswd_str
setenv_str	runas_strs          r   r&   zSudoers.content   s    99IIEZZ%%DJJ%7Eyy/"&++Y2
&*oo{2"&++Y2
<@JJ<RK&&TZZ&8XZ	NUU!! V 
 	
r!   c                 X   | j                   dk(  ry | j                  j                  d| j                   dk(        }|y |dddg}| j                  j                  || j	                               \  }}}|d	k7  r3| j                  j                  d
j                  |xs |      ||       y y )Nabsentvisudorequired)rG   z-cz-f-)datar   z)Failed to validate sudoers rule:
{stdout})stdout)msgrJ   stderr)r   r   get_bin_pathrun_commandr&   	fail_jsonr>   )r   visudo_pathcheck_commandrcrJ   rL   s         r   validatezSudoers.validate   s    ??h&kk..x$//U_B_.`$dD#6![[44]4XFF7KK!!&R&Y&Yagaqkq&Y&r  |B  KQ!  R r!   c                     | j                   dk(  r"| j                         r| j                          yy| j                          | j                         r| j	                         ry| j                          y)NrE   TF)r   r0   r.   rS   r9   r%   r-   s    r   runzSudoers.run   sO    ::!{{};;=T\\^

r!   N)__name__
__module____qualname__r(   r    r%   r.   r0   r9   r&   rS   rU    r!   r   r	   r	      s0    I6$,)0
*Rr!   r	   c                  P   dddi ddiddddddddddd	ddd ddd
ddddgdi dg ddd} t        | ddggddddgfg      }t        |      }	 |j                         }|j                  |       y # t        $ r%}|j                  t        |             Y d }~y d }~ww xY w)Nliststr)typeelementsrG   TboolF)r]   defaultALLz/etc/sudoers.dpresentrE   )r`   choicesdetect)rE   rd   rG   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )argument_specmutually_exclusivesupports_check_moderequired_if)changed)rK   )r   r	   rU   	exit_json	ExceptionrO   r   )re   r   sudoersri   es        r   mainrn     s    
 
 

 

 

 

 

 '

 !!8,
 7
M*MX ##W-. y:,78	F foG+++-) +Yq\**+s   "A7 7	B% B  B%__main__)
__future__r   r   r   r]   __metaclass__DOCUMENTATIONEXAMPLESr   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   objectr	   rn   rV   rY   r!   r   <module>rw      sX    A @Yv8t 
 4 Abf bJ:+z zF r!   