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