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.

213 lines
5.7 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 your mijn-host API key yet."
  20. _err "Please add MIJNHOST_API_KEY to the env."
  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 DNS record" "${fulldomain}."
  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 your mijn-host API key yet."
  68. _err "Please add MIJNHOST_API_KEY to the env."
  69. return 1
  70. fi
  71. _debug "Detecting root zone for" "${fulldomain}."
  72. if ! _get_root "$fulldomain"; then
  73. _err "Invalid domain"
  74. return 1
  75. fi
  76. _debug "Removing DNS record for TXT value" "${txtvalue}."
  77. # Construct the API URL
  78. api_url="$MIJNHOST_API/domains/$_domain/dns"
  79. # Get current records
  80. _mijnhost_rest GET "$api_url" ""
  81. if [ "$_code" != "200" ]; then
  82. _err "Error getting current DNS enties ($_code)"
  83. return 1
  84. fi
  85. _debug2 "Get current records response:" "$response"
  86. records=$(echo "$response" | _egrep_o '"records":\[.*\]' | sed 's/"records"://')
  87. _debug2 "Current records:" "$records"
  88. updated_records=$(echo "$records" | sed -E "s/\{[^}]*\"value\":\"$txtvalue\"[^}]*\},?//g" | sed 's/,]/]/g')
  89. _debug2 "Updated records:" "$updated_records"
  90. # Build the new payload
  91. data="{\"records\": $updated_records}"
  92. # Use the _put method to update the records
  93. _mijnhost_rest PUT "$api_url" "$data"
  94. if [ "$_code" = "200" ]; then
  95. _info "DNS record removed successfully."
  96. return 0
  97. else
  98. _err "Error removing DNS record ($_code)."
  99. return 1
  100. fi
  101. }
  102. # Helper function to detect the root zone
  103. _get_root() {
  104. domain=$1
  105. # Get current records
  106. _debug "Getting current domains"
  107. _mijnhost_rest GET "$MIJNHOST_API/domains" ""
  108. if [ "$_code" != "200" ]; then
  109. _err "error getting current domains ($_code)"
  110. return 1
  111. fi
  112. # Extract root domains from response
  113. rootDomains=$(echo "$response" | _egrep_o '"domain":"[^"]*"' | sed -E 's/"domain":"([^"]*)"/\1/')
  114. _debug "Root domains:" "$rootDomains"
  115. for rootDomain in $rootDomains; do
  116. if _contains "$domain" "$rootDomain"; then
  117. _domain="$rootDomain"
  118. _sub_domain=$(echo "$domain" | sed "s/.$rootDomain//g")
  119. _debug "Found root domain" "$_domain" "and subdomain" "$_sub_domain" "for" "$domain"
  120. return 0
  121. fi
  122. done
  123. return 1
  124. }
  125. # Helper function for rest calls
  126. _mijnhost_rest() {
  127. m=$1
  128. ep="$2"
  129. data="$3"
  130. MAX_REQUEST_RETRY_TIMES=5
  131. _request_retry_times=0
  132. _retry_sleep=5 #Initial sleep time in seconds.
  133. while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do
  134. _debug2 _request_retry_times "$_request_retry_times"
  135. export _H1="API-Key: $MIJNHOST_API_KEY"
  136. export _H2="Content-Type: application/json"
  137. # clear headers from previous request to avoid getting wrong http code on timeouts
  138. : >"$HTTP_HEADER"
  139. _debug "$ep"
  140. if [ "$m" != "GET" ]; then
  141. _debug2 "data $data"
  142. response="$(_post "$data" "$ep" "" "$m")"
  143. else
  144. response="$(_get "$ep")"
  145. fi
  146. _ret="$?"
  147. _debug2 "response $response"
  148. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  149. _debug "http response code $_code"
  150. if [ "$_code" = "401" ]; then
  151. # we have an invalid API token, maybe it is expired?
  152. _err "Access denied. Invalid API token."
  153. return 1
  154. fi
  155. if [ "$_ret" != "0" ] || [ -z "$_code" ] || [ "$_code" = "400" ] || _contains "$response" "DNS records not managed by mijn.host"; then #Sometimes API errors out
  156. _request_retry_times="$(_math "$_request_retry_times" + 1)"
  157. _info "REST call error $_code retrying $ep in ${_retry_sleep}s"
  158. _sleep "$_retry_sleep"
  159. _retry_sleep="$(_math "$_retry_sleep" \* 2)"
  160. continue
  161. fi
  162. break
  163. done
  164. if [ "$_request_retry_times" = "$MAX_REQUEST_RETRY_TIMES" ]; then
  165. _err "Error mijn.host API call was retried $MAX_REQUEST_RETRY_TIMES times."
  166. _err "Calling $ep failed."
  167. return 1
  168. fi
  169. response="$(echo "$response" | _normalizeJson)"
  170. return 0
  171. }