-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap-delMember.sh
70 lines (61 loc) · 1.4 KB
/
ldap-delMember.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
69
70
#!/bin/bash
usage()
{
echo "usage: $0 -u <uid> -G group1,group2..."
}
BASE=`grep "^BASE" /etc/openldap/ldap.conf | cut -d' ' -f2`
if [ -z $BASE ]; then
echo "System is not configured with LDAP Base domain, please fix it."
exit 1
fi
BASE_DOMAIN="ou=Group,$BASE"
BINDDN="cn=Manager,$BASE"
while getopts ":u:G:" opt_char
do
case $opt_char in
u)
USER=$OPTARG
;;
G)
GRPS=$OPTARG
;;
\?)
echo "$OPTARG is not a valid option."
usage
exit
;;
esac
done
if [ -z "$USER" ]; then
echo "User name can not be empty."
exit 1
fi
GROUP_LIST=`echo $GRPS | awk 'BEGIN {
FS=","
ORS=" "
}
{
x=1
while ( x<NF ) {
print $x
x++
}
print $NF "\n"
} '`
PEOPLE_BASE="ou=People,$BASE"
SEARCH_RESULT=`ldapsearch -x -b "$PEOPLE_BASE" -s one -LLL "(uid=$USER)" uid | grep dn`
if [ -z "$SEARCH_RESULT" ]; then
echo "$USER is not existed in LDAP, nothing can be done"
exit 1
fi
if [ -e del_member.ldif ]; then
rm del_member.ldif
fi
for GRP in $GROUP_LIST
do
sed "s/DEPARTMENT/$GRP/; s/ACTION/delete/; s/USER/$USER/" grpMember.template >> del_member.ldif
done
sed -i "s/BASE_DOMAIN/$BASE_DOMAIN/g" del_member.ldif
ldapmodify -W -x -D "$BINDDN" -f del_member.ldif
rm del_member.ldif
echo "$USER is successfully removed from the group(s)."