a
    á `5:  ã                   @   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m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 dd
lmZ ddlmZ ddlmZ e e¡ZG dd„ de ƒZ!G dd„ dej"ƒZ#G dd„ dej$ƒZ%G dd„ dej"ƒZ&dS )z!Creates ACME accounts for server.é    N)Úserialization)Úfields)Úmessages)Ú
ClientBase)Úerrors)Ú
interfaces)Úutil)Ú	constants)Úos)Ú
filesystemc                   @   sH   e Zd ZdZG dd„ dejƒZddd„Zedd„ ƒZ	d	d
„ Z
dd„ ZdS )ÚAccountzá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.

    c                   @   s2   e Zd ZdZe d¡Ze d¡Z	ejdddZ
dS )zAccount.MetaaÍ  Account metadata

        :ivar datetime.datetime creation_dt: Creation date and time (UTC).
        :ivar str creation_host: FQDN of host, where account has been created.
        :ivar str register_to_eff: If not None, Certbot will register the provided
                                        email during the account registration.

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

        Úcreation_dtÚcreation_hostÚregister_to_effT)Z	omitemptyN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úacme_fieldsZRFC3339Fieldr   ÚjoseÚFieldr   r   © r   r   ú;/usr/lib/python3/dist-packages/certbot/_internal/account.pyÚMeta&   s   

r   Nc                 C   s¢   || _ || _|d u r<| jtjjtjdjddt 	¡ d dn|| _
zt ¡ }W n  tyn   tjddd}Y n0 | | j j  ¡ jtjjtjjd¡ | ¡ | _d S )	N)Ztzr   )Zmicrosecond)r   r   r   Úmd5F)Zusedforsecurity)ÚencodingÚformat)ÚkeyÚregrr   ÚdatetimeZnowÚpytzZUTCÚreplaceÚsocketZgetfqdnÚmetaÚhashlibr   Ú
ValueErrorÚnewÚupdateZ
public_keyZpublic_bytesr   ZEncodingZPEMZPublicFormatZSubjectPublicKeyInfoZ	hexdigestÚid)Úselfr   r   r#   Zhasherr   r   r   Ú__init__6   s&    üüüþzAccount.__init__c                 C   s&   d  t | jj¡| jj| jdd… ¡S )z3Short account identification string, useful for UI.z{1}@{0} ({2})Né   )r   Ú	pyrfc3339Zgenerater#   r   r   r(   ©r)   r   r   r   ÚslugQ   s
    ÿÿzAccount.slugc                 C   s   d  | jj| j| j| j¡S )Nz<{0}({1}, {2}, {3})>)r   Ú	__class__r   r   r(   r#   r-   r   r   r   Ú__repr__W   s    ÿzAccount.__repr__c                 C   s0   t || jƒo.| j|jko.| j|jko.| j|jkS ©N)Ú
isinstancer/   r   r   r#   )r)   Úotherr   r   r   Ú__eq__[   s    
ÿ
ÿ
þzAccount.__eq__)N)r   r   r   r   r   ZJSONObjectWithFieldsr   r*   Úpropertyr.   r0   r4   r   r   r   r   r      s   	

r   c                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚAccountMemoryStoragezIn-memory account storage.Nc                 C   s   |d ur|ni | _ d S r1   )Úaccounts)r)   Zinitial_accountsr   r   r   r*   d   s    zAccountMemoryStorage.__init__c                 C   s   t t | j¡ƒS r1   )ÚlistÚsixZ
itervaluesr7   r-   r   r   r   Úfind_allg   s    zAccountMemoryStorage.find_allc                 C   s*   |j | jv rt d|j ¡ || j|j < d S )NzOverwriting account: %s)r(   r7   ÚloggerÚdebug)r)   ÚaccountÚclientr   r   r   Úsavej   s    zAccountMemoryStorage.savec                 C   s.   z| j | W S  ty(   t |¡‚Y n0 d S r1   )r7   ÚKeyErrorr   ÚAccountNotFound©r)   Ú
account_idr   r   r   Úloado   s    zAccountMemoryStorage.load)N)r   r   r   r   r*   r:   r?   rD   r   r   r   r   r6   a   s
   
r6   c                   @   s   e Zd ZdZe d¡ZdS )Ú$RegistrationResourceWithNewAuthzrURIaf  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.
    Únew_authzr_uriN)r   r   r   r   r   r   rF   r   r   r   r   rE   u   s   rE   c                   @   sÔ   e Zd ZdZdd„ Zdd„ Zdd„ Zedd	„ ƒZed
d„ ƒZ	edd„ ƒZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0S )1ÚAccountFileStoragezMAccounts file storage.

    :ivar .IConfig config: Client configuration

    c                 C   s   || _ t |jd| j j¡ d S ©NiÀ  )Úconfigr   Úmake_or_verify_dirÚaccounts_dirÚstrict_permissions)r)   rI   r   r   r   r*   †   s    zAccountFileStorage.__init__c                 C   s   |   || jj¡S r1   )Ú!_account_dir_path_for_server_pathrI   Úserver_pathrB   r   r   r   Ú_account_dir_pathŠ   s    z$AccountFileStorage._account_dir_pathc                 C   s   | j  |¡}tj ||¡S r1   )rI   Úaccounts_dir_for_server_pathr
   ÚpathÚjoin)r)   rC   rN   rK   r   r   r   rM      s    z4AccountFileStorage._account_dir_path_for_server_pathc                 C   s   t j |d¡S )Nz	regr.json©r
   rQ   rR   ©ÚclsÚaccount_dir_pathr   r   r   Ú
_regr_path‘   s    zAccountFileStorage._regr_pathc                 C   s   t j |d¡S )Nzprivate_key.jsonrS   rT   r   r   r   Ú	_key_path•   s    zAccountFileStorage._key_pathc                 C   s   t j |d¡S )Nz	meta.jsonrS   rT   r   r   r   Ú_metadata_path™   s    z!AccountFileStorage._metadata_pathc              	   C   sÐ   | j  |¡}zt |¡}W n ty0   g  Y S 0 g }|D ]>}z| |  ||¡¡ W q: tjyv   t	j
ddd Y q:0 q:|sÌ|tjv rÌtj| }|  |¡}|rÈz|  ||¡ W n tyÆ   g  Y S 0 |}|S )NzAccount loading problemT)Úexc_info)rI   rP   r
   ÚlistdirÚOSErrorÚappendÚ_load_for_server_pathr   ÚAccountStorageErrorr;   r<   r	   ÚLE_REUSE_SERVERSÚ_find_all_for_server_pathÚ_symlink_to_accounts_dir)r)   rN   rK   Z
candidatesr7   rC   Úprev_server_pathZprev_accountsr   r   r   ra      s*    



z,AccountFileStorage._find_all_for_server_pathc                 C   s   |   | jj¡S r1   )ra   rI   rN   r-   r   r   r   r:   ¸   s    zAccountFileStorage.find_allc                 C   s(   |   ||¡}|   ||¡}t ||¡ d S r1   )rM   r
   Úsymlink)r)   rc   rN   rC   Úprev_account_dirZnew_account_dirr   r   r   Ú_symlink_to_account_dir»   s    z*AccountFileStorage._symlink_to_account_dirc                 C   sJ   | j  |¡}tj |¡r$t |¡ n
t |¡ | j  |¡}t ||¡ d S r1   )rI   rP   r
   rQ   ÚislinkÚunlinkÚrmdirrd   )r)   rc   rN   rK   re   r   r   r   rb   À   s    
z+AccountFileStorage._symlink_to_accounts_dirc              
   C   sz  |   ||¡}tj |¡s||tjv rntj| }|  ||¡}| j |¡}t 	|¡r^|  
|||¡ n|  ||¡ |S t d| ¡‚zÀt|  |¡ƒ }tj | ¡ ¡}W d   ƒ n1 s²0    Y  t|  |¡ƒ }	tj |	 ¡ ¡}
W d   ƒ n1 sð0    Y  t|  |¡ƒ }tj | ¡ ¡}W d   ƒ n1 s00    Y  W n0 tyl } zt |¡‚W Y d }~n
d }~0 0 t||
|ƒS )NúAccount at %s does not exist)rM   r
   rQ   Úisdirr	   r`   r^   rI   rP   r[   rf   rb   r   rA   ÚopenrW   r   ÚRegistrationResourceZ
json_loadsÚreadrX   r   ZJWKrY   r   r   ÚIOErrorr_   )r)   rC   rN   rV   rc   Zprev_loaded_accountrK   Ú	regr_filer   Úkey_filer   Úmetadata_filer#   Úerrorr   r   r   r^   É   s.    


ÿ..4 z(AccountFileStorage._load_for_server_pathc                 C   s   |   || jj¡S r1   )r^   rI   rN   rB   r   r   r   rD   æ   s    zAccountFileStorage.loadc              
   C   sh   z4|   |¡}|  ||¡ |  ||¡ |  |||¡ W n. tyb } zt |¡‚W Y d}~n
d}~0 0 dS )zšCreate a new account.

        :param Account account: account to create
        :param ClientBase client: ACME client associated to the account

        N)Ú_prepareÚ_createÚ_update_metaÚ_update_regrro   r   r_   ©r)   r=   r>   Údir_pathrs   r   r   r   r?   é   s    
zAccountFileStorage.savec              
   C   sP   z|   |¡}|  |||¡ W n. tyJ } zt |¡‚W Y d}~n
d}~0 0 dS )z¦Update the registration resource.

        :param Account account: account to update
        :param ClientBase client: ACME client associated to the account

        N)rt   rw   ro   r   r_   rx   r   r   r   Úupdate_regrù   s
    
zAccountFileStorage.update_regrc              
   C   sN   z|   |¡}|  ||¡ W n. tyH } zt |¡‚W Y d}~n
d}~0 0 dS )zVUpdate the meta resource.

        :param Account account: account to update

        N)rt   rv   ro   r   r_   )r)   r=   ry   rs   r   r   r   Úupdate_meta  s
    
zAccountFileStorage.update_metac                 C   sT   |   |¡}tj |¡s$t d| ¡‚|  || jj¡ t 	| jj
¡sP|  | jj¡ dS )znDelete registration info from disk

        :param account_id: id of account which should be deleted

        rj   N)rO   r
   rQ   rk   r   rA   Ú#_delete_account_dir_for_server_pathrI   rN   r[   rK   Ú$_delete_accounts_dir_for_server_path)r)   rC   rV   r   r   r   Údelete  s    
ÿzAccountFileStorage.deletec                 C   s(   t  | j|¡}|  ||¡}t |¡ d S r1   )Ú	functoolsÚpartialrM   Ú!_delete_links_and_find_target_dirÚshutilZrmtree)r)   rC   rN   Ú	link_funcÚnonsymlinked_dirr   r   r   r|   %  s    z6AccountFileStorage._delete_account_dir_for_server_pathc                 C   s"   | j j}|  ||¡}t |¡ d S r1   )rI   rP   r   r
   ri   )r)   rN   rƒ   r„   r   r   r   r}   *  s    z7AccountFileStorage._delete_accounts_dir_for_server_pathc           
      C   sœ   ||ƒ}i }t jD ]}||t j| < qd}|rrd}||v r*|| }||ƒ}tj |¡r*t |¡|kr*d}|}|}q*tj |¡r˜t |¡}	t |¡ |	}qr|S )a/  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

        TF)r	   r`   r
   rQ   rg   r   Úreadlinkrh   )
r)   rN   rƒ   ry   Zreused_serversÚkZpossible_next_linkZnext_server_pathZnext_dir_pathÚtargetr   r   r   r   /  s&    


z4AccountFileStorage._delete_links_and_find_target_dirc                 C   s"   |   |j¡}t |d| jj¡ |S rH   )rO   r(   r   rJ   rI   rL   )r)   r=   rV   r   r   r   rt   V  s    zAccountFileStorage._preparec                 C   sJ   t j|  |¡ddd }| |j ¡ ¡ W d   ƒ n1 s<0    Y  d S )NÚwé   )Úchmod)r   Z	safe_openrX   Úwriter   Ú
json_dumps)r)   r=   ry   rq   r   r   r   ru   \  s    zAccountFileStorage._createc                 C   sz   t |  |¡dƒV}|j}t|jdƒr:t|jji |jd}ntj	i |jd}| 
| ¡ ¡ W d   ƒ n1 sl0    Y  d S )Nrˆ   z	new-authz)rF   ÚbodyÚuri)r   rŽ   )rl   rW   r   ÚhasattrZ	directoryrE   Z	new_authzrŽ   r   rm   r‹   rŒ   )r)   r=   Úacmery   rp   r   r   r   r   rw   a  s    ýþzAccountFileStorage._update_regrc                 C   sD   t |  |¡dƒ }| |j ¡ ¡ W d   ƒ n1 s60    Y  d S )Nrˆ   )rl   rY   r‹   r#   rŒ   )r)   r=   ry   rr   r   r   r   rv   t  s    zAccountFileStorage._update_metaN)r   r   r   r   r*   rO   rM   ÚclassmethodrW   rX   rY   ra   r:   rf   rb   r^   rD   r?   rz   r{   r~   r|   r}   r   rt   ru   rw   rv   r   r   r   r   rG   €   s6   


	'rG   )'r   r   r   r$   Zloggingr‚   r"   Zcryptography.hazmat.primitivesr   Zjosepyr   r,   r    r9   r   r   r   r   Zacme.clientr   Zcertbotr   r   r   Zcertbot._internalr	   Zcertbot.compatr
   r   Z	getLoggerr   r;   Úobjectr   ZAccountStorager6   rm   rE   rG   r   r   r   r   Ú<module>   s2   
E