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.

137 lines
3.5 KiB

  1. #!/usr/bin/env sh
  2. #Author: meowthink
  3. #Created 01/14/2017
  4. #Utilize namesilo.com API to finish dns-01 verifications.
  5. Namesilo_API="https://www.namesilo.com/api"
  6. ######## Public functions #####################
  7. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  8. dns_namesilo_add() {
  9. fulldomain=$1
  10. txtvalue=$2
  11. if [ -z "$Namesilo_Key" ]; then
  12. Namesilo_Key=""
  13. _err "API token for namesilo.com is missing."
  14. _err "Please specify that in your environment variable."
  15. return 1
  16. fi
  17. #save the api key and email to the account conf file.
  18. _saveaccountconf Namesilo_Key "$Namesilo_Key"
  19. if ! _get_root "$fulldomain"; then
  20. _err "Unable to find domain specified."
  21. return 1
  22. fi
  23. _debug _sub_domain "$_sub_domain"
  24. _debug _domain "$_domain"
  25. _debug txtvalue "$txtvalue"
  26. if _namesilo_rest GET "dnsAddRecord?version=1&type=xml&key=$Namesilo_Key&domain=$_domain&rrtype=TXT&rrhost=$_sub_domain&rrvalue=$txtvalue"; then
  27. retcode=$(printf "%s\n" "$response" | _egrep_o "<code>300")
  28. if [ "$retcode" ]; then
  29. _info "Successfully added TXT record, ready for validation."
  30. return 0
  31. else
  32. _err "Unable to add the DNS record."
  33. return 1
  34. fi
  35. fi
  36. }
  37. #Usage: fulldomain txtvalue
  38. #Remove the txt record after validation.
  39. dns_namesilo_rm() {
  40. fulldomain=$1
  41. txtvalue=$2
  42. if ! _get_root "$fulldomain"; then
  43. _err "Unable to find domain specified."
  44. return 1
  45. fi
  46. # Get the record id.
  47. if _namesilo_rest GET "dnsListRecords?version=1&type=xml&key=$Namesilo_Key&domain=$_domain"; then
  48. retcode=$(printf "%s\n" "$response" | _egrep_o "<code>300")
  49. if [ "$retcode" ]; then
  50. _record_id=$(printf "%s\n" "$response" | _egrep_o "<record_id>([^<]*)</record_id><type>TXT</type><host>$fulldomain</host>" | _egrep_o "<record_id>([^<]*)</record_id>" | sed -r "s/<record_id>([^<]*)<\/record_id>/\1/" | tail -n 1)
  51. _debug record_id "$_record_id"
  52. _info "Successfully retrieved the record id for ACME challenge."
  53. else
  54. _err "Unable to retrieve the record id."
  55. return 1
  56. fi
  57. fi
  58. # Remove the DNS record using record id.
  59. if _namesilo_rest GET "dnsDeleteRecord?version=1&type=xml&key=$Namesilo_Key&domain=$_domain&rrid=$_record_id"; then
  60. retcode=$(printf "%s\n" "$response" | _egrep_o "<code>300")
  61. if [ "$retcode" ]; then
  62. _info "Successfully removed the TXT record."
  63. return 0
  64. else
  65. _err "Unable to remove the DNS record."
  66. return 1
  67. fi
  68. fi
  69. }
  70. #################### Private functions below ##################################
  71. # _acme-challenge.www.domain.com
  72. # returns
  73. # _sub_domain=_acme-challenge.www
  74. # _domain=domain.com
  75. _get_root() {
  76. domain=$1
  77. i=2
  78. p=1
  79. if ! _namesilo_rest GET "listDomains?version=1&type=xml&key=$Namesilo_Key"; then
  80. return 1
  81. fi
  82. # Need to exclude the last field (tld)
  83. numfields=$(echo "$domain" | _egrep_o "\." | wc -l)
  84. while [ $i -le "$numfields" ]; do
  85. host=$(printf "%s" "$domain" | cut -d . -f $i-100)
  86. _debug host "$host"
  87. if [ -z "$host" ]; then
  88. return 1
  89. fi
  90. if _contains "$response" "$host"; then
  91. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  92. _domain="$host"
  93. return 0
  94. fi
  95. p=$i
  96. i=$(_math "$i" + 1)
  97. done
  98. return 1
  99. }
  100. _namesilo_rest() {
  101. method=$1
  102. param=$2
  103. data=$3
  104. if [ "$method" != "GET" ]; then
  105. response="$(_post "$data" "$Namesilo_API/$param" "" "$method")"
  106. else
  107. response="$(_get "$Namesilo_API/$param")"
  108. fi
  109. if [ "$?" != "0" ]; then
  110. _err "error $param"
  111. return 1
  112. fi
  113. _debug2 response "$response"
  114. return 0
  115. }