Hi,

In my excitement to send the patch previously, I'm embarrassed to say
that I hadn't tested it.  I hadn't written it a while ago against an
older version of caff, and I could have sworn it worked then.  In any
event, this new version definitely works, as I've used it to send both
signed and unsigned emails.  And I tested both with and without a
local-user specified.

I've also discovered in my testing that handling a bad passphrase is not
graceful.  The program immediately exits with a bunch of output from
gnupg.  Fortunately, the phrase "bad passphrase" does appear in the
output!

Ben

-- 
Ben Love
http://www.kylimar.com/
--- /usr/bin/caff	2010-02-11 13:26:29.000000000 -0500
+++ caff	2011-05-05 01:53:47.000000000 -0400
@@ -327,6 +327,7 @@
 my ($REVISION_NUMER) = $REVISION =~ /(\d+)/;
 my $VERSION = "0.0.0.$REVISION_NUMER";
 
+my $PASSPHRASE = '';
 
 ##
 # Display an error message on STDERR and then exit.
@@ -498,6 +499,7 @@
 	check_executable("gpg-delsig", $CONFIG{'gpg-delsig'});
 	$CONFIG{'secret-keyring'} = ($ENV{'GNUPGHOME'} || "$ENV{'HOME'}/.gnupg") . '/secring.gpg'
 		unless defined $CONFIG{'secret-keyring'};
+	$CONFIG{'sign-and-encrypt'} = 0 unless defined $CONFIG{'sign-and-encrypt'};
 	$CONFIG{'no-download'} = 0 unless defined $CONFIG{'no-download'};
 	$CONFIG{'no-sign'} = 0 unless defined $CONFIG{'no-sign'};
 	$CONFIG{'key-files'} = () unless defined $CONFIG{'key-files'};
@@ -511,6 +513,18 @@
 	};
 };
 
+sub read_passphrase($) {
+	my ($user) = @_;
+	use Term::ReadKey;
+	ReadMode('noecho');
+	print "Enter PGP passphrase".(defined $user ? " for key $user:" : ":");
+	my $passphrase = ReadLine(0);
+	chomp $passphrase;
+	ReadMode('normal');
+	print "\n";
+	return $passphrase;
+};
+
 sub make_gpg_fds() {
 	my %fds = (
 		stdin => IO::Handle->new(),
@@ -800,7 +814,20 @@
 		if (defined $CONFIG{'also-encrypt-to'}) {
 			$gpg->options->push_recipients($_) foreach @{$CONFIG{'also-encrypt-to'}};
 		}
-		my $pid = $gpg->encrypt(handles => $handles);
+		my $pid;
+		if ($CONFIG{'sign-and-encrypt'}) {
+			my @local_user = &get_local_user_keys();
+			my $user = $local_user[0];
+			if (!$PASSPHRASE) {
+				$PASSPHRASE = read_passphrase($user);
+			};
+			$gpg->passphrase( $PASSPHRASE );
+			$gpg->options->push_extra_args( "--secret-keyring=$CONFIG{'secret-keyring'}" );
+			$gpg->options->push_extra_args( "--local-user=$local_user[0]" ) if defined $user;
+			$pid = $gpg->sign_and_encrypt(handles => $handles);
+		} else {
+			$pid = $gpg->encrypt(handles => $handles);
+		}
 		my ($stdout, $stderr, $status) = readwrite_gpg($message, $inputfd, $stdoutfd, $stderrfd, $statusfd);
 		waitpid $pid, 0;
 		if ($stdout eq '') {

Attachment: signature.asc
Description: Digital signature

Reply via email to