improvements
[automated-distro-installer] / wrt-setup-local
index 3d2edb85e513645d678aea7c236f69787b5d8afe..11c932708bc98e0f5a30f5a6dd83573ae1a3117e 100755 (executable)
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
+# shellcheck disable=SC1091 # somewhat dynamic
+
+set -e; . /usr/local/lib/bash-bear; set +e
 
-f=/usr/local/lib/bash-bear;test -r $f || { echo "error: $0 no $f" >&2;exit 1;}; . $f
 
 usage() {
   cat <<EOF
@@ -53,7 +55,7 @@ libremanage_host=wrt2
 lanip=1
 while getopts hm:t:yz opt; do
   case $opt in
-    h) usage ;;
+    h) usage ;;
     t)
       case $2 in
         2|3)
@@ -112,6 +114,7 @@ fi
 secrets=false
 if [[ -e /root/router-secrets ]]; then
   secrets=true
+  # shellcheck source=/p/router-secrets
   source /root/router-secrets
 fi
 
@@ -139,7 +142,7 @@ pmirror() {
   # doesn't go into the firmware. build new firmware if you want
   # lots of upgrades. I think /tmp/opkg-lists is a pre openwrt 14 location.
   f=(/var/opkg-lists/*)
-  if ! (( $(date -r $f +%s) + 60*60*24 > $(date +%s) )); then
+  if ! (( $(date -r ${f[0]} +%s) + 60*60*24 > $(date +%s) )); then
     if ! opkg update; then
       echo "$0: warning: opkg update failed" >&2
     fi
@@ -157,7 +160,7 @@ pi() {
       pmirror
     fi
   done
-  if [[ $to_install ]]; then
+  if (( ${#to_install[@]} >= 1 )); then
     opkg install ${to_install[@]}
   fi
 }
@@ -232,13 +235,13 @@ elif $secrets; then
   ssid=${rssid[$h]}
 fi
 
-: ${ssid:=librecmc}
+: "${ssid:=librecmc}"
 
 
 if $secrets; then
   key=${rkey[$h]}
 fi
-: ${key:=pictionary49}
+: "${key:=pictionary49}"
 
 mask=255.255.0.0
 cidr=16
@@ -536,8 +539,7 @@ EOF
 #         option config /etc/openvpn/client.conf
 # EOF
 
-wgip4=10.3.0.1/24
-wgip6=fdfd::1/64
+
 wgport=26000
 
 network_restart=false
@@ -577,10 +579,10 @@ if $network_restart; then
   v /etc/init.d/network reload
 fi
 
-firewall-cedit() {
 
-  if $client; then
-    cedit wific /etc/config/firewall <<EOF
+### begin firewall edits ###
+if $client; then
+  cedit wific /etc/config/firewall <<EOF || firewall_restart=true
 config zone
  option name    wwan
  option input    REJECT
@@ -590,11 +592,11 @@ config zone
  option mtu_fix  1
  option network  wwan
 EOF
-  fi
+fi
 
-  case $hostname in
-    wrt)
-      cedit host /etc/config/firewall <<EOF
+case $hostname in
+  wrt)
+    cedit host /etc/config/firewall <<EOF || firewall_restart=true
 config redirect
  option name ssh
  option src              wan
@@ -602,9 +604,9 @@ config redirect
  option dest_ip          $l.3
  option dest             lan
 EOF
-      ;;
-    cmc)
-      cedit host /etc/config/firewall <<EOF
+    ;;
+  cmc)
+    cedit host /etc/config/firewall <<EOF || firewall_restart=true
 config redirect
  option name ssh
  option src              wan
@@ -612,11 +614,43 @@ config redirect
  option dest_ip          $l.2
  option dest             lan
 EOF
-      ;;
-  esac
+    ;;
+esac
+
+{
+  # shellcheck source=/p/c/cmc-firewall-data
+  . /root/cmc-firewall-data
+  # sets $http_ip
+  # shellcheck source=/p/c/cmc-firewall-data-http
+  . /root/cmc-firewall-data-http
+  cat <<EOF
+config redirect
+ option name http
+ option src              wan
+ option src_dport        80
+ option dest             lan
+ option dest_ip          $l.$http_ip
+ option proto            tcp
+config rule
+ option src              wan
+ option target           ACCEPT
+ option dest_port        80
+ option proto            tcp
+
+config redirect
+ option name https
+ option src              wan
+ option src_dport        443
+ option dest             lan
+ option dest_ip          $l.$http_ip
+ option proto            tcp
+config rule
+ option src              wan
+ option target           ACCEPT
+ option dest_port        443
+ option proto            tcp
 
 
-  cedit /etc/config/firewall <<EOF
 ## begin no external dns for ziva
 config rule
  option src  lan
@@ -666,7 +700,6 @@ config rule
  option target REJECT
 ## end no external dns for ziva
 
-$(. /root/cmc-firewall-data)
 
 config rule
  option src              wan
@@ -785,31 +818,6 @@ config rule
 #  option target           ACCEPT
 #  option dest_port        8000
 
-config redirect
- option name http
- option src              wan
- option src_dport        80
- option dest             lan
- option dest_ip          $l.7
- option proto            tcp
-config rule
- option src              wan
- option target           ACCEPT
- option dest_port        80
- option proto            tcp
-
-config redirect
- option name https
- option src              wan
- option src_dport        443
- option dest             lan
- option dest_ip          $l.7
- option proto            tcp
-config rule
- option src              wan
- option target           ACCEPT
- option dest_port        443
- option proto            tcp
 
 # config redirect
 # option name httpskd8448
@@ -891,8 +899,9 @@ config rule
  option family ipv6
 
 EOF
-}
-firewall-cedit || firewall_restart=true
+} | cedit /etc/config/firewall || firewall_restart=true
+### end firewall edits ###
+
 
 # firewall comment:
 # not using and in newer wrt, fails, probably due to nonexistent file, error output
@@ -1000,7 +1009,7 @@ prefetch: yes
 qname-minimisation: yes
 rrset-roundrobin: yes
 use-caps-for-id: yes
-do-ip6: no
+do-ip6: yes
 private-domain: b8.nz
 local-zone: "10.in-addr.arpa." transparent
 access-control-view: 10.2.0.31/32 "youtube"
@@ -1028,9 +1037,11 @@ EOF
   # order to be comprehensive
 
 
-  cedit /etc/unbound/unbound_ext.conf <<EOF || unbound_restart=true
 
-$(. /root/ptr-data)
+  {
+    # shellcheck source=/p/c/ptr-data
+    . /root/ptr-data
+    cat  <<EOF
 
 local-data-ptr: "10.2.0.1 cmc.b8.nz"
 
@@ -1073,6 +1084,7 @@ view:
   # try global if no match in view
   view-first: yes
 EOF
+  } | cedit /etc/unbound/unbound_ext.conf || unbound_restart=true
 
 
   if $unbound_restart; then
@@ -1103,15 +1115,17 @@ fi # end if $ap
 # so make sure we have this dir or else dnsmasq will fail
 # to start.
 mkdir -p /mnt/usb/tftpboot
-cedit /etc/dnsmasq.conf  <<EOF || dnsmasq_restart=true
 
+{
+  # generated with host-info-update
+  # shellcheck source=/p/c/dnsmasq-data
+  . /root/dnsmasq-data
+  cat <<EOF
 # no dns
 port=0
 server=/b8.nz/#
 ptr-record=1.0.2.10.in-addr.arpa.,cmc.b8.nz
 
-# generated with host-info-update
-$(. /root/dnsmasq-data)
 
 # https://ret2got.wordpress.com/2018/01/19/how-your-ethereum-can-be-stolen-using-dns-rebinding/
 stop-dns-rebind
@@ -1184,6 +1198,8 @@ dhcp-optsfile=/var/run/dnsmasq/dhcpopts.conf
 # for debugging dhcp
 #log-queries=extra
 EOF
+} | cedit /etc/dnsmasq.conf || dnsmasq_restart=true
+
 
 
 if $dnsmasq_restart && ! $dev2 && ! $ap; then