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.

355 lines
7.9 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/local/bin/sh
  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. xml_content=$(printf '<?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" >/dev/null; then
  68. _info "Adding record"
  69. _inwx_add_record "$_domain" "$_sub_domain" "$txtvalue"
  70. else
  71. _record_id=$(printf '%s' "$response" | _egrep_o '.*(<member><name>record){1}(.*)([0-9]+){1}' | _egrep_o '<name>id<\/name><value><int>[0-9]+' | _egrep_o '[0-9]+')
  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. xml_content=$(printf '<?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" >/dev/null; then
  136. _info "Do not need to delete record"
  137. else
  138. _record_id=$(printf '%s' "$response" | _egrep_o '.*(<member><name>record){1}(.*)([0-9]+){1}' | _egrep_o '<name>id<\/name><value><int>[0-9]+' | _egrep_o '[0-9]+')
  139. _info "Deleting record"
  140. _inwx_delete_record "$_record_id"
  141. fi
  142. }
  143. #################### Private functions below ##################################
  144. _inwx_login() {
  145. xml_content=$(printf '<?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. _H1=$(_inwx_login)
  179. export _H1
  180. xml_content='<?xml version="1.0" encoding="UTF-8"?>
  181. <methodCall>
  182. <methodName>nameserver.list</methodName>
  183. </methodCall>'
  184. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  185. while true; do
  186. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  187. _debug h "$h"
  188. if [ -z "$h" ]; then
  189. #not valid
  190. return 1
  191. fi
  192. if _contains "$response" "$h"; then
  193. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  194. _domain="$h"
  195. return 0
  196. fi
  197. p=$i
  198. i=$(_math "$i" + 1)
  199. done
  200. return 1
  201. }
  202. _inwx_delete_record() {
  203. record_id=$1
  204. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  205. <methodCall>
  206. <methodName>nameserver.deleteRecord</methodName>
  207. <params>
  208. <param>
  209. <value>
  210. <struct>
  211. <member>
  212. <name>id</name>
  213. <value>
  214. <int>%s</int>
  215. </value>
  216. </member>
  217. </struct>
  218. </value>
  219. </param>
  220. </params>
  221. </methodCall>' "$record_id")
  222. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  223. if ! printf "%s" "$response" | grep "Command completed successfully" >/dev/null; then
  224. _err "Error"
  225. return 1
  226. fi
  227. return 0
  228. }
  229. _inwx_update_record() {
  230. record_id=$1
  231. txtval=$2
  232. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  233. <methodCall>
  234. <methodName>nameserver.updateRecord</methodName>
  235. <params>
  236. <param>
  237. <value>
  238. <struct>
  239. <member>
  240. <name>content</name>
  241. <value>
  242. <string>%s</string>
  243. </value>
  244. </member>
  245. <member>
  246. <name>id</name>
  247. <value>
  248. <int>%s</int>
  249. </value>
  250. </member>
  251. </struct>
  252. </value>
  253. </param>
  254. </params>
  255. </methodCall>' "$txtval" "$record_id")
  256. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  257. if ! printf "%s" "$response" | grep "Command completed successfully" >/dev/null; then
  258. _err "Error"
  259. return 1
  260. fi
  261. return 0
  262. }
  263. _inwx_add_record() {
  264. domain=$1
  265. sub_domain=$2
  266. txtval=$3
  267. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  268. <methodCall>
  269. <methodName>nameserver.createRecord</methodName>
  270. <params>
  271. <param>
  272. <value>
  273. <struct>
  274. <member>
  275. <name>domain</name>
  276. <value>
  277. <string>%s</string>
  278. </value>
  279. </member>
  280. <member>
  281. <name>type</name>
  282. <value>
  283. <string>TXT</string>
  284. </value>
  285. </member>
  286. <member>
  287. <name>content</name>
  288. <value>
  289. <string>%s</string>
  290. </value>
  291. </member>
  292. <member>
  293. <name>name</name>
  294. <value>
  295. <string>%s</string>
  296. </value>
  297. </member>
  298. </struct>
  299. </value>
  300. </param>
  301. </params>
  302. </methodCall>' "$domain" "$txtval" "$sub_domain")
  303. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  304. if ! printf "%s" "$response" | grep "Command completed successfully" >/dev/null; then
  305. _err "Error"
  306. return 1
  307. fi
  308. return 0
  309. }