U
    ]16                     @   s   d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ eeZejjZd2d	d
Zd3ddZd4ddZdd Zdd Zdd Zdddddddddd d!d"d#d$d%d&d'gZd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Z dS )5z=Decide which plugins to use for authentication & installation    )print_functionN)errors)
interfaces)os)util<How would you like to authenticate and install certificates?c                 C   s   t | |||tjtjfS )zPick configurator plugin.)pick_pluginr   IAuthenticator
IInstallerconfigdefaultpluginsquestion r   ;/usr/lib/python3/dist-packages/certbot/plugins/selection.pypick_configurator   s       
r   +How would you like to install certificates?c                 C   s   t | |||tjfS )zPick installer plugin.)r   r   r
   r   r   r   r   pick_installer   s        r   4How would you like to authenticate with the ACME CA?c                 C   s   t | |||tjfS )zPick authentication plugin.)r   r   r	   r   r   r   r   pick_authenticator"   s        r   c                    s   t | \}  sdS | fdd}||  |tjf}t|dkrXtd  |rt	|
 d }td| || S td  dS )	a/  
    Get an unprepared interfaces.IInstaller object.

    :param certbot.interfaces.IConfig config: Configuration
    :param certbot.plugins.disco.PluginsRegistry plugins:
        All plugins registered as entry points.

    :returns: Unprepared installer plugin or None
    :rtype: IPlugin or None
    Nc                    s
   | j  kS NnameZp_epreq_instr   r   <lambda>8       z*get_unprepared_installer.<locals>.<lambda>   zfFound multiple installers with the name %s, Certbot is unable to determine which one to use. Skipping.r   zSelecting plugin: %sz:Could not select or initialize the requested installer %s.)cli_plugin_requestsfilterinitverifyr   r
   lenr   PluginSelectionErrorlistvaluesloggerdebug)r   r   _Z
installersinstr   r   r   get_unprepared_installer)   s&    

r,   c           	         s    dk	r|  fdd}n| jr,td| |}||  ||}|  |	 }t
|dkrtd| ttt||}|dkrdS | S t
|dkrt| d }td| |jrdS | S td	 dS dS )
a  Pick plugin.

    :param certbot.interfaces.IConfig: Configuration
    :param str default: Plugin name supplied by user or ``None``.
    :param certbot.plugins.disco.PluginsRegistry plugins:
        All plugins registered as entry points.
    :param str question: Question to be presented to the user in case
        multiple candidates are found.
    :param list ifaces: Interfaces that plugins must provide.

    :returns: Initialized plugin.
    :rtype: IPlugin

    Nc                    s
   | j  kS r   r   r   r   r   r   r   X   r   zpick_plugin.<locals>.<lambda>a  Missing command line flags. For non-interactive execution, you will need to specify a plugin on the command line.  Run with '--help plugins' to see a list of options, and see https://eff.org/letsencrypt-plugins for more detail on what the plugins do and how to use them.r   zMultiple candidate plugins: %sr   zSingle candidate plugin: %szNo candidate plugin)r!   Znoninteractive_moder   MissingCommandlineFlagZvisibleifacesr"   r#   prepareZ	availabler$   r(   r)   choose_pluginr&   sixZ
itervaluesr'   misconfigured)	r   r   r   r   r/   ZfilteredZverifiedprepared	plugin_epr   r-   r   r   G   s2    


r   c           	      C   s   dd | D }t dd | D }ttj}dtjkrl|t dkrldd t| D d }|j|||d	\}}n|j||d
d\}}|tj	kr| | }|j
rttjjd| dd q|S q dS q dS )zAllow the user to choose their plugin.

    :param list prepared: List of `~.PluginEntryPoint`.
    :param str question: Question to be presented to the user.

    :returns: Plugin entry point chosen by the user.
    :rtype: `~.PluginEntryPoint`

    c                 S   s    g | ]}|j |jrd nd qS )z [Misconfigured] )Zdescription_with_namer3   .0r5   r   r   r   
<listcomp>   s   z!choose_plugin.<locals>.<listcomp>c                 s   s   | ]}|j V  qd S r   r   r7   r   r   r   	<genexpr>   s     z choose_plugin.<locals>.<genexpr>ZCERTBOT_AUTO)apachenginxc                 S   s   g | ]\}}|j d kr|qS )r;   r   )r8   npr   r   r   r9      s     
 r   r-   T)Zforce_interactivezxThe selected plugin encountered an error while parsing your server configuration and cannot be used. The error was:

{0}F)pauseN)setz_utilr   ZIDisplayr   environ	enumerateZmenudisplay_utilZOKr3   Znotificationformatr0   )	r4   r   ZoptsnamesZdispZ
apache_idxcodeindexr5   r   r   r   r1   }   s(    



r1   webrootmanual
standalonedns-cloudflaredns-cloudxnsdns-digitaloceandns-dnsimpledns-dnsmadeeasy
dns-gehirn
dns-google
dns-linode
dns-luadns	dns-nsonedns-ovhdns-rfc2136dns-route53dns-sakuracloudc                 C   sB   |r| |jnd| _|r&| |jnd| _td| j| j dS )zFUpdate the config entries to reflect the plugins we actually selected.Nz0Plugins selected: Authenticator %s, Installer %s)Z	find_initr   authenticator	installerr(   info)r   r   Zauthr+   r   r   r   record_chosen_plugins   s     r]   c                 C   s>  t | \}}d}|dkrd}|dkr`d }}ddlm} |tkrh|shd|tj|}	t|	nd	 }}|d
krtd}|dks|dkrd}| j	rt
d| d }
}|dkr||krt| || }
}n&|s|rt| |||}|rt| ||}
t
d|
| |r|std|| |r(|
s(td|| t| ||
| ||
fS )a;  
    Figure out which configurator we're going to use, modifies
    config.authenticator and config.installer strings to reflect that choice if
    necessary.

    :raises errors.PluginSelectionError if there was a problem

    :returns: (an `IAuthenticator` or None, an `IInstaller` or None)
    :rtype: tuple
    NZenhancezMWhich installer would you like to use to configure the selected enhancements?runTr   cli_commandzWith the {0} plugin, you probably want to use the "certonly" command, eg:{1}{1}    {2} certonly --{0}{1}{1}(Alternatively, add a --installer flag. See https://eff.org/letsencrypt-plugins{1} and "--help plugins" for more information.)FZcertonlyZinstallzRSpecifying an authenticator doesn't make sense when running Certbot with verb "%s"z*Selected authenticator %s and installer %sr[   rZ   )r    certbot.clir`   noninstaller_pluginsrE   r   linesepr   r.   rZ   r(   Zwarningr   r   r   r)   diagnose_configurator_problemr]   )r   r   Zverbreq_authr   Zinstaller_questionZ	need_instZ	need_authr`   msgrZ   r[   r   r   r   choose_configurator_plugins   sJ      rg   c                 C   s6   |s| S | r2| |kr2d}t |t| t||S )z
    Setting configurators multiple ways is okay, as long as they all agree
    :param str previously: previously identified request for the installer/authenticator
    :param str requested: the request currently being processed
    zIToo many flags setting configurators/installers/authenticators {0} -> {1})r   r%   rE   repr)Z
previouslyZnowrf   r   r   r   set_configurator   s    ri   c                 C   s  | j  }}t|| j}t|| j}| jr<t|d}t|d}| jrVt|d}t|d}| jrft|d}| jrvt|d}| jrt|d}| j	rt|d}| j
rt|d}| jrt|d}| jrt|d	}| jrt|d
}| jrt|d}| jrt|d}| jrt|d}| jrt|d}| jr,t|d}| jr>t|d}| jrPt|d}| jrbt|d}| jrtt|d}td|| ||fS )z
    Figure out which plugins the user requested with CLI and config options

    :returns: (requested authenticator string or None, requested installer string or None)
    :rtype: tuple
    r<   r;   rK   rI   rJ   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   z+Requested authenticator %s and installer %s)Zconfiguratorri   r[   rZ   r<   r;   rK   rI   rJ   Zdns_cloudflareZdns_cloudxnsZdns_digitaloceanZdns_dnsimpleZdns_dnsmadeeasyZ
dns_gehirnZ
dns_googleZ
dns_linodeZ
dns_luadnsZ	dns_nsoneZdns_ovhZdns_rfc2136Zdns_route53Zdns_sakuracloudr(   r)   )r   r   re   r   r   r   r      sZ    





















r    c                 C   sd   |r,||krd |}qVd ||| j}n*| dkrLddlm} d |}n
d | }t|dS )	a>  
    Raise the most helpful error message about a plugin being unavailable

    :param str cfg_type: either "installer" or "authenticator"
    :param str requested: the plugin that was requested
    :param .PluginsRegistry plugins: available plugins

    :raises error.PluginSelectionError: if there was a problem
    z8The requested {0} plugin does not appear to be installedzkThe {0} plugin is not working; there may be problems with your existing configuration.
The error was: {1!r}r[   r   r_   zCertbot doesn't know how to automatically configure the web server on this system. However, it can still get a certificate for you. Please run "{0} certonly" to do so. You'll need to manually configure your web server to use the resulting certificate.z/{0} could not be determined or is not installedN)rE   Zproblemra   r`   r   r%   )Zcfg_typeZ	requestedr   rf   r`   r   r   r   rd   8  s     
rd   )r   )r   )r   )!__doc__Z
__future__r   Zloggingr2   Zzope.componentzopeZcertbotr   r   Zcertbot.compatr   Zcertbot.displayr   rD   Z	getLogger__name__r(   Z	componentZ
getUtilityrA   r   r   r   r,   r   r1   rb   r]   rg   ri   r    rd   r   r   r   r   <module>   sL   
 
 
 
6'
         @7