Skip to content

Latest commit

 

History

History
422 lines (323 loc) · 17.5 KB

FACTORY.md

File metadata and controls

422 lines (323 loc) · 17.5 KB

FACTORY.md — Build & Infrastructure Runbook

Operating manual for an agent (or human) bringing sloth up from a cold clone. Charter and ethics live in MISSION.md; pattern in dark-factory.md; working rules in AGENTS.md; concept depth in docs/wiki/; per-view detail in docs/views/.

This file answers one question: what do I need to install, run, and ship the binary?


0. TL;DR

# Linux (Debian/Ubuntu)
sudo apt-get install -y build-essential libpcap-dev libncursesw5-dev
git clone https://github.com/SpaceTrucker2196/sloth.git
cd sloth
make test    # green ⇒ env is sound (no root, no terminal, no network)
make         # builds ./sloth-wifi and ./sloth-rf; ./sloth is a symlink to sloth-wifi
sudo ./sloth # needs CAP_NET_RAW + CAP_NET_ADMIN for pcap + nl80211

If make test passes and make produces no warnings, the factory is operational.


1. Supported platforms

Platform Build Run Notes
Linux yes (primary) yes (full) rtnetlink, nl80211, INET_DIAG, /proc, libpcap
macOS / Darwin yes (build & test) partial BSD platform stub; for development only — fake platform drives all tests
*BSD yes (build only) stub src/platform/bsd.c is minimal
Windows not maintained n/a src/platform/win32.c exists but is a stub
Other yes (stub build) n/a src/platform/stub.c returns empty data

The production target is Linux. Other platforms exist so the test binary can build anywhere; they are not feature-complete.


2. Toolchain

Tool Minimum Why
C compiler C99 (gcc ≥ 4.6 or clang ≥ 3.3) code is -std=c99 -D_DEFAULT_SOURCE
make GNU make Makefile uses GNU conditional syntax
pkg-config optional not used by the Makefile; CMake path uses it
git any modern clone + commit
pthread, libm system linked unconditionally

No autotools, no meson, no bazel. A CMakeLists.txt exists at root but is a stale alternate build path that hardcodes the old ntop name — use the Makefile.


3. Runtime dependencies

For the full build (default):

  • libpcap — packet capture (capture thread, BPF filter, pcap export).
  • libncurses (Linux: libncursesw, macOS: unified libncurses) — TUI.

For the test build: none beyond pthread and libm. Tests run headless and offline.

For the embedded build (make embedded): none beyond pthread and libm. No capture, no TUI; useful for headless data collectors.

Install per distro

# Debian / Ubuntu
sudo apt-get install -y build-essential libpcap-dev libncursesw5-dev

# Fedora / RHEL
sudo dnf install -y gcc make libpcap-devel ncurses-devel

# Alpine
apk add build-base libpcap-dev ncurses-dev

# Arch
sudo pacman -S --needed base-devel libpcap ncurses

# macOS (development only — runs against fake platform)
xcode-select --install
brew install libpcap ncurses

4. Build matrix

All targets driven by the root Makefile.

Command What you get
make full build → ./sloth-wifi and ./sloth-rf, with ./sloth a symlink to sloth-wifi (ncurses + pcap + nl80211)
make WITH_PCAP=0 no capture; the packets/probe views render a disabled message
make WITH_NCURSES=0 headless; TPRINT falls back to printf
make WITH_WIFI=0 drops nl80211 source on Linux
make embedded shortcut: WITH_NCURSES=0 WITH_PCAP=0
make test builds ./sloth_test, runs all assertions
make test-capture-path builds ./sloth_test_pcap (WITH_PCAP), drives the real pcap_dispatch → on_packet path
make cppcheck CI's cppcheck command verbatim, with CI's version guard. Green here means the cppcheck job is green. Hard-fails unless cppcheck --version is exactly Cppcheck 2.13.0; no distro ships it, so build from source per the recipe above the target
make clean removes objects, sloth-wifi, sloth-rf, the sloth symlink, sloth_test
make install PREFIX=... installs $PREFIX/bin/sloth-wifi and sloth-rf, with bin/sloth a symlink (default /usr/local)

Override knobs (env or make CC=clang ...):

  • CC — compiler (default cc)
  • CFLAGS — default -O2 -Wall -Wextra -std=c99 -D_DEFAULT_SOURCE
  • LDFLAGS — extra linker flags
  • PREFIX — install root (default /usr/local)

Hard rules: warning-clean builds, green tests, VIEW_COUNT sync (see AGENTS.md — Discipline).


5. Test discipline

  • Assertion count: the last line of make test. Not repeated here — a number copied into prose is a number that goes stale (see AGENTS.md — Architecture).
  • No root required. No terminal required. No network required.
  • All kernel-facing code is replaced by tests/fake_platform.c (see [[platform-vtable]] in the wiki).
  • TUI render functions execute their full code path against tests/null_tui.c (ncurses no-ops; TPRINT becomes printf).
  • Protocol parsers (DNS, TLS, JA3, QUIC, HTTP, NTP, ICMP, mDNS, NBNS, DHCP, SSDP) are tested with hand-crafted byte arrays built from the relevant RFCs. Parsers never feed themselves their own output.
  • src/md5.c is validated against every RFC 1321 test vector.

Adding a feature without adding a test is a regression in the factory's review capability. Do not skip.


6. Running sloth

6.1 Capabilities

Need Capability Effect if missing
libpcap capture CAP_NET_RAW no packets, no L7 logs
nl80211 scan / stations CAP_NET_ADMIN WiFi view empty
/proc/<pid>/fd/* of other users root (or same uid) sockets show no PID/process
Monitor-mode capture CAP_NET_ADMIN on the iface no probes, no beacons, no EAPOL

Easiest path during development: sudo ./sloth. For unattended deployment, prefer file-grant caps:

sudo setcap cap_net_raw,cap_net_admin=eip ./sloth-wifi   # the file, not the ./sloth symlink
./sloth   # runs unprivileged

6.2 Invocation forms

./sloth                                  # TUI on default iface
./sloth -i eth0                          # pin capture iface
./sloth -o /var/log/sloth.jsonl          # JSONL forensic stream
./sloth --pcap-dir /var/sloth/pcap       # per-alert pcap dumps
./sloth --eapol-dir /var/sloth/eapol     # PMKID + handshake export (hashcat 22000)
./sloth --data-socket unix:/var/run/sloth.sock   # local read-only JSONL stream
./sloth --data-socket tcp:100.64.0.5:8765 --data-socket-allow-remote
                                         # JSONL over TCP. A non-loopback bind is
                                         # refused without the opt-in flag (#86);
                                         # the stream has no auth and no TLS.

Flags compose. The JSONL schema is documented in README.md and docs/wiki/jsonl-schema.md; a Python reference consumer that exercises every record type lives at examples/consumer/.

6.3 WiFi SIGINT prep (out-of-band)

Sloth never touches link state. Monitor mode must be set up externally before sloth starts:

sudo ip link set wlan1 down
sudo iw dev wlan1 set type monitor
sudo ip link set wlan1 up
sudo ./sloth --eapol-dir /tmp/sloth-eapol

Tested chipset: rtl88XXau. Any card whose driver supports ARPHRD_IEEE80211_RADIOTAP should work.

6.4 Minimum terminal

100×33 for the [[dashboard]] composite view. Below that, the tiling collapses and panels become unreadable.


7. Deployment

Sloth is two daemons (sloth-wifi and the headless sloth-rf, ADR 0010), no config file, no daemon manager required.

  • sudo make install drops /usr/local/bin/sloth-wifi and sloth-rf, with /usr/local/bin/sloth a symlink to the first.
  • For systemd, write a unit that runs sloth -o ... --pcap-dir ... --eapol-dir ... with AmbientCapabilities=CAP_NET_RAW CAP_NET_ADMIN. No template shipped — keep deployment under operator control.
  • The binary has no telemetry, no phone-home, no auto-update. Anything that talks back to a remote violates MISSION.md §2.

8. CI

.github/workflows/ci.yml runs on every push and PR:

runs-on: ubuntu-latest
steps:
  - actions/checkout@<40-hex-digest>   # v4 — pinned by digest; see
                                       # tests/test_ci_pins.c
  - apt-get install libncurses-dev libpcap-dev
  - make
  - make test

That's the gate. Both steps must be green for a PR to be considered mergeable by the factory's review loop.


9. Code layout (where to put things)

Path Contains
include/sloth.h shared structs, view_t enum, VIEW_COUNT, platform_ops_t
src/main.c CLI, signal handlers, main poll loop
src/tui.c ncurses rendering, key polling, ANSI fallback
src/platform/linux*.c rtnetlink, nl80211, /proc readers, INET_DIAG
src/platform/{bsd,win32,stub}.c non-Linux backends
src/capture/capture.c libpcap thread, per-protocol parser dispatch
src/capture/probe.c 802.11 probe / beacon capture
src/{dns,tls,quic,http,ntp,icmp,dns}_log.c ring buffers + snapshot helpers
src/{alerts,beacon_detect,devices,threat_intel,filter,jsonl,alert_pcap,top_hosts,ip_color,ip_owner,host_cache,probe_pnl,eapol_log,seqnum_track,assoc_track}.c synthesis + export
src/views/<name>.c one file per VIEW_* (sloth-wifi's)
src/md5.c embedded MD5 for JA3 (RFC 1321 verified)
tests/ unit tests, fake platform, scenarios
docs/views/<name>.md per-view protocol/observation reference
docs/wiki/<name>.md concept-oriented knowledge base

Hard rules: no new root files, stage by specific path, never stage wifi-sigint/ or the binaries, no coloured row backgrounds (see AGENTS.md — Hard "don't"s).


10. Adding work (agent-facing recipes)

10.1 Add a view

See AGENTS.md "How to add a new view" — 11-step checklist that keeps VIEW_COUNT synced, lays out the file, wires a keybind, and demands a test + a per-view doc.

10.2 Add an alert rule

See AGENTS.md "How to add an alert rule" — 6 steps, ending in a find_alert(type) >= 0 assertion. Concept page: docs/wiki/alerts.md.

10.3 Risk gate (converge loop step 6)

The converge loop consults agents/risk_score.sh before shipping: a zero-deps POSIX-sh scorer that rates the pending diff (working tree vs origin/main) for blast radius. The green suite answers "is it correct"; the risk score answers "how much should a human look first".

Signal Weight
Touches agents/, .github/, .githooks/ +40
External-contract narrowing: ≥10 deleted lines in src/jsonl.c / src/main.c, or any deletion in src/jsonl.h +30
External-contract widening: docs/wiki/jsonl-schema.md touched with no such source deletions — a documented additive change +10
Forensic-output path: src/jsonl.c, src/alert_pcap.c, src/pcap_write.c, src/eapol_log.c +20
New files +5 each, cap +20
Deleted files +10 each, cap +30
Churn > 300 lines (> 800) +15 (+30)
docs/ + tests/ + *.md only (agent surface always hot) forces 0

Threshold: RISK_THRESHOLD env, default 50. Modes:

  • Advisory (default): score is reported in the order's issue comment; the loop proceeds regardless.
  • Enforcing (marker file agents/risk_enforce exists): at/above threshold the loop stops without shipping and surfaces the breakdown on the issue. Creating the marker is an owner decision — weights and threshold must be owner-reviewed first (issue #45 acceptance).

Verification: sh agents/risk_score.sh --selftest (canned name-status / numstat fixtures — same no-circular-tests rule as the parsers).

Calibration note (changed 2026-09-30). The contract proxy used to score any touch of docs/wiki/jsonl-schema.md, and any deletion in src/jsonl.[ch] / src/main.c, at +30. Two consequences, both measured against real history:

  • Adding a JSONL field the documented way means touching jsonl.c (+20 forensic path) and the schema doc (+30) — so following the repo's own discipline summed to exactly the threshold. Four of fourteen real code commits scored ≥50, all of them routine additive observability work.
  • A genuine break (a field removed from the emitter and struck from the schema) also scored 50. The gate returned the same answer for "documented a new field" and "deleted a field", which is no answer.

§4.3 already draws the line in the right place — it names the stop-and-ask as changing the schema in a non-additive way — so the weights now implement that wording. Additive-and-documented scores +10; narrowing keeps +30. The source-deletion proxies also gained a 10-line floor, because main.c is a thousand lines of poll loop rather than a flag table and two deleted lines there are not evidence a CLI flag went away. jsonl.h is exempt: it is small enough that one deleted line really can be a removed field.

Re-scored against the same fourteen commits: 0 now reach the threshold, and the synthetic break still scores 50. Enforcing mode is therefore a live option in a way it was not before — turning the marker on under the old weights would have halted a quarter of routine work.

Scoring note (changed 2026-07-28). agents/ was git-crypt encrypted until then, so instruction files appeared as binary in diffs: their numstat lines were -, churn from them counted as zero, and only the +40 path signal fired. Now that they are plaintext, churn counts too — so an agents/ edit scores 40 plus its churn tier and can cross the 50 threshold on its own. That is the correct behaviour (a large rewrite of the rules governing agents is riskier than a one-line fix) but it means historical scores for instruction-file commits are not comparable with new ones.

10.4 Add a platform op

  1. Add the function pointer to platform_ops_t in include/sloth.h.
  2. Implement it in every backend: linux*.c, bsd.c, win32.c, stub.c.
  3. Implement it in tests/fake_platform.c with controllable in-memory data.
  4. Write the test that drives the new fake.
  5. Views read from sloth_state_t. Views must never call platform ops directly.

11. Common failures and fixes

Symptom Cause Fix
fatal error: pcap.h: No such file missing libpcap headers apt-get install libpcap-dev (or make WITH_PCAP=0)
cannot find -lncursesw (Linux) missing wide-char ncurses apt-get install libncursesw5-dev
cannot find -lncurses (macOS) shouldn't happen — macOS ships it; check Xcode CLT xcode-select --install
Empty connections view no CAP_NET_RAW or wrong uid sudo or setcap
No process names on sockets sockets owned by other uids run as root
Empty WiFi view on Linux missing CAP_NET_ADMIN or no wifi iface check iw dev; run with caps
No probes / beacons / EAPOL iface not in monitor mode see §6.3
make test fails on a fresh clone toolchain too old, or local mod make clean && make test; verify gcc --version is ≥ 4.6
Build warnings regression in the factory's quality gate fix before commit — warnings are not negotiable
VIEW_COUNT mismatch test failure added a view without bumping the constant update include/sloth.h, tests/test_state.c, tests/test_arp.c

12. Git & release workflow

Branching, commit format, push cadence, and destructive-op rules: (see AGENTS.md — Conventions, Hard "don't"s).

CI gates merge: make + make test. If either fails, the commit doesn't go in.

Version bumps. A change to SLOTH_VERSION in include/sloth.h updates SECURITY.md in the same commit: the supported-version line there is checked strictly against the header by tests/test_docs_consistency.c since 8931549 (#96), so a bump without it turns the suite red. Tag signing stays a manual owner step (git tag -s, #142); CI holds no key.


13. Cold-start sanity loop

To verify an agent's environment is wired correctly from scratch:

# 1. Source of truth
git clone https://github.com/SpaceTrucker2196/sloth.git
cd sloth

# 2. Build, then test
make test      # must exit 0 with all assertions green
make           # must exit 0 with zero warnings

# 3. Smoke run (Linux, with caps)
sudo ./sloth   # press [?] for help, [o] for dashboard, [q] to quit

If steps 2–3 succeed, the factory is fully operational and the agent can start taking work from MISSION.md §6 ("Direction").


14. Where to read next

If you want… Read
The non-negotiable charter MISSION.md
The dark-factory pattern itself dark-factory.md
Working rules for the repo AGENTS.md
Concept-level wiki docs/wiki/index.md
Per-view protocol reference docs/views/README.md
User-facing release notes README.md