
\\                  @   s   d  Z  d d l 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 e j e  Z Gd d	   d	 e	 j  Z d S)
z3A class that performs HTTP-01 challenges for Apache    N)ListSet)errors)common)VirtualHost)get_aug_pathc                   s   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   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                s   t  t |   j | |   t j j |  j j d  d  |  _ t j j |  j j d  d  |  _	 t j 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__os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
   -   s    			zApacheHttp01.__init__c             C   sv   |  j  s g  S|  j j d d  |  j j t |  j j j   |  j   |  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   s   |  j  j d  r d g } |  j  j d k  r= | j d  n | j d  x= | D]5 } | d |  j  j j k rQ |  j  j | d d	 qQ Wd
 S)z>Make sure that we have the needed modules available for http01zhandle-modulesZrewrite      Z
authz_hostZ
authz_core_moduleZtempTN)r$   r%   )r   r   versionappendparsermodulesZ
enable_mod)r   Zneeded_modulesmodr   r   r   r   N   s    	z#ApacheHttp01.prepare_http01_modulesc                s  g  } t  |  j j j    x; |  j D]0 } x' |  j | j  D] } | j |  q> Wq% Wd } x6 | D]. } t   f d d   | j	 D  rf d } qf W| s x! |  j
   D] } | j |  q Wx | D] } |  j |  q W|  j j j d |  j  |  j j j d |  j  |  j j d
 k  r9|  j } |  j } n |  j } |  j } | j |  j  } | j |  j  }	 t j d |  t |  j d   }
 |
 j |  Wd  QRXt j d	 |	  t |  j d   }
 |
 j |	  Wd  QRXd  S)NFc             3   s-   |  ]# } | j    p$ | j     k Vq d  S)N)is_wildcardget_port).0a)	http_portr   r   	<genexpr>g   s    z+ApacheHttp01._mod_config.<locals>.<genexpr>Tr$   r%   z(writing a pre config file with text:
 %swz)writing a post config file with text:
 %s)r$   r%   )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   )r0   r   r!   [   s>    "
			zApacheHttp01._mod_configc             C   sI   g  } x< |  j  j D]. } |  j  j | j   |  r | j |  q W| S)zReturn 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   r4   Zmatching_vhostsrF   r   r   r   r3      s
    zApacheHttp01._matching_vhostsc                s   t  |  j j j    g  } xL |  j j D]> } t   f d d   | j D  r( | j s( | j |  q( W| s t	 j
 d j      | S)Nc             3   s-   |  ]# } | j    p$ | j     k Vq d  S)N)r,   r-   )r.   r/   )r   r   r   r1      s    z0ApacheHttp01._relevant_vhosts.<locals>.<genexpr>zUnable 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   rG   r5   r6   Zsslr(   r   ZPluginErrorr?   )r   Zrelevant_vhostsrF   r   )r   r   r7      s    "	zApacheHttp01._relevant_vhostsc             C   sl   t  j j |  j  s8 t  j |  j  t  j |  j d  g  } x' |  j D] } | j |  j |   qH W| S)Ni  )	r   r   isdirr   makedirschmodr   r(   _set_up_challenge)r   r"   achallr   r   r   r       s    zApacheHttp01._set_up_challengesc          
   C   s   | j    \ } } t j j |  j | j j d   } |  j j j	 d |  t
 | d   } | j | j    Wd  QRXt j | d  | S)NtokenTwbi  )Zresponse_and_validationr   r   r   r   rD   encoder   r9   r:   rB   rC   rJ   )r   rL   ZresponseZ
validationnamefr   r   r   rK      s    $zApacheHttp01._set_up_challengec             C   s   | |  j  k r t j d d | j | j  |  j j j | j d |  j	  |  j j j
 | j d |  j  | j s |  j j j
 t |  j j j d  d | j  |  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 treez=Adding a temporary challenge validation Include for name: %s zin: %sIncludedefaultN)r   r@   rA   rP   Zfilepr   r)   Zadd_dir_beginningr   r   Zadd_dirr   enabledr   locadd)r   rF   r   r   r   r8      s    	z'ApacheHttp01._set_up_include_directives)__name__
__module____qualname____doc__r;   r<   r=   r>   r
   r#   r   r!   r3   r7   r    rK   r8   r   r   )r   r   r      s   
-r   )rZ   Zloggingr   Zacme.magic_typingr   r   Zcertbotr   Zcertbot.pluginsr   Zcertbot_apache.objr   Zcertbot_apache.parserr   Z	getLoggerrW   r@   ZTLSSNI01r   r   r   r   r   <module>   s   