ssh

package
v0.0.0-...-6b8ee43 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: AGPL-3.0 Imports: 33 Imported by: 0

Documentation

Overview

Package ssh implements the SSH server that serves the interactive CLI over public-key-authenticated sessions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandExecutor

type CommandExecutor func(input string) (string, error)

CommandExecutor executes an operational command and returns the output. The SSH server calls this directly — no socket indirection needed since the server runs as a goroutine inside the daemon process.

type CommandExecutorFactory

type CommandExecutorFactory func(username, remoteAddr string) CommandExecutor

CommandExecutorFactory creates a per-session CommandExecutor. The username is the authenticated SSH user; remoteAddr is the SSH client's IP:port. The returned executor can use both for authorization and accounting.

type Config

type Config struct {
	Listen        string
	ListenAddrs   []string // all listen addresses (first == Listen)
	HostKeyPath   string
	HostCertPath  string          // optional: path to SSH host certificate (signed by CA)
	ConfigDir     string          // directory of the config file; used for host-key default
	ConfigPath    string          // path to config file; used by SSH sessions for concurrent editing
	Storage       storage.Storage // when set, host key is read from/stored to blob
	IdleTimeout   uint32
	MaxSessions   int
	Users         []authz.UserConfig
	Authenticator authz.Authenticator // pluggable auth backend (nil = use Users with bcrypt)
	AuditRecorder audit.Recorder      // records failed authentication attempts when set
	Executor      CommandExecutor     // injected by daemon, not from config
}

Config holds SSH server configuration parsed from the config file.

type LoginWarningsFunc

type LoginWarningsFunc func() []contract.LoginWarning

LoginWarningsFunc returns login warnings to display when an SSH session starts. Returns nil when no warnings exist. Injected by the daemon via SetLoginWarnings after the reactor starts.

type PluginProtocolFunc

type PluginProtocolFunc func(ctx context.Context, reader io.ReadCloser, writer io.WriteCloser) error

PluginProtocolFunc handles a "plugin protocol" SSH session by running the 5-stage plugin handshake and runtime command loop over the SSH channel. The SSH channel provides bidirectional io.ReadCloser/io.WriteCloser transport. Blocks until the session ends. Called by execMiddleware.

type RebootFunc

type RebootFunc func()

RebootFunc is called when the SSH server receives a "reboot" exec command. It initiates graceful shutdown followed by an OS-level system reboot.

type RestartFunc

type RestartFunc func()

RestartFunc is called when the SSH server receives a "restart" exec command. It writes the GR marker to zefs, then shuts down the daemon.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the SSH server subsystem. It serves the config editor over SSH with password authentication. Exec commands (non-interactive) are dispatched through the executor.

func NewServer

func NewServer(cfg Config) (*Server, error)

NewServer creates a new SSH server with the given configuration. It applies defaults for unset fields but does not start listening. If HostKeyPath is empty, it defaults to ssh_host_ed25519_key in ConfigDir. If ConfigDir is also empty, it resolves from the binary location via paths.DefaultConfigDir(). Wish auto-generates the key file if it does not exist.

func (*Server) ActiveSessions

func (s *Server) ActiveSessions() int32

ActiveSessions returns the current number of active SSH sessions.

func (*Server) Address

func (s *Server) Address() string

Address returns the listen address. If the server has bound to a port (e.g., port 0), it returns the actual bound address. Otherwise returns the configured address.

func (*Server) ExecutorForUser

func (s *Server) ExecutorForUser(username, remoteAddr string) CommandExecutor

ExecutorForUser returns a CommandExecutor for the given username. Returns nil if no executor factory is set.

func (*Server) HasSessionModelFactory

func (s *Server) HasSessionModelFactory() bool

HasSessionModelFactory reports whether a session model factory has been set.

func (*Server) LoginWarningsFunc

func (s *Server) LoginWarningsFunc() LoginWarningsFunc

LoginWarningsFunc returns the login warnings function, or nil if not set.

func (*Server) MaxSessions

func (s *Server) MaxSessions() int

MaxSessions returns the configured maximum concurrent sessions.

func (*Server) MonitorFactoryFunc

func (s *Server) MonitorFactoryFunc() contract.MonitorFactory

MonitorFactoryFunc returns the monitor factory, or nil if not set.

func (*Server) Name

func (s *Server) Name() string

Name returns the subsystem identifier.

func (*Server) Reload

func (s *Server) Reload(_ context.Context, _ ze.ConfigProvider) error

Reload applies configuration changes. It implements ze.Subsystem.

func (*Server) RestartFunc

func (s *Server) RestartFunc() RestartFunc

RestartFunc returns the restart callback, or nil if not set.

func (*Server) SetExecutorFactory

func (s *Server) SetExecutorFactory(f CommandExecutorFactory)

SetExecutorFactory sets the per-session command executor factory. Called after the reactor starts and the Dispatcher is available. New sessions created after this call will use the factory to create per-session executors with the authenticated username.

func (*Server) SetLoginWarnings

func (s *Server) SetLoginWarnings(f LoginWarningsFunc)

SetLoginWarnings sets the function that returns login warnings for new SSH sessions. Called by the daemon after the reactor starts, alongside SetExecutorFactory. The function is called once per session in createSessionModel.

func (*Server) SetMonitorFactory

func (s *Server) SetMonitorFactory(f contract.MonitorFactory)

SetMonitorFactory sets the factory for TUI monitor sessions. Called after the reactor starts to wire streaming monitor into interactive SSH sessions.

func (*Server) SetPluginProtocolFunc

func (s *Server) SetPluginProtocolFunc(f PluginProtocolFunc)

SetPluginProtocolFunc sets the handler for "plugin protocol" SSH sessions. Called after the reactor starts to enable plugin debug shell.

func (*Server) SetRebootFunc

func (s *Server) SetRebootFunc(f RebootFunc)

SetRebootFunc sets the callback for "reboot" exec commands. Called by the daemon to wire graceful shutdown + OS reboot via SSH.

func (*Server) SetRestartFunc

func (s *Server) SetRestartFunc(f RestartFunc)

SetRestartFunc sets the callback for "restart" exec commands. Called by the daemon to wire graceful restart (GR marker + shutdown) via SSH.

func (*Server) SetSessionModelFactory

func (s *Server) SetSessionModelFactory(f contract.SessionModelFactory)

SetSessionModelFactory sets the factory that creates TUI models for SSH sessions. Called by the hub before starting the SSH server.

func (*Server) SetShutdownFunc

func (s *Server) SetShutdownFunc(f ShutdownFunc)

SetShutdownFunc sets the callback for "stop" exec commands. Called by the daemon to wire graceful shutdown via SSH.

func (*Server) SetStreamingExecutorFactory

func (s *Server) SetStreamingExecutorFactory(f StreamingExecutorFactory)

SetStreamingExecutorFactory sets the factory for streaming commands (e.g., monitor). Called after the reactor starts, alongside SetExecutorFactory.

func (*Server) ShutdownFunc

func (s *Server) ShutdownFunc() ShutdownFunc

ShutdownFunc returns the shutdown callback, or nil if not set.

func (*Server) Start

func (s *Server) Start(ctx context.Context, _ ze.EventBus, _ ze.ConfigProvider) error

Start launches the SSH server. It implements ze.Subsystem. The listener is created synchronously so bind failures are reported immediately.

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Stop gracefully shuts down the SSH server. It implements ze.Subsystem.

func (*Server) StreamingExecutorForUser

func (s *Server) StreamingExecutorForUser(username, remoteAddr string) StreamingExecutor

StreamingExecutorForUser returns a StreamingExecutor for the given username. Returns nil if no streaming executor factory is set.

func (*Server) Users

func (s *Server) Users() []authz.UserConfig

Users returns the configured user list.

type ShutdownFunc

type ShutdownFunc func()

ShutdownFunc is called when the SSH server receives a "stop" exec command.

type StreamingExecutor

type StreamingExecutor func(ctx context.Context, w io.Writer, args []string) error

StreamingExecutor executes a streaming command, writing output line-by-line to the writer until the context is canceled or a write error occurs. Used by the monitor command to stream live BGP events over SSH.

type StreamingExecutorFactory

type StreamingExecutorFactory func(username, remoteAddr string) StreamingExecutor

StreamingExecutorFactory creates a StreamingExecutor. Called when execMiddleware detects a streaming command (e.g., "bgp monitor"). The username and remoteAddr are passed through so streaming commands share the same authorization/accounting context as normal commands.

Directories

Path Synopsis
Package yang embeds and registers the SSH configuration YANG module.
Package yang embeds and registers the SSH configuration YANG module.

Jump to

Keyboard shortcuts

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