root/trunk/twisted/conch/manhole_tap.py

Revision 32723, 4.2 KB (checked in by teratorn, 8 months ago)

Merge remove-opt-user-5283: --user option to `twistd manhole' was dead code

Author: teratorn
Reviewer: exarkun, glyph
Fixes: #5283

Line 
1# Copyright (c) Twisted Matrix Laboratories.
2# See LICENSE for details.
3
4"""
5TAP plugin for creating telnet- and ssh-accessible manhole servers.
6
7@author: Jp Calderone
8"""
9
10from zope.interface import implements
11
12from twisted.internet import protocol
13from twisted.application import service, strports
14from twisted.conch.ssh import session
15from twisted.conch import interfaces as iconch
16from twisted.cred import portal, checkers
17from twisted.python import usage
18
19from twisted.conch.insults import insults
20from twisted.conch import manhole, manhole_ssh, telnet
21
22class 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
31class chainedProtocolFactory:
32    def __init__(self, namespace):
33        self.namespace = namespace
34   
35    def __call__(self):
36        return insults.ServerProtocol(manhole.ColoredManhole, self.namespace)
37
38class _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
53class 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
67def 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
Note: See TracBrowser for help on using the browser.