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