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.

306 lines
9.6 KiB

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