184 lines
4.7 KiB
Bash
Executable File
184 lines
4.7 KiB
Bash
Executable File
#!/bin/bash
|
|
############################################################
|
|
# Decription: manage user in directory
|
|
#
|
|
# Author: Gilles Mouchet (gilles.mouchet@gmail.com)
|
|
# Creation Date: 27-Dec-2024
|
|
# Version: 1.0
|
|
# Install:
|
|
# see README.md
|
|
# Usage: ./manage_user.sh --help
|
|
# Changelog:
|
|
# V1.0 - 28-Dec-2024 - GMo
|
|
# Added
|
|
# - Creation of script from scratch
|
|
#
|
|
############################################################
|
|
|
|
# debug
|
|
#set -x
|
|
|
|
#------------------------------------------------------------------------------
|
|
# printUsge
|
|
function printUsage {
|
|
/bin/cat << EOF
|
|
|
|
Usage: $progName [options]
|
|
|
|
Options:
|
|
-a <user_cn>,--add <user_cn> Add user in the directory
|
|
IMPORTANT: The first and last name are separated by a space.
|
|
Spaces in the first and last name must be replaced by hyphens.
|
|
Examples: Von Doe becomes Van-Doe
|
|
-d <user_cn>,--del=<user_cn> Deleted user from directory
|
|
-h,--help Show this help
|
|
-v,--version Show version
|
|
|
|
Examples:
|
|
Add user
|
|
$progName -a "Yvan Descloux"
|
|
$progName --add="John Von-Doe"
|
|
|
|
Delete user John Von-Doe
|
|
$progName -d "John Von-Doe"
|
|
|
|
Delete user Yvan Descloux
|
|
$progName --del="Yvan Descloux"
|
|
|
|
EOF
|
|
}
|
|
|
|
#------------------------------------------------------------------------------
|
|
# addUser
|
|
function addUser {
|
|
# check if the format is coorect
|
|
if [ $(echo $user_to_add | grep -o " " | wc -l) != "1" ]; then
|
|
echo "'$user_to_add' format is not correct"
|
|
echo "The first name and last name must be separated by at least one space"
|
|
exit 1
|
|
fi
|
|
# parse cn
|
|
first_name=$(echo $user_to_add | cut -d' ' -f1)
|
|
last_name=$(echo $user_to_add | cut -d' ' -f2)
|
|
# get next uidNumber
|
|
getNextUidNumber
|
|
# set password (lastname)
|
|
user_pass=$(slappasswd -s $last_name)
|
|
# set home dir
|
|
home_dir=${first_name:0:3}${last_name}
|
|
lhome_dir=$(echo "${home_dir,,}") #,, set lowercase
|
|
# set mail address
|
|
email="${first_name,,}"."${last_name,,}"@$LDAP_USER_MAIL_DOMAIN
|
|
# add user in directory
|
|
ldapadd -x -H $LDAP_SRV -D "$LDAP_MANAGER_USER" -w $LDAP_MANAGER_PASS <<EOF
|
|
dn: cn=$first_name $last_name,ou=people,$LDAP_BASE
|
|
cn: $first_name $last_name
|
|
displayName: $first_name $last_name
|
|
givenName: $first_name
|
|
sn: $last_name
|
|
objectClass: posixAccount
|
|
objectClass: inetOrgPerson
|
|
objectClass: shadowAccount
|
|
uid: $first_name.$last_name
|
|
uidNumber: $next_uidNumber
|
|
gidNumber: $next_uidNumber
|
|
userPassword: $user_pass
|
|
mail: $email
|
|
homeDirectory: /home/$lhome_dir
|
|
EOF
|
|
}
|
|
#------------------------------------------------------------------------------
|
|
# delUser
|
|
function delUser {
|
|
#echo $user_to_del
|
|
# request confirmation
|
|
echo -e "\nYou will DELETE 'cn=$user_to_del,ou=people,$LDAP_BASE' from the directory"
|
|
read -p "Are you sure? " -n 1 -r
|
|
echo -e "\n"
|
|
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
ldapdelete -x -H $LDAP_SRV -D "$LDAP_MANAGER_USER" -w $LDAP_MANAGER_PASS \
|
|
"cn=$user_to_del,ou=people,$LDAP_BASE"
|
|
if [ "$?" == "0" ]; then
|
|
echo "$user_to_del successfully deleted from directory"
|
|
fi
|
|
fi
|
|
}
|
|
#------------------------------------------------------------------------------
|
|
# main
|
|
# version
|
|
version="v1.0.0"
|
|
# script name
|
|
progName="./$(/bin/basename $0)"
|
|
# read all functios
|
|
source functions.sh
|
|
# read config
|
|
readConfig
|
|
# check that a parameter exists
|
|
if [ -z "$1" ]; then
|
|
printUsage
|
|
exit 1
|
|
fi
|
|
|
|
while test $# -gt 0
|
|
do
|
|
case "$1" in
|
|
-a|--add*)
|
|
# --add parameter
|
|
if [[ $1 =~ add= ]]; then
|
|
user_to_add="$(echo $1 | cut -f2 -d=)"
|
|
if [ -z "${user_to_add}" ]; then
|
|
printUsage
|
|
exit 1
|
|
fi
|
|
addUser
|
|
shift
|
|
# -a parameter
|
|
else
|
|
# check if argument from -a exist
|
|
if [ -z "$2" ]; then
|
|
printUsage
|
|
exit 1
|
|
fi
|
|
user_to_add="$2"
|
|
addUser
|
|
shift 2
|
|
fi
|
|
;;
|
|
-d|--del*)
|
|
# --del parameter
|
|
if [[ $1 =~ del= ]]; then
|
|
user_to_del="$(echo $1 | cut -f2 -d=)"
|
|
if [ -z "${user_to_del}" ]; then
|
|
printUsage
|
|
exit 1
|
|
fi
|
|
delUser
|
|
shift
|
|
# -d parameter
|
|
else
|
|
# check if argument from -d exist
|
|
if [ -z "$2" ]; then
|
|
printUsage
|
|
exit 1
|
|
fi
|
|
user_to_del="$2"
|
|
delUser
|
|
shift 2
|
|
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
|