HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/conch/client/__pycache__/default.cpython-38.pyc
U

��W[�.�@s�dZddlmZddlmZddlmZmZmZm	Z
ddlmZddl
mZddlmZmZmZddlmZmZmZdd	lmZmZdd
lmZddlZddlZddlZddlZer�ddl Z dZ!e"Z#d
d�Z$dd�Z%dd�Z&Gdd�dej'�Z'dS)aN
Various classes and functions for implementing user-interaction in the
command-line conch client.

You probably shouldn't use anything in this module directly, since it assumes
you are sitting at an interactive terminal.  For example, to programmatically
interact with a known_hosts database, use L{twisted.conch.client.knownhosts}.
�)�print_function)�log)�nativeString�	raw_input�_PY3�_b64decodebytes)�FilePath)�
ConchError)�common�keys�userauth)�defer�protocol�reactor)�KnownHostsFile�	ConsoleUI)�agentNz~/.ssh/known_hostscCsV|jjd}tj�|�}t�t|jjdp4tj	�
t���}tdd��}|�
||||�S)a%
    Verify a host's key.

    This function is a gross vestige of some bad factoring in the client
    internals.  The actual implementation, and a better signature of this logic
    is in L{KnownHostsFile.verifyHostKey}.  This function is not deprecated yet
    because the callers have not yet been rehabilitated, but they should
    eventually be changed to call that method instead.

    However, this function does perform two functions not implemented by
    L{KnownHostsFile.verifyHostKey}.  It determines the path to the user's
    known_hosts file based on the options (which should really be the options
    object's job), and it provides an opener to L{ConsoleUI} which opens
    '/dev/tty' so that the user will be prompted on the tty of the process even
    if the input and output of the process has been redirected.  This latter
    part is, somewhat obviously, not portable, but I don't know of a portable
    equivalent that could be used.

    @param host: Due to a bug in L{SSHClientTransport.verifyHostKey}, this is
    always the dotted-quad IP address of the host being connected to.
    @type host: L{str}

    @param transport: the client transport which is attempting to connect to
    the given host.
    @type transport: L{SSHClientTransport}

    @param fingerprint: the fingerprint of the given public key, in
    xx:xx:xx:... format.  This is ignored in favor of getting the fingerprint
    from the key itself.
    @type fingerprint: L{str}

    @param pubKey: The public key of the server being connected to.
    @type pubKey: L{str}

    @return: a L{Deferred} which fires with C{1} if the key was successfully
    verified, or fails if the key could not be successfully verified.  Failure
    types may include L{HostKeyChanged}, L{UserRejectedKey}, L{IOError} or
    L{KeyboardInterrupt}.
    �host�known-hostscSstdddd�S)N�/dev/ttyzr+br)�	buffering)�_open�rr�>/usr/lib/python3/dist-packages/twisted/conch/client/default.py�<lambda>X�zverifyHostKey.<locals>.<lambda>)�factory�optionsr�KeyZ
fromStringr�fromPathr�os�path�
expanduser�_KNOWN_HOSTSr�
verifyHostKey)�	transportr�pubKeyZfingerprintZ
actualHostZ	actualKeyZkhZuirrrr$*s(
�r$c
	Cs"t�|�d}d}|dsHtj�tj�d��sHtd�t�tj�d��|dpRt}zt	tj�|�d�}Wnt
k
r�YdSX|��|��D]�}|��}t
|�dkr�q�|dd�\}	}
}||	�d	�kr�q�|
|kr�q�zt|�}WnYq�YnX||k�rW5QR�d
Sd}q�W5QRX|S)z�
    Checks to see if host is in the known_hosts file for the user.

    @return: 0 if it isn't, 1 if it is and is the same, 2 if it's changed.
    @rtype: L{int}
    rrz~/.ssh/zCreating ~/.ssh directory...z~/.ssh�rb�N�,��)r
ZgetNSr r!�existsr"�print�mkdirr#�open�IOError�	readlines�split�len�decodebytes)
rr&r�keyTypeZretValZkh_fileZknown_hosts�liner2ZhostsZhostKeyTypeZ
encodedKeyZ
decodedKeyrrr�isInKnownHosts]s8

r7cCsXt�t|dptj�t���}g}|��D]$}|�|�r*|j	|kr*|�
|j	�q*|pVdS)a�
    Look in known_hosts for a key corresponding to C{host}.
    This can be used to change the order of supported key types
    in the KEXINIT packet.

    @type host: L{str}
    @param host: the host to check in known_hosts
    @type options: L{twisted.conch.client.options.ConchOptions}
    @param options: options passed to client
    @return: L{list} of L{str} representing key types or L{None}.
    rN)rrrr r!r"r#ZiterentriesZmatchesHostr5�append)rrZ
knownHostsZkeyTypes�entryrrr�getHostKeyAlgorithms�s
�

r:c@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zddd�Z	dd�Z
dd�Zdd�Zdd�Z
edd��Zeejdd���Zd
S)�SSHUserAuthClientcGs:tjj||f|��d|_||_g|_|js6ddg|_dS)Nz
~/.ssh/id_rsaz
~/.ssh/id_dsa)rr;�__init__�keyAgentr�	usedFiles�	identitys)�self�userr�argsrrrr<�szSSHUserAuthClient.__init__cCsfdtjkrV|jdsVt�d�t�ttj	�}|�
tjd�}|�|j�|�
|j�ntj�|�dS)NZ
SSH_AUTH_SOCKZnoagentzusing agent)r �environrr�msgrZ
ClientCreatorrrZSSHAgentClientZconnectUNIXZaddCallback�	_setAgentZ
addErrback�_ebSetAgentrr;�serviceStarted)r@Zcc�drrrrG�s
z SSHUserAuthClient.serviceStartedcCs|jr|jj��d|_dS�N)r=r%ZloseConnection�r@rrr�serviceStopped�sz SSHUserAuthClient.serviceStoppedcCs ||_|j��}|�|j�|SrI)r=Z
getPublicKeysZaddBothrF)r@�arHrrrrE�s
zSSHUserAuthClient._setAgentcCstj�|�dSrI)rr;rG)r@�frrrrF�szSSHUserAuthClient._ebSetAgentc
Cs\|���Jzt�|�}|WW5QR�Sttfk
rLt�td��YnXW5QRXdS)z�
        Prompt for a password using L{getpass.getpass}.

        @param prompt: Written on tty to ask for the input.
        @type prompt: L{str}
        @return: The input.
        @rtype: L{str}
        ZPEBKACN)�_replaceStdoutStdin�getpass�KeyboardInterruptr0r-r	�r@�prompt�prrr�_getPassword�s	

zSSHUserAuthClient._getPasswordNcCsl|rt|�}ndt|j�|jj��jf}z |�|��t���}t	�
|�WStk
rft	��YSXdS)Nz%s@%s's password: )
rrAr%ZgetPeerrrT�encode�sys�getdefaultencodingr
�succeedr	�failrQrrr�getPassword�s
�zSSHUserAuthClient.getPasswordcs��jr�j��}|dk	r|S�fdd��jjD�}t�t�jj��t�t|��|sZdS|d}t�|��j�|�t	j
�|�}|d7}t	j
�|�s����Szt
j�|�WSt
jk
r����YSXdS)z�
        Get a public key from the key agent if possible, otherwise look in
        the next configured identity file for one.
        Ncsg|]}|�jkr|�qSr)r>)�.0�xrJrr�
<listcomp>�s
z2SSHUserAuthClient.getPublicKey.<locals>.<listcomp>rz.pub)r=�getPublicKeyrr?rrD�strr>r8r r!r"r,rr�fromFile�BadKeyError)r@�key�files�filerrJrr^�s(

zSSHUserAuthClient.getPublicKeycCs,|js|j�|��|�Stj�|||�SdS)z�
        Extend the base signing behavior by using an SSH agent to sign the
        data, if one is available.

        @type publicKey: L{Key}
        @type signData: L{bytes}
        N)r>r=�signDataZblobrr;)r@Z	publicKeyrerrrreszSSHUserAuthClient.signDatac
Cs�tj�|jd�}tj�|�s"dSzt�tj�	|��WStj
k
r�td�D]v}d|jd}z4|�|��
t���}t�tjj	||d��WYStjtfk
r�YnXt�td��YS�Yn"tk
r�t�t��YnXdS)z�
        Try to load the private key from the last used file identified by
        C{getPublicKey}, potentially asking for the passphrase if the key is
        encrypted.
        ���Nr(zEnter passphrase for key '%s': )Z
passphrasezbad password)r r!r"r>r,r
rXrrr`ZEncryptedKeyError�rangerTrUrV�getfilesystemencodingrar	rYrPr-r�stop)r@rd�irRrSrrr�
getPrivateKeys(
� zSSHUserAuthClient.getPrivateKeyc	Cs�g}|���f|r t|�d��|r2t|�d��|D]6\}}|�d�}|r\|�t|��q6|�t�|��q6W5QRXt�|�S)Nzutf-8)rNr-�decoder8rrOr
rX)r@�nameZinstructionZpromptsZ	responsesrRZechorrr�getGenericAnswers,s

z#SSHUserAuthClient.getGenericAnswerscCs4tdd�}tdd�}tr,t�|�}t�|�}||fS)ah
        Open /dev/tty as two streams one in read, one in write mode,
        and return them.

        @return: File objects for reading and writing to /dev/tty,
                 corresponding to standard input and standard output.
        @rtype: A L{tuple} of L{io.TextIOWrapper} on Python 3.
                A L{tuple} of binary files on Python 2.
        rr'�wb)r/r�io�
TextIOWrapper)�cls�stdin�stdoutrrr�_openTty<s



zSSHUserAuthClient._openTtyc	csRtjtj}}|��\t_t_z
dVW5tj��tj��||t_t_XdS)zv
        Contextmanager that replaces stdout and stdin with /dev/tty
        and resets them when it is done.
        N)rVrtrsru�close)rrZoldoutZoldinrrrrNOs


z%SSHUserAuthClient._replaceStdoutStdin)N)�__name__�
__module__�__qualname__r<rGrKrErFrTrZr^rerkrn�classmethodru�
contextlib�contextmanagerrNrrrrr;�s 	

r;)(�__doc__Z
__future__rZtwisted.pythonrZtwisted.python.compatrrrrr4Ztwisted.python.filepathrZtwisted.conch.errorr	Ztwisted.conch.sshr
rrZtwisted.internetr
rrZtwisted.conch.client.knownhostsrrZtwisted.conch.clientrr rVrOr{rpr#r/rr$r7r:r;rrrr�<module>s$	 3(