From: Frank Brehm Date: Mon, 14 Feb 2022 16:19:53 +0000 (+0100) Subject: Executing migration in migrate-sieve X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=4be8f5677793df9e69cd8f37f7c07fa523708051;p=pixelpark%2Fldap-migration.git Executing migration in migrate-sieve --- diff --git a/migrate-sieve b/migrate-sieve index dca354c..2b2a4b0 100755 --- a/migrate-sieve +++ b/migrate-sieve @@ -17,6 +17,7 @@ GREEN="" # BLUE="" CYAN="" NORMAL="" +HAS_COLORS="n" BASENAME=$(basename "${0}") BASE_DIR=$( dirname "$0" ) @@ -44,9 +45,11 @@ DEFAULT_ENV="${ENVIRONMENT}" LDAP_URL="${LDAP_URIS[${ENVIRONMENT}]}" SEARCH_BASE="o=isp" -VMAIL_ROOT_DIR='/var/lib/vmail' +VMAIL_ROOT_DIR='/var/lib/vmail/vmail1' VMAIL_USER='vmail' VMAIL_GROUP='vmail' +SIEVE_FILE_BASE="Normal.sieve" +DOVECOT_SIEVE_FILE_BASE=".dovecot.sieve" declare -a MAIL_DNS=() @@ -84,7 +87,7 @@ detect_color() { # BLUE="\\033[38;5;27m" CYAN="\\033[38;5;36m" NORMAL="\\033[39m" - # HAS_COLORS="y" + HAS_COLORS="y" else RED="" YELLOW="" @@ -265,7 +268,7 @@ get_options() { # BLUE="" CYAN="" NORMAL="" - # HAS_COLORS="n" + HAS_COLORS="n" shift ;; --nocolor) @@ -275,7 +278,7 @@ get_options() { # BLUE="" CYAN="" NORMAL="" - # HAS_COLORS="n" + HAS_COLORS="n" shift ;; -h|--help) @@ -353,12 +356,102 @@ get_options() { } +#------------------------------------------------ +CHOWN() { + + local cmd="chown $*" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd="chown --verbose $*" + fi + if [[ "${SIMULATE}" == "y" ]] ; then + info "Executing: ${cmd}" + return + fi + debug "Executing: ${cmd}" + if [[ "${VERBOSE}" != "y" ]] ; then + chown "$@" + else + chown --verbose "$@" + fi + +} + +#------------------------------------------------ +CHMOD() { + + local cmd="chmod $*" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd="chmod --verbose $*" + fi + if [[ "${SIMULATE}" == "y" ]] ; then + info "Executing: ${cmd}" + return + fi + debug "Executing: ${cmd}" + if [[ "${VERBOSE}" != "y" ]] ; then + chmod "$@" + else + chmod --verbose "$@" + fi + +} + +#------------------------------------------------------------------------------ +MKDIR() { + + local cmd="mkdir $*" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd="mkdir --verbose $*" + fi + if [[ "${SIMULATE}" == "y" ]] ; then + info "Executing: ${cmd}" + return + fi + debug "Executing: ${cmd}" + eval ${cmd} +} + +#------------------------------------------------------------------------------ +LN() { + + local cmd="ln $*" + if [[ "${VERBOSE}" == "y" ]] ; then + cmd="ln --verbose $*" + fi + if [[ "${SIMULATE}" == "y" ]] ; then + info "Executing: ${cmd}" + return + fi + debug "Executing: ${cmd}" + if [[ "${VERBOSE}" != "y" ]] ; then + ln "$@" + else + ln --verbose "$@" + fi + +} + +#------------------------------------------------------------------------------ +ensure_vmail_dir() { + + local vdir="$1" + if [[ ! -d "${vdir}" ]] ; then + MKDIR "${vdir}" + fi + CHMOD 0700 "${vdir}" + CHOWN "${VMAIL_USER}:${VMAIL_GROUP}" "${vdir}" + +} + #------------------------------------------------ LDAPSEARCH() { + local search_base="$1" + shift + local cmd="ldapsearch -x -H '${LDAP_URL}' " cmd+="-D '${BIND_DN}' -y '${BIND_PW_FILE}' -LLL -o ldif-wrap=no " - cmd+="-b '${SEARCH_BASE}' -s sub" + cmd+="-b '${search_base}' -s sub" while [[ "$#" -gt '0' ]] ; do cmd+=" '${1}'" shift @@ -381,12 +474,13 @@ get_all_sieve_dns() { local line= local dn= - for line in $( LDAPSEARCH '(mailSieveRuleSource=*)' dn ); do + for line in $( LDAPSEARCH "${SEARCH_BASE}" '(mailSieveRuleSource=*)' dn ); do if echo "${line}" | grep -iq '^dn:' ; then if [[ "${VERBOSE}" == "y" ]] ; then empty_line fi - dn=$( echo "${line}" | sed -e 's/^dn::? *//i' ) + # dn=$( echo "${line}" | sed -e 's/^dn::? *//i' ) + dn=$( echo "${line}" | sed -e 's/^dn: *//i' ) debug "Found DN '${CYAN}${dn}${NORMAL}'." MAIL_DNS+=( "${dn}" ) fi @@ -400,7 +494,118 @@ get_all_sieve_dns() { exit 7 fi - info "Found ${#MAIL_DNS[*]} DNs of entries with '${CYAN}mailSieveRuleSource${CYAN}' attribute." + info "Found ${#MAIL_DNS[*]} DNs of entries with '${CYAN}mailSieveRuleSource${NORMAL}' attribute." + +} + +#------------------------------------------------ +migrate_entry() { + + local dn="$1" + local result= + local cn= + local mail_address= + + empty_line + debug "Migrating Sieve script of '${CYAN}${dn}${NORMAL}' ..." + + result=$( LDAPSEARCH "${dn}" cn mail mailSieveRuleSource ) + + mail_address=$( echo "${result}" | grep -i '^mail:' ) + if [[ -z "${mail_address}" ]] ; then + warn "Did not found a mail address for DN '${YEOLLOW}${dn}${NORMAL}', stepping over." + return + fi + mail_address=$( echo "${mail_address}" | sed -e 's/^mail: *//i' ) + + cn=$( echo "${result}" | grep -i '^cn:' ) + if [[ -n "${cn}" ]] ; then + if echo "${cn}" | grep -iq '^cn: ' ; then + cn=$( echo "${cn}" | sed -e 's/^cn: *//i' ) + else + cn=$( echo "${cn}" | sed -e 's/^cn:: *//i' | base64 -d ) + fi + else + cn="Unknown" + fi + + info "Migrating Sieve script of '${CYAN}${cn} <${mail_address}>${NORMAL}' ..." + + local domain=$( echo "${mail_address}" | sed -e 's/.*@//' ) + local domain_dir="${VMAIL_ROOT_DIR}/${domain}" + local mailbox_dir="${domain_dir}/${mail_address}" + local dovecot_sieve_file="${mailbox_dir}/${DOVECOT_SIEVE_FILE_BASE}" + local sieve_dir="${mailbox_dir}/sieve" + local sieve_file="${sieve_dir}/${SIEVE_FILE_BASE}" + local dovecot_link_target="sieve/${SIEVE_FILE_BASE}" + local sieve_info= + local color_param='--color=always' + + if [[ "${HAS_COLORS}" == "n" ]] ; then + color_param='--color=never' + fi + + debug "Exporting Sieve to '${CYAN}${sieve_file}${NORMAL}' ..." + ensure_vmail_dir "${domain_dir}" + ensure_vmail_dir "${mailbox_dir}" + ensure_vmail_dir "${sieve_dir}" + + if [[ -f "${sieve_file}" ]] ; then + info "Sieve file '${CYAN}${sieve_file}${NORMAL}' is already existing." + else + + sieve_info=$( echo "${result}" | grep -i '^mailSieveRuleSource:' ) + if echo "${sieve_info}" | grep -iq '^mailSieveRuleSource: ' ; then + sieve_info=$( echo "${sieve_info}" | sed -e 's/^mailSieveRuleSource: *//i' ) + else + sieve_info=$( echo "${sieve_info}" | sed -e 's/^mailSieveRuleSource:: *//i' | base64 -d ) + fi + + if [[ "${SIMULATE}" == "y" ]] ; then + info "Sieve script:" + if [[ $( echo "${sieve_info}" | wc -l ) -gt 6 ]] ; then + echo "${sieve_info}" | head -n 5 + echo "..." + else + echo "${sieve_info}" + fi + else + echo "${sieve_info}" > "${sieve_file}" + fi + CHMOD 0600 "${sieve_file}" + CHOWN "${VMAIL_USER}:${VMAIL_GROUP}" "${sieve_file}" + fi + + if [[ -e "${sieve_file}" && "${QUIET}" != "y" ]] ; then + ls -l "${color_param}" "${sieve_file}" + fi + + debug "Ensuring symlink '${CYAN}${dovecot_sieve_file}${NORMAL}' -> '${CYAN}${dovecot_link_target}${NORMAL}' ..." + if [[ -L "${dovecot_sieve_file}" ]] ; then + info "Symlink '${CYAN}${dovecot_sieve_file}${NORMAL}' already exists." + else + if [[ -e "${dovecot_sieve_file}" ]] ; then + info "Path '${CYAN}${dovecot_sieve_file}${NORMAL}' already exists, but is not a symlink." + else + LN -s "${dovecot_link_target}" "${dovecot_sieve_file}" + CHOWN -h "${VMAIL_USER}:${VMAIL_GROUP}" "${dovecot_sieve_file}" + fi + fi + if [[ -e "${sieve_file}" && "${QUIET}" != "y" ]] ; then + ls -l "${color_param}" "${dovecot_sieve_file}" + fi + +} + +#------------------------------------------------ +migrate_all() { + + local dn= + + for dn in "${MAIL_DNS[@]}" ; do + sleep 0.3 + migrate_entry "${dn}" + done } @@ -411,6 +616,11 @@ main() { get_all_sieve_dns + migrate_all + + empty_line + info "Finished." + } main "$@"