
    M/eJ                     *   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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 G d dej0                        Z G d d      ZdeddfdZdedefdZy)z$Certbot user-supplied configuration.    N)Any)Dict)List)Optional)parse)errors)util)	constants)misc)osc                       e Zd ZdZ ej
                         Z	  ej
                         Z	  ej
                         Z	  ej
                         Z		  ej
                         Z
y)ArgumentSourcez;Enum for describing where a configuration argument was set.N)__name__
__module____qualname____doc__enumautoCOMMAND_LINECONFIG_FILEDEFAULTENV_VARRUNTIME     7/usr/lib/python3/dist-packages/certbot/configuration.pyr   r      sU    E499;L4$))+K6diikGNdiikG;diikG0r   r   c                      e Zd ZdZdej
                  ddfdZdeee	f   ddfdZ
dedefd	Zdeeef   fd
ZdeddfdZedeeee	f      fd       ZdedefdZdededdfdZedefd       Zej,                  deddfd       Zedee   fd       Zej,                  deddfd       Zedefd       Zej,                  deddfd       Zedefd       Zej,                  deddfd       Zedefd       Zej,                  deddfd       Zedefd        Zedefd!       Zedefd"       Zedefd#       Zedefd$       Z edefd%       Z!edefd&       Z"edefd'       Z#edefd(       Z$edefd)       Z%edefd*       Z&edefd+       Z'edefd,       Z(ede)e   fd-       Z*edefd.       Z+edefd/       Z,edefd0       Z-edee   fd1       Z.edefd2       Z/d3edefd4Z0edefd5       Z1edefd6       Z2edefd7       Z3edefd8       Z4edefd9       Z5edefd:       Z6edefd;       Z7edefd<       Z8edefd=       Z9d>edd fd?Z:y)@NamespaceConfiga4  Configuration wrapper around :class:`argparse.Namespace`.

    Please note that the following attributes are dynamically resolved using
    :attr:`~certbot.configuration.NamespaceConfig.work_dir` and relative
    paths defined in :py:mod:`certbot._internal.constants`:

      - `accounts_dir`
      - `csr_dir`
      - `in_progress_dir`
      - `key_dir`
      - `temp_checkpoint_dir`

    And the following paths are dynamically resolved using
    :attr:`~certbot.configuration.NamespaceConfig.config_dir` and relative
    paths defined in :py:mod:`certbot._internal.constants`:

      - `default_archive_dir`
      - `live_dir`
      - `renewal_configs_dir`

    :ivar namespace: Namespace typically produced by
        :meth:`argparse.ArgumentParser.parse_args`.
    :type namespace: :class:`argparse.Namespace`

    	namespacereturnNc                 4   |  t         j                  | d|       t         j                  | dd        t         j                  | di        t        j                  j	                  | j
                  j                        | j
                  _        t        j                  j	                  | j
                  j                        | j
                  _        t        j                  j	                  | j
                  j                        | j
                  _        t        |        y )Nr   _argument_sources_previously_accessed_mutables)
object__setattr__r   pathabspathr   
config_dirwork_dirlogs_dir_check_config_sanity)selfr   s     r   __init__zNamespaceConfig.__init__A   s    4i84!4d;4!@"E$&GGOODNN4M4M$N!"$''//$..2I2I"J"$''//$..2I2I"J 	T"r   argument_sourcesc                 2    t         j                  | d|       y)al  
        Associate the NamespaceConfig with a dictionary describing where each of
        its arguments came from, e.g. `{ 'email': ArgumentSource.CONFIG_FILE }`.
        This is necessary for making runtime evaluations on whether an argument
        was specified by the user or not (see `set_by_user`).

        For an example of how to build such a dictionary, see
        `certbot._internal.cli.helpful.HelpfulArgumentParser._build_sources_dict`

        :ivar argument_sources: dictionary of argument names to their :class:`ArgumentSource`
        :type argument_sources: :class:`Dict[str, ArgumentSource]`
        r"   N)r$   r%   )r,   r.   s     r   set_argument_sourcesz$NamespaceConfig.set_argument_sourcesO   s     	4!46FGr   varc           	         ddl m} ddl m} ddlm} | j
                  t        d      ||v ry|dv r&|j                  |       \  }}|d	k(  r|duS |d
k(  r|duS || j
                  v rB| j
                  |   t        j                  k7  r"t        j                  d|t        | |             y|j                  |g       D ]<  }| j                  |      st        j                  d||j                  |g               y y)ad  
        Return True if a particular config variable has been set by the user
        (via CLI or config file) including if the user explicitly set it to the
        default, or if it was dynamically set at runtime.  Returns False if the
        variable was assigned a default value.

        Raises an exception if `argument_sources` is not set.
        r   )DEPRECATED_OPTIONS)VAR_MODIFIERS)	selectionNzoNamespaceConfig.set_by_user called without an ArgumentSources dict. See NamespaceConfig.set_argument_sources().F)authenticator	installerr6   r7   zVar %s=%s (set by user).T)#certbot._internal.cli.cli_constantsr3   r4   certbot._internal.pluginsr5   r.   RuntimeErrorcli_plugin_requestsr   r   loggerdebuggetattrgetset_by_user)r,   r1   r3   r4   r5   authinstmodifiers           r   r@   zNamespaceConfig.set_by_usera   s    	KE7  (>? ? $$00"66t<JD$o%4''k!4''$'''D,A,A#,F.J`J`,`LL3S'$:LM%))#r2 	H)7**335		 r   c                 ,    t        | j                        S )zQ
        Returns a dictionary mapping all argument names to their values
        )varsr   r,   s    r   to_dictzNamespaceConfig.to_dict   s     DNN##r   namec                     | j                   :t        j                  | j                   |<   || j                  v r| j                  |= yyy)a)  
        If an argument_sources dict was set, overwrites an argument's source to
        be ArgumentSource.RUNTIME. Used when certbot sets an argument's values
        at runtime. This also clears the modified value from
        _previously_accessed_mutables since it is no longer needed.
        N)r"   r   r   r#   )r,   rH   s     r   _mark_runtime_overridez&NamespaceConfig._mark_runtime_override   sL     !!-+9+A+AD""4(t99966t< : .r   c                     | j                   j                         j                         D ]2  \  }}t        | j                  |      }||k7  s"| j                  |       4 | j                  S )zPReturns _argument_sources after handling any changes to accessed mutable values.)r#   copyitemsr>   r   rJ   r"   )r,   rH   
prev_valuecurrent_values       r   r.   z NamespaceConfig.argument_sources   sd     !% B B G G I O O Q 	2D*#DNND9M
*++D1	2 %%%r   c                     | j                   }t        | j                  |      }|U||vs||   t        j                  k7  r;|| j
                  vr-t        |      s"t        j                  |      | j
                  |<   |S N)	r.   r>   r   r   r   r#   _is_immutablerL   deepcopy)r,   rH   arg_sourcesvalues       r   __getattr__zNamespaceConfig.__getattr__   su    ++-" ;&+d*;~?U?U*U
 tAAA-X]J^?C}}U?SD66t<r   rU   c                 T    | j                  |       t        | j                  ||       y rQ   )rJ   setattrr   )r,   rH   rU   s      r   r%   zNamespaceConfig.__setattr__   s     ##D)e,r   c                 .    | j                   j                  S )zACME Directory Resource URI.)r   serverrF   s    r   rZ   zNamespaceConfig.server   s     ~~$$$r   server_c                 H    | j                  d       || j                  _        y )NrZ   )rJ   r   rZ   )r,   r[   s     r   rZ   zNamespaceConfig.server   s    ##H- 'r   c                 .    | j                   j                  S )zEmail used for registration and recovery contact.

        Use comma to register multiple emails,
        ex: u1@example.com,u2@example.com. (default: Ask).
        )r   emailrF   s    r   r^   zNamespaceConfig.email   s     ~~###r   mailc                 H    | j                  d       || j                  _        y )Nr^   )rJ   r   r^   )r,   r_   s     r   r^   zNamespaceConfig.email   s    ##G,#r   c                 .    | j                   j                  S )zSize of the RSA key.)r   rsa_key_sizerF   s    r   rb   zNamespaceConfig.rsa_key_size   s     ~~***r   ksizec                 H    | j                  d       || j                  _        y)zSet the rsa_key_size propertyrb   N)rJ   r   rb   )r,   rc   s     r   rb   zNamespaceConfig.rsa_key_size   s     	##N3&+#r   c                 .    | j                   j                  S )z`The SECG elliptic curve name to use.

        Please see RFC 8446 for supported values.
        )r   elliptic_curverF   s    r   rf   zNamespaceConfig.elliptic_curve   s     ~~,,,r   ecurvec                 H    | j                  d       || j                  _        y)zSet the elliptic_curve propertyrf   N)rJ   r   rf   )r,   rg   s     r   rf   zNamespaceConfig.elliptic_curve   s     	##$45(.%r   c                 .    | j                   j                  S )zhType of generated private key.

        Only *ONE* per invocation can be provided at this time.
        )r   key_typerF   s    r   rj   zNamespaceConfig.key_type   s     ~~&&&r   ktypec                 H    | j                  d       || j                  _        y)zSet the key_type propertyrj   N)rJ   r   rj   )r,   rk   s     r   rj   zNamespaceConfig.key_type   s     	##J/"'r   c                 .    | j                   j                  S )zAdds the OCSP Must-Staple extension to the certificate.

        Autoconfigures OCSP Stapling for supported setups
        (Apache version >= 2.3.3 ).
        )r   must_staplerF   s    r   rn   zNamespaceConfig.must_staple       ~~)))r   c                 .    | j                   j                  S )zConfiguration directory.)r   r(   rF   s    r   r(   zNamespaceConfig.config_dir  s     ~~(((r   c                 .    | j                   j                  S )zWorking directory.)r   r)   rF   s    r   r)   zNamespaceConfig.work_dir  s     ~~&&&r   c                 8    | j                  | j                        S )z2Directory where all account information is stored.)accounts_dir_for_server_pathserver_pathrF   s    r   accounts_dirzNamespaceConfig.accounts_dir  s     001A1ABBr   c                     t         j                  j                  | j                  j                  t
        j                        S )z Configuration backups directory.)r   r&   joinr   r)   r
   
BACKUP_DIRrF   s    r   
backup_dirzNamespaceConfig.backup_dir  s)     ww||DNN33Y5I5IJJr   c                     t        j                  dt               t        j                  j                  | j                  j                  t        j                        S )zBDirectory where new Certificate Signing Requests (CSRs) are saved.z[NamespaceConfig.csr_dir is deprecated and will be removed in an upcoming release of Certbot)
warningswarnDeprecationWarningr   r&   rw   r   r(   r
   CSR_DIRrF   s    r   csr_dirzNamespaceConfig.csr_dir   >     	 +,>	@ww||DNN55y7H7HIIr   c                     t         j                  j                  | j                  j                  t
        j                        S )z:Directory used before a permanent checkpoint is finalized.)r   r&   rw   r   r)   r
   IN_PROGRESS_DIRrF   s    r   in_progress_dirzNamespaceConfig.in_progress_dir'  s)     ww||DNN33Y5N5NOOr   c                     t        j                  dt               t        j                  j                  | j                  j                  t        j                        S )zKeys storage.z[NamespaceConfig.key_dir is deprecated and will be removed in an upcoming release of Certbot)
r{   r|   r}   r   r&   rw   r   r(   r
   KEY_DIRrF   s    r   key_dirzNamespaceConfig.key_dir,  r   r   c                     t         j                  j                  | j                  j                  t
        j                        S )zTemporary checkpoint directory.)r   r&   rw   r   r)   r
   TEMP_CHECKPOINT_DIRrF   s    r   temp_checkpoint_dirz#NamespaceConfig.temp_checkpoint_dir3  s0     ww||NN##Y%B%BD 	Dr   c                 .    | j                   j                  S )zDisable verification of the ACME server's certificate.

        The root certificates trusted by Certbot can be overriden by setting the
        REQUESTS_CA_BUNDLE environment variable.
        )r   no_verify_sslrF   s    r   r   zNamespaceConfig.no_verify_ssl9  s     ~~+++r   c                 .    | j                   j                  S )zPort used in the http-01 challenge.

        This only affects the port Certbot listens on.
        A conforming ACME server will still attempt to connect on port 80.
        )r   http01_portrF   s    r   r   zNamespaceConfig.http01_portB  ro   r   c                 .    | j                   j                  S )z;The address the server listens to during http-01 challenge.)r   http01_addressrF   s    r   r   zNamespaceConfig.http01_addressK  s     ~~,,,r   c                 .    | j                   j                  S )zPort used to serve HTTPS.

        This affects which port Nginx will listen on after a LE certificate
        is installed.
        )r   
https_portrF   s    r   r   zNamespaceConfig.https_portP  s     ~~(((r   c                 .    | j                   j                  S )zuList of user specified preferred challenges.

        Sorted with the most preferred challenge listed first.
        )r   pref_challsrF   s    r   r   zNamespaceConfig.pref_challsY  s     ~~)))r   c                 .    | j                   j                  S )a  Allow only a subset of names to be authorized to perform validations.

        When performing domain validation, do not consider it a failure
        if authorizations can not be obtained for a strict subset of
        the requested domains. This may be useful for allowing renewals for
        multiple domains to succeed even if some domains no longer point
        at this system.
        )r   allow_subset_of_namesrF   s    r   r   z%NamespaceConfig.allow_subset_of_namesa  s     ~~333r   c                 .    | j                   j                  S )zEnable strict permissions checks.

        Require that all configuration files are owned by the current
        user; only needed if your config is somewhere unsafe like /tmp/.
        )r   strict_permissionsrF   s    r   r   z"NamespaceConfig.strict_permissionsm  s     ~~000r   c                 .    | j                   j                  S )zDisable renewal updates.

        If updates provided by installer enhancements when Certbot is being run
        with "renew" verb should be disabled.
        )r   disable_renew_updatesrF   s    r   r   z%NamespaceConfig.disable_renew_updatesv  s     ~~333r   c                 .    | j                   j                  S )zSet the preferred certificate chain.

        If the CA offers multiple certificate chains, prefer the chain whose
        topmost certificate was issued from this Subject Common Name.
        If no match, the default offered chain will be used.
        )r   preferred_chainrF   s    r   r   zNamespaceConfig.preferred_chain  s     ~~---r   c                     t        j                  | j                  j                        }|j                  |j
                  z   j                  dt        j
                  j                        S )zFile path based on ``server``./)	r   urlparser   rZ   netlocr&   replacer   sep)r,   parseds     r   rt   zNamespaceConfig.server_path  sD      5 56+44S"''++FFr   rt   c                     t        j                  |      }t        j                  j	                  | j
                  j                  t        j                  |      S )z/Path to accounts directory based on server_path)	r   .underscores_for_unsupported_characters_in_pathr   r&   rw   r   r(   r
   ACCOUNTS_DIR)r,   rt   s     r   rs   z,NamespaceConfig.accounts_dir_for_server_path  s@    II+Vww||NN%%y'='={L 	Lr   c                     t         j                  j                  | j                  j                  t
        j                        S rQ   )r   r&   rw   r   r(   r
   ARCHIVE_DIRrF   s    r   default_archive_dirz#NamespaceConfig.default_archive_dir  s'    ww||DNN55y7L7LMMr   c                     t         j                  j                  | j                  j                  t
        j                        S rQ   )r   r&   rw   r   r(   r
   LIVE_DIRrF   s    r   live_dirzNamespaceConfig.live_dir  s'    ww||DNN55y7I7IJJr   c                     t         j                  j                  | j                  j                  t
        j                        S rQ   )r   r&   rw   r   r(   r
   RENEWAL_CONFIGS_DIRrF   s    r   renewal_configs_dirz#NamespaceConfig.renewal_configs_dir  s.    ww||NN%%y'D'DF 	Fr   c                     t         j                  j                  | j                  j                  t
        j                        S )z>Path to directory with hooks to run with the renew subcommand.)r   r&   rw   r   r(   r
   RENEWAL_HOOKS_DIRrF   s    r   renewal_hooks_dirz!NamespaceConfig.renewal_hooks_dir  s.     ww||DNN55%779 	9r   c                 r    t         j                  j                  | j                  t        j
                        S )z8Path to the pre-hook directory for the renew subcommand.)r   r&   rw   r   r
   RENEWAL_PRE_HOOKS_DIRrF   s    r   renewal_pre_hooks_dirz%NamespaceConfig.renewal_pre_hooks_dir  s*     ww||D22%;;= 	=r   c                 r    t         j                  j                  | j                  t        j
                        S )z;Path to the deploy-hook directory for the renew subcommand.)r   r&   rw   r   r
   RENEWAL_DEPLOY_HOOKS_DIRrF   s    r   renewal_deploy_hooks_dirz(NamespaceConfig.renewal_deploy_hooks_dir  s,     ww||D22%>>@ 	@r   c                 r    t         j                  j                  | j                  t        j
                        S )z9Path to the post-hook directory for the renew subcommand.)r   r&   rw   r   r
   RENEWAL_POST_HOOKS_DIRrF   s    r   renewal_post_hooks_dirz&NamespaceConfig.renewal_post_hooks_dir  s*     ww||D22%<<> 	>r   c                 .    | j                   j                  S )zuThis option specifies how long (in seconds) Certbot will wait
        for the server to issue a certificate.
        )r   issuance_timeoutrF   s    r   r   z NamespaceConfig.issuance_timeout  s    
 ~~...r   c                 .    | j                   j                  S )zThis option specifies whether Certbot should generate a new private
        key when replacing a certificate, even if reuse_key is set.
        )r   new_keyrF   s    r   r   zNamespaceConfig.new_key  s    
 ~~%%%r   _memoc                 6   t        j                  | j                        } t        |       |      }t        j                  |dt        j                  | j                               t        j                  |dt        j                  | j                               |S )Nr"   r#   )rL   rS   r   typer$   r%   r.   r#   )r,   r   new_ns
new_configs       r   __deepcopy__zNamespaceConfig.__deepcopy__  sr    t~~.T$Z'
:':DMM$J_J_<`a:'F==)K)KL	Nr   );r   r   r   r   argparse	Namespacer-   r   strr   r0   boolr@   r   rG   rJ   propertyr   r.   rV   r%   rZ   setterr^   intrb   rf   rj   rn   r(   r)   ru   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rt   rs   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   &   sg   4#("4"4 # #HT#~:M5N HSW H$(s (t (T$c3h $
=3 
=4 
= &(4^0C+D"E & &&  - -C -D - % % % ]](c (d ( ( $x} $ $ \\$# $$ $ $ +c + + ,# ,$ , ,
 - - - /S /T / /
 '# ' ' __(c (d ( (
 *T * * )C ) ) '# ' ' Cc C C KC K K J J J P P P J J J DS D D
 ,t , , *S * * - - - )C ) ) *T#Y * * 	4t 	4 	4 1D 1 1 4t 4 4 .# . . GS G G
L L L NS N N K# K K FS F F 93 9 9
 =s = =
 @# @ @
 > > >
 /# / / & & &# *; r   r   configr    c                     | j                   | j                  k(  r.t        j                  dj	                  | j                              | j
                  j                  1| j
                  j                  D ]  }t        j                  |        yy)zValidate command line options and display error message if
    requirements are not met.

    :param config: NamespaceConfig instance holding user configuration
    :type args: :class:`certbot.configuration.NamespaceConfig`

    z;Trying to run http-01 and https-port on the same port ({0})N)	r   r   r   ConfigurationErrorformatr   domainsr	   enforce_domain_sanity)r   domains     r   r+   r+     s     V...''%%+VF,=,=%>@ 	@
 +&&.. 	/F&&v.	/ ,r   rU   c                     t        | t              rt        d | D              S t        t        t
        t        t        t        t        fD ]  }t        | |      s y | du S )zIs value of an immutable type?c              3   2   K   | ]  }t        |        y wrQ   )rR   ).0subvalues     r   	<genexpr>z _is_immutable.<locals>.<genexpr>  s     Ax=*As   TN)

isinstancetupleallr   floatcomplexr   bytesr   	frozenset)rU   immutable_types     r   rR   rR     sT    %A5AAAwUD)M e^, D=r   )r   r   rL   r   loggingtypingr   r   r   r   urllibr   r{   certbotr   r	   certbot._internalr
   certbot.compatr   r   	getLoggerr   r<   Enumr   r   r+   r   rR   r   r   r   <module>r      s    *             '   
		8	$1TYY 1l l^/ /T /*	 	 	r   