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.

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