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.

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