dns_freedns.sh 13 KB

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