2017-03-05 14:18:31 +01:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
######## Public functions #####################
|
|
|
|
|
|
|
|
#Usage: dns_knot_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
|
|
|
|
dns_knot_add() {
|
|
|
|
fulldomain=$1
|
|
|
|
txtvalue=$2
|
|
|
|
_checkKey || return 1
|
|
|
|
[ -n "${KNOT_SERVER}" ] || KNOT_SERVER="localhost"
|
|
|
|
# save the dns server and key to the account.conf file.
|
|
|
|
_saveaccountconf KNOT_SERVER "${KNOT_SERVER}"
|
|
|
|
_saveaccountconf KNOT_KEY "${KNOT_KEY}"
|
|
|
|
|
|
|
|
if ! _get_root "$fulldomain"; then
|
|
|
|
_err "Domain does not exist."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_info "Adding ${fulldomain}. 60 TXT \"${txtvalue}\""
|
|
|
|
|
2021-12-12 14:17:13 +01:00
|
|
|
knsupdate <<EOF
|
2017-03-05 14:18:31 +01:00
|
|
|
server ${KNOT_SERVER}
|
2021-12-12 14:17:13 +01:00
|
|
|
key ${KNOT_KEY}
|
2017-03-05 14:18:31 +01:00
|
|
|
zone ${_domain}.
|
|
|
|
update add ${fulldomain}. 60 TXT "${txtvalue}"
|
|
|
|
send
|
|
|
|
quit
|
|
|
|
EOF
|
|
|
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
_err "Error updating domain."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_info "Domain TXT record successfully added."
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
#Usage: dns_knot_rm _acme-challenge.www.domain.com
|
|
|
|
dns_knot_rm() {
|
|
|
|
fulldomain=$1
|
|
|
|
_checkKey || return 1
|
|
|
|
[ -n "${KNOT_SERVER}" ] || KNOT_SERVER="localhost"
|
|
|
|
|
|
|
|
if ! _get_root "$fulldomain"; then
|
|
|
|
_err "Domain does not exist."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_info "Removing ${fulldomain}. TXT"
|
|
|
|
|
2021-12-12 14:17:13 +01:00
|
|
|
knsupdate <<EOF
|
2017-03-05 14:18:31 +01:00
|
|
|
server ${KNOT_SERVER}
|
2021-12-12 14:17:13 +01:00
|
|
|
key ${KNOT_KEY}
|
2017-03-05 14:18:31 +01:00
|
|
|
zone ${_domain}.
|
|
|
|
update del ${fulldomain}. TXT
|
|
|
|
send
|
|
|
|
quit
|
|
|
|
EOF
|
|
|
|
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
_err "error updating domain"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_info "Domain TXT record successfully deleted."
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
#################### Private functions below ##################################
|
|
|
|
# _acme-challenge.www.domain.com
|
|
|
|
# returns
|
|
|
|
# _domain=domain.com
|
|
|
|
_get_root() {
|
|
|
|
domain=$1
|
|
|
|
i="$(echo "$fulldomain" | tr '.' ' ' | wc -w)"
|
2017-03-07 14:21:22 +01:00
|
|
|
i=$(_math "$i" - 1)
|
2017-03-05 14:18:31 +01:00
|
|
|
|
|
|
|
while true; do
|
2017-03-07 14:21:22 +01:00
|
|
|
h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
|
2017-03-05 14:18:31 +01:00
|
|
|
if [ -z "$h" ]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
_domain="$h"
|
|
|
|
return 0
|
|
|
|
done
|
|
|
|
_debug "$domain not found"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
_checkKey() {
|
|
|
|
if [ -z "${KNOT_KEY}" ]; then
|
|
|
|
_err "You must specify a TSIG key to authenticate the request."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|