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.

370 lines
12 KiB

  1. #!/usr/bin/env sh
  2. #This file name is "dns_freedns.sh"
  3. #So, here must be a method dns_freedns_add()
  4. #Which will be called by acme.sh to add the txt record to your api system.
  5. #returns 0 means success, otherwise error.
  6. #
  7. #Author: David Kerr
  8. #Report Bugs here: https://github.com/dkerr64/acme.sh
  9. #
  10. ######## Public functions #####################
  11. # Export FreeDNS userid and password in folowing variables...
  12. # FREEDNS_User=username
  13. # FREEDNS_Password=password
  14. # login cookie is saved in acme account config file so userid / pw
  15. # need to be set only when changed.
  16. #Usage: dns_freedns_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  17. dns_freedns_add() {
  18. fulldomain="$1"
  19. txtvalue="$2"
  20. _info "Add TXT record using FreeDNS"
  21. _debug "fulldomain: $fulldomain"
  22. _debug "txtvalue: $txtvalue"
  23. if [ -z "$FREEDNS_User" ] || [ -z "$FREEDNS_Password" ]; then
  24. if [ -z "$FREEDNS_COOKIE" ]; then
  25. _err "You did not specify the FreeDNS username and password yet."
  26. _err "Please export as FREEDNS_User / FREEDNS_Password and try again."
  27. return 1
  28. fi
  29. using_cached_cookies="true"
  30. else
  31. FREEDNS_COOKIE="$(_freedns_login "$FREEDNS_User" "$FREEDNS_Password")"
  32. if [ -z "$FREEDNS_COOKIE" ]; then
  33. return 1
  34. fi
  35. using_cached_cookies="false"
  36. fi
  37. _debug "FreeDNS login cookies: $FREEDNS_COOKIE (cached = $using_cached_cookies)"
  38. _saveaccountconf FREEDNS_COOKIE "$FREEDNS_COOKIE"
  39. htmlpage="$(_freedns_retrieve_subdomain_page "$FREEDNS_COOKIE")"
  40. if [ "$?" != "0" ]; then
  41. if [ "$using_cached_cookies" = "true" ]; then
  42. _err "Has your FreeDNS username and password channged? If so..."
  43. _err "Please export as FREEDNS_User / FREEDNS_Password and try again."
  44. fi
  45. return 1
  46. fi
  47. # split our full domain name into two parts...
  48. top_domain="$(echo "$fulldomain" | rev | cut -d. -f -2 | rev)"
  49. sub_domain="$(echo "$fulldomain" | rev | cut -d. -f 3- | rev)"
  50. # Now convert the tables in the HTML to CSV. This litte gem from
  51. # http://stackoverflow.com/questions/1403087/how-can-i-convert-an-html-table-to-csv
  52. subdomain_csv="$(echo "$htmlpage" \
  53. | grep -i -e '</\?TABLE\|</\?TD\|</\?TR\|</\?TH' \
  54. | sed 's/^[\ \t]*//g' \
  55. | tr -d '\n' \
  56. | sed 's/<\/TR[^>]*>/\n/Ig' \
  57. | sed 's/<\/\?\(TABLE\|TR\)[^>]*>//Ig' \
  58. | sed 's/^<T[DH][^>]*>\|<\/\?T[DH][^>]*>$//Ig' \
  59. | sed 's/<\/T[DH][^>]*><T[DH][^>]*>/,/Ig' \
  60. | grep 'edit.php?' \
  61. | grep "$top_domain")"
  62. # The above beauty ends with striping out rows that do not have an
  63. # href to edit.php and do not have the top domain we are looking for.
  64. # So all we should be left with is CSV of table of subdomains we are
  65. # interested in.
  66. # Now we have to read through this table and extract the data we need
  67. lines=$(echo "$subdomain_csv" | wc -l)
  68. nl='
  69. '
  70. i=0
  71. found=0
  72. while [ $i -lt $lines ]; do
  73. i=$(_math $i + 1 )
  74. line="$(echo "$subdomain_csv" | cut -d "$nl" -f $i)"
  75. tmp="$(echo "$line" | cut -d ',' -f 1)"
  76. if [ $found = 0 ] && _startswith "$tmp" "<td>$top_domain"; then
  77. # this line will contain DNSdomainid for the top_domain
  78. tmp="$(echo "$line" | cut -d ',' -f 2)"
  79. url=${tmp#*=}
  80. url=${url%%>*}
  81. DNSdomainid=${url#*domain_id=}
  82. found=1
  83. else
  84. # lines contain DNS records for all subdomains
  85. dns_href="$(echo "$line" | cut -d ',' -f 2)"
  86. tmp=${dns_href#*>}
  87. DNSname=${tmp%%<*}
  88. DNStype="$(echo "$line" | cut -d ',' -f 3)"
  89. if [ "$DNSname" = "$fulldomain" ] && [ "$DNStype" = "TXT" ]; then
  90. tmp=${dns_href#*=}
  91. url=${tmp%%>*}
  92. DNSdataid=${url#*data_id=}
  93. # Now get current value for the TXT record. This method may
  94. # produce inaccurate results as the value field is truncated
  95. # on this webpage. To get full value we would need to load
  96. # another page. However we don't really need this so long as
  97. # there is only one TXT record for the acme chalenge subdomain.
  98. tmp="$(echo "$line" | cut -d ',' -f 4)"
  99. # strip the html double-quotes off the value
  100. tmp=${tmp#&quot;}
  101. DNSvalue=${tmp%&quot;}
  102. if [ $found != 0 ]; then
  103. break
  104. # we are breaking out of the loop at the first match of DNS name
  105. # and DNS type (if we are past finding the domainid). This assumes
  106. # that there is only ever one TXT record for the LetsEncrypt/acme
  107. # challenge subdomain. This seems to be a reasonable assumption
  108. # as the acme client deletes the TXT record on successful validation.
  109. fi
  110. else
  111. DNSname=""
  112. DNStype=""
  113. fi
  114. fi
  115. done
  116. _debug "DNSname: $DNSname DNStype: $DNStype DNSdomainid: $DNSdomainid DNSdataid: $DNSdataid"
  117. _debug "DNSvalue: $DNSvalue"
  118. if [ -z "$DNSdomainid" ]; then
  119. # If domain ID is empty then something went wrong (top level
  120. # domain not found at FreeDNS). Cannot proceed.
  121. _debug "$htmlpage"
  122. _debug "$subdomain_csv"
  123. _err "Domain $top_domain not found at FreeDNS"
  124. return 1
  125. fi
  126. if [ -z "$DNSdataid" ]; then
  127. # If data ID is empty then specific subdomain does not exist yet, need
  128. # to create it this should always be the case as the acme client
  129. # deletes the entry after domain is validated.
  130. _freedns_add_txt_record "$FREEDNS_COOKIE" "$DNSdomainid" "$sub_domain" "$txtvalue"
  131. return $?
  132. else
  133. if [ "$txtvalue" = "$DNSvalue" ]; then
  134. # if value in TXT record matches value requested then DNS record
  135. # does not need to be updated. But...
  136. # Testing value match fails. Website is truncating the value field.
  137. # So for now we will always go down the else path. Though in theory
  138. # should never come here anyway as the acme client deletes
  139. # the TXT record on successful validation, so we should not even
  140. # have found a TXT record !!
  141. _info "No update necessary for $fulldomain at FreeDNS"
  142. return 0
  143. else
  144. # Delete the old TXT record (with the wrong value)
  145. _freedns_delete_txt_record "$FREEDNS_COOKIE" "$DNSdataid"
  146. if [ "$?" = "0" ]; then
  147. # And add in new TXT record with the value provided
  148. _freedns_add_txt_record "$FREEDNS_COOKIE" "$DNSdomainid" "$sub_domain" "$txtvalue"
  149. fi
  150. return $?
  151. fi
  152. fi
  153. return 0
  154. }
  155. #Usage: fulldomain txtvalue
  156. #Remove the txt record after validation.
  157. dns_freedns_rm() {
  158. fulldomain="$1"
  159. txtvalue="$2"
  160. _info "Delete TXT record using FreeDNS"
  161. _debug "fulldomain: $fulldomain"
  162. _debug "txtvalue: $txtvalue"
  163. # Need to read cookie from conf file again in case new value set
  164. # during login to FreeDNS when TXT record was created.
  165. #TODO acme.sh does not have a _readaccountconf() fuction
  166. FREEDNS_COOKIE="$(_read_conf "$ACCOUNT_CONF_PATH" "FREEDNS_COOKIE")"
  167. _debug "FreeDNS login cookies: $FREEDNS_COOKIE"
  168. htmlpage="$(_freedns_retrieve_subdomain_page "$FREEDNS_COOKIE")"
  169. if [ "$?" != "0" ]; then
  170. return 1
  171. fi
  172. # Now convert the tables in the HTML to CSV. This litte gem from
  173. # http://stackoverflow.com/questions/1403087/how-can-i-convert-an-html-table-to-csv
  174. subdomain_csv="$(echo "$htmlpage" \
  175. | grep -i -e '</\?TABLE\|</\?TD\|</\?TR\|</\?TH' \
  176. | sed 's/^[\ \t]*//g' \
  177. | tr -d '\n' \
  178. | sed 's/<\/TR[^>]*>/\n/Ig' \
  179. | sed 's/<\/\?\(TABLE\|TR\)[^>]*>//Ig' \
  180. | sed 's/^<T[DH][^>]*>\|<\/\?T[DH][^>]*>$//Ig' \
  181. | sed 's/<\/T[DH][^>]*><T[DH][^>]*>/,/Ig' \
  182. | grep 'edit.php?' \
  183. | grep "$fulldomain")"
  184. # The above beauty ends with striping out rows that do not have an
  185. # href to edit.php and do not have the domain name we are looking for.
  186. # So all we should be left with is CSV of table of subdomains we are
  187. # interested in.
  188. # Now we have to read through this table and extract the data we need
  189. lines=$(echo "$subdomain_csv" | wc -l)
  190. nl='
  191. '
  192. i=0
  193. found=0
  194. while [ $i -lt $lines ]; do
  195. i=$(_math $i + 1 )
  196. line="$(echo "$subdomain_csv" | cut -d "$nl" -f $i)"
  197. dns_href="$(echo "$line" | cut -d ',' -f 2)"
  198. tmp=${dns_href#*>}
  199. DNSname=${tmp%%<*}
  200. DNStype="$(echo "$line" | cut -d ',' -f 3)"
  201. if [ "$DNSname" = "$fulldomain" ] && [ "$DNStype" = "TXT" ]; then
  202. tmp=${dns_href#*=}
  203. url=${tmp%%>*}
  204. DNSdataid=${url#*data_id=}
  205. tmp="$(echo "$line" | cut -d ',' -f 4)"
  206. # strip the html double-quotes off the value
  207. tmp=${tmp#&quot;}
  208. DNSvalue=${tmp%&quot;}
  209. _debug "DNSvalue: $DNSvalue"
  210. # if [ "$DNSvalue" = "$txtvalue" ]; then
  211. # Testing value match fails. Website is truncating the value
  212. # field. So for now we will assume that there is only one TXT
  213. # field for the sub domain and just delete it. Currently this
  214. # is a safe assumption.
  215. _freedns_delete_txt_record "$FREEDNS_COOKIE" "$DNSdataid"
  216. return $?
  217. # fi
  218. fi
  219. done
  220. # If we get this far we did not find a match.
  221. # Not necessarily an error, but log anyway.
  222. _debug2 "$subdomain_csv"
  223. _info "Cannot delete TXT record for $fulldomain/$txtvalue. Does not exist at FreeDNS"
  224. return 0
  225. }
  226. #################### Private functions below ##################################
  227. # usage: _freedns_login username password
  228. # print string "cookie=value" etc.
  229. # returns 0 success
  230. _freedns_login() {
  231. username="$1"
  232. password="$2"
  233. url="https://freedns.afraid.org/zc.php?step=2"
  234. _debug "Login to FreeDNS as user $username"
  235. htmlpage="$(_post "username=$(_freedns_urlencode "$username")&password=$(_freedns_urlencode "$password")&submit=Login&action=auth" "$url")"
  236. if [ "$?" != "0" ]; then
  237. _err "FreeDNS login failed for user $username bad RC from _post"
  238. return 1
  239. fi
  240. cookies="$(grep -i '^Set-Cookie.*dns_cookie.*$' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
  241. # if cookies is not empty then logon successful
  242. if [ -z "$cookies" ]; then
  243. _debug "$htmlpage"
  244. _err "FreeDNS login failed for user $username. Check $HTTP_HEADER file"
  245. return 1
  246. fi
  247. printf "%s" "$cookies"
  248. return 0
  249. }
  250. # usage _freedns_retrieve_subdomain_page login_cookies
  251. # echo page retrieved (html)
  252. # returns 0 success
  253. _freedns_retrieve_subdomain_page() {
  254. export _H1="Cookie:$1"
  255. url="https://freedns.afraid.org/subdomain/"
  256. _debug "Retrieve subdmoain page from FreeDNS"
  257. htmlpage="$(_get "$url")"
  258. if [ "$?" != "0" ]; then
  259. _err "FreeDNS retrieve subdomins failed bad RC from _get"
  260. return 1
  261. fi
  262. if [ -z "$htmlpage" ]; then
  263. _err "FreeDNS returned empty subdomain page"
  264. return 1
  265. fi
  266. _debug2 "$htmlpage"
  267. printf "%s" "$htmlpage"
  268. return 0
  269. }
  270. # usage _freedns_add_txt_record login_cookies domain_id subdomain value
  271. # returns 0 success
  272. _freedns_add_txt_record() {
  273. export _H1="Cookie:$1"
  274. domain_id="$2"
  275. subdomain="$3"
  276. value="$(_freedns_urlencode "$4")"
  277. url="http://freedns.afraid.org/subdomain/save.php?step=2"
  278. htmlpage="$(_post "type=TXT&domain_id=$domain_id&subdomain=$subdomain&address=%22$value%22&send=Save%21" "$url")"
  279. if [ "$?" != "0" ]; then
  280. _err "FreeDNS failed to add TXT record for $subdomain bad RC from _post"
  281. return 1
  282. fi
  283. if ! grep "200 OK" "$HTTP_HEADER" >/dev/null; then
  284. _debug "$htmlpage"
  285. _err "FreeDNS failed to add TXT record for $subdomain. Check $HTTP_HEADER file"
  286. return 1
  287. fi
  288. _info "Added acme challenge TXT record for $fulldomain at FreeDNS"
  289. return 0
  290. }
  291. # usage _freedns_delete_txt_record login_cookies data_id
  292. # returns 0 success
  293. _freedns_delete_txt_record() {
  294. export _H1="Cookie:$1"
  295. data_id="$2"
  296. url="https://freedns.afraid.org/subdomain/delete2.php"
  297. htmlheader="$(_get "$url?data_id%5B%5D=$data_id&submit=delete+selected" "onlyheader")"
  298. if [ "$?" != "0" ]; then
  299. _err "FreeDNS failed to delete TXT record for $data_id bad RC from _get"
  300. return 1
  301. fi
  302. if ! _contains "$htmlheader" "200 OK"; then
  303. _debug "$htmlheader"
  304. _err "FreeDNS failed to delete TXT record $data_id"
  305. return 1
  306. fi
  307. _info "Deleted acme challenge TXT record for $fulldomain at FreeDNS"
  308. return 0
  309. }
  310. # urlencode magic from...
  311. # http://askubuntu.com/questions/53770/how-can-i-encode-and-decode-percent-encoded-strings-on-the-command-line
  312. # The _urlencode function in acme.sh does not work !
  313. _freedns_urlencode() {
  314. # urlencode <string>
  315. length="${#1}"
  316. for ((i = 0; i < length; i++)); do
  317. c="${1:i:1}"
  318. case $c in
  319. [a-zA-Z0-9.~_-]) printf "$c" ;;
  320. *) printf '%%%02X' "'$c" ;;
  321. esac
  322. done
  323. }