/usr/share/perl5/EBox/CGI/CA/DownloadFiles.pm is in zentyal-ca 2.3.6+quantal1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (C) 2008-2012 eBox Technologies S.L.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package EBox::CGI::CA::DownloadFiles;
# CGI to download key pair and certificates from a specific user or
# to download public key and certificate from Certification Authority
use strict;
use warnings;
use base 'EBox::CGI::ClientBase';
use EBox::Gettext;
use EBox::Global;
# Method: new
#
# Constructor for DownloadFiles CGI
#
# Returns:
#
# DownloadFiles - The object recently created
sub new
{
my $class = shift;
my $self = $class->SUPER::new('title' => __('Certification Authority'),
@_);
# To download something, we need errorchain
$self->{errorchain} = 'CA/Index';
bless($self, $class);
return $self;
}
# Process the HTTP query
sub _process
{
my ($self) = @_;
$self->{ca} = EBox::Global->modInstance('ca');
# Check if the CA infrastructure has been created
my @array = ();
$self->{cn} = $self->unsafeParam('cn');
# We have to check it manually if it exists
if ( not defined($self->{cn}) or ($self->{cn} eq "") ) {
throw EBox::Exceptions::DataMissing(data => __('Common Name'));
}
# Transform %40 in @
$self->{cn} =~ s/%40/@/g;
# Transform %20 in space
$self->{cn} =~ s/%20/ /g;
my $metaDataCert = $self->{ca}->getCertificateMetadata( cn => $self->{cn});
if (not defined($metaDataCert) ) {
throw EBox::Exceptions::External(__x("Common name: {cn} does NOT exist in database"
, cn => $self->{cn}));
}
my $files = {};
# If it is the CA certificate, only possibility to download Public Key and certificate
if ($metaDataCert->{"isCACert"}) {
$files->{publicKey} = $self->{ca}->CAPublicKey();
$files->{certificate} = $self->{ca}->getCACertificateMetadata()->{path};
} else {
$files = $self->{ca}->getKeys($self->{cn});
$files->{certificate} = $self->{ca}->getCertificateMetadata(cn => $self->{cn})->{path};
$files->{p12} = $self->{ca}->getP12KeyStore($self->{cn});
}
my $zipfile;
if ( $metaDataCert->{"isCACert"} ) {
$zipfile = EBox::Config->tmp() . "CA-key-and-cert.tar.gz";
} else {
$zipfile = EBox::Config->tmp() . "keys-and-cert-" . $self->{cn} . ".tar.gz";
}
unlink($zipfile);
# We make symbolic links in order to make dir-plained tar file
my ($linkPrivate, $linkPublic, $linkCert, $linkP12);
if ( $metaDataCert->{"isCACert"} ) {
$linkPublic = "ca-public-key.pem";
$linkCert = "ca-cert.pem";
} else {
$linkPrivate = $self->{cn} . "-private-key.pem";
$linkPublic = $self->{cn} . "-public-key.pem";
$linkCert = $self->{cn} . "-cert.pem";
$linkP12 = $self->{cn} . ".p12";
}
link($files->{privateKey}, EBox::Config->tmp() . $linkPrivate)
if ($linkPrivate);
link($files->{publicKey}, EBox::Config->tmp() . $linkPublic);
link($files->{certificate}, EBox::Config->tmp() . $linkCert);
link($files->{p12}, EBox::Config->tmp() . $linkP12)
if ($linkP12);
my $tarArgs = qq{'$zipfile' };
$tarArgs .= qq{'$linkPrivate' } if ( $linkPrivate );
$tarArgs .= qq{'$linkPublic' '$linkCert'};
$tarArgs .= qq{ '$linkP12'} if ( $linkP12 );
# -h to dump what links point to
my $ret = system('tar -C ' . EBox::Config->tmp() . ' -czhf ' . $tarArgs);
unlink(EBox::Config->tmp() . $linkPrivate) if ($linkPrivate);
unlink(EBox::Config->tmp() . $linkPublic);
unlink(EBox::Config->tmp() . $linkCert);
unlink(EBox::Config->tmp() . $linkP12) if ($linkP12);
if ($ret != 0) {
throw EBox::Exceptions::External(__("Error creating file") . ": $!");
}
# Setting the file
$self->{downfile} = $zipfile;
# Remove trailing slashes, only name
$zipfile =~ s/^.+\///;
$self->{downfilename} = $zipfile;
}
# Overwrite the _print method to send the file
sub _print
{
my ($self) = @_;
if ($self->{error} or not defined($self->{downfile})) {
$self->SUPER::_print;
return;
}
open( my $keyFile, $self->{downfile} )
or throw EBox::Exceptions::Internal("Could NOT open key file.");
print($self->cgi()->header(-type => 'application/octet-stream',
-attachment => $self->{downfilename}));
while(<$keyFile>) {
print $_;
}
close($keyFile);
}
1;
|