#!/usr/bin/perl --
# Automatic PGP encryption/decryption (mainly for emacs).
# Copyright (C) 1993 Ian Jackson.

# This file 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, or (at your option)
# any later version.

# It 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 GNU Emacs; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# chop($ENV{'PGPPATH'}=`pwd`) unless length($ENV{'PGPPATH'});
$ENV{'PGPPATH'}=$ENV{'HOME'}."/.pgp" unless length($ENV{'PGPPATH'});
$config_pgppath= $ENV{'PGPPATH'};
$config_wrapperdir=	"$config_pgppath/.wrapper";
$config_socketname=	"pass-socket";
$config_allkeys=	"$config_pgppath/public-keys.pgp";
# $config_l_bindir=	"/usr/local/bin";
$config_l_bindir=	"/usr/bin"; # for Debian
$config_execuser=	'root';
$config_execgroup=	'root';
$config_execmode=	'755';
# $config_l_emacs=	"/usr/local/emacs"; # for Emacs 18
# $config_l_emacs=	"/usr/local/lib/emacs"; # for Emacs 19
$config_l_emacs=	"/usr/share/emacs/site-lisp"; # for Debian
$config_lispuser=	'root';
$config_lispgroup=	'root';
$config_lispmode=	'644';
# $config_i_info=	"$config_l_emacs/info/auto-pgp.info"; # for Emacs 18
# $config_i_info=	"/usr/local/info/auto-pgp.info"; # for Emacs 19
$config_i_info=		"/usr/share/info/auto-pgp.info"; # for Debian
$config_infouser=	'root';
$config_infogroup=	'root';
$config_infomode=	'644';
$config_lisp=		"auto-pgp";
# $config_i_lisp=	"$config_l_emacs/lisp/$config_lisp.el"; # for Emacs 18
# $config_i_lisp=	"$config_l_emacs/site-lisp/$config_lisp.el"; # 19
$config_i_lisp=	"$config_l_emacs/$config_lisp.el"; # for Debian
#$config_i_mandir=	"/usr/local/man/man1";
$config_i_mandir=	"/usr/share/man/man1"; # for Debian
$config_i_manext=	"1";
$config_manuser=	'root';
$config_mangroup=	'root';
$config_manmode=	'644';
$config_x_normalpgp=	"pgp";
$config_l_pgpauto=	"pgp-auto";
$config_i_pgpauto=	"$config_l_bindir/$config_l_pgpauto";
$config_x_pgpauto=	"$config_l_pgpauto"; # or perhaps $config_i_pgpauto
$config_l_decrypt=	"pgp-decrypt";
$config_i_decrypt=	"$config_l_bindir/$config_l_decrypt";
$config_x_decrypt=	"$config_l_decrypt"; # or perhaps $config_i_decrypt
$config_l_ringsearch=	"pgp-ringsearch";
$config_i_ringsearch=	"$config_l_bindir/$config_l_ringsearch";
$config_x_ringsearch=	"$config_l_ringsearch"; # or perhaps $config_i_ringsearch
$config_l_pixie=	"pgp-pixie";
$config_i_pixie=	"$config_l_bindir/$config_l_pixie";
$config_x_pixie=	"$config_l_pixie"; # or perhaps $config_i_pixie
# Passphrase server.
# Deliberately very simple so you can see what's going on.
# This one needs to be left running in a terminal window.
# An X version would be nice.

# This file is COPYRIGHT - see notice above or file COPYRIGHT
# in this directory.

#require 'sys/socket.ph';
use Socket; # updated for perl 5 in Debian Linux

$0 =~ m|[^/]+$|; $name= $&;

# Check for spurious arguments
@ARGV && die "$name: usage: $name\n";

# Change into the right directory, creating it if necessary.
if (!chdir($config_wrapperdir)) {
    warn "$name: $config_wrapperdir: $! -- now creating it\n";
    mkdir($config_wrapperdir,0700) || die "$name: creating $config_wrapperdir: $!\n";
    chdir($config_wrapperdir) || die "$name: changing to $config_wrapperdir: $!\n";
}

# Make sure it's not accessible by others
@s= stat('.');
@s || die "stat $config_wrapperdir: $!\n";
($s[2] & 0777) == 0700 || die "$name: $config_wrapperdir must be mode 700 (rwx------)";

unlink($config_socketname);
socket(S,&PF_UNIX, &SOCK_STREAM, 0) || die "creating socket: $!\n";
bind(S,pack('S',&AF_UNIX).$config_socketname) || die "bind to $config_socketname: $!\n";
listen(S,5) || die "listening: $!\n";

system('stty','-echo') && die "stty -echo gave $?\n";

print STDERR "OK, type in your passphrase: ";

$passphrase= <STDIN>;

system('stty','echo') && die "stty echo gave $?\n";

print STDERR <<'END';

Thanks. If you think you got it wrong, just kill me and try again.

Achtung! Ensure this program dies when you log out, if not sooner!
END

for(;;) {
    defined($t=accept(NS,S)) || die "accept: $!\n";
    print NS $passphrase;
    $_=`date`; chop;
    print("$_: $name supplied passphrase\n") ||
        die "Failed to write log message to stdout: $!\n";
    close(NS);
}

exit 0;
