## OpenCA - Command
## (c) 1998-2001 by Massimiliano Pala and OpenCA Group
##
##   File Name: bpViewUser
##       Brief: view user of batch processor
## Description: 
##  Parameters: ID

sub cmdBpViewUser {

my $ID = $query->param( 'ID' );

my ( $dateColTitle, $rows, @cols, @list );

my $doc = 'BP_ViewUserSheet';

## Get required parameters ( return only the value string )
my $sheet    = getRequired( $doc );
my $batchdir = getRequired( 'BP_DIR' );

if (defined $ID) {
    $ID = join ("/", split (/ */, $ID));
}
$batchdir .= "/".$ID;

opendir DIR, $batchdir;
my @list = readdir DIR;
closedir DIR;

## Get the base Page ( got in $page variable )
my $page   = $query->getFile( "$sheet" );
my $uptime = $tools->getDate();
 
configError ( i18nGettext ("Error loading file __FILE__!", "__FILE__", $sheet) ) if ( not $page);

$table = $query->startTable (COLS          => [ gettext ("Parameter"), gettext ("Value") ],
                             WIDTH         => "100%",
                             TITLE_BGCOLOR => "#DDCCFF" );

## lists
my @csrList = ();

## Process all Directories
foreach $dirs ( @list ) {

    if ($dirs =~ /^ID$/) {

        $id_table = $query->startTable (COLS          => [ gettext ("ID"), $tools->getFile ($batchdir."/ID") ],
                                     WIDTH         => "100%",
                                     TITLE_BGCOLOR => "#DDCCFF" );
        $id_table .= $query->endTable();

    } elsif ($dirs =~ /^data$/) {

        opendir DIR, $batchdir."/data";
        my @h_list = readdir DIR;
        closedir DIR;

        $data_table = $table;
        @h_list = sort @h_list;
        foreach my $file (@h_list) {

            @cols = ();

            if ($file =~ /^DN$/) {
                push @cols, gettext ("Distinguished Name");
                push @cols, $tools->getFile ($batchdir."/data/DN");
            } elsif ($file =~ /^SUBJECT_ALT_NAME$/) {
                push @cols, gettext ("Subject Alternative Name");
                push @cols, $tools->getFile ($batchdir."/data/SUBJECT_ALT_NAME");
            } elsif ($file =~ /^ROLE$/) {
                push @cols, gettext ("Role");
                push @cols, $tools->getFile ($batchdir."/data/ROLE");
            } elsif ($file =~ /^\.|\.\.$/) {
                next;
            } else {
                push @cols, gettext ("Unknown File");
                push @cols, $file;
            }
            $data_table .= $query->addTableLine( DATA =>[ @cols ]);
        }
        $data_table .= $query->endTable();

    } elsif ($dirs =~ /^acl$/) {

        opendir DIR, $batchdir."/acl";
        my @h_list = readdir DIR;
        closedir DIR;

        $acl_table = $table;
        foreach my $file (@h_list) {

            @cols = ();

            if ($file =~ /^newPIN$/) {
                push @cols, gettext ("New PIN");
                push @cols, gettext ("allowed");
            } elsif ($file =~ /^newKey$/) {
                push @cols, gettext ("New Key");
                push @cols, gettext ("allowed");
            } elsif ($file =~ /^backupKey$/) {
                push @cols, gettext ("Key Backup explicitly allowed");
                push @cols, gettext ("allowed");
            } elsif ($file =~ /^newCSR$/) {
                push @cols, gettext ("New CSR");
                push @cols, gettext ("allowed");
            } elsif ($file =~ /^approvePendingCSR$/) {
                push @cols, gettext ("Approve CSR");
                push @cols, gettext ("allowed");
            } elsif ($file =~ /^renewCSR$/) {
                push @cols, gettext ("Renew CSR");
                push @cols, gettext ("allowed");
            } elsif ($file =~ /^approveRenewedCSR$/) {
                push @cols, gettext ("Approve renewed CSR");
                push @cols, gettext ("allowed");
            } elsif ($file =~ /^\.|\.\.$/) {
                next;
            } else {
                push @cols, gettext ("Unknown Permission");
                push @cols, $file;
            }
            $acl_table .= $query->addTableLine( DATA =>[ @cols ]);
        }
        if (getRequired ("BP_KEY_BACKUP_MODE") =~ /DENY/) {
            @cols = ();
            push @cols, gettext ("Key Backup overwritten by configuration");
            push @cols, gettext ("permission denied");
            $acl_table .= $query->addTableLine( DATA =>[ @cols ]);
        } elsif (getRequired ("BP_KEY_BACKUP_MODE") =~ /FORCE/) {
            @cols = ();
            push @cols, gettext ("Key Backup overwritten by configuration");
            push @cols, gettext ("backup enforced");
            $acl_table .= $query->addTableLine( DATA =>[ @cols ]);
        } 
        $acl_table .= $query->endTable();

    } elsif ($dirs =~ /^public$/) {

        opendir DIR, $batchdir."/public";
        my @h_list = readdir DIR;
        closedir DIR;

        $public_table = $table;
        foreach my $file (@h_list) {

            @cols = ();

            if ($file =~ /^hashedPIN$/) {
                push @cols, gettext ("Hashed PIN");
                push @cols, $tools->getFile ($batchdir."/public/hashedPIN");
            } elsif ($file =~ /^csrList$/) {
                push @cols, gettext ("Issued Requests");
                $reqfile = $tools->getFile ($batchdir."/public/csrList");
                @csrList    = split /\n/, $reqfile;
                my $links = "";
				my $old = undef;
                foreach my $req (@csrList) {
					next if ($req eq $old);
					$old = $req;
                    $links .= "<br>\n"
                        if ($links);
                    $links .= "<a href=\"$self?cmd=viewCSR&key=$req&dataType=REQUEST\">$req</a>";
                }
                push @cols, $links;
                $public_table .= $query->addTableLine( DATA =>[ @cols ]);
                @cols = ();

                push @cols, gettext ("Issued Certificates");
                my $links = "";
				my $old = undef;
                foreach my $csr (@csrList) {
					next if ($csr eq $old);
					$old = $csr;
                    my @certList = $db->searchItems (DATATYPE=>'CERTIFICATE', CSR_SERIAL=>$csr);
                    foreach my $cert (@certList) {
                        $links .= "<br>\n"
                            if ($links);
                        $links .= "<a href=\"$self?cmd=viewCert&key=".$cert->getSerial()."\">".$cert->getSerial()."</a>";
                    }
                }
                push @cols, $links;
            } elsif ($file =~ /^\.|\.\.$/) {
                next;
            } else {
                push @cols, gettext ("Unknown File");
                push @cols, $file;
            }
            $public_table .= $query->addTableLine( DATA =>[ @cols ]);
        }
        $public_table .= $query->endTable();

    } elsif ($dirs =~ /^private$/) {

        opendir DIR, $batchdir."/private";
        my @h_list = readdir DIR;
        closedir DIR;

        $private_table = $table;
        foreach my $file (@h_list) {

            @cols = ();

            if ($file =~ /^purePIN$/) {
                push @cols, gettext ("PIN");
                push @cols, $tools->getFile ($batchdir."/private/purePIN");
            } elsif ($file =~ /^privateKey$/) {
                push @cols, gettext ("private key");
                push @cols, "<pre>".$tools->getFile ($batchdir."/private/privateKey")."</pre>";
            } elsif ($file =~ /^\.|\.\.$/) {
                next;
            } else {
                push @cols, gettext ("Unknown File");
                push @cols, $file;
            }
            $private_table .= $query->addTableLine( DATA =>[ @cols ]);
        }
        $private_table .= $query->endTable();

    } elsif ($dirs =~ /^keybackup$/) {

        opendir DIR, $batchdir."/keybackup";
        my @h_list = readdir DIR;
        closedir DIR;

        $keybackup_table = $table;
        foreach my $file (@h_list) {

            next if ($file !~ /msg$/i);

            @cols = ();

            my $key = $file;
            $key =~ s/.msg$//;
            my $link = "<a href=\"\" onClick=\"document.forms[0].key_digest.value='".$key.
                       "';getParams( 'bpRecoverKey', document.forms[0] ); return false\">Key</a>";
            push @cols, gettext ("Only private key");
            push @cols, $link;
            $keybackup_table .= $query->addTableLine( DATA =>[ @cols ]);
        }

        opendir DIR, $batchdir."/keybackup";
        my @h_list = readdir DIR;
        closedir DIR;

        foreach my $file (@h_list) {

            next if ($file !~ /keydigest$/i);

            @cols = ();

            my $digest = $tools->getFile ($file);
			my $csr_serial = $file;
			$csr_serial =~ s/.keydigest//;
			$csr_serial =~ s/csr_//;
            my @certList = $db->searchItems (DATATYPE=>'CERTIFICATE', CSR_SERIAL=>$csr_serial);
			foreach my $cert (@certList) {

				## keyrecovery
				my $link = "<a href=\"\" onClick=\"document.forms[0].key.value='".$cert->getSerial().
						"';document.forms[0].format.value='pkcs12';".
						"getParams( 'bpRecoverCert', document.forms[0] ); return false\">PKCS#12</a>, ".
						"<a href=\"\" onClick=\"document.forms[0].key.value='".$cert->getSerial().
						"';document.forms[0].format.value='pkcs8';".
						"getParams( 'bpRecoverCert', document.forms[0] ); return false\">PKCS#8</a>, ".
						"<a href=\"\" onClick=\"document.forms[0].key.value='".$cert->getSerial().
						"';document.forms[0].format.value='openssl';".
						"getParams( 'bpRecoverCert', document.forms[0] ); return false\">SSLeay</a>";
				push @cols, i18nGettext ("Certificate __CERT_SERIAL__", "__CERT_SERIAL__", $cert->getSerial());
				push @cols, $link;
				$keybackup_table .= $query->addTableLine( DATA =>[ @cols ]);

			}
        }
		$keybackup_table .= $query->endTable();
    }
    ## other files are ignored

}

## Substitute the Variables in the $page
$page = $query->subVar( $page, '__uptime__',           $uptime );
$page = $query->subVar( $page, '__id__',               $tools->getFile ($batchdir."/ID") );
$page = $query->subVar( $page, '__id_table__',         $id_table );
$page = $query->subVar( $page, '__data_table__',       $data_table );
$page = $query->subVar( $page, '__acl_table__',        $acl_table );
$page = $query->subVar( $page, '__public_table__',     $public_table );
$page = $query->subVar( $page, '__private_table__',    $private_table );
$page = $query->subVar( $page, '__keybackup_table__',  $keybackup_table );

print "$page";

}

1;

