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

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