Last active 1699795081

Usbguard rofi script so that I can manage usbguard from a UI instead of manually typing the usbguard commands into the keyboard

jmjl's Avatar jmjl revised this gist 1699795048. Go to revision

1 file changed, 104 insertions

usbguard-rofi.sh(file created)

@@ -0,0 +1,104 @@
1 + #!/bin/sh
2 + # Copyright Julián Marcos — EPLv2
3 + # Texts
4 +
5 + RELIST_ACT="*AA: Listar de nuevo. (Re-chequea los estados)"
6 + EXIT_ACT="*AB: Salir"
7 + FILTER_ACT="*AC: Filtrar"
8 +
9 + BLOCK_ACT="Bloquear"
10 + REJECT_ACT="Quitar"
11 + ALLOW_ACT="Permitir"
12 +
13 + EXIT="Salir"
14 +
15 + YES="Sí"
16 + NO="No"
17 +
18 + FILTER_BASED_ON="Filtrar basado en"
19 + FILTER_DENIED="Denegados"
20 + FILTER_ALLOWED="Permitidos"
21 + CHOOSE="Elegir"
22 + CHOOSE_DEVICE="$CHOOSE dispositivo"
23 + CHOOSE_ACTION="$CHOOSE acción"
24 + APPLY_PERMANENTLY="¿Aplicar permanentemente?"
25 +
26 + NO_DEVICE_CHOSEN="No has elegido ningún dispositivo"
27 + NO_ACTION_CHOSEN="No has elegido ninguna acción"
28 + INVALID_ACTION="Acción invalida"
29 +
30 + # Options
31 + PERMANENT_TOGGLE="0"
32 + SCRIPT_NAME="$(basename "$0")"
33 +
34 +
35 + usbguard() {
36 + #shellcheck disable=SC2068
37 + doas /usr/bin/usbguard $@
38 + }
39 + notify_exit() {
40 + notify-send "$SCRIPT_NAME" "$1"
41 + exit 1
42 + }
43 +
44 + filtrar_opt() {
45 + based_on="$(rofi -dmenu -p "$FILTER_BASED_ON"<<EoF
46 + $FILTER_DENIED
47 + $FILTER_ALLOWED
48 + EoF
49 + )"
50 + case "$based_on" in
51 + "$FILTER_ALLOWED") based_on="allow";;
52 + "$FILTER_DENIED") based_on="block";;
53 + esac
54 + exec "$0" "$based_on"
55 + }
56 +
57 + chosen="$(rofi -dmenu -p "$CHOOSE_DEVICE"<<EoF
58 + $(usbguard list-devices|grep "$1")
59 + $RELIST_ACT
60 + $EXIT_ACT
61 + $FILTER_ACT
62 + EoF
63 + )"
64 + case "$chosen" in
65 + "") notify_exit "$NO_DEVICE_CHOSEN";;
66 + "$EXIT_ACT") exit;;
67 + "$EXIT") exit;;
68 + "$FILTER_ACT") filtrar_opt;;
69 + "$RELIST_ACT") exec $0 "$1";;
70 + esac
71 + id="$(echo "$chosen" | cut -d: -f1)"
72 +
73 + action="$(rofi -dmenu -p "$CHOOSE_ACTION" <<EoF
74 + $ALLOW_ACT
75 + $BLOCK_ACT
76 + $REJECT_ACT
77 + $EXIT
78 + EoF
79 + )"
80 + case "$action" in
81 + "") notify_exit "$NO_ACTION_CHOSEN";;
82 + "$ALLOW_ACT") CMD="allow-device";;
83 + "$BLOCK_ACT") CMD="block-device";;
84 + "$EXIT") exit;;
85 + "$REJECT_ACT") CMD="reject-device";;
86 + *) notify_exit_invalid_param "$INVALID_ACTION: $action";;
87 + esac
88 +
89 + permanent_opt="--"
90 + [ "$PERMANENT_TOGGLE" = "1" ] && {
91 + permanent="$(rofi -dmenu -p "$APPLY_PERMANENTLY" <<EoF
92 + $NO
93 + $YES
94 + $EXIT
95 + EoF
96 + )"
97 + case "$permanent" in
98 + "$YES") permanent_opt="-p";;
99 + "$EXIT") exit;;
100 + *) permanent_opt="--";;
101 + esac
102 + }
103 +
104 + usbguard "$CMD" "$permanent_opt" "$id"
Newer Older