-
Notifications
You must be signed in to change notification settings - Fork 1
/
ldap
executable file
·308 lines (253 loc) · 11.7 KB
/
ldap
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env bash
# -------------------------------------------------------------------------------
#
# Copyright (c) 2016 Slava Vladyshevsky. All rights reserved.
# Licensed under the MIT License. See LICENSE file in the project root.
#
# Author: Slava Vladyshevsky <slava.vladyshevsky(a)gmail.com>
# Project: DevOps Automation
#
# LDAP/Active Directory CLI management tool
#
# -------------------------------------------------------------------------------
### configuration section begin
set -a
# shell environment may override the default value for this variable
CLI_HOME=${CLI_HOME:-"/opt/cli"}
set +a
### configuration section end
[[ -f ${CLI_HOME}/cli.lib ]] && . ${CLI_HOME}/cli.lib \
|| { echo >&2 "cannot load ${CLI_HOME}/cli.lib, exiting"; exit 127; }
if [[ $# -lt 2 ]]; then
echo "LDAP/AD CLI management tool"
echo "Usage: ./${ME} <object> <action> [<args 1> ... <arg N>]"
echo "Objects and actions:"
egrep '^[[:space:]]*#H#' ${0} | sed 's/#H#/ /g'
exit 0
fi
obj=${1:-}
cmd=${2:-}
shift 2
# this temporary folder will be removed upon exit or error
makeTempDir
# the LDAP credentials may be fetched from the secret vault too
#LDAP_BINDDN=$(${CLI_HOME}/vault get /secret/cli/ldap/binddn)
#${CLI_HOME}/vault get /secret/cli/ldap/bindpw > ${TMP_DIR}/bindpw
echo "${LDAP_BINDPW}" > ${TMP_DIR}/bindpw
# checking required variables and tools
assertVar LDAP_BINDDN
assertVar LDAP_BINDPW
assertVar LDAP_URI
assertVar LDAP_BASE
assertVar LDAP_EXT
command -v adtool &>/dev/null || die "required tool adtool is not found, exiting"
function attr_validator () { validateString attr 64 'a-zA-Z0-9_.-'; }
function container_validator () { validateString container 64 ' a-zA-Z0-9_.-'; }
function first_validator () { validateString first 32 'a-zA-Z0-9-'; }
function format_validator () { assertLen format 32; }
function group_validator () { validateString group 64 ' a-zA-Z0-9_.-'; }
function last_validator () { validateString last 32 'a-zA-Z0-9-'; }
function login_validator () { validateString login 20 'a-zA-Z0-9_.-'; }
function mail_validator () { validateRegexp mail 64 '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,8}$'; }
function name_validator () { validateString name 64 ' a-zA-Z0-9-'; }
function org_validator () { validateString org 64 ' a-zA-Z0-9_.-'; }
function password_validator () { validateString password 16 'a-zA-Z0-9_~!@#$%^/.,:;)(}{=+-'; }
function pattern_validator () { validateString pattern 64 ' a-zA-Z0-9_.-*'; }
function value_validator () { assertLen value 256; }
function adcmd() {
BASE="${BASE:-$LDAP_BASE}"
adtool -H "${LDAP_URI}" -b "${BASE}" -D "${LDAP_BINDDN}" -w $(<${TMP_DIR}/bindpw) "$@" >${TMP_DIR}/out
[[ ${FORMAT:-} ]] && sed ${FORMAT} ${TMP_DIR}/out || cat ${TMP_DIR}/out
}
case ${obj} in
#############################################################################
#H# group <list|add|del|users|adduser|deluser> <args...>
group)
case ${cmd} in
###======================================================================
#H# list (dir) [--org <org name>] [--format <short|long>]
list|dir)
getOptions "org:|format:" "$@"
case ${format:=short} in
long) FORMAT='' ;;
short) FORMAT='s/^..=\([^,]*\),.*$/\1/' ;;
*) die "unknown format \"${format}\"" ;;
esac
[[ ${org:-} ]] && BASE="OU=${org},${LDAP_EXT}" || BASE="${LDAP_BASE}"
adcmd search objectclass group
;;
###======================================================================
#H# add (create) [--org <org name>] --group <group name>
add|create)
getOptions "org:|group" "$@"
[[ ${org:-} ]] && BASE="OU=${org},${LDAP_EXT}" || BASE="${LDAP_BASE}"
adcmd groupcreate "${group}" "CN=Groups,${BASE}"
msg "successfully added group \"${group}\""
;;
###======================================================================
#H# del (remove) [--org <org name>] --group <group name>
del|remove)
getOptions "org:|group" "$@"
[[ ${org:-} ]] && BASE="OU=${org},${LDAP_EXT}" || BASE="${LDAP_BASE}"
adcmd groupdelete "${group}"
msg "successfully deleted group \"${group}\""
;;
###======================================================================
#H# users (members) --group <group name> [--format <short|long>]
users|members)
getOptions "group|format:" "$@"
case ${format:=short} in
long) FORMAT='' ;;
short) FORMAT='s/^..=\([^,]*\),.*$/\1/' ;;
*) die "unknown format \"${format}\"" ;;
esac
adcmd attributeget "${group}" member 2>/dev/null || true
;;
###======================================================================
#H# adduser (useradd) --group <group name> --org <org name> --name <user name>
adduser|useradd)
getOptions "group|org|name" "$@"
adcmd groupadduser "${group}" "${name}" "CN=Users,OU=${org},${LDAP_EXT}"
msg "successfully added user \"${org}/${name}\" to the group \"${group}\""
;;
###======================================================================
#H# deluser (userdel) --group <group name> --org <org name> --name <user name>
deluser|userdel)
getOptions "group|org|name" "$@"
adcmd groupremoveuser "${group}" "${name}" "CN=Users,OU=${org},${LDAP_EXT}"
msg "successfully removed user \"${org}/${name}\" from the group \"${group}\""
;;
###======================================================================
#H#
*) die "unknown command: ${cmd}" ;;
esac ;;
#############################################################################
#H# org <list|add|del> <args...>
org)
case ${cmd} in
###======================================================================
#H# list (dir) [--pattern <pattern *>] [--format <short|long>]
list|dir)
getOptions "pattern:|format:" "$@"
case ${format:=short} in
long) FORMAT='' ;;
short) FORMAT='s/^..=\([^,]*\),.*$/\1/' ;;
*) die "unknown format \"${format}\"" ;;
esac
if [[ ${pattern:-} ]]; then
adcmd search OU "${pattern}"
else
adcmd list "${LDAP_EXT}"
fi
;;
###======================================================================
#H# add (create) --org <org name>
add|create)
getOptions "org" "$@"
adcmd oucreate "${org}" "${LDAP_EXT}"
msg "successfully added organization \"${org}\""
# creating the underling OU structure
for name in "Users" "Groups" "Service Accounts"; do
adcmd containercreate "${name}" "OU=${org},${LDAP_EXT}"
msg "successfully added container \"${org}/${name}\""
done
;;
###======================================================================
#H# del (remove) --org <org name>
del|remove)
getOptions "org" "$@"
BASE="OU=${org},${LDAP_EXT}"
# removing the underling OU structure
for name in "Users" "Groups" "Service Accounts"; do
[[ $(adcmd list "CN=${name},OU=${org},${LDAP_EXT}") ]] && die "container \"${org}/${name}\" is not empty"
adcmd containerdelete "${name}"
msg "successfully deleted container \"${org}/${name}\""
done
BASE="${LDAP_EXT}"
[[ $(adcmd list "OU=${org},${LDAP_EXT}") ]] && die "organization \"${org}\" is not empty"
adcmd oudelete "${org}"
msg "successfully deleted organization \"${org}\""
;;
###======================================================================
#H#
*) die "unknown command: ${cmd}" ;;
esac ;;
#############################################################################
#H# user <list|add|del> <args...>
user)
case ${cmd} in
###======================================================================
#H# groups (memberof) [--org <org name>] --name <user name> [--format <short|long>]
groups|memberof)
getOptions "org:|name|format:" "$@"
case ${format:=short} in
long) FORMAT='' ;;
short) FORMAT='s/^..=\([^,]*\),.*$/\1/' ;;
*) die "unknown format \"${format}\"" ;;
esac
[[ ${org:-} ]] && BASE="OU=${org},${LDAP_EXT}" || BASE="${LDAP_BASE}"
adcmd attributeget "${name}" memberOf 2>/dev/null || true
;;
###======================================================================
#H# list (dir) [--org <org name>] [--container <container>] [--format <short|long>]
list|dir)
getOptions "org:|container:|pattern:|format:" "$@"
case ${format:=short} in
long) FORMAT='' ;;
short) FORMAT='s/^..=\([^,]*\),.*$/\1/' ;;
*) die "unknown format \"${format}\"" ;;
esac
[[ ${org:-} ]] && BASE="OU=${org},${LDAP_EXT}" || BASE="${LDAP_BASE}"
BASE="CN=${container:-Users},${BASE}"
adcmd search objectClass "user"
;;
###======================================================================
#H# add (create) [--org <org name>] [--container <container>] --first <first name> --last <last name>
#H# --login <login name> --mail <email> --password <password>
add|create)
getOptions "org:|container:|first|last|login|mail|password" "$@"
[[ ${org:-} ]] && BASE="OU=${org},${LDAP_EXT}" || BASE="${LDAP_BASE}"
[[ $(adcmd search userPrincipalName ${login}) ]] && die "the user with login \"${login}\" already exists"
name="${first} ${last}"
# Create and rename because "Name Length Limits from the Schema":
# backward compatibility the limit is 20 characters for login name.
# http://technet.microsoft.com/en-us/library/active-directory-maximum-limits-scalability%28v=ws.10%29.aspx
adcmd usercreate ${login} "CN=${container:-Users},${BASE}"
msg "successfully added user ${login} : \"${name}\""
adcmd userrename ${login} "${name}"
adcmd setpass "${name}" "${password}" && msg "set password for user \"${name}\""
adcmd userunlock "${name}" && msg "unlocked user \"${name}\""
msg "setting AD attributes for user \"${name}\""
adcmd attributereplace "${name}" userPrincipalName "${login}"
adcmd attributereplace "${name}" sAMAccountName "${login}"
adcmd attributereplace "${name}" mail "${mail}"
adcmd attributereplace "${name}" givenName "${first}"
adcmd attributereplace "${name}" sn "${last}"
adcmd attributereplace "${name}" displayName "${name}"
;;
###======================================================================
#H# del (remove) [--org <org name>] --name <user name>
del|remove)
getOptions "org:|name" "$@"
[[ ${org:-} ]] && BASE="OU=${org},${LDAP_EXT}" || BASE="${LDAP_BASE}"
adcmd userdelete "${name}"
msg "successfully deleted user \"${name}\""
;;
###======================================================================
#H# getattr --org <org name> --name <user name> --attr <attribute name>
getattr)
getOptions "org|name|attr" "$@"
adcmd attributeget "${name}" ${attr}
;;
###======================================================================
#H# setattr --org <org name> --name <user name> --attr <attribute name> --value <attr value>
setattr)
getOptions "org|name|attr|value" "$@"
adcmd attributereplace "${name}" ${attr} "${value}"
;;
###======================================================================
#H#
*) die "unknown command: ${cmd}" ;;
esac ;;
*) die "unknown object: ${obj}" ;;
esac