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.

178 lines
4.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
1 month ago
1 month ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_namecom_info='Name.com
  4. Site: Name.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_namecom
  6. Options:
  7. Namecom_Username Username
  8. Namecom_Token API Token
  9. Author: RaidenII
  10. '
  11. ######## Public functions #####################
  12. Namecom_API="https://api.name.com/v4"
  13. #Usage: dns_namecom_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_namecom_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. Namecom_Username="${Namecom_Username:-$(_readaccountconf_mutable Namecom_Username)}"
  18. Namecom_Token="${Namecom_Token:-$(_readaccountconf_mutable Namecom_Token)}"
  19. # First we need name.com credentials.
  20. if [ -z "$Namecom_Username" ]; then
  21. Namecom_Username=""
  22. _err "Username for name.com is missing."
  23. _err "Please specify that in your environment variable."
  24. return 1
  25. fi
  26. if [ -z "$Namecom_Token" ]; then
  27. Namecom_Token=""
  28. _err "API token for name.com is missing."
  29. _err "Please specify that in your environment variable."
  30. return 1
  31. fi
  32. _debug Namecom_Username "$Namecom_Username"
  33. _secure_debug Namecom_Token "$Namecom_Token"
  34. # Save them in configuration.
  35. _saveaccountconf_mutable Namecom_Username "$Namecom_Username"
  36. _saveaccountconf_mutable Namecom_Token "$Namecom_Token"
  37. # Login in using API
  38. if ! _namecom_login; then
  39. return 1
  40. fi
  41. # Find domain in domain list.
  42. if ! _namecom_get_root "$fulldomain"; then
  43. _err "Unable to find domain specified."
  44. return 1
  45. fi
  46. # Add TXT record.
  47. _namecom_addtxt_json="{\"host\":\"$_sub_domain\",\"type\":\"TXT\",\"answer\":\"$txtvalue\",\"ttl\":\"300\"}"
  48. if _namecom_rest POST "domains/$_domain/records" "$_namecom_addtxt_json"; then
  49. _retvalue=$(echo "$response" | _egrep_o "\"$_sub_domain\"")
  50. if [ "$_retvalue" ]; then
  51. _info "Successfully added TXT record, ready for validation."
  52. return 0
  53. else
  54. _err "Unable to add the DNS record."
  55. return 1
  56. fi
  57. fi
  58. }
  59. #Usage: fulldomain txtvalue
  60. #Remove the txt record after validation.
  61. dns_namecom_rm() {
  62. fulldomain=$1
  63. txtvalue=$2
  64. Namecom_Username="${Namecom_Username:-$(_readaccountconf_mutable Namecom_Username)}"
  65. Namecom_Token="${Namecom_Token:-$(_readaccountconf_mutable Namecom_Token)}"
  66. if ! _namecom_login; then
  67. return 1
  68. fi
  69. # Find domain in domain list.
  70. if ! _namecom_get_root "$fulldomain"; then
  71. _err "Unable to find domain specified."
  72. return 1
  73. fi
  74. # Get the record id.
  75. if _namecom_rest GET "domains/$_domain/records"; then
  76. _record_id=$(echo "$response" | _egrep_o "\"id\":[0-9]+,\"domainName\":\"$_domain\",\"host\":\"$_sub_domain\",\"fqdn\":\"$fulldomain.\",\"type\":\"TXT\",\"answer\":\"$txtvalue\"" | cut -d \" -f 3 | _egrep_o [0-9]+)
  77. _debug record_id "$_record_id"
  78. if [ "$_record_id" ]; then
  79. _info "Successfully retrieved the record id for ACME challenge."
  80. else
  81. _err "Unable to retrieve the record id."
  82. return 1
  83. fi
  84. fi
  85. # Remove the DNS record using record id.
  86. if _namecom_rest DELETE "domains/$_domain/records/$_record_id"; then
  87. _info "Successfully removed the TXT record."
  88. return 0
  89. else
  90. _err "Unable to delete record id."
  91. return 1
  92. fi
  93. }
  94. #################### Private functions below ##################################
  95. _namecom_rest() {
  96. method=$1
  97. param=$2
  98. data=$3
  99. export _H1="Authorization: Basic $_namecom_auth"
  100. export _H2="Content-Type: application/json"
  101. if [ "$method" != "GET" ]; then
  102. response="$(_post "$data" "$Namecom_API/$param" "" "$method")"
  103. else
  104. response="$(_get "$Namecom_API/$param")"
  105. fi
  106. if [ "$?" != "0" ]; then
  107. _err "error $param"
  108. return 1
  109. fi
  110. _debug2 response "$response"
  111. return 0
  112. }
  113. _namecom_login() {
  114. # Auth string
  115. # Name.com API v4 uses http basic auth to authenticate
  116. # need to convert the token for http auth
  117. _namecom_auth=$(printf "%s:%s" "$Namecom_Username" "$Namecom_Token" | _base64)
  118. if _namecom_rest GET "hello"; then
  119. retcode=$(echo "$response" | _egrep_o "\"username\"\:\"$Namecom_Username\"")
  120. if [ "$retcode" ]; then
  121. _info "Successfully logged in."
  122. else
  123. _err "$response"
  124. _err "Please add your ip to api whitelist"
  125. _err "Logging in failed."
  126. return 1
  127. fi
  128. fi
  129. }
  130. _namecom_get_root() {
  131. domain=$1
  132. i=2
  133. p=1
  134. if ! _namecom_rest GET "domains"; then
  135. return 1
  136. fi
  137. # Need to exclude the last field (tld)
  138. numfields=$(echo "$domain" | _egrep_o "\." | wc -l)
  139. while [ $i -le "$numfields" ]; do
  140. host=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  141. _debug host "$host"
  142. if [ -z "$host" ]; then
  143. return 1
  144. fi
  145. if _contains "$response" "$host"; then
  146. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  147. _domain="$host"
  148. return 0
  149. fi
  150. p=$i
  151. i=$(_math "$i" + 1)
  152. done
  153. return 1
  154. }