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.

177 lines
5.3 KiB

  1. #!/usr/bin/env sh
  2. # acme.sh JamoTech helper script
  3. # This is to be used on client systems and used by Ansible
  4. # to deploy SSL certificates on the jamo.tech domain to
  5. # customer servers for web panels and the likes to their
  6. # customer jamo.tech subdomain.
  7. #
  8. ######## Public functions #####################
  9. # API Calls to be made
  10. # _get("https://api.corp-jamo.tech/dns/v1/records/exists.php?access=accesskey&hostname=subdomain&target=10.8.0.1&type=A")
  11. # _get("https://api.corp-jamo.tech/dns/v1/records/exists.php?access=accesskey&hostname=_acme-challenge.subdomain&target=ACMEKEY&type=TXT")
  12. # _get("https://api.corp-jamo.tech/dns/v1/records/add.php?access=accesskey&hostname=subdomain&target=10.8.0.1&type=A")
  13. # _get("https://api.corp-jamo.tech/dns/v1/records/add.php?access=accesskey&hostname=_acme-challenge.subdomain&target=ACMEKEY&type=TXT")
  14. # _get("https://api.corp-jamo.tech/dns/v1/records/remove.php?access=accesskey&hostname=subdomain&target=10.8.0.1&type=A")
  15. # _get("https://api.corp-jamo.tech/dns/v1/records/remove.php?access=accesskey&hostname=_acme-challenge.subdomain&target=ACMEKEY&type=TXT")
  16. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  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. #Usage: fulldomain txtvalue
  41. #Remove the txt record after validation.
  42. dns_jamotech_rm() {
  43. fulldomain=$1
  44. txtvalue=$2
  45. JTECH_ENDIP="${JTECH_ENDIP:-$(_readaccountconf_mutable JTECH_ENDIP)}"
  46. JTECH_KEY="${JTECH_KEY:-$(_readaccountconf_mutable JTECH_KEY)}"
  47. if [ "$JTECH_ENDIP" ]; then
  48. _saveaccountconf_mutable JTECH_ENDIP "$JTECH_ENDIP"
  49. else
  50. _err "You need to specify an end IP by running 'export JTECH_ENDIP=IP'"
  51. return 1
  52. fi
  53. if [ "$JTECH_KEY" ]; then
  54. _saveaccountconf_mutable JTECH_KEY "$JTECH_KEY"
  55. else
  56. _err "You need to specify an API Key by running 'export JTECH_KEY=APIKEY'"
  57. return 1
  58. fi
  59. _info "Using jamotech-clean to remove the TXT record"
  60. _get_root
  61. _remove_record
  62. _debug fulldomain "$fulldomain"
  63. _debug txtvalue "$txtvalue"
  64. }
  65. #################### Private functions below ##################################
  66. # _acme-challenge.client.jamo.tech
  67. # returns
  68. # _txthost="_acme-challenge.client"
  69. # _subhost="client"
  70. _get_root() {
  71. domain=$fulldomain
  72. txtdomain=${domain%.jamo.tech}
  73. subdomain=$(echo "$txtdomain" | cut -d'.' -f2-)
  74. _debug "txtdomain = $txtdomain"
  75. _debug "subdomain = $subdomain"
  76. _debug "Domain: $domain TXTDomain: $txtdomain Subdomain: $subdomain"
  77. if [ -z "$domain" ] || [ -z "$txtdomain" ] || [ -z "$subdomain" ] ; then
  78. _err "We weren't able to determine the records which need to be created."
  79. return 1
  80. fi
  81. _txthost="$txtdomain"
  82. _subhost="$subdomain"
  83. _err "$domain not found"
  84. return 1
  85. }
  86. _check_record() {
  87. server_record="https://api.corp-jamo.tech/dns/v1/records/exists.php?access=$JTECH_KEY&hostname=$subdomain&target=$JTECH_ENDIP&type=A"
  88. txt_record="https://api.corp-jamo.tech/dns/v1/records/exists.php?access=$JTECH_KEY&hostname=$txtdomain&target=$txtvalue&type=TXT"
  89. _debug "API ENDPOINTS $server_record $txt_record"
  90. response="$(_get "$server_record")"
  91. if [ "$?" != "0" ]; then
  92. _err "error"
  93. return 1
  94. fi
  95. if _contains "$response" '"exists":"true"}'; then
  96. _err "Record already exists."
  97. return 1
  98. fi
  99. response="$(_get "$txt_record")"
  100. if [ "$?" != "0" ]; then
  101. _err "error"
  102. return 1
  103. fi
  104. if _contains "$response" '"exists":"true"}'; then
  105. _err "Record already exists."
  106. return 1
  107. fi
  108. }
  109. _create_record() {
  110. _check_record
  111. server_record="https://api.corp-jamo.tech/dns/v1/records/add.php?access=$JTECH_KEY&hostname=$subdomain&target=$JTECH_ENDIP&type=A"
  112. txt_record="https://api.corp-jamo.tech/dns/v1/records/add.php?access=$JTECH_KEY&hostname=$txtdomain&target=$txtvalue&type=TXT"
  113. _debug "API ENDPOINTS $server_record $txt_record"
  114. response="$(_get "$server_record")"
  115. if [ "$?" != "0" ]; then
  116. _err "error"
  117. return 1
  118. fi
  119. response="$(_get "$txt_record")"
  120. if [ "$?" != "0" ]; then
  121. _err "error"
  122. return 1
  123. fi
  124. return 0
  125. }
  126. _remove_record() {
  127. server_record="https://api.corp-jamo.tech/dns/v1/records/remove.php?access=$JTECH_KEY&hostname=$subdomain&target=$JTECH_ENDIP&type=A"
  128. txt_record="https://api.corp-jamo.tech/dns/v1/records/remove.php?access=$JTECH_KEY&hostname=$txtdomain&target=$txtvalue&type=TXT"
  129. _debug "API ENDPOINTS $server_record $txt_record"
  130. response="$(_get "$server_record")"
  131. if [ "$?" != "0" ]; then
  132. _err "error"
  133. return 1
  134. fi
  135. response="$(_get "$txt_record")"
  136. if [ "$?" != "0" ]; then
  137. _err "error"
  138. return 1
  139. fi
  140. return 0
  141. }