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.

495 lines
15 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
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. #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. <<<<<<< HEAD
  17. <<<<<<< HEAD
  18. =======
  19. >>>>>>> no supporting HTTP API as well
  20. _get_authentication
  21. if [ "$dynv6_token" ]; then
  22. _dns_dynv6_add_http
  23. return $?
  24. <<<<<<< HEAD
  25. =======
  26. _get_keyfile
  27. _info "using keyfile $dynv6_keyfile"
  28. _your_hosts="$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts)"
  29. if ! _get_domain "$fulldomain" "$_your_hosts"; then
  30. _err "Host not found on your account"
  31. return 1
  32. fi
  33. _debug "found host on your account"
  34. returnval="$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts \""$_host"\" records set \""$_record"\" txt data \""$txtvalue"\")"
  35. _debug "Dynv6 returend this after record was added: $returnval"
  36. if _contains "$returnval" "created"; then
  37. return 0
  38. elif _contains "$returnval" "updated"; then
  39. return 0
  40. >>>>>>> first attempt to make travis happy
  41. else
  42. =======
  43. else
  44. >>>>>>> no supporting HTTP API as well
  45. _info "using key file $dynv6_keyfile"
  46. _your_hosts="$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts)"
  47. if ! _get_domain "$fulldomain" "$_your_hosts"; then
  48. _err "Host not found on your account"
  49. return 1
  50. fi
  51. _debug "found host on your account"
  52. returnval="$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts \""$_host"\" records set \""$_record"\" txt data \""$txtvalue"\")"
  53. _debug "Dynv6 returned this after record was added: $returnval"
  54. if _contains "$returnval" "created"; then
  55. return 0
  56. elif _contains "$returnval" "updated"; then
  57. return 0
  58. else
  59. _err "Something went wrong! it does not seem like the record was added successfully"
  60. return 1
  61. fi
  62. return 1
  63. fi
  64. return 1
  65. }
  66. #Usage: fulldomain txtvalue
  67. #Remove the txt record after validation.
  68. dns_dynv6_rm() {
  69. fulldomain=$1
  70. txtvalue=$2
  71. _info "Using dynv6 API"
  72. _debug fulldomain "$fulldomain"
  73. _debug txtvalue "$txtvalue"
  74. <<<<<<< HEAD
  75. <<<<<<< HEAD
  76. _get_authentication
  77. if [ "$dynv6_token" ]; then
  78. _dns_dynv6_rm_http
  79. return $?
  80. else
  81. =======
  82. _get_authentication
  83. if [ "$dynv6_token" ]; then
  84. _dns_dynv6_rm_http
  85. return $?
  86. else
  87. >>>>>>> no supporting HTTP API as well
  88. _info "using key file $dynv6_keyfile"
  89. _your_hosts="$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts)"
  90. if ! _get_domain "$fulldomain" "$_your_hosts"; then
  91. _err "Host not found on your account"
  92. return 1
  93. fi
  94. _debug "found host on your account"
  95. _info "$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts "\"$_host\"" records del "\"$_record\"" txt)"
  96. return 0
  97. <<<<<<< HEAD
  98. fi
  99. =======
  100. _get_keyfile
  101. _info "using keyfile $dynv6_keyfile"
  102. _your_hosts="$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts)"
  103. if ! _get_domain "$fulldomain" "$_your_hosts"; then
  104. _err "Host not found on your account"
  105. return 1
  106. fi
  107. _debug "found host on your account"
  108. _info "$(ssh -i "$dynv6_keyfile" api@dynv6.com hosts "\"$_host\"" records del "\"$_record\"" txt)"
  109. return 0
  110. >>>>>>> first attempt to make travis happy
  111. =======
  112. fi
  113. >>>>>>> no supporting HTTP API as well
  114. }
  115. #################### Private functions below ##################################
  116. #Usage: No Input required
  117. #returns
  118. #dynv6_keyfile the path to the new key file that has been generated
  119. _generate_new_key() {
  120. dynv6_keyfile="$(eval echo ~"$USER")/.ssh/dynv6"
  121. _info "Path to key file used: $dynv6_keyfile"
  122. if [ ! -f "$dynv6_keyfile" ] && [ ! -f "$dynv6_keyfile.pub" ]; then
  123. _debug "generating key in $dynv6_keyfile and $dynv6_keyfile.pub"
  124. ssh-keygen -f "$dynv6_keyfile" -t ssh-ed25519 -N ''
  125. else
  126. _err "There is already a file in $dynv6_keyfile or $dynv6_keyfile.pub"
  127. return 1
  128. fi
  129. }
  130. #Usage: _acme-challenge.www.example.dynv6.net "$_your_hosts"
  131. #where _your_hosts is the output of ssh -i ~/.ssh/dynv6.pub api@dynv6.com hosts
  132. #returns
  133. #_host= example.dynv6.net
  134. #_record=_acme-challenge.www
  135. #aborts if not a valid domain
  136. _get_domain() {
  137. #_your_hosts="$(ssh -i ~/.ssh/dynv6.pub api@dynv6.com hosts)"
  138. _full_domain="$1"
  139. _your_hosts="$2"
  140. _your_hosts="$(echo "$_your_hosts" | awk '/\./ {print $1}')"
  141. for l in $_your_hosts; do
  142. #echo "host: $l"
  143. if test "${_full_domain#*$l}" != "$_full_domain"; then
  144. _record="${_full_domain%.$l}"
  145. _host=$l
  146. _debug "The host is $_host and the record $_record"
  147. return 0
  148. fi
  149. done
  150. _err "Either their is no such host on your dnyv6 account or it cannot be accessed with this key"
  151. return 1
  152. }
  153. # Usage: No input required
  154. #returns
  155. #dynv6_keyfile path to the key that will be used
  156. _get_authentication() {
  157. <<<<<<< HEAD
  158. dynv6_token="${DYNV6_TOKEN:-$(_readaccountconf_mutable dynv6_token)}"
  159. if [ "$dynv6_token" ]; then
  160. _debug "Found HTTP Token. Going to use the HTTP API and not the SSH API"
  161. if [ "$DYNV6_TOKEN" ]; then
  162. _saveaccountconf_mutable dynv6_token "$dynv6_token"
  163. fi
  164. else
  165. =======
  166. if [ "$DYNV6_TOKEN" ]; then
  167. _debug "Going to use the HTTP Token you specifed and saving it for futur use"
  168. _saveaccountconf_mutable dynv6_token "$DYNV6_TOKEN"
  169. dynv6_token="$DYNV6_TOKEN"
  170. elif [ "$(_readaccountconf_mutable dynv6_token)" ]; then
  171. _debug "Found a previously used HTTP token going to use that"
  172. dynv6_token="$(_readaccountconf_mutable dynv6_token)"
  173. else
  174. >>>>>>> no supporting HTTP API as well
  175. _debug "no HTTP token found. Looking for an SSH key"
  176. dynv6_keyfile="${dynv6_keyfile:-$(_readaccountconf_mutable dynv6_keyfile)}"
  177. _debug "Your key is $dynv6_keyfile"
  178. if [ -z "$dynv6_keyfile" ]; then
  179. if [ -z "$KEY" ]; then
  180. _err "You did not specify a key to use with dynv6"
  181. _info "Creating new dynv6 API key to add to dynv6.com"
  182. _generate_new_key
  183. _info "Please add this key to dynv6.com $(cat "$dynv6_keyfile.pub")"
  184. _info "Hit Enter to continue"
  185. read -r _
  186. #save the credentials to the account conf file.
  187. else
  188. dynv6_keyfile="$KEY"
  189. fi
  190. _saveaccountconf_mutable dynv6_keyfile "$dynv6_keyfile"
  191. <<<<<<< HEAD
  192. fi
  193. fi
  194. }
  195. _dns_dynv6_add_http() {
  196. _debug "Got HTTP token form _get_authentication method. Going to use the HTTP API"
  197. if ! _get_zone_id "$fulldomain"; then
  198. _err "Could not find a matching zone for $fulldomain. Maybe your HTTP Token is not authorized to access the zone"
  199. return 1
  200. fi
  201. _get_zone_name "$_zone_id"
  202. record="${fulldomain%%.$_zone_name}"
  203. _set_record TXT "$record" "$txtvalue"
  204. if _contains "$response" "$txtvalue"; then
  205. _info "Successfully added record"
  206. return 0
  207. else
  208. _err "Something went wrong while adding the record"
  209. return 1
  210. fi
  211. }
  212. _dns_dynv6_rm_http() {
  213. _debug "Got HTTP token form _get_authentication method. Going to use the HTTP API"
  214. if ! _get_zone_id "$fulldomain"; then
  215. _err "Could not find a matching zone for $fulldomain. Maybe your HTTP Token is not authorized to access the zone"
  216. return 1
  217. fi
  218. _get_zone_name "$_zone_id"
  219. record="${fulldomain%%.$_zone_name}"
  220. _get_record_id "$_zone_id" "$record" "$txtvalue"
  221. _del_record "$_zone_id" "$_record_id"
  222. if [ -z "$response" ]; then
  223. _info "Successfully deleted record"
  224. return 0
  225. else
  226. _err "Something went wrong while deleting the record"
  227. return 1
  228. fi
  229. }
  230. #get the zoneid for a specifc record or zone
  231. #usage: _get_zone_id §record
  232. #where $record is the record to get the id for
  233. #returns _zone_id the id of the zone
  234. _get_zone_id() {
  235. record="$1"
  236. _debug "getting zone id for $record"
  237. _dynv6_rest GET zones
  238. zones="$(echo "$response" | tr '}' '\n' | tr ',' '\n' | grep name | sed 's/\[//g' | tr -d '{' | tr -d '"')"
  239. #echo $zones
  240. selected=""
  241. for z in $zones; do
  242. z="${z#name:}"
  243. _debug zone: "$z"
  244. if _contains "$record" "$z"; then
  245. _debug "$z found in $record"
  246. selected="$z"
  247. fi
  248. done
  249. if [ -z "$selected" ]; then
  250. _err "no zone found"
  251. return 1
  252. fi
  253. zone_id="$(echo "$response" | tr '}' '\n' | grep "$selected" | tr ',' '\n' | grep id | tr -d '"')"
  254. _zone_id="${zone_id#id:}"
  255. _debug "zone id: $_zone_id"
  256. }
  257. _get_zone_name() {
  258. _zone_id="$1"
  259. _dynv6_rest GET zones/"$_zone_id"
  260. _zone_name="$(echo "$response" | tr ',' '\n' | tr -d '{' | grep name | tr -d '"')"
  261. _zone_name="${_zone_name#name:}"
  262. }
  263. #usaage _get_record_id $zone_id $record
  264. # where zone_id is thevalue returned by _get_zone_id
  265. # and record ist in the form _acme.www for an fqdn of _acme.www.example.com
  266. # returns _record_id
  267. _get_record_id() {
  268. _zone_id="$1"
  269. record="$2"
  270. value="$3"
  271. _dynv6_rest GET "zones/$_zone_id/records"
  272. if ! _get_record_id_from_response "$response"; then
  273. _err "no such record $record found in zone $_zone_id"
  274. return 1
  275. fi
  276. }
  277. _get_record_id_from_response() {
  278. response="$1"
  279. _record_id="$(echo "$response" | tr '}' '\n' | grep "\"name\":\"$record\"" | grep "\"data\":\"$value\"" | tr ',' '\n' | grep id | tr -d '"' | tr -d 'id:')"
  280. #_record_id="${_record_id#id:}"
  281. if [ -z "$_record_id" ]; then
  282. _err "no such record: $record found in zone $_zone_id"
  283. return 1
  284. fi
  285. _debug "record id: $_record_id"
  286. return 0
  287. }
  288. #usage: _set_record TXT _acme_challenge.www longvalue 12345678
  289. #zone id is optional can also be set as vairable bevor calling this method
  290. _set_record() {
  291. type="$1"
  292. record="$2"
  293. value="$3"
  294. if [ "$4" ]; then
  295. _zone_id="$4"
  296. fi
  297. data="{\"name\": \"$record\", \"data\": \"$value\", \"type\": \"$type\"}"
  298. #data='{ "name": "acme.test.thorn.dynv6.net", "type": "A", "data": "192.168.0.1"}'
  299. echo "$data"
  300. #"{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"
  301. _dynv6_rest POST "zones/$_zone_id/records" "$data"
  302. }
  303. _del_record() {
  304. _zone_id=$1
  305. _record_id=$2
  306. _dynv6_rest DELETE zones/"$_zone_id"/records/"$_record_id"
  307. }
  308. _dynv6_rest() {
  309. m=$1 #method GET,POST,DELETE or PUT
  310. ep="$2" #the endpoint
  311. data="$3"
  312. _debug "$ep"
  313. token_trimmed=$(echo "$dynv6_token" | tr -d '"')
  314. export _H1="Authorization: Bearer $token_trimmed"
  315. export _H2="Content-Type: application/json"
  316. if [ "$m" != "GET" ]; then
  317. _debug data "$data"
  318. response="$(_post "$data" "$dynv6_api/$ep" "" "$m")"
  319. else
  320. response="$(_get "$dynv6_api/$ep")"
  321. =======
  322. fi
  323. >>>>>>> no supporting HTTP API as well
  324. fi
  325. }
  326. _dns_dynv6_add_http(){
  327. _debug "Got HTTP token form _get_authentication method. Going to use the HTTP API"
  328. if ! _get_zone_id "$fulldomain" ;then
  329. _err "Could not find a matching zone for $fulldomain. Maybe your HTTP Token is not authorized to access the zone"
  330. return 1
  331. fi
  332. _get_zone_name "$_zone_id"
  333. record="${fulldomain%%.$_zone_name}"
  334. _set_record TXT "$record" "$txtvalue"
  335. if _contains "$response" "$txtvalue"; then
  336. _info "Successfully added record"
  337. return 0
  338. else
  339. _err "Something went wrong while adding the record"
  340. return 1
  341. fi
  342. }
  343. _dns_dynv6_rm_http(){
  344. _debug "Got HTTP token form _get_authentication method. Going to use the HTTP API"
  345. if ! _get_zone_id "$fulldomain" ;then
  346. _err "Could not find a matching zone for $fulldomain. Maybe your HTTP Token is not authorized to access the zone"
  347. return 1
  348. fi
  349. _get_zone_name "$_zone_id"
  350. record="${fulldomain%%.$_zone_name}"
  351. _get_record_id "$_zone_id" "$record" "$txtvalue"
  352. _del_record "$_zone_id" "$_record_id"
  353. if [ -z "$response" ] ; then
  354. _info "Successfully deleted record"
  355. return 0
  356. else
  357. _err "Something went wrong while deleting the record"
  358. return 1
  359. fi
  360. }
  361. #get the zoneid for a specifc record or zone
  362. #usage: _get_zone_id §record
  363. #where $record is the record to get the id for
  364. #returns _zone_id the id of the zone
  365. _get_zone_id(){
  366. record="$1"
  367. _debug "getting zone id for $record"
  368. _dynv6_rest GET zones
  369. zones="$(echo "$response" | tr '}' '\n' | tr ',' '\n' | grep name | sed 's/\[//g' | tr -d '{' | tr -d '"')"
  370. #echo $zones
  371. selected=""
  372. for z in $zones; do
  373. z="${z#name:}"
  374. _debug zone: "$z"
  375. if _contains "$record" "$z"; then
  376. _debug "$z found in $record"
  377. selected="$z"
  378. fi
  379. done
  380. if [ -z "$selected" ]; then
  381. _err "no zone found"
  382. return 1
  383. fi
  384. zone_id="$(echo "$response" | tr '}' '\n' | grep "$selected" | tr ',' '\n' | grep id | tr -d '"')"
  385. _zone_id="${zone_id#id:}"
  386. _debug "zone id: $_zone_id"
  387. }
  388. _get_zone_name(){
  389. _zone_id="$1"
  390. _dynv6_rest GET zones/"$_zone_id"
  391. _zone_name="$(echo "$response" | tr ',' '\n'| tr -d '{'|grep name|tr -d '"')"
  392. _zone_name="${_zone_name#name:}"
  393. }
  394. #usaage _get_record_id $zone_id $record
  395. # where zone_id is thevalue returned by _get_zone_id
  396. # and record ist in the form _acme.www for an fqdn of _acme.www.example.com
  397. # returns _record_id
  398. _get_record_id(){
  399. _zone_id="$1"
  400. record="$2"
  401. value="$3"
  402. _dynv6_rest GET "zones/$_zone_id/records"
  403. if ! _get_record_id_from_response "$response" ; then
  404. _err "no such record $record found in zone $_zone_id"
  405. return 1
  406. fi
  407. }
  408. _get_record_id_from_response(){
  409. response="$1"
  410. _record_id="$(echo "$response" | tr '}' '\n' | grep "\"name\":\"$record\"" | grep "\"data\":\"$value\"" | tr ',' '\n' | grep id |tr -d '"'|tr -d 'id:')"
  411. #_record_id="${_record_id#id:}"
  412. if [ -z "$_record_id" ]; then
  413. _err "no such record: $record found in zone $_zone_id"
  414. return 1
  415. fi
  416. _debug "record id: $_record_id"
  417. return 0
  418. }
  419. #usage: _set_record TXT _acme_challenge.www longvalue 12345678
  420. #zone id is optional can also be set as vairable bevor calling this method
  421. _set_record(){
  422. type="$1"
  423. record="$2"
  424. value="$3"
  425. if [ "$4" ]; then
  426. _zone_id="$4"
  427. fi
  428. data="{\"name\": \"$record\", \"data\": \"$value\", \"type\": \"$type\"}"
  429. #data='{ "name": "acme.test.thorn.dynv6.net", "type": "A", "data": "192.168.0.1"}'
  430. echo "$data"
  431. #"{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"
  432. _dynv6_rest POST "zones/$_zone_id/records" "$data"
  433. }
  434. _del_record(){
  435. _zone_id=$1
  436. _record_id=$2
  437. _dynv6_rest DELETE zones/"$_zone_id"/records/"$_record_id"
  438. }
  439. _dynv6_rest() {
  440. m=$1 #method GET,POST,DELETE or PUT
  441. ep="$2" #the endpoint
  442. data="$3"
  443. _debug "$ep"
  444. token_trimmed=$(echo "$dynv6_token" | tr -d '"')
  445. export _H1="Authorization: Bearer $token_trimmed"
  446. export _H2="Content-Type: application/json"
  447. if [ "$m" != "GET" ]; then
  448. _debug data "$data"
  449. response="$(_post "$data" "$dynv6_api/$ep" "" "$m")"
  450. else
  451. response="$(_get "$dynv6_api/$ep")"
  452. fi
  453. if [ "$?" != "0" ]; then
  454. _err "error $ep"
  455. return 1
  456. fi
  457. _debug2 response "$response"
  458. return 0
  459. }