-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcheck_ldap_dn.pl
147 lines (118 loc) · 3.93 KB
/
check_ldap_dn.pl
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
#!/usr/bin/perl -w
#====================================================================
# What's this ?
#====================================================================
# Script designed for Nagios ( http://www.nagios.org )
# Checks if a DN is in an LDAP Server
# Returns Nagios Code
#
# Copyright (C) 2004 Clement OUDOT
# Copyright (C) 2009 LTB-project.org
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#====================================================================
#====================================================================
# Packages
#====================================================================
use strict;
use Net::LDAP;
use Getopt::Std;
#====================================================================
# Global parameters
#====================================================================
my %code = ( Ok => 0, Warning => 1, Critical => 2, Unknown => 3 );
my ( $host, $port, $binddn, $bindpw, $dn ) = &options;
my $timeout = 5;
my $version = 3;
#====================================================================
# Main program
#====================================================================
main();
sub main {
# LDAP Connection
my $ldap = Net::LDAP->new(
$host,
port => $port,
version => $version,
timeout => $timeout
);
unless ($ldap) {
print "LDAP Critical : Pb with LDAP connection\n";
exit $code{Critical};
}
# Bind
if ( $binddn && $bindpw ) {
# Bind witch credentials
my $req_bind = $ldap->bind( $binddn, password => $bindpw );
if ( $req_bind->code ) {
print "LDAP Unknown : Bind Error "
. $req_bind->code . " : "
. $req_bind->error . "\n";
exit $code{Unknown};
}
}
else {
# Bind anonymous
my $req_bind = $ldap->bind();
if ( $req_bind->code ) {
print "LDAP Unknown : Bind Error "
. $req_bind->code . " : "
. $req_bind->error . "\n";
exit $code{Unknown};
}
}
# Base Search
my $req_search = $ldap->search(
base => $dn,
scope => 'base',
filter => 'objectClass=*',
attrs => ['1.1']
);
if ( $req_search->code == 32 ) {
# No such object Error
print "LDAP Critical : $dn not present\n";
$ldap->unbind();
exit $code{Critical};
}
elsif ( $req_search->code ) {
print "LDAP Unknown : Search Error "
. $req_search->code . " : "
. $req_search->error . "\n";
$ldap->unbind();
exit $code{Unknown};
}
else {
print "LDAP OK : $dn is present\n";
$ldap->unbind();
exit $code{Ok};
}
}
sub options {
# Get and check args
my %opts;
getopt( 'HpDWb', \%opts );
&usage unless ( exists( $opts{"H"} ) );
&usage unless ( exists( $opts{"b"} ) );
$opts{"p"} = 389 unless ( exists( $opts{"p"} ) );
$opts{"D"} = 0 unless ( exists( $opts{"D"} ) );
$opts{"w"} = 0 unless ( exists( $opts{"W"} ) );
return ( $opts{"H"}, $opts{"p"}, $opts{"D"}, $opts{"W"}, $opts{"b"} );
}
sub usage {
# Print Help/Error message
print
"LDAP Unknown : Usage :\n$0 -H hostname [-p port] [-D binddn -W bindpw] -b dn\n";
exit $code{Unknown};
}