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