o
    c                      @   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rTd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)TYPE_CHECKING)!KeyAuthorizationChallengeResponse)errors)"KeyAuthorizationAnnotatedChallenge)
filesystem)os)common)VirtualHost)get_aug_path)ApacheConfiguratorc                       s   e Zd ZdZdZdZd fdd	Zdee fd
dZ	d ddZ
d ddZdedee fddZdee fddZdee fddZdee fddZdedefddZdeddfddZ  ZS )!ApacheHttp01zFClass that performs HTTP-01 challenges within the Apache configurator.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>
    configuratorr   returnNc                    sb   t  | |  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.confhttp_challenges)super__init__r	   pathjoinr   confchallenge_conf_prechallenge_conf_postconfigwork_dirchallenge_dirsetmoded_vhosts)selfr   	__class__ M/opt/certbot/lib/python3.10/site-packages/certbot_apache/_internal/http_01.pyr   '   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   saveensure_listenstrr   http01_portprepare_http01_modules_set_up_challenges_mod_config)r   	responsesr!   r!   r"   perform5   s   zApacheHttp01.performc                 C   sH   | j dr ddg}|D ]}|d | j jjvr| j j|dd qdS dS )z>Make sure that we have the needed modules available for http01zhandle-modulesrewrite
authz_core_moduleT)tempN)r   r   parsermodules
enable_mod)r   needed_modulesmodr!   r!   r"   r(   H   s   z#ApacheHttp01.prepare_http01_modulesc           	         sT  g }t | jjj | jD ]
}|| |j7 }qd}|D ]}t fdd|jD r+d}q|r5|| 	 7 }n|| 
 7 }|D ]}| | q=| jjd| j | jjd| j | j| j}| j| j}td| t| jd}|| W d    n1 sw   Y  td| t| jd}|| W d    d S 1 sw   Y  d S )NFc                 3   $    | ]}|  p|  kV  qd S Nis_wildcardget_port.0a	http_portr!   r"   	<genexpr>]      " z+ApacheHttp01._mod_config.<locals>.<genexpr>Tz(writing a pre config file with text:
 %swz)writing a post config file with text:
 %s)r&   r   r   r'   r#   _matching_vhostsdomainanyaddrs_unnamed_vhosts_relevant_vhosts_set_up_include_directivesreverterregister_file_creationr   r   CONFIG_TEMPLATE24_PREformatr   CONFIG_TEMPLATE24_POSTloggerdebugopenwrite)	r   selected_vhostschallfoundvhostvhconfig_text_preconfig_text_postnew_confr!   r>   r"   r*   Q   s<   
"zApacheHttp01._mod_configrD   c                 C   s2   g }| j jD ]}| j | |r|| q|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   vhostsdomain_in_names	get_namesappend)r   rD   matching_vhostsrV   r!   r!   r"   rC   {   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   r6   r7   r8   r;   r'   r!   r"   r@      rA   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'   r[   rE   rF   sslr^   r   PluginErrorrM   )r   relevant_vhostsrV   r!   r`   r"   rH      s   
zApacheHttp01._relevant_vhostsc                 C   s   dd | j jD S )z1Return all VirtualHost objects with no ServerNamec                 S   s   g | ]	}|j d u r|qS r7   )name)r<   rW   r!   r!   r"   
<listcomp>   s    z0ApacheHttp01._unnamed_vhosts.<locals>.<listcomp>)r   r[   )r   r!   r!   r"   rG      s   zApacheHttp01._unnamed_vhostsc                 C   s   t j| jsFtd2 z	t| jd W n  ty6 } z|jtj	tj
fvr,tdW Y d }~nd }~ww W d    n1 sAw   Y  g }| jD ]
}|| | qK|S )N   i  z*Couldn't create root for http-01 challenge)r	   r   isdirr   r   
temp_umaskmakedirsOSErrorerrnoEEXISTEISDIRr   rb   r#   r^   _set_up_challenge)r   	exceptionr+   achallr!   r!   r"   r)      s$   
zApacheHttp01._set_up_challengesrp   c                 C   s|   |  \}}tj| j|jd}| jj	d| t
|d}||  W d    n1 s1w   Y  t|d |S )NtokenTwbi  )response_and_validationr	   r   r   r   rT   encoder   rJ   rK   rQ   rR   r   chmod)r   rp   response
validationrd   fr!   r!   r"   rn      s   zApacheHttp01._set_up_challengerV   c                 C   s   || j vr@td|j|j | jj|jd| j	 | jj
|jd| j |js8| jj
t| jjjd d|j | j | dS 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: %sIncludedefaultN)r   rO   rP   rd   filepr   r1   add_dir_beginningr   r   add_dirr   enabledr   locadd)r   rV   r!   r!   r"   rI      s$   


z'ApacheHttp01._set_up_include_directives)r   r   r   N)r   N)__name__
__module____qualname____doc__rL   rN   r   r   r   r,   r(   r*   r&   r   rC   rH   rG   r)   r   rn   rI   __classcell__r!   r!   r   r"   r      s     	

	*
r   )r   rk   loggingtypingr   r   r   acme.challengesr   certbotr   certbot.achallengesr   certbot.compatr   r	   certbot.pluginsr
   certbot_apache._internal.objr   certbot_apache._internal.parserr   %certbot_apache._internal.configuratorr   	getLoggerr   rO   ChallengePerformerr   r!   r!   r!   r"   <module>   s$    
