
    f+              
          U d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZmZmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZmZmZ ddlmZ ddlmZ dZdddeege ed       ed      gg dZeed<    ee      Z  ej@                  e!      Z"djG                  eez   D  cg c]	  } | dvs|  c}       Z$de%de&de%fdZ'd Z(defdZ)de&ded ed!e%ddf
d"Z*d$d#Z+yc c} w )%zFSet Passwords: Set user passwords and enable/disable SSH password auth    N)ascii_lettersdigits)dedent)List)featuressubputil)Cloud)Config)
MetaSchemaget_meta_doc)ALL_DISTROSDistroug_util)PER_INSTANCE)update_ssh_configa  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.

The ``chpasswd`` config key accepts a dictionary containing either or both of
``users`` and ``expire``. The ``users`` key is used to assign a password to a
corresponding pre-existing user. The ``expire`` key is used to set
whether to expire all user passwords specified by this module,
such that a password will need to be reset on the user's next login.

.. note::
    Prior to cloud-init 22.3, the ``expire`` key only applies to plain text
    (including ``RANDOM``) passwords. Post 22.3, the ``expire`` key applies to
    both plain text and hashed passwords.

``password`` config key is used to set the default user's password. It is
ignored if the ``chpasswd`` ``users`` is used. Note: the ``list`` keyword is
deprecated in favor of ``users``.
cc_set_passwordszSet Passwordsz7Set user passwords and enable/disable SSH password authz            # Set a default password that would need to be changed
            # at first login
            ssh_pwauth: true
            password: password1
            aU              # Disable ssh password authentication
            # Don't require users to change their passwords on next login
            # Set the password for user1 to be 'password1' (OS does hashing)
            # Set the password for user2 to a pre-hashed password
            # Set the password for user3 to be a randomly generated password,
            #   which will be written to the system console
            ssh_pwauth: false
            chpasswd:
              expire: false
              users:
                - name: user1
                  password: password1
                  type: text
                - name: user2
                  password: $6$rounds=4096$5DJ8a9WMTEzIo5J4$Yms6imfeBvf3Yfu84mQBerh18l7OR1Wm1BJXZqFSpJ6BVas0AYJqIjP7czkOaAZHZi1kxQ5Y1IhgWN8K9NgxR1
                - name: user3
                  type: RANDOM
            )idnametitledescriptiondistros	frequencyexamplesactivate_by_schema_keysmeta loLOI01
users_listpw_typereturnc                     | sg S | D cg c].  }|j                  dd      |k(  r|d   |j                  dd      f0 c}S c c}w )zDeither password or type: RANDOM is required, user is always requiredtypehashr   passwordRANDOM)get)r   r    items      C/usr/lib/python3/dist-packages/cloudinit/config/cc_set_passwords.pyget_users_by_typer*   `   sZ      	
 #
xx'72 &\488J9:

s   3?c                     	 | j                  d|       t        j                  d       y # t        j                  $ r }t        j                  d|       Y d }~y d }~ww xY w)NrestartzRestarted the SSH daemon.zm'ssh_pwauth' configuration may not be applied. Cloud-init was unable to restart SSH daemon due to error: '%s')manage_serviceLOGdebugr   ProcessExecutionErrorwarning)distroservicees      r)   _restart_ssh_daemonr5   m   sS    
i1		-.%% 
>	
 	

s   '* AAAr2   c                    |j                  dd      }d}t        | t              rt        j                  ddd       t        j
                  |       rd}nct        j                  |       rd	}nKd
| d}| | j                         dk(  rt        j                  d||        yt        j                  d||        yt        ||i      }|st        j                  d|       y|j                         rSt        j                  ddddd|g      j                  j                         }|j                         dv rt!        ||       yyt!        ||       y)zApply sshd PasswordAuthentication changes.

    @param pw_auth: config setting from 'pw_auth'.
                    Best given as True, False, or "unchanged".
    @param distro: an instance of the distro class for the target distribution

    @return: Nonessh_svcnamesshPasswordAuthenticationz-Using a string value for the 'ssh_pwauth' key22.2z&Use a boolean value with 'ssh_pwauth'.
deprecateddeprecated_versionextra_messageyesnozLeaving SSH config 'z' unchanged.N	unchangedz%s ssh_pwauth=%sz$%s Unrecognized value: ssh_pwauth=%sz/No need to restart SSH service, %s not updated.	systemctlshowz
--propertyActiveStatez--value)active
activating	reloading)
get_option
isinstancestrr	   	deprecateis_trueis_falselowerr.   r/   r1   r   uses_systemdr   stdoutstripr5   )pw_authr2   r3   cfg_namecfg_valbmsgupdatedstates           r)   handle_ssh_pwauthrX   y   s<    u5G'H'3F%B	

 ||G	w	%hZ|<?gmmo<II($8 	 KK>gN7 34G		CXN			
 & 	 ;;=AA0 B 	FG,    r   cfgcloudargsc                 	   |j                   }|r|d   }d|v r%d|d   v r|d   d= nt        j                  |dd       }d}g }g }d|v r|d   }	t        j                  |	dg       }d|	v r|	d   rt        j                  dd	d
       t        |	d   t              r-t        j                  d       t        j                  |	d|      }nUt        j                  ddd       t        j                  d       t        j                  |	d      }
|
r|
j                         }t        j                  |	d|      }|sU|sS|rQt        j                  ||      \  }}t        j                  |      \  }}|r	|d|g}nt        j                  d       g }|s|rAt        |d      }|D cg c]  \  }}|	 }}}t        |d      }|D cg c]  \  }}|	 }}}g }t        |d      D ]I  \  }}t!               }|j#                  |       |j#                  ||f       |j#                  | d|        K t%        j&                  d      }|D ]  }|j)                  dd      \  }}|j+                  |      )d|vr%|j#                  ||f       |j#                  |       R|dk(  s|dk(  r t!               }|j#                  |d|       |j#                  ||f       |j#                  |        |r*	 t        j                  d|       |j-                  |d       |r*	 t        j                  d|       |j-                  |d       t3        |      r.d d!j5                  |      f}t        j6                  d"|z  dd#       |r[|}t8        j:                  r||z  }g }|D ]%  }	 |j=                  |       |j#                  |       ' |rt        j                  d%|       t?        |jA                  d&      |       t3        |      r$t        j                  d't3        |             |d(   y c c}}w c c}}w # t.        $ r7}|j#                  |       t        j0                  t        d|       Y d }~Td }~ww xY w# t.        $ r7}|j#                  |       t        j0                  t        d|       Y d }~kd }~ww xY w# t.        $ r7}|j#                  |       t        j0                  t        d$|       Y d }~Xd }~ww xY w))Nr   chpasswdlistr%   Tusers)defaultzConfig key 'lists'z22.3zUse 'users' instead.r;   z$Handling input for chpasswd as list.zThe chpasswd multiline stringr:   zUse string type instead.z0Handling input for chpasswd as multiline string.expire:z2No default or defined user to change password for.textr$   r&   z\$(1|2a|2y|5|6)(\$.+){2}   RzChanging password for %s:F)hashedz,Failed to set passwords with chpasswd for %szSetting hashed password for %s:z3Failed to set hashed passwords with chpasswd for %sz%Set the following 'random' passwords

z%s
%s
)stderrfallback_to_stdoutzFailed to set 'expire' for %szExpired passwords for: %s users
ssh_pwauthz+%s errors occurred, re-raising the last one)!r2   r	   get_cfg_option_strget_cfg_option_listrK   rI   r_   r.   r/   
splitlinesget_cfg_option_boolr   normalize_users_groupsextract_defaultr1   r*   rand_user_passwordappendrecompilesplitmatchr^   	Exceptionlogexclenjoin	multi_logr   EXPIRE_APPLIES_TO_HASHED_USERSexpire_passwdrX   r'   )r   rZ   r[   r\   r2   r%   rb   plistr   chfg	multiliner`   _groupsuser_user_configerrorsplist_in_hashed_plist_inhashed_usersrandlistproglineupr4   blurbusers_to_expireexpired_userss                                r)   handler      s   \\F73z?!:J'**3
DAFEJS:--dGRH
T>d6lNN/#)4
 $v,-		@A00vuE>'-"<
 		LM 33D&A	%002E))$&A%X"99#vF&66u=| $h/0EKKLMF
 %Z8%-.'$..+J?,;<q<<(X> 	2GD!)+HLLOOT8,-OOtfAhZ01		2 zz56 	 D::c1%DAqzz!}(S\&&1v.##A& 8qH}*,AOOq!$45A'Q	  		5u=7 			;\J= x=8		(#E NNU"5U #O66</M$ II((+!((+I 		;]Kcggl+V4
6{		?VMRj ] /<@  a G   a I  4 ! IMM!$KK%DaHHIsT    P P!%)P' )Q* "R-'	Q'0,Q""Q'*	R*3,R%%R*-	S-6,S((S-c                 8    t        j                  | t              S )N)select_from)r	   rand_strPW_SET)pwlens    r)   rs   rs   5  s    ==F33rY   )   ),__doc__loggingru   stringr   r   textwrapr   typingr   	cloudinitr   r   r	   cloudinit.cloudr
   cloudinit.configr   cloudinit.config.schemar   r   cloudinit.distrosr   r   r   cloudinit.settingsr   cloudinit.ssh_utilr   MODULE_DESCRIPTIONr   __annotations__	getLogger__name__r.   r|   r   r_   rJ   r*   r5   rX   r   rs   )xs   0r)   <module>r      s5   M  	 (   * * ! # < : : + 0 0 F%}	
 		
>  "M'j 'R t
g! 
]V3Jq	7I!J	K
$ 
 
 
	
1-v 1-hE E6 E% Et E EP4q Ks   !	C+C