[4] | 1 | package LATMOS::Accounts::Bases::Ad; |
---|
| 2 | |
---|
| 3 | use 5.010000; |
---|
| 4 | use strict; |
---|
| 5 | use warnings; |
---|
| 6 | |
---|
[289] | 7 | use base qw(LATMOS::Accounts::Bases::Ldap); |
---|
[63] | 8 | use Net::LDAP; |
---|
[4] | 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 ); |
---|
[189] | 13 | use Unicode::Map8; |
---|
| 14 | use Unicode::String qw(utf16); |
---|
[276] | 15 | use LATMOS::Accounts::Log; |
---|
[4] | 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 | |
---|
[1071] | 40 | =head2 new(%config) |
---|
[4] | 41 | |
---|
| 42 | Create a new LATMOS::Ad object for windows AD $domain. |
---|
| 43 | |
---|
[1071] | 44 | config: |
---|
[4] | 45 | |
---|
[71] | 46 | =over 4 |
---|
[4] | 47 | |
---|
[71] | 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 | |
---|
[4] | 68 | =cut |
---|
| 69 | |
---|
| 70 | sub new { |
---|
[1071] | 71 | my ($class, %config) = @_; |
---|
[4] | 72 | |
---|
[1071] | 73 | $config{domain} or do { |
---|
[1038] | 74 | la_log(LA_ERR, |
---|
| 75 | "Cannot instanciate base %s w/o domain name", |
---|
[1071] | 76 | $config{label} || '(unknown label)' |
---|
[1038] | 77 | ); |
---|
| 78 | return; |
---|
| 79 | }; |
---|
| 80 | |
---|
[63] | 81 | my $self = { |
---|
[1071] | 82 | _top_dn => join(',', map { "dc=$_" } split('\.', $config{domain})), |
---|
[63] | 83 | }; |
---|
[4] | 84 | |
---|
[63] | 85 | bless($self, $class); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | sub load { |
---|
| 89 | my ($self) = @_; |
---|
[792] | 90 | |
---|
| 91 | # If already load, just say ok ! |
---|
| 92 | $self->{_ldap} and return 1; |
---|
| 93 | |
---|
[4] | 94 | # At this point, if still no $server, DNS search |
---|
[63] | 95 | |
---|
| 96 | my $ldap; |
---|
[1071] | 97 | my @ldapservers = ($self->config('server') |
---|
| 98 | ? ($self->_ldap_url($self->config('server'))) |
---|
[287] | 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) { |
---|
[276] | 105 | $self->log(LA_DEBUG, "Trying to connect to ldap %s", $tryserv); |
---|
[63] | 106 | $ldap = Net::LDAP->new( |
---|
| 107 | $tryserv, |
---|
| 108 | ) and last; |
---|
[4] | 109 | } |
---|
[287] | 110 | |
---|
[276] | 111 | if($ldap) { |
---|
[280] | 112 | $self->log(LA_DEBUG, "Connect to ldap server done"); |
---|
[276] | 113 | } else { |
---|
[280] | 114 | $self->log(LA_ERR, "Cannot connect to any ldap server"); |
---|
[276] | 115 | return; # cannot connect to any ldap :\ |
---|
[189] | 116 | }; |
---|
[4] | 117 | |
---|
[1071] | 118 | my $login = $self->config('login'); |
---|
[63] | 119 | $login =~ m/@/ or $login .= '@' . $self->ad_domain; |
---|
[4] | 120 | |
---|
[1071] | 121 | my $msg = $ldap->bind($login, password => $self->config('password')); |
---|
[63] | 122 | $msg->code and do { |
---|
[276] | 123 | $self->log(LA_ERR, "Cannot bind ldap: %s", $msg->error); |
---|
[63] | 124 | return; |
---|
| 125 | }; |
---|
| 126 | |
---|
| 127 | $self->{_ldap} = $ldap; |
---|
| 128 | return 1; |
---|
[4] | 129 | } |
---|
| 130 | |
---|
| 131 | sub _query_zone_ads { |
---|
[63] | 132 | my ($self) = @_; |
---|
[4] | 133 | require Net::DNS; |
---|
| 134 | my @urllist; |
---|
| 135 | |
---|
| 136 | my $resolver = Net::DNS::Resolver->new; |
---|
[289] | 137 | my $query = $resolver->query("_ldap._tcp.dc._msdcs." . $self->ad_domain, |
---|
| 138 | "SRV") or return; |
---|
[4] | 139 | foreach my $rr ( |
---|
| 140 | sort { $a->priority <=> $b->priority || $a->weight <=> $b->weight } |
---|
| 141 | grep { $_->type eq 'SRV' } $query->answer) { |
---|
[189] | 142 | push(@urllist, $self->_ldap_url($rr->target)); # $rr->port)); don't use port |
---|
[4] | 143 | } |
---|
| 144 | |
---|
| 145 | @urllist |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | =head2 ad_domain |
---|
| 149 | |
---|
| 150 | Return the active directory zone |
---|
| 151 | |
---|
| 152 | =cut |
---|
| 153 | |
---|
| 154 | sub ad_domain { |
---|
[1086] | 155 | return $_[0]->config('domain') |
---|
[4] | 156 | } |
---|
| 157 | |
---|
| 158 | 1; |
---|
| 159 | |
---|
| 160 | __END__ |
---|
| 161 | |
---|
| 162 | =head1 SEE ALSO |
---|
| 163 | |
---|
| 164 | =head1 AUTHOR |
---|
| 165 | |
---|
| 166 | Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt> |
---|
| 167 | |
---|
| 168 | =head1 COPYRIGHT AND LICENSE |
---|
| 169 | |
---|
| 170 | Copyright (C) 2008 CNRS SA/CETP/LATMOS |
---|
| 171 | |
---|
| 172 | This library is free software; you can redistribute it and/or modify |
---|
| 173 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 174 | at your option, any later version of Perl 5 you may have available. |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | =cut |
---|