# --
# Copyright (C) 2001-2017 OTRS AG, http://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

## no critic (Modules::RequireExplicitPackage)
use strict;
use warnings;
use utf8;

use vars (qw($Self));
use File::Path qw(mkpath rmtree);

my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $MainObject   = $Kernel::OM->Get('Kernel::System::Main');

$ConfigObject->Set(
    Key   => 'CheckEmailAddresses',
    Value => 0,
);

# copy all
# discard main + config

my $DBObject = $Kernel::OM->Get('Kernel::System::DB');

my $HomeDir = $ConfigObject->Get('Home');

# create directory for certificates and private keys
my $CertPath    = $ConfigObject->Get('Home') . "/var/tmp/certs";
my $PrivatePath = $ConfigObject->Get('Home') . "/var/tmp/private";
mkpath( [$CertPath],    0, 0770 );    ## no critic
mkpath( [$PrivatePath], 0, 0770 );    ## no critic

# set SMIME paths
$ConfigObject->Set(
    Key   => 'SMIME::CertPath',
    Value => $CertPath,
);
$ConfigObject->Set(
    Key   => 'SMIME::PrivatePath',
    Value => $PrivatePath,
);

my $OpenSSLBin = $ConfigObject->Get('SMIME::Bin');

# set config
$ConfigObject->Set(
    Key   => 'SMIME',
    Value => 1,
);
$ConfigObject->Set(
    Key   => 'SMIME::FetchFromCustomer',
    Value => 1,
);

# check if openssl is located there
if ( !-e $OpenSSLBin ) {

    # maybe it's a mac with macport
    if ( -e '/opt/local/bin/openssl' ) {
        $ConfigObject->Set(
            Key   => 'SMIME::Bin',
            Value => '/opt/local/bin/openssl',
        );
    }
}

my $SMIMEObject = $Kernel::OM->Get('Kernel::System::Crypt::SMIME');

if ( !$SMIMEObject ) {
    print STDERR "NOTICE: No SMIME support!\n";

    if ( !-e $OpenSSLBin ) {
        $Self->False(
            1,
            "No such $OpenSSLBin!",
        );
    }
    elsif ( !-x $OpenSSLBin ) {
        $Self->False(
            1,
            "$OpenSSLBin not executable!",
        );
    }
    elsif ( !-e $CertPath ) {
        $Self->False(
            1,
            "No such $CertPath!",
        );
    }
    elsif ( !-d $CertPath ) {
        $Self->False(
            1,
            "No such $CertPath directory!",
        );
    }
    elsif ( !-w $CertPath ) {
        $Self->False(
            1,
            "$CertPath not writable!",
        );
    }
    elsif ( !-e $PrivatePath ) {
        $Self->False(
            1,
            "No such $PrivatePath!",
        );
    }
    elsif ( !-d $Self->{PrivatePath} ) {
        $Self->False(
            1,
            "No such $PrivatePath directory!",
        );
    }
    elsif ( !-w $PrivatePath ) {
        $Self->False(
            1,
            "$PrivatePath not writable!",
        );
    }
    return 1;
}

# get helper object
$Kernel::OM->ObjectParamAdd(
    'Kernel::System::UnitTest::Helper' => {
        RestoreDatabase => 1,
    },
);
my $Helper = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
my $Random = $Helper->GetRandomID();

# get existing certificates
my @CertList  = $SMIMEObject->CertificateList();
my $CertCount = 0;
for my $Cert ( sort @CertList ) {
    $CertCount++;
}

# first stage
my $TableName             = 'UT_' . $Random;
my @UnitTestCustomerUsers = (
    {
        FirstName       => 'Hans',
        LastName        => 'Hansen',
        Login           => 'unittest1',
        Email           => 'unittest@example.org',
        Status          => 1,
        CertificateType => 'PEM',
        Certifiacate    => '-----BEGIN CERTIFICATE-----
MIIEXjCCA0agAwIBAgIJAPIBQyBe/HbpMA0GCSqGSIb3DQEBBQUAMHwxCzAJBgNV
BAYTAkRFMQ8wDQYDVQQIEwZCYXllcm4xEjAQBgNVBAcTCVN0cmF1YmluZzEQMA4G
A1UEChMHT1RSUyBBRzERMA8GA1UEAxMIdW5pdHRlc3QxIzAhBgkqhkiG9w0BCQEW
FHVuaXR0ZXN0QGV4YW1wbGUub3JnMB4XDTEyMDUwODEzMTEzMloXDTI2MDExNTEz
MTEzMlowfDELMAkGA1UEBhMCREUxDzANBgNVBAgTBkJheWVybjESMBAGA1UEBxMJ
U3RyYXViaW5nMRAwDgYDVQQKEwdPVFJTIEFHMREwDwYDVQQDE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aP6zWPsKjftcev2Q5ik1L7N9eCynBF3a2U0TPVkfFyzuO58k96vUhKltOb
nj2wbQO4KjM12YLUuvahk5se
-----END CERTIFICATE-----
',
    },
    {
        FirstName       => 'Klaus',
        LastName        => 'Klausen',
        Login           => 'unittest2',
        Email           => 'smimeuser1@test.com',
        Status          => 1,
        CertificateType => 'P7B',
        Certifiacate    => '-----BEGIN CERTIFICATE-----
MIIFjTCCA3UCCQDt3sB/CPz9rjANBgkqhkiG9w0BAQUFADB7MQswCQYDVQQGEwJN
WDEQMA4GA1UECBMHSmFsaXNjbzEQMA4GA1UEChMHT1RSUyBBRzERMA8GA1UECxMI
T1RSUyBMYWIxETAPBgNVBAMTCE9UUlMgTGFiMSIwIAYJKoZIhvcNAQkBFhNvdHJz
bGFiQGV4YW1wbGUuY29tMB4XDTEyMDUxNTAyNTIwNloXDTIyMDUxMzAyNTIwNlow
gZUxCzAJBgNVBAYTAk1YMRAwDgYDVQQIEwdKYWxpc2NvMRQwEgYDVQQHEwtHdWFk
YWxhamFyYTEQMA4GA1UEChMHT1RSUyBBRzERMA8GA1UECxMIT1RSUyBMYWIxFTAT
BgNVBAMTDFNNSU1FIFVzZXIgMTEiMCAGCSqGSIb3DQEJARYTc21pbWV1c2VyMUB0
ZXN0LmNvbTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAJ/cr+sMwWW3
SWLazwZ9O/dScatebBsQ3zEof/f6rO7zSppCPg/iA69VRZ9/wDJLe815bA9UBFXg
M+u/gI0Mnm8JPpPxXc+qqiTyWiNF25Tmxh3BWrFEcrx/3IRQ41G+3mbXWe5NcuWZ
unFbA8OGXW33BqQoV6SZjCxow/uz+YIkNXyNcHYRI87OrvCDhoc7qn7J6RFcDoyb
Sc8Tc5RasFiNvzE2vjodqKmQt1tpjucv15cBtSDTOwANtYYbcFgcUBqUZbeG2MbJ
e8791yXw5ZhoOOahroeszzlwk9r/d8x20iMeKm8O8nGq8+EDDPAZPhyzRfFgQexf
lu34P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RBY36dJ
-----END CERTIFICATE-----
',
    }
);

# Create Table
{
    my $XMLLoginTable = '<Table Name="' . $TableName . '">
        <Column Name="id" Required="true" PrimaryKey="true" AutoIncrement="true" Type="INTEGER"/>
        <Column Name="login" Required="true" Size="200" Type="VARCHAR"/>
        <Column Name="email" Required="true" Size="150" Type="VARCHAR"/>
        <Column Name="customer_id" Required="true" Size="150" Type="VARCHAR"/>
        <Column Name="pw" Required="false" Size="64" Type="VARCHAR"/>
        <Column Name="title" Required="false" Size="50" Type="VARCHAR"/>
        <Column Name="first_name" Required="true" Size="100" Type="VARCHAR"/>
        <Column Name="last_name" Required="true" Size="100" Type="VARCHAR"/>
        <Column Name="userSMIMECertificate" Required="false" Type="LONGBLOB"/>
        <Column Name="valid_id" Required="true" Type="SMALLINT"/>
        <Column Name="create_time" Required="true" Type="DATE"/>
        <Column Name="create_by" Required="true" Type="INTEGER"/>
        <Column Name="change_time" Required="true" Type="DATE"/>
        <Column Name="change_by" Required="true" Type="INTEGER"/>
    </Table>';
    my @XMLARRAY = $Kernel::OM->Get('Kernel::System::XML')->XMLParse( String => $XMLLoginTable );
    my @SQL = $DBObject->SQLProcessor( Database => \@XMLARRAY );
    my @SQLPost = $DBObject->SQLProcessorPost( Database => \@XMLARRAY );

    for my $SQL ( @SQL, @SQLPost ) {
        $Self->True(
            $DBObject->Do( SQL => $SQL ),
            "Login-table $TableName created",
        );
    }
}

# Fill Table
my $SQL = "INSERT INTO $TableName
                (login, email, customer_id, pw, title, first_name, last_name, userSMIMECertificate, valid_id, create_time,
                create_by, change_time, change_by)
            VALUES
                (?,?,?,?,?,?,?,?,1,current_timestamp,1,current_timestamp,1)";
my $Pwd = $MainObject->GenerateRandomString(
    Length => 8,
);
foreach my $CustomerUser (@UnitTestCustomerUsers) {
    my $Return = $DBObject->Do(
        SQL  => $SQL,
        Bind => [
            \$CustomerUser->{Login}, \$CustomerUser->{Email}, \'unittest_customer_id',
            \$Pwd,
            \'Mr', \$CustomerUser->{FirstName}, \$CustomerUser->{LastName}, \$CustomerUser->{Certifiacate}
        ],
    );
    $DBObject->Prepare(
        SQL  => "SELECT id, email from $TableName where login = ?",
        Bind => [ \$CustomerUser->{Login} ],
    );
    while ( my @Row = $DBObject->FetchrowArray() ) {
        $Self->Is(
            $Row[1],
            $CustomerUser->{Email},
            "CustomerUser $Row[0] with $CustomerUser->{CertificateType} added into CustomerUserTable ",
        );
    }
}

# Add Table as CustomerUser Table in Config
my $NewConfig;
my $FileLocation;

$ConfigObject->Set(
    Key   => "Customer::AuthModule10",
    Value => 'Kernel::System::CustomerAuth::DB',
);
$ConfigObject->Set(
    Key   => "Customer::AuthModule::DB::Table10",
    Value => $TableName,
);
$ConfigObject->Set(
    Key   => "Customer::AuthModule::DB::CustomerKey10",
    Value => 'login',
);
$ConfigObject->Set(
    Key   => "Customer::AuthModule::DB::CustomerPassword10",
    Value => 'pw',
);
$ConfigObject->Set(
    Key   => "Customer::AuthModule::DB::CryptType10",
    Value => 'plain',
);
my %CustomerUserConfig = (
    Name   => 'Database Backend',
    Module => 'Kernel::System::CustomerUser::DB',
    Params => {
        Table => $TableName,
    },
    ReadOnly => 1,

    # customer unique id
    CustomerKey => 'login',

    # customer #
    CustomerID                         => 'customer_id',
    CustomerUserListFields             => [ 'first_name', 'last_name', 'email' ],
    CustomerUserSearchFields           => [ 'login', 'first_name', 'last_name', 'customer_id' ],
    CustomerUserSearchPrefix           => '*',
    CustomerUserSearchSuffix           => '*',
    CustomerUserSearchListLimit        => 250,
    CustomerUserPostMasterSearchFields => ['email'],
    CustomerUserNameFields     => [ 'title', 'first_name', 'last_name' ],
    CustomerUserEmailUniqCheck => 1,

    # show now own tickets in customer panel, CompanyTickets
    CustomerUserExcludePrimaryCustomerID => 0,

    # admin can't change customer preferences
    AdminSetPreferences => 0,

    # cache time to live in sec. - cache any ldap queries
    CacheTTL => 120,
    Map      => [

        # note: Login, Email and CustomerID are mandatory!
        # if you need additional attributes from AD, just map them here.
        # var, frontend, storage, shown (1=always,2=lite), required, storage-type, http-link, readonly
        [ 'UserSalutation',       'Title or salutation', 'title',                1, 0, 'var', '', 0 ],
        [ 'UserFirstname',        'Firstname',           'first_name',           1, 1, 'var', '', 0 ],
        [ 'UserLastname',         'Lastname',            'last_name',            1, 1, 'var', '', 0 ],
        [ 'UserLogin',            'Username',            'login',                1, 1, 'var', '', 0 ],
        [ 'UserPassword',         'Password',            'pw',                   0, 0, 'var', '', 0 ],
        [ 'UserEmail',            'Email',               'email',                1, 1, 'var', '', 0 ],
        [ 'UserCustomerID',       'CustomerID',          'customer_id',          0, 1, 'var', '', 0 ],
        [ 'UserSMIMECertificate', 'SMIMECertificate',    'userSMIMECertificate', 0, 1, 'var', '', 0 ],
    ],
);
my $Return = $ConfigObject->Set(
    Key   => "CustomerUser10",
    Value => \%CustomerUserConfig,
);

my $AuthObject = $Kernel::OM->Get('Kernel::System::CustomerAuth');

# login check
$DBObject->Prepare(
    SQL => "SELECT id, login, pw, email from $TableName",
);
my %Customers;
while ( my @Row = $DBObject->FetchrowArray() ) {
    $Customers{ $Row[1] } = $Row[2];
}

for my $Customer ( sort keys %Customers ) {
    my $AuthResult = $AuthObject->Auth(
        User => $Customer,
        Pw   => $Customers{$Customer},
    );

    $Self->Is(
        $AuthResult,
        $Customer,
        "CustomerUser $Customer login OK",
    );
}

# check against existing certificates
@CertList = $SMIMEObject->CertificateList();
my $OldOKCount = 0;
for my $Cert ( sort @CertList ) {
    $OldOKCount++;
}
$Self->Is(
    $OldOKCount,
    $CertCount,
    "NewCertificates not yet imported",
);

# get CustomerUser-List
my $CustomerUserObject = $Kernel::OM->Get('Kernel::System::CustomerUser');
my %List               = $CustomerUserObject->CustomerSearch(
    CustomerID => 'unittest_customer_id',
    Valid      => 1,
);

CUSTOMERUSER:
for my $CustomerUser ( sort keys %List ) {
    my %User = $CustomerUserObject->CustomerUserDataGet(
        User => $CustomerUser,
    );
    next CUSTOMERUSER if !$User{UserSMIMECertificate};

    # 1st try with CertificateSearch
    # add
    my @CertificateFilename = $SMIMEObject->CertificateSearch(
        Search => $User{UserEmail},
    );

    $Self->True(
        $CertificateFilename[0]{Filename},
        "Certificate for $User{UserEmail} was imported",
    );

    # check
    my $Certificate = $SMIMEObject->CertificateGet(
        Filename => $CertificateFilename[0]{Filename},
    );

    # remove
    my %Remove = $SMIMEObject->CertificateRemove(
        Filename => $CertificateFilename[0]{Filename},
    );
    $Self->False(
        $Remove{Success},
        "$Remove{Message}",
    );

    #2nd try - fetching from customer
    my @Files = $SMIMEObject->FetchFromCustomer(
        Search => $User{UserEmail},
    );

    my @CertificateFilename2nd = $SMIMEObject->CertificateSearch(
        Search  => $User{UserEmail},
        DontAdd => 1,
    );

    $Self->True(
        $CertificateFilename2nd[0],
        "Certificate for $User{UserEmail} found",
    );

    # remove
    my %Remove2nd = $SMIMEObject->CertificateRemove(
        Filename => $CertificateFilename2nd[0]{Filename},
    );
    $Self->False(
        $Remove2nd{Success},
        "$Remove2nd{Message}",
    );

}

# TODO - second stage
# create a real LDAP server to test against

$DBObject->Do( SQL => 'Drop table ' . $TableName );

# delete needed test directories
for my $Directory ( $CertPath, $PrivatePath ) {
    my $Success = rmtree( [$Directory] );
    $Self->True(
        $Success,
        "Directory deleted - '$Directory'",
    );
}

# cleanup is done by RestoreDatabase.

1;
