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.

243 lines
5.2 KiB

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