U vf>@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z ddl m Z mZddlmZmZddlmZddlmZmZdd lmZmZed Zd Zed Zd ZdZdZeedZ e!e "D]\Z#Z$e$e e#d<qGdddZ%Gdddej&Z'Gddde'Z(Gddde'Z)ddZ*GdddZ+Gddde%Z,Gd d!d!Z-Gd"d#d#e%Z.Gd$d%d%eZ/dS)&z SSH Agent interface N)DEBUG)select)io_sleepbyte_chr) SSHExceptionAuthenticationException)Message)PKeyUnknownKeyType)asbytes get_logger )z rsa-sha2-256z rsa-sha2-512z-cert-v01@openssh.comc@s<eZdZddZddZddZddZd d Zd d Zd S)AgentSSHcCsd|_d|_dSN)_conn_keysselfrr?/opt/alt/python38/lib/python3.8/site-packages/paramiko/agent.py__init__>szAgentSSH.__init__cCs|jS)a 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. This method performs no IO, just returns the list of keys retrieved when the connection was made. :return: a tuple of `.AgentKey` objects representing keys available on the SSH agent )rrrrrget_keysBs zAgentSSH.get_keyscCsd||_|t\}}|tkr$tdg}t|D] }|t|| | dq4t ||_ dS)Nz!could not get keys from ssh-agent)agentblobcomment) r _send_messagecSSH2_AGENTC_REQUEST_IDENTITIESSSH2_AGENT_IDENTITIES_ANSWERrrangeZget_intappendAgentKey get_binaryget_texttupler)rconnptyperesultkeysirrr_connectQszAgentSSH._connectcCs$|jdk r|jd|_d|_dSr)rcloserrrrr_closeas  zAgentSSH._closecCsXt|}|jtdt|||d}t|td|d}t | |fS)Nz>Irr) r rsendstructpacklen _read_allrunpackordZget_byte)rmsgdatarrrr gs  zAgentSSH._send_messagecCsb|j|}t||kr^t|dkr,td|j|t|}t|dkrTtd||7}q |S)Nrlost ssh-agent)rrecvr4r)rwantedr+extrarrrr5ns     zAgentSSH._read_allN) __name__ __module__ __qualname__rrr.r0r r5rrrrr=s rc@s0eZdZdZddZddZddZdd Zd S) AgentProxyThreadz@ Class in charge of communication between two channels. cCs"tjj||jd||_d|_dS)N)targetF) threadingThreadrrun_agent_exitrrrrrrszAgentProxyThread.__init__cCspz\|\}}||_||_|jt|jtsR|jjdksJt|jjdsRt d| WnYnXdS)NfilenozUnable to connect to SSH agent) get_connection_AgentProxyThread__inrZ_AgentProxyThread__addrrFconnect isinstanceintrhasattrr _communicate)rraddrrrrrEs      zAgentProxyThread.runcCsddl}||j|j}||j|j|tjB|jst|jj |jgggd}|dD]}|jj |kr|jj d}t |dkr|j |q| qqV|j|krV|j d}t |dkr|jj |qV| qqVttq0dS)Nrg?i)fcntlrKZF_GETFLZF_SETFLos O_NONBLOCKrGrrFrr;r4r1r0timesleepr)rrSZoldflagseventsfdr9rrrrPs&      zAgentProxyThread._communicatecCs d|_|j|jjdS)NT)rGrKr/rFrrrrrr0s zAgentProxyThread._closeN)r>r?r@__doc__rrErPr0rrrrrAzs rAc@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.) cCst||dSN)rArrHrrrrszAgentLocalProxy.__init__cCsTttjtj}z0||j|d|\}}||fWSYnXdS)zX Return a pair of socket object and string address. May block! N)socketAF_UNIX SOCK_STREAMbindrF _get_filenamelistenaccept)rr)rQrRrrrrJs   zAgentLocalProxy.get_connectionNr>r?r@rZrrJrrrrr[sr[c@s eZdZdZddZddZdS)AgentRemoteProxyzA Class to be used when wanting to ask a remote SSH Agent cCst||||_dSr\)rAr_AgentRemoteProxy__chan)rrZchanrrrrs zAgentRemoteProxy.__init__cCs |jdfSr\)rgrrrrrJszAgentRemoteProxy.get_connectionNrerrrrrfsrfcCsdtjkrLtjdkrLttjtj}z|tjd|WSYdSXnHtjdkrddlm }m }d}| r|| }n| r| }|SdSdS)zj Returns some SSH agent object, or None if none were found/supported. .. versionadded:: 2.10 SSH_AUTH_SOCKwin32Nr]) win_pageant win_openssh)rTenvironsysplatformr^r_r`rLrjrkZcan_talk_to_agentZPageantConnectionZOpenSSHAgentConnection)r)rjrkrrrget_agent_connections    rpc@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|||_|jdSr\)rZ_AgentClientProxy__chanRrfthreadstartrZ chanRemoterrrrs zAgentClientProxy.__init__cCs |dSr\r/rrrr__del__ szAgentClientProxy.__del__cCst}|sdS||_dS)zJ Method automatically called by ``AgentProxyThread.run``. N)rprrr)rrrrL szAgentClientProxy.connectcCs6t|drd|j_|jd|jdk r2|jdS)zh Close the current connection and terminate the agent Should be called manually rrTN)rOrrrGjoinrr/rrrrr/s    zAgentClientProxy.closeN)r>r?r@rZrrvrLr/rrrrrqs   rqc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)AgentServerProxya Allows an SSH server to access a forwarded agent. This also creates a unix domain socket on the system to allow external programs to also access the agent. For this reason, you probably only want to create one of these. :meth:`connect` must be called before it is usable. This will also load the list of keys the agent contains. You must also call :meth:`close` in order to clean up the unix socket and the thread that maintains it. (:class:`contextlib.closing` might be helpful to you.) :param .Transport t: Transport used for SSH Agent communication forwarding :raises: `.SSHException` -- mostly if we lost the agent cCsPt|||_td|_t|jtj |jd|_ t ||_ |j dS)NZsshproxyz /sshproxy.ssh)rr_AgentServerProxy__ttempfilemkdtemp_dirrTchmodstatS_IRWXU_filer[rrrs)rtrrrr4s    zAgentServerProxy.__init__cCs |dSr\rurrrrrv=szAgentServerProxy.__del__cCs2|j}|dkrtd|d||dS)Nr:z auth-agent)r{Zopen_forward_agent_channelrset_namer.)rZ conn_sockrrrrL@s   zAgentServerProxy.connectcCs8t|jt|jd|j_|jd|dS)zk Terminate the agent, clean the files, close connections Should be called manually TrxN) rTremoverrmdirr~rrrGryr0rrrrr/Gs    zAgentServerProxy.closecCs d|iS)z Helper for the environment under unix :return: a dict containing the ``SSH_AUTH_SOCK`` environment variables rh)rbrrrrget_envRszAgentServerProxy.get_envcCs|jSr\)rrrrrrb[szAgentServerProxy._get_filenameN) r>r?r@rZrrvrLr/rrbrrrrrz"s   rzc@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|_||_||jg|_dSr\)rZ_AgentRequestHandler__chanCZrequest_forward_agent_forward_agent_handler"_AgentRequestHandler__clientProxys)rZ chanClientrrrrus zAgentRequestHandler.__init__cCs|jt|dSr\)rr$rqrtrrrr{sz*AgentRequestHandler._forward_agent_handlercCs |dSr\rurrrrrv~szAgentRequestHandler.__del__cCs|jD] }|qdSr\)rr/)rprrrr/s zAgentRequestHandler.closeN)r>r?r@rZrrrvr/rrrrr_s rc@s eZdZdZddZddZdS)Agenta 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 .. versionchanged:: 2.10 Added support for native openssh agent on windows (extending previous putty pageant support) cCs&t|t}|sdS||dSr\)rrrpr.rwrrrrs  zAgent.__init__cCs |dS)z1 Close the SSH agent connection. N)r0rrrrr/sz Agent.closeN)r>r?r@rZrr/rrrrrsrcs`eZdZdZdddZddZddZd d Zfd d Zd dZ e ddZ dddZ Z S)r%a 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. .. versionchanged:: 3.2 Added the ``comment`` kwarg and attribute. .. versionchanged:: 3.2 Added the ``.inner_key`` attribute holding a reference to the 'real' key instance this key is a proxy for, if one was obtainable, else None. roc Cs|||_||_||_t|}||_tt|_d|_ zt j |j|d|_ Wn,t k rvd}| t||jYnXdS)N)Zkey_type key_bytesz5Unable to derive inner_key for agent key of type {!r})rrrrr'namer __file___logger inner_keyr Zfrom_type_stringr logrformat)rrrrr8errrrrrs   zAgentKey.__init__cOs|jj||Sr\)rr)rargskwargsrrrrsz AgentKey.logcCs|jr|jS|jSr\)rr rrrrrr szAgentKey.asbytescCs|jSr\)rrrrrget_nameszAgentKey.get_namecs|jdk r|jStSr\)rget_bitssuperr __class__rrrs  zAgentKey.get_bitscCs|jdkrt|t|j|S)zO Proxy any un-implemented methods/properties to the inner_key. N)rAttributeErrorgetattr)rrrrr __getattr__s zAgentKey.__getattr__cCs ||jg}|jr|jjS|Sr\)rrr_fields)rfallbackrrrrszAgentKey._fieldsNcCsbt}|t|||||t|d|j |\}}|t krZt d| S)Nrzkey cannot be used for signing) rZadd_bytecSSH2_AGENTC_SIGN_REQUESTZ add_stringr Zadd_intALGORITHM_FLAG_MAPgetrr SSH2_AGENT_SIGN_RESPONSErr&)rr9 algorithmr8r*r+rrr sign_ssh_datas  zAgentKey.sign_ssh_data)ro)N)r>r?r@rZrrr rrrpropertyrr __classcell__rrrrr%s   r%)0rZrTr^r2rmrCrVr|rloggingrrZparamiko.commonrrZparamiko.ssh_exceptionrrZparamiko.messagerZ paramiko.pkeyr r Z paramiko.utilr r r!r"rrZSSH_AGENT_RSA_SHA2_256ZSSH_AGENT_RSA_SHA2_512rlistitemskeyvaluerrDrAr[rfrprqrzrrr%rrrrsH   =; +='"