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.

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