You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.2 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_mijnhost_info='mijn.host
  4. Domains: mijn.host
  5. Site: mijn.host
  6. Docs: https://mijn.host/api/doc/
  7. Options:
  8. MIJN_HOST_API_KEY API Key
  9. '
  10. ######## Public functions ###################### Constants for your mijn-host API
  11. MIJN_HOST_API="https://mijn.host/api/v2"
  12. # Add TXT record for domain verification
  13. dns_mijn_host_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. MIJN_HOST_API_KEY="${MIJN_HOST_API_KEY:-$(_readaccountconf_mutable MIJN_HOST_API_KEY)}"
  17. if [ -z "$MIJN_HOST_API_KEY" ]; then
  18. MIJN_HOST_API_KEY=""
  19. _err "You haven't specified mijn-host API key yet."
  20. _err "Please set it and try again."
  21. return 1
  22. fi
  23. # Save the API key for future use
  24. _saveaccountconf_mutable MIJN_HOST_API_KEY "$MIJN_HOST_API_KEY"
  25. _debug "First detect the root zone"
  26. if ! _get_root "$fulldomain"; then
  27. _err "Invalid domain"
  28. return 1
  29. fi
  30. _debug _sub_domain "$_sub_domain"
  31. _debug _domain "$_domain"
  32. _debug "Add TXT record"
  33. # Build the payload for the API
  34. data="{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"value\":\"$txtvalue\",\"ttl\":120}"
  35. export _H1="API-Key: $MIJN_HOST_API_KEY"
  36. export _H2="Content-Type: application/json"
  37. # Construct the API URL
  38. api_url="$MIJN_HOST_API/domains/$_domain/dns"
  39. # Getting previous records
  40. get_response="$(_get "$api_url")"
  41. records=$(echo "$get_response" | _egrep_o '"records":\[.*\]' | sed 's/"records"://')
  42. _debug "Current records" "$records"
  43. # Updating the records
  44. updated_records=$(echo "$records" | sed -E "s/\]( *$)/,$data\]/")
  45. _debug "Updated records" "$updatedrecords"
  46. # data
  47. data="{\"records\": $updated_records}"
  48. _debug "json data add_dns PUT call:" "$data"
  49. # Use the _post method to make the API request
  50. response="$(_post "$data" "$api_url" "" "PUT")"
  51. _debug "Response to PUT dns_add" "$response"
  52. if ! _contains "$response" "200"; then
  53. _err "Error adding TXT record: $response"
  54. return 1
  55. fi
  56. _info "TXT record added successfully"
  57. return 0
  58. }
  59. # Remove TXT record after verification
  60. dns_mijn_host_rm() {
  61. fulldomain=$1
  62. txtvalue=$2
  63. MIJN_HOST_API_KEY="${MIJN_HOST_API_KEY:-$(_readaccountconf_mutable MIJN_HOST_API_KEY)}"
  64. if [ -z "$MIJN_HOST_API_KEY" ]; then
  65. MIJN_HOST_API_KEY=""
  66. _err "You haven't specified mijn-host API key yet."
  67. return 1
  68. fi
  69. _debug "First detect the root zone"
  70. if ! _get_root "$fulldomain"; then
  71. _err "Invalid domain"
  72. return 1
  73. fi
  74. _debug "Removing TXT record"
  75. # Build the payload for the API
  76. export _H1="API-Key: $MIJN_HOST_API_KEY"
  77. export _H2="Content-Type: application/json"
  78. # Construct the API URL
  79. api_url="$MIJN_HOST_API/domains/$_domain/dns"
  80. # Get current records
  81. response="$(_get "$api_url")"
  82. _debug "Get current records response:" "$response"
  83. records=$(echo "$get_response" | _egrep_o '"records":\[.*\]' | sed 's/"records"://')
  84. _debug "Current records:" "$records"
  85. updated_records=$(echo "$updated_records" | sed -E "s/\{[^}]*\"value\":\"$txtvalue\"[^}]*\},?//g" | sed 's/,]/]/g')
  86. _debug "Updated records:" "$updated_records"
  87. # Build the new payload
  88. data="{\"records\": $updated_records}"
  89. _debug "Payload:" "$data"
  90. # Use the _put method to update the records
  91. response="$(_post "$data" "$api_url" "" "PUT")"
  92. _debug "Response:" "$response"
  93. if ! _contains "$response" "200"; then
  94. _err "Error updating TXT record: $response"
  95. return 1
  96. fi
  97. _info "TXT record removed successfully"
  98. return 0
  99. }
  100. # Helper function to detect the root zone
  101. _get_root() {
  102. domain=$1
  103. # Get all domains
  104. export _H1="API-Key: $MIJN_HOST_API_KEY"
  105. export _H2="Content-Type: application/json"
  106. # Construct the API URL
  107. api_url="$MIJN_HOST_API/domains"
  108. # Get current records
  109. response="$(_get "$api_url")"
  110. if ! _contains "$response" "200"; then
  111. _err "Error listing domains: $response"
  112. return 1
  113. fi
  114. # Extract root domains from response
  115. rootDomains=$(echo "$response" | _egrep_o '"domain":"[^"]*"' | sed -E 's/"domain":"([^"]*)"/\1/')
  116. _debug "Root domains:" "$rootDomains"
  117. for rootDomain in $rootDomains; do
  118. if _contains "$domain" "$rootDomain"; then
  119. _domain="$rootDomain"
  120. _sub_domain=$(echo "$domain" | sed "s/.$rootDomain//g")
  121. return 0
  122. fi
  123. done
  124. return 1
  125. }