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