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.

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