forked from ltb-project/ldap-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanLdapBrokenAliases.sh
189 lines (165 loc) · 4.55 KB
/
cleanLdapBrokenAliases.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
#!/bin/bash
#========================================================================
# Script for OpenLDAP 2.3.x minimum
#
# This script will attempt to remove any broken aliases into an
# OpenLDAP directory.
#
# Take some command lined parameters :
# - Option "-b <searchbase>" specified the base where to search
# for broken aliases. No default, it must be specified.
#
# Tested on :
# - GNU/Linux platform ;
#
# Dependences into the PATH :
# - awk
# - sed
# - perl
# - openldap utils (ldapsearch, ldapdelete)
#
# Copyright (C) 2009 Thomas CHEMINEAU
# Copyright (C) 2009 LINAGORA
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# GPL License: http://www.gnu.org/licenses/gpl.txt
#
#========================================================================
# More contributions on http://www.linagora.org
#========================================================================
#========================================================================
# Changelog
#========================================================================
# Version 0.1 (2009):
# - First version
# Author: Thomas CHEMINEAU (LINAGORA)
#========================================================================
#------------------------------------------------------------------------
# PARAMETERS
#------------------------------------------------------------------------
#
# LDAP host URI
# eg: ldap://localhost:389
#
MY_LDAP_URI="ldap://localhost:389"
#
# LDAP bind DN which have write rights
# eg: cn=Manager,dc=example,dc=com
#
MY_LDAP_BINDDN="cn=Manager,dc=example,dc=com"
#
# LDAP bind password
#
MY_LDAP_BINDPW="secret"
#
# Log header format
# Could include unix commands
#
MY_LOG_HEADER="`date +\"%b %e %T\"` `hostname` `basename $0`[$$]:"
#------------------------------------------------------------------------
# INIT
#------------------------------------------------------------------------
# Some others parameters. It is recommended to not change them.
MY_LDAP_AUTHTOKEN="-D ${MY_LDAP_BINDDN} -w ${MY_LDAP_BINDPW} -H ${MY_LDAP_URI}"
MY_LDAP_SEARCHBASE=""
MY_SCRIPTNAME="$0"
#------------------------------------------------------------------------
# FUNCTIONS
#------------------------------------------------------------------------
#
# Delete all broken aliases into a specific tree.
#
delete_broken_aliases() {
# $1: search base dn
for alias_dn in `search_dn "$1" "sub" "(objectclass=alias)"`
do
object_dn=`search_aliasedObjectName "${alias_dn}"`
if [ `test_dn "${object_dn}"` -ne 0 ] ; then
if [ `delete_dn "${alias_dn}"` -eq 0 ] ; then
print_trace "removing broken alias ${alias_dn} [OK]"
else
print_trace "removing broken alias ${alias_dn} [FAILED]"
fi
fi
done
}
#
# Delete an entry identified by a DN.
#
delete_dn() {
# $1: entry dn
ldapdelete ${MY_LDAP_AUTHTOKEN} "$1" > /dev/null 2>&1
echo $?
}
#
# Print information.
#
print_trace() {
# $1: a message
echo "${MY_LOG_HEADER} $1"
}
#
# Print usage.
#
print_usage() {
echo "Usage : ${MY_SCRIPTNAME}]" 1>&2
echo "\t-b <searchbase>" 1>&2
}
#
# Get the aliasedObjectName value of an LDAP alias.
#
search_aliasedObjectName() {
# $1: alias dn
ldapsearch -LLL ${MY_LDAP_AUTHTOKEN} -b "$1" -s base aliasedObjectName \
| perl -p0e 's/\n //g' | grep -i "aliasedObjectName" | awk -F': ' '{print $2}'
}
#
# Do a LDAP search and return all DN found.
#
search_dn() {
# $1: base dn
# $2: scope
# $3: filter
ldapsearch -LLL ${MY_LDAP_AUTHTOKEN} -b "$1" -S "" -s "$2" "$3" dn \
| perl -p0e 's/\n //g' | awk -F': ' '{print $2}'
}
#
# Test if a entry exists.
#
test_dn() {
# $1: entry dn
ldapsearch -LLL ${MY_LDAP_AUTHTOKEN} -b "$1" -s base dn > /dev/null 2>&1
echo $?
}
#------------------------------------------------------------------------
# MAIN
#------------------------------------------------------------------------
if [ "$#" -ne "2" ]; then
echo "Error: wrong number of arguments"
print_usage
exit 1
fi
while [ "$1" != "" ]; do
case "$1" in
-b)
shift
MY_LDAP_SEARCHBASE="$1"
shift
;;
*)
print_usage
exit 1
;;
esac
done
delete_broken_aliases "${MY_LDAP_SEARCHBASE}"
exit 0