aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/manhole.py
blob: 2e20084bfd1a6ea69573e8b4bcb389b6192e49e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# This file is part of Buildbot.  Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

from __future__ import with_statement


import os
import types
import binascii
import base64
from twisted.python import log
from twisted.application import service, strports
from twisted.cred import checkers, portal
from twisted.conch import manhole, telnet
try:
    from twisted.conch import checkers as conchc, manhole_ssh
    _hush_pyflakes = [manhole_ssh, conchc]
    del _hush_pyflakes
except ImportError:
    manhole_ssh = None
    conchc = None
from twisted.conch.insults import insults
from twisted.internet import protocol

from buildbot import config
from buildbot.util import ComparableMixin
from zope.interface import implements # requires Twisted-2.0 or later

# makeTelnetProtocol and _TelnetRealm are for the TelnetManhole

class makeTelnetProtocol:
    # this curries the 'portal' argument into a later call to
    # TelnetTransport()
    def __init__(self, portal):
        self.portal = portal

    def __call__(self):
        auth = telnet.AuthenticatingTelnetProtocol
        return telnet.TelnetTransport(auth, self.portal)

class _TelnetRealm:
    implements(portal.IRealm)

    def __init__(self, namespace_maker):
        self.namespace_maker = namespace_maker

    def requestAvatar(self, avatarId, *interfaces):
        if telnet.ITelnetProtocol in interfaces:
            namespace = self.namespace_maker()
            p = telnet.TelnetBootstrapProtocol(insults.ServerProtocol,
                                               manhole.ColoredManhole,
                                               namespace)
            return (telnet.ITelnetProtocol, p, lambda: None)
        raise NotImplementedError()


class chainedProtocolFactory:
    # this curries the 'namespace' argument into a later call to
    # chainedProtocolFactory()
    def __init__(self, namespace):
        self.namespace = namespace
    
    def __call__(self):
        return insults.ServerProtocol(manhole.ColoredManhole, self.namespace)

if conchc:
    class AuthorizedKeysChecker(conchc.SSHPublicKeyDatabase):
        """Accept connections using SSH keys from a given file.

        SSHPublicKeyDatabase takes the username that the prospective client has
        requested and attempts to get a ~/.ssh/authorized_keys file for that
        username. This requires root access, so it isn't as useful as you'd
        like.

        Instead, this subclass looks for keys in a single file, given as an
        argument. This file is typically kept in the buildmaster's basedir. The
        file should have 'ssh-dss ....' lines in it, just like authorized_keys.
        """

        def __init__(self, authorized_keys_file):
            self.authorized_keys_file = os.path.expanduser(authorized_keys_file)

        def checkKey(self, credentials):
            with open(self.authorized_keys_file) as f:
                for l in f.readlines():
                    l2 = l.split()
                    if len(l2) < 2:
                        continue
                    try:
                        if base64.decodestring(l2[1]) == credentials.blob:
                            return 1
                    except binascii.Error:
                        continue
            return 0


class _BaseManhole(service.MultiService):
    """This provides remote access to a python interpreter (a read/exec/print
    loop) embedded in the buildmaster via an internal SSH server. This allows
    detailed inspection of the buildmaster state. It is of most use to
    buildbot developers. Connect to this by running an ssh client.
    """

    def __init__(self, port, checker, using_ssh=True):
        """
        @type port: string or int
        @param port: what port should the Manhole listen on? This is a
        strports specification string, like 'tcp:12345' or
        'tcp:12345:interface=127.0.0.1'. Bare integers are treated as a
        simple tcp port.

        @type checker: an object providing the
        L{twisted.cred.checkers.ICredentialsChecker} interface
        @param checker: if provided, this checker is used to authenticate the
        client instead of using the username/password scheme. You must either
        provide a username/password or a Checker. Some useful values are::
            import twisted.cred.checkers as credc
            import twisted.conch.checkers as conchc
            c = credc.AllowAnonymousAccess # completely open
            c = credc.FilePasswordDB(passwd_filename) # file of name:passwd
            c = conchc.UNIXPasswordDatabase # getpwnam() (probably /etc/passwd)

        @type using_ssh: bool
        @param using_ssh: If True, accept SSH connections. If False, accept
                          regular unencrypted telnet connections.
        """

        # unfortunately, these don't work unless we're running as root
        #c = credc.PluggableAuthenticationModulesChecker: PAM
        #c = conchc.SSHPublicKeyDatabase() # ~/.ssh/authorized_keys
        # and I can't get UNIXPasswordDatabase to work

        service.MultiService.__init__(self)
        if type(port) is int:
            port = "tcp:%d" % port
        self.port = port # for comparison later
        self.checker = checker # to maybe compare later

        def makeNamespace():
            master = self.master
            namespace = {
                'master': master,
                'status': master.getStatus(),
                'show': show,
                }
            return namespace

        def makeProtocol():
            namespace = makeNamespace()
            p = insults.ServerProtocol(manhole.ColoredManhole, namespace)
            return p

        self.using_ssh = using_ssh
        if using_ssh:
            r = manhole_ssh.TerminalRealm()
            r.chainedProtocolFactory = makeProtocol
            p = portal.Portal(r, [self.checker])
            f = manhole_ssh.ConchFactory(p)
        else:
            r = _TelnetRealm(makeNamespace)
            p = portal.Portal(r, [self.checker])
            f = protocol.ServerFactory()
            f.protocol = makeTelnetProtocol(p)
        s = strports.service(self.port, f)
        s.setServiceParent(self)


    def startService(self):
        service.MultiService.startService(self)
        if self.using_ssh:
            via = "via SSH"
        else:
            via = "via telnet"
        log.msg("Manhole listening %s on port %s" % (via, self.port))


class TelnetManhole(_BaseManhole, ComparableMixin):
    """This Manhole accepts unencrypted (telnet) connections, and requires a
    username and password authorize access. You are encouraged to use the
    encrypted ssh-based manhole classes instead."""

    compare_attrs = ["port", "username", "password"]

    def __init__(self, port, username, password):
        """
        @type port: string or int
        @param port: what port should the Manhole listen on? This is a
        strports specification string, like 'tcp:12345' or
        'tcp:12345:interface=127.0.0.1'. Bare integers are treated as a
        simple tcp port.

        @param username:
        @param password: username= and password= form a pair of strings to
                         use when authenticating the remote user.
        """

        self.username = username
        self.password = password

        c = checkers.InMemoryUsernamePasswordDatabaseDontUse()
        c.addUser(username, password)

        _BaseManhole.__init__(self, port, c, using_ssh=False)

class PasswordManhole(_BaseManhole, ComparableMixin):
    """This Manhole accepts encrypted (ssh) connections, and requires a
    username and password to authorize access.
    """

    compare_attrs = ["port", "username", "password"]

    def __init__(self, port, username, password):
        """
        @type port: string or int
        @param port: what port should the Manhole listen on? This is a
        strports specification string, like 'tcp:12345' or
        'tcp:12345:interface=127.0.0.1'. Bare integers are treated as a
        simple tcp port.

        @param username:
        @param password: username= and password= form a pair of strings to
                         use when authenticating the remote user.
        """

        if not manhole_ssh:
            config.error("pycrypto required for ssh mahole.")
        self.username = username
        self.password = password

        c = checkers.InMemoryUsernamePasswordDatabaseDontUse()
        c.addUser(username, password)

        _BaseManhole.__init__(self, port, c)

class AuthorizedKeysManhole(_BaseManhole, ComparableMixin):
    """This Manhole accepts ssh connections, and requires that the
    prospective client have an ssh private key that matches one of the public
    keys in our authorized_keys file. It is created with the name of a file
    that contains the public keys that we will accept."""

    compare_attrs = ["port", "keyfile"]

    def __init__(self, port, keyfile):
        """
        @type port: string or int
        @param port: what port should the Manhole listen on? This is a
        strports specification string, like 'tcp:12345' or
        'tcp:12345:interface=127.0.0.1'. Bare integers are treated as a
        simple tcp port.

        @param keyfile: the name of a file (relative to the buildmaster's
                        basedir) that contains SSH public keys of authorized
                        users, one per line. This is the exact same format
                        as used by sshd in ~/.ssh/authorized_keys .
        """

        if not manhole_ssh:
            config.error("pycrypto required for ssh mahole.")

        # TODO: expanduser this, and make it relative to the buildmaster's
        # basedir
        self.keyfile = keyfile
        c = AuthorizedKeysChecker(keyfile)
        _BaseManhole.__init__(self, port, c)

class ArbitraryCheckerManhole(_BaseManhole, ComparableMixin):
    """This Manhole accepts ssh connections, but uses an arbitrary
    user-supplied 'checker' object to perform authentication."""

    compare_attrs = ["port", "checker"]

    def __init__(self, port, checker):
        """
        @type port: string or int
        @param port: what port should the Manhole listen on? This is a
        strports specification string, like 'tcp:12345' or
        'tcp:12345:interface=127.0.0.1'. Bare integers are treated as a
        simple tcp port.

        @param checker: an instance of a twisted.cred 'checker' which will
                        perform authentication
        """

        if not manhole_ssh:
            config.error("pycrypto required for ssh mahole.")

        _BaseManhole.__init__(self, port, checker)

## utility functions for the manhole

def show(x):
    """Display the data attributes of an object in a readable format"""
    print "data attributes of %r" % (x,)
    names = dir(x)
    maxlen = max([0] + [len(n) for n in names])
    for k in names:
        v = getattr(x,k)
        t = type(v)
        if t == types.MethodType: continue
        if k[:2] == '__' and k[-2:] == '__': continue
        if t is types.StringType or t is types.UnicodeType:
            if len(v) > 80 - maxlen - 5:
                v = `v[:80 - maxlen - 5]` + "..."
        elif t in (types.IntType, types.NoneType):
            v = str(v)
        elif v in (types.ListType, types.TupleType, types.DictType):
            v = "%s (%d elements)" % (v, len(v))
        else:
            v = str(t)
        print "%*s : %s" % (maxlen, k, v)
    return x