
"T`                 @   s   d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z d  d l m Z e j	 e
  Z Gd d   d e j j j  Z Gd d   d e  Z d S)	    N)log)subp)utilc                   s   e  Z d  Z d Z d Z d g Z   f d d   Z d d   Z d d	   Z d
 d d  Z	 d d   Z
 d d   Z d d   Z d d d  Z d d   Z d d   Z d d   Z   S)NetBSDzS
    Distro subclass for NetBSD.

    (N.B. OpenBSD inherits from this class.)
    z*/usr/pkg/etc/sudoers.d/90-cloud-init-usersZgroupaddc                s   t    j | | |  t j j d  rs d d d g |  _ d d d g |  _ d d d g |  _ d d d g |  _ n d d	 g |  _ d
 g |  _ d  S)Nz/usr/pkg/bin/pkginZpkginz-yinstallremoveupdatezfull-upgradeZpkg_addz-UZ
pkg_delete)	super__init__ospathexistsZpkg_cmd_install_prefixZpkg_cmd_remove_prefixZpkg_cmd_update_prefixZpkg_cmd_upgrade_prefix)selfnameZcfgpaths)	__class__ :/usr/lib/python3/dist-packages/cloudinit/distros/netbsd.pyr
      s    zNetBSD.__init__c             C   s   d d | | g S)Nusermodz-Gr   )r   Zmember_nameZ
group_namer   r   r   _get_add_member_to_group_cmd&   s    z#NetBSD._get_add_member_to_group_cmdc       
      K   s  t  j |  r# t j d |  d Sd g } d g } d d d d d d	 d
 d d d i } d d d d d d i } x | j   D]{ \ } } | | k r | r t | t  r | j | | | g  q~ | | k r~ | r~ | j | |  | j | |  q~ Wd | k sd | k r/| d g 7} | d g 7} | | g 7} | | g 7} t j d |  y t	 j	 | d | Wn( t
 k
 rt  j t d |    Yn X| j d d   }	 |	 d  k	 r|  j | |	 d d d  S)Nz!User %s already exists, skipping.FZuseraddZhomedirz-dZgecosz-cZprimary_groupz-ggroupsz-Gshellz-sZno_user_groupz--no-user-groupsystemz--systemZno_log_initz--no-log-initZno_create_homez-mzAdding user %sZ	logstringzFailed to create user %spasswdhashedT)r   Zis_userLOGinfoitems
isinstancestrextendappendr   	Exceptionlogexcget
set_passwd)
r   r   kwargsZadduser_cmdZlog_adduser_cmdZadduser_optsZadduser_flagskeyvalZ
passwd_valr   r   r   add_user)   sB    		!zNetBSD.add_userFc             C   s   | r | } nG t  t d  s2 t j d |  d  St j } t j | t j |   } y t j d d | | g  Wn( t k
 r t j	 t d |    Yn X|  j
 |  d  S)NMETHOD_BLOWFISHzICannot set non-encrypted password for user %s. Python >= 3.7 is required.r   z-pzFailed to set password for %s)hasattrcryptr   errorr*   Zmksaltr   r"   r   r#   unlock_passwd)r   userr   r   Z	hashed_pwmethodr   r   r   r%   Z   s"    		zNetBSD.set_passwdc             C   sI   y t  j  d d | g  Wn( t k
 rD t j t d |    Yn Xd  S)Nr   z-Fz"Failed to set pw expiration for %s)r   r"   r   r#   r   )r   r/   r   r   r   force_passwd_changer   s
    zNetBSD.force_passwd_changec             C   sL   y t  j  d d d | g  Wn( t k
 rG t j t d |    Yn Xd  S)Nr   z-CyeszFailed to lock user %s)r   r"   r   r#   r   )r   r   r   r   r   lock_passwdy   s
    zNetBSD.lock_passwdc             C   sL   y t  j  d d d | g  Wn( t k
 rG t j t d |    Yn Xd  S)Nr   z-CnozFailed to unlock user %s)r   r"   r   r#   r   )r   r   r   r   r   r.      s
    zNetBSD.unlock_passwdNc             C   s   t  j d  d  S)NzCannot set the locale.)r   debug)r   ZlocaleZout_fnr   r   r   apply_locale   s    zNetBSD.apply_localec             C   s   t  j d  d  S)Nz'NetBSD cannot rename network interface.)r   r5   )r   Z	netconfigr   r   r   apply_network_config_names   s    z!NetBSD.apply_network_config_namesc             C   s?   t  j   } t  j   } t j j   } d | | f | d <| S)z9Return env vars used in NetBSD package_command operationsz:http://cdn.netbsd.org/pub/pkgsrc/packages/NetBSD/%s/%s/AllZPKG_PATH)platformreleasemachiner   environcopy)r   Z
os_releaseZos_archer   r   r   _get_pkg_cmd_environ   s    zNetBSD._get_pkg_cmd_environc             C   s   d  S)Nr   )r   r   r   r   update_package_sources   s    zNetBSD.update_package_sources)__name__
__module____qualname____doc__Zci_sudoers_fnZgroup_add_cmd_prefixr
   r   r)   r%   r1   r3   r.   r6   r7   r>   r?   r   r   )r   r   r      s   	1r   c               @   s   e  Z d  Z d S)DistroN)r@   rA   rB   r   r   r   r   rD      s   rD   )r,   r   r8   Zcloudinit.distros.bsdZ	cloudinitr   Zloggingr   r   Z	getLoggerr@   r   ZdistrosZbsdZBSDr   rD   r   r   r   r   <module>   s   