-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap-deluser.sh
68 lines (59 loc) · 1.62 KB
/
ldap-deluser.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
usage()
{
echo "usage: $0 -u <uid> [ -r ] -h <help>"
echo " -r : this optional command option will delete user's home directory along with the user".
}
BASE=`grep "^BASE" /etc/openldap/ldap.conf | cut -d' ' -f2`
BINDDN="cn=Manager,$BASE"
GROUP_BASE="ou=Group,$BASE"
if [ -z $BASE ]; then
echo "System is not configured with LDAP Base domain, please fix it."
exit 1
fi
DEL_HOME_DIR="false"
while getopts ":u:rh" opt_char
do
case $opt_char in
u)
USERID=$OPTARG
;;
r)
DEL_HOME_DIR="true"
;;
h)
usage
exit
;;
\?)
echo "$OPTARG is not a valid option."
usage
exit
;;
esac
done
if [ -z "$USERID" ]; then
echo "User name can not be empty."
exit 1
fi
PEOPLE_BASE="ou=People,$BASE"
SEARCH_RESULT=`ldapsearch -x -b "$PEOPLE_BASE" -s one -LLL "(uid=$USERID)" uid | grep dn`
if [ -z "$SEARCH_RESULT" ]; then
echo "$USERID is not existed in LDAP, nothing can be done"
exit 1
fi
if [ -e del_user.ldif ]; then
rm del_user.ldif
fi
sed "s/USER/$USERID/g; s/BASE/$BASE/" del_user.template > del_user.ldif
for GRP in `ldapsearch -x -b $GROUP_BASE -s one -LLL "(memberuid=$USERID)" cn | grep ^cn | awk '{print $2}'`
do
sed "s/DEPARTMENT/$GRP/; s/ACTION/delete/; s/USER/$USERID/" grpMember.template >> del_user.ldif
done
sed -i "s/BASE_DOMAIN/$GROUP_BASE/" del_user.ldif
ldapmodify -W -x -D $BINDDN -f del_user.ldif
rm del_user.ldif
if [ "$DEL_HOME_DIR" == "true" ]; then
echo "Deleting $USERID home directory ..."
rm -rf /home/$USERID
fi