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.

167 lines
4.1 KiB

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. # First we need name.com credentials.
  9. if [ -z "$Namecom_Username" ]; then
  10. Namecom_Username=""
  11. _err "Username for name.com is missing."
  12. _err "Please specify that in your environment variable."
  13. return 1
  14. fi
  15. if [ -z "$Namecom_Token" ]; then
  16. Namecom_Token=""
  17. _err "API token for name.com is missing."
  18. _err "Please specify that in your environment variable."
  19. return 1
  20. fi
  21. # Save them in configuration.
  22. _saveaccountconf Namecom_Username "$Namecom_Username"
  23. _saveaccountconf Namecom_Token "$Namecom_Token"
  24. # Auth string
  25. # Name.com API v4 uses http basic auth to authenticate
  26. # need to convert the token for http auth
  27. _namecom_auth=$(printf "%s:%s" "$Namecom_Username" "$Namecom_Token" | base64)
  28. #Usage: dns_namecom_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  29. dns_namecom_add() {
  30. fulldomain=$1
  31. txtvalue=$2
  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=$(printf "%s\n" "$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=$1
  58. txtvalue=$2
  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=$(printf "%s\n" "$response" | _egrep_o "\"id\":[0-9]+,\"domainName\":\"$_domain\",\"host\":\"$_sub_domain\"" | 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. _debug response "$response"
  104. return 0
  105. }
  106. _namecom_login() {
  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. }