
    a$2                        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ZddlZddl	m	Z	 ddl
mZmZ ddlmZ ddlmZmZ ddlmZ ddlmZ dd	lmZ  ed
      ZdZ ed      ZdZ G d de      Z G d dej<                        Z G d de      Z  G d de      Z! G d de      Z" G d de      Z# G d de      Z$ G d de      Z% G d de      Z&y) z
SSH Agent interface
    N)select)asbytesio_sleep)byte_chr)SSHExceptionAuthenticationException)Message)PKey)retry_on_signal            c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)AgentSSHc                      d | _         d| _        y N )_conn_keysselfs    .lib/python3.12/site-packages/paramiko/agent.py__init__zAgentSSH.__init__/   s    

    c                     | j                   S )a4  
        Return the list of keys available through the SSH agent, if any.  If
        no SSH agent was running (or it couldn't be contacted), an empty list
        will be returned.

        :return:
            a tuple of `.AgentKey` objects representing keys available on the
            SSH agent
        )r   r   s    r   get_keyszAgentSSH.get_keys3   s     zzr   c                 <   || _         | j                  t              \  }}|t        k7  rt	        d      g }t        |j                               D ];  }|j                  t        | |j                                      |j                          = t        |      | _        y )Nz!could not get keys from ssh-agent)r   _send_messagecSSH2_AGENTC_REQUEST_IDENTITIESSSH2_AGENT_IDENTITIES_ANSWERr   rangeget_intappendAgentKey
get_binary
get_stringtupler   )r   connptyperesultkeysis         r   _connectzAgentSSH._connect?   s    
**+JKv00BCCv~~'( 	 AKKv'8'8':;<	  4[
r   c                 l    | j                   | j                   j                          d | _         d| _        y r   )r   closer   r   s    r   _closezAgentSSH._closeJ   s*    ::!JJ

r   c                 H   t        |      }| j                  j                  t        j                  dt        |            |z          | j                  d      }t        | j                  t        j                  d|      d               }t        |j                               |fS )Nz>I   r   )r   r   sendstructpacklen	_read_allr	   unpackordget_byte)r   msgdatas      r   r   zAgentSSH._send_messageP   sv    cl

D#c(3c9:~~a dnnV]]4%>q%ABC3<<>"C''r   c                 2   | j                   j                  |      }t        |      |k  rmt        |      dk(  rt        d      | j                   j                  |t        |      z
        }t        |      dk(  rt        d      ||z  }t        |      |k  rm|S )Nr   lost ssh-agent)r   recvr7   r   )r   wantedr+   extras       r   r8   zAgentSSH._read_allW   s    (&kF"6{a"#344JJOOFS[$89E5zQ"#344eOF &kF" r   N)	__name__
__module____qualname__r   r   r.   r1   r   r8   r   r   r   r   r   .   s     
	!(	r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)AgentProxyThreadz@
    Class in charge of communication between two channels.
    c                 v    t         j                  j                  | | j                         || _        d| _        y )N)targetF)	threadingThreadr   run_agent_exitr   agents     r   r   zAgentProxyThread.__init__h   s-    !!$txx!8
r   c                 ^   	 | j                         \  }}|| _        || _        | j                  j	                          t        | j                  t              sA| j                  j                   t        | j                  j                  d      st        d      | j                          y #   xY w)NfilenozUnable to connect to SSH agent)get_connection_AgentProxyThread__inr_AgentProxyThread__addrrM   connect
isinstanceintr   hasattrr   _communicate)r   raddrs      r   rL   zAgentProxyThread.runm   s    	++-IQ DJDKKK!dkk3/!!)t{{00(;-.NOO	s   B&B) )B,c                 v   dd l }|j                  | j                  |j                        }|j                  | j                  |j                  |t        j
                  z         | j                  sJt        | j                  j                  | j                  gg g d      }|d   D ]  }| j                  j                  |k(  ra| j                  j                  j                  d      }t        |      dk7  r| j                  j                  |       k| j                           nr| j                  |k(  s| j                  j                  d      }t        |      dk7  r&| j                  j                  j                  |       | j                           n t        j                  t                | j                  sIy y )Nr   g      ?i   )fcntlrT   F_GETFLF_SETFLos
O_NONBLOCKrN   r   rM   r   r@   r7   r4   r1   timesleepr   )r   r^   oldflagseventsfdr=   s         r   rZ   zAgentProxyThread._communicate   s2   ;;tzz5==9DJJx"--/GH**T[[..

;RSIFQi ;;$$*;;,,11#6D4yA~

-ZZ2%::??3/D4yA~))..t4 JJx # **r   c                     d| _         | j                  j                          | j                  j                  j                          y )NT)rN   rT   r0   rM   r   r   s    r   r1   zAgentProxyThread._close   s/    


!r   N)rC   rD   rE   __doc__r   rL   rZ   r1   r   r   r   rG   rG   c   s    
&!0"r   rG   c                       e Zd ZdZd Zd Zy)AgentLocalProxyz
    Class to be used when wanting to ask a local SSH Agent being
    asked from a remote fake agent (so use a unix socket for ex.)
    c                 0    t         j                  | |       y N)rG   r   rO   s     r   r   zAgentLocalProxy.__init__   s    !!$.r   c                    t        j                   t         j                  t         j                        }	 |j                  | j                  j                                |j                  d       |j                         \  }}||fS #   xY w)zX
        Return a pair of socket object and string address.

        May block!
           )socketAF_UNIXSOCK_STREAMbindrM   _get_filenamelistenaccept)r   r)   r[   r\   s       r   rS   zAgentLocalProxy.get_connection   si     }}V^^V-?-?@	IIdkk//12KKNIQd7N	s   AB BNrC   rD   rE   ri   r   rS   r   r   r   rk   rk      s    
/r   rk   c                       e Zd ZdZd Zd Zy)AgentRemoteProxyzA
    Class to be used when wanting to ask a remote SSH Agent
    c                 >    t         j                  | |       || _        y rm   )rG   r   _AgentRemoteProxy__chan)r   rP   chans      r   r   zAgentRemoteProxy.__init__   s    !!$.r   c                     | j                   d fS rm   )r{   r   s    r   rS   zAgentRemoteProxy.get_connection   s    {{D  r   Nrw   r   r   r   ry   ry      s    !r   ry   c                   (    e Zd ZdZd Zd Zd Zd Zy)AgentClientProxya  
    Class proxying request as a client:

    #. client ask for a request_forward_agent()
    #. server creates a proxy and a fake SSH Agent
    #. server ask for establishing a connection when needed,
       calling the forward_agent_handler at client side.
    #. the forward_agent_handler launch a thread for connecting
       the remote fake agent and the local agent
    #. Communication occurs ...
    c                 v    d | _         || _        t        | |      | _        | j                  j	                          y rm   )r   _AgentClientProxy__chanRry   threadstartr   
chanRemotes     r   r   zAgentClientProxy.__init__   s.    
!&tZ8r   c                 $    | j                          y rm   r0   r   s    r   __del__zAgentClientProxy.__del__       

r   c                 t   dt         j                  v r\t        j                  dk7  rIt	        j                  t        j
                  t        j                        	 t        fd       | _        yt        j                  dk(  r/ddlm	} |j                         r|j                         | _        yyy#  Y yxY w)zJ
        Method automatically called by ``AgentProxyThread.run``.
        SSH_AUTH_SOCKwin32c                  H     j                  t        j                  d         S )Nr   )rV   ra   environ)r)   s   r   <lambda>z*AgentClientProxy.connect.<locals>.<lambda>   s    DLLO)DE r   Nr   )ra   r   sysplatformrp   rq   rr   r   paramiko.win_pageantwin_pageantcan_talk_to_agentPageantConnectionr   )r   r   r)   s     @r   rV   zAgentClientProxy.connect   s     rzz)0G==1C1CDDE  
 \\W$6,,."446 
	  s   B3 3B7c                     t        | d      r,d| j                  _        | j                  j                  d       | j                  | j                  j                          yy)zh
        Close the current connection and terminate the agent
        Should be called manually
        r   T  N)rY   r   rN   joinr   r0   r   s    r   r0   zAgentClientProxy.close   sK    
 4" $DKKKKT"::!JJ "r   N)rC   rD   rE   ri   r   r   rV   r0   r   r   r   r   r      s    
2	r   r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	AgentServerProxyz
    :param .Transport t: Transport used for SSH Agent communication forwarding

    :raises: `.SSHException` -- mostly if we lost the agent
    c                 H   t         j                  |        || _        t        j                  d      | _        t        j                  | j
                  t        j                         | j
                  dz   | _
        t        |       | _        | j                  j                          y )Nsshproxyz/sshproxy.ssh)r   r   _AgentServerProxy__ttempfilemkdtemp_dirra   chmodstatS_IRWXU_filerk   r   r   )r   ts     r   r   zAgentServerProxy.__init__  sk    $$$Z0	
DLL)YY0
%d+r   c                 $    | j                          y rm   r   r   s    r   r   zAgentServerProxy.__del__  r   r   c                     | j                   j                         }|t        d      |j                  d       | j	                  |       y )Nr?   z
auth-agent)r   open_forward_agent_channelr   set_namer.   )r   	conn_socks     r   rV   zAgentServerProxy.connect  s@    HH779	/00<(i r   c                     t        j                  | j                         t        j                  | j                         d| j
                  _        | j
                  j                  d       | j                          y)zk
        Terminate the agent, clean the files, close connections
        Should be called manually
        Tr   N)	ra   remover   rmdirr   r   rN   r   r1   r   s    r   r0   zAgentServerProxy.close  sK    
 			$**
 r   c                 &    d| j                         iS )z
        Helper for the environnement under unix

        :return:
            a dict containing the ``SSH_AUTH_SOCK`` environnement variables
        r   )rt   r   s    r   get_envzAgentServerProxy.get_env$  s      !3!3!566r   c                     | j                   S rm   )r   r   s    r   rt   zAgentServerProxy._get_filename-  s    zzr   N)
rC   rD   rE   ri   r   r   rV   r0   r   rt   r   r   r   r   r      s%    !	7r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)AgentRequestHandlera  
    Primary/default implementation of SSH agent forwarding functionality.

    Simply instantiate this class, handing it a live command-executing session
    object, and it will handle forwarding any local SSH agent processes it
    finds.

    For example::

        # Connect
        client = SSHClient()
        client.connect(host, port, username)
        # Obtain session
        session = client.get_transport().open_session()
        # Forward local agent
        AgentRequestHandler(session)
        # Commands executed after this point will see the forwarded agent on
        # the remote end.
        session.exec_command("git clone https://my.git.repository/")
    c                 d    d | _         || _        |j                  | j                         g | _        y rm   )r   _AgentRequestHandler__chanCrequest_forward_agent_forward_agent_handler"_AgentRequestHandler__clientProxys)r   
chanClients     r   r   zAgentRequestHandler.__init__G  s,    
!(()D)DE r   c                 L    | j                   j                  t        |             y rm   )r   r$   r   r   s     r   r   z*AgentRequestHandler._forward_agent_handlerM  s    ""#3J#?@r   c                 $    | j                          y rm   r   r   s    r   r   zAgentRequestHandler.__del__P  r   r   c                 F    | j                   D ]  }|j                           y rm   )r   r0   )r   ps     r   r0   zAgentRequestHandler.closeS  s     $$ 	AGGI	r   N)rC   rD   rE   ri   r   r   r   r0   r   r   r   r   r   1  s    *!Ar   r   c                       e Zd ZdZd Zd Zy)AgentaL  
    Client interface for using private keys from an SSH agent running on the
    local machine.  If an SSH agent is running, this class can be used to
    connect to it and retrieve `.PKey` objects which can be used when
    attempting to authenticate to remote SSH servers.

    Upon initialization, a session with the local machine's SSH agent is
    opened, if one is running. If no agent is running, initialization will
    succeed, but `get_keys` will return an empty tuple.

    :raises: `.SSHException` --
        if an SSH agent is found, but speaks an incompatible protocol
    c                    t         j                  |        dt        j                  v rit        j
                  dk7  rVt        j                  t        j                  t        j                        }	 |j                  t        j                  d          n<t        j
                  dk(  r(ddl
m} |j                         r|j                         }ny y | j                  |       y #  Y y xY w)Nr   r   ro   )r   )r   r   ra   r   r   r   rp   rq   rr   rV    r   r   r   r.   )r   r)   r   s      r   r   zAgent.__init__g  s    $rzz)0G==1C1CDDRZZ89 \\W$%,,."446 ds   ."C C#c                 $    | j                          y)z1
        Close the SSH agent connection.
        N)r1   r   s    r   r0   zAgent.close}  s     	r   N)rC   rD   rE   ri   r   r0   r   r   r   r   r   X  s    ,r   r   c                   >    e Zd ZdZd Zd Zd Zd Zed        Z	d Z
y)	r%   z
    Private key held in a local SSH agent.  This type of key can be used for
    authenticating to a remote server (signing).  Most other key operations
    work as expected.
    c                 j    || _         || _        d | _        t        |      j	                         | _        y rm   )rP   blobpublic_blobr	   get_textname)r   rP   r   s      r   r   zAgentKey.__init__  s,    
	DM**,	r   c                     | j                   S rm   )r   r   s    r   r   zAgentKey.asbytes      yyr   c                 "    | j                         S rm   )r   r   s    r   __str__zAgentKey.__str__  s    ||~r   c                     | j                   S rm   )r   r   s    r   get_namezAgentKey.get_name  r   r   c                     t         rm   )NotImplementedErrorr   s    r   _fieldszAgentKey._fields  s    !!r   c                 >   t               }|j                  t               |j                  | j                         |j                  |       |j                  d       | j                  j                  |      \  }}|t        k7  rt        d      |j                         S )Nr   zkey cannot be used for signing)r	   add_bytecSSH2_AGENTC_SIGN_REQUEST
add_stringr   add_intrP   r   SSH2_AGENT_SIGN_RESPONSEr   r&   )r   r=   r<   r*   r+   s        r   sign_ssh_datazAgentKey.sign_ssh_data  sy    i./tyy!tA

005v,,?@@  ""r   N)rC   rD   rE   ri   r   r   r   r   propertyr   r   r   r   r   r%   r%     s4    - " "	#r   r%   )'ri   ra   rp   r5   r   rJ   rc   r   r   r   paramiko.commonr   r   paramiko.py3compatr   paramiko.ssh_exceptionr   r   paramiko.messager	   paramiko.pkeyr
   paramiko.utilr   r    r!   r   r   objectr   rK   rG   rk   ry   r   r   r   r   r%   r   r   r   <module>r      s   & 
   
      - ' H $  )"*2, ! $RL  2v 2j8"y'' 8"v& 2
!' 
!8v 8v/x /d$& $N)H )X##t ##r   