ipfence home page local-first intrusion prevention for Linux

Manual pages

Storage: the database

Package internal/store. One SQLite file, /var/lib/ipfence/ipfence.db, readable only by root. It is ipfence's memory and the intended state of the firewall.

Opening safely

In order:

  1. If the path exists it must be a regular file (no symlink), owned by the current user, closed to others.
  2. A non-empty file must begin with SQLite's signature.
  3. Its application_id must be ipfence's (0x49504643, "IPFC"); a database with tables but another ID is refused.
  4. PRAGMA quick_check must pass.
  5. Its schema version must not be newer than the program's.

Any failure leaves the file exactly as found and stops with a message. Only then is the database set to write-ahead logging with full synchronization, and migrations applied in one transaction.

A missing database is created with mode 0600.

Settings used

Setting Why
journal_mode=WAL readers and the writer do not block each other
synchronous=FULL committed data survives power loss
busy_timeout=5000 wait briefly for a lock
immediate transactions take the write lock at the start
one connection all access is serialized anyway

Tables

Times are milliseconds since 1970 (UTC); 0 means "none".

meta

key value
install_id 32 random hex characters; marks firewall ownership
enforcement on or off

schema_version

version, applied_at: one row per applied migration.

bans

Column Meaning
id increasing number
prefix 203.0.113.50/32, 198.51.100.0/24
is_host 1 for a single address
origin auto, manual, escalation
service, detector which detector, empty for manual
reason short text with counts
offense repeat number
observed 1 if decided while enforcement was off
created_at, expires_at expires_at 0 = permanent
ended_at 0 while active
end_reason expired, unbanned, allowlisted, replaced
enforced, enforce_error result of the last firewall write

A partial unique index allows only one active ban per prefix.

Real rows from the walkthrough:

Plain text

(1, '203.0.113.50/32', 'auto', 'ssh', '5 SSH authentication failures ...', 1, 'replaced')
(2, '203.0.113.50/32', 'auto', 'ssh', '5 SSH authentication failures ...', 1, 'unbanned')
(3, '203.0.113.50/32', 'auto', 'ssh', '5 SSH authentication failures ...', 2, 'allowlisted')
(4, '198.51.100.0/24', 'manual', '', 'manual: abusive network', 0, '')

Row 1 was the dry-run decision (offense shown as 1 but not counted); row 2 the first real ban; row 3 the second offense.

offenses

prefix, count, last_end. last_end answers "up to when have events been dealt with?"

hits

Recent events counting toward thresholds: prefix, detector, event_key, ts, weight, high_confidence. (detector, event_key) is unique, which is the duplicate guard. Cleared when the address is banned.

allow

prefix, note, created_at: entries from ipfence allow. Settings-file entries are not stored here.

cursors

source (journal:ssh, file:/var/log/nginx/access.log), cursor (journal), dev, inode, offset, head, head_len (files), updated_at.

counters

name, value. See Counters.

Transactions

Go

err := db.Update(ctx, func(tx *store.Tx) error { ... }) // commit if nil
err := db.View(ctx, func(tx *store.Tx) error { ... })   // never commits

If any statement inside Update failed, even one whose error was ignored, nothing is committed.

Growth limits

Data Limit
active bans 20,000
ended bans 90 days, 10,000 rows
hits 24 hours, 50,000 rows
offense records forget_after, 100,000 rows

Looking inside

Prefer ipfence status --json. For debugging, read-only:

Shell commands

sudo python3 - <<'EOF'
import sqlite3
db = sqlite3.connect("file:/var/lib/ipfence/ipfence.db?mode=ro", uri=True)
for row in db.execute("select id, prefix, origin, offense, end_reason from bans order by id desc limit 20"):
    print(row)
EOF

Do not edit the database while the daemon runs. Use the commands.

Changing the schema

Append a new migration string to migrations in store.go. Never edit a released one. Older programs refuse newer databases.

Tests

store_test.go: identity across reopen, read-only mode, refusal and preservation of foreign, corrupt and newer files, symlinks, ban lifecycle and uniqueness, hit scoring and pruning, atomic rollback, and round trips of every record type.