dns_kinghost.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env sh
  2. ############################################################
  3. # KingHost API support #
  4. # http://api.kinghost.net/doc/ #
  5. # #
  6. # Author: Felipe Keller Braz <felipebraz@kinghost.com.br> #
  7. # Report Bugs here: https://github.com/kinghost/acme.sh #
  8. # #
  9. # Values to export: #
  10. # export KINGHOST_Username="email@provider.com" #
  11. # export KINGHOST_Password="xxxxxxxxxx" #
  12. ############################################################
  13. KING_Api="https://api.kinghost.net/acme"
  14. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  15. # Used to add txt record
  16. dns_kinghost_add() {
  17. fulldomain=$1
  18. txtvalue=$2
  19. KINGHOST_Username="${KINGHOST_Username:-$(_readaccountconf_mutable KINGHOST_Username)}"
  20. KINGHOST_Password="${KINGHOST_Password:-$(_readaccountconf_mutable KINGHOST_Password)}"
  21. if [ -z "$KINGHOST_Username" ] || [ -z "$KINGHOST_Password" ]; then
  22. KINGHOST_Username=""
  23. KINGHOST_Password=""
  24. _err "You don't specify KingHost api password and email yet."
  25. _err "Please create you key and try again."
  26. return 1
  27. fi
  28. #save the credentials to the account conf file.
  29. _saveaccountconf_mutable KINGHOST_Username "$KINGHOST_Username"
  30. _saveaccountconf_mutable KINGHOST_Password "$KINGHOST_Password"
  31. _debug "Getting txt records"
  32. _kinghost_rest GET "dns" "name=$fulldomain&content=$txtvalue"
  33. #This API call returns "status":"ok" if dns record does not exists
  34. #We are creating a new txt record here, so we expect the "ok" status
  35. if ! echo "$response" | grep '"status":"ok"' >/dev/null; then
  36. _err "Error"
  37. _err "$response"
  38. return 1
  39. fi
  40. _kinghost_rest POST "dns" "name=$fulldomain&content=$txtvalue"
  41. if ! echo "$response" | grep '"status":"ok"' >/dev/null; then
  42. _err "Error"
  43. _err "$response"
  44. return 1
  45. fi
  46. return 0
  47. }
  48. # Usage: fulldomain txtvalue
  49. # Used to remove the txt record after validation
  50. dns_kinghost_rm() {
  51. fulldomain=$1
  52. txtvalue=$2
  53. KINGHOST_Password="${KINGHOST_Password:-$(_readaccountconf_mutable KINGHOST_Password)}"
  54. KINGHOST_Username="${KINGHOST_Username:-$(_readaccountconf_mutable KINGHOST_Username)}"
  55. if [ -z "$KINGHOST_Password" ] || [ -z "$KINGHOST_Username" ]; then
  56. KINGHOST_Password=""
  57. KINGHOST_Username=""
  58. _err "You don't specify KingHost api key and email yet."
  59. _err "Please create you key and try again."
  60. return 1
  61. fi
  62. _kinghost_rest DELETE "dns" "name=$fulldomain&content=$txtvalue"
  63. if ! echo "$response" | grep '"status":"ok"' >/dev/null; then
  64. _err "Error"
  65. _err "$response"
  66. return 1
  67. fi
  68. return 0
  69. }
  70. #################### Private functions below ##################################
  71. _kinghost_rest() {
  72. method=$1
  73. uri="$2"
  74. data="$3"
  75. _debug "$uri"
  76. export _H1="X-Auth-Email: $KINGHOST_Username"
  77. export _H2="X-Auth-Key: $KINGHOST_Password"
  78. if [ "$method" != "GET" ]; then
  79. _debug data "$data"
  80. response="$(_post "$data" "$KING_Api/$uri.json" "" "$method")"
  81. else
  82. response="$(_get "$KING_Api/$uri.json?$data")"
  83. fi
  84. if [ "$?" != "0" ]; then
  85. _err "error $uri"
  86. return 1
  87. fi
  88. _debug2 response "$response"
  89. return 0
  90. }