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.

202 lines
5.1 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_line_number=$(echo "$response" | grep -n "$_sub_domain" | cut -d : -f 1)
  58. record_line_number=$(_math "$record_line_number" - 1)
  59. record_id=$(echo "$response" | _head_n "$record_line_number" | _tail_n 1 1 | _egrep_o "[0-9]{1,}")
  60. _debug "record_id" "$record_id"
  61. _uno_rest PUT "my/products/$h/dns/records/$record_id" "{\"name\":\"$fulldomain\",\"type\":\"TXT\",\"data\":\"$txtvalue\",\"ttl\":120}"
  62. if _contains "$response" "\"status\": 200" >/dev/null; then
  63. _info "Updated, OK"
  64. return 0
  65. fi
  66. _err "Update error"
  67. return 1
  68. fi
  69. }
  70. #fulldomain txtvalue
  71. dns_unoeuro_rm() {
  72. fulldomain=$1
  73. txtvalue=$2
  74. Uno_Key="${Uno_Key:-$(_readaccountconf_mutable Uno_Key)}"
  75. Uno_User="${Uno_User:-$(_readaccountconf_mutable Uno_User)}"
  76. if [ -z "$Uno_Key" ] || [ -z "$Uno_User" ]; then
  77. Uno_Key=""
  78. Uno_User=""
  79. _err "You haven't specified a UnoEuro api key and account yet."
  80. _err "Please create your key and try again."
  81. return 1
  82. fi
  83. if ! _contains "$Uno_User" "UE"; then
  84. _err "It seems that the Uno_User=$Uno_User is not a valid username."
  85. _err "Please check and retry."
  86. return 1
  87. fi
  88. _debug "First detect the root zone"
  89. if ! _get_root "$fulldomain"; then
  90. _err "invalid domain"
  91. return 1
  92. fi
  93. _debug _domain_id "$_domain_id"
  94. _debug _sub_domain "$_sub_domain"
  95. _debug _domain "$_domain"
  96. _debug "Getting txt records"
  97. _uno_rest GET "my/products/$h/dns/records"
  98. if ! _contains "$response" "\"status\": 200"; then
  99. _err "Error"
  100. return 1
  101. fi
  102. if ! _contains "$response" "$_sub_domain"; then
  103. _info "Don't need to remove."
  104. else
  105. record_line_number=$(echo "$response" | grep -n "$_sub_domain" | cut -d : -f 1)
  106. record_line_number=$(_math "$record_line_number" - 1)
  107. record_id=$(echo "$response" | _head_n "$record_line_number" | _tail_n 1 1 | _egrep_o "[0-9]{1,}")
  108. _debug "record_id" "$record_id"
  109. if [ -z "$record_id" ]; then
  110. _err "Can not get record id to remove."
  111. return 1
  112. fi
  113. if ! _uno_rest DELETE "my/products/$h/dns/records/$record_id"; then
  114. _err "Delete record error."
  115. return 1
  116. fi
  117. _contains "$response" "\"status\": 200"
  118. fi
  119. }
  120. #################### Private functions below ##################################
  121. #_acme-challenge.www.domain.com
  122. #returns
  123. # _sub_domain=_acme-challenge.www
  124. # _domain=domain.com
  125. # _domain_id=sdjkglgdfewsdfg
  126. _get_root() {
  127. domain=$1
  128. i=2
  129. p=1
  130. while true; do
  131. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  132. _debug h "$h"
  133. if [ -z "$h" ]; then
  134. #not valid
  135. return 1
  136. fi
  137. if ! _uno_rest GET "my/products/$h/dns/records"; then
  138. return 1
  139. fi
  140. if _contains "$response" "\"status\": 200"; then
  141. _domain_id=$h
  142. if [ "$_domain_id" ]; then
  143. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  144. _domain=$h
  145. return 0
  146. fi
  147. return 1
  148. fi
  149. p=$i
  150. i=$(_math "$i" + 1)
  151. done
  152. return 1
  153. }
  154. _uno_rest() {
  155. m=$1
  156. ep="$2"
  157. data="$3"
  158. _debug "$ep"
  159. export _H1="Content-Type: application/json"
  160. if [ "$m" != "GET" ]; then
  161. _debug data "$data"
  162. response="$(_post "$data" "$Uno_Api/$Uno_User/$Uno_Key/$ep" "" "$m")"
  163. else
  164. response="$(_get "$Uno_Api/$Uno_User/$Uno_Key/$ep")"
  165. fi
  166. if [ "$?" != "0" ]; then
  167. _err "error $ep"
  168. return 1
  169. fi
  170. _debug2 response "$response"
  171. return 0
  172. }