-
Notifications
You must be signed in to change notification settings - Fork 3
/
ds_add_to_group.sh
executable file
·164 lines (143 loc) · 3.49 KB
/
ds_add_to_group.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
# Add Augmented user to OD group
# Usage:
# [-h] help
# [-v] version
# [-a] Add user to admin group by adding to the /Local/Defaults admin group along with
# the /LDAPv3/127.0.0.1 admin group and bwanadmingroup
# [-u] Partners username to augments, add multiple with ""
# [-g[ Groups to add user to
#
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
DIRADMIN="diradmin"
DIRADMINPW="CHANGE_ME"
DEFAULT_GROUP="workgroup"
ADMIN_GROUP="bwanadmingroup"
LDAP_ADMIN_GROUP="admin"
LOCAL_ADMIN_GROUP="admin"
export PATH
shopt -s nocasematch
EXITCODE=0
PROGRAM=`basename $0`
VERSION=1.0
error()
{
echo "$@" 1>&2
usage_and_exit 1
}
usage()
{
cat <<EOF
Usage:
$PROGRAM
[-h] help
[-v] version
[-a] Add user to admin group
[-u] Partners username(s) to use
[-g] Group(s) to add user to
EOF
}
usage_and_exit()
{
usage
exit $1
}
version()
{
echo "$PROGRAM version $VERSION"
}
warning()
{
echo "$@" 1>&2
EXITCODE=`expr $EXITCODE + 1`
}
yes_no()
{
case $1 in
yes | y)
return 0
;;
no | n)
return 1
;;
-*) error "Unrecognized option: $1"
return 1
;;
*)
break
;;
esac
}
add_to_groups()
{
groups_proc=`echo $groups | sed 's/,//g'`
for group in $groups_proc
do
if [[ `dscl /LDAPv3/127.0.0.1 -read /Groups/$group 2>/dev/null | grep -c RecordName:` -eq 1 ]]; then
if [[ `dseditgroup -o checkmember -n /LDAPv3/127.0.0.1 -m $partners_uid -t user $group 2>/dev/null | grep -c '^no'` -eq 1 ]]; then
dseditgroup -o edit -n /LDAPv3/127.0.0.1 -u $DIRADMIN -P $DIRADMINPW -a $partners_uid -t user $group
echo "## Added to group: $group"
else
echo "## $partners_uid is already a member of $group."
fi
else
echo "################################"
echo "The group $group does not exist."
fi
done
if $make_admin; then
dseditgroup -o edit -n /LDAPv3/127.0.0.1 -u $DIRADMIN -P $DIRADMINPW -a $partners_uid -t user $ADMIN_GROUP
dseditgroup -o edit -n /LDAPv3/127.0.0.1 -u $DIRADMIN -P $DIRADMINPW -a $partners_uid -t user $LDAP_ADMIN_GROUP
sudo dseditgroup -o edit -n /Local/Default -u $DIRADMIN -P $DIRADMINPW -a $partners_uid -t user $LOCAL_ADMIN_GROUP
echo "########################################################"
echo "Added user: $partners_uid to admin group"
fi
dsmemberutil flushcache
}
users=
groups=
make_admin=false
while getopts :hvau:g: opt
do
case $opt in
h) usage_and_exit 0
;;
v) version
exit 0
;;
u) users=$OPTARG
;;
g) groups=$OPTARG
;;
a) make_admin=true
;;
'?') echo "$0: invalid opton -$OPTARG" >&2
usage_and_exit 0
exit 1
;;
esac
done
## remove any commas put in to seperate users in " "
users_proc=`echo $users | sed 's/,//g'`
for partners_uid in $users_proc
do
if [[ `dscl /Search -read /Augments/Users:$partners_uid 2>/dev/null | grep -c RealName` -eq 1 ]]; then
user_info=(`dscl /Active\ Directory/All\ Domains -read /Users/$partners_uid GeneratedUID RealName UniqueID | cut -d : -f 2 | sed 's/, /,/g'`)
GeneratedUID=${user_info[0]}
RealName=${user_info[1]}
UniqueID=${user_info[2]}
echo "############################################"
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo "############################################"
echo -n "Are you sure you want add $partners_uid to the group(s) [ $groups ] (yes,no)? "
read response
if yes_no $response -eq 0 ; then
add_to_groups
fi
else
echo "############################################"
echo "Augmented record $partners_uid doesn't exist"
echo "############################################"
fi
done
exit 0