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.

326 lines
11 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  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 following 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. FREEDNS_User=""
  25. FREEDNS_Password=""
  26. if [ -z "$FREEDNS_COOKIE" ]; then
  27. _err "You did not specify the FreeDNS username and password yet."
  28. _err "Please export as FREEDNS_User / FREEDNS_Password and try again."
  29. return 1
  30. fi
  31. using_cached_cookies="true"
  32. else
  33. FREEDNS_COOKIE="$(_freedns_login "$FREEDNS_User" "$FREEDNS_Password")"
  34. if [ -z "$FREEDNS_COOKIE" ]; then
  35. return 1
  36. fi
  37. using_cached_cookies="false"
  38. fi
  39. _debug "FreeDNS login cookies: $FREEDNS_COOKIE (cached = $using_cached_cookies)"
  40. _saveaccountconf FREEDNS_COOKIE "$FREEDNS_COOKIE"
  41. # split our full domain name into two parts...
  42. i="$(echo "$fulldomain" | tr '.' ' ' | wc -w)"
  43. i="$(_math "$i" - 1)"
  44. top_domain="$(echo "$fulldomain" | cut -d. -f "$i"-100)"
  45. i="$(_math "$i" - 1)"
  46. sub_domain="$(echo "$fulldomain" | cut -d. -f -"$i")"
  47. _debug "top_domain: $top_domain"
  48. _debug "sub_domain: $sub_domain"
  49. # Sometimes FreeDNS does not return the subdomain page but rather
  50. # returns a page regarding becoming a premium member. This usually
  51. # happens after a period of inactivity. Immediately trying again
  52. # returns the correct subdomain page. So, we will try twice to
  53. # load the page and obtain our domain ID
  54. attempts=2
  55. while [ "$attempts" -gt "0" ]; do
  56. attempts="$(_math "$attempts" - 1)"
  57. htmlpage="$(_freedns_retrieve_subdomain_page "$FREEDNS_COOKIE")"
  58. if [ "$?" != "0" ]; then
  59. if [ "$using_cached_cookies" = "true" ]; then
  60. _err "Has your FreeDNS username and password changed? If so..."
  61. _err "Please export as FREEDNS_User / FREEDNS_Password and try again."
  62. fi
  63. return 1
  64. fi
  65. subdomain_csv="$(echo "$htmlpage" | tr -d "\n\r" | _egrep_o '<form .*</form>' | sed 's/<tr>/@<tr>/g' | tr '@' '\n' | grep edit.php | grep "$top_domain")"
  66. _debug3 "subdomain_csv: $subdomain_csv"
  67. # The above beauty ends with striping out rows that do not have an
  68. # href to edit.php and do not have the top domain we are looking for.
  69. # So all we should be left with is CSV of table of subdomains we are
  70. # interested in.
  71. # Now we have to read through this table and extract the data we need
  72. lines="$(echo "$subdomain_csv" | wc -l)"
  73. i=0
  74. found=0
  75. DNSdomainid=""
  76. while [ "$i" -lt "$lines" ]; do
  77. i="$(_math "$i" + 1)"
  78. line="$(echo "$subdomain_csv" | sed -n "${i}p")"
  79. _debug2 "line: $line"
  80. if [ $found = 0 ] && _contains "$line" "<td>$top_domain</td>"; then
  81. # this line will contain DNSdomainid for the top_domain
  82. DNSdomainid="$(echo "$line" | _egrep_o "edit_domain_id *= *.*>" | cut -d = -f 2 | cut -d '>' -f 1)"
  83. _debug2 "DNSdomainid: $DNSdomainid"
  84. found=1
  85. break
  86. fi
  87. done
  88. if [ -z "$DNSdomainid" ]; then
  89. # If domain ID is empty then something went wrong (top level
  90. # domain not found at FreeDNS).
  91. if [ "$attempts" = "0" ]; then
  92. # exhausted maximum retry attempts
  93. _err "Domain $top_domain not found at FreeDNS"
  94. return 1
  95. fi
  96. else
  97. # break out of the 'retry' loop... we have found our domain ID
  98. break
  99. fi
  100. _info "Domain $top_domain not found at FreeDNS"
  101. _info "Retry loading subdomain page ($attempts attempts remaining)"
  102. done
  103. # Add in new TXT record with the value provided
  104. _debug "Adding TXT record for $fulldomain, $txtvalue"
  105. _freedns_add_txt_record "$FREEDNS_COOKIE" "$DNSdomainid" "$sub_domain" "$txtvalue"
  106. return $?
  107. }
  108. #Usage: fulldomain txtvalue
  109. #Remove the txt record after validation.
  110. dns_freedns_rm() {
  111. fulldomain="$1"
  112. txtvalue="$2"
  113. _info "Delete TXT record using FreeDNS"
  114. _debug "fulldomain: $fulldomain"
  115. _debug "txtvalue: $txtvalue"
  116. # Need to read cookie from conf file again in case new value set
  117. # during login to FreeDNS when TXT record was created.
  118. # acme.sh does not have a _readaccountconf() function
  119. FREEDNS_COOKIE="$(_read_conf "$ACCOUNT_CONF_PATH" "FREEDNS_COOKIE")"
  120. _debug "FreeDNS login cookies: $FREEDNS_COOKIE"
  121. # Sometimes FreeDNS does not return the subdomain page but rather
  122. # returns a page regarding becoming a premium member. This usually
  123. # happens after a period of inactivity. Immediately trying again
  124. # returns the correct subdomain page. So, we will try twice to
  125. # load the page and obtain our TXT record.
  126. attempts=2
  127. while [ "$attempts" -gt "0" ]; do
  128. attempts="$(_math "$attempts" - 1)"
  129. htmlpage="$(_freedns_retrieve_subdomain_page "$FREEDNS_COOKIE")"
  130. if [ "$?" != "0" ]; then
  131. return 1
  132. fi
  133. subdomain_csv="$(echo "$htmlpage" | tr -d "\n\r" | _egrep_o '<form .*</form>' | sed 's/<tr>/@<tr>/g' | tr '@' '\n' | grep edit.php | grep "$fulldomain")"
  134. _debug3 "subdomain_csv: $subdomain_csv"
  135. # The above beauty ends with striping out rows that do not have an
  136. # href to edit.php and do not have the domain name we are looking for.
  137. # So all we should be left with is CSV of table of subdomains we are
  138. # interested in.
  139. # Now we have to read through this table and extract the data we need
  140. lines="$(echo "$subdomain_csv" | wc -l)"
  141. i=0
  142. found=0
  143. DNSdataid=""
  144. while [ "$i" -lt "$lines" ]; do
  145. i="$(_math "$i" + 1)"
  146. line="$(echo "$subdomain_csv" | sed -n "${i}p")"
  147. _debug3 "line: $line"
  148. DNSname="$(echo "$line" | _egrep_o 'edit.php.*</a>' | cut -d '>' -f 2 | cut -d '<' -f 1)"
  149. _debug2 "DNSname: $DNSname"
  150. if [ "$DNSname" = "$fulldomain" ]; then
  151. DNStype="$(echo "$line" | sed 's/<td/@<td/g' | tr '@' '\n' | sed -n '4p' | cut -d '>' -f 2 | cut -d '<' -f 1)"
  152. _debug2 "DNStype: $DNStype"
  153. if [ "$DNStype" = "TXT" ]; then
  154. DNSdataid="$(echo "$line" | _egrep_o 'data_id=.*' | cut -d = -f 2 | cut -d '>' -f 1)"
  155. _debug2 "DNSdataid: $DNSdataid"
  156. DNSvalue="$(echo "$line" | sed 's/<td/@<td/g' | tr '@' '\n' | sed -n '5p' | cut -d '>' -f 2 | cut -d '<' -f 1)"
  157. if _startswith "$DNSvalue" "&quot;"; then
  158. # remove the quotation from the start
  159. DNSvalue="$(echo "$DNSvalue" | cut -c 7-)"
  160. fi
  161. if _endswith "$DNSvalue" "..."; then
  162. # value was truncated, remove the dot dot dot from the end
  163. DNSvalue="$(echo "$DNSvalue" | sed 's/...$//')"
  164. elif _endswith "$DNSvalue" "&quot;"; then
  165. # else remove the closing quotation from the end
  166. DNSvalue="$(echo "$DNSvalue" | sed 's/......$//')"
  167. fi
  168. _debug2 "DNSvalue: $DNSvalue"
  169. if [ -n "$DNSdataid" ] && _startswith "$txtvalue" "$DNSvalue"; then
  170. # Found a match. But note... Website is truncating the
  171. # value field so we are only testing that part that is not
  172. # truncated. This should be accurate enough.
  173. _debug "Deleting TXT record for $fulldomain, $txtvalue"
  174. _freedns_delete_txt_record "$FREEDNS_COOKIE" "$DNSdataid"
  175. return $?
  176. fi
  177. fi
  178. fi
  179. done
  180. done
  181. # If we get this far we did not find a match (after two attempts)
  182. # Not necessarily an error, but log anyway.
  183. _debug3 "$subdomain_csv"
  184. _info "Cannot delete TXT record for $fulldomain, $txtvalue. Does not exist at FreeDNS"
  185. return 0
  186. }
  187. #################### Private functions below ##################################
  188. # usage: _freedns_login username password
  189. # print string "cookie=value" etc.
  190. # returns 0 success
  191. _freedns_login() {
  192. export _H1="Accept-Language:en-US"
  193. username="$1"
  194. password="$2"
  195. url="https://freedns.afraid.org/zc.php?step=2"
  196. _debug "Login to FreeDNS as user $username"
  197. htmlpage="$(_post "username=$(printf '%s' "$username" | _url_encode)&password=$(printf '%s' "$password" | _url_encode)&submit=Login&action=auth" "$url")"
  198. if [ "$?" != "0" ]; then
  199. _err "FreeDNS login failed for user $username bad RC from _post"
  200. return 1
  201. fi
  202. cookies="$(grep -i '^Set-Cookie.*dns_cookie.*$' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
  203. # if cookies is not empty then logon successful
  204. if [ -z "$cookies" ]; then
  205. _debug3 "htmlpage: $htmlpage"
  206. _err "FreeDNS login failed for user $username. Check $HTTP_HEADER file"
  207. return 1
  208. fi
  209. printf "%s" "$cookies"
  210. return 0
  211. }
  212. # usage _freedns_retrieve_subdomain_page login_cookies
  213. # echo page retrieved (html)
  214. # returns 0 success
  215. _freedns_retrieve_subdomain_page() {
  216. export _H1="Cookie:$1"
  217. export _H2="Accept-Language:en-US"
  218. url="https://freedns.afraid.org/subdomain/"
  219. _debug "Retrieve subdomain page from FreeDNS"
  220. htmlpage="$(_get "$url")"
  221. if [ "$?" != "0" ]; then
  222. _err "FreeDNS retrieve subdomains failed bad RC from _get"
  223. return 1
  224. elif [ -z "$htmlpage" ]; then
  225. _err "FreeDNS returned empty subdomain page"
  226. return 1
  227. fi
  228. _debug3 "htmlpage: $htmlpage"
  229. printf "%s" "$htmlpage"
  230. return 0
  231. }
  232. # usage _freedns_add_txt_record login_cookies domain_id subdomain value
  233. # returns 0 success
  234. _freedns_add_txt_record() {
  235. export _H1="Cookie:$1"
  236. export _H2="Accept-Language:en-US"
  237. domain_id="$2"
  238. subdomain="$3"
  239. value="$(printf '%s' "$4" | _url_encode)"
  240. url="https://freedns.afraid.org/subdomain/save.php?step=2"
  241. htmlpage="$(_post "type=TXT&domain_id=$domain_id&subdomain=$subdomain&address=%22$value%22&send=Save%21" "$url")"
  242. if [ "$?" != "0" ]; then
  243. _err "FreeDNS failed to add TXT record for $subdomain bad RC from _post"
  244. return 1
  245. elif ! grep "200 OK" "$HTTP_HEADER" >/dev/null; then
  246. _debug3 "htmlpage: $htmlpage"
  247. _err "FreeDNS failed to add TXT record for $subdomain. Check $HTTP_HEADER file"
  248. return 1
  249. elif _contains "$htmlpage" "security code was incorrect"; then
  250. _debug3 "htmlpage: $htmlpage"
  251. _err "FreeDNS failed to add TXT record for $subdomain as FreeDNS requested security code"
  252. _err "Note that you cannot use automatic DNS validation for FreeDNS public domains"
  253. return 1
  254. fi
  255. _debug3 "htmlpage: $htmlpage"
  256. _info "Added acme challenge TXT record for $fulldomain at FreeDNS"
  257. return 0
  258. }
  259. # usage _freedns_delete_txt_record login_cookies data_id
  260. # returns 0 success
  261. _freedns_delete_txt_record() {
  262. export _H1="Cookie:$1"
  263. export _H2="Accept-Language:en-US"
  264. data_id="$2"
  265. url="https://freedns.afraid.org/subdomain/delete2.php"
  266. htmlheader="$(_get "$url?data_id%5B%5D=$data_id&submit=delete+selected" "onlyheader")"
  267. if [ "$?" != "0" ]; then
  268. _err "FreeDNS failed to delete TXT record for $data_id bad RC from _get"
  269. return 1
  270. elif ! _contains "$htmlheader" "200 OK"; then
  271. _debug2 "htmlheader: $htmlheader"
  272. _err "FreeDNS failed to delete TXT record $data_id"
  273. return 1
  274. fi
  275. _info "Deleted acme challenge TXT record for $fulldomain at FreeDNS"
  276. return 0
  277. }