U
    ˜­“]D  ã                   @   s„   d Z ddlZddlmZ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 e¡ZG d
d„ dejƒZdS )z3A class that performs HTTP-01 challenges for Apacheé    N)ÚListÚSet)Úerrors)Úos)Ú
filesystem)Úcommon)ÚVirtualHost)Úget_aug_pathc                       sp   e Zd ZdZdZdZdZdZ‡ f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 )ÚApacheHttp01zFClass that performs HTTP-01 challenges within the Apache configurator.zn        RewriteEngine on
        RewriteRule ^/\.well-known/acme-challenge/([A-Za-z0-9-_=]+)$ {0}/$1 [L]

    zä        <Directory {0}>
            Order Allow,Deny
            Allow from all
        </Directory>
        <Location /.well-known/acme-challenge>
            Order Allow,Deny
            Allow from all
        </Location>
    zo        RewriteEngine on
        RewriteRule ^/\.well-known/acme-challenge/([A-Za-z0-9-_=]+)$ {0}/$1 [END]
    z´        <Directory {0}>
            Require all granted
        </Directory>
        <Location /.well-known/acme-challenge>
            Require all granted
        </Location>
    c                    sd   t t| ƒj||Ž tj | j d¡d¡| _tj | j d¡d¡| _	tj | jj
jd¡| _tƒ | _d S )Nzchallenge-locationzle_http_01_challenge_pre.confzle_http_01_challenge_post.confZhttp_challenges)Úsuperr
   Ú__init__r   ÚpathÚjoinÚconfiguratorÚconfÚchallenge_conf_preÚchallenge_conf_postÚconfigZwork_dirÚchallenge_dirÚsetÚmoded_vhosts)ÚselfÚargsÚkwargs©Ú	__class__© ú8/usr/lib/python3/dist-packages/certbot_apache/http_01.pyr   1   s    
þ
þþzApacheHttp01.__init__c                 C   sX   | j s
g S | j dd¡ | j t| jjjƒ¡ |  ¡  |  ¡ }|  	¡  | j dd¡ |S )zPerform all HTTP-01 challenges.zChanges before challenge setupTzHTTP Challenge)
Úachallsr   ZsaveZensure_listenÚstrr   Úhttp01_portÚprepare_http01_modulesÚ_set_up_challengesÚ_mod_config)r   Ú	responsesr   r   r   Úperform>   s    ÿzApacheHttp01.performc                 C   sd   | j  d¡r`dg}| j jdk r*| d¡ n
| d¡ |D ]&}|d | j jjkr8| j j|dd q8d	S )
z>Make sure that we have the needed modules available for http01zhandle-modulesZrewrite©é   é   Z
authz_hostZ
authz_coreÚ_moduleT)ZtempN)r   r   ÚversionÚappendÚparserÚmodulesZ
enable_mod)r   Zneeded_modulesÚmodr   r   r   r!   R   s    
z#ApacheHttp01.prepare_http01_modulesc              	      sV  g }t | jjjƒ‰ | jD ] }|  |j¡D ]}| |¡ q(qd}|D ] }t‡ fdd„|j	D ƒƒrBd}qB|s€|  
¡ D ]}| |¡ qp|D ]}|  |¡ q„| jj d| j¡ | jj d| j¡ | jjdk rÒ| j}| j}n| j}| j}| | j¡}| | j¡}	t d|¡ t| jdƒ}
|
 |¡ W 5 Q R X t d|	¡ t| jdƒ}
|
 |	¡ W 5 Q R X d S )	NFc                 3   s"   | ]}|  ¡ p| ¡ ˆ kV  qd S ©N©Zis_wildcardZget_port©Ú.0Úa©Z	http_portr   r   Ú	<genexpr>k   s     z+ApacheHttp01._mod_config.<locals>.<genexpr>Tr&   z(writing a pre config file with text:
 %sÚwz)writing a post config file with text:
 %s)r   r   r   r    r   Ú_matching_vhostsÚdomainr+   ÚanyÚaddrsÚ_relevant_vhostsÚ_set_up_include_directivesÚreverterÚregister_file_creationr   r   r*   ÚCONFIG_TEMPLATE22_PREÚCONFIG_TEMPLATE22_POSTÚCONFIG_TEMPLATE24_PREÚCONFIG_TEMPLATE24_POSTÚformatr   ÚloggerÚdebugÚopenÚwrite)r   Zselected_vhostsÚchallZvhÚfoundÚvhostZconfig_template_preZconfig_template_postZconfig_text_preZconfig_text_postZnew_confr   r4   r   r#   _   sF    
 ÿ ÿzApacheHttp01._mod_configc                 C   s2   g }| j jD ] }| j  | ¡ |¡r| |¡ q|S )z²Return all VirtualHost objects that have the requested domain name or
        a wildcard name that would match the domain in ServerName or ServerAlias
        directive.
        )r   ÚvhostsZdomain_in_namesZ	get_namesr+   )r   r8   Zmatching_vhostsrJ   r   r   r   r7   Œ   s
    zApacheHttp01._matching_vhostsc                    s`   t | jjjƒ‰ g }| jjD ],}t‡ fdd„|jD ƒƒr|js| |¡ q|s\t	 
d ˆ ¡¡‚|S )Nc                 3   s"   | ]}|  ¡ p| ¡ ˆ kV  qd S r/   r0   r1   ©r    r   r   r5   ž   s     z0ApacheHttp01._relevant_vhosts.<locals>.<genexpr>z²Unable to find a virtual host listening on port {0} which is currently needed for Certbot to prove to the CA that you control your domain. Please add a virtual host for port {0}.)r   r   r   r    rK   r9   r:   Zsslr+   r   ZPluginErrorrC   )r   Zrelevant_vhostsrJ   r   rL   r   r;   š   s    ýÿzApacheHttp01._relevant_vhostsc                 C   s@   t j | j¡st | jd¡ g }| jD ]}| |  |¡¡ q&|S )Nií  )	r   r   Úisdirr   r   Úmakedirsr   r+   Ú_set_up_challenge)r   r$   Úachallr   r   r   r"   ª   s    
zApacheHttp01._set_up_challengesc              	   C   sh   |  ¡ \}}tj | j|j d¡¡}| jj 	d|¡ t
|dƒ}| | ¡ ¡ W 5 Q R X t |d¡ |S )NÚtokenTÚwbi¤  )Zresponse_and_validationr   r   r   r   rH   Úencoder   r=   r>   rF   rG   r   Úchmod)r   rP   ZresponseZ
validationÚnameÚfr   r   r   rO   ´   s    zApacheHttp01._set_up_challengec                 C   s€   || j kr|t d|j|j¡ | jj |jd| j	¡ | jj 
|jd| j¡ |jsp| jj 
t| jjjd ƒd|j¡ | j  |¡ dS )zIncludes override configuration to the beginning and to the end of
        VirtualHost. Note that this include isn't added to Augeas search treezCAdding a temporary challenge validation Include for name: %s in: %sZIncludeÚdefaultN)r   rD   rE   rU   Zfilepr   r,   Zadd_dir_beginningr   r   Zadd_dirr   Úenabledr	   ZlocÚadd)r   rJ   r   r   r   r<   À   s.    
 þ  ÿ  ÿ þz'ApacheHttp01._set_up_include_directives)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r?   r@   rA   rB   r   r%   r!   r#   r7   r;   r"   rO   r<   Ú__classcell__r   r   r   r   r
      s   	-
r
   )r]   ZloggingZacme.magic_typingr   r   Zcertbotr   Zcertbot.compatr   r   Zcertbot.pluginsr   Zcertbot_apache.objr   Zcertbot_apache.parserr	   Z	getLoggerrZ   rD   ZTLSSNI01r
   r   r   r   r   Ú<module>   s   
