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.

110 lines
3.3 KiB

  1. #!/usr/bin/env sh
  2. #KINGHOST_username="xxxx@sss.com"
  3. #KINGHOST_Password="sdfsdfsdfljlbjkljlkjsdfoiwje"
  4. KING_Api="https://api.kinghost.net/acme"
  5. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  6. # Used to add txt record
  7. dns_kinghost_add() {
  8. fulldomain=$1
  9. txtvalue=$2
  10. KINGHOST_username="${KINGHOST_username:-$(_readaccountconf_mutable KINGHOST_username)}"
  11. KINGHOST_Password="${KINGHOST_Password:-$(_readaccountconf_mutable KINGHOST_Password)}"
  12. if [ -z "$KINGHOST_username" ] || [ -z "$KINGHOST_Password" ]; then
  13. KINGHOST_username=""
  14. KINGHOST_Password=""
  15. _err "You don't specify KingHost api password and email yet."
  16. _err "Please create you key and try again."
  17. return 1
  18. fi
  19. #save the credentials to the account conf file.
  20. _saveaccountconf_mutable KINGHOST_username "$KINGHOST_username"
  21. _saveaccountconf_mutable KINGHOST_Password "$KINGHOST_Password"
  22. _debug "Getting txt records"
  23. _kinghost_rest GET "dns" "name=$fulldomain&content=$txtvalue"
  24. #This API call returns "status":"ok" if dns record does not exists
  25. #We are creating a new txt record here, so we expect the "ok" status
  26. if ! printf "%s" "$response" | grep '"status":"ok"' >/dev/null; then
  27. _err "Error"
  28. _err "$response"
  29. return 1
  30. fi
  31. _kinghost_rest POST "dns" "name=$fulldomain&content=$txtvalue"
  32. if ! printf "%s" "$response" | grep '"status":"ok"' >/dev/null; then
  33. _err "Error"
  34. _err "$response"
  35. return 1
  36. fi
  37. return 0;
  38. }
  39. # Usage: fulldomain txtvalue
  40. # Used to remove the txt record after validation
  41. dns_kinghost_rm() {
  42. fulldomain=$1
  43. txtvalue=$2
  44. KINGHOST_Password="${KINGHOST_Password:-$(_readaccountconf_mutable KINGHOST_Password)}"
  45. KINGHOST_username="${KINGHOST_username:-$(_readaccountconf_mutable KINGHOST_username)}"
  46. if [ -z "$KINGHOST_Password" ] || [ -z "$KINGHOST_username" ]; then
  47. KINGHOST_Password=""
  48. KINGHOST_username=""
  49. _err "You don't specify KingHost api key and email yet."
  50. _err "Please create you key and try again."
  51. return 1
  52. fi
  53. _debug "Getting txt records"
  54. _kinghost_rest GET "dns" "name=$fulldomain&content=$txtvalue"
  55. #This API call returns "status":"ok" if dns record does not exists
  56. #We are removing a txt record here, so the record must exists
  57. if printf "%s" "$response" | grep '"status":"ok"' >/dev/null; then
  58. _err "Error"
  59. _err "$response"
  60. return 1
  61. fi
  62. _kinghost_rest DELETE "dns" "name=$fulldomain&content=$txtvalue"
  63. if ! printf "%s" "$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. }