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.

247 lines
5.4 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
  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/' | tr "\n" ' ' | tr -d ' ' | tr ';' ' ')
  53. nr_of_deleted_records=0
  54. _info "Fetching txt record"
  55. for record in $records; do
  56. _debug record "$record"
  57. record_data=$(echo "$record" | cut -d "," -f 3 | sed 's/"//g' | grep "data" | cut -d ":" -f 2)
  58. record_type=$(echo "$record" | cut -d "," -f 4 | sed 's/"//g' | grep "type" | cut -d ":" -f 2)
  59. _debug2 record_data "$record_data"
  60. _debug2 record_type "$record_type"
  61. if [ "$record_data" = "$txtvalue" ] && [ "$record_type" = "TXT" ]; then
  62. record_id=$(echo "$record" | cut -d "," -f 1 | grep "record_id" | cut -d ":" -f 2)
  63. _info "Deleting record $record"
  64. _debug2 record_id "$record_id"
  65. if [ "$record_id" -gt 0 ]; then
  66. if ! _simply_delete_record "$_domain" "$_sub_domain" "$record_id"; then
  67. _err "Record with id $record_id could not be deleted"
  68. return 1
  69. fi
  70. nr_of_deleted_records=1
  71. break
  72. else
  73. _err "Fetching record_id could not be done, this should not happen, exiting function. Failing record is $record"
  74. break
  75. fi
  76. fi
  77. done
  78. if [ "$nr_of_deleted_records" -eq 0 ]; then
  79. _err "No record deleted, the DNS record needs to be removed manually."
  80. else
  81. _info "Deleted $nr_of_deleted_records record"
  82. fi
  83. return 0
  84. }
  85. #################### Private functions below ##################################
  86. _simply_load_config() {
  87. SIMPLY_Api="${SIMPLY_Api:-$(_readaccountconf_mutable SIMPLY_Api)}"
  88. SIMPLY_AccountName="${SIMPLY_AccountName:-$(_readaccountconf_mutable SIMPLY_AccountName)}"
  89. SIMPLY_ApiKey="${SIMPLY_ApiKey:-$(_readaccountconf_mutable SIMPLY_ApiKey)}"
  90. if [ -z "$SIMPLY_Api" ]; then
  91. SIMPLY_Api="$SIMPLY_Api_Default"
  92. fi
  93. if [ -z "$SIMPLY_AccountName" ] || [ -z "$SIMPLY_ApiKey" ]; then
  94. SIMPLY_AccountName=""
  95. SIMPLY_ApiKey=""
  96. _err "A valid Simply API account and apikey not provided."
  97. _err "Please provide a valid API user and try again."
  98. return 1
  99. fi
  100. return 0
  101. }
  102. _simply_save_config() {
  103. if [ "$SIMPLY_Api" != "$SIMPLY_Api_Default" ]; then
  104. _saveaccountconf_mutable SIMPLY_Api "$SIMPLY_Api"
  105. fi
  106. _saveaccountconf_mutable SIMPLY_AccountName "$SIMPLY_AccountName"
  107. _saveaccountconf_mutable SIMPLY_ApiKey "$SIMPLY_ApiKey"
  108. }
  109. _simply_get_all_records() {
  110. domain=$1
  111. if ! _simply_rest GET "my/products/$domain/dns/records"; then
  112. return 1
  113. fi
  114. return 0
  115. }
  116. _get_root() {
  117. domain=$1
  118. i=2
  119. p=1
  120. while true; do
  121. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  122. if [ -z "$h" ]; then
  123. #not valid
  124. return 1
  125. fi
  126. if ! _simply_rest GET "my/products/$h/dns"; then
  127. return 1
  128. fi
  129. if _contains "$response" '"code":"NOT_FOUND"'; then
  130. _debug "$h not found"
  131. else
  132. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  133. _domain="$h"
  134. return 0
  135. fi
  136. p="$i"
  137. i=$(_math "$i" + 1)
  138. done
  139. return 1
  140. }
  141. _simply_add_record() {
  142. domain=$1
  143. sub_domain=$2
  144. txtval=$3
  145. data="{\"name\": \"$sub_domain\", \"type\":\"TXT\", \"data\": \"$txtval\", \"priority\":0, \"ttl\": 3600}"
  146. if ! _simply_rest POST "my/products/$domain/dns/records" "$data"; then
  147. _err "Adding record not successfull!"
  148. return 1
  149. fi
  150. return 0
  151. }
  152. _simply_delete_record() {
  153. domain=$1
  154. sub_domain=$2
  155. record_id=$3
  156. _debug record_id "Delete record with id $record_id"
  157. if ! _simply_rest DELETE "my/products/$domain/dns/records/$record_id"; then
  158. _err "Deleting record not successfull!"
  159. return 1
  160. fi
  161. return 0
  162. }
  163. _simply_rest() {
  164. m=$1
  165. ep="$2"
  166. data="$3"
  167. _debug2 data "$data"
  168. _debug2 ep "$ep"
  169. _debug2 m "$m"
  170. export _H1="Content-Type: application/json"
  171. if [ "$m" != "GET" ]; then
  172. response="$(_post "$data" "$SIMPLY_Api/$SIMPLY_AccountName/$SIMPLY_ApiKey/$ep" "" "$m")"
  173. else
  174. response="$(_get "$SIMPLY_Api/$SIMPLY_AccountName/$SIMPLY_ApiKey/$ep")"
  175. fi
  176. if [ "$?" != "0" ]; then
  177. _err "error $ep"
  178. return 1
  179. fi
  180. _debug2 response "$response"
  181. if _contains "$response" "Invalid account authorization"; then
  182. _err "It seems that your api key or accountnumber is not correct."
  183. return 1
  184. fi
  185. return 0
  186. }