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.

194 lines
4.8 KiB

7 years ago
7 years ago
  1. #!/usr/bin/env sh
  2. #
  3. #Uno_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
  4. #
  5. #Uno_User="UExxxxxx"
  6. Uno_Api="https://api.unoeuro.com/1/$Uno_User/$Uno_Key"
  7. ######## Public functions #####################
  8. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_unoeuro_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. Uno_Key="${Uno_Key:-$(_readaccountconf_mutable Uno_Key)}"
  13. Uno_User="${Uno_User:-$(_readaccountconf_mutable Uno_User)}"
  14. if [ -z "$Uno_Key" ] || [ -z "$Uno_User" ]; then
  15. Uno_Key=""
  16. Uno_User=""
  17. _err "You haven't specified a UnoEuro api key and account yet."
  18. _err "Please create your key and try again."
  19. return 1
  20. fi
  21. if ! _contains "$Uno_User" "UE"; then
  22. _err "It seems that the Uno_User=$Uno_User is not a valid email address."
  23. _err "Please check and retry."
  24. return 1
  25. fi
  26. #save the api key and email to the account conf file.
  27. _saveaccountconf_mutable Uno_Key "$Uno_Key"
  28. _saveaccountconf_mutable Uno_User "$Uno_User"
  29. _debug "First detect the root zone"
  30. if ! _get_root "$fulldomain"; then
  31. _err "invalid domain"
  32. return 1
  33. fi
  34. _debug _domain_id "$_domain_id"
  35. _debug _sub_domain "$_sub_domain"
  36. _debug _domain "$_domain"
  37. _debug "Getting txt records"
  38. _uno_rest GET "my/products/$h/dns/records"
  39. if ! _contains "$response" "\"status\": 200" >/dev/null; then
  40. _err "Error"
  41. return 1
  42. fi
  43. if ! _contains "$response" "$_sub_domain" >/dev/null; then
  44. _info "Adding record"
  45. if _uno_rest POST "my/products/$h/dns/records" "{\"name\":\"$fulldomain\",\"type\":\"TXT\",\"data\":\"$txtvalue\",\"ttl\":120}"; then
  46. if _contains "$response" "\"status\": 200" >/dev/null; then
  47. _info "Added, OK"
  48. return 0
  49. else
  50. _err "Add txt record error."
  51. return 1
  52. fi
  53. fi
  54. _err "Add txt record error."
  55. else
  56. _info "Updating record"
  57. record_id=$(echo "$response" | grep -B 1 "$_sub_domain" | _head_n -1 | _egrep_o "[0-9]{1,}")
  58. _debug "record_id" "$record_id"
  59. _uno_rest PUT "my/products/$h/dns/records/$record_id" "{\"name\":\"$fulldomain\",\"type\":\"TXT\",\"data\":\"$txtvalue\",\"ttl\":120}"
  60. if _contains "$response" "\"status\": 200" >/dev/null; then
  61. _info "Updated, OK"
  62. return 0
  63. fi
  64. _err "Update error"
  65. return 1
  66. fi
  67. }
  68. #fulldomain txtvalue
  69. dns_unoeuro_rm() {
  70. fulldomain=$1
  71. txtvalue=$2
  72. Uno_Key="${Uno_Key:-$(_readaccountconf_mutable Uno_Key)}"
  73. Uno_User="${Uno_User:-$(_readaccountconf_mutable Uno_User)}"
  74. if [ -z "$Uno_Key" ] || [ -z "$Uno_User" ]; then
  75. Uno_Key=""
  76. Uno_User=""
  77. _err "You haven't specified a UnoEuro api key and account yet."
  78. _err "Please create your key and try again."
  79. return 1
  80. fi
  81. _debug "First detect the root zone"
  82. if ! _get_root "$fulldomain"; then
  83. _err "invalid domain"
  84. return 1
  85. fi
  86. _debug _domain_id "$_domain_id"
  87. _debug _sub_domain "$_sub_domain"
  88. _debug _domain "$_domain"
  89. _debug "Getting txt records"
  90. _uno_rest GET "my/products/$h/dns/records"
  91. if ! _contains "$response" "\"status\": 200" >/dev/null; then
  92. _err "Error"
  93. return 1
  94. fi
  95. if ! _contains "$response" "$_sub_domain" >/dev/null; then
  96. _info "Don't need to remove."
  97. else
  98. record_id=$(echo "$response" | grep -B 1 "$_sub_domain" | _head_n -1 | _egrep_o "[0-9]{1,}")
  99. _debug "record_id" "$record_id"
  100. if [ -z "$record_id" ]; then
  101. _err "Can not get record id to remove."
  102. return 1
  103. fi
  104. if ! _uno_rest DELETE "my/products/$h/dns/records/$record_id"; then
  105. _err "Delete record error."
  106. return 1
  107. fi
  108. _contains "$response" "\"status\": 200"
  109. fi
  110. }
  111. #################### Private functions below ##################################
  112. #_acme-challenge.www.domain.com
  113. #returns
  114. # _sub_domain=_acme-challenge.www
  115. # _domain=domain.com
  116. # _domain_id=sdjkglgdfewsdfg
  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. _debug h "$h"
  124. if [ -z "$h" ]; then
  125. #not valid
  126. return 1
  127. fi
  128. if ! _uno_rest GET "my/products/$h/dns/records"; then
  129. return 1
  130. fi
  131. if _contains "$response" "\"status\": 200" >/dev/null; then
  132. _domain_id=$h
  133. if [ "$_domain_id" ]; then
  134. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  135. _domain=$h
  136. return 0
  137. fi
  138. return 1
  139. fi
  140. p=$i
  141. i=$(_math "$i" + 1)
  142. done
  143. return 1
  144. }
  145. _uno_rest() {
  146. m=$1
  147. ep="$2"
  148. data="$3"
  149. _debug "$ep"
  150. #export _H1="X-Auth-Email: $Uno_User"
  151. #export _H2="X-Auth-Key: $Uno_Key"
  152. export _H1="Content-Type: application/json"
  153. if [ "$m" != "GET" ]; then
  154. _debug data "$data"
  155. response="$(_post "$data" "$Uno_Api/$ep" "" "$m")"
  156. else
  157. response="$(_get "$Uno_Api/$ep")"
  158. fi
  159. if [ "$?" != "0" ]; then
  160. _err "error $ep"
  161. return 1
  162. fi
  163. _debug2 response "$response"
  164. return 0
  165. }