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.

372 lines
12 KiB

4 years ago
4 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_freedns_info='FreeDNS
  4. Site: FreeDNS.afraid.org
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_freedns
  6. Options:
  7. FREEDNS_User Username
  8. FREEDNS_Password Password
  9. Issues: github.com/acmesh-official/acme.sh/issues/2305
  10. Author: David Kerr <https://github.com/dkerr64>
  11. '
  12. ######## Public functions #####################
  13. # Export FreeDNS userid and password in following variables...
  14. # FREEDNS_User=username
  15. # FREEDNS_Password=password
  16. # login cookie is saved in acme account config file so userid / pw
  17. # need to be set only when changed.
  18. #Usage: dns_freedns_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  19. dns_freedns_add() {
  20. fulldomain="$1"
  21. txtvalue="$2"
  22. _info "Add TXT record using FreeDNS"
  23. _debug "fulldomain: $fulldomain"
  24. _debug "txtvalue: $txtvalue"
  25. if [ -z "$FREEDNS_User" ] || [ -z "$FREEDNS_Password" ]; then
  26. FREEDNS_User=""
  27. FREEDNS_Password=""
  28. if [ -z "$FREEDNS_COOKIE" ]; then
  29. _err "You did not specify the FreeDNS username and password yet."
  30. _err "Please export as FREEDNS_User / FREEDNS_Password and try again."
  31. return 1
  32. fi
  33. using_cached_cookies="true"
  34. else
  35. FREEDNS_COOKIE="$(_freedns_login "$FREEDNS_User" "$FREEDNS_Password")"
  36. if [ -z "$FREEDNS_COOKIE" ]; then
  37. return 1
  38. fi
  39. using_cached_cookies="false"
  40. fi
  41. _debug "FreeDNS login cookies: $FREEDNS_COOKIE (cached = $using_cached_cookies)"
  42. _saveaccountconf FREEDNS_COOKIE "$FREEDNS_COOKIE"
  43. # We may have to cycle through the domain name to find the
  44. # TLD that we own...
  45. i=1
  46. wmax="$(echo "$fulldomain" | tr '.' ' ' | wc -w)"
  47. while [ "$i" -lt "$wmax" ]; do
  48. # split our full domain name into two parts...
  49. sub_domain="$(echo "$fulldomain" | cut -d. -f -"$i")"
  50. i="$(_math "$i" + 1)"
  51. top_domain="$(echo "$fulldomain" | cut -d. -f "$i"-100)"
  52. _debug "sub_domain: $sub_domain"
  53. _debug "top_domain: $top_domain"
  54. DNSdomainid="$(_freedns_domain_id "$top_domain")"
  55. if [ "$?" = "0" ]; then
  56. _info "Domain $top_domain found at FreeDNS, domain_id $DNSdomainid"
  57. break
  58. else
  59. _info "Domain $top_domain not found at FreeDNS, try with next level of TLD"
  60. fi
  61. done
  62. if [ -z "$DNSdomainid" ]; then
  63. # If domain ID is empty then something went wrong (top level
  64. # domain not found at FreeDNS).
  65. _err "Domain $top_domain not found at FreeDNS"
  66. return 1
  67. fi
  68. # Add in new TXT record with the value provided
  69. _debug "Adding TXT record for $fulldomain, $txtvalue"
  70. _freedns_add_txt_record "$FREEDNS_COOKIE" "$DNSdomainid" "$sub_domain" "$txtvalue"
  71. return $?
  72. }
  73. #Usage: fulldomain txtvalue
  74. #Remove the txt record after validation.
  75. dns_freedns_rm() {
  76. fulldomain="$1"
  77. txtvalue="$2"
  78. _info "Delete TXT record using FreeDNS"
  79. _debug "fulldomain: $fulldomain"
  80. _debug "txtvalue: $txtvalue"
  81. # Need to read cookie from conf file again in case new value set
  82. # during login to FreeDNS when TXT record was created.
  83. FREEDNS_COOKIE="$(_readaccountconf "FREEDNS_COOKIE")"
  84. _debug "FreeDNS login cookies: $FREEDNS_COOKIE"
  85. TXTdataid="$(_freedns_data_id "$fulldomain" "TXT")"
  86. if [ "$?" != "0" ]; then
  87. _info "Cannot delete TXT record for $fulldomain, record does not exist at FreeDNS"
  88. return 1
  89. fi
  90. _debug "Data ID's found, $TXTdataid"
  91. # now we have one (or more) TXT record data ID's. Load the page
  92. # for that record and search for the record txt value. If match
  93. # then we can delete it.
  94. lines="$(echo "$TXTdataid" | wc -l)"
  95. _debug "Found $lines TXT data records for $fulldomain"
  96. i=0
  97. while [ "$i" -lt "$lines" ]; do
  98. i="$(_math "$i" + 1)"
  99. dataid="$(echo "$TXTdataid" | sed -n "${i}p")"
  100. _debug "$dataid"
  101. htmlpage="$(_freedns_retrieve_data_page "$FREEDNS_COOKIE" "$dataid")"
  102. if [ "$?" != "0" ]; then
  103. if [ "$using_cached_cookies" = "true" ]; then
  104. _err "Has your FreeDNS username and password changed? If so..."
  105. _err "Please export as FREEDNS_User / FREEDNS_Password and try again."
  106. fi
  107. return 1
  108. fi
  109. echo "$htmlpage" | grep "value=\"&quot;$txtvalue&quot;\"" >/dev/null
  110. if [ "$?" = "0" ]; then
  111. # Found a match... delete the record and return
  112. _info "Deleting TXT record for $fulldomain, $txtvalue"
  113. _freedns_delete_txt_record "$FREEDNS_COOKIE" "$dataid"
  114. return $?
  115. fi
  116. done
  117. # If we get this far we did not find a match
  118. # Not necessarily an error, but log anyway.
  119. _info "Cannot delete TXT record for $fulldomain, $txtvalue. Does not exist at FreeDNS"
  120. return 0
  121. }
  122. #################### Private functions below ##################################
  123. # usage: _freedns_login username password
  124. # print string "cookie=value" etc.
  125. # returns 0 success
  126. _freedns_login() {
  127. export _H1="Accept-Language:en-US"
  128. username="$1"
  129. password="$2"
  130. url="https://freedns.afraid.org/zc.php?step=2"
  131. _debug "Login to FreeDNS as user $username"
  132. htmlpage="$(_post "username=$(printf '%s' "$username" | _url_encode)&password=$(printf '%s' "$password" | _url_encode)&submit=Login&action=auth" "$url")"
  133. if [ "$?" != "0" ]; then
  134. _err "FreeDNS login failed for user $username bad RC from _post"
  135. return 1
  136. fi
  137. cookies="$(grep -i '^Set-Cookie.*dns_cookie.*$' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
  138. # if cookies is not empty then logon successful
  139. if [ -z "$cookies" ]; then
  140. _debug3 "htmlpage: $htmlpage"
  141. _err "FreeDNS login failed for user $username. Check $HTTP_HEADER file"
  142. return 1
  143. fi
  144. printf "%s" "$cookies"
  145. return 0
  146. }
  147. # usage _freedns_retrieve_subdomain_page login_cookies
  148. # echo page retrieved (html)
  149. # returns 0 success
  150. _freedns_retrieve_subdomain_page() {
  151. export _H1="Cookie:$1"
  152. export _H2="Accept-Language:en-US"
  153. url="https://freedns.afraid.org/subdomain/"
  154. _debug "Retrieve subdomain page from FreeDNS"
  155. htmlpage="$(_get "$url")"
  156. if [ "$?" != "0" ]; then
  157. _err "FreeDNS retrieve subdomains failed bad RC from _get"
  158. return 1
  159. elif [ -z "$htmlpage" ]; then
  160. _err "FreeDNS returned empty subdomain page"
  161. return 1
  162. fi
  163. _debug3 "htmlpage: $htmlpage"
  164. printf "%s" "$htmlpage"
  165. return 0
  166. }
  167. # usage _freedns_retrieve_data_page login_cookies data_id
  168. # echo page retrieved (html)
  169. # returns 0 success
  170. _freedns_retrieve_data_page() {
  171. export _H1="Cookie:$1"
  172. export _H2="Accept-Language:en-US"
  173. data_id="$2"
  174. url="https://freedns.afraid.org/subdomain/edit.php?data_id=$2"
  175. _debug "Retrieve data page for ID $data_id from FreeDNS"
  176. htmlpage="$(_get "$url")"
  177. if [ "$?" != "0" ]; then
  178. _err "FreeDNS retrieve data page failed bad RC from _get"
  179. return 1
  180. elif [ -z "$htmlpage" ]; then
  181. _err "FreeDNS returned empty data page"
  182. return 1
  183. fi
  184. _debug3 "htmlpage: $htmlpage"
  185. printf "%s" "$htmlpage"
  186. return 0
  187. }
  188. # usage _freedns_add_txt_record login_cookies domain_id subdomain value
  189. # returns 0 success
  190. _freedns_add_txt_record() {
  191. export _H1="Cookie:$1"
  192. export _H2="Accept-Language:en-US"
  193. domain_id="$2"
  194. subdomain="$3"
  195. value="$(printf '%s' "$4" | _url_encode)"
  196. url="https://freedns.afraid.org/subdomain/save.php?step=2"
  197. htmlpage="$(_post "type=TXT&domain_id=$domain_id&subdomain=$subdomain&address=%22$value%22&send=Save%21" "$url")"
  198. if [ "$?" != "0" ]; then
  199. _err "FreeDNS failed to add TXT record for $subdomain bad RC from _post"
  200. return 1
  201. elif ! grep "200 OK" "$HTTP_HEADER" >/dev/null; then
  202. _debug3 "htmlpage: $htmlpage"
  203. _err "FreeDNS failed to add TXT record for $subdomain. Check $HTTP_HEADER file"
  204. return 1
  205. elif _contains "$htmlpage" "security code was incorrect"; then
  206. _debug3 "htmlpage: $htmlpage"
  207. _err "FreeDNS failed to add TXT record for $subdomain as FreeDNS requested security code"
  208. _err "Note that you cannot use automatic DNS validation for FreeDNS public domains"
  209. return 1
  210. fi
  211. _debug3 "htmlpage: $htmlpage"
  212. _info "Added acme challenge TXT record for $fulldomain at FreeDNS"
  213. return 0
  214. }
  215. # usage _freedns_delete_txt_record login_cookies data_id
  216. # returns 0 success
  217. _freedns_delete_txt_record() {
  218. export _H1="Cookie:$1"
  219. export _H2="Accept-Language:en-US"
  220. data_id="$2"
  221. url="https://freedns.afraid.org/subdomain/delete2.php"
  222. htmlheader="$(_get "$url?data_id%5B%5D=$data_id&submit=delete+selected" "onlyheader")"
  223. if [ "$?" != "0" ]; then
  224. _err "FreeDNS failed to delete TXT record for $data_id bad RC from _get"
  225. return 1
  226. elif ! _contains "$htmlheader" "200 OK"; then
  227. _debug2 "htmlheader: $htmlheader"
  228. _err "FreeDNS failed to delete TXT record $data_id"
  229. return 1
  230. fi
  231. _info "Deleted acme challenge TXT record for $fulldomain at FreeDNS"
  232. return 0
  233. }
  234. # usage _freedns_domain_id domain_name
  235. # echo the domain_id if found
  236. # return 0 success
  237. _freedns_domain_id() {
  238. # Start by escaping the dots in the domain name
  239. search_domain="$(echo "$1" | sed 's/\./\\./g')"
  240. # Sometimes FreeDNS does not return the subdomain page but rather
  241. # returns a page regarding becoming a premium member. This usually
  242. # happens after a period of inactivity. Immediately trying again
  243. # returns the correct subdomain page. So, we will try twice to
  244. # load the page and obtain our domain ID
  245. attempts=2
  246. while [ "$attempts" -gt "0" ]; do
  247. attempts="$(_math "$attempts" - 1)"
  248. htmlpage="$(_freedns_retrieve_subdomain_page "$FREEDNS_COOKIE")"
  249. if [ "$?" != "0" ]; then
  250. if [ "$using_cached_cookies" = "true" ]; then
  251. _err "Has your FreeDNS username and password changed? If so..."
  252. _err "Please export as FREEDNS_User / FREEDNS_Password and try again."
  253. fi
  254. return 1
  255. fi
  256. domain_id="$(echo "$htmlpage" | tr -d " \t\r\n\v\f" | sed 's/<tr>/@<tr>/g' | tr '@' '\n' |
  257. grep "<td>$search_domain</td>\|<td>$search_domain(.*)</td>" |
  258. sed -n 's/.*\(edit\.php?edit_domain_id=[0-9a-zA-Z]*\).*/\1/p' |
  259. cut -d = -f 2)"
  260. # The above beauty extracts domain ID from the html page...
  261. # strip out all blank space and new lines. Then insert newlines
  262. # before each table row <tr>
  263. # search for the domain within each row (which may or may not have
  264. # a text string in brackets (.*) after it.
  265. # And finally extract the domain ID.
  266. if [ -n "$domain_id" ]; then
  267. printf "%s" "$domain_id"
  268. return 0
  269. fi
  270. _debug "Domain $search_domain not found. Retry loading subdomain page ($attempts attempts remaining)"
  271. done
  272. _debug "Domain $search_domain not found after retry"
  273. return 1
  274. }
  275. # usage _freedns_data_id domain_name record_type
  276. # echo the data_id(s) if found
  277. # return 0 success
  278. _freedns_data_id() {
  279. # Start by escaping the dots in the domain name
  280. search_domain="$(echo "$1" | sed 's/\./\\./g')"
  281. record_type="$2"
  282. # Sometimes FreeDNS does not return the subdomain page but rather
  283. # returns a page regarding becoming a premium member. This usually
  284. # happens after a period of inactivity. Immediately trying again
  285. # returns the correct subdomain page. So, we will try twice to
  286. # load the page and obtain our domain ID
  287. attempts=2
  288. while [ "$attempts" -gt "0" ]; do
  289. attempts="$(_math "$attempts" - 1)"
  290. htmlpage="$(_freedns_retrieve_subdomain_page "$FREEDNS_COOKIE")"
  291. if [ "$?" != "0" ]; then
  292. if [ "$using_cached_cookies" = "true" ]; then
  293. _err "Has your FreeDNS username and password changed? If so..."
  294. _err "Please export as FREEDNS_User / FREEDNS_Password and try again."
  295. fi
  296. return 1
  297. fi
  298. data_id="$(echo "$htmlpage" | tr -d " \t\r\n\v\f" | sed 's/<tr>/@<tr>/g' | tr '@' '\n' |
  299. grep "<td[a-zA-Z=#]*>$record_type</td>" |
  300. grep "<ahref.*>$search_domain</a>" |
  301. sed -n 's/.*\(edit\.php?data_id=[0-9a-zA-Z]*\).*/\1/p' |
  302. cut -d = -f 2)"
  303. # The above beauty extracts data ID from the html page...
  304. # strip out all blank space and new lines. Then insert newlines
  305. # before each table row <tr>
  306. # search for the record type withing each row (e.g. TXT)
  307. # search for the domain within each row (which is within a <a..>
  308. # </a> anchor. And finally extract the domain ID.
  309. if [ -n "$data_id" ]; then
  310. printf "%s" "$data_id"
  311. return 0
  312. fi
  313. _debug "Domain $search_domain not found. Retry loading subdomain page ($attempts attempts remaining)"
  314. done
  315. _debug "Domain $search_domain not found after retry"
  316. return 1
  317. }