U
    ]                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ejejejejG dd dejZdS )zManual authenticator plugin    N)
challenges)Dict)achallenges)errors)hooks)
interfaces)reverter)os)commonc                       s   e Zd ZdZdZdZdZdZdZdZ	dZ
 fd	d
Zedd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Z  ZS )!AuthenticatorzManual authenticator

    This plugin allows the user to perform the domain validation
    challenge(s) themselves. This either be done manually by the user or
    through shell scripts provided to Certbot.

    z2Manual configuration or run your own shell scriptsTai  Authenticate through manual configuration or custom shell scripts. When using shell scripts, an authenticator script must be provided. The environment variables available to this script depend on the type of challenge. $CERTBOT_DOMAIN will always contain the domain being authenticated. For HTTP-01 and DNS-01, $CERTBOT_VALIDATION is the validation string, and $CERTBOT_TOKEN is the filename of the resource requested when performing an HTTP-01 challenge. An additional cleanup script can also be provided and can use the additional variable $CERTBOT_AUTH_OUTPUT which contains the stdout output from the auth script.zPlease deploy a DNS TXT record under the name
{domain} with the following value:

{validation}

Before continuing, verify the record is deployed.zuCreate a file containing just this data:

{validation}

And make it available on your web server at this URL:

{uri}
z
(This must be set up in addition to the previous challenges; do not remove,
replace, or undo the previous challenge tasks yet.)
z
(This must be set up in addition to the previous challenges; do not remove,
replace, or undo the previous challenge tasks yet. Note that you might be
asked to create multiple distinct TXT records with the same name. This is
permitted by DNS standards.)
c                    sB   t t| j|| t| j| _| j  t | _d| _	d| _
d S )NF)superr   __init__r   ZReverterconfigrecovery_routinedictenvsubsequent_dns_challengesubsequent_any_challenge)selfargskwargs	__class__ 8/usr/lib/python3/dist-packages/certbot/plugins/manual.pyr   C   s    
zAuthenticator.__init__c                 C   s*   |ddd |ddd |dddd	 d S )
N	auth-hookz8Path or command to execute for the authentication script)helpcleanup-hookz1Path or command to execute for the cleanup scriptpublic-ip-logging-ok
store_truez5Automatically allows public IP logging (default: Ask))actionr   r   )clsaddr   r   r   add_parser_argumentsL   s    z"Authenticator.add_parser_argumentsc                 C   s4   | j jr(| ds(td| d|   d S )Nr   zdAn authentication script must be provided with --{0} when using the manual plugin non-interactively.)r   Znoninteractive_modeconfr   PluginErrorformatoption_name_validate_hooksr   r   r   r   prepareU   s    zAuthenticator.preparec                 C   sL   | j jrHdD ]:}| |}|d k	r| |d td  }t|| qd S )N)r   r   z-hook)r   Zvalidate_hooksr$   r'   lenr   Zvalidate_hook)r   namehookZhook_prefixr   r   r   r(   ]   s    
zAuthenticator._validate_hooksc                 C   s   dS )NzThis plugin allows the user to customize setup for domain validation challenges either through shell scripts provided by the user or by performing the setup manually.r   r)   r   r   r   	more_infoe   s    zAuthenticator.more_infoc                 C   s   t jt jgS N)r   HTTP01DNS01)r   domainr   r   r   get_chall_prefk   s    zAuthenticator.get_chall_prefc                 C   sL   |    | dr| j}n| j}g }|D ]}|| |||j q(|S )Nr   )_verify_ip_logging_okr$   _perform_achall_with_script_perform_achall_manuallyappendZresponseaccount_key)r   achallsZperform_achallZ	responsesachallr   r   r   performo   s    
zAuthenticator.performc                 C   s`   |  ds\d| d}d}tjtj}|j||ddrRt	| j
| dd n
tdd S )Nr   z--{0}zNOTE: The IP of this machine will be publicly logged as having requested this certificate. If you're running certbot in manual mode on a machine that is not your server, please ensure you're okay with that.

Are you OK with your IP being logged?T)cli_flagforce_interactivez#Must agree to IP logging to proceed)r$   r&   r'   zope	component
getUtilityr   IDisplayZyesnosetattrr   destr   r%   )r   r<   msgdisplayr   r   r   r4   |   s    
z#Authenticator._verify_ip_logging_okc                 C   sx   t |j||jd}t|jtjr6|jd|d< nt	j
dd  t	j
| | d\}}| |d< || j|< d S )N)ZCERTBOT_DOMAINZCERTBOT_VALIDATIONtokenCERTBOT_TOKENr   ZCERTBOT_AUTH_OUTPUT)r   r2   
validationr8   
isinstancechallr   r0   encoder	   environpopupdate_execute_hookstripr   )r   r:   r   _outr   r   r   r5      s    
z)Authenticator._perform_achall_with_scriptc                 C   s   | |j}t|jtjrH| jj||jd| j	j
|j|j|d}n*t|jtjsZt| jj||j|d}t|jtjr| jr|| j7 }d| _n| jr|| j7 }tjtj}|j|ddd d| _d S )NrF   )r:   Zencoded_tokenZporturirH   )r2   rH   TF)Zwrapr=   )rH   r8   rI   rJ   r   r0   _HTTP_INSTRUCTIONSr&   rK   r   Zhttp01_portrS   r2   r1   AssertionError_DNS_INSTRUCTIONSZvalidation_domain_namer   &_SUBSEQUENT_DNS_CHALLENGE_INSTRUCTIONSr   "_SUBSEQUENT_CHALLENGE_INSTRUCTIONSr>   r?   r@   r   rA   Znotification)r   r:   rH   rD   rE   r   r   r   r6      s.     
 


z&Authenticator._perform_achall_manuallyc                 C   sZ   |  drL|D ]<}| j|}d|kr4tjdd  tj| | d q| j  d S )Nr   rG   )	r$   r   rM   r	   rL   rN   rO   r   r   )r   r9   r:   r   r   r   r   cleanup   s    
zAuthenticator.cleanupc                 C   s   t | || |S r/   )r   Zexecuter'   r$   )r   Z	hook_namer   r   r   rO      s    zAuthenticator._execute_hook)__name__
__module____qualname____doc__ZdescriptionZhiddenZlong_descriptionrV   rT   rX   rW   r   classmethodr#   r*   r(   r.   r3   r;   r4   r5   r6   rY   rO   __classcell__r   r   r   r   r      s,   
		

r   )r]   Zzope.componentr>   Zzope.interfaceZacmer   Zacme.magic_typingr   Zcertbotr   r   r   r   r   Zcertbot.compatr	   Zcertbot.pluginsr
   Z	interfaceZimplementerZIAuthenticatorZproviderZIPluginFactoryZPluginr   r   r   r   r   <module>   s   