From 83aab7d07181883089789231135d62685a2267ca Mon Sep 17 00:00:00 2001 From: Gilles Mouchet Date: Fri, 27 Dec 2024 16:38:44 +0100 Subject: [PATCH] v1.0.0 --- .gitignore | 1 + .vscode/settings.json | 5 ++ README.md | 23 ++++++++ functions.sh | 20 +++++++ ldap.conf.dist | 12 +++++ list_user.sh | 119 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 180 insertions(+) create mode 100644 .gitignore create mode 100755 .vscode/settings.json create mode 100644 README.md create mode 100644 functions.sh create mode 100644 ldap.conf.dist create mode 100755 list_user.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ce3ce26 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +ldap.conf \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100755 index 0000000..b105bc6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "editor.fontSize": 13, + "terminal.integrated.fontSize": 13, + "window.zoomLevel": 1.4, +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4625a2c --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# Description +Groups scripts for managing an openldap directory +## Installation +```bash +git clone https://gitweb.dyndns.org/scripts/ldap-tools.git +``` +## Configuration +Copy `ldap.conf.dist` to `ldap.conf` +Edit the `ldap.conf` and set the parameters according to your configuration (the parameters are commented in file) + +## Scripts list +|script|description|usage| +|:-----|:----------|-----| +|list_user.sh|List directory users|`./list_user.sh --help`| + +## Changelog +#### [1.0.0] - 2024-12-27 +##### Added +- Config file ldap.conf.dist +- Functions scripts +- List_user script (v1.0.0) +- README.md +- Initial version by [GMo](mailto:gilles.mouchet@gmail.com) \ No newline at end of file diff --git a/functions.sh b/functions.sh new file mode 100644 index 0000000..9034d9d --- /dev/null +++ b/functions.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +function readConfig { + confDir=. + cfgFile=${confDir}/config.conf + if [ ! -f $cfgFile ]; then + echo "The conf file '$cfgFile' does not exist !" + exit 1 + fi + + # Read config file + . $cfgFile +} + +# check if ldapsearch exist +ldapsearch_path=$(command -v ldapsearch) +if [ "$?" == "1" ]; then + echo "ldapsearch doesn't exist. Please install openldap-client package" + exit 1 +fi diff --git a/ldap.conf.dist b/ldap.conf.dist new file mode 100644 index 0000000..a47122a --- /dev/null +++ b/ldap.conf.dist @@ -0,0 +1,12 @@ +#!/bin/bash + +# directory base +LDAP_BASE="dc=gmolab,dc=net" + +# openldap server +LDAP_SRV=ldap://kleenex.gmolab.net + +# openldap manager username and password +LDAP_MANAGER_USER="cn=Admin LDAP,ou=people,$LDAP_BASE" +LDAP_MANAGER_PASS=secret + diff --git a/list_user.sh b/list_user.sh new file mode 100755 index 0000000..33b7bf7 --- /dev/null +++ b/list_user.sh @@ -0,0 +1,119 @@ +#!/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 print_usage { + +/bin/cat << EOF + +Usage: $progName [options] + +Options: + -a,--all Show all existing users in the directory + -n ,--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 + print_usage + exit 1 + fi + ldap_arg="cn=$_TAG" + shift + # -n parameter + else + # check if argument from -n exist + if [ -z "$2" ]; then + print_usage + exit 1 + fi + _TAG="$2" + ldap_arg="cn=$_TAG" + shift 2 + #fi + fi + ;; + -h|--help|help) + print_usage + 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 + print_usage +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 \ No newline at end of file