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.

420 lines
9.4 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 -i "^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. <params>
  223. <param>
  224. <value>
  225. <struct>
  226. <member>
  227. <name>pagelimit</name>
  228. <value>
  229. <int>9999</int>
  230. </value>
  231. </member>
  232. </struct>
  233. </value>
  234. </param>
  235. </params>
  236. </methodCall>'
  237. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  238. while true; do
  239. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  240. _debug h "$h"
  241. if [ -z "$h" ]; then
  242. #not valid
  243. return 1
  244. fi
  245. if _contains "$response" "$h"; then
  246. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  247. _domain="$h"
  248. return 0
  249. fi
  250. p=$i
  251. i=$(_math "$i" + 1)
  252. done
  253. return 1
  254. }
  255. _inwx_delete_record() {
  256. record_id=$1
  257. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  258. <methodCall>
  259. <methodName>nameserver.deleteRecord</methodName>
  260. <params>
  261. <param>
  262. <value>
  263. <struct>
  264. <member>
  265. <name>id</name>
  266. <value>
  267. <int>%s</int>
  268. </value>
  269. </member>
  270. </struct>
  271. </value>
  272. </param>
  273. </params>
  274. </methodCall>' "$record_id")
  275. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  276. if ! printf "%s" "$response" | grep "Command completed successfully" >/dev/null; then
  277. _err "Error"
  278. return 1
  279. fi
  280. return 0
  281. }
  282. _inwx_update_record() {
  283. record_id=$1
  284. txtval=$2
  285. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  286. <methodCall>
  287. <methodName>nameserver.updateRecord</methodName>
  288. <params>
  289. <param>
  290. <value>
  291. <struct>
  292. <member>
  293. <name>content</name>
  294. <value>
  295. <string>%s</string>
  296. </value>
  297. </member>
  298. <member>
  299. <name>id</name>
  300. <value>
  301. <int>%s</int>
  302. </value>
  303. </member>
  304. </struct>
  305. </value>
  306. </param>
  307. </params>
  308. </methodCall>' "$txtval" "$record_id")
  309. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  310. if ! printf "%s" "$response" | grep "Command completed successfully" >/dev/null; then
  311. _err "Error"
  312. return 1
  313. fi
  314. return 0
  315. }
  316. _inwx_add_record() {
  317. domain=$1
  318. sub_domain=$2
  319. txtval=$3
  320. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  321. <methodCall>
  322. <methodName>nameserver.createRecord</methodName>
  323. <params>
  324. <param>
  325. <value>
  326. <struct>
  327. <member>
  328. <name>domain</name>
  329. <value>
  330. <string>%s</string>
  331. </value>
  332. </member>
  333. <member>
  334. <name>type</name>
  335. <value>
  336. <string>TXT</string>
  337. </value>
  338. </member>
  339. <member>
  340. <name>content</name>
  341. <value>
  342. <string>%s</string>
  343. </value>
  344. </member>
  345. <member>
  346. <name>name</name>
  347. <value>
  348. <string>%s</string>
  349. </value>
  350. </member>
  351. </struct>
  352. </value>
  353. </param>
  354. </params>
  355. </methodCall>' "$domain" "$txtval" "$sub_domain")
  356. response="$(_post "$xml_content" "$INWX_Api" "" "POST")"
  357. if ! printf "%s" "$response" | grep "Command completed successfully" >/dev/null; then
  358. _err "Error"
  359. return 1
  360. fi
  361. return 0
  362. }