chore: Revise open-dkim config generator

This commit is contained in:
Brennan Kinney 2025-02-15 13:43:56 +13:00 committed by GitHub
parent f2fedff251
commit 8fa186ae76
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 46 additions and 30 deletions

View File

@ -57,20 +57,21 @@ ${ORANGE}EXAMPLES${RESET}
${ORANGE}EXIT STATUS${RESET} ${ORANGE}EXIT STATUS${RESET}
Exit status is 0 if command was successful. If wrong arguments are provided or arguments contain Exit status is 0 if command was successful. If wrong arguments are provided or arguments contain
errors, the script will exit early with exit status 2. errors, the script will exit early with a non-zero exit status.
" "
} }
_require_n_parameters_or_print_usage 0 "${@}" _require_n_parameters_or_print_usage 0 "${@}"
# Parse the command args through iteration:
while [[ ${#} -gt 0 ]]; do while [[ ${#} -gt 0 ]]; do
case "${1}" in case "${1}" in
( 'keysize' ) ( 'keysize' )
if [[ -n ${2+set} ]]; then if [[ -n ${2+set} ]]; then
KEYSIZE="${2}" KEYSIZE="${2}"
shift _log 'debug' "Keysize set to '${KEYSIZE}'"
shift
else else
_exit_with_error "No keysize provided after 'keysize' argument" _exit_with_error "No keysize provided after 'keysize' argument"
fi fi
@ -78,10 +79,8 @@ while [[ ${#} -gt 0 ]]; do
( 'selector' ) ( 'selector' )
if [[ -n ${2+set} ]]; then if [[ -n ${2+set} ]]; then
# shellcheck disable=SC2034
SELECTOR="${2}" SELECTOR="${2}"
shift _log 'debug' "Selector set to '${SELECTOR}'"
shift
else else
_exit_with_error "No selector provided after 'selector' argument" _exit_with_error "No selector provided after 'selector' argument"
fi fi
@ -90,23 +89,31 @@ while [[ ${#} -gt 0 ]]; do
( 'domain' ) ( 'domain' )
if [[ -n ${2+set} ]]; then if [[ -n ${2+set} ]]; then
DOMAINS="${2}" DOMAINS="${2}"
shift _log 'debug' "Domain(s) set to '${DOMAIN}'"
shift
else else
_exit_with_error "No domain(s) provided after 'domain' argument" _exit_with_error "No domain(s) provided after 'domain' argument"
fi fi
;; ;;
( 'help' )
__usage
exit 0
;;
( * ) ( * )
__usage __usage
_exit_with_error "Unknown options '${1}' ${2:+and \'${2}\'}" _exit_with_error "Unknown option(s) '${1}' ${2:+"and '${2}'"}"
;; ;;
esac esac
# Discard these two args (option + value) now that they've been processed:
shift 2
done done
DATABASE_VHOST='/tmp/vhost.dkim'
# Prepare a file with one domain per line: # Prepare a file with one domain per line:
# Depends on methods from `scripts/helpers/postfix.sh`:
DATABASE_VHOST='/tmp/vhost.dkim'
function _generate_domains_config() { function _generate_domains_config() {
local TMP_VHOST='/tmp/vhost.dkim.tmp' local TMP_VHOST='/tmp/vhost.dkim.tmp'
@ -130,49 +137,58 @@ if [[ ! -s ${DATABASE_VHOST} ]]; then
exit 0 exit 0
fi fi
# Generate the keypairs and associated OpenDKIM config files:
OPENDKIM_BASE_DIR='/tmp/docker-mailserver/opendkim'
while read -r DKIM_DOMAIN; do while read -r DKIM_DOMAIN; do
mkdir -p "/tmp/docker-mailserver/opendkim/keys/${DKIM_DOMAIN}" OPENDKIM_DOMAINKEY_DIR="${OPENDKIM_BASE_DIR}/keys/${DKIM_DOMAIN}"
mkdir -p "${OPENDKIM_DOMAINKEY_DIR}"
if [[ ! -f "/tmp/docker-mailserver/opendkim/keys/${DKIM_DOMAIN}/${SELECTOR}.private" ]]; then DKIM_KEY_FILE="${OPENDKIM_DOMAINKEY_DIR}/${SELECTOR}.private"
_log 'info' "Creating DKIM private key '/tmp/docker-mailserver/opendkim/keys/${DKIM_DOMAIN}/${SELECTOR}.private'" if [[ ! -f "${DKIM_KEY}" ]]; then
_log 'info' "Creating DKIM private key '${DKIM_KEY_FILE}'"
opendkim-genkey \ opendkim-genkey \
--bits="${KEYSIZE}" \ --bits="${KEYSIZE}" \
--subdomains \ --subdomains \
--domain="${DKIM_DOMAIN}" \ --domain="${DKIM_DOMAIN}" \
--selector="${SELECTOR}" \ --selector="${SELECTOR}" \
--directory="/tmp/docker-mailserver/opendkim/keys/${DKIM_DOMAIN}" --directory="${OPENDKIM_DOMAINKEY_DIR}"
fi fi
# fix permissions to use the same user:group as /tmp/docker-mailserver/opendkim/keys # Ensure permissions match the user:group of the base directory:
chown -R "$(stat -c '%U:%G' /tmp/docker-mailserver/opendkim/keys)" "/tmp/docker-mailserver/opendkim/keys/${DKIM_DOMAIN}" chown -R "$(stat -c '%U:%G' "${OPENDKIM_BASE_DIR}")" "${OPENDKIM_DOMAINKEY_DIR}"
# write to KeyTable if necessary # write to KeyTable if necessary
KEYTABLEENTRY="${SELECTOR}._domainkey.${DKIM_DOMAIN} ${DKIM_DOMAIN}:${SELECTOR}:/etc/opendkim/keys/${DKIM_DOMAIN}/${SELECTOR}.private" KEY_TABLE_FILE="${OPENDKIM_BASE_DIR}/KeyTable"
if [[ ! -f "/tmp/docker-mailserver/opendkim/KeyTable" ]]; then KEY_TABLE_ENTRY="${SELECTOR}._domainkey.${DKIM_DOMAIN} ${DKIM_DOMAIN}:${SELECTOR}:/etc/opendkim/keys/${DKIM_DOMAIN}/${SELECTOR}.private"
if [[ ! -f "${KEY_TABLE_FILE}" ]]; then
_log 'debug' 'Creating DKIM KeyTable' _log 'debug' 'Creating DKIM KeyTable'
echo "${KEYTABLEENTRY}" >/tmp/docker-mailserver/opendkim/KeyTable echo "${KEY_TABLE_ENTRY}" > "${KEY_TABLE_FILE}"
else else
if ! grep -q "${KEYTABLEENTRY}" "/tmp/docker-mailserver/opendkim/KeyTable"; then # If no existing entry, add one:
echo "${KEYTABLEENTRY}" >>/tmp/docker-mailserver/opendkim/KeyTable if ! grep -q "${KEY_TABLE_ENTRY}" "${KEY_TABLE_FILE}"; then
echo "${KEY_TABLE_ENTRY}" >> "${KEY_TABLE_FILE}"
fi fi
fi fi
# write to SigningTable if necessary # write to SigningTable if necessary
SIGNINGTABLEENTRY="*@${DKIM_DOMAIN} ${SELECTOR}._domainkey.${DKIM_DOMAIN}" SIGNING_TABLE_FILE="${OPENDKIM_BASE_DIR}/SigningTable"
if [[ ! -f /tmp/docker-mailserver/opendkim/SigningTable ]]; then SIGNING_TABLE_ENTRY="*@${DKIM_DOMAIN} ${SELECTOR}._domainkey.${DKIM_DOMAIN}"
if [[ ! -f "${SIGNING_TABLE_FILE}" ]]; then
_log 'debug' 'Creating DKIM SigningTable' _log 'debug' 'Creating DKIM SigningTable'
echo "*@${DKIM_DOMAIN} ${SELECTOR}._domainkey.${DKIM_DOMAIN}" >/tmp/docker-mailserver/opendkim/SigningTable echo "*@${DKIM_DOMAIN} ${SELECTOR}._domainkey.${DKIM_DOMAIN}" > "${SIGNING_TABLE_FILE}"
else else
if ! grep -q "${SIGNINGTABLEENTRY}" /tmp/docker-mailserver/opendkim/SigningTable; then # If no existing entry, add one:
echo "${SIGNINGTABLEENTRY}" >>/tmp/docker-mailserver/opendkim/SigningTable if ! grep -q "${SIGNING_TABLE_ENTRY}" "${SIGNING_TABLE_FILE}"; then
echo "${SIGNING_TABLE_ENTRY}" >> "${SIGNING_TABLE_FILE}"
fi fi
fi fi
done < <(_get_valid_lines_from_file "${DATABASE_VHOST}") done < <(_get_valid_lines_from_file "${DATABASE_VHOST}")
# create TrustedHosts if missing # create TrustedHosts if missing
if [[ -d /tmp/docker-mailserver/opendkim ]] && [[ ! -f /tmp/docker-mailserver/opendkim/TrustedHosts ]]; then TRUSTED_HOSTS_FILE="${OPENDKIM_BASE_DIR}/TrustedHosts"
if [[ -d "${OPENDKIM_BASE_DIR}" ]] && [[ ! -f "${TRUSTED_HOSTS_FILE}" ]]; then
_log 'debug' 'Creating DKIM TrustedHosts' _log 'debug' 'Creating DKIM TrustedHosts'
echo "127.0.0.1" >/tmp/docker-mailserver/opendkim/TrustedHosts echo "127.0.0.1" > "${TRUSTED_HOSTS_FILE}"
echo "localhost" >>/tmp/docker-mailserver/opendkim/TrustedHosts echo "localhost" >> "${TRUSTED_HOSTS_FILE}"
fi fi