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.

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