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.

121 lines
5.0 KiB

  1. #!/usr/bin/env bash
  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_ID=""
  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_add_txt "$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. dnsever_delete_txt "$DNSEVER_ID" "$DNSEVER_PW" "$fulldomain" "$txtvalue"
  41. return $?
  42. }
  43. #################### Private functions below ##################################
  44. dnsever_add_txt(){
  45. local login_id="$1"; local login_password="$2"; local fulldomain="$3"; local new_txt="$4"
  46. local n=$(echo $fulldomain | grep -o '\.' | wc -l)
  47. local f=$(seq 1 $(($n - 1)) | paste -d, -s -)
  48. local subname=$(echo $fulldomain | cut -f $f -d .)
  49. local user_domain=$(echo $fulldomain | cut -f $n,$(( $n + 1 )) -d .)
  50. curl -sS -k -c /tmp/dnsever.txt -d "login_id=$login_id" -d "login_password=$login_password" https://kr.dnsever.com/index.html > /tmp/dnsever.html
  51. local skey=$(curl -s -k -b /tmp/dnsever.txt https://kr.dnsever.com/start.html | grep skey | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p")
  52. _info "dnsever_add_txt skey=$skey user_domain=$user_domain selected_menu=edittxt command=add_txt subname=$subname new_txt=$new_txt"
  53. if [ -z "$skey" -o -z "$user_domain" -o -z "$subname" -o -z "$new_txt" ]; then
  54. _err "dnsever_add_txt ERROR skey or user_domain or subname or new_txt was empty"
  55. return 1
  56. fi
  57. curl -sS -k -b /tmp/dnsever.txt -d "skey=$skey" -d "user_domain=$user_domain" -d 'selected_menu=edittxt' \
  58. -d "subname=$subname" -d "new_txt=$new_txt" -d 'command=add_txt' \
  59. "https://kr.dnsever.com/start.html?user_domain=$user_domain&selected_menu=edittxt" > /tmp/dnsever.html
  60. return $?
  61. }
  62. dnsever_delete_txt(){
  63. local login_id="$1"; local login_password="$2"; local fulldomain="$3"; local txtvalue="$4"
  64. local n=$(echo $fulldomain | grep -o '\.' | wc -l)
  65. local f=$(seq 1 $(($n - 1)) | paste -d, -s -)
  66. local subname=$(echo $fulldomain | cut -f $f -d .)
  67. local user_domain=$(echo $fulldomain | cut -f $n,$(( $n + 1 )) -d .)
  68. curl -sS -k -c /tmp/dnsever.txt -d "login_id=$login_id" -d "login_password=$login_password" https://kr.dnsever.com/index.html > /tmp/dnsever.html
  69. curl -sS -k -b /tmp/dnsever.txt "https://kr.dnsever.com/start.html?user_domain=$user_domain&selected_menu=edittxt" > /tmp/dnsever.html
  70. local skey=$(cat /tmp/dnsever.html | grep skey | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p")
  71. _info "dnsever_delete_txt skey=$skey subname=$subname user_domain=$user_domain"
  72. if [ -z "$skey" -o -z "$user_domain" -o -z "$subname" ]; then
  73. _err "dnsever_delete_txt ERROR skey or user_domain or subname was empty"
  74. return 1
  75. fi
  76. local matched=$(grep "$fulldomain" /tmp/dnsever.html | sed -n -e "s/^.*name=['\"]\(.*\)['\"].*value.*$/\1/p" | sed 's/domain_for_txt_//g')
  77. local n; local checked; local input;
  78. for n in $matched; do
  79. local seq=$(cat /tmp/dnsever.html | grep seq_$n | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*/\1/p")
  80. local old_txt=$(cat /tmp/dnsever.html | grep old_txt_$n | sed -n -e "s/^.*value=['\"]\(.*\)['\"].*id=.*$/\1/p")
  81. if [ "$txtvalue" != "$old_txt" ]; then
  82. _info "dnsever_delete_txt skip deleting seq=$seq fulldomain=$fulldomain due to old_txt=$old_txt was different from txtvalue=$txtvalue skip"
  83. continue
  84. fi
  85. checked="$checked,$n"
  86. input="$input -d domain_for_txt_$n=$fulldomain -d seq_$n=$seq -d old_txt_$n=$old_txt"
  87. done
  88. local check=$(echo "$checked" | sed 's/^,//')
  89. _info "dnsever_delete_txt skey=$skey user_domain=$user_domain selected_menu=edittxt command=delete_txt check[]=$check $input"
  90. if [ -z "$skey" -o -z "$user_domain" -o -z "$check" -o -z "$input" ]; then
  91. _err "dnsever_delete_txt ERROR skey or user_domain or check[] or other parameters was empty. Maybe $fulldomain=$txtvalue was not found."
  92. return 1
  93. fi
  94. curl -sS -k -b /tmp/dnsever.txt -d "skey=$skey" -d "user_domain=$user_domain" -d 'selected_menu=edittxt' \
  95. -d "check[]=$check" $input -d 'command=delete_txt' \
  96. "https://kr.dnsever.com/start.html?user_domain=$user_domain&selected_menu=edittxt" > /tmp/dnsever.html
  97. return $?
  98. }