[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Pgp with MH & MIME




--------Enter additional text


-----BEGIN PGP SIGNED MESSAGE-----



> Some time back there was some debate here about whether MIME
> ("Multipurpose Internet Mail Extensions") features could be exploited
> to make encryption easier to use.  I didn't pay much attention at the
> time, because I didn't know anything about MIME.

Here is my latest copy of "pgpedit", I have modified it to insert a
"Content-Type" line to that MIME mailer will automaticly 


#!/usr/local/bin/perl
#!/usr/local/bin/tperl4.036

# pgpedit V1.5
# Mon Mar  1 00:40:49 PST 1993
# Peter Shipley

# to install (mh instructions)
# place this script in your path (eg ~/bin)
# create few links:
#
#       ln -s /usr/local/mh/bin/repl ~/bin/pgprepl
#       ln -s /usr/local/mh/bin/comp ~/bin/pgpcomp
#       ln -s /usr/local/mh/bin/forw ~/bin/pgpforw
#
# Add the following to your .mh_profile:
#
#       pgprepl: -editor pgpedit
#       pgpcomp: -editor pgpedit
#	pgpforw: -editor pgpedit
#
# To use just use the commands pgprepl or pgpcomp instead of repl or comp
#
# If you are Useing MH 6.8 with MIME you may also want to add the following:
#
#	mhn-show-text/x-pgp: %ppgp -m '%F'
#	mhn-store-text/x-pgp: %m%P.pgp
#
# This `instruct' MH how to interpet the MIME header "Content-Type: text/x-pgp"
# and have MH automaticly invoke pgp for devcryption or signature verification
#




require "sys/wait.ph";

#custom stuff here

#default editor
$defaulteditor="/usr/ucb/vi";


$pgpcommand="/usr/local/bin/pgp -sta +clearsig=on";

$debug=0;

#test the arg count
    die "bad argument count" if ($#ARGV > 0);

#set the umask to something really paranoid
    umask(07177);

#set the path if there is not one
    $ENV{'PATH'} = '/bin:/usr/ucb:/usr/bin' unless $ENV{'PATH'};

# get the editor
    if ($ENV{'VISUAL'}) {
	$editor=$ENV{'VISUAL'} ;
    } elsif ($ENV{'EDITOR'}) {
	$editor=$ENV{'EDITOR'} ;
    } else {
	$editor = $defaulteditor;
    }

    $editor = $defaulteditor if $editor =~ /pgpedit/ ;

# prepare the file.
    &prepfile($ARGV[0]);

# run the editor
    &run($editor, $ARGV[0]);

# have pgp sign the file
    &SignMailFile($ARGV[0]);

# the end
    exit 0;



sub SignMailFile {

    local($INPUT) = @_;

    local($BODY) = "/tmp/pgp$$.body";
    local($PGPBODY) = "$BODY".".asc";
    local(@header);


    print "$INPUT \n" if ($debug);

    open(INPUT, "+>>$INPUT" ) || die "failed to open $INPUT: $!\n";
    seek(INPUT,0, 0);

    open(BODY, "+>$BODY") || die "failed to open $BODY: $!\n";
    select(BODY); $| = 1; select(STDOUT);

    while (<INPUT>) {
	last if (/^--------/);
	push (@header, $_);
    }


    if ($debug) {
	print STDERR "\n--header--\n";
	print STDERR "@header \n" ;
	print STDERR "\n--BODY--\n";
	print STDERR "$BODY \n" ;
    }

    while (<INPUT>) {
	print BODY $_;
    }

    print STDERR  "$pgpcommand $BODY\n" if ($debug);

    `$pgpcommand $BODY`;

#    &run($pgpcommand, $BODY);

    if (! $? ) {

	truncate(BODY, 0); unlink($BODY); close(BODY);
	truncate(INPUT,0);

	open(PGPBODY) || die "failed to open $PGPBODY: $!\n";

	seek(INPUT, 0, 0);

	print INPUT @header;
	print INPUT "Content-Type: text/x-pgp; charset=\"us-ascii\"\n";
	print INPUT "--------\n\n";

	print INPUT <PGPBODY>;

    } else {
    
	truncate(BODY, 0); unlink($BODY); close(BODY);

	if ( -f $PGPBODY ) {
	    truncate(PGPBODY, 0); unlink($PGPBODY); close(PGPBODY);
	}
    }

    return;
}



sub prepfile {

    local($FILE) = @_;
    local(@data);


    open(FILE, "+>>$FILE" ) || die "failed to open $FILE: $!\n";
    seek(FILE, 0, 0);

    while(<FILE>) {
	next if ( ?text/x-pgp? );
	push (@data, $_);
	last if ( /^--------/ );
    }

#	push (@data, "\n\n\n");

    while(<FILE>) { last if (/^-----BEGIN PGP SIGNED MESSAGE-----/); }

    if( !eof(FILE) ) {

	while(<FILE>) { 
	    last if (/^-----BEGIN PGP SIGNATURE-----/);
	    push (@data , $_); 
	}

	seek(FILE, 0, 0);
	truncate(FILE, 0);

	print FILE @data;

    }
    close(FILE);
	
    return;
}


sub run {
    local($editor, $file) = @_;
    local($pid);

FORK: {
        if ($pid = fork) {
		wait;
        } elsif (defined $pid) { # $pid is zero here if defined
                exec "$editor $file";
        } elsif ($! =~ /No more process/) {     
                # EAGAIN, supposedly recoverable fork error
                sleep 5;
                redo FORK;
        } else {
                # weird fork error
                die "Can't fork: $!\n";
        }
}
    return;
}


-----BEGIN PGP SIGNATURE-----
Version: 2.1

iQBFAgUBK5HPZchmn7GUWLLFAQHGfgF9HFh4CwpEGi0UxYtswMA6VCxDSA/UaKzZ
An7db1dLCOzZ3FEf7YjAi8Fi80482b+/
=j2S2
-----END PGP SIGNATURE-----