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.

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