Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Mint a one-use session for each front door connection

e075258f0e4456f654680eeb7ba246e80d6c9a97 · Keenan Nemetz · 16d ago · parent 5ff8dfc9

Mint a one-use session for each front door connection

sshd can authenticate a Vole key and then hand the request to something else.
This adds the two ends of that handoff, plus the capability that crosses it.

`vole ssh-authorized-keys` is sshd's AuthorizedKeysCommand. It opens the roster
and the run credentials without running a schema or a migration — a write on the
path sshd takes before it knows who is connecting is a write nobody watches — and
for an active key prints the client's key back with `restrict,command=` and a
token. An unknown key, a retired account, and an expired credential all produce
no line and no row, so sshd refuses before any Vole process runs.

The token is 256 random bits, stored only as a SHA-256 hash, good for one use and
sixty seconds, and bound to the client endpoint sshd reports for the connection.
That binding is what closes the one weakness of a capability in argv: `/proc`
lets another local user read the forced command, but not spend it from their own
connection. A token that turns up at the wrong endpoint is deleted, not left for
the rest of its minute.

`vole ssh-exec` is the forced command, and `vole ssh-shell` is the account's
login shell. sshd runs a forced command *through* the login shell with `-c`, so a
shell that refuses everything would break every command it fronts: this one
recognises the single line ssh-authorized-keys printed, execs it directly, and
refuses anything else, including an interactive session. Generating that line
therefore refuses a config path containing whitespace rather than quoting it into
shell syntax.

The wire between them is `internal/sshwire`: one hello, one accept or refusal,
then length-prefixed stdin, stdout, stderr and exit frames over the Unix socket,
so the streams stay separate and backpressure is a blocked writer rather than a
buffer. `ssh_socket` is where that socket lives; it is absolute-only because
sshd's working directory is not the instance's.

The socket listener, the token sweeper, and the configuration that enables the
mode are #305. Until then nothing here runs unless sshd is wired to it.

internal/sshwire/sshwire.go +117

@@ -0,0 +1,117 @@
1 // Package sshwire is the local protocol between `vole ssh-exec`, which sshd runs
2 // as a forced command, and `vole serve`.
3 //
4 // One request per connection: a hello naming the session token, the command the
5 // client asked for, and the connection it arrived on; one accept or refusal;
6 // then the session's streams and its exit status. Every message is a
7 // length-prefixed frame, so stdout and stderr stay separate by construction and
8 // a slow reader on either end pushes back on the other instead of buffering.
9 //
10 // The socket is local and private to the service, so this is a protocol, not a
11 // cryptographic boundary: the session token is what authenticates a request.
12 // Framing exists so one byte stream carries four kinds of message without any of
13 // them being parsed out of another.
14 package sshwire
15
16 import (
17 "encoding/binary"
18 "encoding/json"
19 "fmt"
20 "io"
21 )
22
23 // MaxFrame bounds one frame. Git moves a pack in chunks; this is the largest
24 // chunk either side will accept, and it keeps a corrupt or hostile length from
25 // becoming an allocation.
26 const MaxFrame = 64 << 10
27
28 // Kind is a frame's first byte.
29 type Kind byte
30
31 const (
32 // KindHello is the client's one request, as JSON.
33 KindHello Kind = 'H'
34 // KindAccept answers the hello, as JSON.
35 KindAccept Kind = 'A'
36 // KindStdin is a chunk of the client's standard input.
37 KindStdin Kind = 'i'
38 // KindStdinEOF says the client's standard input ended.
39 KindStdinEOF Kind = 'I'
40 // KindStdout is a chunk of the command's standard output.
41 KindStdout Kind = 'o'
42 // KindStderr is a chunk of the command's standard error.
43 KindStderr Kind = 'e'
44 // KindExit is the command's exit status, as JSON. It is the last frame.
45 KindExit Kind = 'x'
46 )
47
48 // Hello is what the forced command asks for.
49 type Hello struct {
50 // Session is the token `vole ssh-authorized-keys` minted for this connection.
51 Session string `json:"session"`
52 // Command is the client's SSH_ORIGINAL_COMMAND, verbatim.
53 Command string `json:"command"`
54 // Remote is the client's SSH_CONNECTION, which the service compares with the
55 // endpoint the token was minted for.
56 Remote string `json:"remote"`
57 }
58
59 // Accept is the service's answer to a hello. A refusal carries the reason the
60 // client should print.
61 type Accept struct {
62 OK bool `json:"ok"`
63 Error string `json:"error,omitempty"`
64 }
65
66 // Exit is a command's status.
67 type Exit struct {
68 Code int `json:"code"`
69 }
70
71 // WriteFrame writes one frame: the kind, a big-endian length, then the payload.
72 func WriteFrame(w io.Writer, kind Kind, payload []byte) error {
73 if len(payload) > MaxFrame {
74 return fmt.Errorf("sshwire: %d-byte frame is larger than %d", len(payload), MaxFrame)
75 }
76 head := [5]byte{byte(kind)}
77 binary.BigEndian.PutUint32(head[1:], uint32(len(payload)))
78 if _, err := w.Write(head[:]); err != nil {
79 return err
80 }
81 if len(payload) == 0 {
82 return nil
83 }
84 _, err := w.Write(payload)
85 return err
86 }
87
88 // ReadFrame reads one frame and its payload. A payload is reused by the caller
89 // immediately, so it is returned as a slice of a fresh buffer.
90 func ReadFrame(r io.Reader) (Kind, []byte, error) {
91 var head [5]byte
92 if _, err := io.ReadFull(r, head[:]); err != nil {
93 return 0, nil, err
94 }
95 kind := Kind(head[0])
96 n := binary.BigEndian.Uint32(head[1:])
97 if n > MaxFrame {
98 return 0, nil, fmt.Errorf("sshwire: %d-byte frame is larger than %d", n, MaxFrame)
99 }
100 if n == 0 {
101 return kind, nil, nil
102 }
103 payload := make([]byte, n)
104 if _, err := io.ReadFull(r, payload); err != nil {
105 return 0, nil, err
106 }
107 return kind, payload, nil
108 }
109
110 // WriteJSON writes one framed JSON message.
111 func WriteJSON(w io.Writer, kind Kind, v any) error {
112 payload, err := json.Marshal(v)
113 if err != nil {
114 return err
115 }
116 return WriteFrame(w, kind, payload)
117 }