o
    x[hv	  ã                   @   s<   d dl Z d dlZd dlmZmZ e  e¡ZG dd„ dƒZdS )é    N)Úatomic_helperÚsubpc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚPasswordConfiguratorzv
    Class for changing configurations related to passwords in a VM. Includes
    setting and expiring passwords.
    c                 C   s¼   t  d¡ |rt |¡}g }tddƒD ]}| d¡d dkr(| | d¡d ¡ qg }tddƒD ]}| d¡d }||v rB| |¡ q0|rPt  d	¡ | d
|¡ |rW|  |¡ t  d¡ dS )a+  
        Main method to perform all functionalities based on configuration file
        inputs.
        @param passwd: encoded admin password.
        @param resetPasswd: boolean to determine if password needs to be reset.
        @return cfg: dict to be used by cloud-init set_passwd code.
        zStarting password configurationz/etc/passwdÚrú:é   Ú0r   z/etc/shadowzSetting admin passwordÚrootzConfigure Password completed!N)	ÚLOGÚinfor   Úb64dÚopenÚsplitÚappendÚ
set_passwdÚreset_password)ÚselfÚpasswdÚresetPasswdÚdistroÚallRootUsersÚlineÚuidUsersListÚuser© r   úT/usr/lib/python3/dist-packages/cloudinit/sources/helpers/vmware/imc/config_passwd.pyÚ	configure   s(   

€
€

zPasswordConfigurator.configurec                 C   s€   t  d¡ |D ]6}z
t dd|g¡ W q tjy= } ztj d¡r,t ddd|g¡ nt  d||¡ W Y d	}~qd	}~ww d	S )
zÜ
        Method to reset password. Use passwd --expire command. Use chage if
        not succeeded using passwd command. Log failure message otherwise.
        @param: list of users for which to expire password.
        zExpiring password.r   z--expirez/usr/bin/chageÚchagez-dr   z/Failed to expire password for %s with error: %sN)r
   r   r   ÚProcessExecutionErrorÚosÚpathÚexistsÚwarning)r   ÚuidUserListr   Úer   r   r   r   3   s   
ý€üýz#PasswordConfigurator.reset_passwordN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s    r   )	Úloggingr   Ú	cloudinitr   r   Ú	getLoggerr%   r
   r   r   r   r   r   Ú<module>   s
   
