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.

129 lines
4.4 KiB

4 years ago
4 years ago
4 years ago
  1. #!/usr/bin/env sh
  2. #Author StefanAbl
  3. #Usage specify a private keyfile to use with dynv6 'export KEY="path/to/keyfile"'
  4. #if no keyfile is specified, you will be asked if you want to create one in /home/$USER/.ssh/dynv6 and /home/$USER/.ssh/dynv6.pub
  5. ######## Public functions #####################
  6. # Please Read this guide first: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  7. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  8. dns_dynv6_add() {
  9. fulldomain=$1
  10. txtvalue=$2
  11. _info "Using dynv6 api"
  12. _debug fulldomain "$fulldomain"
  13. _debug txtvalue "$txtvalue"
  14. _get_keyfile
  15. _info "using keyfile $dynv6_keyfile"
  16. _your_hosts="$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts)"
  17. if ! _get_domain "$fulldomain" "$_your_hosts"; then
  18. _err "Host not found on your account"
  19. return 1
  20. fi
  21. # if ! _contains "$_your_hosts" "$_host"; then
  22. # _debug "The host is $_host and the record $_record"
  23. # _debug "Dynv6 returned $_your_hosts"
  24. # _err "The host $_host does not exists on your dynv6 account"
  25. # return 1
  26. # fi
  27. _debug "found host on your account"
  28. returnval="$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts \""$_host"\" records set \""$_record"\" txt data \""$txtvalue"\")"
  29. _debug "Dynv6 returend this after record was added: $returnval"
  30. if _contains "$returnval" "created"; then
  31. return 0
  32. elif _contains "$returnval" "updated"; then
  33. return 0
  34. else
  35. _err "Something went wrong! it does not seem like the record was added succesfully"
  36. return 1
  37. fi
  38. return 1
  39. }
  40. #Usage: fulldomain txtvalue
  41. #Remove the txt record after validation.
  42. dns_dynv6_rm() {
  43. fulldomain=$1
  44. txtvalue=$2
  45. _info "Using dynv6 api"
  46. _debug fulldomain "$fulldomain"
  47. _debug txtvalue "$txtvalue"
  48. _get_keyfile
  49. _info "using keyfile $dynv6_keyfile"
  50. _your_hosts="$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts)"
  51. if ! _get_domain "$fulldomain" "$_your_hosts"; then
  52. _err "Host not found on your account"
  53. return 1
  54. fi
  55. # if ! _contains "$_your_hosts" "$_host"; then
  56. # _debug "The host is $_host and the record $_record"
  57. # _debug "Dynv6 returned $_your_hosts"
  58. # _err "The host $_host does not exists on your dynv6 account"
  59. # return 1
  60. # fi
  61. _debug "found host on your account"
  62. _info "$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts "\"$_host\"" records del "\"$_record\"" txt)"
  63. return 0
  64. }
  65. #################### Private functions below ##################################
  66. #Usage: No Input required
  67. #returns
  68. #dynv6_keyfile the path to the new keyfile that has been generated
  69. _generate_new_key() {
  70. dynv6_keyfile="$(eval echo ~"$USER")/.ssh/dynv6"
  71. _info "Path to key file used: $dynv6_keyfile"
  72. if [ ! -f "$dynv6_keyfile" ] && [ ! -f "$dynv6_keyfile.pub" ]; then
  73. _debug "generating key in $dynv6_keyfile and $dynv6_keyfile.pub"
  74. ssh-keygen -f "$dynv6_keyfile" -t ssh-ed25519 -N ''
  75. else
  76. _err "There is already a file in $dynv6_keyfile or $dynv6_keyfile.pub"
  77. return 1
  78. fi
  79. }
  80. #Usage: _acme-challenge.www.example.dynv6.net "$_your_hosts"
  81. #where _your_hosts is the output of ssh -i ~/.ssh/dynv6.pub api@dynv6.com hosts
  82. #returns
  83. #_host= example.dynv6.net
  84. #_record=_acme-challenge.www
  85. #aborts if not a valid domain
  86. _get_domain() {
  87. #_your_hosts="$(ssh -i ~/.ssh/dynv6.pub api@dynv6.com hosts)"
  88. _full_domain="$1"
  89. _your_hosts="$2"
  90. _your_hosts="$(echo "$_your_hosts" | awk '/\./ {print $1}')"
  91. for l in $_your_hosts; do
  92. #echo "host: $l"
  93. if test "${_full_domain#*$l}" != "$_full_domain"; then
  94. _record="${_full_domain%.$l}"
  95. _host=$l
  96. _debug "The host is $_host and the record $_record"
  97. return 0
  98. fi
  99. done
  100. _err "Either their is no such host on your dnyv6 account or it cannot be accessed with this key"
  101. return 1
  102. }
  103. # Usage: No input required
  104. #returns
  105. #dynv6_keyfile path to the key that will be used
  106. _get_keyfile() {
  107. _debug "get keyfile method called"
  108. dynv6_keyfile="${dynv6_keyfile:-$(_readaccountconf_mutable dynv6_keyfile)}"
  109. _debug "Your key is $dynv6_keyfile"
  110. if [ -z "$dynv6_keyfile" ]; then
  111. if [ -z "$KEY" ]; then
  112. _err "You did not specify a key to use with dynv6"
  113. _info "Creating new dynv6 api key to add to dynv6.com"
  114. _generate_new_key
  115. _info "Please add this key to dynv6.com $(cat "$dynv6_keyfile.pub")"
  116. _info "Hit Enter to contiue"
  117. read -r _
  118. #save the credentials to the account conf file.
  119. else
  120. dynv6_keyfile="$KEY"
  121. fi
  122. _saveaccountconf_mutable dynv6_keyfile "$dynv6_keyfile"
  123. fi
  124. }