Skip to main content

gunnar_ssh/
server.rs

1//! The russh server: authentication, the exec dispatch, and the channel
2//! hand-off (the ACL model, the two-listener split).
3//!
4//! # The shape of a connection
5//!
6//! 0. **`auth_none`**, the anonymous read path, and the only place
7//!    gunnar reads the SSH username. See below.
8//! 1. **`auth_publickey`**: russh has already verified that the client owns the
9//!    offered key. gunnar fingerprints it (SHA-256 over the wire blob, OpenSSH's
10//!    printed form, [`crate::access::fingerprint_of_key`]) and asks
11//!    `gunnar-core`'s identity table through [`Control`]. The SSH *username* is
12//!    logged and otherwise ignored: identity comes from the key, so
13//!    `git@host:ns/repo.git` works and `git` means nothing (the ACL model).
14//! 2. **`channel_open_session`**. The channel is kept, because the exec request
15//!    arrives as a separate message that only carries a [`ChannelId`], and the
16//!    stream can only be taken from the `Channel` itself. A watchdog is armed
17//!    here so a session that authenticates and then never execs cannot sit on
18//!    the connection.
19//! 3. **`env_request`**, answered against [`ENV_ALLOWLIST`], which holds
20//!    exactly one name: `GIT_PROTOCOL`. Everything else is refused. See below.
21//! 4. **`exec_request`**: [`Dispatch::authorise`] parses, resolves and checks
22//!    the grant. Only then is the channel's stream handed to [`GitTransport`].
23//! 5. **everything else**. `shell_request`, `subsystem_request`,
24//!    `pty_request` and `x11_request` are answered with a channel failure.
25//!    **No shell, ever.**
26//!
27//! # Why the transport is spawned and not awaited
28//!
29//! russh calls handler methods from the session's own read loop (see
30//! `russh::server::Session::run`). Awaiting a whole clone inside `exec_request`
31//! would stop the loop that has to deliver the client's bytes to the channel;
32//! the transfer would deadlock on its first read. russh's own `sftp_server`
33//! example awaits inline; gunnar does not.
34//!
35//! # The exit status, and why the channel is split rather than consumed
36//!
37//! `Channel::into_stream()` wraps the channel in russh's `ChannelCloseOnDrop`,
38//! which spawns a task sending `ChannelMsg::Close` when the transport lets the
39//! stream go. That `Close` raced the `exit-status` the server sent next, and
40//! won often enough that a *successful* push reported failure. Measured
41//! 2026-08-03: a complete 244-byte advertisement delivered with `ssh` exiting
42//! **255**. See [`crate::channel`].
43//!
44//! The channel is therefore `split()` instead. The read half becomes an owned
45//! [`ChannelReader`], the write half stays with the server, and `exit-status`
46//! and `close` go out through **the same sender the data went through**, in
47//! order, after the transport has finished with it. Nothing sleeps and nothing
48//! races.
49//!
50//! # One span per connection, and what hangs off it
51//!
52//! ```text
53//!   ssh.connection { conn, peer, principal, repo }     gunnar::ssh
54//!     └── git.transfer { service, access }             gunnar::git
55//!           ├── git.upload_pack | git.receive_pack     gunnar::git
56//!           └── policy.gate { ref_name }               gunnar::policy
57//! ```
58//!
59//! `principal` and `repo` are declared empty when the connection opens and
60//! **recorded** when `auth_publickey` and `exec_request` learn them, so a line
61//! emitted at any depth carries all four fields without any call site restating
62//! them. That is the difference from what was here before: connection identity
63//! was an ad-hoc `?self.peer` copy-pasted to fourteen call sites, there was no
64//! id at all, and two lines from the same connection could not be tied
65//! together.
66//!
67//! Two hand-offs would drop the span if they were not made explicit, and each
68//! is a place where an operator would silently lose half a connection:
69//!
70//! * `tokio::spawn`: the exec watchdog and the transfer task both take
71//!   `.instrument(span)`, because a spawned task does not inherit a
72//!   thread-local span;
73//! * `spawn_blocking`: `gunnar-cli`'s `WireTransport::serve` captures
74//!   `Span::current()` and enters it on the blocking thread, which is where the
75//!   whole git conversation actually happens.
76//!
77//! # How an anonymous session arrives, and why it is `auth_none` on one
78//! # username
79//!
80//! SSH has no unauthenticated mode: a client must complete *some* userauth
81//! method before it may open a channel. Three shapes were available.
82//!
83//! | | why not |
84//! |---|---|
85//! | accept `none` for **every** username | **This is the one that would break the server.** OpenSSH's client always sends a `none` request first, to learn the method list. A server that answers it with SUCCESS authenticates *every* stock client as anonymous before it ever offers its key, so every existing push would start failing, and every existing user would silently lose their identity. Measured against the russh dispatch: `server/encrypted.rs` calls `auth_none` on that first probe regardless of the advertised `methods`. |
86//! | a **well-known published key** | The anonymous-FTP shape. It works, but a stranger has to obtain a key file before they can clone, which is worse than the problem it solves, and gunnar would then hold a key whose private half is public in the same table that authenticates real users. |
87//! | **`none`, on one reserved username** | Taken. |
88//!
89//! `ssh://anonymous@host/ns/repo.git` gets the `none` method accepted and a
90//! session whose principal is
91//! [`Principal::anonymous`](gunnar_core::Principal::anonymous). Every other
92//! username gets `none` refused, exactly as russh's default handler refused it
93//! before this existed, and the client falls through to publickey unchanged.
94//!
95//! **Why it cannot become a bypass**, in the order the objections come:
96//!
97//! * *Does it weaken private repositories?* No. The username selects the
98//!   authentication method; the **authorisation** answer still comes from
99//!   `AuthTable::may` (the ACL model), where the anonymous principal is one rule, `Read` on
100//!   `Public`, and is refused everything else including `may_namespace` and
101//!   `may_server`. There is no second ACL (one implementation).
102//! * *Can a caller claim to be someone by picking a username?* No. The
103//!   anonymous principal is the **only** identity a username can select, it is
104//!   never in the identity table, [`Principal::new`] and
105//!   `AuthTable::add_principal` refuse its id, so no key and no grant can ever
106//!   attach to it.
107//! * *Can an authenticated user be silently downgraded?* No. `none` is refused
108//!   for their username, so nothing changes for them; and a user who *does*
109//!   type `anonymous@` asked for strictly less access, which is not an
110//!   escalation.
111//! * *Does the username now reach an authorisation decision, against the ACL model?* It
112//!   reaches exactly one, in the direction of *removing* everything. The ACL model's
113//!   rule exists so that a username cannot be used to become somebody; this
114//!   uses it to become nobody.
115//!
116//! **`none` is not advertised** in [`RusshConfig::methods`], which stays
117//! publickey-only. Advertising it would tell every client that `none` is
118//! available when it is available only for one name (the failure list is
119//! supposed to be true), and russh calls `auth_none` on the client's opening
120//! probe either way, which is the only place it is ever needed.
121//!
122//! [`SshConfig::allow_anonymous`](crate::SshConfig::allow_anonymous) closes the
123//! whole path at the listener for an appliance that wants it shut.
124//!
125//! # `GIT_PROTOCOL`, and why it is an allowlist of one
126//!
127//! A git client announces protocol v2 over SSH by sending
128//! `GIT_PROTOCOL=version=2` as a channel environment request. Refusing every
129//! `env_request`, which this server used to do, meant every session fell back
130//! to v0. Stock `git` copes; `gix`'s blocking v0 client does not, because it
131//! requires `multi_ack_detailed` and `gunnar_wire::caps` correctly declines to
132//! advertise a capability it does not implement. The measured consequence was
133//! that `gunnar clone` could not clone from `gunnar serve` at all.
134//!
135//! Accepting the variable is **not** the same as accepting an environment. An
136//! SSH server that passes arbitrary client-supplied names through has handed
137//! the client `LD_PRELOAD`, `PATH` and every other lever there is. [`ENV_ALLOWLIST`]
138//! names the one variable gunnar reads; every other name is refused with a
139//! channel failure, exactly as before. The accepted value is not an input to
140//! any authorisation decision: it reaches [`ServiceRequest::git_protocol`] and
141//! nothing else, and it only ever selects the *framing* the wire answers in.
142
143use std::collections::HashMap;
144use std::net::SocketAddr;
145use std::sync::atomic::{AtomicBool, Ordering};
146use std::sync::Arc;
147use std::time::Duration;
148
149/// How long [`BoundServer::shutdown`] waits for sessions to answer the
150/// disconnect before it aborts the listener. Longer than the gRPC grace (2 s):
151/// a git transfer mid-pack deserves a moment to finish its write.
152pub const SSH_SHUTDOWN_GRACE: Duration = Duration::from_secs(10);
153
154use russh::keys::ssh_key::PublicKey;
155use russh::server::{Auth, Config as RusshConfig, Handler, Msg, Server, Session};
156use russh::{Channel, ChannelId, Disconnect, MethodKind, MethodSet};
157use tokio::net::TcpListener;
158use tokio::task::JoinHandle;
159use tracing::Instrument;
160
161use gunnar_core::observe::{RequestId, ServerMetrics, TARGET_GIT, TARGET_SSH};
162use gunnar_core::RepoRef;
163
164use crate::access::{fingerprint_of_key, Access, Fingerprint, Principal};
165use crate::admission::{Admission, AdmissionStats};
166use crate::channel::{
167    serve_until_session_ends, session_ended_error, session_over, ChannelReader, SessionBound,
168};
169use crate::command::GitService;
170use crate::command::{self, CommandError};
171use crate::config::{SshConfig, ANONYMOUS_SSH_USER};
172use crate::control::Control;
173use crate::governor::OpClass;
174use crate::seam::{Delivery, GitTransport, ServiceRequest};
175
176// ───────────────────────────────────────────────────────────────────────────
177// The authorisation decision, without any SSH in it
178// ───────────────────────────────────────────────────────────────────────────
179
180/// Why an exec request was not served.
181#[derive(Debug, thiserror::Error)]
182pub enum DispatchError {
183    /// The command did not parse. **The `bash` case, and every other one.**
184    #[error("{0}")]
185    Command(#[from] CommandError),
186    /// The repository does not exist.
187    #[error("no such repository: {0}")]
188    NoSuchRepo(RepoRef),
189    /// The principal does not hold enough access.
190    #[error("{principal} holds {held} on {repo}, needs {needed}")]
191    Denied {
192        /// Who asked.
193        principal: String,
194        /// What they asked for.
195        repo: RepoRef,
196        /// What the command requires.
197        needed: Access,
198        /// What they actually hold, rendered as `none` when they hold nothing.
199        held: String,
200    },
201    /// **This box is a read-only STANDBY** and the write is refused.
202    ///
203    /// A distinct arm, not a [`DispatchError::Denied`]: the writer holds the
204    /// grant — on a primary this push would land — so "you do not have access"
205    /// would be a lie that sends them to an administrator about permissions.
206    /// The honest sentence is that this box does not take client writes; push
207    /// to the primary. It is only ever reached by a principal who already
208    /// passed the grant check, so it leaks no repository the writer could not
209    /// already see.
210    #[error("{principal}: this box is a standby; writes go to the primary ({repo})")]
211    Standby {
212        /// Who asked.
213        principal: String,
214        /// The repository they tried to write.
215        repo: RepoRef,
216        /// The box is a browse replica, so the
217        /// refusal says so: a stranger who found a public repository here and
218        /// tried to push to it should learn what this box is, not merely that
219        /// it is frozen.
220        browse: bool,
221    },
222    /// **This box is a TWIN and serves no git at all** — not a read, not a
223    /// write, not to anybody (2026-09-21, lane T4).
224    ///
225    /// Its own arm and not a [`DispatchError::Standby`], because a standby is a
226    /// box that serves reads and refuses writes, and a twin is a box that
227    /// serves NEITHER: `serve.rs` says why in one line — *a read would grow the
228    /// caches the twin must not hold* — and the twin's whole value is that its
229    /// disk holds the primary's log and nothing derived.
230    ///
231    /// **Why this exists at the door rather than only at the bind.** The bind
232    /// is chosen ONCE, in `serve::start`, from `stores.is_twin()` at that
233    /// instant. A box becomes a twin over the wire, through
234    /// `Bootstrap.BecomeTwin`, long after that — and the owner's one-medium
235    /// ruling makes that the ONLY way it happens, because a twin is now
236    /// installed from the same image as a main with no per-box parameter to
237    /// say which it is. MEASURED under KVM by lane T3, 2026-09-21: a box
238    /// conferred at runtime kept `0.0.0.0:2222` bound for the rest of its life,
239    /// the `this box is a TWIN` warning never printed, and its volume ended up
240    /// holding five `objects.exploded` cache files. A socket that cannot be
241    /// moved without a restart is not a reason to serve git from it in the
242    /// meantime.
243    #[error("{principal}: this box is a TWIN; it serves no git to anyone — clone and push to the primary it follows ({repo})")]
244    Twin {
245        /// Who asked.
246        principal: String,
247        /// What they asked for.
248        repo: RepoRef,
249    },
250    /// **Anonymous reads are not served here** (`--anonymous refuse`); the
251    /// browse replica serves them, and `browse_host` names it when the
252    /// operator did. Raised before the repository is even looked up, so a
253    /// stranger learns nothing about what exists on the primary.
254    #[error("anonymous reads are refused on this box ({repo}); browse host: {browse_host:?}")]
255    AnonymousElsewhere {
256        /// The repository the stranger asked for.
257        repo: RepoRef,
258        /// Where anonymous reads are served, when the operator named it.
259        browse_host: Option<String>,
260    },
261    /// **The gate chain refuses this**: the disk is under its floor (every
262    /// build), or (`metered`) a Suspended or Retention tenant's push or LFS
263    /// upload, an anonymous read of a lapsed tenant's public repository, or a
264    /// push at the pack quota.
265    ///
266    /// Its own arm for the same reason [`DispatchError::Standby`] is one: the
267    /// caller holds the grant, so "you do not have access" would be a lie
268    /// that sends a paying customer to an administrator about permissions.
269    /// The honest sentence names the fact — free bytes and the floor, or the
270    /// state, the date and where to renew — and it is only ever reached AFTER
271    /// the grant check admitted the request, so it leaks nothing about a
272    /// repository the caller could not already see.
273    #[error("{namespace}: {line}")]
274    Plan {
275        /// The tenant.
276        namespace: String,
277        /// What the client is told, verbatim.
278        line: String,
279    },
280    /// The identity table or the catalog could not be read.
281    ///
282    /// **Deliberately not folded into [`DispatchError::Denied`].** The tables
283    /// live in storage (the Arrow-IPC ref log) and a read can fail; answering a storage
284    /// fault with "you do not have access" would turn an outage into a silent,
285    /// convincing permission denial that nobody would think to investigate.
286    /// The ACL model is explicit that `may(...)` returns `Result<bool>` for this
287    /// reason, and this variant is where that `Result` lands.
288    #[error("the control plane could not answer: {0}")]
289    Store(#[from] gunnar_core::Error),
290}
291
292impl DispatchError {
293    /// What the client is told.
294    ///
295    /// `None` means "answer with a channel failure and say nothing": the
296    /// request was not even a git command, so there is no git client on the
297    /// other end that would understand a pkt-line.
298    ///
299    /// **"No such repository" and "no permission" produce the same string on
300    /// purpose.** A client that could tell them apart could enumerate private
301    /// repositories one guess at a time. The server log keeps the distinction;
302    /// the wire does not.
303    ///
304    /// Those two arms are **one arm**, and were two identical `format!`s until
305    /// 2026-08-05. That is the twinning one implementation forbids, and this is the case it
306    /// forbids it for: the property is that the two strings are equal, so two
307    /// copies of the string put the property one careless edit away from being
308    /// false, in the direction where the wire starts leaking which private
309    /// repositories exist.
310    /// `a_missing_repository_and_a_forbidden_one_are_one_string_to_the_client`
311    /// guards it from the far side of a real session; this makes the guard hard
312    /// to need.
313    pub fn client_message(&self) -> Option<String> {
314        match self {
315            // A verb gunnar knows and refuses BY NAME gets its sentence; the
316            // rest of the parse failures are not git clients at all.
317            DispatchError::Command(CommandError::NotServed { verb, why }) => {
318                Some(format!("{verb}: {why}"))
319            }
320            DispatchError::Command(_) => None,
321            DispatchError::NoSuchRepo(repo) | DispatchError::Denied { repo, .. } => Some(format!(
322                "repository not found, or you do not have access: {repo}"
323            )),
324            // A standby refusal is told plainly — the writer is authorised, so
325            // there is no repository to hide from them, and "push to the
326            // primary" is the one thing they need to hear to succeed.
327            // A twin refusal is told plainly for the same reason, and it is told
328            // to EVERYONE including a stranger: it names no repository and
329            // reveals nothing this box holds — "this is a twin" is a fact about
330            // the box, not about its contents.
331            DispatchError::Twin { .. } => Some(
332                "this box is a TWIN: it holds a copy of the primary's transaction log and serves \
333                 no git to anyone, read or write. Clone and push to the primary it follows"
334                    .to_string(),
335            ),
336            DispatchError::Standby { browse: false, .. } => {
337                Some("this box is a standby; push to the primary".to_string())
338            }
339            DispatchError::Standby { browse: true, .. } => Some(
340                "this box is a browse replica: it serves public repositories read-only and \
341                 takes no writes; push to the primary"
342                    .to_string(),
343            ),
344            DispatchError::AnonymousElsewhere {
345                browse_host: Some(host),
346                repo,
347            } => Some(format!(
348                "anonymous reads are served by the browse replica, not here: clone \
349                 {repo} from {host} (or authenticate with a registered key)"
350            )),
351            DispatchError::AnonymousElsewhere {
352                browse_host: None, ..
353            } => Some(
354                "this server does not serve anonymous reads; authenticate with a registered \
355                 key, or use the browse replica"
356                    .to_string(),
357            ),
358            // The gate's refusal is told in full: the state, the date and the
359            // URL are the three things a lapsed tenant needs to get going again,
360            // and the free bytes and the floor are what a full box's operator
361            // needs; hiding any of them would turn a fact into a support ticket.
362            DispatchError::Plan { line, .. } => Some(line.clone()),
363            // A fault, not a decision. The client is told the server broke,
364            // because telling it "you do not have access" would be a lie that
365            // sends the user to their administrator about permissions.
366            DispatchError::Store(_) => {
367                Some("the gunnar control plane is unavailable; try again".to_string())
368            }
369        }
370    }
371}
372
373/// Parse → resolve → check, with no SSH anywhere in it.
374///
375/// Kept separate from the [`Handler`] so the security decision can be tested
376/// directly, exhaustively and without a handshake, and so there is exactly one
377/// place where it is made.
378#[derive(Clone, Debug)]
379pub struct Dispatch {
380    control: Control,
381}
382
383impl Dispatch {
384    /// Build a dispatch over the control plane.
385    pub fn new(control: Control) -> Self {
386        Dispatch { control }
387    }
388
389    /// The control plane this dispatch authenticates and authorises against.
390    pub fn control(&self) -> &Control {
391        &self.control
392    }
393
394    /// Decide whether `principal` may run `raw` on this server.
395    ///
396    /// Parse (which includes the sandbox), resolve, then check the grant. A
397    /// storage failure at either lookup comes back as
398    /// [`DispatchError::Store`] rather than as a denial. See
399    /// [`crate::control`].
400    ///
401    /// **The client's environment is not a parameter here, and must not
402    /// become one.** The returned [`ServiceRequest`] carries
403    /// `git_protocol: None`; the handler attaches the allowlisted value with
404    /// [`ServiceRequest::with_git_protocol`] *after* this decision has already
405    /// been made, so there is no path by which anything the client typed into
406    /// its environment can reach the grant check.
407    pub fn authorise(
408        &self,
409        principal: &Principal,
410        key_fingerprint: Option<&Fingerprint>,
411        raw: &[u8],
412    ) -> Result<ServiceRequest, DispatchError> {
413        let parsed = command::parse(raw)?;
414        self.authorise_service(principal, key_fingerprint, parsed.service, parsed.repo)
415    }
416
417    /// The same decision, entered from a **URL path** rather than an exec line.
418    ///
419    /// Smart HTTP does not send `git-upload-pack '<path>'`; it sends
420    /// `POST /<path>/git-upload-pack`. The verb and the path arrive already
421    /// separated, and the path has no shell quoting on it, so there is nothing
422    /// for [`command::parse`] to do — but everything **after** the parse is
423    /// identical and must stay identical: resolve, ask the ACL, and refuse in
424    /// the one way that does not tell a caller whether a repository exists.
425    ///
426    /// So this is where that decision lives and [`Dispatch::authorise`] is now
427    /// a parser in front of it. Two entry points, one answer (one implementation). A second
428    /// copy of this in `gunnar-http` is precisely the thing that ends with SSH
429    /// and HTTP disagreeing about who may push, and the disagreement being
430    /// found by whoever pushes.
431    ///
432    /// `repo` is a [`RepoRef`], which is to say it has already been through
433    /// [`gunnar_core::resolve_repo_path`]. The sandbox is not optional on this
434    /// path either: the caller cannot construct a `RepoRef` without it.
435    pub fn authorise_service(
436        &self,
437        principal: &Principal,
438        key_fingerprint: Option<&Fingerprint>,
439        service: GitService,
440        path: RepoRef,
441    ) -> Result<ServiceRequest, DispatchError> {
442        let required = service.required_access();
443        // **A TWIN serves no git**, before anything else is asked — before the
444        // anonymous policy, before the catalog, before the grant. A read on a
445        // twin grows exactly the caches the twin exists not to hold, and the
446        // box's own `browse` flag (which `become_twin` sets) would otherwise
447        // OVERRIDE `--anonymous refuse` and serve a stranger. This is a live
448        // read of the same flag `ServerStores` flips, so the refusal begins the
449        // instant `Bootstrap.BecomeTwin` returns and does not wait for a
450        // restart to move the listener.
451        if self.control.is_twin() {
452            return Err(DispatchError::Twin {
453                principal: principal.id.clone(),
454                repo: path,
455            });
456        }
457        // **`--anonymous refuse`** (the browse replica), before the catalog is consulted:
458        // a stranger on a primary that has a browse replica is sent there, and
459        // learns nothing about what this box holds on the way.
460        if principal.id == gunnar_core::ANONYMOUS_PRINCIPAL_ID && self.control.refuses_anonymous() {
461            return Err(DispatchError::AnonymousElsewhere {
462                repo: path,
463                browse_host: self.control.browse_host().map(str::to_owned),
464            });
465        }
466        let (repo, prospective) = match self.control.resolve(&path)? {
467            Some(repo) => (repo, false),
468            // The repository does not exist. For exactly one shape of request
469            // — a PUSH, by a principal the control plane already permits to
470            // create repositories here — absence is the first-repository door
471            // rather than a refusal: the repository is created and the push
472            // proceeds through the unchanged grant check below.
473            //
474            // # Why a door here, and not a fourth verb or the control plane
475            //
476            // The shipped appliance runs `--no-grpc` (gRPC v1 is
477            // unauthenticated, loopback-only), has no shell by construction,
478            // and its exec surface is three git verbs that all want a
479            // repository — so after `--admin-key` enrolled the bootstrap
480            // admin (a8ba0665), the admin could authenticate and then do
481            // NOTHING, forever (measured under KVM, 2026-08-25). The
482            // alternatives lost on the code, not on taste:
483            //
484            // * **a create verb** would be a wire surface no git client
485            //   speaks, would need its own client incantation, and does not
486            //   fit the dispatch's own shape: `SERVICES` carries per-service
487            //   *repo grant levels* and this decision compares levels — a
488            //   verb whose target does not exist has no repo to hold a level
489            //   on, so it would be a special case beside the table, and a
490            //   probe-discoverable one for every stranger;
491            // * **turning gRPC on** is the door the unauthenticated v1 and
492            //   `gunnar-grpc/tests/loopback_default.rs` exist to keep shut on
493            //   a mailed medium: `Bootstrap.Enroll` with no credential is a
494            //   race between the customer and whoever else finds the box.
495            //
496            // Push-to-create adds no verb, no parse production and no
497            // vocabulary: `Control::create_for_push` asks the exact two
498            // questions `Repos.CreateNamespace`/`Repos.Create` gate on, so
499            // this door and the RPC door can never disagree (one implementation) — and it
500            // sits HERE, where the two entry points (exec line, smart-HTTP
501            // URL) have already fanned in, so SSH and HTTP answer a push to a
502            // missing repository identically.
503            //
504            // Only a WRITE: ReceivePack, and (2026-09-28) an LFS upload.
505            // Reading never creates: a `git clone` of a typo, or an LFS
506            // download, stays an error.
507            //
508            // **Why the LFS upload too.** git-lfs's pre-push hook runs AFTER
509            // git has taken receive-pack's advertisement and BEFORE it sends
510            // a command, and it asks `git-lfs-transfer <repo> upload` first.
511            // MEASURED 2026-09-28 (git-lfs 3.8.0, and on a real box): into a
512            // repository that did not exist yet that exec was the fused
513            // not-found, the hook died — `batch request: exec request failed
514            // on channel 0: exit status 255` — and the push that would have
515            // created the repository never sent its commands. The upload is
516            // the same act as the push (it puts content in the repository),
517            // by the same principal, and it is asked the same two questions
518            // (`push_create_spec`), so it opens the same door and nobody else
519            // learns anything new. Nothing is written here either: the
520            // transport creates the repository when the first OBJECT arrives
521            // (`gunnar-server` `ProspectiveLfs`), so a client that only
522            // batches, or hangs up, leaves nothing.
523            //
524            // For everyone `create_for_push` answers `None` for, the refusal
525            // below is bit-identical to what it always was — the one fused
526            // "repository not found, or you do not have access" — so the
527            // door's existence is not observable to a key it does not admit.
528            //
529            // **Nothing is written here.** The door answers with the
530            // repository it WOULD create (`prospective`), the transport
531            // advertises that, and the catalog row and the store are made
532            // only once the client has sent commands that pass the hash
533            // check — see `Control::push_create_spec` for the ghost
534            // repository this order closes.
535            None if service == GitService::ReceivePack
536                || service == GitService::LfsTransfer(crate::command::Operation::Upload) =>
537            {
538                match self.control.push_create_spec(principal, &path)? {
539                    Some(spec) => {
540                        // The name filter (2026-10-05) — asked only of a
541                        // pusher the door admits, so its sentence tells
542                        // nobody else anything.
543                        if let Some(line) = self.control.push_create_name_refusal(principal, &path)
544                        {
545                            return Err(DispatchError::Plan {
546                                namespace: path.namespace().to_owned(),
547                                line,
548                            });
549                        }
550                        (spec.prospect()?, true)
551                    }
552                    None => return Err(DispatchError::NoSuchRepo(path)),
553                }
554            }
555            None => return Err(DispatchError::NoSuchRepo(path)),
556        };
557        let held = self.control.access_for(principal, &repo)?;
558        // **A pull mirror takes no person's push** (GUN-FIELD-TEST, 2026-10-31). Its references
559        // are the upstream's: a pushed commit was accepted and the next sync moved the branch
560        // back over it, without a word to anybody. Asked for a write by a non-`Machine`
561        // principal only (the standby freeze's rule: a box's own machine identities are not
562        // people), and read only then, so a read costs nothing new.
563        let mirror_of = if required.permits(Access::Write)
564            && principal.kind != gunnar_core::PrincipalKind::Machine
565        {
566            self.control.pull_mirror_of(&path)?
567        } else {
568            None
569        };
570        match held {
571            // **The standby freeze.** Checked here, INSIDE the authorised arm,
572            // so it is only ever reached by a writer the grant check already
573            // admitted — an unauthorised push still gets the fused not-found and
574            // learns nothing new. A write-requiring service by a non-`Machine`
575            // principal on a frozen box is refused: this box has abdicated or is
576            // a configured standby, and a client write here would be a split
577            // the primary never sees. The primary's own replication push is a
578            // `Machine` identity and sails through — that is what a standby is
579            // for.
580            Some(level)
581                if level.permits(required)
582                    && required.permits(Access::Write)
583                    && self.control.write_frozen_for(principal) =>
584            {
585                Err(DispatchError::Standby {
586                    principal: principal.id.clone(),
587                    repo: path,
588                    browse: self.control.is_browse(),
589                })
590            }
591            // The pull mirror (above): inside the authorised arm, so a stranger still gets the
592            // fused not-found and learns nothing about what this box mirrors.
593            Some(level) if level.permits(required) && mirror_of.is_some() => {
594                Err(DispatchError::Plan {
595                    namespace: path.namespace().to_owned(),
596                    line: format!(
597                        "{path} is a pull mirror of {}: its references are the upstream's, and \
598                         the next sync moves them back over anything pushed here. Push to the \
599                         upstream, or stop mirroring (`gunnar control mirror remove {path}`) to \
600                         make this repository its own",
601                        mirror_of.as_deref().unwrap_or_default()
602                    ),
603                })
604            }
605            // **An encrypted repository takes no LFS upload** (wiring tracer B
606            // 2026-10-08). git-lfs uploads in the pre-push hook, BEFORE the
607            // pack the encrypted gate reads, and an LFS object is the
608            // plaintext of a `filter=lfs` file that never went through
609            // `filter=gunnar-crypt`: refused here, for SSH and smart HTTP
610            // alike, before a byte is stored. Asked inside the authorised arm,
611            // so a stranger still gets the fused not-found. A download is never
612            // refused.
613            Some(level)
614                if level.permits(required)
615                    && repo.encrypted
616                    && service == GitService::LfsTransfer(crate::command::Operation::Upload) =>
617            {
618                Err(DispatchError::Plan {
619                    namespace: path.namespace().to_owned(),
620                    line: format!(
621                        "{path} is encrypted, so it takes no LFS objects: LFS stores files as \
622                         they are, outside your members' encryption. Track the file with \
623                         `filter=gunnar-crypt` instead of `filter=lfs`"
624                    ),
625                })
626            }
627            Some(level) if level.permits(required) => {
628                // **The gate chain**, asked LAST: after the grant (so a
629                // refusal leaks nothing to a stranger) and after the standby
630                // freeze (so a standby stays one whatever the gates say). A
631                // write asks `Write`; an anonymous read asks `AnonymousRead`;
632                // an authenticated read asks nothing at all, because the
633                // owner's clone is allowed in every state — data is never held
634                // hostage. See `crate::plan`.
635                let PlanOutcome {
636                    notice: plan_notice,
637                    ceiling: pack_ceiling,
638                    pack_mark,
639                    allowance: lfs_allowance,
640                } = self.ask_plan(principal, service, required, &repo)?;
641                Ok(ServiceRequest {
642                    service,
643                    repo,
644                    prospective,
645                    principal: principal.clone(),
646                    access: level,
647                    // The key that authenticated this session, carried
648                    // rather than guessed back out of the identity table. A
649                    // parameter and not a builder, so it cannot be forgotten into
650                    // `None`.
651                    key_fingerprint: key_fingerprint.cloned(),
652                    git_protocol: None,
653                    delivery: Delivery::Session,
654                    // Derived from `key_fingerprint` by the transport rather
655                    // than spelled here: see `ServiceRequest::credential`.
656                    credential: None,
657                    plan_notice,
658                    pack_ceiling,
659                    pack_mark,
660                    lfs_allowance,
661                    cancel: crate::channel::Cancel::new(),
662                })
663            }
664            other => Err(DispatchError::Denied {
665                principal: principal.id.clone(),
666                repo: path,
667                needed: required,
668                held: match other {
669                    Some(level) => level.to_string(),
670                    None => "none".to_string(),
671                },
672            }),
673        }
674    }
675
676    /// **Ask the gate chain**, for a request the grant check already admitted.
677    /// `Ok(Some(line))` is an allowed request that carries a warning (Grace);
678    /// `Err(Plan)` is the refusal, with the line the client is told.
679    ///
680    /// The three shapes, and why each asks what it asks:
681    ///
682    /// * a **write** (`git-receive-pack`, LFS upload) asks
683    ///   [`PlanAction::Write`](crate::plan::PlanAction::Write), and if that is
684    ///   allowed asks [`PlanGate::pack_quota`](crate::plan::PlanGate::pack_quota)
685    ///   — both before a single pack byte is read;
686    /// * an **anonymous read** asks
687    ///   [`PlanAction::AnonymousRead`](crate::plan::PlanAction::AnonymousRead):
688    ///   a lapsed tenant's public surface is what costs the provider and earns
689    ///   nothing;
690    /// * an **authenticated read** asks nothing. The owner clones their own
691    ///   repositories in every state, and so does anyone they granted. That is
692    ///   rule 1 of `gunnar_core::entitlement`, and it is enforced here by the
693    ///   absence of a question rather than by an answer.
694    ///
695    /// **The chain.** Gates are asked in the order `Control::with_plan_gate`
696    /// added them — disk first — and the FIRST `Refuse` is the answer; a later
697    /// gate is not consulted, so a full box is told it is full and not also
698    /// that its plan lapsed. Each gate is asked BOTH its questions before the
699    /// next gate is asked anything: its ladder (`verdict`), then, for a write
700    /// that grows data, its quota (`pack_quota`). Within one gate that order is
701    /// what tells a Suspended tenant they are suspended rather than also full;
702    /// across gates it is what makes "disk first" mean disk first — the disk
703    /// gate only ever refuses from `pack_quota`, and a chain that asked every
704    /// ladder before any quota would let a plan's ladder outrank it whatever
705    /// the order (`tests/disk_headroom.rs` caught exactly that, red first).
706    /// Warnings from every allowing gate are joined.
707    ///
708    /// With no gate wired (`Control::with_plan_gate` never called) every
709    /// namespace is Free and this is a no-op. A `Machine` principal —
710    /// replication, the primary's copy arriving — is asked only of the gates
711    /// that say they apply to it ([`PlanGate::applies_to_machine`]): the disk
712    /// does, a plan does not, because refusing a lapsed tenant's replication
713    /// would make their standby diverge from the primary.
714    fn ask_plan(
715        &self,
716        principal: &Principal,
717        service: GitService,
718        required: Access,
719        repo: &gunnar_core::Repo,
720    ) -> Result<PlanOutcome, DispatchError> {
721        use crate::plan::{PlanAction, PlanVerdict};
722        let machine = principal.kind == gunnar_core::PrincipalKind::Machine;
723        let gates: Vec<_> = self
724            .control
725            .plan_gates()
726            .iter()
727            .filter(|gate| !machine || gate.applies_to_machine())
728            .collect();
729        if gates.is_empty() {
730            return Ok(PlanOutcome::default());
731        }
732        let anonymous = principal.id == gunnar_core::ANONYMOUS_PRINCIPAL_ID;
733        let action = if required.permits(Access::Write) {
734            PlanAction::Write
735        } else if anonymous {
736            PlanAction::AnonymousRead
737        } else {
738            return Ok(PlanOutcome::default());
739        };
740        let refused = |line: String| DispatchError::Plan {
741            namespace: repo.namespace.clone(),
742            line,
743        };
744        // `service` is named here so a future fourth write-shaped verb has to
745        // decide whether it grows the tenant's data.
746        let grows_data = matches!(
747            service,
748            GitService::ReceivePack | GitService::LfsTransfer(crate::command::Operation::Upload)
749        );
750        let mut notices: Vec<String> = Vec::new();
751        let mut ceiling: Option<crate::plan::PackCeiling> = None;
752        let mut allowance: Option<gunnar_lfs::Allowance> = None;
753        // **Marked BEFORE any gate walks the disk, and that ordering is the
754        // contract**, not an accident of where the line sits. A gate's
755        // `lfs_allowance` measures the namespace's bytes off the disk; an upload
756        // admitted DURING that walk may or may not have landed bytes for the
757        // walk to see. Taking the mark first puts such an admission after the
758        // mark, so it is charged — and charged twice if the walk saw it too,
759        // which refuses a shade early. Taking the mark after the walk would put
760        // it before the mark and possibly outside the walk, which admits too
761        // much. Only one of those two roundings is a quota that leaks. See
762        // `gunnar_lfs::ledger`.
763        let mark = gunnar_lfs::AllowanceLedger::shared().mark(&repo.namespace);
764        // …and the same standing in the PACK meter, taken in the same breath and
765        // for the same reason: `pack_ceiling` below measures the namespace's
766        // packfile bytes off the disk, and a push admitted during that walk has
767        // to land after this mark to be counted.
768        //
769        // **Two marks and not one, because they are two meters.**
770        // `caps["pack_bytes"]` and `caps["lfs_bytes"]` are capped separately on
771        // the signed fact and reported separately by `Usage.Read`, and the two
772        // numbers these marks are judged against are computed from different
773        // caps. One shared counter would charge an LFS upload against a push's
774        // ceiling — cutting a push mid-transfer over room its own cap never
775        // lost — which is a quota that refuses wrongly, the same defect as one
776        // that admits wrongly. They share a disk; they do not share a cap. See
777        // `gunnar_lfs::AllowanceLedger::shared_pack`.
778        let pack_mark = gunnar_lfs::AllowanceLedger::shared_pack().mark(&repo.namespace);
779        for gate in &gates {
780            match gate.verdict(&repo.namespace, action) {
781                PlanVerdict::Allow => {}
782                PlanVerdict::Warn(line) => notices.push(line),
783                PlanVerdict::Refuse(line) => return Err(refused(line)),
784            }
785            // The quota, only for a write this gate's ladder allowed.
786            if action == PlanAction::Write && grows_data {
787                if let PlanVerdict::Refuse(line) = gate.pack_quota(&repo.namespace) {
788                    return Err(refused(line));
789                }
790                // **The smallest ceiling in the chain wins**, which is the
791                // byte-wise spelling of first-`Refuse`-wins one line above: a
792                // push has to satisfy every gate, so it is held to the
793                // tightest one, and the line the client reads is that gate's
794                // own. Asked only where `pack_quota` is asked, so a read and a
795                // deletion are never given a ceiling to trip over.
796                if let Some(asked) = gate.pack_ceiling(&repo.namespace) {
797                    let tighter = match &ceiling {
798                        Some(held) => asked.bytes < held.bytes,
799                        None => true,
800                    };
801                    if tighter {
802                        ceiling = Some(asked);
803                    }
804                }
805                // …and the same tightest-wins rule for the LFS wire, for the
806                // same reason: a transfer has to satisfy every gate, so it is
807                // held to the smallest room any of them will admit, and the
808                // sentence the tenant reads is that gate's own.
809                if let Some(asked) = gate.lfs_allowance(&repo.namespace) {
810                    let tighter = match &allowance {
811                        Some(held) => asked.bytes < held.bytes,
812                        None => true,
813                    };
814                    if tighter {
815                        allowance = Some(asked);
816                    }
817                }
818            }
819        }
820        Ok(PlanOutcome {
821            notice: (!notices.is_empty()).then(|| notices.join("; ")),
822            // The tightest gate's number and the standing taken before any gate
823            // looked, carried side by side rather than folded into one type:
824            // see `ServiceRequest::pack_mark` for why the ceiling stays exactly
825            // what `PlanGate::pack_ceiling` returned.
826            pack_mark: ceiling.is_some().then_some(pack_mark),
827            ceiling,
828            // The tightest gate's number, bound to the standing taken above it.
829            // `mark` is dropped unused when no gate asked for an allowance,
830            // which costs one map read and holds nothing: there is no
831            // reservation in a mark to leak.
832            allowance: allowance
833                .map(|allowance| gunnar_lfs::NamespaceAllowance::new(mark, allowance)),
834        })
835    }
836}
837
838/// What [`Dispatch::ask_plan`] learned: everything the gate chain wants carried
839/// into the transfer, as opposed to the refusals, which are returned as errors
840/// and never reach here.
841///
842/// A struct and not a tuple because both fields are `Option` and both are
843/// about the plan: a `(Option<String>, Option<PackCeiling>)` at the call site
844/// is two anonymous maybes that the next field added would make three.
845#[derive(Default)]
846struct PlanOutcome {
847    /// See [`crate::ServiceRequest::plan_notice`].
848    notice: Option<String>,
849    /// See [`crate::ServiceRequest::pack_ceiling`].
850    ceiling: Option<crate::plan::PackCeiling>,
851    /// See [`crate::ServiceRequest::pack_mark`]. `Some` exactly when `ceiling`
852    /// is: a mark with no number to be judged against is bookkeeping nobody
853    /// reads, and a number with no mark is the bug this closed.
854    pack_mark: Option<gunnar_lfs::Mark>,
855    /// See [`crate::ServiceRequest::lfs_allowance`]. A third `Option` on this
856    /// struct, and the reason the struct exists rather than a tuple.
857    allowance: Option<gunnar_lfs::NamespaceAllowance>,
858}
859
860// ───────────────────────────────────────────────────────────────────────────
861// The server
862// ───────────────────────────────────────────────────────────────────────────
863
864struct Shared {
865    dispatch: Dispatch,
866    transport: Arc<dyn GitTransport>,
867    exec_grace: Option<Duration>,
868    /// Whether `auth_none` on [`ANONYMOUS_SSH_USER`] is accepted.
869    allow_anonymous: bool,
870    admission: Arc<Admission>,
871    metrics: Arc<ServerMetrics>,
872    /// **Are the stores open?** See [`GunnarSsh::with_readiness`].
873    ready: Arc<std::sync::atomic::AtomicBool>,
874    /// [`SshConfig::max_auth_attempts`], carried here because the CONNECTION
875    /// enforces it and russh 0.61.2 does not. See
876    /// [`Connection::auth_attempts`].
877    config_max_auth_attempts: usize,
878    /// [`SshConfig::session_end_grace`] and
879    /// [`SshConfig::session_end_grace_crowded`].
880    session_end_grace: (Duration, Duration),
881    /// [`SshConfig::transfer_idle_timeout`] (GUN-SSH-SLOT-HANG, [`crate::idle`]).
882    transfer_idle: Option<Duration>,
883    /// The sealed box's tunnel-address door ([`TUNNEL_ADDRESS_VERB`]), or
884    /// `None` on a build with no WireGuard tunnel.
885    tunnel_door: Option<TunnelAddressDoor>,
886    /// The repository-settings door ([`REPO_VERB`]), or `None` on a server
887    /// with no control plane (`serve --no-grpc`).
888    repo_door: Option<RepoDoor>,
889}
890
891/// **The one control verb on this listener** (2026-09-21, T17):
892/// `gunnar-tunnel-address [a.b.c.d/prefix]`.
893///
894/// The sealed installer runs `serve --no-grpc`, so its SSH door is its only
895/// door, and a box's WireGuard tunnel address is per box and not on the
896/// medium. So it arrives here, held to **server-wide Admin** and answered by
897/// the closure `gunnar-server` injects — the SAME function the gRPC doors call.
898/// No argument is a read. Output is `key value` lines on stdout, exit 0; a
899/// refusal is `error: <why>` on stdout, exit 1 (stdout and not stderr, so a
900/// client reading only the data band still gets the sentence). Not a git
901/// verb, so it never enters [`Dispatch::authorise`] or the transfer path.
902pub const TUNNEL_ADDRESS_VERB: &str = "gunnar-tunnel-address";
903
904/// **The operator's fire drill** (stinkies ssh-double-exec-error, 2026-09-30): ONE exec
905/// verb, held to server-wide Admin like [`TUNNEL_ADDRESS_VERB`], that says exactly one
906/// ERROR naming itself a drill and who asked — what the estate's watch raises on purpose
907/// to prove its alert path end to end. Nothing is wrong when it is said; no other caller
908/// can raise the server's ERROR at will.
909pub const FIRE_DRILL_VERB: &str = "gunnar-fire-drill";
910
911/// **A member's SSH public keys, for an encrypted repository's key ring**
912/// (2026-10-06): `gunnar-recipient-keys '<namespace>/<repository>' '<member>'`.
913///
914/// What `gunnar crypt add-member NAME --from-server` asks when it is given no
915/// control address — on an installed estate the forge's SSH door (tcp/2222
916/// through the front) is the one door a member's key opens; the control plane
917/// is bound to the WireGuard overlay. The SAME rule as `Repos.RecipientKeys`:
918/// the caller reads the repository (otherwise it is told what a stranger is
919/// told, the one "not found, or no access" sentence), and so must the person
920/// named — gunnar hands out keys only for someone who could read it anyway,
921/// never a directory of everybody's keys. Output is one OpenSSH public key
922/// line per key on stdout, exit 0; a refusal is `error: <why>` on stdout,
923/// exit [`EXIT_DENIED`] (stdout, like [`TUNNEL_ADDRESS_VERB`]'s). An anonymous
924/// session is refused the channel. Not a git verb: it never enters
925/// [`Dispatch::authorise`] or the transfer path, and it changes nothing.
926pub const RECIPIENT_KEYS_VERB: &str = "gunnar-recipient-keys";
927
928/// **Who this key is** (wiring tracer B 2026-10-08): `gunnar-whoami` answers the principal id
929/// the session authenticated as, one line, exit 0 — what `gunnar crypt init` names its first
930/// member by (the account name `add-member --from-server` looks members up by). Reveals nothing
931/// the key's holder does not already know. An anonymous session is refused the channel. Not a
932/// git verb.
933pub const WHOAMI_VERB: &str = "gunnar-whoami";
934
935/// **git-lfs's fallback verb**, answered with a sentence instead of a channel
936/// failure. Gunnar serves Git LFS over pure SSH only (`git-lfs-transfer`,
937/// git-lfs >= 3.0); git-lfs asks this verb only when a pure-SSH session failed
938/// or was refused, and then prints what is on stderr after `batch request:`.
939pub const LFS_AUTHENTICATE_VERB: &str = "git-lfs-authenticate";
940
941/// What [`LFS_AUTHENTICATE_VERB`] answers, on stderr with exit 1. It must never
942/// say `not found` / `no such file`: git-lfs reads those as "no such verb" and
943/// goes on to a guessed HTTPS endpoint, which Gunnar does not serve.
944pub const LFS_AUTHENTICATE_MESSAGE: &str =
945    "Gunnar serves Git LFS over pure SSH only (git-lfs-transfer, \
946git-lfs 3.0 or newer); there is no HTTPS LFS endpoint. The pure SSH transfer just failed or was \
947refused (the server was busy or restarting): run the same command again.\n";
948
949/// **A repository's settings, over the forge's door** (owner, 2026-10-14):
950/// `gunnar-repo show '<namespace>/<repository>'`,
951/// `gunnar-repo visibility '<namespace>/<repository>' public|private`,
952/// `gunnar-repo default-branch '<namespace>/<repository>' '<refs/heads/…>'`.
953///
954/// What the client `gunnar repo show|visibility|default-branch` asks. On an
955/// installed estate the forge's SSH door (tcp/2222 through the front) is the
956/// one door a person's key opens; the control plane is bound to the WireGuard
957/// overlay. The verb decides NOTHING itself: it hands the authenticated
958/// principal and the ask to [`RepoDoor`], which `gunnar-server` answers with
959/// the SAME `Repos.Get` / `Repos.SetVisibility` / `Repos.SetDefaultBranch`
960/// handlers the control plane mounts — so the account-admin rule for
961/// visibility, the repository-Admin rule for the default branch, the audit
962/// event and the browse replica's notice are the gRPC door's, not a copy.
963/// Output is `key value` lines on stdout, exit 0; a refusal is
964/// `error: <why>` on stdout, exit [`EXIT_DENIED`]. An anonymous session is
965/// refused the channel. Not a git verb: it never enters
966/// [`Dispatch::authorise`] or the transfer path.
967pub const REPO_VERB: &str = "gunnar-repo";
968
969/// What a [`REPO_VERB`] line asks.
970#[derive(Debug, Clone, PartialEq, Eq)]
971pub enum RepoAction {
972    /// `Repos.Get`.
973    Show,
974    /// `Repos.SetVisibility`: `true` is public.
975    Visibility(bool),
976    /// `Repos.SetDefaultBranch`, the branch exactly as sent (the server
977    /// validates it; the client expands a short name).
978    DefaultBranch(String),
979    /// `Repos.Create` of an empty repository in this object format (`true`
980    /// is sha256): what a client that knows its own format asks before its
981    /// first push, since a push-made repository can only be sha1.
982    Create {
983        /// `true`: a sha256 repository; `false`: sha1.
984        sha256: bool,
985    },
986}
987
988/// One [`REPO_VERB`] ask, with who asked: the principal the session's key
989/// authenticated as (re-read at the exec, never cached) and that key's
990/// fingerprint.
991#[derive(Debug, Clone, PartialEq, Eq)]
992pub struct RepoAsk {
993    /// The principal the session's key authenticated as.
994    pub principal: String,
995    /// That key's fingerprint.
996    pub fingerprint: String,
997    /// The repository the verb names.
998    pub repo: RepoRef,
999    /// The verb itself.
1000    pub action: RepoAction,
1001}
1002
1003/// What answers [`REPO_VERB`]: the ask in, the `key value` lines or the
1004/// refusal's sentence out.
1005pub type RepoDoor = Arc<
1006    dyn Fn(
1007            RepoAsk,
1008        )
1009            -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<String, String>> + Send>>
1010        + Send
1011        + Sync,
1012>;
1013
1014/// Parse a [`REPO_VERB`] exec line. `None`: not this verb. `Some(Err)`: this
1015/// verb, malformed. Each argument may be single-quoted the way git quotes a
1016/// path; the path goes through the one sandbox
1017/// ([`gunnar_core::resolve_repo_path`]).
1018pub fn parse_repo_verb(raw: &[u8]) -> Option<Result<(RepoRef, RepoAction), String>> {
1019    let text = std::str::from_utf8(raw).ok()?;
1020    let mut tokens = text.split(' ').filter(|t| !t.is_empty());
1021    if tokens.next()? != REPO_VERB {
1022        return None;
1023    }
1024    let usage = || {
1025        format!(
1026            "the line is not `{REPO_VERB} show <namespace>/<repository>`, \
1027             `{REPO_VERB} visibility <namespace>/<repository> public|private` or \
1028             `{REPO_VERB} default-branch <namespace>/<repository> <branch>` or \
1029             `{REPO_VERB} create <namespace>/<repository> sha1|sha256`"
1030        )
1031    };
1032    if raw.len() > crate::command::MAX_EXEC_LEN || raw.iter().any(|b| *b < 0x20 || *b == 0x7f) {
1033        return Some(Err(usage()));
1034    }
1035    let mut args = Vec::new();
1036    for token in tokens {
1037        match crate::command::unquote(token) {
1038            Ok(arg) => args.push(arg),
1039            Err(_) => return Some(Err(usage())),
1040        }
1041    }
1042    let (sub, path, value) = match args.as_slice() {
1043        [sub, path] => (sub.as_str(), path.as_str(), None),
1044        [sub, path, value] => (sub.as_str(), path.as_str(), Some(value.as_str())),
1045        _ => return Some(Err(usage())),
1046    };
1047    let action = match (sub, value) {
1048        ("show", None) => RepoAction::Show,
1049        ("visibility", Some("public")) => RepoAction::Visibility(true),
1050        ("visibility", Some("private")) => RepoAction::Visibility(false),
1051        ("visibility", Some(other)) => {
1052            return Some(Err(format!(
1053                "visibility is `public` or `private`, not `{}`",
1054                other.escape_default()
1055            )))
1056        }
1057        ("default-branch", Some(branch)) if !branch.is_empty() => {
1058            RepoAction::DefaultBranch(branch.to_string())
1059        }
1060        ("create", Some("sha256")) => RepoAction::Create { sha256: true },
1061        ("create", Some("sha1")) => RepoAction::Create { sha256: false },
1062        ("create", Some(other)) => {
1063            return Some(Err(format!(
1064                "an object format is `sha1` or `sha256`, not `{}`",
1065                other.escape_default()
1066            )))
1067        }
1068        _ => return Some(Err(usage())),
1069    };
1070    let repo = match gunnar_core::resolve_repo_path(path) {
1071        Ok(repo) => repo,
1072        Err(err) => return Some(Err(err.to_string())),
1073    };
1074    Some(Ok((repo, action)))
1075}
1076
1077/// Parse a [`RECIPIENT_KEYS_VERB`] exec line. `None`: not this verb.
1078/// `Some(Err)`: this verb, malformed. The path and the member may each be
1079/// single-quoted the way git quotes a path; the path goes through the one
1080/// sandbox ([`gunnar_core::resolve_repo_path`]) and the member through the
1081/// principal id rule.
1082pub fn parse_recipient_keys_verb(raw: &[u8]) -> Option<Result<(RepoRef, String), String>> {
1083    let text = std::str::from_utf8(raw).ok()?;
1084    let (verb, rest) = text.split_once(' ').unwrap_or((text, ""));
1085    if verb != RECIPIENT_KEYS_VERB {
1086        return None;
1087    }
1088    let usage =
1089        || format!("the line is not `{RECIPIENT_KEYS_VERB} <namespace>/<repository> <member>`");
1090    if raw.len() > crate::command::MAX_EXEC_LEN || raw.iter().any(|b| *b < 0x20 || *b == 0x7f) {
1091        return Some(Err(usage()));
1092    }
1093    let Some((path, member)) = rest.trim().rsplit_once(' ') else {
1094        return Some(Err(usage()));
1095    };
1096    let (Ok(path), Ok(member)) = (
1097        crate::command::unquote(path.trim()),
1098        crate::command::unquote(member.trim()),
1099    ) else {
1100        return Some(Err(usage()));
1101    };
1102    let repo = match gunnar_core::resolve_repo_path(&path) {
1103        Ok(repo) => repo,
1104        Err(err) => return Some(Err(err.to_string())),
1105    };
1106    if gunnar_core::Principal::new(
1107        member.clone(),
1108        member.clone(),
1109        gunnar_core::PrincipalKind::User,
1110    )
1111    .is_err()
1112    {
1113        return Some(Err(format!(
1114            "`{}` is not an account name",
1115            member.escape_default()
1116        )));
1117    }
1118    Some(Ok((repo, member)))
1119}
1120
1121/// **The answer to [`RECIPIENT_KEYS_VERB`]** for `caller`: `(exit status,
1122/// stdout)`. Reads the identity table and the catalog; changes nothing.
1123pub fn recipient_keys_answer(
1124    control: &Control,
1125    caller: &str,
1126    repo: &RepoRef,
1127    member: &str,
1128) -> (u32, String) {
1129    if control.is_twin() {
1130        let said = DispatchError::Twin {
1131            principal: caller.to_string(),
1132            repo: repo.clone(),
1133        }
1134        .client_message()
1135        .unwrap_or_default();
1136        return (EXIT_DENIED, format!("error: {said}\n"));
1137    }
1138    let hidden = || {
1139        let said = DispatchError::NoSuchRepo(repo.clone())
1140            .client_message()
1141            .unwrap_or_default();
1142        (EXIT_DENIED, format!("error: {said}\n"))
1143    };
1144    // The member is told Gunnar could not answer; what broke is the operator's (the journal).
1145    let fault = |err: gunnar_core::Error| {
1146        tracing::error!(target: "gunnar::ssh", event = "ssh.recipient_keys.fault", %err, "gunnar-recipient-keys: the identity table or the catalog could not be read");
1147        (
1148            EXIT_DENIED,
1149            "error: Gunnar could not answer this just now; try again in a while\n".to_string(),
1150        )
1151    };
1152    let entry = match control.catalog().get(repo) {
1153        Ok(Some(entry)) => entry,
1154        Ok(None) => return hidden(),
1155        Err(err) => return fault(err),
1156    };
1157    match control.auth().may(caller, &entry, gunnar_core::Level::Read) {
1158        Ok(true) => {}
1159        Ok(false) => return hidden(),
1160        Err(err) => return fault(err),
1161    }
1162    match control.auth().may(member, &entry, gunnar_core::Level::Read) {
1163        Ok(true) => {}
1164        Ok(false) => {
1165            return (
1166                EXIT_DENIED,
1167                format!(
1168                    "error: {member} cannot read {}/{}: grant them access before adding them as a member of its key ring\n",
1169                    repo.namespace(),
1170                    repo.name()
1171                ),
1172            )
1173        }
1174        Err(err) => return fault(err),
1175    }
1176    // **The member's ONE encryption key** (CRYPT-YUBI 2026-10-26): a ring is wrapped to the
1177    // person's encryption public key, never to an SSH key.
1178    let Some(keys) = control.encryption_keys() else {
1179        return (
1180            EXIT_DENIED,
1181            "error: this Gunnar keeps no encryption keys\n".to_string(),
1182        );
1183    };
1184    match keys.public_of(member) {
1185        Ok(Some(public)) => (0, format!("{}\n", public.trim())),
1186        Ok(None) => (
1187            EXIT_DENIED,
1188            format!(
1189                "error: {member} has no encryption key yet: {member} makes one with `gunnar crypt key new` \
1190                 (or by adding a security key on their Account page), then the ring can be wrapped to it\n"
1191            ),
1192        ),
1193        Err(gunnar_core::encryption_key::KeyRefusal::Store(err)) => fault(err),
1194        Err(other) => (EXIT_DENIED, format!("error: {other}\n")),
1195    }
1196}
1197
1198/// **The answer to [`gunnar_crypt::door::VERB`]** (`gunnar-user-key`, CRYPT-YUBI) for
1199/// `caller`, the session's own principal: `(exit status, stdout)`. Reads or changes the
1200/// caller's own encryption key only; there is no name to give.
1201pub fn user_key_answer(
1202    control: &Control,
1203    principal: &gunnar_core::Principal,
1204    request: gunnar_crypt::door::Request,
1205) -> (u32, String) {
1206    let caller = principal.id.as_str();
1207    use gunnar_core::encryption_key::{KeyRefusal, UnlockMethod};
1208    use gunnar_crypt::door::Request;
1209    if control.is_twin() {
1210        return (
1211            EXIT_DENIED,
1212            "error: this is a twin; ask the primary\n".to_string(),
1213        );
1214    }
1215    let Some(keys) = control.encryption_keys() else {
1216        return (
1217            EXIT_DENIED,
1218            "error: this Gunnar keeps no encryption keys\n".to_string(),
1219        );
1220    };
1221    let now = std::time::SystemTime::now()
1222        .duration_since(std::time::UNIX_EPOCH)
1223        .map(|d| d.as_millis() as u64)
1224        .unwrap_or(0);
1225    let said = |r: Result<gunnar_core::encryption_key::EncryptionKey, KeyRefusal>| match r {
1226        Ok(key) => (0, key.to_answer().text()),
1227        Err(KeyRefusal::Store(err)) => {
1228            tracing::error!(target: "gunnar::ssh", event = "ssh.user_key.fault", %err, "gunnar-user-key: the store could not be read or written");
1229            (
1230                EXIT_DENIED,
1231                "error: Gunnar could not answer this just now; try again in a while\n".to_string(),
1232            )
1233        }
1234        Err(other) => (EXIT_DENIED, format!("error: {other}\n")),
1235    };
1236    match request {
1237        Request::Get => match keys.get(caller) {
1238            Ok(Some(key)) => (0, key.to_answer().text()),
1239            Ok(None) => (0, String::new()),
1240            Err(e) => said(Err(e)),
1241        },
1242        // The account's audit, the same kinds the gRPC door writes (RA4-3).
1243        Request::Create { public_key, method } => {
1244            let kind = method.kind.clone();
1245            let r = UnlockMethod::from_line(method)
1246                .and_then(|m| keys.create(caller, &public_key, m, now));
1247            if r.is_ok() {
1248                control.encryption_key_event(
1249                    principal,
1250                    "encryption-key.created",
1251                    &format!("first method {kind}"),
1252                );
1253                tracing::info!(target: "gunnar::audit", event = "access.encryption_key_created", principal = %caller, method = %kind, "an encryption key was created");
1254            }
1255            said(r)
1256        }
1257        Request::Add(method) => {
1258            let kind = method.kind.clone();
1259            let r = UnlockMethod::from_line(method).and_then(|m| keys.add_method(caller, m, now));
1260            if r.is_ok() {
1261                control.encryption_key_event(principal, "encryption-key.method-added", &kind);
1262                tracing::info!(target: "gunnar::audit", event = "access.unlock_method_added", principal = %caller, method = %kind, "an unlock method was added");
1263            }
1264            said(r)
1265        }
1266        Request::Remove(id) => {
1267            let r = keys.remove_method(caller, &id);
1268            if r.is_ok() {
1269                control.encryption_key_event(principal, "encryption-key.method-removed", &id);
1270                tracing::info!(target: "gunnar::audit", event = "access.unlock_method_removed", principal = %caller, method_id = %id, "an unlock method was removed");
1271            }
1272            said(r)
1273        }
1274    }
1275}
1276
1277/// What answers [`TUNNEL_ADDRESS_VERB`]: the address (empty = a read) in, the
1278/// answer's lines or the refusal out.
1279pub type TunnelAddressDoor = Arc<dyn Fn(&str) -> Result<String, String> + Send + Sync>;
1280
1281/// Parse a [`TUNNEL_ADDRESS_VERB`] exec line. `None`: not this verb. `Some(Err)`:
1282/// this verb, malformed. Printable ASCII only, at most one argument, short.
1283pub fn parse_tunnel_address_verb(raw: &[u8]) -> Option<Result<String, String>> {
1284    let text = std::str::from_utf8(raw).ok()?;
1285    let mut tokens = text.split(' ').filter(|t| !t.is_empty());
1286    if tokens.next()? != TUNNEL_ADDRESS_VERB {
1287        return None;
1288    }
1289    if raw.len() > 64 || !raw.iter().all(|b| (0x20..0x7f).contains(b)) {
1290        return Some(Err(
1291            "the line is not `gunnar-tunnel-address [a.b.c.d/prefix]`".to_string(),
1292        ));
1293    }
1294    let address = tokens.next().unwrap_or("").to_string();
1295    if tokens.next().is_some() {
1296        return Some(Err(
1297            "one argument at most: `gunnar-tunnel-address [a.b.c.d/prefix]`".to_string(),
1298        ));
1299    }
1300    Some(Ok(address))
1301}
1302
1303/// **The key exchanges gunnar's SSH door offers, post-quantum FIRST**
1304/// (2026-10-06, the PQ transport lane).
1305///
1306/// `mlkem768x25519-sha256` (draft-ietf-sshm-mlkem-hybrid-kex: ML-KEM-768 and
1307/// X25519, the session key from both) is what OpenSSH ≥ 9.9 offers first and
1308/// 10.0 makes its default — russh 0.63 runs it on RustCrypto's `ml-kem`. The
1309/// classical exchanges stay after it, so an older client still connects
1310/// (preferred, never required). `sntrup761x25519-sha512` is not here: russh
1311/// does not implement it.
1312///
1313/// **gunnar's own list, not russh's default**, though the two agree today:
1314/// a russh bump that reorders or drops its `Preferred::DEFAULT` must not
1315/// change what this door offers without a red test here. SSH lets the
1316/// CLIENT's order decide; the server's job is to offer the hybrid at all.
1317pub static KEX_ORDER: &[russh::kex::Name] = &[
1318    russh::kex::MLKEM768X25519_SHA256,
1319    russh::kex::CURVE25519,
1320    russh::kex::CURVE25519_PRE_RFC_8731,
1321    russh::kex::DH_GEX_SHA256,
1322    russh::kex::DH_G18_SHA512,
1323    russh::kex::DH_G17_SHA512,
1324    russh::kex::DH_G16_SHA512,
1325    russh::kex::DH_G15_SHA512,
1326    russh::kex::DH_G14_SHA256,
1327    russh::kex::EXTENSION_SUPPORT_AS_CLIENT,
1328    russh::kex::EXTENSION_SUPPORT_AS_SERVER,
1329    russh::kex::EXTENSION_OPENSSH_STRICT_KEX_AS_CLIENT,
1330    russh::kex::EXTENSION_OPENSSH_STRICT_KEX_AS_SERVER,
1331];
1332
1333/// russh's algorithm preferences with gunnar's [`KEX_ORDER`]; ciphers, MACs
1334/// and host-key algorithms stay russh's.
1335pub fn preferred() -> russh::Preferred {
1336    russh::Preferred {
1337        kex: std::borrow::Cow::Borrowed(KEX_ORDER),
1338        ..russh::Preferred::DEFAULT
1339    }
1340}
1341
1342/// gunnar's SSH listener.
1343pub struct GunnarSsh {
1344    config: SshConfig,
1345    shared: Arc<Shared>,
1346    /// **A connection that arrives at a refused LOCAL address is dropped**
1347    /// before a byte is spoken (`gunnar serve --wg-only`: the utility
1348    /// network's address — every box-to-box connection runs over the tunnel).
1349    refused_local: Option<RefusedLocal>,
1350}
1351
1352/// Is a connection that arrived at this local address refused?
1353pub type RefusedLocal = Arc<dyn Fn(std::net::IpAddr) -> bool + Send + Sync>;
1354
1355impl GunnarSsh {
1356    /// Build a server over the control plane and the wire seam.
1357    pub fn new(config: SshConfig, control: Control, transport: Arc<dyn GitTransport>) -> Self {
1358        let exec_grace = config.exec_grace;
1359        let allow_anonymous = config.allow_anonymous;
1360        let config_max_auth_attempts = config.max_auth_attempts;
1361        let session_end_grace = (config.session_end_grace, config.session_end_grace_crowded);
1362        let transfer_idle = config.transfer_idle_timeout;
1363        // The memory budget travels with the two concurrency numbers: one
1364        // constructor, so a configuration cannot arrive with a governed
1365        // concurrency limit and an unmetered budget. `None` falls back to the
1366        // shipped constant rather than to "unlimited" — an operator who did not
1367        // set a budget wants the default, not the removal of a gate.
1368        let budget = config
1369            .memory_budget_bytes
1370            .unwrap_or(crate::governor::DEFAULT_MEMORY_BUDGET_BYTES);
1371        let wait = config
1372            .admission_wait
1373            .unwrap_or(crate::governor::DEFAULT_ADMISSION_WAIT);
1374        let admission = Arc::new(
1375            if config.per_principal_limit_is_hard {
1376                Admission::hard_capped(
1377                    config.max_concurrent_transfers,
1378                    config.max_transfers_per_principal,
1379                )
1380                .with_budget(budget)
1381            } else {
1382                Admission::with_memory_budget(
1383                    config.max_concurrent_transfers,
1384                    config.max_transfers_per_principal,
1385                    budget,
1386                )
1387            }
1388            // The anonymous door and its width are configured together, so
1389            // an appliance that opens the door cannot forget the ceiling.
1390            .with_anonymous_ceiling(config.max_anonymous_transfers)
1391            .with_wait(wait)
1392            .with_keyed_wait(config.keyed_admission_wait),
1393        );
1394        // The usage share (`admit-share`): the configured policy, unseeded
1395        // until `with_flow` hands it the durable ledger to seed from.
1396        #[cfg(feature = "admit-share")]
1397        let admission = Arc::new(
1398            Arc::try_unwrap(admission)
1399                .expect("the gate is not shared yet")
1400                .with_policy(config.admit.build()),
1401        );
1402        GunnarSsh {
1403            config,
1404            refused_local: None,
1405            shared: Arc::new(Shared {
1406                dispatch: Dispatch::new(control),
1407                transport,
1408                exec_grace,
1409                allow_anonymous,
1410                admission,
1411                metrics: Arc::new(ServerMetrics::new()),
1412                // Ready unless somebody hands us a flag to watch. A listener
1413                // built by a test has nothing to wait for, and a default of
1414                // "not ready" would make every such test hang on a sentence
1415                // about a boot that never happened.
1416                ready: Arc::new(std::sync::atomic::AtomicBool::new(true)),
1417                config_max_auth_attempts,
1418                session_end_grace,
1419                transfer_idle,
1420                tunnel_door: None,
1421                repo_door: None,
1422            }),
1423        }
1424    }
1425
1426    /// **Watch a readiness flag**, and answer git traffic with a *retryable*
1427    /// sentence until it is set.
1428    ///
1429    /// # The boot window — MEASURED 2026-08-30
1430    ///
1431    /// After a power cut the appliance answered ssh :2222 and grpc :50051
1432    /// **0.6–0.7 s before its stores had finished their post-crash open**, and
1433    /// a read in that window came back `the repository is unavailable; try
1434    /// again`. That sentence is `gunnar_wire::Error::Store`'s, it arrives with
1435    /// a *denial* exit status, and it is what a client is told when a
1436    /// repository is genuinely broken — so a harness could not tell "not yet"
1437    /// from "gone". The first power-cut door reported **64 acked pushes
1438    /// missing**; a boot one second later found every one of them, and the same
1439    /// window wedged a whole bench run's clone and fetch phases.
1440    ///
1441    /// The fix is not to bind later — PID 1 must answer quickly or an operator
1442    /// watching a console sees a dead box — but to answer **honestly**. While
1443    /// the flag is clear every git exec is refused in git's own protocol with
1444    /// [`EXIT_BUSY`] (75, EX_TEMPFAIL — the same status the admission gate
1445    /// uses, which is the status that means *retry*) and a sentence containing
1446    /// the word **starting**, which is the word a script may key on. Nothing is
1447    /// served, nothing is opened, and nothing looks like a missing repository.
1448    ///
1449    /// A flag and not a trait: `gunnar-ssh` may not read the operating system,
1450    /// and "have the stores finished opening" is `gunnar-server`'s fact to
1451    /// publish, not this crate's to discover.
1452    pub fn with_readiness(mut self, ready: Arc<std::sync::atomic::AtomicBool>) -> Self {
1453        // `Arc::get_mut` cannot be used: `bind` has not run, but `Shared` is
1454        // already behind an `Arc`. Rebuilding it is one allocation at start-up
1455        // and keeps every field in one place.
1456        let shared = Arc::try_unwrap(self.shared).unwrap_or_else(|shared| {
1457            panic!(
1458                "readiness must be set before the listener is shared: {} strong refs",
1459                Arc::strong_count(&shared)
1460            )
1461        });
1462        self.shared = Arc::new(Shared { ready, ..shared });
1463        self
1464    }
1465
1466    /// **Refuse every connection that arrives at a local address `refused`
1467    /// names** — closed at accept, nothing said, logged once per peer at
1468    /// debug. `gunnar serve --wg-only` names the utility address: the public's
1469    /// git clients still reach SSH, a box over plain IP does not.
1470    pub fn with_refused_local(mut self, refused: RefusedLocal) -> Self {
1471        self.refused_local = Some(refused);
1472        self
1473    }
1474
1475    /// **Answer [`TUNNEL_ADDRESS_VERB`]** with `door` — `gunnar-server` hands in
1476    /// its `appliance::tunnel_address_answer`, the function both gRPC doors
1477    /// call. Without one the verb is answered "no WireGuard tunnel".
1478    pub fn with_tunnel_address_door(mut self, door: TunnelAddressDoor) -> Self {
1479        let shared = Arc::try_unwrap(self.shared).unwrap_or_else(|shared| {
1480            panic!(
1481                "the tunnel door must be set before the listener is shared: {} strong refs",
1482                Arc::strong_count(&shared)
1483            )
1484        });
1485        self.shared = Arc::new(Shared {
1486            tunnel_door: Some(door),
1487            ..shared
1488        });
1489        self
1490    }
1491
1492    /// **Answer [`REPO_VERB`]** with `door` — `gunnar-server` hands in the
1493    /// control plane's own `Repos` handlers. Without one the verb is answered
1494    /// "this server answers repository settings only on its control plane".
1495    pub fn with_repo_door(mut self, door: RepoDoor) -> Self {
1496        let shared = Arc::try_unwrap(self.shared).unwrap_or_else(|shared| {
1497            panic!(
1498                "the repository door must be set before the listener is shared: {} strong refs",
1499                Arc::strong_count(&shared)
1500            )
1501        });
1502        self.shared = Arc::new(Shared {
1503            repo_door: Some(door),
1504            ..shared
1505        });
1506        self
1507    }
1508
1509    /// **Give the admission gate the per-namespace flow ledger.**
1510    ///
1511    /// The gate is what all three front doors — this exec line, smart HTTP and
1512    /// the gRPC transfer gate — ask before a transfer, so it is the one place a
1513    /// refusal can be charged to a tenant once rather than three times. The
1514    /// exec line's own `no-grant` denial reaches the same ledger back through
1515    /// `Admission::flow`.
1516    ///
1517    /// A listener built without one refuses exactly as it always did and counts
1518    /// the refusal only in its own `AdmissionStats`, which is what a test that
1519    /// drives `gunnar-ssh` alone wants — and what every build before the ledger
1520    /// existed did.
1521    ///
1522    /// The gate is rebuilt rather than mutated for the reason
1523    /// [`Self::with_readiness`] rebuilds `Shared`: `bind` has not run, but the
1524    /// `Arc` is already there.
1525    pub fn with_flow(mut self, flow: Arc<gunnar_core::FlowLedger>) -> Self {
1526        let shared =
1527            Arc::get_mut(&mut self.shared).expect("the shared state is not published until `bind`");
1528        let gate = Arc::try_unwrap(std::mem::replace(
1529            &mut shared.admission,
1530            Arc::new(Admission::unlimited()),
1531        ))
1532        .unwrap_or_else(|gate| {
1533            panic!(
1534                "the flow ledger must be set before the gate is shared: {} strong refs",
1535                Arc::strong_count(&gate)
1536            )
1537        });
1538        // admit-share: the policy is rebuilt SEEDED from this ledger — its
1539        // table is then a cache of the ledger, filled once per namespace.
1540        #[cfg(feature = "admit-share")]
1541        let gate = gate.with_policy(
1542            self.config
1543                .admit
1544                .build_seeded(Some(Admission::flow_seed(Arc::clone(&flow)))),
1545        );
1546        shared.admission = Arc::new(gate.with_flow(flow));
1547        self
1548    }
1549
1550    /// Share the server-wide counters with the rest of the process.
1551    ///
1552    /// **Not a second set of counters.** `gunnar serve` mints one
1553    /// [`ServerMetrics`] and hands the same `Arc` to this listener and to the
1554    /// git transport, so "connections refused" and "pushes rejected by gate"
1555    /// are numbers about one server rather than two crates' private tallies.
1556    /// A listener built without this one still counts, into a set nobody else
1557    /// can read, which is what a test that only drives `gunnar-ssh` wants.
1558    pub fn with_metrics(mut self, metrics: Arc<ServerMetrics>) -> Self {
1559        let shared =
1560            Arc::get_mut(&mut self.shared).expect("the shared state is not published until `bind`");
1561        shared.metrics = metrics;
1562        self
1563    }
1564
1565    /// The counters this listener writes to.
1566    pub fn metrics(&self) -> &Arc<ServerMetrics> {
1567        &self.shared.metrics
1568    }
1569
1570    /// The russh configuration this server runs with.
1571    ///
1572    /// `methods` is publickey **only**, and stays that way even with the
1573    /// anonymous path open: `none` is available for exactly one username,
1574    /// so advertising it to everybody would make the failure list a lie and
1575    /// invite every client to try something that will not work. russh calls
1576    /// [`Handler::auth_none`] on the client's opening probe regardless of what
1577    /// is advertised, which is the only place it is needed.
1578    ///
1579    /// `password` is not offered and there is no handler for it, so the
1580    /// rejecting default is never the last line of defence.
1581    fn russh_config(&self, host_key: russh::keys::PrivateKey) -> RusshConfig {
1582        RusshConfig {
1583            server_id: russh::SshId::Standard(self.config.server_id.clone().into()),
1584            preferred: preferred(),
1585            methods: MethodSet::from(&[MethodKind::PublicKey][..]),
1586            auth_rejection_time: self.config.auth_rejection_time,
1587            auth_rejection_time_initial: Some(Duration::ZERO),
1588            keys: vec![host_key],
1589            max_auth_attempts: self.config.max_auth_attempts,
1590            inactivity_timeout: self.config.inactivity_timeout,
1591            keepalive_interval: self.config.keepalive_interval,
1592            nodelay: true,
1593            ..RusshConfig::default()
1594        }
1595    }
1596
1597    /// Bind the listener and start accepting.
1598    ///
1599    /// Returns once the socket is bound, so a caller (or a test) knows the
1600    /// real address before the first client connects. That matters for
1601    /// [`SshConfig::ephemeral_loopback`], where the port is chosen by the
1602    /// kernel.
1603    pub async fn bind(self, host_key: russh::keys::PrivateKey) -> std::io::Result<BoundServer> {
1604        let listener = TcpListener::bind(self.config.bind).await?;
1605        let local_addr = listener.local_addr()?;
1606        let russh_config = Arc::new(self.russh_config(host_key));
1607        let shared = self.shared.clone();
1608        let admission = Arc::clone(&self.shared.admission);
1609        let metrics = Arc::clone(&self.shared.metrics);
1610        let (handle, _) = tokio::sync::broadcast::channel::<String>(1);
1611
1612        let join = tokio::spawn(accept_loop(
1613            listener,
1614            russh_config,
1615            Factory { shared },
1616            handle.clone(),
1617            self.refused_local.clone(),
1618        ));
1619
1620        tracing::info!(
1621            target: TARGET_SSH,
1622            %local_addr,
1623            max_concurrent_transfers = ?admission.max_concurrent(),
1624            max_transfers_per_principal = ?admission.max_per_principal(),
1625            per_principal_is_hard = admission.per_principal_is_hard(),
1626            "gunnar ssh listener bound"
1627        );
1628        Ok(BoundServer {
1629            local_addr,
1630            handle,
1631            join,
1632            admission,
1633            metrics,
1634        })
1635    }
1636}
1637
1638/// How long the accept loop waits after the kernel refused it a descriptor,
1639/// before it asks again. A session ending in that window frees what the next
1640/// `accept` needs; nothing shorter gains anything, nothing longer is felt.
1641pub const ACCEPT_BACKOFF: Duration = Duration::from_millis(100);
1642
1643/// Is this `accept` failure the kind the loop must outlive?
1644///
1645/// The resource ones — no descriptor left in the process (`EMFILE`) or in the
1646/// system (`ENFILE`), no buffer or memory for the socket — and the one the
1647/// client causes, `ECONNABORTED` (it hung up between SYN and `accept`).
1648/// Everything else (`EBADF`, `EINVAL`, a closed listener) is the listener
1649/// itself being gone, and the loop must return that.
1650pub fn accept_error_is_transient(err: &std::io::Error) -> bool {
1651    // Linux errno values, spelled here rather than through a `libc` crate this
1652    // crate does not otherwise carry: ENOMEM 12, ENFILE 23, EMFILE 24,
1653    // ECONNABORTED 103, ENOBUFS 105.
1654    matches!(err.raw_os_error(), Some(12 | 23 | 24 | 103 | 105))
1655        || matches!(
1656            err.kind(),
1657            std::io::ErrorKind::ConnectionAborted | std::io::ErrorKind::OutOfMemory
1658        )
1659}
1660
1661/// **The accept loop, owned here so that it cannot die of `EMFILE`.**
1662///
1663/// russh's `run_on_socket` is the same loop with one difference that cost a
1664/// whole appliance: its `Err(e) => return Err(e)` on `accept`. MEASURED
1665/// 2026-08-29 on the rig: the 202nd repository touched since boot ran the
1666/// process out of descriptors, the next `accept` failed with `EMFILE`, the
1667/// loop returned, and every later client got `kex_exchange_identification:
1668/// read: Connection reset by peer` until reboot — the kernel kept completing
1669/// handshakes into a backlog nobody drained — while gRPC on its own listener
1670/// answered as if nothing were wrong. A server that has no descriptor for the
1671/// next client must say so and try again in a moment, not resign.
1672///
1673/// So on a transient failure ([`accept_error_is_transient`]) it logs a WARN
1674/// on `gunnar::ssh` — a row on the appliance dashboard — sleeps
1675/// [`ACCEPT_BACKOFF`] and continues. Everything else is what russh did: one
1676/// task per connection, `set_nodelay`, `run_stream`, the session raced against
1677/// the shutdown broadcast so `BoundServer::shutdown` still disconnects every
1678/// live session by application, and session errors go through
1679/// [`note_session_end`] exactly as `Server::handle_session_error` did.
1680async fn accept_loop(
1681    listener: TcpListener,
1682    config: Arc<RusshConfig>,
1683    mut factory: Factory,
1684    shutdown: tokio::sync::broadcast::Sender<String>,
1685    refused_local: Option<RefusedLocal>,
1686) -> std::io::Result<()> {
1687    let mut shutdown_rx = shutdown.subscribe();
1688    let mut refused_in_a_row: u32 = 0;
1689    loop {
1690        tokio::select! {
1691            _ = shutdown_rx.recv() => {
1692                tracing::debug!(target: TARGET_SSH, "ssh listener shutdown requested");
1693                return Ok(());
1694            }
1695            accepted = listener.accept() => {
1696                let (socket, peer) = match accepted {
1697                    Ok(pair) => pair,
1698                    Err(err) if accept_error_is_transient(&err) => {
1699                        refused_in_a_row += 1;
1700                        tracing::warn!(
1701                            target: TARGET_SSH,
1702                            event = "ssh.accept.refused",
1703                            error = %err,
1704                            refused_in_a_row,
1705                            "ssh accept failed: {err}; the listener stays up and retries in {} ms",
1706                            ACCEPT_BACKOFF.as_millis()
1707                        );
1708                        tokio::time::sleep(ACCEPT_BACKOFF).await;
1709                        continue;
1710                    }
1711                    Err(err) => {
1712                        tracing::error!(
1713                            target: TARGET_SSH,
1714                            event = "ssh.accept.failed",
1715                            error = %err,
1716                            "ssh accept failed with an error the listener cannot outlive"
1717                        );
1718                        return Err(err);
1719                    }
1720                };
1721                if let (Some(refused), Ok(local)) = (&refused_local, socket.local_addr()) {
1722                    if refused(local.ip()) {
1723                        tracing::debug!(target: TARGET_SSH, event = "ssh.accept.plain_ip_refused", %peer, %local, "a connection at the utility address is refused (--wg-only): boxes speak over the tunnel");
1724                        drop(socket);
1725                        continue;
1726                    }
1727                }
1728                if refused_in_a_row > 0 {
1729                    tracing::info!(
1730                        target: TARGET_SSH,
1731                        event = "ssh.accept.recovered",
1732                        refused_in_a_row,
1733                        "ssh accept works again"
1734                    );
1735                    refused_in_a_row = 0;
1736                }
1737                let handler = factory.new_client(Some(peer));
1738                let config = Arc::clone(&config);
1739                let mut session_shutdown = shutdown.subscribe();
1740                tokio::spawn(async move {
1741                    if config.nodelay {
1742                        if let Err(err) = socket.set_nodelay(true) {
1743                            tracing::debug!(target: TARGET_SSH, %err, "set_nodelay failed");
1744                        }
1745                    }
1746                    let socket = HandshakeQuickAck::new(socket);
1747                    let session = match russh::server::run_stream(config, socket, handler).await {
1748                        Ok(session) => session,
1749                        Err(err) => {
1750                            note_session_end(&err);
1751                            return;
1752                        }
1753                    };
1754                    let handle = session.handle();
1755                    tokio::select! {
1756                        reason = session_shutdown.recv() => {
1757                            let reason = reason.unwrap_or_default();
1758                            let _ = handle
1759                                .disconnect(russh::Disconnect::ByApplication, reason, String::new())
1760                                .await;
1761                        }
1762                        ended = session => {
1763                            if let Err(err) = ended {
1764                                note_session_end(&err);
1765                            }
1766                        }
1767                    }
1768                });
1769            }
1770        }
1771    }
1772}
1773
1774/// **The accepted socket, with `TCP_QUICKACK` re-armed before each of its first reads**
1775/// (GUN-PERF, 2026-10-31).
1776///
1777/// OpenSSH turns `TCP_NODELAY` on only after authentication, so in the key exchange its
1778/// KEXINIT and its key-exchange init are two writes under Nagle: the second waits for
1779/// the first to be acknowledged. This server has already sent its own KEXINIT by then
1780/// and has nothing to carry that ACK, so the kernel delays it, about 40 ms, on most
1781/// connections. Every `git` over SSH paid it (`ls-remote` 50 to 70 ms over SSH against
1782/// 15 to 20 ms over HTTP). Quick-ACK mode is not sticky (the kernel leaves it again
1783/// after it sends), so it is re-armed before each read of the handshake, and only
1784/// there: the bulk transfer after it keeps the kernel's own ACK pacing.
1785struct HandshakeQuickAck {
1786    inner: tokio::net::TcpStream,
1787    reads_left: u8,
1788}
1789
1790impl HandshakeQuickAck {
1791    /// The key exchange and the authentication are well inside this many reads.
1792    const HANDSHAKE_READS: u8 = 32;
1793
1794    fn new(inner: tokio::net::TcpStream) -> Self {
1795        HandshakeQuickAck {
1796            inner,
1797            reads_left: Self::HANDSHAKE_READS,
1798        }
1799    }
1800
1801    /// A send is what puts the kernel back into delayed-ACK mode (it reads the exchange
1802    /// as interactive), and the client answers a send: so in the handshake each send is
1803    /// followed by quick-ACK again, before that answer arrives.
1804    fn rearm_after_send(&self, wrote: &std::task::Poll<std::io::Result<usize>>) {
1805        if self.reads_left > 0 && matches!(wrote, std::task::Poll::Ready(Ok(n)) if *n > 0) {
1806            let _ = self.inner.set_quickack(true);
1807        }
1808    }
1809}
1810
1811impl tokio::io::AsyncRead for HandshakeQuickAck {
1812    fn poll_read(
1813        mut self: std::pin::Pin<&mut Self>,
1814        cx: &mut std::task::Context<'_>,
1815        buf: &mut tokio::io::ReadBuf<'_>,
1816    ) -> std::task::Poll<std::io::Result<()>> {
1817        if self.reads_left > 0 {
1818            self.reads_left -= 1;
1819            // Only slower when it fails; never a reason to drop the connection.
1820            let _ = self.inner.set_quickack(true);
1821        }
1822        std::pin::Pin::new(&mut self.inner).poll_read(cx, buf)
1823    }
1824}
1825
1826impl tokio::io::AsyncWrite for HandshakeQuickAck {
1827    fn poll_write(
1828        mut self: std::pin::Pin<&mut Self>,
1829        cx: &mut std::task::Context<'_>,
1830        buf: &[u8],
1831    ) -> std::task::Poll<std::io::Result<usize>> {
1832        let wrote = std::pin::Pin::new(&mut self.inner).poll_write(cx, buf);
1833        self.rearm_after_send(&wrote);
1834        wrote
1835    }
1836    fn poll_write_vectored(
1837        mut self: std::pin::Pin<&mut Self>,
1838        cx: &mut std::task::Context<'_>,
1839        bufs: &[std::io::IoSlice<'_>],
1840    ) -> std::task::Poll<std::io::Result<usize>> {
1841        let wrote = std::pin::Pin::new(&mut self.inner).poll_write_vectored(cx, bufs);
1842        self.rearm_after_send(&wrote);
1843        wrote
1844    }
1845    fn is_write_vectored(&self) -> bool {
1846        self.inner.is_write_vectored()
1847    }
1848    fn poll_flush(
1849        mut self: std::pin::Pin<&mut Self>,
1850        cx: &mut std::task::Context<'_>,
1851    ) -> std::task::Poll<std::io::Result<()>> {
1852        std::pin::Pin::new(&mut self.inner).poll_flush(cx)
1853    }
1854    fn poll_shutdown(
1855        mut self: std::pin::Pin<&mut Self>,
1856        cx: &mut std::task::Context<'_>,
1857    ) -> std::task::Poll<std::io::Result<()>> {
1858        std::pin::Pin::new(&mut self.inner).poll_shutdown(cx)
1859    }
1860}
1861
1862/// A bound, running listener.
1863pub struct BoundServer {
1864    local_addr: SocketAddr,
1865    /// The shutdown broadcast every live session and the accept loop listen on.
1866    handle: tokio::sync::broadcast::Sender<String>,
1867    join: JoinHandle<std::io::Result<()>>,
1868    admission: Arc<Admission>,
1869    metrics: Arc<ServerMetrics>,
1870}
1871
1872impl BoundServer {
1873    /// The address actually bound, with the kernel's choice of port filled in.
1874    pub fn local_addr(&self) -> SocketAddr {
1875        self.local_addr
1876    }
1877
1878    /// What the admission gate has done since this listener started.
1879    ///
1880    /// This is the visibility half of the fix. A limit whose saturation cannot
1881    /// be read is the same undiagnosable "git is slow" the accidental 512-thread
1882    /// ceiling produced; `admitted`, `refused_*` and `peak_in_flight` are what
1883    /// an operator, or a test, looks at instead.
1884    pub fn admission(&self) -> AdmissionStats {
1885        self.admission.stats()
1886    }
1887
1888    /// The admission gate itself, so a **second transport can route through the
1889    /// SAME one** (one implementation).
1890    ///
1891    /// `gunnar-http` is a second front door onto the same server, and a git
1892    /// transfer over HTTP has to count against the same server-wide and
1893    /// per-principal ceilings — and the same memory budget — as one over SSH,
1894    /// or an appliance's real concurrency is silently twice the number written
1895    /// on the box. Handing out the `Arc` (rather than rebuilding a gate from
1896    /// the same numbers) is what makes it one gate with one set of counters,
1897    /// not two that happen to agree: on 2026-08-31 the HTTP front was found with no gate
1898    /// at all, and two gates would have been the next-worst answer.
1899    pub fn admission_gate(&self) -> Arc<Admission> {
1900        Arc::clone(&self.admission)
1901    }
1902
1903    /// The server-wide counters.
1904    pub fn metrics(&self) -> gunnar_core::observe::MetricsSnapshot {
1905        self.metrics.snapshot()
1906    }
1907
1908    /// The counter block itself, for a caller that wants to keep reading it
1909    /// after this listener is consumed by [`BoundServer::shutdown`].
1910    pub fn metrics_handle(&self) -> &Arc<ServerMetrics> {
1911        &self.metrics
1912    }
1913
1914    /// Ask every session to disconnect, then wait for the accept loop to stop
1915    /// — for at most [`SSH_SHUTDOWN_GRACE`].
1916    ///
1917    /// **Bounded, the way the gRPC half already is** (`GRPC_SHUTDOWN_GRACE` in
1918    /// gunnar-server): the accept loop only ends when its sessions do, and a
1919    /// session whose peer never answers the disconnect — MEASURED 2026-09-28 on
1920    /// loki, a primary's replication session into a standby being stopped, 1 run
1921    /// in 17 under a parallel test binary — held `shutdown` for an hour. A
1922    /// shutdown that can hang forever is a restart, a drain or a growth that can
1923    /// hang forever. Past the grace the listener task is aborted and the reason
1924    /// is logged; the sessions go with the process.
1925    pub async fn shutdown(self, reason: &str) -> std::io::Result<()> {
1926        self.shutdown_within(reason, SSH_SHUTDOWN_GRACE).await
1927    }
1928
1929    /// [`Self::shutdown`] with an explicit grace (tests use a short one).
1930    pub async fn shutdown_within(self, reason: &str, grace: Duration) -> std::io::Result<()> {
1931        let _ = self.handle.send(reason.to_string());
1932        let abort = self.join.abort_handle();
1933        match tokio::time::timeout(grace, self.join).await {
1934            Ok(Ok(result)) => result,
1935            Ok(Err(err)) if err.is_cancelled() => Ok(()),
1936            Ok(Err(err)) => Err(std::io::Error::other(err)),
1937            Err(_elapsed) => {
1938                tracing::warn!(
1939                    target: TARGET_SSH,
1940                    ?grace,
1941                    "the SSH listener did not stop within its grace: a session did not \
1942                     answer the disconnect. Aborting the listener so the shutdown completes."
1943                );
1944                abort.abort();
1945                Ok(())
1946            }
1947        }
1948    }
1949}
1950
1951struct Factory {
1952    shared: Arc<Shared>,
1953}
1954
1955impl Server for Factory {
1956    type Handler = Connection;
1957
1958    fn new_client(&mut self, peer_addr: Option<SocketAddr>) -> Connection {
1959        // ── ONE span per connection, and one id ────────────────────────────
1960        //
1961        // `principal` and `repo` are declared `Empty` and filled in by
1962        // `auth_publickey` and `exec_request` as they become known, so every
1963        // line emitted under this connection carries all three WITHOUT the
1964        // call site repeating them. Before this, connection identity in the
1965        // SSH logs was an ad-hoc `?self.peer` copy-pasted to fourteen call
1966        // sites, and there was no way at all to tie two lines together.
1967        let conn = RequestId::next();
1968        let span = tracing::info_span!(
1969            target: TARGET_SSH,
1970            "ssh.connection",
1971            %conn,
1972            peer = tracing::field::Empty,
1973            principal = tracing::field::Empty,
1974            repo = tracing::field::Empty,
1975        );
1976        if let Some(peer) = peer_addr {
1977            span.record("peer", tracing::field::display(peer));
1978        }
1979        self.shared.metrics.add_connections_opened(1);
1980        span.in_scope(|| tracing::debug!(target: TARGET_SSH, "ssh connection accepted"));
1981        Connection {
1982            shared: self.shared.clone(),
1983            span,
1984            principal: None,
1985            key_fingerprint: None,
1986            channels: HashMap::new(),
1987            lfs_slots: HashMap::new(),
1988            exec_seen: Arc::new(AtomicBool::new(false)),
1989            misuse_said: false,
1990            auth_attempts: 0,
1991            session_over: tokio::sync::watch::channel(false).0,
1992        }
1993    }
1994
1995    /// The session loop stopped. Usually because the client hung up, which is
1996    /// how every git client ends a transfer and is not news.
1997    ///
1998    /// A free function does the deciding, for the same reason
1999    /// [`note_teardown`] is one: the rule can then be asserted without standing
2000    /// up a russh session, and what is asserted is the line an operator reads.
2001    fn handle_session_error(&mut self, error: anyhow::Error) {
2002        note_session_end(&error);
2003    }
2004}
2005
2006/// How many `git-lfs-transfer` channels of one connection share one admission slot
2007/// ([`Connection::lfs_slots`]): git-lfs's pool is `lfs.concurrenttransfers` + 1 (9 by
2008/// default); 32 leaves room for a client that raised it, and bounds what one slot carries.
2009pub const LFS_CHANNELS_PER_SLOT: usize = 32;
2010
2011/// **How long a transfer waits for a slot before it is told it waits**
2012/// (GUN-SSH-SLOT-HANG): a wait the client cannot see is a hang to whoever runs it.
2013const WAIT_NOTICE_AFTER: Duration = Duration::from_millis(500);
2014
2015/// One open session channel, before its exec request arrives.
2016struct OpenChannel {
2017    /// The channel itself. The exec request carries only a [`ChannelId`], and
2018    /// the byte stream can only be taken from the `Channel`.
2019    channel: Channel<Msg>,
2020    /// The value of `GIT_PROTOCOL`, if the client sent it and
2021    /// [`ENV_ALLOWLIST`] accepted the name. Kept beside the channel it was sent
2022    /// on so that it cannot outlive it or be read for a different one.
2023    git_protocol: Option<String>,
2024}
2025
2026/// One client connection.
2027struct Connection {
2028    shared: Arc<Shared>,
2029    /// The `ssh.connection` span every line from this connection is emitted
2030    /// under. Cloned (cheap: it is an id, not the data) rather than borrowed
2031    /// at each entry point, because entering it borrows `self` immutably while
2032    /// every handler needs `&mut self`.
2033    span: tracing::Span,
2034    /// Set by `auth_publickey`, and the only thing that authorises anything.
2035    principal: Option<Principal>,
2036    /// The key `auth_publickey` authenticated this session with.
2037    ///
2038    /// Beside `principal` rather than inside it, because a principal is a
2039    /// server-side identity that may hold several keys and this is the one that
2040    /// made *this* connection. Stays `None` for the anonymous path, which
2041    /// authenticated with no key at all.
2042    key_fingerprint: Option<Fingerprint>,
2043    channels: HashMap<ChannelId, OpenChannel>,
2044    /// **The admission slot this connection's `git-lfs-transfer` channels share**, per
2045    /// repository and operation (GHUNT-7, 2026-10-28). Stock git-lfs multiplexes its
2046    /// pure-SSH pool over ONE connection (`lfs.ssh.automultiplex`): `concurrenttransfers`
2047    /// (8) + 1 channels, opened at once and kept open, mostly idle. A slot per channel let
2048    /// one git-lfs on a box whose limit is under nine wait for slots its own idle channels
2049    /// held: the push failed after the keyed wait, or hung. One client, one slot; a slot
2050    /// carries at most [`LFS_CHANNELS_PER_SLOT`] channels, past that a channel asks the
2051    /// gate as any transfer does.
2052    lfs_slots: HashMap<(String, GitService), std::sync::Weak<crate::admission::AdmissionGuard>>,
2053    exec_seen: Arc<AtomicBool>,
2054    /// A request for a channel this connection does not hold open (a client's second
2055    /// `exec` on one channel) was said already: WARN once per connection, then quiet —
2056    /// a client's protocol misuse is never the server's ERROR (stinkies ssh-double-exec-error).
2057    misuse_said: bool,
2058    /// **How many publickey offers this connection has had refused.**
2059    ///
2060    /// Counted here because russh does not count it. `SshConfig::    /// max_auth_attempts` is handed to russh 0.61.2's `Config`, which declares
2061    /// the field, defaults it to 10 and `Debug`-prints it — and never compares
2062    /// it to anything. VERIFIED by reading the whole of that crate's source:
2063    /// the only three occurrences are the declaration, the default and the
2064    /// `Debug` impl. (russh 0.63 does compare it, and carries tests named for
2065    /// exactly this; gunnar is pinned to 0.61.2 through an exact `ssh-key`
2066    /// dependency, and a limit that works is not worth an SSH-stack bump to
2067    /// obtain.)
2068    ///
2069    /// So until 2026-09-11 a connection could offer keys without bound: a
2070    /// dictionary of public keys against the identity table, at
2071    /// `auth_rejection_time` apiece, on one TCP connection that never had to be
2072    /// re-established.
2073    auth_attempts: usize,
2074    /// **Set when this session ends** — by `Drop`, because russh drops the
2075    /// handler on every way a session ends (hang-up, unanswered keepalives,
2076    /// inactivity, protocol error, shutdown). Every transfer's writer watches
2077    /// it ([`SessionBound`]): russh's own writer parks on the SSH window and
2078    /// nothing wakes it when the session dies, which left a fetch to a dead
2079    /// client holding its fan worker and admission slot forever (cov-13
2080    /// finding 4, 2026-10-06).
2081    session_over: tokio::sync::watch::Sender<bool>,
2082}
2083
2084impl Drop for Connection {
2085    fn drop(&mut self) {
2086        self.session_over.send_replace(true);
2087    }
2088}
2089
2090/// Why a session that authenticated may no longer act.
2091///
2092/// Two variants and not a `bool`, for [`Control::principal_for_key`]'s reason:
2093/// "nobody holds this key any more" and "the identity table could not be read"
2094/// call for entirely different operator action, and both refuse.
2095#[derive(Debug)]
2096enum Stale {
2097    /// The key that opened this session no longer resolves to anyone, or
2098    /// resolves to somebody else. Revoked, expired, or re-registered.
2099    Revoked,
2100    /// The identity table could not be read. Refused, because failing open is
2101    /// not on the table — the same answer `auth_publickey` gives.
2102    Unreadable(gunnar_core::Error),
2103}
2104
2105impl Connection {
2106    /// **Who this session may act as right now — asked again, per exec.**
2107    ///
2108    /// `auth_publickey` caches the principal, and until 2026-09-11 every exec
2109    /// on the connection was authorised out of that cache. Revocation
2110    /// therefore took effect immediately for a NEW connection and never for an
2111    /// open one: `DocRef::read` caches nothing, so the identity table was
2112    /// current the whole time and nothing asked it. A long-lived session — and
2113    /// git over SSH holds one open for as long as a client wants to keep
2114    /// pushing — outlived the credential that opened it, for as long as it
2115    /// liked. An operator who revokes a leaked key and sees the revocation take
2116    /// effect on their own next connection has every reason to believe it took
2117    /// effect everywhere.
2118    ///
2119    /// Cheap enough to do per exec: it is the same single
2120    /// `principal_by_fingerprint` lookup the connection already pays once at
2121    /// auth, and an exec is about to fork a git process and move a pack.
2122    ///
2123    /// A key that now belongs to a DIFFERENT identity is refused rather than
2124    /// adopted. This session authenticated as somebody; if the key has since
2125    /// been registered to somebody else, this is not that session, and
2126    /// continuing as the new holder would make re-registration a way to inherit
2127    /// an open connection.
2128    ///
2129    /// The anonymous path has no fingerprint — it authenticated with no key at
2130    /// all — so there is nothing to re-read, and `Principal::anonymous()` is not
2131    /// in the identity table to be revoked from. It keeps the cached value, and
2132    /// what anonymity may DO is still `Dispatch::authorise`'s answer.
2133    fn live_principal(&self) -> Result<Principal, Stale> {
2134        let Some(cached) = self.principal.clone() else {
2135            return Err(Stale::Revoked);
2136        };
2137        let Some(fingerprint) = self.key_fingerprint.as_ref() else {
2138            return Ok(cached);
2139        };
2140        match self
2141            .shared
2142            .dispatch
2143            .control()
2144            .principal_for_key(fingerprint)
2145        {
2146            Ok(Some(now)) if now.id == cached.id => Ok(now),
2147            Ok(_) => Err(Stale::Revoked),
2148            Err(err) => Err(Stale::Unreadable(err)),
2149        }
2150    }
2151
2152    /// **Count a refused publickey offer, and say whether this connection has
2153    /// had its last one.**
2154    ///
2155    /// One place, called from every refusing arm of `auth_publickey`, so a new
2156    /// refusal reason cannot arrive uncounted — which is how a limit stops
2157    /// applying to exactly the case somebody added last.
2158    ///
2159    /// `0` is no limit, which is russh's own reading of the same field and the
2160    /// reading an operator who writes `0` expects.
2161    ///
2162    /// The `none` probe is deliberately NOT counted. Every OpenSSH client opens
2163    /// with one to learn the method list, so counting it would spend one of the
2164    /// six on the ordinary first packet of an ordinary session — and it carries
2165    /// no key, so it cannot be the dictionary attempt the limit is for.
2166    fn refused_auth(&mut self) -> bool {
2167        self.auth_attempts += 1;
2168        let max = self.shared.config_max_auth_attempts;
2169        max > 0 && self.auth_attempts >= max
2170    }
2171
2172    /// The error that ends a connection which has offered too many keys.
2173    ///
2174    /// An `Err` out of a handler, because russh 0.61.2's `auth_publickey` is
2175    /// handed no session and no handle, so there is nothing to call
2176    /// `disconnect` on — and the run loop treating a handler error as the end
2177    /// of the session is exactly the outcome wanted. It is a `warn!`, not a
2178    /// `debug!`: one of these is a client with a full agent, and a hundred is
2179    /// somebody reading the identity table one key at a time.
2180    fn too_many_auth_attempts(&self) -> anyhow::Error {
2181        tracing::warn!(
2182            target: TARGET_SSH,
2183            event = "ssh.auth.too_many",
2184            attempts = self.auth_attempts,
2185            max = self.shared.config_max_auth_attempts,
2186            "disconnecting: too many refused publickey offers on one connection"
2187        );
2188        self.shared.metrics.add_connections_refused(1);
2189        anyhow::anyhow!(
2190            "too many authentication failures ({} refused publickey offers)",
2191            self.auth_attempts
2192        )
2193    }
2194}
2195
2196impl Handler for Connection {
2197    type Error = anyhow::Error;
2198
2199    /// **The anonymous read path.** Accept the `none` method for exactly
2200    /// one username, and refuse it for every other.
2201    ///
2202    /// The refusing branch is byte-for-byte what russh's default handler did
2203    /// before this method existed: `Auth::reject()`, which makes russh drop
2204    /// `MethodKind::None` from the advertised set and let the client proceed to
2205    /// publickey. So a client that does not ask for anonymity is on exactly the
2206    /// path it was on yesterday.
2207    ///
2208    /// The accepting branch does **not** consult the catalog, the repository or
2209    /// the ACL: it establishes *who* is asking (nobody), and *what nobody may
2210    /// do* is `Dispatch::authorise`'s answer, through
2211    /// [`AuthTable::may`](gunnar_core::AuthTable::may), like everyone else.
2212    /// Deciding here would be a second ACL.
2213    async fn auth_none(&mut self, user: &str) -> Result<Auth, Self::Error> {
2214        let span = self.span.clone();
2215        let _entered = span.enter();
2216        if !self.shared.allow_anonymous || user != ANONYMOUS_SSH_USER {
2217            // Not a refusal worth a `warn!`: EVERY OpenSSH client opens with a
2218            // `none` probe to learn the method list, so this is the ordinary
2219            // first packet of an ordinary authenticated session.
2220            tracing::debug!(
2221                target: TARGET_SSH,
2222                event = "ssh.auth.none.refused",
2223                reason = if self.shared.allow_anonymous {
2224                    "not-the-anonymous-user"
2225                } else {
2226                    "anonymous-access-disabled"
2227                },
2228                decorative_ssh_user = %user,
2229                "the none method is refused; publickey is the only way in"
2230            );
2231            return Ok(Auth::reject());
2232        }
2233
2234        let anonymous = Principal::anonymous();
2235        span.record("principal", tracing::field::display(&anonymous.id));
2236        self.shared.metrics.add_connections_anonymous(1);
2237        tracing::info!(
2238            target: TARGET_SSH,
2239            event = "ssh.auth.anonymous",
2240            // Named so nobody later reads this line as "a key was accepted".
2241            // It is on `gunnar::ssh` at `info` because "who is reading my
2242            // public repositories" must be answerable from a production log.
2243            authenticated = false,
2244            "anonymous session accepted: no key, public repositories only"
2245        );
2246        self.principal = Some(anonymous);
2247        Ok(Auth::Accept)
2248    }
2249
2250    async fn auth_publickey(
2251        &mut self,
2252        user: &str,
2253        key: russh::server::VerifiedPublicKey<'_>,
2254    ) -> Result<Auth, Self::Error> {
2255        // russh 0.65: the key arrives with the algorithm its signature was verified
2256        // under; the identity is the key alone, as before.
2257        let public_key: &PublicKey = key.public_key;
2258        let span = self.span.clone();
2259        let _entered = span.enter();
2260        // Over the key's WIRE BLOB, not over its type: russh's `ssh-key` is
2261        // 0.7.0-rc.10 and `gunnar-core`'s is 0.6.7, so this is the only value
2262        // that can reach the identity table. It is byte-for-byte the
2263        // string `ssh-keygen -lf` prints for the same key.
2264        //
2265        // `None` means the key would not re-encode. That used to be an
2266        // `expect()`, a panic in the auth handler, over client-supplied bytes.
2267        // It is a refusal now; `fingerprint_of_key` has already
2268        // logged why.
2269        let Some(fingerprint) = fingerprint_of_key(public_key) else {
2270            let refusal = refuse_unencodable_key(&self.shared.metrics, user);
2271            if self.refused_auth() {
2272                return Err(self.too_many_auth_attempts());
2273            }
2274            return Ok(refusal);
2275        };
2276        let started = std::time::Instant::now();
2277        // `peer` is a field of the enclosing span from here down, so it is not
2278        // repeated on any line below (one implementation, and the whole point of the span).
2279        match self
2280            .shared
2281            .dispatch
2282            .control()
2283            .principal_for_key(&fingerprint)
2284        {
2285            Ok(Some(principal)) => {
2286                span.record("principal", tracing::field::display(&principal.id));
2287                self.shared.metrics.add_connections_accepted(1);
2288                tracing::info!(
2289                    target: TARGET_SSH,
2290                    event = "ssh.auth.accepted",
2291                    %fingerprint,
2292                    // Logged so an operator can see what was sent, and named so
2293                    // nobody later mistakes it for an input to the decision.
2294                    decorative_ssh_user = %user,
2295                    elapsed_ms = started.elapsed().as_millis() as u64,
2296                    "publickey accepted"
2297                );
2298                self.principal = Some(principal);
2299                // Kept, not recomputed and not dropped. This is the
2300                // value the identity table was probed with one line above, so
2301                // it is the key that authenticated, by construction.
2302                self.key_fingerprint = Some(fingerprint);
2303                Ok(Auth::Accept)
2304            }
2305            Ok(None) => {
2306                self.shared.metrics.add_connections_refused(1);
2307                tracing::warn!(
2308                    target: TARGET_SSH,
2309                    event = "ssh.auth.refused",
2310                    reason = "unknown-key",
2311                    %fingerprint,
2312                    decorative_ssh_user = %user,
2313                    elapsed_ms = started.elapsed().as_millis() as u64,
2314                    "publickey refused: no principal holds this key"
2315                );
2316                if self.refused_auth() {
2317                    return Err(self.too_many_auth_attempts());
2318                }
2319                Ok(Auth::reject())
2320            }
2321            // The key is still refused, since failing open is not on the table, but
2322            // this is a FAULT, not "unknown key", and the log has to say so.
2323            // An identity table that has become unreadable looks exactly like
2324            // an empty one from the client's side (the ACL model).
2325            Err(err) => {
2326                self.shared.metrics.add_connections_refused(1);
2327                self.shared.metrics.add_auth_faults(1);
2328                tracing::error!(
2329                    target: TARGET_SSH,
2330                    event = "ssh.auth.refused",
2331                    reason = "identity-table-unreadable",
2332                    %fingerprint,
2333                    decorative_ssh_user = %user,
2334                    elapsed_ms = started.elapsed().as_millis() as u64,
2335                    %err,
2336                    "publickey refused: the identity table could not be read"
2337                );
2338                if self.refused_auth() {
2339                    return Err(self.too_many_auth_attempts());
2340                }
2341                Ok(Auth::reject())
2342            }
2343        }
2344    }
2345
2346    async fn channel_open_session(
2347        &mut self,
2348        channel: Channel<Msg>,
2349        // russh 0.63 (2026-10-03): the open is answered through this handle, not
2350        // a returned bool; dropping it unanswered would refuse the channel.
2351        reply: russh::server::ChannelOpenHandle,
2352        session: &mut Session,
2353    ) -> Result<(), Self::Error> {
2354        // The exec request arrives later, carrying only a `ChannelId`, and the
2355        // byte stream can only be taken from the `Channel` itself, so it is
2356        // kept here. russh's own sftp example does the same.
2357        self.channels.insert(
2358            channel.id(),
2359            OpenChannel {
2360                channel,
2361                git_protocol: None,
2362            },
2363        );
2364
2365        // A session that authenticates, opens a channel and then does nothing is
2366        // not idle, because it answers keepalives, so the inactivity timeout never
2367        // fires and the connection would be held indefinitely. Arm a watchdog.
2368        if let Some(grace) = self.shared.exec_grace {
2369            let handle = session.handle();
2370            let exec_seen = self.exec_seen.clone();
2371            // `.instrument`, not a captured `peer`: a spawned task does NOT
2372            // inherit the thread-local span, so the watchdog's line would
2373            // otherwise be the one line in the whole connection that could not
2374            // be tied back to it.
2375            let span = self.span.clone();
2376            tokio::spawn(
2377                async move {
2378                    tokio::time::sleep(grace).await;
2379                    if !exec_seen.load(Ordering::SeqCst) {
2380                        tracing::info!(
2381                            target: TARGET_SSH,
2382                            event = "ssh.watchdog.disconnect",
2383                            ?grace,
2384                            "disconnecting: authenticated but never asked for a git command"
2385                        );
2386                        note_teardown(
2387                            "watchdog-disconnect",
2388                            handle
2389                                .disconnect(
2390                                    Disconnect::ByApplication,
2391                                    "no git command requested".to_string(),
2392                                    String::new(),
2393                                )
2394                                .await,
2395                        );
2396                    }
2397                }
2398                .instrument(span),
2399            );
2400        }
2401        reply.accept().await;
2402        Ok(())
2403    }
2404
2405    async fn exec_request(
2406        &mut self,
2407        channel: ChannelId,
2408        data: &[u8],
2409        session: &mut Session,
2410    ) -> Result<(), Self::Error> {
2411        self.exec_seen.store(true, Ordering::SeqCst);
2412        let span = self.span.clone();
2413        let _entered = span.enter();
2414
2415        // **The credential is re-read here, not remembered.** See
2416        // [`Connection::live_principal`]: russh will not dispatch an exec
2417        // before authentication succeeds, so an unauthenticated session is
2418        // unreachable rather than checked — but a session whose key was
2419        // REVOKED while it was open is entirely reachable, and until
2420        // 2026-09-11 it was authorised out of a cache filled at auth time.
2421        let principal = match self.live_principal() {
2422            Ok(principal) => principal,
2423            Err(stale) => {
2424                self.shared.metrics.add_exec_refused(1);
2425                let (reason, detail) = match &stale {
2426                    Stale::Revoked => ("key-revoked", String::new()),
2427                    Stale::Unreadable(err) => ("identity-table-unreadable", format!("{err}")),
2428                };
2429                tracing::warn!(
2430                    target: TARGET_SSH,
2431                    event = "ssh.exec.denied",
2432                    reason,
2433                    fingerprint = self.key_fingerprint.as_ref().map(tracing::field::display),
2434                    detail = %detail,
2435                    "exec denied: the key that opened this session no longer authorises it"
2436                );
2437                // Answered in git's own protocol, like every other denial on
2438                // this path: an `ERR ` pkt-line is what `git` prints verbatim
2439                // after `remote: `. A bare `channel_failure` here would put the
2440                // client back on "Could not read from remote repository", which
2441                // is the failure `report_error` was found unused for.
2442                session.channel_success(channel)?;
2443                session.data(channel, err_packet(REVOKED_MESSAGE)?)?;
2444                session.exit_status_request(channel, EXIT_DENIED)?;
2445                session.close(channel)?;
2446                // And the session goes with it. Refusing the one exec would
2447                // leave the connection open for the next one, which is the
2448                // finding restated a request later: a revoked key must not keep
2449                // a channel it can keep asking on.
2450                note_teardown(
2451                    "revoked-key-disconnect",
2452                    session
2453                        .handle()
2454                        .disconnect(
2455                            Disconnect::ByApplication,
2456                            "the key that opened this session no longer authorises it".to_string(),
2457                            String::new(),
2458                        )
2459                        .await,
2460                );
2461                return Ok(());
2462            }
2463        };
2464
2465        // ── the operator's fire drill: ONE ERROR on purpose ─────────────
2466        //
2467        // What the estate's watch raises to prove its alert path end to end (estate-box
2468        // 8A). Held to server-wide Admin before it says anything; an anonymous session
2469        // is refused the channel like the tunnel verb below.
2470        if std::str::from_utf8(data).map(str::trim) == Ok(FIRE_DRILL_VERB) {
2471            if self.key_fingerprint.is_none() {
2472                tracing::warn!(target: TARGET_SSH, event = "ssh.exec.refused", reason = "anonymous-control-verb", "exec refused: {FIRE_DRILL_VERB} needs a key");
2473                session.channel_failure(channel)?;
2474                return Ok(());
2475            }
2476            let (exit, out) = match self
2477                .shared
2478                .dispatch
2479                .control()
2480                .auth()
2481                .may_server(&principal.id, gunnar_core::Level::Admin)
2482            {
2483                Ok(true) => {
2484                    tracing::error!(
2485                        target: TARGET_SSH,
2486                        event = "ssh.fire_drill",
2487                        principal = %principal.id,
2488                        "fire drill: {} asked for this ERROR to prove the alert path end to end — nothing is wrong",
2489                        principal.id
2490                    );
2491                    (
2492                        0,
2493                        "fire drill: one ERROR said on this server's log (nothing is wrong)\n"
2494                            .to_string(),
2495                    )
2496                }
2497                Ok(false) => {
2498                    tracing::warn!(target: TARGET_SSH, event = "ssh.exec.denied", reason = "not-server-admin", principal = %principal.id, "{FIRE_DRILL_VERB} refused");
2499                    (EXIT_DENIED, format!("error: {FIRE_DRILL_VERB} needs server-wide Admin, which {} does not hold\n", principal.id))
2500                }
2501                Err(err) => (
2502                    EXIT_DENIED,
2503                    format!("error: the identity table could not be read: {err}\n"),
2504                ),
2505            };
2506            session.channel_success(channel)?;
2507            session.data(channel, out.into_bytes())?;
2508            session.exit_status_request(channel, exit)?;
2509            session.close(channel)?;
2510            return Ok(());
2511        }
2512
2513        // ── git-lfs's fallback: answered, never a channel failure (2026-10-25) ──
2514        if std::str::from_utf8(data)
2515            .ok()
2516            .and_then(|l| l.split_whitespace().next())
2517            == Some(LFS_AUTHENTICATE_VERB)
2518        {
2519            tracing::info!(
2520                target: TARGET_SSH,
2521                event = "ssh.lfs_authenticate.answered",
2522                principal = %principal.id,
2523                command = %String::from_utf8_lossy(data),
2524                "git-lfs asked for its HTTPS fallback: its pure-SSH transfer failed or was refused just before"
2525            );
2526            session.channel_success(channel)?;
2527            session.extended_data(channel, 1, LFS_AUTHENTICATE_MESSAGE.as_bytes().to_vec())?;
2528            session.exit_status_request(channel, 1)?;
2529            session.close(channel)?;
2530            return Ok(());
2531        }
2532
2533        // ── who this key is (wiring tracer B 2026-10-08) ──
2534        if std::str::from_utf8(data).map(str::trim) == Ok(WHOAMI_VERB) {
2535            if self.key_fingerprint.is_none() {
2536                tracing::warn!(target: TARGET_SSH, event = "ssh.exec.refused", reason = "anonymous-control-verb", "exec refused: {WHOAMI_VERB} needs a key");
2537                session.channel_failure(channel)?;
2538                return Ok(());
2539            }
2540            session.channel_success(channel)?;
2541            session.data(channel, format!("{}\n", principal.id).into_bytes())?;
2542            session.exit_status_request(channel, 0)?;
2543            session.close(channel)?;
2544            return Ok(());
2545        }
2546
2547        // ── a repository's settings (owner, 2026-10-14) ──
2548        //
2549        // Decided by the control plane's own `Repos` handlers through
2550        // [`RepoDoor`]: this listener only proves who asks (the key, re-read
2551        // above) and carries the answer. An anonymous session is refused the
2552        // channel, before the pause and the plan gates (not a transfer).
2553        if let Some(parsed) = parse_repo_verb(data) {
2554            let Some(fingerprint) = self.key_fingerprint.as_ref().map(|f| f.to_string()) else {
2555                tracing::warn!(target: TARGET_SSH, event = "ssh.exec.refused", reason = "anonymous-control-verb", "exec refused: {REPO_VERB} needs a key");
2556                session.channel_failure(channel)?;
2557                return Ok(());
2558            };
2559            let (exit, out) = match parsed {
2560                Err(why) => (1, format!("error: {why}\n")),
2561                Ok((repo, action)) => match self.shared.repo_door.clone() {
2562                    None => (
2563                        1,
2564                        "error: this server answers repository settings only on its control plane\n".to_string(),
2565                    ),
2566                    Some(door) => {
2567                        let ask = RepoAsk { principal: principal.id.clone(), fingerprint, repo: repo.clone(), action: action.clone() };
2568                        let answer = door(ask).await;
2569                        tracing::info!(
2570                            target: TARGET_SSH,
2571                            event = "ssh.control",
2572                            verb = REPO_VERB,
2573                            principal = %principal.id,
2574                            repo = %repo,
2575                            action = ?action,
2576                            decision = if answer.is_ok() { "allow" } else { "deny" },
2577                            "repository settings asked over the forge's door"
2578                        );
2579                        match answer {
2580                            Ok(said) => (0, if said.ends_with('\n') { said } else { format!("{said}\n") }),
2581                            Err(why) => (EXIT_DENIED, format!("error: {why}\n")),
2582                        }
2583                    }
2584                },
2585            };
2586            session.channel_success(channel)?;
2587            session.data(channel, out.into_bytes())?;
2588            session.exit_status_request(channel, exit)?;
2589            session.close(channel)?;
2590            return Ok(());
2591        }
2592
2593        // ── a member's keys for an encrypted repository's ring (2026-10-06) ──
2594        //
2595        // A read of the identity table, held to the same rule as
2596        // `Repos.RecipientKeys` (see [`recipient_keys_answer`]). Before the
2597        // pause and the plan gates for the tunnel verb's reason: not a
2598        // transfer. An anonymous session is refused the channel.
2599        if let Some(parsed) = parse_recipient_keys_verb(data) {
2600            if self.key_fingerprint.is_none() {
2601                tracing::warn!(target: TARGET_SSH, event = "ssh.exec.refused", reason = "anonymous-control-verb", "exec refused: {RECIPIENT_KEYS_VERB} needs a key");
2602                session.channel_failure(channel)?;
2603                return Ok(());
2604            }
2605            let (exit, out) = match parsed {
2606                Err(why) => (1, format!("error: {why}\n")),
2607                Ok((repo, member)) => {
2608                    let (exit, out) = recipient_keys_answer(
2609                        self.shared.dispatch.control(),
2610                        &principal.id,
2611                        &repo,
2612                        &member,
2613                    );
2614                    tracing::info!(
2615                        target: TARGET_SSH,
2616                        event = "ssh.control",
2617                        verb = RECIPIENT_KEYS_VERB,
2618                        principal = %principal.id,
2619                        repo = %repo,
2620                        member = %member,
2621                        decision = if exit == 0 { "allow" } else { "deny" },
2622                        "recipient keys asked over the forge's door"
2623                    );
2624                    (exit, out)
2625                }
2626            };
2627            session.channel_success(channel)?;
2628            session.data(channel, out.into_bytes())?;
2629            session.exit_status_request(channel, exit)?;
2630            session.close(channel)?;
2631            return Ok(());
2632        }
2633
2634        // ── the caller's own encryption key (CRYPT-YUBI 2026-10-26) ──────
2635        //
2636        // The session's principal reads or changes ITS OWN key and methods: the
2637        // public half, and wraps that are ciphertext to this server. Not a
2638        // transfer; an anonymous session is refused the channel.
2639        if let Some(parsed) = gunnar_crypt::door::parse_request(data) {
2640            if self.key_fingerprint.is_none() {
2641                tracing::warn!(target: TARGET_SSH, event = "ssh.exec.refused", reason = "anonymous-control-verb", "exec refused: {} needs a key", gunnar_crypt::door::VERB);
2642                session.channel_failure(channel)?;
2643                return Ok(());
2644            }
2645            let (exit, out) = match parsed {
2646                Err(why) => (1, format!("error: {why}\n")),
2647                Ok(request) => {
2648                    let verb = match &request {
2649                        gunnar_crypt::door::Request::Get => "get",
2650                        gunnar_crypt::door::Request::Create { .. } => "create",
2651                        gunnar_crypt::door::Request::Add(_) => "add",
2652                        gunnar_crypt::door::Request::Remove(_) => "remove",
2653                    };
2654                    let (exit, out) =
2655                        user_key_answer(self.shared.dispatch.control(), &principal, request);
2656                    tracing::info!(
2657                        target: TARGET_SSH,
2658                        event = "ssh.control",
2659                        verb = gunnar_crypt::door::VERB,
2660                        action = verb,
2661                        principal = %principal.id,
2662                        decision = if exit == 0 { "allow" } else { "deny" },
2663                        "own encryption key over the forge's door"
2664                    );
2665                    (exit, out)
2666                }
2667            };
2668            session.channel_success(channel)?;
2669            session.data(channel, out.into_bytes())?;
2670            session.exit_status_request(channel, exit)?;
2671            session.close(channel)?;
2672            return Ok(());
2673        }
2674
2675        // ── the ONE control verb: the tunnel address (T17) ─────────────
2676        //
2677        // Before the pause and the plan gates: those are about transfers, and
2678        // this is not one. Never through `Dispatch::authorise`, which knows
2679        // only git verbs. An anonymous session gets the channel refused like a
2680        // shell would; everyone else is held to server-wide Admin BEFORE the
2681        // door runs.
2682        if let Some(parsed) = parse_tunnel_address_verb(data) {
2683            if self.key_fingerprint.is_none() {
2684                tracing::warn!(target: TARGET_SSH, event = "ssh.exec.refused", reason = "anonymous-control-verb", "exec refused: {TUNNEL_ADDRESS_VERB} needs a key");
2685                session.channel_failure(channel)?;
2686                return Ok(());
2687            }
2688            let (exit, out) = match self
2689                .shared
2690                .dispatch
2691                .control()
2692                .auth()
2693                .may_server(&principal.id, gunnar_core::Level::Admin)
2694            {
2695                Ok(false) => {
2696                    tracing::warn!(target: TARGET_SSH, event = "ssh.exec.denied", reason = "not-server-admin", principal = %principal.id, "{TUNNEL_ADDRESS_VERB} refused");
2697                    (EXIT_DENIED, format!("error: {TUNNEL_ADDRESS_VERB} needs server-wide Admin, which {} does not hold\n", principal.id))
2698                }
2699                Err(err) => (
2700                    EXIT_DENIED,
2701                    format!("error: the identity table could not be read: {err}\n"),
2702                ),
2703                Ok(true) => match parsed {
2704                    Err(why) => (1, format!("error: {why}\n")),
2705                    Ok(address) => match &self.shared.tunnel_door {
2706                        None => (
2707                            1,
2708                            "error: this box has no WireGuard tunnel (built without wg-client)\n"
2709                                .to_string(),
2710                        ),
2711                        Some(door) => match door(&address) {
2712                            Ok(answer) => {
2713                                tracing::warn!(target: TARGET_SSH, event = "ssh.control", verb = TUNNEL_ADDRESS_VERB, principal = %principal.id, address = %address, decision = "allow", "tunnel address door answered");
2714                                (
2715                                    0,
2716                                    if answer.ends_with('\n') {
2717                                        answer
2718                                    } else {
2719                                        format!("{answer}\n")
2720                                    },
2721                                )
2722                            }
2723                            Err(why) => {
2724                                tracing::warn!(target: TARGET_SSH, event = "ssh.control", verb = TUNNEL_ADDRESS_VERB, principal = %principal.id, address = %address, decision = "deny", reason = %why, "tunnel address door refused");
2725                                (1, format!("error: {why}\n"))
2726                            }
2727                        },
2728                    },
2729                },
2730            };
2731            session.channel_success(channel)?;
2732            session.data(channel, out.into_bytes())?;
2733            session.exit_status_request(channel, exit)?;
2734            session.close(channel)?;
2735            return Ok(());
2736        }
2737
2738        // ── the PAUSED door, before the plan gates ──────────────────────
2739        //
2740        // MEASURED under KVM (bench R1, 2026-09-15): with the gate paused for
2741        // a growth and `/data` already unmounted, a push knocked, the plan
2742        // gates ran FIRST, and the disk-floor check statvfs'd the read-only
2743        // guard tmpfs under the mountpoint — the client was told "/data has
2744        // 64.0 KiB free … no push until space is reclaimed (`gunnar control
2745        // repo delete --purge`)". The wrong sentence, with advice to delete
2746        // repositories, on a box that is growing. The pause is the box's own
2747        // state and says nothing about any repository, so it is answered
2748        // before the command is even resolved: the same words and the same
2749        // EX_TEMPFAIL for every principal and every path.
2750        if let Some(pause) = self.shared.admission.paused() {
2751            self.shared.admission.note_refused_paused();
2752            tracing::info!(
2753                target: TARGET_GIT,
2754                event = "git.transfer.refused",
2755                reason = pause.reason(),
2756                command = %String::from_utf8_lossy(data),
2757                "transfer REFUSED: the admission gate is paused"
2758            );
2759            session.channel_success(channel)?;
2760            session.data(channel, err_packet(pause.words())?)?;
2761            session.exit_status_request(channel, EXIT_BUSY)?;
2762            session.close(channel)?;
2763            return Ok(());
2764        }
2765
2766        let request =
2767            match self
2768                .shared
2769                .dispatch
2770                .authorise(&principal, self.key_fingerprint.as_ref(), data)
2771            {
2772                Ok(request) => request,
2773                Err(err) => {
2774                    self.shared.metrics.add_exec_refused(1);
2775                    // **THE counting point for a tenant's `auth_failures`**: a
2776                    // principal that authenticated and then held no grant on
2777                    // the repository it named.
2778                    //
2779                    // Only `Denied`, and deliberately only `Denied`. The other
2780                    // arms are a different sentence: `NoSuchRepo` hides a
2781                    // typo behind the same wire message on purpose and would
2782                    // charge a namespace that may not exist; `Standby` and
2783                    // `Plan` are the box and the plan refusing, not the
2784                    // credential; `Command` never named a repository at all.
2785                    // And the OTHER half of "authentication failed" — a key
2786                    // that resolves to nobody — happens before any exec line
2787                    // and belongs to no namespace, so it stays where it is, on
2788                    // the box's own `connections_refused`.
2789                    if let (DispatchError::Denied { repo, .. }, Some(flow)) =
2790                        (&err, self.shared.admission.flow())
2791                    {
2792                        flow.auth_failure(repo.namespace(), gunnar_core::now_ms());
2793                    }
2794                    let raw = String::from_utf8_lossy(data);
2795                    match err.client_message() {
2796                        // Not a git command at all: `bash`, `scp`, an empty
2797                        // string. Refuse the channel request itself; the client is
2798                        // not speaking a protocol that could read a reply.
2799                        // git-lfs asks SSH for its credentials (`git-lfs-authenticate`,
2800                        // `git-lfs-transfer`) before every transfer and goes to its
2801                        // https endpoint when refused: a client's known first step,
2802                        // said at INFO (RUN A 2026-10-08: seven a minute during one LFS
2803                        // push failed the log gate as WARN spam). Anything else stays WARN.
2804                        None if is_lfs_over_ssh(&raw) => {
2805                            tracing::info!(
2806                                target: TARGET_SSH,
2807                                event = "ssh.exec.refused",
2808                                reason = "lfs-over-ssh",
2809                                command = %raw,
2810                                "exec refused: git-lfs asks SSH first and uses its https endpoint"
2811                            );
2812                            session.channel_failure(channel)?;
2813                        }
2814                        None => {
2815                            tracing::warn!(
2816                                target: TARGET_SSH,
2817                                event = "ssh.exec.refused",
2818                                reason = "not-a-git-command",
2819                                command = %raw,
2820                                %err,
2821                                "exec refused"
2822                            );
2823                            session.channel_failure(channel)?;
2824                        }
2825                        // A git client asked for something it may not have. Accept
2826                        // the channel and answer in its own protocol, so it prints
2827                        // the reason instead of "Could not read from remote
2828                        // repository".
2829                        Some(message) => {
2830                            tracing::warn!(
2831                                target: TARGET_SSH,
2832                                event = "ssh.exec.denied",
2833                                // The distinction the *wire* deliberately hides:
2834                                // "no such repository" and "no permission" are one
2835                                // string to the client so a caller cannot enumerate
2836                                // private repositories. The log keeps it.
2837                                reason = match &err {
2838                                    DispatchError::NoSuchRepo(_) => "no-such-repository",
2839                                    DispatchError::Denied { .. } => "no-grant",
2840                                    DispatchError::Standby { .. } => "standby-read-only",
2841                                    DispatchError::Twin { .. } => "twin-serves-no-git",
2842                                    DispatchError::AnonymousElsewhere { .. } => {
2843                                        "anonymous-elsewhere"
2844                                    }
2845                                    DispatchError::Plan { .. } => "gate-refused",
2846                                    DispatchError::Store(_) => "control-plane-unavailable",
2847                                    DispatchError::Command(_) => "not-a-git-command",
2848                                },
2849                                command = %raw,
2850                                %err,
2851                                "exec denied"
2852                            );
2853                            session.channel_success(channel)?;
2854                            session.data(channel, err_packet(&message)?)?;
2855                            session.exit_status_request(channel, EXIT_DENIED)?;
2856                            session.close(channel)?;
2857                        }
2858                    }
2859                    return Ok(());
2860                }
2861            };
2862
2863        let Some(open) = self.channels.remove(&channel) else {
2864            // The CLIENT's misuse (a second exec on one channel, or a channel never
2865            // opened): refused, said at WARN once per connection — any caller that may
2866            // run one git command could otherwise raise the server's ERROR at will.
2867            if std::mem::replace(&mut self.misuse_said, true) {
2868                tracing::debug!(target: TARGET_SSH, ?channel, "exec request for a channel that is not open (said already on this connection)");
2869            } else {
2870                tracing::warn!(
2871                    target: TARGET_SSH,
2872                    event = "ssh.exec.misuse",
2873                    ?channel,
2874                    "exec request for a channel that is not open (a second exec on one channel, or one never opened) — refused; said once per connection"
2875                );
2876            }
2877            session.channel_failure(channel)?;
2878            return Ok(());
2879        };
2880
2881        // The repository is known from here on, so it goes on the CONNECTION
2882        // span: every line the transfer emits underneath inherits it, and the
2883        // "which repository was that connection touching" question is answered
2884        // even for a line that has nothing to do with git.
2885        span.record("repo", tracing::field::display(&request.repo));
2886
2887        // Attached *after* the decision above, never before it: an environment
2888        // variable selects the framing of the answer and nothing else.
2889        let request = request.with_git_protocol(open.git_protocol);
2890
2891        // ── the explicit ceiling ───────────────────────────────────────────
2892        //
2893        // After authorisation, deliberately. "The server is busy" is a fact
2894        // about the server, and telling it to a client that may not touch this
2895        // repository at all would leak that the repository exists. The order
2896        // also means the gate meters *transfers gunnar was going to serve*,
2897        // which is what the counter is supposed to mean.
2898        //
2899        // It refuses rather than queues; `crate::admission` sets out why at
2900        // length. In one line: a git client retries, and a fast refusal with a
2901        // sentence in it beats a socket that goes quiet while still holding the
2902        // session, the channel and a slot.
2903        // The class the governor meters this operation as. `git-upload-pack` is
2904        // clone, fetch AND ls-remote on the wire and cannot be told apart before
2905        // the stream is open, which is *after* this decision by design; see
2906        // `OpClass::for_ssh_service` for what that costs and why the middle
2907        // class is the right guess.
2908        // ── the BOOT WINDOW door, before admission ─────────────────────────
2909        //
2910        // After the repository is known and authorised, so a stranger cannot
2911        // learn from this whether a repository exists, and before a slot is
2912        // taken, so a boot does not spend its gate on requests it cannot serve.
2913        // See `GunnarSsh::with_readiness` for what was measured.
2914        // A KEYED caller is queued for the stores to open, up to its keyed
2915        // wait: a restart (a release, an upgrade) lasts seconds, and git-lfs's
2916        // pure-SSH transfer cannot retry a refusal — it falls back to
2917        // `git-lfs-authenticate` and fails the push (gunnar.rs 2026-10-25). A
2918        // stranger is still answered at once.
2919        if !self.shared.ready.load(std::sync::atomic::Ordering::Relaxed)
2920            && request.principal.id != gunnar_core::ANONYMOUS_PRINCIPAL_ID
2921        {
2922            let until =
2923                std::time::Instant::now() + self.shared.admission.wait_for(&request.principal.id);
2924            while !self.shared.ready.load(std::sync::atomic::Ordering::Relaxed)
2925                && std::time::Instant::now() < until
2926            {
2927                tokio::time::sleep(std::time::Duration::from_millis(100)).await;
2928            }
2929        }
2930        if !self.shared.ready.load(std::sync::atomic::Ordering::Relaxed) {
2931            tracing::info!(
2932                target: TARGET_GIT,
2933                event = "git.transfer.refused",
2934                reason = "starting",
2935                service = %request.service,
2936                "transfer REFUSED: the stores are still opening after a restart"
2937            );
2938            session.channel_success(channel)?;
2939            session.data(channel, err_packet(STARTING_MESSAGE)?)?;
2940            session.exit_status_request(channel, EXIT_BUSY)?;
2941            session.close(channel)?;
2942            return Ok(());
2943        }
2944
2945        let class = OpClass::for_ssh_service(request.service);
2946        // One git-lfs, one slot (GHUNT-7, `Connection::lfs_slots`): a `git-lfs-transfer`
2947        // channel joins the slot an earlier channel of this connection holds for the same
2948        // repository and operation, while it carries fewer than LFS_CHANNELS_PER_SLOT.
2949        // Each channel holds the slot twice (the task and its metered stream).
2950        let lfs_key = matches!(request.service, GitService::LfsTransfer(_))
2951            .then(|| (request.repo.to_string(), request.service));
2952        let joined = lfs_key
2953            .as_ref()
2954            .and_then(|k| self.lfs_slots.get(k))
2955            .and_then(std::sync::Weak::upgrade)
2956            .filter(|slot| Arc::strong_count(slot) < 2 * LFS_CHANNELS_PER_SLOT);
2957        // `admit_within`, not `try_admit`: over the limit the transfer parks for
2958        // a bounded moment instead of being refused on the spot. A refused
2959        // client reconnects, and a reconnect is a whole SSH handshake — more
2960        // than the wait costs. `crate::governor::DEFAULT_ADMISSION_WAIT` has the
2961        // measurement; `--admission-wait-ms 0` is the old behaviour exactly.
2962        let admitted = if let Some(slot) = joined {
2963            slot
2964        } else {
2965            // GUN-SSH-SLOT-HANG: a wait is never silent. Past WAIT_NOTICE_AFTER the client
2966            // is told on stderr that it waits for a slot, how full the gate is, and how
2967            // long it waits before it is refused by name.
2968            let admitted_or_refused = {
2969                let admit = self.shared.admission.admit_within(
2970                    &request.principal.id,
2971                    &request.repo.namespace,
2972                    class,
2973                );
2974                tokio::pin!(admit);
2975                tokio::select! {
2976                    biased;
2977                    done = &mut admit => done,
2978                    () = tokio::time::sleep(WAIT_NOTICE_AFTER) => {
2979                        let stats = self.shared.admission.stats();
2980                        let bound = self.shared.admission.wait_for(&request.principal.id).as_secs();
2981                        let notice = format!(
2982                            "gunnar: waiting for a transfer slot: {} of {} transfers are in use; \
2983                             refused by name after {bound} s if none frees\n",
2984                            stats.in_flight, stats.limit
2985                        );
2986                        let _ = session.extended_data(channel, 1, notice.into_bytes());
2987                        admit.await
2988                    }
2989                }
2990            };
2991            match admitted_or_refused {
2992                Ok((guard, waited)) => {
2993                    // AGGREGATE, at most once a second. A row per waiting client is
2994                    // the appliance's whole twenty-row console under load (the
2995                    // measured 5411 in one 256-client run); a row per second
2996                    // carrying the totals is the same fact, readable.
2997                    if !waited.is_zero() && self.shared.admission.wait_row_due() {
2998                        let stats = self.shared.admission.stats();
2999                        tracing::info!(
3000                            target: TARGET_GIT,
3001                            event = "git.transfer.waited",
3002                            class = class.name(),
3003                            waited_ms = waited.as_millis() as u64,
3004                            waited_total = stats.admitted_after_wait,
3005                            mean_wait_ms = stats.mean_wait_ms(),
3006                            peak_wait_ms = stats.peak_wait_ms,
3007                            in_flight = stats.in_flight,
3008                            limit = stats.limit,
3009                            "transfers are WAITING for a slot and are then served: {} so far, mean \
3010                             {} ms, worst {} ms against a bound of {} ms",
3011                            stats.admitted_after_wait,
3012                            stats.mean_wait_ms(),
3013                            stats.peak_wait_ms,
3014                            self.shared.admission.wait().as_millis() as u64
3015                        );
3016                    }
3017                    let slot = Arc::new(guard);
3018                    if let Some(k) = lfs_key {
3019                        self.lfs_slots.retain(|_, w| w.strong_count() > 0);
3020                        self.lfs_slots.insert(k, Arc::downgrade(&slot));
3021                    }
3022                    slot
3023                }
3024                Err(refusal) => {
3025                    let stats = self.shared.admission.stats();
3026                    tracing::warn!(
3027                        target: TARGET_GIT,
3028                        event = "git.transfer.refused",
3029                        // Which gate, not just "a gate": the anonymous ceiling
3030                        // fires with the box nearly empty, and a line that
3031                        // still said `admission-gate-full` there would send an
3032                        // operator to raise a limit that was never reached.
3033                        reason = refusal.reason(),
3034                        service = %request.service,
3035                        class = class.name(),
3036                        %refusal,
3037                        in_flight = stats.in_flight,
3038                        peak_in_flight = stats.peak_in_flight,
3039                        // The governed limit, so a log line says what was actually
3040                        // in force rather than what was configured. Without it the
3041                        // shed is invisible again.
3042                        limit = stats.limit,
3043                        reserved_bytes = stats.reserved_bytes,
3044                        refused_total = stats.refused(),
3045                        // The bound the transfer waited out before this. Without it
3046                        // a reader cannot tell an instant refusal from one the
3047                        // server spent two seconds trying to avoid.
3048                        waited_ms = self.shared.admission.wait().as_millis() as u64,
3049                        peak_wait_ms = stats.peak_wait_ms,
3050                        "transfer REFUSED by the admission gate"
3051                    );
3052                    // Answered in the client's own protocol, like a denial, so git
3053                    // prints the reason instead of "Could not read from remote
3054                    // repository". Saturation the operator can see and the user
3055                    // cannot is only half a fix.
3056                    session.channel_success(channel)?;
3057                    session.data(channel, err_packet(&refusal.client_message())?)?;
3058                    session.exit_status_request(channel, EXIT_BUSY)?;
3059                    session.close(channel)?;
3060                    return Ok(());
3061                }
3062            }
3063        };
3064
3065        // ── the span the whole transfer runs under ─────────────────────────
3066        //
3067        // A CHILD of `ssh.connection`, so it inherits `conn`, `peer`,
3068        // `principal` and `repo` and states none of them again. `service` is
3069        // the one thing that is new. Everything `gunnar-cli`'s transport and
3070        // `gunnar-policy`'s gate emit lands underneath it, in the same
3071        // process, without either of them being handed an id to repeat.
3072        let transfer = tracing::info_span!(
3073            target: TARGET_GIT,
3074            "git.transfer",
3075            service = %request.service,
3076            access = %request.access,
3077        );
3078        transfer.in_scope(|| {
3079            tracing::info!(
3080                target: TARGET_GIT,
3081                event = "git.transfer.started",
3082                git_protocol = ?request.git_protocol,
3083                "serving"
3084            )
3085        });
3086        let in_flight = self.shared.metrics.transfer_started();
3087        let metrics = Arc::clone(&self.shared.metrics);
3088        let started = std::time::Instant::now();
3089
3090        session.channel_success(channel)?;
3091
3092        let transport = self.shared.transport.clone();
3093        // Split, NOT `into_stream()`. `into_stream` wraps the channel in
3094        // russh's `ChannelCloseOnDrop`, which spawns a `Close` the instant the
3095        // transport lets the stream go, and that `Close` raced (and beat) the
3096        // `exit-status` sent next, which is how a successful push came to
3097        // report failure. Neither half closes the channel on drop, so the two
3098        // messages below are ordered by the sender's own queue.
3099        let (read_half, write_half) = open.channel.split();
3100        // METERED, so the memory ledger learns this transfer's real size as it
3101        // happens rather than being charged a flat per-class constant at
3102        // admission. `crate::admission::Metered` is the only place a byte count
3103        // reaches the governor, and it wraps the duplex the transport was
3104        // already going to be handed — no second copy, no second truth.
3105        // IDLE-BOUND (GUN-SSH-SLOT-HANG): a transfer that moves no bytes either way
3106        // while it waits on the client is ended after `--transfer-idle-secs`, and its
3107        // slot is given back; `idle` says afterwards whether that is how it ended.
3108        let (joined_stream, idle) = crate::idle::Idle::maybe(
3109            tokio::io::join(
3110                ChannelReader::new(read_half),
3111                // Bound to the session: a write parked on the SSH window ends
3112                // with an error when the session does (see `SessionBound`).
3113                SessionBound::new(write_half.make_writer(), self.session_over.subscribe()),
3114            ),
3115            self.shared.transfer_idle,
3116        );
3117        let stream = crate::admission::Metered::new(joined_stream, Arc::clone(&admitted));
3118        let idle_secs = self
3119            .shared
3120            .transfer_idle
3121            .map(|t| t.as_secs())
3122            .unwrap_or_default();
3123        // Spawned, not awaited: see the module docs. Awaiting here would stop
3124        // the session loop that has to feed this very stream.
3125        // ── THE STANDING FAN, not a fresh thread ────────────────────────
3126        //
3127        // This used to be `tokio::spawn` alone, and the task itself was never
3128        // the cost — tasks are cheap. The cost is behind it: `SyncIoBridge`
3129        // hands the transfer to tokio's BLOCKING pool, which spawns an OS
3130        // thread on demand, per operation, up to 512 of them. That is
3131        // per-request initialisation, and it is what the fans replace.
3132        //
3133        // The future is driven by `Handle::block_on` on a worker that was
3134        // already alive and parked before this client connected. A full queue
3135        // refuses here, which is the only place a refusal is free.
3136        let handle = tokio::runtime::Handle::current();
3137        let pipelines = crate::pipeline::fans();
3138        let over = self.session_over.subscribe();
3139        let (calm_grace, crowded_grace) = self.shared.session_end_grace;
3140        let body = async move {
3141            // ── the CoDel dequeue ──────────────────────────────────────
3142            //
3143            // The FIRST line of the task, deliberately. Everything between
3144            // `try_admit` above and this point is the transfer WAITING:
3145            // tokio's ready queue, and behind it the blocking-pool handoff
3146            // that `SyncIoBridge` needs before a single byte moves. That
3147            // wait is the queue the record called invisible, and this call is
3148            // what makes the governor able to see it. Measured here rather
3149            // than at the end because sojourn is wait time; service time
3150            // filed as sojourn would make every large clone look like a
3151            // standing queue.
3152            admitted.entered_service();
3153            // ★ The WHOLE transfer is bound to the session, not only its writes
3154            // (`SessionBound`): a client that goes silent while main is still
3155            // building the pack has no write parked on the window to wake, and
3156            // the build can take minutes (the UpCloud mockup's LD1/LD2,
3157            // 2026-10-09: 2 min 10 s on TCG). When the session ends — the
3158            // keepalives unanswered, a hang-up — the transfer ends with it and
3159            // gives back its fan worker, admission slot and store handle.
3160            let cancel = request.cancel.clone();
3161            let served = serve_until_session_ends(transport.serve(request, Box::new(stream)), over.clone(), &cancel, || {
3162                // Stinky row 19 (2026-10-10): a hung-up client's worker is one
3163                // a queued client waits for, so the grace is short when any do.
3164                crate::channel::grace_for(calm_grace, crowded_grace, pipelines.queued(class))
3165            }, || {
3166                crate::seam::TransportError::Io(session_ended_error())
3167            })
3168            .await;
3169            // ── closed for idleness: counted, said, and told to the client ──
3170            if idle.timed_out() {
3171                metrics.add_transfers_idle_closed(1);
3172                tracing::warn!(
3173                    target: TARGET_GIT,
3174                    event = "git.transfer.idle_closed",
3175                    idle_secs,
3176                    "transfer CLOSED: no bytes moved for {idle_secs} s while it waited on the \
3177                     client; its admission slot is free again"
3178                );
3179                let told = format!(
3180                    "gunnar: closed this git transfer: {} for {idle_secs} s while it waited on \
3181                     this client; its transfer slot is free again\n",
3182                    crate::idle::IDLE_CLOSED
3183                );
3184                let sent = tokio::select! {
3185                    sent = write_half.extended_data_bytes(1, told.into_bytes()) => sent,
3186                    () = session_over(over.clone()) => Err(russh::Error::IO(session_ended_error())),
3187                };
3188                note_teardown("idle-notice", sent);
3189            }
3190            let status = match served {
3191                Ok(()) => 0,
3192                Err(err) => {
3193                    metrics.add_transfers_failed(1);
3194                    // ── the refusal the client used to never hear ───────
3195                    //
3196                    // Until 2026-08-05 this arm logged the whole reason and
3197                    // sent the client an exit status and nothing else, so
3198                    // every post-authorisation refusal — the repository's
3199                    // store lock held by a `gunnar gc`, a storage fault on
3200                    // open, an unwired service — reached the user as
3201                    // `fatal: the remote end hung up unexpectedly`. The two
3202                    // gates ABOVE this one, in this same function, both
3203                    // answer in the client's own protocol; this one did not,
3204                    // and the asymmetry is what made it invisible.
3205                    //
3206                    // `client_message()` is `None` whenever bytes may
3207                    // already have gone out, so this cannot append a
3208                    // pkt-line to a half-written pack — see
3209                    // `TransportError::client_message`.
3210                    let told = err.client_message();
3211                    tracing::warn!(
3212                        target: TARGET_GIT,
3213                        event = "git.transfer.failed",
3214                        %err,
3215                        // Whether the user was given a reason, said in the
3216                        // log, because "the client was told" and "the client
3217                        // saw a dead socket" must not be the same line.
3218                        client_told = told.is_some(),
3219                        "transport ended with an error"
3220                    );
3221                    if let Some(message) = told {
3222                        match err_packet(&message) {
3223                            Ok(packet) => {
3224                                // Raced against the session's end: `data_bytes`
3225                                // waits for SSH window like the stream does.
3226                                let sent = tokio::select! {
3227                                    sent = write_half.data_bytes(packet) => sent,
3228                                    () = session_over(over.clone()) => Err(russh::Error::IO(session_ended_error())),
3229                                };
3230                                note_teardown("refusal-packet", sent)
3231                            }
3232                            Err(err) => tracing::error!(
3233                                target: TARGET_GIT,
3234                                %err,
3235                                "the refusal could not be encoded, so the client is \
3236                                 getting only an exit status"
3237                            ),
3238                        }
3239                    }
3240                    EXIT_TRANSPORT_FAILED
3241                }
3242            };
3243            // The channel is still open here, nothing has been dropped that
3244            // could have closed it, so the status arrives before the close,
3245            // through the same sender the data went through.
3246            if write_half.exit_status(status).await.is_err() {
3247                // WARN only when the client missed a verdict it needed. A
3248                // client that took everything it asked for and hung up
3249                // before the `exit-status` has lost nothing — it is how
3250                // `ls-remote` and `fetch` end — and warning about it made
3251                // `grep -i error` useless on a completely healthy server:
3252                // MEASURED 2026-08-06, 9 successful transfers, 9 of these.
3253                // A client that missed a NON-ZERO status did lose
3254                // something: it believes the transfer worked.
3255                if status == 0 {
3256                    tracing::debug!(
3257                        target: TARGET_GIT,
3258                        event = "git.exit_status.undelivered",
3259                        ?channel,
3260                        status,
3261                        "the client hung up before the exit status; it had already \
3262                         been served"
3263                    );
3264                } else {
3265                    tracing::warn!(
3266                        target: TARGET_GIT,
3267                        event = "git.exit_status.undelivered",
3268                        ?channel,
3269                        status,
3270                        "the client was gone before a NON-ZERO exit status, so it \
3271                         does not know this transfer failed"
3272                    );
3273                }
3274            }
3275            note_teardown("channel-close", write_half.close().await);
3276            // The duration an operator asked "why was that clone slow"
3277            // about: the WHOLE transfer, from admission to the last byte
3278            // acknowledged, not the state machine's own view of itself.
3279            tracing::info!(
3280                target: TARGET_GIT,
3281                event = "git.transfer.finished",
3282                status,
3283                elapsed_ms = started.elapsed().as_millis() as u64,
3284                "transfer finished"
3285            );
3286            // Moved into this task and dropped here, so the slot is held for the
3287            // whole transfer and released by `Drop` rather than by a call that
3288            // an early return could skip. The in-flight gauge is released the
3289            // same way, and for the same reason.
3290            drop(admitted);
3291            drop(in_flight);
3292        }
3293        .instrument(transfer);
3294
3295        // ── WHAT THE FAN IS ACTUALLY CARRYING ───────────────────────────────
3296        //
3297        // `Pipelines` has counted `served`, `queued` and `workers` since it was
3298        // written and NOTHING reported them, so the only evidence the fans were
3299        // doing anything was the absence of refusals — which is not evidence,
3300        // it is silence. Asked "is the queue filling", the honest answer was an
3301        // inference from the workload's shape.
3302        //
3303        // `submit` already returns the queue depth after the push, so this
3304        // costs one integer. Emitted only when an operation ACTUALLY QUEUED
3305        // (depth > 1, i.e. it did not go straight to a free worker), because a
3306        // line per transfer on a 102-client arm is noise that hides the signal.
3307        // `served` and `workers` ride along so one line answers "how deep, out
3308        // of how many, having done how much".
3309        match pipelines.submit(
3310            class,
3311            Box::new(move || {
3312                handle.block_on(body);
3313            }),
3314        ) {
3315            Ok(depth) if depth > 1 => tracing::info!(
3316                target: TARGET_GIT,
3317                event = "git.pipeline.queued",
3318                class = ?class,
3319                depth,
3320                workers = pipelines.workers(class),
3321                served = pipelines.served(class),
3322                "an operation waited for a worker"
3323            ),
3324            Ok(_) => {}
3325            Err(refused) => {
3326                // The fan is saturated. Say so in the client's own protocol
3327                // rather than dropping the channel — a dead socket is how a
3328                // refusal becomes "the remote end hung up unexpectedly".
3329                tracing::warn!(
3330                    target: TARGET_GIT,
3331                    event = "git.transfer.refused",
3332                    class = ?class,
3333                    depth = pipelines.queued(class),
3334                    workers = pipelines.workers(class),
3335                    served = pipelines.served(class),
3336                    %refused,
3337                    "the standing fan for this class refused the operation"
3338                );
3339                return Err(anyhow::anyhow!("{refused}"));
3340            }
3341        }
3342        Ok(())
3343    }
3344
3345    // ── everything below is the "no shell, ever" half ──────────────────────
3346    //
3347    // russh's defaults for these return `Ok(())` **without** answering the
3348    // channel request, which leaves a client waiting rather than failing. Each
3349    // one is answered explicitly.
3350
3351    async fn shell_request(
3352        &mut self,
3353        channel: ChannelId,
3354        session: &mut Session,
3355    ) -> Result<(), Self::Error> {
3356        self.refuse(channel, session, "shell").await
3357    }
3358
3359    async fn subsystem_request(
3360        &mut self,
3361        channel: ChannelId,
3362        name: &str,
3363        session: &mut Session,
3364    ) -> Result<(), Self::Error> {
3365        // Includes `sftp`. gunnar serves git and nothing else.
3366        self.span.in_scope(|| {
3367            tracing::warn!(
3368                target: TARGET_SSH,
3369                event = "ssh.request.refused",
3370                subsystem = %name,
3371                "subsystem refused"
3372            )
3373        });
3374        self.refuse(channel, session, "subsystem").await
3375    }
3376
3377    async fn pty_request(
3378        &mut self,
3379        channel: ChannelId,
3380        _term: &str,
3381        _col_width: u32,
3382        _row_height: u32,
3383        _pix_width: u32,
3384        _pix_height: u32,
3385        _modes: &[(russh::Pty, u32)],
3386        session: &mut Session,
3387    ) -> Result<(), Self::Error> {
3388        self.refuse(channel, session, "pty").await
3389    }
3390
3391    async fn x11_request(
3392        &mut self,
3393        channel: ChannelId,
3394        _single_connection: bool,
3395        _x11_auth_protocol: &str,
3396        _x11_auth_cookie: &str,
3397        _x11_screen_number: u32,
3398        session: &mut Session,
3399    ) -> Result<(), Self::Error> {
3400        self.refuse(channel, session, "x11").await
3401    }
3402
3403    async fn env_request(
3404        &mut self,
3405        channel: ChannelId,
3406        variable_name: &str,
3407        variable_value: &str,
3408        session: &mut Session,
3409    ) -> Result<(), Self::Error> {
3410        // An ALLOWLIST, not a filter: everything that is not named here is
3411        // refused, so adding a variable is a deliberate edit to one array and
3412        // the default for anything new is "no". See the module docs for why an
3413        // SSH server that passes an environment through is an attack surface.
3414        let span = self.span.clone();
3415        let _entered = span.enter();
3416        if !ENV_ALLOWLIST.contains(&variable_name) {
3417            // Same refusal, same event, same fields — two levels. See
3418            // [`ENV_ROUTINE`] for the 406-of-406 measurement that split them.
3419            if env_refusal_is_routine(variable_name) {
3420                tracing::debug!(
3421                    target: TARGET_SSH,
3422                    event = "ssh.env.refused",
3423                    reason = "not-allowlisted",
3424                    routine = true,
3425                    variable = %variable_name,
3426                    "env refused: gunnar reads exactly one environment variable"
3427                );
3428            } else {
3429                tracing::warn!(
3430                    target: TARGET_SSH,
3431                    event = "ssh.env.refused",
3432                    reason = "not-allowlisted",
3433                    routine = false,
3434                    variable = %variable_name,
3435                    "env refused: gunnar reads exactly one environment variable"
3436                );
3437            }
3438            session.channel_failure(channel)?;
3439            return Ok(());
3440        }
3441
3442        // A value gunnar will not read is not worth holding. `version=2` is
3443        // nine bytes; the cap is generous and exists so a client cannot make
3444        // the server keep an unbounded string per channel.
3445        if variable_value.len() > MAX_ENV_VALUE {
3446            tracing::warn!(
3447                target: TARGET_SSH,
3448                event = "ssh.env.refused",
3449                reason = "value-too-long",
3450                variable = %variable_name,
3451                length = variable_value.len(),
3452                "env refused: the value is implausibly long"
3453            );
3454            session.channel_failure(channel)?;
3455            return Ok(());
3456        }
3457
3458        let Some(open) = self.channels.get_mut(&channel) else {
3459            tracing::warn!(
3460                target: TARGET_SSH,
3461                ?channel,
3462                "env request for a channel that was never opened"
3463            );
3464            session.channel_failure(channel)?;
3465            return Ok(());
3466        };
3467        if variable_name != ENV_GIT_PROTOCOL {
3468            // `ENV_ALLOWLIST` grew a name and nothing here was taught to carry
3469            // it. Fail closed and say so, rather than quietly filing an unknown
3470            // variable in the `GIT_PROTOCOL` slot.
3471            tracing::error!(
3472                target: TARGET_SSH,
3473                variable = %variable_name,
3474                "env allowlisted but not carried anywhere; refusing"
3475            );
3476            session.channel_failure(channel)?;
3477            return Ok(());
3478        }
3479        tracing::debug!(
3480            target: TARGET_SSH,
3481            variable = %variable_name,
3482            value = %variable_value,
3483            "env accepted"
3484        );
3485        open.git_protocol = Some(variable_value.to_string());
3486        session.channel_success(channel)?;
3487        Ok(())
3488    }
3489
3490    async fn channel_close(
3491        &mut self,
3492        channel: ChannelId,
3493        _session: &mut Session,
3494    ) -> Result<(), Self::Error> {
3495        self.channels.remove(&channel);
3496        Ok(())
3497    }
3498}
3499
3500impl Connection {
3501    async fn refuse(
3502        &mut self,
3503        channel: ChannelId,
3504        session: &mut Session,
3505        what: &'static str,
3506    ) -> Result<(), anyhow::Error> {
3507        self.span.in_scope(|| {
3508            tracing::warn!(
3509                target: TARGET_SSH,
3510                event = "ssh.request.refused",
3511                request = what,
3512                "refused: gunnar has no shell"
3513            )
3514        });
3515        session.channel_failure(channel)?;
3516        Ok(())
3517    }
3518}
3519
3520/// The session loop ended: say so in words an operator's `grep -i error` will
3521/// not find, unless it really was an error.
3522///
3523/// See [`peer_hung_up`]. `error=early eof` fired on **every healthy fetch**
3524/// before this existed.
3525fn note_session_end(error: &anyhow::Error) {
3526    if peer_hung_up(error.as_ref()) {
3527        tracing::debug!(
3528            target: TARGET_SSH,
3529            event = "ssh.session.closed",
3530            reason = "peer-hung-up",
3531            "the client closed the connection"
3532        );
3533        return;
3534    }
3535    tracing::debug!(target: TARGET_SSH, %error, "ssh session ended with an error");
3536}
3537
3538/// Note that a best-effort teardown step did not work.
3539///
3540/// # Best-effort stays best-effort. What changes is that it stops being silent
3541///
3542/// Closing a channel and disconnecting a watchdogged session are both allowed
3543/// to fail: the peer may already be gone, which is the ordinary case and not
3544/// worth an operator's attention. Neither ever became an error, and neither
3545/// should. But a **systemic** failure to tear sessions down, from descriptor
3546/// exhaustion, the failure mode the unbounded handle cache used to
3547/// produce, was previously indistinguishable from a quiet server, because
3548/// `let _ =` leaves nothing behind at any level.
3549///
3550/// `debug!` rather than `warn!`: one of these is normal and a thousand is not,
3551/// and the thing that tells them apart is a count in a journal, not a severity
3552/// on a line that fires every time a client hangs up first.
3553/// The one encoder for a client-visible refusal: `ERR <message>` as a pkt-line.
3554///
3555/// Three call sites now answer a refusal in the client's own protocol — the
3556/// dispatch denial, the admission gate and a transfer refused before its first
3557/// byte — and a fourth is a matter of time. One implementation: the framing is written once,
3558/// so a refusal cannot acquire a subtly different shape depending on which gate
3559/// produced it. `ERR` is git's own out-of-band error line, which every git
3560/// version since 1.6 prints verbatim instead of "Could not read from remote
3561/// repository".
3562fn err_packet(message: &str) -> anyhow::Result<Vec<u8>> {
3563    let mut packet = Vec::new();
3564    gunnar_pktline::write_text(&mut packet, format!("ERR {message}").as_bytes())
3565        .map_err(|e| anyhow::anyhow!("encoding ERR packet: {e}"))?;
3566    Ok(packet)
3567}
3568
3569///
3570/// # …and "the peer is already gone" is not a failure at all
3571///
3572/// Closing a channel whose peer has already hung up returns
3573/// `russh::Error::SendError`, and until 2026-08-06 that was logged as
3574/// `ssh.teardown.failed err=Channel send error` on **every healthy transfer**
3575/// (MEASURED that day: 9 successful transfers, 6 of these). It is the same
3576/// fact `handle_session_error` and the undelivered exit status already report,
3577/// counted a third time in the third set of words with "err" in it. It is
3578/// separated here by [`peer_hung_up`] so the systemic case this function exists
3579/// for — descriptor exhaustion, a wedged sender — is still the only thing that
3580/// says `failed`.
3581fn note_teardown<E: std::error::Error + 'static>(step: &'static str, result: Result<(), E>) {
3582    if let Err(err) = result {
3583        if peer_hung_up(&err) {
3584            tracing::debug!(
3585                target: TARGET_SSH,
3586                event = "ssh.teardown.peer_gone",
3587                step,
3588                "the peer had already gone, so this teardown step had nothing to say \
3589                 it to"
3590            );
3591            return;
3592        }
3593        tracing::debug!(
3594            target: TARGET_SSH,
3595            event = "ssh.teardown.failed",
3596            step,
3597            %err,
3598            "a session teardown step did not complete"
3599        );
3600    }
3601}
3602
3603/// **Is this failure just "the peer hung up first"?**
3604///
3605/// The one classifier for that question (one implementation). Three places used to answer it
3606/// independently, and all three answered *"no"*, which is why a completely
3607/// healthy `gunnar ls-remote` left this in the journal:
3608///
3609/// ```text
3610///   DEBUG ssh session ended with an error   error=early eof
3611///   WARN  the client was gone before the exit status  channel=ChannelId(2) status=0
3612///   DEBUG ssh.teardown.failed  step="channel-close"  err=Channel send error
3613///   INFO  transfer finished  status=0  elapsed_ms=106
3614/// ```
3615///
3616/// MEASURED on the reference desktop 2026-08-06 from the live server's own journal, at
3617/// `10:55:10`, for a transfer that succeeded. Nothing was damaged — `status=0`,
3618/// `transfer finished` — but three lines carrying `error`, `err` and `failed`
3619/// per healthy call means `grep -i error` finds only noise, and that is the
3620/// first command an operator runs.
3621///
3622/// # Classified on the cause, never on the message text
3623///
3624/// A `contains("early eof")` would be a guard that silently stops matching the
3625/// day tokio rewords its `read_exact`, and the failure would be invisible
3626/// because it fails in the *quiet* direction — back to noise, not to a crash.
3627/// So this walks the source chain and matches concrete things:
3628/// `io::ErrorKind`, and russh's own named variants. `early eof` is
3629/// `ErrorKind::UnexpectedEof` (tokio's `read_exact`) and `Channel send error`
3630/// is `russh::Error::SendError`; neither string appears below.
3631///
3632/// # What it deliberately does NOT swallow
3633///
3634/// Only the socket going away. A key exchange failure, a MAC failure, a
3635/// protocol desync, a decryption failure — every one of those is still an error
3636/// and still logged as one. `tests/teardown_noise.rs` asserts both directions,
3637/// because a classifier that answered `true` for everything would produce a
3638/// beautifully quiet journal for a server that was on fire.
3639fn peer_hung_up(err: &(dyn std::error::Error + 'static)) -> bool {
3640    use std::io::ErrorKind;
3641
3642    let mut cause: Option<&(dyn std::error::Error + 'static)> = Some(err);
3643    while let Some(e) = cause {
3644        if let Some(io) = e.downcast_ref::<std::io::Error>() {
3645            if matches!(
3646                io.kind(),
3647                ErrorKind::UnexpectedEof
3648                    | ErrorKind::BrokenPipe
3649                    | ErrorKind::ConnectionReset
3650                    | ErrorKind::ConnectionAborted
3651                    | ErrorKind::NotConnected
3652            ) {
3653                return true;
3654            }
3655        }
3656        if let Some(r) = e.downcast_ref::<russh::Error>() {
3657            if matches!(
3658                r,
3659                // The channel's sender is gone: nothing left to send to.
3660                russh::Error::SendError
3661                    // russh's own names for a hung-up peer.
3662                    | russh::Error::HUP
3663                    | russh::Error::Disconnect
3664            ) {
3665                return true;
3666            }
3667        }
3668        cause = e.source();
3669    }
3670    false
3671}
3672
3673/// Refuse a key that would not re-encode.
3674///
3675/// A free function taking only the counters, so the decision this makes (a
3676/// **rejection**, and a counted one) can be asserted without standing up a
3677/// russh session. The alternative, an `expect()`, aborted the auth handler.
3678///
3679/// `auth_faults` is deliberately **not** moved: that counter means "the
3680/// identity table could not be read", and this key never reached it. The `warn`
3681/// [`crate::access::fingerprint_of_key`] emits carries
3682/// `reason = "key-does-not-re-encode"`, which is what tells the two apart.
3683fn refuse_unencodable_key(metrics: &gunnar_core::observe::ServerMetrics, user: &str) -> Auth {
3684    metrics.add_connections_refused(1);
3685    tracing::warn!(
3686        target: TARGET_SSH,
3687        event = "ssh.auth.refused",
3688        reason = "key-does-not-re-encode",
3689        decorative_ssh_user = %user,
3690        "publickey refused: the offered key did not re-encode"
3691    );
3692    Auth::reject()
3693}
3694
3695/// The one environment variable a git client may send: how it asks for
3696/// protocol v2 over SSH.
3697pub const ENV_GIT_PROTOCOL: &str = "GIT_PROTOCOL";
3698
3699/// **Every** environment variable this server accepts from a client.
3700///
3701/// An allowlist, checked by `env_request`. `LD_PRELOAD`, `PATH`, `GIT_DIR` and
3702/// everything else a client might try is refused with a channel failure, the
3703/// same answer this server gave to *all* environment requests before
3704/// [`ENV_GIT_PROTOCOL`] was needed. Nothing here is ever executed, exported to
3705/// a process or consulted by the grant check; gunnar has no shell and spawns
3706/// nothing, so the only reason an accepted name exists is that the *wire* reads
3707/// it.
3708pub const ENV_ALLOWLIST: [&str; 1] = [ENV_GIT_PROTOCOL];
3709
3710/// The refused names that mean **nothing at all**, because OpenSSH sends them
3711/// on its own without being asked.
3712///
3713/// Still refused — [`ENV_ALLOWLIST`] is unchanged and this list grants nothing.
3714/// What it changes is the *level* the refusal is reported at, and the reason it
3715/// has to exist is a measurement.
3716///
3717/// MEASURED on the reference desktop 2026-08-05, an 18-repository ingest over real SSH with stock
3718/// git 2.53.0 as the client, 203 connections, `RUST_LOG=info`:
3719///
3720/// | WARN lines | of which `ssh.env.refused` for `LANG`/`COLORTERM` |
3721/// |---|---|
3722/// | 406 | **406 (100%)** |
3723///
3724/// Zero warnings of any other kind were emitted in the whole run. OpenSSH's
3725/// shipped `ssh_config` carries `SendEnv LANG LC_*`, and desktop terminals add
3726/// `COLORTERM`, so every `git` over `ssh` produces two of these before it has
3727/// asked for anything. An operator who greps for `WARN` on a live gunnar
3728/// therefore reads noise at a 100% false-positive rate and will not see the
3729/// first real one — which is the same disease as a check that cannot go red,
3730/// wearing the other colour.
3731///
3732/// A client naming something NOT on this list is a different event and keeps
3733/// its `warn`: `LD_PRELOAD`, `PATH`, `GIT_DIR`, `GIT_ALTERNATE_OBJECT_DIRECTORIES`
3734/// are what an attacker tries, and nothing sends them by accident.
3735pub const ENV_ROUTINE: [&str; 4] = ["LANG", "LANGUAGE", "COLORTERM", "TERM"];
3736
3737/// Whether `name` is a routine refusal ([`ENV_ROUTINE`], or any `LC_*`, which
3738/// OpenSSH sends as a glob).
3739pub fn env_refusal_is_routine(name: &str) -> bool {
3740    ENV_ROUTINE.contains(&name) || name.starts_with("LC_")
3741}
3742
3743/// The longest environment value the server will hold.
3744///
3745/// `version=2` is nine bytes. This is a bound on what one channel can make the
3746/// server remember, not a syntax check; the grammar is `gunnar-wire`'s.
3747pub const MAX_ENV_VALUE: usize = 256;
3748
3749/// Exit status reported when access was refused. Matches what `git` itself
3750/// reports for a failed remote helper.
3751pub const EXIT_DENIED: u32 = 128;
3752
3753/// **What a client is told when the key that opened its session stopped
3754/// authorising it.**
3755///
3756/// One string, named here rather than spelled at the refusal site, so the test
3757/// that asserts what reaches the client reads the same constant the server
3758/// sends. It says what happened and what to do, because "denied" on a
3759/// connection that worked a second ago is otherwise indistinguishable from the
3760/// server having broken.
3761///
3762/// It does NOT distinguish a revoked key from an unreadable identity table, for
3763/// the reason the whole of this surface merges "no such repository" with "no
3764/// permission": the difference is operator information, and the log keeps it.
3765pub const REVOKED_MESSAGE: &str =
3766    "the key that opened this session no longer authorises it; reconnect";
3767
3768/// Exit status reported when the wire failed mid-transfer.
3769pub const EXIT_TRANSPORT_FAILED: u32 = 1;
3770
3771/// Exit status reported when the admission gate refused a transfer.
3772///
3773/// `EX_TEMPFAIL` from `sysexits.h`, and deliberately **not** [`EXIT_DENIED`]:
3774/// "try again" and "you may not" are different answers, and a retry script that
3775/// cannot tell them apart either hammers a permission error forever or gives up
3776/// on a transient one.
3777pub const EXIT_BUSY: u32 = 75;
3778
3779/// What a client is told while the server's stores are still opening.
3780///
3781/// **The word `starting` is load-bearing** and is asserted by
3782/// `tests/readiness.rs`: it is what separates this from
3783/// `gunnar_wire::Error::Store`'s `the repository is unavailable; try again`,
3784/// which is what a client hears when a repository is genuinely broken. The
3785/// power-cut door read the second sentence during the first condition and
3786/// reported 64 acked pushes missing that were all present a second later. A
3787/// harness, a CI script and a person all have to be able to tell "not yet" from
3788/// "gone", and the only place that distinction can be made is here, in the
3789/// words.
3790///
3791/// It carries [`EXIT_BUSY`], not [`EXIT_DENIED`], for the same reason: 75 means
3792/// retry.
3793pub const STARTING_MESSAGE: &str =
3794    "the server is starting: its stores are still opening after a restart, and it will serve \
3795     this in a moment; retry";
3796
3797#[cfg(test)]
3798mod tests {
3799    use super::*;
3800
3801    /// `gunnar-recipient-keys`: the path through the one sandbox (git's
3802    /// quoting taken off), the member through the principal id rule, and
3803    /// nothing else on the line; another verb is not this one.
3804    #[test]
3805    fn the_recipient_keys_line_takes_a_sandboxed_path_and_an_account_name() {
3806        let ok = |line: &str| {
3807            parse_recipient_keys_verb(line.as_bytes())
3808                .expect("this verb")
3809                .expect("well formed")
3810        };
3811        let (repo, member) = ok("gunnar-recipient-keys 'team/vault.git' 'bob'");
3812        assert_eq!(
3813            (repo.namespace(), repo.name(), member.as_str()),
3814            ("team", "vault", "bob")
3815        );
3816        let (repo, member) = ok("gunnar-recipient-keys team/vault bob");
3817        assert_eq!(
3818            (repo.namespace(), repo.name(), member.as_str()),
3819            ("team", "vault", "bob")
3820        );
3821        let (repo, _) = ok("gunnar-recipient-keys '/team/vault.git' bob");
3822        assert_eq!(repo.name(), "vault");
3823        assert!(parse_recipient_keys_verb(b"git-upload-pack 'team/vault.git'").is_none());
3824        assert!(parse_recipient_keys_verb(b"gunnar-recipient-keysx a/b c").is_none());
3825        for bad in [
3826            "gunnar-recipient-keys",
3827            "gunnar-recipient-keys 'team/vault.git'",
3828            "gunnar-recipient-keys '../etc/passwd' bob",
3829            "gunnar-recipient-keys team/vault 'bo b'",
3830            "gunnar-recipient-keys team/vault anonymous",
3831            "gunnar-recipient-keys team/vault 'bob",
3832            "gunnar-recipient-keys team/vault bob\u{1}",
3833        ] {
3834            assert!(
3835                matches!(parse_recipient_keys_verb(bad.as_bytes()), Some(Err(_))),
3836                "{bad:?} is refused"
3837            );
3838        }
3839        let long = format!("gunnar-recipient-keys team/vault {}", "b".repeat(600));
3840        assert!(matches!(
3841            parse_recipient_keys_verb(long.as_bytes()),
3842            Some(Err(_))
3843        ));
3844    }
3845
3846    /// **The door offers the post-quantum hybrid first**, from gunnar's own
3847    /// list: `mlkem768x25519-sha256`, then the classical fallbacks, and the
3848    /// strict-kex markers (Terrapin) kept.
3849    #[test]
3850    fn the_door_offers_mlkem768x25519_first_from_gunnars_own_list() {
3851        let p = preferred();
3852        assert!(
3853            std::ptr::eq(p.kex.as_ref().as_ptr(), KEX_ORDER.as_ptr()),
3854            "the server's list is gunnar's KEX_ORDER, not whatever russh defaults to"
3855        );
3856        let names: Vec<&str> = p.kex.iter().map(|n| n.as_ref()).collect();
3857        assert_eq!(
3858            names[..3],
3859            [
3860                "mlkem768x25519-sha256",
3861                "curve25519-sha256",
3862                "curve25519-sha256@libssh.org"
3863            ],
3864            "{names:?}"
3865        );
3866        assert!(
3867            names.contains(&"kex-strict-s-v00@openssh.com"),
3868            "strict kex stays: {names:?}"
3869        );
3870        assert_eq!(
3871            p.cipher,
3872            russh::Preferred::DEFAULT.cipher,
3873            "only the key exchange is gunnar's own"
3874        );
3875    }
3876
3877    /// The accept loop outlives a descriptor shortage and a client that hung
3878    /// up mid-handshake; it returns on a listener that is gone.
3879    #[test]
3880    fn transient_accept_errors_are_named() {
3881        for errno in [24, 23, 105, 12, 103] {
3882            assert!(
3883                accept_error_is_transient(&std::io::Error::from_raw_os_error(errno)),
3884                "errno {errno}"
3885            );
3886        }
3887        for errno in [9, 22, 88] {
3888            assert!(
3889                !accept_error_is_transient(&std::io::Error::from_raw_os_error(errno)),
3890                "errno {errno}"
3891            );
3892        }
3893        assert!(!accept_error_is_transient(&std::io::Error::other("closed")));
3894    }
3895
3896    /// A teardown that fails is **reported**, and one that works is
3897    /// silent.
3898    ///
3899    /// Driven through the helper rather than through a live channel: making
3900    /// `Channel::close()` fail on demand means a peer that has vanished at an
3901    /// exact moment, which is a race to arrange and a race to assert on. What
3902    /// the helper decides is the whole of what changed, `let _ =` against a
3903    /// line in the journal, and both directions are asserted, because a
3904    /// `note_teardown` that logged unconditionally would put a line in an
3905    /// operator's journal on every clean disconnect and make the signal
3906    /// useless.
3907    /// Capture every `tracing` line `emit` produces, rendered as
3908    /// `<target> <field>=<value> …`.
3909    ///
3910    /// One capture for every test in this module that reads the journal (one
3911    /// implementation): there were two copies of this layer forty lines apart, and a second
3912    /// copy of a capture is a second chance to filter out the line under test.
3913    fn captured_lines(emit: impl FnOnce()) -> Vec<String> {
3914        use std::sync::{Arc, Mutex};
3915        use tracing_subscriber::layer::SubscriberExt;
3916        use tracing_subscriber::Layer as _;
3917
3918        #[derive(Clone, Default)]
3919        struct Capture(Arc<Mutex<Vec<String>>>);
3920        struct CaptureLayer(Capture);
3921        impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for CaptureLayer {
3922            fn on_event(
3923                &self,
3924                event: &tracing::Event<'_>,
3925                _: tracing_subscriber::layer::Context<'_, S>,
3926            ) {
3927                struct Render(String);
3928                impl tracing::field::Visit for Render {
3929                    fn record_debug(
3930                        &mut self,
3931                        field: &tracing::field::Field,
3932                        value: &dyn std::fmt::Debug,
3933                    ) {
3934                        self.0.push_str(&format!("{}={value:?} ", field.name()));
3935                    }
3936                }
3937                let mut render = Render(String::new());
3938                event.record(&mut render);
3939                self.0 .0.lock().expect("capture").push(format!(
3940                    "{} {}",
3941                    event.metadata().target(),
3942                    render.0
3943                ));
3944            }
3945        }
3946
3947        let capture = Capture::default();
3948        let subscriber = tracing_subscriber::registry().with(
3949            CaptureLayer(capture.clone())
3950                .with_filter(tracing_subscriber::filter::LevelFilter::TRACE),
3951        );
3952        tracing::subscriber::with_default(subscriber, emit);
3953        let lines = capture.0.lock().expect("capture").clone();
3954        lines
3955    }
3956
3957    #[test]
3958    fn a_failed_teardown_step_is_reported_and_a_clean_one_is_not() {
3959        let lines = captured_lines(|| {
3960            note_teardown::<std::io::Error>("channel-close", Ok(()));
3961            note_teardown(
3962                "watchdog-disconnect",
3963                Err(std::io::Error::other("the peer was already gone")),
3964            );
3965        });
3966
3967        assert_eq!(
3968            lines.len(),
3969            1,
3970            "expected exactly one line — the failure — got {lines:?}"
3971        );
3972        assert!(
3973            lines[0].starts_with(gunnar_core::observe::TARGET_SSH),
3974            "the line is not on the ssh target: {}",
3975            lines[0]
3976        );
3977        assert!(
3978            lines[0].contains("watchdog-disconnect") && lines[0].contains("already gone"),
3979            "the line names neither the step nor the error: {}",
3980            lines[0]
3981        );
3982    }
3983
3984    /// **RED-WHEN-BROKEN: a real failure is still reported as one.**
3985    ///
3986    /// The dangerous direction of [`peer_hung_up`] is not missing a hung-up
3987    /// peer — that only restores the noise. It is answering `true` for
3988    /// everything, which gives a beautifully quiet journal for a server that is
3989    /// on fire, and does it in a way no other test would notice.
3990    ///
3991    /// So every case is asserted in both directions, against **rendered log
3992    /// output** rather than against the predicate, because the predicate is
3993    /// private and the log line is what an operator has.
3994    #[test]
3995    fn a_real_failure_is_still_reported_as_one() {
3996        // Benign: the peer went away. Each of these is a shape MEASURED in the
3997        // live journal on 2026-08-06 for a transfer that succeeded.
3998        let benign: Vec<(&str, Box<dyn std::error::Error + 'static>)> = vec![
3999            // `err=Channel send error` — the channel's sender is gone.
4000            ("russh SendError", Box::new(russh::Error::SendError)),
4001            ("russh HUP", Box::new(russh::Error::HUP)),
4002            ("russh Disconnect", Box::new(russh::Error::Disconnect)),
4003            // `error=early eof` — tokio's `read_exact` on a closed socket.
4004            (
4005                "early eof",
4006                Box::new(std::io::Error::new(
4007                    std::io::ErrorKind::UnexpectedEof,
4008                    "early eof",
4009                )),
4010            ),
4011            (
4012                "broken pipe",
4013                Box::new(std::io::Error::from(std::io::ErrorKind::BrokenPipe)),
4014            ),
4015            (
4016                "connection reset",
4017                Box::new(std::io::Error::from(std::io::ErrorKind::ConnectionReset)),
4018            ),
4019        ];
4020        for (what, err) in benign {
4021            let lines = captured_lines(|| note_teardown("channel-close", Err(BoxedError(err))));
4022            assert_eq!(lines.len(), 1, "{what}: expected one line, got {lines:?}");
4023            let line = lines[0].to_ascii_lowercase();
4024            assert!(
4025                line.contains("peer_gone"),
4026                "{what} was not classified as a hung-up peer: {}",
4027                lines[0]
4028            );
4029            assert!(
4030                !line.contains("error") && !line.contains("err=") && !line.contains("fail"),
4031                "{what} is benign but its line is still found by `grep -i error`: {}",
4032                lines[0]
4033            );
4034        }
4035
4036        // NOT benign: the protocol itself went wrong. These must keep saying so
4037        // — this is the half that stops the classifier becoming a blanket yes.
4038        let real: Vec<(&str, Box<dyn std::error::Error + 'static>)> = vec![
4039            ("key exchange", Box::new(russh::Error::Kex)),
4040            ("packet auth", Box::new(russh::Error::PacketAuth)),
4041            ("decryption", Box::new(russh::Error::DecryptionError)),
4042            ("inconsistent state", Box::new(russh::Error::Inconsistent)),
4043            ("no common algo", Box::new(russh::Error::WrongChannel)),
4044            (
4045                "permission denied on disk",
4046                Box::new(std::io::Error::from(std::io::ErrorKind::PermissionDenied)),
4047            ),
4048            (
4049                "out of descriptors",
4050                Box::new(std::io::Error::from(std::io::ErrorKind::Other)),
4051            ),
4052        ];
4053        for (what, err) in real {
4054            let lines = captured_lines(|| note_teardown("channel-close", Err(BoxedError(err))));
4055            assert_eq!(lines.len(), 1, "{what}: expected one line, got {lines:?}");
4056            assert!(
4057                lines[0].contains("ssh.teardown.failed"),
4058                "{what} was swallowed as benign, so the classifier is a blanket yes: {}",
4059                lines[0]
4060            );
4061            assert!(
4062                lines[0].contains("err="),
4063                "{what} lost the error itself, so the line names no cause: {}",
4064                lines[0]
4065            );
4066        }
4067    }
4068
4069    /// A boxed error that is still `std::error::Error`, so the table above can
4070    /// hold two concrete types in one `Vec` and still reach the classifier's
4071    /// `downcast_ref` through `source()`.
4072    #[derive(Debug)]
4073    struct BoxedError(Box<dyn std::error::Error + 'static>);
4074
4075    impl std::fmt::Display for BoxedError {
4076        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4077            self.0.fmt(f)
4078        }
4079    }
4080
4081    impl std::error::Error for BoxedError {
4082        fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
4083            Some(self.0.as_ref())
4084        }
4085    }
4086
4087    /// The session-level half of the same rule: an ended session says nothing
4088    /// an operator greps for when the client simply hung up, and still says
4089    /// `error` when it did not.
4090    ///
4091    /// Asserted through the real `Handler` method rather than the predicate,
4092    /// because the method is what russh calls.
4093    #[test]
4094    fn a_hung_up_session_is_not_logged_as_an_error() {
4095        let quiet = captured_lines(|| {
4096            note_session_end(&anyhow::Error::new(std::io::Error::new(
4097                std::io::ErrorKind::UnexpectedEof,
4098                "early eof",
4099            )));
4100        });
4101        assert_eq!(quiet.len(), 1, "expected one line, got {quiet:?}");
4102        let line = quiet[0].to_ascii_lowercase();
4103        assert!(
4104            line.contains("peer-hung-up") && !line.contains("error"),
4105            "a client hanging up is still logged as an error: {}",
4106            quiet[0]
4107        );
4108
4109        let loud = captured_lines(|| {
4110            note_session_end(&anyhow::Error::new(russh::Error::Kex));
4111        });
4112        assert_eq!(loud.len(), 1, "expected one line, got {loud:?}");
4113        assert!(
4114            loud[0].to_ascii_lowercase().contains("error"),
4115            "a key-exchange failure was swallowed as a hung-up peer: {}",
4116            loud[0]
4117        );
4118    }
4119
4120    /// An unencodable key is **refused and counted**, never accepted.
4121    ///
4122    /// The assertion is on the applied output, the `Auth` value that goes back
4123    /// to russh and the counter an operator reads, not on "it did not panic".
4124    /// A body that returned `Auth::Accept` would let a key nobody could
4125    /// fingerprint onto the server, which is the one outcome worse than the
4126    /// panic this replaced.
4127    #[test]
4128    fn a_key_that_does_not_re_encode_is_rejected_and_counted() {
4129        let metrics = gunnar_core::observe::ServerMetrics::default();
4130        let auth = refuse_unencodable_key(&metrics, "decorative");
4131
4132        assert!(
4133            matches!(auth, Auth::Reject { .. }),
4134            "an unencodable key must be rejected, got {auth:?}"
4135        );
4136        let snapshot = metrics.snapshot();
4137        assert_eq!(
4138            snapshot.connections_refused, 1,
4139            "the refusal must be visible to an operator"
4140        );
4141        // Not an identity-table fault, and the counters must not blur the two.
4142        assert_eq!(snapshot.auth_faults, 0);
4143        assert_eq!(snapshot.connections_accepted, 0);
4144    }
4145}
4146
4147/// Pure: whether an exec line is git-lfs asking SSH (`git-lfs-authenticate …`,
4148/// `git-lfs-transfer …`) — refused, and said at INFO.
4149pub fn is_lfs_over_ssh(command: &str) -> bool {
4150    let first = command.split_whitespace().next().unwrap_or("");
4151    // The transfer verb is named through the dispatch table's own LFS row, never spelled here
4152    // (`command_parsing::the_dispatch_table_is_the_only_place_a_verb_is_named`).
4153    // `git-lfs-authenticate` is no verb gunnar dispatches: it is git-lfs's own probe.
4154    first == "git-lfs-authenticate"
4155        || crate::command::SERVICES.iter().any(|spec| {
4156            spec.verb == first && matches!(spec.service, crate::command::GitService::LfsTransfer(_))
4157        })
4158}
4159
4160#[cfg(test)]
4161mod lfs_over_ssh_tests {
4162    /// ★ git-lfs's SSH probe is a client's known step, not a defect (RUN A 2026-10-08).
4163    #[test]
4164    fn git_lfs_asking_ssh_is_known_and_anything_else_is_not() {
4165        assert!(super::is_lfs_over_ssh(
4166            "git-lfs-authenticate /cbc/vault upload"
4167        ));
4168        assert!(super::is_lfs_over_ssh(
4169            "git-lfs-transfer 'acme/site' download"
4170        ));
4171        for other in [
4172            "bash",
4173            "scp -t /etc",
4174            "",
4175            "git-lfs-authenticatex /a upload",
4176            "echo git-lfs-authenticate",
4177        ] {
4178            assert!(!super::is_lfs_over_ssh(other), "{other:?}");
4179        }
4180    }
4181}