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.

245 lines
8.2 KiB

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