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.

304 lines
6.7 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
  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. _loopia_add_sub_domain "$_domain" "$_sub_domain"
  27. _loopia_add_record "$_domain" "$_sub_domain" "$txtvalue"
  28. }
  29. dns_loopia_rm() {
  30. fulldomain=$1
  31. txtvalue=$2
  32. if ! _loopia_load_config; then
  33. return 1
  34. fi
  35. _loopia_save_config
  36. _debug "First detect the root zone"
  37. if ! _get_root "$fulldomain"; then
  38. _err "invalid domain"
  39. return 1
  40. fi
  41. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  42. <methodCall>
  43. <methodName>removeSubdomain</methodName>
  44. <params>
  45. <param>
  46. <value><string>%s</string></value>
  47. </param>
  48. <param>
  49. <value><string>%s</string></value>
  50. </param>
  51. <param>
  52. <value><string>%s</string></value>
  53. </param>
  54. <param>
  55. <value><string>%s</string></value>
  56. </param>
  57. </params>
  58. </methodCall>' "$LOOPIA_User" "$LOOPIA_Password" "$_domain" "$_sub_domain")
  59. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  60. if ! _contains "$response" "OK"; then
  61. _err "Error could not get txt records"
  62. return 1
  63. fi
  64. }
  65. #################### Private functions below ##################################
  66. _loopia_load_config() {
  67. LOOPIA_Api="${LOOPIA_Api:-$(_readaccountconf_mutable LOOPIA_Api)}"
  68. LOOPIA_User="${LOOPIA_User:-$(_readaccountconf_mutable LOOPIA_User)}"
  69. LOOPIA_Password="${LOOPIA_Password:-$(_readaccountconf_mutable LOOPIA_Password)}"
  70. if [ -z "$LOOPIA_Api" ]; then
  71. LOOPIA_Api="$LOOPIA_Api_Default"
  72. fi
  73. if [ -z "$LOOPIA_User" ] || [ -z "$LOOPIA_Password" ]; then
  74. LOOPIA_User=""
  75. LOOPIA_Password=""
  76. _err "A valid Loopia API user and password not provided."
  77. _err "Please provide a valid API user and try again."
  78. return 1
  79. fi
  80. return 0
  81. }
  82. _loopia_save_config() {
  83. if [ "$LOOPIA_Api" != "$LOOPIA_Api_Default" ]; then
  84. _saveaccountconf_mutable LOOPIA_Api "$LOOPIA_Api"
  85. fi
  86. _saveaccountconf_mutable LOOPIA_User "$LOOPIA_User"
  87. _saveaccountconf_mutable LOOPIA_Password "$LOOPIA_Password"
  88. }
  89. _loopia_get_records() {
  90. domain=$1
  91. sub_domain=$2
  92. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  93. <methodCall>
  94. <methodName>getZoneRecords</methodName>
  95. <params>
  96. <param>
  97. <value><string>%s</string></value>
  98. </param>
  99. <param>
  100. <value><string>%s</string></value>
  101. </param>
  102. <param>
  103. <value><string>%s</string></value>
  104. </param>
  105. <param>
  106. <value><string>%s</string></value>
  107. </param>
  108. </params>
  109. </methodCall>' "$LOOPIA_User" "$LOOPIA_Password" "$domain" "$sub_domain")
  110. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  111. if ! _contains "$response" "<array>"; then
  112. _err "Error"
  113. return 1
  114. fi
  115. return 0
  116. }
  117. _get_root() {
  118. domain=$1
  119. _debug "get root"
  120. domain=$1
  121. i=2
  122. p=1
  123. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  124. <methodCall>
  125. <methodName>getDomains</methodName>
  126. <params>
  127. <param>
  128. <value><string>%s</string></value>
  129. </param>
  130. <param>
  131. <value><string>%s</string></value>
  132. </param>
  133. </params>
  134. </methodCall>' "$LOOPIA_User" "$LOOPIA_Password")
  135. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  136. while true; do
  137. h=$(echo "$domain" | cut -d . -f $i-100)
  138. if [ -z "$h" ]; then
  139. #not valid
  140. return 1
  141. fi
  142. if _contains "$response" "$h"; then
  143. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  144. _domain="$h"
  145. return 0
  146. fi
  147. p=$i
  148. i=$(_math "$i" + 1)
  149. done
  150. return 1
  151. }
  152. _loopia_add_record() {
  153. domain=$1
  154. sub_domain=$2
  155. txtval=$3
  156. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  157. <methodCall>
  158. <methodName>addZoneRecord</methodName>
  159. <params>
  160. <param>
  161. <value><string>%s</string></value>
  162. </param>
  163. <param>
  164. <value><string>%s</string></value>
  165. </param>
  166. <param>
  167. <value><string>%s</string></value>
  168. </param>
  169. <param>
  170. <value><string>%s</string></value>
  171. </param>
  172. <param>
  173. <value>
  174. <struct>
  175. <member>
  176. <name>type</name>
  177. <value><string>TXT</string></value>
  178. </member>
  179. <member>
  180. <name>priority</name>
  181. <value><int>0</int></value>
  182. </member>
  183. <member>
  184. <name>ttl</name>
  185. <value><int>300</int></value>
  186. </member>
  187. <member>
  188. <name>rdata</name>
  189. <value><string>%s</string></value>
  190. </member>
  191. </struct>
  192. </value>
  193. </param>
  194. </params>
  195. </methodCall>' "$LOOPIA_User" "$LOOPIA_Password" "$domain" "$sub_domain" "$txtval")
  196. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  197. if ! _contains "$response" "OK"; then
  198. _err "Error"
  199. return 1
  200. fi
  201. return 0
  202. }
  203. _sub_domain_exists() {
  204. domain=$1
  205. sub_domain=$2
  206. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  207. <methodCall>
  208. <methodName>getSubdomains</methodName>
  209. <params>
  210. <param>
  211. <value><string>%s</string></value>
  212. </param>
  213. <param>
  214. <value><string>%s</string></value>
  215. </param>
  216. <param>
  217. <value><string>%s</string></value>
  218. </param>
  219. </params>
  220. </methodCall>' "$LOOPIA_User" "$LOOPIA_Password" "$domain")
  221. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  222. if _contains "$response" "$sub_domain"; then
  223. return 0
  224. fi
  225. return 1
  226. }
  227. _loopia_add_sub_domain() {
  228. domain=$1
  229. sub_domain=$2
  230. if _sub_domain_exists "$domain" "$sub_domain"; then
  231. return 0
  232. fi
  233. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  234. <methodCall>
  235. <methodName>addSubdomain</methodName>
  236. <params>
  237. <param>
  238. <value><string>%s</string></value>
  239. </param>
  240. <param>
  241. <value><string>%s</string></value>
  242. </param>
  243. <param>
  244. <value><string>%s</string></value>
  245. </param>
  246. <param>
  247. <value><string>%s</string></value>
  248. </param>
  249. </params>
  250. </methodCall>' "$LOOPIA_User" "$LOOPIA_Password" "$domain" "$sub_domain")
  251. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  252. if ! _contains "$response" "OK"; then
  253. _err "Error"
  254. return 1
  255. fi
  256. return 0
  257. }