agent.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- test-case-name: twisted.conch.test.test_default -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Accesses the key agent for user authentication.
  6. Maintainer: Paul Swartz
  7. """
  8. import os
  9. from twisted.conch.ssh import agent, channel, keys
  10. from twisted.internet import protocol, reactor
  11. from twisted.python import log
  12. class SSHAgentClient(agent.SSHAgentClient):
  13. def __init__(self):
  14. agent.SSHAgentClient.__init__(self)
  15. self.blobs = []
  16. def getPublicKeys(self):
  17. return self.requestIdentities().addCallback(self._cbPublicKeys)
  18. def _cbPublicKeys(self, blobcomm):
  19. log.msg('got %i public keys' % len(blobcomm))
  20. self.blobs = [x[0] for x in blobcomm]
  21. def getPublicKey(self):
  22. """
  23. Return a L{Key} from the first blob in C{self.blobs}, if any, or
  24. return L{None}.
  25. """
  26. if self.blobs:
  27. return keys.Key.fromString(self.blobs.pop(0))
  28. return None
  29. class SSHAgentForwardingChannel(channel.SSHChannel):
  30. def channelOpen(self, specificData):
  31. cc = protocol.ClientCreator(reactor, SSHAgentForwardingLocal)
  32. d = cc.connectUNIX(os.environ['SSH_AUTH_SOCK'])
  33. d.addCallback(self._cbGotLocal)
  34. d.addErrback(lambda x:self.loseConnection())
  35. self.buf = ''
  36. def _cbGotLocal(self, local):
  37. self.local = local
  38. self.dataReceived = self.local.transport.write
  39. self.local.dataReceived = self.write
  40. def dataReceived(self, data):
  41. self.buf += data
  42. def closed(self):
  43. if self.local:
  44. self.local.loseConnection()
  45. self.local = None
  46. class SSHAgentForwardingLocal(protocol.Protocol):
  47. pass