1 package SL::Auth::LDAP;
3 use English '-no_match_vars';
5 use Scalar::Util qw(weaken);
6 use SL::Auth::Constants qw(:all);
11 $main::lxdebug->enter_sub();
13 if (!defined eval "require Net::LDAP;") {
14 die 'The module "Net::LDAP" is not installed.';
20 $self->{auth} = shift;
25 $main::lxdebug->leave_sub();
31 $main::lxdebug->enter_sub();
34 my $cfg = $self->{auth}->{LDAP_config};
37 $main::lxdebug->leave_sub();
42 my $port = $cfg->{port} || 389;
43 $self->{ldap} = Net::LDAP->new($cfg->{host}, 'port' => $port);
46 $main::form->error($main::locale->text('The LDAP server "#1:#2" is unreachable. Please check config/lx_office.conf.', $cfg->{host}, $port));
50 my $mesg = $self->{ldap}->start_tls('verify' => 'none');
51 if ($mesg->is_error()) {
52 $main::form->error($main::locale->text('The connection to the LDAP server cannot be encrypted (SSL/TLS startup failure). Please check config/lx_office.conf.'));
56 if ($cfg->{bind_dn}) {
57 my $mesg = $self->{ldap}->bind($cfg->{bind_dn}, 'password' => $cfg->{bind_password});
58 if ($mesg->is_error()) {
59 $main::form->error($main::locale->text('Binding to the LDAP server as "#1" failed. Please check config/lx_office.conf.', $cfg->{bind_dn}));
63 $main::lxdebug->leave_sub();
69 $main::lxdebug->enter_sub();
76 $cfg = $self->{auth}->{LDAP_config};
78 $filter = "$cfg->{filter}";
82 $login =~ s|\\|\\\\|g;
83 $login =~ s|\(|\\\(|g;
84 $login =~ s|\)|\\\)|g;
85 $login =~ s|\*|\\\*|g;
86 $login =~ s|\x00|\\00|g;
88 if ($filter =~ m|<\%login\%>|) {
89 substr($filter, $LAST_MATCH_START[0], $LAST_MATCH_END[0] - $LAST_MATCH_START[0]) = $login;
92 if ((substr($filter, 0, 1) ne '(') || (substr($filter, -1, 1) ne ')')) {
93 $filter = "($filter)";
96 $filter = "(&${filter}($cfg->{attribute}=${login}))";
99 $filter = "$cfg->{attribute}=${login}";
103 $main::lxdebug->leave_sub();
109 $main::lxdebug->enter_sub();
115 $self->{dn_cache} ||= { };
117 if ($self->{dn_cache}->{$login}) {
118 $main::lxdebug->leave_sub();
119 return $self->{dn_cache}->{$login};
122 my $cfg = $self->{auth}->{LDAP_config};
124 my $filter = $self->_get_filter($login);
126 my $mesg = $ldap->search('base' => $cfg->{base_dn}, 'scope' => 'sub', 'filter' => $filter);
128 if ($mesg->is_error() || (0 == $mesg->count())) {
129 $main::lxdebug->leave_sub();
133 my $entry = $mesg->entry(0);
134 $self->{dn_cache}->{$login} = $entry->dn();
136 $main::lxdebug->leave_sub();
138 return $self->{dn_cache}->{$login};
142 $main::lxdebug->enter_sub();
146 my $password = shift;
147 my $is_crypted = shift;
150 $main::lxdebug->leave_sub();
154 my $ldap = $self->_connect();
157 $main::lxdebug->leave_sub();
161 my $dn = $self->_get_user_dn($ldap, $login);
163 $main::lxdebug->message(LXDebug->DEBUG2(), "LDAP authenticate: dn $dn");
166 $main::lxdebug->leave_sub();
170 my $mesg = $ldap->bind($dn, 'password' => $password);
172 $main::lxdebug->message(LXDebug->DEBUG2(), "LDAP authenticate: bind mesg " . $mesg->error());
174 $main::lxdebug->leave_sub();
176 return $mesg->is_error() ? ERR_PASSWORD : OK;
179 sub can_change_password {
183 sub requires_cleartext_password {
187 sub change_password {
192 $main::lxdebug->enter_sub();
194 my $form = $main::form;
195 my $locale = $main::locale;
198 my $cfg = $self->{auth}->{LDAP_config};
201 $form->error($locale->text('config/lx_office.conf: Key "authentication/ldap" is missing.'));
204 if (!$cfg->{host} || !$cfg->{attribute} || !$cfg->{base_dn}) {
205 $form->error($locale->text('config/lx_office.conf: Missing parameters in "authentication/ldap". Required parameters are "host", "attribute" and "base_dn".'));
208 $main::lxdebug->leave_sub();