From ea6830ed92c11fdac3d2c307e86c013cd5d79ad8 Mon Sep 17 00:00:00 2001
From: Christian Busch <chris@debilux.org>
Date: Wed, 22 Jul 2015 21:50:01 +0200
Subject: [PATCH 1/2] config file checks and sample config

Changed the way the existence of config files is checked. It may be a
bit slower than before when there are many config files but maybe it
avoids other trouble.
Updated the sample config.
---
 nsupdate.d/sample.config.dist |   7 ++
 nsupdate.sh                   | 205 +++++++++++++++++-----------------
 2 files changed, 108 insertions(+), 104 deletions(-)

diff --git a/nsupdate.d/sample.config.dist b/nsupdate.d/sample.config.dist
index 0cfd681..f82c58d 100755
--- a/nsupdate.d/sample.config.dist
+++ b/nsupdate.d/sample.config.dist
@@ -5,9 +5,16 @@ IP_CHECK_SITE="https://ip.dblx.io"
 
 # use drill instead of nslookup for hostname lookup
 USE_DRILL="YES"
+
+# Use IPv6 connection
 IPV6="NO"
+
+# Update an MX record
 MX="NO"
 
+# Suppress all messages
+SILENT=NO
+
 # Login credentials for the inwx admin interface
 INWX_USER="USERNAME"
 INWX_PASS='PASSWORD'
diff --git a/nsupdate.sh b/nsupdate.sh
index 32ff655..fa4016a 100755
--- a/nsupdate.sh
+++ b/nsupdate.sh
@@ -24,116 +24,113 @@
 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-##################
-# check required #
-##################
+# check required tools
 command -v curl >/dev/null 2>&1 || { echo >&2 "I require curl but it's not installed. Note: all needed items are listed in the README.md file."; exit 1; }
 command -v awk >/dev/null 2>&1 || { echo >&2 "I require awk but it's not installed. Note: all needed items are listed in the README.md file."; exit 1; }
 command -v drill >/dev/null 2>&1 || command -v nslookup >/dev/null 2>&1 || { echo >&2 "I need drill or nslookup installed. Note: all needed items are listed in the README.md file."; exit 1; }
-##################
-# check config   #
-##################
-configfiles=$(shopt -s nullglob dotglob; echo nsupdate.d/*.config)
-if (( ! ${#configfiles} ));then
-    echo "There does not seem to be any config file available." ; exit 1;
-fi
-##################
 
 LOG=$0.log
 SILENT=NO
-# Loop through configs
-for f in $(dirname $0)/nsupdate.d/*.config
-do
-   if [ "$SILENT" == "NO" ]; then
-      echo "Starting nameserver update with config file $f"
-   fi
-   ## Set record type to IPv4
-   TYPE=A
-   CONNECTION_TYPE=4
 
-   source $f
-
-   ## Set record type to MX
-   if [[ "$MX" == "YES" ]]; then
-      TYPE=MX
-   fi
-
-   ## Set record type to IPv6
-   if [[ "$IPV6" == "YES" ]]; then
-      TYPE=AAAA
-      CONNECTION_TYPE=6
-   fi
-
-   if [[ "$USE_DRILL" == "YES" ]]; then
-      if [[ "$TYPE" == "MX" ]]; then
-		echo looking up MX records with drill currently not supported!
-		exit 1;
-	  else
-	    NSLOOKUP=$(drill $DOMAIN @ns.inwx.de $TYPE | head -7 | tail -1 | awk '{print $5}')   
-	  fi
-   else
-   	  if [[ "$TYPE" == "MX" ]]; then
-		PART_NSLOOKUP=$(nslookup -sil -type=$TYPE $DOMAIN - ns.inwx.de | tail -2 | head -1 | cut -d' ' -f5)
-		NSLOOKUP=${PART_NSLOOKUP%"."}
-      else
-	  	NSLOOKUP=$(nslookup -sil -type=$TYPE $DOMAIN - ns.inwx.de | tail -2 | head -1 | cut -d' ' -f2)
-	  fi
-   fi
-
-   # WAN_IP=`curl -s -$CONNECTION_TYPE ${IP_CHECK_SITE}| grep -Eo '\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>'`
-   WAN_IP=`curl -s -$CONNECTION_TYPE ${IP_CHECK_SITE}`
-
-
-   API_XML="<?xml version=\"1.0\"?>
-   <methodCall>
-      <methodName>nameserver.updateRecord</methodName>
-      <params>
-         <param>
-            <value>
-               <struct>
-                  <member>
-                     <name>user</name>
-                     <value>
-                        <string>$INWX_USER</string>
-                     </value>
-                  </member>
-                  <member>
-                     <name>pass</name>
-                     <value>
-                        <string>$INWX_PASS</string>
-                     </value>
-                  </member>
-                  <member>
-                     <name>id</name>
-                     <value>
-                        <int>$INWX_DOMAIN_ID</int>
-                     </value>
-                  </member>
-                  <member>
-                     <name>content</name>
-                     <value>
-                        <string>$WAN_IP</string>
-                     </value>
-                  </member>
-               </struct>
-            </value>
-         </param>
-      </params>
-   </methodCall>"
+# Check if there are any usable config files
+if ls $(dirname $0)/nsupdate.d/*.config 1> /dev/null 2>&1; then
    
-   if [ ! "$NSLOOKUP" == "$WAN_IP" ]; then
-      curl -silent -v -XPOST -H"Content-Type: application/xml" -d "$API_XML" https://api.domrobot.com/xmlrpc/
-      echo "$(date) - $DOMAIN updated. Old IP: "$NSLOOKUP "New IP: "$WAN_IP >> $LOG
-   elif [ "$SILENT" == "NO" ]; then
-      echo "$(date) - No update needed for $DOMAIN. Current IP: "$NSLOOKUP >> $LOG
-   fi
+   # Loop through configs
+   for f in $(dirname $0)/nsupdate.d/*.config
+   do
+      if [ "$SILENT" == "NO" ]; then
+         echo "Starting nameserver update with config file $f"
+      fi
+      ## Set record type to IPv4
+      TYPE=A
+      CONNECTION_TYPE=4
 
-   unset DOMAIN
-   unset IPV6
-   unset MX
-   unset WAN_IP
-   unset NSLOOKUP
-   unset INWX_PASS
-   unset INWX_USER
-   unset INWX_DOMAIN_ID
-done
+      source $f
+
+      ## Set record type to MX
+      if [[ "$MX" == "YES" ]]; then
+         TYPE=MX
+      fi
+
+      ## Set record type to IPv6
+      if [[ "$IPV6" == "YES" ]]; then
+         TYPE=AAAA
+         CONNECTION_TYPE=6
+      fi
+
+      if [[ "$USE_DRILL" == "YES" ]]; then
+         if [[ "$TYPE" == "MX" ]]; then
+         echo looking up MX records with drill currently not supported!
+         exit 1;
+        else
+          NSLOOKUP=$(drill $DOMAIN @ns.inwx.de $TYPE | head -7 | tail -1 | awk '{print $5}')   
+        fi
+      else
+           if [[ "$TYPE" == "MX" ]]; then
+         PART_NSLOOKUP=$(nslookup -sil -type=$TYPE $DOMAIN - ns.inwx.de | tail -2 | head -1 | cut -d' ' -f5)
+         NSLOOKUP=${PART_NSLOOKUP%"."}
+         else
+         NSLOOKUP=$(nslookup -sil -type=$TYPE $DOMAIN - ns.inwx.de | tail -2 | head -1 | cut -d' ' -f2)
+        fi
+      fi
+
+      # WAN_IP=`curl -s -$CONNECTION_TYPE ${IP_CHECK_SITE}| grep -Eo '\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>'`
+      WAN_IP=`curl -s -$CONNECTION_TYPE ${IP_CHECK_SITE}`
+
+
+      API_XML="<?xml version=\"1.0\"?>
+      <methodCall>
+         <methodName>nameserver.updateRecord</methodName>
+         <params>
+            <param>
+               <value>
+                  <struct>
+                     <member>
+                        <name>user</name>
+                        <value>
+                           <string>$INWX_USER</string>
+                        </value>
+                     </member>
+                     <member>
+                        <name>pass</name>
+                        <value>
+                           <string>$INWX_PASS</string>
+                        </value>
+                     </member>
+                     <member>
+                        <name>id</name>
+                        <value>
+                           <int>$INWX_DOMAIN_ID</int>
+                        </value>
+                     </member>
+                     <member>
+                        <name>content</name>
+                        <value>
+                           <string>$WAN_IP</string>
+                        </value>
+                     </member>
+                  </struct>
+               </value>
+            </param>
+         </params>
+      </methodCall>"
+      
+      if [ ! "$NSLOOKUP" == "$WAN_IP" ]; then
+         curl -silent -v -XPOST -H"Content-Type: application/xml" -d "$API_XML" https://api.domrobot.com/xmlrpc/
+         echo "$(date) - $DOMAIN updated. Old IP: "$NSLOOKUP "New IP: "$WAN_IP >> $LOG
+      elif [ "$SILENT" == "NO" ]; then
+         echo "$(date) - No update needed for $DOMAIN. Current IP: "$NSLOOKUP >> $LOG
+      fi
+
+      unset DOMAIN
+      unset IPV6
+      unset MX
+      unset WAN_IP
+      unset NSLOOKUP
+      unset INWX_PASS
+      unset INWX_USER
+      unset INWX_DOMAIN_ID
+   done
+else
+   echo "There does not seem to be any config file available in $(dirname $0)/nsupdate.d/." ; exit 1;
+fi

From cba262d112440d52d44a7049358a7b7c43b654a8 Mon Sep 17 00:00:00 2001
From: Christian Busch <chris@debilux.org>
Date: Wed, 22 Jul 2015 22:12:44 +0200
Subject: [PATCH 2/2] Updated README:md

---
 README.md | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 085f7a9..509e942 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
 
 Update nameserver entrys at inwx with the current WAN IP (DynDNS)
 
-nsbackup is a bash script that uses curl and the inwx API to update nameserver entrys at inwx with the current WAN IP. It supports IPv4 and IPv6.
+nsbackup is a shell script that uses curl and the inwx API to update nameserver entrys at inwx with the current WAN IP. It supports IPv4 and IPv6.
 
 Place your config files in the _nsupdate.d_ folder.
 
@@ -14,6 +14,12 @@ At least one config file needs to exist ending with _.config_. A "sample.config.
 
 ## Changelog
 
+**2015-07-22**
+
+- Changed the way how the existence of config files is checked
+- Updated the sample config file to reflect new options from the last updates
+- The script is reported to work in csh and sh too
+
 **2015-06-30**
 
 - Fixed the check for config files. Can now handle more than one file