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