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