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.

163 lines
4.9 KiB

  1. #!/usr/bin/env sh
  2. # JamoTech Customer Domain amce Helper
  3. # This script is intended to be run via
  4. # acme.sh on managed customer systems
  5. # to allow customers to create and renew
  6. # SSL certificates on their client
  7. # subdomain e.g (client.jamo.tech)
  8. # without the need for support staff
  9. # to create TXT records.
  10. # API Calls to be made
  11. # _get("https://api.corp-jamo.tech/dns/v1/records/exists.php?access=accesskey&hostname=subdomain&target=10.8.0.1&type=A")
  12. # _get("https://api.corp-jamo.tech/dns/v1/records/exists.php?access=accesskey&hostname=_acme-challenge.subdomain&target=ACMEKEY&type=TXT")
  13. # _get("https://api.corp-jamo.tech/dns/v1/records/add.php?access=accesskey&hostname=subdomain&target=10.8.0.1&type=A")
  14. # _get("https://api.corp-jamo.tech/dns/v1/records/add.php?access=accesskey&hostname=_acme-challenge.subdomain&target=ACMEKEY&type=TXT")
  15. # _get("https://api.corp-jamo.tech/dns/v1/records/remove.php?access=accesskey&hostname=subdomain&target=10.8.0.1&type=A")
  16. # _get("https://api.corp-jamo.tech/dns/v1/records/remove.php?access=accesskey&hostname=_acme-challenge.subdomain&target=ACMEKEY&type=TXT")
  17. dns_jamotech_add() {
  18. fulldomain=$1
  19. txtvalue=$2
  20. JTECH_ENDIP="${JTECH_ENDIP:-$(_readaccountconf_mutable JTECH_ENDIP)}"
  21. JTECH_KEY="${JTECH_KEY:-$(_readaccountconf_mutable JTECH_KEY)}"
  22. if [ "$JTECH_ENDIP" ]; then
  23. _saveaccountconf_mutable JTECH_ENDIP "$JTECH_ENDIP"
  24. else
  25. _err "You need to specify an end IP by running 'export JTECH_ENDIP=IP'"
  26. return 1
  27. fi
  28. if [ "$JTECH_KEY" ]; then
  29. _saveaccountconf_mutable JTECH_KEY "$JTECH_KEY"
  30. else
  31. _err "You need to specify an API Key by running 'export JTECH_KEY=APIKEY'"
  32. return 1
  33. fi
  34. _info "Using jamotech-register to add the TXT record"
  35. _get_root
  36. _create_record
  37. _debug fulldomain "$fulldomain"
  38. _debug txtvalue "$txtvalue"
  39. }
  40. dns_jamotech_rm() {
  41. fulldomain=$1
  42. txtvalue=$2
  43. JTECH_ENDIP="${JTECH_ENDIP:-$(_readaccountconf_mutable JTECH_ENDIP)}"
  44. JTECH_KEY="${JTECH_KEY:-$(_readaccountconf_mutable JTECH_KEY)}"
  45. if [ "$JTECH_ENDIP" ]; then
  46. _saveaccountconf_mutable JTECH_ENDIP "$JTECH_ENDIP"
  47. else
  48. _err "You need to specify an end IP by running 'export JTECH_ENDIP=IP'"
  49. return 1
  50. fi
  51. if [ "$JTECH_KEY" ]; then
  52. _saveaccountconf_mutable JTECH_KEY "$JTECH_KEY"
  53. else
  54. _err "You need to specify an API Key by running 'export JTECH_KEY=APIKEY'"
  55. return 1
  56. fi
  57. _info "Using jamotech-clean to remove the TXT record"
  58. _get_root
  59. _remove_record
  60. _debug fulldomain "$fulldomain"
  61. _debug txtvalue "$txtvalue"
  62. }
  63. #################### Private functions below ##################################
  64. _get_root() {
  65. domain=$fulldomain
  66. txtdomain=${domain%.jamo.tech}
  67. subdomain=$(echo "$txtdomain" | cut -d'.' -f2-)
  68. _debug "txtdomain = $txtdomain"
  69. _debug "subdomain = $subdomain"
  70. _debug "Domain: $domain TXTDomain: $txtdomain Subdomain: $subdomain"
  71. if [ -z "$domain" ] || [ -z "$txtdomain" ] || [ -z "$subdomain" ]; then
  72. _err "We weren't able to determine the records which need to be created."
  73. return 1
  74. fi
  75. _txthost="$txtdomain"
  76. _subhost="$subdomain"
  77. _err "$domain not found"
  78. return 1
  79. }
  80. _check_record() {
  81. server_record="https://api.corp-jamo.tech/dns/v1/records/exists.php?access=$JTECH_KEY&hostname=$_subhost&target=$JTECH_ENDIP&type=A"
  82. txt_record="https://api.corp-jamo.tech/dns/v1/records/exists.php?access=$JTECH_KEY&hostname=$_txthost&target=$txtvalue&type=TXT"
  83. _debug "API ENDPOINTS $server_record $txt_record"
  84. response="$(_get "$server_record")"
  85. if [ "$?" != "0" ]; then
  86. _err "error"
  87. return 1
  88. fi
  89. if _contains "$response" '"exists":"true"}'; then
  90. _err "Record already exists."
  91. return 1
  92. fi
  93. response="$(_get "$txt_record")"
  94. if [ "$?" != "0" ]; then
  95. _err "error"
  96. return 1
  97. fi
  98. if _contains "$response" '"exists":"true"}'; then
  99. _err "Record already exists."
  100. return 1
  101. fi
  102. }
  103. _create_record() {
  104. _check_record
  105. server_record="https://api.corp-jamo.tech/dns/v1/records/add.php?access=$JTECH_KEY&hostname=$_subhost&target=$JTECH_ENDIP&type=A"
  106. txt_record="https://api.corp-jamo.tech/dns/v1/records/add.php?access=$JTECH_KEY&hostname=$_txthost&target=$txtvalue&type=TXT"
  107. _debug "API ENDPOINTS $server_record $txt_record"
  108. response="$(_get "$server_record")"
  109. if [ "$?" != "0" ]; then
  110. _err "error"
  111. return 1
  112. fi
  113. response="$(_get "$txt_record")"
  114. if [ "$?" != "0" ]; then
  115. _err "error"
  116. return 1
  117. fi
  118. return 0
  119. }
  120. _remove_record() {
  121. server_record="https://api.corp-jamo.tech/dns/v1/records/remove.php?access=$JTECH_KEY&hostname=$_subhost&target=$JTECH_ENDIP&type=A"
  122. txt_record="https://api.corp-jamo.tech/dns/v1/records/remove.php?access=$JTECH_KEY&hostname=$_txthost&target=$txtvalue&type=TXT"
  123. _debug "API ENDPOINTS $server_record $txt_record"
  124. response="$(_get "$server_record")"
  125. if [ "$?" != "0" ]; then
  126. _err "error"
  127. return 1
  128. fi
  129. response="$(_get "$txt_record")"
  130. if [ "$?" != "0" ]; then
  131. _err "error"
  132. return 1
  133. fi
  134. return 0
  135. }