a }|gi$@s^dZddlZddlZddlZddlmZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z dd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZee Z!er$eej"e ej#fZ$GdddZ%Gdddej&ej'Z'ej(ddddZ)dS)zStandalone Authenticator.N)Any)Callable) DefaultDict)Dict)Iterable)List)Mapping)Set)Tuple)Type) TYPE_CHECKING)crypto) challenges) standalone) achallenges)errors) interfaces)util)commonc@seZdZdZeeeejej ffe e j j ddddZdeeejee jddd Zedd d d Zeee jfd ddZdS) ServerManageraStandalone 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! N)certshttp_01_resourcesreturncCsi|_||_||_dSN) _instancesrr)selfrrrH/usr/lib/python3.9/site-packages/certbot/_internal/plugins/standalone.py__init__1szServerManager.__init__)portchallenge_type listenaddrrc Cs|tjksJ||jvr"|j|S||f}zt||j}Wn0tyl}zt||WYd}~n d}~00| | dd}||j|<|S)aRun 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) rHTTP01racme_standaloneHTTP01DualNetworkedServersrOSErrorrStandaloneBindErrorZ serve_forever getsocknames)rr r!r"addressserverserrorZ real_portrrrrun8s  " zServerManager.run)r rcCsF|j|}|D]}tjdg|ddRq||j|=dS)zWStop ACME server running on the specified ``port``. :param int port: zStopping server at %s:%d...N)rr)loggerdebugZshutdown_and_server_close)rr instanceZsocknamerrrstop[s   zServerManager.stoprcCs |jS)zReturn all running instances. Once the server is stopped using `stop`, it will not be returned. :returns: Mapping from ``port`` to ``servers``. :rtype: tuple )rcopyrrrrrunninghs zServerManager.running)r)__name__ __module__ __qualname____doc__rbytesr r ZPKeyZX509r r%HTTP01RequestHandlerHTTP01Resourcerintr r Challengestrr&r-r2rr6rrrrr$s     # rcs eZdZdZdZeeddfdd Zeedddd d Z e d d d Z dd ddZ e e eejdddZe ejeejdddZejejdddZejejdddZejeejejfdddZe ejddddZeeje dd d!ZZS)" AuthenticatoraStandalone 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).N)argskwargsrcs@tj|i|tt|_i|_t|_t|j|j|_ dSr) superr collections defaultdictsetservedrrrr+)rrBrC __class__rrrs  zAuthenticator.__init__).N)addrcCsdSrr)clsrKrrradd_parser_argumentssz"Authenticator.add_parser_argumentsr3cCsdS)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.rr5rrr more_infoszAuthenticator.more_infocCsdSrrr5rrrprepareszAuthenticator.prepare)domainrcCstjgSr)rr$)rrPrrrget_chall_prefszAuthenticator.get_chall_pref)achallsrcsfdd|DS)Ncsg|]}|qSr)_try_perform_single).0achallr5rr z)Authenticator.perform..r)rrRrr5rperformszAuthenticator.perform)rUrc CsBz ||WStjy:}zt|WYd}~qd}~00qdSr)_perform_singlerr(_handle_perform_error)rrUr,rrrrSs z!Authenticator._try_perform_singlecCs"||\}}|j|||Sr)_perform_http_01rHrK)rrUr+responserrrrYszAuthenticator._perform_singlecCsX|jj}|jj}|jj|tj|d}|\}}tj j |j ||d}|j |||fS)N)r")challr\ validation)config http01_porthttp01_addressr+r-rr$Zresponse_and_validationr%r<r=r]rrK)rrUr addrr+r\r^resourcerrrr[s  zAuthenticator._perform_http_01cCsb|jD]$\}}|D]}||vr||qq |jD]\}}|j|s>|j|q>dSr)rHitemsremover+r6r2)rrRZunused_serversZserver_achallsrUr r+rrrcleanups zAuthenticator.cleanup)failed_achallsrcCs:|jj|jj}}|r$|d|nd|}d|dS)N:zport zThe 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.)r_r`ra)rrgr rbZ neat_addrrrr auth_hints zAuthenticator.auth_hint) r7r8r9r: descriptionrr classmethodrrMr@rNrOrr rr?rQrAnnotatedChallengerZChallengeResponserXrSrYr r%r&r[rfri __classcell__rrrIrrAus,     rA)r,rcCsd|jjtjkr td|j|jjtjkr\d|j}tj |dddd}|s`t|n|dS)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.ZRetryZCancelF)default) Z socket_errorerrnoZEACCESrZ PluginErrorformatr Z EADDRINUSE display_utilZyesno)r,msgZ should_retryrrrrZs rZ)*r:rEroZloggingtypingrrrrrrrr r r r ZOpenSSLr Zacmerrr%ZcertbotrrrZcertbot.displayrrqZcertbot.pluginsrZ getLoggerr7r/ZBaseDualNetworkedServersrlZ ServedTyperZPluginrAr(rZrrrrs@                    Q]