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.

171 lines
6.5 KiB

5 years ago
  1. #!/usr/bin/env sh
  2. #
  3. # This deploy script deploys to a Sophos XG appliance
  4. # DEPLOY_SOPHOSXG_HOST="<NO DEFAULT - REQUIRED - host:port>"
  5. # DEPLOY_SOPHOSXG_USER="<NO DEFAULT - REQUIRED - string>"
  6. # DEPLOY_SOPHOSXG_PASSWORD="<NO DEFAULT - REQUIRED - string>"
  7. # DEPLOY_SOPHOSXG_NAME="domain"
  8. # DEPLOY_SOPHOSXG_PFX_PASSWORD="s0ph0sXG"
  9. # DEPLOY_SOPHOSXG_HTTPS_INSECURE="1"
  10. ######## Public functions #####################
  11. #action pfx user password name pfxpass host [insecure]
  12. sophosxg_do_req() {
  13. # check number of args
  14. [ $# -eq 8 ] || return 1
  15. # set vars
  16. _do_req_action="$1"
  17. _do_req_pfx="$2"
  18. _do_req_user="$3"
  19. _do_req_password="$4"
  20. _do_req_name="$5"
  21. _do_req_pfxpass="$6"
  22. _do_req_host="$7"
  23. _do_req_insecure="$8"
  24. # static values - as variables in case these need to change
  25. _do_req_boundary="SOPHOSXGPOST"
  26. _do_req_certfile="certificate.p12"
  27. # dont verify certs if config set
  28. if [ "${_do_req_insecure}" = "1" ]; then
  29. # shellcheck disable=SC2034
  30. HTTPS_INSECURE="1"
  31. fi
  32. # build POST body
  33. _do_req_post="$(printf '%s--%s\r\n' "" "${_do_req_boundary}")"
  34. _do_req_post="$(printf '%sContent-Type: application/xml; charset=utf-8\r\n' "${_do_req_post}")"
  35. _do_req_post="$(printf '%sContent-Disposition: form-data; name="reqxml"\r\n' "${_do_req_post}")"
  36. _do_req_post="$(printf '%s<Request>\r\n' "${_do_req_post}")"
  37. _do_req_post="$(printf '%s<Login>\r\n' "${_do_req_post}")"
  38. _do_req_post="$(printf '%s<Username>%s</Username><Password>%s</Password>\r\n' "${_do_req_post}" "${_do_req_user}" "${_do_req_password}")"
  39. _do_req_post="$(printf '%s</Login>\r\n' "${_do_req_post}")"
  40. _do_req_post="$(printf '%s<Set operation="%s">\r\n' "${_do_req_post}" "${_do_req_action}")"
  41. _do_req_post="$(printf '%s<Certificate>\r\n' "${_do_req_post}")"
  42. _do_req_post="$(printf '%s<Name>%s</Name>\r\n' "${_do_req_post}" "${_do_req_name}")"
  43. _do_req_post="$(printf '%s<Action>UploadCertificate</Action>\r\n' "${_do_req_post}")"
  44. _do_req_post="$(printf '%s<CertificateFormat>pkcs12</CertificateFormat>\r\n' "${_do_req_post}")"
  45. _do_req_post="$(printf '%s<Password>%s</Password>\r\n' "${_do_req_post}" "${_do_req_pfxpass}")"
  46. _do_req_post="$(printf '%s<CertificateFile>%s</CertificateFile>\r\n' "${_do_req_post}" "${_do_req_certfile}")"
  47. _do_req_post="$(printf '%s</Certificate>\r\n' "${_do_req_post}")"
  48. _do_req_post="$(printf '%s</Set>\r\n' "${_do_req_post}")"
  49. _do_req_post="$(printf '%s</Request>\r\n' "${_do_req_post}")"
  50. _do_req_post="$(printf '%s--%s\r\n' "${_do_req_post}" "${_do_req_boundary}")"
  51. _do_req_post="$(printf '%sContent-Type: application/octet-stream\r\n' "${_do_req_post}")"
  52. _do_req_post="$(printf '%sContent-Disposition: form-data; filename="%s"; name="file"\r\n' "${_do_req_post}" "${_do_req_certfile}")"
  53. _do_req_post="$(printf '%s%s\r\n' "${_do_req_post}" "$(_base64 <"${_do_req_pfx}")")"
  54. _do_req_post="$(printf '%s--%s--\r\n' "${_do_req_post}" "${_do_req_boundary}")"
  55. # do POST
  56. _post "${_do_req_post}" "https://${_do_req_host}/webconsole/APIController?" "" "POST" "multipart/form-data; boundary=${_do_req_boundary}"
  57. }
  58. #domain keyfile certfile cafile fullchain
  59. sophosxg_deploy() {
  60. _cdomain="$1"
  61. _ckey="$2"
  62. _ccert="$3"
  63. _cca="$4"
  64. _cfullchain="$5"
  65. # Some defaults
  66. DEFAULT_SOPHOSXG_PFX_PASSWORD="s0ph0sXG"
  67. DEFAULT_SOPHOSXG_NAME="$_cdomain"
  68. DEFAULT_SOPHOSXG_HTTPS_INSECURE="1"
  69. _debug _cdomain "$_cdomain"
  70. _debug _ckey "$_ckey"
  71. _debug _ccert "$_ccert"
  72. _debug _cca "$_cca"
  73. _debug _cfullchain "$_cfullchain"
  74. # HOST is required
  75. _getdeployconf DEPLOY_SOPHOSXG_HOST
  76. _devug2 DEPLOY_SOPHOSXG_HOST "${DEPLOY_SOPHOSXG_HOST}"
  77. if [ -z "${DEPLOY_SOPHOSXG_HOST}" ]; then
  78. _err "DEPLOY_SOPHOSXG_HOST not defined."
  79. return 1
  80. fi
  81. _savedeployconf DEPLOY_SOPHOSXG_HOST "${DEPLOY_SOPHOSXG_HOST}"
  82. # USER is required
  83. _getdeployconf DEPLOY_SOPHOSXG_USER
  84. _devug2 DEPLOY_SOPHOSXG_USER "${DEPLOY_SOPHOSXG_USER}"
  85. if [ -z "${DEPLOY_SOPHOSXG_USER}" ]; then
  86. _err "DEPLOY_SOPHOSXG_USER not defined."
  87. return 1
  88. fi
  89. _savedeployconf DEPLOY_SOPHOSXG_USER "${DEPLOY_SOPHOSXG_USER}"
  90. # PASSWORD is required
  91. _getdeployconf DEPLOY_SOPHOSXG_PASSWORD
  92. _devug2 DEPLOY_SOPHOSXG_PASSWORD "${DEPLOY_SOPHOSXG_PASSWORD}"
  93. if [ -z "${DEPLOY_SOPHOSXG_PASSWORD}" ]; then
  94. _err "DEPLOY_SOPHOSXG_PASSWORD not defined."
  95. return 1
  96. fi
  97. _savedeployconf DEPLOY_SOPHOSXG_PASSWORD "${DEPLOY_SOPHOSXG_PASSWORD}"
  98. # PFX_PASSWORD is optional. If not provided then use default
  99. _getdeployconf DEPLOY_SOPHOSXG_PFX_PASSWORD
  100. _devug2 DEPLOY_SOPHOSXG_PFX_PASSWORD "${DEPLOY_SOPHOSXG_PFX_PASSWORD}"
  101. if [ -z "${DEPLOY_SOPHOSXG_PFX_PASSWORD}" ]; then
  102. DEPLOY_SOPHOSXG_PFX_PASSWORD="${DEFAULT_SOPHOSXG_PFX_PASSWORD}"
  103. fi
  104. _savedeployconf DEPLOY_SOPHOSXG_PFX_PASSWORD "${DEPLOY_SOPHOSXG_PFX_PASSWORD}"
  105. # NAME is optional. If not provided then use $_cdomain
  106. _getdeployconf DEPLOY_SOPHOSXG_NAME
  107. _devug2 DEPLOY_SOPHOSXG_NAME "${DEPLOY_SOPHOSXG_NAME}"
  108. if [ -z "${DEPLOY_SOPHOSXG_NAME}" ]; then
  109. DEPLOY_SOPHOSXG_NAME="${DEFAULT_SOPHOSXG_NAME}"
  110. fi
  111. _savedeployconf DEPLOY_SOPHOSXG_NAME "${DEPLOY_SOPHOSXG_NAME}"
  112. # HTTPS_INSECURE is optional. Defaults to 1 (true)
  113. _getdeployconf DEPLOY_SOPHOSXG_HTTPS_INSECURE
  114. _devug2 DEPLOY_SOPHOSXG_HTTPS_INSECURE "${DEPLOY_SOPHOSXG_HTTPS_INSECURE}"
  115. if [ -z "${DEPLOY_SOPHOSXG_HTTPS_INSECURE}" ]; then
  116. DEPLOY_SOPHOSXG_HTTPS_INSECURE="${DEFAULT_SOPHOSXG_HTTPS_INSECURE}"
  117. fi
  118. _savedeployconf DEPLOY_SOPHOSXG_HTTPS_INSECURE "${DEPLOY_SOPHOSXG_HTTPS_INSECURE}"
  119. # create temp pkcs12 file
  120. _info "Generating pkcs12 file"
  121. _import_pkcs12="$(_mktemp)"
  122. if [ ! -f "$_import_pkcs12" ]; then
  123. _err "Error creating temp file for pkcs12"
  124. return 1
  125. fi
  126. if ! _toPkcs "$_import_pkcs12" "$_ckey" "$_ccert" "$_cca" "$DEPLOY_SOPHOSXG_PFX_PASSWORD"; then
  127. _err "Error exporting to pkcs12"
  128. [ -f "$_import_pkcs12" ] && rm -f "$_import_pkcs12"
  129. return 1
  130. fi
  131. # do upload of cert via HTTP POST - attempt to "update" and on failure try "add"
  132. _req_action_success="no"
  133. for _req_action in update add; do
  134. _info "Uploading certificate: $_req_action"
  135. if sophosxg_do_req "$_req_action" "$_import_pkcs12" "$DEPLOY_SOPHOSXG_USER" "$DEPLOY_SOPHOSXG_PASSWORD" "$DEPLOY_SOPHOSXG_NAME" "$DEPLOY_SOPHOSXG_PFX_PASSWORD" "$DEPLOY_SOPHOSXG_HOST" "$DEPLOY_SOPHOSXG_HTTPS_INSECURE"; then
  136. _req_action_success="yes"
  137. break
  138. fi
  139. _info "$_req_action failed"
  140. done
  141. # clean up pfx
  142. [ -f "$_import_pkcs12" ] && rm -f "$_import_pkcs12"
  143. # check final result
  144. if [ "$_req_action_success" = "no" ]; then
  145. _err "Upload failed permanently"
  146. return 1
  147. fi
  148. return 0
  149. }