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.

115 lines
4.0 KiB

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