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.

142 lines
3.7 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=$(echo "$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. if [ "$_record_id" ]; then
  53. _info "Successfully retrieved the record id for ACME challenge."
  54. else
  55. _info "Empty record id, it seems no such record."
  56. return 0
  57. fi
  58. else
  59. _err "Unable to retrieve the record id."
  60. return 1
  61. fi
  62. fi
  63. # Remove the DNS record using record id.
  64. if _namesilo_rest GET "dnsDeleteRecord?version=1&type=xml&key=$Namesilo_Key&domain=$_domain&rrid=$_record_id"; then
  65. retcode=$(printf "%s\n" "$response" | _egrep_o "<code>300")
  66. if [ "$retcode" ]; then
  67. _info "Successfully removed the TXT record."
  68. return 0
  69. else
  70. _err "Unable to remove the DNS record."
  71. return 1
  72. fi
  73. fi
  74. }
  75. #################### Private functions below ##################################
  76. # _acme-challenge.www.domain.com
  77. # returns
  78. # _sub_domain=_acme-challenge.www
  79. # _domain=domain.com
  80. _get_root() {
  81. domain=$1
  82. i=2
  83. p=1
  84. if ! _namesilo_rest GET "listDomains?version=1&type=xml&key=$Namesilo_Key"; then
  85. return 1
  86. fi
  87. # Need to exclude the last field (tld)
  88. numfields=$(echo "$domain" | _egrep_o "\." | wc -l)
  89. while [ $i -le "$numfields" ]; do
  90. host=$(printf "%s" "$domain" | cut -d . -f $i-100)
  91. _debug host "$host"
  92. if [ -z "$host" ]; then
  93. return 1
  94. fi
  95. if _contains "$response" "$host"; then
  96. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  97. _domain="$host"
  98. return 0
  99. fi
  100. p=$i
  101. i=$(_math "$i" + 1)
  102. done
  103. return 1
  104. }
  105. _namesilo_rest() {
  106. method=$1
  107. param=$2
  108. data=$3
  109. if [ "$method" != "GET" ]; then
  110. response="$(_post "$data" "$Namesilo_API/$param" "" "$method")"
  111. else
  112. response="$(_get "$Namesilo_API/$param")"
  113. fi
  114. if [ "$?" != "0" ]; then
  115. _err "error $param"
  116. return 1
  117. fi
  118. _debug2 response "$response"
  119. return 0
  120. }