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.

328 lines
7.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
3 months ago
7 years ago
3 months ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_loopia_info='Loopia.se
  4. Site: Loopia.se
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_loopia
  6. Options:
  7. LOOPIA_Api API URL. E.g. "https://api.loopia.<TLD>/RPCSERV" where the <TLD> is one of: com, no, rs, se. Default: "se".
  8. LOOPIA_User Username
  9. LOOPIA_Password Password
  10. '
  11. LOOPIA_Api_Default="https://api.loopia.se/RPCSERV"
  12. ######## Public functions #####################
  13. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  14. dns_loopia_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. if ! _loopia_load_config; then
  18. return 1
  19. fi
  20. _loopia_save_config
  21. _debug "First detect the root zone"
  22. if ! _get_root "$fulldomain"; then
  23. _err "invalid domain"
  24. return 1
  25. fi
  26. _debug _sub_domain "$_sub_domain"
  27. _debug _domain "$_domain"
  28. _info "Adding record"
  29. if ! _loopia_add_sub_domain "$_domain" "$_sub_domain"; then
  30. return 1
  31. fi
  32. if ! _loopia_add_record "$_domain" "$_sub_domain" "$txtvalue"; then
  33. return 1
  34. fi
  35. }
  36. dns_loopia_rm() {
  37. fulldomain=$1
  38. txtvalue=$2
  39. if ! _loopia_load_config; then
  40. return 1
  41. fi
  42. _loopia_save_config
  43. _debug "First detect the root zone"
  44. if ! _get_root "$fulldomain"; then
  45. _err "invalid domain"
  46. return 1
  47. fi
  48. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  49. <methodCall>
  50. <methodName>removeSubdomain</methodName>
  51. <params>
  52. <param>
  53. <value><string>%s</string></value>
  54. </param>
  55. <param>
  56. <value><string>%s</string></value>
  57. </param>
  58. <param>
  59. <value><string>%s</string></value>
  60. </param>
  61. <param>
  62. <value><string>%s</string></value>
  63. </param>
  64. </params>
  65. </methodCall>' "$LOOPIA_User" "$Encoded_Password" "$_domain" "$_sub_domain")
  66. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  67. if ! _contains "$response" "OK"; then
  68. err_response=$(echo "$response" | sed 's/.*<string>\(.*\)<\/string>.*/\1/')
  69. _err "Error could not get txt records: $err_response"
  70. return 1
  71. fi
  72. }
  73. #################### Private functions below ##################################
  74. _loopia_load_config() {
  75. LOOPIA_Api="${LOOPIA_Api:-$(_readaccountconf_mutable LOOPIA_Api)}"
  76. LOOPIA_User="${LOOPIA_User:-$(_readaccountconf_mutable LOOPIA_User)}"
  77. LOOPIA_Password="${LOOPIA_Password:-$(_readaccountconf_mutable LOOPIA_Password)}"
  78. if [ -z "$LOOPIA_Api" ]; then
  79. LOOPIA_Api="$LOOPIA_Api_Default"
  80. fi
  81. if [ -z "$LOOPIA_User" ] || [ -z "$LOOPIA_Password" ]; then
  82. LOOPIA_User=""
  83. LOOPIA_Password=""
  84. _err "A valid Loopia API user and password not provided."
  85. _err "Please provide a valid API user and try again."
  86. return 1
  87. fi
  88. if _contains "$LOOPIA_Password" "'" || _contains "$LOOPIA_Password" '"'; then
  89. _err "Password contains a quotation mark or double quotation marks and this is not supported by dns_loopia.sh"
  90. return 1
  91. fi
  92. Encoded_Password=$(_xml_encode "$LOOPIA_Password")
  93. return 0
  94. }
  95. _loopia_save_config() {
  96. if [ "$LOOPIA_Api" != "$LOOPIA_Api_Default" ]; then
  97. _saveaccountconf_mutable LOOPIA_Api "$LOOPIA_Api"
  98. fi
  99. _saveaccountconf_mutable LOOPIA_User "$LOOPIA_User"
  100. _saveaccountconf_mutable LOOPIA_Password "$LOOPIA_Password"
  101. }
  102. _loopia_get_records() {
  103. domain=$1
  104. sub_domain=$2
  105. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  106. <methodCall>
  107. <methodName>getZoneRecords</methodName>
  108. <params>
  109. <param>
  110. <value><string>%s</string></value>
  111. </param>
  112. <param>
  113. <value><string>%s</string></value>
  114. </param>
  115. <param>
  116. <value><string>%s</string></value>
  117. </param>
  118. <param>
  119. <value><string>%s</string></value>
  120. </param>
  121. </params>
  122. </methodCall>' "$LOOPIA_User" "$Encoded_Password" "$domain" "$sub_domain")
  123. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  124. if ! _contains "$response" "<array>"; then
  125. err_response=$(echo "$response" | sed 's/.*<string>\(.*\)<\/string>.*/\1/')
  126. _err "Error: $err_response"
  127. return 1
  128. fi
  129. return 0
  130. }
  131. _get_root() {
  132. domain=$1
  133. _debug "get root"
  134. domain=$1
  135. i=2
  136. p=1
  137. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  138. <methodCall>
  139. <methodName>getDomains</methodName>
  140. <params>
  141. <param>
  142. <value><string>%s</string></value>
  143. </param>
  144. <param>
  145. <value><string>%s</string></value>
  146. </param>
  147. </params>
  148. </methodCall>' "$LOOPIA_User" "$Encoded_Password")
  149. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  150. while true; do
  151. h=$(echo "$domain" | cut -d . -f "$i"-100)
  152. if [ -z "$h" ]; then
  153. #not valid
  154. return 1
  155. fi
  156. if _contains "$response" "$h"; then
  157. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  158. _domain="$h"
  159. return 0
  160. fi
  161. p=$i
  162. i=$(_math "$i" + 1)
  163. done
  164. return 1
  165. }
  166. _loopia_add_record() {
  167. domain=$1
  168. sub_domain=$2
  169. txtval=$3
  170. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  171. <methodCall>
  172. <methodName>addZoneRecord</methodName>
  173. <params>
  174. <param>
  175. <value><string>%s</string></value>
  176. </param>
  177. <param>
  178. <value><string>%s</string></value>
  179. </param>
  180. <param>
  181. <value><string>%s</string></value>
  182. </param>
  183. <param>
  184. <value><string>%s</string></value>
  185. </param>
  186. <param>
  187. <value>
  188. <struct>
  189. <member>
  190. <name>type</name>
  191. <value><string>TXT</string></value>
  192. </member>
  193. <member>
  194. <name>priority</name>
  195. <value><int>0</int></value>
  196. </member>
  197. <member>
  198. <name>ttl</name>
  199. <value><int>300</int></value>
  200. </member>
  201. <member>
  202. <name>rdata</name>
  203. <value><string>%s</string></value>
  204. </member>
  205. </struct>
  206. </value>
  207. </param>
  208. </params>
  209. </methodCall>' "$LOOPIA_User" "$Encoded_Password" "$domain" "$sub_domain" "$txtval")
  210. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  211. if ! _contains "$response" "OK"; then
  212. err_response=$(echo "$response" | sed 's/.*<string>\(.*\)<\/string>.*/\1/')
  213. _err "Error: $err_response"
  214. return 1
  215. fi
  216. return 0
  217. }
  218. _sub_domain_exists() {
  219. domain=$1
  220. sub_domain=$2
  221. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  222. <methodCall>
  223. <methodName>getSubdomains</methodName>
  224. <params>
  225. <param>
  226. <value><string>%s</string></value>
  227. </param>
  228. <param>
  229. <value><string>%s</string></value>
  230. </param>
  231. <param>
  232. <value><string>%s</string></value>
  233. </param>
  234. </params>
  235. </methodCall>' "$LOOPIA_User" "$Encoded_Password" "$domain")
  236. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  237. if _contains "$response" "$sub_domain"; then
  238. return 0
  239. fi
  240. return 1
  241. }
  242. _loopia_add_sub_domain() {
  243. domain=$1
  244. sub_domain=$2
  245. if _sub_domain_exists "$domain" "$sub_domain"; then
  246. return 0
  247. fi
  248. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  249. <methodCall>
  250. <methodName>addSubdomain</methodName>
  251. <params>
  252. <param>
  253. <value><string>%s</string></value>
  254. </param>
  255. <param>
  256. <value><string>%s</string></value>
  257. </param>
  258. <param>
  259. <value><string>%s</string></value>
  260. </param>
  261. <param>
  262. <value><string>%s</string></value>
  263. </param>
  264. </params>
  265. </methodCall>' "$LOOPIA_User" "$Encoded_Password" "$domain" "$sub_domain")
  266. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  267. if ! _contains "$response" "OK"; then
  268. err_response=$(echo "$response" | sed 's/.*<string>\(.*\)<\/string>.*/\1/')
  269. _err "Error: $err_response"
  270. return 1
  271. fi
  272. return 0
  273. }
  274. _xml_encode() {
  275. encoded_string=$1
  276. encoded_string=$(echo "$encoded_string" | sed 's/&/\&amp;/')
  277. encoded_string=$(echo "$encoded_string" | sed 's/</\&lt;/')
  278. encoded_string=$(echo "$encoded_string" | sed 's/>/\&gt;/')
  279. printf "%s" "$encoded_string"
  280. }