1 package SL::Auth::LDAP;
3 use English '-no_match_vars';
5 use SL::Auth::Constants qw(:all);
10 $main::lxdebug->enter_sub();
12 if (!defined eval "require Net::LDAP;") {
13 die 'The module "Net::LDAP" is not installed.';
19 $self->{auth} = shift;
23 $main::lxdebug->leave_sub();
29 $main::lxdebug->enter_sub();
32 my $cfg = $self->{auth}->{LDAP_config};
35 $main::lxdebug->leave_sub();
40 my $port = $cfg->{port} || 389;
41 $self->{ldap} = Net::LDAP->new($cfg->{host}, 'port' => $port);
44 $main::form->error($main::locale->text('The LDAP server "#1:#2" is unreachable. Please check config/authentication.pl.', $cfg->{host}, $port));
48 my $mesg = $self->{ldap}->start_tls('verify' => 'none');
49 if ($mesg->is_error()) {
50 $main::form->error($main::locale->text('The connection to the LDAP server cannot be encrypted (SSL/TLS startup failure). Please check config/authentication.pl.'));
54 if ($cfg->{bind_dn}) {
55 my $mesg = $self->{ldap}->bind($cfg->{bind_dn}, 'password' => $cfg->{bind_password});
56 if ($mesg->is_error()) {
57 $main::form->error($main::locale->text('Binding to the LDAP server as "#1" failed. Please check config/authentication.pl.', $cfg->{bind_dn}));
61 $main::lxdebug->leave_sub();
67 $main::lxdebug->enter_sub();
74 $cfg = $self->{auth}->{LDAP_config};
76 $filter = "$cfg->{filter}";
80 $login =~ s|\\|\\\\|g;
81 $login =~ s|\(|\\\(|g;
82 $login =~ s|\)|\\\)|g;
83 $login =~ s|\*|\\\*|g;
84 $login =~ s|\x00|\\00|g;
86 if ($filter =~ m|<\%login\%>|) {
87 substr($filter, $LAST_MATCH_START[0], $LAST_MATCH_END[0] - $LAST_MATCH_START[0]) = $login;
90 if ((substr($filter, 0, 1) ne '(') || (substr($filter, -1, 1) ne ')')) {
91 $filter = "($filter)";
94 $filter = "(&${filter}($cfg->{attribute}=${login}))";
97 $filter = "$cfg->{attribute}=${login}";
101 $main::lxdebug->leave_sub();
107 $main::lxdebug->enter_sub();
113 $self->{dn_cache} ||= { };
115 if ($self->{dn_cache}->{$login}) {
116 $main::lxdebug->leave_sub();
117 return $self->{dn_cache}->{$login};
120 my $cfg = $self->{auth}->{LDAP_config};
122 my $filter = $self->_get_filter($login);
124 my $mesg = $ldap->search('base' => $cfg->{base_dn}, 'scope' => 'sub', 'filter' => $filter);
126 if ($mesg->is_error() || (0 == $mesg->count())) {
127 $main::lxdebug->leave_sub();
131 my $entry = $mesg->entry(0);
132 $self->{dn_cache}->{$login} = $entry->dn();
134 $main::lxdebug->leave_sub();
136 return $self->{dn_cache}->{$login};
140 $main::lxdebug->enter_sub();
144 my $password = shift;
145 my $is_crypted = shift;
148 $main::lxdebug->leave_sub();
152 my $ldap = $self->_connect();
155 $main::lxdebug->leave_sub();
159 my $dn = $self->_get_user_dn($ldap, $login);
161 $main::lxdebug->message(LXDebug->DEBUG2(), "LDAP authenticate: dn $dn");
164 $main::lxdebug->leave_sub();
168 my $mesg = $ldap->bind($dn, 'password' => $password);
170 $main::lxdebug->message(LXDebug->DEBUG2(), "LDAP authenticate: bind mesg " . $mesg->error());
172 $main::lxdebug->leave_sub();
174 return $mesg->is_error() ? ERR_PASSWORD : OK;
177 sub can_change_password {
181 sub change_password {
186 $main::lxdebug->enter_sub();
188 my $form = $main::form;
189 my $locale = $main::locale;
192 my $cfg = $self->{auth}->{LDAP_config};
195 $form->error($locale->text('config/authentication.pl: Key "LDAP_config" is missing.'));
198 if (!$cfg->{host} || !$cfg->{attribute} || !$cfg->{base_dn}) {
199 $form->error($locale->text('config/authentication.pl: Missing parameters in "LDAP_config". Required parameters are "host", "attribute" and "base_dn".'));
202 $main::lxdebug->leave_sub();