HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/useradd.cpython-310.pyc
o

�N�gkZ�@s�dZddlZddlZddlZddlZddlZddlZddlZddl	Zddl
ZddlZddlm
Z
zddlZdZWneyCdZYnwe�e�ZdZdd�Zd	d
�ZdEdd�Zd
d�Zdd�ZdEdd�Z																	dFdd�ZdGdd�ZdHdd�ZdHdd�ZdEdd�ZdEdd�Z dEd d!�Z!dHd"d#�Z"dHd$d%�Z#dEd&d'�Z$dEd(d)�Z%dEd*d+�Z&dEd,d-�Z'dEd.d/�Z(dEd0d1�Z)dEd2d3�Z*d4d5�Z+d6d7�Z,ej-j.j/�0d8�d9d:��Z1d;d<�Z2dEd=d>�Z3dEd?d@�Z4dEdAdB�Z5dEdCdD�Z6dS)Ia$
Manage users with the useradd command

.. important::
    If you feel that Salt should be using this module to manage users on a
    minion, and it is using a different module (or gives an error similar to
    *'user.info' is not available*), see :ref:`here
    <module-provider-override>`.
�N)�CommandExecutionErrorTF�usercCstr
tddvr
tSdS)zL
    Set the user module if the kernel is Linux, OpenBSD, NetBSD or AIX
    �kernel)�Linux�OpenBSD�NetBSD�AIX)Fzuseradd execution module not loaded: either pwd python library not available or system not one of Linux, OpenBSD, NetBSD or AIX)�HAS_PWD�
__grains__�__virtualname__�rr�H/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/useradd.py�__virtual__&srcCs t|t�s	t|�Stjj�|�S)zS
    Usernames can only contain ascii chars, so make sure we return a str type
    )�
isinstance�str�salt�utils�stringutilsZto_str��namerrr
�_quote_username4s
rcCs�|durtddkrtjt|d�}nt�tj�}tjj�	|t
|��j��dd�}|s,iSt
|�dkr=|�d�t
|�dks2tjj�|d	�tjj�|d
�tjj�|d�tjj�|d�tjj�|d�d
�S)zD
    Retrieve GECOS field info and return it in dictionary form
    Nrr��root�,���r���)�fullname�
roomnumber�	workphone�	homephone�other)r
�	functools�partial�	_getpwnam�pwd�getpwnamrrr�
to_unicoder�pw_gecos�split�len�append�data�decode)rrr)�gecos_fieldrrr
�
_get_gecos>s&��
��r2c
Cs@d�|�dd�|�dd�|�dd�|�dd�|�dd���d�S)	z�
    Accepts a dictionary entry containing GECOS field names and their values,
    and returns a full GECOS comment string, to be used with usermod.
    z{},{},{},{},{}r rr!r"r#r$r)�format�get�rstrip)Z
gecos_dictrrr
�_build_gecosYs




��r6cCs&tjj�|�}|std|�d���|S)zO
    Utility function wrapper to error out early if a command is not found
    z	Command 'z' cannot be found)rr�path�whichr)�cmdZ_cmdrrr
�_whichgsr:cCs�|durd}nt|t�st|�}ntjj�|�}t||d�}|s"dS|||kr*dSt�|�}|||<t	d�g}|durIt
ddkrI|�d	|f�|�d
t|�|f�t
d|dd�t||d��|�|kS)
z:
    Common code to change a user's GECOS information
    NrrFT�usermodrr�-Rz-c�cmd.run�Zpython_shell)rrrrrr*r2�copy�deepcopyr:r
�extendr6�__salt__r4)r�key�valuer�pre_infoZ
gecos_datar9rrr
�
_update_gecosqs$



rFrcCsdtd�g}|r|�d|g�|dvr|�d|g�|dvr%|�d|g�n�|r8|�d�tddkr7t�d	�n�|d
ur�||vr�d}tddkr�z=tjj�	|��,}|D]!}tjj
�|�}d
|d
d�vreqSd|vrt|�dtd|�g�Wd
�n1swYWnWt
y�tjd|tjd�YnFwd}z9tjj�	|��(}|D]}tjj
�|�}d|d
d�vr�q�|�d|��dg�Wd
�n1s�wYWn	t
y�Ynw|dur�|�d�|
r�|�d�ntddkr�tddkr�|�d�|�r|�d�|d
u�r|�d|g�|�s"tddk�r"|�d�|�r8tddk�r8tddk�r8|�d �tddk�rK|d
u�rK|�d!|g�|�|�|d
u�rctddk�rc|�d"|f�td#|dd$�}|d%d&k�rtdS|�r~t|||d'�|�r�t|||d'�|	�r�t||	|d'�|
�r�t||
|d'�|�r�t|||d'�|�r�t|||d'�d(S))a�
    Add a user to the minion

    name
        Username LOGIN to add

    uid
        User ID of the new account

    gid
        Name or ID of the primary group of the new account

    groups
        List of supplementary groups of the new account

    home
        Home directory of the new account

    shell
        Login shell of the new account

    unique
        If not True, the user account can have a non-unique UID

    system
        Create a system account

    fullname
        GECOS field for the full name

    roomnumber
        GECOS field for the room number

    workphone
        GECOS field for the work phone

    homephone
        GECOS field for the home phone

    other
        GECOS field for other information

    createhome
        Create the user's home directory

    loginclass
        Login class for the new account (OpenBSD)

    nologinit
        Do not add the user to the lastlog and faillog databases

    root
        Directory to chroot into

    usergroup
        Create and add the user to a new primary group of the same name

    CLI Example:

    .. code-block:: bash

        salt '*' user.add name <uid> <gid> <groups> <home> <shell>
    Zuseradd�-s)Nr�-u�-gz-Urrz1'usergroup' is only supported on GNU/Linux hosts.Nz/etc/login.defsrZUSERGROUPS_ENAB�Zyeszfile.group_to_gidzError reading %s)Zexc_info_on_loglevelz/etc/usermgmt.conf�groupr���Fz-N�-mrz-M�-l�-drz-o�-r�-Lr<�cmd.run_allr>�retcoderrT)r:rAr.r
�log�warningrr�files�fopenrr*rB�OSError�debug�logging�DEBUGr,�chgroups�
chfullname�chroomnumber�chworkphone�chhomephone�chother)r�uid�gid�groups�home�shell�unique�systemr r!r"r#r$Z
createhome�
loginclassZ	nologinitrZ	usergroupr9Z	defs_file�fp_�lineZ
usermgmt_file�retrrr
�add�s�
S

����
�����
�




�



rmcCs�td�g}|r|�d�|rtddkrtddkr|�d�|�|�|dur5tddkr5|�d|f�td	|d
d�}|dd
krEdS|ddkrftddvrSd
Sd|dvs_d|dvrft�d�dSd
S)aV
    Remove a user from the minion

    name
        Username to delete

    remove
        Remove home directory and mail spool

    force
        Force some actions that would fail otherwise

    root
        Directory to chroot into

    CLI Example:

    .. code-block:: bash

        salt '*' user.delete name remove=True force=True
    ZuserdelrPrrrz-fNr<rRFr>rSrT�Z	os_family)ZDebianzvar/mail�stderrzvar/spool/mailzrWhile the userdel exited with code 12, this is a known bug on debian based distributions. See http://goo.gl/HH3FzT)r:r.r
rArBrTrY)r�remove�forcerr9rlrrr
�deletePs(



�rrcCsndtvr
|s
tdSg}|durtddkrtjt|d�}nt�tj�}|�D]	}|�t|��q'|td<|S)z�
    Return the list of all info for all users

    refresh
        Force a refresh of user information

    root
        Directory to chroot into

    CLI Example:

    .. code-block:: bash

        salt '*' user.getent
    zuser.getentNrrr)	�__context__r
r%r&�	_getpwallr(�getpwallr.�_format_info)Zrefreshrrlrur/rrr
�getent�s
rwcCs�t||d�}|std|�d���|||krdStd�g}|dur.tddkr.|�d	|f�|r;tdd
kr;|�d�|�|||f�td|d
d�t||d��|�|kS)z.
    Change an attribute for a named user
    r�User 'z' does not existTr;Nrrr<rrMr=Fr>)�inforr:r
rAr.rBr4)rrCrD�param�persistrrEr9rrr
�	_chattrib�s

r|cC�t|d|d|d�S)z�
    Change the uid for a named user

    name
        User to modify

    uid
        New UID for the user account

    root
        Directory to chroot into

    CLI Example:

    .. code-block:: bash

        salt '*' user.chuid foo 4376
    rbrHr�r|)rrbrrrr
�chuid��rcCr})a
    Change the default group of the user

    name
        User to modify

    gid
        Force use GID as new primary group

    root
        Directory to chroot into

    CLI Example:

    .. code-block:: bash

        salt '*' user.chgid foo 4376
    rcrIrr~)rrcrrrr
�chgid�r�r�cCr})a

    Change the default shell of the user

    name
        User to modify

    shell
        New login shell for the user account

    root
        Directory to chroot into

    CLI Example:

    .. code-block:: bash

        salt '*' user.chshell foo /bin/zsh
    rfrGrr~)rrfrrrr
�chshell�r�r�cCst|d|d||d�S)a�
    Change the home directory of the user, pass True for persist to move files
    to the new home directory if the old home directory exist.

    name
        User to modify

    home
        New home directory for the user account

    persist
        Move contents of the home directory to the new location

    root
        Directory to chroot into

    CLI Example:

    .. code-block:: bash

        salt '*' user.chhome foo /home/users/foo True
    rerO)r{rr~)rrer{rrrr
�chhomesr�c	Csrt|t�r
|�d�}tt|��}|t|�krdStd�g}tddkr6|r0tddkr0|�d�|�d�n
|r>|�d�n|�d	�|r]tddkr]|�d�	|�dd�	|�|g�n
|�d�	|�|g�|d
urxtddkrx|�d|f�t
d|d
d�}tddkr�tddkr�|ddkr�d|dvr�d}|D]}dd||g}t
d|d
d�dkr�d
}q�|S|ddkS)a�
    Change the groups to which this user belongs

    name
        User to modify

    groups
        Groups to set for the user

    append : False
        If ``True``, append the specified group(s). Otherwise, this function
        will replace the user's groups with the specified group(s).

    root
        Directory to chroot into

    CLI Examples:

    .. code-block:: bash

        salt '*' user.chgroups foo wheel,root
        salt '*' user.chgroups foo wheel,root append=True
    rTr;rrrz-az-Gz-SNr<rRFr>rSrznot found inroZgpasswdzcmd.retcode)rrr,�set�list_groupsr:r
r.rA�joinrB)	rrdr.rZugrpsr9�resultrlrKrrr
r\s:




$�r\cC�t|d||d�S)a
    Change the user's Full Name

    name
        User to modify

    fullname
        GECOS field for the full name

    root
        Directory to chroot into

    CLI Example:

    .. code-block:: bash

        salt '*' user.chfullname foo "Foo Bar"
    r r�rF)rr rrrr
r]_�r]cCr�)z
    Change the user's Room Number

    CLI Example:

    .. code-block:: bash

        salt '*' user.chroomnumber foo 123
    r!rr�)rr!rrrr
r^us
r^cCr�)a
    Change the user's Work Phone

    name
        User to modify

    workphone
        GECOS field for the work phone

    root
        Directory to chroot into

    CLI Example:

    .. code-block:: bash

        salt '*' user.chworkphone foo 7735550123
    r"rr�)rr"rrrr
r_�r�r_cCr�)a
    Change the user's Home Phone

    name
        User to modify

    homephone
        GECOS field for the home phone

    root
        Directory to chroot into

    CLI Example:

    .. code-block:: bash

        salt '*' user.chhomephone foo 7735551234
    r#rr�)rr#rrrr
r`�r�r`cCr�)a
    Change the user's other GECOS attribute

    name
        User to modify

    other
        GECOS field for other information

    root
        Directory to chroot into

    CLI Example:

    .. code-block:: bash

        salt '*' user.chother foobar
    r$rr�)rr$rrrr
ra�r�racCsntddkrdS|t|�krdStd�d||g}|dur)tddkr)|�d	|f�td
|dd�t|�|kS)aX
    Change the default login class of the user

    name
        User to modify

    loginclass
        Login class for the new account

    root
        Directory to chroot into

    .. note::
        This function only applies to OpenBSD systems.

    CLI Example:

    .. code-block:: bash

        salt '*' user.chloginclass foo staff
    rrFTr;rQNrr<r=r>)r
�get_loginclassr:rArB)rrirr9rrr
�chloginclass�sr�cCs^|durtddkrtjt|d�}nt�tj�}z|t|��}Wt|�Sty.iYSw)z�
    Return user information

    name
        User to get the information

    root
        Directory to chroot into

    CLI Example:

    .. code-block:: bash

        salt '*' user.info root
    Nrrr)	r
r%r&r'r(r)r�KeyErrorrv)rrr)r/rrr
ry�s��ryc
CsttddkrdStdd|gdd�}|��D]}|�d�r5z|�dd	�d	}W|Sttfy4Yqwqd
}|S)z�
    Get the login class of the user

    name
        User to get the information

    .. note::
        This function only applies to OpenBSD systems.

    CLI Example:

    .. code-block:: bash

        salt '*' user.get_loginclass foo
    rrFzcmd.run_stdout�userinfor>�classNrr)r
rB�
splitlines�
startswithr,�
ValueError�
IndexError)rr�rkrlrrr
r�
s
���r�c
Cs~tjj�|j��dd�}t|�dkr|�d�t|�dks|jt	|j
�|j|j
|j|j
|j|d|d|d|d|dd	�S)
z1
    Return user information in a pretty way
    rrrrrrrr)rcrdrer�passwdrfrbr r!r"r#r$)rrrr*r+r,r-r.�pw_gidr��pw_name�pw_dir�	pw_passwd�pw_shell�pw_uid)r/r1rrr
rv,s"
��rv�idcCstdddd|g�S)z�
    Return the primary group of the named user

    .. versionadded:: 2016.3.0

    name
        User to get the information

    CLI Example:

    .. code-block:: bash

        salt '*' user.primary_group saltadmin
    r=r�rIz-n)rBrrrr
�
primary_groupFsr�cCstjj�|�S)z�
    Return a list of groups the named user belongs to

    name
        User to get the information

    CLI Example:

    .. code-block:: bash

        salt '*' user.list_groups foo
    )rrrZget_group_listrrrr
r�Ys
r�cCsD|durtddkrtjt|d�}nt�tj�}tdd�|�D��S)z�
    Return a list of all users

    root
        Directory to chroot into

    CLI Example:

    .. code-block:: bash

        salt '*' user.list_users
    Nrrrcss�|]}|jVqdS�N)r�)�.0rrrr
�	<genexpr>{s�zlist_users.<locals>.<genexpr>)r
r%r&rtr(ru�sorted)rrurrr
�
list_usersis
r�cCs.t||d�rtd|�d���t|d|d|d�S)a
    Change the username for a named user

    name
        User to modify

    new_name
        New value of the login name

    root
        Directory to chroot into

    CLI Example:

    .. code-block:: bash

        salt '*' user.rename name new_name
    rrxz' already existsrrN)ryrr|)r�new_namerrrr
�rename~sr�cCs�|sdn|}tj�|d�}tjj�|��A}|D]5}tjj�|�}|�	��
d�}|d|krLt|d�t|d�|d<|d<t�
|�Wd�SqWd�t�1sXwYt�)zL
    Alternative implementation for getpwnam, that use only /etc/passwd
    �/�
etc/passwd�:rrrN)�osr7r�rrrVrWrr*�stripr,�intr(�
struct_passwdr�)rrr�rjrk�compsrrr
r'�s "��
��r'ccs��|sdn|}tj�|d�}tjj�|��3}|D]'}tjj�|�}|�	��
d�}t|d�t|d�|d<|d<t�
|�VqWd�dS1sKwYdS)zL
    Alternative implemetantion for getpwall, that use only /etc/passwd
    r�r�r�rrN)r�r7r�rrrVrWrr*r�r,r�r(r�)rr�rjrkr�rrr
rt�s�"�"�rtr�)NNNNNTFrrrrrTNFNN)FFN)FN)7�__doc__r?r%rZr�Zsalt.utils.datarZsalt.utils.decorators.pathZsalt.utils.filesZsalt.utils.pathZsalt.utils.stringutilsZsalt.utils.userZsalt.exceptionsrr(r	�ImportError�	getLogger�__name__rTrrrr2r6r:rFrmrrrwr|rr�r�r�r\r]r^r_r`rar�ryr�rvrZ
decoratorsr7r8r�r�r�r�r'rtrrrr
�<module>s�
�





�
D
;






@





%$