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_BIND_PW='3o.o0dlt'
LDAP_BASE_DN='o=isp'
+USER="frank.brehm"
+SIEVE_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: USER_NAME SIEVE_FILE
+ Usage: ${BASENAME} [-s|--simulate] [-d|--debug] [-v|--verbose] [-U|--user USER] SIEVE_FILE
+ ${BASENAME} [-h|--help]
EOF
}
-if [[ "$#" -ne 2 ]] ; then
- usage >&2
- exit 1
-fi
+#------------------------------------------------------------------------------
+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
-USER="${1}"
-SIEVE_FILE="${2}"
+ if [[ "${DEBUG}" = "y" ]] ; then
+ set -x
+ fi
-if [[ ! -f "${SIEVE_FILE}" ]] ; then
- echo "Sieve file '${SIEVE_FILE}' existiert nicht." >&2
- echo >&2
- usage >&2
- exit 1
-fi
-SIEVE_FILE=$( readlink -f "${SIEVE_FILE}" )
+ if [[ "$#" -ne 1 ]] ; then
+ usage >&2
+ exit 1
+ fi
-echo "Suche DN für User '${USER}' ..."
+ SIEVE_FILE="${1}"
-USER_DN=$( ldapsearch -LLL \
- -H "${LDAP_SERVER}" \
- -b "${LDAP_BASE_DN}" \
- -x -D "${LDAP_BIND_DN}" -w "${LDAP_BIND_PW}" \
- "uid=${USER}" \
- dn | sed -e 's/^dn:[ ][ ]*//i' )
+}
-if [[ -z "${USER_DN}" ]] ; then
- echo "User '${USER}' nicht im LDAP gefunden." >&2
- echo >&2
- exit 1
-fi
+#########################################
+# Some often used funktions
-LDIF_FILE=$( mktemp modify-sieve-XXXXXXXX.ldif )
+#------------------------------------------------------------------------------
+my_date() {
+ date +'%F %T.%N %:::z'
+}
#------------------------------------------------------------------------------
-cleanup_tmp_script() {
+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 -fv "${LDIF_FILE}"
+ RM -f "${LDIF_FILE}"
fi
fi
}
-trap cleanup_tmp_script INT TERM EXIT
+################################################################################
+##
+## Main
+##
+################################################################################
+main() {
-cat > "${LDIF_FILE}" <<-EOF
- dn: ${USER_DN}
- changetype: modify
- EOF
+ get_options "$@"
-echo
-echo "Fand DN '${USER_DN}'."
-echo
-echo "Suche existierenden Sieve Eintrag ..."
-echo
-
-CUR_SIEVE64=$( ldapsearch -LLL \
- -H "${LDAP_SERVER}" \
- -b "${USER_DN}" \
- -x -D "${LDAP_BIND_DN}" -w "${LDAP_BIND_PW}" \
- "objectclass=*" mailSieveRuleSource | \
- grep -i '^mailSieveRuleSource:' || true )
-
-if [[ -z "${CUR_SIEVE64}" ]] ; then
- echo "Füge Attribut mailSieveRuleSource hinzu ..."
- cat >> "${LDIF_FILE}" <<-EOF
- add: mailSieveRuleSource
- EOF
-else
- echo "Ändere Attribut mailSieveRuleSource ..."
- cat >> "${LDIF_FILE}" <<-EOF
- replace: mailSieveRuleSource
+ if [[ "${SIMULATE}" == "y" ]] ; then
+ info "Simulation mode, nothing is really done."
+ 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}" )
+
+ info "Suche DN für User '${USER}' ..."
+
+ USER_DN=$( ldapsearch -LLL \
+ -H "${LDAP_SERVER}" \
+ -b "${LDAP_BASE_DN}" \
+ -x -D "${LDAP_BIND_DN}" -w "${LDAP_BIND_PW}" \
+ "uid=${USER}" \
+ dn | sed -e 's/^dn:[ ][ ]*//i' )
+
+ if [[ -z "${USER_DN}" ]] ; then
+ error "User '${USER}' nicht im LDAP gefunden." >&2
+ echo >&2
+ exit 1
+ fi
+
+ 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
-fi
-echo "mailSieveRuleSource:< file://${SIEVE_FILE}" >> "${LDIF_FILE}"
-echo "-" >> "${LDIF_FILE}"
-echo '' >> "${LDIF_FILE}"
+ echo
+ info "Fand DN '${USER_DN}'."
+ echo
+ info "Suche existierenden Sieve Eintrag ..."
+ echo
+
+ CUR_SIEVE64=$( ldapsearch -LLL \
+ -H "${LDAP_SERVER}" \
+ -b "${USER_DN}" \
+ -x -D "${LDAP_BIND_DN}" -w "${LDAP_BIND_PW}" \
+ "objectclass=*" mailSieveRuleSource | \
+ grep -i '^mailSieveRuleSource:' || true )
-echo
-echo "Resultierendes LDIF:"
-echo "--------------------"
-echo
-cat "${LDIF_FILE}"
+ 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}\""
+ CMD+=" -x -D \"${LDAP_BIND_DN}\" -w \"${LDAP_BIND_PW}\""
+ CMD+=" -f \"$( readlink -f "${LDIF_FILE}" )\""
+
+ echo
+ info "Ausführen:"
+ echo "${CMD}"
+ echo
-CMD="ldapmodify -H \"${LDAP_SERVER}\""
-CMD+=" -x -D \"${LDAP_BIND_DN}\" -w \"${LDAP_BIND_PW}\""
-CMD+=" -f \"$( readlink -f "${LDIF_FILE}" )\""
+ if [[ "${SIMULATE}" != "y" ]] ; then
+ eval ${CMD}
+ echo
+ fi
+
+ info "Fertig."
+
+}
-echo
-echo "Ausführen:"
-echo "${CMD}"
-echo
+main "$@"
-eval ${CMD}
+exit 0
-echo
# vim: ts=4 list