Files
postfixadmin/ADDITIONS/postfixadmin-mailbox-postpassword.sh
T
Rodrigo Cortés Cano 510715cef0 Harden optional mailbox hook scripts (#1080)
- backport only the generic hardening of the existing optional Maildir and Dovecot hook examples
- validate the documented hook argument contracts before constructing filesystem paths
- quote filesystem operations, replace legacy shell constructs and propagate command failures
- make domain and mailbox deletion safely retryable when their target is already absent
- update the Dovecot password example for Dovecot 2.3.19 or newer, with a Dovecot 2.4 setting note

The hooks remain optional and disabled by default. Administrators must review `basedir`, `trashbase` and the service account used by sudo. The mail-crypt password example now requires Dovecot 2.3.19 or newer.
2026-07-19 09:52:50 +01:00

47 lines
1.8 KiB
Bash

#!/bin/bash
# Example script for Dovecot's mail-crypt plugin.
# Requires Dovecot 2.3.19 or newer for the -O/-N prompt behaviour.
# https://doc.dovecot.org/2.3/configuration_manual/mail_crypt_plugin/
# Dovecot 2.3 setting name. For Dovecot 2.4, use:
# private_password_setting=crypt_user_key_password
private_password_setting=plugin/mail_crypt_private_password
fail()
{
printf '%s\n' "$0: $*" >&2
exit 1
}
[[ $# -eq 2 ]] || fail "expected username and domain arguments"
username=$1
domain=$2
[[ -n $username && -n $domain && $username == *@"$domain" ]] || fail "invalid username or domain"
IFS= read -r -d '' old_password || fail "could not read the NUL-terminated old password"
IFS= read -r -d '' new_password || fail "could not read the NUL-terminated new password"
[[ -n $new_password ]] || fail "new password must not be empty"
openssl_path=$(command -v openssl 2>/dev/null) || fail "could not find openssl in PATH"
doveadm_path=$(command -v doveadm 2>/dev/null) || fail "could not find doveadm in PATH"
if [[ -z $old_password ]]; then
old_password=$("$openssl_path" rand -hex 16) || fail "could not generate a temporary password"
[[ -n $old_password ]] || fail "openssl returned an empty temporary password"
if ! "$doveadm_path" -o "$private_password_setting=$old_password" \
mailbox cryptokey generate -u "$username" -U; then
fail "could not generate the mailbox cryptokey"
fi
fi
# Since Dovecot 2.3.19, -O asks for the old password and -N asks for the new
# password twice. Keep passwords on stdin instead of command-line arguments.
if ! printf '%s\n%s\n%s\n' "$old_password" "$new_password" "$new_password" | \
"$doveadm_path" mailbox cryptokey password -u "$username" -O -N; then
fail "could not update the mailbox cryptokey password"
fi
exit 0