ó
Á£ô_c           @   s¦  d  Z  d d l Z d d l Z d d l Z d d l m 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 e j e ƒ Z e r@e e j e e j f Z n  d e  f d „  ƒ  YZ! e	 j" j# e j$ ƒ e	 j" j% e j& ƒ d e j' f d „  ƒ  Yƒ ƒ Z( d „  Z) d S(   s   Standalone Authenticator.iÿÿÿÿN(   t   errno(   t
   challenges(   t
   standalone(   t   DefaultDict(   t   Dict(   t   Set(   t   Tuple(   t   TYPE_CHECKING(   t   achallenges(   t   errors(   t
   interfaces(   t   commont   ServerManagerc           B   s5   e  Z d  Z d „  Z d d „ Z d „  Z d „  Z RS(   s§  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!

    c         C   s   i  |  _  | |  _ | |  _ d  S(   N(   t
   _instancest   certst   http_01_resources(   t   selfR   R   (    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyt   __init__-   s    		t    c         C   s®   | t  j k s t ‚ | |  j k r/ |  j | S| | f } y t j | |  j ƒ } Wn( t j k
 r~ } t	 j
 | | ƒ ‚ n X| j ƒ  | j ƒ  d d } | |  j | <| S(   s  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

        i    i   (   R   t   HTTP01t   AssertionErrorR   t   acme_standalonet   HTTP01DualNetworkedServersR   t   sockett   errorR	   t   StandaloneBindErrort   serve_forevert   getsocknames(   R   t   portt   challenge_typet
   listenaddrt   addresst   serversR   t	   real_port(    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyt   run2   s    
c         C   sP   |  j  | } x( | j ƒ  D] } t j d | d  Œ q W| j ƒ  |  j  | =d S(   sW   Stop ACME server running on the specified ``port``.

        :param int port:

        s   Stopping server at %s:%d...i   N(   R   R   t   loggert   debugt   shutdown_and_server_close(   R   R   t   instancet   sockname(    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyt   stopT   s    	
c         C   s   |  j  j ƒ  S(   sÉ   Return all running instances.

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

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

        (   R   t   copy(   R   (    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyt   runninga   s    
(   t   __name__t
   __module__t   __doc__R   R"   R(   R*   (    (    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyR       s
   	"	t   Authenticatorc           B   st   e  Z d  Z d Z d „  Z e d „  ƒ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z RS(   s  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.
    s   Spin up a temporary webserverc         O   s\   t  t |  ƒ j | | Ž  t j t ƒ |  _ i  |  _ t ƒ  |  _ t	 |  j |  j ƒ |  _
 d  S(   N(   t   superR.   R   t   collectionst   defaultdictt   sett   servedR   R   R   R    (   R   t   argst   kwargs(    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyR   {   s
    	c         C   s   d  S(   N(    (   t   clst   add(    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyt   add_parser_argumentsŠ   s    c         C   s   d S(   NsÞ   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.(    (   R   (    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyt	   more_infoŽ   s    c         C   s   d  S(   N(    (   R   (    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyt   prepare”   s    c         C   s
   t  j g S(   N(   R   R   (   R   t   domain(    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyt   get_chall_pref—   s    c         C   s    g  | D] } |  j  | ƒ ^ q S(   N(   t   _try_perform_single(   R   t   achallst   achall(    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyt   perform›   s    c         C   sE   x> t  r@ y |  j | ƒ SWq t j k
 r< } t | ƒ q Xq Wd  S(   N(   t   Truet   _perform_singleR	   R   t   _handle_perform_error(   R   R?   R   (    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyR=   ž   s
    	c         C   s-   |  j  | ƒ \ } } |  j | j | ƒ | S(   N(   t   _perform_http_01R3   R7   (   R   R?   R    t   response(    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyRB   ¥   s    c         C   s†   |  j  j } |  j  j } |  j j | t j d | ƒ} | j ƒ  \ } } t j	 j
 d | j d | d | ƒ } |  j j | ƒ | | f S(   NR   t   challRE   t
   validation(   t   configt   http01_portt   http01_addressR    R"   R   R   t   response_and_validationR   t   HTTP01RequestHandlert   HTTP01ResourceRF   R   R7   (   R   R?   R   t   addrR    RE   RG   t   resource(    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyRD   ª   s    c         C   sš   xJ |  j  j ƒ  D]9 \ } } x* | D]" } | | k r# | j | ƒ q# q# Wq WxF t j |  j j ƒ  ƒ D], \ } } |  j  | sf |  j j | ƒ qf qf Wd  S(   N(   R3   t   itemst   removet   sixt	   iteritemsR    R*   R(   (   R   R>   t   unused_serverst   server_achallsR?   R   R    (    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyt   cleanup´   s    %(   R+   R,   R-   t   descriptionR   t   classmethodR8   R9   R:   R<   R@   R=   RB   RD   RV   (    (    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyR.   n   s   									
c         C   s¯   |  j  j t j k r3 t j d j |  j ƒ ƒ ‚ n  |  j  j t j k r¥ t	 j
 j t j ƒ } d j |  j ƒ } | j | d d d t ƒ} | s« t j | ƒ ‚ q« n |  ‚ d  S(   Ns†   Could not bind TCP port {0} because you don't have the appropriate permissions (for example, you aren't running this program as root).sª   Could 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.t   Retryt   Cancelt   default(   t   socket_errorR    t   socket_errorst   EACCESR	   t   PluginErrort   formatR   t
   EADDRINUSEt   zopet	   componentt
   getUtilityR
   t   IDisplayt   yesnot   False(   R   t   displayt   msgt   should_retry(    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyRC   ¿   s    (*   R-   R0   t   loggingR   R    R]   t   OpenSSLRR   t   zope.interfaceRb   t   acmeR   R   R   t   acme.magic_typingR   R   R   R   R   t   certbotR   R	   R
   t   certbot.pluginsR   t	   getLoggerR+   R#   t   BaseDualNetworkedServerst"   KeyAuthorizationAnnotatedChallenget
   ServedTypet   objectR   t	   interfacet   implementert   IAuthenticatort   providert   IPluginFactoryt   PluginR.   RC   (    (    (    sH   /usr/lib/python2.7/site-packages/certbot/_internal/plugins/standalone.pyt   <module>   s8   NO