]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Adding bin/get-ldap-mail
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 8 May 2019 13:33:03 +0000 (15:33 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 8 May 2019 13:33:03 +0000 (15:33 +0200)
bin/get-ldap-mail [new file with mode: 0755]

diff --git a/bin/get-ldap-mail b/bin/get-ldap-mail
new file mode 100755 (executable)
index 0000000..ae530a2
--- /dev/null
@@ -0,0 +1,162 @@
+#!/bin/bash
+
+set -e
+set -u
+
+BASE_NAME="$( basename ${0} )"
+MY_REAL_NAME=$( readlink -f $0 )
+BIN_DIR=$( dirname "${MY_REAL_NAME}" )
+BASE_DIR=$( dirname "${BIN_DIR}" )
+LIB_DIR="${BASE_DIR}/lib"
+CONF_DIR="${BASE_DIR}/etc"
+
+if [[ -f "${LIB_DIR}/functions.rc" ]] ; then
+    . "${LIB_DIR}/functions.rc"
+else
+    echo "Bash resource file '${LIB_DIR}/functions.rc' not found" >&2
+    exit 5
+fi
+
+declare -a TOKENS=()
+
+detect_color
+
+DESCRIPTION=$( cat <<-EOF
+       Get all relevant information about the given mail addresses from LDAP.
+
+       EOF
+)
+
+#------------------------------------------------------------------------------
+usage() {
+    cat <<-EOF
+       Usage: ${BASE_NAME} [Common Options] [LDAP Options] <EMAIL> [<EMAIL> ...]
+              ${BASE_NAME} [-h|--help]
+              ${BASE_NAME} [-V|--version]
+
+           Mandatory Parameter(s):
+               EMAIL: The E-Mail address of the account or group to search.
+
+           LDAP Options:
+       EOF
+
+    echo "${LDAP_USAGE_MSG}"
+    echo
+    echo "    Common Options:"
+    echo "${STD_USAGE_MSG}"
+
+}
+
+#------------------------------------------------------------------------------
+get_options() {
+
+    local tmp=
+    local base_dir=
+
+    set +e
+    tmp=$( getopt -o ${LDAP_STD_OPTS_SHORT}${STD_SHORT_OPTIONS} \
+                    --long ${LDAP_STD_OPTS_LONG},${STD_LONG_OPTIONS} \
+                    -n "${BASE_NAME}" -- "$@" )
+    if [[ $? != 0 ]] ; then
+        echo "" >&2
+        usage >&2
+        exit 1
+    fi
+    set -e
+
+    # Note the quotes around `$TEMP': they are essential!
+    eval set -- "${tmp}"
+    eval_common_options "$@"
+    if [[ "${DEBUG}" == 'y' ]] ; then
+        declare -p REMAINING_OPTS
+        declare -p REMAINING_ARGS
+    fi
+
+    eval_ldap_options "${REMAINING_OPTS[@]}" "${REMAINING_ARGS[@]}"
+
+    if [[ "${DEBUG}" == 'y' ]] ; then
+        declare -p REMAINING_OPTS
+        declare -p REMAINING_ARGS
+    fi
+
+    if [[ "${#REMAINING_OPTS[@]}" -gt 0 ]] ; then
+        error "Unknown options: ${REMAINING_OPTS[*]}"
+        echo >&2
+        usage >&2
+        exit 2
+    fi
+
+    if [[ "${#REMAINING_ARGS[@]}" == "0" ]] ; then
+        error "No E-Mail addresses given to retrieve."
+        echo >&2
+        usage >&2
+        exit 2
+    fi
+
+    local i=0
+    local token=
+    for token in "${REMAINING_ARGS[@]}" ; do
+        if [[ "$i" == 0 ]]; then
+            i=1
+            continue
+        fi
+        TOKENS+=(${token})
+        i=$(( $i + 1 ))
+    done
+
+    if [[ "${DEBUG}" == 'y' ]] ; then
+        declare -p TOKENS
+    fi
+
+}
+
+#------------------------------------------------------------------------------
+main() {
+
+    get_options "$@"
+
+    local ldap_filter_oclass="(|"
+    local oclass=
+
+    for oclass in inetLocalMailRecipient inetMailGroup inetMailingListUser inetOrgPerson \
+            inetResource mailGroup mailGroupMember mailRecipient; do
+        ldap_filter_oclass+="(objectClass=${oclass})"
+    done
+    ldap_filter_oclass+=")"
+
+    local filter_tpl="(&${ldap_filter_oclass}(|(mail=@@ADDRESS@@)(mailAlternateAddress=@@ADDRESS@@)"
+    filter_tpl+="(mailEquivalentAddress=@@ADDRESS@@)))"
+
+    local oifs="${IFS}"
+    IFS="
+"
+
+    local token=
+    local cmd=
+    local filter=
+
+    local cmd_base="ldapsearch -LLL -o ldif-wrap=no "
+    cmd_base+="-h \"${LDAP_HOST}\" -p ${LDAP_PORT} -b \"${LDAP_BASE}\""
+    cmd_base+=" -x -D \"${LDAP_USR}\" -y \"${LDAP_PWD_FILE}\" "
+
+    for token in "${TOKENS[@]}" ; do
+
+        echo >&2
+        info "Getting DN of LDAP-Object with E-Mail address '${GREEN}${token}${NORMAL}' ..." >&2
+
+        filter=$( echo "${filter_tpl}" | sed -e "s/@@ADDRESS@@/${token}/g" )
+        cmd="${cmd_base} \"${filter}\" dn cn mail mailAlternateAddress mailEquivalentAddress "
+        cmd+="mgrpRFC822MailMember uniqueMember memberURL mailForwardingAddress mailRoutingAddress"
+        debug "Executing: ${cmd}"
+        echo >&2
+        eval ${cmd}
+
+    done
+
+}
+
+main "$@"
+
+exit 0
+
+# vim: et list