ok

Mini Shell

Direktori : /opt/alt/python37/lib/python3.7/site-packages/clcommon/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/clcommon/__pycache__/clpwd.cpython-37.pyc

B

E!wdJ�@s�ddlmZddlmZddlmZddlZddlZddlZddlmZddl	m
Z
Gdd	�d	�Zddd�Zed
d��Z
dd�Zddd�ZdS)�)�absolute_import)�division)�print_functionN)�contextmanager�)�FormattedExceptionc@s�eZdZdZGdd�de�Zddd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zddd�ZdS) �ClPwdz/etc/login.defscseZdZ�fdd�Z�ZS)zClPwd.NoSuchUserExceptioncs(ttj|�jdd|id�f|�|�dS)NzNo such user (%(user)s)�user)�message�context)�superr�NoSuchUserException�__init__)�selfr	�args�kwargs)�	__class__��?/opt/alt/python37/lib/python3.7/site-packages/clcommon/clpwd.pyrsz"ClPwd.NoSuchUserException.__init__)�__name__�
__module__�__qualname__r�
__classcell__rr)rrr
sr
NcCs8i|_i|_i|_i|_|dkr.|�d�|_n||_dS)Ni�)�
_user_key_map�_uid_key_map�_user_full_map�
_uid_full_map�get_sys_min_uid�_min_uid)rZmin_uidrrrr szClPwd.__init__cCs|��|jS)N)�_load_passwd_databaser)rrrr�
get_user_dict*szClPwd.get_user_dictcCs|��|jS)N)rr)rrrr�get_uid_dict.szClPwd.get_uid_dictcCs|��|jS)N)rr)rrrr�get_user_full_dict2szClPwd.get_user_full_dictcCs|��|jS)N)rr)rrrr�get_uid_full_dict6szClPwd.get_uid_full_dictcCs0y|��|Stk
r*t�|��YnXdS)z*
        Return pw_entry for user
        N)r"�KeyErrorrr
)rr	rrr�get_pw_by_name:szClPwd.get_pw_by_namecCs0y|��|Stk
r*t�|��YnXdS)z7
        Return list of passwd entries for uid
        N)r#r$rr
)r�uidrrr�
get_pw_by_uidCszClPwd.get_pw_by_uidcCs2y|��|jStk
r,t�|��YnXdS)z&
        Returns uid for user
        N)r"�pw_uidr$rr
)rr	rrr�get_uidLsz
ClPwd.get_uidcCs2y|��|jStk
r,t�|��YnXdS)z`
        Returns homedir for a user
        @param user: string
        @return: string
        N)r"�pw_dirr$rr
)rr	rrr�get_homedirUszClPwd.get_homedircCs�|js�x�t��D]|}||j|j<|j|jkr8g|j|j<|j|j�|�|j|jkr||j|j<|j|j	krzg|j	|j<|j	|j�|�qWdS)zZ
        Loads the passwd database and fills user_to_uid and user_to_homedir maps
        N)
r�pwdZgetpwallr�pw_namer(�appendrrr)r�entryrrrr`szClPwd._load_passwd_databasecCs>y|��|}Wntk
r.t�|��YnXdd�|D�S)zs
        Return names of users with uid specified
        @param uid: int
        @return: list of strings
        cSsg|]
}|j�qSr)r-)�.0r/rrr�
<listcomp>{sz#ClPwd.get_names.<locals>.<listcomp>)r#r$rr
)rr&�entriesrrr�	get_namesps
zClPwd.get_names��c	Csvtj�|j�rrt|jd��}|��}WdQRXxB|D]:}|�d�r4yt|�d�d�	��St
k
rlYq4Xq4W|S)z�
        Return system defined MIN_UID from /etc/login.def or def_min_uid
        @param def_min_uid: int
        @return: MIN_UID: int
        �rNZUID_MINr)�os�path�exists�LOGIN_DEF_FILE�open�	readlines�
startswith�int�split�strip�
ValueError)rZdef_min_uid�f�lines�linerrrr}s


zClPwd.get_sys_min_uid)N)r4)rrrr9rr
rr r!r"r#r%r'r)r+rr3rrrrrrs

			
rTcs�t��}|dkr��r�t���}�fdd�t��D�}|j}||krL|�|�t�|�|rtt�	|j�t�
|j�nt�|j�t�
|j�|r��tjd<|jtjd<dS)z�
    Drop current root privileges to user
    :param effective_or_real: if True - drop euid, else - drop ruid
    :param user: name of unix user
    :param set_env: bool -> if true set $HOME and $USER env variables
    :return: None
    rcsg|]}�|jkr|j�qSr)Zgr_memZgr_gid)r0�group)r	rrr1�sz(drop_user_privileges.<locals>.<listcomp>ZUSER�HOMEN)r6�geteuidr,�getpwnam�grpZgetgrallZpw_gidr.�	setgroups�setegid�seteuidr(�setgid�setuid�environr*)r	�effective_or_real�set_env�current_euidZuser_pwdZuser_groupsZmain_user_groupr)r	r�drop_user_privileges�s 



rRc	cs~t��t��t��}}}yt|ddd�Wntk
rLt�|��YnXz
dVWdt�|�t�	|�t�
|�XdS)zk
    Temporary drop privileges to some user
    :type user: str
    :raises: ClPwd.NoSuchUserException
    TF)rOrPN)r6�getuid�getgid�	getgroupsrRr$rr
rKrJrI)r	Zold_uidZold_gidZ
old_groupsrrr�drop_privileges�s


rVcCsl|dkrtd��ddlm}ddlm}||�}y|d\}}Wn&tk
rf|dd|id	���YnX|S)
zw
    Resolve document root by username
    :param user: str -> name of unix user
    :return: str -> document root
    Nz-Cannot resolve docroot without specified userr)�userdomains)�NoDomainrz!No domain for user %(user)s foundr	)r
r)r�cpapirW�cpapi.cpapiexceptionsrX�
IndexError)r	rWrXZdomains_list�_�resultrrr�_resolve_doc_root_by_user�sr^c	Cs�ddlm}ddlm}|}d}t��}|dk	r`||�\}}|dkrJ|}n||krZ|d��|}n2|dk	rtt|d�}n|dkr�t�|�j	}t|d�}||fS)a
    Resolve username and doc_root by domain,
    or resolve document root by username,
    or resolve document root and username by effective uid
    :param user: str -> name of unix user
    :param domain: str -> domain of panel user
    :return: tuple -> user, doc_root
    r)�docroot)�
IncorrectDataNzUser and domain are in conflict)r	r)
rYr_rZr`r6rFr^r,�getpwuidr-)	r	Zdomainr_r`Zresult_userZresult_doc_rootrQZdoc_rootZdomain_userrrr�resolve_username_and_doc_root�s$
rb)NTT)NN)Z
__future__rrrr6r,rH�
contextlibrZclexceptionrrrRrVr^rbrrrr�<module>	s|


Zerion Mini Shell 1.0