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.

118 lines
3.8 KiB

  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. _debug "Getting txt records"
  63. _kinghost_rest GET "dns" "name=$fulldomain&content=$txtvalue"
  64. #This API call returns "status":"ok" if dns record does not exists
  65. #We are removing a txt record here, so the record must exists
  66. if echo "$response" | grep '"status":"ok"' >/dev/null; then
  67. _err "Error"
  68. _err "$response"
  69. return 1
  70. fi
  71. _kinghost_rest DELETE "dns" "name=$fulldomain&content=$txtvalue"
  72. if ! echo "$response" | grep '"status":"ok"' >/dev/null; then
  73. _err "Error"
  74. _err "$response"
  75. return 1
  76. fi
  77. return 0;
  78. }
  79. #################### Private functions below ##################################
  80. _kinghost_rest() {
  81. method=$1
  82. uri="$2"
  83. data="$3"
  84. _debug "$uri"
  85. export _H1="X-Auth-Email: $KINGHOST_Username"
  86. export _H2="X-Auth-Key: $KINGHOST_Password"
  87. if [ "$method" != "GET" ]; then
  88. _debug data "$data"
  89. response="$(_post "$data" "$KING_Api/$uri.json" "" "$method")"
  90. else
  91. response="$(_get "$KING_Api/$uri.json?$data")"
  92. fi
  93. if [ "$?" != "0" ]; then
  94. _err "error $uri"
  95. return 1
  96. fi
  97. _debug2 response "$response"
  98. return 0
  99. }