ipv6 allow firewall
[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 set -eE -o pipefail
19 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
20
21
22 usage() {
23 cat <<EOF
24 usage: ${0##*/} [-h] [-t 2|test] [-m WIRELESS_MAC]
25 setup my router in general: dhcp, dns, etc.
26
27 Type 2 is for setting up a backup device. Type test is for setting up a
28 testing device.
29
30 Passing an empty string for WIRELESS_MAC will cause the device's native
31 mac to be used.
32
33 EOF
34
35 exit $1
36 }
37
38
39 dev2=false
40 test=false
41 hostname=wrt
42 libremanage_host=wrt2
43
44 lanip=1
45 while getopts hm:t: opt; do
46 case $opt in
47 h) usage ;;
48 t)
49 case $2 in
50 2)
51 dev2=true
52 lanip=4
53 hostname=wrt2
54 libremanage_host=wrt
55 ;;
56 test)
57 test=true
58 ;;
59 *) echo "$0: unexpected arg to -t: $*" >&2; usage 1 ;;
60 esac
61 ;;
62 m) mac=$OPTARG ;;
63 *) echo "$0: Internal error! unexpected args: $*" >&2 ; usage 1 ;;
64 esac
65 done
66 shift "$((OPTIND-1))" # Discard the options and sentinel --
67
68 if [[ ! $mac ]] && ! $test; then
69 # if we wanted to increment it
70 #WIRELESSMAC=${WIRELESSMAC:0: -1}$((${WIRELESSMAC: -1} + 2))
71 mac=$WIRELESSMAC
72 fi
73
74 if (( $# != 0 )); then
75 usage 1
76 fi
77
78
79 macpre=${mac:0: -1}
80 macsuf=${mac: -1}
81
82 pmirror() {
83 # background: upgrading all packages is not recommended because it
84 # doesn't go into the firmware. build new firmware if you want
85 # lots of upgrades. I think /tmp/opkg-lists is a pre openwrt 14 location.
86 f=(/var/opkg-lists/*)
87 if ! (( $(date -r $f +%s) + 60*60*24 > $(date +%s) )); then
88 opkg update
89 fi
90 }
91
92 pi() {
93 for x in "$@"; do
94 if [[ ! $(opkg list-installed "$x") ]]; then
95 pmirror
96 opkg install "$@"
97 fi
98 done
99 }
100
101 v() {
102 printf "+ %s\n" "$*"
103 "$@"
104 }
105
106 ######### uci example:#######
107 # # https://wiki.openwrt.org/doc/uci
108 # wan_index=$(uci show firewall | sed -rn 's/firewall\.@zone\[([0-9])+\]\.name=wan/\1/p')
109 # wan="firewall.@zone[$wan_index]"
110 # if [[ $(uci get firewall.@forwarding[0].dest) != $forward_dest ]]; then
111 # # default is wan
112 # v uci set firewall.@forwarding[0].dest=$forward_dest
113 # uci commit firewall
114 # firewall_restart=true
115 # fi
116 ####### end uci example #####
117
118 uset() {
119 local key="$1"
120 local val="$2"
121 local service="${key%%.*}"
122 restart_var=${service}_restart
123 if [[ ! ${!restart_var} ]]; then
124 eval $restart_var=false
125 fi
126 if [[ $(uci get "$key") != "$val" ]]; then
127 v uci set "$key"="$val"
128 uci commit $service
129 eval $restart_var=true
130 fi
131 }
132
133
134 ### network config
135 ###
136 ssid="check out gnu.org"
137 lan=10.0.0.0
138 if $test; then
139 ssid="gnuv3"
140 lan=10.1.0.0
141 fi
142 mask=255.255.0.0
143 cidr=16
144 l=${lan%.0}
145
146 passwd -l root ||: #already locked fails
147
148 sed -ibak '/^root:/d' /etc/shadow
149 # /root/router created by manually running passwd then copying the resulting
150 # line. We have no mkpasswd on wrt/librecmc, then we scp it in.
151 cat /root/router >>/etc/shadow
152 # otherwise, serial console gets root login with no password
153 uset system.@system[0].ttylogin 1
154
155
156 cat >/usr/bin/archlike-pxe-mount <<'EOFOUTER'
157 #!/bin/bash
158 # symlinks are collapsed for nfs mount points, so use a bind mount.
159 # tried putting this in /etc/config/fstab,
160 # then doing block mount, it didn't work. This doesn't persist across reboots,
161 # todo: figure that out
162 rm -f /etc/fstab
163 for d in /run/{arch,parabola}iso/bootmnt; do
164 cat >>/etc/fstab <<EOF
165 /mnt/usb/tftpboot $d none bind 0 0
166 EOF
167 mount | grep $d &>/dev/null || mount $d
168 done
169 /etc/init.d/nfsd restart
170 EOFOUTER
171 chmod +x /usr/bin/archlike-pxe-mount
172
173 sed -i '/^root:/s,/bin/ash$,/bin/bash,' /etc/passwd
174 # usb, screen, relay are for libremanage
175 v pi kmod-usb-storage block-mount kmod-fs-ext4 nfs-kernel-server \
176 tcpdump openvpn-openssl adblock libusb-compat /root/relay_1.0-1_mips_24kc.ipk \
177 screen kmod-usb-serial-cp210x kmod-usb-serial-ftdi
178
179 cat >/etc/libremanage.conf <<EOF
180 ${libremanage_host}_type=switch
181 ${libremanage_host}_channel=1
182 EOF
183
184
185
186 v /etc/init.d/fstab enable ||:
187
188 # rebooting makes mounting work, but comparing lsmod,
189 # i'm guessing this will too. todo, test it.
190 # 255 == module already loaded
191 for mod in scsi_mod sd_mod; do v modprobe $mod || [[ $? == 255 ]]; done
192
193 # for archlike pxe. The default settings in the installer expect to find
194 # the NFS at one of these dirs
195 mkdir -p /run/archiso/bootmnt
196 mkdir -p /run/parabolaiso/bootmnt
197
198 # todo: at some later time, i found /mnt/usb not mounted, watch to see if
199 # that is the case after running this or rebooting.
200 # wiki says safe to do in case of fstab changes:
201
202 ## ian: usb broke on old router. if that happens, can just comment this to disable problems
203 echo | cedit /etc/config/fstab ||:
204 cedit /etc/config/fstab <<EOF || { v block umount; v block mount; }
205 config global automount
206 option from_fstab 1
207 option anon_mount 1
208
209 config mount
210 # /overlay is an / overlay mount for installing extra packages, etc.
211 # https://openwrt.org/docs/guide-user/additional-software/extroot_configuration
212 option target /mnt/usb
213 # option target /overlay
214 option device /dev/sda1
215 option fstype ext4
216 option options rw,async,noatime,nodiratime
217 option enabled 0
218 EOF
219
220
221 # ian: disabled because afaik I don't need it, no benefit.
222 # config global autoswap
223 # option from_fstab 1
224 # option anon_swap 1
225
226 # config swap
227 # option device /dev/sda1
228 # option enabled 1
229
230
231
232
233 # exportfs -ra wont cut it when its the same path, but now a bind mount
234 # todo: restart nfs when nfs is enabled?
235 #cedit /etc/exports <<EOF || v /etc/init.d/nfsd restart ||:
236 cedit /etc/exports <<EOF ||:
237 /mnt/usb $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
238 # for arch pxe
239 /run/archiso/bootmnt $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
240 /run/parabolaiso/bootmnt $lan/$cidr(rw,no_root_squash,insecure,sync,no_subtree_check)
241 EOF
242
243
244 # todo: enable nfs when we need it only.
245 # v /etc/init.d/portmap start
246 # v /etc/init.d/nfsd start
247 # v /etc/init.d/portmap enable
248 # v /etc/init.d/nfsd enable
249
250
251
252
253
254
255
256 uset dropbear.@dropbear[0].PasswordAuth 0
257 uset dropbear.@dropbear[0].RootPasswordAuth 0
258 uset dropbear.@dropbear[0].Port 2220
259 if ! cmp -s /root/dropbear_rsa_host_key /etc/dropbear/dropbear_rsa_host_key; then
260 cp /root/dropbear_rsa_host_key /etc/dropbear/dropbear_rsa_host_key
261 dropbear_restart=true
262 fi
263
264 wireless_restart=false
265 key=pictionary49
266 for x in 0 1; do
267 uset wireless.default_radio$x.ssid "$ssid"
268 uset wireless.default_radio$x.key $key
269 uset wireless.default_radio$x.encryption psk2
270 if [[ $mac ]]; then
271 uset wireless.default_radio$x.macaddr $macpre$((macsuf + 2*x))
272 fi
273 # secondary device has wireless disabled
274 uset wireless.radio$x.disabled $dev2
275 done
276
277 if $wireless_restart; then
278 v wifi
279 fi
280
281
282 ########## openvpn exampl
283 ########## missing firewall settings for routing lan
284 ########## traffic
285 # v /etc/init.d/openvpn start
286 # v /etc/init.d/openvpn enable
287
288 # # from https://wiki.openwrt.org/doc/uci/firewall
289 # # todo: not sure if /etc/init.d/network needs restarting.
290 # # I did, and I had to restart the vpn afterwards.
291 # # This maps a uci interface to a real interface which is
292 # # managed outside of uci.
293 # v cedit /etc/config/network <<'EOF' ||:
294 # config interface 'tun0'
295 # option ifname 'tun0'
296 # option proto 'none'
297 # EOF
298 # v cedit /etc/config/openvpn <<'EOF' || v /etc/init.d/openvpn restart
299 # config openvpn my_client_config
300 # option enabled 1
301 # option config /etc/openvpn/client.conf
302 # EOF
303
304
305
306 v cedit /etc/config/network <<EOF || v /etc/init.d/network reload
307 config 'route' 'transmission'
308 option 'interface' 'lan'
309 option 'target' '10.173.0.0'
310 option 'netmask' '255.255.0.0'
311 option 'gateway' '$l.3'
312 EOF
313
314 firewall_restart=false
315 v cedit /etc/config/firewall <<EOF || firewall_restart=true
316
317
318 config redirect
319 option name ssh
320 option src wan
321 option src_dport 22
322 option dest_ip $l.8
323 option dest lan
324 config rule
325 option src wan
326 option target ACCEPT
327 option dest_port 22
328
329 config redirect
330 option name sshalt
331 option src wan
332 option src_dport 2222
333 option dest_port 22
334 option dest_ip $l.3
335 option dest lan
336 config rule
337 option src wan
338 option target ACCEPT
339 option dest_port 2222
340
341 config rule
342 option src wan
343 option target ACCEPT
344 option dest_port 2220
345
346
347 config redirect
348 option src wan
349 option src_dport 443
350 option dest lan
351 option dest_ip $l.8
352 option proto tcp
353 config rule
354 option src wan
355 option target ACCEPT
356 option dest_port 443
357 option proto tcp
358
359 config redirect
360 option src wan
361 option src_dport 1196
362 option dest lan
363 option dest_ip $l.8
364 option proto udp
365 config rule
366 option src wan
367 option target ACCEPT
368 option dest_port 1196
369 option proto udp
370
371
372 config redirect
373 option src wan
374 option src_dport 80
375 option dest lan
376 option dest_ip $l.8
377 option proto tcp
378 config rule
379 option src wan
380 option target ACCEPT
381 option dest_port 80
382 option proto tcp
383
384 config redirect
385 option name syncthing
386 option src wan
387 option src_dport 22001
388 option dest_ip $l.8
389 option dest lan
390 config rule
391 option src wan
392 option target ACCEPT
393 option dest_port 22001
394
395
396 config rule
397 option name ssh-ipv6
398 option src wan
399 option dest lan
400 # note, using mac transform, we could allow all traffic to a host like this,
401 # replacing 1 as appropriate
402 #option dest_ip ::111:11ff:fe11:1111/::ffff:ffff:ffff:ffff
403 option dest_port 22
404 option target ACCEPT
405 option family ipv6
406
407 config rule
408 option name http-ipv6
409 option src wan
410 option dest lan
411 option dest_port 80
412 option target ACCEPT
413 option family ipv6
414
415 config rule
416 option name http-ipv6
417 option src wan
418 option dest lan
419 option dest_port 80
420 option target ACCEPT
421 option family ipv6
422
423 config rule
424 option name node-exporter
425 option src wan
426 option dest lan
427 option dest_port 9101
428 option target ACCEPT
429 option family ipv6
430
431 config rule
432 option name mail587-ipv6
433 option src wan
434 option dest lan
435 option dest_port 587
436 option target ACCEPT
437 option family ipv6
438
439
440 EOF
441
442
443
444
445 dnsmasq_restart=false
446 v cedit /etc/hosts <<EOF || dnsmasq_restart=true
447 127.0.1.1 wrt
448 $l.1 wrt
449 $l.2 kd
450 $l.3 frodo
451 $l.4 wrt2
452 $l.5 x2
453 $l.6 demohost
454 $l.7 x3
455 $l.8 tp b8.nz faiserver
456 $l.9 bb8
457 72.14.176.105 li
458
459 # netns creation looks for next free subnet starting at 10.173, but I only
460 # use one, and I would keep this one as the first created.
461 10.173.0.2 transmission
462 EOF
463
464 #mail_host=$(grep -F mail.iankelling.org /etc/hosts | awk '{print $1}')
465 # if [[ $mail_host ]]; then
466 # sed -i '/^$mail_host/a mail.iankelling.org' /etc/hosts
467 # fi
468
469
470 # avoid using the dns servers that my isp tells me about.
471 if [[ $(uci get dhcp.@dnsmasq[0].resolvfile 2>/dev/null) ]]; then
472 # default is '/tmp/resolv.conf.auto', we switch to the dnsmasq default of
473 # /etc/resolv.conf. not sure why I did this.
474 v uci delete dhcp.@dnsmasq[0].resolvfile
475 uci commit dhcp
476 dnsmasq_restart=true
477 fi
478
479 uset dhcp.@dnsmasq[0].domain b8.nz
480 uset dhcp.@dnsmasq[0].local /b8.nz/
481 uset system.@system[0].hostname $hostname
482
483 if [[ $(uci get adblock.global.adb_enabled) != 1 ]]; then
484 v uci set adblock.global.adb_enabled=1
485 uci commit adblock
486 /etc/init.d/adblock restart
487 fi
488 # https://github.com/openwrt/packages/tree/master/net/adblock/files
489 cat >/etc/crontabs/root <<'EOF'
490 0 06 * * * /etc/init.d/adblock reload
491 EOF
492
493
494 # useful: http://wiki.openwrt.org/doc/howto/dhcp.dnsmasq
495
496 # sometimes /mnt/usb fails, cuz it's just a flash drive,
497 # so make sure we have this dir or else dnsmasq will fail
498 # to start.
499 mkdir -p /mnt/usb/tftpboot
500 v cedit /etc/dnsmasq.conf <<EOF || dnsmasq_restart=true
501
502 # https://ret2got.wordpress.com/2018/01/19/how-your-ethereum-can-be-stolen-using-dns-rebinding/
503 stop-dns-rebind
504
505 # this says the ip of default gateway and dns server,
506 # but I think they are unneded and default
507 #dhcp-option=3,$l.1
508 #dhcp-option=6,$l.1
509
510
511
512 # results from googling around dnsmasq optimizations
513 # about 50k in memory. router has 62 megs.
514 # in a browsing session, I probably won't ever do 5000 lookups
515 # before the ttl expiration or whatever does expiration.
516 cache-size=10000
517
518 # ask all servers, use the one which responds first.
519 # http://ma.ttwagner.com/make-dns-fly-with-dnsmasq-all-servers/
520 all-servers
521
522 # namebench benchmarks dns servers. google's dns was only
523 # slightly less fast than some others, and I trust it more
524 # to give accurate results, stay relatively fast, and
525 # not do anythin too malicious, so just use that.
526 # download namebench and run it like this:
527 # 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
528 # google
529 server=8.8.4.4
530 server=8.8.8.8
531 server=2001:4860:4860::8888
532 server=2001:4860:4860::8844
533
534
535 # to fixup existin ips, on the client you can do
536 # sudo dhclient -r; sudo dhclient <interface-name>
537
538 # default dhcp range is 100-150
539 # bottom port, iPXE (PCI 03:00.0) in seabios boot menu
540 dhcp-host=c8:60:00:31:6b:75,set:kd,$l.2,kd
541 # top port, iPXE (PCI 04:00.0) in seabios boot menu
542 #dhcp-host=c8:60:00:2b:15:07,set:kd,$l.2,kd
543 dhcp-host=00:26:18:97:bb:16,set:frodo,$l.3,frodo
544 # 4 is reserved for a staticly configured host.
545 dhcp-host=00:1f:16:16:39:24,set:x2,$l.5,x2
546 # This is so fai can have an explicit name to use for testing,
547 # or else any random machine which did a pxe boot would get
548 # reformatted. The mac is from doing a virt-install, cancelling it,
549 # and copying the generated mac, so it should be randomish.
550 dhcp-host=52:54:00:9c:ef:ad,set:demohost,$l.6,demohost
551 dhcp-host=00:1f:16:14:01:d8,set:tp,$l.7,x3
552 dhcp-host=80:fa:5b:1c:6e:cf,set:tp,$l.8,tp
553
554 # faiserver vm
555 dhcp-host=52:54:00:56:09:f9,set:faiserver,$l.15,faiserver
556
557 # This is the ip it picks by default if dhcp fails,
558 # so might as well use it.
559 # hostname is the name it uses according to telnet
560 dhcp-host=b4:75:0e:94:29:ca,set:switch9429ca,$l.251,switch9429ca
561
562 # template
563 # dhcp-host=,$l.,
564
565 # Just leave the tftp server up even if we aren't doing pxe boot.
566 # It has no sensitive info.
567 enable-tftp=br-lan
568 tftp-root=/mnt/usb/tftpboot
569 EOF
570
571 uset network.lan.ipaddr $l.$lanip
572 uset network.lan.netmask $mask
573 uset dhcp.wan.ignore $dev2 # default is false
574 uset dhcp.lan.ignore $dev2 # default is false
575 if $dev2; then
576 uset network.lan.gateway $l.1
577 uset network.wan.proto none
578 uset network.wan6.proto none
579 else
580 # these are the defaults
581 uset network.lan.gateway ''
582 uset network.wan.proto dhcp
583 uset network.wan6.proto dhcpv6
584 fi
585
586
587 if $dnsmasq_restart; then
588 v /etc/init.d/dnsmasq restart
589 fi
590
591 if $firewall_restart; then
592 v /etc/init.d/firewall restart
593 fi
594
595
596
597 if $network_restart; then
598 reboot
599 fi
600 if $dropbear_restart; then
601 v /etc/init.d/dropbear restart
602 fi
603
604 exit 0