summaryrefslogtreecommitdiff
path: root/update-ddns.in
blob: 0c27f35877de1649c3400787d10cfdc1f5c1386d (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
#!/bin/bash

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

i=0

while read -r ipVer updateUrl dnsName
do

  if [[ "${ipVer}" = '#'* ]] || [ -z "${ipVer}" ]
  then
    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

for dnsName in $( \
  echo "${dnsNamen[@]}" | \
    tr ' ' '\n' | \
    uniq
)
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 'FEHLER: Ich kann nicht zwei verschiedene IP-Versionen automatisch ermitteln lassen (weil das Erich nicht braucht).'
        exit 1
      fi
      curlForceIpVer="${ipCmdVers[${j}]}"
    else
      dnsIps="$(
        dig $1 "${dnsName}" ${dnsVers[${j}]} | \
          grep -v '^\s*\(;\|$\)' | \
          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 | \
          sort -u
      )"

      if [ $(echo "${ips}" | wc -l) -eq 0 ]
      then
        updateIps="${updateIps}"',auto'
        if [ -n "${curlForceIpVer}" ] && [ "${curlForceIpVer}" != "${ipCmdVers[${j}]}" ]
        then
          >&2 echo 'FEHLER: Ich kann nicht zwei verschiedene IP-Versionen automatisch ermitteln lassen (weil das Erich nicht braucht).'
          exit 1
        fi
        curlForceIpVer="${ipCmdVers[${j}]}"
      else
        updateIps="${updateIps},$(echo -n "${ips}" | tr '\n' ',')"
      fi
    fi

    if [ -z "$(
      (
        echo "${dnsIps}"
        echo "${ips}"
      ) | \
        sort | \
        uniq -d
    )" ]
    then
      needsUpdate=true
    fi
  done

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