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.

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