various fixes, upgrade nfsroot to buster
[automated-distro-installer] / wrt-setup-local
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 set -eE -o pipefail
19 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
20
21
22 usage() {
23 cat <<EOF
24 usage: ${0##*/} [-h] [-t 2|3|test] [-m WIRELESS_MAC]
25 setup my router in general: dhcp, dns, etc.
26
27 Type 2 or 3 is for setting up a backup device, there are two kinds so
28 that you can switch the main device to a backup, then a backup to the
29 main. Type test is for setting up a testing device.
30
31 Passing an empty string for WIRELESS_MAC will cause the device's native
32 mac to be used.
33
34 EOF
35
36 exit $1
37 }
38
39
40
41
42 dev2=false
43 test=false
44 libremanage_host=wrt2
45
46 if [[ -e /p/router-secrets ]]; then
47 source /p/router-secrets
48 fi
49 rmac=$(cat /sys/class/net/eth0/address)
50 if [[ $rhost ]]; then
51 hostname=${rhost[$rmac]}
52 fi
53 : ${hostname:=wrt}
54
55
56 lanip=1
57 while getopts hm:t: opt; do
58 case $opt in
59 h) usage ;;
60 t)
61 case $2 in
62 2|3)
63 dev2=true
64 libremanage_host=$hostname
65 ;;&
66 2)
67 lanip=4
68 hostname=wrt2
69 ;;
70 3)
71 lanip=14
72 hostname=wrt3
73 ;;
74 test)
75 test=true
76 ;;
77 *) echo "$0: unexpected arg to -t: $*" >&2; usage 1 ;;
78 esac
79 ;;
80 m) mac=$OPTARG ;;
81 *) echo "$0: Internal error! unexpected args: $*" >&2 ; usage 1 ;;
82 esac
83 done
84 shift "$((OPTIND-1))" # Discard the options and sentinel --
85
86 if [[ ! $mac ]] && ! $test; then
87 # if we wanted to increment it
88 #WIRELESSMAC=${WIRELESSMAC:0: -1}$((${WIRELESSMAC: -1} + 2))
89 mac=$WIRELESSMAC
90 fi
91
92 if (( $# != 0 )); then
93 usage 1
94 fi
95
96
97 macpre=${mac:0: -1}
98 macsuf=${mac: -1}
99
100
101 p_updated=false
102 pmirror() {
103 if $p_updated; then
104 return
105 fi
106 # background: upgrading all packages is not recommended because it
107 # doesn't go into the firmware. build new firmware if you want
108 # lots of upgrades. I think /tmp/opkg-lists is a pre openwrt 14 location.
109 f=(/var/opkg-lists/*)
110 if ! (( $(date -r $f +%s) + 60*60*24 > $(date +%s) )); then
111 if ! opkg update; then
112 echo "$0: warning: opkg update failed" >&2
113 fi
114 p_updated=true
115 fi
116 }
117
118 pi() {
119 to_install=()
120 for p in "$@"; do
121 pname=${p##*/}
122 pname=${pname%%_*}
123 if [[ ! $(opkg list-installed "$pname") ]]; then
124 to_install+=($p)
125 pmirror
126 fi
127 done
128 if [[ $to_install ]]; then
129 opkg install ${to_install[@]}
130 fi
131 }
132
133 v() {
134 printf "+ %s\n" "$*"
135 "$@"
136 }
137
138 ######### uci example:#######
139 # # https://wiki.openwrt.org/doc/uci
140 # wan_index=$(uci show firewall | sed -rn 's/firewall\.@zone\[([0-9])+\]\.name=wan/\1/p')
141 # wan="firewall.@zone[$wan_index]"
142 # if [[ $(uci get firewall.@forwarding[0].dest) != $forward_dest ]]; then
143 # # default is wan
144 # v uci set firewall.@forwarding[0].dest=$forward_dest
145 # uci commit firewall
146 # firewall_restart=true
147 # fi
148 ####### end uci example #####
149
150 uset() {
151 printf "+ uset %s\n" "$*"
152 local key="$1"
153 local val="$2"
154 local service="${key%%.*}"
155 restart_var=${service}_restart
156 if [[ ! ${!restart_var} ]]; then
157 eval $restart_var=false
158 fi
159 if [[ $(uci get "$key") != "$val" ]]; then
160 v uci set "$key"="$val"
161 uci commit $service
162 eval $restart_var=true
163 fi
164 }
165
166 udel() {
167 printf "+ udel %s\n" "$*"
168 local key="$1"
169 local val="$2"
170 local service="${key%%.*}"
171 restart_var=${service}_restart
172 if [[ ! ${!restart_var} ]]; then
173 eval $restart_var=false
174 fi
175 if uci get "$key" &>/dev/null; then
176 v uci set "$key"="$val"
177 uci commit $service
178 eval $restart_var=true
179 fi
180 }
181
182
183
184 ### network config
185 ###
186 ssid="check out gnu.org"
187 lan=10.0.0.0
188 if $test; then
189 ssid="gnuv3"
190 lan=10.1.0.0
191 elif [[ $hostname == cmc ]]; then
192 ssid=Svenska
193 lan=10.2.0.0
194 fi
195
196 if [[ $rkey ]]; then
197 key=${rkey[$rmac]}
198 fi
199 : ${key:=pictionary49}
200
201 mask=255.255.0.0
202 cidr=16
203 l=${lan%.0}
204
205 passwd -l root ||: #already locked fails
206
207 sed -ibak '/^root:/d' /etc/shadow
208 # /root/router created by manually running passwd then copying the resulting
209 # line. We have no mkpasswd on wrt/librecmc, then we scp it in.
210 cat /root/router >>/etc/shadow
211 # otherwise, serial console gets root login with no password
212 uset system.@system[0].ttylogin 1
213
214
215 cat >/usr/bin/archlike-pxe-mount <<'EOFOUTER'
216 #!/bin/bash
217 # symlinks are collapsed for nfs mount points, so use a bind mount.
218 # tried putting this in /etc/config/fstab,
219 # then doing block mount, it didn't work. This doesn't persist across reboots,
220 # todo: figure that out
221 rm -f /etc/fstab
222 for d in /run/{arch,parabola}iso/bootmnt; do
223 cat >>/etc/fstab <<EOF
224 /mnt/usb/tftpboot $d none bind 0 0
225 EOF
226 mount | grep $d &>/dev/null || mount $d
227 done
228 /etc/init.d/nfsd restart
229 EOFOUTER
230 chmod +x /usr/bin/archlike-pxe-mount
231
232 sed -i '/^root:/s,/bin/ash$,/bin/bash,' /etc/passwd
233 # usb, screen, relay are for libremanage
234 v pi kmod-usb-storage block-mount kmod-fs-ext4 nfs-kernel-server \
235 tcpdump openvpn-openssl adblock libusb-compat /root/relay_1.0-1_mips_24kc.ipk \
236 screen kmod-usb-serial-cp210x kmod-usb-serial-ftdi
237
238 cat >/etc/libremanage.conf <<EOF
239 ${libremanage_host}_type=switch
240 ${libremanage_host}_channel=1
241 EOF
242
243
244
245 v /etc/init.d/fstab enable ||:
246
247 # rebooting makes mounting work, but comparing lsmod,
248 # i'm guessing this will too. todo, test it.
249 # 255 == module already loaded
250 for mod in scsi_mod sd_mod; do v modprobe $mod || [[ $? == 255 ]]; done
251
252 # for archlike pxe. The default settings in the installer expect to find
253 # the NFS at one of these dirs
254 mkdir -p /run/archiso/bootmnt
255 mkdir -p /run/parabolaiso/bootmnt
256
257 # todo: at some later time, i found /mnt/usb not mounted, watch to see if
258 # that is the case after running this or rebooting.
259 # wiki says safe to do in case of fstab changes:
260
261 ## ian: usb broke on old router. if that happens, can just comment this to disable problems
262 # echo | cedit /etc/config/fstab ||:
263 v cedit /etc/config/fstab <<EOF || { v block umount; v block mount; }
264 config global automount
265 option from_fstab 1
266 option anon_mount 1
267
268 config mount
269 # /overlay is an / overlay mount for installing extra packages, etc.
270 # https://openwrt.org/docs/guide-user/additional-software/extroot_configuration
271 option target /mnt/usb
272 # option target /overlay
273 option device /dev/sda1
274 option fstype ext4
275 option options rw,async,noatime,nodiratime
276 option enabled 0
277 EOF
278
279
280 # ian: disabled because afaik I don't need it, no benefit.
281 # config global autoswap
282 # option from_fstab 1
283 # option anon_swap 1
284
285 # config swap
286 # option device /dev/sda1
287 # option enabled 1
288
289
290
291
292 # exportfs -ra wont cut it when its the same path, but now a bind mount
293 # todo: restart nfs when nfs is enabled?
294 #cedit /etc/exports <<EOF || v /etc/init.d/nfsd restart ||:
295 cedit /etc/exports <<EOF ||:
296 /mnt/usb $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
297 # for arch pxe
298 /run/archiso/bootmnt $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
299 /run/parabolaiso/bootmnt $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
300 EOF
301
302
303 # todo: enable nfs when we need it only.
304 # v /etc/init.d/portmap start
305 # v /etc/init.d/nfsd start
306 # v /etc/init.d/portmap enable
307 # v /etc/init.d/nfsd enable
308
309
310
311
312
313
314
315 uset dropbear.@dropbear[0].PasswordAuth 0
316 uset dropbear.@dropbear[0].RootPasswordAuth 0
317 uset dropbear.@dropbear[0].Port 2220
318 if ! cmp -s /root/dropbear_rsa_host_key /etc/dropbear/dropbear_rsa_host_key; then
319 cp /root/dropbear_rsa_host_key /etc/dropbear/dropbear_rsa_host_key
320 dropbear_restart=true
321 fi
322
323 wireless_restart=false
324
325 for x in 0 1; do
326 uset wireless.default_radio$x.ssid "$ssid"
327 uset wireless.default_radio$x.key $key
328 uset wireless.default_radio$x.encryption psk2
329 if [[ $mac ]]; then
330 uset wireless.default_radio$x.macaddr $macpre$((macsuf + 2*x))
331 fi
332 # secondary device has wireless disabled
333 uset wireless.radio$x.disabled $dev2
334 done
335
336 if $wireless_restart; then
337 v wifi
338 fi
339
340
341 ########## openvpn exampl
342 ########## missing firewall settings for routing lan
343 ########## traffic
344 # v /etc/init.d/openvpn start
345 # v /etc/init.d/openvpn enable
346
347 # # from https://wiki.openwrt.org/doc/uci/firewall
348 # # todo: not sure if /etc/init.d/network needs restarting.
349 # # I did, and I had to restart the vpn afterwards.
350 # # This maps a uci interface to a real interface which is
351 # # managed outside of uci.
352 # v cedit /etc/config/network <<'EOF' ||:
353 # config interface 'tun0'
354 # option ifname 'tun0'
355 # option proto 'none'
356 # EOF
357 # v cedit /etc/config/openvpn <<'EOF' || v /etc/init.d/openvpn restart
358 # config openvpn my_client_config
359 # option enabled 1
360 # option config /etc/openvpn/client.conf
361 # EOF
362
363
364
365 v cedit /etc/config/network <<EOF || v /etc/init.d/network reload
366 config 'route' 'transmission'
367 option 'interface' 'lan'
368 option 'target' '10.173.0.0'
369 option 'netmask' '255.255.0.0'
370 option 'gateway' '$l.3'
371 EOF
372
373 firewall_restart=false
374 v cedit /etc/config/firewall <<EOF || firewall_restart=true
375
376 config redirect
377 option name ssh
378 option src wan
379 option src_dport 22
380 option dest_ip $l.8
381 option dest lan
382 config rule
383 option src wan
384 option target ACCEPT
385 option dest_port 22
386
387 config redirect
388 option name sshkd
389 option src wan
390 option src_dport 2202
391 option dest_port 22
392 option dest_ip $l.2
393 option dest lan
394 config rule
395 option src wan
396 option target ACCEPT
397 option dest_port 2202
398
399 config redirect
400 option name sshfrodo
401 option src wan
402 option src_dport 2203
403 option dest_port 22
404 option dest_ip $l.3
405 option dest lan
406 config rule
407 option src wan
408 option target ACCEPT
409 option dest_port 2203
410
411 config redirect
412 option name sshx2
413 option src wan
414 option src_dport 2205
415 option dest_port 22
416 option dest_ip $l.5
417 option dest lan
418 config rule
419 option src wan
420 option target ACCEPT
421 option dest_port 2205
422
423 config redirect
424 option name sshx3
425 option src wan
426 option src_dport 2207
427 option dest_port 22
428 option dest_ip $l.7
429 option dest lan
430 config rule
431 option src wan
432 option target ACCEPT
433 option dest_port 2207
434
435 config redirect
436 option name sshtp
437 option src wan
438 option src_dport 2208
439 option dest_port 22
440 option dest_ip $l.8
441 option dest lan
442 config rule
443 option src wan
444 option target ACCEPT
445 option dest_port 2208
446
447
448 config rule
449 option name sshwrt
450 option src wan
451 option target ACCEPT
452 option dest_port 2220
453
454
455 config redirect
456 option name vpntp
457 option src wan
458 option src_dport 1196
459 option dest lan
460 option dest_ip $l.8
461 option proto udp
462 config rule
463 option src wan
464 option target ACCEPT
465 option dest_port 1196
466 option proto udp
467
468
469 config redirect
470 option name httptp
471 option src wan
472 option src_dport 80
473 option dest lan
474 option dest_ip $l.8
475 option proto tcp
476 config rule
477 option src wan
478 option target ACCEPT
479 option dest_port 80
480 option proto tcp
481
482 config redirect
483 option name httpstp
484 option src wan
485 option src_dport 443
486 option dest lan
487 option dest_ip $l.8
488 option proto tcp
489 config rule
490 option src wan
491 option target ACCEPT
492 option dest_port 443
493 option proto tcp
494
495 config redirect
496 option name syncthing
497 option src wan
498 option src_dport 22001
499 option dest_ip $l.8
500 option dest lan
501 config rule
502 option src wan
503 option target ACCEPT
504 option dest_port 22001
505
506
507 config rule
508 option name ssh-ipv6
509 option src wan
510 option dest lan
511 # note, using mac transform, we could allow all traffic to a host like this,
512 # replacing 1 as appropriate
513 #option dest_ip ::111:11ff:fe11:1111/::ffff:ffff:ffff:ffff
514 option dest_port 22
515 option target ACCEPT
516 option family ipv6
517
518 config rule
519 option name http-ipv6
520 option src wan
521 option dest lan
522 option dest_port 80
523 option target ACCEPT
524 option family ipv6
525
526 config rule
527 option name https-ipv6
528 option src wan
529 option dest lan
530 option dest_port 443
531 option target ACCEPT
532 option family ipv6
533
534 config rule
535 option name node-exporter
536 option src wan
537 option dest lan
538 option dest_port 9101
539 option target ACCEPT
540 option family ipv6
541
542 config rule
543 option name mail587-ipv6
544 option src wan
545 option dest lan
546 option dest_port 587
547 option target ACCEPT
548 option family ipv6
549
550
551 EOF
552
553
554
555
556 dnsmasq_restart=false
557 v cedit /etc/hosts <<EOF || dnsmasq_restart=true
558 127.0.1.1 $hostname
559 $l.1 $hostname
560 $l.2 kd
561 $l.3 frodo
562 $l.4 wrt2
563 $l.5 x2
564 $l.6 demohost
565 #$l.7 x3
566 $l.8 tp b8.nz faiserver
567 $l.9 bb8
568 $l.14 wrt3
569 2600:3c00::f03c:91ff:fe6d:baf8 li
570 72.14.176.105 li
571 2a01:7e01::f03c:91ff:feb5:baec l2
572 172.105.84.95 l2
573
574 # netns creation looks for next free subnet starting at 10.173, but I only
575 # use one, and I would keep this one as the first created.
576 10.173.0.2 transmission
577 EOF
578
579 #mail_host=$(grep -F mail.iankelling.org /etc/hosts | awk '{print $1}')
580 # if [[ $mail_host ]]; then
581 # sed -i '/^$mail_host/a mail.iankelling.org' /etc/hosts
582 # fi
583
584
585 uset dhcp.@dnsmasq[0].domain b8.nz
586 uset dhcp.@dnsmasq[0].local /b8.nz/
587 uset system.@system[0].hostname $hostname
588
589 if [[ $(uci get adblock.global.adb_enabled) != 1 ]]; then
590 v uci set adblock.global.adb_enabled=1
591 uci commit adblock
592 /etc/init.d/adblock restart
593 fi
594 # https://github.com/openwrt/packages/tree/master/net/adblock/files
595 cat >/etc/crontabs/root <<'EOF'
596 0 06 * * * /etc/init.d/adblock reload
597 EOF
598
599
600 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
601
602 # sometimes /mnt/usb fails, cuz it's just a flash drive,
603 # so make sure we have this dir or else dnsmasq will fail
604 # to start.
605 mkdir -p /mnt/usb/tftpboot
606 v cedit /etc/dnsmasq.conf <<EOF || dnsmasq_restart=true
607 server=/dmarctest.b8.nz/#
608 server=/_domainkey.b8.nz/#
609 server=/_dmarc.b8.nz/#
610 server=/ns1.b8.nz/#
611 server=/ns2.b8.nz/#
612 mx-host=b8.nz,mail.iankelling.org,10
613 txt-record=b8.nz,"v=spf1 a ?all"
614
615
616 # https://ret2got.wordpress.com/2018/01/19/how-your-ethereum-can-be-stolen-using-dns-rebinding/
617 stop-dns-rebind
618
619 # this says the ip of default gateway and dns server,
620 # but I think they are unneded and default
621 #dhcp-option=3,$l.1
622 #dhcp-option=6,$l.1
623
624
625
626 # results from googling around dnsmasq optimizations
627 # about 50k in memory. router has 62 megs.
628 # in a browsing session, I probably won't ever do 5000 lookups
629 # before the ttl expiration or whatever does expiration.
630 cache-size=10000
631
632 # ask all servers, use the one which responds first.
633 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
634 all-servers
635
636 # namebench benchmarks dns servers. google's dns was only
637 # slightly less fast than some others, and I trust it more
638 # to give accurate results, stay relatively fast, and
639 # not do anythin too malicious, so just use that.
640 # download namebench and run it like this:
641 # for x in all regional isp global preferred nearby; do ./namebench.py -s \$x -c US -i firefox -m weighted -J 10 -w; echo \$x; hr; done
642 # google
643 server=8.8.4.4
644 server=8.8.8.8
645 server=2001:4860:4860::8888
646 server=2001:4860:4860::8844
647
648
649 # to fixup existin ips, on the client you can do
650 # sudo dhclient -r; sudo dhclient <interface-name>
651
652 # default dhcp range is 100-150
653 # bottom port, iPXE (PCI 03:00.0) in seabios boot menu
654 dhcp-host=c8:60:00:31:6b:75,set:kd,$l.2,kd
655 # top port, iPXE (PCI 04:00.0) in seabios boot menu
656 #dhcp-host=c8:60:00:2b:15:07,set:kd,$l.2,kd
657 dhcp-host=00:26:18:97:bb:16,set:frodo,$l.3,frodo
658 # 4 is reserved for a staticly configured host.
659 dhcp-host=00:1f:16:16:39:24,set:x2,$l.5,x2
660 # This is so fai can have an explicit name to use for testing,
661 # or else any random machine which did a pxe boot would get
662 # reformatted. The mac is from doing a virt-install, cancelling it,
663 # and copying the generated mac, so it should be randomish.
664 dhcp-host=52:54:00:9c:ef:ad,set:demohost,$l.6,demohost
665 dhcp-host=00:1f:16:14:01:d8,set:tp,$l.7,x3
666 dhcp-host=80:fa:5b:1c:6e:cf,set:tp,$l.8,tp
667
668 # faiserver vm
669 dhcp-host=52:54:00:56:09:f9,set:faiserver,$l.15,faiserver
670
671 # This is the ip it picks by default if dhcp fails,
672 # so might as well use it.
673 # hostname is the name it uses according to telnet
674 dhcp-host=b4:75:0e:94:29:ca,set:switch9429ca,$l.251,switch9429ca
675
676 # template
677 # dhcp-host=,$l.,
678
679 # Just leave the tftp server up even if we aren't doing pxe boot.
680 # It has no sensitive info.
681 enable-tftp=br-lan
682 tftp-root=/mnt/usb/tftpboot
683 EOF
684
685 uset network.lan.ipaddr $l.$lanip
686 uset network.lan.netmask $mask
687 if $dev2; then
688 uset network.lan.gateway $l.1
689 uset network.wan.proto none
690 uset network.wan6.proto none
691 /etc/init.d/dnsmasq stop
692 /etc/init.d/dnsmasq disable
693 /etc/init.d/odhcpd stop
694 /etc/init.d/odhcpd disable
695 # things i tried to keep dnsmasq running but not enabled except local dns,
696 # but it didnt work right and i dont need it anyways.
697 # uset dhcp.wan.ignore $dev2 # default is false
698 # uset dhcp.lan.ignore $dev2 # default is false
699 # uset dhcp.@dnsmasq[0].interface lo
700 # uset dhcp.@dnsmasq[0].localuse 0
701 # uset dhcp.@dnsmasq[0].resolvfile /etc/dnsmasq.conf
702 # uset dhcp.@dnsmasq[0].noresolv 1
703 # todo: populate /etc/resolv.conf with a static value
704
705 else
706 # these are the defaults
707 uset network.lan.gateway ''
708 uset network.wan.proto dhcp
709 uset network.wan6.proto dhcpv6
710 /etc/init.d/dnsmasq start
711 # todo: figure out why this returns 1
712 /etc/init.d/dnsmasq enable ||:
713 /etc/init.d/odhcpd start
714 /etc/init.d/odhcpd enable
715 fi
716
717
718 if $dnsmasq_restart; then
719 v /etc/init.d/dnsmasq restart
720 fi
721
722 if $firewall_restart; then
723 v /etc/init.d/firewall restart
724 fi
725
726
727
728 if $network_restart; then
729 reboot
730 fi
731 if $dropbear_restart; then
732 v /etc/init.d/dropbear restart
733 fi
734
735 exit 0