diff --git a/CMakeLists.txt b/CMakeLists.txt index 26e263985..1bc6998ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -221,6 +221,8 @@ set_tests_properties(npu_repack_path PROPERTIES SKIP_RETURN_CODE 77) # layouts: no root, no mounts, no device, no VM. add_test(NAME one_bit_update_slots COMMAND sh ${CMAKE_SOURCE_DIR}/tests/one_bit_update_slots_test.sh ${CMAKE_SOURCE_DIR}) +add_test(NAME one_bit_network_conf + COMMAND sh ${CMAKE_SOURCE_DIR}/tests/one_bit_network_conf_test.sh ${CMAKE_SOURCE_DIR}) add_test(NAME npu_import_contract COMMAND ${CMAKE_SOURCE_DIR}/tests/npu_import_contract.sh ${CMAKE_SOURCE_DIR}) set_tests_properties(npu_import_contract PROPERTIES SKIP_RETURN_CODE 77) diff --git a/docs/os-v3.md b/docs/os-v3.md index e8f985a02..09a6da9f6 100644 --- a/docs/os-v3.md +++ b/docs/os-v3.md @@ -156,11 +156,10 @@ named here as what they are: | logging | `06onboard-logs` keeps the boot's logs on `/data/logs` — the partition, not the filesystem that the next appliance replaces | `/data/logs` non-empty on the installed boot | | packages and services | `apt` works and its packages land in the save layer; the appliance's own services are `/etc/init.d` scripts, which is what `rc.services` runs (there is no systemd here, and nothing pretends there is) | — | -**Not present, and named:** a **persisted static address**. The console's "set a static address" is *session only* on -purpose — a wrong one on a headless box is a walk to the machine — and the persistent form is not a one-line change: on -this base connman owns the interface, so it has to be a connman service config, with the console (or the install -screen, #710) as its recovery path. That is the remaining piece of engine#712's third item, and it is a product decision -rather than a row to add to a gate. +| an optional static address | `1bit-network set-static [gateway] [dns…]` writes **`/data/net.conf`** — on the data partition, so it outlives an appliance update — and `/etc/init.d/14onboard-net` applies it at boot through **connman's own CLI** (`connmanctl config --ipv4 manual …`), with `ip` only as the fallback for a link connman has no service for yet. The console's static action stays session-only, because a wrong address on a headless box is a walk to the machine; this one is persistent **and reversible in one command**: `1bit-network reset` deletes the file and hands the interface back to DHCP. An unparsable or unusable file is not applied at all — the boot leaves the link on DHCP and says so in `/var/log/1bit-network.log` | `tests/one_bit_network_conf_test.sh` (the address and file validation, 26 arms, no root or interface needed) and the installed boot's `1bit-network` rows | + +**Recovery, stated where someone under pressure will look:** `1bit-network reset` on tty1, the serial console, or the +console menu's bash item. The file is `/data/net.conf`; deleting it is the same thing. ## The Ubuntu plan (history, not a target) diff --git a/os/v3/puppy/README.md b/os/v3/puppy/README.md index f5bedd642..7db5b0d19 100644 --- a/os/v3/puppy/README.md +++ b/os/v3/puppy/README.md @@ -160,6 +160,7 @@ this recipe ships are: | `05onboard-data` | finds `1BIT-DATA` (`1bit.data=` on the boot line, else the label), mounts it at `/data`, creates the contract dirs, points `/etc/1bit/lemond.conf` at `/data/lemonade/lemond.conf`, copies `/data/ssh/authorized_keys` and links the host key from `/data/ssh`, and registers `/data/models` in Lemonade's `user_models.json` | | `10lemonade` | starts `/opt/1bit-package/lemond-onebit`, binds `0.0.0.0:13305`, logs to `/var/log/lemond-onebit.log`. **No key by default** (cycle 20, the owner's "blank key": the appliance ships like stock Lemonade). A key is opt-in and lives in `/etc/1bit/lemond.conf` (mode 600, a symlink onto `/data`) — console menu item 1 mints one, removes it, and restarts this service so lemond comes up in the other mode; nothing else in the image holds it | | `07onboard-user` | **the installed machine's normal account** (engine#712): on an installed boot only (`/data` mounted with `1bit-install`'s `.installed` on it) it creates `onebit` — home, bash, **locked password** so a key is the only way in — installs `/data/ssh/authorized_keys` as its `0600` key file (re-applied at every installed boot, exactly as `20onboard-ssh` does for root), and appends **one** sudo rule, `onebit ALL=(ALL) NOPASSWD: ALL`, backed up and proved with `sudo -n true` as that user, rolled back if it fails. A live stick exits without touching anything: a machine that forgets stays root-only | +| `14onboard-net` + `usr/local/bin/1bit-network` | **the persistent network identity** (engine#712): `1bit-network set-static [gateway] [dns…]` validates the answer, writes `/data/net.conf` (data partition, so it survives an appliance update) and applies it now through connman's own CLI; `14onboard-net` applies the same file at every boot, and does nothing at all when the file is absent or does not validate (a bad file leaves the link on DHCP rather than half-applying). `1bit-network reset` is the whole recovery path: it deletes the file and gives the interface back to DHCP. The console's own static action stays session-only on purpose | | `20onboard-ssh` | `ssh-keygen -A`, `/root/.ssh` 700 + `authorized_keys` 600, then sshd. An image with no key provisions no listener | | `21serial-console` | a root shell on `/dev/ttyS0`, only when the command line carries `1bit.serial=1` (that shell has no password, so it is opt-in) | | `26onboard-board` | `os/mini/board.sh` as `/usr/sbin/1bit-board` (the path release-check's `fans` row reads): EC fan curve, power profile and the detached boost loop, on a board whose DMI it knows; on any other machine it writes nothing and says so | diff --git a/os/v3/puppy/container-build.sh b/os/v3/puppy/container-build.sh index c5628ad51..04cead47e 100644 --- a/os/v3/puppy/container-build.sh +++ b/os/v3/puppy/container-build.sh @@ -313,6 +313,8 @@ if [ -n "${PAYLOAD:-}" ]; then install -D -m 755 /recipe/embed/usr/local/bin/1bit-logs "$skeleton/usr/local/bin/1bit-logs" # engine#712: the installed machine's OS-level update (the other slot plus one GRUB default flip) install -D -m 755 /recipe/embed/usr/local/bin/1bit-update "$skeleton/usr/local/bin/1bit-update" + # engine#712: the persistent network identity (a static address on /data, applied at boot, one command to forget) + install -D -m 755 /recipe/embed/usr/local/bin/1bit-network "$skeleton/usr/local/bin/1bit-network" install -D -m 755 /recipe/embed/etc/init.d/06onboard-logs "$skeleton/etc/init.d/06onboard-logs" install -D -m 755 /recipe/embed/etc/init.d/08onboard-presets "$skeleton/etc/init.d/08onboard-presets" install -D -m 755 /recipe/embed/root/Startup/1bit-lemonade-webapp \ @@ -357,7 +359,7 @@ if [ -n "${PAYLOAD:-}" ]; then chmod 0755 "$skeleton/usr/local/bin/1bit-tty1" chmod 0755 "$skeleton"/etc/init.d/03onboard-efivars "$skeleton"/etc/init.d/05onboard-data \ "$skeleton"/etc/init.d/06onboard-logs "$skeleton"/etc/init.d/07onboard-user \ - "$skeleton"/etc/init.d/08onboard-presets \ + "$skeleton"/etc/init.d/08onboard-presets "$skeleton"/etc/init.d/14onboard-net \ "$skeleton"/etc/init.d/10lemonade \ "$skeleton"/etc/init.d/18onboard-mdns \ "$skeleton"/etc/init.d/20onboard-ssh "$skeleton"/etc/init.d/21serial-console \ @@ -648,6 +650,7 @@ if [ -n "${PAYLOAD:-}" ]; then usr/local/bin/1bit-record usr/local/bin/1bit-lemonade-webapp \ usr/local/bin/1bit-webapp-proxy usr/local/bin/1bit-desktop usr/local/bin/1bit-logs \ etc/init.d/06onboard-logs etc/init.d/07onboard-user etc/init.d/08onboard-presets \ + etc/init.d/14onboard-net usr/local/bin/1bit-network \ root/Startup/1bit-lemonade-webapp \ etc/personal_settings_popup_disabled etc/1bit/palemoon-prefs.js \ etc/init.d/24onboard-webapp opt/1bit/webapp/index.html \ diff --git a/os/v3/puppy/embed/etc/init.d/14onboard-net b/os/v3/puppy/embed/etc/init.d/14onboard-net new file mode 100755 index 000000000..270c873a2 --- /dev/null +++ b/os/v3/puppy/embed/etc/init.d/14onboard-net @@ -0,0 +1,99 @@ +#!/bin/sh +# Copyright 2026 bong-water-water-bong +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# /etc/init.d/14onboard-net - apply the persistent network identity, if an install has one (engine#712). +# +# `/data/net.conf` is written by `1bit-network set-static` and lives on the DATA partition, so it survives an appliance +# update (nothing the OS set carries is touched). This runs after 05onboard-data has mounted /data and before the +# services that talk to the network; with no file, or a file that does not validate, it does NOTHING and says so - a +# machine that cannot reach the LAN is worse than one on DHCP, and the repair path has to stay obvious: +# +# recovery: 1bit-network reset (tty1, the serial console, or the console menu's bash item) +# +# It never writes the file, never guesses an address, and never leaves the interface without one: connman is asked for +# the manual configuration first (it owns the link on this base), and `ip` is only the fallback for a boot where connman +# has no service for that interface yet. +set -u + +# shellcheck disable=SC2154 # iface/static/gateway/dns are set by read_conf, sourced from 1bit-network below +iface=''; static=''; gateway=''; dns='' # set by read_conf; defined here for `set -u` +DATA=/data +CONF=$DATA/net.conf +LOG=/var/log/1bit-network.log +log() { printf '%s\n' "$*" | tee -a "$LOG"; } + +[ "${1:-start}" = "stop" ] && exit 0 +mkdir -p /tmp 2> /dev/null +: >> "$LOG" 2> /dev/null || LOG=/dev/null + +[ -f "$CONF" ] || { log "14onboard-net: no $CONF: the interface stays on DHCP (connman)"; exit 0; } + +# the same parse and validation as 1bit-network, sourced rather than copied: one definition of "a usable net.conf" +NETWORK=/usr/local/bin/1bit-network +if [ -r "$NETWORK" ]; then + # shellcheck disable=SC1090 # the path is the image's own, and this is the point: one definition of the format + ONEBIT_NETWORK_LIB=1 . "$NETWORK" +fi +if ! command -v read_conf > /dev/null 2>&1 || ! command -v conf_ok > /dev/null 2>&1; then + log "14onboard-net: cannot load the parser from $NETWORK: leaving the network alone (recovery: 1bit-network reset)" + exit 0 +fi + +read_conf "$CONF" +if ! why=$(conf_ok 2>&1); then + log "14onboard-net: $CONF is not usable ($why): leaving the network on DHCP. Recovery: 1bit-network reset" + exit 0 +fi + +# wait for the interface to exist: on a cold boot the driver may still be binding when rc.services starts +i=0 +while [ ! -e "/sys/class/net/$iface" ] && [ $i -lt 20 ]; do sleep 1; i=$((i + 1)); done +if [ ! -e "/sys/class/net/$iface" ]; then + log "14onboard-net: $iface did not appear in 20 s: leaving the network alone" + exit 0 +fi + +applied=no +if command -v connmanctl > /dev/null 2>&1; then + # the service connman has for this interface: "__", looked up by the service list rather than + # guessed, and only the connected one (`*` marker) is reconfigured + svc=$(connmanctl services 2> /dev/null | awk -v i="$iface" '/^\*/ { print $2; exit }') + if [ -n "$svc" ]; then + if [ -n "$gateway" ]; then + connmanctl config "$svc" --ipv4 manual "$static" "$gateway" $dns >> "$LOG" 2>&1 + else + connmanctl config "$svc" --ipv4 manual "$static" >> "$LOG" 2>&1 + fi + [ $? -eq 0 ] && applied=yes + fi +fi + +if [ "$applied" = no ]; then + # connman has no service for it (a boot before connman settled, or a bare interface): put it on with ip, which + # connman leaves alone for as long as it has no service of its own for that link + if ip addr add "$static" dev "$iface" >> "$LOG" 2>&1; then + [ -n "$gateway" ] && ip route replace default via "$gateway" dev "$iface" >> "$LOG" 2>&1 + applied=yes + fi +fi + +if [ "$applied" = yes ]; then + log "14onboard-net: $iface carries $static${gateway:+ via $gateway} from $CONF" + exit 0 +fi +log "14onboard-net: could not apply $static to $iface (see $LOG); the link keeps whatever DHCP gave it." \ + "Recovery: 1bit-network reset" +exit 0 diff --git a/os/v3/puppy/embed/usr/local/bin/1bit-network b/os/v3/puppy/embed/usr/local/bin/1bit-network new file mode 100755 index 000000000..d17642bf7 --- /dev/null +++ b/os/v3/puppy/embed/usr/local/bin/1bit-network @@ -0,0 +1,216 @@ +#!/bin/sh +# Copyright 2026 bong-water-water-bong +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# 1bit-network show what the machine's address is now, and what /data/net.conf asks for +# 1bit-network set-static [gateway] [dns...] +# persist a static address on this install and apply it now +# 1bit-network reset forget it: back to DHCP (the recovery command for a wrong address) +# 1bit-network --check parse /data/net.conf and say what would be applied, changing nothing +# +# The appliance takes its address from connman (DHCP). A server also wants the *option* of a fixed address, and the +# console's own "set a static address" is deliberately session-only - a wrong address on a headless box is a walk to +# the machine. So the fixed address lives in ONE file on the data partition, `/data/net.conf`, which survives an +# appliance update because it is not on the OS set, and `reset` - or deleting that file - is the whole recovery, from +# tty1, the serial console or the console menu. +# +# connman owns the interface on this base, so the address is set with connman's own CLI (`connmanctl config +# --ipv4 manual ...`): poking `ip addr add` behind connman's back gets undone the next time connman touches the link. +# `ip` is the fallback for a boot where connman has no service for that interface yet. +# +# Exit: 0 done, 1 usage, 2 refused (bad or unsafe input, nothing written), 3 failed while applying. +set -u + +DATA=/data +CONF=$DATA/net.conf +LOG=/var/log/1bit-network.log +say() { printf '%s\n' "$*" | tee -a "$LOG"; } +die() { say "1bit-network: REFUSED: $*" >&2; exit 2; } +fail() { say "1bit-network: FAILED: $*" >&2; exit 3; } +usage() { sed -n '/^# 1bit-network show/,/^# Exit:/p' "$0" | sed 's/^# \{0,1\}//'; } + +# ── the file: one `key=value` per line, nothing else. Pure functions, so tests/one_bit_network_conf_test.sh can run +# them without root, an interface or a VM (ONEBIT_NETWORK_LIB=1 stops before anything is applied). +valid_addr() { # $1 addr/prefix -> 0 when it is IPv4 with a prefix 1..32 + case $1 in + *[!0-9./]*|"") return 1 ;; + esac + case $1 in + */*) ;; + *) return 1 ;; + esac + ip=${1%/*}; pfx=${1#*/} + case $pfx in + ""|*[!0-9]*) return 1 ;; + esac + [ "$pfx" -ge 1 ] && [ "$pfx" -le 32 ] || return 1 + oldifs=$IFS; IFS=. + set -- $ip + IFS=$oldifs + [ $# -eq 4 ] || return 1 + for o in "$@"; do + case $o in + ""|*[!0-9]*) return 1 ;; + esac + [ "$o" -le 255 ] || return 1 + done + return 0 +} +valid_ip() { # $1 plain IPv4 + valid_addr "$1/32" 2> /dev/null +} +valid_iface() { # $1 interface name that the kernel knows + [ -n "$1" ] || return 1 + case $1 in + *[!a-zA-Z0-9_.:-]*) return 1 ;; + esac + [ -e "/sys/class/net/$1" ] +} +# read_conf FILE: sets iface/static/gateway/dns from the file, refusing anything that is not one of the four keys +read_conf() { + iface=''; static=''; gateway=''; dns='' + [ -f "$1" ] || return 0 + while IFS= read -r line; do + case $line in + ''|'#'*) continue ;; + esac + key=${line%%=*} + val=${line#*=} + case $key in + iface) iface=$val ;; + static) static=$val ;; + gateway) gateway=$val ;; + dns) dns=$val ;; + *) echo "1bit-network: ignoring unknown key '$key' in $1" >&2 ;; + esac + done < "$1" + return 0 +} +conf_ok() { # the loaded values have to be usable, or the boot must not touch the network at all + valid_iface "${iface:-}" || { echo "iface '${iface:-}' is not an interface on this machine"; return 1; } + valid_addr "${static:-}" || { echo "static '${static:-}' is not IPv4 addr/prefix"; return 1; } + [ -z "${gateway:-}" ] || valid_ip "$gateway" || { echo "gateway '${gateway:-}' is not an IPv4 address"; return 1; } + for d in ${dns:-}; do + valid_ip "$d" || { echo "dns '$d' is not an IPv4 address"; return 1; } + done + return 0 +} +# on this boot: the interface that carries the default route, else the first one that is not loopback and is up +default_iface() { + dev=$(ip route show default 2> /dev/null | awk '/^default/ { for (i = 1; i <= NF; i++) if ($i == "dev") { print $(i + 1); exit } }') + [ -n "$dev" ] && { echo "$dev"; return 0; } + for d in /sys/class/net/*; do + n=$(basename "$d") + [ "$n" = lo ] && continue + [ "$(cat "$d/operstate" 2> /dev/null)" = up ] && { echo "$n"; return 0; } + done + return 1 +} + +# sourced by tests with ONEBIT_NETWORK_LIB=1: the functions above, nothing below +if [ "${ONEBIT_NETWORK_LIB:-}" = 1 ]; then return 0 2> /dev/null || exit 0; fi + +mkdir -p /tmp 2> /dev/null +: >> "$LOG" 2> /dev/null || LOG=/dev/null + +mode=show +case "${1:-show}" in + -h|--help|help) usage; exit 0 ;; + show|--status) mode=show ;; + --check) mode=check ;; + set-static) mode=set-static ;; + reset|--reset) mode=reset ;; + *) usage >&2; exit 1 ;; +esac + +if [ "$mode" = show ]; then + say "1bit-network: $(hostname 2> /dev/null), interface $(default_iface 2> /dev/null || echo '')" + ip -4 -o addr show 2> /dev/null | awk '{ print " " $2 " " $4 }' | tee -a "$LOG" + ip route show default 2> /dev/null | sed 's/^/ default: /' | tee -a "$LOG" + if [ -f "$CONF" ]; then + read_conf "$CONF" + say " $CONF: iface=$iface static=$static gateway=${gateway:--} dns=${dns:--}" + else + say " $CONF: absent (DHCP)" + fi + exit 0 +fi + +if [ "$mode" = check ]; then + [ -f "$CONF" ] || { say "1bit-network: no $CONF: DHCP"; exit 0; } + read_conf "$CONF" + if why=$(conf_ok 2>&1); then + say "1bit-network: $CONF parses: iface=$iface static=$static gateway=${gateway:--} dns=${dns:--}" + exit 0 + fi + say "1bit-network: $CONF is not usable: $why (the boot would leave the network alone)" >&2 + exit 2 +fi + +if [ "$mode" = reset ]; then + read_conf "$CONF" + rm -f "$CONF" || fail "cannot remove $CONF" + # give the interface back to DHCP through connman, so a wrong address really is gone and not just forgotten + if [ -n "${iface:-}" ] && command -v connmanctl > /dev/null 2>&1; then + svc=$(connmanctl services 2> /dev/null | awk -v i="$iface" ' + /^\*/ { name = $2; mac = $3 } + NF == 1 && name { print name; exit } + { }' | head -1) + [ -n "$svc" ] && connmanctl config "$svc" --ipv4 dhcp >> "$LOG" 2>&1 && \ + say "1bit-network: $svc back to DHCP" + fi + say "1bit-network: reset - $CONF removed, the next boot and this one use DHCP" + say " (if the link still carries the old address, restart the network: /etc/init.d/connman restart)" + exit 0 +fi + +# ── set-static +[ "$(id -u)" = 0 ] || die "run as root (it writes $CONF and reconfigures the interface)" +[ $# -ge 2 ] || die "usage: 1bit-network set-static [gateway] [dns...]" +[ -d "$DATA" ] && grep -q " $DATA " /proc/mounts 2> /dev/null || \ + die "no $DATA: a static address only persists on an installed machine (a stick forgets by design)" +addr=$2; gw=${3:-}; shift 2; [ $# -gt 0 ] && shift; dnsv="$*" +valid_addr "$addr" || die "'$addr' is not IPv4 with a prefix (e.g. 192.168.50.20/24)" +[ -z "$gw" ] || valid_ip "$gw" || die "gateway '$gw' is not an IPv4 address" +for d in $dnsv; do valid_ip "$d" || die "dns '$d' is not an IPv4 address"; done +[ -n "$gw" ] || say "1bit-network: no gateway given: the machine will keep its current default route" +dev=$(default_iface) || die "no interface is up, so there is nothing to give an address to (plug the cable in first)" +valid_iface "$dev" || die "'$dev' is not an interface" + +# a static address without the machine's own subnet gateway is the classic way to lock yourself out: say it once +case " $addr " in + *"/32 "*) say "1bit-network: WARNING: a /32 has no on-link neighbours: only the gateway will be reachable" ;; +esac + +tmp=$CONF.tmp +{ + echo "# written by 1bit-network on $(date -u +%FT%TZ); applied at boot by /etc/init.d/14onboard-net" + echo "# recovery: 1bit-network reset (from tty1, the serial console or the console menu)" + echo "iface=$dev" + echo "static=$addr" + [ -n "$gw" ] && echo "gateway=$gw" + [ -n "$dnsv" ] && echo "dns=$dnsv" +} > "$tmp" || fail "cannot write $tmp" +mv -f "$tmp" "$CONF" || fail "cannot install $CONF" +sync +say "1bit-network: persisted to $CONF (iface=$dev static=$addr gateway=${gw:--} dns=${dnsv:--})" + +/etc/init.d/14onboard-net start || fail "applied the file but the interface did not take the address (see $LOG)" +if ip -4 -o addr show dev "$dev" 2> /dev/null | grep -q " ${addr%/*}/"; then + say "1bit-network: $dev carries ${addr} now, and will again at the next boot" + [ -z "$gw" ] || say " gateway $gw" + exit 0 +fi +fail "$dev does not carry ${addr%/*} after applying $CONF (see $LOG)" diff --git a/os/v3/puppy/install-probe.sh b/os/v3/puppy/install-probe.sh index d1602d66e..868645c6f 100755 --- a/os/v3/puppy/install-probe.sh +++ b/os/v3/puppy/install-probe.sh @@ -682,6 +682,45 @@ if assert_pupsfs: "Lemonade tcp/13305, mDNS udp/5353) the only ACCEPT rules, loopback+established exempted; a time " "source (%s), hostname %s, /data/logs %s file(s)" % (wait_s, ntp.strip(), hn, logs)) + # ---- engine#712 the persistent network identity: the address the machine has now becomes a static one in + # /data/net.conf, the machine reboots, and it must come up carrying it - then `1bit-network reset` must take + # it away. The value is the machine's OWN current address, so nothing later in this run loses the network. + cur = val("CUR", "ip -4 -o addr show scope global 2>/dev/null | awk '{ print $2 \" \" $4; exit }'") + parts = cur.split() + if len(parts) == 2 and "/" in parts[1]: + dev, addr = parts[0], parts[1] + gw = val("GW", "ip route show default 2>/dev/null | awk '/^default/ { for (i=1;i<=NF;i++) if ($i==\"via\") { print $(i+1); exit } }'") + setr = val("SETSTATIC", "1bit-network set-static %s %s 2>&1 | tr '\\n' '; '" % (addr, gw or ""), timeout=240) + say("INFO net set-static %s %s -> %s" % (addr, gw or "-", setr[:300])) + if "persisted to /data/net.conf" not in setr: + say("FAIL net set-static did not persist the address: %s" % (setr[-200:] or "")); ok = False + else: + say("PASS net a static address persisted on the data partition and applied now") + net_after = val("NETNOW", "ip -4 -o addr show dev %s 2>/dev/null | awk '{ print $4 }'" % dev) + if addr not in net_after: + say("FAIL net the interface does not carry %s right after set-static (%s)" % (addr, net_after or "")); ok = False + if not v.reboot(): + say("FAIL net the machine did not come back after the static-address reboot"); ok = False + v.close() + else: + per = val("PERSIST", "ip -4 -o addr show dev %s 2>/dev/null | awk '{ print $4 }'" % dev) + f = val("CONF", "grep -c '^static=' /data/net.conf 2>/dev/null") + if addr not in per: + say("FAIL net after the reboot the interface does not carry %s (%s): the file was not applied" % (addr, per or "")); ok = False + elif f != "1": + say("FAIL net /data/net.conf is gone after the reboot (static= lines: %s)" % (f or "0")); ok = False + else: + say("PASS net %s carries %s across a reboot, read from /data/net.conf on the data partition" % (dev, addr)) + reset = val("RESETNET", "1bit-network reset 2>&1 | tr '\\n' '; '", timeout=180) + left = val("LEFT", "ls /data/net.conf 2>&1 | head -1") + if "reset" not in reset: + say("FAIL net 1bit-network reset did not report: %s" % (reset[-200:] or "")); ok = False + elif "No such file" not in left: + say("FAIL net /data/net.conf is still there after reset (%s)" % left); ok = False + else: + say("PASS net reset removed /data/net.conf and handed the link back to DHCP") + else: + say("INFO net no global IPv4 address on this boot (%s): the static-address rows are a SKIP" % (cur or "")) wrote = val("WROTE", "sh -c 'echo 712 > /root/712-persist; sync; cat /root/712-persist'") if wrote != "712": print("FAIL persist could not write to the installed root (/root/712-persist: %s)" % (wrote or "")); ok = False diff --git a/tests/one_bit_network_conf_test.sh b/tests/one_bit_network_conf_test.sh new file mode 100755 index 000000000..db1373c06 --- /dev/null +++ b/tests/one_bit_network_conf_test.sh @@ -0,0 +1,91 @@ +#!/bin/sh +# Copyright 2026 bong-water-water-bong +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# one_bit_network_conf_test.sh: the address validation and the /data/net.conf parse of +# os/v3/puppy/.../1bit-network, run as the real functions over real files. No root, no interface, no VM: the script is +# sourced with ONEBIT_NETWORK_LIB=1, which defines its functions and returns before it touches anything. +# +# This is the file that can make an installed machine unreachable, so what it must never do is accept a value the +# kernel would refuse or half-read a file: a bad address has to be a refusal *before* anything is written, and an +# unparsable file has to leave the network alone rather than apply half of it. +# +# usage: tests/one_bit_network_conf_test.sh +set -u +root=${1:?usage: one_bit_network_conf_test.sh } +script=$root/os/v3/puppy/embed/usr/local/bin/1bit-network +[ -f "$script" ] || { echo "no $script"; exit 1; } + +fail=0 +check() { if [ "$2" = 0 ]; then echo "ok $1"; else echo "FAIL $1"; fail=1; fi; } +t=$(mktemp -d); trap 'rm -rf "$t"' EXIT + +# shellcheck disable=SC1090 +ONEBIT_NETWORK_LIB=1 . "$script" +check "the script sources as a library without running (ONEBIT_NETWORK_LIB=1)" 0 + +# 1. addresses the kernel would take +for a in 10.0.2.50/24 192.168.50.20/24 172.16.0.1/12 1.1.1.1/32 0.0.0.0/1 255.255.255.255/32; do + valid_addr "$a"; check "valid_addr accepts $a" $? +done + +# 2. and the ones it must refuse, each of which would otherwise be written into a boot-time file +for a in "" 10.0.2.50 10.0.2.50/ 10.0.2.50/33 10.0.2.50/0 10.0.2/24 10.0.2.256/24 10.0.2.-1/24 \ + 10.0.2.50/24x abc/24 1.2.3.4.5/24 " 10.0.2.50/24" 10.0.2.50/24extra; do + valid_addr "$a" && { check "valid_addr refuses '$a'" 1; continue; } + check "valid_addr refuses '$a'" 0 +done + +# 3. gateways and DNS are plain addresses: a prefix there is a mistake, not a shortcut +valid_ip 10.0.2.2; check "valid_ip accepts a plain address" $? +valid_ip 10.0.2.2/24 && { check "valid_ip refuses a CIDR (a gateway is not a prefix)" 1; } || check "valid_ip refuses a CIDR (a gateway is not a prefix)" 0 + +# 4. the file itself: comments, blank lines, an unknown key (ignored with a warning), and the four keys it uses +cat > "$t/good.conf" <<'CONF' +# written by hand in this test +iface=lo +static=10.0.2.50/24 +gateway=10.0.2.2 +dns=1.1.1.1 8.8.8.8 +CONF +read_conf "$t/good.conf" +[ "$iface" = lo ] && [ "$static" = 10.0.2.50/24 ] && [ "$gateway" = 10.0.2.2 ] && \ + [ "$dns" = "1.1.1.1 8.8.8.8" ]; check "a well-formed file is read into iface/static/gateway/dns" $? +conf_ok > /dev/null; check "and it validates (lo is an interface on any machine)" $? + +printf 'iface=lo\nstatic=10.0.2.50/24\nbogus=1\n' > "$t/unknown.conf" +warn=$(read_conf "$t/unknown.conf" 2>&1) +case $warn in *"unknown key 'bogus'"*) true ;; *) false ;; esac +check "an unknown key is reported, not silently ignored [$warn]" $? +conf_ok > /dev/null; check "and it does not stop the file from validating" $? + +# 5. the cases where conf_ok has to refuse, because the boot then leaves the network alone instead of applying it +for bad in "iface=lo\nstatic=10.0.2.50\n" "iface=lo\nstatic=10.0.2.50/24\ngateway=nope\n" \ + "iface=lo\nstatic=10.0.2.50/24\ndns=nope\n" "iface=nosuchiface0\nstatic=10.0.2.50/24\n"; do + printf "$bad" > "$t/bad.conf" + read_conf "$t/bad.conf" + if conf_ok > "$t/why" 2>&1; then check "conf_ok refuses '$(printf "$bad" | tr '\n' ';')'" 1 + else check "conf_ok refuses '$(printf "$bad" | tr '\n' ';')' [$(cat "$t/why")]" 0; fi +done + +# 6. an empty or absent file is "no opinion", not an error: the boot must treat it as DHCP +: > "$t/empty.conf" +read_conf "$t/empty.conf" +[ -z "$iface" ] && [ -z "$static" ]; check "an empty file parses to nothing (DHCP)" $? +read_conf "$t/does-not-exist" +[ -z "$static" ]; check "an absent file parses to nothing (DHCP)" $? + +[ $fail = 0 ] && echo "one_bit_network_conf: ok" || echo "one_bit_network_conf: FAILED" +exit $fail