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.

407 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/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. #TODO Not using acme.sh _post() function because I need to capture the cookies.
  230. cookie_file="$(curl --silent \
  231. --user-agent "$USER_AGENT" \
  232. --data "username=$(_freedns_urlencode "$username")&password=$(_freedns_urlencode "$password")&submit=Login&action=auth" \
  233. --cookie-jar - \
  234. $url )"
  235. if [ $? != 0 ]; then
  236. _err "FreeDNS login failed for user $username bad RC from cURL: $?"
  237. return 1
  238. fi
  239. # convert from cookie file format to cookie string
  240. cookies=""
  241. found=0
  242. IFS=$'\n'
  243. for line in $cookie_file
  244. do
  245. # strip spaces from start and end of line
  246. line="$(echo "$line" | xargs)"
  247. if [ $found = 0 ]; then
  248. # first line, validate that it is a cookie file
  249. if _contains "$line" "Netscape HTTP Cookie File"; then
  250. found=1
  251. else
  252. _debug "$cookie_file"
  253. _err "FreeDNS login failed for user $username bad cookie file"
  254. unset IFS
  255. return 1
  256. fi
  257. else
  258. # after first line skip blank line or comments
  259. if [ -n "$line" -a "$(echo $line | cut -c 1)" != "#" ]; then
  260. cookie_name="$(echo $line | cut -d ' ' -f 6)"
  261. if [ "$cookie_name" = "dns_cookie" ]; then
  262. # found the login cookie, that is all we need.
  263. cookies="$cookie_name=$(echo $line | cut -d ' ' -f 7)"
  264. break;
  265. fi
  266. fi
  267. fi
  268. done
  269. unset IFS
  270. # if cookies is not empty then logon successful
  271. if [ -z "$cookies" ]; then
  272. _err "FreeDNS login failed for user $username"
  273. return 1
  274. fi
  275. echo "$cookies"
  276. return 0
  277. }
  278. # usage _freedns_retrieve_subdomain_page login_cookies
  279. # echo page retrieved (html)
  280. # returns 0 success
  281. _freedns_retrieve_subdomain_page() {
  282. cookies=$1
  283. url="https://freedns.afraid.org/subdomain/"
  284. _debug "Retrieve subdmoain page from FreeDNS"
  285. #TODO Not using acme.sh _get() function becuase I need to pass in the cookies.
  286. htmlpage="$(curl --silent \
  287. --user-agent "$USER_AGENT" \
  288. --cookie "$cookies" \
  289. $url )"
  290. if [ $? != 0 ]; then
  291. _err "FreeDNS retrieve subdomins failed bad RC from cURL: $?"
  292. return 1
  293. fi
  294. if [ -z "$htmlpage" ]; then
  295. _err "FreeDNS returned empty subdomain page"
  296. return 1
  297. fi
  298. _debug2 "$htmlpage"
  299. echo "$htmlpage"
  300. return 0
  301. }
  302. _freedns_add_txt_record() {
  303. cookies=$1
  304. domain_id=$2
  305. subdomain=$3
  306. value="$(_freedns_urlencode "$4")"
  307. url="http://freedns.afraid.org/subdomain/save.php?step=2"
  308. #TODO Not using acme.sh _get() function becuase I need to pass in the cookies.
  309. htmlpage="$(curl --silent \
  310. --user-agent "$USER_AGENT" \
  311. --cookie "$cookies" \
  312. --data "type=TXT&domain_id=$domain_id&subdomain=$subdomain&address=%22$value%22&send=Save%21" \
  313. $url )"
  314. if [ $? != 0 ]; then
  315. _err "FreeDNS failed to add TXT record for $subdomain bad RC from cURL: $?"
  316. return 1
  317. fi
  318. # returned page should be empty on success
  319. if [ -n "$htmlpage" ]; then
  320. _debug "$htmlpage"
  321. _err "FreeDNS failed to add TXT record for $subdomain"
  322. return 1
  323. fi
  324. _info "Added acme challenge TXT record for $fulldomain at FreeDNS"
  325. return 0
  326. }
  327. _freedns_delete_txt_record() {
  328. cookies=$1
  329. data_id=$2
  330. url="https://freedns.afraid.org/subdomain/delete2.php"
  331. #TODO Not using acme.sh _get() function becuase I need to pass in the cookies.
  332. htmlpage="$(curl --silent \
  333. --user-agent "$USER_AGENT" \
  334. --cookie "$cookies" \
  335. "$url?data_id%5B%5D=$data_id&submit=delete+selected" )"
  336. if [ $? != 0 ]; then
  337. _err "FreeDNS failed to delete TXT record for $subdomain bad RC from cURL: $?"
  338. return 1
  339. fi
  340. # returned page should be empty on success
  341. if [ -n "$htmlpage" ]; then
  342. _debug "$htmlpage"
  343. _err "FreeDNS failed to delete TXT record $data_id"
  344. return 1
  345. fi
  346. _info "Deleted acme challenge TXT record for $fulldomain at FreeDNS"
  347. return 0
  348. }
  349. # urlencode magic from...
  350. # http://askubuntu.com/questions/53770/how-can-i-encode-and-decode-percent-encoded-strings-on-the-command-line
  351. # The _urlencode function in acme.sh does not work !
  352. _freedns_urlencode() {
  353. # urlencode <string>
  354. length="${#1}"
  355. for (( i = 0; i < length; i++ )); do
  356. c="${1:i:1}"
  357. case $c in
  358. [a-zA-Z0-9.~_-]) printf "$c" ;;
  359. *) printf '%%%02X' "'$c"
  360. esac
  361. done
  362. }