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.

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