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.

234 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. _inithttp
  61. response=$(_post "login_id=$login_id&login_password=$login_password" "https://kr.dnsever.com/index.html")
  62. if [ $? != 0 ] || [ -z "$response" ]; then
  63. _err "dnsever_txt:$action ERROR login failed. Please check https://kr.dnsever.com/index.html with login_id=$login_id login_password=$login_password"
  64. return 1
  65. fi
  66. _H2="$(grep PHPSESSID "$HTTP_HEADER" | sed s/^Set-//)"
  67. export _H2
  68. response=$(_post "" "https://kr.dnsever.com/start.html")
  69. if [ $? != 0 ] || [ -z "$response" ]; then
  70. _err "dnsever_txt:$action ERROR login failed. Please check https://kr.dnsever.com/start.html after login"
  71. return 1
  72. fi
  73. skey=$(printf "%s\n" "$response" | grep skey | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p")
  74. if [ -z "$skey" ]; then
  75. _err "dnsever_txt:$action ERROR login failed with login_id=$login_id login_password=$login_password"
  76. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  77. return 1
  78. fi
  79. user_domain=$(dnsever_select_user_domain "$fulldomain" "$response")
  80. if [ -z "$user_domain" ]; then
  81. _err "dnsever_txt:$action ERROR no matching domain in DNSEver"
  82. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  83. return 1
  84. fi
  85. if [ "$action" = "add" ]; then
  86. subname=$(echo "$fulldomain" | sed "s/\.$user_domain\$//")
  87. if [ -z "$subname" ] || [ -z "$txt" ]; then
  88. _err "dnsever_txt ERROR subname=$subname or txt=$txt is empty"
  89. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  90. return 1
  91. fi
  92. _info "dnsever_txt:$action skey=$skey user_domain=$user_domain selected_menu=edittxt command=add_txt subname=$subname txt=$txt"
  93. response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt" "https://kr.dnsever.com/start.html")
  94. if [ $? != 0 ] || [ -z "$response" ]; then
  95. _err "dnsever_txt:$action ERROR failed to get TXT records from DNSEver"
  96. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  97. return 1
  98. fi
  99. check=$(dnsever_check "$fulldomain" "$txt" "$response")
  100. if [ $? = 0 ] || [ -n "$check" ]; then
  101. _err "dnsever_txt:$action ERROR $fulldomain=$txt already exists"
  102. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  103. return 1
  104. fi
  105. 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")
  106. if [ $? != 0 ] || [ -z "$response" ]; then
  107. _err "dnsever_txt:$action ERROR failed to add_text $fulldomain=$txt"
  108. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  109. return 1
  110. fi
  111. check=$(dnsever_check "$fulldomain" "$txt" "$response")
  112. if [ $? != 0 ] || [ -z "$check" ]; then
  113. _err "dnsever_txt:$action ERROR failed to get newly added $fulldomain=$txt"
  114. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  115. return 1
  116. fi
  117. elif [ "$action" = "delete" ]; then
  118. response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt" "https://kr.dnsever.com/start.html")
  119. if [ $? != 0 ] || [ -z "$response" ]; then
  120. _err "dnsever_txt:$action ERROR failed to get TXT records from DNSEver"
  121. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  122. return 1
  123. fi
  124. check=$(dnsever_check "$fulldomain" "$txt" "$response")
  125. if [ $? != 0 ] || [ -z "$check" ]; then
  126. _err "dnsever_txt:$action ERROR $fulldomain=$txt does not exists"
  127. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  128. return 1
  129. fi
  130. _info "dnsever_txt:$action skey=$skey user_domain=$user_domain selected_menu=edittxt command=delete_txt$(echo "$check" | sed 's/\&/ /g')"
  131. response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt&command=delete_txt&$check" "https://kr.dnsever.com/start.html")
  132. if [ $? != 0 ] || [ -z "$response" ]; then
  133. _err "dnsever_txt:$action ERROR failed to delete $fulldomain=$txt from DNSEver"
  134. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  135. return 1
  136. fi
  137. response=$(_post "skey=$skey&user_domain=$user_domain&selected_menu=edittxt" "https://kr.dnsever.com/start.html")
  138. if [ $? != 0 ] || [ -z "$response" ]; then
  139. _err "dnsever_txt:$action ERROR failed to get $fulldomain=$txt from DNSEver"
  140. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  141. return 1
  142. fi
  143. check=$(dnsever_check "$fulldomain" "$txt" "$response")
  144. if [ $? = 0 ] && [ -n "$check" ]; then
  145. _err "dnsever_txt:$action ERROR $fulldomain=$txt still exists"
  146. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  147. return 1
  148. fi
  149. else
  150. _err "dnsever_txt:$action action should be add or delete"
  151. fi
  152. response=$(_post "skey=$skey" "https://kr.dnsever.com/logout.php")
  153. return 0
  154. }
  155. dnsever_select_user_domain() {
  156. fulldomain="$1"
  157. response="$2"
  158. domains=$(printf "%s\n" "$response" | grep OPTION | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p" | grep -v "^$")
  159. nmax=0
  160. selected=""
  161. for domain in $domains; do
  162. if echo "$fulldomain" | grep -q "$domain\$"; then
  163. n=${#domain}
  164. if [ "$n" -gt $nmax ]; then
  165. nmax=$n
  166. selected="$domain"
  167. fi
  168. fi
  169. done
  170. echo "$selected"
  171. }
  172. dnsever_check() {
  173. fulldomain="$1"
  174. old_txt="$2"
  175. response="$3"
  176. matched=$(printf "%s\n" "$response" | grep "$fulldomain" | sed -n -e "s/^.*name=['\"]\(.*\)['\"].*value.*$/\1/p" | sed 's/domain_for_txt_//g')
  177. check=""
  178. for n in $matched; do
  179. seq=$(printf "%s\n" "$response" | grep "seq_$n" | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p")
  180. old_txt=$(printf "%s\n" "$response" | grep "old_txt_$n" | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*id=.*$/\1/p")
  181. if [ "$txtvalue" != "$old_txt" ]; then
  182. _info "dnsever_check skip seq=$seq fulldomain=$fulldomain due to old_txt=$old_txt is different from txtvalue=$txtvalue skip"
  183. continue
  184. fi
  185. check="${check}&check[]=$n&domain_for_txt_$n=$fulldomain&seq_$n=$seq&old_txt_$n=$old_txt"
  186. done
  187. if [ -z "$check" ]; then
  188. return 1
  189. fi
  190. echo "$check"
  191. return 0
  192. }