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.

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