Current File : //lib/python2.7/site-packages/cloudinit/config/cc_set_passwords.pyo
�
�jbc@s�dZddlZddlZddlmZddlmZddlm	Z	ddlm
Z
ddlmZm
Z
eje�Zdjgee
D]Zed	kr�e^q��Zdd
d�Zd�Zd
d�Zed�ZdS(s�
Set Passwords
-------------
**Summary:** Set user passwords and enable/disable SSH password authentication

This module consumes three top-level config keys: ``ssh_pwauth``, ``chpasswd``
and ``password``.

The ``ssh_pwauth`` config key determines whether or not sshd will be configured
to accept password authentication.  True values will enable password auth,
false values will disable password auth, and the literal string ``unchanged``
will leave it unchanged.  Setting no value will also leave the current setting
on-disk unchanged.

The ``chpasswd`` config key accepts a dictionary containing either or both of
``expire`` and ``list``.

If the ``list`` key is provided, it should contain a list of
``username:password`` pairs.  This can be either a YAML list (of strings), or a
multi-line string with one pair per line.  Each user will have the
corresponding password set.  A password can be randomly generated by specifying
``RANDOM`` or ``R`` as a user's password.  A hashed password, created by a tool
like ``mkpasswd``, can be specified; a regex
(``r'\$(1|2a|2y|5|6)(\$.+){2}'``) is used to determine if a password value
should be treated as a hash.

.. note::
    The users specified must already exist on the system.  Users will have been
    created by the ``cc_users_groups`` module at this point.

By default, all users on the system will have their passwords expired (meaning
that they will have to be reset the next time the user logs in).  To disable
this behaviour, set ``expire`` under ``chpasswd`` to a false value.

If a ``list`` of user/password pairs is not specified under ``chpasswd``, then
the value of the ``password`` config key will be used to set the default user's
password.

**Internal name:** ``cc_set_passwords``

**Module frequency:** per instance

**Supported distros:** all

**Config keys**::

    ssh_pwauth: <yes/no/unchanged>

    password: password1
    chpasswd:
        expire: <true/false>

    chpasswd:
        list: |
            user1:password1
            user2:RANDOM
            user3:password3
            user4:R

    ##
    # or as yaml list
    ##
    chpasswd:
        list:
            - user1:password1
            - user2:RANDOM
            - user3:password3
            - user4:R
            - user4:$6$rL..$ej...
i����N(tug_util(tlog(tupdate_ssh_config(tutil(t
ascii_letterstdigitsttloLOI01tsshcCs)d}|d	krdg}ntj|�r6d}nmtj|�rNd}nUd|}|d	ksv|j�dkr�tjd||�ntjd||�d	Sti||6�}|s�tjd
|�d	Sd|kr�t	|�d|g}nt	|�|dg}tj
|�tjd
�d	S(s5Apply sshd PasswordAuthentication changes.

    @param pw_auth: config setting from 'pw_auth'.
                    Best given as True, False, or "unchanged".
    @param service_cmd: The service command list (['service'])
    @param service_name: The name of the sshd service for the system.

    @return: NonetPasswordAuthenticationtservicetyestnos"Leaving ssh config '%s' unchanged.t	unchangeds%s ssh_pwauth=%ss$%s Unrecognized value: ssh_pwauth=%sNs/No need to restart ssh service, %s not updated.t	systemctltrestartsRestarted the ssh daemon.(tNoneRtis_truetis_falsetlowertLOGtdebugtwarningRtlisttsubp(tpw_authtservice_cmdtservice_nametcfg_nametcfg_valtbmsgtupdatedtcmd((sE/usr/lib/python2.7/site-packages/cloudinit/config/cc_set_passwords.pythandle_ssh_pwauthas*			

cCs�t|�dkrI|d}d|kr^d|dkr^|dd=q^ntj|dd�}t}d}d|kr |d}d|kr|drt|dt�r�|jd�tj|d|�}q|jd�tj|d|�}|r|j	�}qntj
|d|�}n|r�|r�tj||j
�\}	}
tj|	�\}}|ryd||fg}q�|jd	�ng}
|rEg}g}g}g}g}	tjd
�}x�|D]�}|jdd�\}}|j|�dk	r%d|kr%|j|�|j|�q�|d
ks=|dkr`t�}|jd||f�n|jd||f�|	j|�q�Wdj|�d}|	r�y$|jd|	�t|j
|�Wq�tk
r�}|
j|�tj|d|	�q�Xndj|�d}|rzy*|jd|�t|j
|dt�Wqztk
rv}|
j|�tj|d|�qzXnt|�r�ddj|�f}tjjd|�n|rEg}xe|	D]]}y!|j
j|�|j|�Wq�tk
r!}|
j|�tj|d|�q�Xq�W|rB|jd|�qBqEnt|jd�d|j
j d|j
j!dd��t|
�r�|jdt|
��|
d�ndS( NitchpasswdRtpasswords$Handling input for chpasswd as list.s0Handling input for chpasswd as multiline string.texpires%s:%ss2No default or defined user to change password for.s\$(1|2a|2y|5|6)(\$.+){2}t:itRtRANDOMs
sChanging password for %s:s,Failed to set passwords with chpasswd for %ssSetting hashed password for %s:thasheds3Failed to set hashed passwords with chpasswd for %ss%Set the following 'random' passwords
s%s
%s
sFailed to set 'expire' for %ssExpired passwords for: %s userst
ssh_pwauthRRtssh_svcnameRs*%s errors occured, re-raising the last onei����("tlenRtget_cfg_option_strRtTruet
isinstanceRRtget_cfg_option_listt
splitlinestget_cfg_option_boolRtnormalize_users_groupstdistrotextract_defaultRtretcompiletsplittmatchtappendtrand_user_passwordtjoinR"t	Exceptiontlogexctsyststderrtwritet
expire_passwdR!tgettinit_cmdt
get_option(t_nametcfgtcloudRtargsR#R$tplisttchfgtuserst_groupstusert_user_configterrorstplist_inthashed_plist_inthashed_userstrandlisttprogtlinetutptch_intethashed_ch_intblurbt
expired_users((sE/usr/lib/python2.7/site-packages/cloudinit/config/cc_set_passwords.pythandle�s�





!
	



icCstj|dt�S(Ntselect_from(Rtrand_strtPW_SET(tpwlen((sE/usr/lib/python2.7/site-packages/cloudinit/config/cc_set_passwords.pyR:�scCs�tj�rQxn|j�D]1}|jd�\}}|j||d|�qWn,dg|rfdgng}tj||�dS(NR%R(R"s-e(Rt
is_FreeBSDR0R7t
set_passwdR(R3RPR(tpentryRVRWR ((sE/usr/lib/python2.7/site-packages/cloudinit/config/cc_set_passwords.pyR"�s(t__doc__R5R>tcloudinit.distrosRt	cloudinitRtloggingtcloudinit.ssh_utilRRtstringRRt	getLoggert__name__RR;txR`RR!R]R:tFalseR"(((sE/usr/lib/python2.7/site-packages/cloudinit/config/cc_set_passwords.pyt<module>Ns&	h