« blib/lib/Lemonldap/NG/Portal/Plugins/SingleSession.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Plugins/Refresh.pm »
| 1 | ##@file | ||||||||||||||||||||||||||||||||||
| 2 | # DBI common functions | ||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||
| 4 | ##@class | ||||||||||||||||||||||||||||||||||
| 5 | # DBI common functions | ||||||||||||||||||||||||||||||||||
| 6 | package Lemonldap::NG::Portal::Lib::DBI; | ||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 19 | ▶ | use DBI; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 19 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 19 | ▶ | use Mouse; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 19 | ▶ | use Crypt::URandom; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | 19 | ▶ | use MIME::Base64 qw/encode_base64url/; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 19 | ▶ | use Lemonldap::NG::Portal::Main::Constants qw(PE_OK PE_DONE); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | 19 | ▶ | use Lemonldap::NG::Common::Lib::DBI qw(check_dbh); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | 19 | extends 'Lemonldap::NG::Common::Module'; | |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | 19 | our $VERSION = '2.23.0'; | |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | # PROPERTIES | ||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | has table => ( | ||||||||||||||||||||||||||||||||||
| 24 | is => 'rw', | ||||||||||||||||||||||||||||||||||
| 25 | lazy => 1, | ||||||||||||||||||||||||||||||||||
| 26 | 327 | ▶ | builder => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||
| 27 | 327 | my $conf = $_[0]->{conf}; | |||||||||||||||||||||||||||||||||
| 28 | 327 | ▶ | return $conf->{dbiUserTable} || $conf->{dbiAuthTable}; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$conf->{'dbiUserTable'} || $conf->{'dbiAuthTable'}
MC/DC0%0/2
$conf->{'dbiUserTable'} || $conf->{'dbiAuthTable'}
$conf->{'dbiUserTable'}
$conf->{'dbiAuthTable'}
Truth table33%1/3
A || B
A= $conf->{'dbiUserTable'}B= $conf->{'dbiAuthTable'}
| |||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||
| 30 | 19 | ); | |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | has pivot => ( | ||||||||||||||||||||||||||||||||||
| 33 | is => 'rw', | ||||||||||||||||||||||||||||||||||
| 34 | lazy => 1, | ||||||||||||||||||||||||||||||||||
| 35 | 19 | ▶ | builder => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||
| 36 | 19 | my $conf = $_[0]->{conf}; | |||||||||||||||||||||||||||||||||
| 37 | 19 | ▶ | return $conf->{userPivot} || $conf->{dbiAuthLoginCol}; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$conf->{'userPivot'} || $conf->{'dbiAuthLoginCol'}
MC/DC0%0/2
$conf->{'userPivot'} || $conf->{'dbiAuthLoginCol'}
$conf->{'userPivot'}
$conf->{'dbiAuthLoginCol'}
Truth table33%1/3
A || B
A= $conf->{'userPivot'}B= $conf->{'dbiAuthLoginCol'}
| |||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||
| 39 | 19 | ); | |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | has mailField => ( | ||||||||||||||||||||||||||||||||||
| 42 | is => 'rw', | ||||||||||||||||||||||||||||||||||
| 43 | lazy => 1, | ||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | builder => sub { | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||
| 45 | 0 | my $conf = $_[0]->{conf}; | |||||||||||||||||||||||||||||||||
| 46 | return | ||||||||||||||||||||||||||||||||||
| 47 | $conf->{dbiMailCol} | ||||||||||||||||||||||||||||||||||
| 48 | || $conf->{userPivot} | ||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | || $conf->{dbiAuthLoginCol}; | ||||||||||||||||||||||||||||||||
|
Condition0%0/6
$conf->{'dbiMailCol'} || $conf->{'userPivot'} || $conf->{'dbiAuthLoginCol'}
$conf->{"dbiMailCol"} or $conf->{"userPivot"}
MC/DC0%0/3
$conf->{'dbiMailCol'} || $conf->{'userPivot'} || $conf->{'dbiAuthLoginCol'}
$conf->{"dbiMailCol"}
$conf->{"userPivot"}
$conf->{'dbiAuthLoginCol'}
Truth table0%0/4
A or B || C
A= $conf->{"dbiMailCol"}B= $conf->{"userPivot"}C= $conf->{'dbiAuthLoginCol'}
| |||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||
| 51 | 19 | ); | |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | # _dbh object: DB connection object | ||||||||||||||||||||||||||||||||||
| 54 | 19 | has _dbh => ( | |||||||||||||||||||||||||||||||||
| 55 | is => 'rw', | ||||||||||||||||||||||||||||||||||
| 56 | lazy => 1, | ||||||||||||||||||||||||||||||||||
| 57 | builder => 'dbh', | ||||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | 395 | ▶ | sub dbh { | ||||||||||||||||||||||||||||||||
|
Subroutine
dbh: called CC=6 SCAR=22.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 61 | 395 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | 395 | my $dbh = check_dbh( $self->{_dbh} ); | |||||||||||||||||||||||||||||||||
| 64 | 395 | ▶ | return $dbh if $dbh; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 65 | 327 | delete $self->{_dbh}; | |||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | 327 | my $conf = $self->{conf}; | |||||||||||||||||||||||||||||||||
| 68 | 327 | $self->{_dbh} = eval { | |||||||||||||||||||||||||||||||||
| 69 | DBI->connect_cached( | ||||||||||||||||||||||||||||||||||
| 70 | $conf->{dbiAuthChain}, $conf->{dbiAuthUser}, | ||||||||||||||||||||||||||||||||||
| 71 | 327 | $conf->{dbiAuthPassword}, { RaiseError => 1, AutoCommit => 1, } | |||||||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||||||||||||||||
| 74 | 327 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 75 | 0 | $self->{p}->logger->error("DBI connection error: $@"); | |||||||||||||||||||||||||||||||||
| 76 | 0 | return 0; | |||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | 327 | ▶ | if ( $conf->{dbiAuthChain} =~ /^dbi:sqlite/i ) { | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 80 | 327 | $self->{_dbh}->{sqlite_unicode} = 1; | |||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||
| 82 | elsif ( $conf->{dbiAuthChain} =~ /^dbi:mysql/i ) { | ||||||||||||||||||||||||||||||||||
| 83 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 84 | 0 | $self->{_dbh}->{mysql_enable_utf8} = 1; | |||||||||||||||||||||||||||||||||
| 85 | 0 | $self->{_dbh}->do("set names 'utf8'"); | |||||||||||||||||||||||||||||||||
| 86 | }; | ||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||
| 88 | elsif ( $conf->{dbiAuthChain} =~ /^dbi:pg/i ) { | ||||||||||||||||||||||||||||||||||
| 89 | 0 | $self->{_dbh}->{pg_enable_utf8} = 1; | |||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | 327 | return $self->{_dbh}; | |||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | # INITIALIZATION | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | # All DBI modules have just to verify that DBI connection is available | ||||||||||||||||||||||||||||||||||
| 98 | 327 | ▶ | sub init { | ||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=12.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 99 | 327 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 100 | 327 | ▶ | $self->_dbh | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 101 | or $self->logger->error("DBI connection has failed, but let's continue"); | ||||||||||||||||||||||||||||||||||
| 102 | 327 | ▶ | unless ( $self->table ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 103 | 0 | $self->logger->error( | |||||||||||||||||||||||||||||||||
| 104 | "SQL Table name is not set, can't load " . ref($self) ); | ||||||||||||||||||||||||||||||||||
| 105 | 0 | return 0; | |||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||
| 107 | 327 | return 1; | |||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | # RUNNING METHODS | ||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | # Return hashed password for use in SQL statement | ||||||||||||||||||||||||||||||||||
| 113 | # @param password clear password | ||||||||||||||||||||||||||||||||||
| 114 | # @param hash hash mechanism | ||||||||||||||||||||||||||||||||||
| 115 | # @return SQL statement string | ||||||||||||||||||||||||||||||||||
| 116 | 29 | ▶ | sub hash_password { | ||||||||||||||||||||||||||||||||
|
Subroutine
hash_password: called CC=2 SCAR=7.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 117 | 29 | my ( $self, $password, $hash ) = @_; | |||||||||||||||||||||||||||||||||
| 118 | 29 | ▶ | if ($hash) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 119 | 0 | $self->logger->debug( "Using " . uc($hash) . " to hash password" ); | |||||||||||||||||||||||||||||||||
| 120 | 0 | return uc($hash) . "($password)"; | |||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||
| 123 | 29 | $self->logger->debug("No password hash, using clear text for password"); | |||||||||||||||||||||||||||||||||
| 124 | 29 | return $password; | |||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | # Return hashed password for use in SQL SELECT statement | ||||||||||||||||||||||||||||||||||
| 128 | # Call hash_password unless encrypt hash is choosen | ||||||||||||||||||||||||||||||||||
| 129 | # @param password clear password | ||||||||||||||||||||||||||||||||||
| 130 | # @param hash hash mechanism | ||||||||||||||||||||||||||||||||||
| 131 | # @return SQL statement string | ||||||||||||||||||||||||||||||||||
| 132 | 27 | ▶ | sub hash_password_for_select { | ||||||||||||||||||||||||||||||||
|
Subroutine
hash_password_for_select: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 133 | 27 | my ( $self, $password, $hash ) = @_; | |||||||||||||||||||||||||||||||||
| 134 | 27 | my $passwordCol = $self->conf->{dbiAuthPasswordCol}; | |||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | 27 | ▶ | if ( $hash =~ /^encrypt$/i ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 137 | 0 | return uc($hash) . "($password,$passwordCol)"; | |||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||
| 139 | else { | ||||||||||||||||||||||||||||||||||
| 140 | 27 | return $self->hash_password( $password, $hash ); | |||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | ## @method protected Lemonldap::NG::Portal::_DBI get_password(ref dbh, string user) | ||||||||||||||||||||||||||||||||||
| 145 | # Get password from database | ||||||||||||||||||||||||||||||||||
| 146 | # @param dbh database handler | ||||||||||||||||||||||||||||||||||
| 147 | # @param user user | ||||||||||||||||||||||||||||||||||
| 148 | # @return password | ||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | sub get_password { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_password: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 150 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 151 | 0 | my $dbh = shift; | |||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my $user = shift || $self->{user}; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
shift() || $self->{'user'}
MC/DC0%0/2
shift() || $self->{'user'}
shift()
$self->{'user'}
Truth table0%0/3
A || B
A= shift()B= $self->{'user'}
| |||||||||||||||||||||||||||||||||||
| 153 | 0 | my $table = $self->conf->{dbiAuthTable}; | |||||||||||||||||||||||||||||||||
| 154 | 0 | my $loginCol = $self->conf->{dbiAuthLoginCol}; | |||||||||||||||||||||||||||||||||
| 155 | 0 | my $passwordCol = $self->conf->{dbiAuthPasswordCol}; | |||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | 0 | my @rows = (); | |||||||||||||||||||||||||||||||||
| 158 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 159 | 0 | my $sth = | |||||||||||||||||||||||||||||||||
| 160 | $dbh->prepare("SELECT $passwordCol FROM $table WHERE $loginCol=?"); | ||||||||||||||||||||||||||||||||||
| 161 | 0 | $sth->execute($user); | |||||||||||||||||||||||||||||||||
| 162 | 0 | @rows = $sth->fetchrow_array(); | |||||||||||||||||||||||||||||||||
| 163 | }; | ||||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 165 | 0 | $self->logger->error("DBI error while getting password: $@"); | |||||||||||||||||||||||||||||||||
| 166 | 0 | return ""; | |||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | if ( @rows == 1 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 170 | 0 | $self->logger->debug("Successfully got password from database"); | |||||||||||||||||||||||||||||||||
| 171 | 0 | return $rows[0]; | |||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||
| 173 | else { | ||||||||||||||||||||||||||||||||||
| 174 | 0 | $self->userLogger->warn("Unable to check password for $user"); | |||||||||||||||||||||||||||||||||
| 175 | 0 | return ""; | |||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||
| 179 | ## @method protected Lemonldap::NG::Portal::_DBI hash_password_from_database | ||||||||||||||||||||||||||||||||||
| 180 | ## (ref dbh, string dbmethod, string dbsalt, string password) | ||||||||||||||||||||||||||||||||||
| 181 | # Hash the given password calling the dbmethod function in database | ||||||||||||||||||||||||||||||||||
| 182 | # @param dbh database handler | ||||||||||||||||||||||||||||||||||
| 183 | # @param dbmethod the database method for hashing | ||||||||||||||||||||||||||||||||||
| 184 | # @param salt the salt used for hashing | ||||||||||||||||||||||||||||||||||
| 185 | # @param password the password to hash | ||||||||||||||||||||||||||||||||||
| 186 | # @return hashed password | ||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | sub hash_password_from_database { | ||||||||||||||||||||||||||||||||
|
Subroutine
hash_password_from_database: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||
| 189 | # Remark: database function must get hexadecimal input | ||||||||||||||||||||||||||||||||||
| 190 | # and send back hexadecimal output | ||||||||||||||||||||||||||||||||||
| 191 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 192 | 0 | my $dbh = shift; | |||||||||||||||||||||||||||||||||
| 193 | 0 | my $dbmethod = shift; | |||||||||||||||||||||||||||||||||
| 194 | 0 | my $dbsalt = shift; | |||||||||||||||||||||||||||||||||
| 195 | 0 | my $password = shift; | |||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||
| 197 | # convert password to hexa | ||||||||||||||||||||||||||||||||||
| 198 | 0 | my $passwordh = unpack "H*", $password; | |||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||
| 200 | 0 | my @rows = (); | |||||||||||||||||||||||||||||||||
| 201 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 202 | 0 | my $sth = $dbh->prepare("SELECT $dbmethod('$passwordh$dbsalt')"); | |||||||||||||||||||||||||||||||||
| 203 | 0 | $sth->execute(); | |||||||||||||||||||||||||||||||||
| 204 | 0 | @rows = $sth->fetchrow_array(); | |||||||||||||||||||||||||||||||||
| 205 | }; | ||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 207 | 0 | $self->logger->error( | |||||||||||||||||||||||||||||||||
| 208 | "DBI error while hashing with '$dbmethod' hash function: $@"); | ||||||||||||||||||||||||||||||||||
| 209 | 0 | $self->userLogger->warn("Unable to check password"); | |||||||||||||||||||||||||||||||||
| 210 | 0 | return ""; | |||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | if ( @rows == 1 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 214 | 0 | $self->logger->debug( | |||||||||||||||||||||||||||||||||
| 215 | "Successfully hashed password with $dbmethod hash function in database" | ||||||||||||||||||||||||||||||||||
| 216 | ); | ||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||
| 218 | # convert salt to binary | ||||||||||||||||||||||||||||||||||
| 219 | 0 | my $dbsaltb = pack 'H*', $dbsalt; | |||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||
| 221 | # convert result to binary | ||||||||||||||||||||||||||||||||||
| 222 | 0 | my $res = pack 'H*', $rows[0]; | |||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||
| 224 | 0 | return encode_base64( $res . $dbsaltb, '' ); | |||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||
| 226 | else { | ||||||||||||||||||||||||||||||||||
| 227 | 0 | $self->userLogger->warn("Unable to check password with '$dbmethod'"); | |||||||||||||||||||||||||||||||||
| 228 | 0 | return ""; | |||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||
| 231 | # Return encode_base64(SQL_METHOD(password + salt) + salt) | ||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||
| 234 | ## @method protected Lemonldap::NG::Portal::_DBI get_salt(string dbhash) | ||||||||||||||||||||||||||||||||||
| 235 | # Return salt from salted hash password | ||||||||||||||||||||||||||||||||||
| 236 | # @param dbhash hash password | ||||||||||||||||||||||||||||||||||
| 237 | # @return extracted salt | ||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | sub get_salt { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_salt: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 239 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 240 | 0 | my $dbhash = shift; | |||||||||||||||||||||||||||||||||
| 241 | 0 | my $dbsalt; | |||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||
| 243 | # get rid of scheme ({sha256}) | ||||||||||||||||||||||||||||||||||
| 244 | 0 | $dbhash =~ s/^\{[^}]+\}(.*)$/$1/; | |||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||
| 246 | # get binary hash | ||||||||||||||||||||||||||||||||||
| 247 | 0 | my $decoded = &decode_base64($dbhash); | |||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||
| 249 | # get last 8 bytes | ||||||||||||||||||||||||||||||||||
| 250 | 0 | $dbsalt = substr $decoded, -8; | |||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||
| 252 | # get hexadecimal version of salt | ||||||||||||||||||||||||||||||||||
| 253 | 0 | $dbsalt = unpack "H*", $dbsalt; | |||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||
| 255 | 0 | return $dbsalt; | |||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||
| 258 | ## @method protected Lemonldap::NG::Portal::_DBI gen_salt() | ||||||||||||||||||||||||||||||||||
| 259 | # Generate 8 bytes of hexadecimal random salt | ||||||||||||||||||||||||||||||||||
| 260 | # @return generated salt | ||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | sub gen_salt { | ||||||||||||||||||||||||||||||||
|
Subroutine
gen_salt: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 262 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 263 | 0 | my $dbsalt; | |||||||||||||||||||||||||||||||||
| 264 | 0 | my @set = ( '0' .. '9', 'A' .. 'F' ); | |||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||
| 266 | 0 | $dbsalt = join '' => map $set[ rand @set ], 1 .. 16; | |||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||
| 268 | 0 | return $dbsalt; | |||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||
| 271 | ## @method protected Lemonldap::NG::Portal::_DBI gen_salt_text() | ||||||||||||||||||||||||||||||||||
| 272 | # Generate 16 bytes of text random salt | ||||||||||||||||||||||||||||||||||
| 273 | # @return generated salt text | ||||||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | sub gen_salt_text { | ||||||||||||||||||||||||||||||||
|
Subroutine
gen_salt_text: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 275 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 276 | 0 | my $dbsalt = encode_base64url( Crypt::URandom::urandom(16) ); | |||||||||||||||||||||||||||||||||
| 277 | 0 | $dbsalt =~ s/-/\./g; | |||||||||||||||||||||||||||||||||
| 278 | 0 | $dbsalt =~ s/_/\//g; | |||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||
| 280 | 0 | return substr( $dbsalt, 0, 16 ); | |||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||
| 283 | ## @method protected Lemonldap::NG::Portal::_DBI get_dynamic_hash_password(ref dbh, | ||||||||||||||||||||||||||||||||||
| 284 | ## string user, string password, string table, string loginCol, string passwordCol) | ||||||||||||||||||||||||||||||||||
| 285 | # Return hashed password for use in SQL statement | ||||||||||||||||||||||||||||||||||
| 286 | # @param dbh database handler | ||||||||||||||||||||||||||||||||||
| 287 | # @param user connected user | ||||||||||||||||||||||||||||||||||
| 288 | # @param password clear password | ||||||||||||||||||||||||||||||||||
| 289 | # @param table authentication table name | ||||||||||||||||||||||||||||||||||
| 290 | # @param loginCol name of the row containing the login | ||||||||||||||||||||||||||||||||||
| 291 | # @param passwordCol name of the row containing the password | ||||||||||||||||||||||||||||||||||
| 292 | # @return hashed password | ||||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | sub get_dynamic_hash_password { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_dynamic_hash_password: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 294 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 295 | 0 | my $dbh = shift; | |||||||||||||||||||||||||||||||||
| 296 | 0 | my $user = shift; | |||||||||||||||||||||||||||||||||
| 297 | 0 | my $password = shift; | |||||||||||||||||||||||||||||||||
| 298 | 0 | my $table = shift; | |||||||||||||||||||||||||||||||||
| 299 | 0 | my $loginCol = shift; | |||||||||||||||||||||||||||||||||
| 300 | 0 | my $passwordCol = shift; | |||||||||||||||||||||||||||||||||
| 301 | 0 | my $dbhash = shift; | |||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||
| 303 | # Authorized hash schemes and salted hash schemes | ||||||||||||||||||||||||||||||||||
| 304 | 0 | my @validSchemes = split / /, $self->conf->{dbiDynamicHashValidSchemes}; | |||||||||||||||||||||||||||||||||
| 305 | my @validSaltedSchemes = split / /, | ||||||||||||||||||||||||||||||||||
| 306 | 0 | $self->conf->{dbiDynamicHashValidSaltedSchemes}; | |||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||
| 308 | 0 | my $dbscheme; # current hash scheme stored in database | |||||||||||||||||||||||||||||||||
| 309 | my $dbmethod; # static hash method corresponding to a database function | ||||||||||||||||||||||||||||||||||
| 310 | 0 | my $dbsalt; # current salt stored in database | |||||||||||||||||||||||||||||||||
| 311 | 0 | my $hash; # hash to compute from user password | |||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||
| 313 | # Get the scheme | ||||||||||||||||||||||||||||||||||
| 314 | 0 | $dbscheme = $dbhash; | |||||||||||||||||||||||||||||||||
| 315 | 0 | $dbscheme =~ s/^\{([^}]+)\}.*/$1/; | |||||||||||||||||||||||||||||||||
| 316 | 0 | ▶ | $dbscheme = "" if $dbscheme eq $dbhash; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||
| 318 | # check for unix style passwords | ||||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | if ( $dbscheme eq "" and $dbhash =~ /^\$(1|5|6)\$/i ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$dbscheme eq "" and $dbhash =~ /^\$(1|5|6)\$/i
MC/DC0%0/2
$dbscheme eq "" and $dbhash =~ /^\$(1|5|6)\$/i
$dbscheme eq ""
$dbhash =~ /^\$(1|5|6)\$/i
Truth table0%0/3
A and B
A= $dbscheme eq ""B= $dbhash =~ /^\$(1|5|6)\$/i
| |||||||||||||||||||||||||||||||||||
| 320 | 0 | $dbscheme = $1; | |||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||
| 322 | #check if unix hash scheme is valid | ||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | if ( not grep( /^unixcrypt$dbscheme$/, @validSaltedSchemes ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 324 | 0 | $self->logger->error( | |||||||||||||||||||||||||||||||||
| 325 | "No valid unix hash scheme: unixcrypt$dbscheme for user $user"); | ||||||||||||||||||||||||||||||||||
| 326 | 0 | $self->userLogger->warn("Unable to check password for $user"); | |||||||||||||||||||||||||||||||||
| 327 | 0 | return; | |||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||
| 330 | $self->logger->debug( | ||||||||||||||||||||||||||||||||||
| 331 | 0 | "Using unixcrypt$dbscheme to hash salted password"); | |||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||
| 333 | 0 | $hash = crypt( $password, $dbhash ); | |||||||||||||||||||||||||||||||||
| 334 | 0 | return $hash; | |||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||
| 337 | # no hash scheme => assume clear text | ||||||||||||||||||||||||||||||||||
| 338 | 0 | ▶ | if ( $dbscheme eq "" ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 339 | 0 | $self->logger->info("Password has no hash scheme"); | |||||||||||||||||||||||||||||||||
| 340 | 0 | return $password; | |||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||
| 344 | # salted hash scheme | ||||||||||||||||||||||||||||||||||
| 345 | 0 | ▶ | if ( grep( /^$dbscheme$/, @validSaltedSchemes ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 346 | 0 | $self->logger->info( | |||||||||||||||||||||||||||||||||
| 347 | "Valid salted hash scheme: $dbscheme found for user $user"); | ||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||
| 349 | # extract non salted hash scheme | ||||||||||||||||||||||||||||||||||
| 350 | 0 | $dbmethod = $dbscheme; | |||||||||||||||||||||||||||||||||
| 351 | 0 | $dbmethod =~ s/^s//i; | |||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||
| 353 | # extract the salt | ||||||||||||||||||||||||||||||||||
| 354 | 0 | $dbsalt = $self->get_salt($dbhash); | |||||||||||||||||||||||||||||||||
| 355 | 0 | $self->logger->debug("Get salt from password: $dbsalt"); | |||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||
| 357 | # Hash password with given hash scheme and salt | ||||||||||||||||||||||||||||||||||
| 358 | 0 | $hash = | |||||||||||||||||||||||||||||||||
| 359 | $self->hash_password_from_database( $dbh, $dbmethod, $dbsalt, | ||||||||||||||||||||||||||||||||||
| 360 | $password ); | ||||||||||||||||||||||||||||||||||
| 361 | 0 | $hash = "{$dbscheme}$hash"; | |||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||
| 363 | 0 | return $hash; | |||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||
| 367 | # static hash scheme | ||||||||||||||||||||||||||||||||||
| 368 | elsif ( grep( /^$dbscheme$/, @validSchemes ) ) { | ||||||||||||||||||||||||||||||||||
| 369 | 0 | $self->logger->info( | |||||||||||||||||||||||||||||||||
| 370 | "Valid hash scheme: $dbscheme found for user $user"); | ||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||
| 372 | # Hash given password with given hash scheme and no salt | ||||||||||||||||||||||||||||||||||
| 373 | 0 | $hash = | |||||||||||||||||||||||||||||||||
| 374 | $self->hash_password_from_database( $dbh, $dbscheme, "", $password ); | ||||||||||||||||||||||||||||||||||
| 375 | 0 | $hash = "{$dbscheme}$hash"; | |||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||
| 377 | 0 | return $hash; | |||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||
| 380 | # no valid hash scheme | ||||||||||||||||||||||||||||||||||
| 381 | else { | ||||||||||||||||||||||||||||||||||
| 382 | 0 | $self->logger->error("No valid hash scheme: $dbscheme for user $user"); | |||||||||||||||||||||||||||||||||
| 383 | 0 | $self->userLogger->warn("Unable to check password for $user"); | |||||||||||||||||||||||||||||||||
| 384 | 0 | return; | |||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||
| 388 | # DEPRECATED: this function returned the password hash as a piece of SQL query | ||||||||||||||||||||||||||||||||||
| 389 | # which is fragile | ||||||||||||||||||||||||||||||||||
| 390 | 0 | ▶ | sub dynamic_hash_password { | ||||||||||||||||||||||||||||||||
|
Subroutine
dynamic_hash_password: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 391 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 392 | 0 | my $dbh = shift; | |||||||||||||||||||||||||||||||||
| 393 | 0 | my $user = shift; | |||||||||||||||||||||||||||||||||
| 394 | 0 | my $password = shift; | |||||||||||||||||||||||||||||||||
| 395 | 0 | my $table = shift; | |||||||||||||||||||||||||||||||||
| 396 | 0 | my $loginCol = shift; | |||||||||||||||||||||||||||||||||
| 397 | 0 | my $passwordCol = shift; | |||||||||||||||||||||||||||||||||
| 398 | 0 | my $dbhash = shift; | |||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||
| 400 | 0 | my $hashed_password = | |||||||||||||||||||||||||||||||||
| 401 | $self->get_dynamic_hash_password( $dbh, $user, $password, $table, | ||||||||||||||||||||||||||||||||||
| 402 | $loginCol, $passwordCol, $dbhash ); | ||||||||||||||||||||||||||||||||||
| 403 | 0 | ▶ | if ($hashed_password) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 404 | 0 | ▶ | if ( $hashed_password eq $password ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 405 | 0 | return "?"; | |||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||
| 407 | else { | ||||||||||||||||||||||||||||||||||
| 408 | 0 | return "'$hashed_password'"; | |||||||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||
| 411 | else { | ||||||||||||||||||||||||||||||||||
| 412 | 0 | return ""; | |||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||
| 416 | ## @method protected Lemonldap::NG::Portal::_DBI get_dynamic_hash_new_password(ref dbh, | ||||||||||||||||||||||||||||||||||
| 417 | ## string user, string password) | ||||||||||||||||||||||||||||||||||
| 418 | # @return hashed password | ||||||||||||||||||||||||||||||||||
| 419 | # @param dbh database handler | ||||||||||||||||||||||||||||||||||
| 420 | # @param user connected user | ||||||||||||||||||||||||||||||||||
| 421 | # @param password clear password | ||||||||||||||||||||||||||||||||||
| 422 | # @param dbscheme the scheme to use for hashing | ||||||||||||||||||||||||||||||||||
| 423 | 0 | ▶ | sub get_dynamic_hash_new_password { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_dynamic_hash_new_password: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 424 | 0 | my ( $self, $req_or_undef, $dbh, $user, $password ) = @_; | |||||||||||||||||||||||||||||||||
| 425 | 0 | ▶ | my $dbscheme = $self->conf->{dbiDynamicHashNewPasswordScheme} || ""; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->conf->{'dbiDynamicHashNewPasswordScheme'} || ''
MC/DC0%0/1
$self->conf->{'dbiDynamicHashNewPasswordScheme'} || ''
$self->conf->{'dbiDynamicHashNewPasswordScheme'}
Truth table0%0/2
A
A= $self->conf->{'dbiDynamicHashNewPasswordScheme'}
| |||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||
| 427 | # Authorized hash schemes and salted hash schemes | ||||||||||||||||||||||||||||||||||
| 428 | 0 | my @validSchemes = split / /, $self->conf->{dbiDynamicHashValidSchemes}; | |||||||||||||||||||||||||||||||||
| 429 | my @validSaltedSchemes = split / /, | ||||||||||||||||||||||||||||||||||
| 430 | 0 | $self->conf->{dbiDynamicHashValidSaltedSchemes}; | |||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||
| 432 | 0 | my $dbmethod; # static hash method corresponding to a database function | |||||||||||||||||||||||||||||||||
| 433 | my $dbsalt; # salt to generate for new hashed password | ||||||||||||||||||||||||||||||||||
| 434 | 0 | my $hash; # hash to compute from user password | |||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||
| 436 | # no hash scheme => assume clear text | ||||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | if ( $dbscheme eq "" ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 438 | 0 | $self->logger->info( | |||||||||||||||||||||||||||||||||
| 439 | "No hash scheme selected, storing password in clear text"); | ||||||||||||||||||||||||||||||||||
| 440 | 0 | return $password; | |||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||
| 444 | # Try to delegate password hashing to a plugin | ||||||||||||||||||||||||||||||||||
| 445 | 0 | ▶ | if ( my $req = $req_or_undef ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 446 | 0 | my $hook_result = {}; | |||||||||||||||||||||||||||||||||
| 447 | 0 | my $h = | |||||||||||||||||||||||||||||||||
| 448 | $self->p->processHook( $req, 'dbiHashPassword', $dbscheme, $password, | ||||||||||||||||||||||||||||||||||
| 449 | $hook_result ); | ||||||||||||||||||||||||||||||||||
| 450 | 0 | ▶ | if ( $h == PE_DONE ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 451 | 0 | my $hashed_password = $hook_result->{hashed_password}; | |||||||||||||||||||||||||||||||||
| 452 | 0 | ▶ | if ($hashed_password) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 453 | 0 | return $hashed_password; | |||||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||||
| 455 | else { | ||||||||||||||||||||||||||||||||||
| 456 | 0 | $self->logger->error( | |||||||||||||||||||||||||||||||||
| 457 | "dbiHashPassword hook did not return a hashed password"); | ||||||||||||||||||||||||||||||||||
| 458 | 0 | return; | |||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||
| 463 | # Check for unix password string | ||||||||||||||||||||||||||||||||||
| 464 | 0 | ▶ | if ( $dbscheme =~ /^unixcrypt(1|5|6)$/i ) { | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 465 | 0 | $self->logger->info("Selected salted hash scheme: $dbscheme"); | |||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||
| 467 | 0 | $dbscheme = $1; | |||||||||||||||||||||||||||||||||
| 468 | 0 | $dbsalt = gen_salt_text(); | |||||||||||||||||||||||||||||||||
| 469 | 0 | $dbsalt = "\$$dbscheme\$$dbsalt\$"; | |||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||
| 471 | 0 | $hash = crypt( $password, $dbsalt ); | |||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||
| 473 | 0 | return $hash; | |||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||
| 477 | # salted hash scheme | ||||||||||||||||||||||||||||||||||
| 478 | elsif ( grep( /^$dbscheme$/, @validSaltedSchemes ) ) { | ||||||||||||||||||||||||||||||||||
| 479 | 0 | $self->logger->info("Selected salted hash scheme: $dbscheme"); | |||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||
| 481 | # extract non salted hash scheme | ||||||||||||||||||||||||||||||||||
| 482 | 0 | $dbmethod = $dbscheme; | |||||||||||||||||||||||||||||||||
| 483 | 0 | $dbmethod =~ s/^s//i; | |||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||
| 485 | # generate the salt | ||||||||||||||||||||||||||||||||||
| 486 | 0 | $dbsalt = $self->gen_salt(); | |||||||||||||||||||||||||||||||||
| 487 | 0 | $self->logger->debug("Generated salt: $dbsalt"); | |||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||
| 489 | # Hash given password with given hash scheme and salt | ||||||||||||||||||||||||||||||||||
| 490 | 0 | $hash = | |||||||||||||||||||||||||||||||||
| 491 | $self->hash_password_from_database( $dbh, $dbmethod, $dbsalt, | ||||||||||||||||||||||||||||||||||
| 492 | $password ); | ||||||||||||||||||||||||||||||||||
| 493 | 0 | $hash = "{$dbscheme}$hash"; | |||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||
| 495 | 0 | return $hash; | |||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||
| 499 | # static hash scheme | ||||||||||||||||||||||||||||||||||
| 500 | elsif ( grep( /^$dbscheme$/, @validSchemes ) ) { | ||||||||||||||||||||||||||||||||||
| 501 | 0 | $self->logger->info("Selected hash scheme: $dbscheme"); | |||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||
| 503 | # Hash given password with given hash scheme and no salt | ||||||||||||||||||||||||||||||||||
| 504 | 0 | $hash = | |||||||||||||||||||||||||||||||||
| 505 | $self->hash_password_from_database( $dbh, $dbscheme, "", $password ); | ||||||||||||||||||||||||||||||||||
| 506 | 0 | $hash = "{$dbscheme}$hash"; | |||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||
| 508 | 0 | return $hash; | |||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||
| 511 | # no valid hash scheme | ||||||||||||||||||||||||||||||||||
| 512 | else { | ||||||||||||||||||||||||||||||||||
| 513 | 0 | $self->logger->error("No selected hash scheme: $dbscheme is invalid"); | |||||||||||||||||||||||||||||||||
| 514 | 0 | $self->userLogger->warn("Unable to store password for $user"); | |||||||||||||||||||||||||||||||||
| 515 | 0 | return; | |||||||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||
| 519 | # DEPRECATED: this function returned the password hash as a piece of SQL query | ||||||||||||||||||||||||||||||||||
| 520 | # which is fragile | ||||||||||||||||||||||||||||||||||
| 521 | 0 | ▶ | sub dynamic_hash_new_password { | ||||||||||||||||||||||||||||||||
|
Subroutine
dynamic_hash_new_password: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 522 | 0 | my ( $self, $dbh, $user, $password ) = @_; | |||||||||||||||||||||||||||||||||
| 523 | 0 | ▶ | my $dbscheme = $self->conf->{dbiDynamicHashNewPasswordScheme} || ""; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->conf->{'dbiDynamicHashNewPasswordScheme'} || ''
MC/DC0%0/1
$self->conf->{'dbiDynamicHashNewPasswordScheme'} || ''
$self->conf->{'dbiDynamicHashNewPasswordScheme'}
Truth table0%0/2
A
A= $self->conf->{'dbiDynamicHashNewPasswordScheme'}
| |||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||
| 525 | # no hash scheme => assume clear text | ||||||||||||||||||||||||||||||||||
| 526 | 0 | ▶ | if ( $dbscheme eq "" ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 527 | 0 | $self->logger->info( | |||||||||||||||||||||||||||||||||
| 528 | "No hash scheme selected, storing password in clear text"); | ||||||||||||||||||||||||||||||||||
| 529 | 0 | return "?"; | |||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||
| 533 | 0 | my $hash = | |||||||||||||||||||||||||||||||||
| 534 | $self->get_dynamic_hash_new_password( undef, $dbh, $user, $password ); | ||||||||||||||||||||||||||||||||||
| 535 | 0 | ▶ | if ($hash) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 536 | 0 | return "'$hash'"; | |||||||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||||||||
| 538 | else { | ||||||||||||||||||||||||||||||||||
| 539 | 0 | return ""; | |||||||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||
| 543 | # This method returns undef if the validation couldn't be performed, so that | ||||||||||||||||||||||||||||||||||
| 544 | # db-side hashing can be tried next | ||||||||||||||||||||||||||||||||||
| 545 | # If validation is successful, return the boolean result | ||||||||||||||||||||||||||||||||||
| 546 | 0 | ▶ | sub try_verify_hash { | ||||||||||||||||||||||||||||||||
|
Subroutine
try_verify_hash: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 547 | 0 | my ( $self, $req, $password, $dbhash ) = @_; | |||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||
| 549 | # Try to delegate password hashing to a plugin | ||||||||||||||||||||||||||||||||||
| 550 | 0 | my $hook_result = {}; | |||||||||||||||||||||||||||||||||
| 551 | 0 | my $h = | |||||||||||||||||||||||||||||||||
| 552 | $self->p->processHook( $req, 'dbiVerifyPassword', $password, $dbhash, | ||||||||||||||||||||||||||||||||||
| 553 | $hook_result ); | ||||||||||||||||||||||||||||||||||
| 554 | 0 | ▶ | if ( $h == PE_DONE ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 555 | 0 | my $result = $hook_result->{result}; | |||||||||||||||||||||||||||||||||
| 556 | 0 | ▶ | $self->logger->warn("Undefined result from dbiVerifyPassword") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 557 | unless defined $result; | ||||||||||||||||||||||||||||||||||
| 558 | 0 | return $result; | |||||||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||||||||
| 560 | 0 | return; | |||||||||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||
| 563 | # Verify user and password with SQL SELECT | ||||||||||||||||||||||||||||||||||
| 564 | # @param user user | ||||||||||||||||||||||||||||||||||
| 565 | # @param password password | ||||||||||||||||||||||||||||||||||
| 566 | # @return boolean result | ||||||||||||||||||||||||||||||||||
| 567 | 27 | ▶ | sub check_password { | ||||||||||||||||||||||||||||||||
|
Subroutine
check_password: called CC=11 SCAR=30.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 568 | 27 | my ( $self, $req_or_user, $password ) = @_; | |||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||
| 570 | 27 | my $req; | |||||||||||||||||||||||||||||||||
| 571 | my $user; | ||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||
| 573 | # If $user is an object then it's a Lemonldap::NG::Portal::Main::Request | ||||||||||||||||||||||||||||||||||
| 574 | # object | ||||||||||||||||||||||||||||||||||
| 575 | 27 | ▶ | if ( ref($req_or_user) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 576 | 27 | $req = $req_or_user; | |||||||||||||||||||||||||||||||||
| 577 | 27 | ▶ | $password ||= $req->data->{password}; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$password ||= $req->data->{"password"}
MC/DC0%0/2
$password ||= $req->data->{"password"}
$password
$req->data->{"password"}
Truth table66%2/3
A ||= B
A= $passwordB= $req->data->{"password"}
| |||||||||||||||||||||||||||||||||||
| 578 | 27 | $user = $req->user; | |||||||||||||||||||||||||||||||||
| 579 | } | ||||||||||||||||||||||||||||||||||
| 580 | else { | ||||||||||||||||||||||||||||||||||
| 581 | 0 | $user = $req_or_user; | |||||||||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||||
| 584 | 27 | my $table = $self->conf->{dbiAuthTable}; | |||||||||||||||||||||||||||||||||
| 585 | 27 | my $loginCol = $self->conf->{dbiAuthLoginCol}; | |||||||||||||||||||||||||||||||||
| 586 | 27 | my $passwordCol = $self->conf->{dbiAuthPasswordCol}; | |||||||||||||||||||||||||||||||||
| 587 | 27 | ▶ | my $dynamicHash = $self->conf->{dbiDynamicHashEnabled} || 0; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->conf->{'dbiDynamicHashEnabled'} || 0
MC/DC0%0/1
$self->conf->{'dbiDynamicHashEnabled'} || 0
$self->conf->{'dbiDynamicHashEnabled'}
Truth table50%1/2
A
A= $self->conf->{'dbiDynamicHashEnabled'}
| |||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||
| 589 | 27 | my $passwordsql; | |||||||||||||||||||||||||||||||||
| 590 | my $comparevalue; | ||||||||||||||||||||||||||||||||||
| 591 | 27 | ▶ | if ( $dynamicHash == 1 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||
| 593 | 0 | my $dbh = $self->dbh; | |||||||||||||||||||||||||||||||||
| 594 | 0 | my $dbhash = | |||||||||||||||||||||||||||||||||
| 595 | $self->get_password( $dbh, $user, $table, $loginCol, $passwordCol ); | ||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||
| 597 | 0 | ▶ | if ( !$dbhash ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 598 | 0 | $self->logger->warn("Password for $user was not found in database"); | |||||||||||||||||||||||||||||||||
| 599 | 0 | return 0; | |||||||||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||
| 602 | # Hooks will not be called if this method was called without a request | ||||||||||||||||||||||||||||||||||
| 603 | 0 | ▶ | if ($req) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||
| 605 | # Attempt to verify password on LemonLDAP side | ||||||||||||||||||||||||||||||||||
| 606 | 0 | my $result = $self->try_verify_hash( $req, $password, $dbhash ); | |||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||
| 608 | # warning: 0 means verification failed. | ||||||||||||||||||||||||||||||||||
| 609 | # undef means no verification was done | ||||||||||||||||||||||||||||||||||
| 610 | 0 | ▶ | if ( defined $result ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 611 | 0 | return $result; | |||||||||||||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||||||||||||||
| 613 | } | ||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||
| 615 | # Compte hash on DB side | ||||||||||||||||||||||||||||||||||
| 616 | 0 | $passwordsql = "?"; | |||||||||||||||||||||||||||||||||
| 617 | 0 | $comparevalue = | |||||||||||||||||||||||||||||||||
| 618 | $self->get_dynamic_hash_password( $dbh, $user, $password, $table, | ||||||||||||||||||||||||||||||||||
| 619 | $loginCol, $passwordCol, $dbhash ); | ||||||||||||||||||||||||||||||||||
| 620 | } | ||||||||||||||||||||||||||||||||||
| 621 | else { | ||||||||||||||||||||||||||||||||||
| 622 | # Static Password hashes | ||||||||||||||||||||||||||||||||||
| 623 | $passwordsql = | ||||||||||||||||||||||||||||||||||
| 624 | $self->hash_password_for_select( "?", | ||||||||||||||||||||||||||||||||||
| 625 | 27 | $self->conf->{dbiAuthPasswordHash} ); | |||||||||||||||||||||||||||||||||
| 626 | 27 | $comparevalue = $password; | |||||||||||||||||||||||||||||||||
| 627 | } | ||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||
| 629 | 27 | my @rows = (); | |||||||||||||||||||||||||||||||||
| 630 | 27 | eval { | |||||||||||||||||||||||||||||||||
| 631 | 27 | my $sth = $self->dbh->prepare( | |||||||||||||||||||||||||||||||||
| 632 | "SELECT $loginCol FROM $table WHERE $loginCol=? AND $passwordCol=$passwordsql" | ||||||||||||||||||||||||||||||||||
| 633 | ); | ||||||||||||||||||||||||||||||||||
| 634 | 27 | $sth->execute( $user, $comparevalue ); | |||||||||||||||||||||||||||||||||
| 635 | 27 | @rows = $sth->fetchrow_array(); | |||||||||||||||||||||||||||||||||
| 636 | }; | ||||||||||||||||||||||||||||||||||
| 637 | 27 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||
| 639 | # If connection isn't available, error is displayed by dbh() | ||||||||||||||||||||||||||||||||||
| 640 | 0 | ▶ | $self->logger->error("DBI error: $@") if ( $self->_dbh ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 641 | 0 | return 0; | |||||||||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||
| 644 | 27 | ▶ | if ( @rows == 1 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 645 | 25 | $self->logger->debug("One row returned by SQL query"); | |||||||||||||||||||||||||||||||||
| 646 | 25 | return 1; | |||||||||||||||||||||||||||||||||
| 647 | } | ||||||||||||||||||||||||||||||||||
| 648 | else { | ||||||||||||||||||||||||||||||||||
| 649 | 2 | $self->userLogger->warn("Bad password for $user"); | |||||||||||||||||||||||||||||||||
| 650 | 2 | return 0; | |||||||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||||
| 653 | } | ||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||
| 655 | 19 | 1; | |||||||||||||||||||||||||||||||||