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.

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