1 # -*- test-case-name: twisted.conch.test.test_tap -*-
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
6 Support module for making SSH servers with twistd.
9 from twisted.conch import unix
10 from twisted.conch import checkers as conch_checkers
11 from twisted.conch.openssh_compat import factory
12 from twisted.cred import portal, checkers, strcred
13 from twisted.python import usage
14 from twisted.application import strports
16 from twisted.cred import pamauth
22 class Options(usage.Options, strcred.AuthOptionMixin):
23 synopsis = "[-i <interface>] [-p <port>] [-d <dir>] "
24 longdesc = ("Makes a Conch SSH server. If no authentication methods are "
25 "specified, the default authentication methods are UNIX passwords, "
26 "SSH public keys, and PAM if it is available. If --auth options are "
27 "passed, only the measures specified will be used.")
29 ["interface", "i", "", "local interface to which we listen"],
30 ["port", "p", "tcp:22", "Port on which to listen"],
31 ["data", "d", "/etc", "directory to look for host keys in"],
32 ["moduli", "", None, "directory to look for moduli in "
33 "(if different from --data)"]
35 compData = usage.Completions(
36 optActions={"data": usage.CompleteDirs(descr="data directory"),
37 "moduli": usage.CompleteDirs(descr="moduli directory"),
38 "interface": usage.CompleteNetInterfaces()}
42 def __init__(self, *a, **kw):
43 usage.Options.__init__(self, *a, **kw)
45 # call the default addCheckers (for backwards compatibility) that will
46 # be used if no --auth option is provided - note that conch's
47 # UNIXPasswordDatabase is used, instead of twisted.plugins.cred_unix's
49 super(Options, self).addChecker(conch_checkers.UNIXPasswordDatabase())
50 super(Options, self).addChecker(conch_checkers.SSHPublicKeyDatabase())
51 if pamauth is not None:
52 super(Options, self).addChecker(
53 checkers.PluggableAuthenticationModulesChecker())
56 def addChecker(self, checker):
58 If addChecker is called, clear out the default checkers first
60 self['credCheckers'] = []
61 self['credInterfaces'] = {}
62 super(Options, self).addChecker(checker)
66 def makeService(config):
68 Construct a service for operating a SSH server.
70 @param config: An L{Options} instance specifying server options, including
71 where server keys are stored and what authentication methods to use.
73 @return: An L{IService} provider which contains the requested SSH server.
76 t = factory.OpenSSHFactory()
78 r = unix.UnixSSHRealm()
79 t.portal = portal.Portal(r, config.get('credCheckers', []))
80 t.dataRoot = config['data']
81 t.moduliRoot = config['moduli'] or config['data']
84 if config['interface']:
86 port += ':interface=' + config['interface']
87 return strports.service(port, t)