U
    ÝÁ]HÃ  ã                   @   s  d 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
Zddlm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 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 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'm(Z) ddl*m+Z, ddl*m-Z- ddl*m.Z/ dZ0e 1e2¡Z3dd„ Z4d d!„ Z5dpd"d#„Z6d$d%„ Z7d&d'„ Z8d(d)„ Z9d*d+„ Z:d,d-„ Z;d.d/„ Z<d0d1„ Z=d2d3„ Z>dqd4d5„Z?drd6d7„Z@d8d9„ ZAd:d;„ ZBd<d=„ ZCd>d?„ ZDd@dA„ ZEdBdC„ ZFdsdDdE„ZGdFdG„ ZHdHdI„ ZIdJdK„ ZJdLdM„ ZKdNdO„ ZLdPdQ„ ZMdRdS„ ZNdTdU„ ZOdVdW„ ZPdXdY„ ZQdZd[„ ZRd\d]„ ZSd^d_„ ZTd`da„ ZUdbdc„ ZVddde„ ZWdfdg„ ZXdhdi„ ZYdjdk„ ZZdtdldm„Z[e2dnkre[ƒ Z\e\rþe3 ]doe\¡ e ^e\¡ dS )uzCertbot main entry point.é    )Úprint_functionN)Úerrors)ÚUnion)Úaccount)Úcert_manager)Úcli)Úclient)Úconfiguration)Ú	constants)Úcrypto_util)Úeff)Úhooks)Ú
interfaces)Úlog)Úrenewal)Úreporter)Ústorage)Úupdater)Úutil)Ú
filesystem)Úmisc)Úos)r   Úops)Údisco)Úenhancements)Ú	selectionz?User chose to cancel the operation and may reinvoke the client.c                 C   s<   | j dkst‚| jrdS tj tj¡}d}| ||j	¡ dS )z­Potentially suggest a donation to support Certbot.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :returns: `None`
    :rtype: None

    ÚrenewNzÀIf you like Certbot, please consider supporting our work by:

Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
Donating to EFF:                    https://eff.org/donate-le

)
ÚverbÚAssertionErrorZstagingÚzopeÚ	componentÚ
getUtilityr   Ú	IReporterÚadd_messageÚLOW_PRIORITY)ÚconfigÚreporter_utilÚmsg© r(   ú./usr/lib/python3/dist-packages/certbot/main.pyÚ _suggest_donation_if_appropriate1   s    
r*   c                 C   s2   t j tj¡}| jdkst‚|jd|jdd dS )z˜Reports on successful dry run

    :param config: Configuration object
    :type config: interfaces.IConfig

    :returns: `None`
    :rtype: None

    r   zThe dry run was successful.F)Zon_crashN)	r   r    r!   r   r"   r   r   r#   ÚHIGH_PRIORITY)r%   r&   r(   r(   r)   Ú_report_successful_dry_runE   s    
 ÿr,   c              	   C   s–   t  |¡ zz|dk	r0t d¡ t ||| |¡ nR|dk	s<t‚t d¡ |  ||¡}|dkrft	 
d¡‚n|dk	r‚t  || ¡ |j¡ W 5 t  |¡ X |S )a!  Authenticate and enroll certificate.

    This method finds the relevant lineage, figures out what to do with it,
    then performs that action. Includes calls to hooks, various reports,
    checks, and requests for user input.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param domains: List of domain names to get a certificate. Defaults to `None`
    :type domains: `list` of `str`

    :param certname: Name of new certificate. Defaults to `None`
    :type certname: str

    :param lineage: Certificate lineage object. Defaults to `None`
    :type lineage: storage.RenewableCert

    :returns: the issued certificate or `None` if doing a dry run
    :rtype: storage.RenewableCert or None

    :raises errors.Error: if certificate could not be obtained

    Nz Renewing an existing certificatezObtaining a new certificateFz!Certificate could not be obtained)r   Zpre_hookZ	post_hookÚloggerÚinfor   Ú
renew_certr   Zobtain_and_enroll_certificater   ÚErrorZdeploy_hookÚnamesÚlive_dir)Ú	le_clientr%   ÚdomainsÚcertnameÚlineager(   r(   r)   Ú_get_and_save_certU   s    


r7   c              
   C   s®   d  | ¡ ¡}dj|jj|d  |¡tjd}| jsV| jsVt	j
 tj¡j|dddddr^d	|fS t	j
 tj¡}| d
j|tjd d  tjdd… ¡tjd|j¡ t t¡‚dS )aù  Figure out what to do if a previous cert had a subset of the names now requested

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param domains: List of domain names
    :type domains: `list` of `str`

    :param cert: Certificate object
    :type cert: storage.RenewableCert

    :returns: Tuple of (str action, cert_or_None) as per _find_lineage_for_domains_and_certname
              action can be: "newcert" | "renew" | "reinstall"
    :rtype: `tuple` of `str`

    z, a   You have an existing certificate that contains a portion of the domains you requested (ref: {0}){br}{br}It contains these names: {1}{br}{br}You requested these names for the new certificate: {2}.{br}{br}Do you want to expand and replace this existing certificate with the new certificate?©ÚbrZExpandÚCancelz--expandT©Zcli_flagÚforce_interactiver   z¾To obtain a new certificate that contains these names without replacing your existing certificate for {0}, you must use the --duplicate option.{br}{br}For example:{br}{br}{1} --duplicate {2}r   ú é   N)Újoinr1   ÚformatÚ
configfileÚfilenamer   ÚlinesepÚexpandZrenew_by_defaultr   r    r!   r   ÚIDisplayÚyesnor"   r#   ÚsysÚargvr+   r   r0   ÚUSER_CANCELLED)r%   r4   ÚcertÚexistingÚquestionr&   r(   r(   r)   Ú_handle_subset_cert_request„   s:    ÿ÷
ÿ  ý ú÷
rM   c                 C   sÚ   |  ¡ sd|fS t | |¡r$d|fS | jr2d|fS dj|jjtjd}| j	dkrVd}n| j	dkrdd}|d	g}t
j tj¡}|j||d
dd}|d
 tjkr¦t d¡‚n(|d d
krºd|fS |d dkrÎd|fS tdƒ‚dS )a¼  Figure out what to do if a lineage has the same names as a previously obtained one

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param lineage: Certificate lineage object
    :type lineage: storage.RenewableCert

    :returns: Tuple of (str action, cert_or_None) as per _find_lineage_for_domains_and_certname
              action can be: "newcert" | "renew" | "reinstall"
    :rtype: `tuple` of `str`

    Ú	reinstallr   z¯You have an existing certificate that has exactly the same domains or certificate name you requested and isn't close to expiry.{br}(ref: {0}){br}{br}What would you like to do?r8   Úrunz.Attempt to reinstall this existing certificateÚcertonlyz%Keep the existing certificate for nowz.Renew & replace the cert (limit ~5 per 7 days)r   T)Údefaultr<   z.Operation canceled. You may re-run the client.r>   zThis is impossibleN)Zensure_deployedr   Zshould_renewrN   r@   rA   rB   r   rC   r   r   r    r!   r   rE   ZmenuÚdisplay_utilZCANCELr   r0   r   )r%   r6   rL   Zkeep_optÚchoicesÚdisplayZresponser(   r(   r)   Ú_handle_identical_cert_request´   s@    ÿ ü

ÿ ÿÿrU   c                 C   sX   | j r
dS t | |¡\}}|dkr.|dkr.dS |dk	r@t| |ƒS |dk	rTt| ||ƒS dS )aö  Determine whether there are duplicated names and how to handle
    them (renew, reinstall, newcert, or raising an error to stop
    the client run if the user chooses to cancel the operation when
    prompted).

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param domains: List of domain names
    :type domains: `list` of `str`

    :returns: Two-element tuple containing desired new-certificate behavior as
              a string token ("reinstall", "renew", or "newcert"), plus either
              a RenewableCert instance or `None` if renewal shouldn't occur.
    :rtype: `tuple` of `str` and :class:`storage.RenewableCert` or `None`

    :raises errors.Error: If the user would like to rerun the client again.

    ©ZnewcertNN©NN)Z	duplicater   Zfind_duplicative_certsrU   rM   )r%   r4   Zident_names_certZsubset_names_certr(   r(   r)   Ú_find_lineage_for_domainsæ   s    
rX   c                 C   s.   t | ||ƒ\}}|dkr"t d¡ |dk|fS )aW  Finds an existing certificate object given domains and/or a certificate name.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param domains: List of domain names
    :type domains: `list` of `str`

    :param certname: Name of certificate
    :type certname: str

    :returns: Two-element tuple of a boolean that indicates if this function should be
              followed by a call to fetch a certificate from the server, and either a
              RenewableCert instance or None.
    :rtype: `tuple` of `bool` and :class:`storage.RenewableCert` or `None`

    rN   z Keeping the existing certificate)Ú&_find_lineage_for_domains_and_certnamer-   r.   )r%   r4   r5   Úactionr6   r(   r(   r)   Ú
_find_cert  s    
r[   c                 C   sz   |st | |ƒS t | |¡}|r^|rTtt | |¡ƒt|ƒkrTt| ||| ¡ ƒ d|fS t| |ƒS |rfdS t 	d 
|¡¡‚dS )a¦  Find appropriate lineage based on given domains and/or certname.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param domains: List of domain names
    :type domains: `list` of `str`

    :param certname: Name of certificate
    :type certname: str

    :returns: Two-element tuple containing desired new-certificate behavior as
              a string token ("reinstall", "renew", or "newcert"), plus either
              a RenewableCert instance or None if renewal should not occur.

    :rtype: `tuple` of `str` and :class:`storage.RenewableCert` or `None`

    :raises errors.Error: If the user would like to rerun the client again.

    r   rV   z}No certificate with name {0} found. Use -d to specify domains, or run certbot certificates to see possible certificate names.N)rX   r   Úlineage_for_certnameÚsetÚdomains_for_certnameÚ_ask_user_to_confirm_new_namesr1   rU   r   ÚConfigurationErrorr@   )r%   r4   r5   r6   r(   r(   r)   rY   $  s     
ÿ
þrY   c                 C   s@   t t| ƒt|ƒ ƒ}t t|ƒt| ƒ ƒ}| ¡  | ¡  ||fS )zWGet lists of items removed from `before`
    and a lists of items added to `after`
    )Úlistr]   Úsort)ZafterZbeforeÚaddedÚremovedr(   r(   r)   Ú_get_added_removedM  s
    re   c                 C   s(   |s
d}ndd  |¡ }|j| tjdS )z%Format list with given character
    z
{br}(None)z	{br}{ch} )Zchr9   )r?   r@   r   rC   )Ú	characterZstringsZ	formattedr(   r(   r)   Ú_format_listW  s    þrg   c                 C   sf   | j r
dS t||ƒ\}}dj|td|ƒtd|ƒtjd}tj t	j
¡}|j|dddd	sbt d
¡‚dS )a÷  Ask user to confirm update cert certname to contain new_domains.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param new_domains: List of new domain names
    :type new_domains: `list` of `str`

    :param certname: Name of certificate
    :type certname: str

    :param old_domains: List of old domain names
    :type old_domains: `list` of `str`

    :returns: None
    :rtype: None

    :raises errors.ConfigurationError: if cert name and domains mismatch

    Nz©You are updating certificate {0} to include new domain(s): {1}{br}{br}You are also removing previously included domain(s): {2}{br}{br}Did you intend to make this change?ú+ú-r8   zUpdate certr:   T)rQ   z+Specified mismatched cert name and domains.)Zrenew_with_new_domainsre   r@   rg   r   rC   r   r    r!   r   rE   rF   r   r`   )r%   Znew_domainsr5   Zold_domainsrc   rd   r'   Úobjr(   r(   r)   r_   c  s    úr_   c                 C   sR   d}| j }| jr| j}n|r(t | |¡}|s8t ||¡}|sJ|sJt d¡‚||fS )a  Retrieve domains and certname from config or user input.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param installer: Installer object
    :type installer: interfaces.IInstaller

    :param `str` question: Overriding dialog question to ask the user if asked
        to choose from domain names.

    :returns: Two-part tuple of domains and certname
    :rtype: `tuple` of list of `str` and `str`

    :raises errors.Error: Usage message, if parameters are not used correctly

    Nz‡Please specify --domains, or --installer that will help in domain names autodiscovery, or --cert-name for an existing certificate name.)r5   r4   r   r^   Údisplay_opsZchoose_namesr   r0   )r%   Ú	installerrL   r4   r5   r(   r(   r)   Ú_find_domains_or_certnameˆ  s    
rm   c           	      C   s”   | j rt| ƒ dS |r|s"tdƒ‚t |¡ ¡ }tj t	j
¡}| jdkrLdnd}|rddj|tjdnd}dj|||tj|tjd}| ||j¡ dS )	aD  Reports the creation of a new certificate to the user.

    :param cert_path: path to certificate
    :type cert_path: str

    :param fullchain_path: path to full chain
    :type fullchain_path: str

    :param key_path: path to private key, if available
    :type key_path: str

    :returns: `None`
    :rtype: None

    Nz No certificates saved to report.rO   z with the "certonly" optionÚ z+Your key file has been saved at:{br}{0}{br}r8   a  Congratulations! Your certificate and chain have been saved at:{br}{0}{br}{1}Your cert will expire on {2}. To obtain a new or tweaked version of this certificate in the future, simply run {3} again{4}. To non-interactively renew *all* of your certificates, run "{3} renew")Údry_runr,   r   r   ZnotAfterÚdater   r    r!   r   r"   r   r@   r   rC   r   Zcli_commandr#   ÚMEDIUM_PRIORITY)	r%   Ú	cert_pathÚfullchain_pathÚkey_pathZexpiryr&   Z
verbswitchZprivkey_statementr'   r(   r(   r)   Ú_report_new_cert±  s.    ÿ ÿÿ    úru   c                    sð   ‡ fdd„}t  ˆ ¡}d}ˆ j dk	r2| ˆ j ¡}n®| ¡ }t|ƒdkrRt |¡}nŽt|ƒdkrh|d }nxˆ jdkr‚ˆ js‚t 	¡ ˆ _zt
jˆ ||d\}}W nD tjk
r²   ‚ Y n. tjk
rÞ   tjddd	 t d
¡‚Y nX |jˆ _ ||fS )aè  Determine which account to use.

    If ``config.account`` is ``None``, it will be updated based on the
    user input. Same for ``config.email``.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :returns: Account and optionally ACME client API (biproduct of new
        registration).
    :rtype: tuple of :class:`certbot.account.Account` and :class:`acme.client.Client`

    :raises errors.Error: If unable to register an account with ACME server

    c                    sL   ˆ j r
dS d | ˆ j¡}tj tj¡}|j|ddddd}|sHt	 
d¡‚d S )NTzhPlease read the Terms of Service at {0}. You must agree in order to register with the ACME server at {1}ZAgreer:   z--agree-tosr;   z?Registration cannot proceed without accepting Terms of Service.)Ztosr@   Zserverr   r    r!   r   rE   rF   r   r0   )Zterms_of_servicer'   rj   Úresult©r%   r(   r)   Ú_tos_cbë  s      ý
 ÿÿz#_determine_account.<locals>._tos_cbNr>   r   )Ztos_cbrn   T)Úexc_infoz.Unable to register an account with ACME server)r   ÚAccountFileStorageÚloadÚfind_allÚlenrk   Zchoose_accountÚemailÚregister_unsafely_without_emailÚ	get_emailr   Úregisterr   ZMissingCommandlineFlagr0   r-   ÚdebugÚid)r%   rx   Úaccount_storageÚacmeÚaccÚaccountsr(   rw   r)   Ú_determine_accountÛ  s6    



  ÿÿ
rˆ   c              
      sD  t j tj¡}t j tj¡}| j}|dkrBd}|j|ddddd}|sX| d|j	¡ dS | j
sbt‚| jstt | ¡| _t t t | | j¡¡| | j¡‰ z&t | ‡ fdd	„gd
d	„ dd	„ ¡ W nz tjk
rð   d ˆ ¡}| d |¡|j¡ Y dS  tk
r4 } z&d}| | j| jˆ |¡}t |¡‚W 5 d}~X Y nX t | ¡ dS )a›  Does the user want to delete their now-revoked certs? If run in non-interactive mode,
    deleting happens automatically.

    :param config: parsed command line arguments
    :type config: interfaces.IConfig

    :returns: `None`
    :rtype: None

    :raises errors.Error: If anything goes wrong, including bad user input, if an overlapping
        archive dir is found for the specified lineage, etc ...
    NzmWould you like to delete the cert(s) you just revoked, along with all earlier and later versions of the cert?zYes (recommended)ZNoT)Ú	yes_labelÚno_labelr<   rQ   zNot deleting revoked certs.c                    s   ˆ S ©Nr(   ©Úx©Zarchive_dirr(   r)   Ú<lambda>>  ó    z(_delete_if_appropriate.<locals>.<lambda>c                 S   s   | j S r‹   rŽ   rŒ   r(   r(   r)   r   ?  r   c                 S   s   | S r‹   r(   rŒ   r(   r(   r)   r   ?  r   z^Not deleting revoked certs due to overlapping archive dirs. More than one lineage is using {0}rn   z_config.default_archive_dir: {0}, config.live_dir: {1}, archive_dir: {2},original exception: {3})r   r    r!   r   rE   r"   Zdelete_after_revokerF   r#   r$   rr   r   r5   r   Zcert_path_to_lineager   Zfull_archive_pathÚ	configobjZ	ConfigObjZrenewal_file_for_certnameZmatch_and_check_overlapsr   ZOverlappingMatchFoundr@   r?   rq   Ú	ExceptionZdefault_archive_dirr2   r0   Údelete)r%   rT   r&   Zattempt_deletionr'   Úer(   rŽ   r)   Ú_delete_if_appropriate  sH    
 ÿ
 þ ÿÿr•   c                 C   s>   |dk	r"t | ƒ\}}t d|¡ nd\}}tj| ||||dS )an  Initialize Let's Encrypt Client

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param authenticator: Acme authentication handler
    :type authenticator: interfaces.IAuthenticator
    :param installer: Installer object
    :type installer: interfaces.IInstaller

    :returns: client: Client object
    :rtype: client.Client

    NzPicked account: %rrW   ©r…   )rˆ   r-   r‚   r   ÚClient)r%   Úauthenticatorrl   r†   r…   r(   r(   r)   Ú_init_le_clientN  s
    r™   c                 C   sª   t  | ¡}| ¡ }tj tj¡}|s(dS tj tj¡j	}d}||dddd}|sTdS t
| ƒ\}}	tj| |dd|	d	}
|
j |j¡ t  | ¡}| | j ¡ | d
|j¡ dS )zúDeactivate account on server

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param unused_plugins: List of plugins (deprecated)
    :type unused_plugins: `list` of `str`

    :returns: `None`
    :rtype: None

    z.Could not find existing account to deactivate.zCAre you sure you would like to irrevocably deactivate your account?Z
DeactivateZAbortT)r‰   rŠ   rQ   zDeactivation aborted.Nr–   zAccount deactivated.)r   rz   r|   r   r    r!   r   r"   rE   rF   rˆ   r   r—   r…   Zdeactivate_registrationÚregrr“   r#   rq   )r%   Úunused_pluginsr„   r‡   r&   rF   ÚpromptZwants_deactivater†   r…   Ú	cb_clientZaccount_filesr(   r(   r)   Ú
unregisteri  s&    
ÿ
rž   c                 C   sD   | j rd}t |¡ t| |ƒS t | ¡}| ¡ }|r8dS t| ƒ dS )a$  Create accounts on the server.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param unused_plugins: List of plugins (deprecated)
    :type unused_plugins: `list` of `str`

    :returns: `None` or a string indicating and error
    :rtype: None or str

    zuUsage 'certbot register --update-registration' is deprecated.
Please use 'certbot update_account [options]' instead.
zmThere is an existing account; registration of a duplicate account with this command is currently unsupported.N)Úupdate_registrationr-   ÚwarningÚupdate_accountr   rz   r|   rˆ   )r%   r›   r'   r„   r‡   r(   r(   r)   r   ’  s    


r   c           
         sô   t  | ¡}| ¡ }tj tj¡‰ ‡ fdd„}|s4dS | jdkrV| j	rHdS t
jdd| _t| ƒ\}}tj| |dd|d}d	d
„ | j d¡D ƒ}|jj}	|j |jj|jjj|dd¡|_|jj|	d|_| ||j¡ t | ¡ |d | j¡ƒ dS )a$  Modify accounts on the server.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param unused_plugins: List of plugins (deprecated)
    :type unused_plugins: `list` of `str`

    :returns: `None` or a string indicating and error
    :rtype: None or str

    c                    s   ˆ   | ˆ j¡S r‹   )r#   rq   )Úm©r&   r(   r)   r   Ê  r   z update_account.<locals>.<lambda>z-Could not find an existing account to update.Nz‚--register-unsafely-without-email provided, however, a new e-mail address must
currently be provided when updating a registration.F)Zoptionalr–   c                 S   s   g | ]}d | ‘qS )zmailto:r(   )Ú.0r~   r(   r(   r)   Ú
<listcomp>Ø  s     z"update_account.<locals>.<listcomp>ú,)Zcontact)Úbody)Úuriz'Your e-mail address was updated to {0}.)r   rz   r|   r   r    r!   r   r"   r~   r   rk   r€   rˆ   r   r—   Úsplitrš   r¨   r…   rŸ   Úupdater§   Z	save_regrr   Zhandle_subscriptionr@   )
r%   r›   r„   r‡   Zadd_msgr†   r…   r   Zacc_contactsZprev_regr_urir(   r£   r)   r¡   ¸  s,    

ÿ

r¡   c                 C   sF   |r|n| }|j dk	st‚| ||j|j |j|j¡ | ||j¡ dS )a†  Install a cert

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param le_client: Client object
    :type le_client: client.Client

    :param domains: List of domains
    :type domains: `list` of `str`

    :param lineage: Certificate lineage object. Defaults to `None`
    :type lineage: storage.RenewableCert

    :returns: `None`
    :rtype: None

    N)rr   r   Zdeploy_certificatert   Ú
chain_pathrs   Úenhance_config)r%   r3   r4   r6   Zpath_providerr(   r(   r)   Ú_install_certå  s    
  ÿr­   c           
   
   C   s*  zt  | |d¡\}}W n0 tjk
rF } zt|ƒ W Y ¢S d}~X Y nX | joR| j}| jsz|szd}tj	| dd|dd | _t
 | |¡st d¡‚| jr t| ƒ} nt
 | ¡r´t d¡‚| jrò| jròt| ƒ t| |ƒ\}}t| d|d	}t| ||ƒ n
t d
¡‚t
 | ¡r&t | | j¡}	t
 |	||| ¡ dS )zòInstall a previously obtained cert in a server.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param plugins: List of plugins
    :type plugins: `list` of `str`

    :returns: `None`
    :rtype: None

    ÚinstallNz,Which certificate would you like to install?F©Zallow_multipleZcustom_promptr   úVOne ore more of the requested enhancements are not supported by the selected installerzLOne or more of the requested enhancements require --cert-name to be provided©r˜   rl   z¤Path to certificate or key was not defined. If your certificate is managed by Certbot, please use --cert-name to define which certificate you would like to install.)Úplug_selÚchoose_configurator_pluginsr   ÚPluginSelectionErrorÚstrrt   rr   r5   r   Úget_certnamesr   Úare_supportedÚNotSupportedErrorÚ_populate_from_certnameÚare_requestedr`   Ú_check_certificate_and_keyrm   r™   r­   r\   Úenable)
r%   Úpluginsrl   Ú_r”   Zcustom_certÚcertname_questionr4   r3   r6   r(   r(   r)   r®      s>    
  þþ




r®   c                 C   sZ   t  | | j¡}|s| S | js&|j| j_| js6|j| j_| jsF|j| j_| jsV|j| j_| S )zfHelper function for install to populate missing config values from lineage
    defined by --cert-name.)r   r\   r5   rt   Ú	namespacerr   r«   rs   )r%   r6   r(   r(   r)   r¹   ;  s    



r¹   c                 C   sP   t j t | j¡¡s&t d | j¡¡‚t j t | j	¡¡sLt d | j	¡¡‚d S )Nz-Error while reading certificate from path {0}z-Error while reading private key from path {0})
r   ÚpathÚisfiler   Úrealpathrr   r   r`   r@   rt   rw   r(   r(   r)   r»   L  s    ÿÿr»   c                 C   sÔ   t  d| j¡ | jdkrg n| j}| ¡  |¡}t  d|¡ tjtj t	j
¡jdd}| jsr| jsr|t|ƒƒ dS | | ¡ | |¡}t  d|¡ | js¨|t|ƒƒ dS | ¡  | ¡ }t  d|¡ |t|ƒƒ dS )zàList server software plugins.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param plugins: List of plugins
    :type plugins: `list` of `str`

    :returns: `None`
    :rtype: None

    zExpected interfaces: %sNzFiltered plugins: %rF©ÚpausezVerified plugins: %rzPrepared plugins: %s)r-   r‚   ÚifacesZvisibleÚ	functoolsÚpartialr   r    r!   r   rE   ÚnotificationZinitZpreparerµ   ZverifyÚ	available)r%   r½   rÆ   ZfilteredÚnotifyZverifiedrÊ   r(   r(   r)   Úplugins_cmdS  s,    
ÿÿ

rÌ   c              
      sh  ddddg}t ‡ fdd„|D ƒƒ}t ˆ ¡sP|sPd}t |tjd ¡ t d	¡‚zt	 
ˆ |d
¡\}}W n0 tjk
r– } zt|ƒ W Y ¢S d}~X Y nX t ˆ |¡s®t d¡‚d}tjˆ d
d|dd ˆ _t ˆ ˆ j¡}	ˆ jrä|	}
n d}t |	|¡}
|
st d¡‚t ˆ ˆ j¡}ˆ js"|jˆ _|rHtˆ d|d}|j|
ˆ jdd t ˆ ¡rdt ||
|ˆ ¡ dS )zöAdd security enhancements to existing configuration

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param plugins: List of plugins
    :type plugins: `list` of `str`

    :returns: `None`
    :rtype: None

    ZhstsZredirectZuirZstaplec                    s   g | ]}t ˆ |ƒ‘qS r(   )Úgetattr)r¤   Zenhrw   r(   r)   r¥   ‰  s     zenhance.<locals>.<listcomp>z|Please specify one or more enhancement types to configure. To list the available enhancement types, run:

%s --help enhance
r   z#No enhancements requested, exiting.ÚenhanceNr°   zFWhich certificate would you like to use to enhance your configuration?Fr¯   zJWhich domain names would you like to enable the selected enhancements for?zAUser cancelled the domain selection. No domains defined, exiting.r±   )Zask_redirect)Úanyr   rº   r-   r    rG   rH   r   ZMisconfigurationErrorr²   r³   r´   rµ   r·   r¸   r   r¶   r5   r^   Únoninteractive_moderk   Zchoose_valuesr0   r\   r«   r™   r¬   r¼   )r%   r½   Zsupported_enhancementsZoldstyle_enhr'   rl   r¾   r”   r¿   Zcert_domainsr4   Zdomain_questionr6   r3   r(   rw   r)   rÎ   z  sJ    

  þþ
rÎ   c                 C   s   t  | j| j| |¡ dS )zýRollback server configuration changes made during install.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param plugins: List of plugins
    :type plugins: `list` of `str`

    :returns: `None`
    :rtype: None

    N)r   Úrollbackrl   Zcheckpoints)r%   r½   r(   r(   r)   rÑ   µ  s    rÑ   c                 C   s   t  d¡ t | ¡ dS )aP  Show changes made to server config during installation

    View checkpoints and associated configuration changes.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param unused_plugins: List of plugins (deprecated)
    :type unused_plugins: `list` of `str`

    :returns: `None`
    :rtype: None

    zZThe config_changes subcommand has been deprecated and will be removed in a future release.N)r-   r    r   Zview_config_changes©r%   r›   r(   r(   r)   Úconfig_changesÅ  s    
rÓ   c                 C   s   t  | ¡ dS )ar  Update the certificate file family symlinks

    Use the information in the config file to make symlinks point to
    the correct archive directory.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param unused_plugins: List of plugins (deprecated)
    :type unused_plugins: `list` of `str`

    :returns: `None`
    :rtype: None

    N)r   Zupdate_live_symlinksrÒ   r(   r(   r)   Úupdate_symlinksØ  s    rÔ   c                 C   s   t  | ¡ dS )aA  Rename a certificate

    Use the information in the config file to rename an existing
    lineage.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param unused_plugins: List of plugins (deprecated)
    :type unused_plugins: `list` of `str`

    :returns: `None`
    :rtype: None

    N)r   Zrename_lineagerÒ   r(   r(   r)   Úrenameê  s    rÕ   c                 C   s   t  | ¡ dS )aA  Delete a certificate

    Use the information in the config file to delete an existing
    lineage.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param unused_plugins: List of plugins (deprecated)
    :type unused_plugins: `list` of `str`

    :returns: `None`
    :rtype: None

    N)r   r“   rÒ   r(   r(   r)   r“   ü  s    r“   c                 C   s   t  | ¡ dS )a  Display information about certs configured with Certbot

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param unused_plugins: List of plugins (deprecated)
    :type unused_plugins: `list` of `str`

    :returns: `None`
    :rtype: None

    N)r   ÚcertificatesrÒ   r(   r(   r)   rÖ     s    rÖ   c              
   C   s`  d | _ | _| jdkr.| jr.t | | j¡| _n| jr@| jrJ| jrJt d¡‚| jdk	r¦t	 
d| jd | jd ¡ t | jd | jd ¡ tj | jd ¡}t | |¡}n0t	 
d| jd ¡ t| ƒ\}}t | |j|j¡}t | jd ¡d }t	 
d| j¡ z | t |¡| j¡ t| ƒ W n2 tjk
rJ } zt|ƒ W Y ¢S d}~X Y nX t | jd ¡ dS )a:  Revoke a previously obtained certificate.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param unused_plugins: List of plugins (deprecated)
    :type unused_plugins: `list` of `str`

    :returns: `None` or string indicating error in case of error
    :rtype: None or str

    NzCError! Exactly one of --cert-path or --cert-name must be specified!zRevoking %s using cert key %sr   r>   zRevoking %s using Account KeyzReason code for revocation: %s)rl   r˜   rr   r5   r   Zcert_path_for_cert_namer   r0   rt   r-   r‚   r   Zverify_cert_matches_priv_keyÚjoseZJWKr{   r   Zacme_from_config_keyrˆ   Úkeyrš   Zpyopenssl_load_certificateÚreasonÚrevokeZComparableX509r•   Úacme_errorsZClientErrorrµ   rk   Zsuccess_revocation)r%   r›   rØ   r…   r†   r¾   rJ   r”   r(   r(   r)   rÚ     s2    

 ÿrÚ   c              
   C   s:  zt  | |d¡\}}W n0 tjk
rF } zt|ƒ W Y ¢S d}~X Y nX t | |¡s^t d¡‚t| ||ƒ}t	| |ƒ\}}t
| ||ƒ\}}	|	}
|r t|| |||	ƒ}
|
rª|
jnd}|
r¸|
jnd}|
rÆ|
jnd}t| |||ƒ t| |||
ƒ t | ¡r|
rt |
||| ¡ |	dks|s$t |¡ n
t |¡ t| ƒ dS )zäObtain a certificate and install.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param plugins: List of plugins
    :type plugins: `list` of `str`

    :returns: `None`
    :rtype: None

    rO   Nr°   )r²   r³   r   r´   rµ   r   r·   r¸   r™   rm   r[   r7   rr   rs   rt   ru   r­   rº   r¼   rk   Zsuccess_installationZsuccess_renewalr*   )r%   r½   rl   r˜   r”   r3   r4   r5   Úshould_get_certr6   Znew_lineagerr   rs   rt   r(   r(   r)   rO   K  s6    
 ÿ
rO   c              	   C   sn   | j \}}| |¡\}}| jr0t d| j¡ dS | ||tj 	| j¡tj 	| j
¡tj 	| j¡¡\}}}||fS )aô  Obtain a cert using a user-supplied CSR

    This works differently in the CSR case (for now) because we don't
    have the privkey, and therefore can't construct the files for a lineage.
    So we just save the cert & chain to disk :/

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param client: Client object
    :type client: client.Client

    :returns: `cert_path` and `fullchain_path` as absolute paths to the actual files
    :rtype: `tuple` of `str`

    z*Dry run: skipping saving certificate to %srW   )Z
actual_csrZobtain_certificate_from_csrro   r-   r‚   rr   Zsave_certificater   rÁ   Únormpathr«   rs   )r%   r3   Úcsrr¾   rJ   Úchainrr   rs   r(   r(   r)   Ú_csr_get_and_save_cert‚  s     
 ÿ   þ
rà   c           	   
   C   sÄ   zt  | |d¡\}}W n2 tjk
rH } zt d|¡ ‚ W 5 d}~X Y nX t| ||ƒ}t|| |d}tj	 
tj¡j}|dkr’|d |j¡dd n.t | ||¡ | ¡  |d | j|j¡dd dS )	a±  Renew & save an existing cert. Do not install it.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param plugins: List of plugins
    :type plugins: `list` of `str`

    :param lineage: Certificate lineage object
    :type lineage: storage.RenewableCert

    :returns: `None`
    :rtype: None

    :raises errors.PluginSelectionError: MissingCommandlineFlag if supplied parameters do not pass

    rP   ú'Could not choose appropriate plugin: %sN)r6   z9new certificate deployed without reload, fullchain is {0}FrÄ   zDnew certificate deployed with reload of {0} server; fullchain is {1})r²   r³   r   r´   r-   r.   r™   r7   r   r    r!   r   rE   rÉ   r@   Z	fullchainr   Zrun_renewal_deployerZrestartrl   )	r%   r½   r6   rl   Úauthr”   r3   Zrenewed_lineagerË   r(   r(   r)   r/   ž  s,    ÿÿ ÿÿr/   c              
   C   s  zt  | |d¡\}}W n2 tjk
rH } zt d|¡ ‚ W 5 d}~X Y nX t| ||ƒ}| jr‚t| |ƒ\}}t	| ||ƒ t
| ƒ dS t| |ƒ\}}	t| ||	ƒ\}
}|
sÄtj tj¡j}|ddd dS t|| ||	|ƒ}|rÞ|jnd}|rì|jnd}|rú|jnd}t	| |||ƒ t
| ƒ dS )af  Authenticate & obtain cert, but do not install it.

    This implements the 'certonly' subcommand.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param plugins: List of plugins
    :type plugins: `list` of `str`

    :returns: `None`
    :rtype: None

    :raises errors.Error: If specified plugin could not be used

    rP   rá   Nz5Certificate not yet due for renewal; no action taken.FrÄ   )r²   r³   r   r´   r-   r.   r™   rÞ   rà   ru   r*   rm   r[   r   r    r!   r   rE   rÉ   r7   rr   rs   rt   )r%   r½   rl   râ   r”   r3   rr   rs   r4   r5   rÜ   r6   rË   rt   r(   r(   r)   rP   È  s.    rP   c                 C   s   zt | ¡ W 5 t  ¡  X dS )a  Renew previously-obtained certificates.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :param unused_plugins: List of plugins (deprecated)
    :type unused_plugins: `list` of `str`

    :returns: `None`
    :rtype: None

    N)r   Zrun_saved_post_hooksr   Zhandle_renewal_requestrÒ   r(   r(   r)   r   ù  s    r   c                 C   sV   t  | jtj| j¡ t  | jtj| j¡ | j| j| j	f}|D ]}t j
|| jd q<dS )z¼Create or verify existence of config, work, and hook directories.

    :param config: Configuration object
    :type config: interfaces.IConfig

    :returns: `None`
    :rtype: None

    )ÚstrictN)r   Zset_up_core_dirZ
config_dirr
   ZCONFIG_DIRS_MODEZstrict_permissionsZwork_dirZrenewal_pre_hooks_dirZrenewal_deploy_hooks_dirZrenewal_post_hooks_dirZmake_or_verify_dir)r%   Z	hook_dirsZhook_dirr(   r(   r)   Úmake_or_verify_needed_dirs  s    
þrä   c                 C   sT   | j r d| _t ttjdƒ¡}n$| jr4t tj¡}nt 	tj| j
¡}tj |¡ dS )zŒSet the displayer

    :param config: Configuration object
    :type config: interfaces.IConfig

    :returns: `None`
    :rtype: None

    TÚwN)ÚquietrÐ   rR   ZNoninteractiveDisplayÚopenr   ÚdevnullrG   ÚstdoutZFileDisplayr<   r   r    ÚprovideUtility)r%   Z	displayerr(   r(   r)   Úset_displayer   s    
ÿrë   c                 C   s   | st jdd… } t ¡  tj ¡ }t dt	j
¡ t d| ¡ t d|¡ t || ¡}t |¡}tj |¡ t ¡  zt |¡ t|ƒ W n" tjk
r¬   |jtkr¨‚ Y nX t jdd… dkrÊt d¡ t|ƒ t |¡}tj |¡ t  |j!¡ | ||¡S )	zðCommand line argument parsing and main script execution.

    :returns: result of requested command

    :raises errors.Error: OS errors triggered by wrong permissions
    :raises errors.Error: error if plugin command is not supported

    r>   Nzcertbot version: %szArguments: %rzDiscovered plugins: %ré   )é   é   zgPython 3.4 support will be dropped in the next release of Certbot - please upgrade your Python version.)"rG   rH   r   Zpre_arg_parse_setupÚplugins_discoZPluginsRegistryr|   r-   r‚   ÚcertbotÚ__version__r   Zprepare_and_parse_argsr	   ZNamespaceConfigr   r    rê   r   Z+raise_for_non_administrative_windows_rightsZpost_arg_parse_setuprä   r   r0   ÚfuncrÌ   Úversion_infor    rë   r   ZReporterr   Zatexit_registerZprint_messages)Zcli_argsr½   Úargsr%   Úreportr(   r(   r)   Úmain6  s0    	





rö   Ú__main__zExiting with message %s)NNN)N)N)N)N)_Ú__doc__Z
__future__r   rÇ   Zlogging.handlersZloggingrG   r‘   Zjosepyr×   Zzope.componentr   r…   r   rÛ   Zacme.magic_typingr   rð   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   Zcertbot.compatr   r   r   Zcertbot.displayrR   r   rk   Zcertbot.pluginsr   rï   r   r   r²   rI   Z	getLoggerÚ__name__r-   r*   r,   r7   rM   rU   rX   r[   rY   re   rg   r_   rm   ru   rˆ   r•   r™   rž   r   r¡   r­   r®   r¹   r»   rÌ   rÎ   rÑ   rÓ   rÔ   rÕ   r“   rÖ   rÚ   rO   rà   r/   rP   r   rä   rë   rö   Z
err_stringr    Úexitr(   r(   r(   r)   Ú<module>   s¢   

/02')
%
)
*<7)&-
;';-7*1
3
