various fixes
[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
19 f=/usr/local/lib/err;test -r $f || { echo "error: $0 no $f" >&2;exit 1;}; . $f
20
21
22 usage() {
23 cat <<EOF
24 usage: ${0##*/} [-h] [-t 2|3|test|client] [-m WIRELESS_MAC] [hostname]
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, or if none is defined in the
32 secrets file, will cause the device's native mac to be used.
33
34 EOF
35
36 exit $1
37 }
38
39
40
41 zblock=false
42 if [[ -e /root/zblock ]]; then
43 zblock=true
44 fi
45
46 dnsmasq_restart=false
47 unbound_restart=false
48 firewall_restart=false
49 dev2=false
50 test=false
51 client=false
52 libremanage_host=wrt2
53 lanip=1
54 while getopts hm:t:yz opt; do
55 case $opt in
56 h) usage ;;
57 t)
58 case $2 in
59 2|3)
60 dev2=true
61 libremanage_host=$hostname
62 ;;&
63 2)
64 lanip=4
65 hostname=wrt2
66 ;;
67 3)
68 lanip=14
69 hostname=wrt3
70 ;;
71 test)
72 test=true
73 ;;
74 client)
75 client=true
76 ;;
77 *) echo "$0: unexpected arg to -t: $*" >&2; usage 1 ;;
78 esac
79 ;;
80 y)
81 zblock=false
82 rm -f /root/zblock
83 ;;
84 z)
85 zblock=true
86 touch /root/zblock
87 ;;
88 m) mac=$OPTARG ;;
89 *) echo "$0: Internal error! unexpected args: $*" >&2 ; usage 1 ;;
90 esac
91 done
92 shift "$((OPTIND-1))" # Discard the options and sentinel --
93
94 if [[ $1 ]]; then
95 h=$1
96 else
97 h=cmc
98 fi
99 if [[ ! $hostname ]]; then
100 hostname=$h
101 fi
102
103
104 secrets=false
105 if [[ -e /root/router-secrets ]]; then
106 secrets=true
107 source /root/router-secrets
108 fi
109
110 if [[ ! $mac ]] && ! $test && $secrets; then
111 # if we wanted to increment it
112 #mac=${mac:0: -1}$((${mac: -1} + 2))
113 mac=${rwmac[$h]}
114 fi
115
116 if (( $# != 0 )); then
117 usage 1
118 fi
119
120
121 macpre=${mac:0: -1}
122 macsuf=${mac: -1}
123
124
125 p_updated=false
126 pmirror() {
127 if $p_updated; then
128 return
129 fi
130 # background: upgrading all packages is not recommended because it
131 # doesn't go into the firmware. build new firmware if you want
132 # lots of upgrades. I think /tmp/opkg-lists is a pre openwrt 14 location.
133 f=(/var/opkg-lists/*)
134 if ! (( $(date -r $f +%s) + 60*60*24 > $(date +%s) )); then
135 if ! opkg update; then
136 echo "$0: warning: opkg update failed" >&2
137 fi
138 p_updated=true
139 fi
140 }
141
142 pi() {
143 to_install=()
144 for p in "$@"; do
145 pname=${p##*/}
146 pname=${pname%%_*}
147 if [[ ! $(opkg list-installed "$pname") ]]; then
148 to_install+=($p)
149 pmirror
150 fi
151 done
152 if [[ $to_install ]]; then
153 opkg install ${to_install[@]}
154 fi
155 }
156
157 v() {
158 printf "+ %s\n" "$*"
159 "$@"
160 }
161
162 ######### uci example:#######
163 # # https://wiki.openwrt.org/doc/uci
164 # wan_index=$(uci show firewall | sed -rn 's/firewall\.@zone\[([0-9])+\]\.name=wan/\1/p')
165 # wan="firewall.@zone[$wan_index]"
166 # if [[ $(uci get firewall.@forwarding[0].dest) != $forward_dest ]]; then
167 # # default is wan
168 # v uci set firewall.@forwarding[0].dest=$forward_dest
169 # uci commit firewall
170 # firewall_restart=true
171 # fi
172 ####### end uci example #####
173
174 uset() {
175 printf "+ uset %s\n" "$*"
176 local key="$1"
177 local val="$2"
178 local service="${key%%.*}"
179 restart_var=${service}_restart
180 if [[ ! ${!restart_var} ]]; then
181 eval $restart_var=false
182 fi
183 if [[ $(uci get "$key") != "$val" ]]; then
184 v uci set "$key"="$val"
185 uci commit $service
186 eval $restart_var=true
187 fi
188 }
189
190 udel() {
191 printf "+ udel %s\n" "$*"
192 local key="$1"
193 local val="$2"
194 local service="${key%%.*}"
195 restart_var=${service}_restart
196 if [[ ! ${!restart_var} ]]; then
197 eval $restart_var=false
198 fi
199 if uci get "$key" &>/dev/null; then
200 v uci set "$key"="$val"
201 uci commit $service
202 eval $restart_var=true
203 fi
204 }
205 cedit() {
206 v command cedit -v "$@"
207 }
208
209
210 ### network config
211 ###
212 lan=10.0.0.0
213 if $test; then
214 lan=10.1.0.0
215 elif [[ $hostname == cmc ]]; then
216 lan=10.2.0.0
217 elif $client; then
218 lan=10.3.0.0
219 fi
220
221 if $test; then
222 ssid="gnuv3"
223 elif $secrets; then
224 ssid=${rssid[$h]}
225 fi
226
227 : ${ssid:=librecmc}
228
229
230 if $secrets; then
231 key=${rkey[$h]}
232 fi
233 : ${key:=pictionary49}
234
235 mask=255.255.0.0
236 cidr=16
237 l=${lan%.0}
238
239 # why did we lock this? i don't know
240 #passwd -l root ||: #already locked fails
241
242 sed -ibak '/^root:/d' /etc/shadow
243 # /root/router created by manually running passwd then copying the resulting
244 # line. We have no mkpasswd on wrt/librecmc, then we scp it in.
245 cat /root/router >>/etc/shadow
246 # otherwise, serial console gets root login with no password
247 uset system.@system[0].ttylogin 1
248
249
250
251 cat >/usr/bin/archlike-pxe-mount <<'EOFOUTER'
252 #!/bin/bash
253 # symlinks are collapsed for nfs mount points, so use a bind mount.
254 # tried putting this in /etc/config/fstab,
255 # then doing block mount, it didn't work. This doesn't persist across reboots,
256 # todo: figure that out
257 rm -f /etc/fstab
258 for d in /run/{arch,parabola}iso/bootmnt; do
259 cat >>/etc/fstab <<EOF
260 /mnt/usb/tftpboot $d none bind 0 0
261 EOF
262 mount | grep $d &>/dev/null || mount $d
263 done
264 /etc/init.d/nfsd restart
265 EOFOUTER
266 chmod +x /usr/bin/archlike-pxe-mount
267
268 sed -i '/^root:/s,/bin/ash$,/bin/bash,' /etc/passwd
269
270
271
272 uset dropbear.@dropbear[0].PasswordAuth 0
273 uset dropbear.@dropbear[0].RootPasswordAuth 0
274 uset dropbear.@dropbear[0].Port 2220
275 if ! cmp -s /root/dropbear_rsa_host_key /etc/dropbear/dropbear_rsa_host_key; then
276 cp /root/dropbear_rsa_host_key /etc/dropbear/dropbear_rsa_host_key
277 dropbear_restart=true
278 fi
279
280 if $dropbear_restart; then
281 v /etc/init.d/dropbear restart
282 fi
283
284
285 uset network.lan.ipaddr $l.$lanip
286 uset network.lan.netmask $mask
287 if $dev2 || $client; then
288 if $dev2; then
289 uset network.lan.gateway $l.1
290 uset network.wan.proto none
291 uset network.wan6.proto none
292 fi
293 /etc/init.d/dnsmasq stop
294 /etc/init.d/dnsmasq disable
295 /etc/init.d/odhcpd stop
296 /etc/init.d/odhcpd disable
297 rm -f /etc/resolv.conf
298 cat >/etc/resolv.conf <<'EOF'
299 nameserver 8.8.8.8
300 nameserver 8.8.4.4
301 EOF
302
303 # things i tried to keep dnsmasq running but not enabled except local dns,
304 # but it didnt work right and i dont need it anyways.
305 # uset dhcp.wan.ignore $dev2 # default is false
306 # uset dhcp.lan.ignore $dev2 # default is false
307 # uset dhcp.@dnsmasq[0].interface lo
308 # uset dhcp.@dnsmasq[0].localuse 0
309 # uset dhcp.@dnsmasq[0].resolvfile /etc/dnsmasq.conf
310 # uset dhcp.@dnsmasq[0].noresolv 1
311 # todo: populate /etc/resolv.conf with a static value
312
313 else
314 # these are the defaults
315
316 # this is not needed unless switching from the above condition.
317 # disabling just to debug
318 #uset network.lan.gateway ''
319
320 uset network.wan.proto dhcp
321 uset network.wan6.proto dhcpv6
322 /etc/init.d/dnsmasq start
323 # todo: figure out why this returns 1
324 /etc/init.d/dnsmasq enable ||:
325 /etc/init.d/odhcpd start
326 /etc/init.d/odhcpd enable
327 fi
328
329 wireless_restart=false
330
331 if $client; then
332 uset wireless.default_radio0.network 'wwan'
333 uset wireless.default_radio0.ssid ${rclientssid[$h]}
334 uset wireless.default_radio0.encryption 'psk2'
335 uset wireless.default_radio0.device 'radio0'
336 uset wireless.default_radio0.mode 'sta'
337 uset wireless.default_radio0.bssid ${rclientbssid[$h]}
338 # todo: look into whether 5g network is available.
339 uset wireless.default_radio0.key ${rclientkey[$h]}
340 uset wireless.radio0.disabled false
341 uset wireless.radio1.disabled true
342 else
343 # defaults, just reseting in case client config ran
344 uset wireless.default_radio0.network lan
345 uset wireless.default_radio0.mode ap
346 for x in 0 1; do
347 uset wireless.default_radio$x.ssid "$ssid"
348 uset wireless.default_radio$x.key $key
349 uset wireless.default_radio$x.encryption psk2
350 if [[ $mac ]]; then
351 uset wireless.default_radio$x.macaddr $macpre$((macsuf + 2*x))
352 fi
353 # secondary device has wireless disabled
354 uset wireless.radio$x.disabled $dev2
355 done
356 fi
357
358 if grep '^OPENWRT_BOARD="mvebu/cortexa9"' /etc/os-release &>/dev/null; then
359 # todo, I also enabled irqbalance, didnt script it though
360 # https://forum.openwrt.org/t/wrt1900acs-wifi-issue-after-upgrade-from-19-07-to-21-02-vacuum-cleaner-legacy-rate-support/113311/28
361 cat >/etc/rc.local <<'EOF'
362 echo "0" >> /sys/kernel/debug/ieee80211/phy0/mwlwifi/tx_amsdu
363 echo "0" >> /sys/kernel/debug/ieee80211/phy1/mwlwifi/tx_amsdu
364 exit 0
365 EOF
366 chmod +x /etc/rc.local
367 /etc/rc.local
368 uset wireless.radio0.disassoc_low_ack 0
369 uset wireless.radio1.disassoc_low_ack 0
370 fi
371 case $HOSTNAME in
372 cmc)
373 # found with https://openwrt.org/docs/guide-user/network/wifi/iwchan
374 uset wireless.radio0.channel 11
375 ;;
376 esac
377
378
379 # usb, screen, relay are for libremanage
380 # rsync is for brc
381 #
382 # relay package temporarily disabled
383 # /root/relay_1.0-1_mips_24kc.ipk
384 #
385 # note: prometheus-node-exporter-lua-openwrt seems to be a dependency of
386 # prometheus-node-exporter-lua in practice.
387 v pi tcpdump screen rsync unbound-daemon unbound-checkconf \
388 kmod-usb-storage block-mount kmod-fs-ext4 \
389 prometheus-node-exporter-lua-openwrt \
390 prometheus-node-exporter-lua
391 # nfs-kernel-server \
392 # openvpn-openssl adblock libusb-compat \
393 # kmod-usb-serial-cp210x kmod-usb-serial-ftdi \
394
395
396 cat >/etc/libremanage.conf <<EOF
397 ${libremanage_host}_type=switch
398 ${libremanage_host}_channel=1
399 EOF
400
401
402
403 v /etc/init.d/fstab enable ||:
404
405 # rebooting makes mounting work, but comparing lsmod,
406 # i'm guessing this will too. todo, test it.
407 # 255 == module already loaded
408 for mod in scsi_mod sd_mod; do v modprobe $mod || [[ $? == 255 ]]; done
409
410 # for archlike pxe. The default settings in the installer expect to find
411 # the NFS at one of these dirs
412 mkdir -p /run/archiso/bootmnt
413 mkdir -p /run/parabolaiso/bootmnt
414
415 # todo: at some later time, i found /mnt/usb not mounted, watch to see if
416 # that is the case after running this or rebooting.
417 # wiki says safe to do in case of fstab changes:
418
419 ## ian: usb broke on old router. if that happens, can just comment this to disable problems
420 # echo | cedit /etc/config/fstab ||:
421 cedit /etc/config/fstab <<EOF || { v block umount; v block mount; }
422 config global automount
423 option from_fstab 1
424 option anon_mount 1
425
426 config mount
427 # /overlay is an / overlay mount for installing extra packages, etc.
428 # https://openwrt.org/docs/guide-user/additional-software/extroot_configuration
429 option target /mnt/usb
430 # option target /overlay
431 option device /dev/sda1
432 option fstype ext4
433 option options rw,async,noatime,nodiratime
434 option enabled 0
435 EOF
436
437
438 # ian: disabled because afaik I don't need it, no benefit.
439 # config global autoswap
440 # option from_fstab 1
441 # option anon_swap 1
442
443 # config swap
444 # option device /dev/sda1
445 # option enabled 1
446
447
448
449
450 # exportfs -ra wont cut it when its the same path, but now a bind mount
451 # todo: restart nfs when nfs is enabled?
452 #cedit /etc/exports <<EOF || v /etc/init.d/nfsd restart ||:
453 cedit /etc/exports <<EOF ||:
454 /mnt/usb $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
455 # for arch pxe
456 /run/archiso/bootmnt $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
457 /run/parabolaiso/bootmnt $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
458 EOF
459
460
461 # todo: enable nfs when we need it only.
462 # v /etc/init.d/portmap start
463 # v /etc/init.d/nfsd start
464 # v /etc/init.d/portmap enable
465 # v /etc/init.d/nfsd enable
466
467
468 cedit /etc/config/prometheus-node-exporter-lua <<'EOF' || /etc/init.d/prometheus-node-exporter-lua restart
469 config prometheus-node-exporter-lua 'main'
470 option listen_ipv6 '0'
471 option listen_interface 'lan'
472 option listen_port '9100
473 EOF
474
475 # default, as of this writing is:
476 # config prometheus-node-exporter-lua 'main'
477 # option listen_interface 'loopback'
478 # option listen_port '9100'
479
480
481
482
483
484 ########## openvpn exampl
485 ########## missing firewall settings for routing lan
486 ########## traffic
487 # v /etc/init.d/openvpn start
488 # v /etc/init.d/openvpn enable
489
490 # # from https://wiki.openwrt.org/doc/uci/firewall
491 # # todo: not sure if /etc/init.d/network needs restarting.
492 # # I did, and I had to restart the vpn afterwards.
493 # # This maps a uci interface to a real interface which is
494 # # managed outside of uci.
495 # cedit /etc/config/network <<'EOF' ||:
496 # config interface 'tun0'
497 # option ifname 'tun0'
498 # option proto 'none'
499 # EOF
500 # cedit /etc/config/openvpn <<'EOF' || v /etc/init.d/openvpn restart
501 # config openvpn my_client_config
502 # option enabled 1
503 # option config /etc/openvpn/client.conf
504 # EOF
505
506 wgip4=10.3.0.1/24
507 wgip6=fdfd::1/64
508 wgport=26000
509
510 network_restart=false
511 if $client; then
512 cedit wific /etc/config/network <<EOF || network_restart=true
513 # https://openwrt.org/docs/guide-user/network/wifi/connect_client_wifi
514 config interface 'wwan'
515 option proto 'dhcp'
516 EOF
517 fi
518
519 cedit /etc/config/network <<EOF || network_restart=true
520 config 'route' 'transmission'
521 option 'interface' 'lan'
522 option 'target' '10.173.0.0'
523 option 'netmask' '255.255.0.0'
524 option 'gateway' '$l.3'
525
526 option interface 'wg0'
527 option proto 'wireguard'
528 option private_key '$(cat /root/wg.key)'
529 option listen_port $wgport
530 list addresses '10.3.0.1/24'
531 list addresses 'fdfd::1/64'
532
533 # tp
534 config wireguard_wg0 'wgclient'
535 option public_key '3q+WJGrm85r59NgeXOIvppxoW4ux/+koSw6Fee1c1TI='
536 option preshared_key '$(cat /root/wg.psk)'
537 list allowed_ips '10.3.0.2/24'
538 list allowed_ips 'fdfd::2/64'
539 EOF
540
541 # Need to reload before we change dnsmasq to give out
542 # static ips in our new range.
543 if $network_restart; then
544 v /etc/init.d/network reload
545 fi
546
547 firewall-cedit() {
548
549 if $client; then
550 cedit wific /etc/config/firewall <<EOF
551 config zone
552 option name wwan
553 option input REJECT
554 option output ACCEPT
555 option forward REJECT
556 option masq 1
557 option mtu_fix 1
558 option network wwan
559 EOF
560 fi
561
562 case $hostname in
563 wrt)
564 cedit host /etc/config/firewall <<EOF
565 config redirect
566 option name ssh
567 option src wan
568 option src_dport 22
569 option dest_ip $l.3
570 option dest lan
571 EOF
572 ;;
573 cmc)
574 cedit host /etc/config/firewall <<EOF
575 config redirect
576 option name ssh
577 option src wan
578 option src_dport 22
579 option dest_ip $l.2
580 option dest lan
581 EOF
582 ;;
583 esac
584
585
586 cedit /etc/config/firewall <<EOF
587 ## begin no external dns for ziva
588 config rule
589 option src lan
590 option src_ip 10.2.0.23
591 option dest_port 53
592 option dest wan
593 option target REJECT
594
595
596 config rule
597 option src wan
598 option dest_ip 10.2.0.23
599 option src_port 53
600 option dest lan
601 option target REJECT
602
603
604 config rule
605 option src lan
606 option src_ip 10.2.0.31
607 option dest_port 53
608 option dest wan
609 option target REJECT
610
611
612 config rule
613 option src wan
614 option dest_ip 10.2.0.31
615 option src_port 53
616 option dest lan
617 option target REJECT
618
619
620 config rule
621 option src lan
622 option src_ip 10.2.0.32
623 option dest_port 53
624 option dest wan
625 option target REJECT
626
627
628 config rule
629 option src wan
630 option dest_ip 10.2.0.32
631 option src_port 53
632 option dest lan
633 option target REJECT
634 ## end no external dns for ziva
635
636
637 config rule
638 option src wan
639 option target ACCEPT
640 option dest_port 22
641
642 config redirect
643 option name promkd
644 option src wan
645 option src_dport 9091
646 option dest_port 9091
647 option dest_ip $l.2
648 option dest lan
649 config rule
650 option src wan
651 option target ACCEPT
652 option dest_port 9091
653
654
655 config redirect
656 option name nagioskd
657 option src wan
658 option src_dport 3005
659 option dest_port 3005
660 option dest_ip $l.2
661 option dest lan
662 config rule
663 option src wan
664 option target ACCEPT
665 option dest_port 3005
666
667
668 config redirect
669 option name sshkd
670 option src wan
671 option src_dport 2202
672 option dest_port 22
673 option dest_ip $l.2
674 option dest lan
675 config rule
676 option src wan
677 option target ACCEPT
678 option dest_port 2202
679
680
681 config redirect
682 option name sshkdalt
683 option src wan
684 option src_dport 8989
685 option dest_port 8989
686 option dest_ip $l.2
687 option dest lan
688 config rule
689 option src wan
690 option target ACCEPT
691 option dest_port 8989
692
693
694 config redirect
695 option name sshx2
696 option src wan
697 option src_dport 2205
698 option dest_port 22
699 option dest_ip $l.5
700 option dest lan
701 config rule
702 option src wan
703 option target ACCEPT
704 option dest_port 2205
705
706 config redirect
707 option name sshx3
708 option src wan
709 option src_dport 2207
710 option dest_port 22
711 option dest_ip $l.7
712 option dest lan
713 config rule
714 option src wan
715 option target ACCEPT
716 option dest_port 2207
717
718 config redirect
719 option name sshtp
720 option src wan
721 option src_dport 2208
722 option dest_port 22
723 option dest_ip $l.8
724 option dest lan
725 config rule
726 option src wan
727 option target ACCEPT
728 option dest_port 2208
729
730 config redirect
731 option name sshbb8
732 option src wan
733 option src_dport 2209
734 option dest_port 22
735 option dest_ip $l.9
736 option dest lan
737 config rule
738 option src wan
739 option target ACCEPT
740 option dest_port 2209
741
742 config redirect
743 option name icecast
744 option src wan
745 option src_dport 8000
746 option dest_port 8000
747 option dest_ip $l.2
748 option dest lan
749 config rule
750 option src wan
751 option target ACCEPT
752 option dest_port 8000
753
754 config rule
755 option name sshwrt
756 option src wan
757 option target ACCEPT
758 option dest_port 2220
759
760 config rule
761 option name wg
762 option src wan
763 option target ACCEPT
764 option dest_port $wgport
765 option proto udp
766
767
768 config redirect
769 option name http
770 option src wan
771 option src_dport 80
772 option dest lan
773 option dest_ip $l.2
774 option proto tcp
775 config rule
776 option src wan
777 option target ACCEPT
778 option dest_port 80
779 option proto tcp
780
781 config redirect
782 option name https
783 option src wan
784 option src_dport 443
785 option dest lan
786 option dest_ip $l.2
787 option proto tcp
788 config rule
789 option src wan
790 option target ACCEPT
791 option dest_port 443
792 option proto tcp
793
794 config redirect
795 option name httpskd8448
796 option src wan
797 option src_dport 8448
798 option dest lan
799 option dest_ip $l.2
800 option proto tcp
801 config rule
802 option src wan
803 option target ACCEPT
804 option dest_port 8448
805 option proto tcp
806
807 config redirect
808 option name syncthing
809 option src wan
810 option src_dport 22001
811 option dest_ip $l.8
812 option dest lan
813 config rule
814 option src wan
815 option target ACCEPT
816 option dest_port 22001
817
818
819 #config redirect
820 # option name i2p
821 # option src wan
822 # option src_dport $i2pport
823 # option dest_ip $l.178
824 # option dest lan
825 #config rule
826 # option src wan
827 # option target ACCEPT
828 # option dest_port $i2pport
829
830 config rule
831 option name ssh-ipv6
832 option src wan
833 option dest lan
834 # note, using mac transform, we could allow all traffic to a host like this,
835 # replacing 1 as appropriate
836 #option dest_ip ::111:11ff:fe11:1111/::ffff:ffff:ffff:ffff
837 option dest_port 22
838 option target ACCEPT
839 option family ipv6
840
841 config rule
842 option name http-ipv6
843 option src wan
844 option dest lan
845 option dest_port 80
846 option target ACCEPT
847 option family ipv6
848
849 config rule
850 option name https-ipv6
851 option src wan
852 option dest lan
853 option dest_port 443
854 option target ACCEPT
855 option family ipv6
856
857 config rule
858 option name node-exporter
859 option src wan
860 option dest lan
861 option dest_port 9101
862 option target ACCEPT
863 option family ipv6
864
865 config rule
866 option name mail587-ipv6
867 option src wan
868 option dest lan
869 option dest_port 587
870 option target ACCEPT
871 option family ipv6
872
873 # not using and in newer wrt, fails, probably due to nonexistent file, error output
874 # on
875
876 # Reference error: left-hand side expression is not an array or object
877 # In [anonymous function](), file /usr/share/ucode/fw4.uc, line 3137, byte 12:
878 # called from function [arrow function] (/usr/share/ucode/fw4.uc:733:71)
879 # called from function foreach ([C])
880 # called from function [anonymous function] (/usr/share/ucode/fw4.uc:733:72)
881 # called from function render_ruleset (/usr/share/firewall4/main.uc:56:24)
882 # called from anonymous function (/usr/share/firewall4/main.uc:143:29)
883
884 # ` if (!inc.enabled) {`
885 # Near here -------^
886 #
887 #
888 # The rendered ruleset contains errors, not doing firewall restart.
889 # /usr/bin/wrt-setup-local:160:error: ""$@"" returned 1
890
891
892 ## include a file with users custom iptables rules
893 #config include
894 # option path /etc/firewall.user
895 # option type 'restore'
896 # option family 'ipv4'
897
898 EOF
899 }
900 firewall-cedit || firewall_restart=true
901
902
903
904
905
906 # not using wireguard for now
907 # if ! uci get firewall.@zone[1].network | grep wg0 &>/dev/null; then
908 # # cant mix cedit plus uci
909 # echo | cedit /etc/config/firewall ||:
910 # uci add_list firewall.@zone[1].network=wg0
911 # uci commit firewall
912 # firewall-cedit ||:
913 # firewall_restart=true
914 # fi
915
916
917
918 cedit /etc/hosts <<EOF ||:
919 127.0.1.1 $hostname
920 EOF
921
922
923 #mail_host=$(grep -F mail.iankelling.org /etc/hosts | awk '{print $1}')
924 # if [[ $mail_host ]]; then
925 # sed -i '/^$mail_host/a mail.iankelling.org' /etc/hosts
926 # fi
927
928
929 uset dhcp.@dnsmasq[0].domain b8.nz
930 uset system.@system[0].hostname $hostname
931 uset dhcp.@dnsmasq[0].local
932
933 # uci doesnt seem to have a way to set an empty value,
934 # if you delete it, it goes back to the default. this seems
935 # to be a decent workaround.
936 # todo: setup /etc/resolv.conf to point to 127.0.0.1
937 # later note: disabled, I dunno why I set this.
938 # uset dhcp.@dnsmasq[0].resolvfile /dev/null
939
940 # if dnsmasq happens to not send out a dns server,
941 # odhcpd will send one out like this:
942 # NetworkManager[953]: <info> [1614982580.5192] dhcp6 (wlan0): option dhcp6_name_servers => 'fd58:5801:8e02::1'
943 # but i dont want ipv6 dns, just keep it simple to ipv4.
944 # I know my isp doesnt have ipv6 right now,
945 # so just stop this thing.
946 # note: tried this, it didn't do anything:
947 # uset dhcp.@odhcpd[0].dns 10.2.0.1
948
949 # iank, disablde while debugging.
950 #/etc/init.d/odhcpd stop
951 #/etc/init.d/odhcpd disable
952
953 # todo: make the above conditional on which server this is.
954
955 # avoid errors in log. current isp doesnt have ipv6
956 uset unbound.@unbound[0].protocol ip4_only
957
958 # todo: im not sure all these are needed, but they all look
959 # like good options.
960 # https://blog.cloudflare.com/dns-over-tls-for-openwrt/
961 # https://gist.github.com/vqiu/7b32d3a19a7a09d32e108d998de166c2
962 #https://blog.thestateofme.com/2018/04/04/howto-secure-your-dns-with-a-raspberry-pi-unbound-and-cloudflare-1-1-1-1/
963 #
964 # # i found that the zone example was having no effect on the config
965 # # here:
966 # https://github.com/openwrt/packages/blob/openwrt-19.07/net/unbound/files/README.md
967 #
968 # # todo: unbound-control, i'm not sure what the purpose of that thing is, some
969 # # kind of coordination with dhcp of dnsmasq, but what?
970 #
971 # note: for debugging, edit /etc/init.d/unbound, change
972 # procd_set_param command $PROG -d -c $UB_TOTAL_CONF
973 # to:
974 # procd_set_param command $PROG -vvv -d -c $UB_TOTAL_CONF
975
976 {
977 cat <<'EOF'
978 do-tcp: yes
979 prefetch: yes
980 qname-minimisation: yes
981 rrset-roundrobin: yes
982 use-caps-for-id: yes
983 do-ip6: no
984 private-domain: b8.nz
985 local-zone: "10.in-addr.arpa." transparent
986 access-control-view: 10.2.0.31/32 "youtube"
987 EOF
988
989 if $zblock; then
990 cat <<'EOF'
991 # no sy until that dongle is used by ziva
992
993 # syw
994 #access-control-view: 10.2.0.7/32 "youtube"
995 # bow
996 access-control-view: 10.2.0.29/32 "youtube"
997 # samsungtab
998 access-control-view: 10.2.0.32/32 "youtube"
999 EOF
1000 fi
1001 } | cedit /etc/unbound/unbound_srv.conf || restart_unbound=true
1002
1003
1004 # dns based blocking vs ip based. with ip, same
1005 # server can have multiple domains. in dns,
1006 # you have to make sure clients to use the local dns.
1007 # https dns will need to be blocked by ip in
1008 # order to be comprehensive
1009
1010 cedit /etc/unbound/unbound_ext.conf <<'EOF' || restart_unbound=true
1011 local-data-ptr: "10.2.0.1 cmc.b8.nz"
1012 local-data-ptr: "10.2.0.2 kd.b8.nz"
1013 local-data-ptr: "10.2.0.3 sy.b8.nz"
1014 local-data-ptr: "10.2.0.4 wrt2.b8.nz"
1015 local-data-ptr: "10.2.0.5 x2.b8.nz"
1016 local-data-ptr: "10.2.0.6 x2w.b8.nz"
1017 local-data-ptr: "10.2.0.7 syw.b8.nz"
1018 local-data-ptr: "10.2.0.8 amy.b8.nz"
1019 local-data-ptr: "10.2.0.9 bb8.b8.nz"
1020 local-data-ptr: "10.2.0.12 demohost.b8.nz"
1021 local-data-ptr: "10.2.0.14 wrt3.b8.nz"
1022 local-data-ptr: "10.2.0.19 brother.b8.nz"
1023 local-data-ptr: "10.2.0.23 amyw.b8.nz"
1024 local-data-ptr: "10.2.0.25 hp.b8.nz"
1025 local-data-ptr: "10.2.0.31 amazontab.b8.nz"
1026 local-data-ptr: "10.2.0.32 samsungtab.b8.nz"
1027 local-data-ptr: "10.173.0.2 transmission.b8.nz"
1028 local-data-ptr: "10.173.8.1 defaultnn.b8.nz"
1029 local-data-ptr: "10.173.8.2 nn.b8.nz"
1030
1031 forward-zone:
1032 name: "."
1033 # forward-addr: 8.8.8.8
1034 # forward-addr: 8.8.8.8
1035 # https://developers.cloudflare.com/1.1.1.1/1.1.1.1-for-families/setup-instructions/dns-over-https
1036 forward-addr: 1.1.1.3@853#family.cloudflare-dns.com
1037 forward-addr: 1.0.0.3@853#family.cloudflare-dns.com
1038 forward-ssl-upstream: yes
1039 forward-first: no
1040
1041 view:
1042 name: "youtube"
1043 local-zone: "googlevideo.com." refuse
1044 local-zone: "video.google.com." refuse
1045 local-zone: "youtu.be." refuse
1046 local-zone: "youtube-nocookie.com." refuse
1047 local-zone: "youtube-ui.l.google.com." refuse
1048 local-zone: "youtube.com." refuse
1049 local-zone: "youtube.googleapis.com." refuse
1050 local-zone: "youtubeeducation.com." refuse
1051 local-zone: "youtubei.googleapis.com." refuse
1052 local-zone: "yt3.ggpht.com." refuse
1053 local-zone: "youtubekids.com." refuse
1054 # try global if no match in view
1055 view-first: yes
1056 EOF
1057
1058
1059 if $restart_unbound; then
1060 /etc/init.d/unbound restart
1061 if ! unbound-checkconf; then
1062 echo $0: error: unbound-checkconf failed >&2
1063 exit 1
1064 fi
1065 fi
1066
1067
1068 # # disabled for now. i want to selectively enable it
1069 # # for specific hosts.
1070 # if [[ $(uci get adblock.global.adb_enabled) != 0 ]]; then
1071 # v uci set adblock.global.adb_enabled=0
1072 # uci commit adblock
1073 # /etc/init.d/adblock restart
1074 # fi
1075 # # https://github.com/openwrt/packages/tree/master/net/adblock/files
1076 # cat >/etc/crontabs/root <<'EOF'
1077 # 0 06 * * * /etc/init.d/adblock reload
1078 # EOF
1079
1080
1081 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
1082
1083 # sometimes /mnt/usb fails, cuz it's just a flash drive,
1084 # so make sure we have this dir or else dnsmasq will fail
1085 # to start.
1086 mkdir -p /mnt/usb/tftpboot
1087 cedit /etc/dnsmasq.conf <<EOF || dnsmasq_restart=true
1088 # no dns
1089 port=0
1090 server=/b8.nz/#
1091 ptr-record=1.0.2.10.in-addr.arpa.,cmc.b8.nz
1092
1093 # https://ret2got.wordpress.com/2018/01/19/how-your-ethereum-can-be-stolen-using-dns-rebinding/
1094 stop-dns-rebind
1095 rebind-domain-ok=b8.nz
1096
1097 # This says the ip of dns server.
1098 # It is default if dnsmasq is doing dns, otherwise, we have to specify it.
1099 # To see it in action, I ran this from a client machine:
1100 # sudo dhcpcd -o domain_name_servers -T
1101 dhcp-option=option:dns-server,$l.1
1102
1103 # use this when doing fai to get the right timezone, its nfsroot is
1104 # setup to use this dhcp option only and call ntpdate.
1105 # generate ips with:
1106 # for h in 0.ubuntu.pool.ntp.org 1.ubuntu.pool.ntp.org ntp.ubuntu.com; do host -t a $h | awk '{print $NF}'; done | while read -r l; do printf ,$l; done
1107 dhcp-option=option:ntp-server,188.165.3.28,202.12.97.45,91.236.251.13,50.205.244.23,78.30.254.80,31.131.0.123,202.65.114.202,94.228.220.14,185.125.190.57,185.125.190.58,91.189.91.157,185.125.190.56,91.189.94.4
1108
1109
1110 # results from googling around dnsmasq optimizations
1111 # about 50k in memory. router has 62 megs.
1112 # in a browsing session, I probably won't ever do 5000 lookups
1113 # before the ttl expiration or whatever does expiration.
1114 cache-size=10000
1115
1116 # ask all servers, use the one which responds first.
1117 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
1118 all-servers
1119
1120 # namebench benchmarks dns servers. google's dns was only
1121 # slightly less fast than some others, and I trust it more
1122 # to give accurate results, stay relatively fast, and
1123 # not do anythin too malicious, so just use that.
1124 # download namebench and run it like this:
1125 # 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
1126 # google
1127 server=1.1.1.3
1128 server=1.0.0.3
1129 server=2606:4700:4700::1113
1130 server=2606:4700:4700::1003
1131
1132 server=10.2.0.1
1133 # server=8.8.4.4
1134 # server=8.8.8.8
1135 # server=2001:4860:4860::8888
1136 # server=2001:4860:4860::8844
1137
1138
1139 # to fixup existin ips, on the client you can do
1140 # sudo dhclient -r; sudo dhclient <interface-name>
1141 # or on cmc,
1142 # /etc/init.d/dnsmasq stop
1143 # vi /tmp/dhcp.leases
1144 # /etc/init.d/dnsmasq start
1145
1146
1147 # default dhcp range is 100-150
1148 # bottom port, iPXE (PCI 03:00.0) in seabios boot menu
1149 dhcp-host=c8:60:00:31:6b:75,set:kd,$l.2,kd
1150 dhcp-host=94:05:bb:1e:2c:2e,set:sy,$l.3,sy
1151 #dhcp-host=94:05:bb:1e:2c:2e,set:bo,$l.38,bo
1152 # top port, iPXE (PCI 04:00.0) in seabios boot menu
1153 #dhcp-host=c8:60:00:2b:15:07,set:kd,$l.2,kd
1154 # 4 is reserved for a staticly configured host wrt2
1155 dhcp-host=f0:de:f1:81:ec:88,set:x2,$l.5,x2
1156 dhcp-host=c4:8e:8f:44:f5:63,set:x2w,$l.6,x2w
1157 dhcp-host=10:51:07:f5:f1:b8,set:syw,$l.7,syw
1158 dhcp-host=80:fa:5b:1c:6e:cf,set:amy,$l.8,amy
1159 # This is so fai can have an explicit name to use for testing,
1160 # or else any random machine which did a pxe boot would get
1161 # reformatted. The mac is from doing a virt-install, cancelling it,
1162 # and copying the generated mac, so it should be randomish.
1163 dhcp-host=52:54:00:9c:ef:ad,set:demohost,$l.12,demohost
1164 ## for using different dhcp server
1165 #dhcp-host=52:54:00:9c:ef:ad,ignore
1166 dhcp-host=62:03:cb:a8:3e:a3,set:trp,$1.13,trp
1167 # 14 = wrt3
1168 dhcp-host=ac:d1:b8:5c:eb:d7,set:x3w,$l.17,x3w
1169 dhcp-host=00:1f:16:14:01:d8,set:x3,$l.18,x3
1170 # BRN001BA98CA823 in dhcp logs
1171 dhcp-host=00:1b:a9:8c:a8:23,set:brother,$l.19,brother
1172
1173 dhcp-host=00:26:b6:f7:d4:d8,set:amyw,$l.23,amyw
1174 dhcp-host=9a:c6:52:6f:ce:7c,set:onep9,$l.24,onep9
1175 dhcp-host=38:63:bb:07:5a:f9,set:hp,$l.25,hp
1176 dhcp-host=00:26:b6:f6:0f:e9,set:frodow,$l.28,frodow
1177 dhcp-host=70:a6:cc:3a:bb:b4,set:bow,$l.29,bow
1178 dhcp-host=6c:56:97:88:7b:74,set:amazontab,$l.31,amazontab
1179 dhcp-host=0a:8a:9b:cf:b5:ec,set:samsungtab,$l.32,samsungtab
1180 dhcp-host=b8:27:eb:78:21:1d,set:pi3b,$l.33,pi3b
1181 # old x2 with bad fan
1182 dhcp-host=00:1f:16:16:39:24,set:x8,$l.38,x8
1183 # server d16:
1184 dhcp-host=38:2c:4a:c9:33:13,set:bigs,$l.48,bigs
1185 dhcp-host=e4:5f:01:07:50:40,set:pi4w,$l.49,pi4
1186 dhcp-host=e4:5f:01:07:50:3f,set:pi4,$l.50,pi4w
1187
1188
1189 # faiserver vm
1190 #dhcp-host=52:54:00:56:09:f9,set:faiserver,$l.15,faiserver
1191
1192 # This is the ip it picks by default if dhcp fails,
1193 # so might as well use it.
1194 # hostname is the name it uses according to telnet
1195 dhcp-host=b4:75:0e:94:29:ca,set:switch9429ca,$l.251,switch9429ca
1196
1197 # template
1198 # dhcp-host=,$l.,
1199
1200 # pxe tftpboot for arch-like. todo: openwrt snapshot from 2022-01, it cant
1201 # access /mnt/usb/tftpboot due to ujail sandbox
1202 #enable-tftp=br-lan
1203 #tftp-root=/mnt/usb/tftpboot
1204 #tftp-root=/var/run/dnsmasq/tftpboot
1205
1206
1207 dhcp-optsfile=/var/run/dnsmasq/dhcpopts.conf
1208
1209 # for debugging dhcp
1210 #log-queries=extra
1211 EOF
1212
1213
1214 if $dnsmasq_restart && ! $dev2; then
1215 # todo: can our ptr records be put in /etc/hosts?
1216 # eg: user normal /etc/hosts records, and they wont be used for A resolution
1217 # due to the other settings, but will be used for ptr? then maybe
1218 # we dont have to restart dnsmasq for a dns update?
1219 #
1220 # interesing link:
1221 # https://www.redpill-linpro.com/techblog/2019/08/27/evaluating-local-dnssec-validators.html#toggling-dnssec-validation-1
1222 # we could turn on dnssec validation when wrt gets dnsmasq > 2.80. currently at 2.80.
1223 # also we can turn off dnssec in systemd-resolved if we know the router is doing it.
1224 #
1225 # Also, reload of dnsmasq seems to break things, wifi
1226 # clients were not getting internet connectivity.
1227
1228 v /etc/init.d/dnsmasq restart
1229 fi
1230
1231 if $firewall_restart; then
1232 v /etc/init.d/firewall restart
1233 fi
1234
1235 # turn off luci
1236 /etc/init.d/uhttpd stop
1237 /etc/init.d/uhttpd disable
1238
1239 # this may just restart the network and take care of the network_restart below.
1240 if $wireless_restart; then
1241 v wifi
1242 fi
1243
1244 # todo: we should catch errors and still run this if needed
1245 if $network_restart; then
1246 reboot
1247 fi
1248
1249 exit 0