U
    wej                  
   @   s   d Z ddlZddl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\ZZzddlZW n& ek
r Z zeZW 5 dZ[X Y nX ddlmZmZ d	ZG d
d dZdd ZG dd dZG dd deZdS )z2
Configuration file (aka ``ssh_config``) support.
    N)sha1)StringIO)partial)NN   )CouldNotCanonicalizeConfigParseError   c                   @   s  e Zd ZdZedZddddddd	d
gdgdddddd
d	gdddd	gddd	gdddddddd	d
g	dZdd Ze	dd Z
e	dd Ze	dd Zdd Zdd Zd2ddZdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdS )3	SSHConfiga  
    Representation of config information as stored in the format used by
    OpenSSH. Queries can be made via `lookup`. The format is described in
    OpenSSH's ``ssh_config`` man page. This class is provided primarily as a
    convenience to posix users (since the OpenSSH format is a de-facto
    standard on posix) but should work fine on Windows too.

    .. versionadded:: 1.6
    z(\w+)(?:\s*=\s*|\s+)(.+)%C%h%l%L%n%p%r%u~%d)ZcontrolpathhostnameidentityfileproxycommandZ	proxyjump
match-execc                 C   s
   g | _ dS )a  
        Create a new OpenSSH config object.

        Note: the newer alternate constructors `from_path`, `from_file` and
        `from_text` are simpler to use, as they parse on instantiation. For
        example, instead of::

            config = SSHConfig()
            config.parse(open("some-path.config")

        you could::

            config = SSHConfig.from_file(open("some-path.config"))
            # Or more directly:
            config = SSHConfig.from_path("some-path.config")
            # Or if you have arbitrary ssh_config text from some other source:
            config = SSHConfig.from_text("Host foo\n\tUser bar")
        N)_config)self r   n/mounts/lovelace/software/anaconda3/envs/qiime2-amplicon-2024.2/lib/python3.8/site-packages/paramiko/config.py__init__H   s    zSSHConfig.__init__c                 C   s   |  t|S )zg
        Create a new, parsed `SSHConfig` from ``text`` string.

        .. versionadded:: 2.7
        )	from_filer   )clstextr   r   r   	from_text]   s    zSSHConfig.from_textc              
   C   s*   t |}| |W  5 Q R  S Q R X dS )zr
        Create a new, parsed `SSHConfig` from the file found at ``path``.

        .. versionadded:: 2.7
        N)openr   )r   pathflor   r   r   	from_pathf   s    
zSSHConfig.from_pathc                 C   s   |  }| | |S )zp
        Create a new, parsed `SSHConfig` from file-like object ``flo``.

        .. versionadded:: 2.7
        )parse)r   r#   objr   r   r   r   p   s    
zSSHConfig.from_filec                 C   sR  dgi d}|D ].}|  }|r|dr.qt| j|}|sNtd||d }|d}|dkr| j	
| di i}|d	kr| ||d	< n| ||d
< q|dkr| dkrd|d |< q|dr|dr|dd }|dkr(||d kr|d | 
| n|g|d |< q||d kr||d |< q| j	
| dS )z
        Read an OpenSSH config from the given file object.

        :param file_obj: a file-like object to read the config file from
        *)hostconfig#zUnparsable line {}r      )r(   matchr)   r(   matchesr   noneN")r   ZlocalforwardZremoteforward)strip
startswithrer,   SETTINGS_REGEXr   formatgrouplowerr   append
_get_hosts_get_matchesendswith)r   file_objcontextliner,   keyvaluer   r   r   r%   {   s6    


zSSHConfig.parsec                 C   s   | j |d}d|kr||d< |dddk}t|dd}|r|d|kr|d	  }| |||}||d< | j ||d
d
d}n| j ||dd
d}|S )a  
        Return a dict (`SSHConfigDict`) of config options for a given hostname.

        The host-matching rules of OpenSSH's ``ssh_config`` man page are used:
        For each parameter, the first obtained value will be used.  The
        configuration files contain sections separated by ``Host`` and/or
        ``Match`` specifications, and that section is only applied for hosts
        which match the given patterns or keywords

        Since the first obtained value for each parameter is used, more host-
        specific declarations should be given near the beginning of the file,
        and general defaults at the end.

        The keys in the returned dict are all normalized to lowercase (look for
        ``"port"``, not ``"Port"``. The values are processed according to the
        rules for substitution variable expansion in ``ssh_config``.

        Finally, please see the docs for `SSHConfigDict` for deeper info on
        features such as optional type conversion methods, e.g.::

            conf = my_config.lookup('myhost')
            assert conf['passwordauthentication'] == 'yes'
            assert conf.as_bool('passwordauthentication') is True

        .. note::
            If there is no explicitly configured ``HostName`` value, it will be
            set to the being-looked-up hostname, which is as close as we can
            get to OpenSSH's behavior around that particular option.

        :param str hostname: the hostname to lookup

        .. versionchanged:: 2.5
            Returns `SSHConfigDict` objects instead of dict literals.
        .. versionchanged:: 2.7
            Added canonicalization support.
        .. versionchanged:: 2.7
            Added ``Match`` support.
        .. versionchanged:: 3.3
            Added ``Match final`` support.
        )r   r   ZcanonicalizehostnameN)yesalwaysZcanonicalizemaxdotsr   .ZcanonicaldomainsT)	canonicalfinalF)_lookupgetintcountsplitcanonicalize)r   r   optionsZcanonZmaxdotsdomainsr   r   r   lookup   s,    *      zSSHConfig.lookupNFc                    s   d krt  | jD ]}| |dg |sH| |dg |||sHq|d  D ]T\ } kr|d k	rx|d d  n| < qT dkrT   fdd|D  qTq|r| |S )Nr(   r-   r)   r   c                 3   s   | ]}|  kr|V  qd S Nr   .0xr?   rL   r   r   	<genexpr>  s     z$SSHConfig._lookup.<locals>.<genexpr>)SSHConfigDictr   _pattern_matchesrG   _does_matchitemsextend_expand_variables)r   r   rL   rD   rE   r=   r@   r   rS   r   rF      s0    


zSSHConfig._lookupc              	   C   s   d}|D ]^}d ||}t||}|dk	r4|d }n&zt|}W n tjk
rX   Y nX |r|  S q|dddkr||S t|dS )ag  
        Return canonicalized version of ``hostname``.

        :param str hostname: Target hostname.
        :param options: An `SSHConfigDict` from a previous lookup pass.
        :param domains: List of domains (e.g. ``["paramiko.org"]``).

        :returns: A canonicalized hostname if one was found, else ``None``.

        .. versionadded:: 2.7
        Fz{}.{}Nr   ZcanonicalizefallbacklocalrA   )r5   _addressfamily_host_lookupsocketgethostbynamegaierrorrG   r   )r   r   rL   rM   founddomain	candidateZfamily_specificr   r   r   rK     s    


zSSHConfig.canonicalizec                 C   s$   t  }| jD ]}||d  q|S )z
        Return the set of literal hostnames defined in the SSH config (both
        explicit hostnames and wildcard entries).
        r(   )setr   update)r   hostsentryr   r   r   get_hostnamesE  s    
zSSHConfig.get_hostnamesc                 C   sZ   t |dr|d}d}|D ]8}|drDt||dd  rD dS t||rd}q|S )NrJ   ,F!r   T)hasattrrJ   r2   fnmatch)r   patternstargetr,   patternr   r   r   rV   O  s    

 
zSSHConfig._pattern_matchesc                 C   sV  g }|d d  }t  }|rR|d}	d }
|dd }|dd }|	d |	d  }}|dkrn| ||	rndS |dkr||}
n|d	krd
S |dkr|p|}| ||}
n|dkr| ||}
nn|dkr|p|}| ||}
nP|dkr| ||}
n:|dkr*| ||d|}td krttj	|dd
dj
}
|
d k	rF| |
|	rFdS ||	 q|S )Nr   r   usertypeparamrD   FrE   allTr(   ZoriginalhostZ	localuserexecr   stdout)Zhidewarn)getpassgetuserpoprG   _should_failrV   	_tokenizeinvokeinvoke_import_errorrunokr8   )r   
match_listtarget_hostnamerD   rE   rL   matched
candidatesZlocal_usernamera   passedZconfigured_hostZconfigured_usertype_rp   Zhostvalrn   Zexec_cmdr   r   r   rW   `  sP    

   
zSSHConfig._does_matchc                 C   s   |d r|S | S )Nnegater   )r   Z
would_passra   r   r   r   rx     s    zSSHConfig._should_failc                 C   s   |  |}|s|S |}|dkr*|d|}d|kr<|d }nt}t }d|krZ|d }	n|}	t dd }
t||
}t	j
d}|
| t| |	 }t|  |||
||||	||d
}|}| D ]"\}}||krq||t|}q|S )a  
        Tokenize a string based on current config/hostname data.

        :param config: Current config data.
        :param target_hostname: Original target connection hostname.
        :param key: Config key being tokenized (used to filter token list).
        :param value: Config value being tokenized.

        :returns: The tokenized version of the input ``value`` string.
        r   portrn   rC   r   r   )
r
   r   r   r   r   r   r   r   r   r   )_allowed_tokensrG   SSH_PORTru   rv   r\   gethostnamerJ   LazyFqdnosr"   
expanduserreprr   encode	hexdigestrX   replacestr)r   r)   r   r?   r@   Zallowed_tokensZconfigured_hostnamer   rn   Z
remoteuserZlocal_hostnameZ
local_fqdnhomedirZtohashreplacements	tokenizedfindr   r   r   r   ry     sD    



zSSHConfig._tokenizec                 C   s   | j |g S )aJ  
        Given config ``key``, return list of token strings to tokenize.

        .. note::
            This feels like it wants to eventually go away, but is used to
            preserve as-strict-as-possible compatibility with OpenSSH, which
            for whatever reason only applies some tokens to some config keys.
        )TOKENS_BY_CONFIG_KEYrG   r   r?   r   r   r   r     s    	zSSHConfig._allowed_tokensc                 C   sr   |D ]h}|| dkrqt | j|||}t|| tr\t|| D ]\}}|||| |< q@q||| ||< q|S )aA  
        Return a dict of config options with expanded substitutions
        for a given original & current target hostname.

        Please refer to :doc:`/api/config` for details.

        :param dict config: the currently parsed config
        :param str hostname: the hostname whose config is being looked up
        N)r   ry   
isinstancelist	enumerate)r   r)   r   k	tokenizerir@   r   r   r   rZ     s    
zSSHConfig._expand_variablesc                 C   s4   zt |W S  tk
r.   td|Y nX dS )z>
        Return a list of host_names from host value.
        zUnparsable host {}N)shlexrJ   
ValueErrorr   r5   )r   r(   r   r   r   r9     s    zSSHConfig._get_hostsc           	         s$  g }t |}|rdddd}|d}|drFd|d< |dd }||d	< |d
krb|| q|sttd||d|d< || qdd |D }d|kr d tt fdd|tt fdd| }}d}t	|rd}n$d|kr|
d|
dkrd}|dk	r t||S )z
        Parse a specific Match config line into a list-of-dicts for its values.

        Performs some parse-time validation as well.
        NF)ro   rp   r   r   rh   Tr   r   ro   )rq   rD   rE   z'Missing parameter to Match '{}' keywordrp   c                 S   s   g | ]}|d  qS )ro   r   rP   r   r   r   
<listcomp>  s     z*SSHConfig._get_matches.<locals>.<listcomp>rq   )rq   rD   c                    s   |  kS rO   r   rR   Z	allowabler   r   <lambda>"      z(SSHConfig._get_matches.<locals>.<lambda>c                    s   |  kS rO   r   r   r   r   r   r   #  r   z>Match does not allow 'all' mixed with anything but 'canonical'rD   z-Match does not allow 'all' before 'canonical')r   rJ   rw   r2   r8   r   r5   r   filteranyindex)	r   r,   r-   tokensr   keywordsr}   Zbaderrr   r   r   r:     s@    




 
zSSHConfig._get_matches)NFF)__name__
__module____qualname____doc__r3   compiler4   r   r   classmethodr    r$   r   r%   rN   rF   rK   rf   rV   rW   rx   ry   r   rZ   r9   r:   r   r   r   r   r	   .   s:   




	

<B
#)
8>	r	   c                 C   sh   | dd }|dkrdS z0tj}|dkr2tj}t| d|tjtjtjW S  tj	k
rb   Y nX dS )a  
    Try looking up ``hostname`` in an IPv4 or IPv6 specific manner.

    This is an odd duck due to needing use in two divergent use cases. It looks
    up ``AddressFamily`` in ``options`` and if it is ``inet`` or ``inet6``,
    this function uses `socket.getaddrinfo` to perform a family-specific
    lookup, returning the result if successful.

    In any other situation -- lookup failure, or ``AddressFamily`` being
    unspecified or ``any`` -- ``None`` is returned instead and the caller is
    expected to do something situation-appropriate like calling
    `socket.gethostbyname`.

    :param str hostname: Hostname to look up.
    :param options: `SSHConfigDict` instance w/ parsed options.
    :returns: ``getaddrinfo``-style tuples, or ``None``, depending.
    Zaddressfamilyr   Ninet)
rG   r7   r\   AF_INET6AF_INETgetaddrinfo
SOCK_DGRAM
IPPROTO_IPAI_CANONNAMEr^   )r   rL   Zaddress_familyfamilyr   r   r   r[   /  s"    r[   c                   @   s"   e Zd ZdZdddZdd ZdS )r   z7
    Returns the host's fqdn on request as string.
    Nc                 C   s   d | _ || _|| _d S rO   )fqdnr)   r(   )r   r)   r(   r   r   r   r   Y  s    zLazyFqdn.__init__c           	      C   sl   | j d krfd }t| j| j}|d k	rP|D ]&}|\}}}}}|r(d|kr(|} qPq(|d kr`t }|| _ | j S )NrC   )r   r[   r(   r)   r\   getfqdn)	r   r   resultsresafsocktypeproto	canonnamesar   r   r   __str__^  s    
zLazyFqdn.__str__)N)r   r   r   r   r   r   r   r   r   r   r   T  s   
r   c                   @   s    e Zd ZdZdd Zdd ZdS )rU   a  
    A dictionary wrapper/subclass for per-host configuration structures.

    This class introduces some usage niceties for consumers of `SSHConfig`,
    specifically around the issue of variable type conversions: normal value
    access yields strings, but there are now methods such as `as_bool` and
    `as_int` that yield casted values instead.

    For example, given the following ``ssh_config`` file snippet::

        Host foo.example.com
            PasswordAuthentication no
            Compression yes
            ServerAliveInterval 60

    the following code highlights how you can access the raw strings as well as
    usefully Python type-casted versions (recalling that keys are all
    normalized to lowercase first)::

        my_config = SSHConfig()
        my_config.parse(open('~/.ssh/config'))
        conf = my_config.lookup('foo.example.com')

        assert conf['passwordauthentication'] == 'no'
        assert conf.as_bool('passwordauthentication') is False
        assert conf['compression'] == 'yes'
        assert conf.as_bool('compression') is True
        assert conf['serveraliveinterval'] == '60'
        assert conf.as_int('serveraliveinterval') == 60

    .. versionadded:: 2.5
    c                 C   s"   | | }t |tr|S | dkS )a  
        Express given key's value as a boolean type.

        Typically, this is used for ``ssh_config``'s pseudo-boolean values
        which are either ``"yes"`` or ``"no"``. In such cases, ``"yes"`` yields
        ``True`` and any other value becomes ``False``.

        .. note::
            If (for whatever reason) the stored value is already boolean in
            nature, it's simply returned.

        .. versionadded:: 2.5
        rA   )r   boolr7   )r   r?   valr   r   r   as_bool  s    
zSSHConfigDict.as_boolc                 C   s   t | | S )z
        Express given key's value as an integer, if possible.

        This method will raise ``ValueError`` or similar if the value is not
        int-appropriate, same as the builtin `int` type.

        .. versionadded:: 2.5
        )rH   r   r   r   r   as_int  s    	zSSHConfigDict.as_intN)r   r   r   r   r   r   r   r   r   r   rU   z  s   !rU   )r   rj   ru   r   r3   r   r\   hashlibr   ior   	functoolsr   rz   r{   ImportErroreZssh_exceptionr   r   r   r	   r[   r   dictrU   r   r   r   r   <module>   s0       %&