
[XMc           @   s   d  Z  d d l m Z d d l Z d d l m Z d d l m Z d d l m	 Z	 m
 Z
 d Z d Z d
 e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e j f d     YZ d d d     YZ d e j f d     YZ d
 d d d d d d g Z d S(   s>   
Ident protocol implementation.

@author: Jean-Paul Calderone
i(   t
   generatorsN(   t   defer(   t   basic(   t   logt   failurei   i   i   t
   IdentErrorc           B   s   e  Z d  Z d Z d   Z RS(   s;   
    Can't determine connection owner; reason unknown.
    s   UNKNOWN-ERRORc         C   s   |  j  S(   N(   t   identDescription(   t   self(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyt   __str__   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR      s   t   NoUserc           B   s   e  Z d  Z d Z RS(   s   
    The connection specified by the port pair is not currently in use or
    currently not owned by an identifiable entity.
    s   NO-USER(   R	   R
   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR   !   s   t   InvalidPortc           B   s   e  Z d  Z d Z RS(   s  
    Either the local or foreign port was improperly specified. This should
    be returned if either or both of the port ids were out of range (TCP
    port numbers are from 1-65535), negative integers, reals or in any
    fashion not recognized as a non-negative integer.
    s   INVALID-PORT(   R	   R
   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR   )   s   t
   HiddenUserc           B   s   e  Z d  Z d Z RS(   s   
    The server was able to identify the user of this port, but the
    information was not returned at the request of the user.
    s   HIDDEN-USER(   R	   R
   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR   3   s   t   IdentServerc           B   sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s  
    The Identification Protocol (a.k.a., "ident", a.k.a., "the Ident
    Protocol") provides a means to determine the identity of a user of a
    particular TCP connection. Given a TCP port number pair, it returns a
    character string which identifies the owner of that connection on the
    server's system.

    Server authors should subclass this class and override the lookup method.
    The default implementation returns an UNKNOWN-ERROR response for every
    query.
    c         C   s   | j  d  } t |  d k r. |  j   n y t t |  \ } } Wn t k
 rd |  j   nk Xt | k o| t k n r t | k o t k n r |  j | |  n |  j	 t
 j t    | |  d  S(   Nt   ,i   (   t   splitt   lent   invalidQueryt   mapt   intt
   ValueErrort	   _MIN_PORTt	   _MAX_PORTt
   validQueryt	   _ebLookupR   t   FailureR   (   R   t   linet   partst   portOnServert   portOnClient(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyt   lineReceivedH   s    8c         C   s   |  j  j   d  S(   N(   t	   transportt   loseConnection(   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR   W   s    c         C   sn   |  j  j   j | f } |  j  j   j | f } t j |  j | |  j |  j | |  j	 |  j
 | |  d S(   s   
        Called when a valid query is received to look up and deliver the
        response.

        @param portOnServer: The server port from the query.
        @param portOnClient: The client port from the query.
        N(   R!   t   getHostt   hostt   getPeerR   t   maybeDeferredt   lookupt   addCallbackt	   _cbLookupt
   addErrbackR   (   R   R   R   t
   serverAddrt
   clientAddr(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR   [   s
    c         C   s-   | \ } } |  j  d | | | | f  d  S(   Ns   %d, %d : USERID : %s : %s(   t   sendLine(   R   t   .1t   sportt   cportt   sysNamet   userId(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR)   k   s    	c         C   sc   | j  t  r/ |  j d | | | j f  n0 t j |  |  j d | | t | j  f  d  S(   Ns   %d, %d : ERROR : %s(   t   checkR   R-   t   valueR   t   err(   R   R   R/   R0   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR   n   s     c         C   s   t     d S(   sh  Lookup user information about the specified address pair.

        Return value should be a two-tuple of system name and username.
        Acceptable values for the system name may be found online at::

            U{http://www.iana.org/assignments/operating-system-names}

        This method may also raise any IdentError subclass (or IdentError
        itself) to indicate user information will not be provided for the
        given query.

        A Deferred may also be returned.

        @param serverAddress: A two-tuple representing the server endpoint
        of the address being queried.  The first element is a string holding
        a dotted-quad IP address.  The second element is an integer
        representing the port.

        @param clientAddress: Like L{serverAddress}, but represents the
        client endpoint of the address being queried.
        N(   R   (   R   t   serverAddresst   clientAddress(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR'   u   s    (	   R	   R
   R   R    R   R   R)   R   R'   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR   ;   s   					t   ProcServerMixinc           B   s   e  Z d  Z d Z y# d d l m Z e d  Z [ Wn e k
 rQ d   Z n Xd   Z d   Z	 d   Z
 d	   Z d
   Z RS(   sI   Implements lookup() to grab entries for responses from /proc/net/tcp
    t   LINUXi(   t   getpwuidc         C   s   | |  d S(   Ni    (    (   R   t   uidR:   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyt   getUsername   s    c         C   s   t     d  S(   N(   R   (   R   R;   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR<      s    c         c   s6   t  d  } | j   x | D] } | j   Vq Wd  S(   Ns   /proc/net/tcp(   t   filet   readlinet   strip(   R   t   ft   L(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyt   entries   s    
c      
   C   s7   d j  t t t j d t j d t | d      S(   Nt   .t   4Bs   =Li   (   t   joinR   t   strt   structt   unpackt   packR   (   R   t   hexstr(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyt   dottedQuadFromHexString   s    c         C   s=   | j  d  \ } } |  j |  } t | d  } | | f S(   Nt   :i   (   R   RK   R   (   R   t   packedt   addrt   port(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyt   unpackAddress   s    c         C   sm   | j    j   } |  j | d  \ } } |  j | d  \ } } t | d  } | | f | | f | f S(   Ni   i   i   (   R?   R   RP   R   (   R   R   R   t	   localAddrt	   localPortt
   remoteAddrt
   remotePortR;   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyt	   parseLine   s
    c         C   sr   xb |  j    D]T } |  j |  \ } } } | | k r | d | d k r |  j |  j |  f Sq Wt    d  S(   Ni   (   RB   RU   t   SYSTEM_NAMER<   R   (   R   R6   R7   t   entRQ   RS   R;   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR'      s
     (   R	   R
   R   RV   t   pwdR:   R<   t   ImportErrorRB   RK   RP   RU   R'   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR8      s   				t   IdentClientc           B   sG   e  Z e e e e f Z d    Z d   Z d   Z	 d   Z
 d   Z RS(   c         C   s   g  |  _  d  S(   N(   t   queries(   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyt   __init__   s    c         C   si   |  j  j t j   | | f  t |  j   d k rC |  j  d d S|  j d | | f  |  j  d d S(   sB   Lookup user information about the specified address pair.
        i   ii    s   %d, %d(   R[   t   appendR   t   DeferredR   R-   (   R   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR'      s
    c         C   s   |  j  s  t j d | f  nd |  j  j d  \ } } } |  j | |  |  j  r |  j d |  j  d d |  j  d d f  n  d  S(   Ns   Unexpected server response: %ri    s   %d, %di   i   (   R[   R   t   msgt   popt   parseResponseR-   (   R   R   t   dt   _(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyR       s    		c         C   s8   x( |  j  D] } | d j t |   q
 Wg  |  _  d  S(   Ni    (   R[   t   errbackR   (   R   t   reasont   q(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyt   connectionLost   s    c         C   s   | j  d d  } t |  d k r: | j t |   n t t j |  \ } } } | d k r x7 |  j D], } | j | k rk | j | |   d  Sqk W| j t |   n | j	 | | f  d  S(   NRL   i   i   t   ERROR(
   R   R   Rd   R   R   RF   R?   t
   errorTypesR   t   callback(   R   t   deferredR   R   t   portst   typet   addInfot   et(    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyRa      s    (   R	   R
   R   R   R   R   Ri   R\   R'   R    Rg   Ra   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyRZ      s   		
			i   i  (    (   R   t
   __future__R    RG   t   twisted.internetR   t   twisted.protocolsR   t   twisted.pythonR   R   R   R   t	   ExceptionR   R   R   R   t   LineOnlyReceiverR   R8   RZ   t   __all__(    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/protocols/ident.pyt   <module>	   s"   
R..