|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +#================================================================ |
| 4 | +# HEADER |
| 5 | +#================================================================ |
| 6 | +#% SYNOPSIS |
| 7 | +#+ lockdown_usb.sh [ENFORCE] |
| 8 | +#% |
| 9 | +#% DESCRIPTION |
| 10 | +#% This script installs a system service that shuts down and disables the |
| 11 | +#% user session whenever an action is detected on a USB port, and configures |
| 12 | +#% udev to forward all USB events to this service. |
| 13 | +#% |
| 14 | +#% Logins are disabled with the nologin(5) mechanism. By default, Ubuntu |
| 15 | +#% 20.04 clears this file whenever the system is restarted. |
| 16 | +#% |
| 17 | +#% It takes one optional parameter: whether or not to enforce this policy. |
| 18 | +#% If this parameter is missing, empty, "false", "falsk", "no" or "nej" |
| 19 | +#% (not case-sensitive), the policy will be removed; otherwise, it will be |
| 20 | +#% enforced. |
| 21 | +#% |
| 22 | +#================================================================ |
| 23 | +#- IMPLEMENTATION |
| 24 | +#- version lockdown_usb.sh (magenta.dk) 1.0.0 |
| 25 | +#- author Alexander Faithfull |
| 26 | +#- copyright Copyright 2021 Magenta ApS |
| 27 | +#- license GNU General Public License |
| 28 | + |
| 29 | +#- |
| 30 | +#================================================================ |
| 31 | +# HISTORY |
| 32 | +# 2021/04/12 : af : Script created |
| 33 | +# |
| 34 | +#================================================================ |
| 35 | +# END_OF_HEADER |
| 36 | +#================================================================ |
| 37 | + |
| 38 | +set -x |
| 39 | + |
| 40 | +lower() { |
| 41 | + echo "$@" | tr '[:upper:]' '[:lower:]' |
| 42 | +} |
| 43 | + |
| 44 | +activate="`lower "$1"`" |
| 45 | + |
| 46 | +if [ "$activate" != "" \ |
| 47 | + -a "$activate" != "false" -a "$activate" != "falsk" \ |
| 48 | + -a "$activate" != "no" -a "$activate" != "nej" ]; then |
| 49 | + mkdir -p /usr/local/lib/os2borgerpc |
| 50 | + |
| 51 | + cat <<"END" > /usr/local/lib/os2borgerpc/usb-monitor |
| 52 | +#!/usr/bin/env python3 |
| 53 | +
|
| 54 | +from os import mkfifo, unlink |
| 55 | +from os.path import exists |
| 56 | +import subprocess |
| 57 | +
|
| 58 | +PIPE = "/var/lib/os2borgerpc/usb-event" |
| 59 | +
|
| 60 | +
|
| 61 | +def lockdown(message): |
| 62 | + """Creates the /etc/nologin file with the specified message and shuts the |
| 63 | + user's session manager down, forcing a logout. |
| 64 | +
|
| 65 | + This function does nothing if /etc/nologin already exists.""" |
| 66 | + if not exists("/etc/nologin"): |
| 67 | + with open("/etc/nologin", "wt") as fp: |
| 68 | + fp.write(message) |
| 69 | + subprocess.run(["su", "-c", "systemctl --user exit 1", "alec"]) |
| 70 | +
|
| 71 | +
|
| 72 | +def main(): |
| 73 | + # Make sure we always start with a fresh FIFO |
| 74 | + try: |
| 75 | + unlink(PIPE) |
| 76 | + except FileNotFoundError: |
| 77 | + pass |
| 78 | +
|
| 79 | + mkfifo(PIPE) |
| 80 | + try: |
| 81 | + while True: |
| 82 | + with open(PIPE, "rt") as fp: |
| 83 | + # Reading from a FIFO should block until the udev helper script |
| 84 | + # gives us a signal. Lock the system immediately when that |
| 85 | + # happens |
| 86 | + content = fp.read() |
| 87 | + lockdown("Systemet er låst -- kontakt venligst personalet") |
| 88 | + finally: |
| 89 | + unlink(PIPE) |
| 90 | +
|
| 91 | +
|
| 92 | +if __name__ == "__main__": |
| 93 | + main() |
| 94 | +END |
| 95 | + chmod 700 /usr/local/lib/os2borgerpc/usb-monitor |
| 96 | + |
| 97 | + cat <<"END" > /etc/systemd/system/os2borgerpc-usb-monitor.service |
| 98 | +[Unit] |
| 99 | +Description=OS2borgerPC USB monitoring service |
| 100 | +
|
| 101 | +[Service] |
| 102 | +Type=simple |
| 103 | +ExecStart=/usr/local/lib/os2borgerpc/usb-monitor |
| 104 | +# It's important that we stop the Python process, stuck in a blocking read, |
| 105 | +# with SIGINT rather than SIGTERM so that its finaliser has a chance to run |
| 106 | +KillSignal=SIGINT |
| 107 | +
|
| 108 | +[Install] |
| 109 | +WantedBy=display-manager.service |
| 110 | +END |
| 111 | + systemctl enable --now os2borgerpc-usb-monitor.service |
| 112 | + |
| 113 | + cat <<"END" > /usr/local/lib/os2borgerpc/on-usb-event |
| 114 | +#!/bin/sh |
| 115 | +
|
| 116 | +if [ -p "/var/lib/os2borgerpc/usb-event" ]; then |
| 117 | + # Use dd with oflag=nonblock to make sure that we don't append to the pipe |
| 118 | + # if the reader isn't yet running |
| 119 | + echo "$@" | dd oflag=nonblock \ |
| 120 | + of=/var/lib/os2borgerpc/usb-event status=none |
| 121 | +fi |
| 122 | +END |
| 123 | + chmod 700 /usr/local/lib/os2borgerpc/on-usb-event |
| 124 | + |
| 125 | + cat <<"END" > /etc/udev/rules.d/99-os2borgerpc-usb-event.rules |
| 126 | +SUBSYSTEM=="usb", TEST=="/var/lib/os2borgerpc/usb-event", RUN{program}="/usr/local/lib/os2borgerpc/on-usb-event '%E{ACTION}' '$sys$devpath'" |
| 127 | +END |
| 128 | +else |
| 129 | + systemctl disable --now os2borgerpc-usb-monitor.service |
| 130 | + rm -f /usr/local/lib/os2borgerpc/on-usb-event \ |
| 131 | + /etc/udev/rules.d/99-os2borgerpc-usb-event.rules \ |
| 132 | + /usr/local/lib/os2borgerpc/usb-monitor \ |
| 133 | + /etc/systemd/system/os2borgerpc-usb-monitor.service |
| 134 | +fi |
| 135 | + |
| 136 | +udevadm control -R |
0 commit comments