#!/usr/bin/env python3 """Host guard for real Bluetooth HID connections. BlueZ rejects LE-only profiles in BR/EDR-only controller mode, which makes BLE keyboards and mice discoverable but unusable. This test guards the dual mode configuration, the HOGP userspace HID settings, and the pair/connect preflight added to bananarchy-bt-obex. """ import ast import pathlib import re BASE = pathlib.Path(__file__).resolve().parent.parent MAIN_CONF = BASE / "overlay/rootfs/etc/bluetooth/main.conf" INPUT_CONF = BASE / "overlay/rootfs/etc/bluetooth/input.conf" BT_OBEX = BASE / "overlay/rootfs/usr/bin/bananarchy-bt-obex" SCAN_HELPER = BASE / "overlay/rootfs/usr/bin/bananarchy-bt-scan.sh" LE_HELPER = BASE / "overlay/rootfs/usr/bin/bananarchy-bt-le-enable.sh" DOAS_RULE = BASE / "overlay/rootfs/etc/doas.d/60-bananarchy-bluetooth.conf" APPLY_OVERLAY = BASE / "scripts/apply-bananarchy-overlay.sh" def read(path): return path.read_text(encoding="utf-8") def require(condition, message): if not condition: raise SystemExit(message) def main(): for path in (MAIN_CONF, INPUT_CONF, BT_OBEX, SCAN_HELPER, LE_HELPER, DOAS_RULE, APPLY_OVERLAY): require(path.is_file(), "missing Bluetooth connection file: %s" % path) require(BT_OBEX.stat().st_mode & 0o111, "bananarchy-bt-obex is not executable") main_conf = read(MAIN_CONF) modes = re.findall(r"(?m)^\s*ControllerMode\s*=\s*(\S+)\s*$", main_conf) require(modes == ["dual"], "BlueZ controller mode must be exactly dual to register LE profiles") input_conf = read(INPUT_CONF) for token in ("UserspaceHID=true", "LEAutoSecurity=true", "ClassicBondedOnly=false", "IdleTimeout=0"): require(token in input_conf, "input.conf is missing the HOGP setting %s" % token) helper_source = read(BT_OBEX) tree = ast.parse(helper_source, filename=str(BT_OBEX)) functions = { node.name for node in tree.body if isinstance(node, ast.FunctionDef) } for name in ("prepare_bt_controller", "set_hog_preferred_bearer", "connect_or_pair_bt_device"): require(name in functions, "bananarchy-bt-obex is missing %s()" % name) for token in ("prepare_bt_controller()", '["timeout", "3", "bluetoothctl", "power", "on"]', '"/usr/bin/bananarchy-bt-le-enable.sh"', '["doas", "-n", helper]', '"00001812-"', '"org.bluez.Device1", "PreferredBearer"', '"s", "le"', "set_hog_preferred_bearer(nonlocal_conn[0], dpath, p)", "if not do_pair and orig_unbonded"): require(token in helper_source, "pair/connect helper is missing %s" % token) require('["pkill", "-f"' not in helper_source, "bananarchy-bt-obex still depends on the missing process killer") require("/proc/" in helper_source, "daemon stop path no longer scans /proc for stale processes") require("bananarchy-bt-le-enable.sh" in read(SCAN_HELPER), "scan helper no longer enables LE") require("btmgmt -i hci0 le on" in read(LE_HELPER), "LE helper no longer enables the LE setting") require("bananarchy-bt-le-enable.sh" in read(DOAS_RULE), "doas rule no longer permits the narrow LE helper") require('cp -af "$BASE_DIR/overlay/rootfs/." "$TARGET_ROOTFS/"' in read(APPLY_OVERLAY), "overlay application no longer installs etc/bluetooth configs") print("[ok] Bluetooth HID profile configuration") # Mirror BlueZ 5.87 profile.c: an LE bearer is rejected in bredr mode. def profile_can_register(mode, bearer): if bearer == "le" and mode == "bredr": return False if bearer == "bredr" and mode == "le": return False return True require(profile_can_register(modes[0], "le"), "dual mode would reject the HOG profile") require(profile_can_register(modes[0], "bredr"), "dual mode would reject classic audio profiles") require(not profile_can_register("bredr", "le"), "the old bredr mode should reject the HOG profile") print("[ok] BlueZ dual-mode profile gate") if __name__ == "__main__": main()