ldap-tools/list_user.sh
2024-12-28 08:25:21 +01:00

119 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
############################################################
# Decription: list user in directory
#
# Author: Gilles Mouchet (gilles.mouchet@gmail.com)
# Creation Date: 27-Dec-2024
# Version: 1.0
# Install:
# see README.md
# Usage: ./list_user.sh --help
# Changelog:
# V1.0 - 27-Dec-2024 - GMo
# Added
# - Creation of script from scratch
#
############################################################
# debug
#set -x
# Function to print help
function printUsage {
/bin/cat << EOF
Usage: $progName [options]
Options:
-a,--all Show all existing users in the directory
-n <user_cn>,--cn=<user_cn> Show seleted user
-h,--help Show this help
-v,--version Show version
Examples:
Show all user
$progName -a
Show user Gilles Mouchet
$progName --cn="Gilles Mouchet"
Show all user with Mouchet in CN
$progName --cn="*Mouchet*"
Show all user with Gilles in CN
$progName -n "Gilles*"
EOF
}
# version
version="v1.0.0"
# script name
progName="./$(/bin/basename $0)"
# read all functios
source functions.sh
# read config
readConfig
#-------------------
# MAIN
#-------------------
while test $# -gt 0
do
case "$1" in
-a|--all*)
ldap_arg="objectClass=inetOrgPerson"
shift
;;
-n|--cn*)
# --cn parameter
if [[ $1 =~ cn= ]]; then
_TAG="$(echo $1 | cut -f2 -d=)"
if [ -z "${_TAG}" ]; then
printUsage
exit 1
fi
ldap_arg="cn=$_TAG"
shift
# -n parameter
else
# check if argument from -n exist
if [ -z "$2" ]; then
printUsage
exit 1
fi
_TAG="$2"
ldap_arg="cn=$_TAG"
shift 2
#fi
fi
;;
-h|--help|help)
printUsage
exit 0
;;
-v|--version)
echo "2022-$(date +Y) $progName $version"
exit
;;
*)
echo "${progName}: invalid option -- '$1'!"
echo -e "Try '$progName help' for more information.\n"
exit 1
;;
esac
done
echo $ldap_arg
if [ -z "${ldap_arg}" ]; then
printUsage
else
#echo "ldapsearch -x -LLL -H $LDAP_SRV -b \"$LDAP_BASE\" -D \"$LDAP_MANAGER_USER\" -w $LDAP_MANAGER_PASS $ldap_arg"
# check that the search result is not null
result=$(ldapsearch -x -LLL -H $LDAP_SRV -b "$LDAP_BASE" -D "$LDAP_MANAGER_USER" -w $LDAP_MANAGER_PASS "$ldap_arg")
if [ -z "${result}" ]; then
echo "Search with $ldap_arg parameter returned no results"
else
ldapsearch -x -LLL -H $LDAP_SRV -b "$LDAP_BASE" -D "$LDAP_MANAGER_USER" -w $LDAP_MANAGER_PASS "$ldap_arg"
fi
fi