-
Notifications
You must be signed in to change notification settings - Fork 1
/
zcs-sync-ad.sh
78 lines (67 loc) · 2.02 KB
/
zcs-sync-ad.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
#!/bin/bash
# zcs-sync-ad.sh syncs AD users and Zimbra users
#
# by RaveMaker - http://ravemaker.net
# Load settings
if [ -f settings.cfg ] ; then
echo "Loading settings..."
source settings.cfg
else
echo "ERROR: Create settings.cfg (from settings.cfg.example)"
exit
fi;
# Folder settings
ADS_TMP=$TMP_DIR/users_ads.lst
ZCS_TMP=$TMP_DIR/users_zcs.lst
DIF_TMP=$TMP_DIR/users_dif.lst
# Clean up users list
rm -f $ADS_TMP $ZCS_TMP $DIF_TMP
# Add excluded accounts to AD list
cat $HOME_DIR/$EXCLUDE_FILE | grep $DOMAIN_NAME > $ADS_TMP
# Extract users from ADS
echo -n "Quering ADS... "
$LDAPSEARCH -x -H $LDAP_SERVER -b $BASEDN -D "$BINDDN" -w $BINDPW "$FILTER" $FIELDS | grep "@$DOMAIN_NAME" | awk '{print $2}' >> $ADS_TMP
sort -k3 $ADS_TMP -o $ADS_TMP
COUNT="$(cat $ADS_TMP | wc -l)"
if [ $COUNT == "0" ]; then exit; fi
echo "Found $COUNT users ($ADS_TMP)"
# Extract users from ZCS
echo -n "Quering ZCS... "
$ZMPROV -l gaa $DOMAIN_NAME > $ZCS_TMP
sort -k3 $ZCS_TMP -o $ZCS_TMP
COUNT="$(cat $ZCS_TMP | wc -l)"
if [ $COUNT == "0" ]; then exit; fi
echo "Found $COUNT users ($ZCS_TMP)"
# Generate diff
echo "Generating diff file ($DIF_TMP)"
diff -u $ZCS_TMP $ADS_TMP | grep "$DOMAIN_NAME" > $DIF_TMP
# Import new users
echo -n "New users: "
cat $DIF_TMP | grep ^+ | wc -l
for i in $(cat $DIF_TMP | grep ^+ | sed s/^+//g);
do
echo -n " - Adding $i ";
$ZMPROV createAccount $i passwd > /dev/null;
RES=$?
if [ "$RES" == "0" ]; then echo "[Ok]"; else echo "[Err]"; fi
done
# Delete old users
echo -n "Old users: "
cat $DIF_TMP | grep ^- | wc -l
for i in $(cat $DIF_TMP | grep ^- | sed s/^-//g);
do
read -p "Delete account: $i [y/N]?"
if [ "$REPLY" == "y" ] || [ "$REPLY" == "Y" ]; then
echo -n "Deleting account $i..."
$ZMPROV deleteAccount $i > /dev/null;
RES=$?
if [ "$RES" == "0" ]; then echo "[Ok]"; else echo "[Err]"; fi
fi
done
# Clean up users list
read -p "Keep user lists [y/N]?"
if [ "$REPLY" != "y" ] && [ "$REPLY" != "Y" ]; then
rm -f $ADS_TMP $ZCS_TMP $DIF_TMP;
fi
echo ""
echo "Done"