[vhffs-dev] [1372] Added GPG support, it should work for any mail sent through the Vhffs::Functions:: send_mail function

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 1372
Author:   gradator
Date:     2009-03-12 23:14:00 +0100 (Thu, 12 Mar 2009)

Log Message:
-----------
Added GPG support, it should work for any mail sent through the Vhffs::Functions::send_mail function

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Conf.pm
    trunk/vhffs-api/src/Vhffs/Functions.pm
    trunk/vhffs-api/src/Vhffs/Object.pm
    trunk/vhffs-api/src/Vhffs/User.pm
    trunk/vhffs-backend/conf/vhffs.conf.dist.in
    trunk/vhffs-panel/alert_submit.pl
    trunk/vhffs-panel/mailinglist/prefs.pl
    trunk/vhffs-robots/src/cron_scheduler.pl
    trunk/vhffs-robots/src/mailing.pl
    trunk/vhffs-test-dependencies.in


Modified: trunk/vhffs-api/src/Vhffs/Conf.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Conf.pm	2009-03-11 20:27:06 UTC (rev 1371)
+++ trunk/vhffs-api/src/Vhffs/Conf.pm	2009-03-12 22:14:00 UTC (rev 1372)
@@ -91,6 +91,11 @@
 	return $Config{'panel'};
 }
 
+sub get_gpg
+{
+	return $Config{'gpg'};
+}
+
 sub get_database
 {
 	return $Config{'database'};

Modified: trunk/vhffs-api/src/Vhffs/Functions.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Functions.pm	2009-03-11 20:27:06 UTC (rev 1371)
+++ trunk/vhffs-api/src/Vhffs/Functions.pm	2009-03-12 22:14:00 UTC (rev 1372)
@@ -260,7 +260,9 @@
 	use MIME::Lite;
 	use MIME::Base64;
 	use Encode;
+	use Crypt::GPG;
 
+	my $vhffs = shift;
 	my $from = shift;
 	my $to = shift;
 	my $mailtag = shift;
@@ -268,23 +270,73 @@
 	my $message = shift;
 	my $precedence = shift;
 
-	$subject = $mailtag.' '.$subject if( defined $mailtag );
+	my ( $gpgbin , $gpgconf );
+	if( defined $vhffs->get_config->get_gpg && defined $vhffs->get_config->get_gpg->{'gpg_bin'} ) {
+		$gpgbin = $vhffs->get_config->get_gpg->{'gpg_bin'};
+		my ( $cleanfrom ) = ( $from =~ /<(.+)>/ );
+		$gpgconf = $vhffs->get_config->get_gpg->{ $cleanfrom || $from };
+	}
+
+	$subject = $mailtag.' '.$subject if defined $mailtag;
 	$subject = Encode::encode_utf8( $subject );
-	$subject = '=?UTF-8?B?'.encode_base64( $subject , '' ).'?=';
+	$subject = '=?UTF-8?B?'.MIME::Base64::encode_base64( $subject , '' ).'?=';
 	$message = Encode::encode_utf8( $message );
 
-	my $msg = new MIME::Lite
-		From    => $from,
-		To      => $to,
-		Subject => $subject,
-		Type    => 'text/plain; charset=utf-8',
-		Encoding => '8bit',
-		Data    => $message;
+	if( defined $gpgconf ) {
 
-	$msg->add('Precedence' => $precedence) if( defined $precedence );
+		my $msg = new MIME::Lite(
+			From        => $from,
+			To          => $to,
+			Subject     => $subject,
+			Type        => 'multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"',
+			Disposition => 'inline'
+			);
+		$msg->add('Precedence' => $precedence) if defined $precedence;
 
-	$msg->send;
+		my $data = $msg->attach(
+			Type => 'text/plain; charset=utf-8',
+			Encoding => '8bit',
+			Disposition => 'inline',
+			Data => $message
+			);
 
+		$ENV{'GNUPGHOME'} = $gpgconf->{'gnupghome'};
+		my $gpg = new Crypt::GPG;
+		$gpg->gpgbin( $gpgbin );
+		$gpg->secretkey( $gpgconf->{'secretkey'} );
+		$gpg->passphrase( $gpgconf->{'passphrase'} );
+		my $sign;
+		eval{ $sign = $gpg->sign( $data->as_string ); };
+		if( defined $sign ) {
+			my $signature = $msg->attach(
+				Type => 'application/pgp-signature; name="signature.asc"',
+				Encoding => '7bit',
+				Disposition => 'inline',
+				Data => $sign
+				);
+			$signature->attr( 'content-description' => 'Digital signature' );
+
+			$msg->send;
+		} else {
+			undef $gpgconf;
+		}
+	}
+
+	unless( defined $gpgconf ) {
+
+		my $msg = new MIME::Lite(
+			From    => $from,
+			To      => $to,
+			Subject => $subject,
+			Type    => 'text/plain; charset=utf-8',
+			Encoding => '8bit',
+			Data    => $message
+			);
+		$msg->add('Precedence' => $precedence) if defined $precedence;
+
+		$msg->send;
+	}
+
 	return 1;
 }
 
@@ -489,7 +541,7 @@
 		$content .= $line;
 	}
 
-	if( send_mail( $keys->{FROM} , $user->get_mail , $keys->{SUBJECT} , $content , $vhffs ) != 1 )
+	if( send_mail( $vhffs , $keys->{FROM} , $user->get_mail , $keys->{SUBJECT} , $content ) != 1 )
 	{
 		return( -2 );
 	}

Modified: trunk/vhffs-api/src/Vhffs/Object.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Object.pm	2009-03-11 20:27:06 UTC (rev 1371)
+++ trunk/vhffs-api/src/Vhffs/Object.pm	2009-03-12 22:14:00 UTC (rev 1372)
@@ -419,7 +419,7 @@
 		$vhffs->get_config->get_host_name
 		);
 
-	Vhffs::Functions::send_mail( $vhffs->get_config->get_moderator_mail , $user->get_mail , $vhffs->get_config->get_mailtag , $subject , $mail );
+	Vhffs::Functions::send_mail( $vhffs , $vhffs->get_config->get_moderator_mail , $user->get_mail , $vhffs->get_config->get_mailtag , $subject , $mail );
 
 	setlocale( LC_ALL , $prevlocale );
 
@@ -469,7 +469,7 @@
 		$vhffs->get_config->get_host_name
 		);
 
-	Vhffs::Functions::send_mail( $vhffs->get_config->get_moderator_mail , $user->get_mail , $vhffs->get_config->get_mailtag , $subject , $mail );
+	Vhffs::Functions::send_mail( $vhffs , $vhffs->get_config->get_moderator_mail , $user->get_mail , $vhffs->get_config->get_mailtag , $subject , $mail );
 
 	setlocale( LC_ALL , $prevlocale );
 
@@ -515,7 +515,7 @@
 		$vhffs->get_config->get_host_name
 		);
 
-	Vhffs::Functions::send_mail( $vhffs->get_config->get_moderator_mail , $user->get_mail , $vhffs->get_config->get_mailtag , $subject , $mail );
+	Vhffs::Functions::send_mail( $vhffs , $vhffs->get_config->get_moderator_mail , $user->get_mail , $vhffs->get_config->get_mailtag , $subject , $mail );
 
 	setlocale( LC_ALL , $prevlocale );
 

Modified: trunk/vhffs-api/src/Vhffs/User.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/User.pm	2009-03-11 20:27:06 UTC (rev 1371)
+++ trunk/vhffs-api/src/Vhffs/User.pm	2009-03-12 22:14:00 UTC (rev 1372)
@@ -346,7 +346,7 @@
 		$vhffs->get_config->get_host_name
 		);
 
-	Vhffs::Functions::send_mail( $vhffs->get_config->get_moderator_mail , $self->get_mail , $vhffs->get_config->get_mailtag , $subject , $mail );
+	Vhffs::Functions::send_mail( $vhffs , $vhffs->get_config->get_moderator_mail , $self->get_mail , $vhffs->get_config->get_mailtag , $subject , $mail );
 
 	setlocale( LC_ALL , $prevlocale );
 
@@ -394,7 +394,7 @@
 	$from = $vhffs->get_config->get_master_mail;
 	$from = "vhffs\@nosrc.com" if( ! defined $from );
 
-	Vhffs::Functions::send_mail( $from , $user->get_mail , $vhffs->get_config->get_mailtag , $subject , $content );
+	Vhffs::Functions::send_mail( $vhffs , $from , $user->get_mail , $vhffs->get_config->get_mailtag , $subject , $content );
 }
 
 

Modified: trunk/vhffs-backend/conf/vhffs.conf.dist.in
===================================================================
--- trunk/vhffs-backend/conf/vhffs.conf.dist.in	2009-03-11 20:27:06 UTC (rev 1371)
+++ trunk/vhffs-backend/conf/vhffs.conf.dist.in	2009-03-12 22:14:00 UTC (rev 1372)
@@ -102,6 +102,41 @@
 
 
 ######
+# GPG
+#################################################
+<gpg>
+	# Path to the gpg binary
+	gpg_bin		=	/usr/bin/gpg
+
+	# Mails accounts
+	# 
+	# This is a generic backend, if the From: match one of
+	# the next entries then the mail will be signed with gpg
+
+	# Admin mail account
+	<admin@localhost>
+		gnupghome = /etc/vhffs/gpg_adm
+		secretkey = 0x12345678
+		passphrase = secret
+	</admin@localhost>
+
+	# Moderator mail account
+	<moderator@localhost>
+		gnupghome = /etc/vhffs/gpg_modo
+		secretkey = 0x12345678
+		passphrase = secret
+	</moderator@localhost>
+
+	# Another dummy example
+	<any-mail-account-specified-@-this-configuration>
+		gnupghome = /etc/vhffs/gpg_otheraccount
+		secretkey = 0x12345678
+		passphrase = secret
+	</any-mail-account-specified-@-this-configuration>
+</gpg>
+
+
+######
 # DATABASE
 #################################################
 <database>

Modified: trunk/vhffs-panel/alert_submit.pl
===================================================================
--- trunk/vhffs-panel/alert_submit.pl	2009-03-11 20:27:06 UTC (rev 1371)
+++ trunk/vhffs-panel/alert_submit.pl	2009-03-12 22:14:00 UTC (rev 1372)
@@ -64,7 +64,7 @@
 	my $subject = Encode::decode_utf8( $cgi->param('SUBJECT') );
 	my $message = gettext('Message sent by the following account').': '.$user->get_username."\n\n".Encode::decode_utf8( $cgi->param('MESSAGE') );
 
-	Vhffs::Functions::send_mail( $from , $to , undef , $subject , $message );
+	Vhffs::Functions::send_mail( $vhffs , $from , $to , undef , $subject , $message );
 	$template->param( MESSAGE => gettext('Message sent successfully') );
 }
 else

Modified: trunk/vhffs-panel/mailinglist/prefs.pl
===================================================================
--- trunk/vhffs-panel/mailinglist/prefs.pl	2009-03-11 20:27:06 UTC (rev 1371)
+++ trunk/vhffs-panel/mailinglist/prefs.pl	2009-03-12 22:14:00 UTC (rev 1372)
@@ -296,6 +296,7 @@
         } else {
        	    $panel->add_info( sprintf( gettext( '%s has been added' ), $member ) );
             Vhffs::Functions::send_mail(
+		$vhffs,
        	        $listengineconfig->{'listmaster'},
                 $member,
        	        undef,

Modified: trunk/vhffs-robots/src/cron_scheduler.pl
===================================================================
--- trunk/vhffs-robots/src/cron_scheduler.pl	2009-03-11 20:27:06 UTC (rev 1371)
+++ trunk/vhffs-robots/src/cron_scheduler.pl	2009-03-12 22:14:00 UTC (rev 1372)
@@ -329,5 +329,5 @@
 	my $body = shift;
 	return undef unless( defined $cron->get_reportmail  &&  $cron->get_reportmail ne '' );
 	my $subject = 'VHFFS Cron '.$cron->get_cronpath;
-	Vhffs::Functions::send_mail( $mailcronfrom , $cron->get_reportmail , $vhffs->get_config->get_mailtag , $subject , $body );
+	Vhffs::Functions::send_mail( $vhffs , $mailcronfrom , $cron->get_reportmail , $vhffs->get_config->get_mailtag , $subject , $body );
 }

Modified: trunk/vhffs-robots/src/mailing.pl
===================================================================
--- trunk/vhffs-robots/src/mailing.pl	2009-03-11 20:27:06 UTC (rev 1371)
+++ trunk/vhffs-robots/src/mailing.pl	2009-03-12 22:14:00 UTC (rev 1372)
@@ -63,7 +63,7 @@
 	foreach my $user ( @{$users} )
 	{
 		next unless defined $user;
-		Vhffs::Functions::send_mail( $from , $user->get_mail , $vhffs->get_config->get_mailtag , $mailings->{$idm}{subject} , $mailings->{$idm}{message} , 'bulk' );
+		Vhffs::Functions::send_mail( $vhffs , $from , $user->get_mail , $vhffs->get_config->get_mailtag , $mailings->{$idm}{subject} , $mailings->{$idm}{message} , 'bulk' );
 	}
 
 	my $query = "UPDATE vhffs_mailings SET state='".Vhffs::Constants::ACTIVATED."' WHERE id_mailing='".$idm."'";

Modified: trunk/vhffs-test-dependencies.in
===================================================================
--- trunk/vhffs-test-dependencies.in	2009-03-11 20:27:06 UTC (rev 1371)
+++ trunk/vhffs-test-dependencies.in	2009-03-12 22:14:00 UTC (rev 1372)
@@ -17,14 +17,17 @@
 	('Locale::gettext', ''),
 	('Digest::MD5', ''),
 	('MIME::Lite', ''),
+	('MIME::Base64', ''),
 	('Crypt::PasswdMD5', ''),
 	('Email::Valid', ''),
 	('File::Basename', ''),
 	('LockFile::Simple', ''),
 	('Mail::Internet', ''),
 	('HTML::Template', ''),
+	('Template', ''),
 	('CGI', ''),
-	('CGI::Session', '')];
+	('CGI::Session', ''),
+	('Crypt::GPG', '')];
 
 $dep{'irc'} = [
 	('Data::Dumper',''),


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/