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.

166 lines
4.2 KiB

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