added comments

This commit is contained in:
Daniel Steglich 2015-03-04 22:22:30 +01:00
parent 1075665a90
commit eab3ac2cfc

View File

@ -1,25 +1,40 @@
#!/bin/bash #!/bin/bash
#Todo: IPv6 ################################################################
#Todo: Other service types than gnudip (generic update URL) # #
#Todo: GET WAN IP from Router via UPnP if supported # The script is part of Freedombox #
# #
# This script is a wrapper around ez-ipupdate and/or wget #
# to update a Dynamic DNS account. The script is used as an #
# interface between plinth and ez-ipupdate #
# the script will store configuration, return configuration #
# to plinth UI and do a dynamic DNS update. The script will #
# also determe if we are behind a NAT device, if we can use #
# ez-ipupdate tool or if we need to do some wget magic #
# #
# Todo: IPv6 #
# Todo: GET WAN IP from Router via UPnP if supported #
# Todo: licence string? #
# author: Daniel Steglich <steglich@datasystems24.de> #
# #
################################################################
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#static values # static values
WGET=$(which wget) WGET=$(which wget)
WGETOPTIONS="-4 -o /dev/null -t 3 -T 3" WGETOPTIONS="-4 -o /dev/null -t 3 -T 3"
EMPTYSTRING="none" EMPTYSTRING="none"
NOIP="0.0.0.0" NOIP="0.0.0.0"
#how often do we poll for IP changes if we are behind a NAT? # how often do we poll for IP changes if we are behind a NAT?
UPDATEMINUTES=5 UPDATEMINUTES=5
#if we do not have a IP check URL, how often should we do a "blind" update # if we do not have a IP check URL, how often should we do a "blind" update
UPDATEMINUTESUNKNOWN=3600 UPDATEMINUTESUNKNOWN=3600
TOOLNAME=ez-ipupdate TOOLNAME=ez-ipupdate
UPDATE_TOOL=$(which ${TOOLNAME}) UPDATE_TOOL=$(which ${TOOLNAME})
DISABLED_STRING='disabled' DISABLED_STRING='disabled'
ENABLED_STRING='enabled' ENABLED_STRING='enabled'
#Dirs and filenames # Dirs and filenames
CFGDIR="/etc/${TOOLNAME}/" CFGDIR="/etc/${TOOLNAME}/"
CFG="${CFGDIR}${TOOLNAME}.conf" CFG="${CFGDIR}${TOOLNAME}.conf"
CFG_disabled="${CFGDIR}${TOOLNAME}.inactive" CFG_disabled="${CFGDIR}${TOOLNAME}.inactive"
@ -30,6 +45,7 @@ HELPERCFG="${CFGDIR}${TOOLNAME}-plinth.cfg"
CRONJOB="/etc/cron.d/${TOOLNAME}" CRONJOB="/etc/cron.d/${TOOLNAME}"
PIDFILE="/var/run/ez-ipupdate.pid" PIDFILE="/var/run/ez-ipupdate.pid"
# this function will parse commandline options
doGetOpt() doGetOpt()
{ {
basicauth=0 basicauth=0
@ -77,25 +93,26 @@ doGetOpt()
done done
} }
# this function will write a persistent config file to disk
doWriteCFG() doWriteCFG()
{ {
mkdir ${CFGDIR} 2> /dev/null mkdir ${CFGDIR} 2> /dev/null
#always write to the inactive config - needs to be enabled via "start" command later # always write to the inactive config - needs to be enabled via "start" command later
local out_file=${CFG_disabled} local out_file=${CFG_disabled}
#reset the last update time # reset the last update time
echo 0 > ${LASTUPDATE} echo 0 > ${LASTUPDATE}
#reset the last updated IP # reset the last updated IP
echo "0.0.0.0" > ${IPFILE} echo "0.0.0.0" > ${IPFILE}
#reset last update (if there is one) # reset last update (if there is one)
rm ${STATUSFILE} 2> /dev/null rm ${STATUSFILE} 2> /dev/null
#find the interface (always the default gateway interface) # find the interface (always the default gateway interface)
default_interface=$(ip route |grep default |awk '{print $5}') default_interface=$(ip route |grep default |awk '{print $5}')
#store the given options in ez-ipupdate compatible config file # store the given options in ez-ipupdate compatible config file
echo "host=${host}" > ${out_file} echo "host=${host}" > ${out_file}
echo "server=${server}" >> ${out_file} echo "server=${server}" >> ${out_file}
echo "user=${user}:${pass}" >> ${out_file} echo "user=${user}:${pass}" >> ${out_file}
@ -103,12 +120,12 @@ doWriteCFG()
echo "retrys=3" >> ${out_file} echo "retrys=3" >> ${out_file}
echo "wildcard" >> ${out_file} echo "wildcard" >> ${out_file}
#store UPDATE URL params # store UPDATE URL params
echo "POSTURL ${updateurl}" > ${HELPERCFG} echo "POSTURL ${updateurl}" > ${HELPERCFG}
echo "POSTAUTH ${basicauth}" >> ${HELPERCFG} echo "POSTAUTH ${basicauth}" >> ${HELPERCFG}
echo "POSTSSLIGNORE ${ignoreCertError}" >> ${HELPERCFG} echo "POSTSSLIGNORE ${ignoreCertError}" >> ${HELPERCFG}
#check if we are behind a NAT Router # check if we are behind a NAT Router
echo "IPURL ${ipurl}" >> ${HELPERCFG} echo "IPURL ${ipurl}" >> ${HELPERCFG}
if [ -z ${ipurl} ];then if [ -z ${ipurl} ];then
echo "NAT unknown" >> ${HELPERCFG} echo "NAT unknown" >> ${HELPERCFG}
@ -116,20 +133,24 @@ doWriteCFG()
doGetWANIP doGetWANIP
ISGLOBAL=$(ip addr ls ${default_interface} | grep ${wanip}) ISGLOBAL=$(ip addr ls ${default_interface} | grep ${wanip})
if [ -z ${ISGLOBAL} ];then if [ -z ${ISGLOBAL} ];then
#we are behind NAT # we are behind NAT
echo "NAT yes" >> ${HELPERCFG} echo "NAT yes" >> ${HELPERCFG}
else else
#we are directly connected # we are directly connected
echo "NAT no" >> ${HELPERCFG} echo "NAT no" >> ${HELPERCFG}
#if this file is added ez-ipupdate will take ip form this interface # if this file is added ez-ipupdate will take ip form this interface
echo "interface=${default_interface}" >> ${out_file} echo "interface=${default_interface}" >> ${out_file}
#if this line is added to config file, ez-ipupdate will be launched on startup via init.d # if this line is added to config file, ez-ipupdate will be launched on startup via init.d
echo "daemon" >> ${out_file} echo "daemon" >> ${out_file}
echo "execute=${0} success" >> ${out_file} echo "execute=${0} success" >> ${out_file}
fi fi
fi fi
} }
# this function will read the config file from disk
# special treatment for empty strings is done here:
# plinth will give empty strings like: ''
# but we don't want this single quotes to be used
doReadCFG() doReadCFG()
{ {
host="" host=""
@ -153,6 +174,10 @@ doReadCFG()
fi fi
} }
# replace vars from url: i.e.:
# https://example.com/update.php?domain=<Domain>&User=<User>&Pass=<Pass>
# also this function will remove the surounding single quotes from the URL string
# as plinth will add them
doReplaceVars() doReplaceVars()
{ {
local url=`echo ${updateurl} | sed "s/<Ip>/${wanip}/g"` local url=`echo ${updateurl} | sed "s/<Ip>/${wanip}/g"`
@ -163,6 +188,10 @@ doReplaceVars()
updateurl=$url updateurl=$url
} }
# doReadCFG() needs to be run before this
# this function will return all configured parameters in a way that
# plinth will understand (plinth know the order of
# parameters this function will return)
doStatus() doStatus()
{ {
PROC=$(pgrep ${TOOLNAME}) PROC=$(pgrep ${TOOLNAME})
@ -216,6 +245,8 @@ doStatus()
fi fi
} }
# ask a public WEB Server for the WAN IP we are comming from
# and store this ip within $wanip
doGetWANIP() doGetWANIP()
{ {
if [ ! -z ${ipurl} ];then if [ ! -z ${ipurl} ];then
@ -225,11 +256,13 @@ doGetWANIP()
rm ${outfile} rm ${outfile}
[ -z ${wanip} ] && wanip=${NOIP} [ -z ${wanip} ] && wanip=${NOIP}
else else
#no WAN IP found because of missing check URL # no WAN IP found because of missing check URL
wanip=${NOIP} wanip=${NOIP}
fi fi
} }
# actualy do the update (using wget or ez-ipupdate or even both)
# this function is called via cronjob
doUpdate() doUpdate()
{ {
if [ ! -z ${server} ];then if [ ! -z ${server} ];then
@ -238,7 +271,6 @@ doUpdate()
if [ ! -z ${updateurl} ];then if [ ! -z ${updateurl} ];then
doReplaceVars doReplaceVars
if [ "${basicauth}" = "enabled" ];then if [ "${basicauth}" = "enabled" ];then
local wgetoptions=" --user $user --password $pass " local wgetoptions=" --user $user --password $pass "
fi fi
@ -247,7 +279,7 @@ doUpdate()
fi fi
$WGET ${wgetoptions} "${updateurl}" $WGET ${wgetoptions} "${updateurl}"
#ToDo: check the returning text from WEB Server. User need to give expected string. # ToDo: check the returning text from WEB Server. User need to give expected string.
if [ $? -eq 0 ];then if [ $? -eq 0 ];then
${0} success $wanip ${0} success $wanip
else else
@ -258,9 +290,12 @@ doUpdate()
cmd=${1} cmd=${1}
shift shift
# decide which config to use
cfgfile="/tmp/none" cfgfile="/tmp/none"
[ -f ${CFG_disabled} ] && cfgfile=${CFG_disabled} [ -f ${CFG_disabled} ] && cfgfile=${CFG_disabled}
[ -f ${CFG} ] && cfgfile=${CFG} [ -f ${CFG} ] && cfgfile=${CFG}
# check what action is requested
case ${cmd} in case ${cmd} in
configure) configure)
doGetOpt ${@} doGetOpt ${@}
@ -274,14 +309,14 @@ case ${cmd} in
mv ${CFG_disabled} ${CFG} mv ${CFG_disabled} ${CFG}
/etc/init.d/${TOOLNAME} start /etc/init.d/${TOOLNAME} start
fi fi
#if we are not behind a NAT device and we use update-URL, add a cronjob # if we are not behind a NAT device and we use update-URL, add a cronjob
#(daemon tool does not support update-URL feature) # (daemon tool does not support update-URL feature)
if [ ! -z $(cat $HELPERCFG |grep ^POSTURL | awk '{print $2}') ];then if [ ! -z $(cat $HELPERCFG |grep ^POSTURL | awk '{print $2}') ];then
echo "*/${UPDATEMINUTES} * * * * root $0 update" > ${CRONJOB} echo "*/${UPDATEMINUTES} * * * * root $0 update" > ${CRONJOB}
$0 update $0 update
fi fi
else else
#if we are behind a NAT device, add a cronjob (daemon tool cannot monitor WAN IP changes) # if we are behind a NAT device, add a cronjob (daemon tool cannot monitor WAN IP changes)
echo "*/${UPDATEMINUTES} * * * * root $0 update" > $CRONJOB echo "*/${UPDATEMINUTES} * * * * root $0 update" > $CRONJOB
$0 update $0 update
fi fi
@ -300,11 +335,11 @@ case ${cmd} in
cat ${cfgfile} |grep -v execute > ${cfgfile}.tmp cat ${cfgfile} |grep -v execute > ${cfgfile}.tmp
mv ${cfgfile}.tmp ${cfgfile} mv ${cfgfile}.tmp ${cfgfile}
echo "execute=${0} success ${wanip}" >> ${cfgfile} echo "execute=${0} success ${wanip}" >> ${cfgfile}
#if we know our WAN IP, only update if IP changes # if we know our WAN IP, only update if IP changes
if [ "${oldip}" != "${wanip}" -a "${wanip}" != ${NOIP} ];then if [ "${oldip}" != "${wanip}" -a "${wanip}" != ${NOIP} ];then
doUpdate doUpdate
fi fi
#if we don't know our WAN IP do a blind update once a hour # if we don't know our WAN IP do a blind update once a hour
if [ "${wanip}" = ${NOIP} ];then if [ "${wanip}" = ${NOIP} ];then
uptime=$(cat /proc/uptime |cut -d . -f 1) uptime=$(cat /proc/uptime |cut -d . -f 1)
LAST=0 LAST=0
@ -325,11 +360,11 @@ case ${cmd} in
date=$(date) date=$(date)
echo "last update done (${date})" > ${STATUSFILE} echo "last update done (${date})" > ${STATUSFILE}
cat /proc/uptime |awk '{print $1}' |cut -d . -f 1 > ${LASTUPDATE} cat /proc/uptime |awk '{print $1}' |cut -d . -f 1 > ${LASTUPDATE}
#if called from cronjob, the current IP is given as parameter # if called from cronjob, the current IP is given as parameter
if [ $# -eq 1 ];then if [ $# -eq 1 ];then
echo ${1} > ${IPFILE} echo ${1} > ${IPFILE}
else else
#if called from ez-ipupdate daemon, no WAN IP is given as parameter # if called from ez-ipupdate daemon, no WAN IP is given as parameter
doGetWANIP doGetWANIP
echo ${wanip} > ${IPFILE} echo ${wanip} > ${IPFILE}
fi fi