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.

102 lines
2.9 KiB

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