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.

292 lines
7.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env sh
  2. #Original Author: Gerardo Trotta <gerardo.trotta@euronet.aero>
  3. #Application username
  4. #ARUBA_AK="xxxxx"
  5. #
  6. #Application password
  7. #ARUBA_AS="xxxxxx"
  8. #
  9. #API key
  10. #ARUBA_TK="xxxxxxxxxxxxxxxx"
  11. #
  12. #Consumer Key
  13. #ARUBA_CK="sdfsdfsdfsdfsdfdsf"
  14. #ARUBA_END_POINT=aruba-b-it
  15. #'aruba-business-it'
  16. ARUBA_BUSINESS_IT='https://api.arubabusiness.it'
  17. _aruba_get_api() {
  18. _ogaep="$1"
  19. case "${_ogaep}" in
  20. aruba-b-it | arubabit)
  21. printf "%s" $ARUBA_BUSINESS_IT
  22. return
  23. ;;
  24. *)
  25. _err "Unknown parameter : $1"
  26. return 1
  27. ;;
  28. esac
  29. }
  30. _initAuth() {
  31. ARUBA_AK="${ARUBA_AK:-$(_readaccountconf_mutable ARUBA_AK)}"
  32. ARUBA_AS="${ARUBA_AS:-$(_readaccountconf_mutable ARUBA_AS)}"
  33. ARUBA_TK="${ARUBA_TK:-$(_readaccountconf_mutable ARUBA_TK)}"
  34. if [ -z "$ARUBA_AK" ] || [ -z "$ARUBA_AS" ] || [ -z "$ARUBA_TK" ]; then
  35. ARUBA_AK=""
  36. ARUBA_AS=""
  37. ARUBA_TK=""
  38. _err "You don't specify ARUBA application key and application secret yet."
  39. _err "Please create you key and try again."
  40. return 1
  41. fi
  42. if [ "$ARUBA_TK" != "$(_readaccountconf ARUBA_TK)" ]; then
  43. _info "It seems that your aruba key is changed, let's clear consumer key first."
  44. _clearaccountconf ARUBA_TK
  45. _clearaccountconf ARUBA_CK
  46. fi
  47. _saveaccountconf_mutable ARUBA_AK "$ARUBA_AK"
  48. _saveaccountconf_mutable ARUBA_AS "$ARUBA_AS"
  49. _saveaccountconf_mutable ARUBA_TK "$ARUBA_TK"
  50. ARUBA_END_POINT="${ARUBA_END_POINT:-$(_readaccountconf_mutable ARUBA_END_POINT)}"
  51. if [ -z "$ARUBA_END_POINT" ]; then
  52. ARUBA_END_POINT="aruba-b-it"
  53. fi
  54. _info "Using ARUBA endpoint: $ARUBA_END_POINT"
  55. if [ "$ARUBA_END_POINT" != "aruba-b-it" ]; then
  56. _saveaccountconf_mutable ARUBA_END_POINT "$ARUBA_END_POINT"
  57. fi
  58. ARUBA_API="$(_aruba_get_api $ARUBA_END_POINT)"
  59. _debug ARUBA_API "$ARUBA_API"
  60. ARUBA_CK="${ARUBA_CK:-$(_readaccountconf_mutable ARUBA_CK)}"
  61. if [ -z "$ARUBA_CK" ]; then
  62. _info "ARUBA consumer key is empty, Let's get one:"
  63. if ! _aruba_authentication; then
  64. _err "Can not get consumer key."
  65. #return and wait for retry.
  66. return 1
  67. fi
  68. fi
  69. #_info "Checking authentication and get domain details"
  70. #if ! _aruba_rest GET "api/domains/dns/$_domain/details" || _contains "$response" "error" || _contains "$response" "denied"; then
  71. # _err "The consumer key is invalid: $ARUBA_CK"
  72. # _err "Please retry to create a new one."
  73. # _clearaccountconf ARUBA_CK
  74. # return 1
  75. #fi
  76. #domainData=$(echo "$response" | tr -d '\r')
  77. ## get all Ids and peek only values
  78. #temp="$(echo "$domainData" | _egrep_o "Id\": [^,]*" | cut -d : -f 2 | head -1)" # first element is zone Id
  79. #domain_id=$temp
  80. #_info "DomainId is: $domain_id"
  81. _info "Consumer key is ok."
  82. return 0
  83. }
  84. ######## Public functions #####################
  85. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  86. dns_aruba_add() {
  87. fulldomain=$1
  88. txtvalue=$2
  89. #_debug _domain "$_domain"
  90. #_sub_domain="_acme-challenge"
  91. if ! _initAuth; then
  92. return 1
  93. fi
  94. _debug "First detect the root zone"
  95. if ! _get_root "$fulldomain"; then
  96. _err "invalid domain"
  97. return 1
  98. fi
  99. _info "Get domain details"
  100. if ! _aruba_rest GET "api/domains/dns/$_domain/details" || _contains "$response" "error" || _contains "$response" "denied"; then
  101. _err "Error reading domn details for : $_domain"
  102. return 1
  103. fi
  104. domainData=$(echo "$response" | tr -d '\r')
  105. # get all Ids and peek only values
  106. temp="$(echo "$domainData" | _egrep_o "Id\": [^,]*" | cut -d : -f 2 | head -1)" # first element is zone Id
  107. domain_id=$temp
  108. _info "DomainId is: $domain_id"
  109. _debug "Check if _acme-challenge record exists in " "$_domain"
  110. if ! _extract_record_id "$fulldomain."; then # notice dot at the end, aruba TXT is like this: _acme-challenge.www.domain.com.
  111. _method="POST"
  112. else
  113. _method="PUT"
  114. fi
  115. _payload="{ \"IdDomain\": $domain_id, \"Type\": \"TXT\", \"Name\": \"$_sub_domain\", \"Content\": \"\\\"$txtvalue\\\"\" }"
  116. _info "Adding record"
  117. if _aruba_rest "$_method" "api/domains/dns/record" "$_payload"; then
  118. if _contains "$response" "$txtvalue"; then
  119. _aruba_rest GET "api/domains/dns/$_domain/details"
  120. _debug "Refresh:$response"
  121. _info "Added, sleep 10 seconds."
  122. _sleep 10
  123. return 0
  124. fi
  125. fi
  126. _err "Add txt record error."
  127. return 1
  128. }
  129. #fulldomain
  130. dns_aruba_rm() {
  131. fulldomain=$1
  132. txtvalue=$2
  133. if ! _initAuth; then
  134. return 1
  135. fi
  136. _sub_domain="_acme-challenge"
  137. _debug "Getting TXT record to delete: $fulldomain."
  138. if ! _extract_record_id "$fulldomain."; then
  139. return 1
  140. fi
  141. _debug "Deleting TXT record: $fulldomain. Id: $_recordId"
  142. if ! _aruba_rest DELETE "api/domains/dns/record/$_recordId"; then
  143. return 1
  144. fi
  145. return 0
  146. }
  147. #################### Private functions below ##################################
  148. #_acme-challenge.www.domain.com
  149. #returns
  150. # _sub_domain=_acme-challenge.www
  151. # _domain=domain.com
  152. _get_root() {
  153. domain=$1
  154. i=2
  155. p=1
  156. while true; do
  157. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  158. if [ -z "$h" ]; then
  159. #not valid
  160. return 1
  161. fi
  162. _debug "doamin to check: $h"
  163. if ! _aruba_rest GET "api/domains/dns/$h/details"; then
  164. return 1
  165. fi
  166. if ! _contains "$response" "error" >/dev/null && ! _contains "$response" "denied" >/dev/null; then
  167. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  168. _domain="$h"
  169. return 0
  170. fi
  171. p=$i
  172. i=$(_math "$i" + 1)
  173. done
  174. return 1
  175. }
  176. # returns TXT record and put it in_record_id, if esists
  177. _extract_record_id() {
  178. subdomain="$1"
  179. _ids="$(echo "$domainData" | _egrep_o '"Id": [^,]+' | cut -d : -f 2)"
  180. #_debug "$_ids"
  181. #_temp="$(echo $domainData | grep -oP "\"DomainId\":\s\d{1,}," | tr -d ' ')"
  182. #_domainids="$(echo $_temp | tr -d ' ')"
  183. _names="$(echo "$domainData" | _egrep_o '"Name": [^,]*' | cut -d : -f 2)"
  184. _debug "$_names"
  185. ARRAY_IDS=$(echo "$_ids" | tr ", " "\n")
  186. ARRAY_NAMES=$_names
  187. j=0
  188. for i in $ARRAY_NAMES; do
  189. if [ "$i" = "$subdomain" ]; then
  190. _debug printf "%s\t%s\n" "$i"
  191. #_arrayname=$i
  192. _arrayId=$j
  193. _info "Found txt record id: $_arrayId"
  194. fi
  195. j=$(_math "$j" + 1)
  196. done
  197. n=0
  198. for i in $ARRAY_IDS; do
  199. if [ "$n" = "$_arrayId" ]; then
  200. _recordId=$i
  201. _info "recordid found: $_recordId"
  202. return 0
  203. fi
  204. n=$(_math "$n" + 1)
  205. done
  206. return 1
  207. }
  208. _aruba_authentication() {
  209. export _H1="Content-Type: application/x-www-form-urlencoded"
  210. export _H2="Authorization-Key: $ARUBA_TK"
  211. _H3=""
  212. _H4=""
  213. _arubadata="grant_type=password&username=$ARUBA_AK&password=$ARUBA_AS"
  214. response="$(_post "$_arubadata" "$ARUBA_API/auth/token")"
  215. _debug "$(_post "$_arubadata" "$ARUBA_API/auth/token")"
  216. _debug3 response "$response"
  217. access_token="$(echo "$response" | _egrep_o "access_token\":\"[^\"]*\"" | cut -d : -f 2 | tr -d '"')"
  218. if [ -z "$access_token" ]; then
  219. _err "Unable to get access_token"
  220. return 1
  221. fi
  222. _secure_debug access_token "$access_token"
  223. ARUBA_CK="$access_token"
  224. _saveaccountconf ARUBA_CK "$ARUBA_CK"
  225. return 0
  226. }
  227. _aruba_rest() {
  228. m=$1
  229. ep="$2"
  230. data="$3"
  231. _debug "$ep"
  232. _aruba_url="$ARUBA_API/$ep"
  233. _debug2 _aruba_url "$_aruba_url"
  234. export _H1="Content-type: application/json"
  235. export _H2="Accept: application/json"
  236. export _H3="Authorization: Bearer $ARUBA_CK"
  237. export _H4="Authorization-Key: $ARUBA_TK"
  238. export _H5="Accept: application/json"
  239. _debug2 _H3 "$_H3"
  240. _debug2 _H4 "$_H4"
  241. if [ "$data" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ] || [ "$m" = "DELETE" ]; then
  242. _debug data "$data"
  243. response="$(_post "$data" "$_aruba_url" "" "$m")"
  244. else
  245. response="$(_get "$_aruba_url")"
  246. fi
  247. if [ "$?" != "0" ] || _contains "$response" "wrong credentials" || _contains "$response" "Unprocessable" || _contains "$response" "denied"; then
  248. _err "Response error $response"
  249. return 1
  250. fi
  251. _debug2 response "$response"
  252. return 0
  253. }