internal/sshd/sshd.go

460 lines · 14101 bytes

  1// Package sshd implements the embedded SSH listener: public-key auth against
  2// registered keys, then dispatch to git transport or control commands.
  3package sshd
  4
  5import (
  6	"context"
  7	"crypto/ed25519"
  8	"crypto/rand"
  9	"encoding/base64"
 10	"encoding/pem"
 11	"errors"
 12	"fmt"
 13	"io"
 14	"log/slog"
 15	"net"
 16	"os"
 17	"path/filepath"
 18	"strconv"
 19	"sync"
 20	"sync/atomic"
 21	"time"
 22
 23	"golang.org/x/crypto/ssh"
 24
 25	"gitbay.org/gitbay/internal/config"
 26	"gitbay.org/gitbay/internal/control"
 27	"gitbay.org/gitbay/internal/gitutil"
 28	"gitbay.org/gitbay/internal/hookd"
 29	"gitbay.org/gitbay/internal/policy"
 30	"gitbay.org/gitbay/internal/protocol"
 31	"gitbay.org/gitbay/internal/store"
 32)
 33
 34type Server struct {
 35	cfg         config.Config
 36	st          *store.Store
 37	sshCfg      *ssh.ServerConfig
 38	authLimiter *rateLimiter
 39	sessions    sync.WaitGroup // accepted connections still being served
 40	mu          sync.Mutex
 41	conns       map[*conn]struct{}
 42	stopping    chan struct{} // closed by Stop
 43	stopOnce    sync.Once
 44}
 45
 46// conn is one accepted connection and how many sessions it is running.
 47// A CLI's shared connection sits idle between commands; on shutdown an
 48// idle connection is closed at once and only a session mid-command is
 49// waited for (#141).
 50type conn struct {
 51	net    net.Conn
 52	active atomic.Int32
 53}
 54
 55func New(cfg config.Config, st *store.Store) (*Server, error) {
 56	s := &Server{cfg: cfg, st: st, authLimiter: newRateLimiter(cfg.Limits.SSHAuthRate, time.Minute), conns: map[*conn]struct{}{}, stopping: make(chan struct{})}
 57
 58	sc := &ssh.ServerConfig{
 59		PublicKeyCallback: s.authenticate,
 60		ServerVersion:     "SSH-2.0-gitbayd",
 61	}
 62	signers, err := loadHostKeys(cfg)
 63	if err != nil {
 64		return nil, err
 65	}
 66	for _, sg := range signers {
 67		sc.AddHostKey(sg)
 68	}
 69	s.sshCfg = sc
 70	return s, nil
 71}
 72
 73// loadHostKeys loads the configured host keys, or generates an ed25519 key
 74// under server.root/ssh/ when none are configured.
 75func loadHostKeys(cfg config.Config) ([]ssh.Signer, error) {
 76	paths := cfg.SSH.HostKeys
 77	if len(paths) == 0 {
 78		p := filepath.Join(cfg.Server.Root, "ssh", "host_ed25519")
 79		if _, err := os.Stat(p); errors.Is(err, os.ErrNotExist) {
 80			if err := generateHostKey(p); err != nil {
 81				return nil, fmt.Errorf("generating host key: %w", err)
 82			}
 83			slog.Info("generated ssh host key", "path", p)
 84		}
 85		paths = []string{p}
 86	}
 87	var signers []ssh.Signer
 88	for _, p := range paths {
 89		raw, err := os.ReadFile(p)
 90		if err != nil {
 91			return nil, fmt.Errorf("host key %s: %w", p, err)
 92		}
 93		sg, err := ssh.ParsePrivateKey(raw)
 94		if err != nil {
 95			return nil, fmt.Errorf("host key %s: %w", p, err)
 96		}
 97		signers = append(signers, sg)
 98	}
 99	return signers, nil
100}
101
102func generateHostKey(path string) error {
103	if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
104		return err
105	}
106	_, priv, err := ed25519.GenerateKey(rand.Reader)
107	if err != nil {
108		return err
109	}
110	block, err := ssh.MarshalPrivateKey(priv, "")
111	if err != nil {
112		return err
113	}
114	return os.WriteFile(path, pem.EncodeToMemory(block), 0o600)
115}
116
117// authenticate resolves the presented key to a registered account. The SSH
118// username is ignored; identity comes from the key alone. When registration
119// is open or invite-based, unknown keys are admitted to run exactly one
120// command: register.
121func (s *Server) authenticate(meta ssh.ConnMetadata, pub ssh.PublicKey) (*ssh.Permissions, error) {
122	ip := remoteIP(meta.RemoteAddr())
123	if !s.authLimiter.allow(ip) {
124		// One audit entry per throttled window, not per rejected attempt.
125		if s.authLimiter.firstThrottle(ip) {
126			s.st.Audit(0, "auth.throttled", map[string]any{"ip": ip, "rate": s.cfg.Limits.SSHAuthRate})
127		}
128		return nil, fmt.Errorf("too many authentication attempts; try again shortly")
129	}
130	fp := ssh.FingerprintSHA256(pub)
131	key, err := s.st.SSHKeyByFingerprint(fp)
132	if err != nil && !errors.Is(err, store.ErrNotFound) {
133		// The store, not the key, failed. Neither a failure against the
134		// limiter nor "unknown key": a busy database during a restart
135		// would otherwise lock every client out for a minute.
136		slog.Error("ssh auth: key lookup", "err", err)
137		return nil, fmt.Errorf("authentication temporarily unavailable")
138	}
139	if err != nil {
140		if s.cfg.Registration.Mode != "closed" {
141			return &ssh.Permissions{Extensions: map[string]string{
142				"anon-key": base64.StdEncoding.EncodeToString(pub.Marshal()),
143			}}, nil
144		}
145		s.authLimiter.fail(ip)
146		s.st.Audit(0, "auth.failed", map[string]any{"ip": ip, "fingerprint": fp})
147		return nil, fmt.Errorf("unknown key %s", fp)
148	}
149	s.authLimiter.success(ip)
150	return &ssh.Permissions{Extensions: map[string]string{
151		"user-id": strconv.FormatInt(key.UserID, 10),
152		"key-id":  strconv.FormatInt(key.ID, 10),
153		"key-fp":  fp,
154		"scope":   key.Scope,
155	}}, nil
156}
157
158// Serve accepts connections on ln until it is closed.
159func (s *Server) Serve(ln net.Listener) error {
160	for {
161		nc, err := ln.Accept()
162		if err != nil {
163			return err
164		}
165		c := &conn{net: nc}
166		s.mu.Lock()
167		s.conns[c] = struct{}{}
168		s.mu.Unlock()
169		s.sessions.Add(1)
170		go func() {
171			defer s.sessions.Done()
172			defer func() {
173				s.mu.Lock()
174				delete(s.conns, c)
175				s.mu.Unlock()
176			}()
177			s.handleConn(c)
178		}()
179	}
180}
181
182// Stop ends the commands that run until something happens (build log
183// --follow), so a shutdown drain waits only for work that finishes. It
184// does not close connections; Shutdown does.
185func (s *Server) Stop() {
186	s.stopOnce.Do(func() { close(s.stopping) })
187}
188
189// Shutdown closes every idle connection, then waits for the ones with a
190// session running, or for ctx. The caller closes the listener first; a
191// push in flight completes rather than being cut mid-pack.
192func (s *Server) Shutdown(ctx context.Context) error {
193	s.Stop()
194	s.mu.Lock()
195	for c := range s.conns {
196		if c.active.Load() == 0 {
197			c.net.Close()
198		}
199	}
200	s.mu.Unlock()
201	done := make(chan struct{})
202	go func() {
203		s.sessions.Wait()
204		close(done)
205	}()
206	select {
207	case <-done:
208		return nil
209	case <-ctx.Done():
210		return ctx.Err()
211	}
212}
213
214func (s *Server) handleConn(c *conn) {
215	defer c.net.Close()
216	sconn, chans, reqs, err := ssh.NewServerConn(c.net, s.sshCfg)
217	if err != nil {
218		return
219	}
220	defer sconn.Close()
221	go ssh.DiscardRequests(reqs)
222
223	for newCh := range chans {
224		if newCh.ChannelType() != "session" {
225			newCh.Reject(ssh.UnknownChannelType, "only session channels are supported")
226			continue
227		}
228		ch, chReqs, err := newCh.Accept()
229		if err != nil {
230			continue
231		}
232		c.active.Add(1)
233		go func() {
234			defer c.active.Add(-1)
235			s.handleSession(sconn, ch, chReqs)
236		}()
237	}
238}
239
240func (s *Server) handleSession(sconn *ssh.ServerConn, ch ssh.Channel, reqs <-chan *ssh.Request) {
241	defer ch.Close()
242	for req := range reqs {
243		switch req.Type {
244		case "exec":
245			var payload struct{ Command string }
246			if err := ssh.Unmarshal(req.Payload, &payload); err != nil {
247				req.Reply(false, nil)
248				continue
249			}
250			req.Reply(true, nil)
251			// x/crypto closes reqs when the client closes the channel. That
252			// is how a follow learns nobody is reading: the CLI's shared
253			// connection outlives a Ctrl-C, the channel does not. Stop
254			// ends it too, for a restart.
255			closed := make(chan struct{})
256			go func() {
257				for r := range reqs {
258					r.Reply(false, nil)
259				}
260				close(closed)
261			}()
262			done := make(chan struct{})
263			go func() {
264				select {
265				case <-closed:
266				case <-s.stopping:
267				}
268				close(done)
269			}()
270			code := s.runExec(sconn, ch, payload.Command, done)
271			sendExit(ch, code)
272			return
273		case "shell":
274			req.Reply(true, nil)
275			fmt.Fprintf(ch, "gitbay control plane: interactive shells are not available.\nTry: ssh %s help\n", s.cfg.Server.SiteURL)
276			sendExit(ch, protocol.ExitUsage)
277			return
278		case "pty-req", "env":
279			// Harmless; accept and ignore.
280			req.Reply(true, nil)
281		default:
282			req.Reply(false, nil)
283		}
284	}
285}
286
287func sendExit(ch ssh.Channel, code int) {
288	var msg = struct{ Status uint32 }{uint32(code)}
289	ch.SendRequest("exit-status", false, ssh.Marshal(&msg))
290}
291
292func (s *Server) runExec(sconn *ssh.ServerConn, ch ssh.Channel, cmdline string, done <-chan struct{}) int {
293	ext := sconn.Permissions.Extensions
294	if blob := ext["anon-key"]; blob != "" {
295		return s.runAnonymous(ch, blob, cmdline)
296	}
297	userID, _ := strconv.ParseInt(ext["user-id"], 10, 64)
298	keyID, _ := strconv.ParseInt(ext["key-id"], 10, 64)
299	user, err := s.st.UserByID(userID)
300	if err != nil {
301		fmt.Fprintln(ch.Stderr(), "account no longer exists")
302		return protocol.ExitDenied
303	}
304	_ = s.st.TouchSSHKey(keyID)
305	return Exec(s.cfg, s.st, user, ext["scope"], ext["key-fp"], cmdline, ch, ch, ch.Stderr(), done, s.stopping)
306}
307
308// runAnonymous handles a session from an unregistered key: the register
309// command and nothing else.
310func (s *Server) runAnonymous(ch ssh.Channel, keyB64, cmdline string) int {
311	raw, err := base64.StdEncoding.DecodeString(keyB64)
312	if err != nil {
313		return protocol.ExitFailure
314	}
315	pub, err := ssh.ParsePublicKey(raw)
316	if err != nil {
317		return protocol.ExitFailure
318	}
319	argv, err := protocol.Tokenize(cmdline)
320	if err != nil {
321		fmt.Fprintf(ch.Stderr(), "cannot parse command: %v\n", err)
322		return protocol.ExitUsage
323	}
324	if len(argv) == 0 || argv[0] != "register" {
325		fmt.Fprintf(ch.Stderr(), "this key is not registered here. Create an account with:\n  ssh <host> register --username <name> %s\n",
326			map[string]string{"open": "--email <address>", "invite": "--invite <code>"}[s.cfg.Registration.Mode])
327		return protocol.ExitDenied
328	}
329	return control.RunRegister(s.cfg, s.st, pub, argv, ch, ch.Stderr())
330}
331
332// Exec runs one SSH exec command line for an authenticated key. It is the
333// single dispatch path shared by the embedded listener and the system-sshd
334// forced command (gitbayd shell).
335func Exec(cfg config.Config, st *store.Store, user store.User, scope, source, cmdline string,
336	stdin io.Reader, stdout, stderr io.Writer, done, stopping <-chan struct{}) int {
337	if user.Disabled {
338		fmt.Fprintln(stderr, "this account is disabled; contact the instance admin")
339		return protocol.ExitDenied
340	}
341	argv, err := protocol.Tokenize(cmdline)
342	if err != nil {
343		fmt.Fprintf(stderr, "cannot parse command: %v\n", err)
344		return protocol.ExitUsage
345	}
346	if len(argv) > 0 {
347		switch argv[0] {
348		case "git-upload-pack", "git-receive-pack", "git-upload-archive":
349			if user.Pending {
350				fmt.Fprintln(stderr, "your account is not active yet: verify your email first")
351				return protocol.ExitDenied
352			}
353			return runGit(cfg, st, user, scope, argv, stdin, stdout, stderr)
354		case "git-lfs-authenticate":
355			// Part of the git transport, not the control plane: usable by
356			// git-scoped and deploy keys, with the transports' access rules.
357			if user.Pending {
358				fmt.Fprintln(stderr, "your account is not active yet: verify your email first")
359				return protocol.ExitDenied
360			}
361			return runLFSAuthenticate(cfg, st, user, scope, argv, stdout, stderr)
362		}
363	}
364	ctx := &control.Ctx{
365		User:     user,
366		Scope:    scope,
367		Source:   source,
368		Store:    st,
369		Cfg:      cfg,
370		Stdin:    stdin,
371		Stdout:   stdout,
372		Stderr:   stderr,
373		Done:     done,
374		Stopping: stopping,
375	}
376	return control.Dispatch(ctx, argv)
377}
378
379// runGit streams a git transport service after access checks.
380func runGit(cfg config.Config, st *store.Store, user store.User, scope string, argv []string,
381	stdin io.Reader, stdout, stderr io.Writer) int {
382	service := argv[0]
383	if len(argv) != 2 {
384		fmt.Fprintf(stderr, "usage: %s <path>\n", service)
385		return protocol.ExitUsage
386	}
387	write := service == "git-receive-pack"
388
389	repo, err := st.RepoByPath(argv[1])
390	if err != nil {
391		fmt.Fprintln(stderr, "repository not found")
392		return protocol.ExitNotFound
393	}
394	if policy.IsDeployScope(scope) {
395		// A deploy key authorizes by its binding alone: one repository,
396		// its mode, nothing inherited from whoever registered it. Any
397		// mismatch reads as nonexistence, same as the access rules.
398		if !policy.DeployScopeAllows(scope, repo.ID, write) {
399			fmt.Fprintln(stderr, "repository not found")
400			return protocol.ExitNotFound
401		}
402	} else {
403		grant, err := st.AccessRole(repo.ID, user.ID)
404		if err != nil {
405			fmt.Fprintln(stderr, "internal error")
406			return protocol.ExitFailure
407		}
408		if !policy.CanRead(user, repo, grant) {
409			// Same answer as nonexistence: private repos must not be enumerable.
410			fmt.Fprintln(stderr, "repository not found")
411			return protocol.ExitNotFound
412		}
413		if !policy.ScopeAllowsGit(scope, repo.Path(), write) {
414			fmt.Fprintf(stderr, "this key's scope (%s) does not allow %s on %s\n", scope, service, repo.Path())
415			return protocol.ExitDenied
416		}
417		if write && !policy.CanWrite(user, repo, grant) {
418			fmt.Fprintf(stderr, "write access to %s denied\n", repo.Path())
419			return protocol.ExitDenied
420		}
421	}
422	if write && repo.Settings.Archived {
423		fmt.Fprintf(stderr, "%s is archived and read-only\n", repo.Path())
424		return protocol.ExitDenied
425	}
426	if write {
427		if mirrored, err := st.PullMirrored(repo.ID); err == nil && mirrored {
428			fmt.Fprintf(stderr, "%s is a pull mirror: its refs come from the upstream; push there instead\n", repo.Path())
429			return protocol.ExitDenied
430		}
431	}
432
433	dir := control.RepoDir(cfg.Server.Root, repo.OwnerName, repo.Name)
434	env := []string{
435		hookd.EnvSocket + "=" + hookd.SocketPath(cfg.Server.Root),
436		hookd.EnvRepoID + "=" + strconv.FormatInt(repo.ID, 10),
437		hookd.EnvUserID + "=" + strconv.FormatInt(user.ID, 10),
438		hookd.EnvScope + "=" + scope,
439	}
440	// A storage quota on the owner rides the same mechanism as the pack
441	// cap: the pack may be no larger than what the owner has left.
442	maxPack := cfg.Limits.MaxPackBytes
443	if write && repo.OwnerKind == "user" {
444		if limit := control.ByteLimit(st, control.QuotaConfig(cfg), repo.OwnerID); limit > 0 {
445			used := control.OwnedBytes(st, cfg.Server.Root, repo.OwnerID)
446			left := limit - used
447			if left <= 0 {
448				fmt.Fprintf(stderr, "%s's storage quota is used up (%d of %d bytes); delete something, or ask an admin to raise the limit\n", repo.OwnerName, used, limit)
449				return protocol.ExitDenied
450			}
451			if maxPack == 0 || left < maxPack {
452				maxPack = left
453			}
454		}
455	}
456	if err := gitutil.Transport(service, dir, stdin, stdout, stderr, env, maxPack); err != nil {
457		return protocol.ExitFailure
458	}
459	return protocol.ExitOK
460}