1717# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
1818
1919"""
20- SSH Agent interface for Unix clients.
20+ SSH Agent interface
2121"""
2222
2323import os
4040SSH2_AGENTC_REQUEST_IDENTITIES , SSH2_AGENT_IDENTITIES_ANSWER , \
4141 SSH2_AGENTC_SIGN_REQUEST , SSH2_AGENT_SIGN_RESPONSE = range (11 , 15 )
4242
43- class AgentSSH (object ):
44- """
45- Client interface for using private keys from an SSH agent running on the
46- local machine. If an SSH agent is running, this class can be used to
47- connect to it and retreive L{PKey} objects which can be used when
48- attempting to authenticate to remote SSH servers.
4943
50- Because the SSH agent protocol uses environment variables and unix-domain
51- sockets, this probably doesn't work on Windows. It does work on most
52- posix platforms though (Linux and MacOS X, for example).
53- """
44+ class AgentSSH (object ):
5445 def __init__ (self ):
5546 self ._conn = None
5647 self ._keys = ()
@@ -61,8 +52,9 @@ def get_keys(self):
6152 no SSH agent was running (or it couldn't be contacted), an empty list
6253 will be returned.
6354
64- @return: a list of keys available on the SSH agent
65- @rtype: tuple of L{AgentKey}
55+ :return:
56+ a tuple of `.AgentKey` objects representing keys available on the
57+ SSH agent
6658 """
6759 return self ._keys
6860
@@ -100,8 +92,11 @@ def _read_all(self, wanted):
10092 result += extra
10193 return result
10294
95+
10396class AgentProxyThread (threading .Thread ):
104- """ Class in charge of communication between two chan """
97+ """
98+ Class in charge of communication between two channels.
99+ """
105100 def __init__ (self , agent ):
106101 threading .Thread .__init__ (self , target = self .run )
107102 self ._agent = agent
@@ -146,6 +141,7 @@ def _close(self):
146141 self .__inr .close ()
147142 self ._agent ._conn .close ()
148143
144+
149145class AgentLocalProxy (AgentProxyThread ):
150146 """
151147 Class to be used when wanting to ask a local SSH Agent being
@@ -155,8 +151,10 @@ def __init__(self, agent):
155151 AgentProxyThread .__init__ (self , agent )
156152
157153 def get_connection (self ):
158- """ Return a pair of socket object and string address
159- May Block !
154+ """
155+ Return a pair of socket object and string address.
156+
157+ May block!
160158 """
161159 conn = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
162160 try :
@@ -168,6 +166,7 @@ def get_connection(self):
168166 raise
169167 return None
170168
169+
171170class AgentRemoteProxy (AgentProxyThread ):
172171 """
173172 Class to be used when wanting to ask a remote SSH Agent
@@ -177,22 +176,20 @@ def __init__(self, agent, chan):
177176 self .__chan = chan
178177
179178 def get_connection (self ):
180- """
181- Class to be used when wanting to ask a local SSH Agent being
182- asked from a remote fake agent (so use a unix socket for ex.)
183- """
184179 return (self .__chan , None )
185180
181+
186182class AgentClientProxy (object ):
187183 """
188184 Class proxying request as a client:
189- -> client ask for a request_forward_agent()
190- -> server creates a proxy and a fake SSH Agent
191- -> server ask for establishing a connection when needed,
185+
186+ #. client ask for a request_forward_agent()
187+ #. server creates a proxy and a fake SSH Agent
188+ #. server ask for establishing a connection when needed,
192189 calling the forward_agent_handler at client side.
193- -> the forward_agent_handler launch a thread for connecting
190+ #. the forward_agent_handler launch a thread for connecting
194191 the remote fake agent and the local agent
195- -> Communication occurs ...
192+ #. Communication occurs ...
196193 """
197194 def __init__ (self , chanRemote ):
198195 self ._conn = None
@@ -205,7 +202,7 @@ def __del__(self):
205202
206203 def connect (self ):
207204 """
208- Method automatically called by the run() method of the AgentProxyThread
205+ Method automatically called by `` AgentProxyThread.run``.
209206 """
210207 if ('SSH_AUTH_SOCK' in os .environ ) and (sys .platform != 'win32' ):
211208 conn = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
@@ -236,11 +233,12 @@ def close(self):
236233 if self ._conn is not None :
237234 self ._conn .close ()
238235
236+
239237class AgentServerProxy (AgentSSH ):
240238 """
241- @ param t : transport used for the Forward for SSH Agent communication
239+ : param .Transport t: Transport used for SSH Agent communication forwarding
242240
243- @raise SSHException: mostly if we lost the agent
241+ :raises SSHException: mostly if we lost the agent
244242 """
245243 def __init__ (self , t ):
246244 AgentSSH .__init__ (self )
@@ -276,8 +274,8 @@ def get_env(self):
276274 """
277275 Helper for the environnement under unix
278276
279- @ return: the SSH_AUTH_SOCK Environnement variables
280- @rtype: dict
277+ : return:
278+ a dict containing the ``SSH_AUTH_SOCK`` environnement variables
281279 """
282280 env = {}
283281 env ['SSH_AUTH_SOCK' ] = self ._get_filename ()
@@ -286,6 +284,7 @@ def get_env(self):
286284 def _get_filename (self ):
287285 return self ._file
288286
287+
289288class AgentRequestHandler (object ):
290289 def __init__ (self , chanClient ):
291290 self ._conn = None
@@ -303,27 +302,22 @@ def close(self):
303302 for p in self .__clientProxys :
304303 p .close ()
305304
305+
306306class Agent (AgentSSH ):
307307 """
308308 Client interface for using private keys from an SSH agent running on the
309309 local machine. If an SSH agent is running, this class can be used to
310- connect to it and retreive L{ PKey} objects which can be used when
310+ connect to it and retreive `. PKey` objects which can be used when
311311 attempting to authenticate to remote SSH servers.
312312
313- Because the SSH agent protocol uses environment variables and unix-domain
314- sockets, this probably doesn't work on Windows. It does work on most
315- posix platforms though (Linux and MacOS X, for example).
316- """
313+ Upon initialization, a session with the local machine's SSH agent is
314+ opened, if one is running. If no agent is running, initialization will
315+ succeed, but `get_keys` will return an empty tuple.
317316
317+ :raises SSHException:
318+ if an SSH agent is found, but speaks an incompatible protocol
319+ """
318320 def __init__ (self ):
319- """
320- Open a session with the local machine's SSH agent, if one is running.
321- If no agent is running, initialization will succeed, but L{get_keys}
322- will return an empty tuple.
323-
324- @raise SSHException: if an SSH agent is found, but speaks an
325- incompatible protocol
326- """
327321 AgentSSH .__init__ (self )
328322
329323 if ('SSH_AUTH_SOCK' in os .environ ) and (sys .platform != 'win32' ):
@@ -350,13 +344,13 @@ def close(self):
350344 """
351345 self ._close ()
352346
347+
353348class AgentKey (PKey ):
354349 """
355350 Private key held in a local SSH agent. This type of key can be used for
356351 authenticating to a remote server (signing). Most other key operations
357352 work as expected.
358353 """
359-
360354 def __init__ (self , agent , blob ):
361355 self .agent = agent
362356 self .blob = blob
0 commit comments