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