qqt

ssh bridge

The ssh bridge — the functional contract

How a name in an ssh config becomes a dial, how the server crosses the ssh channel, who may listen on a far-side port, and where the certificates live — as numbered, testable requirements.

|hadron|: QQT-010 the bridge grants a client exactly the authority its own ssh login already has: the far-side port it may listen on is decided by the kernel’s bind, the identity it dials is the host key ssh already verified, and no second account, CA distribution, or ACL file is introduced.

A host name in ~/.ssh/config should name the box qqt dials, and a box that can be reached by ssh should be able to run the tunnel with no install step, no certificate to copy, and no daemon to supervise. The bridge is that path: it resolves the name through the operator’s own ssh client, ships the server over the ssh channel it just authenticated, and admits a second user to a running server only when the kernel says that user could have bound the port itself.

The tunnel is not made autonomous. Every new connection passes the ssh gate again — that is the property the whole contract hangs on.

This contract is the mechanism the command surface reaches for, not the surface itself: qqt remote up <box> is the verb, and the mount, gate, and port authority below are what it does. The vocabulary, the forward flags, and the rule that nobody types a certificate are docs/functional_requirements_qqt-cli.md.

1. The gate

id the requirement
SB-1 Authority to use a far-side port is re-established through ssh on every connect. A running server must not admit a client whose ssh authority has lapsed.
SB-2 The re-check must cost no new authentication when the operator’s own multiplexed connection is warm: a ControlMaster exec is the check, not a fresh login.
SB-3 A client certificate must carry a validity window bounded by one configured value, so revocation bites within that window even while an ssh master stays open.

The window is the one place the two facts meet: an open ssh master outlives an authorized_keys edit, and a certificate outlives the connection that minted it. Bounding the certificate makes the ssh master’s lifetime irrelevant to authorization.

2. Name resolution

A typed host name resolves through the ssh client on the box — that client’s own grammar, not one this crate re-implements. The crate still carries a reader for the box that has no client; it is the second link, and it is held to the client’s answers wherever it claims to give one.

id the requirement
SB-4 When an ssh client is present, the name must resolve through its own config dump (ssh -G), so Host, Match, Include, globs, negation, ordering, and unknown-keyword policy are the client’s semantics by construction. The one exception is --no-ssh-client (SB-34).
SB-5 A typed name must be usable without a port: the resolved config supplies one when it names one, an explicit :port on -r must override it, and where neither does the chain must dial ssh’s own default port (cited from its home in the chain’s source, never copied here) and must report the port as defaulted, never as one the config named. A config that omits Port is nearly every real one, so refusing it would refuse ordinary use. On the client lane the attribution is the client’s own answer: ssh -G reports one port for a Port 22 block and for a portless one alike, so that lane reports the port it dialled and does not claim provenance it cannot observe.
SB-6 With no ssh client present, -r must fall back to the built-in resolver and then to the typed name, and must reach the same dial target the client would for the same config — including the default port. A box with no ssh reads the file a client would and dials what it says; a clientless lane that refused every Port-less block would refuse ordinary use.
SB-7 An address literal must be dialled as written and must not be rewritten by a config block. This is a declared deviation from ssh, which lets Host 10.0.0.* apply to a literal. The rule covers an IPv4 dotted quad and a bracketed IPv6 literal alike, and reaches neither the client nor the list.
SB-8 A value the ssh client reports as fatal to itself (an unparsable Port, an unknown keyword without IgnoreUnknown) must be reported as a resolution failure, never silently accepted or silently dropped.
SB-33 One resolve must invoke the ssh client at most once: the host name and the port must both come from a single ssh -G dump, because a second dump re-runs Match exec and may answer the same name differently.
SB-34 --no-ssh-client must resolve with no client invocation at all, answering from the host list alone, so a scripted dial’s answer cannot depend on which the client does with a config. A Match the list reads still runs its command under this flag — the list honours Match exec too, so the flag removes the client, not the config’s own side effects.
SB-35 --ssh-config <PATH> must pin one file for both links — the client reads it with -F, the fallback reads it directly — so an answer comes from the same bytes whichever link supplied it.
SB-37 A HostName containing %h must expand the token to the name as typed, in a block whose pattern names one host and in a wildcard block alike, so Host gpu* + HostName %h.example dials <typed>.example. Both lanes must agree on the expansion.
SB-36 The wait on ssh -G must be bounded by one fixed deadline the client link owns — the bound lives where it is set and is cited, never copied here — after which the client is killed and the wait reported as a resolution failure, so a Match exec that never returns cannot hang the dial.

Match exec is the sharp edge of SB-4: the client runs the command during a config dump. The bridge must inherit that behaviour rather than filter it, because filtering it would answer with semantics the client does not have.

SB-33 and SB-36 are that inheritance’s price. Because the command is side-effecting, a second dump is a second side effect and possibly a second answer, so the two fields are read from one invocation; and because the command is arbitrary, it may never return, so the invocation is killed at a fixed deadline rather than waited on.

3. The mount

One ssh exec carries the whole install. The bytes travel on stdin, so no scp, sftp, or rsync subsystem is required on either end.

id the requirement
SB-9 The payload must arrive on the ssh channel’s stdin and be executed from a directory created by mktemp -d (0700), so no other user can swap the binary between write and exec.
SB-10 The far side must fall back across $XDG_RUNTIME_DIR, /dev/shm, /tmp, and the qqt state directory when a candidate is mounted noexec.
SB-11 The launched server must detach from the ssh session (setsid, log file over the standard descriptors) so the tunnel survives the channel closing.
SB-12 The handshake must travel on stdout as one machine-readable line: port, address, identity fingerprint, pid, and whether the server started or attached.
SB-13 A second mount must re-exec a retained binary instead of re-sending it, and must refuse a retained binary whose recorded digest no longer matches the local one.

The retained binary is content-addressed for SB-13: a stale payload is a silent version skew otherwise, and the failure would surface as a protocol mismatch with no cause named.

4. Port authority

Ports below 1024 are out of scope: excluding them removes the only case in which the uid genuinely changes the permission the kernel grants.

case far-side state the answer
A nothing holds the port start the server on it; mint this user a client certificate for that door
B a qqt server of the same uid holds it attach: the incumbent mints the client certificate and returns the dial parameters
C another uid holds it refuse, naming the holder’s uid
id the requirement
SB-14 The decision for case B must come from the kernel: SO_PEERCRED on the incumbent’s control socket must name the caller’s uid, compared against the uid that bound the port.
SB-15 The control socket must live inside a 0700 directory owned by that uid, so a stranger cannot connect() even before the uid comparison runs.
SB-16 Cases A and B must mint the certificate from the same CA — the one the running server owns for that port — so a client admitted by either path is indistinguishable at the TLS handshake.
SB-17 Case C and every unclassifiable bind failure must exit non-zero with the owning uid named; a silent fall-back to another port is forbidden.
SB-18 The SO_REUSEPORT probe is optional and diagnostic only; the admitted path must not depend on joining a live reuseport group.

The optional probe exists because it is the literal question — the kernel performs the bind that would have been performed — but joining a live group rehashes 4-tuples and drops datagrams, so it can never be the mechanism the gate rests on.

5. The certificate store

One directory per door, on both sides. The CA private key never leaves the box that minted it.

$XDG_STATE_HOME/qqt/certs/<port>/   far side — one directory per door
  ca-cert.pem  ca-key.pem           the door's own CA (key is 0600, far side only)
  server/server-cert.pem            the listener's identity, minted around the
  server/server-key.pem             box's ssh host key (CLI-17)
  client/client-cert.pem            the last client minted
  client/client-key.pem
  run/control                       the local authority check
  qqt-<digest>                      the retained payload

$XDG_STATE_HOME/qqt/certs/          near side
  client/{ca-cert,client-cert,client-key}.pem
$XDG_STATE_HOME/qqt/index/<host>_<port>  -> ../../servers/<fingerprint>/

The near side carries no server half and no CA key: it verifies the door against the identity ssh already gave it (SB-28), so its ca-cert.pem is the door’s, received over the authenticated channel.

id the requirement
SB-19 Far-side material must be scoped per port, never per host, so a certificate minted for one door is worthless at another.
SB-20 Near-side material must be scoped by the server’s identity fingerprint, with host:port as an index only, so two boxes on the same port never share an entry and a renamed box keeps its certificate.
SB-21 The CA private key must stay on the box that minted it; the near side receives the door’s ca-cert.pem and its own client pair only.
SB-22 Directories must be 0700 and private keys 0600; a store found group- or world-accessible must be refused with a diagnostic, never silently re-chmodded.
SB-23 Deleting $XDG_STATE_HOME/qqt/certs/<port> must revoke every client minted for that door; no separate revocation list is required for this path.
SB-38 The store must live under $XDG_STATE_HOME/qqt (~/.local/state/qqt by default), so the material’s home is the state directory the rest of the crate already uses, not a second one invented for the bridge.

SB-38 settles a layout this contract previously declared and the code never adopted: ~/.ssh/qqt was written into the requirement table while every shipped path used ~/.local/state/qqt/certs. One home, named once.

6. Lifecycle

id the requirement
SB-24 The server must live while at least one QUIC connection is live and must exit after a configured idle window with none.
SB-25 Two named exits are available without waiting for idleness: a stop command and the process signal it sends.
SB-26 The server must not be restarted by any supervisor: the next mount is the restart, so a reboot costs one ssh round trip and no stored state.
SB-27 A reconnect must follow the mount path when the port has gone free, so a lost server and a never-started server share one code path.

7. Identity

id the requirement
SB-28 The identity a client pins must be the one ssh already verified for that host, or a fingerprint delivered over the same authenticated channel; a bare trust-on-first-use pin that neither source covers is forbidden.
SB-29 A changed identity must fail loudly, named as an identity change, and must not overwrite the stored entry.
SB-30 Access and identity must stay separate decisions: the certificate answers who may connect, the ssh login answers whether they may, and neither store is overloaded with the other’s meaning.

8. Multi-user, now and later

id the requirement
SB-31 No account, group, or ACL file is consulted in the single-user path; the uid comparison of SB-14 is the whole authority model.
SB-32 Cross-user sharing of one port must arrive as one rule over that same comparison (the socket’s group and mode), never as a second authority source.

9. Declared deviations

These are choices, not gaps, and each one is visible in behaviour:

deviation what it means
Address literals bypass the config (SB-7) ssh would let Host * rewrite an address and supply its port; qqt keeps “an address is dialled as written” as the older promise. A Host 10.0.0.* block the client applies to 10.0.0.5 never reaches the dial, and the literal’s Match exec never runs. A literal keeps the port the operator wrote beside it: -r 1.2.3.4:2222 dials exactly that, and -r 1.2.3.4 with no :port dials port 22 as defaulted — ssh would instead let the block hand it a different port.
An IPv6 literal is written bracketed on -r and dialled bare -r [fd00::7]:22 and -r [fe80::1%eth0]:22 dial fd00::7 and fe80::1%eth0: the brackets exist only to delimit the :port split, because only a bracket-aware split keeps a zone suffix intact. -r fd00::7 is therefore a grammar error rather than a name, where ssh accepts the unbracketed form.
The default config’s home comes from $HOME, the client’s from the passwd entry with --ssh-config both links read the same file (SB-35). Without it, the fallback reads $HOME/.ssh/config while the client resolves ~ from the passwd entry and then reads /etc/ssh/ssh_config — so under sudo or a systemd unit, where $HOME and the passwd home differ, the clientless lane can answer from a file the client would never read.
Unknown keywords are tolerated in the built-in fallback the ssh client itself exits 255 on a keyword or a Match attribute it does not know; the fallback exists to read a file, not to police it, so it reads the keywords it consumes and ignores the rest. On a box with a client this costs the fallback lane only; with no client, a config the client would refuse still dials.
A fatal Port does not stop the fallback the client terminates on Port not-a-port (or Port 0) and reports it as fatal (SB-8); the fallback treats an unparsable port as absent and keeps reading, so a later Port 2222 in the same block still supplies a port. The two lanes therefore answer differently for exactly the configs the client refuses — the client-lane answer is the one that governs when a client is present.
Ports below 1024 are unsupported that is the only place capability, not uid, would decide; an unprivileged server cannot bind one.
A stopped server is not restarted by a supervisor (SB-26) the mount is cheap enough to be the restart.
The door is named by the command surface, not the wire this contract says “server” and “client” because the mechanism has two ends; the operator types local up and remote up <box> (CLI-1). The wire words survive as aliases (CLI-6).

10. Where each requirement is proven

area the proof
resolution (SB-4 … SB-8, SB-33 … SB-36) four layers: three #[cfg(test)] modules beside the code (the crate has no lib target and so no tests/ directory), plus two shell harnesses for the questions a unit test cannot ask. docker/scenarios.sh answers the config/name pairs against the host’s own ssh -G across both lanes and sweeps every alias the host’s real config reaches through its Include chain; docker/run-tunnel.sh proves a payload crosses while the client dials a config alias in a container with no ssh client, so the byte rides the fallback link. The unit layer: src/hosts/differential.rs (declared #[cfg(test)] mod differential) is the corpus of config/name pairs answered twice — by the host’s own ssh -G and by the fallback — asserting the client’s answer, the fallback’s answer, and the deviations that corpus row declares — the SB-7 literal and the tolerated unknown keyword — as deviations, so a fallback that silently starts agreeing also fails; it skips loudly on a box with no usable client. src/resolve.rs’s #[cfg(test)] mod tests holds the chain’s own: literal pass-through, explicit-port override, the defaulted port, the client’s fatal answer, the fallback decline, --no-ssh-client, and the one-invocation count that proves SB-33. src/ssh_dump.rs’s tests cover the dump parser and the bounded run — a Match exec that sleeps past the deadline is killed and reported, which is SB-36.
mount (SB-9 … SB-13) an end-to-end run to a box with no scp, a noexec mount, and a digest mismatch
authority (SB-14 … SB-18) the three cases against one box: a free port, a port held by the same uid, a port held by another uid
store (SB-19 … SB-23, SB-38) a layout inspection plus a handshake attempted with a certificate minted for a different door
lifecycle (SB-24 … SB-27) connect, drop, reconnect inside and outside the idle window; then a stop and a re-mount
identity (SB-28 … SB-30) a changed host key, and a certificate presented under a pin it never earned
multi-user (SB-31, SB-32) two uids against one port, with and without the sharing rule

11. What is built

remote up <box> is the mount, end to end: it reads the box’s ssh host key over the channel the login just authenticated, mints the door’s material around that key, streams this binary and the far half over the same channel, starts the door detached, and dials. No certificate is typed, no scp runs, and no --certs-dir appears (CLI-16): the operator’s command line is qqt remote up <box> -L <spec>.

The mount runs on the operator’s own ssh client with its config intact — not -F /dev/null. That is the difference between a bridge and a second login system: ssh gpu-box and qqt remote up gpu-box reach the same box by the same alias, with the same IdentityFile, through the same ProxyJump. The lane adds only what a non-interactive mount needs and a config does not supply — BatchMode=yes, so it never blocks on a prompt, and StrictHostKeyChecking=accept-new, so a first touch joins while a changed key still aborts.

Proven live on two boxes, each starting from no state at all:

what how it was proven
the identity is the ssh key (CLI-17, SB-28) the stored pin is byte-identical to the box’s ssh_host_ed25519_key SPKI, and its fingerprint is the one ssh-keygen -lf prints
the pin bites (SB-29) a corrupted identity.spki refuses the handshake with invalid peer certificate, exit 1; restoring it reconnects
a re-mount is idempotent (SB-13) a second remote up logs reused the door material, keeps the same CA fingerprint, and still carries traffic
the tunnel carries traffic a far-side HTTP origin answers 200 on the local bind in ~2.7 ms; -R serves a local origin back to the box (both directions on separate runs)
teardown is idempotent (CLI-5) two consecutive remote down calls both exit 0

The store is the one §5 declares: $XDG_STATE_HOME/qqt/certs/servers/<identity>/ holds the near side keyed by the door’s identity, and $XDG_STATE_HOME/qqt/certs/index/<host>_<port> points at it, so one entry serves every user of a box and a renamed box keeps its certificate.

The control socket, the SO_PEERCRED gate and the multi-user cases (SB-1 … SB-3, SB-14 … SB-18, SB-31, SB-32) are not built: one door admits one ssh user’s clients, and a second user attaching to a running door is still owed. local up starts this box’s door and mints on request but does not yet take the door’s own port from a config; --port is the one home of the number.

qqt up <box> survives unchanged as the legacy ship-and-start (its own --certs-dir and --verify still apply); remote up is the contract’s verb and the one the quick starts name.

← All documentation