diff options
author | Jeff Forcier <jeff@bitprophet.org> | 2018-09-17 18:48:29 -0700 |
---|---|---|
committer | Jeff Forcier <jeff@bitprophet.org> | 2018-09-17 18:48:29 -0700 |
commit | 30d49449e38cb10a254983605d576d48a76a387b (patch) | |
tree | 4b1231b1a8a33c73659ef178878f3016c523b4c3 /paramiko/agent.py | |
parent | 75e6c377089c949df19b497fd9ba408ca82628c2 (diff) | |
parent | dc82971c1af0d147433c948dc440ffabdc276a7c (diff) |
Merge branch '2.2' into 2.3
Diffstat (limited to 'paramiko/agent.py')
-rw-r--r-- | paramiko/agent.py | 56 |
1 files changed, 32 insertions, 24 deletions
diff --git a/paramiko/agent.py b/paramiko/agent.py index 7a4dde21..0630ebf3 100644 --- a/paramiko/agent.py +++ b/paramiko/agent.py @@ -43,7 +43,6 @@ cSSH2_AGENTC_SIGN_REQUEST = byte_chr(13) SSH2_AGENT_SIGN_RESPONSE = 14 - class AgentSSH(object): def __init__(self): self._conn = None @@ -65,7 +64,7 @@ class AgentSSH(object): self._conn = conn ptype, result = self._send_message(cSSH2_AGENTC_REQUEST_IDENTITIES) if ptype != SSH2_AGENT_IDENTITIES_ANSWER: - raise SSHException('could not get keys from ssh-agent') + raise SSHException("could not get keys from ssh-agent") keys = [] for i in range(result.get_int()): keys.append(AgentKey(self, result.get_binary())) @@ -80,19 +79,19 @@ class AgentSSH(object): def _send_message(self, msg): msg = asbytes(msg) - self._conn.send(struct.pack('>I', len(msg)) + msg) + self._conn.send(struct.pack(">I", len(msg)) + msg) l = self._read_all(4) - msg = Message(self._read_all(struct.unpack('>I', l)[0])) + msg = Message(self._read_all(struct.unpack(">I", l)[0])) return ord(msg.get_byte()), msg def _read_all(self, wanted): result = self._conn.recv(wanted) while len(result) < wanted: if len(result) == 0: - raise SSHException('lost ssh-agent') + raise SSHException("lost ssh-agent") extra = self._conn.recv(wanted - len(result)) if len(extra) == 0: - raise SSHException('lost ssh-agent') + raise SSHException("lost ssh-agent") result += extra return result @@ -101,6 +100,7 @@ class AgentProxyThread(threading.Thread): """ Class in charge of communication between two channels. """ + def __init__(self, agent): threading.Thread.__init__(self, target=self.run) self._agent = agent @@ -115,12 +115,9 @@ class AgentProxyThread(threading.Thread): # The address should be an IP address as a string? or None self.__addr = addr self._agent.connect() - if ( - not isinstance(self._agent, int) and - ( - self._agent._conn is None or - not hasattr(self._agent._conn, 'fileno') - ) + if not isinstance(self._agent, int) and ( + self._agent._conn is None + or not hasattr(self._agent._conn, "fileno") ): raise AuthenticationException("Unable to connect to SSH agent") self._communicate() @@ -130,6 +127,7 @@ class AgentProxyThread(threading.Thread): def _communicate(self): import fcntl + oldflags = fcntl.fcntl(self.__inr, fcntl.F_GETFL) fcntl.fcntl(self.__inr, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) while not self._exit: @@ -162,6 +160,7 @@ class AgentLocalProxy(AgentProxyThread): 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.) """ + def __init__(self, agent): AgentProxyThread.__init__(self, agent) @@ -185,6 +184,7 @@ class AgentRemoteProxy(AgentProxyThread): """ Class to be used when wanting to ask a remote SSH Agent """ + def __init__(self, agent, chan): AgentProxyThread.__init__(self, agent) self.__chan = chan @@ -205,6 +205,7 @@ class AgentClientProxy(object): the remote fake agent and the local agent #. Communication occurs ... """ + def __init__(self, chanRemote): self._conn = None self.__chanR = chanRemote @@ -218,16 +219,18 @@ class AgentClientProxy(object): """ Method automatically called by ``AgentProxyThread.run``. """ - if ('SSH_AUTH_SOCK' in os.environ) and (sys.platform != 'win32'): + if ("SSH_AUTH_SOCK" in os.environ) and (sys.platform != "win32"): conn = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: retry_on_signal( - lambda: conn.connect(os.environ['SSH_AUTH_SOCK'])) + lambda: conn.connect(os.environ["SSH_AUTH_SOCK"]) + ) except: # probably a dangling env var: the ssh agent is gone return - elif sys.platform == 'win32': + elif sys.platform == "win32": import paramiko.win_pageant as win_pageant + if win_pageant.can_talk_to_agent(): conn = win_pageant.PageantConnection() else: @@ -255,12 +258,13 @@ class AgentServerProxy(AgentSSH): :raises: `.SSHException` -- mostly if we lost the agent """ + def __init__(self, t): AgentSSH.__init__(self) self.__t = t - self._dir = tempfile.mkdtemp('sshproxy') + self._dir = tempfile.mkdtemp("sshproxy") os.chmod(self._dir, stat.S_IRWXU) - self._file = self._dir + '/sshproxy.ssh' + self._file = self._dir + "/sshproxy.ssh" self.thread = AgentLocalProxy(self) self.thread.start() @@ -270,8 +274,8 @@ class AgentServerProxy(AgentSSH): def connect(self): conn_sock = self.__t.open_forward_agent_channel() if conn_sock is None: - raise SSHException('lost ssh-agent') - conn_sock.set_name('auth-agent') + raise SSHException("lost ssh-agent") + conn_sock.set_name("auth-agent") self._connect(conn_sock) def close(self): @@ -292,7 +296,7 @@ class AgentServerProxy(AgentSSH): :return: a dict containing the ``SSH_AUTH_SOCK`` environnement variables """ - return {'SSH_AUTH_SOCK': self._get_filename()} + return {"SSH_AUTH_SOCK": self._get_filename()} def _get_filename(self): return self._file @@ -319,6 +323,7 @@ class AgentRequestHandler(object): # the remote end. session.exec_command("git clone https://my.git.repository/") """ + def __init__(self, chanClient): self._conn = None self.__chanC = chanClient @@ -350,18 +355,20 @@ class Agent(AgentSSH): :raises: `.SSHException` -- if an SSH agent is found, but speaks an incompatible protocol """ + def __init__(self): AgentSSH.__init__(self) - if ('SSH_AUTH_SOCK' in os.environ) and (sys.platform != 'win32'): + if ("SSH_AUTH_SOCK" in os.environ) and (sys.platform != "win32"): conn = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: - conn.connect(os.environ['SSH_AUTH_SOCK']) + conn.connect(os.environ["SSH_AUTH_SOCK"]) except: # probably a dangling env var: the ssh agent is gone return - elif sys.platform == 'win32': + elif sys.platform == "win32": from . import win_pageant + if win_pageant.can_talk_to_agent(): conn = win_pageant.PageantConnection() else: @@ -384,6 +391,7 @@ class AgentKey(PKey): authenticating to a remote server (signing). Most other key operations work as expected. """ + def __init__(self, agent, blob): self.agent = agent self.blob = blob @@ -407,5 +415,5 @@ class AgentKey(PKey): msg.add_int(0) ptype, result = self.agent._send_message(msg) if ptype != SSH2_AGENT_SIGN_RESPONSE: - raise SSHException('key cannot be used for signing') + raise SSHException("key cannot be used for signing") return result.get_binary() |