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.
|
c5f2c5d4
Keenan Nemetz
16d ago
|
1 | package ssh |
| 2 | ||
| 3 | // The front door's transport. | |
| 4 | // | |
| 5 | // sshd authenticates the key and runs a forced command as the `git` account, | |
| 6 | // which has no access to the instance's data. That command (`vole ssh-exec`) | |
| 7 | // hands the request to this process over a Unix socket, and this file turns the | |
| 8 | // request back into the same Session the built-in listener produces. The verb | |
| 9 | // that runs is the verb `exec` dispatches, with the account and the credential | |
| 10 | // scope the token carries: one dispatch path, one set of access rules. | |
| 11 | ||
| 12 | import ( | |
| 13 | "encoding/json" | |
| 14 | "errors" | |
| 15 | "fmt" | |
| 16 | "io" | |
| 17 | "net" | |
| 18 | "sync" | |
| 19 | "time" | |
| 20 | ||
| 21 | "rodent.software/vole/internal/sshwire" | |
| 22 | ) | |
| 23 | ||
| 24 | // sweepInterval is how often expired tokens are dropped. Tokens live for a | |
| 25 | // minute, so this keeps the table within about two minutes of rows; the mint | |
| 26 | // path prunes as well, which bounds an instance that authenticates hard. | |
| 27 | const sweepInterval = 30 * time.Second | |
| 28 | ||
| 29 | // ServeLocal answers framed requests from the forced command until the listener | |
| 30 | // is closed. | |
| 31 | func (s *Server) ServeLocal(ln net.Listener) error { | |
| 32 | if s.cfg.Sessions == nil { | |
| 33 | return errors.New("ssh: the front door needs a session token store") | |
| 34 | } | |
| 35 | s.sweepOnce.Do(func() { go s.sweepSessions() }) | |
| 36 | return s.accept(ln, s.handleLocal) | |
| 37 | } | |
| 38 | ||
| 39 | // sweepSessions drops tokens that expired without being spent: a connection | |
| 40 | // that never reached the forced command, or one whose command never ran. | |
| 41 | func (s *Server) sweepSessions() { | |
| 42 | t := time.NewTicker(sweepInterval) | |
| 43 | defer t.Stop() | |
| 44 | for { | |
| 45 | select { | |
| 46 | case <-s.done: | |
| 47 | return | |
| 48 | case now := <-t.C: | |
| 49 | if n, err := s.cfg.Sessions.Prune(now.UTC()); err != nil { | |
| 50 | s.cfg.Log.Warn("ssh: cannot prune session tokens", "err", err) | |
| 51 | } else if n > 0 { | |
| 52 | s.cfg.Log.Debug("ssh: dropped expired session tokens", "count", n) | |
| 53 | } | |
| 54 | } | |
| 55 | } | |
| 56 | } | |
| 57 | ||
| 58 | // handleLocal serves one framed request. The handshake is a deadline-bounded | |
| 59 | // hello, then the token, then the session: a connection that says nothing, or | |
| 60 | // answers slowly, cannot hold a slot. | |
| 61 | func (s *Server) handleLocal(conn net.Conn) { | |
| 62 | defer conn.Close() | |
| 63 | if err := conn.SetDeadline(time.Now().Add(s.cfg.handshakeTimeout())); err != nil { | |
| 64 | return | |
| 65 | } | |
| 66 | kind, payload, err := sshwire.ReadFrame(conn) | |
| 67 | if err != nil || kind != sshwire.KindHello { | |
| 68 | return | |
| 69 | } | |
| 70 | var hello sshwire.Hello | |
| 71 | if err := json.Unmarshal(payload, &hello); err != nil { | |
| 72 | return | |
| 73 | } | |
| 74 | if hello.Command == "" { | |
| 75 | s.refuseLocal(conn, "no command: this account runs no shell") | |
| 76 | return | |
| 77 | } | |
| 78 | ||
| 79 | tok, err := s.cfg.Sessions.Consume(hello.Session, hello.Remote, time.Now().UTC()) | |
| 80 | if err != nil { | |
| 81 | s.cfg.Log.Error("ssh: cannot read the session token table", "err", err) | |
| 82 | s.refuseLocal(conn, "the instance cannot read its session store") | |
| 83 | return | |
| 84 | } | |
| 85 | if tok == nil { | |
| 86 | // One answer for unknown, spent, expired, and minted-for-somebody-else: a | |
| 87 | // caller that could tell them apart could probe the table. | |
| 88 | s.cfg.Log.Warn("ssh: refused a front door session", "remote", hello.Remote) | |
| 89 | s.refuseLocal(conn, "no session: the token is unknown, spent, expired, or was minted for another connection") | |
| 90 | return | |
| 91 | } | |
| 92 | ||
| 93 | // The request is answered before the command runs, so a client that is | |
| 94 | // waiting for an answer never confuses one with the command's output. | |
| 95 | if err := conn.SetDeadline(time.Time{}); err != nil { | |
| 96 | return | |
| 97 | } | |
| 98 | if err := sshwire.WriteJSON(conn, sshwire.KindAccept, sshwire.Accept{OK: true}); err != nil { | |
| 99 | return | |
| 100 | } | |
| 101 | s.cfg.Log.Info("ssh front door", "user", tok.Account, "remote", hello.Remote) | |
| 102 | s.exec(&localSession{conn: conn}, hello.Command, tok.Account, tok.Key, hello.Remote) | |
| 103 | } | |
| 104 | ||
| 105 | // refuseLocal answers a request that cannot be served, then ends the connection. | |
| 106 | func (s *Server) refuseLocal(conn net.Conn, reason string) { | |
| 107 | _ = sshwire.WriteJSON(conn, sshwire.KindAccept, sshwire.Accept{Error: reason}) | |
| 108 | } | |
| 109 | ||
| 110 | // localSession is a Session carried by frames on a Unix socket. | |
| 111 | // | |
| 112 | // Reading is a single goroutine (git copying its stdin); writing is not, because | |
| 113 | // stdout, stderr and the exit frame all travel on the same socket and share one | |
| 114 | // lock. A write larger than a frame is split rather than refused, so a caller | |
| 115 | // that writes a 1 MiB buffer still gets all of it across. | |
| 116 | type localSession struct { | |
| 117 | conn net.Conn | |
| 118 | mu sync.Mutex | |
| 119 | buf []byte // stdin bytes read from a frame and not yet consumed | |
| 120 | } | |
| 121 | ||
| 122 | var _ Session = (*localSession)(nil) | |
| 123 | ||
| 124 | // Accept is the hello's answer, already sent: the exec request of a local | |
| 125 | // session is the request itself, and answering it twice would put a second | |
| 126 | // control frame in the client's stream. | |
| 127 | func (s *localSession) Accept() error { return nil } | |
| 128 | ||
| 129 | // CloseWrite is a no-op. A stream of frames has no half-close to signal: the | |
| 130 | // client stops reading when the exit frame or the connection ends. | |
| 131 | func (s *localSession) CloseWrite() error { return nil } | |
| 132 | ||
| 133 | func (s *localSession) Close() error { return s.conn.Close() } | |
| 134 | ||
| 135 | // Exit sends the status the client turns into its own exit code. | |
| 136 | func (s *localSession) Exit(code int) { | |
| 137 | s.mu.Lock() | |
| 138 | defer s.mu.Unlock() | |
| 139 | _ = sshwire.WriteJSON(s.conn, sshwire.KindExit, sshwire.Exit{Code: code}) | |
| 140 | } | |
| 141 | ||
| 142 | func (s *localSession) Write(p []byte) (int, error) { | |
| 143 | return s.write(sshwire.KindStdout, p) | |
| 144 | } | |
| 145 | ||
| 146 | // Stderr is a writer that frames its bytes as the session's error stream, so a | |
| 147 | // client can keep the two apart. | |
| 148 | func (s *localSession) Stderr() io.Writer { return localStream{s: s, kind: sshwire.KindStderr} } | |
| 149 | ||
| 150 | type localStream struct { | |
| 151 | s *localSession | |
| 152 | kind sshwire.Kind | |
| 153 | } | |
| 154 | ||
| 155 | func (w localStream) Write(p []byte) (int, error) { return w.s.write(w.kind, p) } | |
| 156 | ||
| 157 | func (s *localSession) write(kind sshwire.Kind, p []byte) (int, error) { | |
| 158 | if len(p) == 0 { | |
| 159 | return 0, nil | |
| 160 | } | |
| 161 | s.mu.Lock() | |
| 162 | defer s.mu.Unlock() | |
| 163 | written := 0 | |
| 164 | for len(p) > 0 { | |
| 165 | n := min(len(p), sshwire.MaxFrame) | |
| 166 | if err := sshwire.WriteFrame(s.conn, kind, p[:n]); err != nil { | |
| 167 | return written, err | |
| 168 | } | |
| 169 | written += n | |
| 170 | p = p[n:] | |
| 171 | } | |
| 172 | return written, nil | |
| 173 | } | |
| 174 | ||
| 175 | func (s *localSession) Read(p []byte) (int, error) { | |
| 176 | if len(s.buf) > 0 { | |
| 177 | n := copy(p, s.buf) | |
| 178 | s.buf = s.buf[n:] | |
| 179 | return n, nil | |
| 180 | } | |
| 181 | for { | |
| 182 | kind, payload, err := sshwire.ReadFrame(s.conn) | |
| 183 | if err != nil { | |
| 184 | return 0, err | |
| 185 | } | |
| 186 | switch kind { | |
| 187 | case sshwire.KindStdin: | |
| 188 | if len(payload) == 0 { | |
| 189 | continue | |
| 190 | } | |
| 191 | n := copy(p, payload) | |
| 192 | if n < len(payload) { | |
| 193 | s.buf = payload[n:] | |
| 194 | } | |
| 195 | return n, nil | |
| 196 | case sshwire.KindStdinEOF: | |
| 197 | return 0, io.EOF | |
| 198 | default: | |
| 199 | return 0, fmt.Errorf("ssh: unexpected %q frame on a session", string(rune(kind))) | |
| 200 | } | |
| 201 | } | |
| 202 | } |