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