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.

188 lines
6.8 KiB

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