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.

184 lines
4.8 KiB

  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. _namecom_login
  29. # Find domain in domain list.
  30. if ! _namecom_get_root "$fulldomain"; then
  31. _err "Unable to find domain specified."
  32. _namecom_logout
  33. return 1
  34. fi
  35. # Add TXT record.
  36. _namecom_addtxt_json="{\"hostname\":\"$_sub_domain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"ttl\":\"300\",\"priority\":\"10\"}"
  37. if _namecom_rest POST "dns/create/$_domain" "$_namecom_addtxt_json"; then
  38. retcode=$(printf "%s\n" "$response" | _egrep_o "\"code\":100")
  39. if [ ! -z "$retcode" ]; then
  40. _info "Successfully added TXT record, ready for validation."
  41. _namecom_logout
  42. return 0
  43. else
  44. _err "Unable to add the DNS record."
  45. _namecom_logout
  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. _namecom_login
  56. # Find domain in domain list.
  57. if ! _namecom_get_root "$fulldomain"; then
  58. _err "Unable to find domain specified."
  59. _namecom_logout
  60. return 1
  61. fi
  62. # Get the record id.
  63. if _namecom_rest GET "dns/list/$_domain"; then
  64. retcode=$(printf "%s\n" "$response" | _egrep_o "\"code\":100")
  65. if [ ! -z "$retcode" ]; then
  66. _record_id=$(printf "%s\n" "$response" | _egrep_o "\"record_id\":\"[0-9]+\",\"name\":\"$fulldomain\",\"type\":\"TXT\"" | cut -d : -f 2 | cut -d \" -f 2)
  67. _debug record_id "$_record_id"
  68. _info "Successfully retrieved the record id for ACME challenge."
  69. else
  70. _err "Unable to retrieve the record id."
  71. _namecom_logout
  72. return 1
  73. fi
  74. fi
  75. # Remove the DNS record using record id.
  76. _namecom_rmtxt_json="{\"record_id\":\"$_record_id\"}"
  77. if _namecom_rest POST "dns/delete/$_domain" "$_namecom_rmtxt_json"; then
  78. retcode=$(printf "%s\n" "$response" | _egrep_o "\"code\":100")
  79. if [ ! -z "$retcode" ]; then
  80. _info "Successfully removed the TXT record."
  81. _namecom_logout
  82. return 0
  83. else
  84. _err "Unable to remove the DNS record."
  85. _namecom_logout
  86. return 1
  87. fi
  88. fi
  89. }
  90. #################### Private functions below ##################################
  91. _namecom_rest() {
  92. method=$1
  93. param=$2
  94. data=$3
  95. export _H1="Content-Type: application/json"
  96. export _H2="Api-Session-Token: $sessionkey"
  97. if [ "$method" != "GET" ]; then
  98. response="$(_post "$data" "$namecom_api/$param" "" "$method")"
  99. else
  100. response="$(_get "$namecom_api/$param")"
  101. fi
  102. if [ "$?" != "0" ]; then
  103. _err "error $param"
  104. return 1
  105. fi
  106. _debug response "$response"
  107. return 0
  108. }
  109. _namecom_login() {
  110. namecom_login_json="{\"username\":\"$namecom_username\",\"api_token\":\"$namecom_token\"}"
  111. if _namecom_rest POST "login" "$namecom_login_json"; then
  112. retcode=$(printf "%s\n" "$response" | _egrep_o "\"code\":100")
  113. if [ ! -z "$retcode" ]; then
  114. _info "Successfully logged in. Fetching session token..."
  115. sessionkey=$(printf "%s\n" "$response" | _egrep_o "\"session_token\":\".+" | cut -d \" -f 4)
  116. if [ ! -z "$sessionkey" ]; then
  117. _debug sessionkey "$sessionkey"
  118. _info "Session key obtained."
  119. else
  120. _err "Unable to get session key."
  121. return 1
  122. fi
  123. else
  124. _err "Logging in failed."
  125. return 1
  126. fi
  127. fi
  128. }
  129. _namecom_logout() {
  130. if _namecom_rest GET "logout"; then
  131. retcode=$(printf "%s\n" "$response" | _egrep_o "\"code\":100")
  132. if [ ! -z "$retcode" ]; then
  133. _info "Successfully logged out."
  134. else
  135. _err "Error logging out."
  136. return 1
  137. fi
  138. fi
  139. }
  140. _namecom_get_root() {
  141. domain=$1
  142. i=2
  143. p=1
  144. if _namecom_rest GET "domain/list"; then
  145. while true; do
  146. host=$(printf "%s" "$domain" | cut -d . -f $i-100)
  147. if [ -z "$host" ]; then
  148. return 1
  149. fi
  150. if _contains "$response" "$host"; then
  151. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  152. _domain="$host"
  153. return 0
  154. fi
  155. p=$i
  156. i=$(_math "$i" + 1)
  157. done
  158. fi
  159. return 1
  160. }