|
| 1 | +#!/usr/bin/env sh |
| 2 | +# shellcheck disable=SC2034 |
| 3 | +dns_calrissia_info='Calrissia.be DNS API |
| 4 | +Site: calrissia.be |
| 5 | +Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_calrissia |
| 6 | +Options: |
| 7 | + CALRISSIA_TOKEN Personal access token |
| 8 | +Issues: github.com/acmesh-official/acme.sh/pull/6802 |
| 9 | +Author: Ward Hus |
| 10 | +' |
| 11 | + |
| 12 | +CALRISSIA_API="https://my.calrissia.com/api" |
| 13 | + |
| 14 | +dns_calrissia_add() { |
| 15 | + fulldomain="$1" |
| 16 | + txtvalue="$2" |
| 17 | + |
| 18 | + _calrissia_load_token || return 1 |
| 19 | + |
| 20 | + if ! _calrissia_get_root "$fulldomain"; then |
| 21 | + _err "Unable to find domain in Calrissia account for: $fulldomain" |
| 22 | + return 1 |
| 23 | + fi |
| 24 | + |
| 25 | + _debug "domain='$_domain' id='$_domain_id' sub='$_sub_domain'" |
| 26 | + _info "Adding TXT record for $fulldomain" |
| 27 | + |
| 28 | + _body="{\"name\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"ttl\":120,\"prio\":0}" |
| 29 | + _response="$(_calrissia_request POST "/domain/$_domain_id/record" "$_body")" |
| 30 | + |
| 31 | + if ! _contains "$_response" '"id"'; then |
| 32 | + _err "Failed to create TXT record: $_response" |
| 33 | + return 1 |
| 34 | + fi |
| 35 | + |
| 36 | + _record_id="$(printf "%s" "$_response" | _egrep_o '"id":[[:space:]]*[0-9]+' | _egrep_o '[0-9]+')" |
| 37 | + _key="$(_calrissia_key "$fulldomain" "$txtvalue")" |
| 38 | + _savedomainconf "CALRISSIA_RECORD_ID_$_key" "$_record_id" |
| 39 | + _savedomainconf "CALRISSIA_DOMAIN_ID_$_key" "$_domain_id" |
| 40 | + |
| 41 | + return 0 |
| 42 | +} |
| 43 | + |
| 44 | +dns_calrissia_rm() { |
| 45 | + fulldomain="$1" |
| 46 | + txtvalue="$2" |
| 47 | + |
| 48 | + _calrissia_load_token || return 1 |
| 49 | + |
| 50 | + _key="$(_calrissia_key "$fulldomain" "$txtvalue")" |
| 51 | + _record_id="$(_readdomainconf "CALRISSIA_RECORD_ID_$_key")" |
| 52 | + _domain_id="$(_readdomainconf "CALRISSIA_DOMAIN_ID_$_key")" |
| 53 | + |
| 54 | + if [ -z "$_record_id" ] || [ -z "$_domain_id" ]; then |
| 55 | + _err "No saved record for $fulldomain; cannot remove" |
| 56 | + return 1 |
| 57 | + fi |
| 58 | + |
| 59 | + _info "Removing TXT record id=$_record_id from domain id=$_domain_id" |
| 60 | + _calrissia_request DELETE "/domain/$_domain_id/record/$_record_id" |
| 61 | + |
| 62 | + _cleardomainconf "CALRISSIA_RECORD_ID_$_key" |
| 63 | + _cleardomainconf "CALRISSIA_DOMAIN_ID_$_key" |
| 64 | + return 0 |
| 65 | +} |
| 66 | + |
| 67 | +#################### |
| 68 | +# Private helpers # |
| 69 | +#################### |
| 70 | + |
| 71 | +# Build a unique conf key from fulldomain and txtvalue. |
| 72 | +_calrissia_key() { |
| 73 | + printf "%s_%s" "$1" "$2" | tr '.-' '__' |
| 74 | +} |
| 75 | + |
| 76 | +_calrissia_load_token() { |
| 77 | + CALRISSIA_TOKEN="${CALRISSIA_TOKEN:-$(_readaccountconf_mutable CALRISSIA_TOKEN)}" |
| 78 | + if [ -z "$CALRISSIA_TOKEN" ]; then |
| 79 | + _err "CALRISSIA_TOKEN is not set. Generate one at https://identity.calrissia.com under API Keys." |
| 80 | + return 1 |
| 81 | + fi |
| 82 | + _saveaccountconf_mutable CALRISSIA_TOKEN "$CALRISSIA_TOKEN" |
| 83 | +} |
| 84 | + |
| 85 | +# Sets _domain, _domain_id, _sub_domain for a given FQDN. |
| 86 | +_calrissia_get_root() { |
| 87 | + _fqdn="$1" |
| 88 | + |
| 89 | + i=1 |
| 90 | + while true; do |
| 91 | + _candidate="$(printf "%s" "$_fqdn" | cut -d . -f "$i"-)" |
| 92 | + [ -z "$_candidate" ] && return 1 |
| 93 | + |
| 94 | + _debug "Trying root domain: $_candidate" |
| 95 | + _response="$(_calrissia_request GET "/domain?full_domain_name=$_candidate")" |
| 96 | + _debug2 "Response: $_response" |
| 97 | + |
| 98 | + _domain_id="$(printf "%s" "$_response" | |
| 99 | + _egrep_o '"id":[[:space:]]*[0-9]+' | |
| 100 | + _head_n 1 | |
| 101 | + _egrep_o '[0-9]+')" |
| 102 | + |
| 103 | + if [ -n "$_domain_id" ]; then |
| 104 | + _sub_domain="$(printf "%s" "$_fqdn" | cut -d . -f "1-$((i - 1))")" |
| 105 | + _domain="$_candidate" |
| 106 | + return 0 |
| 107 | + fi |
| 108 | + |
| 109 | + i=$((i + 1)) |
| 110 | + done |
| 111 | +} |
| 112 | + |
| 113 | +_calrissia_request() { |
| 114 | + _method="$1" |
| 115 | + _path="$2" |
| 116 | + _body="$3" |
| 117 | + export _H1="Authorization: Bearer $CALRISSIA_TOKEN" |
| 118 | + export _H2="Accept: application/json" |
| 119 | + if [ "$_method" = "GET" ]; then |
| 120 | + _get "$CALRISSIA_API$_path" |
| 121 | + else |
| 122 | + _post "$_body" "$CALRISSIA_API$_path" "" "$_method" "application/json" |
| 123 | + fi |
| 124 | +} |
0 commit comments