a
    `H                     @   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 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)
interfaces)reverter)util)hooks)misc)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  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 scriptsTa  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. For both authenticator and cleanup script, on HTTP-01 and DNS-01 challenges, $CERTBOT_REMAINING_CHALLENGES will be equal to the number of challenges that remain after the current one, and $CERTBOT_ALL_DOMAINS contains a comma-separated list of all domains that are challenged for the current certificate.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                    sD   t t| j|i | t| j| _| j  i | _d| _d| _	d S )NF)
superr   __init__r   ZReverterconfigrecovery_routineenvsubsequent_dns_challengesubsequent_any_challenge)selfargskwargs	__class__ B/usr/lib/python3/dist-packages/certbot/_internal/plugins/manual.pyr   H   s    
zAuthenticator.__init__c                 C   s*   |ddd |ddd t |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 scriptzpublic-ip-logging-okr   )r   Zadd_deprecated_argument)clsaddr   r   r   add_parser_argumentsQ   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   ZPluginErrorformatoption_name_validate_hooksr   r   r   r   prepareY   s    zAuthenticator.preparec                 C   sL   | j jrHdD ]:}| |}|d u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%   a   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_infoi   s    zAuthenticator.more_infoc                 C   s   t jt jgS N)r   HTTP01DNS01)r   domainr   r   r   get_chall_prefo   s    zAuthenticator.get_chall_prefc                 C   sF   g }|D ]8}|  dr$| || n
| | |||j q|S )Nr   )r"   _perform_achall_with_script_perform_achall_manuallyappendZresponseaccount_key)r   achallsZ	responsesachallr   r   r   performs   s    

zAuthenticator.performc              	   C   s   t |j||jddd |D tt||| d d}t|j	t
jr`|j	d|d< ntjdd  tj| | d\}}| |d	< || j|< d S )
N,c                 s   s   | ]}|j V  qd S r,   )r/   ).0Z
one_achallr   r   r   	<genexpr>       z<Authenticator._perform_achall_with_script.<locals>.<genexpr>   )ZCERTBOT_DOMAINZCERTBOT_VALIDATIONZCERTBOT_ALL_DOMAINSZCERTBOT_REMAINING_CHALLENGEStokenCERTBOT_TOKENr   ZCERTBOT_AUTH_OUTPUT)dictr/   
validationr4   joinstrr(   index
isinstancechallr   r-   encoder   environpopupdate_execute_hookstripr   )r   r6   r5   r   _outr   r   r   r1   }   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sZJ | 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 )Nr=   )r6   Zencoded_tokenZporturir@   )r/   r@   TF)ZwrapZforce_interactive)r@   r4   rD   rE   r   r-   _HTTP_INSTRUCTIONSr#   rF   r   Zhttp01_portrN   r/   r.   _DNS_INSTRUCTIONSZvalidation_domain_namer   &_SUBSEQUENT_DNS_CHALLENGE_INSTRUCTIONSr   "_SUBSEQUENT_CHALLENGE_INSTRUCTIONSzopeZ	componentZ
getUtilityr   ZIDisplayZnotification)r   r6   r@   msgZdisplayr   r   r   r2      s*    


z&Authenticator._perform_achall_manuallyc                 C   sZ   |  drL|D ]<}| j|}d|vr4tjdd  tj| | d q| j  d S )Nr   r>   )	r"   r   rH   r   rG   rI   rJ   r   r   )r   r5   r6   r   r   r   r   cleanup   s    
zAuthenticator.cleanupc                 C   s    t j| || |t dS )N)r   )r
   Zexecute_commandr$   r"   r   Zenv_no_snap_for_external_calls)r   Z	hook_namer   r   r   rJ      s    zAuthenticator._execute_hook)__name__
__module____qualname____doc__ZdescriptionZhiddenZlong_descriptionrP   rO   rR   rQ   r   classmethodr!   r'   r%   r+   r0   r7   r1   r2   rU   rJ   __classcell__r   r   r   r   r      s*   		


r   )rY   Zzope.componentrS   Zzope.interfaceZacmer   Zacme.magic_typingr   Zcertbotr   r   r   r   r   Zcertbot._internalr	   Zcertbot.compatr
   r   Zcertbot.pluginsr   Z	interfaceZimplementerZIAuthenticatorZproviderZIPluginFactoryZPluginr   r   r   r   r   <module>   s    