dns_unoeuro.sh 4.5 KB

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