aboutsummaryrefslogtreecommitdiff
path: root/share/hydractl/usb-disable
blob: 8c7d4a3d6b9c17c51c6d3b209bf61455fd2c9ca3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
#
# USB hotplug switcher.
# See https://links.fluxo.info/tags/badusb
#     https://www.kernel.org/doc/Documentation/usb/authorization.txt
#     http://marc.info/?l=git-commits-head&m=131166366113680&w=2
#     https://github.com/dkopecek/usbguard (alternative approach)

# Load.
source $APP_BASE/lib/hydra/functions || exit 1
hydra_config_load

# Parameters
BASENAME="`basename $0`"

# Sudo config
if [ "`whoami`" != 'root' ]; then
  SUDO="sudo"
fi

# Set hotplug state
function usb_set_state {
  echo "Applying at /sys/module/usbcore/parameters/authorized_default..."
  $SUDO su -c "echo $1 > /sys/module/usbcore/parameters/authorized_default"

  for bus in /sys/bus/usb/devices/usb*; do
    echo "Applying at ${bus}/authorized_default..."
    $SUDO su -c "echo $1 > ${bus}/authorized_default"
  done
}

# Dispatch
if [ "$BASENAME" == 'usb-enable' ]; then
  usb_set_state 1
elif [ "$BASENAME" == 'usb-disable' ]; then
  usb_set_state 0
elif [ "$BASENAME" == 'usb-status' ] || [ "$BASENAME" == 'usb-toggle' ]; then
  status="`cat /sys/module/usbcore/parameters/authorized_default`"

  # The "authorized_default" module parameter of usbcore controls the default
  # for the authorized_default variable of each USB host controller.
  #
  # -1 is authorized for all devices except wireless (default, old behaviour)
  # 0 is unauthorized for all devices
  # 1 is authorized for all devices
  if [ "$status" == "0" ]; then
    if [ "$BASENAME" == 'usb-toggle' ]; then
      echo "Hotplug was disabled, enabling it..."
      usb_set_state 1
    else
      echo "Hotplug disabled."
    fi
  elif [ "$status" == "1" ]; then
    if [ "$BASENAME" == 'usb-toggle' ]; then
      echo "Hotplug was enabled, disabling it..."
      usb_set_state 0
    else
      echo "Hotplug enabled."
    fi
  elif [ "$status" == "-1" ]; then
    if [ "$BASENAME" == 'usb-toggle' ]; then
      echo "Hotplug was enabled, disabling it..."
      usb_set_state 0
    else
      echo "Hotplug enabled except wireless"
    fi
  fi
fi