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.

188 lines
5.0 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. _debug retcode "$retcode"
  40. if [ ! -z "$retcode" ]; then
  41. _info "Successfully added TXT record, ready for validation."
  42. _namecom_logout
  43. return 0
  44. else
  45. _err "Unable to add the DNS record."
  46. _namecom_logout
  47. return 1
  48. fi
  49. fi
  50. }
  51. #Usage: fulldomain txtvalue
  52. #Remove the txt record after validation.
  53. dns_namecom_rm() {
  54. fulldomain=$1
  55. txtvalue=$2
  56. _namecom_login
  57. # Find domain in domain list.
  58. if ! _namecom_get_root "$fulldomain"; then
  59. _err "Unable to find domain specified."
  60. _namecom_logout
  61. return 1
  62. fi
  63. # Get the record id.
  64. if _namecom_rest GET "dns/list/$_domain"; then
  65. retcode=$(printf "%s\n" "$response" | _egrep_o "\"code\":100")
  66. _debug retcode "$retcode"
  67. if [ ! -z "$retcode" ]; then
  68. _record_id=$(printf "%s\n" "$response" | _egrep_o "\"record_id\":\"[0-9]+\",\"name\":\"$fulldomain\",\"type\":\"TXT\"" | cut -d : -f 2 | cut -d \" -f 2)
  69. _debug record_id "$_record_id"
  70. _info "Successfully retrieved the record id for ACME challenge."
  71. else
  72. _err "Unable to retrieve the record id."
  73. _namecom_logout
  74. return 1
  75. fi
  76. fi
  77. # Remove the DNS record using record id.
  78. _namecom_rmtxt_json="{\"record_id\":\"$_record_id\"}"
  79. if _namecom_rest POST "dns/delete/$_domain" "$_namecom_rmtxt_json"; then
  80. retcode=$(printf "%s\n" "$response" | _egrep_o "\"code\":100")
  81. _debug retcode "$retcode"
  82. if [ ! -z "$retcode" ]; then
  83. _info "Successfully removed the TXT record."
  84. _namecom_logout
  85. return 0
  86. else
  87. _err "Unable to remove the DNS record."
  88. _namecom_logout
  89. return 1
  90. fi
  91. fi
  92. }
  93. #################### Private functions below ##################################
  94. _namecom_rest() {
  95. method=$1
  96. param=$2
  97. data=$3
  98. export _H1="Content-Type: application/json"
  99. export _H2="Api-Session-Token: $sessionkey"
  100. if [ "$method" != "GET" ]; then
  101. response="$(_post "$data" "$namecom_api/$param" "" "$method")"
  102. else
  103. response="$(_get "$namecom_api/$param")"
  104. fi
  105. if [ "$?" != "0" ]; then
  106. _err "error $param"
  107. return 1
  108. fi
  109. _debug response "$response"
  110. return 0
  111. }
  112. _namecom_login() {
  113. namecom_login_json="{\"username\":\"$namecom_username\",\"api_token\":\"$namecom_token\"}"
  114. if _namecom_rest POST "login" "$namecom_login_json"; then
  115. retcode=$(printf "%s\n" "$response" | _egrep_o "\"code\":100")
  116. _debug retcode "$retcode"
  117. if [ ! -z "$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 [ ! -z "$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. while true; do
  150. host=$(printf "%s" "$domain" | cut -d . -f $i-100)
  151. if [ -z "$host" ]; then
  152. return 1
  153. fi
  154. if _contains "$response" "$host"; then
  155. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  156. _domain="$host"
  157. return 0
  158. fi
  159. p=$i
  160. i=$(_math "$i" + 1)
  161. done
  162. fi
  163. return 1
  164. }