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.

210 lines
5.5 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. MIJNHOST_API_KEY API Key
  9. '
  10. ######## Public functions ###################### Constants for your mijn-host API
  11. MIJNHOST_API="https://mijn.host/api/v2"
  12. # Add TXT record for domain verification
  13. dns_mijnhost_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. MIJNHOST_API_KEY="${MIJNHOST_API_KEY:-$(_readaccountconf_mutable MIJNHOST_API_KEY)}"
  17. if [ -z "$MIJNHOST_API_KEY" ]; then
  18. MIJNHOST_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 MIJNHOST_API_KEY "$MIJNHOST_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. _debug2 _sub_domain "$_sub_domain"
  31. _debug2 _domain "$_domain"
  32. _debug "Adding TXT record"
  33. # Construct the API URL
  34. api_url="$MIJNHOST_API/domains/$_domain/dns"
  35. # Getting previous records
  36. _mijnhost_rest GET "$api_url" ""
  37. if [ "$_code" != "200" ]; then
  38. _err "Error getting current DNS enties ($_code)"
  39. return 1
  40. fi
  41. records=$(echo "$response" | _egrep_o '"records":\[.*\]' | sed 's/"records"://')
  42. _debug2 "Current records" "$records"
  43. # Build the payload for the API
  44. data="{\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"value\":\"$txtvalue\",\"ttl\":300}"
  45. _debug2 "Record to add: " "$data"
  46. # Updating the records
  47. updated_records=$(echo "$records" | sed -E "s/\]( *$)/,$data\]/")
  48. _debug2 "Updated records" "$updated_records"
  49. # data
  50. data="{\"records\": $updated_records}"
  51. _mijnhost_rest PUT "$api_url" "$data"
  52. if [ "$_code" = "200" ]; then
  53. _info "DNS record succesfully added"
  54. return 0
  55. else
  56. _err "Error adding DNS record ($_code)"
  57. return 1
  58. fi
  59. }
  60. # Remove TXT record after verification
  61. dns_mijnhost_rm() {
  62. fulldomain=$1
  63. txtvalue=$2
  64. MIJNHOST_API_KEY="${MIJNHOST_API_KEY:-$(_readaccountconf_mutable MIJNHOST_API_KEY)}"
  65. if [ -z "$MIJNHOST_API_KEY" ]; then
  66. MIJNHOST_API_KEY=""
  67. _err "You haven't specified mijn-host API key yet."
  68. return 1
  69. fi
  70. _debug "First detect the root zone"
  71. if ! _get_root "$fulldomain"; then
  72. _err "Invalid domain"
  73. return 1
  74. fi
  75. _debug "Removing TXT record" "$txtvalue" "for" "$fulldomain"
  76. # Construct the API URL
  77. api_url="$MIJNHOST_API/domains/$_domain/dns"
  78. # Get current records
  79. _mijnhost_rest GET "$api_url" ""
  80. if [ "$_code" != "200" ]; then
  81. _err "Error getting current DNS enties ($_code)"
  82. return 1
  83. fi
  84. _debug2 "Get current records response:" "$response"
  85. records=$(echo "$response" | _egrep_o '"records":\[.*\]' | sed 's/"records"://')
  86. _debug2 "Current records:" "$records"
  87. updated_records=$(echo "$records" | sed -E "s/\{[^}]*\"value\":\"$txtvalue\"[^}]*\},?//g" | sed 's/,]/]/g')
  88. _debug2 "Updated records:" "$updated_records"
  89. # Build the new payload
  90. data="{\"records\": $updated_records}"
  91. # Use the _put method to update the records
  92. _mijnhost_rest PUT "$api_url" "$data"
  93. if [ "$_code" = "200" ]; then
  94. _info "DNS record removed successfully"
  95. return 0
  96. else
  97. _err "Error removing DNS record ($_code)"
  98. return 1
  99. fi
  100. }
  101. # Helper function to detect the root zone
  102. _get_root() {
  103. domain=$1
  104. # Get current records
  105. _debug "Getting current domains"
  106. _mijnhost_rest GET "$MIJNHOST_API/domains" ""
  107. if [ "$_code" != "200" ]; then
  108. _err "error getting current domains ($_code)"
  109. return 1
  110. fi
  111. # Extract root domains from response
  112. rootDomains=$(echo "$response" | _egrep_o '"domain":"[^"]*"' | sed -E 's/"domain":"([^"]*)"/\1/')
  113. _debug "Root domains:" "$rootDomains"
  114. for rootDomain in $rootDomains; do
  115. if _contains "$domain" "$rootDomain"; then
  116. _domain="$rootDomain"
  117. _sub_domain=$(echo "$domain" | sed "s/.$rootDomain//g")
  118. _debug "Found root domain" "$_domain" "and subdomain" "$_sub_domain" "for" "$domain"
  119. return 0
  120. fi
  121. done
  122. return 1
  123. }
  124. # Helper function for rest calls
  125. _mijnhost_rest() {
  126. m=$1
  127. ep="$2"
  128. data="$3"
  129. MAX_REQUEST_RETRY_TIMES=5
  130. _request_retry_times=0
  131. while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
  132. _debug3 _request_retry_times "$_request_retry_times"
  133. export _H1="API-Key: $MIJNHOST_API_KEY"
  134. export _H2="Content-Type: application/json"
  135. # clear headers from previous request to avoid getting wrong http code on timeouts
  136. : >"$HTTP_HEADER"
  137. _debug "$ep"
  138. if [ "$m" != "GET" ]; then
  139. _debug2 "data $data"
  140. response="$(_post "$data" "$ep" "" "$m")"
  141. else
  142. response="$(_get "$ep")"
  143. fi
  144. _ret="$?"
  145. _debug2 "response $response"
  146. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  147. _debug "http response code $_code"
  148. if [ "$_code" = "401" ]; then
  149. # we have an invalid API token, maybe it is expired?
  150. _err "Access denied. Invalid API token."
  151. return 1
  152. fi
  153. if [ "$_ret" != "0" ] || [ -z "$_code" ]; then
  154. _request_retry_times="$(_math "$_request_retry_times" + 1)"
  155. _info "REST call error $_code retrying $ep in $_request_retry_times s"
  156. # Sleep 10 times the number of retries in seconds, to increase backoff time
  157. _sleep "$(_math "$_request_retry_times" \* 10)"
  158. continue
  159. fi
  160. break
  161. done
  162. if [ "$_request_retry_times" = "$MAX_REQUEST_RETRY_TIMES" ]; then
  163. _err "Error mijn.host API call was retried $MAX_REQUEST_RETRY_TIMES times."
  164. _err "Calling $ep failed."
  165. return 1
  166. fi
  167. response="$(echo "$response" | _normalizeJson)"
  168. return 0
  169. }