
    M/e29                        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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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# ddl"m$Z$  ejJ                  e&      Z' G d d      Z( G d dejR                        Z* G d dejR                        Z+y)z!Creates ACME accounts for server.    N)Any)Callable)cast)Dict)List)Mapping)Optional)serialization)fields)messages)ClientV2)configuration)errors)
interfaces)util)	constants)
filesystem)osc            	           e Zd ZdZ G d dej
                        Z	 ddej                  dej                  de
d   ddfd	Zedefd
       ZdefdZdedefdZy)AccountzACME 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                       e Zd ZU dZ ej
                  d      Zej                  ed<    e	j                  d      Zeed<    e	j                  dd      Zeed<   y)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)	omitemptyN)__name__
__module____qualname____doc__acme_fieldsrfc3339r   datetime__annotations__josefieldr   strr        ;/usr/lib/python3/dist-packages/certbot/_internal/account.pyMetar   -   sS    
	 *=)<)<])KX&&K'TZZ8s8)tzz*;tLLr)   r+   Nregrkeymetareturnc           
         || _         || _        |b| j                  t        j                  j	                  t
        j                        j                  d      t        j                         d       n|| _
        	 t        j                         }|j'                  | j                   j                   j)                         j+                  t,        j.                  j0                  t,        j2                  j4                               |j7                         | _        y # t        $ r5 t        j                  di t        t         t"        t$        f   ddi      }Y w xY w)	N)tzr   )microsecond)r   r   r   usedforsecurityF)encodingformat)md5)r-   r,   r+   r#   nowpytzUTCreplacesocketgetfqdnr.   hashlibr6   
ValueErrornewr   r   r'   r   update
public_keypublic_bytesr
   EncodingPEMPublicFormatSubjectPublicKeyInfo	hexdigestid)selfr,   r-   r.   hashers        r*   __init__zAccount.__init__=   s   	
 &*\	 II ))---:BBqBQ ..* 	  " 8<	 			_[[]F 	dhhll--/<<"++// --BB = D 	

 ""$  	_ [[^$wsCx/@CTV[B\*]^F		_s   ;D ;EEc                     dj                  t        j                  | j                  j                        | j                  j
                  | j                  dd       S )z3Short account identification string, useful for UI.z{1}@{0} ({2})N   )r5   	pyrfc3339generater.   r   r   rH   rI   s    r*   slugzAccount.slug\   sL     %%i&8&8II!!'#$(II$;$;TWWRa[J 	Jr)   c                     dj                  | j                  j                  | j                  | j                  | j
                        S )Nz<{0}({1}, {2}, {3})>)r5   	__class__r   r,   rH   r.   rP   s    r*   __repr__zAccount.__repr__b   s6    %,,NN##TYYD 	Dr)   otherc                     t        || j                        xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S N)
isinstancerS   r-   r,   r.   )rI   rU   s     r*   __eq__zAccount.__eq__f   sT    5$..1 (EII%(*.))uzz*A(		UZZ'	)r)   rW   )r   r   r   r    r%   JSONObjectWithFieldsr+   r   RegistrationResourceJWKr	   rK   propertyr'   rQ   rT   r   boolrY   r(   r)   r*   r   r   #   s    Mt(( M" +/%X:: % %'%37%> Jc J J
D# D)C )D )r)   r   c                   h    e Zd ZdZddeeeef      ddfdZde	e   fdZ
dededdfd	Zd
edefdZy)AccountMemoryStoragezIn-memory account storage.Ninitial_accountsr/   c                 &    ||| _         y i | _         y rW   )accounts)rI   ra   s     r*   rK   zAccountMemoryStorage.__init__o   s    ,<,H(br)   c                 H    t        | j                  j                               S rW   )listrc   valuesrP   s    r*   find_allzAccountMemoryStorage.find_allr   s    DMM((*++r)   accountclientc                     |j                   | j                  v r t        j                  d|j                          || j                  |j                   <   y )NzOverwriting account: %s)rH   rc   loggerdebug)rI   rh   ri   s      r*   savezAccountMemoryStorage.saveu   s7    ::&LL2GJJ?$+gjj!r)   
account_idc                 f    	 | j                   |   S # t        $ r t        j                  |      w xY wrW   )rc   KeyErrorr   AccountNotFoundrI   rn   s     r*   loadzAccountMemoryStorage.loadz   s6    	5==,, 	5((44	5s    0rW   )r   r   r   r    r	   r   r'   r   rK   r   rg   r   rm   rs   r(   r)   r*   r`   r`   l   sg    $Q$sG|2D)E QQU Q,$w- ,,G ,X ,$ ,
5s 5w 5r)   r`   c                      e Zd ZdZdej
                  ddfdZdedefdZdededefd	Z	e
d
edefd       Ze
d
edefd       Ze
d
edefd       Zdedee   fdZdee   fdZdedededdfdZdededdfdZdededefdZdedefdZdededdfdZdeddfdZdeddfdZdeddfdZdededdfdZdeddfdZdedeegef   defdZdedefdZded eddfd!Z ded eddfd"Z!ded eddfd#Z"y)$AccountFileStoragezjAccounts file storage.

    :ivar certbot.configuration.NamespaceConfig config: Client configuration

    configr/   Nc                 |    || _         t        j                  |j                  d| j                   j                         y Ni  )rv   r   make_or_verify_diraccounts_dirstrict_permissions)rI   rv   s     r*   rK   zAccountFileStorage.__init__   s+     3 3UDKK<Z<Z[r)   rn   c                 N    | j                  || j                  j                        S rW   )!_account_dir_path_for_server_pathrv   server_pathrr   s     r*   _account_dir_pathz$AccountFileStorage._account_dir_path   s    55j$++BYBYZZr)   r~   c                 x    | j                   j                  |      }t        j                  j	                  ||      S rW   )rv   accounts_dir_for_server_pathr   pathjoin)rI   rn   r~   rz   s       r*   r}   z4AccountFileStorage._account_dir_path_for_server_path   s+    {{??Lww||L*55r)   account_dir_pathc                 B    t         j                  j                  |d      S )Nz	regr.jsonr   r   r   clsr   s     r*   
_regr_pathzAccountFileStorage._regr_path       ww||,k::r)   c                 B    t         j                  j                  |d      S )Nzprivate_key.jsonr   r   s     r*   	_key_pathzAccountFileStorage._key_path   s    ww||,.@AAr)   c                 B    t         j                  j                  |d      S )Nz	meta.jsonr   r   s     r*   _metadata_pathz!AccountFileStorage._metadata_path   r   r)   c                     | j                   j                  |      }	 t        j                  |      }g }|D ]$  }	 |j                  | j                  ||             & |sM|t        j                  v r;t        j                  |   }| j                  |      }|r	 | j                  ||       |}|S # t        $ r g cY S w xY w# t        j                  $ r t        j                  dd       Y w xY w# t        $ r g cY S w xY w)NzAccount loading problemT)exc_info)rv   r   r   listdirOSErrorappend_load_for_server_pathr   AccountStorageErrorrk   rl   r   LE_REUSE_SERVERS_find_all_for_server_path_symlink_to_accounts_dir)rI   r~   rz   
candidatesrc   rn   prev_server_pathprev_accountss           r*   r   z,AccountFileStorage._find_all_for_server_path   s   {{??L	L1J $ 	GJG : ::{ ST	G K9+E+EE(99+F ::;KLM112BKP %H+  	I	 -- G6FG  Is4   B. !B?C/ .B<;B<?*C,+C,/C=<C=c                 L    | j                  | j                  j                        S rW   )r   rv   r~   rP   s    r*   rg   zAccountFileStorage.find_all   s    --dkk.E.EFFr)   r   c                 x    | j                  ||      }| j                  ||      }t        j                  ||       y rW   )r}   r   symlink)rI   r   r~   rn   prev_account_dirnew_account_dirs         r*   _symlink_to_account_dirz*AccountFileStorage._symlink_to_account_dir   s7    AA*N^_@@[Y


#_5r)   c                 0   | j                   j                  |      }t        j                  j	                  |      rt        j
                  |       nt        j                  |       | j                   j                  |      }t        j                  ||       y rW   )rv   r   r   r   islinkunlinkrmdirr   )rI   r   r~   rz   r   s        r*   r   z+AccountFileStorage._symlink_to_accounts_dir   sc    {{??L77>>,'IIl#HH\";;CCDTU


#\2r)   c                 F   | j                  ||      }t        j                  j                  |      s|t        j
                  v r~t        j
                  |   }| j                  ||      }| j                  j                  |      }t        j                  |      r| j                  |||       |S | j                  ||       |S t        j                  d| d      	 t        | j                  |            5 }t         j"                  j%                  |j'                               }d d d        t        | j)                  |            5 }	t*        j,                  j%                  |	j'                               }
d d d        t        | j/                  |            5 }t0        j2                  j%                  |j'                               }d d d        t1        
      S # 1 sw Y   xY w# 1 sw Y   rxY w# 1 sw Y   .xY w# t4        $ r}t        j6                  |      d }~ww xY w)NAccount at  does not exist)r}   r   r   isdirr   r   r   rv   r   r   r   r   r   rq   openr   r   r[   
json_loadsreadr   r%   r\   r   r   r+   IOErrorr   )rI   rn   r~   r   r   prev_loaded_accountrz   	regr_filer,   key_filer-   metadata_filer.   errors                 r*   r   z(AccountFileStorage._load_for_server_path   s   AA*kZww}}-.i888#,#=#=k#J &*&@&@M]&^##{{GGT::l+001A;PZ[ +* 112BKP**((;7G6H)XYY	4doo&678 RI44??	@PQRdnn%567 ;8hh))(--/:;d))*:;< E||..}/A/A/CDE
 tS$''R R; ;E E 	4,,U33	4s`   G= 6.G$"G= .G%4"G= .G1G= G"G= %G.*G= 1G:6G= =	H HH c                 N    | j                  || j                  j                        S rW   )r   rv   r~   rr   s     r*   rs   zAccountFileStorage.load   s    ))*dkk6M6MNNr)   rh   ri   c                     	 | j                  |      }| j                  ||       | j                  ||       | j                  ||       y# t        $ r}t        j                  |      d}~ww xY w)zCreate a new account.

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

        N)_prepare_create_update_meta_update_regrr   r   r   )rI   rh   ri   dir_pathr   s        r*   rm   zAccountFileStorage.save   sd    	4}}W-HLL(+gx0gx0 	4,,U33	4s   AA
 
	A-A((A-c                     	 | j                  |      }| j                  ||       y# t        $ r}t        j                  |      d}~ww xY w)z^Update the registration resource.

        :param Account account: account to update

        N)r   r   r   r   r   rI   rh   r   r   s       r*   update_regrzAccountFileStorage.update_regr   F    	4}}W-Hgx0 	4,,U33	4   #& 	A	AA	c                     	 | j                  |      }| j                  ||       y# t        $ r}t        j                  |      d}~ww xY w)zVUpdate the meta resource.

        :param Account account: account to update

        N)r   r   r   r   r   r   s       r*   update_metazAccountFileStorage.update_meta  r   r   c                    | j                  |      }t        j                  j                  |      st	        j
                  d| d      | j                  || j                  j                         t        j                  | j                  j                        s&| j                  | j                  j                         yy)znDelete registration info from disk

        :param account_id: id of account which should be deleted

        r   r   N)r   r   r   r   r   rq   #_delete_account_dir_for_server_pathrv   r~   r   rz   $_delete_accounts_dir_for_server_path)rI   rn   r   s      r*   deletezAccountFileStorage.delete  s      11*=ww}}-.((;7G6H)XYY00T[[=T=TU zz$++22355dkk6M6MN 4r)   c                     t        j                  | j                  |      }| j                  ||      }t	        j
                  |       y rW   )	functoolspartialr}   !_delete_links_and_find_target_dirshutilrmtree)rI   rn   r~   	link_funcnonsymlinked_dirs        r*   r   z6AccountFileStorage._delete_account_dir_for_server_path!  s:    %%d&L&LjY	AA+yY&'r)   c                 ~    | j                   j                  }| j                  ||      }t        j                  |       y rW   )rv   r   r   r   r   )rI   r~   r   r   s       r*   r   z7AccountFileStorage._delete_accounts_dir_for_server_path&  s1    KK<<	AA+yY
!"r)   r   c                     ||      }i }t         j                  j                         D ]
  \  }}|||<    d}|rSd}||v rJ||   } ||      }	t        j                  j                  |	      rt        j                  |	      |k(  rd}|}|	}|rSt        j                  j                  |      rLt        j                  |      }
t        j                  |       |
}t        j                  j                  |      rL|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   itemsr   r   r   r   readlinkr   )rI   r~   r   r   reused_serverskvpossible_next_linknext_server_pathnext_dir_pathtargets              r*   r   z4AccountFileStorage._delete_links_and_find_target_dir+  s     [) ..446 	"DAq !N1	" " !&n,#1+#>  )*: ;77>>-0Z5H5H5W[c5c)-&"2K,H ! ggnnX&((2FIIhH ggnnX&
 r)   c                     | j                  |j                        }t        j                  |d| j                  j
                         |S rx   )r   rH   r   ry   rv   r{   )rI   rh   r   s      r*   r   zAccountFileStorage._prepareS  s:    11'**= 0%9W9WXr)   r   c                     t        j                  | j                  |      dd      5 }|j                  |j                  j                                d d d        y # 1 sw Y   y xY w)Nw   )chmod)r   	safe_openr   writer-   
json_dumps)rI   rh   r   r   s       r*   r   zAccountFileStorage._createX  sK    ^^DNN84cG 	58NN7;;1134	5 	5 	5s   *AA$c                     t        | j                  |      d      5 }t        j                  i |j                  j
                        }|j                  |j                                d d d        y # 1 sw Y   y xY w)Nr   )bodyuri)r   r   r   r[   r,   r   r   r   )rI   rh   r   r   r,   s        r*   r   zAccountFileStorage._update_regr\  sa    $//(+S1 	/Y00LL$$&D OODOO-.		/ 	/ 	/s   AA00A9c                     t        | j                  |      d      5 }|j                  |j                  j	                                d d d        y # 1 sw Y   y xY w)Nr   )r   r   r   r.   r   )rI   rh   r   r   s       r*   r   zAccountFileStorage._update_metac  sI    $%%h/5 	; 7 7 9:	; 	; 	;s   *AA)#r   r   r   r    r   NamespaceConfigrK   r'   r   r}   classmethodr   r   r   r   r   r   rg   r   r   r   rs   r   rm   r   r   r   r   r   r   r   r   r   r   r   r(   r)   r*   ru   ru      sc   
\}<< \ \[C [C [6C 6c 6VY 6 ;# ;# ; ; B B B B ;c ;c ; ;S T'] 6G$w- G6 6# 6,/64863 33 3SW 3( (# (' (8Os Ow O4G 4X 4$ 4
47 
4t 
4
47 
4t 
4O O O (c (PS (X\ (
# # #
&S &5=seSj5I&NQ&P   C  
5w 5# 5$ 5/G /s /t /;G ;s ;t ;r)   ru   ),r    r#   r   r=   loggingr   r;   typingr   r   r   r   r   r   r	   cryptography.hazmat.primitivesr
   josepyr%   rN   r8   acmer   r!   r   acme.clientr   certbotr   r   r   r   certbot._internalr   certbot.compatr   r   	getLoggerr   rk   r   AccountStorager`   ru   r(   r)   r*   <module>r      s    '              8    &    !    ' % 			8	$F) F)R5:44 5*d;22 d;r)   