From: cme@ellisun.sw.stratus.com (Carl Ellison)
Newsgroups: sci.crypt
Subject: csh script for interfacing encryption to Mail
Date: 17 Nov 92 21:16:08 GMT
Organization: Stratus Computer, Software Engineering
NNTP-Posting-Host: ellisun.sw.stratus.com

The following csh script edits an encrypted file, with or without making a
backup copy (<fname>~).  I use it with my own private encryption program
(imaginatively named encr and decr).  qdecr is a version of decr which
prefixes decrypted lines with ">" quotes.  gpw is a small program which
gets a typed password from the terminal (with echo off) and echos it to
stdout.

My decr knows an encrypted portion of a file from a plaintext portion
(eg., like UUDECODE does) -- so decr will decrypt a mixed file (or one
with no encryption).  encr, however, encrypts the whole file -- so this
script doesn't re-encrypt just portions of the file.

In my .mailrc, I have the lines:
	set EDITOR=mail_eedit
	set VISUAL=emacs
so that I can invoke encryption with ~e and straight editing with ~v.

Of course, you don't have to use this for encryption.  You can compress
mail or uncompress it at these points, or do any other transformation
you want to on the mail file (as long as it remains transmittable via
SMTP).

Enjoy,
	Carl

P.S.  I'm not posting my private encryption program but even if I did so,
it would probably be a disservice.  Much better secret-key systems are
freely available.

=========================== cut here =======================================
#!/bin/csh -f
# edit an encrypted file, reencrypting the output, possibly with a
# separate password

# if this is called mail_eedit, then don't make the backup copy of $ofile
# before writing it.

# This script uses md5d for verifying password spelling.  md5d is md5driver.c
# a public domain wrapper for the
#  RSA Data Security, Inc. Message Digest Algorithm # 5

set prog_name = "`echo $0 | sed -e 's,^.*/,,'`"

if ($prog_name == "mail_eedit") then
	set decr_name = "qdecr"
else
	set decr_name = "decr"
endif

# Use any directory for cryptdir which you protect well enough
#  for it to contain temporary copies of plaintext files

set cryptdir = ~/crypt

set passwords = ${cryptdir}/passwords
set tmpfile = ${cryptdir}/cr$$.tmp
set cpyfile = ${cryptdir}/cr$$.cpy

if ($#argv == 0) goto usage_message

if ($argv[1] == "-c") then
	switch ($#argv)
	case 3:
		set ofile = $argv[3]
		set ifile = $argv[2]
		breaksw
	case 2:
		set ifile = $argv[2]
		set ofile = $ifile
		breaksw
	default:
		goto usage_message
	endsw
else
	switch ($#argv)
	case 2:
		set ofile = $argv[2]
		set ifile = $argv[1]
		breaksw
	case 1:
		set ifile = $argv[1]
		set ofile = $ifile
		breaksw
	default:
		goto usage_message
	endsw
endif

# check password against the file of message digests of all passwords

set pw = "`gpw`"
if ("$pw" == "") goto found
set md = `md5d -s"$pw"`
set okm = `cat ${passwords}`
foreach ok ($okm)
    if ($md[1] == $ok) goto found
end

echo "Password not known.  Re-type it to record it; <CR> to abort."

set pw2 = "`gpw`"
if ("$pw" == "$pw2") goto addit
echo "Mismatch.  Password not added."
exit(1)

addit:
	echo "$md[1]" >> ${passwords}
found:
	if ("$pw" == "") then
		cp $ifile  ${tmpfile}
	else
		$decr_name "$pw" <$ifile > ${tmpfile}
	endif

	cp ${tmpfile} ${cpyfile}
	$EEDITOR  ${tmpfile}

	if ($#argv > 1) goto changeout
	if { cmp -s  ${tmpfile} ${cpyfile} } goto fini

changeout:

# either $#argv > 1, in which case I'm changing passwords or writing to a
# different file, or the file was changed by the editor

	set pwold = "$pw"
	if ($argv[1] == "-c") set pw = "`gpw`"
	if ("$pw" == "") goto foundout
	set md = `md5d -s"$pw"`
	set okm = `cat ${passwords}`
	foreach ok ($okm)
	if ($md[1] == $ok) goto foundout
	end

	echo "Password not known.  Re-type it to record it; <CR> to abort."

	set pw2 = "`gpw`"
	if ("$pw" == "$pw2") goto additout
	echo "Mismatch.  Password not added.  Using the input password."
	set pw = "$pwold"
	goto foundout

additout:
	echo "$md[1]" >> ${passwords}
foundout:

# we're going to write the $ofile.  make a copy first, by renaming it.
# unless this is the mail_eedit version

	if ($prog_name != "mail_eedit") mv -f ${ofile} ${ofile}~

	if ("$pw" == "") then
		cp ${tmpfile}  $ofile 
	else
		encr "$pw" < ${tmpfile} >$ofile
	endif

fini:
	rm ${cryptdir}/cr$$.*
	exit(0)

usage_message:
	echo "usage: $prog_name [-c] ifile [ofile]"
	echo "	where -c calls for changing the password"
	exit(0)
============================ end of enclosure ===============================
-- 
-- <<Disclaimer: All opinions expressed are my own, of course.>>
-- Carl Ellison						cme@sw.stratus.com
-- Stratus Computer Inc.	M3-2-BKW		TEL: (508)460-2783
-- 55 Fairbanks Boulevard ; Marlborough MA 01752-1298	FAX: (508)624-7488
Newsgroups: sci.crypt