prepare-dovecot.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. project_dir="${MAILHUB_PROJECT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)}"
  4. data_dir="${MAILHUB_DATA_DIR:-${project_dir}/data}"
  5. fail() {
  6. echo "Dovecot preparation failed: $*" >&2
  7. exit 1
  8. }
  9. command -v openssl >/dev/null 2>&1 || fail "openssl is required."
  10. [[ ! -L "${data_dir}" ]] || fail "data directory must not be a symbolic link."
  11. mkdir -p "${data_dir}"
  12. data_dir="$(cd "${data_dir}" && pwd -P)"
  13. secret_file="${MAILHUB_DOVECOT_SECRET_FILE:-${data_dir}/secrets/dovecot_auth_secret}"
  14. webmail_secret_file="${MAILHUB_WEBMAIL_SSO_SECRET_FILE:-${data_dir}/secrets/webmail_sso_secret}"
  15. maildir_root="${MAILHUB_MAILDIR_ROOT:-${data_dir}/maildir}"
  16. host_uid="$(id -u)"
  17. if [[ "$(uname -s)" == "Linux" && "${host_uid}" != "0" && "${host_uid}" != "1000" ]]; then
  18. fail "Linux preparation must run as root or host uid 1000 so the rootless containers can read and write their bind mounts."
  19. fi
  20. case "${secret_file}" in
  21. "${data_dir}"/*) ;;
  22. *) fail "secret file must stay inside the MailHub data directory." ;;
  23. esac
  24. case "${webmail_secret_file}" in
  25. "${data_dir}"/*) ;;
  26. *) fail "Webmail SSO secret file must stay inside the MailHub data directory." ;;
  27. esac
  28. case "${maildir_root}" in
  29. "${data_dir}"/*) ;;
  30. *) fail "Maildir root must stay inside the MailHub data directory." ;;
  31. esac
  32. [[ ! -L "${secret_file}" ]] || fail "secret file must not be a symbolic link."
  33. [[ ! -L "${webmail_secret_file}" ]] || fail "Webmail SSO secret file must not be a symbolic link."
  34. [[ ! -L "${maildir_root}" ]] || fail "Maildir root must not be a symbolic link."
  35. mkdir -p "$(dirname "${secret_file}")" "$(dirname "${webmail_secret_file}")" "${maildir_root}"
  36. secret_dir="$(dirname "${secret_file}")"
  37. webmail_secret_dir="$(dirname "${webmail_secret_file}")"
  38. prepare_secret() {
  39. local file="$1"
  40. local prefix="$2"
  41. local temporary_secret
  42. if [[ ! -f "${file}" ]]; then
  43. umask 077
  44. temporary_secret="$(mktemp "$(dirname "${file}")/.${prefix}.XXXXXX")"
  45. if ! openssl rand -hex 32 >"${temporary_secret}"; then
  46. rm -f -- "${temporary_secret}"
  47. fail "unable to generate ${prefix} secret."
  48. fi
  49. chmod 0600 "${temporary_secret}"
  50. mv "${temporary_secret}" "${file}"
  51. fi
  52. [[ -f "${file}" ]] || fail "${prefix} secret path must be a regular file."
  53. local value
  54. value="$(tr -d '\r\n' <"${file}")"
  55. [[ "${value}" =~ ^[0-9a-fA-F]+$ ]] || fail "${prefix} secret must contain only hexadecimal characters."
  56. (( ${#value} >= 64 && ${#value} <= 512 )) || fail "${prefix} secret must contain 64-512 hexadecimal characters."
  57. }
  58. prepare_secret "${secret_file}" "dovecot-auth"
  59. prepare_secret "${webmail_secret_file}" "webmail-sso"
  60. if [[ "${host_uid}" == "0" ]]; then
  61. # MailHub runs as uid/gid 1000. The Webmail secret is group-readable so a
  62. # Roundcube container can join supplemental gid 1000 without making it public.
  63. chown 1000:1000 \
  64. "${secret_dir}" \
  65. "${webmail_secret_dir}" \
  66. "${secret_file}" \
  67. "${webmail_secret_file}" \
  68. "${maildir_root}"
  69. fi
  70. chmod 0700 "${maildir_root}"
  71. chmod 0750 "${secret_dir}" "${webmail_secret_dir}"
  72. chmod 0400 "${secret_file}"
  73. chmod 0440 "${webmail_secret_file}"
  74. echo "Dovecot storage and authentication secrets are ready."