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.

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