#!/usr/bin/env bash set -Eeuo pipefail # Fikakassan kiosk setup # Debian 13 / X11 / Openbox / Chromium / NetworkManager # # Run as root: # sudo ./setup-kiosk.sh # # The script: # - installs the kiosk stack and Python runtime # - uses the existing Debian user as the kiosk user # - installs the Fikakassan app from the directory containing this script # - starts FastAPI automatically at boot # - configures Wi-Fi through NetworkManager # - autologins the kiosk user # - starts Chromium in true kiosk mode # - disables suspend/blanking and common escape routes # - blocks ordinary Internet traffic over the Wi-Fi hotspot while allowing # full Internet access over wired Ethernet when a cable is connected # - keeps Wi-Fi and Ethernet connected simultaneously # # Before running, put this script in the root of the Fikakassan project. KIOSK_USER="${KIOSK_USER:-joakim}" APP_DIR="${APP_DIR:-/opt/fikakassa}" KIOSK_URL="${KIOSK_URL:-http://127.0.0.1:8000/}" WIFI_CONNECTION="${WIFI_CONNECTION:-Fikakassan-WiFi}" WIFI_SSID="${WIFI_SSID:-}" WIFI_PASSWORD="${WIFI_PASSWORD:-}" # Set to 0 if you explicitly want ordinary Internet access from the kiosk. LOCK_DOWN_INTERNET="${LOCK_DOWN_INTERNET:-1}" log() { printf '\n\033[1;32m==> %s\033[0m\n' "$*"; } warn() { printf '\n\033[1;33mWARNING: %s\033[0m\n' "$*" >&2; } die() { printf '\n\033[1;31mERROR: %s\033[0m\n' "$*" >&2; exit 1; } [[ $EUID -eq 0 ]] || die "Kör skriptet som root eller med sudo." command -v apt-get >/dev/null || die "Det här skriptet är avsett för Debian/derivat med apt." SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" [[ -f "$SCRIPT_DIR/app/main.py" ]] || die "app/main.py saknas. Kör skriptet från Fikakassans projektkatalog." [[ -f "$SCRIPT_DIR/requirements.txt" ]] || die "requirements.txt saknas." if [[ -z "$WIFI_SSID" ]]; then read -r -p "Wi-Fi SSID för Samsung A50-hotspot: " WIFI_SSID fi if [[ -z "$WIFI_PASSWORD" ]]; then read -r -s -p "Wi-Fi-lösenord: " WIFI_PASSWORD echo fi log "Installerar systempaket" export DEBIAN_FRONTEND=noninteractive apt-get update apt-get install -y \ network-manager \ openssh-server \ python3 \ python3-venv \ python3-pip \ xorg \ xinit \ openbox \ chromium \ unclutter \ x11-xserver-utils \ fonts-dejavu \ fonts-liberation \ nftables \ rsync systemctl enable NetworkManager.service systemctl enable ssh.service systemctl enable nftables.service log "Kontrollerar kiosk-användare" if ! id "$KIOSK_USER" >/dev/null 2>&1; then die "Användaren '$KIOSK_USER' finns inte. Skapa användaren först eller ange KIOSK_USER=..." fi # Use the existing Debian user for the kiosk session. Do not alter its # existing group memberships or sudo permissions. usermod -s /bin/bash "$KIOSK_USER" log "Installerar Fikakassan i $APP_DIR" install -d -o root -g root "$APP_DIR" rsync -a --delete \ --exclude='.venv/' \ --exclude='__pycache__/' \ --exclude='*.pyc' \ "$SCRIPT_DIR/" "$APP_DIR/" # The kiosk user needs to read the application, but not modify it. chown -R root:root "$APP_DIR" find "$APP_DIR" -type d -exec chmod 755 {} + find "$APP_DIR" -type f -exec chmod 644 {} + log "Skapar Python virtualenv" rm -rf "$APP_DIR/.venv" python3 -m venv "$APP_DIR/.venv" # Do not execute the pip wrapper directly. Some Debian filesystems/policies # can leave the wrapper without execute permission. Running pip through the # virtualenv Python avoids that failure. "$APP_DIR/.venv/bin/python" -m pip install \ --disable-pip-version-check \ --no-cache-dir \ -r "$APP_DIR/requirements.txt" log "Skapar systemd-tjänst för Fikakassan" cat > /etc/systemd/system/fikakassa.service </dev/null 2>&1 || true nmcli connection add type wifi \ ifname "$WIFI_IF" \ con-name "$WIFI_CONNECTION" \ ssid "$WIFI_SSID" \ wifi-sec.key-mgmt wpa-psk \ wifi-sec.psk "$WIFI_PASSWORD" \ ipv4.method auto \ ipv4.never-default yes \ ipv4.route-metric 600 \ ipv6.method disabled nmcli connection modify "$WIFI_CONNECTION" \ connection.autoconnect yes \ connection.autoconnect-retries 0 \ connection.autoconnect-priority 100 \ connection.mdns 0 \ ipv4.never-default yes \ ipv4.route-metric 600 \ ipv6.method disabled \ ipv6.never-default yes # Try immediately, but do not abort the installation if the A50 hotspot is # currently off or temporarily not visible. if nmcli connection up "$WIFI_CONNECTION"; then log "Wi-Fi anslutet till $WIFI_SSID" else warn "Wi-Fi-hotspotten '$WIFI_SSID' är inte tillgänglig just nu. Profilen är skapad och kommer att återansluta automatiskt när hotspotten startas." fi # Wired Ethernet is the maintenance/Internet path. Ensure every currently # known Ethernet profile prefers a normal DHCP default route with a low metric. while IFS=: read -r CON_NAME CON_TYPE; do [[ "$CON_TYPE" == "802-3-ethernet" ]] || continue nmcli connection modify "$CON_NAME" \ ipv4.never-default no \ ipv4.route-metric 100 \ ipv6.never-default no \ connection.autoconnect yes || true done < <(nmcli -t -f NAME,TYPE connection show) # Create a DHCP Ethernet profile for each physical Ethernet interface that # currently has no suitable profile. This makes plugging in a cable later # reliably provide the maintenance/Internet path. while read -r ETH_IF; do [[ -n "$ETH_IF" ]] || continue PROFILE="Fikakassa-Ethernet-$ETH_IF" if ! nmcli -t -f NAME connection show | grep -Fxq "$PROFILE"; then nmcli connection add type ethernet \ ifname "$ETH_IF" \ con-name "$PROFILE" \ ipv4.method auto \ ipv4.never-default no \ ipv4.route-metric 100 \ ipv6.method auto \ ipv6.never-default no \ connection.autoconnect yes >/dev/null fi done < <(nmcli -t -f DEVICE,TYPE device status | awk -F: '$2 == "ethernet" {print $1}') log "Konfigurerar lokal brandvägg" if [[ "$LOCK_DOWN_INTERNET" == "1" ]]; then # The kiosk needs: # - local LAN traffic to the A50/kiosk # - DNS for NTP hostname resolution # - UDP/123 for NTP # - DHCP # - established traffic # # It must NOT be able to browse the Internet through the phone. # A wired Ethernet connection (interface names en*) is intentionally # exempted from the outbound lockdown, so plugging in a cable gives # normal Internet access for updates and development while Wi-Fi remains # available for the A50/local kiosk network. # # All RFC1918 traffic is permitted so the Android bridge can talk to the # kiosk even if Samsung changes the hotspot subnet. cat > /etc/nftables.conf <<'EOF' #!/usr/sbin/nft -f flush ruleset table inet kiosk_filter { chain input { type filter hook input priority filter; policy drop; iifname "lo" accept ct state established,related accept # ICMP is useful for diagnostics and does not provide Internet browsing. ip protocol icmp accept # DHCP client traffic. udp sport 67 udp dport 68 accept # SSH and Fikakassan are available from the local network only. ip saddr 10.0.0.0/8 tcp dport 22 accept ip saddr 172.16.0.0/12 tcp dport 22 accept ip saddr 192.168.0.0/16 tcp dport 22 accept ip saddr 10.0.0.0/8 tcp dport 8000 accept ip saddr 172.16.0.0/12 tcp dport 8000 accept ip saddr 192.168.0.0/16 tcp dport 8000 accept } chain forward { type filter hook forward priority filter; policy drop; } chain output { type filter hook output priority filter; policy drop; oifname "lo" accept ct state established,related accept # DHCP client. udp sport 68 udp dport 67 accept # A wired Ethernet connection is the maintenance/Internet path. # Allow normal outbound traffic there; Wi-Fi remains locked to local # networks plus DNS/NTP. oifname "en*" accept # DNS and NTP are explicitly allowed. udp dport 53 accept tcp dport 53 accept udp dport 123 accept # Permit local/private networks only. ip daddr 10.0.0.0/8 accept ip daddr 172.16.0.0/12 accept ip daddr 192.168.0.0/16 accept # ICMP for local diagnostics. ip daddr 10.0.0.0/8 ip protocol icmp accept ip daddr 172.16.0.0/12 ip protocol icmp accept ip daddr 192.168.0.0/16 ip protocol icmp accept } } EOF nft -f /etc/nftables.conf else warn "Internet-lockdown är avstängt (LOCK_DOWN_INTERNET=0)." fi log "Stänger av automatisk sleep/blanking" mkdir -p /etc/systemd/logind.conf.d cat > /etc/systemd/logind.conf.d/90-fikakassa.conf <<'EOF' [Login] HandlePowerKey=ignore HandleSuspendKey=ignore HandleHibernateKey=ignore HandleLidSwitch=ignore HandleLidSwitchExternalPower=ignore IdleAction=ignore EOF systemctl restart systemd-logind.service || true log "Konfigurerar X11/Openbox" KIOSK_HOME="/home/$KIOSK_USER" install -d -o "$KIOSK_USER" -g "$KIOSK_USER" \ "$KIOSK_HOME/.config/openbox" \ "$KIOSK_HOME/.config" cat > "$KIOSK_HOME/.xinitrc" <<'EOF' #!/bin/sh exec openbox-session EOF chown "$KIOSK_USER:$KIOSK_USER" "$KIOSK_HOME/.xinitrc" chmod 755 "$KIOSK_HOME/.xinitrc" # A small explicit launcher makes the kiosk session independent of shell # startup quirks. cat > /usr/local/bin/fikakassa-startx < "$KIOSK_HOME/.config/openbox/autostart" < /etc/systemd/system/getty@tty1.service.d/override.conf < "$KIOSK_HOME/.bash_profile" <<'EOF' # Start graphical kiosk session automatically on tty1. if [ -z "$DISPLAY" ] && [ "$(tty 2>/dev/null)" = "/dev/tty1" ]; then exec /usr/local/bin/fikakassa-startx fi EOF chown "$KIOSK_USER:$KIOSK_USER" "$KIOSK_HOME/.bash_profile" chmod 644 "$KIOSK_HOME/.bash_profile" systemctl daemon-reload systemctl enable getty@tty1.service # Validate the files before restarting tty1. This is intentionally fatal: a # kiosk without its graphical startup should not silently fall back to tty. test -x "$KIOSK_HOME/.xinitrc" test -x "$KIOSK_HOME/.config/openbox/autostart" test -f "$KIOSK_HOME/.bash_profile" test -x /usr/local/bin/fikakassa-startx # Restarting getty will log the current terminal user out. This is expected # when the script is run interactively on tty1. log "Sätter systemtid/NTP" # systemd-timesyncd is enough; no Internet browsing is required. apt-get install -y systemd-timesyncd systemctl enable --now systemd-timesyncd.service || true log "Aktiverar automatisk omstart av Chromium om X-sessionen kraschar" # Xorg/startx returns to tty1 if Chromium/Openbox dies. systemd getty will # respawn the login shell, which starts X again through .bash_profile. cat < Xorg -> Openbox -> Chromium Internet-lockdown: $([[ "$LOCK_DOWN_INTERNET" == "1" ]] && echo "AKTIV: endast lokalt nät + DNS/NTP" || echo "AVSTÄNGD") Starta om maskinen nu: reboot OBS: "Power on efter strömavbrott" är en BIOS/UEFI-inställning och kan inte garanteras av Linux. På X1 Yoga behöver "Power on with AC attach" / motsvarande BIOS-inställning aktiveras om maskinen ska starta automatiskt efter ett strömavbrott. EOF