ó
½-'Nc           @   sd   d  d l  Z  d  d l Z d  d l Z d  d l m Z d  d l m Z m Z d e f d „  ƒ  YZ	 d S(   iÿÿÿÿN(   t   md5crypt(   t   UserManagementErrort   UserProvidert   UserManagementc           B   s¤   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d d d d d d d „ Z d „  Z	 d „  Z
 e d „ Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z RS(   s   Manage system users and groups.c         C   s   | p t  ƒ  |  _ d  S(   N(   R   t	   _provider(   t   selft   provider(    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyt   __init__   s    c	         C   s  t  j d | ƒ d | | p d | p( d | p1 d f }	 d | d d |	 g }
 | r{ |
 j d t |  j j | ƒ ƒ g ƒ n  |  j |
 ƒ \ } } | d k rµ t d	 | | f ƒ ‚ n  |  j | | ƒ | r|  j d
 | d g ƒ \ } } | d k rt d | | f ƒ ‚ q| | 7} n  | S(   s¯   Add C{username} to the computer.

        @raises UserManagementError: Raised when C{adduser} fails.
        @raises UserManagementError: Raised when C{passwd} fails.
        s   Adding user %s.s   %s,%s,%s,%st    t   addusers   --disabled-passwords   --gecoss   --gidi    s   Error adding user %s.
%st   passwds   -es(   Error resetting password for user %s.
%s(	   t   loggingt   infot   extendt   strR   t   get_gidt
   call_popenR   t   _set_password(   R   t   usernamet   namet   passwordt   require_password_resett   primary_group_namet   locationt
   work_phonet
   home_phonet   gecost   commandt   resultt   outputt
   new_output(    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyt   add_user   s(    	c         C   s–   | j  d ƒ } | j  d ƒ } t j d ƒ j  d ƒ d  } t j | | ƒ } |  j d d | | g ƒ \ } } | d k r’ t d | | f ƒ ‚ n  | S(	   Nt   asciii   t   base64iÿÿÿÿt   usermods   -pi    s&   Error setting password for user %s.
%s(   t   encodet   ost   urandomR    R   R   (   R   R   R   t   saltt   cryptedR   R   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyR   3   s    !c         C   sk   |  j  j | ƒ } d d t | ƒ | g } |  j | ƒ \ } } | d k rg t d | | | f ƒ ‚ n  | S(   NR"   s   -gi    s+   Error setting primary group to %d for%s.
%s(   R   R   R   R   R   (   R   R   t	   groupnamet   primary_gidR   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyt   _set_primary_group?   s    c         C   s  |  j  j | ƒ } t j d | | ƒ | r> |  j | | ƒ n  | rW |  j | | ƒ n  d g }	 xW d | f d | f d | f d | f g D]+ \ }
 } | d
 k	 r‹ |	 |
 | g 7}	 q‹ q‹ Wt |	 ƒ d k r|  j |	 | g ƒ \ } } | d k rt	 d	 | | f ƒ ‚ n  | Sd
 S(   s/   Update details for the account matching C{uid}.s'   Updating metadata for user %s (UID %d).t   chfns   -rs   -fs   -ws   -hi   i    s%   Error setting details for user %s.
%sN(
   R   t   get_uidR   R   R   R*   t   Nonet   lenR   R   (   R   R   R   R   R   t   work_numbert   home_numberR   t   uidR   t   optiont   valueR   R   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyt   set_user_detailsI   s"    	"c         C   sl   |  j  j | ƒ } t j d | | ƒ |  j d d | g ƒ \ } } | d k rh t d | | f ƒ ‚ n  d S(   sX   
        Lock the account matching C{username} to prevent them from logging in.
        s   Locking out user %s (UID %d).R"   s   -Li    s   Error locking user %s.
%sN(   R   R,   R   R   R   R   (   R   R   R1   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyt	   lock_userb   s    c         C   sl   |  j  j | ƒ } t j d | | ƒ |  j d d | g ƒ \ } } | d k rh t d | | f ƒ ‚ n  | S(   s(   Unlock the account matching C{username}.s   Unlocking user %s (UID %d).R"   s   -Ui    s   Error unlocking user %s.
%s(   R   R,   R   R   R   R   (   R   R   R1   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyt   unlock_userm   s    c         C   s›   |  j  j | ƒ } d | g } | rG t j d | | ƒ | j d ƒ n t j d | | ƒ |  j | ƒ \ } } | d k r— t d | | | f ƒ ‚ n  | S(   s:   Remove the account matching C{username} from the computer.t   delusers<   Removing user %s (UID %d) and deleting their home directory.s   --remove-homes@   Removing user %s (UID %d) without deleting their home directory.i    s#   Error removing user %s (UID %d).
%s(   R   R,   R   R   t   appendR   R   (   R   R   t   delete_homeR1   R   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyt   remove_userx   s    	
	
c         C   sT   t  j d | ƒ |  j d | g ƒ \ } } | d k rP t d | | f ƒ ‚ n  | S(   s1   Add C{group} with the C{addgroup} system command.s   Adding group %s.t   addgroupi    s   Error adding group %s.
%s(   R   R   R   R   (   R   R(   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyt	   add_groupŠ   s    c         C   s~   |  j  j | ƒ } t j d | | | ƒ d d | | g } |  j | ƒ \ } } | d k rz t d | | | | f ƒ ‚ n  | S(   s-   Update details for the group matching C{gid}.s!   Renaming group %s (GID %d) to %s.t   groupmods   -ni    s*   Error renaming group %s (GID %d) to %s.
%s(   R   R   R   R   R   R   (   R   R(   t   new_namet   gidR   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyt   set_group_details“   s    		c         C   s   |  j  j | ƒ } |  j  j | ƒ } t j d | | | | ƒ |  j d d | | g ƒ \ } } | d k rŒ t d | | | | | f ƒ ‚ n  | S(   s‚   
        Add the user matching C{username} to the group matching C{groupname}
        with the C{gpasswd} system command.
        s-   Adding user %s (UID %d) to group %s (GID %d).t   gpasswds   -ai    s6   Error adding user %s (UID %d) to group %s (GID %d).
%s(   R   R,   R   R   R   R   R   (   R   R   R(   R1   R?   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyt   add_group_member    s    	!c         C   s   |  j  j | ƒ } |  j  j | ƒ } t j d | | | | ƒ |  j d d | | g ƒ \ } } | d k rŒ t d | | | | | f ƒ ‚ n  | S(   s‡   
        Remove the user matching C{username} from the group matching
        C{groupname} with the C{gpasswd} system command.
        s1   Removing user %s (UID %d) from group %s (GID %d).RA   s   -di    s;   Error removing user %s (UID %d) from group %s (GID (%d).
%s(   R   R,   R   R   R   R   R   (   R   R   R(   R1   R?   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyt   remove_group_member°   s    	!	c         C   sl   |  j  j | ƒ } t j d | | ƒ |  j d | g ƒ \ } } | d k rh t d | | | f ƒ ‚ n  | S(   s;   Remove the account matching C{groupname} from the computer.s   Removing group %s (GID %d).t   groupdeli    s$   Error removing group %s (GID %d).
%s(   R   R   R   R   R   R   (   R   R(   R?   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyt   remove_groupÁ   s    c         C   sI   |  j  j | d t j d t j ƒ} | j j ƒ  } | j ƒ  } | | f S(   Nt   stdoutt   stderr(   R   t   popent
   subprocesst   PIPEt   STDOUTRF   t   readt   wait(   R   t   argsRH   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyR   Ë   s
    N(   t   __name__t
   __module__t   __doc__R-   R   R   R   R*   R4   R5   R6   t   FalseR:   R<   R@   RB   RC   RE   R   (    (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyR      s"   			
									
(
   R$   R   RI   t   landscape.libR    t   landscape.user.providerR   R   t   objectR   (    (    (    s=   /usr/lib/python2.7/dist-packages/landscape/user/management.pyt   <module>   s
   