ó
Y[c           @   sƒ  d  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 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 m Z d d l m Z d d l m Z d d l m Z d d l m Z d d	 l m Z e j e ƒ Z d
 e f d „  ƒ  YZ d „  Z d e j  f d „  ƒ  YZ! d e j" f d „  ƒ  YZ# d e j  f d „  ƒ  YZ$ d S(   s!   Creates ACME accounts for server.iÿÿÿÿN(   t   serialization(   t   fields(   t   messages(   t	   constants(   t   errors(   t
   interfaces(   t   utilt   Accountc           B   sT   e  Z d  Z d e j f d „  ƒ  YZ d d „ Z e d „  ƒ Z	 d „  Z
 d „  Z RS(   sá   ACME protocol registration.

    :ivar .RegistrationResource regr: Registration Resource
    :ivar .JWK key: Authorized Account Key
    :ivar .Meta: Account metadata
    :ivar str id: Globally unique account identifier.

    t   Metac           B   s,   e  Z d  Z e j d ƒ Z e j d ƒ Z RS(   s+  Account metadata

        :ivar datetime.datetime creation_dt: Creation date and time (UTC).
        :ivar str creation_host: FQDN of host, where account has been created.

        .. note:: ``creation_dt`` and ``creation_host`` are useful in
            cross-machine migration scenarios.

        t   creation_dtt   creation_host(	   t   __name__t
   __module__t   __doc__t   acme_fieldst   RFC3339FieldR	   t   joset   FieldR
   (    (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyR   '   s   	c         C   s©   | |  _  | |  _ | d  k rZ |  j d t j j d t j ƒ j d d ƒ d t	 j
 ƒ  ƒ n | |  _ t j |  j  j  j ƒ  j d t j j d t j j ƒ ƒ j ƒ  |  _ d  S(   NR	   t   tzt   microsecondi    R
   t   encodingt   format(   t   keyt   regrt   NoneR   t   datetimet   nowt   pytzt   UTCt   replacet   sockett   getfqdnt   metat   hashlibt   md5t
   public_keyt   public_bytesR    t   Encodingt   PEMt   PublicFormatt   SubjectPublicKeyInfot	   hexdigestt   id(   t   selfR   R   R    (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt   __init__4   s    		Qc         C   s/   d j  t j |  j j ƒ |  j j |  j d  ƒ S(   s3   Short account identification string, useful for UI.s   {1}@{0} ({2})i   (   R   t	   pyrfc3339t   generateR    R	   R
   R*   (   R+   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt   slugH   s    c         C   s%   d j  |  j j |  j |  j |  j ƒ S(   Ns   <{0}({1}, {2}, {3})>(   R   t	   __class__R   R   R*   R    (   R+   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt   __repr__N   s    c         C   sF   t  | |  j ƒ oE |  j | j k oE |  j | j k oE |  j | j k S(   N(   t
   isinstanceR0   R   R   R    (   R+   t   other(    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt   __eq__R   s    $N(   R   R   R   R   t   JSONObjectWithFieldsR   R   R,   t   propertyR/   R1   R4   (    (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyR      s   	c         C   sH   t  j j t j ƒ } | d k r% d S| j d j |  j ƒ | j	 ƒ d S(   s.   Informs the user about their new ACME account.Ns   Your account credentials have been saved in your Certbot configuration directory at {0}. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal.(
   t   zopet	   componentt   queryUtilityR   t	   IReporterR   t   add_messageR   t
   config_dirt   MEDIUM_PRIORITY(   t   configt   reporter(    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt   report_new_accountX   s    	t   AccountMemoryStoragec           B   s5   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z RS(   s   In-memory account storage.c         C   s   | d  k	 r | n i  |  _ d  S(   N(   R   t   accounts(   R+   t   initial_accounts(    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyR,   j   s    c         C   s   t  t j |  j ƒ ƒ S(   N(   t   listt   sixt
   itervaluesRB   (   R+   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt   find_allm   s    c         C   s<   | j  |  j k r( t j d | j  ƒ n  | |  j | j  <d  S(   Ns   Overwriting account: %s(   R*   RB   t   loggert   debug(   R+   t   accountt   acme(    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt   savep   s    c         C   s6   y |  j  | SWn  t k
 r1 t j | ƒ ‚ n Xd  S(   N(   RB   t   KeyErrorR   t   AccountNotFound(   R+   t
   account_id(    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt   loadv   s    N(   R   R   R   R   R,   RG   RL   RP   (    (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyRA   g   s
   		t$   RegistrationResourceWithNewAuthzrURIc           B   s   e  Z d  Z e j d ƒ Z RS(   sf  A backwards-compatible RegistrationResource with a new-authz URI.

       Hack: Certbot versions pre-0.11.1 expect to load
       new_authzr_uri as part of the account. Because people
       sometimes switch between old and new versions, we will
       continue to write out this field for some time so older
       clients don't crash in that scenario.
    t   new_authzr_uri(   R   R   R   R   R   RR   (    (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyRQ   |   s   t   AccountFileStoragec           B   sË   e  Z d  Z d „  Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sM   Accounts file storage.

    :ivar .IConfig config: Client configuration

    c         C   s2   | |  _  t j | j d t j ƒ  |  j  j ƒ d  S(   NiÀ  (   R>   R   t   make_or_verify_dirt   accounts_dirt   ost   geteuidt   strict_permissions(   R+   R>   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyR,      s    	c         C   s   |  j  | |  j j ƒ S(   N(   t!   _account_dir_path_for_server_pathR>   t   server_path(   R+   RO   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt   _account_dir_path’   s    c         C   s%   |  j  j | ƒ } t j j | | ƒ S(   N(   R>   t   accounts_dir_for_server_pathRV   t   patht   join(   R+   RO   RZ   RU   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyRY   •   s    c         C   s   t  j j | d ƒ S(   Ns	   regr.json(   RV   R]   R^   (   t   clst   account_dir_path(    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt
   _regr_path™   s    c         C   s   t  j j | d ƒ S(   Ns   private_key.json(   RV   R]   R^   (   R_   R`   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt	   _key_path   s    c         C   s   t  j j | d ƒ S(   Ns	   meta.json(   RV   R]   R^   (   R_   R`   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt   _metadata_path¡   s    c         C   s	  |  j  j | ƒ } y t j | ƒ } Wn t k
 r9 g  SXg  } xU | D]M } y | j |  j | | ƒ ƒ WqG t j k
 r“ t	 j
 d d t ƒqG XqG W| r| t j k rt j | } |  j | ƒ } | rü y |  j | | ƒ Wqü t k
 rø g  SXn  | } n  | S(   Ns   Account loading problemt   exc_info(   R>   R\   RV   t   listdirt   OSErrort   appendt   _load_for_server_pathR   t   AccountStorageErrorRH   RI   t   TrueR   t   LE_REUSE_SERVERSt   _find_all_for_server_patht   _symlink_to_accounts_dir(   R+   RZ   RU   t
   candidatesRB   RO   t   prev_server_patht   prev_accounts(    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyRl   ¥   s*    	c         C   s   |  j  |  j j ƒ S(   N(   Rl   R>   RZ   (   R+   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyRG   À   s    c         C   s8   |  j  | | ƒ } |  j  | | ƒ } t j | | ƒ d  S(   N(   RY   RV   t   symlink(   R+   Ro   RZ   RO   t   prev_account_dirt   new_account_dir(    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt   _symlink_to_account_dirÃ   s    c         C   sg   |  j  j | ƒ } t j j | ƒ r4 t j | ƒ n t j | ƒ |  j  j | ƒ } t j | | ƒ d  S(   N(   R>   R\   RV   R]   t   islinkt   unlinkt   rmdirRq   (   R+   Ro   RZ   RU   Rr   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyRm   È   s    c         C   sÄ  |  j  | | ƒ } t j j | ƒ s³ | t j k r t j | } |  j | | ƒ } |  j j | ƒ } t j	 | ƒ r‰ |  j
 | | | ƒ n |  j | | ƒ | St j d | ƒ ‚ n  y¦ t |  j | ƒ ƒ  } t j j | j ƒ  ƒ } Wd  QXt |  j | ƒ ƒ  }	 t j j |	 j ƒ  ƒ }
 Wd  QXt |  j | ƒ ƒ  } t j j | j ƒ  ƒ } Wd  QXWn" t k
 r}} t j | ƒ ‚ n Xt | |
 | ƒ } | j | k rÀt j d j | | j ƒ ƒ ‚ n  | S(   Ns   Account at %s does not exists/   Account ids mismatch (expected: {0}, found: {1}(   RY   RV   R]   t   isdirR   Rk   Rh   R>   R\   Re   Rt   Rm   R   RN   t   openRa   R   t   RegistrationResourcet
   json_loadst   readRb   R   t   JWKRc   R   R   t   IOErrorRi   R*   R   (   R+   RO   RZ   R`   Ro   t   prev_loaded_accountRU   t	   regr_fileR   t   key_fileR   t   metadata_fileR    t   errort   acc(    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyRh   Ñ   s6    "c         C   s   |  j  | |  j j ƒ S(   N(   Rh   R>   RZ   (   R+   RO   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyRP   ô   s    c         C   s   |  j  | | d t ƒd  S(   Nt	   regr_only(   t   _savet   False(   R+   RJ   RK   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyRL   ÷   s    c         C   s   |  j  | | d t ƒd S(   sm   Save the registration resource.

        :param Account account: account whose regr should be saved

        R…   N(   R†   Rj   (   R+   RJ   RK   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt	   save_regrú   s    c         C   s|   |  j  | ƒ } t j j | ƒ s7 t j d | ƒ ‚ n  |  j | |  j j ƒ t j	 |  j j
 ƒ sx |  j |  j j ƒ n  d S(   sn   Delete registration info from disk

        :param account_id: id of account which should be deleted

        s   Account at %s does not existN(   R[   RV   R]   Rx   R   RN   t#   _delete_account_dir_for_server_pathR>   RZ   Re   RU   t$   _delete_accounts_dir_for_server_path(   R+   RO   R`   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt   delete  s    c         C   s8   t  j |  j | ƒ } |  j | | ƒ } t j | ƒ d  S(   N(   t	   functoolst   partialRY   t!   _delete_links_and_find_target_dirt   shutilt   rmtree(   R+   RO   RZ   t	   link_funct   nonsymlinked_dir(    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyR‰     s    c         C   s/   |  j  j } |  j | | ƒ } t j | ƒ d  S(   N(   R>   R\   RŽ   RV   Rw   (   R+   RZ   R‘   R’   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyRŠ     s    c   
      C   sð   | | ƒ } i  } x" t  j D] } | | t  j | <q Wt } xq | r° t } | | k r@ | | } | | ƒ } t j j | ƒ r­ t j | ƒ | k r­ t } | } | } q­ q@ q@ Wx8 t j j | ƒ rë t j | ƒ }	 t j | ƒ |	 } q´ W| S(   s/  Delete symlinks and return the nonsymlinked directory path.

        :param str server_path: file path based on server
        :param callable link_func: callable that returns possible links
            given a server_path

        :returns: the final, non-symlinked target
        :rtype: str

        (	   R   Rk   Rj   R‡   RV   R]   Ru   t   readlinkRv   (
   R+   RZ   R‘   t   dir_patht   reused_serverst   kt   possible_next_linkt   next_server_patht   next_dir_patht   target(    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyRŽ     s&    	
'
c   
      C   sr  |  j  | j ƒ } t j | d t j ƒ  |  j j ƒ yt |  j	 | ƒ d ƒ w } | j
 } t | j d ƒ r” t d | j j d i  d | j ƒ } n t j d i  d | j ƒ } | j | j ƒ  ƒ Wd  QX| sHt j |  j | ƒ d d d ƒ } | j | j j ƒ  ƒ Wd  QXt |  j | ƒ d ƒ  } | j | j j ƒ  ƒ Wd  QXn  Wn" t k
 rm}	 t j |	 ƒ ‚ n Xd  S(	   NiÀ  t   ws	   new-authzRR   t   bodyt   urit   chmodi   (   R[   R*   R   RT   RV   RW   R>   RX   Ry   Ra   R   t   hasattrt	   directoryRQ   t	   new_authzR   R   Rz   t   writet
   json_dumpst	   safe_openRb   R   Rc   R    R~   R   Ri   (
   R+   RJ   RK   R…   R`   R€   R   R   R‚   Rƒ   (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyR†   D  s0    			#(   R   R   R   R,   R[   RY   t   classmethodRa   Rb   Rc   Rl   RG   Rt   Rm   Rh   RP   RL   Rˆ   R‹   R‰   RŠ   RŽ   R†   (    (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyRS   ‡   s(   									#							'(%   R   R   RŒ   R!   t   loggingRV   R   R   t   cryptography.hazmat.primitivesR    t   josepyR   R-   R   RE   t   zope.componentR7   RK   R   R   R   t   certbotR   R   R   R   t	   getLoggerR   RH   t   objectR   R@   t   AccountStorageRA   Rz   RQ   RS   (    (    (    s3   /usr/lib/python2.7/dist-packages/certbot/account.pyt   <module>   s2   ;	