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.

285 lines
8.7 KiB

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. #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. dynv6_token="${DYNV6_TOKEN:-$(_readaccountconf_mutable dynv6_token)}"
  109. if [ "$dynv6_token" ]; then
  110. _debug "Found HTTP Token. Going to use the HTTP API and not the SSH API"
  111. if [ "$DYNV6_TOKEN" ]; then
  112. _saveaccountconf_mutable dynv6_token "$dynv6_token"
  113. fi
  114. else
  115. _debug "no HTTP token found. Looking for an SSH key"
  116. dynv6_keyfile="${dynv6_keyfile:-$(_readaccountconf_mutable dynv6_keyfile)}"
  117. _debug "Your key is $dynv6_keyfile"
  118. if [ -z "$dynv6_keyfile" ]; then
  119. if [ -z "$KEY" ]; then
  120. _err "You did not specify a key to use with dynv6"
  121. _info "Creating new dynv6 API key to add to dynv6.com"
  122. _generate_new_key
  123. _info "Please add this key to dynv6.com $(cat "$dynv6_keyfile.pub")"
  124. _info "Hit Enter to continue"
  125. read -r _
  126. #save the credentials to the account conf file.
  127. else
  128. dynv6_keyfile="$KEY"
  129. fi
  130. _saveaccountconf_mutable dynv6_keyfile "$dynv6_keyfile"
  131. fi
  132. fi
  133. }
  134. _dns_dynv6_add_http() {
  135. _debug "Got HTTP token form _get_authentication method. Going to use the HTTP API"
  136. if ! _get_zone_id "$fulldomain"; then
  137. _err "Could not find a matching zone for $fulldomain. Maybe your HTTP Token is not authorized to access the zone"
  138. return 1
  139. fi
  140. _get_zone_name "$_zone_id"
  141. record="${fulldomain%%.$_zone_name}"
  142. _set_record TXT "$record" "$txtvalue"
  143. if _contains "$response" "$txtvalue"; then
  144. _info "Successfully added record"
  145. return 0
  146. else
  147. _err "Something went wrong while adding the record"
  148. return 1
  149. fi
  150. }
  151. _dns_dynv6_rm_http() {
  152. _debug "Got HTTP token form _get_authentication method. Going to use the HTTP API"
  153. if ! _get_zone_id "$fulldomain"; then
  154. _err "Could not find a matching zone for $fulldomain. Maybe your HTTP Token is not authorized to access the zone"
  155. return 1
  156. fi
  157. _get_zone_name "$_zone_id"
  158. record="${fulldomain%%.$_zone_name}"
  159. _get_record_id "$_zone_id" "$record" "$txtvalue"
  160. _del_record "$_zone_id" "$_record_id"
  161. if [ -z "$response" ]; then
  162. _info "Successfully deleted record"
  163. return 0
  164. else
  165. _err "Something went wrong while deleting the record"
  166. return 1
  167. fi
  168. }
  169. #get the zoneid for a specifc record or zone
  170. #usage: _get_zone_id §record
  171. #where $record is the record to get the id for
  172. #returns _zone_id the id of the zone
  173. _get_zone_id() {
  174. record="$1"
  175. _debug "getting zone id for $record"
  176. _dynv6_rest GET zones
  177. zones="$(echo "$response" | tr '}' '\n' | tr ',' '\n' | grep name | sed 's/\[//g' | tr -d '{' | tr -d '"')"
  178. #echo $zones
  179. selected=""
  180. for z in $zones; do
  181. z="${z#name:}"
  182. _debug zone: "$z"
  183. if _contains "$record" "$z"; then
  184. _debug "$z found in $record"
  185. selected="$z"
  186. fi
  187. done
  188. if [ -z "$selected" ]; then
  189. _err "no zone found"
  190. return 1
  191. fi
  192. zone_id="$(echo "$response" | tr '}' '\n' | grep "$selected" | tr ',' '\n' | grep id | tr -d '"')"
  193. _zone_id="${zone_id#id:}"
  194. _debug "zone id: $_zone_id"
  195. }
  196. _get_zone_name() {
  197. _zone_id="$1"
  198. _dynv6_rest GET zones/"$_zone_id"
  199. _zone_name="$(echo "$response" | tr ',' '\n' | tr -d '{' | grep name | tr -d '"')"
  200. _zone_name="${_zone_name#name:}"
  201. }
  202. #usaage _get_record_id $zone_id $record
  203. # where zone_id is thevalue returned by _get_zone_id
  204. # and record ist in the form _acme.www for an fqdn of _acme.www.example.com
  205. # returns _record_id
  206. _get_record_id() {
  207. _zone_id="$1"
  208. record="$2"
  209. value="$3"
  210. _dynv6_rest GET "zones/$_zone_id/records"
  211. if ! _get_record_id_from_response "$response"; then
  212. _err "no such record $record found in zone $_zone_id"
  213. return 1
  214. fi
  215. }
  216. _get_record_id_from_response() {
  217. response="$1"
  218. _record_id="$(echo "$response" | tr '}' '\n' | grep "\"name\":\"$record\"" | grep "\"data\":\"$value\"" | tr ',' '\n' | grep id | tr -d '"' | tr -d 'id:')"
  219. #_record_id="${_record_id#id:}"
  220. if [ -z "$_record_id" ]; then
  221. _err "no such record: $record found in zone $_zone_id"
  222. return 1
  223. fi
  224. _debug "record id: $_record_id"
  225. return 0
  226. }
  227. #usage: _set_record TXT _acme_challenge.www longvalue 12345678
  228. #zone id is optional can also be set as vairable bevor calling this method
  229. _set_record() {
  230. type="$1"
  231. record="$2"
  232. value="$3"
  233. if [ "$4" ]; then
  234. _zone_id="$4"
  235. fi
  236. data="{\"name\": \"$record\", \"data\": \"$value\", \"type\": \"$type\"}"
  237. #data='{ "name": "acme.test.thorn.dynv6.net", "type": "A", "data": "192.168.0.1"}'
  238. echo "$data"
  239. #"{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"
  240. _dynv6_rest POST "zones/$_zone_id/records" "$data"
  241. }
  242. _del_record() {
  243. _zone_id=$1
  244. _record_id=$2
  245. _dynv6_rest DELETE zones/"$_zone_id"/records/"$_record_id"
  246. }
  247. _dynv6_rest() {
  248. m=$1 #method GET,POST,DELETE or PUT
  249. ep="$2" #the endpoint
  250. data="$3"
  251. _debug "$ep"
  252. token_trimmed=$(echo "$dynv6_token" | tr -d '"')
  253. export _H1="Authorization: Bearer $token_trimmed"
  254. export _H2="Content-Type: application/json"
  255. if [ "$m" != "GET" ]; then
  256. _debug data "$data"
  257. response="$(_post "$data" "$dynv6_api/$ep" "" "$m")"
  258. else
  259. response="$(_get "$dynv6_api/$ep")"
  260. fi
  261. if [ "$?" != "0" ]; then
  262. _err "error $ep"
  263. return 1
  264. fi
  265. _debug2 response "$response"
  266. return 0
  267. }