dns_pointhq.sh 4.1 KB

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