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.

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