From 48386b54992fe0039e2159925c487e967d916034 Mon Sep 17 00:00:00 2001 From: peterv99 <145142820+peterv99@users.noreply.github.com> Date: Wed, 18 Dec 2024 19:57:05 +0100 Subject: [PATCH 1/3] Add support for mijn.host DNS challenge Shamefully stolen from stalled pull request by shubhamku044. Pull request: https://github.com/acmesh-official/acme.sh/pull/5287 --- dnsapi/dns_mijn_host.sh | 146 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 dnsapi/dns_mijn_host.sh diff --git a/dnsapi/dns_mijn_host.sh b/dnsapi/dns_mijn_host.sh new file mode 100644 index 00000000..e0e2f89c --- /dev/null +++ b/dnsapi/dns_mijn_host.sh @@ -0,0 +1,146 @@ +#!/usr/bin/env sh +# shellcheck disable=SC2034 +dns_mijnhost_info='mijn.host +Domains: mijn.host +Site: mijn.host +Docs: https://mijn.host/api/doc/api-3563900 +Options: + MIJN_HOST_API_KEY API Key + MIJN_HOST_ENDPOINT_API API Endpoint URL. E.g. "https://mijn.host/api/v2" +' + +######## Public functions ###################### Constants for your mijn-host API +MIJN_HOST_API="https://mijn.host/api/v2" + +# Add TXT record for domain verification +dns_mijn_host_add() { + fulldomain=$1 + txtvalue=$2 + + MIJN_HOST_API_KEY="${MIJN_HOST_API_KEY:-$(_readaccountconf_mutable MIJN_HOST_API_KEY)}" + if [ -z "$MIJN_HOST_API_KEY" ]; then + MIJN_HOST_API_KEY="" + _err "You haven't specified mijn-host API key yet." + _err "Please set it and try again." + return 1 + fi + + # Save the API key for future use + _saveaccountconf_mutable MIJN_HOST_API_KEY "$MIJN_HOST_API_KEY" + + _debug "First detect the root zone" + if ! _get_root "$fulldomain"; then + _err "Invalid domain" + return 1 + fi + + _debug "Add TXT record" + + # Build the payload for the API + data="{\"type\":\"TXT\",\"name\":\"$subdomain\",\"value\":\"$txtvalue\",\"ttl\":120}" + + export _H1="API-Key: $MIJN_HOST_API_KEY" + export _H2="Content-Type: application/json" + + extracted_domain="${fulldomain#*_acme-challenge.}" + + # Construct the API URL + api_url="$MIJN_HOST_API/domains/$extracted_domain/dns" + + # Getting preivous records + get_response="$(_get "$api_url")" + records=$(echo "$get_response" | jq -r '.data.records') + + # Updating the records + updated_records=$(echo "$records" | jq --argjson data "$data" '. += [$data]') + + # data + data="{\"records\": $updated_records}" + + # Use the _post method to make the API request + response="$(_post "$data" "$api_url" "" "PUT")" + + if _contains "$response" "error"; then + _err "Error adding TXT record: $response" + return 1 + fi + + _info "TXT record added successfully" + return 0 +} + +# Remove TXT record after verification +dns_mijn_host_rm() { + fulldomain=$1 + txtvalue=$2 + + MIJN_HOST_API_KEY="${MIJN_HOST_API_KEY:-$(_readaccountconf_mutable MIJN_HOST_API_KEY)}" + if [ -z "$MIJN_HOST_API_KEY" ]; then + MIJN_HOST_API_KEY="" + _err "You haven't specified mijn-host API key yet." + return 1 + fi + + _debug "First detect the root zone" + if ! _get_root "$fulldomain"; then + _err "Invalid domain" + return 1 + fi + + _debug "Removing TXT record" + + # Build the payload for the API + export _H1="API-Key: $MIJN_HOST_API_KEY" + export _H2="Content-Type: application/json" + + extracted_domain="${fulldomain#*_acme-challenge.}" + + # Construct the API URL + api_url="$MIJN_HOST_API/domains/$extracted_domain/dns" + + # Get current records + response="$(_get "$api_url")" + + updated_records=$(echo "$response" | jq '.data.records') + + updated_records=$(echo "$updated_records" | jq --arg value "$txtvalue" 'map(select(.value != $value))') + + # Build the new payload + data="{\"records\": $updated_records}" + + # Use the _put method to update the records + response="$(_post "$data" "$api_url" "" "PUT")" + + if _contains "$response" "error"; then + _err "Error updating TXT record: $response" + return 1 + fi + + _info "TXT record removed successfully" + return 0 +} + +# Helper function to detect the root zone +_get_root() { + domain=$1 + i=2 + p=1 + + while true; do + h=$(printf "%s" "$domain" | cut -d . -f $i-) + if [ -z "$h" ]; then + return 1 + fi + + if _contains "$(dig ns "$h")" "mijn.host"; then + root_zone="$h" + subdomain=$(printf "%s" "$domain" | cut -d . -f 1-$p) + return 0 + fi + + p=$i + i=$(_math "$i" + 1) + done + + return 1 +} From 9b2873668f5743dd24ce558683474146b7b76d70 Mon Sep 17 00:00:00 2001 From: Peter Vos Date: Tue, 24 Dec 2024 10:13:42 +0100 Subject: [PATCH 2/3] Removed unused variable MIJN_HOST_ENDPOINT_API --- dnsapi/dns_mijn_host.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/dnsapi/dns_mijn_host.sh b/dnsapi/dns_mijn_host.sh index e0e2f89c..54dc579a 100644 --- a/dnsapi/dns_mijn_host.sh +++ b/dnsapi/dns_mijn_host.sh @@ -6,7 +6,6 @@ Site: mijn.host Docs: https://mijn.host/api/doc/api-3563900 Options: MIJN_HOST_API_KEY API Key - MIJN_HOST_ENDPOINT_API API Endpoint URL. E.g. "https://mijn.host/api/v2" ' ######## Public functions ###################### Constants for your mijn-host API From 3aa8365e5168458a5aa1c2572efb19acc528e2d4 Mon Sep 17 00:00:00 2001 From: Peter Vos Date: Tue, 24 Dec 2024 10:33:05 +0100 Subject: [PATCH 3/3] Fixed shfmt errors --- dnsapi/dns_mijn_host.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/dnsapi/dns_mijn_host.sh b/dnsapi/dns_mijn_host.sh index 54dc579a..5caf5a22 100644 --- a/dnsapi/dns_mijn_host.sh +++ b/dnsapi/dns_mijn_host.sh @@ -26,7 +26,7 @@ dns_mijn_host_add() { # Save the API key for future use _saveaccountconf_mutable MIJN_HOST_API_KEY "$MIJN_HOST_API_KEY" - + _debug "First detect the root zone" if ! _get_root "$fulldomain"; then _err "Invalid domain" @@ -34,7 +34,7 @@ dns_mijn_host_add() { fi _debug "Add TXT record" - + # Build the payload for the API data="{\"type\":\"TXT\",\"name\":\"$subdomain\",\"value\":\"$txtvalue\",\"ttl\":120}" @@ -52,7 +52,7 @@ dns_mijn_host_add() { # Updating the records updated_records=$(echo "$records" | jq --argjson data "$data" '. += [$data]') - + # data data="{\"records\": $updated_records}" @@ -87,19 +87,19 @@ dns_mijn_host_rm() { fi _debug "Removing TXT record" - + # Build the payload for the API export _H1="API-Key: $MIJN_HOST_API_KEY" export _H2="Content-Type: application/json" extracted_domain="${fulldomain#*_acme-challenge.}" - + # Construct the API URL api_url="$MIJN_HOST_API/domains/$extracted_domain/dns" - + # Get current records response="$(_get "$api_url")" - + updated_records=$(echo "$response" | jq '.data.records') updated_records=$(echo "$updated_records" | jq --arg value "$txtvalue" 'map(select(.value != $value))') @@ -109,7 +109,7 @@ dns_mijn_host_rm() { # Use the _put method to update the records response="$(_post "$data" "$api_url" "" "PUT")" - + if _contains "$response" "error"; then _err "Error updating TXT record: $response" return 1 @@ -126,14 +126,14 @@ _get_root() { p=1 while true; do - h=$(printf "%s" "$domain" | cut -d . -f $i-) + h=$(printf "%s" "$domain" | cut -d . -f "$i"-) if [ -z "$h" ]; then return 1 fi if _contains "$(dig ns "$h")" "mijn.host"; then root_zone="$h" - subdomain=$(printf "%s" "$domain" | cut -d . -f 1-$p) + subdomain=$(printf "%s" "$domain" | cut -d . -f 1-"$p") return 0 fi