1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
package Kernel::System::Auth::DB;
use strict;
use warnings;
use Crypt::PasswdMD5 qw(unix_md5_crypt apache_md5_crypt);
use Digest::SHA;
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::System::DB',
'Kernel::System::Encode',
'Kernel::System::Log',
'Kernel::System::Main',
'Kernel::System::Valid',
);
sub new {
my ( $Type, %Param ) = @_;
# allocate new hash for object
my $Self = {};
bless( $Self, $Type );
# Debug 0=off 1=on
$Self->{Debug} = 0;
# get config object
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
# get user table
$Self->{UserTable} = $ConfigObject->Get( 'DatabaseUserTable' . $Param{Count} )
|| 'users';
$Self->{UserTableUserID} = $ConfigObject->Get( 'DatabaseUserTableUserID' . $Param{Count} )
|| 'id';
$Self->{UserTableUserPW} = $ConfigObject->Get( 'DatabaseUserTableUserPW' . $Param{Count} )
|| 'pw';
$Self->{UserTableUser} = $ConfigObject->Get( 'DatabaseUserTableUser' . $Param{Count} )
|| 'login';
return $Self;
}
sub GetOption {
my ( $Self, %Param ) = @_;
# check needed stuff
if ( !$Param{What} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need What!"
);
return;
}
# module options
my %Option = (
PreAuth => 0,
);
# return option
return $Option{ $Param{What} };
}
sub Auth {
my ( $Self, %Param ) = @_;
# check needed stuff
if ( !$Param{User} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need User!"
);
return;
}
# get params
my $User = $Param{User} || '';
my $Pw = $Param{Pw} || '';
my $RemoteAddr = $ENV{REMOTE_ADDR} || 'Got no REMOTE_ADDR env!';
my $UserID = '';
my $GetPw = '';
my $Method;
# get database object
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
# sql query
my $SQL = "SELECT $Self->{UserTableUserPW}, $Self->{UserTableUserID}, $Self->{UserTableUser} "
. " FROM "
. " $Self->{UserTable} "
. " WHERE "
. " valid_id IN ( ${\(join ', ', $Kernel::OM->Get('Kernel::System::Valid')->ValidIDsGet())} ) AND "
. " $Self->{UserTableUser} = '" . $DBObject->Quote($User) . "'";
$DBObject->Prepare( SQL => $SQL );
while ( my @Row = $DBObject->FetchrowArray() ) {
$GetPw = $Row[0];
$UserID = $Row[1];
$User = $Row[2];
}
# get needed objects
my $EncodeObject = $Kernel::OM->Get('Kernel::System::Encode');
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
# crypt given pw
my $CryptedPw = '';
my $Salt = $GetPw;
if (
$ConfigObject->Get('AuthModule::DB::CryptType')
&& $ConfigObject->Get('AuthModule::DB::CryptType') eq 'plain'
)
{
$CryptedPw = $Pw;
$Method = 'plain';
}
# md5, bcrypt or sha pw
elsif ( $GetPw !~ /^.{13}$/ ) {
# md5 pw
if ( $GetPw =~ m{\A \$.+? \$.+? \$.* \z}xms ) {
# strip Salt
$Salt =~ s/^(\$.+?\$)(.+?)\$.*$/$2/;
my $Magic = $1;
# encode output, needed by unix_md5_crypt() only non utf8 signs
$EncodeObject->EncodeOutput( \$Pw );
$EncodeObject->EncodeOutput( \$Salt );
if ( $Magic eq '$apr1$' ) {
$CryptedPw = apache_md5_crypt( $Pw, $Salt );
$Method = 'apache_md5_crypt';
}
else {
$CryptedPw = unix_md5_crypt( $Pw, $Salt );
$Method = 'unix_md5_crypt';
}
}
# sha256 pw
elsif ( $GetPw =~ m{\A [0-9a-f]{64} \z}xmsi ) {
my $SHAObject = Digest::SHA->new('sha256');
$EncodeObject->EncodeOutput( \$Pw );
$SHAObject->add($Pw);
$CryptedPw = $SHAObject->hexdigest();
$Method = 'sha256';
}
# sha512 pw
elsif ( $GetPw =~ m{\A [0-9a-f]{128} \z}xmsi ) {
my $SHAObject = Digest::SHA->new('sha512');
$EncodeObject->EncodeOutput( \$Pw );
$SHAObject->add($Pw);
$CryptedPw = $SHAObject->hexdigest();
$Method = 'sha256';
}
elsif ( $GetPw =~ m{^BCRYPT:} ) {
# require module, log errors if module was not found
if ( !$Kernel::OM->Get('Kernel::System::Main')->Require('Crypt::Eksblowfish::Bcrypt') )
{
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message =>
"User: '$User' tried to authenticate with bcrypt but 'Crypt::Eksblowfish::Bcrypt' is not installed!",
);
return;
}
# get salt and cost from stored PW string
my ( $Cost, $Salt, $Base64Hash ) = $GetPw =~ m{^BCRYPT:(\d+):(.{16}):(.*)$}xms;
# remove UTF8 flag, required by Crypt::Eksblowfish::Bcrypt
$EncodeObject->EncodeOutput( \$Pw );
# calculate password hash with the same cost and hash settings
my $Octets = Crypt::Eksblowfish::Bcrypt::bcrypt_hash(
{
key_nul => 1,
cost => $Cost,
salt => $Salt,
},
$Pw
);
$CryptedPw = "BCRYPT:$Cost:$Salt:" . Crypt::Eksblowfish::Bcrypt::en_base64($Octets);
$Method = 'bcrypt';
}
# sha1 pw
elsif ( $GetPw =~ m{\A [0-9a-f]{40} \z}xmsi ) {
my $SHAObject = Digest::SHA->new('sha1');
# encode output, needed by sha1_hex() only non utf8 signs
$EncodeObject->EncodeOutput( \$Pw );
$SHAObject->add($Pw);
$CryptedPw = $SHAObject->hexdigest();
$Method = 'sha1';
}
# No-13-chars-long crypt pw (e.g. in Fedora28).
else {
$EncodeObject->EncodeOutput( \$Pw );
$EncodeObject->EncodeOutput( \$User );
# Encode output, needed by crypt() only non utf8 signs.
$CryptedPw = crypt( $Pw, $User );
$EncodeObject->EncodeInput( \$CryptedPw );
$Method = 'crypt';
}
}
# crypt pw
else {
# strip Salt only for (Extended) DES, not for any of Modular crypt's
if ( $Salt !~ /^\$\d\$/ ) {
$Salt =~ s/^(..).*/$1/;
}
# encode output, needed by crypt() only non utf8 signs
$EncodeObject->EncodeOutput( \$Pw );
$EncodeObject->EncodeOutput( \$Salt );
$CryptedPw = crypt( $Pw, $Salt );
$Method = 'crypt';
}
# just in case for debug!
if ( $Self->{Debug} > 0 ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'notice',
Message =>
"User: '$User' tried to authenticate with Pw: '$Pw' ($UserID/$Method/$CryptedPw/$GetPw/$Salt/$RemoteAddr)",
);
}
# just a note
if ( !$Pw ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'notice',
Message => "User: $User without Pw!!! (REMOTE_ADDR: $RemoteAddr)",
);
return;
}
# login note
elsif ( ( ($GetPw) && ($User) && ($UserID) ) && $CryptedPw eq $GetPw ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'notice',
Message => "User: $User authentication ok (Method: $Method, REMOTE_ADDR: $RemoteAddr).",
);
return $User;
}
# just a note
elsif ( ($UserID) && ($GetPw) ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'notice',
Message =>
"User: $User authentication with wrong Pw!!! (Method: $Method, REMOTE_ADDR: $RemoteAddr)"
);
return;
}
# just a note
else {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'notice',
Message => "User: $User doesn't exist or is invalid!!! (REMOTE_ADDR: $RemoteAddr)"
);
return;
}
}
1;
|