You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tap.py 3.1KB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- test-case-name: twisted.conch.test.test_tap -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Support module for making SSH servers with twistd.
  6. """
  7. from twisted.conch import unix
  8. from twisted.conch import checkers as conch_checkers
  9. from twisted.conch.openssh_compat import factory
  10. from twisted.cred import portal, strcred
  11. from twisted.python import usage
  12. from twisted.application import strports
  13. class Options(usage.Options, strcred.AuthOptionMixin):
  14. synopsis = "[-i <interface>] [-p <port>] [-d <dir>] "
  15. longdesc = ("Makes a Conch SSH server. If no authentication methods are "
  16. "specified, the default authentication methods are UNIX passwords "
  17. "and SSH public keys. If --auth options are "
  18. "passed, only the measures specified will be used.")
  19. optParameters = [
  20. ["interface", "i", "", "local interface to which we listen"],
  21. ["port", "p", "tcp:22", "Port on which to listen"],
  22. ["data", "d", "/etc", "directory to look for host keys in"],
  23. ["moduli", "", None, "directory to look for moduli in "
  24. "(if different from --data)"]
  25. ]
  26. compData = usage.Completions(
  27. optActions={"data": usage.CompleteDirs(descr="data directory"),
  28. "moduli": usage.CompleteDirs(descr="moduli directory"),
  29. "interface": usage.CompleteNetInterfaces()}
  30. )
  31. def __init__(self, *a, **kw):
  32. usage.Options.__init__(self, *a, **kw)
  33. # Call the default addCheckers (for backwards compatibility) that will
  34. # be used if no --auth option is provided - note that conch's
  35. # UNIXPasswordDatabase is used, instead of twisted.plugins.cred_unix's
  36. # checker
  37. super(Options, self).addChecker(conch_checkers.UNIXPasswordDatabase())
  38. super(Options, self).addChecker(conch_checkers.SSHPublicKeyChecker(
  39. conch_checkers.UNIXAuthorizedKeysFiles()))
  40. self._usingDefaultAuth = True
  41. def addChecker(self, checker):
  42. """
  43. Add the checker specified. If any checkers are added, the default
  44. checkers are automatically cleared and the only checkers will be the
  45. specified one(s).
  46. """
  47. if self._usingDefaultAuth:
  48. self['credCheckers'] = []
  49. self['credInterfaces'] = {}
  50. self._usingDefaultAuth = False
  51. super(Options, self).addChecker(checker)
  52. def makeService(config):
  53. """
  54. Construct a service for operating a SSH server.
  55. @param config: An L{Options} instance specifying server options, including
  56. where server keys are stored and what authentication methods to use.
  57. @return: A L{twisted.application.service.IService} provider which contains
  58. the requested SSH server.
  59. """
  60. t = factory.OpenSSHFactory()
  61. r = unix.UnixSSHRealm()
  62. t.portal = portal.Portal(r, config.get('credCheckers', []))
  63. t.dataRoot = config['data']
  64. t.moduliRoot = config['moduli'] or config['data']
  65. port = config['port']
  66. if config['interface']:
  67. # Add warning here
  68. port += ':interface=' + config['interface']
  69. return strports.service(port, t)