Mercurial > public > mercurial-scm > hg-stable
diff mercurial/sshpeer.py @ 36015:48a3a9283f09
sshpeer: initial definition and implementation of new SSH protocol
The existing SSH protocol has several design flaws. Future commits
will elaborate on these flaws as new features are introduced
to combat these flaws. For now, hopefully you can take me for my
word that a ground up rewrite of the SSH protocol is needed.
This commit lays the foundation for a new SSH protocol by defining
a mechanism to upgrade the SSH transport channel away from the
default (version 1) protocol to something modern (which we'll call
"version 2" for now).
This upgrade process is detailed in the internals documentation
for the wire protocol. The gist of it is the client sends a
request line preceding the "hello" command/line which basically
says "I'm requesting an upgrade: here's what I support." If the
server recognizes that line, it processes the upgrade request and
the transport channel is switched to use the new version of the
protocol. If not, it sends an empty response, which is how all
Mercurial SSH servers from the beginning of time reacted to unknown
commands. The upgrade request is effectively ignored and the client
continues to use the existing version of the protocol as if nothing
happened.
The new version of the SSH protocol is completely identical to
version 1 aside from the upgrade dance and the bytes that follow.
The immediate bytes that follow the protocol switch are defined to
be a length framed "capabilities: " line containing the remote's
advertised capabilities. In reality, this looks very similar to
what the "hello" response would look like. But it will evolve
quickly.
The methodology by which the protocol will evolve is important.
I'm not going to introduce the new protocol all at once. That would
likely lead to endless bike shedding and forward progress would
stall. Instead, I intend to tricle out new features and diversions
from the existing protocol in small, incremental changes.
To support the gradual evolution of the protocol, the on-the-wire
advertised protocol name contains an "exp" to denote "experimental"
and a 4 digit field to capture the sub-version of the protocol.
Whenever we make a BC change to the wire protocol, we can increment
this version and lock out all older clients because it will appear
as a completely different protocol version. This means we can incur
as many breaking changes as we want. We don't have to commit to
supporting any one feature or idea for a long period of time. We
can even evolve the handshake mechanism, because that is defined
as being an implementation detail of the negotiated protocol version!
Hopefully this lowers the barrier to accepting changes to the
protocol and for experimenting with "radical" ideas during its
development.
In core, sshpeer received most of the attention. We haven't even
implemented the server bits for the new protocol in core yet.
Instead, we add very primitive support to our test server, mainly
just to exercise the added code paths in sshpeer.
Differential Revision: https://phab.mercurial-scm.org/D2061
# no-check-commit because of required foo_bar naming
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Tue, 06 Feb 2018 11:08:36 -0800 |
parents | 556218e08e25 |
children | 625038cb4b1d |
line wrap: on
line diff
--- a/mercurial/sshpeer.py Tue Feb 06 10:51:15 2018 -0800 +++ b/mercurial/sshpeer.py Tue Feb 06 11:08:36 2018 -0800 @@ -8,6 +8,7 @@ from __future__ import absolute_import import re +import uuid from .i18n import _ from . import ( @@ -15,6 +16,7 @@ pycompat, util, wireproto, + wireprotoserver, ) def _serverquote(s): @@ -162,15 +164,24 @@ hint = ui.config('ui', 'ssherrorhint') raise error.RepoError(msg, hint=hint) - # The handshake consists of sending 2 wire protocol commands: - # ``hello`` and ``between``. + # The handshake consists of sending wire protocol commands in reverse + # order of protocol implementation and then sniffing for a response + # to one of them. + # + # Those commands (from oldest to newest) are: # - # The ``hello`` command (which was introduced in Mercurial 0.9.1) - # instructs the server to advertise its capabilities. + # ``between`` + # Asks for the set of revisions between a pair of revisions. Command + # present in all Mercurial server implementations. # - # The ``between`` command (which has existed in all Mercurial servers - # for as long as SSH support has existed), asks for the set of revisions - # between a pair of revisions. + # ``hello`` + # Instructs the server to advertise its capabilities. Introduced in + # Mercurial 0.9.1. + # + # ``upgrade`` + # Requests upgrade from default transport protocol version 1 to + # a newer version. Introduced in Mercurial 4.6 as an experimental + # feature. # # The ``between`` command is issued with a request for the null # range. If the remote is a Mercurial server, this request will @@ -186,6 +197,18 @@ # RFC 822 like lines. Of these, the ``capabilities:`` line contains # the capabilities of the server. # + # The ``upgrade`` command isn't really a command in the traditional + # sense of version 1 of the transport because it isn't using the + # proper mechanism for formatting insteads: instead, it just encodes + # arguments on the line, delimited by spaces. + # + # The ``upgrade`` line looks like ``upgrade <token> <capabilities>``. + # If the server doesn't support protocol upgrades, it will reply to + # this line with ``0\n``. Otherwise, it emits an + # ``upgraded <token> <protocol>`` line to both stdout and stderr. + # Content immediately following this line describes additional + # protocol and server state. + # # In addition to the responses to our command requests, the server # may emit "banner" output on stdout. SSH servers are allowed to # print messages to stdout on login. Issuing commands on connection @@ -195,6 +218,14 @@ requestlog = ui.configbool('devel', 'debug.peer-request') + # Generate a random token to help identify responses to version 2 + # upgrade request. + token = bytes(uuid.uuid4()) + upgradecaps = [ + ('proto', wireprotoserver.SSHV2), + ] + upgradecaps = util.urlreq.urlencode(upgradecaps) + try: pairsarg = '%s-%s' % ('0' * 40, '0' * 40) handshake = [ @@ -204,6 +235,11 @@ pairsarg, ] + # Request upgrade to version 2 if configured. + if ui.configbool('experimental', 'sshpeer.advertise-v2'): + ui.debug('sending upgrade request: %s %s\n' % (token, upgradecaps)) + handshake.insert(0, 'upgrade %s %s\n' % (token, upgradecaps)) + if requestlog: ui.debug('devel-peer-request: hello\n') ui.debug('sending hello command\n') @@ -217,12 +253,31 @@ except IOError: badresponse() + # Assume version 1 of wire protocol by default. + protoname = wireprotoserver.SSHV1 + reupgraded = re.compile(b'^upgraded %s (.*)$' % re.escape(token)) + lines = ['', 'dummy'] max_noise = 500 while lines[-1] and max_noise: try: l = stdout.readline() _forwardoutput(ui, stderr) + + # Look for reply to protocol upgrade request. It has a token + # in it, so there should be no false positives. + m = reupgraded.match(l) + if m: + protoname = m.group(1) + ui.debug('protocol upgraded to %s\n' % protoname) + # If an upgrade was handled, the ``hello`` and ``between`` + # requests are ignored. The next output belongs to the + # protocol, so stop scanning lines. + break + + # Otherwise it could be a banner, ``0\n`` response if server + # doesn't support upgrade. + if lines[-1] == '1\n' and l == '\n': break if l: @@ -235,20 +290,39 @@ badresponse() caps = set() - for l in reversed(lines): - # Look for response to ``hello`` command. Scan from the back so - # we don't misinterpret banner output as the command reply. - if l.startswith('capabilities:'): - caps.update(l[:-1].split(':')[1].split()) - break - # Error if we couldn't find a response to ``hello``. This could - # mean: + # For version 1, we should see a ``capabilities`` line in response to the + # ``hello`` command. + if protoname == wireprotoserver.SSHV1: + for l in reversed(lines): + # Look for response to ``hello`` command. Scan from the back so + # we don't misinterpret banner output as the command reply. + if l.startswith('capabilities:'): + caps.update(l[:-1].split(':')[1].split()) + break + elif protoname == wireprotoserver.SSHV2: + # We see a line with number of bytes to follow and then a value + # looking like ``capabilities: *``. + line = stdout.readline() + try: + valuelen = int(line) + except ValueError: + badresponse() + + capsline = stdout.read(valuelen) + if not capsline.startswith('capabilities: '): + badresponse() + + caps.update(capsline.split(':')[1].split()) + # Trailing newline. + stdout.read(1) + + # Error if we couldn't find capabilities, this means: # # 1. Remote isn't a Mercurial server # 2. Remote is a <0.9.1 Mercurial server # 3. Remote is a future Mercurial server that dropped ``hello`` - # support. + # and other attempted handshake mechanisms. if not caps: badresponse()