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.

272 lines
6.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_simply_info='Simply.com
  4. Site: Simply.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_simply
  6. Options:
  7. SIMPLY_AccountName Account name
  8. SIMPLY_ApiKey API Key
  9. '
  10. #SIMPLY_Api="https://api.simply.com/2/"
  11. SIMPLY_Api_Default="https://api.simply.com/2"
  12. #This is used for determining success of REST call
  13. SIMPLY_SUCCESS_CODE='"status":200'
  14. ######## Public functions #####################
  15. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  16. dns_simply_add() {
  17. fulldomain=$1
  18. txtvalue=$2
  19. if ! _simply_load_config; then
  20. return 1
  21. fi
  22. _simply_save_config
  23. _debug "First detect the root zone"
  24. if ! _get_root "$fulldomain"; then
  25. _err "invalid domain"
  26. return 1
  27. fi
  28. _debug _sub_domain "$_sub_domain"
  29. _debug _domain "$_domain"
  30. _info "Adding record"
  31. if ! _simply_add_record "$_domain" "$_sub_domain" "$txtvalue"; then
  32. _err "Could not add DNS record"
  33. return 1
  34. fi
  35. return 0
  36. }
  37. dns_simply_rm() {
  38. fulldomain=$1
  39. txtvalue=$2
  40. if ! _simply_load_config; then
  41. return 1
  42. fi
  43. _simply_save_config
  44. _debug "Find the DNS zone"
  45. if ! _get_root "$fulldomain"; then
  46. _err "invalid domain"
  47. return 1
  48. fi
  49. _debug _sub_domain "$_sub_domain"
  50. _debug _domain "$_domain"
  51. _debug txtvalue "$txtvalue"
  52. _info "Getting all existing records"
  53. if ! _simply_get_all_records "$_domain"; then
  54. _err "invalid domain"
  55. return 1
  56. fi
  57. records=$(echo "$response" | tr '{' "\n" | grep 'record_id\|type\|data\|\name' | sed 's/\"record_id/;\"record_id/' | tr "\n" ' ' | tr -d ' ' | tr ';' ' ')
  58. nr_of_deleted_records=0
  59. _info "Fetching txt record"
  60. for record in $records; do
  61. _debug record "$record"
  62. record_data=$(echo "$record" | sed -n "s/.*\"data\":\"\([^\"]*\)\".*/\1/p")
  63. record_type=$(echo "$record" | sed -n "s/.*\"type\":\"\([^\"]*\)\".*/\1/p")
  64. _debug2 record_data "$record_data"
  65. _debug2 record_type "$record_type"
  66. if [ "$record_data" = "$txtvalue" ] && [ "$record_type" = "TXT" ]; then
  67. record_id=$(echo "$record" | cut -d "," -f 1 | grep "record_id" | cut -d ":" -f 2)
  68. _info "Deleting record $record"
  69. _debug2 record_id "$record_id"
  70. if [ "$record_id" -gt 0 ]; then
  71. if ! _simply_delete_record "$_domain" "$_sub_domain" "$record_id"; then
  72. _err "Record with id $record_id could not be deleted"
  73. return 1
  74. fi
  75. nr_of_deleted_records=1
  76. break
  77. else
  78. _err "Fetching record_id could not be done, this should not happen, exiting function. Failing record is $record"
  79. break
  80. fi
  81. fi
  82. done
  83. if [ "$nr_of_deleted_records" -eq 0 ]; then
  84. _err "No record deleted, the DNS record needs to be removed manually."
  85. else
  86. _info "Deleted $nr_of_deleted_records record"
  87. fi
  88. return 0
  89. }
  90. #################### Private functions below ##################################
  91. _simply_load_config() {
  92. SIMPLY_Api="${SIMPLY_Api:-$(_readaccountconf_mutable SIMPLY_Api)}"
  93. SIMPLY_AccountName="${SIMPLY_AccountName:-$(_readaccountconf_mutable SIMPLY_AccountName)}"
  94. SIMPLY_ApiKey="${SIMPLY_ApiKey:-$(_readaccountconf_mutable SIMPLY_ApiKey)}"
  95. if [ -z "$SIMPLY_Api" ]; then
  96. SIMPLY_Api="$SIMPLY_Api_Default"
  97. fi
  98. if [ -z "$SIMPLY_AccountName" ] || [ -z "$SIMPLY_ApiKey" ]; then
  99. SIMPLY_AccountName=""
  100. SIMPLY_ApiKey=""
  101. _err "A valid Simply API account and apikey not provided."
  102. _err "Please provide a valid API user and try again."
  103. return 1
  104. fi
  105. return 0
  106. }
  107. _simply_save_config() {
  108. if [ "$SIMPLY_Api" != "$SIMPLY_Api_Default" ]; then
  109. _saveaccountconf_mutable SIMPLY_Api "$SIMPLY_Api"
  110. fi
  111. _saveaccountconf_mutable SIMPLY_AccountName "$SIMPLY_AccountName"
  112. _saveaccountconf_mutable SIMPLY_ApiKey "$SIMPLY_ApiKey"
  113. }
  114. _simply_get_all_records() {
  115. domain=$1
  116. if ! _simply_rest GET "my/products/$domain/dns/records/"; then
  117. return 1
  118. fi
  119. return 0
  120. }
  121. _get_root() {
  122. domain=$1
  123. i=2
  124. p=1
  125. while true; do
  126. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  127. if [ -z "$h" ]; then
  128. #not valid
  129. return 1
  130. fi
  131. if ! _simply_rest GET "my/products/$h/dns/"; then
  132. return 1
  133. fi
  134. if ! _contains "$response" "$SIMPLY_SUCCESS_CODE"; then
  135. _debug "$h not found"
  136. else
  137. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  138. _domain="$h"
  139. return 0
  140. fi
  141. p="$i"
  142. i=$(_math "$i" + 1)
  143. done
  144. return 1
  145. }
  146. _simply_add_record() {
  147. domain=$1
  148. sub_domain=$2
  149. txtval=$3
  150. data="{\"name\": \"$sub_domain\", \"type\":\"TXT\", \"data\": \"$txtval\", \"priority\":0, \"ttl\": 3600}"
  151. if ! _simply_rest POST "my/products/$domain/dns/records/" "$data"; then
  152. _err "Adding record not successfull!"
  153. return 1
  154. fi
  155. if ! _contains "$response" "$SIMPLY_SUCCESS_CODE"; then
  156. _err "Call to API not sucessfull, see below message for more details"
  157. _err "$response"
  158. return 1
  159. fi
  160. return 0
  161. }
  162. _simply_delete_record() {
  163. domain=$1
  164. sub_domain=$2
  165. record_id=$3
  166. _debug record_id "Delete record with id $record_id"
  167. if ! _simply_rest DELETE "my/products/$domain/dns/records/$record_id/"; then
  168. _err "Deleting record not successfull!"
  169. return 1
  170. fi
  171. if ! _contains "$response" "$SIMPLY_SUCCESS_CODE"; then
  172. _err "Call to API not sucessfull, see below message for more details"
  173. _err "$response"
  174. return 1
  175. fi
  176. return 0
  177. }
  178. _simply_rest() {
  179. m=$1
  180. ep="$2"
  181. data="$3"
  182. _debug2 data "$data"
  183. _debug2 ep "$ep"
  184. _debug2 m "$m"
  185. basicauth=$(printf "%s:%s" "$SIMPLY_AccountName" "$SIMPLY_ApiKey" | _base64)
  186. if [ "$basicauth" ]; then
  187. export _H1="Authorization: Basic $basicauth"
  188. fi
  189. export _H2="Content-Type: application/json"
  190. if [ "$m" != "GET" ]; then
  191. response="$(_post "$data" "$SIMPLY_Api/$ep" "" "$m")"
  192. else
  193. response="$(_get "$SIMPLY_Api/$ep")"
  194. fi
  195. if [ "$?" != "0" ]; then
  196. _err "error $ep"
  197. return 1
  198. fi
  199. response="$(echo "$response" | _normalizeJson)"
  200. _debug2 response "$response"
  201. if _contains "$response" "Invalid account authorization"; then
  202. _err "It seems that your api key or accountnumber is not correct."
  203. return 1
  204. fi
  205. return 0
  206. }