-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap.lib.sh
executable file
·360 lines (303 loc) · 10 KB
/
ldap.lib.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash
################################################################################
# ldap.lib.sh - Bash library functions related to LDAP searches
################################################################################
#
# Copyright (C) 2013 - 2014 stepping stone GmbH
# Bern, Switzerland
# http://www.stepping-stone.ch
#
# Authors:
# Christian Affolter <[email protected]>
#
# Licensed under the EUPL, Version 1.1.
#
# You may not use this work except in compliance with the
# Licence.
# You may obtain a copy of the Licence at:
#
# http://www.osor.eu/eupl
#
# Unless required by applicable law or agreed to in
# writing, software distributed under the Licence is
# distributed on an "AS IS" basis,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied.
# See the Licence for the specific language governing
# permissions and limitations under the Licence.
#
################################################################################
# The path to the lib directory.
# The default value only works if not sourced or executed from within $PATH
LIB_DIR=${LIB_DIR:="$(readlink -f ${0%/*})"}
source "${LIB_DIR}/input-output.lib.sh"
LDAPSEARCH_CMD="${LDAPSEARCH_CMD:="/usr/bin/ldapsearch"}"
test -f "${LDAPSEARCH_CMD}" || \
die "Missing ldapsearch command: '${LDAPSEARCH_CMD}'"
LDAPADD_CMD="${LDAPADD_CMD:="/usr/bin/ldapadd"}"
test -f "${LDAPADD_CMD}" || \
die "Missing ldapadd command: '${LDAPADD_CMD}'"
GREP_CMD="${GREP_CMD:="/bin/grep"}"
##
# Private variables, do not overwrite them
#
# Default LDAP server URI
_LDAP_URI="ldap://localhost"
# Default base DN
_LDAP_BASE_DN="dc=example,dc=com"
# Default bind DN and password file for binding.
_LDAP_BIND_DN="cn=Manager,${_LDAP_BASE_DN}"
_LDAP_BIND_PASSWORD_FILE="~/.ldappasswd"
# Default search scope
_LDAP_SEARCH_SCOPE="sub"
# Default LDAP search filter
_LDAP_SEARCH_FILTER='(objectClass=*)'
# Set default LDAP server URI
#
# ldapSetServerUri uri
function ldapSetServerUri()
{
_LDAP_URI="${1}"
}
# Set default LDAP base DN
#
# ldapSetBaseDn baseDn
function ldapSetBaseDn()
{
_LDAP_BASE_DN="${1}"
}
# Set default DN and password file to use for binding.
#
# ldapSetBindCredentials bindDn bindPasswordFile
function ldapSetBindCredentials()
{
_LDAP_BIND_DN="${1}"
_LDAP_BIND_PASSWORD_FILE="${2}"
test -f "${_LDAP_BIND_PASSWORD_FILE}" || \
die "Missing LDAP password file: '${_LDAP_BIND_PASSWORD_FILE}'"
}
# Set default LDAP search scope
#
# Scope is one of 'base', 'one' or 'sub' and defaults to 'sub'.
#
# ldapSetSearchScope scope
function ldapSetSearchScope()
{
_LDAP_SEARCH_SCOPE="${1}"
}
# Set default LDAP search filter
#
# Defaults to '(objectclass=*)'.
#
# ldapSetSearchFilter filter
function ldapSetSearchFilter()
{
_LDAP_SEARCH_FILTER="${1}"
}
# Set default LDAP settings
#
# Wrapper function for
# - ldapSetBindCredentials()
# - ldapSetBaseDn()
# - ldapSetServerUri()
#
# ldapSetDefaults bindDn bindPasswordFile baseDn uri
function ldapSetDefaults ()
{
local bindDn="${1}"
local bindPasswordFile="${2}"
local baseDn="${3}"
local uri="${4}"
ldapSetBindCredentials "${bindDn}" "${bindPasswordFile}"
ldapSetBaseDn "${baseDn}"
ldapSetServerUri "${uri}"
}
# Simplified ldapsearch wrapper.
#
# All arguments are optional and have global default values set.
# See the following function descriptions in order to set the global defaults:
# - ldapSetServerUri()
# - ldapSetBaseDn()
# - ldapSetBindCredentials()
# - ldapSetSearchScope()
# - ldapSetSearchFilter()
#
# The bind password is expected to be in a file (in order to hide it from the
# process list), see ldapSetBindCredentials().
#
# ldapSearch \
# [filter [baseDn [scope [attributes [bindDn [bindPasswordFile [uri]]]]]]]
function ldapSearch ()
{
local filter="${1:-"${_LDAP_SEARCH_FILTER}"}"
local baseDn="${2:-"${_LDAP_BASE_DN}"}"
local scope="${3:-"${_LDAP_SEARCH_SCOPE}"}"
local attributes="${4}"
local bindDn="${5:-"${_LDAP_BIND_DN}"}"
local bindPasswordFile="${6:-"${_LDAP_BIND_PASSWORD_FILE}"}"
local uri="${7:-"${_LDAP_URI}"}"
${LDAPSEARCH_CMD} -b "${baseDn}" \
-LLL \
-s "${scope}" \
-D "${bindDn}" \
-y "${bindPasswordFile}" \
-H "${uri}" \
-W \
-x \
"${filter}" \
${attributes} 2> >(error -)
return $?
}
# Gets the value of a specific attribute out of an LDIF string read from STDIN
#
# Multi-line values and multi-valued attributes aware.
#
# ldapGetAttributeValueFromLdif LdapAttribute [(TRUE|false)]
function ldapGetAttributeValueFromLdif ()
{
local attribute="${1:-"dn"}"
local lookForMultiValuedAttribute=${2:-true}
local lookForAttributeLine=true
local lookForMultiLineValue=false
local ldifLine=""
local value=""
while true; do
while $lookForAttributeLine; do
# Check if it's a line starting with the desired attribute
if ${GREP_CMD} -E -q "^${attribute}: " <<< "$ldifLine"; then
# strip off the leading attribute name, for example: 'cn: '
value="${ldifLine/${attribute}: /}"
lookForMultiLineValue=true
lookForAttributeLine=false
break # do not read again
fi
# Read must be done after the attribute check, otherwise a
# (possible) existing ldifLine from the multi-line value loop
# below will be overwritten.
if ! read; then
# end of input reached
break 2 # exit
fi
# No <NAME> was provided to the above read builtin, this prevents
# word-splitting, which would remove the trailing white space on
# multi-lined values. The current line is available in $REPLY.
ldifLine="$REPLY"
done
while $lookForMultiLineValue; do
if ! read; then
# end of input reached
if test -n "${value}"; then
echo "${value}"
value=""
fi
break 2 # exit
fi
ldifLine="$REPLY"
# Check for multi-line value (a line starting with a space)
if [ "${ldifLine:0:1}" == " " ]; then
# Append it to the previous line without the trailing whitespace
value+="${ldifLine:1}"
else
# Either no multi-line value or the last line of it.
echo "${value}"
value=''
lookForMultiLineValue=false
lookForAttributeLine=true
if ! $lookForMultiValuedAttribute; then
break 2 # exit, no multi-valued attributes expected
fi
fi
done
done
}
# Loads an LDIF file into an LDAP directory.
#
# All but the first argument are optional and have global default values set.
# See the following function descriptions in order to set the global defaults:
# - ldapSetServerUri()
# - ldapSetBaseDn()
# - ldapSetBindCredentials()
#
# The bind password is expected to be in a file (in order to hide it from the
# process list), see ldapSetBindCredentials().
#
# ldapAddLdif ldifFile [bindDn [bindPasswordFile [uri]]]
function ldapAddLdif ()
{
local ldifFile="${1}"
local bindDn="${2:-"${_LDAP_BIND_DN}"}"
local bindPasswordFile="${3:-"${_LDAP_BIND_PASSWORD_FILE}"}"
local uri="${4:-"${_LDAP_URI}"}"
${LDAPADD_CMD} -D "${bindDn}" \
-y "${bindPasswordFile}" \
-H "${uri}" \
-x \
-f "${ldifFile}" 2> >(error -)
return $?
}
# Loads all LDIF files from a given folder into an LDAP directory.
#
# All the files to load must have a .ldif file ending.
# Returns 0 on success otherwise the number of failed LDIFs to load
#
# See ldapAddLdif for more details.
#
# ldapLoadLdifs ldifDir [bindDn [bindPasswordFile [uri]]]
function ldapLoadLdifs ()
{
local ldifDir="${1}"
local bindDn="${2:-"${_LDAP_BIND_DN}"}"
local bindPasswordFile="${3:-"${_LDAP_BIND_PASSWORD_FILE}"}"
local uri="${4:-"${_LDAP_URI}"}"
debug "ldifDir: ${ldifDir}"
debug "bindDn: ${bindDn}"
debug "bindPasswordFile: ${bindPasswordFile}"
debug "uri: ${uri}"
if ! test -d "${ldifDir}"; then
error "Missing LDIF directory: '${ldifDir}'"
return 1
fi
local returnValue=0
for ldif in ${ldifDir}/*.ldif; do
debug "Loading LDIF: ${ldif}"
if ! ldapAddLdif "${ldif}" "${bindDn}" "${bindPasswordFile}" "${uri}"
then
error "Unable to load LDIF: ${ldif}"
((returnValue++))
fi
done
return $returnValue
}
# Wait until a connection to a given LDAP URI is available
#
# Periodically performs an ldapsearch on the NULL DN until a connection is
# possible or a timout occoured.
#
# This is helpfull if a slapd was started just before (either manually or via
# init script) but is not yet ready for processing.
# See http://www.openldap.org/its/index.cgi/?findid=6848
# The functions is inspired by SUSE's slapd init script.
#
# Returns 0 as soon as the LDAP connection is available and 1 if a timeout
# occured.
#
# The timeout defaults to 10 seconds.
#
# ldapWaitForConnection [uri timeout]]
function ldapWaitForConnection()
{
local uri="${1:-"${_LDAP_URI}"}"
local timeout="${2:-10}" # in seconds
local dateCmd="date +%s" # time in seconds since the epoch
local startTime="$( ${dateCmd} )"
debug "uri: ${uri}"
debug "timeOut: ${timeout}"
debug "startTime: ${startTime}"
while [ $(( $( ${dateCmd} ) - ${startTime} )) -lt ${timeout} ]; do
${LDAPSEARCH_CMD} -b "" -s base -x -H "${uri}" &>/dev/null && return 0
debug "seconds passed: $(( $( ${dateCmd} ) - ${startTime} ))"
sleep 1
done
return 1 # timed out
}