forked from thedunston/bash_cli_zt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvancedZT.bash
324 lines (208 loc) · 7.02 KB
/
advancedZT.bash
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/bin/bash
# This is run on the self-hosted controller workstation because the settings only apply to this host
# Currently only allowManagementFrom and interfacePrefixBlacklist are supported
# To modify the local.conf file.
# The default directory where it should be found is the root of the zerotier directory
# which is "/var/lib/zerotier-one" directory
# eg: /var/lib/zerotier-one/local.conf
# Source: https://github.com/zerotier/ZeroTierOne/tree/master/service
source "functions.bash"
if [[ -f ${bkLocalConfig} ]]; then
rm -f ${bkLocalConfig}
fi
# Local controller ID
CONTROLLER_ID=$(zerotier-cli info | cut -d' ' -f 3)
function copyBkConfig() {
cp ${localConfig} ${bkLocalConfig}
}
function copyConfig() {
cp ${bkLocalConfig} ${localConfig}
}
function menuHeader() {
clear
echo "########################################################"
echo " ${1}"
echo "########################################################"
}
function advZT() {
clear
echo
echo "This menu allows editing the local.conf file which has options to modify"
echo "various settings provided by ZeroTier. More information is available here"
echo "# Source: https://github.com/zerotier/ZeroTierOne/tree/master/service"
echo "Currently only \"allowManagementFrom\" and \"interfacePrefixBlacklist\""
echo "are supported."
read -p "Press Enter when done."
menuHeader "Here you can edit the local.conf file."
echo ""
echo "1. Edit IPs/Nets that can manage this controller"
echo "2. Edit Interfaces to blacklist"
echo "[E]xit"
read -p "Select a numeric option to manage the local.conf file: " option
if ! [[ -f ${localConfig} ]]; then
# Copy the file template.
cp ${localConfigTemplate} ${localConfig}
fi
current_val=$(grep allowManagementFrom ${localConfig} | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2}')
case ${option} in
# Get the current value
1)
function manageChoice () {
# Check if the file is new
menuHeader "Manage IPs/Nets that can manage this controller"
echo "1. Enter a new IP/Network"
echo "2. Delete an IP/Network"
echo "[B]ack to main menu"
read -p "Please enter a numeric option: " mChoice
}
manageChoice
# Submenu case statement
case ${mChoice} in
1)
# Check the IP
echo ""
# Set the IPs allowed to manage this controller
function ipAllow() {
menuHeader "IPs and networks must contain masks. Separate each IP/Net with a space."
echo "eg. 192.168.0.1/32"
echo "eg. 192.168.1.1/32 192.168.1.2/32"
echo "eg. 192.168.0.1/32 192.168.2.0/24"
read -p "Please enter an IP or Network that can manage this controller or Enter to go back to the main menu: " theIP
if [[ "${theIP}" == "" ]]; then
advZT
fi
# Check the IPs/Nets entered
if [[ "${theIP}" =~ [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2} ]]; then
# Check the IPs/Nets provided
for eachIPNet in ${theIP}; do
ipnet_chk ${eachIPNet} ipAllow
done
else
ipAllow
fi
}
ipAllow
# Add the IPs
if [[ "${current_val}" == "" ]]; then
# Format the IP
ips=$(echo ${theIP} | sed -e 's/^/"/g;s/$/"/g;s/ /","/g;s/ //g;s/\//\\\//g')
# Copy current local.conf to backup config
copyBkConfig
# Make changes to the backup file
sed -i "/allowManagementFrom/s/\[\]/\[${ips}\]/g" ${bkLocalConfig}
cat ${bkLocalConfig} | jq
chk_jq "" ipAllow
# If in proper json format then replace the local.conf file.
copyConfig
rm ${bkLocalConfig}
systemctl restart zerotier-one
allDone "IP was added" ipAllow
else
# Concatenate the existing allowed IPs/Nets
ips=$(echo "${theIP} ${current_val}" | tr ' ' '\n' |sort -u | paste -sd ' ' - | sed -e 's/^/"/g;s/$/"/g;s/ /","/g;s/ //g;s/\//\\\//g')
# Copy current local.conf to backup config
copyBkConfig
# Add the new IP/Net
sed -i -e "/allowManagementFrom/s/\[.*\]/\[${ips}\]/g" ${bkLocalConfig}
cat ${bkLocalConfig} | jq
chk_jq ipAllow
# If in proper json format then replace the local.conf file.
copyConfig
rm ${bkLocalConfig}
systemctl restart zerotier-one
allDone "IP was added" ipAllow
fi
;;
2) # Delete the IPs/Nets
function delIPNet() {
menuHeader "IP and/or Networks to delete"
listLocalConf allowManagementFrom
# Replace spaces with pipe
delPatt=$(echo ${currSetting} | sed 's/ /|/g')
# Remove IPs/Nets
del=$(echo ${ENTITIES} | tr ' ' '\n' | egrep -v "${delPatt}" | sort -u | paste -sd ' ' -| sed -e 's/^/"/g;s/$/"/g;s/ /","/g;s/ //g;s/\//\\\//g')
copyBkConfig
if [[ "${del}" == "\"\"" ]]; then
sed -i -e "/allowManagementFrom/s/\[.*\]/\[\]/g" ${bkLocalConfig}
else
sed -i -e "/allowManagementFrom/s/\[.*\]/\[${del}\]/g" ${bkLocalConfig}
fi
# Check the proper json syntax
cat ${bkLocalConfig} | jq
chk_jq "" delIPNet
copyConfig
systemctl restart zerotier-one
allDone "IP was deleted" ipAllow
}
delIPNet
;;
b|B)
advZT
;;
e|E)
exit 0
;;
*) read -p "Invalid option. Press ENTER when done."
manageChoice
esac
;;
2)
current_if=$(grep interfacePrefixBlacklist ${localConfig} | egrep -o '\[.*\]' | sed -e 's/","/ /g;s/\[//g;s/\]//g;s/[",]/_/g;s/_//g')
clear
function manageIF() {
menuHeader "Add Interfaces"
echo "1. Add Interface."
echo "2. Delete Interface"
echo "[B]ack to main menu"
read -p "Please select a numeric value: " choice
case ${choice} in
1) # Add interface
listIf
# Parse the interfaces listed
x=$(echo ${current_if} | tr '\n' ' ' | sed "s/$/${intF}/g" | tr ' ' '\n' | sort -u)
# Make the changes to the temporary local.conf file first
copyBkConfig
ips=$(echo ${x} |tr '\n' ' '| sed -e 's/^/"/g;s/$/"/g;s/ /","/g;s/ //g;s/,""//g')
sed -i "/interfacePrefixBlacklist/s/\[.*\]/\[${ips}\]/g" ${bkLocalConfig}
cat ${bkLocalConfig} |jq
chk_jq "" manageIF
# If their are no errors from the json object then copy to the ZT home directory
copyConfig
systemctl restart zerotier-one
allDone "Interface was added" manageIF
;;
2) # Remove interface
menuHeader "Remove Current Interfaces"
listLocalConf interfacePrefixBlacklist
x=$(echo ${current_if} | sed "s/${currSetting}//g" |sort -u | tr '\n' ' ' |grep '.')
copyBkConfig
ips=$(echo ${x}| sed -e 's/^/"/g;s/$/"/g;s/ /","/g;s/ //g;s/_//g')
sed -i "/interfacePrefixBlacklist/s/\[.*\]/\[${ips}\]/g" ${bkLocalConfig}
cat ${bkLocalConfig} |jq
chk_jq "" manageIF
copyConfig
systemctl restart zerotier-one
allDone "Interface was added" manageIF
;;
b|B)
advZT
;;
*)
read -p "Invalid option. Press Enter to continue."
manageIF
;;
esac
}
manageIF
;;
e|E)
exit 0
;;
*)
read -p "Invalid option. Press Enter to continue."
advZT
;;
esac
advZT
}
advZT