-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathnsupdate.sh
executable file
·222 lines (192 loc) · 4.9 KB
/
nsupdate.sh
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/sh
# Copyright (C) 2020 Michel Stam <michel@reverze.net>
# Copyright (C) 2023 Michal Roszkowski
#
# This file is part of uacme.
#
# uacme is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# uacme is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Commands
DIG=dig
NSUPDATE=nsupdate
# Server to which updates will be sent. If not specified it will
# be obtained from MNAME in the SOA record.
NSUPDATE_SERVER=
# Files
# {NSUPDATE,DIG}_KEY
# If you wish to sign transactions using TSIG, specify the keyfile
# here. If you do, also make sure named.conf specifies the
# key "KEYNAME"; in the zone that must be updated (and disallow
# all others for safety)
NSUPDATE_KEY=
DIG_KEY=
ARGS=5
E_BADARGS=85
if [ $# -ne "$ARGS" ]; then
echo "Usage: $(basename "$0") method type ident token auth" 1>&2
exit $E_BADARGS
fi
METHOD=$1
TYPE=$2
IDENT=$3
TOKEN=$4
AUTH=$5
ns_getns()
{
local zone=$1
local answer
[ -n "$zone" ] && answer=$($DIG ${DIG_KEY:+-k ${DIG_KEY}} +noall +nottl +noclass +answer "$zone" NS) || return
local owner
local type
local rdata
while read -r owner type rdata; do
[ "$type" = NS ] && echo $rdata
done <<-EOF
$answer
EOF
}
ns_getall()
{
local name=$1
local answer
local zone
local primary
[ -n "$name" ] && answer=$($DIG ${DIG_KEY:+-k ${DIG_KEY}} +noall +nottl +noclass +answer +authority "$name" SOA) || return
name=${name%.}.
local owner
local type
local rdata
while read -r owner type rdata; do
case "$type" in
CNAME)
name=$rdata
;;
DNAME)
name=${name%$owner}$rdata
;;
SOA)
zone=$owner
set -- $rdata && primary=$1
;;
esac
done <<-EOF
$answer
EOF
echo $name $zone $primary
}
ns_ispresent()
{
local challenge=$2
set -- $(ns_getall "$1")
local name=$1
local nameservers=$(ns_getns "$2")
local answer
local target
local rc=1
local ns
for ns in $nameservers; do
answer=$($DIG ${DIG_KEY:+-k ${DIG_KEY}} +noall +nottl +noclass +answer "@$ns" "$name" TXT) || continue
target=
local owner
local type
local rdata
while read -r owner type rdata; do
case "$type" in
CNAME)
target=$rdata
;;
DNAME)
[ -n "$target" ] && target=${target%$owner}$rdata || target=${name%$owner}$rdata
;;
TXT)
[ "$rdata" = \"$challenge\" ] && rc=0 && continue 2
target=
;;
esac
done <<-EOF
$answer
EOF
[ -n "$target" ] && ns_ispresent "$target" "$challenge" && rc=0 || return 1
done
return $rc
}
ns_doupdate()
{
local action=$1
local challenge=$3
set -- $(ns_getall "$2")
local name=$1
local zone=$2
local server=${NSUPDATE_SERVER:-$3}
local ttl=300
[ -n "$server" ] && [ -n "$zone" ] && [ -n "$name" ] && [ -n "$challenge" ] || return 1
$NSUPDATE ${NSUPDATE_KEY:+-k ${NSUPDATE_KEY}} -v <<-EOF
server ${server}
zone ${zone}
update ${action} ${name} ${ttl} IN TXT ${challenge}
send
EOF
return $?
}
ns_update()
{
local action=$1
local name=$2
local challenge=$3
local retries=5
local delay=5
local count=0
ns_doupdate "$action" "$name" "$challenge" || return 1
while sleep $delay; do
case "$action" in
add)
ns_ispresent "$name" "$challenge" && break
;;
del)
ns_ispresent "$name" "$challenge" || break
;;
*)
return 1
esac
[ $count -lt $retries ] || return 1
count=$((count + 1))
done
return 0
}
case "$METHOD" in
"begin")
case "$TYPE" in
dns-01)
ns_update add "_acme-challenge.$IDENT" "$AUTH"
exit $?
;;
*)
exit 1
;;
esac
;;
"done"|"failed")
case "$TYPE" in
dns-01)
ns_update del "_acme-challenge.$IDENT" "$AUTH"
exit $?
;;
*)
exit 1
;;
esac
;;
*)
echo "$0: invalid method" 1>&2
exit 1
esac