home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / sci / crypt / 4938 < prev    next >
Encoding:
Internet Message Format  |  1992-11-17  |  4.9 KB

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!bu.edu!transfer.stratus.com!ellisun.sw.stratus.com!cme
  2. From: cme@ellisun.sw.stratus.com (Carl Ellison)
  3. Newsgroups: sci.crypt
  4. Subject: csh script for interfacing encryption to Mail
  5. Message-ID: <1ebneoINNn5q@transfer.stratus.com>
  6. Date: 17 Nov 92 21:16:08 GMT
  7. Organization: Stratus Computer, Software Engineering
  8. Lines: 171
  9. NNTP-Posting-Host: ellisun.sw.stratus.com
  10.  
  11. The following csh script edits an encrypted file, with or without making a
  12. backup copy (<fname>~).  I use it with my own private encryption program
  13. (imaginatively named encr and decr).  qdecr is a version of decr which
  14. prefixes decrypted lines with ">" quotes.  gpw is a small program which
  15. gets a typed password from the terminal (with echo off) and echos it to
  16. stdout.
  17.  
  18. My decr knows an encrypted portion of a file from a plaintext portion
  19. (eg., like UUDECODE does) -- so decr will decrypt a mixed file (or one
  20. with no encryption).  encr, however, encrypts the whole file -- so this
  21. script doesn't re-encrypt just portions of the file.
  22.  
  23. In my .mailrc, I have the lines:
  24.     set EDITOR=mail_eedit
  25.     set VISUAL=emacs
  26. so that I can invoke encryption with ~e and straight editing with ~v.
  27.  
  28. Of course, you don't have to use this for encryption.  You can compress
  29. mail or uncompress it at these points, or do any other transformation
  30. you want to on the mail file (as long as it remains transmittable via
  31. SMTP).
  32.  
  33. Enjoy,
  34.     Carl
  35.  
  36. P.S.  I'm not posting my private encryption program but even if I did so,
  37. it would probably be a disservice.  Much better secret-key systems are
  38. freely available.
  39.  
  40. =========================== cut here =======================================
  41. #!/bin/csh -f
  42. # edit an encrypted file, reencrypting the output, possibly with a
  43. # separate password
  44.  
  45. # if this is called mail_eedit, then don't make the backup copy of $ofile
  46. # before writing it.
  47.  
  48. # This script uses md5d for verifying password spelling.  md5d is md5driver.c
  49. # a public domain wrapper for the
  50. #  RSA Data Security, Inc. Message Digest Algorithm # 5
  51.  
  52. set prog_name = "`echo $0 | sed -e 's,^.*/,,'`"
  53.  
  54. if ($prog_name == "mail_eedit") then
  55.     set decr_name = "qdecr"
  56. else
  57.     set decr_name = "decr"
  58. endif
  59.  
  60. # Use any directory for cryptdir which you protect well enough
  61. #  for it to contain temporary copies of plaintext files
  62.  
  63. set cryptdir = ~/crypt
  64.  
  65. set passwords = ${cryptdir}/passwords
  66. set tmpfile = ${cryptdir}/cr$$.tmp
  67. set cpyfile = ${cryptdir}/cr$$.cpy
  68.  
  69. if ($#argv == 0) goto usage_message
  70.  
  71. if ($argv[1] == "-c") then
  72.     switch ($#argv)
  73.     case 3:
  74.         set ofile = $argv[3]
  75.         set ifile = $argv[2]
  76.         breaksw
  77.     case 2:
  78.         set ifile = $argv[2]
  79.         set ofile = $ifile
  80.         breaksw
  81.     default:
  82.         goto usage_message
  83.     endsw
  84. else
  85.     switch ($#argv)
  86.     case 2:
  87.         set ofile = $argv[2]
  88.         set ifile = $argv[1]
  89.         breaksw
  90.     case 1:
  91.         set ifile = $argv[1]
  92.         set ofile = $ifile
  93.         breaksw
  94.     default:
  95.         goto usage_message
  96.     endsw
  97. endif
  98.  
  99. # check password against the file of message digests of all passwords
  100.  
  101. set pw = "`gpw`"
  102. if ("$pw" == "") goto found
  103. set md = `md5d -s"$pw"`
  104. set okm = `cat ${passwords}`
  105. foreach ok ($okm)
  106.     if ($md[1] == $ok) goto found
  107. end
  108.  
  109. echo "Password not known.  Re-type it to record it; <CR> to abort."
  110.  
  111. set pw2 = "`gpw`"
  112. if ("$pw" == "$pw2") goto addit
  113. echo "Mismatch.  Password not added."
  114. exit(1)
  115.  
  116. addit:
  117.     echo "$md[1]" >> ${passwords}
  118. found:
  119.     if ("$pw" == "") then
  120.         cp $ifile  ${tmpfile}
  121.     else
  122.         $decr_name "$pw" <$ifile > ${tmpfile}
  123.     endif
  124.  
  125.     cp ${tmpfile} ${cpyfile}
  126.     $EEDITOR  ${tmpfile}
  127.  
  128.     if ($#argv > 1) goto changeout
  129.     if { cmp -s  ${tmpfile} ${cpyfile} } goto fini
  130.  
  131. changeout:
  132.  
  133. # either $#argv > 1, in which case I'm changing passwords or writing to a
  134. # different file, or the file was changed by the editor
  135.  
  136.     set pwold = "$pw"
  137.     if ($argv[1] == "-c") set pw = "`gpw`"
  138.     if ("$pw" == "") goto foundout
  139.     set md = `md5d -s"$pw"`
  140.     set okm = `cat ${passwords}`
  141.     foreach ok ($okm)
  142.     if ($md[1] == $ok) goto foundout
  143.     end
  144.  
  145.     echo "Password not known.  Re-type it to record it; <CR> to abort."
  146.  
  147.     set pw2 = "`gpw`"
  148.     if ("$pw" == "$pw2") goto additout
  149.     echo "Mismatch.  Password not added.  Using the input password."
  150.     set pw = "$pwold"
  151.     goto foundout
  152.  
  153. additout:
  154.     echo "$md[1]" >> ${passwords}
  155. foundout:
  156.  
  157. # we're going to write the $ofile.  make a copy first, by renaming it.
  158. # unless this is the mail_eedit version
  159.  
  160.     if ($prog_name != "mail_eedit") mv -f ${ofile} ${ofile}~
  161.  
  162.     if ("$pw" == "") then
  163.         cp ${tmpfile}  $ofile 
  164.     else
  165.         encr "$pw" < ${tmpfile} >$ofile
  166.     endif
  167.  
  168. fini:
  169.     rm ${cryptdir}/cr$$.*
  170.     exit(0)
  171.  
  172. usage_message:
  173.     echo "usage: $prog_name [-c] ifile [ofile]"
  174.     echo "    where -c calls for changing the password"
  175.     exit(0)
  176. ============================ end of enclosure ===============================
  177. -- 
  178. -- <<Disclaimer: All opinions expressed are my own, of course.>>
  179. -- Carl Ellison                        cme@sw.stratus.com
  180. -- Stratus Computer Inc.    M3-2-BKW        TEL: (508)460-2783
  181. -- 55 Fairbanks Boulevard ; Marlborough MA 01752-1298    FAX: (508)624-7488
  182.