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.

232 lines
7.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. #Here is a sample custom api script.
  3. #This file name is "dns_dnsever.sh"
  4. #So, here must be a method dns_dnsever_add()
  5. #Which will be called by acme.sh to add the txt record to your api system.
  6. #returns 0 means success, otherwise error.
  7. #
  8. #Author: hiska
  9. #Report Bugs here: https://github.com/hiskang/acme.sh
  10. #
  11. ######## Public functions #####################
  12. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_dnsever_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. _info "Using dnsever"
  17. _debug fulldomain "$fulldomain"
  18. _debug txtvalue "$txtvalue"
  19. if [ -z "$DNSEVER_ID" ] || [ -z "$DNSEVER_PW" ]; then
  20. DNSEVER_ID=""
  21. DNSEVER_PW=""
  22. _err "You don't specify dnsever.com ID or PW yet."
  23. _err "Please create you key and try again."
  24. return 1
  25. fi
  26. #save the api key and email to the account conf file.
  27. _saveaccountconf DNSEVER_ID "$DNSEVER_ID"
  28. _saveaccountconf DNSEVER_PW "$DNSEVER_PW"
  29. dnsever_txt "add" "$DNSEVER_ID" "$DNSEVER_PW" "$fulldomain" "$txtvalue"
  30. return $?
  31. }
  32. #Usage: fulldomain txtvalue
  33. #Remove the txt record after validation.
  34. dns_dnsever_rm() {
  35. fulldomain=$1
  36. txtvalue=$2
  37. _info "Using dnsever"
  38. _debug fulldomain "$fulldomain"
  39. _debug txtvalue "$txtvalue"
  40. if [ -z "$DNSEVER_ID" ] || [ -z "$DNSEVER_PW" ]; then
  41. DNSEVER_ID=""
  42. DNSEVER_PW=""
  43. _err "You don't specify dnsever.com ID or PW yet."
  44. _err "Please create you key and try again."
  45. return 1
  46. fi
  47. #save the api key and email to the account conf file.
  48. _saveaccountconf DNSEVER_ID "$DNSEVER_ID"
  49. _saveaccountconf DNSEVER_PW "$DNSEVER_PW"
  50. dnsever_txt "delete" "$DNSEVER_ID" "$DNSEVER_PW" "$fulldomain" "$txtvalue"
  51. return $?
  52. }
  53. #################### Private functions below ##################################
  54. dnsever_txt() {
  55. action="$1"
  56. login_id="$2"
  57. login_password="$3"
  58. fulldomain="$4"
  59. txt="$5"
  60. response=$(_post "login_id=$login_id&login_password=$login_password" "https://kr.dnsever.com/index.html")
  61. if [ $? != 0 ] || [ -z "$response" ]; then
  62. _err "dnsever_txt:$action ERROR login failed. Please check https://kr.dnsever.com/index.html with login_id=$login_id login_password=$login_password"
  63. return 1
  64. fi
  65. _H1="$(grep PHPSESSID "$HTTP_HEADER" | sed s/^Set-//)"
  66. export _H1
  67. response=$(_post "" "https://kr.dnsever.com/start.html")
  68. if [ $? != 0 ] || [ -z "$response" ]; then
  69. _err "dnsever_txt:$action ERROR login failed. Please check https://kr.dnsever.com/start.html after login"
  70. return 1
  71. fi
  72. skey=$(printf "%s\n" "$response" | grep skey | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p")
  73. if [ -z "$skey" ]; then
  74. _err "dnsever_txt:$action ERROR login failed with login_id=$login_id login_password=$login_password"
  75. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  76. return 1
  77. fi
  78. user_domain=$(dnsever_select_user_domain "$fulldomain" "$response")
  79. if [ -z "$user_domain" ]; then
  80. _err "dnsever_txt:$action ERROR no matching domain in DNSEver"
  81. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  82. return 1
  83. fi
  84. if [ "$action" = "add" ]; then
  85. subname=$(echo "$fulldomain" | sed "s/\.$user_domain\$//")
  86. if [ -z "$subname" ] || [ -z "$txt" ]; then
  87. _err "dnsever_txt ERROR subname=$subname or txt=$txt is empty"
  88. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  89. return 1
  90. fi
  91. _info "dnsever_txt:$action skey=$skey user_domain=$user_domain selected_menu=edittxt command=add_txt subname=$subname txt=$txt"
  92. response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt" "https://kr.dnsever.com/start.html")
  93. if [ $? != 0 ] || [ -z "$response" ]; then
  94. _err "dnsever_txt:$action ERROR failed to get TXT records from DNSEver"
  95. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  96. return 1
  97. fi
  98. check=$(dnsever_check "$fulldomain" "$txt" "$response")
  99. if [ $? = 0 ] || [ -n "$check" ]; then
  100. _err "dnsever_txt:$action ERROR $fulldomain=$txt already exists"
  101. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  102. return 1
  103. fi
  104. response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt&command=add_txt&subname=$subname&new_txt=$txt" "https://kr.dnsever.com/start.html")
  105. if [ $? != 0 ] || [ -z "$response" ]; then
  106. _err "dnsever_txt:$action ERROR failed to add_text $fulldomain=$txt"
  107. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  108. return 1
  109. fi
  110. check=$(dnsever_check "$fulldomain" "$txt" "$response")
  111. if [ $? != 0 ] || [ -z "$check" ]; then
  112. _err "dnsever_txt:$action ERROR failed to get newly added $fulldomain=$txt"
  113. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  114. return 1
  115. fi
  116. elif [ "$action" = "delete" ]; then
  117. response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt" "https://kr.dnsever.com/start.html")
  118. if [ $? != 0 ] || [ -z "$response" ]; then
  119. _err "dnsever_txt:$action ERROR failed to get TXT records from DNSEver"
  120. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  121. return 1
  122. fi
  123. check=$(dnsever_check "$fulldomain" "$txt" "$response")
  124. if [ $? != 0 ] || [ -z "$check" ]; then
  125. _err "dnsever_txt:$action ERROR $fulldomain=$txt does not exists"
  126. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  127. return 1
  128. fi
  129. _info "dnsever_txt:$action skey=$skey user_domain=$user_domain selected_menu=edittxt command=delete_txt$(echo "$check" | sed 's/\&/ /g')"
  130. response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt&command=delete_txt&$check" "https://kr.dnsever.com/start.html")
  131. if [ $? != 0 ] || [ -z "$response" ]; then
  132. _err "dnsever_txt:$action ERROR failed to delete $fulldomain=$txt from DNSEver"
  133. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  134. return 1
  135. fi
  136. response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt" "https://kr.dnsever.com/start.html")
  137. if [ $? != 0 ] || [ -z "$response" ]; then
  138. _err "dnsever_txt:$action ERROR failed to get $fulldomain=$txt from DNSEver"
  139. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  140. return 1
  141. fi
  142. check=$(dnsever_check "$fulldomain" "$txt" "$response")
  143. if [ $? = 0 ] && [ -n "$check" ]; then
  144. _err "dnsever_txt:$action ERROR $fulldomain=$txt still exists"
  145. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  146. return 1
  147. fi
  148. else
  149. _err "dnsever_txt:$action action should be add or delete"
  150. fi
  151. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  152. return 0
  153. }
  154. dnsever_select_user_domain() {
  155. fulldomain="$1"
  156. response="$2"
  157. domains=$(printf "%s\n" "$response" | grep OPTION | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p" | grep -v "^$")
  158. nmax=0
  159. selected=""
  160. for domain in $domains; do
  161. if echo "$fulldomain" | grep -q "$domain\$"; then
  162. n=${#domain}
  163. if [ "$n" -gt $nmax ]; then
  164. nmax=$n
  165. selected="$domain"
  166. fi
  167. fi
  168. done
  169. echo "$selected"
  170. }
  171. dnsever_check() {
  172. fulldomain="$1"
  173. old_txt="$2"
  174. response="$3"
  175. matched=$(printf "%s\n" "$response" | grep "$fulldomain" | sed -n -e "s/^.*name=['\"]\(.*\)['\"].*value.*$/\1/p" | sed 's/domain_for_txt_//g')
  176. check=""
  177. for n in $matched; do
  178. seq=$(printf "%s\n" "$response" | grep "seq_$n" | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p")
  179. old_txt=$(printf "%s\n" "$response" | grep "old_txt_$n" | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*id=.*$/\1/p")
  180. if [ "$txtvalue" != "$old_txt" ]; then
  181. _info "dnsever_check skip seq=$seq fulldomain=$fulldomain due to old_txt=$old_txt is different from txtvalue=$txtvalue skip"
  182. continue
  183. fi
  184. check="${check}&check[]=$n&domain_for_txt_$n=$fulldomain&seq_$n=$seq&old_txt_$n=$old_txt"
  185. done
  186. if [ -z "$check" ]; then
  187. return 1
  188. fi
  189. echo "$check"
  190. return 0
  191. }