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.

302 lines
6.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
  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. <struct>
  174. <member>
  175. <name>type</name>
  176. <value><string>TXT</string></value>
  177. </member>
  178. <member>
  179. <name>priority</name>
  180. <value><int>0</int></value>
  181. </member>
  182. <member>
  183. <name>ttl</name>
  184. <value><int>60</int></value>
  185. </member>
  186. <member>
  187. <name>rdata</name>
  188. <value><string>%s</string></value>
  189. </member>
  190. </struct>
  191. </param>
  192. </params>
  193. </methodCall>' $LOOPIA_User $LOOPIA_Password "$domain" "$sub_domain" "$txtval")
  194. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  195. if ! _contains "$response" "OK"; then
  196. _err "Error"
  197. return 1
  198. fi
  199. return 0
  200. }
  201. _sub_domain_exists() {
  202. domain=$1
  203. sub_domain=$2
  204. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  205. <methodCall>
  206. <methodName>getSubdomains</methodName>
  207. <params>
  208. <param>
  209. <value><string>%s</string></value>
  210. </param>
  211. <param>
  212. <value><string>%s</string></value>
  213. </param>
  214. <param>
  215. <value><string>%s</string></value>
  216. </param>
  217. </params>
  218. </methodCall>' $LOOPIA_User $LOOPIA_Password "$domain")
  219. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  220. if _contains "$response" "$sub_domain"; then
  221. return 0
  222. fi
  223. return 1
  224. }
  225. _loopia_add_sub_domain() {
  226. domain=$1
  227. sub_domain=$2
  228. if _sub_domain_exists "$domain" "$sub_domain"; then
  229. return 0
  230. fi
  231. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  232. <methodCall>
  233. <methodName>addSubdomain</methodName>
  234. <params>
  235. <param>
  236. <value><string>%s</string></value>
  237. </param>
  238. <param>
  239. <value><string>%s</string></value>
  240. </param>
  241. <param>
  242. <value><string>%s</string></value>
  243. </param>
  244. <param>
  245. <value><string>%s</string></value>
  246. </param>
  247. </params>
  248. </methodCall>' $LOOPIA_User $LOOPIA_Password "$domain" "$sub_domain")
  249. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  250. if ! _contains "$response" "OK"; then
  251. _err "Error"
  252. return 1
  253. fi
  254. return 0
  255. }