
    Vh+                     |    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
Z
d dlmZ d dlmZ d
dZd Zed	k(  r e        yy)    )absolute_importdivisionprint_functiona
  
module: pam_limits
author:
  - "Sebastien Rohaut (@usawa)"
short_description: Modify Linux PAM limits
description:
  - The M(community.general.pam_limits) module modifies PAM limits.
  - The default file is V(/etc/security/limits.conf).
  - For the full documentation, see C(man 5 limits.conf).
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
    version_added: 2.0.0
  diff_mode:
    support: full
    version_added: 2.0.0
options:
  domain:
    type: str
    description:
      - A username, @groupname, wildcard, UID/GID range.
    required: true
  limit_type:
    type: str
    description:
      - Limit type, see C(man 5 limits.conf) for an explanation.
    required: true
    choices: ["hard", "soft", "-"]
  limit_item:
    type: str
    description:
      - The limit to be set.
    required: true
    choices:
      - "core"
      - "data"
      - "fsize"
      - "memlock"
      - "nofile"
      - "rss"
      - "stack"
      - "cpu"
      - "nproc"
      - "as"
      - "maxlogins"
      - "maxsyslogins"
      - "priority"
      - "locks"
      - "sigpending"
      - "msgqueue"
      - "nice"
      - "rtprio"
      - "chroot"
  value:
    type: str
    description:
      - The value of the limit.
      - Value must either be V(unlimited), V(infinity) or V(-1), all of which indicate no limit, or a limit of 0 or larger.
      - Value must be a number in the range -20 to 19 inclusive, if O(limit_item) is set to V(nice) or V(priority).
      - Refer to the C(man 5 limits.conf) manual pages for more details.
    required: true
  backup:
    description:
      - Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered
        it incorrectly.
    required: false
    type: bool
    default: false
  use_min:
    description:
      - If set to V(true), the minimal value will be used or conserved.
      - If the specified value is inferior to the value in the file, file content is replaced with the new value, else content
        is not modified.
    required: false
    type: bool
    default: false
  use_max:
    description:
      - If set to V(true), the maximal value will be used or conserved.
      - If the specified value is superior to the value in the file, file content is replaced with the new value, else content
        is not modified.
    required: false
    type: bool
    default: false
  dest:
    type: str
    description:
      - Modify the limits.conf path.
    required: false
    default: "/etc/security/limits.conf"
  comment:
    type: str
    description:
      - Comment associated with the limit.
    required: false
    default: ''
notes:
  - If O(dest) file does not exist, it is created.
a-  
- name: Add or modify nofile soft limit for the user joe
  community.general.pam_limits:
    domain: joe
    limit_type: soft
    limit_item: nofile
    value: 64000

- name: Add or modify fsize hard limit for the user smith. Keep or set the maximal value
  community.general.pam_limits:
    domain: smith
    limit_type: hard
    limit_item: fsize
    value: 1000000
    use_max: true

- name: Add or modify memlock, both soft and hard, limit for the user james with a comment
  community.general.pam_limits:
    domain: james
    limit_type: '-'
    limit_item: memlock
    value: unlimited
    comment: unlimited memory lock for james

- name: Add or modify hard nofile limits for wildcard domain
  community.general.pam_limits:
    domain: '*'
    limit_type: hard
    limit_item: nofile
    value: 39693561
N)AnsibleModule)	to_nativec                    |dv r9	 dt        |      cxk  xr dk  nc }|s| j                  |d|d|d       y y |d	v s-|j                         s| j                  |d|d|d
       y y y # t        $ r d}Y ^w xY w)Nnicepriorityi   Fz
 Value of z
 for item z is invalid. Value must be a number in the range -20 to 19 inclusive. Refer to the limits.conf(5) manual pages for more details.msg	unlimitedinfinityz-1z is invalid. Value must either be 'unlimited', 'infinity' or -1, all of which indicate no limit, or a limit of 0 or larger. Refer to the limits.conf(5) manual pages for more details.)int
ValueError	fail_jsonisdigit)moduleitemvalueprefixvalids        p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/pam_limits.py_assert_is_valid_valuer      s    ##	3u:++E aginpt"v w  66%--/06tE 	F ;J6  	E	s   A0 0A>=A>c                  4   g d} g d}d}t        t        t        dd      t        dd|      t        dd|       t        dd      t        dd	
      t        dd	
      t        dd	
      t        |d
      t        ddd      	      d      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }	|j                  d   }
|j                  d   }|j                  d   }d}d}t        j                  j                  |      r:t        j                  |t        j                        s|j                  d|z         n|t        j                  j                  |      }t        j                  j                  |      r)t        j                  |t        j                        rd}d}n|j                  d|z         |r|	r|j                  d       t        |||       |
r|j                  |      }t        j                  d      }|rg }n t        |d      5 }t!        |      }d d d        d}t#        j$                  d      }d}|}D ]  }t'        |d !      }|j)                  d"      r|j+                  |       4t        j,                  |d#|      j/                         }|s|j+                  |       m|j1                  d"d$      d%   }	 |j1                  d"d$      d$   }|j5                         }|s|}|j1                  d#      }t7        |      d&k7  r|j+                  |       |d%   }|d$   }|d'   }|d(   }t        |||d)|z  *       ||k(  r||k(  r||k(  rd}||k(  r|}|j+                  |       .|d+vr	|d,v }|d,v } ndx}} |r1|r|}n,| r|}n't9        t;        t=        |      t=        |                  }|	r8|r| r|}n1|r|}n,| r|}n't9        t?        t=        |      t=        |                  }||k7  r8d}|rd-|z   }|d.z   |z   d.z   |z   d.z   |z   |z   d/z   }!|!}|j+                  |!       |}|j+                  |       |j+                  |        |s6d}|rd-|z   }|d.z   |z   d.z   |z   d.z   |z   |z   d/z   }!|!}|j+                  |!       |jA                          t        |jB                  d0      5 }"|"jE                         }#d d d        |jF                  sn|rt        |d1      5  	 d d d        |jI                  t        j                  jK                  |jB                        t        j                  jK                  |             	 |jM                          t        ||t        d2jO                  |      #3      4      }$|
r|$d5<    |jP                  d6i |$ y # 1 sw Y   ~xY w# t2        $ r d}Y w xY w# 1 sw Y   xY w# 1 sw Y   xY w# t2        $ r Y w xY w)7N)coredatafsizememlocknofilerssstackcpunprocas	maxloginsmaxsysloginsr   locks
sigpendingmsgqueuer
   rtpriochroot)softhard-z/etc/security/limits.confTstr)requiredtype)r3   r4   choicesFbool)defaultr4    )r3   r7   r4   )	domain
limit_type
limit_itemr   use_maxuse_minbackupdestcomment)argument_specsupports_check_moder9   r:   r;   r   r<   r=   r>   r?   r@   z%s is not writable. Use sudor   zFdirectory %s is not writable (check presence, access rights, use sudo)z0Cannot use use_min and use_max at the same time.z\s+rbzw+)modesurrogate_or_strict)errors#    r            z$Invalid configuration found in '%s'.)r   r	   r   z	#	
ra    )beforeafter)changedr   diffbackup_file ))r   dictparamsospathisfileaccessW_OKr   dirnameisdirr   backup_localrecompileopenlisttempfileNamedTemporaryFiler   
startswithwritesubstripsplit	Exceptionrstriplenr2   maxr   minflushnameread
check_modeatomic_moveabspathclosejoin	exit_json)%	pam_items	pam_typeslimits_confr   r9   r:   r;   r   r<   r=   r>   new_commentrT   does_not_existlimits_conf_dirrV   space_patternlinesfmessagenffound	new_valuelinenewlineold_commentline_fieldsline_domain	line_type	line_itemactual_valueactual_value_unlimitedvalue_unlimited	new_limitcontentcontent_newres_argss%                                        r   mainr      s   GI &I-KE2TyITyI51V4V4F3k6%%@

 !F ]]8$F|,J|,JMM'"EmmI&GmmI&G]]8$F--'K--	*KGN	ww~~k"yybgg.!?+!MN''//+677==)bii.Q!NG!il{!{|7OP6:u5 ))+6JJv&M+t$ 	GE	 G		$	$$	/BEI Q&;<??3HHTN&&T288:HHTN --Q'*	**S!,Q/K .."%KmmC({q HHTN!!nN	N	"1~vy,&L{&Z	\ & Y*%<jAXE$ 44)59X)X&"'+J"J;@@&) ,I$ %I #CE
C4E$F GI)o ,I+ %I$ ,I #CE
C4E$F GI L("'+"5K"TMJ6=
JTQT]]`kknrr	##HHTNcQf +-KTMJ.5
BTIIUXccfjj	
HHJ	bggs	 %wlln% k3'  	277??2773RWW__[5QR

 %<H "-F x A	 	2  	K	\% %
   sH   WW!.W3!W?X W!W0/W03W<?X	XX__main__)r8   )
__future__r   r   r   r4   __metaclass__DOCUMENTATIONEXAMPLESrZ   rb   rf   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   r   r   __name__rW   rQ   r   <module>r      sV    A @dL@ 
 	  4 AF}!@ zF rQ   