2016-11-22 00:24:26 +01:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
########
|
|
|
|
# Custom cyon.ch DNS API for use with [acme.sh](https://github.com/Neilpang/acme.sh)
|
|
|
|
#
|
|
|
|
# Usage: acme.sh --issue --dns dns_cyon -d www.domain.com
|
|
|
|
#
|
|
|
|
# Dependencies:
|
|
|
|
# -------------
|
|
|
|
# - oathtool (When using 2 Factor Authentication)
|
|
|
|
#
|
2016-12-27 00:56:05 +01:00
|
|
|
# Issues:
|
|
|
|
# -------
|
|
|
|
# Any issues / questions / suggestions can be posted here:
|
|
|
|
# https://github.com/noplanman/cyon-api/issues
|
2016-11-22 00:24:26 +01:00
|
|
|
#
|
2016-12-27 00:56:05 +01:00
|
|
|
# Author: Armando Lüscher <armando@noplanman.ch>
|
2016-11-22 00:24:26 +01:00
|
|
|
########
|
|
|
|
|
|
|
|
dns_cyon_add() {
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_load_credentials \
|
|
|
|
&& _cyon_load_parameters "$@" \
|
|
|
|
&& _cyon_print_header "add" \
|
|
|
|
&& _cyon_login \
|
|
|
|
&& _cyon_change_domain_env \
|
|
|
|
&& _cyon_add_txt \
|
|
|
|
&& _cyon_logout
|
2016-11-22 00:24:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dns_cyon_rm() {
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_load_credentials \
|
|
|
|
&& _cyon_load_parameters "$@" \
|
|
|
|
&& _cyon_print_header "delete" \
|
|
|
|
&& _cyon_login \
|
|
|
|
&& _cyon_change_domain_env \
|
|
|
|
&& _cyon_delete_txt \
|
|
|
|
&& _cyon_logout
|
2016-11-22 00:24:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#########################
|
|
|
|
### PRIVATE FUNCTIONS ###
|
|
|
|
#########################
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_load_credentials() {
|
2016-11-22 00:24:26 +01:00
|
|
|
# Convert loaded password to/from base64 as needed.
|
2016-12-28 14:51:39 +01:00
|
|
|
if [ "${CY_Password_B64}" ]; then
|
|
|
|
CY_Password="$(printf "%s" "${CY_Password_B64}" | _dbase64 "multiline")"
|
|
|
|
elif [ "${CY_Password}" ]; then
|
|
|
|
CY_Password_B64="$(printf "%s" "${CY_Password}" | _base64)"
|
2016-11-22 00:24:26 +01:00
|
|
|
fi
|
|
|
|
|
2016-12-28 14:51:39 +01:00
|
|
|
if [ -z "${CY_Username}" ] || [ -z "${CY_Password}" ]; then
|
2016-11-23 11:16:51 +01:00
|
|
|
# Dummy entries to satify script checker.
|
2016-12-28 14:51:39 +01:00
|
|
|
CY_Username=""
|
|
|
|
CY_Password=""
|
|
|
|
CY_OTP_Secret=""
|
2016-11-23 11:16:51 +01:00
|
|
|
|
2016-11-22 00:24:26 +01:00
|
|
|
_err ""
|
|
|
|
_err "You haven't set your cyon.ch login credentials yet."
|
|
|
|
_err "Please set the required cyon environment variables."
|
|
|
|
_err ""
|
2016-11-23 11:16:51 +01:00
|
|
|
return 1
|
2016-11-22 00:24:26 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Save the login credentials to the account.conf file.
|
|
|
|
_debug "Save credentials to account.conf"
|
2016-12-28 14:51:39 +01:00
|
|
|
_saveaccountconf CY_Username "${CY_Username}"
|
|
|
|
_saveaccountconf CY_Password_B64 "$CY_Password_B64"
|
|
|
|
if [ ! -z "${CY_OTP_Secret}" ]; then
|
|
|
|
_saveaccountconf CY_OTP_Secret "$CY_OTP_Secret"
|
2016-11-23 11:16:51 +01:00
|
|
|
else
|
2016-12-28 14:51:39 +01:00
|
|
|
_clearaccountconf CY_OTP_Secret
|
2016-11-22 00:24:26 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_is_idn() {
|
2016-11-23 11:16:51 +01:00
|
|
|
_idn_temp="$(printf "%s" "${1}" | tr -d "[0-9a-zA-Z.,-_]")"
|
|
|
|
_idn_temp2="$(printf "%s" "${1}" | grep -o "xn--")"
|
2016-11-22 00:24:26 +01:00
|
|
|
[ "$_idn_temp" ] || [ "$_idn_temp2" ]
|
|
|
|
}
|
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
# comment on https://stackoverflow.com/a/10797966
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_urlencode() {
|
|
|
|
curl -Gso /dev/null -w "%{url_effective}" --data-urlencode @- "" | cut -c 3-
|
2016-11-23 11:16:51 +01:00
|
|
|
}
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_load_parameters() {
|
2016-11-22 00:24:26 +01:00
|
|
|
# Read the required parameters to add the TXT entry.
|
2016-12-28 13:37:24 +01:00
|
|
|
fulldomain="$(printf "%s" "${1}" | tr '[A-Z]' '[a-z]')"
|
2016-11-22 00:24:26 +01:00
|
|
|
fulldomain_idn="${fulldomain}"
|
|
|
|
|
|
|
|
# Special case for IDNs, as cyon needs a domain environment change,
|
|
|
|
# which uses the "pretty" instead of the punycode version.
|
2016-12-16 03:44:48 +01:00
|
|
|
if _cyon_is_idn "${fulldomain}"; then
|
2016-11-22 00:24:26 +01:00
|
|
|
if ! _exists idn; then
|
2016-11-23 11:16:51 +01:00
|
|
|
_err "Please install idn to process IDN names."
|
|
|
|
_err ""
|
|
|
|
return 1
|
2016-11-22 00:24:26 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
fulldomain="$(idn -u "${fulldomain}")"
|
|
|
|
fulldomain_idn="$(idn -a "${fulldomain}")"
|
|
|
|
fi
|
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
_debug fulldomain "${fulldomain}"
|
|
|
|
_debug fulldomain_idn "${fulldomain_idn}"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
txtvalue="${2}"
|
|
|
|
_debug txtvalue "${txtvalue}"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
# This header is required for curl calls.
|
|
|
|
_H1="X-Requested-With: XMLHttpRequest"
|
2016-11-22 00:24:26 +01:00
|
|
|
}
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_print_header() {
|
2016-11-23 11:16:51 +01:00
|
|
|
if [ "${1}" = "add" ]; then
|
2016-11-22 00:24:26 +01:00
|
|
|
_info ""
|
|
|
|
_info "+---------------------------------------------+"
|
|
|
|
_info "| Adding DNS TXT entry to your cyon.ch domain |"
|
|
|
|
_info "+---------------------------------------------+"
|
|
|
|
_info ""
|
|
|
|
_info " * Full Domain: ${fulldomain}"
|
|
|
|
_info " * TXT Value: ${txtvalue}"
|
|
|
|
_info ""
|
2016-11-23 11:16:51 +01:00
|
|
|
elif [ "${1}" = "delete" ]; then
|
2016-11-22 00:24:26 +01:00
|
|
|
_info ""
|
|
|
|
_info "+-------------------------------------------------+"
|
|
|
|
_info "| Deleting DNS TXT entry from your cyon.ch domain |"
|
|
|
|
_info "+-------------------------------------------------+"
|
|
|
|
_info ""
|
|
|
|
_info " * Full Domain: ${fulldomain}"
|
|
|
|
_info ""
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_get_cookie_header() {
|
2017-01-04 03:19:58 +01:00
|
|
|
printf "Cookie: %s" "$(grep "cyon=" "$HTTP_HEADER" | grep "^Set-Cookie:" | _tail_n 1 | _egrep_o 'cyon=[^;]*;' | tr -d ';')"
|
2016-11-23 11:16:51 +01:00
|
|
|
}
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_login() {
|
2016-11-22 00:24:26 +01:00
|
|
|
_info " - Logging in..."
|
|
|
|
|
2016-12-28 14:51:39 +01:00
|
|
|
username_encoded="$(printf "%s" "${CY_Username}" | _cyon_urlencode)"
|
|
|
|
password_encoded="$(printf "%s" "${CY_Password}" | _cyon_urlencode)"
|
2016-11-23 11:16:51 +01:00
|
|
|
|
|
|
|
login_url="https://my.cyon.ch/auth/index/dologin-async"
|
|
|
|
login_data="$(printf "%s" "username=${username_encoded}&password=${password_encoded}&pathname=%2F")"
|
|
|
|
|
|
|
|
login_response="$(_post "$login_data" "$login_url")"
|
2016-11-22 00:24:26 +01:00
|
|
|
_debug login_response "${login_response}"
|
|
|
|
|
|
|
|
# Bail if login fails.
|
2016-12-16 03:44:48 +01:00
|
|
|
if [ "$(printf "%s" "${login_response}" | _cyon_get_response_success)" != "success" ]; then
|
|
|
|
_err " $(printf "%s" "${login_response}" | _cyon_get_response_message)"
|
2016-11-23 11:16:51 +01:00
|
|
|
_err ""
|
|
|
|
return 1
|
2016-11-22 00:24:26 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
_info " success"
|
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
# NECESSARY!! Load the main page after login, to get the new cookie.
|
2016-12-16 03:44:48 +01:00
|
|
|
_H2="$(_cyon_get_cookie_header)"
|
|
|
|
_get "https://my.cyon.ch/" >/dev/null
|
2016-11-22 00:24:26 +01:00
|
|
|
|
|
|
|
# todo: instead of just checking if the env variable is defined, check if we actually need to do a 2FA auth request.
|
|
|
|
|
|
|
|
# 2FA authentication with OTP?
|
2016-12-28 14:51:39 +01:00
|
|
|
if [ ! -z "${CY_OTP_Secret}" ]; then
|
2016-11-22 00:24:26 +01:00
|
|
|
_info " - Authorising with OTP code..."
|
|
|
|
|
|
|
|
if ! _exists oathtool; then
|
2016-11-23 11:16:51 +01:00
|
|
|
_err "Please install oathtool to use 2 Factor Authentication."
|
|
|
|
_err ""
|
|
|
|
return 1
|
2016-11-22 00:24:26 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Get OTP code with the defined secret.
|
2016-12-28 14:51:39 +01:00
|
|
|
otp_code="$(oathtool --base32 --totp "${CY_OTP_Secret}" 2>/dev/null)"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
login_otp_url="https://my.cyon.ch/auth/multi-factor/domultifactorauth-async"
|
|
|
|
login_otp_data="totpcode=${otp_code}&pathname=%2F&rememberme=0"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
login_otp_response="$(_post "$login_otp_data" "$login_otp_url")"
|
|
|
|
_debug login_otp_response "${login_otp_response}"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
|
|
|
# Bail if OTP authentication fails.
|
2016-12-16 03:44:48 +01:00
|
|
|
if [ "$(printf "%s" "${login_otp_response}" | _cyon_get_response_success)" != "success" ]; then
|
|
|
|
_err " $(printf "%s" "${login_otp_response}" | _cyon_get_response_message)"
|
2016-11-23 11:16:51 +01:00
|
|
|
_err ""
|
|
|
|
return 1
|
2016-11-22 00:24:26 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
_info " success"
|
|
|
|
fi
|
|
|
|
|
|
|
|
_info ""
|
|
|
|
}
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_logout() {
|
2016-11-23 11:16:51 +01:00
|
|
|
_info " - Logging out..."
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_get "https://my.cyon.ch/auth/index/dologout" >/dev/null
|
2016-11-23 11:16:51 +01:00
|
|
|
|
|
|
|
_info " success"
|
|
|
|
_info ""
|
|
|
|
}
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_change_domain_env() {
|
2016-11-22 00:24:26 +01:00
|
|
|
_info " - Changing domain environment..."
|
|
|
|
|
|
|
|
# Get the "example.com" part of the full domain name.
|
2016-11-23 11:16:51 +01:00
|
|
|
domain_env="$(printf "%s" "${fulldomain}" | sed -E -e 's/.*\.(.*\..*)$/\1/')"
|
2016-11-22 00:24:26 +01:00
|
|
|
_debug "Changing domain environment to ${domain_env}"
|
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
domain_env_url="https://my.cyon.ch/user/environment/setdomain/d/${domain_env}/gik/domain%3A${domain_env}"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
domain_env_response="$(_get "${domain_env_url}")"
|
2016-11-22 00:24:26 +01:00
|
|
|
_debug domain_env_response "${domain_env_response}"
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
if ! _cyon_check_if_2fa_missed "${domain_env_response}"; then return 1; fi
|
2016-11-22 00:24:26 +01:00
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
domain_env_success="$(printf "%s" "${domain_env_response}" | _egrep_o '"authenticated":\w*' | cut -d : -f 2)"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
|
|
|
# Bail if domain environment change fails.
|
|
|
|
if [ "${domain_env_success}" != "true" ]; then
|
2016-12-16 03:44:48 +01:00
|
|
|
_err " $(printf "%s" "${domain_env_response}" | _cyon_get_response_message)"
|
2016-11-23 11:16:51 +01:00
|
|
|
_err ""
|
|
|
|
return 1
|
2016-11-22 00:24:26 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
_info " success"
|
|
|
|
_info ""
|
|
|
|
}
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_add_txt() {
|
2016-11-22 00:24:26 +01:00
|
|
|
_info " - Adding DNS TXT entry..."
|
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
add_txt_url="https://my.cyon.ch/domain/dnseditor/add-record-async"
|
|
|
|
add_txt_data="zone=${fulldomain_idn}.&ttl=900&type=TXT&value=${txtvalue}"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
add_txt_response="$(_post "$add_txt_data" "$add_txt_url")"
|
|
|
|
_debug add_txt_response "${add_txt_response}"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
if ! _cyon_check_if_2fa_missed "${add_txt_response}"; then return 1; fi
|
2016-11-23 11:16:51 +01:00
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
add_txt_message="$(printf "%s" "${add_txt_response}" | _cyon_get_response_message)"
|
|
|
|
add_txt_status="$(printf "%s" "${add_txt_response}" | _cyon_get_response_status)"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
|
|
|
# Bail if adding TXT entry fails.
|
2016-11-23 11:16:51 +01:00
|
|
|
if [ "${add_txt_status}" != "true" ]; then
|
|
|
|
_err " ${add_txt_message}"
|
|
|
|
_err ""
|
|
|
|
return 1
|
2016-11-22 00:24:26 +01:00
|
|
|
fi
|
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
_info " success (TXT|${fulldomain_idn}.|${txtvalue})"
|
2016-11-22 00:24:26 +01:00
|
|
|
_info ""
|
|
|
|
}
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_delete_txt() {
|
2016-11-22 00:24:26 +01:00
|
|
|
_info " - Deleting DNS TXT entry..."
|
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
list_txt_url="https://my.cyon.ch/domain/dnseditor/list-async"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
list_txt_response="$(_get "${list_txt_url}" | sed -e 's/data-hash/\\ndata-hash/g')"
|
2016-11-22 00:24:26 +01:00
|
|
|
_debug list_txt_response "${list_txt_response}"
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
if ! _cyon_check_if_2fa_missed "${list_txt_response}"; then return 1; fi
|
2016-11-22 00:24:26 +01:00
|
|
|
|
|
|
|
# Find and delete all acme challenge entries for the $fulldomain.
|
2016-11-23 11:16:51 +01:00
|
|
|
_dns_entries="$(printf "%b\n" "${list_txt_response}" | sed -n 's/data-hash=\\"\([^"]*\)\\" data-identifier=\\"\([^"]*\)\\".*/\1 \2/p')"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
2016-11-22 18:31:38 +01:00
|
|
|
printf "%s" "${_dns_entries}" | while read -r _hash _identifier; do
|
|
|
|
dns_type="$(printf "%s" "$_identifier" | cut -d'|' -f1)"
|
|
|
|
dns_domain="$(printf "%s" "$_identifier" | cut -d'|' -f2)"
|
2016-11-22 15:21:04 +01:00
|
|
|
|
|
|
|
if [ "${dns_type}" != "TXT" ] || [ "${dns_domain}" != "${fulldomain_idn}." ]; then
|
|
|
|
continue
|
|
|
|
fi
|
2016-11-22 00:24:26 +01:00
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
hash_encoded="$(printf "%s" "${_hash}" | _cyon_urlencode)"
|
|
|
|
identifier_encoded="$(printf "%s" "${_identifier}" | _cyon_urlencode)"
|
2016-11-23 11:16:51 +01:00
|
|
|
|
|
|
|
delete_txt_url="https://my.cyon.ch/domain/dnseditor/delete-record-async"
|
|
|
|
delete_txt_data="$(printf "%s" "hash=${hash_encoded}&identifier=${identifier_encoded}")"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
2016-11-23 11:16:51 +01:00
|
|
|
delete_txt_response="$(_post "$delete_txt_data" "$delete_txt_url")"
|
2016-11-22 00:24:26 +01:00
|
|
|
_debug delete_txt_response "${delete_txt_response}"
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
if ! _cyon_check_if_2fa_missed "${delete_txt_response}"; then return 1; fi
|
2016-11-22 00:24:26 +01:00
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
delete_txt_message="$(printf "%s" "${delete_txt_response}" | _cyon_get_response_message)"
|
|
|
|
delete_txt_status="$(printf "%s" "${delete_txt_response}" | _cyon_get_response_status)"
|
2016-11-22 00:24:26 +01:00
|
|
|
|
|
|
|
# Skip if deleting TXT entry fails.
|
|
|
|
if [ "${delete_txt_status}" != "true" ]; then
|
2016-11-22 15:21:04 +01:00
|
|
|
_err " ${delete_txt_message} (${_identifier})"
|
2016-11-22 00:24:26 +01:00
|
|
|
else
|
2016-11-22 15:21:04 +01:00
|
|
|
_info " success (${_identifier})"
|
2016-11-22 00:24:26 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
_info " done"
|
|
|
|
_info ""
|
|
|
|
}
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_get_response_message() {
|
2016-11-22 18:06:16 +01:00
|
|
|
_egrep_o '"message":"[^"]*"' | cut -d : -f 2 | tr -d '"'
|
|
|
|
}
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_get_response_status() {
|
2016-11-22 18:06:16 +01:00
|
|
|
_egrep_o '"status":\w*' | cut -d : -f 2
|
|
|
|
}
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_get_response_success() {
|
2016-11-22 18:06:16 +01:00
|
|
|
_egrep_o '"onSuccess":"[^"]*"' | cut -d : -f 2 | tr -d '"'
|
|
|
|
}
|
|
|
|
|
2016-12-16 03:44:48 +01:00
|
|
|
_cyon_check_if_2fa_missed() {
|
2016-11-22 00:24:26 +01:00
|
|
|
# Did we miss the 2FA?
|
2016-11-23 11:16:51 +01:00
|
|
|
if test "${1#*multi_factor_form}" != "${1}"; then
|
|
|
|
_err " Missed OTP authentication!"
|
2016-12-16 03:44:48 +01:00
|
|
|
_err ""
|
|
|
|
return 1
|
2016-11-23 11:16:51 +01:00
|
|
|
fi
|
2016-11-22 00:24:26 +01:00
|
|
|
}
|