| 1 | # Copyright (c) Twisted Matrix Laboratories. |
|---|
| 2 | # See LICENSE for details. |
|---|
| 3 | |
|---|
| 4 | """ |
|---|
| 5 | TAP plugin for creating telnet- and ssh-accessible manhole servers. |
|---|
| 6 | |
|---|
| 7 | @author: Jp Calderone |
|---|
| 8 | """ |
|---|
| 9 | |
|---|
| 10 | from zope.interface import implements |
|---|
| 11 | |
|---|
| 12 | from twisted.internet import protocol |
|---|
| 13 | from twisted.application import service, strports |
|---|
| 14 | from twisted.conch.ssh import session |
|---|
| 15 | from twisted.conch import interfaces as iconch |
|---|
| 16 | from twisted.cred import portal, checkers |
|---|
| 17 | from twisted.python import usage |
|---|
| 18 | |
|---|
| 19 | from twisted.conch.insults import insults |
|---|
| 20 | from twisted.conch import manhole, manhole_ssh, telnet |
|---|
| 21 | |
|---|
| 22 | class makeTelnetProtocol: |
|---|
| 23 | def __init__(self, portal): |
|---|
| 24 | self.portal = portal |
|---|
| 25 | |
|---|
| 26 | def __call__(self): |
|---|
| 27 | auth = telnet.AuthenticatingTelnetProtocol |
|---|
| 28 | args = (self.portal,) |
|---|
| 29 | return telnet.TelnetTransport(auth, *args) |
|---|
| 30 | |
|---|
| 31 | class chainedProtocolFactory: |
|---|
| 32 | def __init__(self, namespace): |
|---|
| 33 | self.namespace = namespace |
|---|
| 34 | |
|---|
| 35 | def __call__(self): |
|---|
| 36 | return insults.ServerProtocol(manhole.ColoredManhole, self.namespace) |
|---|
| 37 | |
|---|
| 38 | class _StupidRealm: |
|---|
| 39 | implements(portal.IRealm) |
|---|
| 40 | |
|---|
| 41 | def __init__(self, proto, *a, **kw): |
|---|
| 42 | self.protocolFactory = proto |
|---|
| 43 | self.protocolArgs = a |
|---|
| 44 | self.protocolKwArgs = kw |
|---|
| 45 | |
|---|
| 46 | def requestAvatar(self, avatarId, *interfaces): |
|---|
| 47 | if telnet.ITelnetProtocol in interfaces: |
|---|
| 48 | return (telnet.ITelnetProtocol, |
|---|
| 49 | self.protocolFactory(*self.protocolArgs, **self.protocolKwArgs), |
|---|
| 50 | lambda: None) |
|---|
| 51 | raise NotImplementedError() |
|---|
| 52 | |
|---|
| 53 | class Options(usage.Options): |
|---|
| 54 | optParameters = [ |
|---|
| 55 | ["telnetPort", "t", None, "strports description of the address on which to listen for telnet connections"], |
|---|
| 56 | ["sshPort", "s", None, "strports description of the address on which to listen for ssh connections"], |
|---|
| 57 | ["passwd", "p", "/etc/passwd", "name of a passwd(5)-format username/password file"]] |
|---|
| 58 | |
|---|
| 59 | def __init__(self): |
|---|
| 60 | usage.Options.__init__(self) |
|---|
| 61 | self['namespace'] = None |
|---|
| 62 | |
|---|
| 63 | def postOptions(self): |
|---|
| 64 | if self['telnetPort'] is None and self['sshPort'] is None: |
|---|
| 65 | raise usage.UsageError("At least one of --telnetPort and --sshPort must be specified") |
|---|
| 66 | |
|---|
| 67 | def makeService(options): |
|---|
| 68 | """Create a manhole server service. |
|---|
| 69 | |
|---|
| 70 | @type options: C{dict} |
|---|
| 71 | @param options: A mapping describing the configuration of |
|---|
| 72 | the desired service. Recognized key/value pairs are:: |
|---|
| 73 | |
|---|
| 74 | "telnetPort": strports description of the address on which |
|---|
| 75 | to listen for telnet connections. If None, |
|---|
| 76 | no telnet service will be started. |
|---|
| 77 | |
|---|
| 78 | "sshPort": strports description of the address on which to |
|---|
| 79 | listen for ssh connections. If None, no ssh |
|---|
| 80 | service will be started. |
|---|
| 81 | |
|---|
| 82 | "namespace": dictionary containing desired initial locals |
|---|
| 83 | for manhole connections. If None, an empty |
|---|
| 84 | dictionary will be used. |
|---|
| 85 | |
|---|
| 86 | "passwd": Name of a passwd(5)-format username/password file. |
|---|
| 87 | |
|---|
| 88 | @rtype: L{twisted.application.service.IService} |
|---|
| 89 | @return: A manhole service. |
|---|
| 90 | """ |
|---|
| 91 | |
|---|
| 92 | svc = service.MultiService() |
|---|
| 93 | |
|---|
| 94 | namespace = options['namespace'] |
|---|
| 95 | if namespace is None: |
|---|
| 96 | namespace = {} |
|---|
| 97 | |
|---|
| 98 | checker = checkers.FilePasswordDB(options['passwd']) |
|---|
| 99 | |
|---|
| 100 | if options['telnetPort']: |
|---|
| 101 | telnetRealm = _StupidRealm(telnet.TelnetBootstrapProtocol, |
|---|
| 102 | insults.ServerProtocol, |
|---|
| 103 | manhole.ColoredManhole, |
|---|
| 104 | namespace) |
|---|
| 105 | |
|---|
| 106 | telnetPortal = portal.Portal(telnetRealm, [checker]) |
|---|
| 107 | |
|---|
| 108 | telnetFactory = protocol.ServerFactory() |
|---|
| 109 | telnetFactory.protocol = makeTelnetProtocol(telnetPortal) |
|---|
| 110 | telnetService = strports.service(options['telnetPort'], |
|---|
| 111 | telnetFactory) |
|---|
| 112 | telnetService.setServiceParent(svc) |
|---|
| 113 | |
|---|
| 114 | if options['sshPort']: |
|---|
| 115 | sshRealm = manhole_ssh.TerminalRealm() |
|---|
| 116 | sshRealm.chainedProtocolFactory = chainedProtocolFactory(namespace) |
|---|
| 117 | |
|---|
| 118 | sshPortal = portal.Portal(sshRealm, [checker]) |
|---|
| 119 | sshFactory = manhole_ssh.ConchFactory(sshPortal) |
|---|
| 120 | sshService = strports.service(options['sshPort'], |
|---|
| 121 | sshFactory) |
|---|
| 122 | sshService.setServiceParent(svc) |
|---|
| 123 | |
|---|
| 124 | return svc |
|---|