
\\                 @   sO  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 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% m& Z' d d l( m) Z* d d l( m+ Z, d d l( m- Z- d Z. e j/ e0  Z1 d d   Z2 d d   Z3 d d d d  d!  Z4 d" d#   Z5 d$ d%   Z6 d& d'   Z7 d( d)   Z8 d* d+   Z9 d, d-   Z: d. d/   Z; d0 d1   Z< d d2 d3  Z= d d4 d5  Z> d6 d7   Z? d8 d9   Z@ d: d;   ZA d< d=   ZB d> d?   ZC d@ dA   ZD d dB dC  ZE dD dE   ZF dF dG   ZG dH dI   ZH dJ dK   ZI dL dM   ZJ dN dO   ZK dP dQ   ZL dR dS   ZM dT dU   ZN dV dW   ZO dX dY   ZP dZ d[   ZQ d\ d]   ZR d^ d_   ZS d` da   ZT db dc   ZU dd de   ZV df dg   ZW dh di   ZX e jY dj d  dk dl  ZZ e0 dm k rKeZ   Z[ e[ r>e1 j\ dn e[  e j] e[  d S)ozCertbot main entry point.    )print_functionN)errors)Union)account)cert_manager)cli)client)compat)configuration)	constants)crypto_util)eff)hooks)
interfaces)log)renewal)reporter)storage)updater)util)r   ops)disco)	selection)enhancementsz?User chose to cancel the operation and may reinvoke the client.c             C   sT   |  j  d k s t  |  j r" d St j j t j  } d } | j | | j	  d S)zPotentially suggest a donation to support Certbot.

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

    :returns: `None`
    :rtype: None

    renewNzIf 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   sG   t  j j t j  } |  j d k s* t  | j d | j d d d S)zReports on successful dry run

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

    :returns: `None`
    :rtype: None

    r   zThe dry run was successful.Zon_crashFN)	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  j |  z | d k	 rB t j d  t j | | |  |  nw | d k	 sT t  t j d  |  j | |  } | d k r t j	 d   n( | d k	 r t  j
 | | j   | j  Wd t  j |  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_hookloggerinfor   
renew_certr   Zobtain_and_enroll_certificater   ErrorZdeploy_hooknameslive_dirZ	post_hook)	le_clientr#   domainscertnamelineager&   r&   r'   _get_and_save_certU   s     r5   c             C   s   d j  | j    } d j | j j | d j  |  d t j } |  j s |  j s t	 j
 j t j  j | d d d d d d	 r d
 | f St	 j
 j t j  } | j d j | t j d d j  t j d d   d t j | j  t j 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Cancelcli_flagz--expandforce_interactiveTr   zTo 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)joinr/   format
configfilefilenameoslinesepexpandZrenew_by_defaultr   r   r   r   IDisplayyesnor    r!   sysargvr)   r   r.   USER_CANCELLED)r#   r2   certZexistingquestionr$   r&   r&   r'   _handle_subset_cert_request   s&    		
&	
rJ   c             C   s>  | j    s d | f St j |  |  r2 d | f S|  j rE d | f Sd j | j j d t j } |  j	 d k r{ d } n |  j	 d k r d } | d	 g } t
 j j t j  } | j | | d
 d d d } | d t j k r t j d   nF | d d k rd | f S| d d k r(d | f Sd 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   zYou 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?r6   runz.Attempt to reinstall this existing certificatecertonlyz%Keep the existing certificate for nowz.Renew & replace the cert (limit ~5 per 7 days)defaultr   r9   Tz.Operation canceled. You may re-run the client.r;   FzThis is impossibleN)Zensure_deployedr   Zshould_renewrK   r=   r>   r?   r@   rA   r   r   r   r   r   rC   Zmenudisplay_utilZCANCELr   r.   r   )r#   r4   rI   Zkeep_optchoicesdisplayZresponser&   r&   r'   _handle_identical_cert_request   s2    

	
		

rR   c             C   sz   |  j  r d St j |  |  \ } } | d k rA | d k rA d S| d k	 rZ t |  |  S| d k	 rv t |  | |  Sd 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.

    newcertN)rS   N)rS   N)Z	duplicater   Zfind_duplicative_certsrR   rJ   )r#   r2   Zident_names_certZsubset_names_certr&   r&   r'   _find_lineage_for_domains   s    	rT   c             C   sA   t  |  | |  \ } } | d k r1 t j 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`

    rK   z Keeping the existing certificate)&_find_lineage_for_domains_and_certnamer+   r,   )r#   r2   r3   actionr4   r&   r&   r'   
_find_cert  s    rW   c             C   s   | s t  |  |  St j |  |  } | r | rx t t j |  |   t |  k rx t |  | | | j    d | f St |  |  S| r d St j	 d j
 |    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   rS   Nz}No certificate with name {0} found. Use -d to specify domains, or run certbot certificates to see possible certificate names.)znewcertN)rT   r   lineage_for_certnamesetdomains_for_certname_ask_user_to_confirm_new_namesr/   rR   r   ConfigurationErrorr=   )r#   r2   r3   r4   r&   r&   r'   rU   #  s    $
rU   c             C   sV   t  t |   t |   } t  t |  t |    } | j   | j   | | f S)zWGet lists of items removed from `before`
    and a lists of items added to `after`
    )listrY   sort)afterZbeforeaddedremovedr&   r&   r'   _get_added_removedL  s
    

rb   c             C   sG   t  |  d k r d } n d d j |  } | j d |  d t j  S)z%Format list with given character
    r   z
{br}(None)z	{br}{ch} Zchr6   )lenr<   r=   r@   rA   )	characterZstringsZ	formattedr&   r&   r'   _format_listV  s    		re   c             C   s   |  j  r d St | |  \ } } d j | t d |  t d |  d t j } t j j t	 j
  } | j | d d d d	 s t j 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

    NzYou 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?+-r6   zUpdate certr7   rN   Tz+Specified mismatched cert name and domains.)Zrenew_with_new_domainsrb   r=   re   r@   rA   r   r   r   r   rC   rD   r   r\   )r#   Znew_domainsr3   Zold_domainsr`   ra   r%   objr&   r&   r'   r[   b  s    	r[   c             C   s{   d } |  j  } |  j r$ |  j } n | r< t j |  |  } | sT t j | |  } | rq | rq t j 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

    NzPlease specify --domains, or --installer that will help in domain names autodiscovery, or --cert-name for an existing certificate name.)r3   r2   r   rZ   display_opsZchoose_namesr   r.   )r#   	installerrI   r2   r3   r&   r&   r'   _find_domains_or_certname  s    		rk   c       	      C   s   |  j  r t |   d S| r# | s/ t d   t j |  j   } t j j t	 j
  } |  j d k rn d n d } | r d j | d t j n d } d j | | | t j | d t j } | j | | 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.rL   z with the "certonly" option z+Your key file has been saved at:{br}{0}{br}r6   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notAfterZdater   r   r   r   r    r   r=   r@   rA   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    	
$rr   c                sK    f d d   } t  j    } d }   j  d k	 rK | j   j   } n | j   } t |  d k r{ t j |  } n t |  d k r | d } n   j d k r   j r t j	     _ y" t
 j   | d | \ } } WnN t j k
 r   Yn7 t j k
 r4t j d d d	 t j d
   Yn 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                sq     j  r d Sd j |    j  } t j j t j  } | j | d d d d d d } | sm t	 j
 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Agreer7   r8   z--agree-tosr9   z?Registration cannot proceed without accepting Terms of Service.)Ztosr=   Zserverr   r   r   r   rC   rD   r   r.   )Zterms_of_servicer%   rh   result)r#   r&   r'   _tos_cb  s    	z#_determine_account.<locals>._tos_cbNr;   r   Ztos_cbrl   exc_infoTz.Unable to register an account with ACME server)r   AccountFileStorageloadfind_allrc   ri   Zchoose_accountemailregister_unsafely_without_email	get_emailr   registerr   ZMissingCommandlineFlagr.   r+   debugid)r#   rt   account_storageacmeaccaccountsr&   )r#   r'   _determine_account  s.    r   c                s  t  j j t j  } t  j j t j  } |  j } | d k rl d } | j | d d d d d d d	 d } | s | j d
 | j	  d S|  j
 s t  |  j s t j |   |  _ t j t j t j |  |  j   |  |  j    y5 t j |    f d d   g d d   d d    Wn t j k
 r^d j    } | j d j |  | j  d SYnX t k
 r} z8 d } | j |  j |  j   |  } t j |   WYd d } ~ Xn Xt j |   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?	yes_labelzYes (recommended)no_labelZNor9   TrN   zNot deleting revoked certs.c                s     S)Nr&   )x)archive_dirr&   r'   <lambda><  s    z(_delete_if_appropriate.<locals>.<lambda>c             S   s   |  j  S)N)r   )r   r&   r&   r'   r   =  s    c             S   s   |  S)Nr&   )r   r&   r&   r'   r   =  s    z^Not deleting revoked certs due to overlapping archive dirs. More than one lineage is using {0}rl   z_config.default_archive_dir: {0}, config.live_dir: {1}, archive_dir: {2},original exception: {3})r   r   r   r   rC   r    Zdelete_after_revokerD   r!   r"   ro   r   r3   r   Zcert_path_to_lineager   Zfull_archive_path	configobjZ	ConfigObjZrenewal_file_for_certnameZmatch_and_check_overlapsr   ZOverlappingMatchFoundr=   r<   rn   	ExceptionZdefault_archive_dirr0   r.   delete)r#   rQ   r$   Zattempt_deletionr%   er&   )r   r'   _delete_if_appropriate  s:    			"r   c             C   sY   | d k	 r1 t  |   \ } } t j 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: %rr   )NN)r   r+   r}   r   Client)r#   authenticatorrj   r   r   r&   r&   r'   _init_le_clientL  s
    r   c             C   s   t  j |   } | j   } t j j t j  } | s: d St j j t j  j	 } d } | | d d d d d d } | s d	 St
 |   \ } }	 t j |  | d
 d
 d |	 }
 |
 j j | j  t  j |   } | j |  j   | j d | j  d
 S)zDeactivate 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?r   Z
Deactivater   ZAbortrN   TzDeactivation aborted.Nr   zAccount deactivated.)r   rv   rx   r   r   r   r   r    rC   rD   r   r   r   r   Zdeactivate_registrationregrr   r!   rn   )r#   unused_pluginsr   r   r$   rD   promptZwants_deactivater   r   	cb_clientZaccount_filesr&   r&   r'   
unregisterg  s"    	r   c             C   sh   |  j  r) d } t j |  t |  |  St j |   } | j   } t |  d k rZ d St |   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

    ztUsage 'certbot register --update-registration' is deprecated.
Please use 'cerbot update_account [options]' instead.
r   zmThere is an existing account; registration of a duplicate account with this command is currently unsupported.N)	update_registrationr+   warningupdate_accountr   rv   rx   rc   r   )r#   r   r%   r   r   r&   r&   r'   r|     s    	
r|   c       
         sl  t  j |   } | j   } t j j t j      f d d   } t |  d k rX d S|  j	 d k r |  j
 rt d St j d d  |  _	 t |   \ } } t j |  | d d d	 | } d
 d   |  j	 j d  D } | j j }	 | j j | j j d | j j j d |    | _ | j j d |	  | _ | j | | j  t j |   | d j |  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  |    j  S)N)r!   rn   )m)r$   r&   r'   r     s    z update_account.<locals>.<lambda>r   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.ZoptionalFr   c             S   s   g  |  ] } d  |  q S)zmailto:r&   ).0ry   r&   r&   r'   
<listcomp>  s   	 z"update_account.<locals>.<listcomp>,bodyZcontacturiz'Your e-mail address was updated to {0}.)r   rv   rx   r   r   r   r   r    rc   ry   rz   ri   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   sc   | r | n |  } | j  d k	 s' t  | j | | j | 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)ro   r   Zdeploy_certificaterq   
chain_pathrp   enhance_config)r#   r1   r2   r4   Zpath_providerr&   r&   r'   _install_cert  s
    r   c       
      C   s  y t  j |  | d  \ } } Wn2 t j k
 rS } z t |  SWYd d } ~ Xn X|  j oc |  j } |  j r | r d } t j	 |  d d d d | d |  _ t
 j |  |  s t j d   |  j r t |   }  n t
 j |   r t j d	   |  j rU|  j rUt |   t |  |  \ } } t |  d
 d d | } t |  | |  n t j d   t
 j |   rt j |  |  j  }	 t
 j |	 | | |   d S)zInstall 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?allow_multipleFcustom_promptr   zVOne ore more of the requested enhancements are not supported by the selected installerzLOne or more of the requested enhancements require --cert-name to be providedr   rj   zPath 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strrq   ro   r3   r   get_certnamesr   are_supportedNotSupportedError_populate_from_certnameare_requestedr\   _check_certificate_and_keyrk   r   r   rX   enable)
r#   pluginsrj   _r   Zcustom_certcertname_questionr2   r1   r4   r&   r&   r'   r     s2    	
r   c             C   s   t  j |  |  j  } | s |  S|  j s7 | j |  j _ |  j sO | j |  j _ |  j sg | j |  j _ |  j s | j |  j _ |  S)zfHelper function for install to populate missing config values from lineage
    defined by --cert-name.)r   rX   r3   rq   	namespacero   r   rp   )r#   r4   r&   r&   r'   r   4  s    				r   c             C   s|   t  j j t  j j |  j   s< t j d j |  j    t  j j t  j j |  j   sx t j d j |  j    d  S)Nz-Error while reading certificate from path {0}z-Error while reading private key from path {0})	r@   pathisfilerealpathro   r   r\   r=   rq   )r#   r&   r&   r'   r   E  s    !!r   c             C   s(  t  j d |  j  |  j d k r( g  n |  j } | j   j |  } t  j d |  t j t j j t	 j
  j d d } |  j r |  j r | t |   d S| j |   | j |  } t  j d |  |  j s | t |   d S| j   | j   } t  j d |  | t |   d S)zList 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: %rpauseFzVerified plugins: %rzPrepared plugins: %s)r+   r}   ifacesZvisible	functoolspartialr   r   r   r   rC   notificationZinitZpreparer   Zverify	available)r#   r   r   ZfilterednotifyZverifiedr   r&   r&   r'   plugins_cmdL  s&    	
r   c                s  d d d d g } t    f d d   | D  } t j    rt | rt d } t j | t j d  t j d	   y t	 j
   | d
  \ } } Wn2 t j k
 r } z t |  SWYd d } ~ Xn Xt j   |  s t j d   d } t j   d
 d d d | d   _ t j     j  }	   j r;|	 }
 n- d } t j |	 |  }
 |
 sht j d   t j     j  }   j s| j   _ | rt   d d d | } | j |
   j d d t j    rt j | |
 |    d S)zAdd 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 Sr&   )getattr)r   Zenh)r#   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.enhanceNzVOne ore more of the requested enhancements are not supported by the selected installerzFWhich certificate would you like to use to enhance your configuration?r   Fr   zJWhich domain names would you like to enable the selected enhancements for?zAUser cancelled the domain selection. No domains defined, exiting.r   rj   Zask_redirect)anyr   r   r+   r   rE   rF   r   ZMisconfigurationErrorr   r   r   r   r   r   r   r   r3   rZ   noninteractive_moderi   Zchoose_valuesr.   rX   r   r   r   r   )r#   r   Zsupported_enhancementsZoldstyle_enhr%   rj   r   r   r   Zcert_domainsr2   Zdomain_questionr4   r1   r&   )r#   r'   r   r  s>    			r   c             C   s    t  j |  j |  j |  |  d S)zRollback 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   rollbackrj   Zcheckpoints)r#   r   r&   r&   r'   r     s    r   c             C   s   t  j |  d |  j 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

    numN)r   Zview_config_changesr   )r#   r   r&   r&   r'   config_changes  s    r   c             C   s   t  j |   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_symlinks)r#   r   r&   r&   r'   update_symlinks  s    r   c             C   s   t  j |   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_lineage)r#   r   r&   r&   r'   rename  s    r   c             C   s   t  j |   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'   r     s    r   c             C   s   t  j |   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   certificates)r#   r   r&   r&   r'   r     s    r   c             C   s  d |  _  |  _ |  j d k rC |  j rC t j |  |  j  |  _ n+ |  j s_ |  j rn |  j rn t j d   |  j d k	 r t	 j
 d |  j d |  j d  t j |  j d |  j d  t j j |  j d  } t j |  |  } nD t	 j
 d |  j d  t |   \ } } t j |  | j | j  } t j |  j d  d } t	 j
 d |  j  y* | j t j |  |  j  t |   Wn2 t j k
 r} z t |  SWYd d } ~ Xn Xt j |  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)rj   r   ro   r3   r   Zcert_path_for_cert_namer   r.   rq   r+   r}   r   Zverify_cert_matches_priv_keyjoseZJWKrw   r   Zacme_from_config_keyr   keyr   Zpyopenssl_load_certificatereasonrevokeZComparableX509r   acme_errorsZClientErrorr   ri   Zsuccess_revocation)r#   r   r   r   r   r   rH   r   r&   r&   r'   r     s,    	r   c             C   s  y t  j |  | d  \ } } Wn2 t j k
 rS } z t |  SWYd d } ~ Xn Xt j |  |  su t j d   t |  | |  } t	 |  |  \ } } t
 |  | |  \ } }	 |	 }
 | r t | |  | | |	  }
 |
 r |
 j n d } |
 r |
 j n d } |
 r|
 j n d } t |  | | |  t |  | | |
  t j |   rh|
 rht j |
 | | |   |	 d k s{| rt j |  n t j |  t |   d S)zObtain 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

    rL   NzVOne ore more of the requested enhancements are not supported by the selected installer)r   r   r   r   r   r   r   r   r   rk   rW   r5   ro   rp   rq   rr   r   r   r   ri   Zsuccess_installationZsuccess_renewalr(   )r#   r   rj   r   r   r1   r2   r3   should_get_certr4   Znew_lineagero   rp   rq   r&   r&   r'   rL   =  s0    rL   c             C   s   |  j  \ } } | j |  \ } } |  j rD t j d |  j  d S| j | | t j j	 |  j  t j j	 |  j
  t j 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 %sN)NN)Z
actual_csrZobtain_certificate_from_csrrm   r+   r}   ro   Zsave_certificater@   r   normpathr   rp   )r#   r1   csrr   rH   chainro   rp   r&   r&   r'   _csr_get_and_save_certs  s    	3r   c       	      C   s
  y t  j |  | d  \ } } Wn; t j k
 r\ } z t j d |    WYd d } ~ Xn Xt |  | |  } t | |  d | } t j	 j
 t j  j } | d k r | d j | j  d d n? t j |  | |  | j   | d j |  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

    rM   z'Could not choose appropriate plugin: %sNr4   z9new certificate deployed without reload, fullchain is {0}r   FzDnew certificate deployed with reload of {0} server; fullchain is {1})r   r   r   r   r+   r,   r   r5   r   r   r   r   rC   r   r=   Z	fullchainr   Zrun_renewal_deployerZrestartrj   )	r#   r   r4   rj   authr   r1   Zrenewed_lineager   r&   r&   r'   r-     s    	
	r-   c             C   s  y t  j |  | d  \ } } Wn; t j k
 r\ } z t j d |    WYd d } ~ Xn Xt |  | |  } |  j r t |  |  \ } } t	 |  | |  t
 |   d St |  |  \ } }	 t |  | |	  \ }
 } |
 s
t j j t j  j } | d d d d St | |  | |	 |  } | r1| j n d } | rF| 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

    rM   z'Could not choose appropriate plugin: %sNz5Certificate not yet due for renewal; no action taken.r   F)r   r   r   r   r+   r,   r   r   r   rr   r(   rk   rW   r   r   r   r   rC   r   r5   ro   rp   rq   )r#   r   rj   r   r   r1   ro   rp   r2   r3   r   r4   r   rq   r&   r&   r'   rM     s.    	
rM   c          
   C   s#   z t  j |   Wd t j   Xd 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handle_renewal_requestr   Zrun_saved_post_hooks)r#   r   r&   r&   r'   r     s    r   c             C   s   t  j |  j t j t j   |  j  t  j |  j t j t j   |  j  |  j	 |  j
 |  j f } x0 | D]( } t  j | d t j   d |  j qi Wd S)zCreate or verify existence of config, work, and hook directories.

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

    :returns: `None`
    :rtype: None

    ZuidstrictN)r   Zset_up_core_dirZ
config_dirr   ZCONFIG_DIRS_MODEr	   Z
os_geteuidZ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   sz   |  j  r0 d |  _ t j t t j d   } n6 |  j rN t j t j  } n t j	 t j |  j
  } t j j |  d S)zSet the displayer

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

    :returns: `None`
    :rtype: None

    TwN)quietr   rO   ZNoninteractiveDisplayopenr@   devnullrE   stdoutZFileDisplayr9   r   r   provideUtility)r#   Z	displayerr&   r&   r'   set_displayer  s    
			r   r;   c             C   s  t  j   t j j   } t j d t j  t j d |   t j d |  t	 j
 | |   } t j |  } t j j |  t j | j  y t  j |  t |  Wn' t j k
 r | j t k r   Yn Xt |  t j |  } t j j |  t j | j  | j | |  S)zCommand 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

    zcertbot version: %szArguments: %rzDiscovered plugins: %r)r   Zpre_arg_parse_setupplugins_discoZPluginsRegistryrx   r+   r}   certbot__version__r   Zprepare_and_parse_argsr
   ZNamespaceConfigr   r   r   r	   Z+raise_for_non_administrative_windows_rightsr   Zpost_arg_parse_setupr   r   r.   funcr   r   r   ZReporterr   Zatexit_registerZprint_messages)Zcli_argsr   argsr#   reportr&   r&   r'   main+  s(    


r   __main__zExiting with message %s)^__doc__Z
__future__r   r   Zlogging.handlersZloggingr@   rE   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   r   Zcertbot.displayrO   r   ri   Zcertbot.pluginsr   r   r   r   r   rG   Z	getLogger__name__r+   r(   r*   r5   rJ   rR   rT   rW   rU   rb   re   r[   rk   rr   r   r   r   r   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rL   r   r-   rM   r   r   r   rF   r   Z
err_stringr   exitr&   r&   r&   r'   <module>   s   /02&)
%)*;7(&,9&9,6*1-	