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/client.go +112
| @@ -0,0 +1,112 @@ | |||
| 1 | package sshwire | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "encoding/json" | ||
| 5 | "errors" | ||
| 6 | "fmt" | ||
| 7 | "io" | ||
| 8 | "net" | ||
| 9 | ) | ||
| 10 | |||
| 11 | // stdinChunk is how much of the client's input one frame carries. It is well | ||
| 12 | // under MaxFrame so a frame's header costs little on a stream of small writes. | ||
| 13 | const stdinChunk = 32 << 10 | ||
| 14 | |||
| 15 | // Exec sends one request to the service's Unix socket and runs the session it | ||
| 16 | // answers. The session's output is written to stdout and its diagnostics to | ||
| 17 | // stderr as they arrive, the caller's input is streamed from stdin, and the | ||
| 18 | // result is the command's own exit status. | ||
| 19 | // | ||
| 20 | // An error means no session ran: the socket could not be reached, the service | ||
| 21 | // refused the token, or the connection ended without a status. The caller turns | ||
| 22 | // that into its own failure; a session that ran and failed is the second return | ||
| 23 | // value, not an error. | ||
| 24 | func Exec(socket string, hello Hello, stdin io.Reader, stdout, stderr io.Writer) (int, error) { | ||
| 25 | if socket == "" { | ||
| 26 | return 0, errors.New("no front door socket is configured") | ||
| 27 | } | ||
| 28 | conn, err := net.Dial("unix", socket) | ||
| 29 | if err != nil { | ||
| 30 | return 0, fmt.Errorf("cannot reach the forge at %s: %w", socket, err) | ||
| 31 | } | ||
| 32 | defer conn.Close() | ||
| 33 | |||
| 34 | if err := WriteJSON(conn, KindHello, hello); err != nil { | ||
| 35 | return 0, fmt.Errorf("cannot send the request: %w", err) | ||
| 36 | } | ||
| 37 | kind, payload, err := ReadFrame(conn) | ||
| 38 | if err != nil { | ||
| 39 | return 0, fmt.Errorf("the forge did not answer: %w", err) | ||
| 40 | } | ||
| 41 | if kind != KindAccept { | ||
| 42 | return 0, fmt.Errorf("the forge answered with an unexpected %q frame", string(rune(kind))) | ||
| 43 | } | ||
| 44 | var accept Accept | ||
| 45 | if err := json.Unmarshal(payload, &accept); err != nil { | ||
| 46 | return 0, fmt.Errorf("the forge's answer is not readable: %w", err) | ||
| 47 | } | ||
| 48 | if !accept.OK { | ||
| 49 | if accept.Error == "" { | ||
| 50 | accept.Error = "the session was refused" | ||
| 51 | } | ||
| 52 | return 0, errors.New(accept.Error) | ||
| 53 | } | ||
| 54 | |||
| 55 | // Input goes out in the background: the service does not answer until the | ||
| 56 | // command has run, and a command that writes before it reads must not wait for | ||
| 57 | // input that may never come. Closing the connection when Exec returns is what | ||
| 58 | // ends this goroutine if the command exits first. | ||
| 59 | go pumpStdin(conn, stdin) | ||
| 60 | |||
| 61 | for { | ||
| 62 | kind, payload, err := ReadFrame(conn) | ||
| 63 | if err != nil { | ||
| 64 | if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) { | ||
| 65 | return 0, errors.New("the session ended without an exit status") | ||
| 66 | } | ||
| 67 | return 0, fmt.Errorf("the session was cut short: %w", err) | ||
| 68 | } | ||
| 69 | switch kind { | ||
| 70 | case KindStdout: | ||
| 71 | if _, err := stdout.Write(payload); err != nil { | ||
| 72 | return 0, err | ||
| 73 | } | ||
| 74 | case KindStderr: | ||
| 75 | if _, err := stderr.Write(payload); err != nil { | ||
| 76 | return 0, err | ||
| 77 | } | ||
| 78 | case KindExit: | ||
| 79 | var exit Exit | ||
| 80 | if err := json.Unmarshal(payload, &exit); err != nil { | ||
| 81 | return 0, fmt.Errorf("the exit status is not readable: %w", err) | ||
| 82 | } | ||
| 83 | return exit.Code, nil | ||
| 84 | } | ||
| 85 | // An unknown frame is ignored rather than fatal: this side cannot know | ||
| 86 | // what a newer service might add, and the exit frame still ends the | ||
| 87 | // session. | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | // pumpStdin streams the caller's input to the session and then says it ended. A | ||
| 92 | // read error is reported as end-of-input: the only thing the service can do with | ||
| 93 | // a client whose input failed is finish the command. | ||
| 94 | func pumpStdin(w io.Writer, stdin io.Reader) { | ||
| 95 | if stdin == nil { | ||
| 96 | _ = WriteFrame(w, KindStdinEOF, nil) | ||
| 97 | return | ||
| 98 | } | ||
| 99 | buf := make([]byte, stdinChunk) | ||
| 100 | for { | ||
| 101 | n, err := stdin.Read(buf) | ||
| 102 | if n > 0 { | ||
| 103 | if err := WriteFrame(w, KindStdin, buf[:n]); err != nil { | ||
| 104 | return | ||
| 105 | } | ||
| 106 | } | ||
| 107 | if err != nil { | ||
| 108 | _ = WriteFrame(w, KindStdinEOF, nil) | ||
| 109 | return | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||