various fixes for etiona and pxe
[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
234
235
236 uset dropbear.@dropbear[0].PasswordAuth 0
237 uset dropbear.@dropbear[0].RootPasswordAuth 0
238 uset dropbear.@dropbear[0].Port 2220
239 if ! cmp -s /root/dropbear_rsa_host_key /etc/dropbear/dropbear_rsa_host_key; then
240 cp /root/dropbear_rsa_host_key /etc/dropbear/dropbear_rsa_host_key
241 dropbear_restart=true
242 fi
243
244 if $dropbear_restart; then
245 v /etc/init.d/dropbear restart
246 fi
247
248
249 uset network.lan.ipaddr $l.$lanip
250 uset network.lan.netmask $mask
251 if $dev2; then
252 uset network.lan.gateway $l.1
253 uset network.wan.proto none
254 uset network.wan6.proto none
255 /etc/init.d/dnsmasq stop
256 /etc/init.d/dnsmasq disable
257 /etc/init.d/odhcpd stop
258 /etc/init.d/odhcpd disable
259 rm -f /etc/resolv.conf
260 cat >/etc/resolv.conf <<'EOF'
261 nameserver 8.8.8.8
262 nameserver 8.8.4.4
263 EOF
264
265 # things i tried to keep dnsmasq running but not enabled except local dns,
266 # but it didnt work right and i dont need it anyways.
267 # uset dhcp.wan.ignore $dev2 # default is false
268 # uset dhcp.lan.ignore $dev2 # default is false
269 # uset dhcp.@dnsmasq[0].interface lo
270 # uset dhcp.@dnsmasq[0].localuse 0
271 # uset dhcp.@dnsmasq[0].resolvfile /etc/dnsmasq.conf
272 # uset dhcp.@dnsmasq[0].noresolv 1
273 # todo: populate /etc/resolv.conf with a static value
274
275 else
276 # these are the defaults
277 uset network.lan.gateway ''
278 uset network.wan.proto dhcp
279 uset network.wan6.proto dhcpv6
280 /etc/init.d/dnsmasq start
281 # todo: figure out why this returns 1
282 /etc/init.d/dnsmasq enable ||:
283 /etc/init.d/odhcpd start
284 /etc/init.d/odhcpd enable
285 fi
286
287 wireless_restart=false
288 for x in 0 1; do
289 uset wireless.default_radio$x.ssid "$ssid"
290 uset wireless.default_radio$x.key $key
291 uset wireless.default_radio$x.encryption psk2
292 if [[ $mac ]]; then
293 uset wireless.default_radio$x.macaddr $macpre$((macsuf + 2*x))
294 fi
295 # secondary device has wireless disabled
296 uset wireless.radio$x.disabled $dev2
297 done
298
299 if $wireless_restart; then
300 v wifi
301 fi
302
303
304
305 # usb, screen, relay are for libremanage
306 # rsync is for brc
307 v pi kmod-usb-storage block-mount kmod-fs-ext4 nfs-kernel-server \
308 tcpdump openvpn-openssl adblock libusb-compat /root/relay_1.0-1_mips_24kc.ipk \
309 screen kmod-usb-serial-cp210x kmod-usb-serial-ftdi rsync
310
311 cat >/etc/libremanage.conf <<EOF
312 ${libremanage_host}_type=switch
313 ${libremanage_host}_channel=1
314 EOF
315
316
317
318 v /etc/init.d/fstab enable ||:
319
320 # rebooting makes mounting work, but comparing lsmod,
321 # i'm guessing this will too. todo, test it.
322 # 255 == module already loaded
323 for mod in scsi_mod sd_mod; do v modprobe $mod || [[ $? == 255 ]]; done
324
325 # for archlike pxe. The default settings in the installer expect to find
326 # the NFS at one of these dirs
327 mkdir -p /run/archiso/bootmnt
328 mkdir -p /run/parabolaiso/bootmnt
329
330 # todo: at some later time, i found /mnt/usb not mounted, watch to see if
331 # that is the case after running this or rebooting.
332 # wiki says safe to do in case of fstab changes:
333
334 ## ian: usb broke on old router. if that happens, can just comment this to disable problems
335 # echo | cedit /etc/config/fstab ||:
336 v cedit /etc/config/fstab <<EOF || { v block umount; v block mount; }
337 config global automount
338 option from_fstab 1
339 option anon_mount 1
340
341 config mount
342 # /overlay is an / overlay mount for installing extra packages, etc.
343 # https://openwrt.org/docs/guide-user/additional-software/extroot_configuration
344 option target /mnt/usb
345 # option target /overlay
346 option device /dev/sda1
347 option fstype ext4
348 option options rw,async,noatime,nodiratime
349 option enabled 0
350 EOF
351
352
353 # ian: disabled because afaik I don't need it, no benefit.
354 # config global autoswap
355 # option from_fstab 1
356 # option anon_swap 1
357
358 # config swap
359 # option device /dev/sda1
360 # option enabled 1
361
362
363
364
365 # exportfs -ra wont cut it when its the same path, but now a bind mount
366 # todo: restart nfs when nfs is enabled?
367 #cedit /etc/exports <<EOF || v /etc/init.d/nfsd restart ||:
368 cedit /etc/exports <<EOF ||:
369 /mnt/usb $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
370 # for arch pxe
371 /run/archiso/bootmnt $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
372 /run/parabolaiso/bootmnt $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
373 EOF
374
375
376 # todo: enable nfs when we need it only.
377 # v /etc/init.d/portmap start
378 # v /etc/init.d/nfsd start
379 # v /etc/init.d/portmap enable
380 # v /etc/init.d/nfsd enable
381
382
383
384
385
386
387
388
389 ########## openvpn exampl
390 ########## missing firewall settings for routing lan
391 ########## traffic
392 # v /etc/init.d/openvpn start
393 # v /etc/init.d/openvpn enable
394
395 # # from https://wiki.openwrt.org/doc/uci/firewall
396 # # todo: not sure if /etc/init.d/network needs restarting.
397 # # I did, and I had to restart the vpn afterwards.
398 # # This maps a uci interface to a real interface which is
399 # # managed outside of uci.
400 # v cedit /etc/config/network <<'EOF' ||:
401 # config interface 'tun0'
402 # option ifname 'tun0'
403 # option proto 'none'
404 # EOF
405 # v cedit /etc/config/openvpn <<'EOF' || v /etc/init.d/openvpn restart
406 # config openvpn my_client_config
407 # option enabled 1
408 # option config /etc/openvpn/client.conf
409 # EOF
410
411
412
413 v cedit /etc/config/network <<EOF || v /etc/init.d/network reload
414 config 'route' 'transmission'
415 option 'interface' 'lan'
416 option 'target' '10.173.0.0'
417 option 'netmask' '255.255.0.0'
418 option 'gateway' '$l.3'
419 EOF
420
421 firewall_restart=false
422 v cedit /etc/config/firewall <<EOF || firewall_restart=true
423
424 config redirect
425 option name ssh
426 option src wan
427 option src_dport 22
428 option dest_ip $l.2
429 option dest lan
430 config rule
431 option src wan
432 option target ACCEPT
433 option dest_port 22
434
435 config redirect
436 option name sshtp
437 option src wan
438 option src_dport 2202
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 2202
446
447 config redirect
448 option name sshfrodo
449 option src wan
450 option src_dport 2203
451 option dest_port 22
452 option dest_ip $l.3
453 option dest lan
454 config rule
455 option src wan
456 option target ACCEPT
457 option dest_port 2203
458
459 config redirect
460 option name sshx2
461 option src wan
462 option src_dport 2205
463 option dest_port 22
464 option dest_ip $l.5
465 option dest lan
466 config rule
467 option src wan
468 option target ACCEPT
469 option dest_port 2205
470
471 config redirect
472 option name sshx3
473 option src wan
474 option src_dport 2207
475 option dest_port 22
476 option dest_ip $l.7
477 option dest lan
478 config rule
479 option src wan
480 option target ACCEPT
481 option dest_port 2207
482
483 config redirect
484 option name sshtp
485 option src wan
486 option src_dport 2208
487 option dest_port 22
488 option dest_ip $l.8
489 option dest lan
490 config rule
491 option src wan
492 option target ACCEPT
493 option dest_port 2208
494
495
496 config rule
497 option name sshwrt
498 option src wan
499 option target ACCEPT
500 option dest_port 2220
501
502
503 config redirect
504 option name vpntp
505 option src wan
506 option src_dport 1196
507 option dest lan
508 option dest_ip $l.8
509 option proto udp
510 config rule
511 option src wan
512 option target ACCEPT
513 option dest_port 1196
514 option proto udp
515
516
517 config redirect
518 option name httptp
519 option src wan
520 option src_dport 80
521 option dest lan
522 option dest_ip $l.8
523 option proto tcp
524 config rule
525 option src wan
526 option target ACCEPT
527 option dest_port 80
528 option proto tcp
529
530 config redirect
531 option name httpstp
532 option src wan
533 option src_dport 443
534 option dest lan
535 option dest_ip $l.8
536 option proto tcp
537 config rule
538 option src wan
539 option target ACCEPT
540 option dest_port 443
541 option proto tcp
542
543 config redirect
544 option name syncthing
545 option src wan
546 option src_dport 22001
547 option dest_ip $l.8
548 option dest lan
549 config rule
550 option src wan
551 option target ACCEPT
552 option dest_port 22001
553
554
555 config rule
556 option name ssh-ipv6
557 option src wan
558 option dest lan
559 # note, using mac transform, we could allow all traffic to a host like this,
560 # replacing 1 as appropriate
561 #option dest_ip ::111:11ff:fe11:1111/::ffff:ffff:ffff:ffff
562 option dest_port 22
563 option target ACCEPT
564 option family ipv6
565
566 config rule
567 option name http-ipv6
568 option src wan
569 option dest lan
570 option dest_port 80
571 option target ACCEPT
572 option family ipv6
573
574 config rule
575 option name https-ipv6
576 option src wan
577 option dest lan
578 option dest_port 443
579 option target ACCEPT
580 option family ipv6
581
582 config rule
583 option name node-exporter
584 option src wan
585 option dest lan
586 option dest_port 9101
587 option target ACCEPT
588 option family ipv6
589
590 config rule
591 option name mail587-ipv6
592 option src wan
593 option dest lan
594 option dest_port 587
595 option target ACCEPT
596 option family ipv6
597
598
599 EOF
600
601
602
603
604 dnsmasq_restart=false
605 v cedit /etc/hosts <<EOF || dnsmasq_restart=true
606 127.0.1.1 $hostname
607 $l.1 $hostname
608 $l.2 kd faiserver
609 $l.3 frodo
610 $l.4 wrt2
611 $l.5 x2
612 $l.6 demohost
613 #$l.7 x3
614 $l.8 tp b8.nz
615 $l.9 bb8
616 $l.14 wrt3
617 2600:3c00::f03c:91ff:fe6d:baf8 li
618 72.14.176.105 li
619 2a01:7e01::f03c:91ff:feb5:baec l2
620 172.105.84.95 l2
621
622 # netns creation looks for next free subnet starting at 10.173, but I only
623 # use one, and I would keep this one as the first created.
624 10.173.0.2 transmission
625 EOF
626
627 #mail_host=$(grep -F mail.iankelling.org /etc/hosts | awk '{print $1}')
628 # if [[ $mail_host ]]; then
629 # sed -i '/^$mail_host/a mail.iankelling.org' /etc/hosts
630 # fi
631
632
633 uset dhcp.@dnsmasq[0].domain b8.nz
634 uset dhcp.@dnsmasq[0].local /b8.nz/
635 uset system.@system[0].hostname $hostname
636
637 if [[ $(uci get adblock.global.adb_enabled) != 1 ]]; then
638 v uci set adblock.global.adb_enabled=1
639 uci commit adblock
640 /etc/init.d/adblock restart
641 fi
642 # https://github.com/openwrt/packages/tree/master/net/adblock/files
643 cat >/etc/crontabs/root <<'EOF'
644 0 06 * * * /etc/init.d/adblock reload
645 EOF
646
647
648 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
649
650 # sometimes /mnt/usb fails, cuz it's just a flash drive,
651 # so make sure we have this dir or else dnsmasq will fail
652 # to start.
653 mkdir -p /mnt/usb/tftpboot
654 v cedit /etc/dnsmasq.conf <<EOF || dnsmasq_restart=true
655 server=/dmarctest.b8.nz/#
656 server=/_domainkey.b8.nz/#
657 server=/_dmarc.b8.nz/#
658 server=/ns1.b8.nz/#
659 server=/ns2.b8.nz/#
660 mx-host=b8.nz,mail.iankelling.org,10
661 txt-record=b8.nz,"v=spf1 a ?all"
662
663
664 # https://ret2got.wordpress.com/2018/01/19/how-your-ethereum-can-be-stolen-using-dns-rebinding/
665 stop-dns-rebind
666
667 # this says the ip of default gateway and dns server,
668 # but I think they are unneded and default
669 #dhcp-option=3,$l.1
670 #dhcp-option=6,$l.1
671
672
673
674 # results from googling around dnsmasq optimizations
675 # about 50k in memory. router has 62 megs.
676 # in a browsing session, I probably won't ever do 5000 lookups
677 # before the ttl expiration or whatever does expiration.
678 cache-size=10000
679
680 # ask all servers, use the one which responds first.
681 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
682 all-servers
683
684 # namebench benchmarks dns servers. google's dns was only
685 # slightly less fast than some others, and I trust it more
686 # to give accurate results, stay relatively fast, and
687 # not do anythin too malicious, so just use that.
688 # download namebench and run it like this:
689 # 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
690 # google
691 server=8.8.4.4
692 server=8.8.8.8
693 server=2001:4860:4860::8888
694 server=2001:4860:4860::8844
695
696
697 # to fixup existin ips, on the client you can do
698 # sudo dhclient -r; sudo dhclient <interface-name>
699
700 # default dhcp range is 100-150
701 # bottom port, iPXE (PCI 03:00.0) in seabios boot menu
702 dhcp-host=c8:60:00:31:6b:75,set:kd,$l.2,kd
703 # top port, iPXE (PCI 04:00.0) in seabios boot menu
704 #dhcp-host=c8:60:00:2b:15:07,set:kd,$l.2,kd
705 dhcp-host=00:26:18:97:bb:16,set:frodo,$l.3,frodo
706 # 4 is reserved for a staticly configured host.
707 dhcp-host=00:1f:16:16:39:24,set:x2,$l.5,x2
708 # This is so fai can have an explicit name to use for testing,
709 # or else any random machine which did a pxe boot would get
710 # reformatted. The mac is from doing a virt-install, cancelling it,
711 # and copying the generated mac, so it should be randomish.
712 dhcp-host=52:54:00:9c:ef:ad,set:demohost,$l.6,demohost
713 dhcp-host=00:1f:16:14:01:d8,set:tp,$l.7,x3
714 dhcp-host=80:fa:5b:1c:6e:cf,set:tp,$l.8,tp
715
716 # faiserver vm
717 dhcp-host=52:54:00:56:09:f9,set:faiserver,$l.15,faiserver
718
719 # This is the ip it picks by default if dhcp fails,
720 # so might as well use it.
721 # hostname is the name it uses according to telnet
722 dhcp-host=b4:75:0e:94:29:ca,set:switch9429ca,$l.251,switch9429ca
723
724 # template
725 # dhcp-host=,$l.,
726
727 # Just leave the tftp server up even if we aren't doing pxe boot.
728 # It has no sensitive info.
729 enable-tftp=br-lan
730 tftp-root=/mnt/usb/tftpboot
731 dhcp-optsfile=/etc/dnsmasq-dhcpopts.conf
732 EOF
733
734
735
736
737 if $dnsmasq_restart && ! $dev2; then
738 v /etc/init.d/dnsmasq restart
739 fi
740
741 if $firewall_restart; then
742 v /etc/init.d/firewall restart
743 fi
744
745
746 # todo: we should catch errors and still run this if needed
747 if $network_restart; then
748 reboot
749 fi
750
751 exit 0