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.

263 lines
5.9 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
  1. #!/usr/bin/env sh
  2. # API-integration for Simply.com (https://www.simply.com)
  3. #SIMPLY_AccountName="accountname"
  4. #SIMPLY_ApiKey="apikey"
  5. #
  6. #SIMPLY_Api="https://api.simply.com/1/[ACCOUNTNAME]/[APIKEY]"
  7. SIMPLY_Api_Default="https://api.simply.com/1"
  8. #This is used for determining success of REST call
  9. SIMPLY_SUCCESS_CODE='"status":200'
  10. ######## Public functions #####################
  11. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  12. dns_simply_add() {
  13. fulldomain=$1
  14. txtvalue=$2
  15. if ! _simply_load_config; then
  16. return 1
  17. fi
  18. _simply_save_config
  19. _debug "First detect the root zone"
  20. if ! _get_root "$fulldomain"; then
  21. _err "invalid domain"
  22. return 1
  23. fi
  24. _debug _sub_domain "$_sub_domain"
  25. _debug _domain "$_domain"
  26. _info "Adding record"
  27. if ! _simply_add_record "$_domain" "$_sub_domain" "$txtvalue"; then
  28. _err "Could not add DNS record"
  29. return 1
  30. fi
  31. return 0
  32. }
  33. dns_simply_rm() {
  34. fulldomain=$1
  35. txtvalue=$2
  36. if ! _simply_load_config; then
  37. return 1
  38. fi
  39. _simply_save_config
  40. _debug "Find the DNS zone"
  41. if ! _get_root "$fulldomain"; then
  42. _err "invalid domain"
  43. return 1
  44. fi
  45. _debug _sub_domain "$_sub_domain"
  46. _debug _domain "$_domain"
  47. _debug txtvalue "$txtvalue"
  48. _info "Getting all existing records"
  49. if ! _simply_get_all_records "$_domain"; then
  50. _err "invalid domain"
  51. return 1
  52. fi
  53. records=$(echo "$response" | tr '{' "\n" | grep 'record_id\|type\|data\|\name' | sed 's/\"record_id/;\"record_id/' | tr "\n" ' ' | tr -d ' ' | tr ';' ' ')
  54. nr_of_deleted_records=0
  55. _info "Fetching txt record"
  56. for record in $records; do
  57. _debug record "$record"
  58. record_data=$(echo "$record" | sed -n "s/.*\"data\":\"\([^\"]*\)\".*/\1/p")
  59. record_type=$(echo "$record" | sed -n "s/.*\"type\":\"\([^\"]*\)\".*/\1/p")
  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" "$SIMPLY_SUCCESS_CODE"; 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. if ! _contains "$response" "$SIMPLY_SUCCESS_CODE"; then
  152. _err "Call to API not sucessfull, see below message for more details"
  153. _err "$response"
  154. return 1
  155. fi
  156. return 0
  157. }
  158. _simply_delete_record() {
  159. domain=$1
  160. sub_domain=$2
  161. record_id=$3
  162. _debug record_id "Delete record with id $record_id"
  163. if ! _simply_rest DELETE "my/products/$domain/dns/records/$record_id/"; then
  164. _err "Deleting record not successfull!"
  165. return 1
  166. fi
  167. if ! _contains "$response" "$SIMPLY_SUCCESS_CODE"; then
  168. _err "Call to API not sucessfull, see below message for more details"
  169. _err "$response"
  170. return 1
  171. fi
  172. return 0
  173. }
  174. _simply_rest() {
  175. m=$1
  176. ep="$2"
  177. data="$3"
  178. _debug2 data "$data"
  179. _debug2 ep "$ep"
  180. _debug2 m "$m"
  181. export _H1="Content-Type: application/json"
  182. if [ "$m" != "GET" ]; then
  183. response="$(_post "$data" "$SIMPLY_Api/$SIMPLY_AccountName/$SIMPLY_ApiKey/$ep" "" "$m")"
  184. else
  185. response="$(_get "$SIMPLY_Api/$SIMPLY_AccountName/$SIMPLY_ApiKey/$ep")"
  186. fi
  187. if [ "$?" != "0" ]; then
  188. _err "error $ep"
  189. return 1
  190. fi
  191. response="$(echo "$response" | _normalizeJson)"
  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. }