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.

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