-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathttoggle
More file actions
executable file
·180 lines (157 loc) · 4.23 KB
/
Copy pathttoggle
File metadata and controls
executable file
·180 lines (157 loc) · 4.23 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/sh
UNITFILE="/etc/nmtrust/trusted_units"
###############################################################################
usage() {
echo "Usage: ttrust [OPTION...]
Toggle the activation of certain systemd units based on the trust of the current network connections.
Options:
-f specify an alternative location for the trusted unit file
-s display the status of the trusted units and exit
-x stop all trusted units, regardless of network trust
-t start all trusted units, regardless of network trust
-q be quiet"
}
file_check() {
if [ ! -f "$UNITFILE" ]; then
if [ "$quiet" != true ]; then
echo "Could not locate trusted unit file: $UNITFILE"
fi
exit 1
fi
}
find_nmtrust() {
if hash nmtrust 2> /dev/null; then
NMTRUST=nmtrust
else
echo "Could not find nmtrust"
exit 127
fi
}
extract_user() {
echo "$1" | sed 's/.*user:\([^,]*\).*/\1/'
}
get_trusted_units() {
TRUSTED_SYSTEM_UNITS=$(grep -v '^#\|,.*user:' "$UNITFILE" | cut -d ',' -f1)
OFFLINE_SYSTEM_UNITS=$(grep -v '^#\|,.*user:' "$UNITFILE" | grep ',.*allow_offline' | cut -d ',' -f1)
TRUSTED_USER_UNITS=$(grep -v '^#' "$UNITFILE" | grep ',.*user:')
OFFLINE_USER_UNITS=$(grep -v '^#' "$UNITFILE" | grep ',.*user:' | grep ',.*allow_offline')
}
user_toggle() {
unit_user=$(extract_user "$2")
unit=$(echo "$2" | cut -d ',' -f1)
if [ "$1" = "status" ]; then
command="SYSTEMD_COLORS=1 systemctl $1 --user $unit | sed '1p;/^\s*Active:/!d'"
else
command="systemctl $1 --user $unit"
fi
if [ "$unit_user" = "$USER" ]; then
eval "$command"
else
sudo -u "$unit_user" bash -c "export XDG_RUNTIME_DIR=/run/user/$(id -u "$unit_user"); $command"
fi
}
# Run systemctl $1 against the unit list $2, announcing it with the message $3.
system_units() {
if [ -n "$2" ]; then
if [ -n "$3" ] && [ "$quiet" != true ]; then
echo "$3"
fi
# $2 is a list split by whitespace.
systemctl "$1" $2
fi
}
# Run systemctl $1 against each user unit in $2, announcing it with the message $3.
user_units() {
if [ -n "$2" ]; then
if [ -n "$3" ] && [ "$quiet" != true ]; then
echo "$3"
fi
echo "$2" | while read -r line; do
user_toggle "$1" "$line"
done
fi
}
start() {
system_units start "$TRUSTED_SYSTEM_UNITS" "Starting trusted system units"
user_units start "$TRUSTED_USER_UNITS" "Starting trusted user units"
}
stop() {
system_units stop "$TRUSTED_SYSTEM_UNITS" "Stopping trusted system units"
user_units stop "$TRUSTED_USER_UNITS" "Stopping trusted user units"
}
start_offline() {
stop
system_units start "$OFFLINE_SYSTEM_UNITS" "Starting trusted system offline units"
user_units start "$OFFLINE_USER_UNITS" "Starting trusted user offline units"
}
status() {
echo "Systemd system units:"
for unit in $TRUSTED_SYSTEM_UNITS
do
SYSTEMD_COLORS=1 systemctl status "$unit" | sed '1p;/^\s*Active:/!d'
done
echo "Systemd user units:"
user_units status "$TRUSTED_USER_UNITS"
}
while getopts ":f:sxtqh" opt; do
case $opt in
f)
UNITFILE=$OPTARG
;;
q)
quiet=true
;;
s)
status=true
;;
x)
stopall=true
;;
t)
startall=true
;;
:)
echo "Option -$OPTARG requires an argument."
usage
exit 1
;;
h | *)
usage
exit
;;
esac
done
# Check if the trusted unit file exists.
file_check
# Get the trusted units
get_trusted_units
# If the status was requested, display it.
if [ "$status" = true ]; then
status
exit $?
fi
# If stopping everything was requested, do it.
if [ "$stopall" = true ]; then
stop
exit $?
fi
# If starting everything was requested, do it.
if [ "$startall" = true ]; then
start
exit $?
fi
# Execute nmtrust.
find_nmtrust
$NMTRUST
result=$?
# Toggle the units as appropriate.
if [ $result -eq 0 ]; then
start
exit $?
elif [ $result -eq 4 ]; then
start_offline
exit $?
else
stop
exit $?
fi