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.

365 lines
7.9 KiB

  1. #!/usr/local/bin/bash
  2. #
  3. #INWX_User="username"
  4. #
  5. #INWX_Password="password"
  6. INWX_Api="https://api.domrobot.com/xmlrpc/"
  7. ######## Public functions #####################
  8. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_inwx_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. INWX_User="${INWX_User:-$(_readaccountconf_mutable INWX_User)}"
  13. INWX_Password="${INWX_Password:-$(_readaccountconf_mutable INWX_Password)}"
  14. if [ -z "$INWX_User" ] || [ -z "$INWX_Password" ]; then
  15. INWX_User=""
  16. INWX_Password=""
  17. _err "You don't specify inwx 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 INWX_User "$INWX_User"
  23. _saveaccountconf_mutable INWX_Password "$INWX_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. _debug "Getting txt records"
  32. printf -v xml_content '<?xml version="1.0" encoding="UTF-8"?>
  33. <methodCall>
  34. <methodName>nameserver.info</methodName>
  35. <params>
  36. <param>
  37. <value>
  38. <struct>
  39. <member>
  40. <name>domain</name>
  41. <value>
  42. <string>%s</string>
  43. </value>
  44. </member>
  45. <member>
  46. <name>type</name>
  47. <value>
  48. <string>TXT</string>
  49. </value>
  50. </member>
  51. <member>
  52. <name>name</name>
  53. <value>
  54. <string>%s</string>
  55. </value>
  56. </member>
  57. </struct>
  58. </value>
  59. </param>
  60. </params>
  61. </methodCall>' $_domain $_sub_domain
  62. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  63. if ! printf "%s" "$response" | grep "Command completed successfully" > /dev/null; then
  64. _err "Error could net get txt records"
  65. return 1
  66. fi
  67. if ! printf "%s" "$response" | grep "count" -q; then
  68. _info "Adding record"
  69. _inwx_add_record $_domain $_sub_domain $txtvalue
  70. else
  71. _record_id=$(printf '%s' $response | sed -nE 's/.*(<member><name>record){1}(.*)(<name>id<\/name><value><int>)([0-9]+){1}.*/\4/p')
  72. _info "Updating record"
  73. _inwx_update_record $_record_id $txtvalue
  74. fi
  75. }
  76. #fulldomain txtvalue
  77. dns_inwx_rm() {
  78. fulldomain=$1
  79. txtvalue=$2
  80. INWX_User="${INWX_User:-$(_readaccountconf_mutable INWX_User)}"
  81. INWX_Password="${INWX_Password:-$(_readaccountconf_mutable INWX_Password)}"
  82. if [ -z "$INWX_User" ] || [ -z "$INWX_Password" ]; then
  83. INWX_User=""
  84. INWX_Password=""
  85. _err "You don't specify inwx user and password yet."
  86. _err "Please create you key and try again."
  87. return 1
  88. fi
  89. #save the api key and email to the account conf file.
  90. _saveaccountconf_mutable INWX_User "$INWX_User"
  91. _saveaccountconf_mutable INWX_Password "$INWX_Password"
  92. _debug "First detect the root zone"
  93. if ! _get_root "$fulldomain"; then
  94. _err "invalid domain"
  95. return 1
  96. fi
  97. _debug _sub_domain "$_sub_domain"
  98. _debug _domain "$_domain"
  99. _debug "Getting txt records"
  100. printf -v xml_content '<?xml version="1.0" encoding="UTF-8"?>
  101. <methodCall>
  102. <methodName>nameserver.info</methodName>
  103. <params>
  104. <param>
  105. <value>
  106. <struct>
  107. <member>
  108. <name>domain</name>
  109. <value>
  110. <string>%s</string>
  111. </value>
  112. </member>
  113. <member>
  114. <name>type</name>
  115. <value>
  116. <string>TXT</string>
  117. </value>
  118. </member>
  119. <member>
  120. <name>name</name>
  121. <value>
  122. <string>%s</string>
  123. </value>
  124. </member>
  125. </struct>
  126. </value>
  127. </param>
  128. </params>
  129. </methodCall>' $_domain $_sub_domain
  130. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  131. if ! printf "%s" "$response" | grep "Command completed successfully" > /dev/null; then
  132. _err "Error could not get txt records"
  133. return 1
  134. fi
  135. if ! printf "%s" "$response" | grep "count" -q; then
  136. _info "Do not need to delete record"
  137. else
  138. _record_id=$(printf '%s' $response | sed -nE 's/.*(<member><name>record){1}(.*)(<name>id<\/name><value><int>)([0-9]+){1}.*/\4/p')
  139. _info "Deleting record"
  140. _inwx_delete_record $_record_id
  141. fi
  142. }
  143. #################### Private functions below ##################################
  144. _inwx_login() {
  145. printf -v xml_content '<?xml version="1.0" encoding="UTF-8"?>
  146. <methodCall>
  147. <methodName>account.login</methodName>
  148. <params>
  149. <param>
  150. <value>
  151. <struct>
  152. <member>
  153. <name>user</name>
  154. <value>
  155. <string>%s</string>
  156. </value>
  157. </member>
  158. <member>
  159. <name>pass</name>
  160. <value>
  161. <string>%s</string>
  162. </value>
  163. </member>
  164. </struct>
  165. </value>
  166. </param>
  167. </params>
  168. </methodCall>' $INWX_User $INWX_Password
  169. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  170. printf "Cookie: %s" "$(grep "domrobot=" "$HTTP_HEADER" | grep "^Set-Cookie:" | _tail_n 1 | _egrep_o 'domrobot=[^;]*;' | tr -d ';')"
  171. }
  172. _get_root() {
  173. domain=$1
  174. _debug "get root"
  175. domain=$1
  176. i=2
  177. p=1
  178. export _H1=$(_inwx_login)
  179. printf -v xml_content '<?xml version="1.0" encoding="UTF-8"?>
  180. <methodCall>
  181. <methodName>nameserver.list</methodName>
  182. </methodCall>'
  183. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  184. while true; do
  185. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  186. _debug h "$h"
  187. if [ -z "$h" ]; then
  188. #not valid
  189. return 1
  190. fi
  191. if _contains "$response" "$h"; then
  192. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  193. _domain="$h"
  194. return 0
  195. fi
  196. p=$i
  197. i=$(_math "$i" + 1)
  198. done
  199. return 1
  200. }
  201. _inwx_delete_record() {
  202. record_id=$1
  203. printf -v xml_content '<?xml version="1.0" encoding="UTF-8"?>
  204. <methodCall>
  205. <methodName>nameserver.deleteRecord</methodName>
  206. <params>
  207. <param>
  208. <value>
  209. <struct>
  210. <member>
  211. <name>id</name>
  212. <value>
  213. <int>%s</int>
  214. </value>
  215. </member>
  216. </struct>
  217. </value>
  218. </param>
  219. </params>
  220. </methodCall>' $record_id
  221. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  222. if ! printf "%s" "$response" | grep "Command completed successfully" >/dev/null; then
  223. _err "Error"
  224. return 1
  225. fi
  226. return 0
  227. }
  228. _inwx_update_record() {
  229. record_id=$1
  230. txtval=$2
  231. printf -v xml_content '<?xml version="1.0" encoding="UTF-8"?>
  232. <methodCall>
  233. <methodName>nameserver.updateRecord</methodName>
  234. <params>
  235. <param>
  236. <value>
  237. <struct>
  238. <member>
  239. <name>content</name>
  240. <value>
  241. <string>%s</string>
  242. </value>
  243. </member>
  244. <member>
  245. <name>id</name>
  246. <value>
  247. <int>%s</int>
  248. </value>
  249. </member>
  250. </struct>
  251. </value>
  252. </param>
  253. </params>
  254. </methodCall>' $txtval $record_id
  255. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  256. if ! printf "%s" "$response" | grep "Command completed successfully" >/dev/null; then
  257. _err "Error"
  258. return 1
  259. fi
  260. return 0
  261. }
  262. _inwx_add_record() {
  263. domain=$1
  264. sub_domain=$2
  265. txtval=$3
  266. _debug domain: $domain
  267. _debug value: $txtval
  268. _debug subd: $sub_domain
  269. printf -v xml_content '<?xml version="1.0" encoding="UTF-8"?>
  270. <methodCall>
  271. <methodName>nameserver.createRecord</methodName>
  272. <params>
  273. <param>
  274. <value>
  275. <struct>
  276. <member>
  277. <name>domain</name>
  278. <value>
  279. <string>%s</string>
  280. </value>
  281. </member>
  282. <member>
  283. <name>type</name>
  284. <value>
  285. <string>TXT</string>
  286. </value>
  287. </member>
  288. <member>
  289. <name>content</name>
  290. <value>
  291. <string>%s</string>
  292. </value>
  293. </member>
  294. <member>
  295. <name>name</name>
  296. <value>
  297. <string>%s</string>
  298. </value>
  299. </member>
  300. </struct>
  301. </value>
  302. </param>
  303. </params>
  304. </methodCall>' $domain $txtval $sub_domain
  305. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  306. if ! printf "%s" "$response" | grep "Command completed successfully" >/dev/null; then
  307. _err "Error"
  308. return 1
  309. fi
  310. return 0
  311. }