various fixes and improvements
[automated-distro-installer] / wrt-setup-local
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18
19 set -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 sshtp
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 config redirect
609 option name sshx2
610 option src wan
611 option src_dport 2205
612 option dest_port 22
613 option dest_ip $l.5
614 option dest lan
615 config rule
616 option src wan
617 option target ACCEPT
618 option dest_port 2205
619
620 config redirect
621 option name sshx3
622 option src wan
623 option src_dport 2207
624 option dest_port 22
625 option dest_ip $l.7
626 option dest lan
627 config rule
628 option src wan
629 option target ACCEPT
630 option dest_port 2207
631
632 config redirect
633 option name sshtp
634 option src wan
635 option src_dport 2208
636 option dest_port 22
637 option dest_ip $l.8
638 option dest lan
639 config rule
640 option src wan
641 option target ACCEPT
642 option dest_port 2208
643
644 config redirect
645 option name sshbb8
646 option src wan
647 option src_dport 2209
648 option dest_port 22
649 option dest_ip $l.9
650 option dest lan
651 config rule
652 option src wan
653 option target ACCEPT
654 option dest_port 2209
655
656 config redirect
657 option name icecast
658 option src wan
659 option src_dport 8000
660 option dest_port 8000
661 option dest_ip $l.2
662 option dest lan
663 config rule
664 option src wan
665 option target ACCEPT
666 option dest_port 8000
667
668 config rule
669 option name sshwrt
670 option src wan
671 option target ACCEPT
672 option dest_port 2220
673
674 config rule
675 option name wg
676 option src wan
677 option target ACCEPT
678 option dest_port $wgport
679 option proto udp
680
681
682 config redirect
683 option name httpkd
684 option src wan
685 option src_dport 80
686 option dest lan
687 option dest_ip $l.2
688 option proto tcp
689 config rule
690 option src wan
691 option target ACCEPT
692 option dest_port 80
693 option proto tcp
694
695 config redirect
696 option name httpskd
697 option src wan
698 option src_dport 443
699 option dest lan
700 option dest_ip $l.2
701 option proto tcp
702 config rule
703 option src wan
704 option target ACCEPT
705 option dest_port 443
706 option proto tcp
707
708 config redirect
709 option name httpskd8448
710 option src wan
711 option src_dport 8448
712 option dest lan
713 option dest_ip $l.2
714 option proto tcp
715 config rule
716 option src wan
717 option target ACCEPT
718 option dest_port 8448
719 option proto tcp
720
721
722 config redirect
723 option name syncthing
724 option src wan
725 option src_dport 22001
726 option dest_ip $l.8
727 option dest lan
728 config rule
729 option src wan
730 option target ACCEPT
731 option dest_port 22001
732
733
734 config rule
735 option name ssh-ipv6
736 option src wan
737 option dest lan
738 # note, using mac transform, we could allow all traffic to a host like this,
739 # replacing 1 as appropriate
740 #option dest_ip ::111:11ff:fe11:1111/::ffff:ffff:ffff:ffff
741 option dest_port 22
742 option target ACCEPT
743 option family ipv6
744
745 config rule
746 option name http-ipv6
747 option src wan
748 option dest lan
749 option dest_port 80
750 option target ACCEPT
751 option family ipv6
752
753 config rule
754 option name https-ipv6
755 option src wan
756 option dest lan
757 option dest_port 443
758 option target ACCEPT
759 option family ipv6
760
761 config rule
762 option name node-exporter
763 option src wan
764 option dest lan
765 option dest_port 9101
766 option target ACCEPT
767 option family ipv6
768
769 config rule
770 option name mail587-ipv6
771 option src wan
772 option dest lan
773 option dest_port 587
774 option target ACCEPT
775 option family ipv6
776
777 # include a file with users custom iptables rules
778 config include
779 option path /etc/firewall.user
780 option type 'restore'
781 option family 'ipv4'
782
783
784 EOF
785 }
786 firewall-cedit || firewall_restart=true
787
788 # not using wireguard for now
789 # if ! uci get firewall.@zone[1].network | grep wg0 &>/dev/null; then
790 # # cant mix cedit plus uci
791 # echo | cedit /etc/config/firewall ||:
792 # uci add_list firewall.@zone[1].network=wg0
793 # uci commit firewall
794 # firewall-cedit ||:
795 # firewall_restart=true
796 # fi
797
798
799
800 cedit /etc/hosts <<EOF
801 127.0.1.1 $hostname
802 EOF
803 # not sure this case statement is needed
804 case $hostname in
805 cmc)
806 cedit host /etc/hosts <<EOF
807 $l.1 $hostname
808 # 127.0.0.1 www.youtube.com
809 # 127.0.0.1 googlevideo.com
810 # 127.0.0.1 youtu.be
811 # 127.0.0.1 youtube-nocookie.com
812 # 127.0.0.1 youtube.com
813 # 127.0.0.1 youtube.googleapis.com
814 # 127.0.0.1 youtubei.googleapis.com
815 # 127.0.0.1 ytimg.com
816 # 127.0.0.1 ytimg.l.google.com
817 EOF
818 ;;
819 esac
820
821
822 #mail_host=$(grep -F mail.iankelling.org /etc/hosts | awk '{print $1}')
823 # if [[ $mail_host ]]; then
824 # sed -i '/^$mail_host/a mail.iankelling.org' /etc/hosts
825 # fi
826
827
828 uset dhcp.@dnsmasq[0].domain b8.nz
829 uset system.@system[0].hostname $hostname
830 uset dhcp.@dnsmasq[0].local
831
832 # uci doesnt seem to have a way to set an empty value,
833 # if you delete it, it goes back to the default. this seems
834 # to be a decent workaround.
835 # todo: setup /etc/resolv.conf to point to 127.0.0.1
836 uset dhcp.@dnsmasq[0].resolvfile /dev/null
837
838 # if dnsmasq happens to not send out a dns server,
839 # odhcpd will send one out like this:
840 # NetworkManager[953]: <info> [1614982580.5192] dhcp6 (wlan0): option dhcp6_name_servers => 'fd58:5801:8e02::1'
841 # but i dont want ipv6 dns, just keep it simple to ipv4.
842 # I know my isp doesnt have ipv6 right now,
843 # so just stop this thing.
844 # note: tried this, it didn't do anything:
845 # uset dhcp.@odhcpd[0].dns 10.2.0.1
846 /etc/init.d/odhcpd stop
847 /etc/init.d/odhcpd disable
848 # todo: make the above conditional on which server this is.
849
850 # avoid errors in log. current isp doesnt have ipv6
851 uset unbound.@unbound[0].protocol ip4_only
852
853 # todo: im not sure all these are needed, but they all look
854 # like good options.
855 # https://blog.cloudflare.com/dns-over-tls-for-openwrt/
856 # https://gist.github.com/vqiu/7b32d3a19a7a09d32e108d998de166c2
857 #https://blog.thestateofme.com/2018/04/04/howto-secure-your-dns-with-a-raspberry-pi-unbound-and-cloudflare-1-1-1-1/
858 #
859 # # i found that the zone example was having no effect on the config
860 # # here:
861 # https://github.com/openwrt/packages/blob/openwrt-19.07/net/unbound/files/README.md
862 #
863 # # todo: unbound-control, i'm not sure what the purpose of that thing is, some
864 # # kind of coordination with dhcp of dnsmasq, but what?
865 #
866 # note: for debugging, edit /etc/init.d/unbound, change
867 # procd_set_param command $PROG -d -c $UB_TOTAL_CONF
868 # to:
869 # procd_set_param command $PROG -vvv -d -c $UB_TOTAL_CONF
870
871 {
872 cat <<'EOF'
873 do-tcp: yes
874 prefetch: yes
875 qname-minimisation: yes
876 rrset-roundrobin: yes
877 use-caps-for-id: yes
878 do-ip6: no
879 private-domain: b8.nz
880 local-zone: "10.in-addr.arpa." transparent
881 access-control-view: 10.2.0.31/32 "youtube"
882 EOF
883
884 if $zblock; then
885 cat <<'EOF'
886 # amy, amyw, samsungtab
887 access-control-view: 10.2.0.8/32 "youtube"
888 access-control-view: 10.2.0.23/32 "youtube"
889 access-control-view: 10.2.0.32/32 "youtube"
890 EOF
891 fi
892 } | cedit /etc/unbound/unbound_srv.conf || restart_unbound=true
893
894
895 # dns based blocking vs ip based. with ip, same
896 # server can have multiple domains. in dns,
897 # you have to make sure clients to use the local dns.
898 # https dns will need to be blocked by ip in
899 # order to be comprehensive
900
901 cedit /etc/unbound/unbound_ext.conf <<'EOF' || restart_unbound=true
902 local-data-ptr: "10.2.0.1 cmc.b8.nz"
903 local-data-ptr: "10.2.0.2 kd.b8.nz"
904 local-data-ptr: "10.2.0.3 sy.b8.nz"
905 local-data-ptr: "10.2.0.4 wrt2.b8.nz"
906 local-data-ptr: "10.2.0.5 x2.b8.nz"
907 local-data-ptr: "10.2.0.6 x2w.b8.nz"
908 local-data-ptr: "10.2.0.7 syw.b8.nz"
909 local-data-ptr: "10.2.0.8 amy.b8.nz"
910 local-data-ptr: "10.2.0.9 bb8.b8.nz"
911 local-data-ptr: "10.2.0.12 demohost.b8.nz"
912 local-data-ptr: "10.2.0.14 wrt3.b8.nz"
913 local-data-ptr: "10.2.0.19 brother.b8.nz"
914 local-data-ptr: "10.2.0.23 amyw.b8.nz"
915 local-data-ptr: "10.2.0.25 hp.b8.nz"
916 local-data-ptr: "10.2.0.31 amazontab.b8.nz"
917 local-data-ptr: "10.2.0.32 samsungtab.b8.nz"
918 local-data-ptr: "10.173.0.2 transmission.b8.nz"
919 local-data-ptr: "10.173.8.1 defaultnn.b8.nz"
920 local-data-ptr: "10.173.8.2 nn.b8.nz"
921
922 forward-zone:
923 name: "."
924 # https://developers.cloudflare.com/1.1.1.1/1.1.1.1-for-families/setup-instructions/dns-over-https
925 forward-addr: 1.1.1.3@853#family.cloudflare-dns.com
926 forward-addr: 1.0.0.3@853#family.cloudflare-dns.com
927 forward-ssl-upstream: yes
928 forward-first: no
929
930 view:
931 name: "youtube"
932 local-zone: "googlevideo.com." refuse
933 local-zone: "video.google.com." refuse
934 local-zone: "youtu.be." refuse
935 local-zone: "youtube-nocookie.com." refuse
936 local-zone: "youtube-ui.l.google.com." refuse
937 local-zone: "youtube.com." refuse
938 local-zone: "youtube.googleapis.com." refuse
939 local-zone: "youtubeeducation.com." refuse
940 local-zone: "youtubei.googleapis.com." refuse
941 local-zone: "yt3.ggpht.com." refuse
942 local-zone: "youtubekids.com." refuse
943 # try global if no match in view
944 view-first: yes
945 EOF
946
947
948 if $restart_unbound; then
949 /etc/init.d/unbound restart
950 if ! unbound-checkconf; then
951 echo $0: error: unbound-checkconf failed >&2
952 exit 1
953 fi
954 fi
955
956
957 # disabled for now. i want to selectively enable it
958 # for specific hosts.
959 if [[ $(uci get adblock.global.adb_enabled) != 0 ]]; then
960 v uci set adblock.global.adb_enabled=0
961 uci commit adblock
962 /etc/init.d/adblock restart
963 fi
964 # https://github.com/openwrt/packages/tree/master/net/adblock/files
965 cat >/etc/crontabs/root <<'EOF'
966 0 06 * * * /etc/init.d/adblock reload
967 EOF
968
969
970 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
971
972 # sometimes /mnt/usb fails, cuz it's just a flash drive,
973 # so make sure we have this dir or else dnsmasq will fail
974 # to start.
975 mkdir -p /mnt/usb/tftpboot
976 cedit /etc/dnsmasq.conf <<EOF || dnsmasq_restart=true
977 # no dns
978 port=0
979 server=/b8.nz/#
980 ptr-record=1.0.2.10.in-addr.arpa.,cmc.b8.nz
981
982 # https://ret2got.wordpress.com/2018/01/19/how-your-ethereum-can-be-stolen-using-dns-rebinding/
983 stop-dns-rebind
984 rebind-domain-ok=b8.nz
985
986 # This says the ip of dns server.
987 # It is default if dnsmasq is doing dns, otherwise, we have to specify it.
988 # To see it in action, I ran this from a client machine:
989 # sudo dhcpcd -o domain_name_servers -T
990 dhcp-option=6,$l.1
991
992
993
994 # results from googling around dnsmasq optimizations
995 # about 50k in memory. router has 62 megs.
996 # in a browsing session, I probably won't ever do 5000 lookups
997 # before the ttl expiration or whatever does expiration.
998 cache-size=10000
999
1000 # ask all servers, use the one which responds first.
1001 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
1002 all-servers
1003
1004 # namebench benchmarks dns servers. google's dns was only
1005 # slightly less fast than some others, and I trust it more
1006 # to give accurate results, stay relatively fast, and
1007 # not do anythin too malicious, so just use that.
1008 # download namebench and run it like this:
1009 # 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
1010 # google
1011 server=1.1.1.3
1012 server=1.0.0.3
1013 server=2606:4700:4700::1113
1014 server=2606:4700:4700::1003
1015
1016 server=10.2.0.1
1017 # server=8.8.4.4
1018 # server=8.8.8.8
1019 # server=2001:4860:4860::8888
1020 # server=2001:4860:4860::8844
1021
1022
1023 # to fixup existin ips, on the client you can do
1024 # sudo dhclient -r; sudo dhclient <interface-name>
1025
1026 # default dhcp range is 100-150
1027 # bottom port, iPXE (PCI 03:00.0) in seabios boot menu
1028 dhcp-host=c8:60:00:31:6b:75,set:kd,$l.2,kd
1029 dhcp-host=94:05:bb:1e:2c:2e,set:sy,$l.3,sy
1030 # top port, iPXE (PCI 04:00.0) in seabios boot menu
1031 #dhcp-host=c8:60:00:2b:15:07,set:kd,$l.2,kd
1032 # 4 is reserved for a staticly configured host wrt2
1033 # old x2 with bad fan
1034 #dhcp-host=00:1f:16:16:39:24,set:x2,$l.5,x2
1035 dhcp-host=f0:de:f1:81:ec:88,set:x2,$l.5,x2
1036 dhcp-host=c4:8e:8f:44:f5:63,set:x2w,$l.6,x2w
1037 dhcp-host=34:7d:f6:ed:ec:07,set:syw,$l.7,syw
1038 dhcp-host=80:fa:5b:1c:6e:cf,set:amy,$l.8,amy
1039 # This is so fai can have an explicit name to use for testing,
1040 # or else any random machine which did a pxe boot would get
1041 # reformatted. The mac is from doing a virt-install, cancelling it,
1042 # and copying the generated mac, so it should be randomish.
1043 dhcp-host=52:54:00:9c:ef:ad,set:demohost,$l.12,demohost
1044 dhcp-host=62:03:cb:a8:3e:a3,set:trp,$1.13,trp
1045 dhcp-host=00:1f:16:14:01:d8,set:x3,$l.18,x3
1046 # BRN001BA98CA823 in dhcp logs
1047 dhcp-host=00:1b:a9:8c:a8:23,set:brother,$l.19,brother
1048
1049 dhcp-host=00:26:b6:f7:d4:d8,set:amyw,$l.23,amyw
1050 dhcp-host=38:63:bb:07:5a:f9,set:hp,$l.25,hp
1051 dhcp-host=00:26:b6:f6:0f:e9,set:frodow,$l.28,frodow
1052 dhcp-host=6c:56:97:88:7b:74,set:amazontab,$l.31,amazontab
1053 dhcp-host=0a:8a:9b:cf:b5:ec,set:samsungtab,$l.32,samsungtab
1054
1055
1056
1057 # faiserver vm
1058 dhcp-host=52:54:00:56:09:f9,set:faiserver,$l.15,faiserver
1059
1060 # This is the ip it picks by default if dhcp fails,
1061 # so might as well use it.
1062 # hostname is the name it uses according to telnet
1063 dhcp-host=b4:75:0e:94:29:ca,set:switch9429ca,$l.251,switch9429ca
1064
1065 # template
1066 # dhcp-host=,$l.,
1067
1068 # Just leave the tftp server up even if we aren't doing pxe boot.
1069 # It has no sensitive info.
1070 enable-tftp=br-lan
1071 tftp-root=/mnt/usb/tftpboot
1072 dhcp-optsfile=/etc/dnsmasq-dhcpopts.conf
1073
1074 #log-queries=extra
1075 EOF
1076
1077
1078 if $dnsmasq_restart && ! $dev2; then
1079 # todo: can our ptr records be put in /etc/hosts?
1080 # eg: user normal /etc/hosts records, and they wont be used for A resolution
1081 # due to the other settings, but will be used for ptr? then maybe
1082 # we dont have to restart dnsmasq for a dns update?
1083 #
1084 # interesing link:
1085 # https://www.redpill-linpro.com/techblog/2019/08/27/evaluating-local-dnssec-validators.html#toggling-dnssec-validation-1
1086 # we could turn on dnssec validation when wrt gets dnsmasq > 2.80. currently at 2.80.
1087 # also we can turn off dnssec in systemd-resolved if we know the router is doing it.
1088 #
1089 # Also, reload of dnsmasq seems to break things, wifi
1090 # clients were not getting internet connectivity.
1091
1092 v /etc/init.d/dnsmasq restart
1093 fi
1094
1095 if $firewall_restart; then
1096 v /etc/init.d/firewall restart
1097 fi
1098
1099 # this may just restart the network and take care of the network_restart below.
1100 if $wireless_restart; then
1101 v wifi
1102 fi
1103
1104 # todo: we should catch errors and still run this if needed
1105 if $network_restart; then
1106 reboot
1107 fi
1108
1109 exit 0