jbc@sdZddlZddlZddlmZddlmZddlm Z ddlm Z ddl m Z m Z ejeZdjge e D]Zed kre^qZdd d Zd Zd dZedZdS(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: password: password1 chpasswd: expire: 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... iN(tug_util(tlog(tupdate_ssh_config(tutil(t ascii_letterstdigitsttloLOI01tsshcCs)d}|d krdg}ntj|r6d}nmtj|rNd}nUd|}|d ksv|jdkrtjd||ntjd||d Sti||6}|stjd |d Sd |krt |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_autht service_cmdt service_nametcfg_nametcfg_valtbmsgtupdatedtcmd((sE/usr/lib/python2.7/site-packages/cloudinit/config/cc_set_passwords.pythandle_ssh_pwauthas*        c Cst|dkrI|d}d|kr^d|dkr^|dd=q^ntj|dd}t}d}d|kr |d}d|kr|drt|dtr|jdtj|d|}q|jdtj|d|}|r|j }qntj |d|}n| r|rt j ||j \} } t j| \} } | ryd| |fg}q|jd ng} |rEg}g}g}g}g} tjd }x|D]}|jd d \}}|j|dk r%d |kr%|j||j|q|d ks=|dkr`t}|jd||fn|jd||f| j|qWdj|d}| ry$|jd| t|j |Wqtk r}| j|tj|d| qXndj|d}|rzy*|jd|t|j |dtWqztk rv}| j|tj|d|qzXnt|rddj|f}tjjd|n|rEg}xe| D]]}y!|j j||j|Wqtk r!}| j|tj|d|qXqW|rB|jd|qBqEnt|jdd|j j d|j j!ddt| r|jdt| | dndS( 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_pwauthRRt ssh_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.pythandles       !        icCstj|dtS(Nt select_from(Rtrand_strtPW_SET(tpwlen((sE/usr/lib/python2.7/site-packages/cloudinit/config/cc_set_passwords.pyR:scCstjrQxn|jD]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.pytNs  & h