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.

139 lines
3.6 KiB

  1. #!/bin/bash
  2. #Author: Neilpang
  3. # original file dns_myapi.sh
  4. # Modified by Chris Polley to support Digital Ocean
  5. #Report Bugs here: https://github.com/Neilpang/acme.sh
  6. #
  7. #depends: doctl (https://github.com/digitalocean/doctl/) v1.5
  8. # (configured using `doctl auth init` and the acocunt's access token
  9. #
  10. ######## Public functions #####################
  11. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  12. dns_doctl_add() {
  13. fulldomain=$1
  14. txtvalue=$2
  15. _info "Using dns_doctl"
  16. _debug fulldomain "$fulldomain"
  17. _debug txtvalue "$txtvalue"
  18. # digitalocean needs the domain to act upon, so split $fulldomain into record-name and domain
  19. # "_acme-challenge" and "www.domain.com" in the above example
  20. # get list of domains authorized
  21. domains_avail=$( doctl compute domain list --no-header --format Domain | tr "$IFS" " " )
  22. _debug domains_avail "$domains_avail"
  23. if [ -z "$domains_avail" ]
  24. then
  25. _err "No domains in DigitalOcean DNS"
  26. return 1
  27. fi
  28. for d in $domains_avail
  29. do
  30. _debug trying_domain "$d"
  31. try_domain=${fulldomain##$d}
  32. try_challenge=${fulldomain%%.$d}
  33. _debug try_domain "$try_domain"
  34. _debug try_challenge "$challenge"
  35. if [ "$fulldomain" == "$try_challenge.$d" ]
  36. then
  37. _debug matches "$d"
  38. domain="$d"
  39. challenge="$try_challenge"
  40. else
  41. _debug no_match "$d"
  42. fi
  43. done
  44. if [ -z "$domain" ]
  45. then
  46. _err "Unable to locate domain of $fulldomain in DigitalOcean DNS"
  47. return 1
  48. fi
  49. record_name="$challenge"
  50. _debug domain "$domain"
  51. _debug record_name "$record_name"
  52. _debug txtvalue "$txtvalue"
  53. id_created=$( doctl compute domain records create $domain --record-data $txtvalue --record-name $record_name --record-type TXT --no-header --format ID )
  54. _debug id_created "$id_created"
  55. _info "Created record $id_created in domain $domain with name $record_name and TXT $txtvalue"
  56. if [ "" != "$id_created" ]
  57. then
  58. return 0
  59. else
  60. _err "Error creating DNS record $fulldomain"
  61. return 1
  62. fi
  63. }
  64. #Usage: fulldomain txtvalue
  65. #Remove the txt record after validation.
  66. dns_doctl_rm() {
  67. fulldomain=$1
  68. txtvalue=$2
  69. _info "Using dns_doctl"
  70. _debug fulldomain "$fulldomain"
  71. _debug txtvalue "$txtvalue"
  72. # get list of domains authorized
  73. domains_avail=$( doctl compute domain list --no-header --format Domain | tr "$IFS" " " )
  74. _debug domains_avail "$domains_avail"
  75. if [ -z "$domains_avail" ]
  76. then
  77. _err "No domains in DigitalOcean DNS"
  78. return 1
  79. fi
  80. for d in $domains_avail
  81. do
  82. _debug trying_domain "$d"
  83. try_domain=${fulldomain##$d}
  84. try_challenge=${fulldomain%%.$d}
  85. _debug try_domain "$try_domain"
  86. _debug try_challenge "$try_challenge"
  87. if [ "$fulldomain" == "$try_challenge.$d" ]
  88. then
  89. _debug matches "$d"
  90. domain="$d"
  91. challenge="$try_challenge"
  92. else
  93. _debug no_match "$d"
  94. fi
  95. done
  96. if [ -z "$domain" ]
  97. then
  98. _err "Unable to locate domain of $fulldomain in DigitalOcean DNS"
  99. return 1
  100. fi
  101. record_name="$challenge"
  102. _debug domain "$domain"
  103. _debug record_name "$record_name"
  104. _debug txtvalue "$txtvalue"
  105. record_ids=$( doctl compute domain records list $domain --no-header --format=ID,Name,Data | grep $record_name | grep $txtvalue | awk '{print $1}' | tr "$IFS" " " )
  106. _debug record_ids "$record_ids"
  107. # could be more than one; delete all matching records
  108. if [ -z "$record_ids" ]
  109. then
  110. _err "Error: Unable to locate any DNS record matching $record_name with TXT $txtvalue -- you will need to delete record\(s\) manually"
  111. #
  112. else
  113. for r in $record_ids
  114. do
  115. _info "Deleting record $r from domain $domain"
  116. doctl compute domain records delete $domain $r
  117. done
  118. fi
  119. }
  120. #################### Private functions below ##################################