ó
½-'Nc           @   s¶   d  d l  m Z d  d l m Z d  d l Z d  d l Z d  d l Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ	 d e f d	 „  ƒ  YZ
 d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   struct_passwd(   t   struct_groupNt   UserManagementErrorc           B   s   e  Z d  Z RS(   s2   Catch all error for problems with User Management.(   t   __name__t
   __module__t   __doc__(    (    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyR      s   t   UserNotFoundErrorc           B   s   e  Z d  Z RS(   s5   Raised when a user couldn't be found by uid/username.(   R   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyR      s   t   GroupNotFoundErrorc           B   s   e  Z d  Z RS(   s7   Raised when a group couldn't be found by gid/groupname.(   R   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyR      s   t   UserProviderBasec           B   s>   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   s(   This is a base class for user Providers.c         C   s%   | p	 g  |  _  d |  _ d |  _ d  S(   Niè  i`ê  (   t   locked_userst   _min_uidt   _max_uid(   t   selfR	   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyt   __init__   s    	c         C   sF  g  } t  ƒ  } x0|  j ƒ  D]"} t | t ƒ s@ t | ƒ } n  | j | k rU q n  g  | j j d ƒ d  D] } | j d d ƒ p‡ d ^ ql } x# t	 | ƒ d k  rµ | j
 d ƒ q“ Wt | ƒ \ } } } }	 | j |  j k }
 | j
 i | j d 6| d 6| j d 6|
 d 6| d	 6| d
 6|	 d 6| j d 6ƒ | j | j ƒ q W| S(   sà   Returns a list of all local users on the computer.

        Each user is represented as a dict with the keys: C{username},
        C{name}, C{uid}, C{enabled}, C{location}, C{work-phone} and
        C{home-phone}.  
        t   ,i   s   utf-8t   replacet   usernamet   namet   uidt   enabledt   locations
   work-phones
   home-phones   primary-gidN(   t   sett   get_user_datat
   isinstanceR    t   pw_namet   pw_gecost   splitt   decodet   Nonet   lent   appendt   tupleR	   t   pw_uidt   pw_gidt   add(   R   t   userst   found_usernamest   usert   xt
   gecos_dataR   R   t
   work_phonet
   home_phoneR   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyt	   get_users   s(    	8c         C   sÖ   t  g  |  j ƒ  D] } | d ^ q ƒ } g  } t  ƒ  } x— |  j ƒ  D]‰ } t | t ƒ si t | ƒ } n  | j | k r~ qE n  | j | j ƒ } | j i | j d 6| j	 d 6t
 | ƒ d 6ƒ | j | j ƒ qE W| S(   s–   Returns a list of groups on the computer.

        Each group is represented as a dict with the keys: C{name},
        C{gid} and C{members}.
        R   R   t   gidt   members(   R   R*   t   get_group_dataR   R   t   gr_namet   intersectiont   gr_memR   t   gr_gidt   listR"   (   R   R&   t
   user_namest   groupst   found_groupnamest   groupt   member_names(    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyt
   get_groups9   s    )	c         C   sC   x, |  j  ƒ  D] } | d | k r | d Sq Wt d | ƒ ‚ d S(   s•   Returns the UID for C{username}.

        @raises UserNotFoundError: Raised if C{username} doesn't match a
            user on the computer.
        R   R   s   UID not found for user %s.N(   R*   R   (   R   R   t   data(    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyt   get_uidM   s    c         C   sC   x, |  j  ƒ  D] } | d | k r | d Sq Wt d | ƒ ‚ d S(   sš   Returns the GID for C{groupname}.

        @raises UserManagementError: Raised if C{groupname} doesn't
            match a group on the computer.
        R   R+   s   Group not found for group %s.N(   R8   R   (   R   t	   groupnameR9   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyt   get_gidX   s    N(	   R   R   R   R   R   R*   R8   R:   R<   (    (    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyR      s   			t   UserProviderc           B   sb   e  Z e j Z d  d d d d d d g Z d d d d	 g Z g  d
 d d „ Z d „  Z d „  Z	 RS(   R   t   passwdR   s   primary-gidt   gecost   homet   shellR   R+   R,   s   /etc/passwds
   /etc/groupc         C   s,   t  t |  ƒ j | ƒ | |  _ | |  _ d  S(   N(   t   superR=   R   t   _passwd_filet   _group_file(   R   R	   t   passwd_filet
   group_file(    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyR   k   s    	c         C   s  g  } t  |  j d ƒ } t j | d |  j d d d t j ƒ} d } xÆ | D]¾ } | d 7} | d j d	 ƒ sL | d j d
 ƒ rˆ qL n  yN | j | d | d t | d ƒ t | d ƒ | d | d | d f ƒ WqL t	 t
 f k
 r	t j d |  j | f ƒ qL XqL W| j ƒ  | S(   sÎ   
        Parse passwd(5) formatted files and return tuples of user data in the
        form (username, password, uid, primary-group-id, gecos data, home
        directory, path to the user's shell)
        t   rt
   fieldnamest	   delimitert   :t   quotingi    i   R   t   +t   -R>   R   s   primary-gidR?   R@   RA   s1   passwd file %s is incorrectly formatted: line %d.(   t   openRC   t   csvt
   DictReadert   passwd_fieldst
   QUOTE_NONEt
   startswithR   t   intt
   ValueErrort	   TypeErrort   loggingt   warnt   close(   R   t	   user_dataRE   t   readert   current_linet   row(    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyR   q   s&    
 	
c         C   s
  g  } t  |  j d ƒ } t j | d |  j d d d t j ƒ} d } x´ | D]¬ } | d 7} | d j d	 ƒ sL | d j d
 ƒ rˆ qL n  y< | j | d | d t | d ƒ | d j	 d ƒ f ƒ WqL t
 t f k
 r÷ t j d |  j | f ƒ qL XqL W| j ƒ  | S(   s²   
        Parse group(5) formatted files and return tuples of group data in the
        form (groupname, group password, group id and a list of member
        usernames).
        RG   RH   RI   RJ   RK   i    i   R   RL   RM   R>   R+   R,   R   s0   group file %s is incorrectly formatted: line %d.(   RN   RD   RO   RP   t   group_fieldsRR   RS   R   RT   R   t   AttributeErrorRU   RW   RX   RY   (   R   t
   group_dataRF   R[   R\   R]   (    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyR-   Ž   s$    
!	
(
   R   R   t
   subprocesst   Popent   popenRQ   R^   R   R   R-   (    (    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyR=   c   s   		(   t   pwdR    t   grpR   RO   Ra   RW   t	   ExceptionR   R   R   t   objectR   R=   (    (    (    s;   /usr/lib/python2.7/dist-packages/landscape/user/provider.pyt   <module>   s   O