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.

291 lines
6.4 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
  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. #save the api key and email to the account conf file.
  16. _saveaccountconf_mutable LOOPIA_User "$LOOPIA_User"
  17. _saveaccountconf_mutable LOOPIA_Password "$LOOPIA_Password"
  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. #save the api key and email to the account conf file.
  36. _saveaccountconf_mutable LOOPIA_User "$LOOPIA_User"
  37. _saveaccountconf_mutable LOOPIA_Password "$LOOPIA_Password"
  38. _debug "First detect the root zone"
  39. if ! _get_root "$fulldomain"; then
  40. _err "invalid domain"
  41. return 1
  42. fi
  43. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  44. <methodCall>
  45. <methodName>removeSubdomain</methodName>
  46. <params>
  47. <param>
  48. <value><string>%s</string></value>
  49. </param>
  50. <param>
  51. <value><string>%s</string></value>
  52. </param>
  53. <param>
  54. <value><string>%s</string></value>
  55. </param>
  56. <param>
  57. <value><string>%s</string></value>
  58. </param>
  59. </params>
  60. </methodCall>' $LOOPIA_User $LOOPIA_Password "$_domain" "$_sub_domain")
  61. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  62. if ! _contains "$response" "OK"; then
  63. _err "Error could not get txt records"
  64. return 1
  65. fi
  66. }
  67. #################### Private functions below ##################################
  68. _loopia_load_config() {
  69. LOOPIA_User="${LOOPIA_User:-$(_readaccountconf_mutable LOOPIA_User)}"
  70. LOOPIA_Password="${LOOPIA_Password:-$(_readaccountconf_mutable LOOPIA_Password)}"
  71. if [ -z "$LOOPIA_User" ] || [ -z "$LOOPIA_Password" ]; then
  72. LOOPIA_User=""
  73. LOOPIA_Password=""
  74. _err "You don't specify loopia user and password yet."
  75. _err "Please create you key and try again."
  76. return 1
  77. fi
  78. return 0
  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. }