From: Frank Brehm Date: Wed, 8 May 2019 10:19:38 +0000 (+0200) Subject: Adding bin/get-ldap-info X-Git-Tag: 0.4.1~12^2~2 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=d7a0d3e3daf7a2a9a39226080f16d89ef4ef76e6;p=pixelpark%2Fpp-admin-tools.git Adding bin/get-ldap-info --- diff --git a/bin/get-ldap-dn b/bin/get-ldap-dn index a95432b..5e9cbcc 100755 --- a/bin/get-ldap-dn +++ b/bin/get-ldap-dn @@ -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 index 0000000..73dd080 --- /dev/null +++ b/bin/get-ldap-info @@ -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] [ ...] + ${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