0
  L   ˆl–ß=¿J•5*�nà¸'TÅ£�þBøŠ³ec‘Â²ìdóÁƒq¦œÆa–Ãa¾”J_)â‚Æ[¸ËjbÑ¿Ä .    #!/usr/bin/perl
#
#    gpgmailtunl - Program that sends encrypted email.
#
#    This file is part of GnuPG.pm.
#
#    Author: Francis J. Lacoste <francis.lacoste@iNsu.COM>
#
#    Copyright (C) 1999, 2000 iNsu Innovations Inc.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    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
#

use strict;

use GnuPG;
use Getopt::Long;

sub usage() {
    die <<EOF;
usage: gpgmailtunl [ options ] --encrypt        or
       gpgmailtunl [ options ] --decrypt
EOF
  exit(64);
}

sub bounce(@) {
    print STDERR "gpgmailtunl: ", @_, "\n";
    exit( 64 );
};

sub read_secret($) {
    open SECRET, shift
      or bounce( "error opening secret file for reading: $!" );
    my $secret = <SECRET>;
    close SECRET;
    chomp $secret;

    return $secret;
}

sub encrypt($\%) {
    my ( $gpg, $opts ) = @_;

    my $from = $opts->{from};
    $opts->{subject}	||= "Encrypted mail";
    $opts->{recipient}	||= $opts->{to};
    my $cmd = "/usr/sbin/sendmail -oi -t";
    $cmd .= " -F\"$from\"" if $from;
    open( SENDMAIL, "|". $cmd )
      or bounce( "error opening pipe to sendmail: $!" );
    select SENDMAIL; $| = 1;
    print SENDMAIL "From: $from\n" if $from;
    print SENDMAIL <<EOF;
To:	    $opts->{to}
Subject:    $opts->{subject}


EOF
    eval {
	$gpg->encrypt( output => \*SENDMAIL, armor => 1,
		       sign		=> $opts->{sign}, 
		       passphrase	=> $opts->{passphrase},
		       recipient	=> $opts->{recipient},
		       "local-user"	=> $opts->{"local-user"},
		     );
    };
    bounce ( $@ ) if $@;
    close SENDMAIL
      or bounce( "error while waiting for sendmail: $!" );
    exit 0;
};

sub decrypt($\%) {
    my ( $gpg, $opts ) = @_;

    open( SENDMAIL, "|/usr/sbin/sendmail -oi -t" )
      or bounce( "error opening pipe to sendmail: $!" );
    select SENDMAIL; $| = 1;
    eval {
	$gpg->decrypt( output => \*SENDMAIL,
		       passphrase => $opts->{passphrase},
		     );
    };
    bounce ( $@ ) if $@;
    close SENDMAIL
      or bounce( "error while waiting for sendmail: $!" );
    exit 0;
}

my %opt = ();

GetOptions( \%opt, "encrypt", "decrypt", "sign", "secret-file=s",
	    "from=s", "to=s", "subject=s", "homedir=s", "local-user=s",
	    "recipient=s",
	  )
  or usage;

bounce( "can't use encrypt and decrypt at the same time" )
  if $opt{encrypt} and $opt{decrypt};

if ( $opt{decrypt} ) {
    map { bounce( "conflicting options with encrypt: $_" )
	    if $opt{$_} } qw( to from subject sign recipient local-user );
} elsif ( $opt{encrypt} ) {
    bounce( "missing to option" ) unless  $opt{to};
} else {
    bounce( "missing encrypt or decrypt" );
}

$opt{passphrase} = read_secret( $opt{"secret-file"})
  if $opt{"secret-file"};

my $gpg = new GnuPG( homedir => $opt{homedir} );

if ($opt{encrypt}) {
    encrypt $gpg, %opt;
} else {
    decrypt $gpg, %opt;
}

__END__

=pod

=head1 NAME

gpgmailtunl - Encrypts an email message into the body of another email.

=head1 SYNOPSIS

gpgmailtunl [options] --encrypt | --decrypt

=head1 DESCRIPTION

B<gpgmailtunl> is a filter program that either encrypts an email message
using the Gnu Privacy Guard and sends it to another recipient or decrypt
an email message and forwards unencrypted to another destination.

It can be used to exchange emails across an open network between two
trusted systems.

=head1 TYPICAL USAGE

This program is intended to 