app

package
v0.1.0-rc6 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: BSD-3-Clause Imports: 44 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 app wires configuration, message, targets and delivery into one sendmail invocation. Everything the process takes from the system comes in through Deps, so a whole invocation runs in tests without /etc, a syslog daemon or a network.

Index

Constants

View Source
const SystemConfigPath = "etc/mailcrier.conf"

SystemConfigPath is the configuration file, relative to the root of SystemDeps().ConfigFS.

Variables

This section is empty.

Functions

func CancelOnSignal

func CancelOnSignal(parent context.Context) (context.Context, context.CancelFunc)

CancelOnSignal returns a context that SIGINT, SIGTERM or SIGHUP cancels, and the function that stops listening. A signal the process was started with ignored, as nohup and a background command of sh do, stays ignored: listening for it would undo that. A cancelled call ends what it waits for as a temporary failure kept in the spool: an HTTP request returns, and a hook, which runs in a process group of its own and so misses a Ctrl-C of the terminal, is killed with its group. After the first signal the default action applies again, so a second one ends the process at once.

func Harden

func Harden(p Process) (args []string, reexecErr error)

Harden runs first in main, before anything reads the environment or formats a time (TZ names a file the time package opens). An elevated process whose environment holds anything sanitizeEnv removes executes itself with the sanitized environment and does not return. When both exec attempts fail, Harden replaces the environment in place and returns the error; Run then works in a restricted mode.

The returned arguments, argv[0] excluded, go to Run. They start with sendmail.MarkerEnvConfig when MAILCRIER_CONFIG was set, so that the process after the exec can warn about the variable it no longer sees. A forged marker yields one extra warning, nothing else.

func Run

func Run(ctx context.Context, d Deps, args []string, stdin io.Reader) (code int)

Run handles one invocation and returns the process exit status; args are the arguments after argv[0].

Every log record and everything the standard log package prints passes through one redactor, which learns the secrets of the configuration right after loading it, before the first record that could quote them.

Types

type Credentials

type Credentials struct {
	// UID and GID are the real user and group of the caller.
	UID, GID int
	// EGID is the effective group; it differs from GID when the kernel
	// applied the setgid bit of the binary.
	EGID int
	// ServiceUID is the uid of the mailcrier system user, -1 when the
	// system has none.
	ServiceUID int
}

Credentials are the ids of the process that decide whether it runs with the group privilege of a setgid binary.

type Deps

type Deps struct {
	// NewLogger returns the logger for a syslog tag. Run calls it once with
	// the default tag and again when the configuration sets another one.
	NewLogger func(tag string) *slog.Logger
	// CloseLog closes what the loggers of NewLogger opened; Run calls it
	// when it returns, after its last record. nil closes nothing.
	CloseLog func()
	// ConfigFS is the file system root, "/" in a real invocation; ConfigPath
	// is the default configuration file relative to it. An absolute path
	// from --config or MAILCRIER_CONFIG and the secret files named in the
	// configuration are read from ConfigFS with the leading slash removed.
	ConfigFS   fs.FS
	ConfigPath string
	// HTTP performs the requests of HTTP-based targets. Run uses a copy
	// with the configured request timeout.
	HTTP *http.Client
	// Hostname is the name of the machine, sent along with each message.
	Hostname string
	// Now returns the current time; it dates a message without a Date
	// header.
	Now func() time.Time
	// Program is argv[0]; its base name selects the newaliases and mailq
	// modes.
	Program string
	// Stdout receives the output of mailq, --version and --help.
	Stdout io.Writer
	// Stderr receives what the standard log package writes: net/http and
	// its HTTP/2 transport print their debug output there.
	Stderr io.Writer
	// SetLogOutput redirects the standard log package, as log.SetOutput.
	SetLogOutput func(w io.Writer)
	// Credentials decide whether the process runs with the group of a
	// setgid binary.
	Credentials Credentials
	// Environ is the environment of the process, as os.Environ, after
	// Harden.
	Environ []string
	// ReexecErr is the error Harden returned: an elevated process that
	// could not execute itself with a clean environment.
	ReexecErr error
	// LookupUserName returns the login name of a uid from the user
	// database; false when there is none.
	LookupUserName func(uid int) (string, bool)
	// IsLogOnStderr reports whether the log goes to stderr, without a
	// syslog socket; nil means it never does.
	IsLogOnStderr func() bool
	// SpoolDir is the spool directory when the configuration sets none,
	// and where a message goes when the configuration is rejected; empty
	// turns the spool off.
	SpoolDir string
	// CatchSignals returns a context that a stop signal cancels, and the
	// function that restores the default action; nil keeps the default
	// action throughout. Run calls it once the message is read, or at once
	// for -q: until then the default action is right, as a Ctrl-C while
	// the message is typed must discard it.
	CatchSignals func(ctx context.Context) (context.Context, context.CancelFunc)
	// contains filtered or unexported fields
}

Deps are the parts of the environment an invocation uses.

func SystemDeps

func SystemDeps() Deps

SystemDeps returns the Deps of a real invocation, to be called after Harden: syslog, the root file system, the ids and environment of the process, and an HTTP transport.

type Process

type Process struct {
	// Argv is the whole command line, argv[0] included.
	Argv []string
	// Environ is the environment, as os.Environ.
	Environ []string
	// Credentials need only UID, GID and EGID here.
	Credentials Credentials
	// Exec replaces the process image, as syscall.Exec; it returns only
	// on failure.
	Exec func(path string, argv, env []string) error
	// ReplaceEnv makes env the whole environment of the process.
	ReplaceEnv func(env []string)
}

Process is what main knows about the process before anything else runs.

func CurrentProcess

func CurrentProcess() Process

CurrentProcess returns what Harden needs, without reading anything but the command line, the environment and the ids.

Jump to

Keyboard shortcuts

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