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

index a95432bee37d5335727ad843ad2f6edf334729c0..5e9cbccedd9295faf53c211d169b5148aa8029f2 100755 (executable)
@@ -153,30 +153,4 @@ main "$@"
 
 exit 0
 
-
-
-main_old() {
-
-    local oifs="${IFS}"
-    IFS="
-"
-
-    for dn in "$@" ; do
-
-        echo >&2
-        echo "Getting LDAP-Object with DN '${dn}' ..." >&2
-        echo >&2
-
-        cmd="ldapsearch -x -LLL -o ldif-wrap=no -h ldap.pixelpark.com -p 389"
-        cmd+=" -b \"${dn}\" -v -x -D \"${LDAP_USR}\" -y \"${LDAP_PWD_FILE}\" "
-        cmd+=" -s base \"objectclass=*\" 2>/dev/null | sort -i"
-        echo "${cmd}" >&2
-        eval ${cmd}
-
-    done
-
-}
-
-main "$@"
-
 # vim: et list
diff --git a/bin/get-ldap-info b/bin/get-ldap-info
new file mode 100755 (executable)
index 0000000..73dd080
--- /dev/null
@@ -0,0 +1,168 @@
+#!/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 complete information about the given LDAP objects by their uid-
+       or mail-Attribute.
+
+       EOF
+)
+
+#------------------------------------------------------------------------------
+usage() {
+    cat <<-EOF
+       Usage: ${BASE_NAME} [Common Options] [LDAP Options] <UID|EMAIL> [<UID|EMAIL> ...]
+              ${BASE_NAME} [-h|--help]
+              ${BASE_NAME} [-V|--version]
+
+           Mandatory Parameter(s):
+               UID|EMAIL:  Either the Uid of the requested object
+                           (Posix name, mostly in the form 'first_name.last_name'), or
+                           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 Uids or 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 oifs="${IFS}"
+    IFS="
+"
+
+    local token=
+    local cmd=
+    local filter=
+    local result=
+
+    local cmd_base="ldapsearch -LLL -o ldif-wrap=no "
+    cmd_base+="-h \"${LDAP_HOST}\" -p ${LDAP_PORT} -x -D \"${LDAP_USR}\" -y \"${LDAP_PWD_FILE}\" "
+
+    for token in "${TOKENS[@]}" ; do
+
+        local -a dns=()
+        local dn=
+
+        echo >&2
+        info "Getting LDAP info about user with token '${GREEN}${token}${NORMAL}' ..." >&2
+
+        filter="(|(uid=${token})(mail=${token})(mailAlternateAddress=${token})(mailEquivalentAddress=${token}))"
+        cmd="${cmd_base} -b \"${LDAP_BASE}\" \"${filter}\" dn 2>/dev/null | grep '^dn' | sed -e 's/^dn:[       ]*//'"
+        debug "Executing: ${cmd}"
+        result=$( eval ${cmd} )
+        if [[ -z "${result}" ]] ; then
+            warn "LDAP object with Uid or Mail '${YELLOW}${token}${NORMAL}' not found."
+            continue
+        fi
+
+        for dn in ${result} ; do
+            echo >&2
+            info "Found DN: '${GREEN}${dn}${NORMAL}'"
+
+            cmd="${cmd_base} -b \"${dn}\" -s base \"objectclass=*\" 2>/dev/null | sort -i"
+            debug "Executing: ${cmd}"
+            eval ${cmd}
+
+        done
+
+    done
+
+}
+
+main "$@"
+
+exit 0
+
+# vim: et list