--- /dev/null
+#!/bin/bash
+
+set -e
+set -u
+
+LDAP_SERVER='ldap://ldap.pixelpark.com'
+LDAP_BIND_DN='cn=admin'
+LDAP_BASE_DN='o=isp'
+LDAP_PWD_FILE="${HOME}/.private/ldap-admin-wonl.txt"
+
+USER="${1:-frank.brehm}"
+
+if [[ ! -f "${LDAP_PWD_FILE}" ]] ; then
+ echo "Password file '${LDAP_PWD_FILE}' not found" >&2
+ exit 3
+fi
+
+if [[ ! -r "${LDAP_PWD_FILE}" ]] ; then
+ echo "Password file '${LDAP_PWD_FILE}' not readable" >&2
+ exit 3
+fi
+
+main() {
+
+ echo "Suche DN für User '${USER}' ..." >&2
+
+ local base_cmd="ldapsearch -x -LLL -o ldif-wrap=no -H \"${LDAP_SERVER}\""
+ base_cmd+=" -D \"${LDAP_BIND_DN}\" -y \"${LDAP_PWD_FILE}\""
+
+ local cmd="${base_cmd} -b \"${LDAP_BASE_DN}\" \"uid=${USER}\" dn | sed -e 's/^dn:[ ][ ]*//i'"
+ echo "${cmd}" >&2
+ local user_dn=$( eval $cmd )
+ echo "Fand DN '${user_dn}'." >&2
+ echo >&2
+
+ cmd="${base_cmd} -b \"${user_dn}\" \"objectclass=*\" mailSieveRuleSource |"
+ cmd+=" grep -v '^dn:' | sed -e 's/^mailSieveRuleSource::*[ ]*//i' -e 's/[ ]//g' -e 's/=//g' |"
+ cmd+="tr -d '[:cntrl:]'"
+ echo "${cmd}" >&2
+ echo >&2
+ local sieve64=$( eval $cmd )
+ # echo "${sieve64}" >&2
+ printf "${sieve64}==" | base64 --decode --ignore-garbage
+
+}
+
+main "$@"
+
+# vim: ts=4 list
+++ /dev/null
-#!/bin/bash
-
-set -e
-set -u
-
-LDAP_SERVER='ldap://ldap.pixelpark.com'
-LDAP_BIND_DN='cn=admin'
-LDAP_BASE_DN='o=isp'
-LDAP_PWD_FILE="${HOME}/.private/ldap-admin-wonl.txt"
-
-USER="${1:-frank.brehm}"
-
-if [[ ! -f "${LDAP_PWD_FILE}" ]] ; then
- echo "Password file '${LDAP_PWD_FILE}' not found" >&2
- exit 3
-fi
-
-if [[ ! -r "${LDAP_PWD_FILE}" ]] ; then
- echo "Password file '${LDAP_PWD_FILE}' not readable" >&2
- exit 3
-fi
-
-main() {
-
- echo "Suche DN für User '${USER}' ..." >&2
-
- local base_cmd="ldapsearch -x -LLL -o ldif-wrap=no -H \"${LDAP_SERVER}\""
- base_cmd+=" -D \"${LDAP_BIND_DN}\" -y \"${LDAP_PWD_FILE}\""
-
- local cmd="${base_cmd} -b \"${LDAP_BASE_DN}\" \"uid=${USER}\" dn | sed -e 's/^dn:[ ][ ]*//i'"
- echo "${cmd}" >&2
- local user_dn=$( eval $cmd )
- echo "Fand DN '${user_dn}'." >&2
- echo >&2
-
- cmd="${base_cmd} -b \"${user_dn}\" \"objectclass=*\" mailSieveRuleSource |"
- cmd+=" grep -v '^dn:' | sed -e 's/^mailSieveRuleSource::*[ ]*//i' -e 's/[ ]//g' -e 's/=//g' |"
- cmd+="tr -d '[:cntrl:]'"
- echo "${cmd}" >&2
- echo >&2
- local sieve64=$( eval $cmd )
- # echo "${sieve64}" >&2
- printf "${sieve64}==" | base64 --decode --ignore-garbage
-
-}
-
-main "$@"
-
-# vim: ts=4 list
--- /dev/null
+#!/bin/bash
+
+set -e
+set -u
+
+VERBOSE="n"
+DEBUG="n"
+SIMULATE="n"
+
+VERSION="2.1"
+
+# console colors:
+RED=""
+YELLOW=""
+GREEN=""
+BLUE=""
+NORMAL=""
+
+HAS_TTY='y'
+
+BASENAME="$(basename ${0})"
+BASE_DIR="$(dirname ${0})"
+
+LDAP_SERVER='ldap://ldap.pixelpark.com'
+LDAP_BIND_DN='cn=admin'
+LDAP_BASE_DN='o=isp'
+LDAP_PWD_FILE="${HOME}/.private/ldap-admin-wonl.txt"
+
+USER="frank.brehm"
+SIEVE_FILE=""
+LDIF_FILE=
+
+#-------------------------------------------------------------------
+detect_color() {
+
+ local safe_term="${TERM//[^[:alnum:]]/?}"
+ local match_lhs=""
+ local use_color="false"
+ [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
+ [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
+ [[ -z ${match_lhs} ]] \
+ && type -P dircolors >/dev/null \
+ && match_lhs=$(dircolors --print-database)
+ [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color="true"
+
+ # 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"
+ NORMAL="\033[39m"
+ else
+ RED=""
+ YELLOW=""
+ GREEN=""
+ BLUE=""
+ NORMAL=""
+ fi
+
+ local my_tty=$(tty)
+ if [[ "${my_tty}" =~ 'not a tty' ]] ; then
+ my_tty='-'
+ fi
+
+ if [[ "${my_tty}" = '-' || "${safe_term}" = "dump" ]] ; then
+ HAS_TTY='n'
+ fi
+
+}
+detect_color
+
+#------------------------------------------------------------------------------
+description() {
+ echo -e $( cat <<-EOF
+ Aktualisiert die Sieve-Regeln im LDAP.
+
+ EOF
+ )
+}
+
+#------------------------------------------------------------------------------
+usage() {
+ cat <<-EOF
+ Usage: ${BASENAME} [-s|--simulate] [-d|--debug] [-v|--verbose] [-U|--user USER] SIEVE_FILE
+ ${BASENAME} [-h|--help]
+
+ EOF
+}
+
+#------------------------------------------------------------------------------
+get_options() {
+
+ local tmp=
+ local base_dir=
+
+ set +e
+ tmp=$( getopt -o sdvU: \
+ --long simulate,debug,verbose,nocolor,user:,help \
+ -n "${BASENAME}" -- "$@" )
+ if [[ $? != 0 ]] ; then
+ echo "" >&2
+ usage >&2
+ exit 1
+ fi
+ set -e
+
+ # Note the quotes around `$TEMP': they are essential!
+ eval set -- "${tmp}"
+
+ local p=
+
+ while true ; do
+ case "$1" in
+ -U|--user)
+ USER="$2"
+ shift
+ shift
+ ;;
+ -s|--simulate)
+ SIMULATE="y"
+ shift
+ ;;
+ -d|--debug)
+ DEBUG="y"
+ shift
+ ;;
+ -v|--verbose)
+ VERBOSE="y"
+ shift
+ ;;
+ --nocolor)
+ RED=""
+ YELLOW=""
+ GREEN=""
+ BLUE=""
+ NORMAL=""
+ shift
+ ;;
+ -h|--help)
+ description
+ 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 [[ "$#" -ne 1 ]] ; then
+ usage >&2
+ exit 1
+ fi
+
+ SIEVE_FILE="${1}"
+
+}
+
+#########################################
+# Some often used funktions
+
+#------------------------------------------------------------------------------
+my_date() {
+ date +'%F %T.%N %:::z'
+}
+
+#------------------------------------------------------------------------------
+debug() {
+ if [[ "${VERBOSE}" != "y" ]] ; then
+ return 0
+ fi
+ echo -e " * [$(my_date)] [${BASENAME}:DEBUG]: $@" >&2
+}
+
+#------------------------------------------------------------------------------
+info() {
+ echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2
+}
+
+#------------------------------------------------------------------------------
+warn() {
+ echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2
+}
+
+#------------------------------------------------------------------------------
+error() {
+ echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2
+}
+
+#------------------------------------------------------------------------------
+RM() {
+ if [[ "${VERBOSE}" == "y" ]] ; then
+ rm --verbose "$@"
+ else
+ rm "$@"
+ fi
+
+}
+
+#------------------------------------------------------------------------------
+cleanup_tmp_file() {
+
+ if [[ -n "${LDIF_FILE}" ]] ; then
+ if [[ -f "${LDIF_FILE}" ]] ; then
+ RM -f "${LDIF_FILE}"
+ fi
+ fi
+
+}
+
+################################################################################
+##
+## Main
+##
+################################################################################
+main() {
+
+ get_options "$@"
+
+ if [[ "${SIMULATE}" == "y" ]] ; then
+ info "Simulation mode, nothing is really done."
+ fi
+
+ if [[ ! -f "${LDAP_PWD_FILE}" ]] ; then
+ error "Password file '${LDAP_PWD_FILE}' not found" >&2
+ exit 3
+ fi
+
+ if [[ ! -r "${LDAP_PWD_FILE}" ]] ; then
+ error "Password file '${LDAP_PWD_FILE}' not readable" >&2
+ exit 3
+ fi
+
+ if [[ ! -f "${SIEVE_FILE}" ]] ; then
+ error "Sieve file '${SIEVE_FILE}' existiert nicht." >&2
+ echo >&2
+ usage >&2
+ exit 1
+ fi
+ SIEVE_FILE=$( readlink -f "${SIEVE_FILE}" )
+
+ local base_cmd="ldapsearch -x -LLL -o ldif-wrap=no -H \"${LDAP_SERVER}\""
+ base_cmd+=" -D \"${LDAP_BIND_DN}\" -y \"${LDAP_PWD_FILE}\""
+
+ info "Suche DN für User '${USER}' ..."
+
+ local cmd="${base_cmd} -b \"${LDAP_BASE_DN}\" \"uid=${USER}\" dn | sed -e 's/^dn:[ ][ ]*//i'"
+ info "Ausführen:"
+ echo "${cmd}"
+ local user_dn=$( eval $cmd )
+ if [[ -z "${user_dn}" ]] ; then
+ error "User '${USER}' nicht im LDAP gefunden." >&2
+ echo >&2
+ exit 1
+ fi
+
+ info "Fand DN '${GREEN}${user_dn}${NORMAL}'."
+
+ LDIF_FILE=$( mktemp modify-sieve-XXXXXXXX.ldif )
+
+ trap cleanup_tmp_file INT TERM EXIT ABRT
+
+ cat > "${LDIF_FILE}" <<-EOF
+ dn: ${user_dn}
+ changetype: modify
+ EOF
+
+ info "Suche existierenden Sieve Eintrag ..."
+ echo
+
+ cmd="${base_cmd} -b \"${user_dn}\" \"objectclass=*\" mailSieveRuleSource |"
+ cmd+=" grep -i '^mailSieveRuleSource:' || true"
+ info "Ausführen:"
+ echo "${cmd}"
+ local cur_sieve64=$( eval $cmd )
+
+ if [[ -z "${cur_sieve64}" ]] ; then
+ info "Füge Attribut mailSieveRuleSource hinzu ..."
+ cat >> "${LDIF_FILE}" <<-EOF
+ add: mailSieveRuleSource
+ EOF
+ else
+ info "Ändere Attribut mailSieveRuleSource ..."
+ cat >> "${LDIF_FILE}" <<-EOF
+ replace: mailSieveRuleSource
+ EOF
+ fi
+
+ echo "mailSieveRuleSource:< file://${SIEVE_FILE}" >> "${LDIF_FILE}"
+ echo "-" >> "${LDIF_FILE}"
+ echo '' >> "${LDIF_FILE}"
+
+ echo
+ echo "Resultierendes LDIF:"
+ echo "--------------------"
+ echo
+ cat "${LDIF_FILE}"
+
+ cmd="ldapmodify -H \"${LDAP_SERVER}\" -x -D \"${LDAP_BIND_DN}\" -y \"${LDAP_PWD_FILE}\""
+ cmd+=" -f \"$( readlink -f "${LDIF_FILE}" )\""
+
+ #CMD="ldapmodify -H \"${LDAP_SERVER}\""
+ #CMD+=" -x -D \"${LDAP_BIND_DN}\" -w \"${LDAP_BIND_PW}\""
+ #CMD+=" -f \"$( readlink -f "${LDIF_FILE}" )\""
+
+ echo
+ info "Ausführen:"
+ echo "${cmd}"
+ echo
+
+ if [[ "${SIMULATE}" != "y" ]] ; then
+ eval ${cmd}
+ echo
+ fi
+
+ info "Fertig."
+
+}
+
+main "$@"
+
+exit 0
+
+
+# vim: ts=4 list
+++ /dev/null
-#!/bin/bash
-
-set -e
-set -u
-
-VERBOSE="n"
-DEBUG="n"
-SIMULATE="n"
-
-VERSION="2.1"
-
-# console colors:
-RED=""
-YELLOW=""
-GREEN=""
-BLUE=""
-NORMAL=""
-
-HAS_TTY='y'
-
-BASENAME="$(basename ${0})"
-BASE_DIR="$(dirname ${0})"
-
-LDAP_SERVER='ldap://ldap.pixelpark.com'
-LDAP_BIND_DN='cn=admin'
-LDAP_BASE_DN='o=isp'
-LDAP_PWD_FILE="${HOME}/.private/ldap-admin-wonl.txt"
-
-USER="frank.brehm"
-SIEVE_FILE=""
-LDIF_FILE=
-
-#-------------------------------------------------------------------
-detect_color() {
-
- local safe_term="${TERM//[^[:alnum:]]/?}"
- local match_lhs=""
- local use_color="false"
- [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
- [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
- [[ -z ${match_lhs} ]] \
- && type -P dircolors >/dev/null \
- && match_lhs=$(dircolors --print-database)
- [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color="true"
-
- # 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"
- NORMAL="\033[39m"
- else
- RED=""
- YELLOW=""
- GREEN=""
- BLUE=""
- NORMAL=""
- fi
-
- local my_tty=$(tty)
- if [[ "${my_tty}" =~ 'not a tty' ]] ; then
- my_tty='-'
- fi
-
- if [[ "${my_tty}" = '-' || "${safe_term}" = "dump" ]] ; then
- HAS_TTY='n'
- fi
-
-}
-detect_color
-
-#------------------------------------------------------------------------------
-description() {
- echo -e $( cat <<-EOF
- Aktualisiert die Sieve-Regeln im LDAP.
-
- EOF
- )
-}
-
-#------------------------------------------------------------------------------
-usage() {
- cat <<-EOF
- Usage: ${BASENAME} [-s|--simulate] [-d|--debug] [-v|--verbose] [-U|--user USER] SIEVE_FILE
- ${BASENAME} [-h|--help]
-
- EOF
-}
-
-#------------------------------------------------------------------------------
-get_options() {
-
- local tmp=
- local base_dir=
-
- set +e
- tmp=$( getopt -o sdvU: \
- --long simulate,debug,verbose,nocolor,user:,help \
- -n "${BASENAME}" -- "$@" )
- if [[ $? != 0 ]] ; then
- echo "" >&2
- usage >&2
- exit 1
- fi
- set -e
-
- # Note the quotes around `$TEMP': they are essential!
- eval set -- "${tmp}"
-
- local p=
-
- while true ; do
- case "$1" in
- -U|--user)
- USER="$2"
- shift
- shift
- ;;
- -s|--simulate)
- SIMULATE="y"
- shift
- ;;
- -d|--debug)
- DEBUG="y"
- shift
- ;;
- -v|--verbose)
- VERBOSE="y"
- shift
- ;;
- --nocolor)
- RED=""
- YELLOW=""
- GREEN=""
- BLUE=""
- NORMAL=""
- shift
- ;;
- -h|--help)
- description
- 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 [[ "$#" -ne 1 ]] ; then
- usage >&2
- exit 1
- fi
-
- SIEVE_FILE="${1}"
-
-}
-
-#########################################
-# Some often used funktions
-
-#------------------------------------------------------------------------------
-my_date() {
- date +'%F %T.%N %:::z'
-}
-
-#------------------------------------------------------------------------------
-debug() {
- if [[ "${VERBOSE}" != "y" ]] ; then
- return 0
- fi
- echo -e " * [$(my_date)] [${BASENAME}:DEBUG]: $@" >&2
-}
-
-#------------------------------------------------------------------------------
-info() {
- echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASENAME}:${GREEN}INFO${NORMAL}] : $@" >&2
-}
-
-#------------------------------------------------------------------------------
-warn() {
- echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASENAME}:${YELLOW}WARN${NORMAL}] : $@" >&2
-}
-
-#------------------------------------------------------------------------------
-error() {
- echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASENAME}:${RED}ERROR${NORMAL}]: $@" >&2
-}
-
-#------------------------------------------------------------------------------
-RM() {
- if [[ "${VERBOSE}" == "y" ]] ; then
- rm --verbose "$@"
- else
- rm "$@"
- fi
-
-}
-
-#------------------------------------------------------------------------------
-cleanup_tmp_file() {
-
- if [[ -n "${LDIF_FILE}" ]] ; then
- if [[ -f "${LDIF_FILE}" ]] ; then
- RM -f "${LDIF_FILE}"
- fi
- fi
-
-}
-
-################################################################################
-##
-## Main
-##
-################################################################################
-main() {
-
- get_options "$@"
-
- if [[ "${SIMULATE}" == "y" ]] ; then
- info "Simulation mode, nothing is really done."
- fi
-
- if [[ ! -f "${LDAP_PWD_FILE}" ]] ; then
- error "Password file '${LDAP_PWD_FILE}' not found" >&2
- exit 3
- fi
-
- if [[ ! -r "${LDAP_PWD_FILE}" ]] ; then
- error "Password file '${LDAP_PWD_FILE}' not readable" >&2
- exit 3
- fi
-
- if [[ ! -f "${SIEVE_FILE}" ]] ; then
- error "Sieve file '${SIEVE_FILE}' existiert nicht." >&2
- echo >&2
- usage >&2
- exit 1
- fi
- SIEVE_FILE=$( readlink -f "${SIEVE_FILE}" )
-
- local base_cmd="ldapsearch -x -LLL -o ldif-wrap=no -H \"${LDAP_SERVER}\""
- base_cmd+=" -D \"${LDAP_BIND_DN}\" -y \"${LDAP_PWD_FILE}\""
-
- info "Suche DN für User '${USER}' ..."
-
- local cmd="${base_cmd} -b \"${LDAP_BASE_DN}\" \"uid=${USER}\" dn | sed -e 's/^dn:[ ][ ]*//i'"
- info "Ausführen:"
- echo "${cmd}"
- local user_dn=$( eval $cmd )
- if [[ -z "${user_dn}" ]] ; then
- error "User '${USER}' nicht im LDAP gefunden." >&2
- echo >&2
- exit 1
- fi
-
- info "Fand DN '${GREEN}${user_dn}${NORMAL}'."
-
- LDIF_FILE=$( mktemp modify-sieve-XXXXXXXX.ldif )
-
- trap cleanup_tmp_file INT TERM EXIT ABRT
-
- cat > "${LDIF_FILE}" <<-EOF
- dn: ${user_dn}
- changetype: modify
- EOF
-
- info "Suche existierenden Sieve Eintrag ..."
- echo
-
- cmd="${base_cmd} -b \"${user_dn}\" \"objectclass=*\" mailSieveRuleSource |"
- cmd+=" grep -i '^mailSieveRuleSource:' || true"
- info "Ausführen:"
- echo "${cmd}"
- local cur_sieve64=$( eval $cmd )
-
- if [[ -z "${cur_sieve64}" ]] ; then
- info "Füge Attribut mailSieveRuleSource hinzu ..."
- cat >> "${LDIF_FILE}" <<-EOF
- add: mailSieveRuleSource
- EOF
- else
- info "Ändere Attribut mailSieveRuleSource ..."
- cat >> "${LDIF_FILE}" <<-EOF
- replace: mailSieveRuleSource
- EOF
- fi
-
- echo "mailSieveRuleSource:< file://${SIEVE_FILE}" >> "${LDIF_FILE}"
- echo "-" >> "${LDIF_FILE}"
- echo '' >> "${LDIF_FILE}"
-
- echo
- echo "Resultierendes LDIF:"
- echo "--------------------"
- echo
- cat "${LDIF_FILE}"
-
- cmd="ldapmodify -H \"${LDAP_SERVER}\" -x -D \"${LDAP_BIND_DN}\" -y \"${LDAP_PWD_FILE}\""
- cmd+=" -f \"$( readlink -f "${LDIF_FILE}" )\""
-
- #CMD="ldapmodify -H \"${LDAP_SERVER}\""
- #CMD+=" -x -D \"${LDAP_BIND_DN}\" -w \"${LDAP_BIND_PW}\""
- #CMD+=" -f \"$( readlink -f "${LDIF_FILE}" )\""
-
- echo
- info "Ausführen:"
- echo "${cmd}"
- echo
-
- if [[ "${SIMULATE}" != "y" ]] ; then
- eval ${cmd}
- echo
- fi
-
- info "Fertig."
-
-}
-
-main "$@"
-
-exit 0
-
-
-# vim: ts=4 list