// Package config loads stewardd's configuration file. It only says how // the daemon runs (where it listens, its files, logging); the DHCP // configuration itself is in the database. package config import ( "bytes" "fmt" "net/netip" "os" "path/filepath" "strings" "time" "go.yaml.in/yaml/v3" ) // DefaultPath is where stewardd looks for its configuration. const DefaultPath = "/etc/steward/steward.yaml" // Config is the daemon configuration. type Config struct { // Listen are the web UI's and API's addresses. Listen []string `yaml:"listen"` TLS TLS `yaml:"tls"` TrustedProxies []string `yaml:"trusted_proxies"` // Socket is a local unix socket for the API, where root and // stewardd's own user need no token ("" for none). Socket string `yaml:"socket"` Database string `yaml:"database"` Log Log `yaml:"log"` Session Session `yaml:"session"` Backup Backup `yaml:"backup"` } // TLS is a certificate for HTTPS. type TLS struct { CertFile string `yaml:"cert_file"` KeyFile string `yaml:"key_file"` } // Log configures logging to stderr (the journal under systemd). type Log struct { Level string `yaml:"level"` // debug, info, warn, error Format string `yaml:"format"` // text or json } // Session configures web UI logins. type Session struct { Lifetime Duration `yaml:"lifetime"` IdleTimeout Duration `yaml:"idle_timeout"` // CookieSecure is auto (secure over HTTPS), always or never. CookieSecure string `yaml:"cookie_secure"` } // Backup configures 'stewardd backup'. type Backup struct { Dir string `yaml:"dir"` Retention Duration `yaml:"retention"` // KeepAtLeast backups are kept however old they are. KeepAtLeast int `yaml:"keep_at_least"` } // Duration is a time.Duration written as "15s" or "12h"; days ("30d") // work too. type Duration time.Duration // UnmarshalYAML implements yaml.Unmarshaler. func (d *Duration) UnmarshalYAML(n *yaml.Node) error { var s string if err := n.Decode(&s); err != nil { return err } v, err := ParseDuration(s) if err != nil { return err } *d = Duration(v) return nil } // ParseDuration parses a Go duration, also accepting days ("30d"). func ParseDuration(s string) (time.Duration, error) { s = strings.TrimSpace(s) if n, ok := strings.CutSuffix(s, "d"); ok { var days int if _, err := fmt.Sscanf(n, "%d", &days); err != nil || fmt.Sprint(days) != n { return 0, fmt.Errorf("invalid duration %q", s) } return time.Duration(days) * 24 * time.Hour, nil } return time.ParseDuration(s) } // Default returns the defaults for Debian/Ubuntu. func Default() *Config { return &Config{ Listen: []string{"127.0.0.1:8067"}, TrustedProxies: []string{}, Socket: "/run/steward/steward.sock", Database: "/var/lib/steward/steward.db", Log: Log{Level: "info", Format: "text"}, Session: Session{Lifetime: Duration(12 * time.Hour), IdleTimeout: Duration(2 * time.Hour), CookieSecure: "auto"}, Backup: Backup{Dir: "/var/backups/steward", Retention: Duration(30 * 24 * time.Hour), KeepAtLeast: 7}, } } // Load reads the file at path over the defaults. A missing file is fine // when allowMissing is set. func Load(path string, allowMissing bool) (*Config, error) { c := Default() b, err := os.ReadFile(path) if err != nil { if os.IsNotExist(err) && allowMissing { return c, c.check() } return nil, err } dec := yaml.NewDecoder(bytes.NewReader(b)) dec.KnownFields(true) if err := dec.Decode(c); err != nil && err.Error() != "EOF" { return nil, fmt.Errorf("%s: %w", path, err) } if err := c.check(); err != nil { return nil, fmt.Errorf("%s: %w", path, err) } return c, nil } func (c *Config) check() error { if len(c.Listen) == 0 { return fmt.Errorf("listen: at least one address is needed") } if c.Socket != "" && !filepath.IsAbs(c.Socket) { return fmt.Errorf("socket must be an absolute path (or \"\" for none)") } if len(c.Socket) > 107 { return fmt.Errorf("socket: the path is %d bytes long; unix sockets allow at most 107", len(c.Socket)) } if (c.TLS.CertFile == "") != (c.TLS.KeyFile == "") { return fmt.Errorf("tls: set both cert_file and key_file") } for _, p := range c.TrustedProxies { if _, err := ParsePrefix(p); err != nil { return fmt.Errorf("trusted_proxies: %v", err) } } if c.Database == "" || !filepath.IsAbs(c.Database) { return fmt.Errorf("database must be an absolute path") } if c.Backup.Dir != "" && !filepath.IsAbs(c.Backup.Dir) { return fmt.Errorf("backup.dir must be an absolute path") } switch c.Session.CookieSecure { case "auto", "always", "never": default: return fmt.Errorf("session.cookie_secure must be auto, always or never") } switch c.Log.Format { case "text", "json": default: return fmt.Errorf("log.format must be text or json") } switch c.Log.Level { case "debug", "info", "warn", "error": default: return fmt.Errorf("log.level must be debug, info, warn or error") } return nil } // ParsePrefix parses an address or a prefix. func ParsePrefix(s string) (netip.Prefix, error) { if strings.Contains(s, "/") { return netip.ParsePrefix(s) } a, err := netip.ParseAddr(s) if err != nil { return netip.Prefix{}, err } return netip.PrefixFrom(a, a.BitLen()), nil } // Proxies returns the trusted proxies. func (c *Config) Proxies() []netip.Prefix { var out []netip.Prefix for _, p := range c.TrustedProxies { if pf, err := ParsePrefix(p); err == nil { out = append(out, pf.Masked()) } } return out } // Example is a commented example configuration with the defaults. const Example = `# Steward daemon configuration (/etc/steward/steward.yaml). # Every setting is optional; the values shown are the defaults. The DHCP # configuration (subnets, pools, reservations, options) is not here: it is # in the database, changed with the steward CLI or the web UI. # Addresses the web UI and API listen on. Keep this off the internet: # bind to localhost or a management network, and use TLS (below) or a # TLS-terminating reverse proxy. listen: - "127.0.0.1:8067" # HTTPS from a certificate you renew yourself (reread when it changes). # tls: # cert_file: /etc/steward/tls/fullchain.pem # key_file: /etc/steward/tls/privkey.pem # Reverse proxies whose X-Forwarded-For and X-Forwarded-Proto are # believed. trusted_proxies: [] # A local socket for the API: root and stewardd's own user use it without # a token ('sudo steward status' just works). "" turns it off. socket: /run/steward/steward.sock database: /var/lib/steward/steward.db log: level: info # debug, info, warn or error format: text # text or json session: lifetime: 12h idle_timeout: 2h cookie_secure: auto # auto (secure over HTTPS), always or never # 'stewardd backup' (the daily steward-backup.timer) writes copies of the # database here and deletes those older than retention, keeping at least # keep_at_least. backup: dir: /var/backups/steward retention: 30d keep_at_least: 7 `