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.

326 lines
10 KiB

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