
    M/e                          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rddlmZ  ej0                  e      Z G d dej6                        Zy)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                        e Zd ZdZdZdZd fdZdee   fdZ	ddZ
dd	Zd
edee   fdZdee   fdZdee   fdZdee   fdZdedefdZdeddfdZ x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>
    returnNc                    t         |   |       |  t        j                  j	                  | j
                  j                  d      d      | _        t        j                  j	                  | j
                  j                  d      d      | _        t        j                  j	                  | j
                  j                  j                  d      | _        t               | _        y )Nzchallenge-locationzle_http_01_challenge_pre.confzle_http_01_challenge_post.confhttp_challenges)super__init__r
   pathjoinconfiguratorconfchallenge_conf_prechallenge_conf_postconfigwork_dirchallenge_dirsetmoded_vhosts)selfr   	__class__s     B/usr/lib/python3/dist-packages/certbot_apache/_internal/http_01.pyr   zApacheHttp01.__init__'   s    &"$'',,""#78+#- $&77<<""#78,$.   WW\\$$-- /2e    c                 v   | 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   saveensure_listenstrr   http01_portprepare_http01_modules_set_up_challenges_mod_config)r!   	responsess     r#   performzApacheHttp01.perform5   s    ||I 	?F''D,=,=,D,D,P,P(QR##%++-	/6r$   c                     | j                   j                  d      rOddg}|D ]E  }|dz   | j                   j                  j                  vs)| j                   j	                  |d       G yy)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mods      r#   r+   z#ApacheHttp01.prepare_http01_modulesH   sp     !!"23'6N% A?$*;*;*B*B*J*JJ%%0040@A 4r$   c                 ,  	 g }t        | j                  j                  j                        	| j                  D ]   }|| j                  |j                        z  }" d}|D ]#  }t        	fd|j                  D              s"d}% |r|| j                         z  }n|| j                         z  }|D ]  }| j                  |        | j                  j                  j                  d| j                         | j                  j                  j                  d| j                         | j                   j#                  | j$                        }| j&                  j#                  | j$                        }t(        j+                  d|       t-        | j                  d      5 }|j/                  |       d d d        t(        j+                  d|       t-        | j                  d      5 }|j/                  |       d d d        y # 1 sw Y   PxY w# 1 sw Y   y xY w)NFc              3   h   K   | ])  }|j                         xs |j                         k(   + y wNis_wildcardget_port).0a	http_ports     r#   	<genexpr>z+ApacheHttp01._mod_config.<locals>.<genexpr>]   s*     UA1==??ajjli&??U   /2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_confrB   s
            @r#   r-   zApacheHttp01._mod_configQ   s   -/))00<<=	 \\ 	CEt44U\\BBO	C
 $ 	EUUU	 t3355O t4466O " 	0B++B/	0 	""99$))	+""99$**	, 44;;D<N<NO66==d>P>PQ@/R$))3/ 	,8NN?+	,ACST$**C0 	-HNN+,	- 	-	, 	,	- 	-s   G>#H
>H
HrG   c                     g }| j                   j                  D ]>  }| j                   j                  |j                         |      s.|j	                  |       @ |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!   rG   matching_vhostsrY   s       r#   rF   zApacheHttp01._matching_vhosts{   sY    
 &&-- 	.E  001BFK  &&u-		. r$   c                 Z   t        | j                  j                  j                        g }| j                  j                  D ]?  }t        fd|j                  D              s"|j                  r/|j                  |       A |s$t        j                  dj                              |S )Nc              3   h   K   | ])  }|j                         xs |j                         k(   + y wr<   r=   )r@   rA   r*   s     r#   rC   z0ApacheHttp01._relevant_vhosts.<locals>.<genexpr>   s*     Wa1==?Aajjlk&AAWrD   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_   rH   rI   sslrb   r   PluginErrorrP   )r!   relevant_vhostsrY   r*   s      @r#   rK   zApacheHttp01._relevant_vhosts   s    $++22>>?-/&&-- 	2EW5;;WWyy#**51	2 $$ {+	- - r$   c                 n    | j                   j                  D cg c]  }|j                  | c}S c c}w )z1Return all VirtualHost objects with no ServerName)r   r_   name)r!   rZ   s     r#   rJ   zApacheHttp01._unnamed_vhosts   s*    !..55IrIIIs   22c                    t         j                  j                  | j                        s?t	        j
                  d      5  	 t	        j                  | j                  d       d d d        g }| j                  D ]"  }|j                  | j                  |             $ |S # t        $ rK}|j                  t        j                  t        j                  fvrt        j                  d      Y d }~d }~ww xY w# 1 sw Y   xY w)N   i  z*Couldn't create root for http-01 challenge)r
   r   isdirr   r	   
temp_umaskmakedirsOSErrorerrnoEEXISTEISDIRr   rg   r&   rb   _set_up_challenge)r!   	exceptionr.   achalls       r#   r,   zApacheHttp01._set_up_challenges   s    ww}}T//0&&u- JJ''(:(:EBJ 	ll 	=FT33F;<	=   J u||U\\.JJ$00HJ J KJJ Js0   C5 B	C2'AC-(C5-C22C55C>rv   c                    |j                         \  }}t        j                  j                  | j                  |j
                  j                  d            }| j                  j                  j                  d|       t        |d      5 }|j                  |j                                d d d        t        j                  |d       |S # 1 sw Y   !xY w)NtokenTwbi  )response_and_validationr
   r   r   r   rW   encoder   rM   rN   rT   rU   r	   chmod)r!   rv   response
validationrj   fs         r#   rt   zApacheHttp01._set_up_challenge   s    %==?*GGLL!3!3V\\5H5H5QR""99$E$ 	)GGJ%%'(	)u%		) 	)s   	 C		CrY   c                 j   || j                   vr$t        j                  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                  |       yy)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    rR   rS   rj   filepr   r5   add_dir_beginningr   r   add_dirr   enabledr   locadd)r!   rY   s     r#   rL   z'ApacheHttp01._set_up_include_directives   s     )))LLU

EKK) $$66

It'>'>@$$,,

It'?'?A ==!!((00 !2!2!9!9!=!=i!HIu{{, !!%( *r$   )r   r   r   N)r   N)__name__
__module____qualname____doc__rO   rQ   r   r   r   r/   r+   r-   r)   r   rF   rK   rJ   r,   r   rt   rL   __classcell__)r"   s   @r#   r   r      s    P4?@ &A(-Ts tK/@ ${"3  Jk!2 JD)J$K  (J @) ) )r$   r   )r   rq   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   rR   ChallengePerformerr    r$   r#   <module>r      s[    9       =  B %  " 4 8H			8	$u)6,, u)r$   