#!/bin/csh -f
# Takes some files and codes them in a secure manner suitable for SMTP:
#
#    tar -> compress -> qf-des -> uuencode

# Usage:
#
#    qf-encrypt <files>
#
# You will be promted for a password to be used as a key and an another word to
# be used as an init vector for CBC mode DES. The init vector is optional -
# simply press return if you do not wish to specify one.
# The time of encyption, the name of the file, the password and, if present,
# the init vector will be mailed to the you (hmm ...).

# Change this for your site, if required.
# Don't need full path names, really ...
set tar      = "/usr/bin/tar"
set des      = "/cs/research/midas4/grappa/saleem/bin/sun4/qf-des"
set compress = "/usr/ucb/compress"
set uuencode = "/usr/bin/uuencode"
set now      = "/usr/ucl/now"
set whoami   = "/usr/ucb/whoami"
set mail     = "/usr/bin/mail"
set tmp      = "/tmp"

# Don't change anything below this line.
# (Actually, I'm sure this can be coded better.)
if ($#argv < 1) then
   echo "usage: qf-encrypt <files>"
   exit 0
endif

set ok = ""
while(${ok} != "y")

    set password = ""
    set ivstring = ""

    while(${password} == "")
        echo -n "Password: "
        set password = $<
    end

    echo -n "I Vector: "
    set ivstring = $<

    set ok = ""
    while((${ok} != "y") && (${ok} != "n") && (${ok} != "q"))
        echo -n "OK [y|n|q]? "
        set ok = $<
        if (${ok} == "q") then
            exit 0
        endif
    end

end

set file = "qf-c-$$"
set mFile = "${tmp}/.${file}"
set tFile = "${tmp}/.${file}.tar"
set cFile = "${tFile}.Z"
set dFile = "${cFile}.des"

set tar = "${tar} cvf"
if (${ivstring} == "") then
    set des = "${des} -m cbc -p ${password}"
else
    set des = "${des} -m cbc -p ${password} -s ${ivstring}"
endif
set compress = "${compress} -c"

# Do it - can I do this without all the tmp files and still get a check at each
# stage?
umask 077 # protect any files created

echo "  - tar ... "
if (! ({(${tar} ${tFile} $*)})) then
    echo "could create tar file ${tFile}"
    goto _error_
endif
echo "  - ... ok"

echo -n "  - compress ... "
if (! ({(${compress} ${tFile} > ${cFile})})) then
    echo "could not compress ${tFile}"
    goto _error_
endif
echo "ok"
rm -f ${tFile}

echo -n "  - encrypt ... "
if (! ({(${des} < ${cFile} > ${dFile})})) then
    echo "qf-des failed"
    goto _error_
endif
echo "ok"
rm -f ${cFile}

echo -n "  - uuencode ... "
if (! ({(${uuencode} ${dFile} ${file} > ${file}.uu)})) then
    echo "could not uuencode ${dFile}"
    goto _error_
endif
echo "ok"
rm -f ${dFile}

echo -n "  - just a mo ... "
echo "`${now}`" > ${mFile}
echo "${file}.uu" >> ${mFile}
echo "password=${password}" >> ${mFile}
if (${ivstring} != "") then
    echo "ivector=${ivstring}" >> ${mFile}
endif
if (! ({(${mail} -s "qf-encrypt" `${whoami}` < ${mFile})})) then
    echo "could not mail qf-encrypt log"
else
    echo "ok"
endif
rm -f ${mFile}

echo "File is ${file}.uu"
goto _exit_

_error_:
echo "qf-encrypt failed"

_exit_:
rm -f ${tFile}* >& /dev/null
exit 0
