
    Vhs+                         d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
mZ d dlmZmZ d Z G d d	e      Z G d
 de      Z G d de      Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: crypttab
short_description: Encrypted Linux block devices
description:
  - Control Linux encrypted block devices that are set up during system boot in C(/etc/crypttab).
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    description:
      - Name of the encrypted block device as it appears in the C(/etc/crypttab) file, or optionally prefixed with V(/dev/mapper/),
        as it appears in the filesystem. V(/dev/mapper/) will be stripped from O(name).
    type: str
    required: true
  state:
    description:
      - Use V(present) to add a line to C(/etc/crypttab) or update its definition if already present.
      - Use V(absent) to remove a line with matching O(name).
      - Use V(opts_present) to add options to those already present; options with different values will be updated.
      - Use V(opts_absent) to remove options from the existing set.
    type: str
    required: true
    choices: [absent, opts_absent, opts_present, present]
  backing_device:
    description:
      - Path to the underlying block device or file, or the UUID of a block-device prefixed with V(UUID=).
    type: str
  password:
    description:
      - Encryption password, the path to a file containing the password, or V(-) or unset if the password should be entered
        at boot.
    type: path
  opts:
    description:
      - A comma-delimited list of options. See V(crypttab(5\)) for details.
    type: str
  path:
    description:
      - Path to file to use instead of V(/etc/crypttab).
      - This might be useful in a chroot environment.
    type: path
    default: /etc/crypttab
author:
  - Steve (@groks)
a  
- name: Set the options explicitly a device which must already exist
  community.general.crypttab:
    name: luks-home
    state: present
    opts: discard,cipher=aes-cbc-essiv:sha256

- name: Add the 'discard' option to any existing options for all devices
  community.general.crypttab:
    name: '{{ item.device }}'
    state: opts_present
    opts: discard
  loop: '{{ ansible_mounts }}'
  when: "'/dev/mapper/luks-' in item.device"
N)AnsibleModule)to_bytes	to_nativec                     t        t        t        dd      t        ddg d      t        d      t        d      t        d      t        dd	      
      d      } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }|j                  d      r|t	        d      d  }|dk7  r$|"| | | j
                  d%ddi| j                   d|v r%||! | j
                  d%dd|z  i| j                   d|fd|fd|fd|ffD ]7  \  }}|	d|v s
d|v s|dk(  s | j
                  d%dd|z  i| j                   9 	 t        |      }	|	j                  |      }
d|v r"
 | | j
                  d%ddi| j                   d\  }}|dk(  r
|
j                         \  }}n|dk(  r;
|
j                  |||      \  }}n	j                  t        d ||||            \  }}nm|dk(  rC
|
j                   j                  |      \  }}nG	j                  t        d ||||            \  }}n%|d k(  r 
|
j                   j                  |      \  }}|r=| j"                  s1t%        |d!      5 }|j'                  t)        	d"#             d d d         | j*                  d%||d$| j                   y # t        $ rH} | j
                  d%dt        |      z  t        j                         d| j                   Y d }~d }~ww xY w# 1 sw Y   }xY w)&NstrT)typerequired)absentopts_absentopts_presentpresent)r   r   choices)r   pathz/etc/crypttab)r   default)namestatebacking_devicepasswordoptsr   )argument_specsupports_check_moder   r   r   r   r   z/dev/mapper/r   msgz>expected one or more of 'backing_device', 'password' or 'opts'z:cannot update 'backing_device' or 'password' when state=%s 	 z.invalid '%s': contains white space or is emptyz*failed to open and parse crypttab file: %s)r   	exceptionr   z,'backing_device' required to add a new entry)F?r   r   wbsurrogate_or_strict)errors)changedr    )r   dictparams
startswithlen	fail_jsonCrypttabmatch	Exceptionr   	traceback
format_excremovesetaddLiner   
check_modeopenwriter   	exit_json)moduler   r   r   r   r   r   arg_nameargcrypttabexisting_lineer$   reasonfs                  n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/crypttab.pymainrA   U   s   540ED:noU+v&5!6?;
 !
F ]]#34N}}Z(H== DMM'"E== D== D~&C'()^38HT\ 	*] 	*!==	* N6(:N 	*Y\aa 	*!==	* "4.+^<%x0!4.* .# Ots{cRiF .!QT\!\ .%}}..LD> t,
 Em38N 	*K 	*!==	* !OGV$+224OGV	)	$+//$OOGV&ll4dNHVZ+[\OGV	.	 $+0044T:OGV&ll4dNHVZ+[\OGV	-	$+0077=OGVv(($ 	FGGHX.CDE	F FBW&BFMMBE  L 	LIIVWLX#,#7#7#9	L=C]]	L 	LL>	F 	Fs$   3K! L5!	L2*=L--L25L>c                   .    e Zd Zg Zd Zd Zd Zd Zd Zy)r+   c                    || _         t        j                   j                  |      st        j                   j                  t        j                   j                  |            s2t        j                  t        j                   j                  |             t        |d      j                          t        |d      5 }|j                         D ]&  }| j                  j                  t        |             ( 	 d d d        y # 1 sw Y   y xY w)Nar)r   osexistsdirnamemakedirsr5   close	readlines_linesappendr3   )selfr   r?   lines       r@   __init__zCrypttab.__init__   s    	ww~~d#77>>"''//$"78BGGOOD12sO!!#$_ 	/ /""4:./	/ 	/ 	/s   ;:C??Dc                 :    | j                   j                  |       y)N)Tz
added line)rL   rM   rN   rO   s     r@   r2   zCrypttab.add   s    4 !    c              #   X   K   | j                   D ]  }|j                         s|  y wN)rL   validrR   s     r@   lineszCrypttab.lines   s'     KK 	Dzz|
	s    **c                 T    | j                         D ]  }|j                  |k(  s|c S  y rU   )rW   r   )rN   r   rO   s      r@   r,   zCrypttab.match   s-    JJL 	DyyD 	 rS   c                     g }| j                   D ]  }|j                  t        |              dj                  |      }t	        |      dk(  r|dz  }|d   dk7  r|dz  }|S )N
r   )rL   rM   r
   joinr)   )rN   rW   rO   r;   s       r@   __str__zCrypttab.__str__   si    KK 	$DLLT#	$99U#x=AHB<4HrS   N)	__name__
__module____qualname__rL   rP   r2   rW   r,   r]   r%   rS   r@   r+   r+      s     F	/"
	rS   r+   c                   8    e Zd Zd	dZd Zd Zd Zd Zd Zd Z	y)
r3   Nc                    || _         || _        || _        || _        t	        |      | _        |L| j                   j                  d      | _         | j                  |      r| j                  |      \  | _        }}}| j                  |||       y )NrZ   )
rO   r   r   r   Optionsr   rstrip_line_valid_split_liner1   )rN   rO   r   r   r   r   s         r@   rP   zLine.__init__   s    		, DM			((.DI%<@<L<LT<R9	>8T40rS   c                     d}|| j                   |k7  r	|| _         d}|| j                  |k7  r	|| _        d}|#t        |      }|| j                  k7  r	|| _        d}|dfS )NFTzupdated line)r   r   rc   r   )rN   r   r   r   r$   s        r@   r1   zLine.set   sv    %$*=*=*O"0DGDMMX$=$DMG4=Dtyy  	&&rS   c                 ~    |j                         r,|j                  d      st        |j                               dvryy)N#)         FT)stripr(   r)   splitrR   s     r@   re   zLine._line_valid   s/    zz|ts3s4::<7HPY7YrS   c                     |j                         }	 |d   }	 |d   }|d   |d   ||fS # t        $ r d }Y w xY w# t        $ r d }Y *w xY w)Nrj   rk   r      )rn   
IndexError)rN   rO   fieldsfield2field3s        r@   rf   zLine._split_line   st    	AYF	AYF q	q	 	  	F	  	F	s   ) : 77AAc                 .    d\  | _         | _        | _        y)N)r   NN)Tzremoved line)rO   r   r   rN   s    r@   r0   zLine.remove  s    4B1	49d1#rS   c                 6    | j                   | j                  yy)NTF)r   r   rv   s    r@   rV   z
Line.valid  s    99 T%8%8%DrS   c                    | j                         r| j                  | j                  g}| j                  | j                  r9| j                  |j                  | j                         n|j                  d       | j                  r$|j                  t        | j                               dj                  |      S | j                  S )Nnoner   )	rV   r   r   r   r   rM   r
   r\   rO   )rN   rr   s     r@   r]   zLine.__str__  s    ::<ii!4!45F}}(DII==,MM$--0MM&)yyc$))n-88F##yyrS   )NNNNN)
r^   r_   r`   rP   r1   re   rf   r0   rV   r]   r%   rS   r@   r3   r3      s%    1'&
 $
rS   r3   c                   f     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
 fd	Z fd
Zd Zd Z xZS )rc   z6opts_string looks like: 'discard,foo=bar,baz=greeble' c                     t         t        |           g | _        |M|j	                  d      D ]8  }|j	                  d      }t        |      dkD  r|d   |d   }}n|d   d }}|| |<   : y y )N,=rp   r   )superrc   rP   itemlistrn   r)   )rN   opts_stringoptkvkv	__class__s         r@   rP   zOptions.__init__  s{    gt%'""((- YYs^r7Q;qE2a5qAqE4qAQ #rS   c                 |    d}t        |      j                         D ]  \  }}|| v r| |   |k7  rd}nd}|| |<    |dfS )NFTzupdated options)rc   items)rN   r   r$   r   r   s        r@   r2   zOptions.add)  s[    K(..0 	DAqDy7a<"GDG	 )))rS   c                 B    d}t        |      D ]  }|| v s| |= d} |dfS )NFTzremoved options)rc   )rN   r   r$   r   s       r@   r0   zOptions.remove4  s<    % 	ADyG	 )))rS   c                     | j                   S rU   )r   rv   s    r@   keyszOptions.keys<  s    }}rS   c                 2    | D cg c]  }| |   	 c}S c c}w rU   r%   rN   keys     r@   valueszOptions.values?  s    %)*cS	***s   c                 6    | D cg c]	  }|| |   f c}S c c}w rU   r%   r   s     r@   r   zOptions.itemsB  s    ,01Sd3i 111s   c                 ,    t        | j                        S rU   )iterr   rv   s    r@   __iter__zOptions.__iter__E  s    DMM""rS   c                 l    || vr| j                   j                  |       t        t        |   ||       y rU   )r   rM   r~   rc   __setitem__)rN   r   valuer   s      r@   r   zOptions.__setitem__H  s-    d?MM  %gt(e4rS   c                 b    | j                   j                  |       t        t        |   |       y rU   )r   r0   r~   rc   __delitem__)rN   r   r   s     r@   r   zOptions.__delitem__M  s$    S!gt(-rS   c                     t        |t              xr3 t        | j                               t        |j                               k(   S rU   )
isinstancerc   sortedr   )rN   objs     r@   __ne__zOptions.__ne__Q  s6    sG,\

1EPSPYPYP[I\1\]]rS   c                     g }| j                         D ]/  \  }}||j                  |       |j                  |d|       1 dj                  |      S )Nr}   r|   )r   rM   r\   )rN   retr   r   s       r@   r]   zOptions.__str__T  sQ    JJL 	-DAqy

1

a+,		-
 xx}rS   )r^   r_   r`   __doc__rP   r2   r0   r   r   r   r   r   r   r   r]   __classcell__)r   s   @r@   rc   rc     s>    @
	**+2#5
.^rS   rc   __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESrF   r.   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   r   rA   objectr+   r3   r&   rc   r^   r%   rS   r@   <module>r      sw    A @1f  
  4 KKC\&v &RK6 K\Ad AH zF rS   