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.

198 lines
4.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
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"
  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 username."
  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. if ! _contains "$Uno_User" "UE"; then
  82. _err "It seems that the Uno_User=$Uno_User is not a valid username."
  83. _err "Please check and retry."
  84. return 1
  85. fi
  86. _debug "First detect the root zone"
  87. if ! _get_root "$fulldomain"; then
  88. _err "invalid domain"
  89. return 1
  90. fi
  91. _debug _domain_id "$_domain_id"
  92. _debug _sub_domain "$_sub_domain"
  93. _debug _domain "$_domain"
  94. _debug "Getting txt records"
  95. _uno_rest GET "my/products/$h/dns/records"
  96. if ! _contains "$response" "\"status\": 200" >/dev/null; then
  97. _err "Error"
  98. return 1
  99. fi
  100. if ! _contains "$response" "$_sub_domain" >/dev/null; then
  101. _info "Don't need to remove."
  102. else
  103. record_id=$(echo "$response" | grep -B 1 "$_sub_domain" | _head_n -1 | _egrep_o "[0-9]{1,}")
  104. _debug "record_id" "$record_id"
  105. if [ -z "$record_id" ]; then
  106. _err "Can not get record id to remove."
  107. return 1
  108. fi
  109. if ! _uno_rest DELETE "my/products/$h/dns/records/$record_id"; then
  110. _err "Delete record error."
  111. return 1
  112. fi
  113. _contains "$response" "\"status\": 200"
  114. fi
  115. }
  116. #################### Private functions below ##################################
  117. #_acme-challenge.www.domain.com
  118. #returns
  119. # _sub_domain=_acme-challenge.www
  120. # _domain=domain.com
  121. # _domain_id=sdjkglgdfewsdfg
  122. _get_root() {
  123. domain=$1
  124. i=2
  125. p=1
  126. while true; do
  127. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  128. _debug h "$h"
  129. if [ -z "$h" ]; then
  130. #not valid
  131. return 1
  132. fi
  133. if ! _uno_rest GET "my/products/$h/dns/records"; then
  134. return 1
  135. fi
  136. if _contains "$response" "\"status\": 200" >/dev/null; then
  137. _domain_id=$h
  138. if [ "$_domain_id" ]; then
  139. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  140. _domain=$h
  141. return 0
  142. fi
  143. return 1
  144. fi
  145. p=$i
  146. i=$(_math "$i" + 1)
  147. done
  148. return 1
  149. }
  150. _uno_rest() {
  151. m=$1
  152. ep="$2"
  153. data="$3"
  154. _debug "$ep"
  155. export _H1="Content-Type: application/json"
  156. if [ "$m" != "GET" ]; then
  157. _debug data "$data"
  158. response="$(_post "$data" "$Uno_Api/$Uno_User/$Uno_Key/$ep" "" "$m")"
  159. else
  160. response="$(_get "$Uno_Api/$Uno_User/$Uno_Key/$ep")"
  161. fi
  162. if [ "$?" != "0" ]; then
  163. _err "error $ep"
  164. return 1
  165. fi
  166. _debug2 response "$response"
  167. return 0
  168. }