Skip to content

A hardened Linux desktop, rebuilt from scratch, with your Windows apps built in. Free and open source.

5.6 KB
Raw Blame
#!/bin/sh
# Applies the protection level chosen in Settings > Privacy and security.
# Runs as root through pkexec (org.wildos.settings.policy), and at start-up
# from wildos-protection.service to load the firewall again. Takes only the
# requests below.
#
#   standard   What Wild OS ships: the hardened system, with a firewall
#              that turns away anything arriving unasked and lets
#              everything this computer starts go out.
#   strict     Standard, plus USB devices found at start-up wait for
#              permission like ones plugged in later.
#   maximum    Strict, plus the firewall blocks network discovery (finding
#              printers and screens), Bluetooth is off and the camera's
#              driver is blocked.
#              Strict and Maximum also switch on some of Advanced
#              protection's options (settings-advanced level).
#   boot      Loads the firewall for the saved level (the service's job).
#
# The screen lock times that go with each level belong to the person, not
# the system, so Settings sets those itself.

set -eu

LEVEL_FILE=/etc/wildos/protection-level
USBGUARD_CONF=/etc/usbguard/usbguard-daemon.conf
USBGUARD_RULES=/etc/usbguard/rules.d/50-wildos-protection.conf
CAMERA_BLOCK=/etc/modprobe.d/60-wildos-protection-camera.conf
FIREWALL=/usr/libexec/wildos/settings-firewall

saved_level() {
    level=$(cat "$LEVEL_FILE" 2>/dev/null || true)
    case "$level" in strict | maximum) echo "$level" ;; *) echo standard ;; esac
}

# The firewall's rules come from settings-firewall, which builds them from
# the level saved here and the choices on Settings > Privacy and security >
# Firewall. Maximum keeps network discovery off whatever is chosen there.
firewall() {
    [ -x "$FIREWALL" ] || return 0
    "$FIREWALL" apply || true
}

# Every device attached when Strict is chosen is allowed first, so nothing
# the person is using stops working; after that, a device that is present
# at start-up but was never allowed waits for permission.
usb() {
    [ -f "$USBGUARD_CONF" ] || return 0
    if [ "$1" = standard ]; then
        sed -i 's/^PresentDevicePolicy=.*/PresentDevicePolicy=allow/' "$USBGUARD_CONF"
        rm -f "$USBGUARD_RULES"
    else
        if [ ! -s "$USBGUARD_RULES" ] && command -v usbguard >/dev/null; then
            mkdir -p "$(dirname "$USBGUARD_RULES")"
            usbguard generate-policy --no-hashes > "$USBGUARD_RULES.new" 2>/dev/null || : > "$USBGUARD_RULES.new"
            chmod 600 "$USBGUARD_RULES.new"
            mv -f "$USBGUARD_RULES.new" "$USBGUARD_RULES"
        fi
        sed -i 's/^PresentDevicePolicy=.*/PresentDevicePolicy=apply-policy/' "$USBGUARD_CONF"
    fi
    systemctl try-restart usbguard.service || true
}

# Bluetooth and the camera are only touched going into or out of Maximum,
# so a choice made elsewhere (Bluetooth off in quick settings) is left
# alone.
bluetooth() {
    if [ "$1" = maximum ]; then
        systemctl disable --now bluetooth.service >/dev/null 2>&1 || true
        rfkill block bluetooth 2>/dev/null || true
    elif [ "$2" = maximum ]; then
        rfkill unblock bluetooth 2>/dev/null || true
        systemctl enable --now bluetooth.service >/dev/null 2>&1 || true
    fi
}

camera() {
    if [ "$1" = maximum ]; then
        printf '%s\n' '# Written by Wild OS Settings: Maximum protection blocks the camera.' \
            'install uvcvideo /bin/false' > "$CAMERA_BLOCK"
        modprobe -r uvcvideo 2>/dev/null || true
    elif [ -f "$CAMERA_BLOCK" ]; then
        rm -f "$CAMERA_BLOCK"
        # The camera works again without a restart.
        modprobe uvcvideo 2>/dev/null || true
    fi
}

case "${1:-}" in
    standard | strict | maximum)
        previous=$(saved_level)
        mkdir -p "$(dirname "$LEVEL_FILE")"
        if [ "$1" = standard ]; then
            rm -f "$LEVEL_FILE"
        else
            echo "$1" > "$LEVEL_FILE"
        fi
        firewall
        # For Settings > System > History.
        if [ "$previous" != "$1" ] && command -v logger >/dev/null 2>&1; then
            logger -t settings-protection "HISTORY {\"what\": \"level\", \"level\": \"$1\", \"from\": \"$previous\", \"by\": ${PKEXEC_UID:-0}}" || true
        fi
        usb "$1"
        bluetooth "$1" "$previous"
        camera "$1"
        # Advanced protection: the level switches some of its options on
        # (settings-advanced has which), never one the person set by hand.
        if [ -x /usr/libexec/wildos/settings-advanced ]; then
            /usr/libexec/wildos/settings-advanced level "$1" || true
        fi
        # Strict and Maximum keep the kernel's 32-bit layer out, so 32-bit
        # apps go off from the next start-up. Every processor flaw
        # protection and the thread choice need nothing here: 40_wildos.cfg
        # reads the level, so going back to Standard brings back the
        # person's own choices.
        if [ "$1" != standard ] && [ -x /usr/libexec/wildos/settings-32bit ]; then
            /usr/libexec/wildos/settings-32bit off || true
        fi
        # Standard relaxes a few of Kicksecure's start-up options for speed
        # (/etc/default/grub.d/40_wildos.cfg reads the saved level), so
        # crossing between Standard and the stricter levels rebuilds the
        # start-up menu. It takes effect from the next start-up.
        case "$previous:$1" in
            standard:strict | standard:maximum | strict:standard | maximum:standard)
                if command -v update-grub >/dev/null 2>&1; then
                    update-grub >/dev/null 2>&1 || true
                fi
                ;;
        esac
        ;;
    boot)
        firewall
        ;;
    *)
        echo "usage: settings-protection standard|strict|maximum|boot" >&2
        exit 2
        ;;
esac