a ‚oePRã@s¢dZddlZddlZddlZddlmZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z ddl Z dd l mZdd lmZdd lmZdd lmZddlmZddlmZddlmZdd lmZddlmZddlmZddlmZddlmZddl m!Z"e #e$¡Z%Gdd„dƒZ&ej'e j(e)ej*dœdd„Z+eej'ee ej,e e-dfdœdd„Z.eej'ej/dœd d!„Z0eej*e)d"œd#d$„Z1dS)%zACME AuthHandler.éN)ÚDict)ÚIterable)ÚList)ÚOptional)ÚSequence)ÚTuple)ÚType)ÚResponse)Ú challenges)Úclient)Úerrors)Úmessages)Ú achallenges)Ú configuration)Ú interfaces)Ú error_handler)ÚAccount)Úutil)Úcommonc@s*eZdZdZejeejee e e ddœdd„Z d#e jejeeee e jdœd d „Ze jee e fd œd d „Ze e jeeeddœdd„Zee je ejdœdd„Ze e eejdœdd„Z e ejddœdd„Z!e je"ee ejdœdd„Z#e e jddœdd„Z$e ejeje d œd!d"„Z%dS)$Ú AuthHandleraðACME Authorization Handler for a client. :ivar auth: Authenticator capable of solving :class:`~acme.challenges.Challenge` types :type auth: certbot.interfaces.Authenticator :ivar acme.client.ClientV2 acme_client: ACME client API. :ivar account: Client's Account :type account: :class:`certbot._internal.account.Account` :ivar list pref_challs: sorted user specified preferred challenges type strings with the most preferred challenge listed first N)ÚauthÚ acme_clientÚaccountÚ pref_challsÚreturncCs||_||_||_||_dS©N)rÚacmerr)Úselfrrrr©rú?C:\Program Files\Certbot\pkgs\certbot\_internal\auth_handler.pyÚ__init__0szAuthHandler.__init__Fé)ÚorderrÚconfigÚ best_effortÚ max_retriesÚ max_time_minsrc Csf|jdd…}|st d¡‚|js,t d¡‚| |¡}|s>|St |j|¡��øz0|j   |¡}|j r~t j d| ||¡dd�Wn>tjy¾} z$t d¡t d¡| ‚WYd} ~ n d} ~ 00t|ƒt|ƒksØJd ƒ‚t||ƒD]\} } |j | j| ¡qât d ¡| ||||¡d d „|Dƒ} | �s4t d ¡‚| WdƒS1�sN0Yt d¡‚dS)a Retrieve all authorizations, perform all challenges required to validate these authorizations, then poll and wait for the authorization to be checked. :param acme.messages.OrderResource orderr: must have authorizations filled in :param certbot.configuration.NamespaceConfig config: current Certbot configuration :param bool best_effort: if True, not all authorizations need to be validated (eg. renew) :param int max_retries: maximum number of retries to poll authorizations :param float max_time_mins: maximum time (in minutes) to poll authorizations :returns: list of all validated authorizations :rtype: List :raises .AuthorizationError: If unable to retrieve all authorizations NzNo authorization to handle.z9No ACME client defined, authorizations cannot be handled.z3Challenges loaded. Press continue to submit to CA. T)Zpausez!Failure in setting up challenges.z0Attempting to clean up outstanding challenges...z(Some challenges have not been performed.zWaiting for verification...cSsg|]}|jjtjkr|‘qSr©ÚbodyÚstatusr Z STATUS_VALID©Ú.0ÚauthzrrrrÚ osÿz5AuthHandler.handle_authorizations..zAll challenges have failed.z?An unexpected error occurred while handling the authorizations.)Úauthorizationsr ÚAuthorizationErrorrÚErrorÚ_choose_challengesrZ ExitHandlerÚ_cleanup_challengesrZperformZdebug_challengesÚ display_utilZ notificationÚ_debug_challenges_msgÚloggerÚcriticalÚinfoÚlenÚzipZanswer_challengeÚchallbÚ_poll_authorizations) rr"r#r$r%r&ÚauthzrsÚachallsZrespsÚerrorÚachallÚrespZauthzrs_validatedrrrÚhandle_authorizations8s@     ÿþ     $z!AuthHandler.handle_authorizations)r"rc Cs–|jst d¡‚dd„|jDƒ}g}g}|D]`}z|j |¡}| |¡Wq,tjyŠ}z&| |¡t d|j |¡WYd}~q,d}~00q,||fS)a~ Deactivate all `valid` authorizations in the order, so that they cannot be re-used in subsequent orders. :param messages.OrderResource orderr: must have authorizations filled in :returns: tuple of list of successfully deactivated authorizations, and list of unsuccessfully deactivated authorizations. :rtype: tuple z?No ACME client defined, cannot deactivate valid authorizations.cSsg|]}|jjtjkr|‘qSrr'r*rrrr-„sÿz?AuthHandler.deactivate_valid_authorizations..z)Failed to deactivate authorization %s: %sN) rr r0r.Zdeactivate_authorizationÚappendÚ acme_errorsr5ÚdebugÚuri)rr"Z to_deactivateZ deactivatedZfailedr,ÚerrrÚdeactivate_valid_authorizationsxs    (z+AuthHandler.deactivate_valid_authorizations)r<r%Údeadline_minutesr$rcs`ˆjst d¡‚dd„t|ƒDƒ}g}tj ¡tj|d�}d}t|ƒD]â} |dkr^t  |¡‡fdd„|  ¡Dƒ}|  ¡D]\} \} } | || <q|dd „|  ¡Dƒ} | D]} t   d | jjj¡q¨| | ¡d d„|  ¡Dƒ}|rîtj ¡|krô�q,t‡fd d „|  ¡Dƒƒ}t||ƒ}|tj ¡ ¡}qH|�rLˆ |¡|�sLt d¡‚|�r\t d¡‚dS)a# Poll the ACME CA server, to wait for confirmation that authorizations have their challenges all verified. The poll may occur several times, until all authorizations are checked (valid or invalid), or a maximum of retries, or the polling deadline is reached. z3No ACME client defined, cannot poll authorizations.cSsi|]\}}||df“qSrr)r+Úindexr,rrrÚ žsÿz4AuthHandler._poll_authorizations..)Zminutesércs"i|]\}\}}|ˆj |¡“qSr)rZpoll)r+rIr,Ú_©rrrrJ©ócSs"g|]\}}|jjtjkr|‘qSr)r(r)r ZSTATUS_INVALID)r+r,rLrrrr-°s ÿz4AuthHandler._poll_authorizations..zChallenge failed for domain %scSs,i|]$\}\}}|jjtjkr|||f“qSr)r(r)r ZSTATUS_PENDING)r+rIr,r@rrrrJ»sþc3s(|] \}}|durˆj |d¡VqdS)Né)rÚ retry_after)r+rLr@rMrrÚ Çsþz3AuthHandler._poll_authorizations..zSome challenges have failed.z0All authorizations were not finalized by the CA.N)rr r0Ú enumerateÚdatetimeZnowZ timedeltaÚrangeÚtimeÚsleepÚitemsÚvaluesr5r7r(Ú identifierÚvalueÚextendÚmaxÚminZ total_secondsÚ_report_failed_authzrsr/)rr<r%rHr$Zauthzrs_to_checkZauthzrs_failed_to_reportZdeadlineZ sleep_secondsrLrIr,Zauthzrs_failedZ authzr_failedrPrrMrr;“sN ÿÿ   ÿ ÿ ÿ ÿ   z AuthHandler._poll_authorizations)r<rcCsn|jst d¡‚dd„|Dƒ}g}|r0t d¡|D]4}|jj}t|| |jj j ¡ƒ}|  |  ||¡¡q4|S)zÒ Retrieve necessary and pending challenges to satisfy server. NB: Necessary and already validated challenges are not retrieved, as they can be reused for a certificate issuance. z5No ACME client defined, cannot choose the challenges.cSsg|]}|jjtjkr|‘qSrr'r*rrrr-äsÿz2AuthHandler._choose_challenges..z$Performing the following challenges:) rr r0r5r7r(r Úgen_challenge_pathÚ_get_chall_prefrYrZr[Ú_challenge_factory)rr<Zpending_authzrsr=r,Zauthzr_challengesÚpathrrrr1Ús  þzAuthHandler._choose_challenges)ÚdomainrcCsjg}|j |¡}|jr\dd„|Dƒ}|jD]}||vr*| tjj|¡q*|rR|St d¡‚|  |¡|S)z{Return list of challenge preferences. :param str domain: domain for which you are requesting preferences cSsh|] }|j’qSr)Útyp)r+ÚchallrrrÚ þrNz.AuthHandler._get_chall_pref..zENone of the preferred challenges are supported by the selected plugin) rZget_chall_prefrrBr Ú ChallengeZTYPESr r/r[)rrcZ chall_prefsZ plugin_prefZplugin_pref_typesrdrrrr`ôs  ÿ zAuthHandler._get_chall_pref)r=rcCst d¡|j |¡dS)z§Cleanup challenges. :param achalls: annotated challenges to cleanup :type achalls: `list` of :class:`certbot.achallenges.AnnotatedChallenge` zCleaning up challengesN)r5r7rZcleanup)rr=rrrr2 s zAuthHandler._cleanup_challenges)r,rbrcCsJ|jst d¡‚g}|D],}|jj|}| t||jj|jjj ƒ¡q|S)atConstruct Namedtuple Challenges :param messages.AuthorizationResource authzr: authorization :param list path: List of indices from `challenges`. :returns: achalls, list of challenge type :class:`certbot.achallenges.AnnotatedChallenge` :rtype: list :raises .errors.Error: if challenge type is not recognized úAccount is not set.) rr r0r(r rBÚchallb_to_achallÚkeyrYrZ)rr,rbr=rIr:rrrras  ÿzAuthHandler._challenge_factory)Úfailed_authzrsrcsĈjst d¡‚i}‡fdd„|Dƒ}|D]}| |jjg¡ |¡q*dˆjj›d�g}t |  ¡dd„d�D]\}}| t |ƒ¡ql|r°t ˆjt jƒr°| d ˆj |¡›d �¡t d  |¡¡d S) z.Notifies the user about failed authorizations.rhcs6g|].}|jjD] }|jrt|ˆjj|jjjƒ‘qqSr)r(r r>rirrjrYrZ)r+r,r:rMrrr-3sþz6AuthHandler._report_failed_authzrs..z= Certbot failed to authenticate some domains (authenticator: z5). The Certificate Authority reported these problems:cSs|dS)Nrr)ÚitemrrrÚ>rNz4AuthHandler._report_failed_authzrs..)rjz Hint: Ú ÚN)rr r0Ú setdefaultr>rdrBrÚnameÚsortedrWÚ_generate_failed_chall_msgÚ isinstanceÚ plugin_commonZPluginZ auth_hintr3ÚnotifyÚjoin)rrkZproblemsÚfailed_achallsr?ÚmsgrLr=rrMrr^.s   ÿÿ z"AuthHandler._report_failed_authzrs)r=r#rc Csø|jdkrðg}i}i}|D]Z}t|jtjƒrJ| |j¡d||j |j¡<t|jtj ƒr| |j¡d||  |j¡<q|r¬|  d¡|  ¡D]\}}|  d|›d|›�¡qŒ|râ|  d¡|  ¡D]\} } |  d| ›d| ›�¡qÂdd  |¡SdSd S) aEConstruct message for debug challenges prompt :param list achalls: A list of :class:`certbot.achallenges.AnnotatedChallenge`. :param certbot.configuration.NamespaceConfig config: current Certbot configuration :returns: Message containing challenge debug info :rtype: str rrnzZThe following URLs should be accessible from the internet and return the value mentioned: zURL: z Expected value: zRThe following FQDNs should return a TXT resource record with the value mentioned: zFQDN: z)Pass "-v" for more info about challenges.N)Z verbose_countrtrer ZHTTP01Z validationÚ account_keyrErcÚDNS01Zvalidation_domain_namerBrWrw) rr=r#ryZhttp01_achallsZ dns01_achallsr?rEZ key_authzZfqdnZkey_authz_hashrrrr4Hs* ÿÿ  z!AuthHandler._debug_challenges_msg)Fr!r!)&Ú__name__Ú __module__Ú __qualname__Ú__doc__rZ Authenticatorrr ZClientV2rrÚstrr r Z OrderResourcerZNamespaceConfigÚboolÚintÚfloatZAuthorizationResourcerArrGr;rrÚAnnotatedChallenger1rr rgr`r2rrar^r4rrrrr s8 ÿ ý ý @ ÿ Gÿ  ÿ ÿr)r:rzrcrcCs^|j}t d|j|¡t|tjƒr2tj|||d�St|tj ƒrLtj ||d�Stj ||d�SdS)a:Converts a ChallengeBody object to an AnnotatedChallenge. :param .ChallengeBody challb: ChallengeBody :param .JWK account_key: Authorized Account Key :param str domain: Domain of the challb :returns: Appropriate AnnotatedChallenge :rtype: :class:`certbot.achallenges.AnnotatedChallenge` z%s challenge for %s)r:rcrz)r:rcN) rer5r7rdrtr ZKeyAuthorizationChallengerZ"KeyAuthorizationAnnotatedChallengeZDNSZOther)r:rzrcrerrrrios  ÿ ri.)ÚchallbsÚ preferencesrc Cs i}d}t|ƒD]\}}|||<||7}qd}|}tdd„tt|ƒƒDƒƒ}d} |D]:} | D]} | | || jj|¡7} q\| |krŠ| }| }d} qT|sœt|ƒ‚|S)a‹Generate a plan to get authority over the identity. :param tuple challbs: A tuple of challenges (:class:`acme.messages.Challenge`) from :class:`acme.messages.AuthorizationResource` to be fulfilled by the client in order to prove possession of the identifier. :param list preferences: List of challenge preferences for domain (:class:`acme.challenges.Challenge` subclasses) :returns: list of indices from ``challenges``. :rtype: list :raises certbot.errors.AuthorizationError: If a path cannot be created that satisfies the CA given the preferences and combinations. rKNcss|] }|fVqdSrr)r+ÚirrrrQ¨rNz%gen_challenge_path..r)rRÚtuplerTr8ÚgetreÚ __class__Ú_report_no_chall_path) r…r†Z chall_costZmax_costr‡Z chall_clsZ best_comboZbest_combo_costÚ combinationsZ combo_totalZcomboZchallenge_indexrrrr_‡s. ÿÿr_)r…rcCs>d}t|ƒdkr*t|djtjƒr*|d7}t |¡t |¡S)zúLogs and return a raisable error reporting that no satisfiable chall path exists. :param challbs: challenges from the authorization that can't be satisfied :returns: An authorization error :rtype: certbot.errors.AuthorizationError zyClient with the currently selected authenticator does not support any combination of challenges that will satisfy the CA.rKrzM You may need to use an authenticator plugin that can do challenges over DNS.) r8rtrer r{r5r6r r/)r…ryrrrr‹¼s ÿ r‹)rxrcCsR|dj}|j}t |¡r |j}g}|D]}| d|j||jjf¡q(d |¡S)aCreates a user friendly error message about failed challenges. :param list failed_achalls: A list of failed :class:`certbot.achallenges.AnnotatedChallenge` with the same error type. :returns: A formatted error message for the client. :rtype: str rz( Domain: %s Type: %s Detail: %s ro) r>rdr Z is_acme_errorÚcoderBrcZdetailrw)rxr>rdryr?rrrrsÏs   ÿ rs)2rrSZloggingrUÚtypingrrrrrrrZjosepyZrequests.modelsr rr r r rCr ZcertbotrrrZcertbot._internalrZcertbot._internal.accountrZcertbot.displayrr3Zcertbot.pluginsrruZ getLoggerr|r5rZ ChallengeBodyZJWKr€r„rirgr‚r_r/r‹rsrrrrÚsH                     Qÿ ÿ 5