ó
¼Š`c           @   s~   d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d e	 f d „  ƒ  YZ
 d S(   iÿÿÿÿ(   t   OrderedDictNt   rndcc           B   s’   e  Z d  Z i d d 6d d 6d d 6d d 6d	 d
 6d d 6Z d „  Z d „  Z e d „ Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z RS(   s   RNDC protocol client libraryi   t   md5i¡   t   sha1i¢   t   sha224i£   t   sha256i¤   t   sha384i¥   t   sha512c         C   sŠ   | |  _  | j ƒ  } | j d ƒ r1 | d } n  | |  _ t t | ƒ |  _ t j | ƒ |  _	 t
 j d d ƒ |  _ d |  _ |  j ƒ  d S(   sþ   Creates a persistent connection to RNDC and logs in
        host - (ip, port) tuple
        algo - HMAC algorithm: one of md5, sha1, sha224, sha256, sha384, sha512
               (with optional prefix 'hmac-')
        secret - HMAC secret, base64 encodeds   hmac-i   i    i   i   Ni   (   t   hostt   lowert
   startswitht   algot   getattrt   hashlibt   hlalgot   base64t	   b64decodet   secrett   randomt   randintt   sert   Nonet   noncet   _rndc__connect_login(   t   selfR   R   R   (    (    s,   /usr/lib/python2.7/site-packages/isc/rndc.pyt   __init__$   s    			c         C   s   t  |  j d | ƒ d ƒ S(   s’   Call a RNDC command, all parsing is done on the server side
        cmd - a complete string with a command (eg 'reload zone example.com')
        t   typet   _data(   t   dictt   _rndc__command(   R   t   cmd(    (    s,   /usr/lib/python2.7/site-packages/isc/rndc.pyt   call5   s    c         C   sy  t  ƒ  } xi| j ƒ  D][\ } } | r: | d k r: q n  | t j d t | ƒ ƒ | j d ƒ 7} t | ƒ t k r¤ | t j d d t | ƒ ƒ | j d ƒ 7} q t | ƒ t k rÜ | t j d d t | ƒ ƒ | 7} q t | ƒ t  k r| t j d d t | ƒ ƒ | 7} q t | ƒ t	 k r[|  j
 | ƒ } | t j d d t | ƒ ƒ | 7} q t d t | ƒ ƒ ‚ q W| S(   Nt   _autht   Bt   asciis   >BIi   i   s#   Cannot serialize element of type %s(   t	   bytearrayt   itemst   structt   packt   lent   encodeR   t   strt   bytesR    t   _rndc__serialize_dictt   NotImplementedError(   R   t   datat   ignore_autht   rvt   kt   vt   sd(    (    s,   /usr/lib/python2.7/site-packages/isc/rndc.pyt   __serialize_dict;   s"    	)/&&&c   	      O   s—  |  j  d 7_  t t j ƒ  ƒ } t | | Ž  } t ƒ  } t ƒ  | d <t ƒ  | d <t |  j  ƒ | d d <t | ƒ | d d <t | d ƒ | d d <|  j d  k	 r¹ |  j | d d <n  | | d	 <|  j | d
 t ƒ} t	 j
 |  j | |  j ƒ j ƒ  } t j | ƒ } |  j d k r4t j d | ƒ | d d <n- t t j d |  j |  j | ƒ ƒ | d d <|  j | ƒ } t j d t | ƒ d d ƒ | } | S(   Ni   R    t   _ctrlt   _sert   _timi<   t   _expt   _nonceR   R.   R   t   22st   hmd5t   B88st   hshas   >IIi   (   R   t   intt   timeR    R)   R   R   R+   t   Truet   hmact   newR   R   t   digestR   t	   b64encodeR   R%   R&   R#   t   _rndc__algosR'   (	   R   t   argst   kwargst   nowR-   t   dt   msgt   hasht   bhash(    (    s,   /usr/lib/python2.7/site-packages/isc/rndc.pyt   __prep_messageO   s,    	
!!#c         C   së   |  j  d  k	 r* | d d |  j  k r* t S|  j d k rJ | d d } n | d d d } t | ƒ t k r€ | j d ƒ } n  | d	 d
 t | ƒ d
 7} t j	 | ƒ } |  j
 | d t ƒ} t j |  j | |  j ƒ j ƒ  } | | k S(   NR4   R8   R   R    R:   R<   i   R"   t   =i   R.   (   R   R   t   FalseR   R   R*   t   decodeR'   R   R   R+   R?   R@   RA   R   R   RB   (   R   RI   RK   t   remote_hasht   my_msgt   my_hash(    (    s,   /usr/lib/python2.7/site-packages/isc/rndc.pyt   __verify_msgj   s    &!c   	      O   sD  |  j  | | Ž  } |  j j | ƒ } | t | ƒ k rE t d ƒ ‚ n  |  j j d ƒ } t | ƒ d k rx t d ƒ ‚ n  t j d | ƒ \ } } | d k r¯ t d | ƒ ‚ n  | d 8} |  j j | t j	 ƒ } t | ƒ | k rò t d ƒ ‚ n  t
 | ƒ t k rt | ƒ } n  |  j | ƒ } |  j | ƒ s@t d	 ƒ ‚ n  | S(
   Ns   Cannot send the messagei   s   Can't read response headers   >IIi   s   Wrong message version %di   s   Can't read response datas   Authentication failure(   t   _rndc__prep_messaget   sockett   sendR'   t   IOErrort   recvR%   t   unpackR,   t   MSG_WAITALLR   R)   R#   t   _rndc__parse_messaget   _rndc__verify_msg(	   R   RE   RF   RI   t   sentt   headert   lengtht   versionR-   (    (    s,   /usr/lib/python2.7/site-packages/isc/rndc.pyt	   __commandy   s(    
c         C   sE   t  j |  j ƒ |  _  d  |  _ |  j d d ƒ } | d d |  _ d  S(   NR   t   nullR4   R8   (   RU   t   create_connectionR   R   R   R   (   R   RI   (    (    s,   /usr/lib/python2.7/site-packages/isc/rndc.pyt   __connect_login–   s    	c         C   s,  d } | | } | d 7} | | | | !j  d ƒ } | | 7} | | } | d 7} t j d | | | d !ƒ d } | d 7} | | | | !} | | 7} | | } | d k r» | | | f S| d k rt ƒ  }	 x8 t | ƒ d k r
|  j | ƒ \ }
 } } | |	 |
 <qÓ W| |	 | f St d | ƒ ‚ d  S(   Ni    i   R"   s   >Ii   i   s   Unknown element type %d(   RO   R%   RY   R    R'   t   _rndc__parse_elementR,   (   R   t   inputt   post   labellent   labelR   t   datalenR-   t   restRH   t   ilabelt   value(    (    s,   /usr/lib/python2.7/site-packages/isc/rndc.pyt   __parse_elementœ   s*    




!


	c         C   sN   t  ƒ  } d  } x8 t | ƒ d k rI |  j | ƒ \ } } } | | | <q W| S(   Ni    (   R    R   R'   Re   (   R   Rf   R/   t   hdataRi   Rm   (    (    s,   /usr/lib/python2.7/site-packages/isc/rndc.pyt   __parse_message¶   s    	(   t   __name__t
   __module__t   __doc__RD   R   R   RN   R+   RT   R\   R   R   Re   R[   (    (    (    s,   /usr/lib/python2.7/site-packages/isc/rndc.pyR      s    

							(   t   collectionsR    R>   R%   R   R@   R   R   RU   t   objectR   (    (    (    s,   /usr/lib/python2.7/site-packages/isc/rndc.pyt   <module>   s   