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.

123 lines
2.4 KiB

  1. #!/usr/bin/env sh
  2. #Godaddy domain api
  3. #
  4. #GD_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
  5. #
  6. #GD_Secret="asdfsdfsfsdfsdfdfsdf"
  7. GD_Api="https://api.godaddy.com/v1"
  8. ######## Public functions #####################
  9. dns_gd_del(){
  10. _err "Not implemented!"
  11. return 1
  12. }
  13. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_gd_add(){
  15. fulldomain=$1
  16. txtvalue=$2
  17. if [ -z "$GD_Key" ] || [ -z "$GD_Secret" ] ; then
  18. _err "You don't specify godaddy api key and secret yet."
  19. _err "Please create you key and try again."
  20. return 1
  21. fi
  22. #save the api key and email to the account conf file.
  23. _saveaccountconf GD_Key "$GD_Key"
  24. _saveaccountconf GD_Secret "$GD_Secret"
  25. _debug "First detect the root zone"
  26. if ! _get_root $fulldomain ; then
  27. _err "invalid domain"
  28. return 1
  29. fi
  30. _debug _domain_id "$_domain_id"
  31. _debug _sub_domain "$_sub_domain"
  32. _debug _domain "$_domain"
  33. _info "Adding record"
  34. if _gd_rest PUT "domains/$_domain/records/TXT/$_sub_domain" "[{\"data\":\"$txtvalue\"}]"; then
  35. if [ "$response" = "{}" ] ; then
  36. _info "Added, sleeping 10 seconds"
  37. sleep 10
  38. #todo: check if the record takes effect
  39. return 0
  40. else
  41. _err "Add txt record error."
  42. _err "$response"
  43. return 1
  44. fi
  45. fi
  46. _err "Add txt record error."
  47. }
  48. #################### Private functions bellow ##################################
  49. #_acme-challenge.www.domain.com
  50. #returns
  51. # _sub_domain=_acme-challenge.www
  52. # _domain=domain.com
  53. # _domain_id=sdjkglgdfewsdfg
  54. _get_root() {
  55. domain=$1
  56. i=2
  57. p=1
  58. while [ '1' ] ; do
  59. h=$(printf $domain | cut -d . -f $i-100)
  60. if [ -z "$h" ] ; then
  61. #not valid
  62. return 1;
  63. fi
  64. if ! _gd_rest GET "domains/$h" ; then
  65. return 1
  66. fi
  67. if printf "$response" | grep '"code":"NOT_FOUND"' >/dev/null ; then
  68. _debug "$h not found"
  69. else
  70. _sub_domain=$(printf $domain | cut -d . -f 1-$p)
  71. _domain=$h
  72. return 0
  73. fi
  74. p=$i
  75. i=$(expr $i + 1)
  76. done
  77. return 1
  78. }
  79. _gd_rest() {
  80. m=$1
  81. ep="$2"
  82. data="$3"
  83. _debug $ep
  84. _H1="Authorization: sso-key $GD_Key:$GD_Secret"
  85. _H2="Content-Type: application/json"
  86. if [ "$data" ] ; then
  87. _debug data "$data"
  88. response="$(_post "$data" "$GD_Api/$ep" "" $m)"
  89. else
  90. response="$(_get "$GD_Api/$ep")"
  91. fi
  92. if [ "$?" != "0" ] ; then
  93. _err "error $ep"
  94. return 1
  95. fi
  96. _debug2 response "$response"
  97. return 0
  98. }