acme.sh/dnsapi/dns_simply.sh

260 lines
5.8 KiB
Bash
Raw Normal View History

2020-11-17 12:19:55 +00:00
#!/usr/bin/env sh
#
#SIMPLY_AccountName="accountname"
#
#SIMPLY_ApiKey="apikey"
#
#SIMPLY_Api="https://api.simply.com/1/[ACCOUNTNAME]/[APIKEY]"
SIMPLY_SUCCESS_CODE='"status": 200'
SIMPLY_Api_Default="https://api.simply.com/1"
Syncing with the original repo (#2) * change arvan api script * change Author name * change name actor * Updated --preferred-chain to issue ISRG properly To support different openssl crl2pkcs7 help cli format * dnsapi/pdns: also normalize json response in detecting root zone * Chain (#3408) * fix https://github.com/acmesh-official/acme.sh/issues/3384 match the issuer to the root CA cert subject * fix format * fix https://github.com/acmesh-official/acme.sh/issues/3384 * remove the alt files. https://github.com/acmesh-official/acme.sh/issues/3384 * upgrade freebsd and solaris * duckdns - fix "integer expression expected" errors (#3397) * fix "integer expression expected" errors * duckdns fix * Update dns_duckdns.sh * Update dns_duckdns.sh * Implement smtp notify hook Support notifications via direct SMTP server connection. Uses Python (2.7.x or 3.4+) to communicate with SMTP server. * Make shfmt happy (I'm open to better ways of formatting the heredoc that embeds the Python script.) * Only save config if send is successful * Add instructions for reporting bugs * Prep for curl or Python; clean up SMTP_* variable usage * Implement curl version of smtp notify-hook * More than one blank line is an abomination, apparently I will not try to use whitespace to group code visually * Fix: Unifi deploy hook support Unifi Cloud Key (#3327) * fix: unifi deploy hook also update Cloud Key nginx certs When running on a Unifi Cloud Key device, also deploy to /etc/ssl/private/cloudkey.{crt,key} and reload nginx. This makes the new cert available for the Cloud Key management app running via nginx on port 443 (as well as the port 8443 Unifi Controller app the deploy hook already supported). Fixes #3326 * Improve settings documentation comments * Improve Cloud Key pre-flight error messaging * Fix typo * Add support for UnifiOS (Cloud Key Gen2) Since UnifiOS does not use the Java keystore (like a Unifi Controller or Cloud Key Gen1 deploy), this also reworks the settings validation and error messaging somewhat. * PR review fixes * Detect unsupported Cloud Key java keystore location * Don't try to restart inactive services (and remove extra spaces from reload command) * Clean up error messages and internal variables * Change to _getdeployconf/_savedeployconf * Switch from cp to cat to preserve file permissions * feat: add huaweicloud error handling * fix: fix freebsd and solaris * support openssl 3.0 fix https://github.com/acmesh-official/acme.sh/issues/3399 * make the fix for rsa key only * Use PROJECT_NAME and VER for X-Mailer header Also add X-Mailer header to Python version * Add _clearaccountconf_mutable() * Rework read/save config to not save default values Add and use _readaccountconf_mutable_default and _saveaccountconf_mutable_default helpers to capture common default value handling. New approach also eliminates need for separate underscore-prefixed version of each conf var. * Implement _rfc2822_date helper * Clean email headers and warn on unsupported address format Just in case, make sure CR or NL don't end up in an email header. * Clarify _readaccountconf_mutable_default * Add Date email header in Python implementation * Use email.policy.default in Python 3 implementation Improves standards compatibility and utf-8 handling in Python 3.3-3.8. (email.policy.default becomes the default in Python 3.9.) * Prefer Python to curl when both available * Change default SMTP_SECURE to "tls" Secure by default. Also try to minimize configuration errors. (Many ESPs/ISPs require STARTTLS, and most support it.) * Update dns_dp.sh 没有encode中文字符会导致提交失败 * No need to include EC parameters explicitly with the private key. (they are embedded) * Fixes response handling and thereby allow issuing of subdomain certs * Adds comment * fix https://github.com/acmesh-official/acme.sh/issues/3402 * dnsapi/ionos: Use POST instead of PATCH for adding TXT record The API now supports a POST route for adding records. Therefore checking for already existing records and including them in a PATCH request is no longer necessary. * fix https://github.com/acmesh-official/acme.sh/issues/3433 * fix https://github.com/acmesh-official/acme.sh/issues/3019 * fix format * Update dns_servercow.sh to support wildcard certs Updated dns_servercow.sh to support txt records with multiple entries. This supports wildcard certificates that require txt records with the same name and different contents. * Update dns_servercow.sh to support wildcard certs Updated dns_servercow.sh to support txt records with multiple entries. This supports wildcard certificates that require txt records with the same name and different contents. * fix https://github.com/acmesh-official/acme.sh/issues/3312 * fix format * feat: add dns_porkbun * fix: prevent rate limit Co-authored-by: Vahid Fardi <vahid.fardi@snapp.cab> Co-authored-by: neil <github@neilpang.com> Co-authored-by: Gnought <1684105+gnought@users.noreply.github.com> Co-authored-by: manuel <manuel@mausz.at> Co-authored-by: jerrm <jerrm@users.noreply.github.com> Co-authored-by: medmunds <medmunds@gmail.com> Co-authored-by: Mike Edmunds <github@to.mikeedmunds.com> Co-authored-by: Easton Man <manyang.me@outlook.com> Co-authored-by: czeming <loser_wind@163.com> Co-authored-by: Geert Hendrickx <geert@hendrickx.be> Co-authored-by: Kristian Johansson <kristian.johansson86@gmail.com> Co-authored-by: Lukas Brocke <lukas@brocke.net> Co-authored-by: anom-human <80478363+anom-human@users.noreply.github.com> Co-authored-by: neil <win10@neilpang.com> Co-authored-by: Quentin Dreyer <quentin.dreyer@rgsystem.com>
2021-03-20 15:01:09 +00:00
2020-11-18 17:01:02 +00:00
######## Public functions #####################
2020-11-17 12:19:55 +00:00
#Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
dns_simply_add() {
fulldomain=$1
txtvalue=$2
if ! _simply_load_config; then
return 1
fi
_simply_save_config
2020-11-18 14:46:16 +00:00
_debug "First detect the root zone"
2020-11-17 12:19:55 +00:00
if ! _get_root "$fulldomain"; then
_err "invalid domain"
return 1
fi
2020-11-18 14:46:16 +00:00
2020-11-17 12:19:55 +00:00
_debug _sub_domain "$_sub_domain"
_debug _domain "$_domain"
_info "Adding record"
2020-11-18 14:46:16 +00:00
if ! _simply_add_record "$_domain" "$_sub_domain" "$txtvalue"; then
2020-11-17 12:19:55 +00:00
_err "Could not add DNS record"
return 1
2020-11-18 14:46:16 +00:00
fi
2020-11-17 12:19:55 +00:00
return 0
}
dns_simply_rm() {
fulldomain=$1
txtvalue=$2
2020-11-18 14:46:16 +00:00
2020-11-17 12:19:55 +00:00
if ! _simply_load_config; then
return 1
fi
_simply_save_config
_debug "First detect the root zone"
2020-11-18 14:55:02 +00:00
2020-11-17 12:19:55 +00:00
if ! _get_root "$fulldomain"; then
_err "invalid domain"
return 1
fi
_debug _sub_domain "$_sub_domain"
_debug _domain "$_domain"
2020-11-17 12:49:32 +00:00
_debug txtvalue "$txtvalue"
2020-11-17 12:19:55 +00:00
_info "Getting all existing records"
2020-11-18 14:46:16 +00:00
2020-11-17 12:19:55 +00:00
if ! _simply_get_all_records "$_domain"; then
_err "invalid domain"
return 1
fi
2020-11-18 14:46:16 +00:00
2020-11-18 14:52:46 +00:00
records=$(echo "$response" | tr '{' "\n" | grep 'record_id\|type\|data\|\name' | sed 's/\"record_id/;\"record_id/' | tr "\n" ' ' | tr -d ' ' | tr ';' ' ')
2020-11-18 13:52:32 +00:00
2020-11-17 12:19:55 +00:00
nr_of_deleted_records=0
2020-11-18 13:52:32 +00:00
_info "Fetching txt record"
2020-11-17 12:19:55 +00:00
2020-11-18 14:52:46 +00:00
for record in $records; do
_debug record "$record"
2020-11-18 14:46:16 +00:00
2020-11-18 14:52:46 +00:00
record_data=$(echo "$record" | cut -d "," -f 3 | sed 's/"//g' | grep "data" | cut -d ":" -f 2)
record_type=$(echo "$record" | cut -d "," -f 4 | sed 's/"//g' | grep "type" | cut -d ":" -f 2)
_debug2 record_data "$record_data"
_debug2 record_type "$record_type"
2020-11-18 14:55:02 +00:00
2020-11-18 14:46:16 +00:00
if [ "$record_data" = "$txtvalue" ] && [ "$record_type" = "TXT" ]; then
2020-11-18 14:12:22 +00:00
record_id=$(echo "$record" | cut -d "," -f 1 | grep "record_id" | cut -d ":" -f 2)
2020-11-18 14:46:16 +00:00
2020-11-18 14:12:22 +00:00
_info "Deleting record $record"
2020-11-18 13:52:32 +00:00
_debug2 record_id "$record_id"
2020-11-18 14:46:16 +00:00
2020-11-18 13:52:32 +00:00
if [ "$record_id" -gt 0 ]; then
2020-11-18 14:46:16 +00:00
if ! _simply_delete_record "$_domain" "$_sub_domain" "$record_id"; then
_err "Record with id $record_id could not be deleted"
return 1
fi
2020-11-18 14:46:16 +00:00
nr_of_deleted_records=1
break
2020-11-18 14:52:46 +00:00
else
_err "Fetching record_id could not be done, this should not happen, exiting function. Failing record is $record"
break
fi
2020-11-17 12:19:55 +00:00
fi
2020-11-18 14:46:16 +00:00
2020-11-17 12:19:55 +00:00
done
2020-11-18 13:52:32 +00:00
if [ "$nr_of_deleted_records" -eq 0 ]; then
2020-11-18 14:52:46 +00:00
_err "No record deleted, the DNS record needs to be removed manually."
2020-11-17 12:19:55 +00:00
else
_info "Deleted $nr_of_deleted_records record"
fi
2020-11-18 14:46:16 +00:00
2020-11-17 12:19:55 +00:00
return 0
}
#################### Private functions below ##################################
_simply_load_config() {
SIMPLY_Api="${SIMPLY_Api:-$(_readaccountconf_mutable SIMPLY_Api)}"
SIMPLY_AccountName="${SIMPLY_AccountName:-$(_readaccountconf_mutable SIMPLY_AccountName)}"
SIMPLY_ApiKey="${SIMPLY_ApiKey:-$(_readaccountconf_mutable SIMPLY_ApiKey)}"
if [ -z "$SIMPLY_Api" ]; then
SIMPLY_Api="$SIMPLY_Api_Default"
fi
2020-11-18 14:37:26 +00:00
2020-11-17 12:19:55 +00:00
if [ -z "$SIMPLY_AccountName" ] || [ -z "$SIMPLY_ApiKey" ]; then
SIMPLY_AccountName=""
SIMPLY_ApiKey=""
_err "A valid Simply API account and apikey not provided."
_err "Please provide a valid API user and try again."
return 1
fi
return 0
}
_simply_save_config() {
if [ "$SIMPLY_Api" != "$SIMPLY_Api_Default" ]; then
_saveaccountconf_mutable SIMPLY_Api "$SIMPLY_Api"
fi
_saveaccountconf_mutable SIMPLY_AccountName "$SIMPLY_AccountName"
_saveaccountconf_mutable SIMPLY_ApiKey "$SIMPLY_ApiKey"
}
_simply_get_all_records() {
2020-11-18 14:37:26 +00:00
domain=$1
2020-11-17 12:19:55 +00:00
if ! _simply_rest GET "my/products/$domain/dns/records"; then
return 1
fi
return 0
}
_get_root() {
domain=$1
i=2
p=1
while true; do
h=$(printf "%s" "$domain" | cut -d . -f $i-100)
if [ -z "$h" ]; then
#not valid
return 1
fi
if ! _simply_rest GET "my/products/$h/dns"; then
return 1
fi
if ! _contains "$response" "$SIMPLY_SUCCESS_CODE"; then
2020-11-17 12:19:55 +00:00
_debug "$h not found"
else
_sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
_domain="$h"
return 0
fi
p="$i"
i=$(_math "$i" + 1)
done
return 1
}
_simply_add_record() {
domain=$1
sub_domain=$2
txtval=$3
2020-11-18 14:37:26 +00:00
2020-11-17 12:19:55 +00:00
data="{\"name\": \"$sub_domain\", \"type\":\"TXT\", \"data\": \"$txtval\", \"priority\":0, \"ttl\": 3600}"
if ! _simply_rest POST "my/products/$domain/dns/records" "$data"; then
_err "Adding record not successfull!"
return 1
fi
2020-11-18 14:37:26 +00:00
if ! _contains "$response" "$SIMPLY_SUCCESS_CODE"; then
_err "Call to API not sucessfull, see below message for more details"
_err "$response"
return 1
fi
2020-11-17 12:19:55 +00:00
return 0
}
_simply_delete_record() {
domain=$1
sub_domain=$2
record_id=$3
2020-11-18 14:37:26 +00:00
2020-11-17 12:49:32 +00:00
_debug record_id "Delete record with id $record_id"
2020-11-18 14:37:26 +00:00
2020-11-17 12:19:55 +00:00
if ! _simply_rest DELETE "my/products/$domain/dns/records/$record_id"; then
_err "Deleting record not successfull!"
return 1
fi
2020-11-18 14:37:26 +00:00
if ! _contains "$response" "$SIMPLY_SUCCESS_CODE"; then
_err "Call to API not sucessfull, see below message for more details"
_err "$response"
return 1
fi
2020-11-17 12:19:55 +00:00
return 0
}
_simply_rest() {
m=$1
ep="$2"
data="$3"
2020-11-18 14:37:26 +00:00
2020-11-17 12:49:32 +00:00
_debug2 data "$data"
_debug2 ep "$ep"
_debug2 m "$m"
2020-11-17 12:19:55 +00:00
export _H1="Content-Type: application/json"
if [ "$m" != "GET" ]; then
response="$(_post "$data" "$SIMPLY_Api/$SIMPLY_AccountName/$SIMPLY_ApiKey/$ep" "" "$m")"
else
response="$(_get "$SIMPLY_Api/$SIMPLY_AccountName/$SIMPLY_ApiKey/$ep")"
fi
if [ "$?" != "0" ]; then
_err "error $ep"
return 1
fi
2020-11-18 14:37:26 +00:00
2020-11-17 12:19:55 +00:00
_debug2 response "$response"
2020-11-18 14:37:26 +00:00
2020-11-17 12:19:55 +00:00
if _contains "$response" "Invalid account authorization"; then
_err "It seems that your api key or accountnumber is not correct."
return 1
fi
2020-11-18 14:46:16 +00:00
2020-11-17 12:19:55 +00:00
return 0
}