
_c           @  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 Z d d l m Z d d l m Z m Z m Z m Z 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/ d d l. m0 Z0 d d l. m1 Z1 d d l2 m3 Z4 d d l2 m Z5 d d l6 m7 Z7 d Z8 e j9 e:  Z; d   Z< d    Z= d d d d!  Z? d"   Z@ d#   ZA d$   ZB d%   ZC d&   ZD d'   ZE d(   ZF d)   ZG d*   ZH d d+  ZI d d,  ZJ d-   ZK d.   ZL d/   ZM d0   ZN d1   ZO d2   ZP d d3  ZQ d4   ZR d5   ZS d6   ZT d7   ZU d8   ZV d9   ZW d:   ZX d;   ZY d<   ZZ d=   Z[ d>   Z\ d?   Z] d@   Z^ dA   Z_ dB   Z` dC   Za dD   Zb dE   Zc d dF  Zd d S(G   s   Certbot main entry point.i(   t   print_functionN(   t   errors(   t   Uniont   Iterablet   Optionalt   Listt   Tuple(   t   crypto_util(   t
   interfaces(   t   util(   t   account(   t   cert_manager(   t   cli(   t   client(   t   configuration(   t	   constants(   t   eff(   t   hooks(   t   log(   t   renewal(   t   reporter(   t   snap_config(   t   storage(   t   updater(   t   disco(   t	   selection(   t
   filesystem(   t   misc(   t   os(   t   ops(   t   enhancementss?   User chose to cancel the operation and may reinvoke the client.c         C  s?   |  j  r d St j j t j  } d } | j | | j  d S(   s   Potentially suggest a donation to support Certbot.

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

    :returns: `None`
    :rtype: None

    Ns   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

(   t   stagingt   zopet	   componentt
   getUtilityR   t	   IReportert   add_messaget   LOW_PRIORITY(   t   configt   reporter_utilt   msg(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt    _suggest_donation_if_appropriate3   s
    	c         C  s2   t  j j t j  } | j d | j d t d S(   s   Reports on successful dry run

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

    :returns: `None`
    :rtype: None

    s   The dry run was successful.t   on_crashN(   R    R!   R"   R   R#   R$   t   HIGH_PRIORITYt   False(   R&   R'   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _report_successful_dry_runG   s    
	c         C  s   t  j |  z | d	 k	 rx t j d j d | j r: d n d d t j | pU | j      t	 j
 | | |  |  n t j d j d | j r d n d d t j |    |  j | |  } | t k r t j d   n  | d	 k	 r
t  j | | j   | j  n  Wd	 t  j |  X| S(
   s!  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

    s   {action} for {domains}t   actions-   Simulating renewal of an existing certificates    Renewing an existing certificatet   domainss    Simulating a certificate requests   Requesting a certificates!   Certificate could not be obtainedN(   R   t   pre_hookt   Nonet   display_utilt   notifyt   formatt   dry_runt   summarize_domain_listt   namesR   t
   renew_certt   obtain_and_enroll_certificateR,   R   t   Errort   deploy_hookt   live_dirt	   post_hook(   t	   le_clientR&   R/   t   certnamet   lineage(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _get_and_save_certW   s(    		#c         C  s   t  j d  s  t  j d  r} |  j j   } | j j   } | | k r} d } | j | j | |  } t j |   q} n  d S(   s|  
    This function ensures that the user will not implicitly migrate an existing key
    from one type to another in the situation where a certificate for that lineage
    already exist and they have not provided explicitly --key-type and --cert-name.
    :param config: Current configuration provided by the client
    :param cert: Matching certificate that could be renewed
    t   key_typeR?   s   Are you trying to change the key type of the certificate named {0} from {1} to {2}? Please provide both --cert-name and --key-type on the command line confirm the change you are trying to make.N(	   R   t
   set_by_cliRB   t   uppert   private_key_typeR4   t   lineagenameR   R:   (   R&   t   certt   new_key_typet   cur_key_typeR(   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt%   _handle_unexpected_key_type_migration   s    	 c      
   C  s   t  |  |  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 t r d	 | f St j d
 j | t j d d j t j d  d t j  t j t   d S(   s  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`

    s   , s   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?t   brt   Expandt   Cancelt   cli_flags   --expandt   force_interactivet   renews   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}i    t    i   N(   RJ   t   joinR7   R4   t
   configfilet   filenameR   t   linesept   expandt   renew_by_defaultR    R!   R"   R   t   IDisplayt   yesnot   TrueR2   R3   t   syst   argvR   R:   t   USER_CANCELLED(   R&   R/   RG   t   existingt   question(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _handle_subset_cert_request   s$    		
 c         C  sH  t  |  |  | j   s# d | f St j |  |  r? d | f S|  j rR d | f Sd j | j j d t j	 } |  j
 d k r d } n |  j
 d k r d } n  | d	 g } t j j t j  } | j | | d
 d d t } | d t j k rt j d   n  | d d k rd | f S| d d k r8d | f St d   d S(   s  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`

    t	   reinstallRP   s   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?RK   t   runs.   Attempt to reinstall this existing certificatet   certonlys%   Keep the existing certificate for nowsB   Renew & replace the certificate (may be subject to CA rate limits)t   defaulti    RO   s.   Operation canceled. You may re-run the client.i   s   This is impossibleN(   RJ   t   ensure_deployedR   t   should_renewRa   R4   RS   RT   R   RU   t   verbR    R!   R"   R   RX   t   menuRZ   R2   t   CANCELR   R:   t   AssertionError(   R&   R@   R_   t   keep_optt   choicest   displayt   response(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _handle_identical_cert_request   s4    

	
			

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(   s  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.

    t   newcertN(   Rp   N(   Rp   N(   NN(   t	   duplicateR1   R   t   find_duplicative_certsRo   R`   (   R&   R/   t   ident_names_certt   subset_names_cert(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _find_lineage_for_domains  s    	c         C  sD   t  |  | |  \ } } | d k r4 t j d  n  | d k | f S(   sW  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`

    Ra   s    Keeping the existing certificate(   t&   _find_lineage_for_domains_and_certnamet   loggert   info(   R&   R/   R?   R.   R@   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt
   _find_cert:  s    c         C  s   | s t  |  |  St j |  |  } | r | r t t j |  |   t |  k r t |  |  t |  | | | j    d | f Sn  t |  |  S| r d St
 j d j |    d S(   s  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.

    RP   Rp   s}   No certificate with name {0} found. Use -d to specify domains, or run certbot certificates to see possible certificate names.N(   s   newcertN(   Ru   R   t   lineage_for_certnamet   sett   domains_for_certnameRJ   t   _ask_user_to_confirm_new_namesR7   Ro   R1   R   t   ConfigurationErrorR4   (   R&   R/   R?   R@   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyRv   R  s    $c         C  sV   t  t |   t |   } t  t |  t |    } | j   | j   | | f S(   sW   Get lists of items removed from `before`
    and a lists of items added to `after`
    (   t   listR{   t   sort(   t   aftert   beforet   addedt   removed(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _get_added_removed}  s
    

c         C  s;   | s d } n d d j  |  } | j d |  d t j  S(   s%   Format list with given character
    s
   {br}(None)s	   {br}{ch} t   chRK   (   RR   R4   R   RU   (   t	   charactert   stringst	   formatted(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _format_list  s    		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 t s t j d	   n  d S(
   s  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

    Ns   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?t   +t   -RK   s   Update certificateRM   Rd   s2   Specified mismatched certificate name and domains.(   t   renew_with_new_domainsR   R4   R   R   RU   R    R!   R"   R   RX   RY   RZ   R   R~   (   R&   t   new_domainsR?   t   old_domainsR   R   R(   t   obj(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyR}     s    	c         C  s   d } |  j } |  j r$ |  j } n | r? t j |  |  } n  | sZ t j | |  } n  | rz | rz t j d   n  | | f S(   s	  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 default 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

    s   Please specify --domains, or --installer that will help in domain names autodiscovery, or --cert-name for an existing certificate name.N(	   R1   R?   R/   R   R|   t   display_opst   choose_namesR   R:   (   R&   t	   installerR_   R/   R?   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _find_domains_or_certname  s    		c   	      C  s   |  j  r t |   d St j |  j   } t j j t j	  } |  j
 d k rV d n d } | rz d j | d t j n d } d j | | | t j | d t j } | j | | j  d S(   sD  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

    NRb   s    with the "certonly" optiont    s+   Your key file has been saved at:{br}{0}{br}RK   s  Congratulations! Your certificate and chain have been saved at:{br}{0}{br}{1}Your certificate 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"(   R5   R-   R   t   notAftert   dateR    R!   R"   R   R#   Rg   R4   R   RU   R   t   cli_commandR$   t   MEDIUM_PRIORITY(	   R&   t	   cert_patht   fullchain_patht   key_patht   expiryR'   t
   verbswitcht   privkey_statementR(   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _report_new_cert  s    	
$c           sV    f d   } t  j    } d	 }   j  d	 k	 rH | j   j   } n | j   } t |  d k rx t j |  } n t |  d k r | d } n   j d	 k r   j	 r t j
     _ n  y/ t j   | d | \ } } t j d  WnL t j k
 r
  n6 t j k
 r?t j d d t t j d   n X| j   _  | | f S(
   s  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._internal.account.Account` and :class:`acme.client.Client`

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

    c           sh     j  r t Sd j |   } t j j t j  } | j | d d d t } | sd t	 j
 d   n  d  S(   Nsp   Please read the Terms of Service at {0}. You must agree in order to register with the ACME server. Do you agree?RN   s   --agree-tosRO   s?   Registration cannot proceed without accepting Terms of Service.(   t   tosRZ   R4   R    R!   R"   R   RX   RY   R   R:   R1   (   t   terms_of_serviceR(   R   t   result(   R&   (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _tos_cb  s    		i   i    t   tos_cbs   Account registered.R   t   exc_infos.   Unable to register an account with ACME serverN(   R
   t   AccountFileStorageR1   t   loadt   find_allt   lenR   t   choose_accountt   emailt   register_unsafely_without_emailt	   get_emailR   t   registerR2   R3   R   t   MissingCommandlineFlagR:   Rw   t   debugRZ   t   id(   R&   R   t   account_storaget   acmet   acct   accounts(    (   R&   s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _determine_account  s0    c      
     s\  t  j j t j  } |  j } | d k rZ d } | j | d d d d d t d t } n  | sd d S|  j	 s t
 j |   |  _	 n  t j t j t j |  |  j	   |  |  j	    y, t
 j |    f d	   g d
   d    Wnj t j k
 rt j d    d St k
 rJ} d } | j |  j |  j   |  } t j |   n Xt
 j |   d S(   s  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 ...
    s{   Would you like to delete the certificate(s) you just revoked, along with all earlier and later versions of the certificate?t	   yes_labels   Yes (recommended)t   no_labelt   NoRO   Rd   Nc           s     S(   N(    (   t   x(   t   archive_dir(    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   <lambda>l  s    c         S  s   |  j  S(   N(   R   (   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyR   m  s    c         S  s   |  S(   N(    (   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyR   m  s    sh   Not deleting revoked certificates due to overlapping archive dirs. More than one certificate is using %ss_   config.default_archive_dir: {0}, config.live_dir: {1}, archive_dir: {2},original exception: {3}(   R    R!   R"   R   RX   t   delete_after_revokeR1   RY   RZ   R?   R   t   cert_path_to_lineageR   t   full_archive_patht	   configobjt	   ConfigObjt   renewal_file_for_certnamet   match_and_check_overlapsR   t   OverlappingMatchFoundRw   t   warningt	   ExceptionR4   t   default_archive_dirR<   R:   t   delete(   R&   Rm   t   attempt_deletionR(   t   e(    (   R   s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _delete_if_appropriateG  s2    			c         C  sY   | d k	 r1 t |   \ } } t j d |  n d \ } } t j |  | | | d | S(   sx  Initialize Let's Encrypt Client

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

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

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

    s   Picked account: %rR   N(   NN(   R1   R   Rw   R   R   t   Client(   R&   t   authenticatorR   R   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _init_le_client{  s
    c         C  s   t  j |   } | j   } | s% d St j j t j  j } d } | | d d d d d t	 } | sk d St
 |   \ } } t j |  | d d d	 | }	 |	 j j | j  t  j |   }
 |
 j |  j   t j d
  d S(   s   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

    s.   Could not find existing account to deactivate.sC   Are you sure you would like to irrevocably deactivate your account?R   t
   DeactivateR   t   AbortRd   s   Deactivation aborted.R   s   Account deactivated.N(   R
   R   R   R    R!   R"   R   RX   RY   RZ   R   R   R   R1   R   t   deactivate_registrationt   regrR   R2   R3   (   R&   t   unused_pluginsR   R   RY   t   promptt   wants_deactivateR   R   t	   cb_clientt   account_files(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt
   unregister  s"    	c         C  s3   t  j |   } | j   } | r% d St |   d S(   s$  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

    sm   There is an existing account; registration of a duplicate account with this command is currently unsupported.N(   R
   R   R   R   R1   (   R&   R   R   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyR     s    
c   
      C  st  t  j |   } | j   } | s% d S|  j d k rV |  j rV t j d t  |  _ n  t	 |   \ } } t
 j |  | d d d | } d } |  j r g  |  j j d  D] } d | ^ q } n  | j j }	 | j j | j j d | j j j d |    | _ | j j d |	  | _ | j | | j  |  j sGt j d	  n) t j |  |  t j d
 j |  j   d S(   s$  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

    s-   Could not find an existing account to update.t   optionalR   t   ,s   mailto:t   bodyt   contactt   urisF   Any contact information associated with this account has been removed.s'   Your e-mail address was updated to {0}.N(    (   R
   R   R   R   R1   R   R   R   R,   R   R   R   t   splitR   R   R   t   update_registrationt   updateR   t   update_regrR2   R3   R   t   prepare_subscriptionR4   (
   R&   R   R   R   R   R   R   t   acc_contactsR   t   prev_regr_uri(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   update_account  s*    	,!	c         C  sN   | r | n |  } | j  | | j | j | j | j  | j | | j  d S(   s  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(   t   deploy_certificateR   R   t
   chain_pathR   t   enhance_config(   R&   R>   R/   R@   t   path_provider(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _install_cert  s    c   
   	   C  s  y t  j |  | d  \ } } Wn t j k
 r> } t |  SX|  j oN |  j } |  j r | r d } t j	 |  d d t
 d | d |  _ n  t j |  |  s t j d   n  |  j r t |   }  n! t j |   r t j d   n  |  j rI|  j rIt |   t |  |  \ } } t |  d d d	 | } t |  | |  n t j d
   t j |   rt j |  |  j  }	 t j |	 | | |   n  d S(   s   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

    t   installs,   Which certificate would you like to install?t   allow_multiplet   custom_prompti    sV   One ore more of the requested enhancements are not supported by the selected installersL   One or more of the requested enhancements require --cert-name to be providedR   R   s   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.N(   t   plug_selt   choose_configurator_pluginsR   t   PluginSelectionErrort   strR   R   R?   R   t   get_certnamesR,   R   t   are_supportedt   NotSupportedErrort   _populate_from_certnamet   are_requestedR~   t   _check_certificate_and_keyR   R   R1   R   Rz   t   enable(
   R&   t   pluginsR   t   _R   t   custom_certt   certname_questionR/   R>   R@   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyR   )  s4    	
c         C  s   t  j |  |  j  } | s |  S|  j s: | j |  j _ n  |  j sU | j |  j _ n  |  j sp | j |  j _ n  |  j s | j |  j _ n  |  S(   sf   Helper function for install to populate missing config values from lineage
    defined by --cert-name.(   R   Rz   R?   R   t	   namespaceR   R   R   (   R&   R@   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyR   d  s    				c         C  s|   t  j j t j |  j   s< t j d j |  j    n  t  j j t j |  j	   sx t j d j |  j	    n  d  S(   Ns-   Error while reading certificate from path {0}s-   Error while reading private key from path {0}(
   R   t   patht   isfileR   t   realpathR   R   R~   R4   R   (   R&   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyR   u  s    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 t } |  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(   s   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

    s   Expected interfaces: %ss   Filtered plugins: %rt   pauseNs   Verified plugins: %rs   Prepared plugins: %s(   Rw   R   t   ifacesR1   t   visiblet	   functoolst   partialR    R!   R"   R   RX   t   notificationR,   t   initt   prepareR   t   verifyt	   available(   R&   R   R  t   filteredR3   t   verifiedR  (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   plugins_cmd|  s&    	
c           s  d d d d g } t    f d   | D  } t j    rt | rt d } t j | t j d  t j d   n  y t	 j
   | d	  \ } } Wn t j k
 r } t |  SXt j   |  s t j d
   n  d } t j   d	 d t d | d   _ t j     j  }	   j r)|	 }
 n0 d } t j |	 |  }
 |
 sYt j d   n  t j     j  }   j s| j   _ n  | rt   d d d | } | j |
   j d t n  t j    rt j | |
 |    n  d S(   s   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

    t   hstst   redirectt   uirt   staplec         3  s   |  ] } t    |  Vq d  S(   N(   t   getattr(   t   .0t   enh(   R&   (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pys	   <genexpr>  s    s|   Please specify one or more enhancement types to configure. To list the available enhancement types, run:

%s --help enhance
i    s#   No enhancements requested, exiting.t   enhancesV   One ore more of the requested enhancements are not supported by the selected installersF   Which certificate would you like to use to enhance your configuration?R   R   sJ   Which domain names would you like to enable the selected enhancements for?sA   User cancelled the domain selection. No domains defined, exiting.R   R   t   redirect_defaultN(   t   anyR   R   Rw   R   R[   R\   R   t   MisconfigurationErrorR   R   R   R   R   R   R   R   R,   R?   R|   t   noninteractive_modeR   t   choose_valuesR:   Rz   R   R   R1   R   R   (   R&   R   t   supported_enhancementst   oldstyle_enhR(   R   R   R   R   t   cert_domainsR/   t   domain_questionR@   R>   (    (   R&   s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyR    s@    			c         C  s    t  j |  j |  j |  |  d S(   s   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   t   rollbackR   t   checkpoints(   R&   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyR     s    c         C  s   t  j |   d S(   sr  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   t   update_live_symlinks(   R&   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   update_symlinks  s    c         C  s   t  j |   d S(   sA  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   t   rename_lineage(   R&   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   rename  s    c         C  s   t  j |   d S(   sA  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   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyR     s    c         C  s   t  j |   d S(   s  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   t   certificates(   R&   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyR&  $  s    c         C  s  d |  _ |  _ |  j d k rC |  j rC t j |  |  j  |  _ n. |  j s_ |  j rq |  j rq t j d   n  |  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 |   Wn t j k
 r} t |  SXt j |  j d  d S(   s:  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

    sC   Error! Exactly one of --cert-path or --cert-name must be specified!s$   Revoking %s using certificate key %si    i   s   Revoking %s using Account Keys   Reason code for revocation: %sN(    R1   R   R   R   R?   R   t   cert_path_for_cert_nameR   R:   R   Rw   R   R   t   verify_cert_matches_priv_keyt   joset   JWKR   R   t   acme_from_config_keyR   t   keyR   t   pyopenssl_load_certificatet   reasont   revoket   ComparableX509R   t   acme_errorst   ClientErrorR   R   t   success_revocation(   R&   R   R,  R   R   R   RG   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyR/  5  s.    	c         C  s  y t  j |  | d  \ } } Wn t j k
 r> } t |  SXt j |  |  sc t j d   n  t |  | |  } t	 |  |  \ } } t
 |  | |  \ } }	 |	 }
 | r t | |  | | |	  }
 n  |
 r |
 j n d } |
 r |
 j n d } |
 r|
 j n d } | r$t |  | | |  n  t |  | | |
  t j |   re|
 ret j |
 | | |   n  |	 d k sx| rt j |  n t j |  t |   t j |  | j  d S(   s   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

    Rb   sV   One ore more of the requested enhancements are not supported by the selected installerN(   R   R   R   R   R   R   R   R   R   R   Ry   RA   R   R1   R   R   R   R   R   R   R   t   success_installationt   success_renewalR)   R   t   handle_subscriptionR
   (   R&   R   R   R   R   R>   R/   R?   t   should_get_certR@   t   new_lineageR   R   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyRb   b  s6    
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(   s  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`

    s*   Dry run: skipping saving certificate to %sN(   NN(   t
   actual_csrt   obtain_certificate_from_csrR5   Rw   R   R   R1   t   save_certificateR   R   t   normpathR   R   (   R&   R>   t   csrR   RG   t   chainR   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   _csr_get_and_save_cert  s    	3c   	      C  s   y t  j |  | d  \ } } Wn) t j k
 rJ } t j d |    n Xt |  | |  } t | |  d | } t j	 j
 t j  j } | d k r | d j | j  d t n? t j |  | |  | j   | d j |  j | j  d t d S(   s  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

    Rc   s'   Could not choose appropriate plugin: %sR@   s9   new certificate deployed without reload, fullchain is {0}R  sD   new certificate deployed with reload of {0} server; fullchain is {1}N(   R   R   R   R   Rw   Rx   R   RA   R    R!   R"   R   RX   R  R1   R4   t	   fullchainR,   R   t   run_renewal_deployert   restartR   (	   R&   R   R@   R   t   authR   R>   t   renewed_lineageR3   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyR8     s    	
	c         C  s  y t  j |  | d  \ } } Wn) t j k
 rJ } t j d |    n Xt |  | |  } |  j r t |  |  \ } } t	 |  | |  t
 |   t j |  | j  d St |  |  \ } }	 t |  | |	  \ }
 } |
 st j j t j  j } | d d t d St | |  | |	 |  } | r2| j n d } | rG| j n d } | r\| j n d } t	 |  | | |  t
 |   t j |  | j  d S(   sf  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

    Rc   s'   Could not choose appropriate plugin: %sNs5   Certificate not yet due for renewal; no action taken.R  (   R   R   R   R   Rw   Rx   R   R=  R?  R   R)   R   R6  R
   R   Ry   R    R!   R"   R   RX   R  R,   RA   R   R1   R   R   (   R&   R   R   RC  R   R>   R   R   R/   R?   R7  R@   R3   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyRc     s2    	

c         C  s#   z t  j |   Wd t j   Xd S(   s  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   t   handle_renewal_requestR   t   run_saved_post_hooks(   R&   R   (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyRP     s    c         C  s{   t  j |  j t j |  j  t  j |  j t j |  j  |  j |  j |  j	 f } x$ | D] } t  j
 | d |  j qW Wd S(   s   Create or verify existence of config, work, and hook directories.

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

    :returns: `None`
    :rtype: None

    t   strictN(   R	   t   set_up_core_dirt
   config_dirR   t   CONFIG_DIRS_MODEt   strict_permissionst   work_dirt   renewal_pre_hooks_dirt   renewal_deploy_hooks_dirt   renewal_post_hooks_dirt   make_or_verify_dir(   R&   t	   hook_dirst   hook_dir(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   make_or_verify_needed_dirs+  s    
c         C  sz   |  j  r0 t |  _ t j t t j d   } n6 |  j rN t j t j	  } n t j
 t j	 |  j  } t j j |  d S(   s   Set the displayer

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

    :returns: `None`
    :rtype: None

    t   wN(   t   quietRZ   R  R2   t   NoninteractiveDisplayt   openR   t   devnullR[   t   stdoutt   FileDisplayRO   R    R!   t   provideUtility(   R&   t	   displayer(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   set_displayer?  s    
			c         C  s  |  s t  j d }  n  t j   t j j d  d k rJ t j |   }  n  t	 j
 j   } t j d t j  t j d t  j d  t j d |   t j d |  t j | |   } t j |  } t j j |  t j   y t j |  t |  Wn) t j k
 r$| j t k r%  q%n Xt  j d d	 k rKt  j! d
 t"  n  t# |  t$ j% |  } t j j |  t& j' | j(  | j | |  S(   s   Run Certbot.

    :param cli_args: command line to Certbot, defaults to ``sys.argv[1:]``
    :type cli_args: `list` of `str`

    :returns: value for `sys.exit` about the exit status of Certbot
    :rtype: `str` or `int` or `None`

    i   t   CERTBOT_SNAPPEDRZ   s   certbot version: %ss#   Location of certbot entry point: %si    s   Arguments: %rs   Discovered plugins: %ri   sd   Python 2 support will be dropped in the next release of Certbot. Please upgrade your Python version.()   R[   R\   R   t   pre_arg_parse_setupR   t   environt   getR   t   prepare_envt   plugins_discot   PluginsRegistryR   Rw   R   t   certbott   __version__R   t   prepare_and_parse_argsR   t   NamespaceConfigR    R!   R[  R   t+   raise_for_non_administrative_windows_rightst   post_arg_parse_setupRS  R   R:   t   funcR  t   version_infot   warningst   warnt   PendingDeprecationWarningR]  R   t   ReporterR	   t   atexit_registert   print_messages(   t   cli_argsR   t   argsR&   t   report(    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   mainU  s:    





(e   t   __doc__t
   __future__R    R  t   logging.handlerst   loggingR[   Rm  R   t   josepyR)  t   zope.componentR    R   R   R1  t   acme.magic_typingR   R   R   R   R   Re  R   R   R	   t   certbot._internalR
   R   R   R   R   R   R   R   R   R   R   R   R   R   t   certbot._internal.pluginsR   Rc  R   R   t   certbot.compatR   R   R   t   certbot.displayR   R   R2   t   certbot.pluginsR   R]   t	   getLoggert   __name__Rw   R)   R-   R1   RA   RJ   R`   Ro   Ru   Ry   Rv   R   R   R}   R   R   R   R   R   R   R   R   R   R   R   R   R  R  R   R#  R%  R   R&  R/  Rb   R?  R8   Rc   RP   RS  R]  Rv  (    (    (    s:   /usr/lib/python2.7/site-packages/certbot/_internal/main.pyt   <module>   s   (		;		3	7	(		+	
		&)*	;	4		(		2	;			'	;						-	:		+	4			