1#!--PERL--
2# -*- indent-tabs-mode: nil; -*-
3# vim:ft=perl:et:sw=4
4# $Id$
5
6# Sympa - SYsteme de Multi-Postage Automatique
7#
8# Copyright (c) 1997, 1998, 1999 Institut Pasteur & Christophe Wolfhugel
9# Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
10# 2006, 2007, 2008, 2009, 2010, 2011 Comite Reseau des Universites
11# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 GIP RENATER
12#
13# This program is free software; you can redistribute it and/or modify
14# it under the terms of the GNU General Public License as published by
15# the Free Software Foundation; either version 2 of the License, or
16# (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
26use lib split(/:/, $ENV{SYMPALIB} || ''), '--modulesdir--';
27use strict;
28use warnings;
29use English qw(-no_match_vars);
30use Getopt::Long;
31use Pod::Usage;
32use Data::Dumper;
33
34use Sympa::Constants;
35use Sympa::Database;
36use Sympa::DatabaseDriver::LDAP;
37use Sympa::Log;    # Show err logs on STDERR.
38
39my %options;
40unless (
41    GetOptions(
42        \%options,
43        (   map {"$_=s"} @{Sympa::DatabaseDriver::LDAP->required_parameters},
44            @{Sympa::DatabaseDriver::LDAP->optional_parameters},
45            qw(use_ssl use_start_tls),    # Deprecated as of 6.2.15
46            qw(filter scope)
47        ),
48        qw(suffix:s attrs:s),
49        qw(help version)
50    )
51) {
52    pod2usage(-exitval => 1, -output => \*STDERR);
53}
54if ($options{'help'}) {
55    pod2usage(0);
56} elsif ($options{'version'}) {
57    printf "Sympa %s\n", Sympa::Constants::VERSION;
58    exit 0;
59}
60
61# Parameters deprecated as of 6.2.15.
62if ($options{use_start_tls}) {
63    $options{use_tls} = 'starttls';
64} elsif ($options{use_ssl}) {
65    $options{use_tls} = 'ldaps';
66}
67delete $options{use_start_tls};
68delete $options{use_ssl};
69
70if ($options{'bind_dn'} and not $options{'bind_password'}) {
71    local $SIG{TERM} = sub { system qw(stty echo) };
72    system qw(stty -echo);
73    print 'Bind password:';
74    my $password = <STDIN>;
75    chomp $password;
76    print "\n";
77    $SIG{TERM}->();
78
79    $options{'bind_password'} = $password;
80}
81
82my $db = Sympa::Database->new('LDAP', %options);
83unless ($db
84    and defined $options{'suffix'}
85    and defined $options{'filter'}) {
86    pod2usage(-exitval => 1, -output => \*STDERR);
87}
88
89print join ' ',
90    map { sprintf '%s=%s', $_, $options{$_} } qw(host suffix filter);
91print "\n";
92
93my ($mesg, $res);
94
95$db->connect or die sprintf "Connect impossible: %s\n", ($db->error || '');
96$mesg = $db->do_operation(
97    'search',
98    base   => $options{'suffix'},
99    filter => $options{'filter'},
100    scope  => ($options{'scope'} || 'sub'),
101    attrs =>
102        ($options{'attrs'} ? [split /\s*,\s*/, $options{'attrs'}] : ['']),
103) or die sprintf "Search  impossible: %s\n", $db->error;
104$res = $mesg->as_struct;
105
106my $cpt = 0;
107foreach my $dn (keys %$res) {
108
109    my $hash = $res->{$dn};
110    print "#$dn\n";
111
112    foreach my $k (keys %$hash) {
113        my $array = $hash->{$k};
114        if ((ref($array) eq 'ARRAY') and ($k ne 'jpegphoto')) {
115            printf "\t%s => %s\n", $k, join(',', @$array);
116        } else {
117            printf "\t%s => %s\n", $k, $array;
118        }
119    }
120    $cpt++;
121}
122
123print "Total : $cpt\n";
124
125$db->disconnect or printf "disconnect impossible: %s\n", $db->error;
126
127__END__
128
129=encoding utf-8
130
131=head1 NAME
132
133sympa_test_ldap, sympa_test_ldap.pl - Testing LDAP connection for Sympa
134
135=head1 SYNOPSIS
136
137  sympa_test_ldap.pl --filter=string --host=string --suffix=string
138  [ --attrs=[ string,...|* ] ]
139  [ --bind_dn=string [ --bind_password=string ] ]
140  [ --port=string ] [ --scope=base|one|sub ]
141  [ --use_tls=starttls|ldaps|none
142    [ --ca_file=string ] [ --ca_path=string ]
143    [ --ca_verify=none|optional|require ]
144    [ --ssl_cert=string ] [ --ssl_ciphers=string ] [ --ssl_key=string ]
145    [ --ssl_version=sslv2|sslv3|tlsv1|tlsv1_1|tlsv1_2|tlsv1_3 ] ]
146
147  sympa_test_ldap.pl --help
148
149  sympa_test_ldap.pl --version
150
151=head1 DESCRIPTION
152
153sympa_test_ldap.pl tests LDAP connection and search operation using LDAP
154driver of Sympa.
155
156=head1 SEE ALSO
157
158L<Sympa::DatabaseDriver::LDAP>.
159
160=head1 HISTORY
161
162testldap.pl was renamed to sympa_test_ldap.pl on Sympa 6.2.
163
164C<--use_ssl> and C<--use_start_tls> options were obsoleted by Sympa 6.2.15.
165C<--use_tls> option would be used instead.
166
167=cut
168