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.

227 lines
5.5 KiB

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_record "$_domain" "$_sub_domain"
  33. _loopia_update_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. _get_root() {
  81. domain=$1
  82. _debug "get root"
  83. domain=$1
  84. i=2
  85. p=1
  86. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  87. <methodCall>
  88. <methodName>getDomains</methodName>
  89. <params>
  90. <param>
  91. <value><string>%s</string></value>
  92. </param>
  93. <param>
  94. <value><string>%s</string></value>
  95. </param>
  96. </params>
  97. </methodCall>' $LOOPIA_User $LOOPIA_Password)
  98. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  99. while true; do
  100. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  101. if [ -z "$h" ]; then
  102. #not valid
  103. return 1
  104. fi
  105. if _contains "$response" "$h"; then
  106. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  107. _domain="$h"
  108. return 0
  109. fi
  110. p=$i
  111. i=$(_math "$i" + 1)
  112. done
  113. return 1
  114. }
  115. _loopia_update_record() {
  116. domain=$1
  117. sub_domain=$2
  118. txtval=$3
  119. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  120. <methodCall>
  121. <methodName>updateZoneRecord</methodName>
  122. <params>
  123. <param>
  124. <value><string>%s</string></value>
  125. </param>
  126. <param>
  127. <value><string>%s</string></value>
  128. </param>
  129. <param>
  130. <value><string>%s</string></value>
  131. </param>
  132. <param>
  133. <value><string>%s</string></value>
  134. </param>
  135. <param>
  136. <struct>
  137. <member>
  138. <name>type</name>
  139. <value><string>TXT</string></value>
  140. </member>
  141. <member>
  142. <name>priority</name>
  143. <value><int>0</int></value>
  144. </member>
  145. <member>
  146. <name>ttl</name>
  147. <value><int>60</int></value>
  148. </member>
  149. <member>
  150. <name>rdata</name>
  151. <value><string>%s</string></value>
  152. </member>
  153. <member>
  154. <name>record_id</name>
  155. <value><int>0</int></value>
  156. </member>
  157. </struct>
  158. </param>
  159. </params>
  160. </methodCall>' $LOOPIA_User $LOOPIA_Password "$domain" "$sub_domain" "$txtval")
  161. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  162. if ! printf "%s" "$response" | grep "OK" >/dev/null; then
  163. _err "Error"
  164. return 1
  165. fi
  166. return 0
  167. }
  168. _loopia_add_record() {
  169. domain=$1
  170. sub_domain=$2
  171. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  172. <methodCall>
  173. <methodName>addSubdomain</methodName>
  174. <params>
  175. <param>
  176. <value><string>%s</string></value>
  177. </param>
  178. <param>
  179. <value><string>%s</string></value>
  180. </param>
  181. <param>
  182. <value><string>%s</string></value>
  183. </param>
  184. <param>
  185. <value><string>%s</string></value>
  186. </param>
  187. </params>
  188. </methodCall>' $LOOPIA_User $LOOPIA_Password "$domain" "$sub_domain")
  189. response="$(_post "$xml_content" "$LOOPIA_Api" "" "POST")"
  190. if ! printf "%s" "$response" | grep "OK" >/dev/null; then
  191. _err "Error"
  192. return 1
  193. fi
  194. return 0
  195. }