gitssh

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: BSD-3-Clause Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gitssh serves git clone and fetch (protocol v2, git-upload-pack) and push (git-receive-pack, through package gitpush) over SSH, in process, with golang.org/x/crypto/ssh. A client signs in with a public key a user registered through the admin API; the only things it can do afterwards are run git-upload-pack on a repository it may read and git-receive-pack on one it may push to. There is no shell, no pty, no subsystem, no forwarding of any kind, no other channel.

Index

Constants

View Source
const (
	MaxAuthTries     = 3
	MaxPreExec       = 16 // channel requests tolerated before the exec
	MaxEnvValueLen   = 256
	DefaultHandshake = 10 * time.Second
	DefaultIdle      = 30 * time.Second // from the session channel to its exec
)

Bounds on one connection.

View Source
const DefaultMaxLifetime = time.Hour

DefaultMaxLifetime is the longest an SSH connection lives.

View Source
const MaxCommandLen = 512

MaxCommandLen bounds an exec request's command line.

Variables

View Source
var ErrCommand = errors.New("only git-upload-pack and git-receive-pack are available: no shell, no other command")

ErrCommand is returned for any command that is not git-upload-pack or git-receive-pack.

View Source
var ErrPush = errors.New("push over SSH is not enabled on this forge; it serves clone and fetch only")

ErrPush is returned for git-receive-pack on a server that has no push engine.

Functions

func ExecService

func ExecService(cmd string) string

ExecService returns the service a command line asks for (git-upload-pack or git-receive-pack, compared whole), or "" for anything else.

func LoadHostKey

func LoadHostKey(path string) (ssh.Signer, error)

LoadHostKey reads the server's SSH host key from an OpenSSH private key file that must be a regular file, not a symbolic link, owned by this user and closed to group and others (the check the TLS key gets). The key must be ed25519 and carry no passphrase. It is never generated here: a host key made silently at start would change on every redeploy and teach clients to ignore the warning. Make it once with ssh-keygen -t ed25519 -f <file> with an empty passphrase.

func ParseExec

func ParseExec(cmd string) (gitrepo.Name, error)

ParseExec reads the command line git sends: git-upload-pack '<path>' or git-receive-pack '<path>' (the path always single-quoted, gitprotocol-pack). The command name is compared whole; the path must be owner/name and holds no quote, backslash or "!" (what git's own quoting would have to escape), so nothing is ever unquoted or interpreted: there is no shell.

Types

type Authenticator

type Authenticator interface {
	AuthenticateSSHKey(ctx context.Context, fingerprint string) (login string, err error)
}

Authenticator maps a key fingerprint to the account that registered it.

type Server

type Server struct {
	Auth  Authenticator
	Repos *gitrepo.Service
	// Push serves git-receive-pack; nil refuses every push.
	Push     *gitpush.Pusher
	Sessions *gitrepo.Limiter // keyed by account; Total and PerKey caps
	HostKey  ssh.Signer
	Log      *slog.Logger

	// RequestTimeout bounds one whole git session (0: gitrepo's default).
	RequestTimeout time.Duration
	// Handshake bounds the SSH handshake and authentication (0: 10 s).
	Handshake time.Duration
	// Idle bounds the wait for the exec once a session channel is open (0: 30 s).
	Idle time.Duration
	// MaxConns and MaxConnsPerHost cap open TCP connections (0: the admin
	// listener's caps).
	MaxConns, MaxConnsPerHost int
	// MaxLifetime ends a connection however active it is (0: 1 hour): an account
	// disabled, a key revoked or a collaborator removed is never served for
	// longer than this by a connection that authenticated before.
	MaxLifetime time.Duration
	// contains filtered or unexported fields
}

Server is the SSH git server.

func (*Server) Disconnect

func (s *Server) Disconnect(login string)

Disconnect closes every live connection of login. DisableUser calls it (see store.Store.OnUserDisabled), so a disabled account's sessions end with it.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, ln net.Listener) error

Serve accepts connections on ln until ctx ends or ln fails.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL