]> Frank Brehm's Git Trees - pixelpark/ldap-migration.git/commitdiff
Adding migrate-sieve
authorFrank Brehm <frank@brehm-online.com>
Mon, 14 Feb 2022 14:42:58 +0000 (15:42 +0100)
committerFrank Brehm <frank@brehm-online.com>
Mon, 14 Feb 2022 14:42:58 +0000 (15:42 +0100)
etc/additional_entries/510.mail-service-user.ldif
migrate-sieve [new file with mode: 0755]

index fcbf17942509acff6b8f5b2efc0f09b8132d0411..92e09abc421426c1b8ac0e1289b4752e38b8abdd 100644 (file)
@@ -3,6 +3,8 @@ description: Generic user for mail services with read access to everywhere.
 inetUserStatus: active
 mail: mail-service@pixelpark.com
 mailForwardingAddress: postmaster@pixelpark.com
+nsLookThroughLimit: -1
+nsSizeLimit: -1
 objectClass: top
 objectClass: inetUser
 objectClass: account
diff --git a/migrate-sieve b/migrate-sieve
new file mode 100755 (executable)
index 0000000..dca354c
--- /dev/null
@@ -0,0 +1,418 @@
+#!/bin/bash
+
+set -e
+set -u
+
+VERBOSE="n"
+DEBUG="n"
+QUIET='n'
+SIMULATE='n'
+
+VERSION="4.1"
+
+# console colors:
+RED=""
+YELLOW=""
+GREEN=""
+# BLUE=""
+CYAN=""
+NORMAL=""
+
+BASENAME=$(basename "${0}")
+BASE_DIR=$( dirname "$0" )
+cd "${BASE_DIR}" || exit 99
+BASE_DIR=$( readlink -f . )
+FQDN=$( hostname -f )
+
+declare -A LDAP_URIS=()
+LDAP_URIS['prd']="ldaps://prd-ds.pixelpark.com"
+LDAP_URIS['test']="ldaps://test-ds.pixelpark.com"
+LDAP_URIS['dev']="ldaps://dev-ldap2.pixelpark.com"
+
+LDAP_URL="${LDAP_URIS['prd']}"
+
+BIND_DN="uid=mail-service,ou=Services,o=Pixelpark,o=isp"
+BIND_PW_FILE="/root/.private/pwd-mail-user-ldap-wonl.txt"
+
+ENVIRONMENT="prd"
+if [[ "${FQDN}" == 'dev-imap01.pixelpark.com' ]] ; then
+    ENVIRONMENT="dev"
+elif [[ "${FQDN}" == 'test-imap01.pixelpark.com' ]] ; then
+    ENVIRONMENT="test"
+fi
+DEFAULT_ENV="${ENVIRONMENT}"
+LDAP_URL="${LDAP_URIS[${ENVIRONMENT}]}"
+
+SEARCH_BASE="o=isp"
+VMAIL_ROOT_DIR='/var/lib/vmail'
+VMAIL_USER='vmail'
+VMAIL_GROUP='vmail'
+
+declare -a MAIL_DNS=()
+
+#-------------------------------------------------------------------
+detect_color() {
+
+    local safe_term="${TERM//[^[:alnum:]]/?}"
+    local match_lhs=""
+    local use_color="false"
+    local term=
+
+    if [[ -f ~/.dir_colors   ]] ; then
+        match_lhs="${match_lhs}$( grep '^TERM ' ~/.dir_colors | sed -e 's/^TERM  *//' -e 's/ .*//')"
+    fi
+    if [[ -f /etc/DIR_COLORS   ]] ; then
+        match_lhs="${match_lhs}$( grep '^TERM ' /etc/DIR_COLORS | sed -e 's/^TERM  *//' -e 's/ .*//')"
+    fi
+    if [[ -z ${match_lhs} ]] ; then
+        type -P dircolors >/dev/null && \
+        match_lhs=$(dircolors --print-database | grep '^TERM ' | sed -e 's/^TERM  *//' -e 's/ .*//')
+    fi
+    for term in ${match_lhs} ; do
+        # shellcheck disable=SC2053
+        if [[ "${safe_term}" == ${term} || "${TERM}" == ${term} ]] ; then
+            use_color="true"
+            break
+        fi
+    done
+
+    # console colors:
+    if [ "${use_color}" = "true" ] ; then
+        RED="\\033[38;5;196m"
+        YELLOW="\\033[38;5;226m"
+        GREEN="\\033[38;5;46m"
+        # BLUE="\\033[38;5;27m"
+        CYAN="\\033[38;5;36m"
+        NORMAL="\\033[39m"
+        # HAS_COLORS="y"
+    else
+        RED=""
+        YELLOW=""
+        GREEN=""
+        # BLUE=""
+        CYAN=""
+        NORMAL=""
+    fi
+
+    local my_tty
+
+    my_tty=$(tty)
+    if [[ "${my_tty}" =~ 'not a tty' ]] ; then
+        my_tty='-'
+    fi
+
+}
+detect_color
+
+#------------------------------------------------------------------------------
+my_date() {
+    date +'%F %T.%N %:::z'
+}
+
+#------------------------------------------------------------------------------
+debug() {
+    if [[ "${VERBOSE}" != "y" ]] ; then
+        return 0
+    fi
+    echo -e " * [$(my_date)] [${BASENAME}:${CYAN}DEBUG${NORMAL}]: $*" >&2
+}
+
+#------------------------------------------------------------------------------
+info() {
+    if [[ "${QUIET}" == "y" ]] ; then
+        return 0
+    fi
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $*" >&2
+    else
+        echo -e " ${GREEN}*${NORMAL} $*" >&2
+    fi
+}
+
+#------------------------------------------------------------------------------
+warn() {
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $*" >&2
+    else
+        echo -e " ${YELLOW}*${NORMAL} [${BASENAME}:${YELLOW}WARN${NORMAL}] : $*" >&2
+    fi
+}
+
+#------------------------------------------------------------------------------
+error() {
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $*" >&2
+    else
+        echo -e " ${RED}*${NORMAL} [${BASENAME}:${RED}ERROR${NORMAL}]: $*" >&2
+    fi
+}
+
+#------------------------------------------------------------------------------
+description() {
+    cat <<-EOF
+
+       Retrieves all Sieve definitions from LDAP and writes them in file 'sieve/Normal.sieve'
+       inside the parent directory of the Maildir of the appropriate Mailbox.
+
+       EOF
+
+}
+
+#------------------------------------------------------------------------------
+check_for_root() {
+    local my_id=$( id -u )
+    if [[ "${my_id}" != "0" ]] ; then
+        error "You must be ${RED}root${NORMAL} to execute this script."
+        echo >&2
+        exit 1
+    fi
+}
+
+#------------------------------------------------------------------------------
+draw_line() {
+    if [[ "${QUIET}" == "y" ]] ; then
+        return 0
+    fi
+    echo "---------------------------------------------------"
+}
+
+#------------------------------------------------------------------------------
+empty_line() {
+    if [[ "${QUIET}" == "y" ]] ; then
+        return 0
+    fi
+    echo
+}
+
+#------------------------------------------------------------------------------
+usage() {
+
+    cat <<-EOF
+       Usage: ${BASENAME} [-E|--env ENVIRONMENT] [-D|--dir DIRECTORY] [-s|--simulate] [-d|--debug] [[-v|--verbose] | [-q|--quiet]] [--nocolor]
+              ${BASENAME} [-h|--help]
+              ${BASENAME} [-V|--version]
+
+           Options:
+               -E|--env|--environment ENVIRONMENT
+                               The LDAP environment to use as the target of this script.
+                               Must be one of '${CYAN}prd${NORMAL}', '${CYAN}test${NORMAL}' or '${CYAN}dev${NORMAL}'. Default: '${CYAN}${DEFAULT_ENV}${NORMAL}'."
+               -D|--dir|--directory DIRECTORY
+                               The root directory of all mail boxes. Default: ${CYAN}${VMAIL_ROOT_DIR}${NORMAL}'.
+               -s|--simulate   Simulation mode, nothing is really done.
+               -d|--debug      Debug output (bash -x).
+               -v|--verbose    Set verbosity on.
+               -q|--quiet      Quiet execution. Mutually exclusive to --verbose.
+               --nocolor       Don't use colors on display.
+               -h|--help       Show this output and exit.
+               -V|--version    Prints out version number of the script and exit.
+
+       EOF
+
+}
+
+#------------------------------------------------------------------------------
+get_options() {
+
+    local tmp=
+    local short_options="E:D:sdvqhV"
+    local long_options="env:,environment:,dir:,directory:,simulate,debug,verbose,quiet,help,version"
+    local py_version=
+    local py_found="n"
+    local ret=
+
+    set +e
+    tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASENAME}" -- "$@" )
+    ret="$?"
+    if [[ "${ret}" != 0 ]] ; then
+        echo "" >&2
+        echo -e "$( usage )" >&2
+        exit 1
+    fi
+    set -e
+
+    # Note the quotes around `$TEMP': they are essential!
+    eval set -- "${tmp}"
+
+    while true ; do
+        case "$1" in
+            -E|--env|--environment)
+                ENVIRONMENT="$2"
+                shift
+                shift
+                ;;
+            -D|--dir|--directory)
+                VMAIL_ROOT_DIR="$2"
+                shift
+                shift
+                ;;
+            -s|--simulate)
+                SIMULATE="y"
+                shift
+                ;;
+            -d|--debug)
+                DEBUG="y"
+                shift
+                ;;
+            -v|--verbose)
+                VERBOSE="y"
+                shift
+                ;;
+            -q|--quiet)
+                QUIET="y"
+                RED=""
+                YELLOW=""
+                GREEN=""
+                # BLUE=""
+                CYAN=""
+                NORMAL=""
+                # HAS_COLORS="n"
+                shift
+                ;;
+            --nocolor)
+                RED=""
+                YELLOW=""
+                GREEN=""
+                # BLUE=""
+                CYAN=""
+                NORMAL=""
+                # HAS_COLORS="n"
+                shift
+                ;;
+            -h|--help)
+                echo -e "$( description )"
+                echo
+                echo -e "$( usage )"
+                exit 0
+                ;;
+            -V|--version)
+                echo "${BASENAME} version: ${VERSION}"
+                exit 0
+                ;;
+            --) shift
+                break
+                ;;
+            *)  echo "Internal error!"
+                exit 1
+                ;;
+        esac
+    done
+
+    if [[ "${DEBUG}" = "y" ]] ; then
+        set -x
+    fi
+
+    if [[ "${VERBOSE}" == "y" && "${QUIET}" == "y" ]] ; then
+        error "Options '${RED}--verbose${NORMAL}' and '${RED}--quiet${NORMAL}' are mutually exclusive."
+        echo >&2
+        echo -e "$( usage )" >&2
+        exit 1
+    fi
+
+    if [[ "${ENVIRONMENT}" != 'prd' && "${ENVIRONMENT}" != 'test' && "${ENVIRONMENT}" != 'dev' ]] ; then
+        error "Wrong environment '${RED}${ENVIRONMENT}${NORMAL}' given."
+        echo >&2
+        echo -e "$( usage )" >&2
+        exit 1
+    fi
+    LDAP_URL="${LDAP_URIS[${ENVIRONMENT}]}"
+
+    local msg="Environment '${CYAN}${ENVIRONMENT}${NORMAL}':\n"
+    msg+="   ${CYAN}*${NORMAL} LDAP URL:         '${CYAN}${LDAP_URL}${NORMAL}'\n"
+    msg+="   ${CYAN}*${NORMAL} Bind DN:          '${CYAN}${BIND_DN}${NORMAL}'\n"
+    msg+="   ${CYAN}*${NORMAL} Password file:    '${CYAN}${BIND_PW_FILE}${NORMAL}'\n"
+    msg+="   ${CYAN}*${NORMAL} LDAP Search Base: '${CYAN}${SEARCH_BASE}${NORMAL}'\n"
+    msg+="   ${CYAN}*${NORMAL} LDAP Root Dir:    '${CYAN}${VMAIL_ROOT_DIR}${NORMAL}'\n"
+
+    info "${msg}"
+
+    if [[ ! -f "${BIND_PW_FILE}" ]] ; then
+        error "Password file '${RED}${BIND_PW_FILE}${NORMAL}' not found" >&2
+        exit 3
+    fi
+
+    if [[ ! -r "${BIND_PW_FILE}" ]] ; then
+        error "Password file '${RED}${BIND_PW_FILE}${NORMAL}' not readable" >&2
+        exit 3
+    fi
+
+    if [[ ! -d "${VMAIL_ROOT_DIR}" ]] ; then
+        error "VMail root directory '${RED}${VMAIL_ROOT_DIR}${NORMAL}' does not exists or is not a directory."
+        exit 3
+    fi
+
+    check_for_root
+
+    if [[ "${SIMULATE}" == "y" ]] ; then
+        echo
+        echo -e "${CYAN}---------------------------------------------${NORMAL}"
+        echo -e "           ${YELLOW}Simulation mode${NORMAL}"
+        echo -e "      ${CYAN}Nothing will be done in real.${NORMAL}"
+        echo -e "${CYAN}---------------------------------------------${NORMAL}"
+        sleep 2
+    fi
+
+}
+
+#------------------------------------------------
+LDAPSEARCH() {
+
+    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"
+    while [[ "$#" -gt '0' ]] ; do
+        cmd+=" '${1}'"
+        shift
+    done
+
+    debug "Executing: ${cmd}"
+    eval ${cmd}
+
+}
+
+#------------------------------------------------
+get_all_sieve_dns() {
+
+    empty_line
+    info "Collecting all DNs having the '${CYAN}mailSieveRuleSource${NORMAL}' attribute."
+
+    local oifs="${IFS}"
+    IFS="
+"
+
+    local line=
+    local dn=
+    for line in $( LDAPSEARCH '(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' )
+            debug "Found DN '${CYAN}${dn}${NORMAL}'."
+            MAIL_DNS+=( "${dn}" )
+        fi
+    done
+
+    IFS="${oifs}"
+    empty_line
+
+    if [[ "${#MAIL_DNS[*]}" == "0" ]] ; then
+        warn "Did not found LDAP entries with a '${CYAN}mailSieveRuleSource${NORMAL}' attribute."
+        exit 7
+    fi
+
+    info "Found ${#MAIL_DNS[*]} DNs of entries with '${CYAN}mailSieveRuleSource${CYAN}' attribute."
+
+}
+
+#------------------------------------------------
+main() {
+
+    get_options "$@"
+
+    get_all_sieve_dns
+
+}
+
+main "$@"
+
+# vim: et list