~landscape/zope3/newer-from-ztk

« back to all changes in this revision

Viewing changes to src/twisted/conch/manhole_ssh.py

  • Committer: Thomas Hervé
  • Date: 2009-07-08 13:52:04 UTC
  • Revision ID: thomas@canonical.com-20090708135204-df5eesrthifpylf8
Remove twisted copy

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- test-case-name: twisted.conch.test.test_manhole -*-
2
 
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
3
 
# See LICENSE for details.
4
 
 
5
 
"""insults/SSH integration support.
6
 
 
7
 
API Stability: Unstable
8
 
 
9
 
@author: U{Jp Calderone<mailto:exarkun@twistedmatrix.com>}
10
 
"""
11
 
 
12
 
from zope.interface import implements
13
 
 
14
 
from twisted.conch import avatar, interfaces as iconch, error as econch
15
 
from twisted.conch.ssh import factory, keys, session
16
 
from twisted.cred import credentials, checkers, portal
17
 
from twisted.python import components
18
 
 
19
 
from twisted.conch.insults import insults
20
 
 
21
 
class _Glue:
22
 
    """A feeble class for making one attribute look like another.
23
 
 
24
 
    This should be replaced with a real class at some point, probably.
25
 
    Try not to write new code that uses it.
26
 
    """
27
 
    def __init__(self, **kw):
28
 
        self.__dict__.update(kw)
29
 
 
30
 
    def __getattr__(self, name):
31
 
        raise AttributeError(self.name, "has no attribute", name)
32
 
 
33
 
class TerminalSessionTransport:
34
 
    def __init__(self, proto, chainedProtocol, avatar, width, height):
35
 
        self.proto = proto
36
 
        self.avatar = avatar
37
 
        self.chainedProtocol = chainedProtocol
38
 
 
39
 
        session = self.proto.session
40
 
 
41
 
        self.proto.makeConnection(
42
 
            _Glue(write=self.chainedProtocol.dataReceived,
43
 
                  loseConnection=lambda: avatar.conn.sendClose(session),
44
 
                  name="SSH Proto Transport"))
45
 
 
46
 
        def loseConnection():
47
 
            self.proto.loseConnection()
48
 
 
49
 
        self.chainedProtocol.makeConnection(
50
 
            _Glue(write=self.proto.write,
51
 
                  loseConnection=loseConnection,
52
 
                  name="Chained Proto Transport"))
53
 
 
54
 
        # XXX TODO
55
 
        # chainedProtocol is supposed to be an ITerminalTransport,
56
 
        # maybe.  That means perhaps its terminalProtocol attribute is
57
 
        # an ITerminalProtocol, it could be.  So calling terminalSize
58
 
        # on that should do the right thing But it'd be nice to clean
59
 
        # this bit up.
60
 
        self.chainedProtocol.terminalProtocol.terminalSize(width, height)
61
 
 
62
 
class TerminalSession(components.Adapter):
63
 
    implements(iconch.ISession)
64
 
 
65
 
    transportFactory = TerminalSessionTransport
66
 
    chainedProtocolFactory = insults.ServerProtocol
67
 
 
68
 
    def getPty(self, term, windowSize, attrs):
69
 
        self.height, self.width = windowSize[:2]
70
 
 
71
 
    def openShell(self, proto):
72
 
        self.transportFactory(
73
 
            proto, self.chainedProtocolFactory(),
74
 
            iconch.IConchUser(self.original),
75
 
            self.width, self.height)
76
 
 
77
 
    def execCommand(self, proto, cmd):
78
 
        raise econch.ConchError("Cannot execute commands")
79
 
 
80
 
    def closed(self):
81
 
        pass
82
 
 
83
 
class TerminalUser(avatar.ConchUser, components.Adapter):
84
 
    def __init__(self, original, avatarId):
85
 
        components.Adapter.__init__(self, original)
86
 
        avatar.ConchUser.__init__(self)
87
 
        self.channelLookup['session'] = session.SSHSession
88
 
 
89
 
class TerminalRealm:
90
 
    userFactory = TerminalUser
91
 
    sessionFactory = TerminalSession
92
 
 
93
 
    transportFactory = TerminalSessionTransport
94
 
    chainedProtocolFactory = insults.ServerProtocol
95
 
 
96
 
    def _getAvatar(self, avatarId):
97
 
        comp = components.Componentized()
98
 
        user = self.userFactory(comp, avatarId)
99
 
        sess = self.sessionFactory(comp)
100
 
 
101
 
        sess.transportFactory = self.transportFactory
102
 
        sess.chainedProtocolFactory = self.chainedProtocolFactory
103
 
 
104
 
        comp.setComponent(iconch.IConchUser, user)
105
 
        comp.setComponent(iconch.ISession, sess)
106
 
 
107
 
        return user
108
 
 
109
 
    def __init__(self, transportFactory=None):
110
 
        if transportFactory is not None:
111
 
            self.transportFactory = transportFactory
112
 
 
113
 
    def requestAvatar(self, avatarId, mind, *interfaces):
114
 
        for i in interfaces:
115
 
            if i is iconch.IConchUser:
116
 
                return (iconch.IConchUser,
117
 
                        self._getAvatar(avatarId),
118
 
                        lambda: None)
119
 
        raise NotImplementedError()
120
 
 
121
 
class ConchFactory(factory.SSHFactory):
122
 
    publicKey = 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAGEArzJx8OYOnJmzf4tfBEvLi8DVPrJ3/c9k2I/Az64fxjHf9imyRJbixtQhlH9lfNjUIx+4LmrJH5QNRsFporcHDKOTwTTYLh5KmRpslkYHRivcJSkbh/C+BR3utDS555mV'
123
 
 
124
 
    publicKeys = {
125
 
        'ssh-rsa' : keys.getPublicKeyString(data = publicKey)
126
 
    }
127
 
    del publicKey
128
 
 
129
 
    privateKey = """-----BEGIN RSA PRIVATE KEY-----
130
 
MIIByAIBAAJhAK8ycfDmDpyZs3+LXwRLy4vA1T6yd/3PZNiPwM+uH8Yx3/YpskSW
131
 
4sbUIZR/ZXzY1CMfuC5qyR+UDUbBaaK3Bwyjk8E02C4eSpkabJZGB0Yr3CUpG4fw
132
 
vgUd7rQ0ueeZlQIBIwJgbh+1VZfr7WftK5lu7MHtqE1S1vPWZQYE3+VUn8yJADyb
133
 
Z4fsZaCrzW9lkIqXkE3GIY+ojdhZhkO1gbG0118sIgphwSWKRxK0mvh6ERxKqIt1
134
 
xJEJO74EykXZV4oNJ8sjAjEA3J9r2ZghVhGN6V8DnQrTk24Td0E8hU8AcP0FVP+8
135
 
PQm/g/aXf2QQkQT+omdHVEJrAjEAy0pL0EBH6EVS98evDCBtQw22OZT52qXlAwZ2
136
 
gyTriKFVoqjeEjt3SZKKqXHSApP/AjBLpF99zcJJZRq2abgYlf9lv1chkrWqDHUu
137
 
DZttmYJeEfiFBBavVYIF1dOlZT0G8jMCMBc7sOSZodFnAiryP+Qg9otSBjJ3bQML
138
 
pSTqy7c3a2AScC/YyOwkDaICHnnD3XyjMwIxALRzl0tQEKMXs6hH8ToUdlLROCrP
139
 
EhQ0wahUTCk1gKA4uPD6TMTChavbh4K63OvbKg==
140
 
-----END RSA PRIVATE KEY-----"""
141
 
    privateKeys = {
142
 
        'ssh-rsa' : keys.getPrivateKeyObject(data = privateKey)
143
 
    }
144
 
    del privateKey
145
 
 
146
 
    def __init__(self, portal):
147
 
        self.portal = portal