usbguard-rofi.sh
· 1.9 KiB · Bash
Raw
#!/bin/sh
# Copyright Julián Marcos — EPLv2
# Texts
RELIST_ACT="*AA: Listar de nuevo. (Re-chequea los estados)"
EXIT_ACT="*AB: Salir"
FILTER_ACT="*AC: Filtrar"
BLOCK_ACT="Bloquear"
REJECT_ACT="Quitar"
ALLOW_ACT="Permitir"
EXIT="Salir"
YES="Sí"
NO="No"
FILTER_BASED_ON="Filtrar basado en"
FILTER_DENIED="Denegados"
FILTER_ALLOWED="Permitidos"
CHOOSE="Elegir"
CHOOSE_DEVICE="$CHOOSE dispositivo"
CHOOSE_ACTION="$CHOOSE acción"
APPLY_PERMANENTLY="¿Aplicar permanentemente?"
NO_DEVICE_CHOSEN="No has elegido ningún dispositivo"
NO_ACTION_CHOSEN="No has elegido ninguna acción"
INVALID_ACTION="Acción invalida"
# Options
PERMANENT_TOGGLE="0"
SCRIPT_NAME="$(basename "$0")"
usbguard() {
#shellcheck disable=SC2068
doas /usr/bin/usbguard $@
}
notify_exit() {
notify-send "$SCRIPT_NAME" "$1"
exit 1
}
filtrar_opt() {
based_on="$(rofi -dmenu -p "$FILTER_BASED_ON"<<EoF
$FILTER_DENIED
$FILTER_ALLOWED
EoF
)"
case "$based_on" in
"$FILTER_ALLOWED") based_on="allow";;
"$FILTER_DENIED") based_on="block";;
esac
exec "$0" "$based_on"
}
chosen="$(rofi -dmenu -p "$CHOOSE_DEVICE"<<EoF
$(usbguard list-devices|grep "$1")
$RELIST_ACT
$EXIT_ACT
$FILTER_ACT
EoF
)"
case "$chosen" in
"") notify_exit "$NO_DEVICE_CHOSEN";;
"$EXIT_ACT") exit;;
"$EXIT") exit;;
"$FILTER_ACT") filtrar_opt;;
"$RELIST_ACT") exec $0 "$1";;
esac
id="$(echo "$chosen" | cut -d: -f1)"
action="$(rofi -dmenu -p "$CHOOSE_ACTION" <<EoF
$ALLOW_ACT
$BLOCK_ACT
$REJECT_ACT
$EXIT
EoF
)"
case "$action" in
"") notify_exit "$NO_ACTION_CHOSEN";;
"$ALLOW_ACT") CMD="allow-device";;
"$BLOCK_ACT") CMD="block-device";;
"$EXIT") exit;;
"$REJECT_ACT") CMD="reject-device";;
*) notify_exit_invalid_param "$INVALID_ACTION: $action";;
esac
permanent_opt="--"
[ "$PERMANENT_TOGGLE" = "1" ] && {
permanent="$(rofi -dmenu -p "$APPLY_PERMANENTLY" <<EoF
$NO
$YES
$EXIT
EoF
)"
case "$permanent" in
"$YES") permanent_opt="-p";;
"$EXIT") exit;;
*) permanent_opt="--";;
esac
}
usbguard "$CMD" "$permanent_opt" "$id"
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" |