Daemon, commands and reports
Packages daemon, control, cli, status and hook.
The single writer
Only one process may change the database and firewall at a time.
Diagram source
flowchart TD
CMD["ipfence command"] --> S{"daemon socket answers?"}
S -- yes --> D["daemon executes it"]
S -- no --> L{"writer lock free?"}
L -- yes --> O["command takes the lock and executes it itself"]
L -- no --> B["exit 4: another writer is busy"]
P["no permission"] --> X["exit 3"]- The lock is
/run/ipfence/writer.lock, held withflock. The kernel releases it if the holder dies. - Offline commands run the same
control.Executecode as the daemon. statuswithout a daemon opens the database read-only, takes no lock, and runs discovery.
Daemon lifecycle (daemon.Run)
Open: create or verify/run/ipfenceand/var/lib/ipfence(mode 0700), lock, open the database, build the firewall and engine.- Log configuration warnings.
- Listen on the control socket.
Maintain(restores bans after a reboot).discover, starting ready readers.- Start the socket server and hook worker.
- Log
readyand notify systemd (READY=1). - Every 15 s
Maintain; every 60 sdiscover. Maintenance errors are logged when they change, not every time. - On SIGINT or SIGTERM: notify
STOPPING=1, stop readers (at most 10 s), close the socket and database, release the lock. The firewall is not touched.
SIGHUP is ignored: there is no live reload.
The control protocol (control)
One connection carries one request line and one response, both JSON.
Request:
JSON
{"op": "ban", "prefix": "198.51.100.0/24", "duration_seconds": 3600, "reason": "noise"}op |
Fields |
|---|---|
status |
none |
ban |
prefix, and exactly one of duration_seconds or permanent; optional reason |
unban |
prefix, optional reset_history |
allow |
prefix, optional note |
unallow |
prefix |
enable, disable |
none |
Response: ok, code, error, result, status. Codes: bad_request,
refused, not_found, unsafe_firewall, permission, internal.
| Limit | Value |
|---|---|
| request | 16 KiB, one object, unknown fields rejected |
| time to send the request | 5 s |
| time to execute | 60 s (finishes even if the daemon is stopping) |
| concurrent clients | 8 |
| response | 32 MiB |
| ban duration | up to 10 years |
Security: the socket is mode 0600 in a 0700 directory, and the server asks the kernel who connected, accepting only root or its own user.
The command line (cli)
| File | Contents |
|---|---|
cli.go |
argument parsing, help, exit codes |
commands.go |
daemon, enable, disable, ban, unban, allow, status |
init.go |
discovery report and first configuration |
test.go |
the dry-run parser |
render.go |
human-readable output |
Options may come before or after arguments. Text printed from the machine or from your notes has control characters removed, so output cannot drive your terminal. No color is used.
ban has one extra guard here: it looks for your SSH client address in the
environment and in parent processes (because sudo removes it) and refuses a range
containing it without --force.
init trusts an SSH client address only if that connection is open right now.
Building the status report
daemon.BuildReport combines the engine snapshot, the discovery report and, for a
running daemon, each reader's health. status.Running turns reader health into the
detector state, and upgrades an unknown parser to yes once a line has parsed.
SchemaVersion (1) changes only if fields change meaning or disappear.
The hook
hook.New returns nothing unless a command is configured. Notifications go into a
queue of 16 and run one at a time: the executable is checked (not replaceable by
others), started without a shell, with only PATH set, working directory /, the
selected fields as one JSON line on standard input, output discarded, and the whole
process group killed at the timeout.
Example input:
JSON
{"address":"203.0.113.50","event":"ban","expires":"2026-11-29T02:57:53Z","reason":"5 SSH authentication failures within 10m (latest: failed-password)","service":"ssh"}Tests
| File | Proves |
|---|---|
daemon_linux_test.go |
lifecycle, second writer excluded, stop keeps bans, restart restores and reads missed lines, unsafe detector stopped, unsafe state refused |
control_test.go, peer_linux_test.go |
validation, error codes, real socket traffic, garbage and oversize, stale socket handling, other users refused |
cli_test.go |
help and usage errors, test has no side effects, init behaviour, status, the ban lifecycle, self-ban guard, exit codes |
status_test.go |
detector states |
hook_test.go, hook_linux_test.go |
disabled by default, selected fields, bounded queue, no shell, clean environment, timeout, refusing replaceable programs |