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.

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