diff --git a/hosts/hl-fwdesk.nix b/hosts/hl-fwdesk.nix index 9a683a1..2dcfdd2 100644 --- a/hosts/hl-fwdesk.nix +++ b/hosts/hl-fwdesk.nix @@ -193,12 +193,13 @@ in enable = true; host = "0.0.0.0"; port = ports.ollama; - package = pkgs.ollama-vulkan; + package = pkgs.ollama-rocm; user = "ollama"; home = "/var/lib/ollama"; environmentVariables = { HSA_ENABLE_SDMA = "0"; OLLAMA_FLASH_ATTENTION = "1"; + OLLAMA_KV_CACHE_TYPE = "q8_0"; OLLAMA_KEEP_ALIVE = "24h"; }; }; diff --git a/modules/home/ABOUTME.md b/modules/home/ABOUTME.md index 2d9c31c..8063bbc 100644 --- a/modules/home/ABOUTME.md +++ b/modules/home/ABOUTME.md @@ -20,7 +20,7 @@ home/ `default.nix` exports two builder functions, both consumed by `core/user.nix`: -- `makeRootUser { hostName }` — minimal root profile (jq, kubectl, btop, …) with a +- `makeRootUser { hostName }` — minimal root profile (jq, kubectl, …) with a `root@` git identity. - `makeCommonUser { name, fullName, email, extraGitConfigs ? [] }` — the full human profile: dev packages, the shared shell/program config, and the user's git identity. diff --git a/modules/home/default.nix b/modules/home/default.nix index 8eadb9a..d0bf72e 100644 --- a/modules/home/default.nix +++ b/modules/home/default.nix @@ -9,8 +9,6 @@ let shellAliases = { cat = "bat"; ls = "eza"; - du = "dust"; - df = "duf"; open = "xdg-open"; }; }; @@ -26,8 +24,6 @@ in kubectl kubectx htop - btop - duf ]; }; programs = @@ -65,13 +61,10 @@ in ipfetch nixd htop - btop fastfetch kubectl kubectx terraform - dust - duf ]; }; programs = diff --git a/modules/openwrt/ABOUTME.md b/modules/openwrt/ABOUTME.md index ddd950c..7657f65 100644 --- a/modules/openwrt/ABOUTME.md +++ b/modules/openwrt/ABOUTME.md @@ -18,8 +18,10 @@ openwrt/ 1. The router config lives as an **agenix secret** (e.g. `secrets/glmain.json.age`). 2. `openwrt-deploy` reads the decrypted JSON from stdin, runs `openwrt-gen dnsmasq`, `openwrt-gen ethers`, and `openwrt-gen firewall` to render the three target files. -3. It SSHes to the router, shows a **colorized diff** against the current files (for the - firewall, only the `skyg_*`-managed rules are diffed), and prompts for confirmation. +3. It SSHes to the router, shows a **colorized unified diff** against the current files + (for the firewall, only the `skyg_*`-managed rules are diffed), each with a + `-> N added, M removed` summary, then lists the `skyg_*` rule names that will be + applied, and prompts for confirmation. 4. On the router it **backs up** the configs it overrides to `/etc/skyg-backups/` with a timestamp, then applies them. `dnsmasq.conf` is validated with `dnsmasq --test` and the firewall is committed + reloaded; **any failure auto-restores** from that timestamp's diff --git a/modules/openwrt/default.nix b/modules/openwrt/default.nix index 1064d4a..14ef85a 100644 --- a/modules/openwrt/default.nix +++ b/modules/openwrt/default.nix @@ -13,48 +13,45 @@ let # Run on the router as: TS= bash skyg-apply.sh # Expects the new files staged at /tmp/skyg_firewall.batch, # /tmp/dnsmasq.new.conf and /tmp/ethers.new. - applyScript = pkgs.writeText { - name = "skyg-apply.sh"; - text = '' - set -euo pipefail - BK=/etc/skyg-backups - mkdir -p "$BK" - cp /etc/config/firewall "$BK/firewall.$TS" - cp /etc/dnsmasq.conf "$BK/dnsmasq.conf.$TS" - cp /etc/ethers "$BK/ethers.$TS" 2>/dev/null || true + applyScript = pkgs.writeText "skyg-apply.sh" '' + set -euo pipefail + BK=/etc/skyg-backups + mkdir -p "$BK" + cp /etc/config/firewall "$BK/firewall.$TS" + cp /etc/dnsmasq.conf "$BK/dnsmasq.conf.$TS" + cp /etc/ethers "$BK/ethers.$TS" 2>/dev/null || true - # --- Firewall: replace the skyg-managed rules --- - for ref in $(uci show firewall | awk -F. '/\.name=.skyg_/{print $1"."$2}'); do - uci -q del "$ref" || true - done - if ! uci batch < /tmp/skyg_firewall.batch; then - echo 'FIREWALL: uci batch failed, restoring' - cp "$BK/firewall.$TS" /etc/config/firewall - exit 1 - fi - if ! uci commit firewall; then - echo 'FIREWALL: uci commit failed, restoring' - cp "$BK/firewall.$TS" /etc/config/firewall - exit 1 - fi - /etc/init.d/firewall reload + # --- Firewall: replace the skyg-managed rules --- + for ref in $(uci show firewall | awk -F. '/\.name=.skyg_/{print $1"."$2}'); do + uci -q del "$ref" || true + done + if ! uci batch < /tmp/skyg_firewall.batch; then + echo 'FIREWALL: uci batch failed, restoring' + cp "$BK/firewall.$TS" /etc/config/firewall + exit 1 + fi + if ! uci commit firewall; then + echo 'FIREWALL: uci commit failed, restoring' + cp "$BK/firewall.$TS" /etc/config/firewall + exit 1 + fi + /etc/init.d/firewall reload - # --- dnsmasq --- - cp /tmp/dnsmasq.new.conf /etc/dnsmasq.conf - if dnsmasq --test; then - /etc/init.d/dnsmasq restart - else - echo 'dnsmasq config invalid, restoring' - cp "$BK/dnsmasq.conf.$TS" /etc/dnsmasq.conf - exit 1 - fi + # --- dnsmasq --- + cp /tmp/dnsmasq.new.conf /etc/dnsmasq.conf + if dnsmasq --test; then + /etc/init.d/dnsmasq restart + else + echo 'dnsmasq config invalid, restoring' + cp "$BK/dnsmasq.conf.$TS" /etc/dnsmasq.conf + exit 1 + fi - # --- ethers --- - cp /tmp/ethers.new /etc/ethers + # --- ethers --- + cp /tmp/ethers.new /etc/ethers - echo 'Done' - ''; - }; + echo 'Done' + ''; in config: @@ -73,6 +70,35 @@ let ETHERS=$(echo "$CONFIG" | ${generator}/bin/openwrt-gen ethers) FIREWALL=$(echo "$CONFIG" | ${generator}/bin/openwrt-gen firewall) + # Colorized unified diff with a per-file change summary. + # $1 = label, $2 = current (may be empty), $3 = new + show_diff() { + local label="$1" old="$2" new="$3" + local oldf newf d + oldf=$(mktemp) newf=$(mktemp) + if [ -n "$old" ]; then printf '%s\n' "$old" > "$oldf"; else : > "$oldf"; fi + if [ -n "$new" ]; then printf '%s\n' "$new" > "$newf"; else : > "$newf"; fi + echo "" + echo "=== $label ===" + if d=$(diff -u "$oldf" "$newf"); then + echo " (no changes)" + else + printf '%s\n' "$d" | awk ' + /^---/ {printf "\033[2m%s\033[0m\n", $0; next} + /^\+\+\+/ {printf "\033[2m%s\033[0m\n", $0; next} + /^@@/ {printf "\033[36m%s\033[0m\n", $0; next} + /^-/ {printf "\033[31m%s\033[0m\n", $0; next} + /^\+/ {printf "\033[32m%s\033[0m\n", $0; next} + {print} + ' + local added removed + added=$(printf '%s\n' "$d" | grep -c '^[+][^+]' || true) + removed=$(printf '%s\n' "$d" | grep -c '^[-][^-]' || true) + echo " -> $added added, $removed removed" + fi + rm -f "$oldf" "$newf" + } + echo "Fetching current config from $SERVER..." CURRENT_DNSMASQ=$(ssh "$SERVER" 'cat /etc/dnsmasq.conf' 2>/dev/null || echo "") CURRENT_ETHERS=$(ssh "$SERVER" 'cat /etc/ethers' 2>/dev/null || echo "") @@ -82,23 +108,13 @@ let # Drop the header comment from the generated firewall for a clean diff NEW_FIREWALL=$(echo "$FIREWALL" | tail -n +2) - echo "" - echo "$DNSMASQ" - echo "=== dnsmasq.conf diff [START] ===" - comm -3 <(echo "$CURRENT_DNSMASQ" | sort) <(echo "$DNSMASQ" | sort) | awk '{if ($0 ~ /^/) print "\033[32m"$0"\033[0m"; else print}' - echo "=== dnsmasq.conf diff [END] ===" - echo "" + show_diff "dnsmasq.conf" "$CURRENT_DNSMASQ" "$DNSMASQ" + show_diff "ethers" "$CURRENT_ETHERS" "$ETHERS" + show_diff "firewall (skyg rules)" "$CURRENT_FIREWALL" "$NEW_FIREWALL" - echo "$ETHERS" - echo "=== ethers diff [START] ===" - comm -3 <(echo "$CURRENT_ETHERS" | sort) <(echo "$ETHERS" | sort) | awk '{if ($0 ~ /^/) print "\033[32m"$0"\033[0m"; else print}' - echo "=== ethers diff [END] ===" echo "" - - echo "$NEW_FIREWALL" - echo "=== firewall (skyg rules) diff [START] ===" - comm -3 <(echo "$CURRENT_FIREWALL" | sort) <(echo "$NEW_FIREWALL" | sort) | awk '{if ($0 ~ /^/) print "\033[32m"$0"\033[0m"; else print}' - echo "=== firewall (skyg rules) diff [END] ===" + echo "skyg firewall rules to be applied:" + echo "$NEW_FIREWALL" | awk -F"'" '/option name/{print " " $2}' echo "" read -p "Deploy to $SERVER? [y/N] " -n 1 -r < /dev/tty; echo @@ -112,8 +128,8 @@ let echo "$ETHERS" | ssh "$SERVER" 'cat > /tmp/ethers.new' echo "$FIREWALL" | ssh "$SERVER" 'cat > /tmp/skyg_firewall.batch' - # Back up, apply, validate, auto-restore on failure - ssh "$SERVER" "TS=$TS bash /tmp/skyg_apply.sh" + # Back up, apply, validate, auto-restore on failure (OpenWrt has no bash, use sh) + ssh "$SERVER" "TS=$TS sh /tmp/skyg_apply.sh" ''; # Dry-run: render the configs locally and write them to .tmp/openwrt-/ diff --git a/modules/openwrt/generator/ABOUTME.md b/modules/openwrt/generator/ABOUTME.md index 5fa6c14..410c301 100644 --- a/modules/openwrt/generator/ABOUTME.md +++ b/modules/openwrt/generator/ABOUTME.md @@ -33,14 +33,18 @@ openwrt-gen firewall < config.json # prints UCI firewall rules (uci batch form ## Input shape (`config.rs`) - `generalMappings`: `[{ ip, domains[] }]` — shared domain→IP records. -- `networks`: `{ : [ { mac, name, id?, domains?, justMac? } ] }` — per-network hosts. +- `networks`: `{ : [ { mac, name, id?, domains?, justMac?, internetOnly? } ] }` — + per-network hosts. A device with `internetOnly: true` is restricted to internet-only + access (matched by MAC), on any network. - `dnsResolvers`: `[{ ip, port?, name? }]` — upstream `server=` entries for dnsmasq. - `internetOnly`: `[, ...]` — networks restricted to internet-only access. ## Internet-only firewall rules (`firewall.rs`) -For each network in `internetOnly`, the generator emits UCI `config rule` blocks that DROP -forwarded traffic from that network to the rest of the LAN (`10.0.0.0/16`): +The generator emits UCI `config rule` blocks that DROP forwarded traffic to the rest of the +LAN (`10.0.0.0/16` IPv4, `fd59:de0a:bff5::/48` IPv6). Two sources of restriction: + +**Network-level** — for each network in `internetOnly`: - **IPv4** — one rule per network matching the whole subnet (catches every device on it). - **IPv6** — one rule per known device MAC. All subnets share a single ULA /64 @@ -48,10 +52,15 @@ forwarded traffic from that network to the rest of the LAN (`10.0.0.0/16`): in IPv6 and blocking is done per known device. An *unknown* IPv6-only device on those subnets is **not** blocked (inherent to the flat-L2 topology). -Rules are named `skyg__drop_lan` (IPv4) and `skyg__drop_lan_v6` (IPv6) so -the deploy script owns exactly these. They are inserted before port-specific ACCEPT rules -(e.g. the existing `sambasharelan`) so the DROPs win. Reverse direction (LAN → restricted -network) stays open, and within-subnet traffic (e.g. cam→cam) is blocked. +**Device-level** — for any device with `internetOnly: true` (on any network): + +- **IPv4 + IPv6** — one rule each matched by that device's MAC. This lets you restrict a + single machine (e.g. a printer on `lab`) without restricting its whole subnet. + +Rules are named `skyg__drop_lan` / `skyg__drop_lan_v6` (network-level) and +`skyg_mac__drop_lan` / `skyg_mac__drop_lan_v6` (device-level, MAC with colons +stripped) so the deploy script owns exactly these. Reverse direction (LAN → restricted +device) stays open, and within-subnet traffic (e.g. cam→cam) is blocked. ## Conventions diff --git a/modules/openwrt/generator/src/config.rs b/modules/openwrt/generator/src/config.rs index 93391af..d3bf66a 100644 --- a/modules/openwrt/generator/src/config.rs +++ b/modules/openwrt/generator/src/config.rs @@ -38,6 +38,9 @@ pub struct Device { pub domains: Option>, #[serde(rename = "justMac", default)] pub just_mac: bool, + /// Restrict this single device to internet-only access (matched by MAC). + #[serde(rename = "internetOnly", default)] + pub internet_only: bool, } #[cfg(test)] @@ -99,4 +102,28 @@ mod tests { let config: Config = serde_json::from_str(json).unwrap(); assert!(config.internet_only.is_empty()); } + + #[test] + fn test_device_internet_only_flag() { + let json = r#"{ + "generalMappings": [], + "networks": { + "lab": [{"mac": "aa:bb:cc:dd:ee:ff", "name": "printer", "internetOnly": true}] + } + }"#; + let config: Config = serde_json::from_str(json).unwrap(); + assert!(config.networks["lab"][0].internet_only); + } + + #[test] + fn test_device_internet_only_defaults_false() { + let json = r#"{ + "generalMappings": [], + "networks": { + "lab": [{"mac": "aa:bb:cc:dd:ee:ff", "name": "printer"}] + } + }"#; + let config: Config = serde_json::from_str(json).unwrap(); + assert!(!config.networks["lab"][0].internet_only); + } } diff --git a/modules/openwrt/generator/src/generators/dnsmasq.rs b/modules/openwrt/generator/src/generators/dnsmasq.rs index f794320..db66cb3 100644 --- a/modules/openwrt/generator/src/generators/dnsmasq.rs +++ b/modules/openwrt/generator/src/generators/dnsmasq.rs @@ -59,6 +59,7 @@ mod tests { id: None, domains, just_mac: false, + internet_only: false, }]); Config { general_mappings: vec![], networks, dns_resolvers: vec![], internet_only: vec![] } } diff --git a/modules/openwrt/generator/src/generators/ethers.rs b/modules/openwrt/generator/src/generators/ethers.rs index af562d3..9785564 100644 --- a/modules/openwrt/generator/src/generators/ethers.rs +++ b/modules/openwrt/generator/src/generators/ethers.rs @@ -39,6 +39,7 @@ mod tests { id: None, domains: None, just_mac, + internet_only: false, }]); Config { general_mappings: vec![], networks, dns_resolvers: vec![], internet_only: vec![] } } diff --git a/modules/openwrt/generator/src/generators/firewall.rs b/modules/openwrt/generator/src/generators/firewall.rs index 0568769..fbb87cb 100644 --- a/modules/openwrt/generator/src/generators/firewall.rs +++ b/modules/openwrt/generator/src/generators/firewall.rs @@ -1,17 +1,18 @@ use crate::config::Config; use crate::network::{prefix_for, process_network, ULA_PREFIX}; -/// Renders UCI firewall rules (in `uci batch` format) that restrict the networks -/// listed in `config.internet_only` to internet-only access. +/// Renders UCI firewall rules (in `uci batch` format) that restrict devices to +/// internet-only access. /// -/// For each restricted network: -/// - IPv4: a single DROP rule matching the whole subnet (catches all devices on it). -/// - IPv6: a DROP rule per known device MAC (all subnets share one /64, so subnets -/// are not distinguishable in IPv6). +/// Two sources of restriction: +/// - **Network-level** (`config.internet_only`): for each restricted network, +/// - IPv4: a single DROP rule matching the whole subnet (catches all devices on it). +/// - IPv6: a DROP rule per known device MAC (all subnets share one /64, so subnets +/// are not distinguishable in IPv6). +/// - **Device-level** (a device with `internetOnly: true`): IPv4 + IPv6 DROP rules +/// matched by that device's MAC, on any network. /// -/// Rules are named `skyg__drop_lan[_v6]` so the deploy script can manage -/// exactly the rules it owns. They are inserted before port-specific ACCEPT rules -/// (e.g. the existing `sambasharelan`) so the DROPs win. +/// Rules are named `skyg_*` so the deploy script can manage exactly the rules it owns. pub fn generate(config: &Config) -> String { let mut out = String::new(); out.push_str("# Managed by skyg openwrt — do not edit by hand\n"); @@ -56,9 +57,49 @@ pub fn generate(config: &Config) -> String { } } + // Device-level: any device flagged internetOnly, regardless of network. + for (network, devices) in &config.networks { + for pd in process_network(network, devices) { + if !pd.device.internet_only { + continue; + } + let slug = mac_slug(&pd.device.mac); + out.push_str(&format!( + "config rule\n\ + \toption name 'skyg_mac_{slug}_drop_lan'\n\ + \toption src 'lan'\n\ + \toption dest 'lan'\n\ + \toption src_mac '{mac}'\n\ + \toption dest_ip '10.0.0.0/16'\n\ + \toption target 'DROP'\n\ + \toption family 'ipv4'\n", + slug = slug, + mac = pd.device.mac, + )); + out.push_str(&format!( + "config rule\n\ + \toption name 'skyg_mac_{slug}_drop_lan_v6'\n\ + \toption src 'lan'\n\ + \toption dest 'lan'\n\ + \toption src_mac '{mac}'\n\ + \toption dest_ip '{ula}'\n\ + \toption target 'DROP'\n\ + \toption family 'ipv6'\n", + slug = slug, + mac = pd.device.mac, + ula = ULA_PREFIX, + )); + } + } + out } +/// MAC address without separators, for use in a UCI rule name. +fn mac_slug(mac: &str) -> String { + mac.replace(':', "") +} + #[cfg(test)] mod tests { use super::*; @@ -76,6 +117,7 @@ mod tests { id: None, domains: None, just_mac: false, + internet_only: false, }, Device { mac: "aa:bb:cc:dd:ee:02".to_string(), @@ -83,6 +125,7 @@ mod tests { id: None, domains: None, just_mac: false, + internet_only: false, }, ], ); @@ -129,4 +172,35 @@ mod tests { let result = generate(&config); assert!(result.lines().count() == 1); // only the header comment } + + #[test] + fn test_device_level_internet_only() { + let mut config = config_with_devices(vec![]); + // Flag one device on a non-restricted network as internet-only. + config + .networks + .entry("lab".to_string()) + .or_insert_with(Vec::new) + .push(Device { + mac: "11:22:33:44:55:66".to_string(), + name: "printer".to_string(), + id: None, + domains: None, + just_mac: false, + internet_only: true, + }); + let result = generate(&config); + assert!(result.contains("skyg_mac_112233445566_drop_lan")); + assert!(result.contains("skyg_mac_112233445566_drop_lan_v6")); + assert!(result.contains("option src_mac '11:22:33:44:55:66'")); + assert!(result.contains("option dest_ip '10.0.0.0/16'")); + assert!(result.contains("option dest_ip 'fd59:de0a:bff5::/48'")); + // No subnet rules were generated (no network-level internetOnly). + assert!(!result.contains("skyg_lab_drop_lan\n")); + } + + #[test] + fn test_mac_slug_strips_colons() { + assert_eq!(mac_slug("aa:bb:cc:dd:ee:ff"), "aabbccddeeff"); + } } diff --git a/modules/openwrt/generator/src/network.rs b/modules/openwrt/generator/src/network.rs index 53092b1..fbfaf9b 100644 --- a/modules/openwrt/generator/src/network.rs +++ b/modules/openwrt/generator/src/network.rs @@ -71,6 +71,7 @@ mod tests { id, domains: None, just_mac, + internet_only: false, } } diff --git a/secrets/glmain.json.age b/secrets/glmain.json.age index d1e992e..14e23bd 100644 Binary files a/secrets/glmain.json.age and b/secrets/glmain.json.age differ