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.

327 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
  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. if [ "$DYNV6_TOKEN" ]; then
  157. _debug "Going to use the HTTP Token you specifed and saving it for futur use"
  158. _saveaccountconf_mutable dynv6_token "$DYNV6_TOKEN"
  159. dynv6_token="$DYNV6_TOKEN"
  160. elif [ "$(_readaccountconf_mutable dynv6_token)" ]; then
  161. _debug "Found a previously used HTTP token going to use that"
  162. dynv6_token="$(_readaccountconf_mutable dynv6_token)"
  163. else
  164. _debug "no HTTP token found. Looking for an SSH key"
  165. dynv6_keyfile="${dynv6_keyfile:-$(_readaccountconf_mutable dynv6_keyfile)}"
  166. _debug "Your key is $dynv6_keyfile"
  167. if [ -z "$dynv6_keyfile" ]; then
  168. if [ -z "$KEY" ]; then
  169. _err "You did not specify a key to use with dynv6"
  170. _info "Creating new dynv6 API key to add to dynv6.com"
  171. _generate_new_key
  172. _info "Please add this key to dynv6.com $(cat "$dynv6_keyfile.pub")"
  173. _info "Hit Enter to continue"
  174. read -r _
  175. #save the credentials to the account conf file.
  176. else
  177. dynv6_keyfile="$KEY"
  178. fi
  179. _saveaccountconf_mutable dynv6_keyfile "$dynv6_keyfile"
  180. fi
  181. fi
  182. }
  183. _dns_dynv6_add_http() {
  184. _debug "Got HTTP token form _get_authentication method. Going to use the HTTP API"
  185. if ! _get_zone_id "$fulldomain"; then
  186. _err "Could not find a matching zone for $fulldomain. Maybe your HTTP Token is not authorized to access the zone"
  187. return 1
  188. fi
  189. _get_zone_name "$_zone_id"
  190. record="${fulldomain%%.$_zone_name}"
  191. _set_record TXT "$record" "$txtvalue"
  192. if _contains "$response" "$txtvalue"; then
  193. _info "Successfully added record"
  194. return 0
  195. else
  196. _err "Something went wrong while adding the record"
  197. return 1
  198. fi
  199. }
  200. _dns_dynv6_rm_http() {
  201. _debug "Got HTTP token form _get_authentication method. Going to use the HTTP API"
  202. if ! _get_zone_id "$fulldomain"; then
  203. _err "Could not find a matching zone for $fulldomain. Maybe your HTTP Token is not authorized to access the zone"
  204. return 1
  205. fi
  206. _get_zone_name "$_zone_id"
  207. record="${fulldomain%%.$_zone_name}"
  208. _get_record_id "$_zone_id" "$record" "$txtvalue"
  209. _del_record "$_zone_id" "$_record_id"
  210. if [ -z "$response" ]; then
  211. _info "Successfully deleted record"
  212. return 0
  213. else
  214. _err "Something went wrong while deleting the record"
  215. return 1
  216. fi
  217. }
  218. #get the zoneid for a specifc record or zone
  219. #usage: _get_zone_id §record
  220. #where $record is the record to get the id for
  221. #returns _zone_id the id of the zone
  222. _get_zone_id() {
  223. record="$1"
  224. _debug "getting zone id for $record"
  225. _dynv6_rest GET zones
  226. zones="$(echo "$response" | tr '}' '\n' | tr ',' '\n' | grep name | sed 's/\[//g' | tr -d '{' | tr -d '"')"
  227. #echo $zones
  228. selected=""
  229. for z in $zones; do
  230. z="${z#name:}"
  231. _debug zone: "$z"
  232. if _contains "$record" "$z"; then
  233. _debug "$z found in $record"
  234. selected="$z"
  235. fi
  236. done
  237. if [ -z "$selected" ]; then
  238. _err "no zone found"
  239. return 1
  240. fi
  241. zone_id="$(echo "$response" | tr '}' '\n' | grep "$selected" | tr ',' '\n' | grep id | tr -d '"')"
  242. _zone_id="${zone_id#id:}"
  243. _debug "zone id: $_zone_id"
  244. }
  245. _get_zone_name() {
  246. _zone_id="$1"
  247. _dynv6_rest GET zones/"$_zone_id"
  248. _zone_name="$(echo "$response" | tr ',' '\n' | tr -d '{' | grep name | tr -d '"')"
  249. _zone_name="${_zone_name#name:}"
  250. }
  251. #usaage _get_record_id $zone_id $record
  252. # where zone_id is thevalue returned by _get_zone_id
  253. # and record ist in the form _acme.www for an fqdn of _acme.www.example.com
  254. # returns _record_id
  255. _get_record_id() {
  256. _zone_id="$1"
  257. record="$2"
  258. value="$3"
  259. _dynv6_rest GET "zones/$_zone_id/records"
  260. if ! _get_record_id_from_response "$response"; then
  261. _err "no such record $record found in zone $_zone_id"
  262. return 1
  263. fi
  264. }
  265. _get_record_id_from_response() {
  266. response="$1"
  267. _record_id="$(echo "$response" | tr '}' '\n' | grep "\"name\":\"$record\"" | grep "\"data\":\"$value\"" | tr ',' '\n' | grep id | tr -d '"' | tr -d 'id:')"
  268. #_record_id="${_record_id#id:}"
  269. if [ -z "$_record_id" ]; then
  270. _err "no such record: $record found in zone $_zone_id"
  271. return 1
  272. fi
  273. _debug "record id: $_record_id"
  274. return 0
  275. }
  276. #usage: _set_record TXT _acme_challenge.www longvalue 12345678
  277. #zone id is optional can also be set as vairable bevor calling this method
  278. _set_record() {
  279. type="$1"
  280. record="$2"
  281. value="$3"
  282. if [ "$4" ]; then
  283. _zone_id="$4"
  284. fi
  285. data="{\"name\": \"$record\", \"data\": \"$value\", \"type\": \"$type\"}"
  286. #data='{ "name": "acme.test.thorn.dynv6.net", "type": "A", "data": "192.168.0.1"}'
  287. echo "$data"
  288. #"{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"
  289. _dynv6_rest POST "zones/$_zone_id/records" "$data"
  290. }
  291. _del_record() {
  292. _zone_id=$1
  293. _record_id=$2
  294. _dynv6_rest DELETE zones/"$_zone_id"/records/"$_record_id"
  295. }
  296. _dynv6_rest() {
  297. m=$1 #method GET,POST,DELETE or PUT
  298. ep="$2" #the endpoint
  299. data="$3"
  300. _debug "$ep"
  301. token_trimmed=$(echo "$dynv6_token" | tr -d '"')
  302. export _H1="Authorization: Bearer $token_trimmed"
  303. export _H2="Content-Type: application/json"
  304. if [ "$m" != "GET" ]; then
  305. _debug data "$data"
  306. response="$(_post "$data" "$dynv6_api/$ep" "" "$m")"
  307. else
  308. response="$(_get "$dynv6_api/$ep")"
  309. >>>>>>> formatting
  310. fi
  311. }