
    M/e|$                        d Z ddlZddl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 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!      Z"ereejF                  eejH                     f   Z% G d d      Z& G d dejN                  ejP                        Z(dejR                  ddfdZ*y)zStandalone Authenticator.    N)Any)Callable)DefaultDict)Dict)Iterable)List)Mapping)Set)Tuple)Type)TYPE_CHECKING)crypto)
challenges)
standalone)achallenges)errors)
interfaces)util)commonc            	          e Zd ZdZdeeeej                  ej                  f   f   de
ej                  j                     ddfdZ	 ddedeej$                     d	edej(                  fd
ZdeddfdZdeeej(                  f   fdZy)ServerManagera  Standalone servers manager.

    Manager for `ACMEServer` and `ACMETLSServer` instances.

    `certs` and `http_01_resources` correspond to
    `acme.crypto_util.SSLSocket.certs` and
    `acme.crypto_util.SSLSocket.http_01_resources` respectively. All
    created servers share the same certificates and resources, so if
    you're running both TLS and non-TLS instances, HTTP01 handlers
    will serve the same URLs!

    certshttp_01_resourcesreturnNc                 .    i | _         || _        || _        y N)
_instancesr   r   )selfr   r   s      F/usr/lib/python3/dist-packages/certbot/_internal/plugins/standalone.py__init__zServerManager.__init__2   s     RT
!2    portchallenge_type
listenaddrc                    |t         j                  k(  sJ || j                  v r| j                  |   S ||f}	 t        j                  || j
                        }|j                          |j                         d   d   }|| j                  |<   |S # t        j                  $ r}t        j                  ||      d}~ww xY w)a  Run ACME server on specified ``port``.

        This method is idempotent, i.e. all calls with the same pair of
        ``(port, challenge_type)`` will reuse the same server.

        :param int port: Port to run the server on.
        :param challenge_type: Subclass of `acme.challenges.Challenge`,
            currently only `acme.challenge.HTTP01`.
        :param str listenaddr: (optional) The address to listen on. Defaults to all addrs.

        :returns: DualNetworkedServers instance.
        :rtype: ACMEServerMixin

        Nr      )r   HTTP01r   acme_standaloneHTTP01DualNetworkedServersr   socketerrorr   StandaloneBindErrorserve_forevergetsocknames)r   r"   r#   r$   addressserversr+   	real_ports           r   runzServerManager.run9   s      !2!22224??"??4((t$	:%@@//1G
 	 ((*1-a0	%,	" || 	:,,UD99	:s    B B="B88B=c                     | j                   |   }|j                         D ]  }t        j                  dg|dd    |j	                          | j                   |= y)zWStop ACME server running on the specified ``port``.

        :param int port:

        zStopping server at %s:%d...N   )r   r.   loggerdebugshutdown_and_server_close)r   r"   instancesocknames       r   stopzServerManager.stop\   sa     ??4( --/ 	(HLL6 ("2A,(	( 	**,OOD!r!   c                 6    | j                   j                         S )zReturn all running instances.

        Once the server is stopped using `stop`, it will not be
        returned.

        :returns: Mapping from ``port`` to ``servers``.
        :rtype: tuple

        )r   copyr   s    r   runningzServerManager.runningi   s     ##%%r!   ) )__name__
__module____qualname____doc__r	   bytesr   r   PKeyX509r
   r(   HTTP01RequestHandlerHTTP01Resourcer    intr   r   	Challengestrr)   r2   r:   r   r>    r!   r   r   r   %   s    3geU6;;3K-L&LM 3$'(L(L([([$\33 !! !T*2F2F-G !!%4%O%O!F" " "
&c?#M#MMN 
&r!   r   c                       e Zd ZdZdZdededdf fdZeded	   ddfd
       Z	de
fdZddZde
deeej                         fdZdeej&                     deej*                     fdZdej&                  dej*                  fdZdej&                  dej*                  fdZdej&                  deej6                  ej*                  f   fdZdeej&                     ddfdZdeej&                     de
fdZ xZS )Authenticatora  Standalone Authenticator.

    This authenticator creates its own ephemeral TCP listener on the
    necessary port in order to respond to incoming http-01
    challenges from the certificate authority. Therefore, it does not
    rely on any existing server program.
    zRuns an HTTP server locally which serves the necessary validation files under the /.well-known/acme-challenge/ request path. Suitable if there is no HTTP server already running. HTTP challenge only (wildcards not supported).argskwargsr   Nc                     t        |   |i | t        j                  t              | _        i | _        t	               | _        t        | j                  | j                        | _	        y r   )
superr    collectionsdefaultdictsetservedr   r   r   r0   )r   rO   rP   	__class__s      r   r    zAuthenticator.__init__   sU    $)&)"-"9"9#"> GI
[^[`$TZZ1G1GHr!   add).Nc                      y r   rL   )clsrX   s     r   add_parser_argumentsz"Authenticator.add_parser_arguments   s    r!   c                      y)NzThis authenticator creates its own ephemeral TCP listener on the necessary port in order to respond to incoming http-01 challenges from the certificate authority. Therefore, it does not rely on any existing server program.rL   r=   s    r   	more_infozAuthenticator.more_info   s    Br!   c                      y r   rL   r=   s    r   preparezAuthenticator.prepare   s    r!   domainc                 $    t         j                  gS r   )r   r'   )r   r`   s     r   get_chall_prefzAuthenticator.get_chall_pref   s    !!""r!   achallsc                 J    |D cg c]  }| j                  |       c}S c c}w r   )_try_perform_single)r   rc   achalls      r   performzAuthenticator.perform   s!    ?FGV((0GGGs    rf   c                     	 	 | j                  |      S # t        j                  $ r}t        |       Y d }~nd }~ww xY w>r   )_perform_singler   r,   _handle_perform_error)r   rf   r+   s      r   re   z!Authenticator._try_perform_single   sA    -++F33-- -%e,,- s    <7<c                 j    | j                  |      \  }}| j                  |   j                  |       |S r   )_perform_http_01rV   rX   )r   rf   r0   responses       r   ri   zAuthenticator._perform_single   s3     11&9G  (r!   c                 n   | j                   j                  }| j                   j                  }| j                  j	                  |t
        j                  |      }|j                         \  }}t        j                  j                  |j                  ||      }| j                  j                  |       ||fS )N)r$   )challrm   
validation)confighttp01_porthttp01_addressr0   r2   r   r'   response_and_validationr(   rG   rH   ro   r   rX   )r   rf   r"   addrr0   rm   rp   resources           r   rl   zAuthenticator._perform_http_01   s     {{&&{{)),,""4):):t"L%==?*"77FF,,j G J""8,  r!   c                 8   | j                   j                         D ]"  \  }}|D ]  }||v s|j                  |        $ | j                  j	                         j                         D ]0  \  }}| j                   |   r| j                  j                  |       2 y r   )rV   itemsremover0   r>   r:   )r   rc   unused_serversserver_achallsrf   r"   r0   s          r   cleanupzAuthenticator.cleanup   s    .2kk.?.?.A 	2*NN! 2^+"))&12	2 "\\11399; 	(MD';;w'!!$'	(r!   failed_achallsc                     | j                   j                  | j                   j                  }}|r| d| nd| }d| dS )N:zport zThe Certificate Authority failed to download the challenge files from the temporary standalone webserver started by Certbot on zt. Ensure that the listed domains point to this machine and that it can accept inbound connections from the internet.)rq   rr   rs   )r   r}   r"   ru   	neat_addrs        r   	auth_hintzAuthenticator.auth_hint   sW    [[,,dkk.H.Hd(,tfAdV$E$.	LLU; W@@ 	Ar!   )r   N) r@   rA   rB   rC   descriptionr   r    classmethodr   r[   rK   r]   r_   r   r   r   rJ   rb   r   AnnotatedChallenger   ChallengeResponserg   re   ri   r   r(   r)   rl   r|   r   __classcell__)rW   s   @r   rN   rN   v   sp   ;KIc IS IT I x	': t  C3 C#S #Xd:;O;O6P-Q #Hx(F(FG H*667H-$/$B$B-GQGcGc- + > >CMC_C_
!{'E'E 
!#O$N$N$.$@$@%A B
!(x(F(FG (D (A[-K-K(L AQT Ar!   rN   r+   r   c                    | j                   j                  t        j                  k(  r.t        j                  dj                  | j                              | j                   j                  t        j                  k(  rLdj                  | j                        }t        j                  |ddd      }|st        j                  |      y | )NzCould not bind TCP port {0} because you don't have the appropriate permissions (for example, you aren't running this program as root).zCould not bind TCP port {0} because it is already in use by another process on this system (such as a web server). Please stop the program in question and then try again.RetryCancelF)default)
socket_errorerrnoEACCESr   PluginErrorformatr"   
EADDRINUSEdisplay_utilyesno)r+   msgshould_retrys      r   rj   rj      s    5<</   VEJJ'	) 	)
 5#3#33 %fUZZ0	 	
 $))#w%P$$S))  r!   )+rC   rS   r   loggingr*   typingr   r   r   r   r   r   r	   r
   r   r   r   OpenSSLr   acmer   r   r(   certbotr   r   r   certbot.displayr   r   certbot.pluginsr   	getLoggerr@   r5   BaseDualNetworkedServersr   
ServedTyper   PluginrN   r,   rj   rL   r!   r   <module>r      s                       .    0 "			8	$00K**+	,JN& N&bZAFMM:#;#; ZAz!;!;  r!   