fixes, improve host-info-update
[distro-setup] / brc2
diff --git a/brc2 b/brc2
index 2795e2e2459941ad5b7df7a07554400fc28f7049..6988b81fd4a062c8d78657894b35cbfd4f5100a6 100644 (file)
--- a/brc2
+++ b/brc2
@@ -2574,10 +2574,13 @@ ilog-local() {
   done
 }
 ilog() {
-  local chan
+  local chan tmpf
+  tmpf=$(mktemp)
   chan="${1:-#fsfsys}"
   # use * instead of -r since that does sorted order
-  sl root@iankelling.org ilog-local "$chan" | less +G
+  sl root@li.b8.nz ilog-local "$chan" > $tmpf
+  less +G $tmpf
+  rm -f $tmpf
 }
 
 o() {
@@ -2644,7 +2647,7 @@ host-info-all() {
 # wrt-setup
 host-info-update() {
 
-  local -A vpn_ips host_ips host_macs nonvpn_ips
+  local -A vpn_ips host_ips host_macs nonvpn_ips all_ips
   local -a root_hosts nonroot_hosts
 
   # the hosts with no mac
@@ -2656,37 +2659,95 @@ host-info-update() {
 
   while read -r ip host mac opts; do
     if [[ $ip == *#*  || ! $host ]]; then continue; fi
-    if [[ $opts == vpn ]]; then
+
+    # opt parsing
+    vpn=false
+    root=false
+    for opt in $opts; do
+      case $opt in
+        user=root)
+          root=true
+          ;;
+        vpn)
+          vpn=true
+          ;;
+      esac
+    done
+
+    all_ips[$host]=$ip
+    if $vpn; then
       vpn_ips[$host]=$ip
     else
       nonvpn_ips[$host]=$ip
     fi
-
-
-    if [[ $opts == user=root ]]; then
-      # note: the b8.nz suffix is for the User part, the IdentityFile
-      # part is redundant to *.b8.nz.
-      root_hosts+=($host i$host $host.b8.nz i$host.b8.nz)
+    if $root; then
+      # note: the reason we have b8.nz suffix here but not for non_root
+      # hosts is that it is for the User part, the IdentityFile part is
+      # redundant to *.b8.nz. Also note ${host}i, we only setup those for vpn hosts, but there is no harm in overspecifying here.
+      root_hosts+=($host ${host}i $host.b8.nz ${host}i.b8.nz)
     else
-      nonroot_hosts+=($host i$host)
+      nonroot_hosts+=($host ${host}i)
     fi
 
     host_ips[$host]=$ip
     host_macs[$host]=$mac
   done </p/c/host-info
 
-  cedit /p/c/subdir_files/.ssh/config <<EOF || [[ $? == 1 ]]
+  {
+    cat <<EOF
 Host ${nonroot_hosts[@]}
 User iank
 IdentityFile ~/.ssh/home
 
 Host ${root_hosts[@]}
 IdentityFile ~/.ssh/home
+
+EOF
+    for host in ${!vpn_ips[@]}; do
+      ipsuf=${vpn_ips[$host]}
+      cat <<EOF
+Host ${host}i
+Hostname b8.nz
+Port $((2200 + ipsuf))
+
 EOF
+    done
+
+    # convenience of one auth key entry
+    for host in ${!all_ips[@]}; do
+      cat <<EOF
+Host $host ${host}i $host.b8.nz ${host}i.b8.nz
+HostKeyAlias $host.b8.nz
+EOF
+    done
+  } | cedit /p/c/subdir_files/.ssh/config || [[ $? == 1 ]]
+
+  {
+    echo "cat <<EOF"
+    for host in ${!vpn_ips[@]}; do
+      ipsuf=${vpn_ips[$host]}
+      i_port=$(( 2200 + ipsuf ))
+      cat <<EOF
+config redirect
+ option name ssh$host
+ option src              wan
+ option src_dport        $i_port
+ option dest_port        22
+ option dest_ip          \$l.$ipsuf
+ option dest             lan
+config rule
+ option src              wan
+ option target           ACCEPT
+ option dest_port        $i_port
+EOF
+    done
+    echo "EOF"
+  } >/p/c/cmc-firewall-data
 
 
   local host ipsuf f files
 
+  # shellcheck disable=SC2016 # shellcheck doesnt know this is sed
   sedi '/edits below here are made automatically/,$d' /p/c/machine_specific/li/filesystem/etc/wireguard/wgmail.conf
   for host in ${!vpn_ips[@]}; do
     ipsuf=${vpn_ips[$host]}
@@ -2735,6 +2796,7 @@ EOF
     echo "cat <<EOF"
     for host in ${!host_ips[@]}; do
       ipsuf=${host_ips[$host]}
+      # shellcheck disable=SC2016 # intentional
       echo 'local-data-ptr: "$l.'$ipsuf $host.b8.nz'"'
     done
     echo "EOF"
@@ -2749,7 +2811,13 @@ EOF
     echo "EOF"
   } | u /p/c/dnsmasq-data
 
+  b8_ip=$(dig +short b8.nz @iankelling.org | tail -1)
+  if [[ ! $b8_ip ]]; then
+    echo "$0: error: got empty b8.nz ip. returning 1"
+    return 1
+  fi
   {
+    echo "@    A       $b8_ip"
     for host in ${!nonvpn_ips[@]}; do
       ipsuf=${nonvpn_ips[$host]}
       echo "$host      A       10.2.0.$ipsuf"
@@ -2768,16 +2836,16 @@ EOF
 
   echo checking for stray files:
 
-  initial_dir=$PWD
+  initial_dir="$PWD"
   cd /a/bin/ds/machine_specific
   ngset
   files=( */filesystem/etc/systemd/system/openvpn-client-tr@.service )
   ngreset
-  cd $initial_dir
+  cd "$initial_dir"
   for f in "${files[@]}"; do
     host=${f%%/*}
     if [[ ! ${vpn_ips[$host]} ]]; then
-      e /a/bin/ds/machine_specific/$host/filesystem/etc/systemd/system/openvpn-client-tr@.service
+      e rm /a/bin/ds/machine_specific/$host/filesystem/etc/systemd/system/openvpn-client-tr@.service
     fi
   done