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.

172 lines
4.6 KiB

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