-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap-addMember.sh
109 lines (97 loc) · 2.37 KB
/
ldap-addMember.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
usage()
{
echo "usage: $0 -u <uid> -G group1,group2..."
}
add_ldapmember() {
USERID=$1
GRPS=$2
if [ -z "$USERID" ]; 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"
} '`
echo "Grouplist: $GROUP_LIST"
BASE=`grep "^BASE" /etc/openldap/ldap.conf | cut -d' ' -f2`
BASE_DOMAIN="ou=Group,$BASE"
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"
return 1
fi
for GRP in $GROUP_LIST
do
sed "s/DEPARTMENT/$GRP/; s/ACTION/add/; s/USER/$USERID/" grpMember.template >> add_member.ldif
done
sed -i "s/BASE_DOMAIN/$BASE_DOMAIN/g" add_member.ldif
echo "$USERID is processed"
return 0
}
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:f:h" opt_char
do
case $opt_char in
u)
USERID=$OPTARG
;;
G)
GRPS=$OPTARG
;;
f)
FILE=$OPTARG
;;
h)
usage
exit 0
;;
\?)
echo "$OPTARG is not a valid option."
usage
exit
;;
esac
done
if [[ -n $FILE && -n $USERID ]] || [[ -n $FILE && -n $GRPS ]]; then
echo "-f can not be assigned together with -u or -G"
exit 1
elif [[ -n $FILE && ! -e $FILE ]]; then
echo "$FILE is not found."
exit 1
fi
# delete add_member.ldif if there is it in directory
if [ -e add_member.ldif ]; then
rm add_member.ldif
fi
# Add group member for single user
if [[ -n $USERID && -n $GRPS ]]; then
add_ldapmember $USERID $GRPS
fi
# Add group member for multiple users from a file
if [[ -n $FILE ]]; then
while IFS=: read USERID GRPS
do
echo "Line: $USERID, $GRPS"
add_ldapmember $USERID $GRPS
done < $FILE
fi
ldapmodify -W -x -D "$BINDDN" -f add_member.ldif
rm add_member.ldif
echo Succeed