#################### main pod documentation begin ################### =head1 NAME Zymonic::Decryptor - Zymonic Decryptor base module. =head1 SYNOPSIS TODO =head1 DESCRIPTION TODO =head1 USAGE TODO =head1 BUGS None we're aware of... =head1 SUPPORT As in the license, Zymonic is provided without warranty or support unless purchased separately, however... If you email zymonic-support@zednax.com your issue will be noted and may receive a response. For security issues, please contact zymonic-security@zednax.com and someone will respond within 8 working hours. =head1 AUTHOR Alex Masidlover et al. CPAN ID: MODAUTHOR Zednax Limited alex.masidlover@zednax.com http://www.zednax.com =head1 COPYRIGHT This program is free software licensed under the... Zymonic Public License 1.0 The full text of the license can be found in the LICENSE file included with this module. Other licenses may be acceptable if including parts of Zymonic in larger projects, please contact Zednax for details. =head1 SEE ALSO perl(1). =cut #################### main pod documentation end ################### package Zymonic::Decryptor; use strict; use warnings; BEGIN { use Exporter (); use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = '0.01'; @ISA = qw(Exporter); #Give a hoot don't pollute, do not export more than needed by default @EXPORT = qw(); @EXPORT_OK = qw(); %EXPORT_TAGS = (); } use Zymonic::FieldFactory; use MIME::Base64; use Storable qw(nfreeze thaw); use Taint::Util qw(untaint); use Zymonic; use Exception::Class ( 'Zymonic::Exception::Decryptor' => { isa => 'Zymonic::Exception', fields => ['decryptor'], description => 'Decryptor related exception', }, 'Zymonic::Exception::Decryptor::MissingConfig' => { isa => 'Zymonic::Exception::Decryptor', fields => [], description => 'Decryptor requires Config object' }, 'Zymonic::Exception::Decryptor::MissingKeyFile' => { isa => 'Zymonic::Exception::Decryptor', fields => ['keytype'], description => 'KeyFile not found' }, 'Zymonic::Exception::Decryptor::ReceiveFailed' => { isa => 'Zymonic::Exception::Decryptor', fields => [], description => 'Could not read incoming data from socket.' }, ); #################### subroutine header begin #################### =head2 new Usage : not to be called directly. Purpose : This is the constructor for the Zymonic Decryptor. Returns : a Zymonic::Decryptor object Argument : nothing Throws : nothing Comment : See Also : Zymonic::Decryptor::init =cut #################### subroutine header end #################### sub new { my $invocant = shift; my $class = ref($invocant) || $invocant; my $self = {@_}; # Remaining args become attributes $self->{zz_decryptor_fields} = ' hostname, d_posix_process_id, has_key, in_use, id, fingerprint '; bless $self, $class; $self->init; return $self; } #################### subroutine header begin #################### =head2 init Usage : $zd->init Purpose : This is called by the constructor for the Zymonic Decryptor to perform initialisation tasks. Returns : nothing Argument : nothing Throws : Zymonic::Exception::Decryptor::MissingConfig Comment : See Also : Zymonic::Decryptor::new =cut #################### subroutine header end #################### sub init { my $self = shift; # Exception if no Config Zymonic::Exception::Decryptor::MissingConfig->throw() unless $self->{config}; # make sure field factory is setup $Zymonic::field_factory = Zymonic::FieldFactory->new() unless ref($Zymonic::field_factory); $self->{ed_table_name} = $self->{config}->{encrypted_data_table} || 'zz_enc_data'; untaint( $self->{ed_table_name} ); } #################### subroutine header begin #################### =head2 load_key_file Usage : $zd->load_key_file($keytype, $keydir) Purpose : This loads a key file. Returns : the key Argument : the keytype (public or private) Throws : Zymonic::Exception::Decryptor::MissingKeyFile Comment : See Also : Zymonic::Decryptor::new =cut #################### subroutine header end #################### sub load_key_file { my $self = shift; my $keytype = shift || ''; my $keydir = shift || ''; my $keyversion = shift || ''; my $keyfile = ''; my $base64 = 0; if ( -f $keydir . '/' . $keytype . 'key' . $keyversion . '.txt' ) { $keyfile = $keydir . '/' . $keytype . 'key' . $keyversion . '.txt'; } elsif ( -f $keydir . '/' . $keytype . 'key' . $keyversion . '.bin' ) { $keyfile = $keydir . '/' . $keytype . 'key' . $keyversion . '.bin'; } elsif ( -f $keydir . '/' . $keytype . 'key' . $keyversion . '.base64' ) { $keyfile = $keydir . '/' . $keytype . 'key' . $keyversion . '.base64'; $base64 = 1; } else { Zymonic::Exception::Decryptor::MissingKeyFile->throw( keytype => $keytype ); } open( FLE, '<', $keyfile ); my $ret = ''; while () { $ret .= ( $base64 ? decode_base64($_) : $_ ); } close(FLE); return $ret; } #################### subroutine header begin #################### =head2 get_table Usage : $self->get_table($zname, $extras); Purpose : This method is called to load and return a Zymonic table object with the given zname Returns : nothing Argument : table zname, extras to pass in constructor Throws : nothing Comment : this function will cache the table object for future calls See Also : Zymonic::Table =cut #################### subroutine header end #################### sub get_table { my $self = shift; my $zname = shift; my $extras = shift || {}; my $cache_key = 'zz_table_' . $zname; unless ( $self->{$cache_key} ) { $self->{$cache_key} = Zymonic::Table->new( parent => $self, zname => $zname, table => $zname, ident => $self->{ident} || '', config => $self->{config} || '', auth => $self->{auth} || '', DB => $self->{DB} || '', session => $self->{session} || '', %{$extras} ); } return $self->{$cache_key}; } 1;