2009-12-10 12:30:43 +01:00
|
|
|
##@file
|
|
|
|
# DBI common functions
|
|
|
|
|
|
|
|
##@class
|
|
|
|
# DBI common functions
|
|
|
|
package Lemonldap::NG::Portal::_DBI;
|
|
|
|
|
|
|
|
use DBI;
|
|
|
|
use base qw(Exporter);
|
|
|
|
use Lemonldap::NG::Portal::Simple;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
our @EXPORT = qw(dbh);
|
|
|
|
|
2012-07-17 10:02:33 +02:00
|
|
|
our $VERSION = '1.2.2';
|
2009-12-10 12:30:43 +01:00
|
|
|
|
2010-10-03 12:26:50 +02:00
|
|
|
## @method protected Lemonldap::NG::Portal::_DBI dbh(string dbiChain, string dbiUser, string dbiPassword)
|
2009-12-21 23:28:38 +01:00
|
|
|
# Create connection to database
|
|
|
|
# @param dbiChain DBI connection chain
|
|
|
|
# @param dbiUser DBI connection user
|
|
|
|
# @param dbiPassword DBI connection password
|
2009-12-10 12:30:43 +01:00
|
|
|
# @return dbh object
|
|
|
|
sub dbh {
|
2009-12-11 22:17:06 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $dbiChain = shift;
|
|
|
|
my $dbiUser = shift;
|
2009-12-10 12:30:43 +01:00
|
|
|
my $dbiPassword = shift;
|
|
|
|
my $dbh;
|
|
|
|
|
|
|
|
# Open connection to database
|
|
|
|
eval {
|
2009-12-11 22:17:06 +01:00
|
|
|
$dbh =
|
|
|
|
DBI->connect_cached( $dbiChain, $dbiUser, $dbiPassword,
|
2009-12-10 12:30:43 +01:00
|
|
|
{ RaiseError => 1, },
|
2009-12-11 22:17:06 +01:00
|
|
|
);
|
2009-12-10 12:30:43 +01:00
|
|
|
};
|
|
|
|
if ($@) {
|
2009-12-11 22:17:06 +01:00
|
|
|
$self->lmLog( "DBI connection error: $@", 'error' );
|
2009-12-10 12:30:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-21 23:28:38 +01:00
|
|
|
$self->{_dbh} = $dbh;
|
2009-12-10 12:30:43 +01:00
|
|
|
return $dbh;
|
|
|
|
}
|
|
|
|
|
2010-10-03 12:26:50 +02:00
|
|
|
## @method protected Lemonldap::NG::Portal::_DBI hash_password(string password, string hash)
|
2009-12-21 23:28:38 +01:00
|
|
|
# Return hashed password for SQL SELECT WHERE clause
|
|
|
|
# @param password clear password
|
|
|
|
# @param hash hash mechanism
|
|
|
|
# @return hashed password
|
|
|
|
sub hash_password {
|
2010-03-01 21:32:28 +01:00
|
|
|
my $self = shift;
|
2009-12-21 23:28:38 +01:00
|
|
|
my $password = shift;
|
2010-03-01 21:32:28 +01:00
|
|
|
my $hash = shift;
|
2009-12-21 23:28:38 +01:00
|
|
|
|
|
|
|
if ( $hash =~ /^(md5|sha|sha1)$/i ) {
|
2009-12-22 09:46:54 +01:00
|
|
|
$self->lmLog( "Using " . uc($hash) . " to hash password", 'debug' );
|
2010-10-25 14:59:46 +02:00
|
|
|
return uc($hash) . "($password)";
|
2009-12-21 23:28:38 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->lmLog( "No valid password hash, using clear text for password",
|
2012-07-17 10:02:33 +02:00
|
|
|
'warning' );
|
2010-10-23 10:09:41 +02:00
|
|
|
return $password;
|
2009-12-21 23:28:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-10-03 12:26:50 +02:00
|
|
|
## @method protected Lemonldap::NG::Portal::_DBI check_password(string user, string password)
|
2009-12-21 23:28:38 +01:00
|
|
|
# Verify user and password with SQL SELECT
|
|
|
|
# @param user user
|
|
|
|
# @param password password
|
|
|
|
# @return boolean result
|
|
|
|
sub check_password {
|
2010-03-01 21:32:28 +01:00
|
|
|
my $self = shift;
|
2009-12-22 09:46:54 +01:00
|
|
|
my $dbh = shift;
|
|
|
|
my $user = $self->{user};
|
|
|
|
my $password = $self->{password};
|
2009-12-21 23:28:38 +01:00
|
|
|
my $table = $self->{dbiAuthTable};
|
|
|
|
my $loginCol = $self->{dbiAuthLoginCol};
|
|
|
|
my $passwordCol = $self->{dbiAuthPasswordCol};
|
|
|
|
|
2009-12-22 09:46:54 +01:00
|
|
|
# Password hash
|
2010-10-25 14:59:46 +02:00
|
|
|
my $passwordsql = $self->hash_password( "?", $self->{dbiAuthPasswordHash} );
|
2009-12-22 09:46:54 +01:00
|
|
|
|
2009-12-21 23:28:38 +01:00
|
|
|
my @rows = ();
|
|
|
|
eval {
|
2009-12-22 09:46:54 +01:00
|
|
|
my $sth = $dbh->prepare(
|
2010-10-25 14:59:46 +02:00
|
|
|
"SELECT $loginCol FROM $table WHERE $loginCol=? AND $passwordCol=$passwordsql"
|
2009-12-21 23:28:38 +01:00
|
|
|
);
|
2010-10-23 10:09:41 +02:00
|
|
|
$sth->execute( $user, $password );
|
2009-12-21 23:28:38 +01:00
|
|
|
@rows = $sth->fetchrow_array();
|
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
$self->lmLog( "DBI error: $@", 'error' );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( @rows == 1 ) {
|
|
|
|
$self->lmLog( "One row returned by SQL query", 'debug' );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->lmLog( "Bad password for $user", 'error' );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-10-03 12:26:50 +02:00
|
|
|
## @method protected Lemonldap::NG::Portal::_DBI modify_password(string user, string password, string userCol, string passwordCol)
|
2009-12-21 23:28:38 +01:00
|
|
|
# Modify password with SQL UPDATE
|
|
|
|
# @param user user
|
|
|
|
# @param password password
|
2009-12-30 20:42:17 +01:00
|
|
|
# @param userCol optional user column
|
|
|
|
# @param passwordCol optional password column
|
2009-12-21 23:28:38 +01:00
|
|
|
# @return boolean result
|
|
|
|
sub modify_password {
|
2010-03-01 21:32:28 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $user = shift;
|
|
|
|
my $password = shift;
|
|
|
|
my $userCol = shift || $self->{dbiAuthLoginCol};
|
2009-12-30 20:42:17 +01:00
|
|
|
my $passwordCol = shift || $self->{dbiAuthPasswordCol};
|
2010-03-01 21:32:28 +01:00
|
|
|
|
2009-12-30 20:42:17 +01:00
|
|
|
my $table = $self->{dbiAuthTable};
|
2009-12-21 23:28:38 +01:00
|
|
|
|
2010-10-25 14:59:46 +02:00
|
|
|
# Password hash
|
|
|
|
my $passwordsql = $self->hash_password( "?", $self->{dbiAuthPasswordHash} );
|
|
|
|
|
2009-12-21 23:28:38 +01:00
|
|
|
eval {
|
2009-12-22 09:46:54 +01:00
|
|
|
my $sth =
|
2010-10-25 14:59:46 +02:00
|
|
|
$self->{_dbh}->prepare(
|
|
|
|
"UPDATE $table SET $passwordCol=$passwordsql WHERE $userCol=?");
|
2010-10-23 10:09:41 +02:00
|
|
|
$sth->execute( $password, $user );
|
2009-12-21 23:28:38 +01:00
|
|
|
};
|
|
|
|
if ($@) {
|
|
|
|
$self->lmLog( "DBI password modification error: $@", 'error' );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-12-10 12:30:43 +01:00
|
|
|
1;
|