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.

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