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.

158 lines
4.4 KiB

  1. #!/usr/bin/env sh
  2. # bug reports to stepan@plyask.in
  3. #
  4. # export VEESP_User="username"
  5. # export VEESP_Password="password"
  6. VEESP_Api="https://secure.veesp.com/api"
  7. ######## Public functions #####################
  8. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_veesp_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. VEESP_Password="${VEESP_Password:-$(_readaccountconf_mutable VEESP_Password)}"
  13. VEESP_User="${VEESP_User:-$(_readaccountconf_mutable VEESP_User)}"
  14. VEESP_auth=$(printf "%s" "$VEESP_User:$VEESP_Password" | _base64)
  15. if [ -z "$VEESP_Password" ] || [ -z "$VEESP_User" ]; then
  16. VEESP_Password=""
  17. VEESP_User=""
  18. _err "You don't specify veesp api key and email 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_mutable VEESP_Password "$VEESP_Password"
  24. _saveaccountconf_mutable VEESP_User "$VEESP_User"
  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 VEESP_rest POST "service/$_service_id/dns/$_domain_id/records" "{\"name\":\"$fulldomain\",\"ttl\":1,\"priority\":0,\"type\":\"TXT\",\"content\":\"$txtvalue\"}"; then
  35. if _contains "$response" "\"success\":true"; then
  36. _info "Added"
  37. #todo: check if the record takes effect
  38. return 0
  39. else
  40. _err "Add txt record error."
  41. return 1
  42. fi
  43. fi
  44. }
  45. # Usage: fulldomain txtvalue
  46. # Used to remove the txt record after validation
  47. dns_veesp_rm() {
  48. fulldomain=$1
  49. txtvalue=$2
  50. VEESP_Password="${VEESP_Password:-$(_readaccountconf_mutable VEESP_Password)}"
  51. VEESP_User="${VEESP_User:-$(_readaccountconf_mutable VEESP_User)}"
  52. VEESP_auth=$(printf "%s" "$VEESP_User:$VEESP_Password" | _base64)
  53. _debug "First detect the root zone"
  54. if ! _get_root "$fulldomain"; then
  55. _err "invalid domain"
  56. return 1
  57. fi
  58. _debug _domain_id "$_domain_id"
  59. _debug _sub_domain "$_sub_domain"
  60. _debug _domain "$_domain"
  61. _debug "Getting txt records"
  62. VEESP_rest GET "service/$_service_id/dns/$_domain_id"
  63. count=$(printf "%s\n" "$response" | _egrep_o "\"type\":\"TXT\",\"content\":\".\"$txtvalue.\"\"" | wc -l | tr -d " ")
  64. _debug count "$count"
  65. if [ "$count" = "0" ]; then
  66. _info "Don't need to remove."
  67. else
  68. record_id=$(printf "%s\n" "$response" | _egrep_o "{\"id\":[^}]*\"type\":\"TXT\",\"content\":\".\"$txtvalue.\"\"" | cut -d\" -f4)
  69. _debug "record_id" "$record_id"
  70. if [ -z "$record_id" ]; then
  71. _err "Can not get record id to remove."
  72. return 1
  73. fi
  74. if ! VEESP_rest DELETE "service/$_service_id/dns/$_domain_id/records/$record_id"; then
  75. _err "Delete record error."
  76. return 1
  77. fi
  78. _contains "$response" "\"success\":true"
  79. fi
  80. }
  81. #################### Private functions below ##################################
  82. #_acme-challenge.www.domain.com
  83. #returns
  84. # _sub_domain=_acme-challenge.www
  85. # _domain=domain.com
  86. # _domain_id=sdjkglgdfewsdfg
  87. _get_root() {
  88. domain=$1
  89. i=2
  90. p=1
  91. if ! VEESP_rest GET "dns"; then
  92. return 1
  93. fi
  94. while true; do
  95. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  96. _debug h "$h"
  97. if [ -z "$h" ]; then
  98. #not valid
  99. return 1
  100. fi
  101. if _contains "$response" "\"name\":\"$h\""; then
  102. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"domain_id\":[^,]*,\"name\":\"$h\"" | cut -d : -f 2 | cut -d , -f 1 | cut -d '"' -f 2)
  103. _debug _domain_id "$_domain_id"
  104. _service_id=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$h\",\"service_id\":[^}]*" | cut -d : -f 3 | cut -d '"' -f 2)
  105. _debug _service_id "$_service_id"
  106. if [ "$_domain_id" ]; then
  107. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  108. _domain="$h"
  109. return 0
  110. fi
  111. return 1
  112. fi
  113. p=$i
  114. i=$(_math "$i" + 1)
  115. done
  116. return 1
  117. }
  118. VEESP_rest() {
  119. m=$1
  120. ep="$2"
  121. data="$3"
  122. _debug "$ep"
  123. export _H1="Accept: application/json"
  124. export _H2="Authorization: Basic $VEESP_auth"
  125. if [ "$m" != "GET" ]; then
  126. _debug data "$data"
  127. export _H3="Content-Type: application/json"
  128. response="$(_post "$data" "$VEESP_Api/$ep" "" "$m")"
  129. else
  130. response="$(_get "$VEESP_Api/$ep")"
  131. fi
  132. if [ "$?" != "0" ]; then
  133. _err "error $ep"
  134. return 1
  135. fi
  136. _debug2 response "$response"
  137. return 0
  138. }