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.

275 lines
8.8 KiB

4 years ago
  1. #!/usr/bin/env sh
  2. #SH_Token="xxxx"
  3. #SH_Username="xxxx"
  4. #SH_Domain_ID="xxxx"
  5. #SH_DNS_Record_ID="xxxx" only for internal use
  6. SH_Api="https://manager.shellrent.com/api2"
  7. ######## Public functions #####################
  8. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_shellrent_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. SH_Token="${SH_Token:-$(_readaccountconf_mutable SH_Token)}"
  13. SH_Username="${SH_Username:-$(_readaccountconf_mutable SH_Username)}"
  14. SH_Domain_ID="${SH_Domain_ID:-$(_readaccountconf_mutable SH_Domain_ID)}"
  15. if [ "$SH_Token" ]; then
  16. _saveaccountconf_mutable SH_Token "$SH_Token"
  17. _saveaccountconf_mutable SH_Username "$SH_Username"
  18. _saveaccountconf_mutable SH_Domain_ID "$SH_Domain_ID"
  19. else
  20. if [ -z "$SH_Token" ] || [ -z "$SH_Username" ]; then
  21. SH_Token=""
  22. SH_Username=""
  23. _err "You didn't specify a Shellrent api key and username yet."
  24. _err "You can get yours from here https://manager.shellrent.com/"
  25. return 1
  26. fi
  27. #save the api key and email to the account conf file.
  28. _saveaccountconf_mutable SH_Token "$SH_Token"
  29. _saveaccountconf_mutable SH_Username "$SH_Username"
  30. fi
  31. _debug "First detect the root zone"
  32. if ! _get_root "$fulldomain"; then
  33. _err "invalid domain"
  34. return 1
  35. fi
  36. _debug _fulldomain "$fulldomain"
  37. _debug _domain_id "$_domain_id"
  38. _debug _sub_domain "$_sub_domain"
  39. _debug _domain "$_domain"
  40. _prefix=$(echo "$fulldomain" | sed "s/.$_domain//g" )
  41. _debug _prefix "$_prefix"
  42. _debug "Getting records ids"
  43. _shellrent_rest GET "/dns_record/index/${_domain_id}"
  44. if ! echo "$response" | tr -d " " | grep \"error\":0 >/dev/null; then
  45. _err "Error"
  46. return 1
  47. fi
  48. # get dns records list
  49. _dns_record_list=$( echo "$response" | cut -d'[' -f2 | cut -d']' -f1 | sed 's/,\?"/ /g' )
  50. _debug _dns_record_list "$_dns_record_list"
  51. for _dns_record in $_dns_record_list; do
  52. _shellrent_rest GET "/dns_record/details/$_domain_id/$_dns_record"
  53. _dns_record_type=$( echo "$response" | sed 's/type/#/g' | cut -d"#" -f2 | cut -d"\"" -f3 )
  54. _debug _dns_record_type "$_dns_record_type"
  55. _dns_record_prefix=$( echo "$response" | sed 's/host/#/g' | cut -d"#" -f2 | cut -d"\"" -f3 )
  56. _debug _dns_record_prefix "$_dns_record_prefix"
  57. _dns_record_destination=$( echo "$response" | sed 's/destination/#/g' | cut -d"#" -f2 | cut -d"\"" -f3 )
  58. _debug _dns_record_destination "$_dns_record_destination"
  59. if [ "$_dns_record_type" = "TXT" ] && [ "$_dns_record_prefix" = "$_prefix" ] && [ "$_dns_record_destination" = "$txtvalue" ]; then
  60. _info "Already exists, OK"
  61. _saveaccountconf_mutable SH_DNS_Record_ID "$_dns_record"
  62. return 0
  63. fi
  64. done
  65. _info "Adding record"
  66. if _shellrent_rest POST "/dns_record/store/$_domain_id" "{\"type\":\"TXT\",\"host\":\"$_prefix\",\"destination\":\"$txtvalue\"}"; then
  67. if _contains "$response" "aggiunto con successo"; then
  68. _info "Added, OK"
  69. _dns_record=$( echo "$response" | sed 's/id/#/g' | cut -d"#" -f2 | cut -d"\"" -f3 )
  70. _debug _dns_record "$_dns_record"
  71. _saveaccountconf_mutable SH_DNS_Record_ID "$_dns_record"
  72. return 0
  73. else
  74. _err "Add txt record error."
  75. return 1
  76. fi
  77. fi
  78. _err "Add txt record error."
  79. return 1
  80. }
  81. #fulldomain txtvalue
  82. dns_shellrent_rm() {
  83. fulldomain=$1
  84. txtvalue=$2
  85. SH_Token="${SH_Token:-$(_readaccountconf_mutable SH_Token)}"
  86. SH_Username="${SH_Username:-$(_readaccountconf_mutable SH_Username)}"
  87. SH_Domain_ID="${SH_Domain_ID:-$(_readaccountconf_mutable SH_Domain_ID)}"
  88. SH_DNS_Record_ID="${SH_DNS_Record_ID:-$(_readaccountconf_mutable SH_DNS_Record_ID)}"
  89. _debug "First detect the root zone"
  90. if ! _get_root "$fulldomain"; then
  91. _err "invalid domain"
  92. return 1
  93. fi
  94. _debug _fulldomain "$fulldomain"
  95. _debug _domain_id "$_domain_id"
  96. _debug _sub_domain "$_sub_domain"
  97. _debug _domain "$_domain"
  98. _prefix=$(echo "$fulldomain" | sed "s/.$_domain//g" )
  99. _debug _prefix "$_prefix"
  100. if [ "$SH_DNS_Record_ID" ]; then
  101. if _shellrent_rest GET "/dns_record/details/$SH_Domain_ID/$SH_DNS_Record_ID"; then
  102. if echo "$response" | tr -d " " | grep \"error\":0 >/dev/null; then
  103. _info "Record Fount. Try to delete it"
  104. _shellrent_rest DELETE "/dns_record/remove/$SH_Domain_ID/$SH_DNS_Record_ID"
  105. return 0
  106. else
  107. _Info "Record NOT Found. Try to search on all records"
  108. fi
  109. fi
  110. fi
  111. _debug "Getting records ids"
  112. _shellrent_rest GET "/dns_record/index/${_domain_id}"
  113. if ! echo "$response" | tr -d " " | grep \"error\":0 >/dev/null; then
  114. _err "Error"
  115. return 1
  116. fi
  117. # get dns records list
  118. _dns_record_list=$( echo "$response" | cut -d'[' -f2 | cut -d']' -f1 | sed 's/,\?"/ /g' )
  119. _debug _dns_record_list "$_dns_record_list"
  120. for _dns_record in $_dns_record_list; do
  121. _shellrent_rest GET "/dns_record/details/$_domain_id/$_dns_record"
  122. _dns_record_type=$( echo "$response" | sed 's/type/#/g' | cut -d"#" -f2 | cut -d"\"" -f3 )
  123. _debug _dns_record_type "$_dns_record_type"
  124. _dns_record_prefix=$( echo "$response" | sed 's/host/#/g' | cut -d"#" -f2 | cut -d"\"" -f3 )
  125. _debug _dns_record_prefix "$_dns_record_prefix"
  126. _dns_record_destination=$( echo "$response" | sed 's/destination/#/g' | cut -d"#" -f2 | cut -d"\"" -f3 )
  127. _debug _dns_record_destination "$_dns_record_destination"
  128. if [ "$_dns_record_type" = "TXT" ] && [ "$_dns_record_prefix" = "$_prefix" ] && [ "$_dns_record_destination" = "$txtvalue" ]; then
  129. _info "Remove Record With ID $_dns_record"
  130. _shellrent_rest DELETE "/dns_record/remove/$_domain_id/$_dns_record"
  131. if ! echo "$response" | tr -d " " | grep \"error\":0 >/dev/null; then
  132. _err "Error on Record Delete"
  133. return 1
  134. fi
  135. return 0
  136. fi
  137. done
  138. _err "Del txt record error."
  139. return 1
  140. }
  141. #################### Private functions below ##################################
  142. #_acme-challenge.www.domain.com
  143. #returns
  144. # _sub_domain=_acme-challenge.www
  145. # _domain=domain.com
  146. # _domain_id=sdjkglgdfewsdfg
  147. _get_root() {
  148. _domain=$1
  149. # Use Zone ID directly if provided
  150. if [ "$SH_Domain_ID" ]; then
  151. if ! _shellrent_rest GET "/domain/details/$SH_Domain_ID"; then
  152. return 1
  153. else
  154. if echo "$response" | grep \"error\":0 >/dev/null; then
  155. _dom=$(echo "$response" | cut -d":" -f8 | cut -d"," -f1 | sed 's/"//g')
  156. if [ "$_dom" ]; then
  157. _sub_domain=$( echo "$_domain" | sed "s/$_dom//g" | sed 's/.$//g')
  158. _domain="$_dom"
  159. _domain_id=$SH_Domain_ID
  160. _debug _domain "$_domain"
  161. _debug _sub_domain "$_sub_domain"
  162. return 0
  163. else
  164. return 1
  165. fi
  166. else
  167. return 1
  168. fi
  169. fi
  170. fi
  171. _shellrent_rest GET "/purchase"
  172. # get puchase list
  173. _purchases_list=$( echo "$response" | cut -d'[' -f2 | cut -d']' -f1 | sed 's/,\?"/ /g' )
  174. _debug _purchases_list "$_purchases_list"
  175. for _purchase in $_purchases_list; do
  176. _shellrent_rest GET "/purchase/details/$_purchase"
  177. # if the purchse have the domain_id value, is a domain
  178. if [ "$(echo "$response" | grep -c domain_id )" -eq 1 ]; then
  179. _debug _is_domain "true"
  180. _domain_id=$( echo "$response" | sed 's/domain_id/#/g' | cut -d"#" -f2 | sed 's/"\?://g' | sed 's/}//g' )
  181. _debug _domain_id "$_domain_id"
  182. # get the domain details
  183. _shellrent_rest GET "/domain/details/$_domain_id"
  184. _api_domain_name=$( echo "$response" | cut -d":" -f8 | cut -d"," -f1 | sed 's/"//g' )
  185. _debug _api_domain_name "$_api_domain_name"
  186. # first check if _domain partially match with the purchase domain
  187. if _contains "$_domain" "$_api_domain_name" ; then
  188. _debug _domain_found "maybe"
  189. _i=1
  190. _dom="$_domain"
  191. #wal thru "point" to get the domain and the subdomain
  192. while [ -n "$_dom" ]; do
  193. _dom=$(echo "$_domain" | cut -d . -f $_i-100)
  194. _debug _dom "$_dom"
  195. if [ "$_dom" = "$_api_domain_name" ]; then
  196. _sub_domain=$( echo "$_domain" | sed "s/$_dom//g" | sed 's/.$//g')
  197. _domain="$_dom"
  198. _debug _domain_found "true"
  199. _debug _domain "$_domain"
  200. _debug _sub_domain "$_sub_domain"
  201. _saveaccountconf_mutable SH_Domain_ID "$_domain_id"
  202. return 0
  203. fi
  204. _i=$(_math "$_i" + 1)
  205. done
  206. else
  207. _debug _domain_found "false"
  208. fi
  209. else
  210. _debug _is_domain "false"
  211. fi
  212. done
  213. return 1
  214. }
  215. _shellrent_rest() {
  216. m=$1
  217. ep="$2"
  218. data="$3"
  219. _debug "$ep"
  220. export _H1="Content-Type: application/json"
  221. export _H2="Authorization: $SH_Username.$SH_Token"
  222. if [ "$m" != "GET" ]; then
  223. _debug data "$data"
  224. response="$(_post "$data" "$SH_Api$ep" "" "$m")"
  225. _response_result=$?
  226. else
  227. response="$(_get "$SH_Api$ep")"
  228. _response_result=$?
  229. fi
  230. if [ "$_response_result" != "0" ]; then
  231. _err "error $ep"
  232. return 1
  233. fi
  234. _debug2 response "$response"
  235. return 0
  236. }