3 L]1@sNdZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z m Z ddl mZddlmZmZddlmZddlmZdd lmZed Zd Zed Zd ZGdddeZGdddejZGdddeZ GdddeZ!GdddeZ"GdddeZ#GdddeZ$GdddeZ%GdddeZ&dS) z SSH Agent interface N)select)asbytesio_sleep)byte_chr) SSHExceptionAuthenticationException)Message)PKey)retry_on_signal c@s<eZdZddZddZddZddZd d Zd d Zd S)AgentSSHcCsd|_f|_dS)N)_conn_keys)selfr4/tmp/pip-install-wfra5znf/paramiko/paramiko/agent.py__init__/szAgentSSH.__init__cCs|jS)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)rrrrget_keys3s zAgentSSH.get_keyscCsh||_|jt\}}|tkr$tdg}x0t|jD] }|jt||j |j q6Wt ||_ dS)Nz!could not get keys from ssh-agent) r _send_messagecSSH2_AGENTC_REQUEST_IDENTITIESSSH2_AGENT_IDENTITIES_ANSWERrrangeZget_intappendAgentKey get_binaryZ get_stringtupler)rconnptyperesultkeysirrr_connect?s zAgentSSH._connectcCs$|jdk r|jjd|_f|_dS)N)rcloser)rrrr_closeJs  zAgentSSH._closecCsXt|}|jjtjdt|||jd}t|jtjd|d}t |j |fS)Nz>Ir) rrsendstructpacklen _read_allrunpackordZget_byte)rmsgdatarrrrPs  zAgentSSH._send_messagecCsf|jj|}xTt||kr`t|dkr.td|jj|t|}t|dkrVtd||7}qW|S)Nrzlost ssh-agent)rrecvr+r)rwantedr!extrarrrr,Ws    zAgentSSH._read_allN) __name__ __module__ __qualname__rrr$r&rr,rrrrr.s   rc@s0eZdZdZddZddZddZdd Zd S) AgentProxyThreadz@ Class in charge of communication between two channels. cCs"tjj||jd||_d|_dS)N)targetF) threadingThreadrrun_agent_exit)ragentrrrrhszAgentProxyThread.__init__c Csty`|j\}}||_||_|jjt|jt rV|jjdksNt|jjd rVt d|j WnYnXdS)NfilenozUnable to connect to SSH agent) get_connection_AgentProxyThread__inrZ_AgentProxyThread__addrr<connect isinstanceintrhasattrr _communicate)rraddrrrrr;ms    zAgentProxyThread.runcCsddl}|j|j|j}|j|j|j|tjBx|jst|jj |jgggd}x|dD]}|jj |kr|jj j d}t |dkr|jj |q|j PqZ|j|krZ|jj d}t |dkr|jj j |qZ|j PqZWtjtq2WdS)Nrg?i)fcntlrAZF_GETFLZF_SETFLos O_NONBLOCKr=rr<rr1r+r(r&timesleepr)rrIZoldflagseventsfdr0rrrrFs&     zAgentProxyThread._communicatecCs d|_|jj|jjjdS)NT)r=rAr%r<r)rrrrr&s zAgentProxyThread._closeN)r4r5r6__doc__rr;rFr&rrrrr7cs r7c@s eZdZdZddZddZdS)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.) cCstj||dS)N)r7r)rr>rrrrszAgentLocalProxy.__init__c CsRtjtjtj}y.|j|jj|jd|j\}}||fSYnXdS)zX Return a pair of socket object and string address. May block! N)socketAF_UNIX SOCK_STREAMbindr< _get_filenamelistenaccept)rrrGrHrrrr@s  zAgentLocalProxy.get_connectionN)r4r5r6rPrr@rrrrrQsrQc@s eZdZdZddZddZdS)AgentRemoteProxyzA Class to be used when wanting to ask a remote SSH Agent cCstj||||_dS)N)r7r_AgentRemoteProxy__chan)rr>Zchanrrrrs zAgentRemoteProxy.__init__cCs |jdfS)N)r[)rrrrr@szAgentRemoteProxy.get_connectionN)r4r5r6rPrr@rrrrrZsrZc@s0eZdZdZddZddZddZdd Zd S) 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 ... cCs&d|_||_t|||_|jjdS)N)rZ_AgentClientProxy__chanRrZthreadstart)r chanRemoterrrrs zAgentClientProxy.__init__cCs |jdS)N)r%)rrrr__del__szAgentClientProxy.__del__c sdtjkrFtjdkrFtjtjtjytfddWqvdSn0tjdkrrddlj }|j rl|j qvdSndS|_ dS)zJ Method automatically called by ``AgentProxyThread.run``. SSH_AUTH_SOCKwin32csjtjdS)Nra)rBrJenvironr)rrrsz*AgentClientProxy.connect..Nr) rJrcsysplatformrSrTrUr Zparamiko.win_pageant win_pageantcan_talk_to_agentPageantConnectionr)rrgr)rrrBs   zAgentClientProxy.connectcCs6t|drd|j_|jjd|jdk r2|jjdS)zh Close the current connection and terminate the agent Should be called manually r]TiN)rEr]r=joinrr%)rrrrr%s    zAgentClientProxy.closeN)r4r5r6rPrr`rBr%rrrrr\s  r\c@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)AgentServerProxyz :param .Transport t: Transport used for SSH Agent communication forwarding :raises: `.SSHException` -- mostly if we lost the agent cCsPtj|||_tjd|_tj|jtj |jd|_ t ||_ |j j dS)NZsshproxyz /sshproxy.ssh)rr_AgentServerProxy__ttempfilemkdtemp_dirrJchmodstatS_IRWXU_filerQr]r^)rtrrrrs    zAgentServerProxy.__init__cCs |jdS)N)r%)rrrrr`szAgentServerProxy.__del__cCs2|jj}|dkrtd|jd|j|dS)Nzlost ssh-agentz auth-agent)rlZopen_forward_agent_channelrset_namer$)rZ conn_sockrrrrBs   zAgentServerProxy.connectcCs8tj|jtj|jd|j_|jjd|jdS)zk Terminate the agent, clean the files, close connections Should be called manually TiN) rJremoversrmdirror]r=rjr&)rrrrr%s    zAgentServerProxy.closecCs d|jiS)z Helper for the environnement under unix :return: a dict containing the ``SSH_AUTH_SOCK`` environnement variables ra)rW)rrrrget_env$szAgentServerProxy.get_envcCs|jS)N)rs)rrrrrW-szAgentServerProxy._get_filenameN) r4r5r6rPrr`rBr%rxrWrrrrrks   rkc@s0eZdZdZddZddZddZdd Zd S) 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/") cCs"d|_||_|j|jg|_dS)N)rZ_AgentRequestHandler__chanCZrequest_forward_agent_forward_agent_handler"_AgentRequestHandler__clientProxys)rZ chanClientrrrrGs zAgentRequestHandler.__init__cCs|jjt|dS)N)r{rr\)rr_rrrrzMsz*AgentRequestHandler._forward_agent_handlercCs |jdS)N)r%)rrrrr`PszAgentRequestHandler.__del__cCsx|jD] }|jqWdS)N)r{r%)rprrrr%Ss zAgentRequestHandler.closeN)r4r5r6rPrrzr`r%rrrrry1s ryc@s eZdZdZddZddZdS)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 Cstj|dtjkrPtjdkrPtjtjtj}y|j tjdWqdSn2tjdkr~ddl m }|j rx|j }qdSndS|j|dS)NrarbrR)rg)rrrJrcrerfrSrTrUrBrgrhrir$)rrrgrrrrgs    zAgent.__init__cCs |jdS)z1 Close the SSH agent connection. N)r&)rrrrr%}sz Agent.closeN)r4r5r6rPrr%rrrrr}Xs r}c@s8eZdZdZddZddZddZdd Zd d Zd S) rz 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. cCs$||_||_d|_t|j|_dS)N)r>blobZ public_blobrZget_textname)rr>rrrrrszAgentKey.__init__cCs|jS)N)r)rrrrrszAgentKey.asbytescCs|jS)N)r)rrrr__str__szAgentKey.__str__cCs|jS)N)r)rrrrget_nameszAgentKey.get_namecCsXt}|jt|j|j|j||jd|jj|\}}|tkrPt d|j S)Nrzkey cannot be used for signing) rZadd_bytecSSH2_AGENTC_SIGN_REQUESTZ add_stringrZadd_intr>rSSH2_AGENT_SIGN_RESPONSErr)rr0r/r r!rrr sign_ssh_datas    zAgentKey.sign_ssh_dataN) r4r5r6rPrrrrrrrrrrs r)'rPrJrSr)rer9rLrmrqrZparamiko.commonrrZparamiko.py3compatrZparamiko.ssh_exceptionrrZparamiko.messagerZ paramiko.pkeyr Z paramiko.utilr rrrrobjectrr:r7rQrZr\rkryr}rrrrrs8     5; ;2',