6 use Scalar::Util qw(weaken);
 
   8 use SL::Auth::Constants qw(:all);
 
  12   $main::lxdebug->enter_sub();
 
  17   $self->{auth} = shift;
 
  22   $main::lxdebug->leave_sub();
 
  28   $main::lxdebug->enter_sub();
 
  34   my $dbh        = $self->{auth}->dbconnect();
 
  37     $main::lxdebug->leave_sub();
 
  41   my $query             = qq|SELECT password FROM auth."user" WHERE login = ?|;
 
  42   my ($stored_password) = $dbh->selectrow_array($query, undef, $login);
 
  44   my ($algorithm, $algorithm2);
 
  46   # Empty password hashes in the database mean just that -- empty
 
  47   # passwords. Hash it for easier comparison.
 
  48   $stored_password               = $self->hash_password(password => $stored_password) unless $stored_password;
 
  49   ($algorithm, $stored_password) = $self->parse_password_entry($stored_password);
 
  50   ($algorithm2, $password)       = $self->parse_password_entry($self->hash_password(password => $password, algorithm => $algorithm, login => $login));
 
  52   $main::lxdebug->leave_sub();
 
  54   return $password eq $stored_password ? OK : ERR_PASSWORD;
 
  57 sub can_change_password {
 
  62   $main::lxdebug->enter_sub();
 
  67   my $is_crypted = shift;
 
  69   my $dbh        = $self->{auth}->dbconnect();
 
  72     $main::lxdebug->leave_sub();
 
  76   $password = $self->hash_password(password => $password) unless $is_crypted;
 
  78   do_query($main::form, $dbh, qq|UPDATE auth."user" SET password = ? WHERE login = ?|, $password, $login);
 
  82   $main::lxdebug->leave_sub();
 
  92   my ($self, %params) = @_;
 
  94   if (!$params{algorithm}) {
 
  95     $params{algorithm}          = 'SHA1';
 
  96     $params{fallback_algorithm} = 'MD5';
 
  99   if ($params{algorithm} eq 'SHA1') {
 
 100     if (eval { require Digest::SHA1; 1 }) {
 
 101       return '{SHA1}' . Digest::SHA1::sha1_hex($params{password});
 
 103     } elsif ($params{fallback_algorithm}) {
 
 104       return $self->hash_password(%params, algorithm => $params{fallback_algorithm});
 
 107       die 'Digest::SHA1 not available';
 
 110   } elsif ($params{algorithm} eq 'MD5') {
 
 112     return '{MD5}' . Digest::MD5::md5_hex($params{password});
 
 114   } elsif ($params{algorithm} eq 'CRYPT') {
 
 115     return '{CRYPT}' . crypt($params{password}, substr($params{login}, 0, 2));
 
 118     croak 'Unsupported hash algorithm ' . $params{algorithm};
 
 122 sub parse_password_entry {
 
 123   my ($self, $password) = @_;
 
 125   return ($1, $2) if $password =~ m/^\{ ([^\}]+) \} (.+)/x;
 
 126   return ('CRYPT', $password);