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