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.

358 lines
9.6 KiB

  1. #!/usr/bin/env sh
  2. #This is the euserv.eu api wrapper for acme.sh
  3. #
  4. #Author: Michael Brueckner
  5. #Report Bugs: https://www.github.com/initit/acme.sh or mbr@initit.de
  6. #
  7. #EUSERV_Username="username"
  8. #
  9. #EUSERV_Password="password"
  10. #
  11. # Dependencies:
  12. # -------------
  13. # - none -
  14. EUSERV_Api="https://api.euserv.net"
  15. ######## Public functions #####################
  16. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  17. dns_euserv_add() {
  18. fulldomain="$(echo "$1" | _lower_case)"
  19. txtvalue=$2
  20. EUSERV_Username="${EUSERV_Username:-$(_readaccountconf_mutable EUSERV_Username)}"
  21. EUSERV_Password="${EUSERV_Password:-$(_readaccountconf_mutable EUSERV_Password)}"
  22. if [ -z "$EUSERV_Username" ] || [ -z "$EUSERV_Password" ]; then
  23. EUSERV_Username=""
  24. EUSERV_Password=""
  25. _err "You don't specify euserv user and password yet."
  26. _err "Please create your key and try again."
  27. return 1
  28. fi
  29. #save the user and email to the account conf file.
  30. _saveaccountconf_mutable EUSERV_Username "$EUSERV_Username"
  31. _saveaccountconf_mutable EUSERV_Password "$EUSERV_Password"
  32. _debug "First detect the root zone"
  33. if ! _get_root "$fulldomain"; then
  34. _err "invalid domain"
  35. return 1
  36. fi
  37. _debug _sub_domain "$_sub_domain"
  38. _debug _domain "$_domain"
  39. _info "Adding record"
  40. _euserv_add_record "$_domain" "$_sub_domain" "$txtvalue"
  41. }
  42. #fulldomain txtvalue
  43. dns_euserv_rm() {
  44. fulldomain="$(echo "$1" | _lower_case)"
  45. txtvalue=$2
  46. EUSERV_Username="${EUSERV_Username:-$(_readaccountconf_mutable EUSERV_Username)}"
  47. EUSERV_Password="${EUSERV_Password:-$(_readaccountconf_mutable EUSERV_Password)}"
  48. if [ -z "$EUSERV_Username" ] || [ -z "$EUSERV_Password" ]; then
  49. EUSERV_Username=""
  50. EUSERV_Password=""
  51. _err "You don't specify euserv user and password yet."
  52. _err "Please create your key and try again."
  53. return 1
  54. fi
  55. #save the user and email to the account conf file.
  56. _saveaccountconf_mutable EUSERV_Username "$EUSERV_Username"
  57. _saveaccountconf_mutable EUSERV_Password "$EUSERV_Password"
  58. _debug "First detect the root zone"
  59. if ! _get_root "$fulldomain"; then
  60. _err "invalid domain"
  61. return 1
  62. fi
  63. _debug "_sub_domain" "$_sub_domain"
  64. _debug "_domain" "$_domain"
  65. _debug "Getting txt records"
  66. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  67. <methodCall>
  68. <methodName>domain.dns_get_active_records</methodName>
  69. <params>
  70. <param>
  71. <value>
  72. <struct>
  73. <member>
  74. <name>login</name>
  75. <value>
  76. <string>%s</string>
  77. </value>
  78. </member>
  79. <member>
  80. <name>password</name>
  81. <value>
  82. <string>%s</string>
  83. </value>
  84. </member>
  85. <member>
  86. <name>domain_id</name>
  87. <value>
  88. <int>%s</int>
  89. </value>
  90. </member>
  91. </struct>
  92. </value>
  93. </param>
  94. </params>
  95. </methodCall>' "$EUSERV_Username" "$EUSERV_Password" "$_euserv_domain_id")
  96. export _H1="Content-Type: text/xml"
  97. response="$(_post "$xml_content" "$EUSERV_Api" "" "POST")"
  98. ok="$(printf '%s' "$response" | grep "<member><name>status</name><value><i4>100</i4></value></member>")"
  99. if [ -z "$ok" ]; then
  100. _err "Error could not get txt records"
  101. _debug "xml_content" "$xml_content"
  102. _debug "response" "$response"
  103. return 1
  104. fi
  105. # _startLine=$(printf '%s' "$_euserv_domain_orders" | grep -n '>domain_name<.*>'"$domain"'<' | cut -d ':' -f 1 )
  106. # _euserv_domain_id=$(printf '%s' "$_euserv_domain_orders" | sed -n "$_startLine"',$p' | grep '>domain_id<' | head -n 1 | sed 's/.*<i4>\([0-9]*\)<\/i4>.*/\1/' )
  107. if ! printf "%s" "$response" | grep '>dns_record_content<.*>'"$txtvalue"'<' >/dev/null; then
  108. _info "Do not need to delete record"
  109. else
  110. # find block where txtvalue is in. the record_id is allways prior this line!
  111. _endLine=$(printf '%s' "$response" | grep -n '>dns_record_content<.*>'"$txtvalue"'<' | cut -d ':' -f 1 )
  112. # record_id is the last entry with a number, identified by the postfix of </name><value><struct>
  113. _record_id=$(printf '%s' "$response" | sed -n '1,'"$_endLine"'p' | grep '</name><value><struct>' | tail -n 1 | sed 's/.*<name>\([0-9]*\)<\/name>.*/\1/' )
  114. _info "Deleting record"
  115. _euserv_delete_record "$_record_id"
  116. fi
  117. }
  118. #################### Private functions below ##################################
  119. _euserv_get_domain_orders() {
  120. # returns: _euserv_domain_orders
  121. _debug "get domain_orders"
  122. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  123. <methodCall>
  124. <methodName>domain.get_domain_orders</methodName>
  125. <params>
  126. <param>
  127. <value>
  128. <struct>
  129. <member>
  130. <name>login</name>
  131. <value><string>%s</string></value>
  132. </member>
  133. <member>
  134. <name>password</name>
  135. <value><string>%s</string></value>
  136. </member>
  137. </struct>
  138. </value>
  139. </param>
  140. </params>
  141. </methodCall>' "$EUSERV_Username" "$EUSERV_Password")
  142. export _H1="Content-Type: text/xml"
  143. response="$(_post "$xml_content" "$EUSERV_Api" "" "POST")"
  144. ok="$(printf '%s' "$response" | grep "<member><name>status</name><value><i4>100</i4></value></member>")"
  145. if [ -z "$ok" ]; then
  146. _err "Error could not get domain orders"
  147. _debug "xml_content" "$xml_content"
  148. _debug "response" "$response"
  149. return 1
  150. fi
  151. _euserv_domain_orders="$response"
  152. return 0
  153. }
  154. _euserv_get_domain_id() {
  155. # returns: _euserv_domain_id
  156. domain=$1
  157. _debug "get domain_id"
  158. _startLine=$(printf '%s' "$_euserv_domain_orders" | grep -n '>domain_name<.*>'"$domain"'<' | cut -d ':' -f 1 )
  159. _euserv_domain_id=$(printf '%s' "$_euserv_domain_orders" | sed -n "$_startLine"',$p' | grep '>domain_id<' | head -n 1 | sed 's/.*<i4>\([0-9]*\)<\/i4>.*/\1/' )
  160. if [ -z "$_euserv_domain_id" ] ; then
  161. _err "Could not find domain_id for domain $domain"
  162. _debug "_euserv_domain_orders" "$_euserv_domain_orders"
  163. return 1
  164. fi
  165. return 0
  166. }
  167. _get_root() {
  168. domain=$1
  169. _debug "get root"
  170. # Just to read the domain_orders once
  171. domain=$1
  172. i=2
  173. p=1
  174. _euserv_get_domain_orders
  175. response="$_euserv_domain_orders"
  176. while true; do
  177. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  178. _debug h "$h"
  179. if [ -z "$h" ]; then
  180. #not valid
  181. return 1
  182. fi
  183. if _contains "$response" "$h"; then
  184. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  185. _domain="$h"
  186. if ! _euserv_get_domain_id "$_domain"; then
  187. _err "invalid domain"
  188. return 1
  189. fi
  190. return 0
  191. fi
  192. p=$i
  193. i=$(_math "$i" + 1)
  194. done
  195. return 1
  196. }
  197. # TODO
  198. _euserv_delete_record() {
  199. record_id=$1
  200. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  201. <methodCall>
  202. <methodName>domain.dns_delete_record</methodName>
  203. <params>
  204. <param>
  205. <value>
  206. <struct>
  207. <member>
  208. <name>login</name>
  209. <value>
  210. <string>%s</string>
  211. </value>
  212. </member>
  213. <member>
  214. <name>password</name>
  215. <value>
  216. <string>%s</string>
  217. </value>
  218. </member>
  219. <member>
  220. <name>dns_record_id</name>
  221. <value>
  222. <int>%s</int>
  223. </value>
  224. </member>
  225. </struct>
  226. </value>
  227. </param>
  228. </params>
  229. </methodCall>' "$EUSERV_Username" "$EUSERV_Password" "$record_id")
  230. export _H1="Content-Type: text/xml"
  231. response="$(_post "$xml_content" "$EUSERV_Api" "" "POST")"
  232. ok="$(printf '%s' "$response" | grep "<member><name>status</name><value><i4>100</i4></value></member>")"
  233. if [ -z "$ok" ]; then
  234. _err "Error deleting record"
  235. _debug "xml_content" "$xml_content"
  236. _debug "response" "$response"
  237. return 1
  238. fi
  239. return 0
  240. }
  241. _euserv_add_record() {
  242. domain=$1
  243. sub_domain=$2
  244. txtval=$3
  245. xml_content=$(printf '<?xml version="1.0" encoding="UTF-8"?>
  246. <methodCall>
  247. <methodName>domain.dns_create_record</methodName>
  248. <params>
  249. <param>
  250. <value>
  251. <struct>
  252. <member>
  253. <name>login</name>
  254. <value>
  255. <string>%s</string>
  256. </value>
  257. </member>
  258. <member>
  259. <name>password</name>
  260. <value>
  261. <string>%s</string></value>
  262. </member>
  263. <member>
  264. <name>domain_id</name>
  265. <value>
  266. <int>%s</int>
  267. </value>
  268. </member>
  269. <member>
  270. <name>dns_record_subdomain</name>
  271. <value>
  272. <string>%s</string>
  273. </value>
  274. </member>
  275. <member>
  276. <name>dns_record_type</name>
  277. <value>
  278. <string>TXT</string>
  279. </value>
  280. </member>
  281. <member>
  282. <name>dns_record_value</name>
  283. <value>
  284. <string>%s</string>
  285. </value>
  286. </member>
  287. <member>
  288. <name>dns_record_ttl</name>
  289. <value>
  290. <int>300</int>
  291. </value>
  292. </member>
  293. </struct>
  294. </value>
  295. </param>
  296. </params>
  297. </methodCall>' "$EUSERV_Username" "$EUSERV_Password" "$_euserv_domain_id" "$sub_domain" "$txtval" )
  298. export _H1="Content-Type: text/xml"
  299. response="$(_post "$xml_content" "$EUSERV_Api" "" "POST")"
  300. ok="$(printf '%s' "$response" | grep "<member><name>status</name><value><i4>100</i4></value></member>")"
  301. if [ -z "$ok" ]; then
  302. _err "Error could not create record"
  303. _debug "xml_content" "$xml_content"
  304. _debug "response" "$response"
  305. return 1
  306. fi
  307. # _dns_record_id="$(echo "$response" | _egrep_o "[\s\S]<name>dns_record_id<\/name>[\s]*?<value>[\s]*?<i4>(\K\d*)")"
  308. # _debug "_dns_record_id" "$_dns_record_id"
  309. return 0
  310. }