Revision 327e6e57
Von Moritz Bunkus vor fast 14 Jahren hinzugefügt
SL/Auth/DB.pm | ||
---|---|---|
1 | 1 |
package SL::Auth::DB; |
2 | 2 |
|
3 |
use strict; |
|
4 |
|
|
5 |
use Carp; |
|
6 |
|
|
3 | 7 |
use SL::Auth::Constants qw(:all); |
4 | 8 |
use SL::DBUtils; |
5 | 9 |
|
6 |
use strict; |
|
7 |
|
|
8 | 10 |
sub new { |
9 | 11 |
$main::lxdebug->enter_sub(); |
10 | 12 |
|
... | ... | |
26 | 28 |
my $self = shift; |
27 | 29 |
my $login = shift; |
28 | 30 |
my $password = shift; |
29 |
my $is_crypted = shift; |
|
30 | 31 |
|
31 | 32 |
my $dbh = $self->{auth}->dbconnect(); |
32 | 33 |
|
... | ... | |
38 | 39 |
my $query = qq|SELECT password FROM auth."user" WHERE login = ?|; |
39 | 40 |
my ($stored_password) = $dbh->selectrow_array($query, undef, $login); |
40 | 41 |
|
41 |
$password = crypt $password, substr($login, 0, 2) if (!$password || !$is_crypted); |
|
42 |
$stored_password = crypt $stored_password, substr($login, 0, 2) if (!$stored_password); |
|
42 |
my ($algorithm, $algorithm2); |
|
43 |
|
|
44 |
# Empty password hashes in the database mean just that -- empty |
|
45 |
# passwords. Hash it for easier comparison. |
|
46 |
$stored_password = $self->hash_password(password => $stored_password) unless $stored_password; |
|
47 |
($algorithm, $stored_password) = $self->parse_password_entry($stored_password); |
|
48 |
($algorithm2, $password) = $self->parse_password_entry($self->hash_password(password => $password, algorithm => $algorithm, login => $login)); |
|
43 | 49 |
|
44 | 50 |
$main::lxdebug->leave_sub(); |
45 | 51 |
|
... | ... | |
65 | 71 |
return ERR_BACKEND; |
66 | 72 |
} |
67 | 73 |
|
68 |
$password = crypt $password, substr($login, 0, 2) if (!$is_crypted);
|
|
74 |
$password = $self->hash_password(password => $password) unless $is_crypted;
|
|
69 | 75 |
|
70 | 76 |
do_query($main::form, $dbh, qq|UPDATE auth."user" SET password = ? WHERE login = ?|, $password, $login); |
71 | 77 |
|
... | ... | |
80 | 86 |
return 1; |
81 | 87 |
} |
82 | 88 |
|
89 |
sub hash_password { |
|
90 |
my ($self, %params) = @_; |
|
91 |
|
|
92 |
if (!$params{algorithm}) { |
|
93 |
$params{algorithm} = 'SHA1'; |
|
94 |
$params{fallback_algorithm} = 'MD5'; |
|
95 |
} |
|
96 |
|
|
97 |
if ($params{algorithm} eq 'SHA1') { |
|
98 |
if (eval { require Digest::SHA1; 1 }) { |
|
99 |
return '{SHA1}' . Digest::SHA1::sha1_hex($params{password}); |
|
100 |
|
|
101 |
} elsif ($params{fallback_algorithm}) { |
|
102 |
return $self->hash_password(%params, algorithm => $params{fallback_algorithm}); |
|
103 |
|
|
104 |
} else { |
|
105 |
die 'Digest::SHA1 not available'; |
|
106 |
} |
|
107 |
|
|
108 |
} elsif ($params{algorithm} eq 'MD5') { |
|
109 |
require Digest::MD5; |
|
110 |
return '{MD5}' . Digest::MD5::md5_hex($params{password}); |
|
111 |
|
|
112 |
} elsif ($params{algorithm} eq 'CRYPT') { |
|
113 |
return '{CRYPT}' . crypt($params{password}, substr($params{login}, 0, 2)); |
|
114 |
|
|
115 |
} else { |
|
116 |
croak 'Unsupported hash algorithm ' . $params{algorithm}; |
|
117 |
} |
|
118 |
} |
|
119 |
|
|
120 |
sub parse_password_entry { |
|
121 |
my ($self, $password) = @_; |
|
122 |
|
|
123 |
return ($1, $2) if $password =~ m/^\{ ([^\}]+) \} (.+)/x; |
|
124 |
return ('CRYPT', $password); |
|
125 |
} |
|
126 |
|
|
83 | 127 |
1; |
Auch abrufbar als: Unified diff
Hashen der Passwörter mittels Digest::SHA1 (Fallback auf Digest::MD5)
Teil eines Fixes für Bug 1606. Hintergrund ist, dass bei Nutzernamen
mit Umlaut als zweitem Char crypt() ungültiges UTF-8 erstellt: Crypt
nimmt die ersten zwei Bytes (nicht Zeichen) einer Zeichenfolge und
stellt diese als Salt dem Ergebnis vorweg. Das führt zu einem
ungültigen UTF-8-Char, falls das erste Zeichen ASCII 7 bit und das
zweite Zeichen UTF-8 ist. Beispiel: "möp"