2016-04-22 12:08:02 -04:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2019-04-10 02:23:48 -04:00
|
|
|
# Copyright 2019 New Vector Ltd.
|
2016-04-22 12:08:02 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2021-08-16 13:11:48 -04:00
|
|
|
import inspect
|
2019-04-10 02:23:48 -04:00
|
|
|
import sys
|
|
|
|
import traceback
|
2021-09-10 12:03:18 -04:00
|
|
|
from typing import Any, Dict, Optional
|
2016-04-22 12:08:02 -04:00
|
|
|
|
|
|
|
from twisted.conch import manhole_ssh
|
2018-07-09 02:09:20 -04:00
|
|
|
from twisted.conch.insults import insults
|
2019-04-10 02:23:48 -04:00
|
|
|
from twisted.conch.manhole import ColoredManhole, ManholeInterpreter
|
2016-04-25 12:34:25 -04:00
|
|
|
from twisted.conch.ssh.keys import Key
|
2018-07-09 02:09:20 -04:00
|
|
|
from twisted.cred import checkers, portal
|
2021-08-16 13:11:48 -04:00
|
|
|
from twisted.internet import defer
|
2021-11-10 15:06:54 -05:00
|
|
|
from twisted.internet.protocol import ServerFactory
|
2021-09-10 12:03:18 -04:00
|
|
|
|
|
|
|
from synapse.config.server import ManholeConfig
|
2016-04-25 12:34:25 -04:00
|
|
|
|
|
|
|
PUBLIC_KEY = (
|
2018-09-11 05:42:10 -04:00
|
|
|
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHhGATaW4KhE23+7nrH4jFx3yLq9OjaEs5"
|
|
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1
|
|
|
|
CkZcXwiv7Nog7AI9qKJEUXLjoqL+vJskBzSOqU3tcd670YQMi1aXSXJqYE202K7o
|
|
|
|
EddTrx3TNpr1D5m/f+6mnXWrc8u9y1+GNx9yz889xMjIBTBI9KqaaOs=
|
2016-04-25 12:34:25 -04:00
|
|
|
-----END RSA PRIVATE KEY-----"""
|
2016-04-22 12:08:02 -04:00
|
|
|
|
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
def manhole(settings: ManholeConfig, globals: Dict[str, Any]) -> ServerFactory:
|
2016-04-22 12:08:02 -04:00
|
|
|
"""Starts a ssh listener with password authentication using
|
|
|
|
the given username and password. Clients connecting to the ssh
|
|
|
|
listener will find themselves in a colored python shell with
|
|
|
|
the supplied globals.
|
|
|
|
|
|
|
|
Args:
|
2021-09-10 12:03:18 -04:00
|
|
|
username: The username ssh clients should auth with.
|
|
|
|
password: The password ssh clients should auth with.
|
|
|
|
globals: The variables to expose in the shell.
|
2016-04-25 09:59:21 -04:00
|
|
|
|
|
|
|
Returns:
|
2021-09-10 12:03:18 -04:00
|
|
|
A factory to pass to ``listenTCP``
|
2016-04-22 12:08:02 -04:00
|
|
|
"""
|
2021-09-06 11:08:03 -04:00
|
|
|
username = settings.username
|
2021-09-10 12:03:18 -04:00
|
|
|
password = settings.password.encode("ascii")
|
2021-09-06 11:08:03 -04:00
|
|
|
priv_key = settings.priv_key
|
|
|
|
if priv_key is None:
|
|
|
|
priv_key = Key.fromString(PRIVATE_KEY)
|
|
|
|
pub_key = settings.pub_key
|
|
|
|
if pub_key is None:
|
|
|
|
pub_key = Key.fromString(PUBLIC_KEY)
|
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(**{username: password})
|
2016-04-22 12:08:02 -04:00
|
|
|
|
|
|
|
rlm = manhole_ssh.TerminalRealm()
|
2021-09-10 12:03:18 -04:00
|
|
|
# mypy ignored here because:
|
|
|
|
# - can't deduce types of lambdas
|
|
|
|
# - variable is Type[ServerProtocol], expr is Callable[[], ServerProtocol]
|
|
|
|
rlm.chainedProtocolFactory = lambda: insults.ServerProtocol( # type: ignore[misc,assignment]
|
2019-06-20 05:32:02 -04:00
|
|
|
SynapseManhole, dict(globals, __name__="__console__")
|
2016-04-22 12:08:02 -04:00
|
|
|
)
|
|
|
|
|
2023-08-15 12:07:13 -04:00
|
|
|
# type-ignore: This is an error in Twisted's annotations. See
|
|
|
|
# https://github.com/twisted/twisted/issues/11812 and /11813 .
|
|
|
|
factory = manhole_ssh.ConchFactory(portal.Portal(rlm, [checker])) # type: ignore[arg-type]
|
2021-09-10 12:03:18 -04:00
|
|
|
|
|
|
|
# conch has the wrong type on these dicts (says bytes to bytes,
|
|
|
|
# should be bytes to Keys judging by how it's used).
|
|
|
|
factory.privateKeys[b"ssh-rsa"] = priv_key # type: ignore[assignment]
|
|
|
|
factory.publicKeys[b"ssh-rsa"] = pub_key # type: ignore[assignment]
|
2016-04-25 12:34:25 -04:00
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
# ConchFactory is a Factory, not a ServerFactory, but they are identical.
|
|
|
|
return factory # type: ignore[return-value]
|
2019-04-10 02:23:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
class SynapseManhole(ColoredManhole):
|
|
|
|
"""Overrides connectionMade to create our own ManholeInterpreter"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2021-09-10 12:03:18 -04:00
|
|
|
def connectionMade(self) -> None:
|
2020-09-18 09:56:44 -04:00
|
|
|
super().connectionMade()
|
2019-04-10 02:23:48 -04:00
|
|
|
|
|
|
|
# replace the manhole interpreter with our own impl
|
|
|
|
self.interpreter = SynapseManholeInterpreter(self, self.namespace)
|
|
|
|
|
|
|
|
# this would also be a good place to add more keyHandlers.
|
|
|
|
|
|
|
|
|
|
|
|
class SynapseManholeInterpreter(ManholeInterpreter):
|
2021-09-10 12:03:18 -04:00
|
|
|
def showsyntaxerror(self, filename: Optional[str] = None) -> None:
|
2019-04-10 02:23:48 -04:00
|
|
|
"""Display the syntax error that just occurred.
|
|
|
|
|
|
|
|
Overrides the base implementation, ignoring sys.excepthook. We always want
|
|
|
|
any syntax errors to be sent to the terminal, rather than sentry.
|
|
|
|
"""
|
|
|
|
type, value, tb = sys.exc_info()
|
2021-09-10 12:03:18 -04:00
|
|
|
assert value is not None
|
2019-04-10 02:23:48 -04:00
|
|
|
sys.last_type = type
|
|
|
|
sys.last_value = value
|
|
|
|
sys.last_traceback = tb
|
|
|
|
if filename and type is SyntaxError:
|
|
|
|
# Work hard to stuff the correct filename in the exception
|
|
|
|
try:
|
|
|
|
msg, (dummy_filename, lineno, offset, line) = value.args
|
|
|
|
except ValueError:
|
|
|
|
# Not the format we expect; leave it alone
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# Stuff in the right filename
|
|
|
|
value = SyntaxError(msg, (filename, lineno, offset, line))
|
|
|
|
sys.last_value = value
|
|
|
|
lines = traceback.format_exception_only(type, value)
|
2019-06-20 05:32:02 -04:00
|
|
|
self.write("".join(lines))
|
2019-04-10 02:23:48 -04:00
|
|
|
|
2021-09-10 12:03:18 -04:00
|
|
|
def showtraceback(self) -> None:
|
2019-04-10 02:23:48 -04:00
|
|
|
"""Display the exception that just occurred.
|
|
|
|
|
|
|
|
Overrides the base implementation, ignoring sys.excepthook. We always want
|
|
|
|
any syntax errors to be sent to the terminal, rather than sentry.
|
|
|
|
"""
|
|
|
|
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
|
|
|
|
sys.last_traceback = last_tb
|
2021-09-10 12:03:18 -04:00
|
|
|
assert last_tb is not None
|
|
|
|
|
2019-04-10 02:23:48 -04:00
|
|
|
try:
|
|
|
|
# We remove the first stack item because it is our own code.
|
|
|
|
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
|
2019-06-20 05:32:02 -04:00
|
|
|
self.write("".join(lines))
|
2019-04-10 02:23:48 -04:00
|
|
|
finally:
|
2021-09-10 12:03:18 -04:00
|
|
|
# On the line below, last_tb and ei appear to be dead.
|
|
|
|
# It's unclear whether there is a reason behind this line.
|
|
|
|
# It conceivably could be because an exception raised in this block
|
|
|
|
# will keep the local frame (containing these local variables) around.
|
|
|
|
# This was adapted taken from CPython's Lib/code.py; see here:
|
|
|
|
# https://github.com/python/cpython/blob/4dc4300c686f543d504ab6fa9fe600eaf11bb695/Lib/code.py#L131-L150
|
|
|
|
last_tb = ei = None # type: ignore
|
|
|
|
|
|
|
|
def displayhook(self, obj: Any) -> None:
|
2021-08-16 13:11:48 -04:00
|
|
|
"""
|
|
|
|
We override the displayhook so that we automatically convert coroutines
|
|
|
|
into Deferreds. (Our superclass' displayhook will take care of the rest,
|
|
|
|
by displaying the Deferred if it's ready, or registering a callback
|
|
|
|
if it's not).
|
|
|
|
"""
|
|
|
|
if inspect.iscoroutine(obj):
|
|
|
|
super().displayhook(defer.ensureDeferred(obj))
|
|
|
|
else:
|
|
|
|
super().displayhook(obj)
|