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.

148 lines
3.8 KiB

6 years ago
6 years ago
6 years ago
3 months ago
3 months ago
3 months ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_namesilo_info='NameSilo.com
  4. Site: NameSilo.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_namesilo
  6. Options:
  7. Namesilo_Key API Key
  8. Author: meowthink
  9. '
  10. #Utilize API to finish dns-01 verifications.
  11. Namesilo_API="https://www.namesilo.com/api"
  12. ######## Public functions #####################
  13. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_namesilo_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. if [ -z "$Namesilo_Key" ]; then
  18. Namesilo_Key=""
  19. _err "API token for namesilo.com is missing."
  20. _err "Please specify that in your environment variable."
  21. return 1
  22. fi
  23. #save the api key and email to the account conf file.
  24. _saveaccountconf Namesilo_Key "$Namesilo_Key"
  25. if ! _get_root "$fulldomain"; then
  26. _err "Unable to find domain specified."
  27. return 1
  28. fi
  29. _debug _sub_domain "$_sub_domain"
  30. _debug _domain "$_domain"
  31. _debug txtvalue "$txtvalue"
  32. if _namesilo_rest GET "dnsAddRecord?version=1&type=xml&key=$Namesilo_Key&domain=$_domain&rrtype=TXT&rrhost=$_sub_domain&rrvalue=$txtvalue"; then
  33. retcode=$(printf "%s\n" "$response" | _egrep_o "<code>300")
  34. if [ "$retcode" ]; then
  35. _info "Successfully added TXT record, ready for validation."
  36. return 0
  37. else
  38. _err "Unable to add the DNS record."
  39. return 1
  40. fi
  41. fi
  42. }
  43. #Usage: fulldomain txtvalue
  44. #Remove the txt record after validation.
  45. dns_namesilo_rm() {
  46. fulldomain=$1
  47. txtvalue=$2
  48. if ! _get_root "$fulldomain"; then
  49. _err "Unable to find domain specified."
  50. return 1
  51. fi
  52. # Get the record id.
  53. if _namesilo_rest GET "dnsListRecords?version=1&type=xml&key=$Namesilo_Key&domain=$_domain"; then
  54. retcode=$(printf "%s\n" "$response" | _egrep_o "<code>300")
  55. if [ "$retcode" ]; then
  56. _record_id=$(echo "$response" | _egrep_o "<record_id>([^<]*)</record_id><type>TXT</type><host>$fulldomain</host>" | _egrep_o "<record_id>([^<]*)</record_id>" | sed -r "s/<record_id>([^<]*)<\/record_id>/\1/" | tail -n 1)
  57. _debug _record_id "$_record_id"
  58. if [ "$_record_id" ]; then
  59. _info "Successfully retrieved the record id for ACME challenge."
  60. else
  61. _info "Empty record id, it seems no such record."
  62. return 0
  63. fi
  64. else
  65. _err "Unable to retrieve the record id."
  66. return 1
  67. fi
  68. fi
  69. # Remove the DNS record using record id.
  70. if _namesilo_rest GET "dnsDeleteRecord?version=1&type=xml&key=$Namesilo_Key&domain=$_domain&rrid=$_record_id"; then
  71. retcode=$(printf "%s\n" "$response" | _egrep_o "<code>300")
  72. if [ "$retcode" ]; then
  73. _info "Successfully removed the TXT record."
  74. return 0
  75. else
  76. _err "Unable to remove the DNS record."
  77. return 1
  78. fi
  79. fi
  80. }
  81. #################### Private functions below ##################################
  82. # _acme-challenge.www.domain.com
  83. # returns
  84. # _sub_domain=_acme-challenge.www
  85. # _domain=domain.com
  86. _get_root() {
  87. domain=$1
  88. i=2
  89. p=1
  90. if ! _namesilo_rest GET "listDomains?version=1&type=xml&key=$Namesilo_Key"; then
  91. return 1
  92. fi
  93. # Need to exclude the last field (tld)
  94. numfields=$(echo "$domain" | _egrep_o "\." | wc -l)
  95. while [ "$i" -le "$numfields" ]; do
  96. host=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  97. _debug host "$host"
  98. if [ -z "$host" ]; then
  99. return 1
  100. fi
  101. if _contains "$response" ">$host</domain>"; then
  102. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  103. _domain="$host"
  104. return 0
  105. fi
  106. p=$i
  107. i=$(_math "$i" + 1)
  108. done
  109. return 1
  110. }
  111. _namesilo_rest() {
  112. method=$1
  113. param=$2
  114. data=$3
  115. if [ "$method" != "GET" ]; then
  116. response="$(_post "$data" "$Namesilo_API/$param" "" "$method")"
  117. else
  118. response="$(_get "$Namesilo_API/$param")"
  119. fi
  120. if [ "$?" != "0" ]; then
  121. _err "error $param"
  122. return 1
  123. fi
  124. _debug2 response "$response"
  125. return 0
  126. }