mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
[general] user (and group): started as root, bbs serve reads the configuration, binds every socket it listens on (the listeners, TCP and unix; the HTTP API and metrics, FTP, NNTP, IRC, finger, gopher, binkp), gives up root (setgroups to the user's groups, setgid, setuid) and executes itself again, the same process (its PID, for systemd and reload), the sockets handed down; each server's listen takes the one for its address (internal/sockets). Nothing is written as root: database, logs, host keys, backups and a unix socket without socket_group are the user's. Other commands as root give root up first, so the database stays the user's (but check and config; backup restore puts things back as root and gives them, but the configuration, to the user). System configuration has Run as user and group. TestRunAs (as root) serves a port under 1024 and a unix socket as nobody, the same process through a reload. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
102 lines
2.3 KiB
Go
102 lines
2.3 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
|
|
"github.com/Neo-Desktop/bbs-sec/internal/sockets"
|
|
)
|
|
|
|
// Bind binds every socket the configuration listens on (as root, before
|
|
// the BBS becomes [general]'s user): the listeners, TCP and unix, and the
|
|
// servers' ports. They are handed down, each server taking its own.
|
|
func Bind(cfg *Config) ([]sockets.Socket, error) {
|
|
var out []sockets.Socket
|
|
fail := func(err error) ([]sockets.Socket, error) {
|
|
for _, s := range out {
|
|
s.File.Close()
|
|
}
|
|
return nil, err
|
|
}
|
|
tcp := func(addr string) error {
|
|
if addr == "" {
|
|
return nil
|
|
}
|
|
for _, s := range out {
|
|
if s.Network == "tcp" && s.Address == addr {
|
|
return nil
|
|
}
|
|
}
|
|
ln, err := net.Listen("tcp", addr)
|
|
if err != nil {
|
|
return fmt.Errorf("%s: %w", addr, err)
|
|
}
|
|
defer ln.Close()
|
|
f, err := ln.(*net.TCPListener).File()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
out = append(out, sockets.Socket{Network: "tcp", Address: addr, File: f})
|
|
return nil
|
|
}
|
|
for _, l := range cfg.Listeners {
|
|
if !l.Unix() {
|
|
if err := tcp(l.Address); err != nil {
|
|
return fail(fmt.Errorf("[listen %s] %w", l.Name, err))
|
|
}
|
|
continue
|
|
}
|
|
u, err := ListenUnix(l)
|
|
if err != nil {
|
|
return fail(fmt.Errorf("[listen %s] %w", l.Name, err))
|
|
}
|
|
if a := cfg.RunAs; a != nil {
|
|
// the user's, as it would be had the BBS made it (its group
|
|
// socket_group's, if said)
|
|
gid := a.GID
|
|
if l.SocketGroup != "" {
|
|
gid = -1
|
|
}
|
|
if err := os.Lchown(l.path(), a.UID, gid); err != nil {
|
|
u.Close()
|
|
return fail(err)
|
|
}
|
|
}
|
|
f, err := u.UnixListener.File()
|
|
u.UnixListener.Close() // not u.Close: the socket stays where it is
|
|
if err != nil {
|
|
return fail(err)
|
|
}
|
|
out = append(out, sockets.Socket{Network: "unix", Address: l.path(), File: f})
|
|
}
|
|
var addrs []string
|
|
if h := cfg.HTTP; h != nil {
|
|
addrs = append(addrs, h.Address, h.Metrics)
|
|
}
|
|
if cfg.FTP != nil {
|
|
addrs = append(addrs, cfg.FTP.Address)
|
|
}
|
|
if cfg.NNTP != nil {
|
|
addrs = append(addrs, cfg.NNTP.Address)
|
|
}
|
|
if i := cfg.IRC; i != nil {
|
|
addrs = append(addrs, i.Address, i.TLSAddress)
|
|
}
|
|
if cfg.Finger != nil {
|
|
addrs = append(addrs, cfg.Finger.Address)
|
|
}
|
|
if cfg.Gopher != nil {
|
|
addrs = append(addrs, cfg.Gopher.Address)
|
|
}
|
|
if cfg.Binkp != nil {
|
|
addrs = append(addrs, cfg.Binkp.Address)
|
|
}
|
|
for _, a := range addrs {
|
|
if err := tcp(a); err != nil {
|
|
return fail(err)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|