source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Ad.pm

Last change on this file was 2175, checked in by nanardon, 5 years ago

Always create internal at db load

  • Property svn:keywords set to Id Rev
File size: 3.8 KB
Line 
1package LATMOS::Accounts::Bases::Ad;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases::Ldap);
8use Net::LDAP;
9use Net::LDAP::Entry;
10use Net::LDAP::Control::Paged;
11use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED ); 
12use Net::LDAP::Util     qw( escape_filter_value );
13use Unicode::Map8;
14use Unicode::String qw(utf16);
15use LATMOS::Accounts::Log;
16
17our $VERSION = (q$Rev: 2051 $ =~ /^Rev: (\d+) /)[0];
18
19=head1 NAME
20
21LATMOS::Ad - Perl extension for blah blah blah
22
23=head1 SYNOPSIS
24
25  use LATMOS::Ad;
26  blah blah blah
27
28=head1 DESCRIPTION
29
30Stub documentation for LATMOS::Ad, created by h2xs. It looks like the
31author of the extension was negligent enough to leave the stub
32unedited.
33
34Blah blah blah.
35
36=head1 FUNCTIONS
37
38=cut
39
40=head2 new(%config)
41
42Create a new LATMOS::Ad object for windows AD $domain.
43
44config:
45
46=over 4
47
48=item domain
49
50The Active directory domain
51
52=item server (optional)
53
54If set, try to connect to this server, if not set, a dns query is performed
55to find AD server, first responding is used.
56
57=item ssl
58
59If set, try to connect using ssl
60
61=item OBJECT_container
62
63The sub path where to find object type of OBJECT and where they should
64be created
65
66=back
67
68=cut
69
70sub new {
71    my ($class, %config) = @_;
72   
73    $config{domain} or do {
74        la_log(LA_ERR,
75            "Cannot instanciate base %s w/o domain name",
76            $config{label} || '(unknown label)'
77        );
78        return;
79    };
80
81    my $self = {
82        _top_dn => join(',', map { "dc=$_" } split('\.', $config{domain})),
83    };
84
85    bless($self, $class);
86}
87
88sub load {
89    my ($self) = @_;
90
91    # If already load, just say ok !
92    $self->{_ldap} and return 1;
93
94    # At this point, if still no $server, DNS search
95
96    my $ldap;
97    my @ldapservers = ($self->config('server')
98        ? ($self->_ldap_url($self->config('server')))
99        : ($self->_query_zone_ads)) or do {
100        la_log(LA_ERR,
101            "Cannot find any ldap server for domain %s", $self->ad_domain);
102        return;
103    };
104    foreach my $tryserv (@ldapservers) {
105        $self->log(LA_DEBUG, "Trying to connect to ldap %s", $tryserv);
106        $ldap = Net::LDAP->new(
107            $tryserv,
108        ) and last;
109    }
110 
111    if($ldap) {
112        $self->log(LA_DEBUG, "Connect to ldap server done");
113    } else {
114        $self->log(LA_ERR, "Cannot connect to any ldap server");
115        return; # cannot connect to any ldap :\
116    };
117   
118    if ($self->config('tls')) {
119        $self->log(LA_DEBUG, "Running start_tls()");
120        $ldap->start_tls(
121            verify => 'none',
122        );
123    }
124
125    my $login = $self->config('login');
126    $login =~ m/@/ or $login .= '@' . $self->ad_domain;
127
128    my $msg = $ldap->bind($login, password => $self->config('password'));
129    $msg->code and do {
130        $self->log(LA_ERR, "Cannot bind ldap: %s", $msg->error);
131        return;
132    };
133
134    $self->{_ldap} = $ldap;
135    return 1;
136}
137
138sub _query_zone_ads {
139    my ($self) = @_;
140    require Net::DNS;
141    my @urllist;
142
143    my $resolver = Net::DNS::Resolver->new;
144    my $query = $resolver->query("_ldap._tcp.dc._msdcs." . $self->ad_domain,
145        "SRV") or return;
146    foreach my $rr (
147        sort { $a->priority <=> $b->priority || $a->weight <=> $b->weight }
148        grep { $_->type eq 'SRV' } $query->answer) {
149        push(@urllist, $self->_ldap_url($rr->target)); # $rr->port)); don't use port
150    }
151
152    @urllist
153}
154
155=head2 ad_domain
156
157Return the active directory zone
158
159=cut
160
161sub ad_domain {
162    return $_[0]->config('domain')
163}
164
1651;
166
167__END__
168
169=head1 SEE ALSO
170
171=head1 AUTHOR
172
173Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt>
174
175=head1 COPYRIGHT AND LICENSE
176
177Copyright (C) 2008 CNRS SA/CETP/LATMOS
178
179This library is free software; you can redistribute it and/or modify
180it under the same terms as Perl itself, either Perl version 5.10.0 or,
181at your option, any later version of Perl 5 you may have available.
182
183
184=cut
Note: See TracBrowser for help on using the repository browser.