-
Notifications
You must be signed in to change notification settings - Fork 6
/
gensettings
executable file
·596 lines (537 loc) · 16.4 KB
/
gensettings
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#!/bin/sh
#
# gensettings - creates the community setting files for each image
# Copyright (C) 2012 Linus Broich <[email protected]>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
fail() {
echo "$1" 1>&2
exit 1
}
gen_fsmsettings() {
local fsm_config=$1
local adhoc_dev=$2
local ffpreconfigure_output=$3
local fsm_uci_config=$4
# Check if adhoc_dev is valid
[ -n $adhoc_dev ] || fail "Invalid wifi device"
# Include FSM config
. $fsm_config
# Check if fsm settings are non-zero strings and abort if thats not the case
[ -n "$net_robinson" \
-a -n "$net_fake" \
-a -n "$net_mesh" \
-a -n "$net_queenrange_start" \
-a -n "$net_queenrange_end" \
-a -n "$net_queenmode" \
-a -n "$net_dhcpleasetime" \
-a -n "$testhosts_inet" \
-a -n "$net_ip6ula" \
-a -n "$meshwan_iface" \
-a -n "$meshwan_iponly_iface" \
-a -n "$batman_iface" \
-a -n "$fsm_list" \
-a -n "$gossip_list" \
-a -n "$force_state" \
-a -n "$splash_enabled" ] || fail "Error: One or more FSM settings are missing!"
# Remove old config if existing
[ -f $ffpreconfigure_output ] && rm -f $ffpreconfigure_output
[ -f $fsm_uci_config ] && rm -f $fsm_uci_config
cat << EOF > $ffpreconfigure_output
adhoc_dev='$adhoc_dev'
meshwan_iponly_iface='$meshwan_iponly_iface'
meshwan_iface='$meshwan_iface'
EOF
cat << EOF > $fsm_uci_config
config inetable 'meshfsm'
option 'vpn_fallback' '$vpn_fallback'
option 'net_queenmode' '$net_queenmode'
option 'net_queenrange_start' '$net_queenrange_start'
option 'net_queenrange_end' '$net_queenrange_end'
option 'net_gateways' '$net_gateways'
option 'net_nameservers' '$net_nameservers'
option 'net_dhcpleasetime' '$net_dhcpleasetime'
option 'net_mesh' '$net_mesh'
option 'net_ip6ula' '$net_ip6ula'
option 'net_robinson' '$net_robinson'
option 'net_fake' '$net_fake'
option 'fsm_list' '$fsm_list'
option 'gossip_list' '$gossip_list'
option 'batman_iface' '$batman_iface'
option 'routedvpn_iface' '$routedvpn_iface'
option 'meshvpn_iface' '$meshvpn_iface'
option 'testhosts_inet' '$testhosts_inet'
option 'testhosts_hybrid' '$testhosts_hybrid'
option 'testhosts_bridge' '$testhosts_bridge'
option 'meshwan_iponly_iface' '$meshwan_iponly_iface'
option 'meshwan_iface' '$meshwan_iface'
option 'splash_enabled' '$splash_enabled'
option 'splash_except_servers' '$splash_except_servers'
option 'splash_except_clients' '$splash_except_clients'
option 'force_state' '$force_state'
EOF
}
gen_wifisettings() {
local wifi_config=$1
local output=$2
# Include wifi config
. $wifi_config
# Check if the necessary wifi settings are non-zero strings and abort if thats not the case
[ -n "$wifi_channel" \
-a -n "$wifi_country" \
-a -n "$wifi_hwmode" \
-a -n "$wifi_htmode" \
-a -n "$wifi_adhoc_ssid" \
-a -n "$wifi_adhoc_mcastrate" \
-a -n "$wifi_hotspot_ssid" ] || fail "Error: One or more wifi settings are missing!"
# Default value for txpower if none is set
[ -z "$wifi_txpower" ] && wifi_txpower='20'
# Remove old config if existing
[ -f $output ] && rm -f $output
# Write wifi-device config
cat <<EOF > $output
config wifi-device radio0
option 'type' 'mac80211'
option 'channel' '$wifi_channel'
option 'phy' 'phy0'
option 'txpower' '$wifi_txpower'
option 'country' '$wifi_country'
option 'hwmode' '$wifi_hwmode'
option 'htmode' '$wifi_htmode'
EOF
# Set wifi_adhoc_bssid according to wifi channel if not set otherwise
if [ -z "$wifi_adhoc_bssid" ]; then
case "$wifi_channel" in
# 2.4GHZ Channels
1)
wifi_adhoc_bssid="12:CA:FF:EE:BA:BE"
;;
2)
wifi_adhoc_bssid="22:CA:FF:EE:BA:BE"
;;
3)
wifi_adhoc_bssid="32:CA:FF:EE:BA:BE"
;;
4)
wifi_adhoc_bssid="42:CA:FF:EE:BA:BE"
;;
5)
wifi_adhoc_bssid="52:CA:FF:EE:BA:BE"
;;
6)
wifi_adhoc_bssid="62:CA:FF:EE:BA:BE"
;;
7)
wifi_adhoc_bssid="72:CA:FF:EE:BA:BE"
;;
8)
wifi_adhoc_bssid="82:CA:FF:EE:BA:BE"
;;
9)
wifi_adhoc_bssid="92:CA:FF:EE:BA:BE"
;;
10)
wifi_adhoc_bssid="02:CA:FF:EE:BA:BE"
;;
11)
wifi_adhoc_bssid="B2:CA:FF:EE:BA:BE"
;;
12)
wifi_adhoc_bssid="C2:CA:FF:EE:BA:BE"
;;
13)
wifi_adhoc_bssid="D2:CA:FF:EE:BA:BE"
;;
14)
wifi_adhoc_bssid="E2:CA:FF:EE:BA:BE"
;;
# 5GHZ Channels
36)
wifi_adhoc_bssid="02:36:CA:FF:EE:EE"
;;
40)
wifi_adhoc_bssid="02:40:CA:FF:EE:EE"
;;
42)
wifi_adhoc_bssid="02:42:CA:FF:EE:EE"
;;
48)
wifi_adhoc_bssid="02:48:CA:FF:EE:EE"
;;
50)
wifi_adhoc_bssid="02:50:CA:FF:EE:EE"
;;
52)
wifi_adhoc_bssid="02:52:CA:FF:EE:EE"
;;
56)
wifi_adhoc_bssid="02:56:CA:FF:EE:EE"
;;
58)
wifi_adhoc_bssid="02:58:CA:FF:EE:EE"
;;
60)
wifi_adhoc_bssid="02:60:CA:FF:EE:EE"
;;
64)
wifi_adhoc_bssid="02:64:CA:FF:EE:EE"
;;
100)
wifi_adhoc_bssid="12:00:CA:FF:EE:EE"
;;
104)
wifi_adhoc_bssid="12:04:CA:FF:EE:EE"
;;
106)
wifi_adhoc_bssid="12:06:CA:FF:EE:EE"
;;
108)
wifi_adhoc_bssid="12:08:CA:FF:EE:EE"
;;
112)
wifi_adhoc_bssid="12:12:CA:FF:EE:EE"
;;
114)
wifi_adhoc_bssid="12:14:CA:FF:EE:EE"
;;
116)
wifi_adhoc_bssid="12:16:CA:FF:EE:EE"
;;
120)
wifi_adhoc_bssid="12:20:CA:FF:EE:EE"
;;
122)
wifi_adhoc_bssid="12:22:CA:FF:EE:EE"
;;
124)
wifi_adhoc_bssid="12:24:CA:FF:EE:EE"
;;
128)
wifi_adhoc_bssid="12:28:CA:FF:EE:EE"
;;
130)
wifi_adhoc_bssid="12:30:CA:FF:EE:EE"
;;
132)
wifi_adhoc_bssid="12:32:CA:FF:EE:EE"
;;
136)
wifi_adhoc_bssid="12:36:CA:FF:EE:EE"
;;
140)
wifi_adhoc_bssid="12:40:CA:FF:EE:EE"
;;
*)
fail "Unknown channel number!"
esac
fi
# Append adhoc wifi-iface
cat <<EOF >> $output
config wifi-iface
option device 'radio0'
option mode 'adhoc'
option network 'adhoc'
option ssid '$wifi_adhoc_ssid'
option bssid '$wifi_adhoc_bssid'
option mcast_rate '$wifi_adhoc_mcastrate'
option hidden '1'
option encryption 'none'
EOF
# Append hotspot ap configuration
cat <<EOF >> $output
config wifi-iface
option device 'radio0'
option network 'mesh'
option mode 'ap'
option ssid '$wifi_hotspot_ssid'
option encryption 'none'
option wmm '0'
EOF
# Append the secondary wifi ap if existant in config
if [ -n "$wifi_ap_ssid" -a -n "$wifi_ap_network" -a -z "$wifi_ap_encryption" ]; then
cat <<EOF >> $output
config wifi-iface
option device 'radio0'
option network '$wifi_ap_network'
option mode 'ap'
option ssid '$wifi_ap_ssid'
option encryption 'none'
option wmm '0'
EOF
# Return the Adhoc Iface
echo "wlan0-2"
elif [ -n "$wifi_ap_ssid" -a -n "$wifi_ap_network" -a -n "$wifi_ap_encryption" -a -n "$wifi_ap_key" ]; then
cat <<EOF >> $output
config wifi-iface
option device 'radio0'
option network '$wifi_ap_network'
option mode 'ap'
option ssid '$wifi_ap_ssid'
option encryption '$wifi_ap_encryption'
option key '$wifi_ap_key'
option wmm '0'
EOF
# Return the Adhoc Iface
echo "wlan0-2"
else
# Adhoc iface is wlan0-1 if no additional AP except the hotspot has been definded
echo "wlan0-1"
fi
}
gen_splashsettings() {
local splash_config=$1
local output=$2
# Include splash config
. $splash_config
# Check if splash settings are non-zero strings and abort if thats not the case
[ -n "$splash_package" \
-a -n "$splash_header" \
-a -n "$splash_website" \
-a -n "$splash_interfaces" ] || fail "Error: One or more splash settings are missing!"
# Remove old config if existing
[ -f $output ] && rm -f $output
cat << EOF > $output
config splash 'settings'
option 'package' '$splash_package'
option 'header' '$splash_header'
option 'website' '$splash_website'
option 'interfaces' '$splash_interfaces'
EOF
}
gen_n2nsettings() {
local n2n_config=$1
local output=$2
# Include n2n config
. $n2n_config
# Check if n2nsettings are non-zero strings and abort if thats not the case
[ -n "$n2n_supernode1" \
-a -n "$n2n_port1" \
-a -n "$n2n_community1" \
-a -n "$n2n_key1" ] || fail "Error: One or more n2n settings are missing!"
# Remove old config if existing
[ -f $output ] && rm -f $output
# Add first n2n interface
cat << EOF > $output
config edge
option ipaddr '1.2.3.4'
option supernode '$n2n_supernode1'
option port '$n2n_port1'
option community '$n2n_community1'
option key '$n2n_key1'
option devname 'n2n1'
option route ''
EOF
# Add second n2n interface if all settings are present (optional interface)
if [ -n "$n2n_supernode2" -a -n "$n2n_port2" -a -n "$n2n_community2" -a -n "$n2n_key2" ]; then
cat << EOF >> $output
config edge
option ipaddr '1.2.3.4'
option supernode '$n2n_supernode2'
option port '$n2n_port2'
option community '$n2n_community2'
option key '$n2n_key2'
option devname 'n2n2'
option route ''
EOF
fi
}
gen_collectdsettings() {
local collectd_config=$1
local output=$2
# Include collectd config
. $collectd_config
# Check if collectd settings are non-zero strings and abort if thats not the case
[ -n "$interfaces" \
-a -n "$server" \
-a -n "$server_port" \
-a -n "$ping_hosts" ] || fail "Error: One or more collectd settings are missing! Remove collectd config or set necessary values"
# Remove old config if existing
[ -f $output ] && rm -f $output
cat << EOF > $output
#Replace Hostname with the MAC Address (Without ":" or "-") plus the hostname
#Hostname "01234567890A-myhostname"
BaseDir "/var/lib/collectd"
PIDFile "/var/run/collectd.pid"
Interval 60
ReadThreads 2
TypesDB "/usr/share/collectd/types.db"
TypesDB "/etc/types.db.mesh"
LoadPlugin network
LoadPlugin interface
LoadPlugin ping
LoadPlugin exec
EOF
# Add interface section
echo "<Plugin interface>" >> $output
for iface in $interfaces
do
echo 'Interface "'$iface'"' >> $output
done
echo "IgnoreSelected false" >> $output
echo "</Plugin>" >> $output
# Add network section
echo "<Plugin network>" >> $output
echo 'Server "'$server'" "'$server_port'"' >> $output
echo "</Plugin>" >> $output
# Add ping hosts
echo "<Plugin ping>" >> $output
for host in $ping_hosts
do
echo 'Host "'$host'"' >> $output
done
echo "</Plugin>" >> $output
cat << EOF >> $output
<Plugin exec>
Exec "nobody:nogroup" "/usr/sbin/collectd-mesh-script.sh"
</Plugin>
EOF
}
gen_fastdsettings() {
local fastd_config=$1
local fastd_peers_path=$2
local fastd_target_folder=$3
local fastd_uci_config=$4
local autoregister_uci_config=$5
# Include fastd config
. $fastd_config
# Check if fastd settings are non-zero strings and abort if thats not the case
[ -n "$fastd_registerserver" \
-a -n "$fastd_autoregister" \
-a -n "$fastd_peers_path" \
-a -n "$fastd_meshvpn_mtu" \
-a -n "$fastd_meshvpn_methods" ] || fail "Error: One or more fastd settings are missing!"
# Add fastd autoregister config
cat << EOF >> $autoregister_uci_config
config autoregister settings
option register_server '$fastd_registerserver'
option autoregister '$fastd_autoregister'
EOF
# Remove old fastd folder if existing
[ -d $fastd_target_folder ] && rm -rf $fastd_target_folder
mkdir -p $fastd_target_folder
# Remove old uci config if existing
[ -f $fastd_uci_config ] && rm -f $fastd_uci_config
# Add mesh-vpn section to fastd UCI config
cat << EOF >> $fastd_uci_config
config fastd mesh_vpn
option enabled 0
list config '/etc/fastd/mesh-vpn/fastd.conf'
list config '/etc/fastd/mesh-vpn/secret.conf'
list config_peer_dir '/etc/fastd/mesh-vpn/peers'
option peer_limit '2'
option syslog_level 'verbose'
option mode 'tap'
option interface 'mesh-vpn'
option mtu '$fastd_meshvpn_mtu'
option secret 'dummy'
EOF
# Add encryption methods to fastd uci config
for method in $fastd_meshvpn_methods
do
echo " list method '$method'" >> $fastd_uci_config
done
# Make peer folder recursivly if not existant
mkdir -p $fastd_target_folder/mesh-vpn/peers
# Add settings to fastd.conf
cat << EOF >> $fastd_target_folder/mesh-vpn/fastd.conf
on up "
#ip link set up dev \$INTERFACE
#batctl -m \"\$(uci -q get fsm.meshfsm.batman_iface)\" if add \$INTERFACE
logger -t fastd \"Interface \$INTERFACE up and will be added by netifd\"
exit 0
";
EOF
# Add peer files for mesh-vpn
for peer in $(find $fastd_peers_path/mesh-vpn -type f)
do
cp $peer $fastd_target_folder/mesh-vpn/peers
done
# Exit-VPN Part
if [ -n "$fastd_exitvpn_mtu" -a -n "$fastd_exitvpn_methods" ]; then
# Add exit-vpn section to fastd UCI config
cat << EOF >> $fastd_uci_config
config fastd exit_vpn
option enabled 0
list config '/etc/fastd/exit-vpn/fastd.conf'
list config '/etc/fastd/exit-vpn/secret.conf'
list config_peer_dir '/etc/fastd/exit-vpn/peers'
option peer_limit '2'
option syslog_level 'verbose'
option mode 'tap'
option interface 'exit-vpn'
option mtu '$fastd_exitvpn_mtu'
option secret 'dummy'
EOF
# Add encryption methods to fastd uci config
for method in $fastd_exitvpn_methods
do
echo " list method '$method'" >> $fastd_uci_config
done
# Make peer folder recursivly if not existant
mkdir -p $fastd_target_folder/exit-vpn/peers
# Add settings to fastd.conf
cat << EOF >> $fastd_target_folder/exit-vpn/fastd.conf
on up "
#Currently no automatic adding of the interface ;)
#ip link set up dev \$INTERFACE
#batctl -m \"\$(uci -q get fsm.meshfsm.batman_iface)\" if add \$INTERFACE
exit 0
";
EOF
# Add peer files for exit-vpn
if [ -d $fastd_peers_path/exit-vpn ]; then
for peer in $(find $fastd_peers_path/exit-vpn -type f)
do
cp $peer $fastd_target_folder/exit-vpn/peers
done
fi
fi
}
gen_configs() {
local repository=$1
local buildtarget=$2
local community=$3
local fsm_config="config/communities/$community/fsm"
local wifi_config="config/communities/$community/wifi"
local splash_config="config/communities/$community/splash"
local n2n_config="config/communities/$community/n2n"
local fastd_config="config/communities/$community/fastd"
local collectd_config="config/communities/$community/collectd"
[ -f "$fsm_config" -a -f "$wifi_config" -a -f "$splash_config" ] || fail "Missing config files for community $community!"
[ ! -d openwrt/$repository/ ] && fail "Error: Repository $repository not existant"
[ ! -d openwrt/$repository/files/etc/defconfig ] && mkdir -p openwrt/$repository/files/etc/defconfig
[ ! -d openwrt/$repository/files/etc/config ] && mkdir -p openwrt/$repository/files/etc/config
# Generate wireless config, returns the wifi dev for the ffpreconfigure config
adhoc_dev=$(gen_wifisettings $wifi_config openwrt/$repository/files/etc/config/wireless)
gen_fsmsettings "$fsm_config" "$adhoc_dev" "openwrt/$repository/files/etc/defconfig/ffpreconfigure" "openwrt/$repository/files/etc/config/fsm"
gen_splashsettings "$splash_config" "openwrt/$repository/files/etc/config/splash"
[ -f "$fastd_config" ] && gen_fastdsettings "$fastd_config" "config/communities/$community/fastd.peers" "openwrt/$repository/files/etc/fastd" "openwrt/$repository/files/etc/config/fastd" "openwrt/$repository/files/etc/config/fastdregister"
[ -f "$n2n_config" ] && gen_n2nsettings "$n2n_config" "openwrt/$repository/files/etc/config/n2n"
[ -f "$collectd_config" ] && gen_collectdsettings "$collectd_config" "openwrt/$repository/files/etc/collectd.conf"
exit 0
}
printArgs () {
fail "Usage:
$0 repository buildtarget community
"
}
[ $# -lt 2 ] && printArgs
case "$1" in
--help|-h)
printArgs
;;
*)
gen_configs $1 $2 $3
;;
esac