summaryrefslogtreecommitdiff
path: root/update-ddns.in
blob: ff589d8b0c5045ac7313206be34508a178e8a46a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash

# version #VERSION#

set -e

stty &> /dev/null || \
  sleep $[$RANDOM%60]

if ! ipCmd="$(which ip 2> /dev/null)"
then
  for p1 in / /usr/
  do
    for p2 in bin/ sbin/
    do
      ipCmd="${p1}${p2}ip"
      [ -x "${ipCmd}" ] && break
    done
    [ -x "${ipCmd}" ] && break
  done
fi

unset ignoreIps

i=0

while read -r ipVer updateUrl dnsName
do

  if [[ "${ipVer}" = '#'* ]] || [ -z "${ipVer}" ]
  then
    continue
  fi

  if [[ "${ipVer}" == 'ignore' ]]
  then
    ignoreIps="${ignoreIps} ${updateUrl} ${dnsName}"
    continue
  fi

  if [[ "${ipVer}" == *"?" ]]
  then
    ipVer="${ipVer%?}"
    [ -z "$(${ipCmd} -o -${ipVer} addr show scope global)" ] && continue
  fi

  if [ "${ipVer}" == '4' ]
  then
    dnsVers[${i}]='A'
    inetVers[${i}]='inet'
    ipCmdVers[${i}]='-4'
  elif [ "${ipVer}" == '6' ]
  then
    dnsVers[${i}]='AAAA'
    inetVers[${i}]='inet6'
    ipCmdVers[${i}]='-6'
  elif [ "${ipVer}" == '46' ] || [ "${ipVer}" == '64' ]
  then
    dnsVers[${i}]='A'
    inetVers[${i}]='inet'
    ipCmdVers[${i}]='-4'

    updateUrls[${i}]="${updateUrl}"
    dnsNamen[${i}]="${dnsName}"
    i=$[${i}+1]

    dnsVers[${i}]='AAAA'
    inetVers[${i}]='inet6'
    ipCmdVers[${i}]='-6'
  else
    >&2 echo 'Error: First column in "#ETCDIR#/update-ddns.conf" needs to be "4", "6", "46" or "64"!'
    exit 1
  fi

  updateUrls[${i}]="${updateUrl}"
  dnsNamen[${i}]="${dnsName}"

  i=$[${i}+1]

done < #ETCDIR#/update-ddns.conf

ignoreIps='^\('"$(
  echo ${ignoreIps} | \
    tr '[:space:]' '\n' | \
    grep -vxF '' | \
    sort -u | \
    sed '
      s@$@\\|@
    ' | \
    tr -d '\n' | \
    sed '
      s@\\|$@@
    '
)"'\)$'

for dnsName in $( \
  echo "${dnsNamen[@]}" | \
    tr ' ' '\n' | \
    sort -u
)
do
  unset updateIps
  unset updateUrl
  unset curlForceIpVer
  needsUpdate=false

  for ((j=0; j<${i}; j++))
  do
    [ "${dnsName}" != "${dnsNamen[${j}]}" ] && continue

    updateUrl="${updateUrls[${j}]}"

    if [[ "${dnsName}" == *'/'* ]]
    then
      dnsIps="$(
        curl ${ipCmdVers[${j}]} "${dnsName}" 2> /dev/null
      )"
      ips='1'
      updateIps="${updateIps}"',auto'
      if [ -n "${curlForceIpVer}" ] && [ "${curlForceIpVer}" != "${ipCmdVers[${j}]}" ]
      then
        >&2 echo 'Error: I cannot determine two different ip versions automatically (because Erich didn'"'"'t need it).'
        exit 1
      fi
      curlForceIpVer="${ipCmdVers[${j}]}"
    else
      dnsIps="$(
        dig $1 "${dnsName}" ${dnsVers[${j}]} | \
          sed '/^;; AUTHORITY SECTION:$/Q' | \
          grep '^[^;]' | \
          grep "\s${dnsVers[${j}]}\s" | \
          awk '{print $5}' | \
          sort -u
      )"

      ips="$(
        "${ipCmd}" -o ${ipCmdVers[${j}]} addr show scope global | \
          awk '{print $4}' | \
          cut -d/ -f1 | \
          grep -vx "${ignoreIps}" | \
          sort -u
      )"

      if [ $(echo "${ips}" | grep -c '.') -eq 0 ]
      then
        updateIps="${updateIps}"',auto'
        if [ -n "${curlForceIpVer}" ] && [ "${curlForceIpVer}" != "${ipCmdVers[${j}]}" ]
        then
          >&2 echo 'Error: I cannot determine two different ip versions automatically (because Erich didn'"'"'t need it).'
          exit 1
        fi
        curlForceIpVer="${ipCmdVers[${j}]}"
      else
        updateIps="${updateIps},$(echo -n "${ips}" | tr '\n' ',')"
      fi
    fi

    if [ "${dnsIps}" != "${ips}" ]
    then
      needsUpdate=true
    fi
  done

  if ${needsUpdate}
  then
    if [[ "${updateUrl}" = *'?'* ]]
    then
      updateIps='&address='"${updateIps#,}"
    else
      updateIps='?address='"${updateIps#,}"
    fi
    curl ${curlForceIpVer} -sS "${updateUrl}${updateIps}" 2> /dev/null
  fi
done