2017-12-09 18:32:53 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
##########
|
2020-01-30 04:06:39 +00:00
|
|
|
# Custom servercow.de DNS API v1 for use with [acme.sh](https://github.com/acmesh-official/acme.sh)
|
2017-12-09 18:32:53 +00:00
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# export SERVERCOW_API_Username=username
|
|
|
|
# export SERVERCOW_API_Password=password
|
|
|
|
# acme.sh --issue -d example.com --dns dns_servercow
|
|
|
|
#
|
|
|
|
# Issues:
|
|
|
|
# Any issues / questions / suggestions can be posted here:
|
|
|
|
# https://github.com/jhartlep/servercow-dns-api/issues
|
|
|
|
#
|
|
|
|
# Author: Jens Hartlep
|
|
|
|
##########
|
|
|
|
|
|
|
|
SERVERCOW_API="https://api.servercow.de/dns/v1/domains"
|
|
|
|
|
|
|
|
# Usage dns_servercow_add _acme-challenge.www.domain.com "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
dns_servercow_add() {
|
2017-12-09 19:14:46 +00:00
|
|
|
fulldomain=$1
|
|
|
|
txtvalue=$2
|
|
|
|
|
|
|
|
_info "Using servercow"
|
|
|
|
_debug fulldomain "$fulldomain"
|
|
|
|
_debug txtvalue "$txtvalue"
|
|
|
|
|
|
|
|
SERVERCOW_API_Username="${SERVERCOW_API_Username:-$(_readaccountconf_mutable SERVERCOW_API_Username)}"
|
|
|
|
SERVERCOW_API_Password="${SERVERCOW_API_Password:-$(_readaccountconf_mutable SERVERCOW_API_Password)}"
|
|
|
|
if [ -z "$SERVERCOW_API_Username" ] || [ -z "$SERVERCOW_API_Password" ]; then
|
|
|
|
SERVERCOW_API_Username=""
|
|
|
|
SERVERCOW_API_Password=""
|
|
|
|
_err "You don't specify servercow api username and password yet."
|
|
|
|
_err "Please create your username and password and try again."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# save the credentials to the account conf file
|
|
|
|
_saveaccountconf_mutable SERVERCOW_API_Username "$SERVERCOW_API_Username"
|
|
|
|
_saveaccountconf_mutable SERVERCOW_API_Password "$SERVERCOW_API_Password"
|
|
|
|
|
|
|
|
_debug "First detect the root zone"
|
|
|
|
if ! _get_root "$fulldomain"; then
|
|
|
|
_err "invalid domain"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_debug _sub_domain "$_sub_domain"
|
|
|
|
_debug _domain "$_domain"
|
2021-03-11 19:25:49 +00:00
|
|
|
|
|
|
|
# check whether a txt record already exists for the subdomain
|
2021-03-11 18:11:02 +00:00
|
|
|
if printf -- "%s" "$response" | grep "{\"name\":\"$_sub_domain\",\"ttl\":20,\"type\":\"TXT\"" >/dev/null; then
|
2021-03-11 19:25:49 +00:00
|
|
|
_info "A txt record with the same name already exists."
|
|
|
|
# trim the string on the left
|
|
|
|
txtvalue_old=${response#*{\"name\":\"$_sub_domain\",\"ttl\":20,\"type\":\"TXT\",\"content\":\"}
|
|
|
|
# trim the string on the right
|
|
|
|
txtvalue_old=${txtvalue_old%%\"*}
|
|
|
|
|
|
|
|
_debug txtvalue_old "$txtvalue_old"
|
|
|
|
|
|
|
|
_info "Add the new txtvalue to the existing txt record."
|
|
|
|
if _servercow_api POST "$_domain" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":[\"$txtvalue\",\"$txtvalue_old\"],\"ttl\":20}"; then
|
|
|
|
if printf -- "%s" "$response" | grep "ok" >/dev/null; then
|
|
|
|
_info "Added additional txtvalue, OK"
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
_err "add txt record error."
|
2021-03-11 18:11:02 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2021-03-11 19:25:49 +00:00
|
|
|
fi
|
|
|
|
_err "add txt record error."
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
_info "There is no txt record with the name yet."
|
|
|
|
if _servercow_api POST "$_domain" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":20}"; then
|
|
|
|
if printf -- "%s" "$response" | grep "ok" >/dev/null; then
|
|
|
|
_info "Added, OK"
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
_err "add txt record error."
|
2021-03-11 18:11:02 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2021-03-11 19:25:49 +00:00
|
|
|
fi
|
|
|
|
_err "add txt record error."
|
|
|
|
return 1
|
2017-12-09 19:14:46 +00:00
|
|
|
fi
|
2021-03-11 19:25:49 +00:00
|
|
|
|
2017-12-09 19:14:46 +00:00
|
|
|
return 1
|
2017-12-09 18:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Usage fulldomain txtvalue
|
|
|
|
# Remove the txt record after validation
|
|
|
|
dns_servercow_rm() {
|
2017-12-09 19:14:46 +00:00
|
|
|
fulldomain=$1
|
|
|
|
txtvalue=$2
|
|
|
|
|
|
|
|
_info "Using servercow"
|
|
|
|
_debug fulldomain "$fulldomain"
|
|
|
|
_debug txtvalue "$fulldomain"
|
|
|
|
|
|
|
|
SERVERCOW_API_Username="${SERVERCOW_API_Username:-$(_readaccountconf_mutable SERVERCOW_API_Username)}"
|
|
|
|
SERVERCOW_API_Password="${SERVERCOW_API_Password:-$(_readaccountconf_mutable SERVERCOW_API_Password)}"
|
|
|
|
if [ -z "$SERVERCOW_API_Username" ] || [ -z "$SERVERCOW_API_Password" ]; then
|
|
|
|
SERVERCOW_API_Username=""
|
|
|
|
SERVERCOW_API_Password=""
|
|
|
|
_err "You don't specify servercow api username and password yet."
|
|
|
|
_err "Please create your username and password and try again."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_debug "First detect the root zone"
|
|
|
|
if ! _get_root "$fulldomain"; then
|
|
|
|
_err "invalid domain"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
_debug _sub_domain "$_sub_domain"
|
|
|
|
_debug _domain "$_domain"
|
|
|
|
|
2017-12-09 19:18:05 +00:00
|
|
|
if _servercow_api DELETE "$_domain" "{\"type\":\"TXT\",\"name\":\"$fulldomain\"}"; then
|
|
|
|
if printf -- "%s" "$response" | grep "ok" >/dev/null; then
|
2017-12-09 19:14:46 +00:00
|
|
|
_info "Deleted, OK"
|
|
|
|
_contains "$response" '"message":"ok"'
|
|
|
|
else
|
|
|
|
_err "delete txt record error."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
fi
|
2017-12-09 18:32:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#################### Private functions below ##################################
|
|
|
|
|
|
|
|
# _acme-challenge.www.domain.com
|
|
|
|
# returns
|
|
|
|
# _sub_domain=_acme-challenge.www
|
|
|
|
# _domain=domain.com
|
|
|
|
_get_root() {
|
2017-12-09 19:14:46 +00:00
|
|
|
fulldomain=$1
|
|
|
|
i=2
|
|
|
|
p=1
|
|
|
|
|
|
|
|
while true; do
|
|
|
|
_domain=$(printf "%s" "$fulldomain" | cut -d . -f $i-100)
|
|
|
|
|
|
|
|
_debug _domain "$_domain"
|
|
|
|
if [ -z "$_domain" ]; then
|
|
|
|
# not valid
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! _servercow_api GET "$_domain"; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2017-12-09 19:22:09 +00:00
|
|
|
if ! _contains "$response" '"error":"no such domain in user context"' >/dev/null; then
|
2017-12-09 19:14:46 +00:00
|
|
|
_sub_domain=$(printf "%s" "$fulldomain" | cut -d . -f 1-$p)
|
|
|
|
if [ -z "$_sub_domain" ]; then
|
|
|
|
# not valid
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
p=$i
|
|
|
|
i=$(_math "$i" + 1)
|
2017-12-09 19:18:05 +00:00
|
|
|
done
|
2017-12-09 19:14:46 +00:00
|
|
|
|
|
|
|
return 1
|
2017-12-09 18:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_servercow_api() {
|
2017-12-09 19:14:46 +00:00
|
|
|
method=$1
|
|
|
|
domain=$2
|
|
|
|
data="$3"
|
|
|
|
|
|
|
|
export _H1="Content-Type: application/json"
|
|
|
|
export _H2="X-Auth-Username: $SERVERCOW_API_Username"
|
|
|
|
export _H3="X-Auth-Password: $SERVERCOW_API_Password"
|
|
|
|
|
|
|
|
if [ "$method" != "GET" ]; then
|
|
|
|
_debug data "$data"
|
|
|
|
response="$(_post "$data" "$SERVERCOW_API/$domain" "" "$method")"
|
|
|
|
else
|
|
|
|
response="$(_get "$SERVERCOW_API/$domain")"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$?" != "0" ]; then
|
|
|
|
_err "error $domain"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
_debug2 response "$response"
|
|
|
|
return 0
|
2017-12-09 18:32:53 +00:00
|
|
|
}
|