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.

168 lines
4.3 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_pointhq_info='pointhq.com PointDNS
  4. Site: pointhq.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_pointhq
  6. Options:
  7. PointHQ_Key API Key
  8. PointHQ_Email Email
  9. Issues: github.com/acmesh-official/acme.sh/issues/2060
  10. '
  11. PointHQ_Api="https://api.pointhq.com"
  12. ######## Public functions #####################
  13. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_pointhq_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. PointHQ_Key="${PointHQ_Key:-$(_readaccountconf_mutable PointHQ_Key)}"
  18. PointHQ_Email="${PointHQ_Email:-$(_readaccountconf_mutable PointHQ_Email)}"
  19. if [ -z "$PointHQ_Key" ] || [ -z "$PointHQ_Email" ]; then
  20. PointHQ_Key=""
  21. PointHQ_Email=""
  22. _err "You didn't specify a PointHQ API key and email yet."
  23. _err "Please create the key and try again."
  24. return 1
  25. fi
  26. if ! _contains "$PointHQ_Email" "@"; then
  27. _err "It seems that the PointHQ_Email=$PointHQ_Email is not a valid email address."
  28. _err "Please check and retry."
  29. return 1
  30. fi
  31. #save the api key and email to the account conf file.
  32. _saveaccountconf_mutable PointHQ_Key "$PointHQ_Key"
  33. _saveaccountconf_mutable PointHQ_Email "$PointHQ_Email"
  34. _debug "First detect the root zone"
  35. if ! _get_root "$fulldomain"; then
  36. _err "invalid domain"
  37. return 1
  38. fi
  39. _debug _sub_domain "$_sub_domain"
  40. _debug _domain "$_domain"
  41. _info "Adding record"
  42. if _pointhq_rest POST "zones/$_domain/records" "{\"zone_record\": {\"name\":\"$_sub_domain\",\"record_type\":\"TXT\",\"data\":\"$txtvalue\",\"ttl\":3600}}"; then
  43. if printf -- "%s" "$response" | grep "$fulldomain" >/dev/null; then
  44. _info "Added, OK"
  45. return 0
  46. else
  47. _err "Add txt record error."
  48. return 1
  49. fi
  50. fi
  51. _err "Add txt record error."
  52. return 1
  53. }
  54. #fulldomain txtvalue
  55. dns_pointhq_rm() {
  56. fulldomain=$1
  57. txtvalue=$2
  58. PointHQ_Key="${PointHQ_Key:-$(_readaccountconf_mutable PointHQ_Key)}"
  59. PointHQ_Email="${PointHQ_Email:-$(_readaccountconf_mutable PointHQ_Email)}"
  60. if [ -z "$PointHQ_Key" ] || [ -z "$PointHQ_Email" ]; then
  61. PointHQ_Key=""
  62. PointHQ_Email=""
  63. _err "You didn't specify a PointHQ API key and email yet."
  64. _err "Please create the key and try again."
  65. return 1
  66. fi
  67. _debug "First detect the root zone"
  68. if ! _get_root "$fulldomain"; then
  69. _err "invalid domain"
  70. return 1
  71. fi
  72. _debug _sub_domain "$_sub_domain"
  73. _debug _domain "$_domain"
  74. _debug "Getting txt records"
  75. _pointhq_rest GET "zones/${_domain}/records?record_type=TXT&name=$_sub_domain"
  76. if ! printf "%s" "$response" | grep "^\[" >/dev/null; then
  77. _err "Error"
  78. return 1
  79. fi
  80. if [ "$response" = "[]" ]; then
  81. _info "No records to remove."
  82. else
  83. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*" | cut -d : -f 2 | tr -d \" | head -n 1)
  84. _debug "record_id" "$record_id"
  85. if [ -z "$record_id" ]; then
  86. _err "Can not get record id to remove."
  87. return 1
  88. fi
  89. if ! _pointhq_rest DELETE "zones/$_domain/records/$record_id"; then
  90. _err "Delete record error."
  91. return 1
  92. fi
  93. _contains "$response" '"status":"OK"'
  94. fi
  95. }
  96. #################### Private functions below ##################################
  97. #_acme-challenge.www.domain.com
  98. #returns
  99. # _sub_domain=_acme-challenge.www
  100. # _domain=domain.com
  101. _get_root() {
  102. domain=$1
  103. i=2
  104. p=1
  105. while true; do
  106. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  107. _debug h "$h"
  108. if [ -z "$h" ]; then
  109. #not valid
  110. return 1
  111. fi
  112. if ! _pointhq_rest GET "zones"; then
  113. return 1
  114. fi
  115. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  116. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  117. _domain=$h
  118. return 0
  119. fi
  120. p=$i
  121. i=$(_math "$i" + 1)
  122. done
  123. return 1
  124. }
  125. _pointhq_rest() {
  126. m=$1
  127. ep="$2"
  128. data="$3"
  129. _debug "$ep"
  130. _pointhq_auth=$(printf "%s:%s" "$PointHQ_Email" "$PointHQ_Key" | _base64)
  131. export _H1="Authorization: Basic $_pointhq_auth"
  132. export _H2="Content-Type: application/json"
  133. export _H3="Accept: application/json"
  134. if [ "$m" != "GET" ]; then
  135. _debug data "$data"
  136. response="$(_post "$data" "$PointHQ_Api/$ep" "" "$m")"
  137. else
  138. response="$(_get "$PointHQ_Api/$ep")"
  139. fi
  140. if [ "$?" != "0" ]; then
  141. _err "error $ep"
  142. return 1
  143. fi
  144. _debug2 response "$response"
  145. return 0
  146. }