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.

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