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.

290 lines
8.8 KiB

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