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.

193 lines
4.9 KiB

7 years ago
7 years ago
  1. #!/usr/bin/env sh
  2. #Author: RaidneII
  3. #Created 06/28/2017
  4. #Utilize name.com API to finish dns-01 verifications.
  5. ######## Public functions #####################
  6. Namecom_API="https://api.name.com/api"
  7. #Usage: dns_namecom_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  8. dns_namecom_add() {
  9. fulldomain=$1
  10. txtvalue=$2
  11. # First we need name.com credentials.
  12. if [ -z "$Namecom_Username" ]; then
  13. Namecom_Username=""
  14. _err "Username for name.com is missing."
  15. _err "Please specify that in your environment variable."
  16. return 1
  17. fi
  18. if [ -z "$Namecom_Token" ]; then
  19. Namecom_Token=""
  20. _err "API token for name.com is missing."
  21. _err "Please specify that in your environment variable."
  22. return 1
  23. fi
  24. # Save them in configuration.
  25. _saveaccountconf Namecom_Username "$Namecom_Username"
  26. _saveaccountconf Namecom_Token "$Namecom_Token"
  27. # Login in using API
  28. if ! _namecom_login; then
  29. return 1
  30. fi
  31. # Find domain in domain list.
  32. if ! _namecom_get_root "$fulldomain"; then
  33. _err "Unable to find domain specified."
  34. _namecom_logout
  35. return 1
  36. fi
  37. # Add TXT record.
  38. _namecom_addtxt_json="{\"hostname\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"ttl\":\"300\",\"priority\":\"10\"}"
  39. if _namecom_rest POST "dns/create/$_domain" "$_namecom_addtxt_json"; then
  40. retcode=$(printf "%s\n" "$response" | _egrep_o "\"code\":100")
  41. if [ "$retcode" ]; then
  42. _info "Successfully added TXT record, ready for validation."
  43. _namecom_logout
  44. return 0
  45. else
  46. _err "Unable to add the DNS record."
  47. _namecom_logout
  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. if ! _namecom_login; then
  58. return 1
  59. fi
  60. # Find domain in domain list.
  61. if ! _namecom_get_root "$fulldomain"; then
  62. _err "Unable to find domain specified."
  63. _namecom_logout
  64. return 1
  65. fi
  66. # Get the record id.
  67. if _namecom_rest GET "dns/list/$_domain"; then
  68. retcode=$(printf "%s\n" "$response" | _egrep_o "\"code\":100")
  69. if [ "$retcode" ]; then
  70. _record_id=$(printf "%s\n" "$response" | _egrep_o "\"record_id\":\"[0-9]+\",\"name\":\"$fulldomain\",\"type\":\"TXT\"" | cut -d \" -f 4)
  71. _debug record_id "$_record_id"
  72. _info "Successfully retrieved the record id for ACME challenge."
  73. else
  74. _err "Unable to retrieve the record id."
  75. _namecom_logout
  76. return 1
  77. fi
  78. fi
  79. # Remove the DNS record using record id.
  80. _namecom_rmtxt_json="{\"record_id\":\"$_record_id\"}"
  81. if _namecom_rest POST "dns/delete/$_domain" "$_namecom_rmtxt_json"; then
  82. retcode=$(printf "%s\n" "$response" | _egrep_o "\"code\":100")
  83. if [ "$retcode" ]; then
  84. _info "Successfully removed the TXT record."
  85. _namecom_logout
  86. return 0
  87. else
  88. _err "Unable to remove the DNS record."
  89. _namecom_logout
  90. return 1
  91. fi
  92. fi
  93. }
  94. #################### Private functions below ##################################
  95. _namecom_rest() {
  96. method=$1
  97. param=$2
  98. data=$3
  99. export _H1="Content-Type: application/json"
  100. export _H2="Api-Session-Token: $sessionkey"
  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. namecom_login_json="{\"username\":\"$Namecom_Username\",\"api_token\":\"$Namecom_Token\"}"
  115. if _namecom_rest POST "login" "$namecom_login_json"; then
  116. retcode=$(printf "%s\n" "$response" | _egrep_o "\"code\":100")
  117. if [ "$retcode" ]; then
  118. _info "Successfully logged in. Fetching session token..."
  119. sessionkey=$(printf "%s\n" "$response" | _egrep_o "\"session_token\":\".+" | cut -d \" -f 4)
  120. if [ ! -z "$sessionkey" ]; then
  121. _debug sessionkey "$sessionkey"
  122. _info "Session key obtained."
  123. else
  124. _err "Unable to get session key."
  125. return 1
  126. fi
  127. else
  128. _err "Logging in failed."
  129. return 1
  130. fi
  131. fi
  132. }
  133. _namecom_logout() {
  134. if _namecom_rest GET "logout"; then
  135. retcode=$(printf "%s\n" "$response" | _egrep_o "\"code\":100")
  136. if [ "$retcode" ]; then
  137. _info "Successfully logged out."
  138. else
  139. _err "Error logging out."
  140. return 1
  141. fi
  142. fi
  143. }
  144. _namecom_get_root() {
  145. domain=$1
  146. i=2
  147. p=1
  148. if ! _namecom_rest GET "domain/list"; then
  149. return 1
  150. fi
  151. # Need to exclude the last field (tld)
  152. numfields=$(echo "$domain" | _egrep_o "\." | wc -l)
  153. while [ $i -le "$numfields" ]; do
  154. host=$(printf "%s" "$domain" | cut -d . -f $i-100)
  155. _debug host "$host"
  156. if [ -z "$host" ]; then
  157. return 1
  158. fi
  159. if _contains "$response" "$host"; then
  160. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  161. _domain="$host"
  162. return 0
  163. fi
  164. p=$i
  165. i=$(_math "$i" + 1)
  166. done
  167. return 1
  168. }