--- DB_new.pm	Fri Feb 13 02:50:36 2004
+++ DB.pm	Tue Jun 22 20:47:37 2004
@@ -1,8 +1,8 @@
 # --
 # Kernel/System/CustomerAuth/DB.pm - provides the db authentification 
-# Copyright (C) 2001-2004 Martin Edenhofer <martin+code@otrs.org>
+# Copyright (C) 2002-2003 Martin Edenhofer <martin+code@otrs.org>
 # --
-# $Id: DB.pm,v 1.9 2004/02/13 00:50:36 martin Exp $
+# $Id: DB.pm,v 1.6 2003/04/03 13:14:20 martin Exp $
 # --
 # This software comes with ABSOLUTELY NO WARRANTY. For details, see 
 # the enclosed file COPYING for license information (GPL). If you 
@@ -17,7 +17,7 @@
 use strict;
 
 use vars qw($VERSION);
-$VERSION = '$Revision: 1.9 $';
+$VERSION = '$Revision: 1.6 $';
 $VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/;
 
 # --
@@ -29,103 +29,133 @@
     my $Self = {};
     bless ($Self, $Type);
 
+    # --
     # check needed objects
-    foreach (qw(LogObject ConfigObject DBObject)) {
+    # --
+    foreach ('LogObject', 'ConfigObject', 'DBObject') {
         $Self->{$_} = $Param{$_} || die "No $_!";
     }
  
+    $Self->{LogObject}->Log(
+          Priority => 'notice',
+          Message => "Значение $Self->{ConfigObject}",
+        );
+
+    # --                                                                                                       
+    # config options                                                                                           
+    # --
+    
+    $Self->{ValidID} = $Self->{ConfigObject}->Get('CustomerAuth')->{ValidID}
+      || die "Need CustomerAuth->ValidID in Kernel/Config.pm!";
+    $Self->{UserField} = $Self->{ConfigObject}->Get('CustomerAuth')->{CustomerAuthUserField}
+      || die "Need CustomerAuth->CustomerAuthUserField in Kernel/Config.pm!";
+    $Self->{PwField} = $Self->{ConfigObject}->Get('CustomerAuth')->{CustomerAuthPwField}
+      || die "Need CustomerAuth->CustomerAuthPwField in Kernel/Config.pm!";
+    $Self->{AuthTable} = $Self->{ConfigObject}->Get('CustomerAuth')->{Params}->{Table}
+      || die "Need CustomerAuth->Params->Table in Kernel/Config.pm!";
+    $Self->{CustIDField} = $Self->{ConfigObject}->Get('CustomerAuth')->{CustomerID}
+      || die "Need CustomerAuth->CustomerID in Kernel/Config.pm!";
+    # --                                                                                                       
+    # create new db connect if DSN is given                                                                    
+    # --                                                                                                       
+    if ($Self->{ConfigObject}->Get('CustomerAuth')->{Params}->{DSN}) {                                         
+        $Self->{DBObject} = Kernel::System::DB->new(                                                           
+            LogObject => $Param{LogObject},                                                                    
+            ConfigObject => $Param{ConfigObject},                                                              
+            DatabaseDSN => $Self->{ConfigObject}->Get('CustomerAuth')->{Params}->{DSN},                        
+            DatabaseUser => $Self->{ConfigObject}->Get('CustomerAuth')->{Params}->{User},                      
+            DatabasePw => $Self->{ConfigObject}->Get('CustomerAuth')->{Params}->{Password},                    
+        ) || die $DBI::errstr;                                                                                 
+    }
+
+    # --
     # Debug 0=off 1=on
+    # --
     $Self->{Debug} = 0;
 
+    # --                                                                                                       
+    # create check item object                                                                                 
+    # --                                                                                                       
+    $Self->{CheckItemObject} = Kernel::System::CheckItem->new(%Param);
     return $Self;
 }
 # --
-sub GetOption {
-    my $Self = shift;
-    my %Param = @_;
-    # check needed stuff
-    if (!$Param{What}) {
-        $Self->{LogObject}->Log(Priority => 'error', Message => "Need What!");
-        return;
-    }
-    # module options
-    my %Option = (
-        PreAuth => 0,
-    );
-    # return option
-    return $Option{$Param{What}};
-}   
-# --
 sub Auth {
     my $Self = shift;
     my %Param = @_;
+    # --
     # check needed stuff
+    # --
     if (!$Param{User}) {
       $Self->{LogObject}->Log(Priority => 'error', Message => "Need User!");
       return;
     }
-    # db quote
-    foreach (keys %Param) {
-        $Param{$_} = $Self->{DBObject}->Quote($Param{$_});
-    }
+    # --
     # get params
+    # --
     my $User = $Param{User} || ''; 
     my $Pw = $Param{Pw} || '';
     my $RemoteAddr = $ENV{REMOTE_ADDR} || 'Got no REMOTE_ADDR env!';
     my $UserID = '';
     my $GetPw = '';
 
+    # --
     # sql query
-    my $SQL = "SELECT pw, login ".
-      " FROM ".
-      " customer_user ".
-      " WHERE ". 
-      " valid_id in ( ${\(join ', ', $Self->{DBObject}->GetValidIDs())} ) ".
-      " AND ".
-      " login = '$User'";
-    $Self->{DBObject}->Prepare(SQL => $SQL);
-    while (my @Row = $Self->{DBObject}->FetchrowArray()) { 
-        $GetPw = $Row[0];
-        $UserID = $Row[1];
-    }
-
-    # crypt given pw 
-    my $CryptedPw = '';
-    my $Salt = $GetPw; 
-    # strip Salt only for (Extended) DES, not for any of Modular crypt's
-    if ($Salt !~ /^\$\d\$/) {
-        $Salt =~ s/^(..).*/$1/;
-    }
-    # and do this check only in such case (unfortunately there is a mod_perl2 
-    # bug on RH8 - check if crypt() is working correctly) :-/
-    if (($Salt =~ /^\$\d\$/) || (crypt('root', 'root@localhost') eq 'roK20XGbWEsSM')) {
-        $CryptedPw = crypt($Pw, $Salt);
-    }
-    else {
-        $Self->{LogObject}->Log(
-            Priority => 'notice',
-            Message => "The crypt() of your mod_perl(2) is not working correctly! Update mod_perl!",
-        );
-        my $TempSalt = quotemeta($Salt);
-        my $TempPw = quotemeta($Pw);
-        my $CMD = "perl -e \"print crypt('$TempPw', '$TempSalt');\"";
-        open (IO, " $CMD | ") || print STDERR "Can't open $CMD: $!";
-        while (<IO>) {
-            $CryptedPw .= $_;
-        }
-        close (IO);
-        chomp $CryptedPw;
-    }
+    # --
+    my $SQL = "SELECT ".$Self->{PwField}.", ".$Self->{CustIDField}.
+	    " FROM ".$Self->{AuthTable}." WHERE ".
+	    $Self->{UserField}." = '$User';";
 
+    $Self->{DBObject}->Prepare(SQL => $SQL);
+    while (my @RowTmp = $Self->{DBObject}->FetchrowArray()) { 
+        $GetPw = $RowTmp[0];
+        $UserID = $RowTmp[1];
+    }
+
+    # --
+    # crypt given pw (unfortunately there is a mod_perl2 bug on RH8 - check if 
+    # crypt() is working correctly) :-/
+    # --
+    
+# This changes has made ecause my database store passwords in unencrypted state   
+    
+#    my $CryptedPw = '';
+#    my $Salt = $GetPw;
+#    $Salt =~ s/^(..).*/$1/;
+#    if (crypt('root', 'root@localhost') eq 'roK20XGbWEsSM') {
+#        $CryptedPw = crypt($Pw, $Salt);
+#    }
+#    else {
+#        $Self->{LogObject}->Log(
+#            Priority => 'notice',
+#            Message => "The crypt() of your mod_perl(2) is not working correctly! Update mod_perl!",
+#        );
+#        my $TempSalt = $Salt;
+#        $TempSalt =~ s/'/\\'/g;
+#        my $TempPw = $Pw;
+#        $TempPw =~ s/'/\\'/g;
+#        my $CMD = "perl -e \"print crypt('$TempPw', '$TempSalt');\"";
+#        open (IO, " $CMD | ") || print STDERR "Can't open $CMD: $!";
+#        while (<IO>) {
+#            $CryptedPw .= $_;
+#        }
+#        close (IO);
+#        chomp $CryptedPw;
+#    }
+    my $CryptedPw = $Pw;
+    # --
     # just in case!
+    # --
     if ($Self->{Debug} > 0) {
         $Self->{LogObject}->Log(
           Priority => 'notice',
-          Message => "CustomerUser: '$User' tried to login with Pw: '$Pw' ($UserID/$CryptedPw/$GetPw/$Salt/$RemoteAddr)",
+          Message => "CustomerUser: '$User' tried to login with Pw: '$Pw' ($UserID/$CryptedPw/$GetPw/$RemoteAddr)"
         );
     }
 
+    # --
     # just a note 
+    # --
     if (!$Pw) {
         $Self->{LogObject}->Log(
           Priority => 'notice',
@@ -133,15 +163,19 @@
         );
         return;
     }
+    # --
     # login note
+    # --
     elsif ((($GetPw)&&($User)&&($UserID)) && $CryptedPw eq $GetPw) {
         $Self->{LogObject}->Log(
           Priority => 'notice',
           Message => "CustomerUser: $User logged in (REMOTE_ADDR: $RemoteAddr).",
         );
-        return $User;
+        return 1;
     }
+    # --
     # just a note
+    # --
     elsif (($UserID) && ($GetPw)) {
         $Self->{LogObject}->Log(
           Priority => 'notice',
@@ -149,7 +183,9 @@
         ); 
         return;
     }
+    # --
     # just a note
+    # --
     else {
         $Self->{LogObject}->Log(
           Priority => 'notice',
@@ -161,3 +197,4 @@
 # --
 
 1;
+
