2 # Copyright (C) 2016 Ian Kelling
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.
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.
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.
19 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
24 usage: ${0##*/} [-h] [-t 2|3|test] [-m WIRELESS_MAC]
25 setup my router in general: dhcp, dns, etc.
27 Type 2 or 3 is for setting up a backup device, there are two kinds so
28 that you can switch the main device to a backup, then a backup to the
29 main. Type test is for setting up a testing device.
31 Passing an empty string for WIRELESS_MAC will cause the device's native
43 firewall_restart
=false
49 if [[ -e /root
/router-secrets
]]; then
51 source /root
/router-secrets
53 rmac
=$
(cat /sys
/class
/net
/eth
0/address
)
55 hostname
=${rhost[$rmac]}
61 while getopts hm
:t
: opt
; do
68 libremanage_host
=$hostname
81 *) echo "$0: unexpected arg to -t: $*" >&2; usage
1 ;;
85 *) echo "$0: Internal error! unexpected args: $*" >&2 ; usage
1 ;;
88 shift "$((OPTIND-1))" # Discard the options and sentinel --
90 if [[ ! $mac ]] && ! $test && $secrets; then
91 # if we wanted to increment it
92 #mac=${mac:0: -1}$((${mac: -1} + 2))
96 if (( $# != 0 )); then
110 # background: upgrading all packages is not recommended because it
111 # doesn't go into the firmware. build new firmware if you want
112 # lots of upgrades. I think /tmp/opkg-lists is a pre openwrt 14 location.
113 f
=(/var
/opkg-lists
/*)
114 if ! (( $
(date -r $f +%s
) + 60*60*24 > $
(date +%s
) )); then
115 if ! opkg update
; then
116 echo "$0: warning: opkg update failed" >&2
127 if [[ ! $
(opkg list-installed
"$pname") ]]; then
132 if [[ $to_install ]]; then
133 opkg
install ${to_install[@]}
142 ######### uci example:#######
143 # # https://wiki.openwrt.org/doc/uci
144 # wan_index=$(uci show firewall | sed -rn 's/firewall\.@zone\[([0-9])+\]\.name=wan/\1/p')
145 # wan="firewall.@zone[$wan_index]"
146 # if [[ $(uci get firewall.@forwarding[0].dest) != $forward_dest ]]; then
148 # v uci set firewall.@forwarding[0].dest=$forward_dest
149 # uci commit firewall
150 # firewall_restart=true
152 ####### end uci example #####
155 printf "+ uset %s\n" "$*"
158 local service
="${key%%.*}"
159 restart_var
=${service}_restart
160 if [[ ! ${!restart_var} ]]; then
161 eval $restart_var=false
163 if [[ $
(uci get
"$key") != "$val" ]]; then
164 v uci
set "$key"="$val"
166 eval $restart_var=true
171 printf "+ udel %s\n" "$*"
174 local service
="${key%%.*}"
175 restart_var
=${service}_restart
176 if [[ ! ${!restart_var} ]]; then
177 eval $restart_var=false
179 if uci get
"$key" &>/dev
/null
; then
180 v uci
set "$key"="$val"
182 eval $restart_var=true
193 elif [[ $hostname == cmc
]]; then
209 : ${key:=pictionary49}
215 passwd
-l root ||
: #already locked fails
217 sed -ibak '/^root:/d' /etc
/shadow
218 # /root/router created by manually running passwd then copying the resulting
219 # line. We have no mkpasswd on wrt/librecmc, then we scp it in.
220 cat /root
/router
>>/etc
/shadow
221 # otherwise, serial console gets root login with no password
222 uset system.@system
[0].ttylogin
1
226 cat >/usr
/bin
/archlike-pxe-mount
<<'EOFOUTER'
228 # symlinks are collapsed for nfs mount points, so use a bind mount.
229 # tried putting this in /etc/config/fstab,
230 # then doing block mount, it didn't work. This doesn't persist across reboots,
231 # todo: figure that out
233 for d in /run/{arch,parabola}iso/bootmnt; do
234 cat >>/etc/fstab <<EOF
235 /mnt/usb/tftpboot $d none bind 0 0
237 mount | grep $d &>/dev/null || mount $d
239 /etc/init.d/nfsd restart
241 chmod +x
/usr
/bin
/archlike-pxe-mount
243 sed -i '/^root:/s,/bin/ash$,/bin/bash,' /etc
/passwd
247 uset dropbear.@dropbear
[0].PasswordAuth
0
248 uset dropbear.@dropbear
[0].RootPasswordAuth
0
249 uset dropbear.@dropbear
[0].Port
2220
250 if ! cmp -s /root
/dropbear_rsa_host_key
/etc
/dropbear
/dropbear_rsa_host_key
; then
251 cp /root
/dropbear_rsa_host_key
/etc
/dropbear
/dropbear_rsa_host_key
252 dropbear_restart
=true
255 if $dropbear_restart; then
256 v
/etc
/init.d
/dropbear restart
260 uset network.lan.ipaddr
$l.
$lanip
261 uset network.lan.netmask
$mask
263 uset network.lan.gateway
$l.1
264 uset network.wan.proto none
265 uset network.wan6.proto none
266 /etc
/init.d
/dnsmasq stop
267 /etc
/init.d
/dnsmasq disable
268 /etc
/init.d
/odhcpd stop
269 /etc
/init.d
/odhcpd disable
270 rm -f /etc
/resolv.conf
271 cat >/etc
/resolv.conf
<<'EOF'
276 # things i tried to keep dnsmasq running but not enabled except local dns,
277 # but it didnt work right and i dont need it anyways.
278 # uset dhcp.wan.ignore $dev2 # default is false
279 # uset dhcp.lan.ignore $dev2 # default is false
280 # uset dhcp.@dnsmasq[0].interface lo
281 # uset dhcp.@dnsmasq[0].localuse 0
282 # uset dhcp.@dnsmasq[0].resolvfile /etc/dnsmasq.conf
283 # uset dhcp.@dnsmasq[0].noresolv 1
284 # todo: populate /etc/resolv.conf with a static value
287 # these are the defaults
288 uset network.lan.gateway
''
289 uset network.wan.proto dhcp
290 uset network.wan6.proto dhcpv6
291 /etc
/init.d
/dnsmasq start
292 # todo: figure out why this returns 1
293 /etc
/init.d
/dnsmasq
enable ||
:
294 /etc
/init.d
/odhcpd start
295 /etc
/init.d
/odhcpd
enable
298 wireless_restart
=false
300 uset wireless.default_radio
$x.ssid
"$ssid"
301 uset wireless.default_radio
$x.key
$key
302 uset wireless.default_radio
$x.encryption psk2
304 uset wireless.default_radio
$x.macaddr
$macpre$
((macsuf
+ 2*x
))
306 # secondary device has wireless disabled
307 uset wireless.radio
$x.disabled
$dev2
310 if $wireless_restart; then
316 # usb, screen, relay are for libremanage
318 v pi kmod-usb-storage block-mount kmod-fs-ext4 nfs-kernel-server \
319 tcpdump openvpn-openssl adblock libusb-compat
/root
/relay_1.0
-1_mips_24kc.ipk \
320 screen kmod-usb-serial-cp210x kmod-usb-serial-ftdi rsync
322 cat >/etc
/libremanage.conf
<<EOF
323 ${libremanage_host}_type=switch
324 ${libremanage_host}_channel=1
329 v
/etc
/init.d
/fstab
enable ||
:
331 # rebooting makes mounting work, but comparing lsmod,
332 # i'm guessing this will too. todo, test it.
333 # 255 == module already loaded
334 for mod
in scsi_mod sd_mod
; do v modprobe
$mod ||
[[ $?
== 255 ]]; done
336 # for archlike pxe. The default settings in the installer expect to find
337 # the NFS at one of these dirs
338 mkdir
-p /run
/archiso
/bootmnt
339 mkdir
-p /run
/parabolaiso
/bootmnt
341 # todo: at some later time, i found /mnt/usb not mounted, watch to see if
342 # that is the case after running this or rebooting.
343 # wiki says safe to do in case of fstab changes:
345 ## ian: usb broke on old router. if that happens, can just comment this to disable problems
346 # echo | cedit /etc/config/fstab ||:
347 v cedit
/etc
/config
/fstab
<<EOF || { v block umount; v block mount; }
348 config global automount
353 # /overlay is an / overlay mount for installing extra packages, etc.
354 # https://openwrt.org/docs/guide-user/additional-software/extroot_configuration
355 option target /mnt/usb
356 # option target /overlay
357 option device /dev/sda1
359 option options rw,async,noatime,nodiratime
364 # ian: disabled because afaik I don't need it, no benefit.
365 # config global autoswap
366 # option from_fstab 1
370 # option device /dev/sda1
376 # exportfs -ra wont cut it when its the same path, but now a bind mount
377 # todo: restart nfs when nfs is enabled?
378 #cedit /etc/exports <<EOF || v /etc/init.d/nfsd restart ||:
379 cedit
/etc
/exports
<<EOF ||:
380 /mnt/usb $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
382 /run/archiso/bootmnt $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
383 /run/parabolaiso/bootmnt $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
387 # todo: enable nfs when we need it only.
388 # v /etc/init.d/portmap start
389 # v /etc/init.d/nfsd start
390 # v /etc/init.d/portmap enable
391 # v /etc/init.d/nfsd enable
400 ########## openvpn exampl
401 ########## missing firewall settings for routing lan
403 # v /etc/init.d/openvpn start
404 # v /etc/init.d/openvpn enable
406 # # from https://wiki.openwrt.org/doc/uci/firewall
407 # # todo: not sure if /etc/init.d/network needs restarting.
408 # # I did, and I had to restart the vpn afterwards.
409 # # This maps a uci interface to a real interface which is
410 # # managed outside of uci.
411 # v cedit /etc/config/network <<'EOF' ||:
412 # config interface 'tun0'
413 # option ifname 'tun0'
414 # option proto 'none'
416 # v cedit /etc/config/openvpn <<'EOF' || v /etc/init.d/openvpn restart
417 # config openvpn my_client_config
419 # option config /etc/openvpn/client.conf
427 v cedit
/etc
/config
/network
<<EOF || v /etc/init.d/network reload
428 config 'route' 'transmission'
429 option 'interface' 'lan'
430 option 'target' '10.173.0.0'
431 option 'netmask' '255.255.0.0'
432 option 'gateway' '$l.3'
434 option interface 'wg0'
435 option proto 'wireguard'
436 option private_key '$(cat /root/wg.key)'
437 option listen_port $wgport
438 list addresses '10.3.0.1/24'
439 list addresses 'fdfd::1/64'
442 config wireguard_wg0 'wgclient'
443 option public_key '3q+WJGrm85r59NgeXOIvppxoW4ux/+koSw6Fee1c1TI='
444 option preshared_key '$(cat /root/wg.psk)'
445 list allowed_ips '10.3.0.2/24'
446 list allowed_ips 'fdfd::2/64'
454 v cedit
host /etc
/config
/firewall
<<EOF
464 v cedit
host /etc
/config
/firewall
<<EOF
475 v cedit
/etc
/config
/firewall
<<EOF
484 option src_dport 2202
491 option dest_port 2202
496 option src_dport 2205
503 option dest_port 2205
508 option src_dport 2207
515 option dest_port 2207
520 option src_dport 2208
527 option dest_port 2208
534 option dest_port 2220
540 option dest_port $wgport
547 option src_dport 1196
554 option dest_port 1196
585 option name syncthing
587 option src_dport 22001
593 option dest_port 22001
600 # note, using mac transform, we could allow all traffic to a host like this,
601 # replacing 1 as appropriate
602 #option dest_ip ::111:11ff:fe11:1111/::ffff:ffff:ffff:ffff
608 option name http-ipv6
616 option name https-ipv6
624 option name node-exporter
627 option dest_port 9101
632 option name mail587-ipv6
641 firewall-cedit || firewall_restart
=true
643 if ! uci get firewall.@zone
[1].network |
grep wg0
&>/dev
/null
; then
644 # cant mix cedit plus uci
645 echo | cedit
/etc
/config
/firewall ||
:
646 uci add_list firewall.@zone
[1].network
=wg0
649 firewall_restart
=true
654 v cedit
/etc
/hosts
<<EOF || dnsmasq_restart=true
659 2600:3c00::f03c:91ff:fe6d:baf8 li
661 2001:ba8:1f1:f0c9::2 bk
663 # netns creation looks for next free subnet starting at 10.173, but I only
664 # use one, and I would keep this one as the first created.
665 10.173.0.2 transmission
669 v cedit
host /etc
/hosts
<<EOF || dnsmasq_restart=true
670 $l.1 $hostname t.b8.nz
674 v cedit
host /etc
/dnsmasq.conf
<<EOF || dnsmasq_restart=true
686 v cedit
host /etc
/hosts
<<EOF || dnsmasq_restart=true
694 v cedit
host /etc
/dnsmasq.conf
<<EOF || dnsmasq_restart=true
697 server=/frodo.b8.nz/#
705 #mail_host=$(grep -F mail.iankelling.org /etc/hosts | awk '{print $1}')
706 # if [[ $mail_host ]]; then
707 # sed -i '/^$mail_host/a mail.iankelling.org' /etc/hosts
711 uset dhcp.@dnsmasq
[0].domain b8.nz
712 uset dhcp.@dnsmasq
[0].
local /b8.nz
/
713 uset system.@system
[0].hostname
$hostname
715 if [[ $
(uci get adblock.global.adb_enabled
) != 1 ]]; then
716 v uci
set adblock.global.adb_enabled
=1
718 /etc
/init.d
/adblock restart
720 # https://github.com/openwrt/packages/tree/master/net/adblock/files
721 cat >/etc
/crontabs
/root
<<'EOF'
722 0 06 * * * /etc/init.d/adblock reload
726 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
728 # sometimes /mnt/usb fails, cuz it's just a flash drive,
729 # so make sure we have this dir or else dnsmasq will fail
731 mkdir
-p /mnt
/usb
/tftpboot
732 v cedit
/etc
/dnsmasq.conf
<<EOF || dnsmasq_restart=true
733 server=/dmarctest.b8.nz/#
734 server=/_domainkey.b8.nz/#
735 server=/_dmarc.b8.nz/#
738 mx-host=b8.nz,mail.iankelling.org,10
739 txt-record=b8.nz,"v=spf1 a ?all"
742 # https://ret2got.wordpress.com/2018/01/19/how-your-ethereum-can-be-stolen-using-dns-rebinding/
745 # this says the ip of default gateway and dns server,
746 # but I think they are unneded and default
752 # results from googling around dnsmasq optimizations
753 # about 50k in memory. router has 62 megs.
754 # in a browsing session, I probably won't ever do 5000 lookups
755 # before the ttl expiration or whatever does expiration.
758 # ask all servers, use the one which responds first.
759 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
762 # namebench benchmarks dns servers. google's dns was only
763 # slightly less fast than some others, and I trust it more
764 # to give accurate results, stay relatively fast, and
765 # not do anythin too malicious, so just use that.
766 # download namebench and run it like this:
767 # 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
771 server=2001:4860:4860::8888
772 server=2001:4860:4860::8844
775 # to fixup existin ips, on the client you can do
776 # sudo dhclient -r; sudo dhclient <interface-name>
778 # default dhcp range is 100-150
779 # bottom port, iPXE (PCI 03:00.0) in seabios boot menu
780 dhcp-host=c8:60:00:31:6b:75,set:kd,$l.2,kd
781 # top port, iPXE (PCI 04:00.0) in seabios boot menu
782 #dhcp-host=c8:60:00:2b:15:07,set:kd,$l.2,kd
783 dhcp-host=00:26:18:97:bb:16,set:frodo,$l.3,frodo
784 # 4 is reserved for a staticly configured host.
785 # old x2 with bad fan
786 #dhcp-host=00:1f:16:16:39:24,set:x2,$l.5,x2
787 dhcp-host=f0:de:f1:81:ec:88,set:x2,$l.5,x2
788 # This is so fai can have an explicit name to use for testing,
789 # or else any random machine which did a pxe boot would get
790 # reformatted. The mac is from doing a virt-install, cancelling it,
791 # and copying the generated mac, so it should be randomish.
792 dhcp-host=52:54:00:9c:ef:ad,set:demohost,$l.6,demohost
793 dhcp-host=00:1f:16:14:01:d8,set:tp,$l.7,x3
794 dhcp-host=80:fa:5b:1c:6e:cf,set:tp,$l.8,tp
797 dhcp-host=52:54:00:56:09:f9,set:faiserver,$l.15,faiserver
799 # This is the ip it picks by default if dhcp fails,
800 # so might as well use it.
801 # hostname is the name it uses according to telnet
802 dhcp-host=b4:75:0e:94:29:ca,set:switch9429ca,$l.251,switch9429ca
807 # Just leave the tftp server up even if we aren't doing pxe boot.
808 # It has no sensitive info.
810 tftp-root=/mnt/usb/tftpboot
811 dhcp-optsfile=/etc/dnsmasq-dhcpopts.conf
817 if $dnsmasq_restart && ! $dev2; then
818 v
/etc
/init.d
/dnsmasq restart
821 if $firewall_restart; then
822 v
/etc
/init.d
/firewall restart
826 # todo: we should catch errors and still run this if needed
827 if $network_restart; then