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.

291 lines
8.8 KiB

4 years ago
4 years ago
4 years ago
1 month ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_dynv6_info='DynV6.com
  4. Site: DynV6.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_dynv6
  6. Options:
  7. DYNV6_TOKEN REST API token. Get from https://DynV6.com/keys
  8. OptionsAlt:
  9. KEY Path to SSH private key file. E.g. "/root/.ssh/dynv6"
  10. Issues: github.com/acmesh-official/acme.sh/issues/2702
  11. Author: StefanAbl
  12. '
  13. dynv6_api="https://dynv6.com/api/v2"
  14. ######## Public functions #####################
  15. # Please Read this guide first: https://github.com/Neilpang/acme.sh/wiki/DNS-API-Dev-Guide
  16. #Usage: dns_dynv6_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  17. dns_dynv6_add() {
  18. fulldomain="$(echo "$1" | _lower_case)"
  19. txtvalue="$2"
  20. _info "Using dynv6 api"
  21. _debug fulldomain "$fulldomain"
  22. _debug txtvalue "$txtvalue"
  23. _get_authentication
  24. if [ "$dynv6_token" ]; then
  25. _dns_dynv6_add_http
  26. return $?
  27. else
  28. _info "using key file $dynv6_keyfile"
  29. _your_hosts="$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts)"
  30. if ! _get_domain "$fulldomain" "$_your_hosts"; then
  31. _err "Host not found on your account"
  32. return 1
  33. fi
  34. _debug "found host on your account"
  35. returnval="$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts \""$_host"\" records set \""$_record"\" txt data \""$txtvalue"\")"
  36. _debug "Dynv6 returned this after record was added: $returnval"
  37. if _contains "$returnval" "created"; then
  38. return 0
  39. elif _contains "$returnval" "updated"; then
  40. return 0
  41. else
  42. _err "Something went wrong! it does not seem like the record was added successfully"
  43. return 1
  44. fi
  45. fi
  46. }
  47. #Usage: fulldomain txtvalue
  48. #Remove the txt record after validation.
  49. dns_dynv6_rm() {
  50. fulldomain="$(echo "$1" | _lower_case)"
  51. txtvalue="$2"
  52. _info "Using dynv6 API"
  53. _debug fulldomain "$fulldomain"
  54. _debug txtvalue "$txtvalue"
  55. _get_authentication
  56. if [ "$dynv6_token" ]; then
  57. _dns_dynv6_rm_http
  58. return $?
  59. else
  60. _info "using key file $dynv6_keyfile"
  61. _your_hosts="$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts)"
  62. if ! _get_domain "$fulldomain" "$_your_hosts"; then
  63. _err "Host not found on your account"
  64. return 1
  65. fi
  66. _debug "found host on your account"
  67. _info "$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts "\"$_host\"" records del "\"$_record\"" txt)"
  68. return 0
  69. fi
  70. }
  71. #################### Private functions below ##################################
  72. #Usage: No Input required
  73. #returns
  74. #dynv6_keyfile the path to the new key file that has been generated
  75. _generate_new_key() {
  76. dynv6_keyfile="$(eval echo ~"$USER")/.ssh/dynv6"
  77. _info "Path to key file used: $dynv6_keyfile"
  78. if [ ! -f "$dynv6_keyfile" ] && [ ! -f "$dynv6_keyfile.pub" ]; then
  79. _debug "generating key in $dynv6_keyfile and $dynv6_keyfile.pub"
  80. ssh-keygen -f "$dynv6_keyfile" -t ssh-ed25519 -N ''
  81. else
  82. _err "There is already a file in $dynv6_keyfile or $dynv6_keyfile.pub"
  83. return 1
  84. fi
  85. }
  86. #Usage: _acme-challenge.www.example.dynv6.net "$_your_hosts"
  87. #where _your_hosts is the output of ssh -i ~/.ssh/dynv6.pub api@dynv6.com hosts
  88. #returns
  89. #_host= example.dynv6.net
  90. #_record=_acme-challenge.www
  91. #aborts if not a valid domain
  92. _get_domain() {
  93. #_your_hosts="$(ssh -i ~/.ssh/dynv6.pub api@dynv6.com hosts)"
  94. _full_domain="$1"
  95. _your_hosts="$2"
  96. _your_hosts="$(echo "$_your_hosts" | awk '/\./ {print $1}')"
  97. for l in $_your_hosts; do
  98. #echo "host: $l"
  99. if test "${_full_domain#*"$l"}" != "$_full_domain"; then
  100. _record=${_full_domain%."$l"}
  101. _host=$l
  102. _debug "The host is $_host and the record $_record"
  103. return 0
  104. fi
  105. done
  106. _err "Either their is no such host on your dnyv6 account or it cannot be accessed with this key"
  107. return 1
  108. }
  109. # Usage: No input required
  110. #returns
  111. #dynv6_keyfile path to the key that will be used
  112. _get_authentication() {
  113. dynv6_token="${DYNV6_TOKEN:-$(_readaccountconf_mutable dynv6_token)}"
  114. if [ "$dynv6_token" ]; then
  115. _debug "Found HTTP Token. Going to use the HTTP API and not the SSH API"
  116. if [ "$DYNV6_TOKEN" ]; then
  117. _saveaccountconf_mutable dynv6_token "$dynv6_token"
  118. fi
  119. else
  120. _debug "no HTTP token found. Looking for an SSH key"
  121. dynv6_keyfile="${dynv6_keyfile:-$(_readaccountconf_mutable dynv6_keyfile)}"
  122. _debug "Your key is $dynv6_keyfile"
  123. if [ -z "$dynv6_keyfile" ]; then
  124. if [ -z "$KEY" ]; then
  125. _err "You did not specify a key to use with dynv6"
  126. _info "Creating new dynv6 API key to add to dynv6.com"
  127. _generate_new_key
  128. _info "Please add this key to dynv6.com $(cat "$dynv6_keyfile.pub")"
  129. _info "Hit Enter to continue"
  130. read -r _
  131. #save the credentials to the account conf file.
  132. else
  133. dynv6_keyfile="$KEY"
  134. fi
  135. _saveaccountconf_mutable dynv6_keyfile "$dynv6_keyfile"
  136. fi
  137. fi
  138. }
  139. _dns_dynv6_add_http() {
  140. _debug "Got HTTP token form _get_authentication method. Going to use the HTTP API"
  141. if ! _get_zone_id "$fulldomain"; then
  142. _err "Could not find a matching zone for $fulldomain. Maybe your HTTP Token is not authorized to access the zone"
  143. return 1
  144. fi
  145. _get_zone_name "$_zone_id"
  146. record=${fulldomain%%."$_zone_name"}
  147. _set_record TXT "$record" "$txtvalue"
  148. if _contains "$response" "$txtvalue"; then
  149. _info "Successfully added record"
  150. return 0
  151. else
  152. _err "Something went wrong while adding the record"
  153. return 1
  154. fi
  155. }
  156. _dns_dynv6_rm_http() {
  157. _debug "Got HTTP token form _get_authentication method. Going to use the HTTP API"
  158. if ! _get_zone_id "$fulldomain"; then
  159. _err "Could not find a matching zone for $fulldomain. Maybe your HTTP Token is not authorized to access the zone"
  160. return 1
  161. fi
  162. _get_zone_name "$_zone_id"
  163. record=${fulldomain%%."$_zone_name"}
  164. _get_record_id "$_zone_id" "$record" "$txtvalue"
  165. _del_record "$_zone_id" "$_record_id"
  166. if [ -z "$response" ]; then
  167. _info "Successfully deleted record"
  168. return 0
  169. else
  170. _err "Something went wrong while deleting the record"
  171. return 1
  172. fi
  173. }
  174. #get the zoneid for a specifc record or zone
  175. #usage: _get_zone_id §record
  176. #where $record is the record to get the id for
  177. #returns _zone_id the id of the zone
  178. _get_zone_id() {
  179. record="$1"
  180. _debug "getting zone id for $record"
  181. _dynv6_rest GET zones
  182. zones="$(echo "$response" | tr '}' '\n' | tr ',' '\n' | grep name | sed 's/\[//g' | tr -d '{' | tr -d '"')"
  183. #echo $zones
  184. selected=""
  185. for z in $zones; do
  186. z="${z#name:}"
  187. _debug zone: "$z"
  188. if _contains "$record" "$z"; then
  189. _debug "$z found in $record"
  190. selected="$z"
  191. fi
  192. done
  193. if [ -z "$selected" ]; then
  194. _err "no zone found"
  195. return 1
  196. fi
  197. zone_id="$(echo "$response" | tr '}' '\n' | grep "$selected" | tr ',' '\n' | grep '"id":' | tr -d '"')"
  198. _zone_id="${zone_id#id:}"
  199. _debug "zone id: $_zone_id"
  200. }
  201. _get_zone_name() {
  202. _zone_id="$1"
  203. _dynv6_rest GET zones/"$_zone_id"
  204. _zone_name="$(echo "$response" | tr ',' '\n' | tr -d '{' | grep name | tr -d '"')"
  205. _zone_name="${_zone_name#name:}"
  206. }
  207. #usaage _get_record_id $zone_id $record
  208. # where zone_id is thevalue returned by _get_zone_id
  209. # and record ist in the form _acme.www for an fqdn of _acme.www.example.com
  210. # returns _record_id
  211. _get_record_id() {
  212. _zone_id="$1"
  213. record="$2"
  214. value="$3"
  215. _dynv6_rest GET "zones/$_zone_id/records"
  216. if ! _get_record_id_from_response "$response"; then
  217. _err "no such record $record found in zone $_zone_id"
  218. return 1
  219. fi
  220. }
  221. _get_record_id_from_response() {
  222. response="$1"
  223. _record_id="$(echo "$response" | tr '}' '\n' | grep "\"name\":\"$record\"" | grep "\"data\":\"$value\"" | tr ',' '\n' | grep id | tr -d '"' | tr -d 'id:')"
  224. #_record_id="${_record_id#id:}"
  225. if [ -z "$_record_id" ]; then
  226. _err "no such record: $record found in zone $_zone_id"
  227. return 1
  228. fi
  229. _debug "record id: $_record_id"
  230. return 0
  231. }
  232. #usage: _set_record TXT _acme_challenge.www longvalue 12345678
  233. #zone id is optional can also be set as vairable bevor calling this method
  234. _set_record() {
  235. type="$1"
  236. record="$2"
  237. value="$3"
  238. if [ "$4" ]; then
  239. _zone_id="$4"
  240. fi
  241. data="{\"name\": \"$record\", \"data\": \"$value\", \"type\": \"$type\"}"
  242. #data='{ "name": "acme.test.thorn.dynv6.net", "type": "A", "data": "192.168.0.1"}'
  243. echo "$data"
  244. #"{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"
  245. _dynv6_rest POST "zones/$_zone_id/records" "$data"
  246. }
  247. _del_record() {
  248. _zone_id=$1
  249. _record_id=$2
  250. _dynv6_rest DELETE zones/"$_zone_id"/records/"$_record_id"
  251. }
  252. _dynv6_rest() {
  253. m=$1 #method GET,POST,DELETE or PUT
  254. ep="$2" #the endpoint
  255. data="$3"
  256. _debug "$ep"
  257. token_trimmed=$(echo "$dynv6_token" | tr -d '"')
  258. export _H1="Authorization: Bearer $token_trimmed"
  259. export _H2="Content-Type: application/json"
  260. if [ "$m" != "GET" ]; then
  261. _debug data "$data"
  262. response="$(_post "$data" "$dynv6_api/$ep" "" "$m")"
  263. else
  264. response="$(_get "$dynv6_api/$ep")"
  265. fi
  266. if [ "$?" != "0" ]; then
  267. _err "error $ep"
  268. return 1
  269. fi
  270. _debug2 response "$response"
  271. return 0
  272. }