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.

286 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
  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. LOOPIA_User="${LOOPIA_User:-$(_readaccountconf_mutable LOOPIA_User)}"
  13. LOOPIA_Password="${LOOPIA_Password:-$(_readaccountconf_mutable LOOPIA_Password)}"
  14. if [ -z "$LOOPIA_User" ] || [ -z "$LOOPIA_Password" ]; then
  15. LOOPIA_User=""
  16. LOOPIA_Password=""
  17. _err "You don't specify loopia user and password yet."
  18. _err "Please create you key and try again."
  19. return 1
  20. fi
  21. #save the api key and email to the account conf file.
  22. _saveaccountconf_mutable LOOPIA_User "$LOOPIA_User"
  23. _saveaccountconf_mutable LOOPIA_Password "$LOOPIA_Password"
  24. _debug "First detect the root zone"
  25. if ! _get_root "$fulldomain"; then
  26. _err "invalid domain"
  27. return 1
  28. fi
  29. _debug _sub_domain "$_sub_domain"
  30. _debug _domain "$_domain"
  31. _info "Adding record"
  32. _loopia_add_sub_domain "$_domain" "$_sub_domain"
  33. _loopia_add_record "$_domain" "$_sub_domain" "$txtvalue"
  34. }
  35. dns_loopia_rm() {
  36. fulldomain=$1
  37. txtvalue=$2
  38. LOOPIA_User="${LOOPIA_User:-$(_readaccountconf_mutable LOOPIA_User)}"
  39. LOOPIA_Password="${LOOPIA_Password:-$(_readaccountconf_mutable LOOPIA_Password)}"
  40. if [ -z "$LOOPIA_User" ] || [ -z "$LOOPIA_Password" ]; then
  41. LOOPIA_User=""
  42. LOOPIA_Password=""
  43. _err "You don't specify LOOPIA user and password yet."
  44. _err "Please create you key and try again."
  45. return 1
  46. fi
  47. #save the api key and email to the account conf file.
  48. _saveaccountconf_mutable LOOPIA_User "$LOOPIA_User"
  49. _saveaccountconf_mutable LOOPIA_Password "$LOOPIA_Password"
  50. _debug "First detect the root zone"
  51. if ! _get_root "$fulldomain"; then
  52. _err "invalid domain"
  53. return 1
  54. fi
  55. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  56. <methodCall>
  57. <methodName>removeSubdomain</methodName>
  58. <params>
  59. <param>
  60. <value><string>%s</string></value>
  61. </param>
  62. <param>
  63. <value><string>%s</string></value>
  64. </param>
  65. <param>
  66. <value><string>%s</string></value>
  67. </param>
  68. <param>
  69. <value><string>%s</string></value>
  70. </param>
  71. </params>
  72. </methodCall>' $LOOPIA_User $LOOPIA_Password "$_domain" "$_sub_domain")
  73. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  74. if ! _contains "$response" "OK"; then
  75. _err "Error could not get txt records"
  76. return 1
  77. fi
  78. }
  79. #################### Private functions below ##################################
  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. }