#!/bin/sh
#
# /usr/local/bin/addprivacy script from the Anonymizing Unix Project
# by van Hauser / THC <vh@reptile.rug.ac.be>
#
# This script adds the privacy setup to all existing users
#
echo 'Please press ENTER to start the privacy setup or press Control-C to abort'
echo 'Note that the anonymous unix install ("make install") and setup'
echo '("make setup") needs be finished!'
read

#
# Maybe CFS was already mounted? It should not!
CFS_MOUNT=`mount|grep localhost:|grep /home`
if [ ! -z "$CFS_MOUNT" ] ; then
    echo "Warning: Can't do my work. You've already mounted CFS."
    echo "Please run the script \"/sbin/init.d/privacy stop\", then restart this script."
    exit 1
fi

#
# Security ;-) Tempdir setup
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
TMPDIR="/tmp/.privacysetup$$"
rm -rf $TMPDIR
mkdir -m 700 $TMPDIR || exit 1

#
# Privacy ;-)
if [ ! -x /usr/local/bin/srm ]; then
    echo "Can't find /usr/local/bin/srm from the secure_delete package! Exiting ..."
    exit 1
fi

#
# Save and remove the homdirs
cd /home || exit 1
for i in * ; do
    if [ -d "$i" ]; then
        tar czf $TMPDIR/$i $i
        srm -flr $i
    fi
done
srm -fllr /home/* /home/.[^.]* > /dev/null 2>&1

sleep 5

#
# Starting CFS
/sbin/init.d/privacy start

#
# Creating the CFS directories
cd $TMPDIR
for i in * ; do
    echo -e "\nCreating CFS homedir for user $i, enter new CFS password:"
    cmkdir -3 /crypt/$i || cmkdir -3 /crypt/$i || cmkdir -3 /crypt/$i
    echo -e "\nPlease reenter the password to mount the CFS homedir of user $i:"
    cattach /crypt/$i $i || cattach /crypt/$i $i || cattach /crypt/$i $i
    cd /home
    tar xzf $TMPDIR/$i
    cp /etc/skel/.bash_logout /etc/skel/.logout $i
    mkdir $i/tmp
    cdetach $i
    chown -R $i /crypt/$i
done

#
# Cleaning up
echo
echo "If you press enter now, all saved user homedirectory data will be wiped."
echo "Maybe you would like to make a crypted backup of these."
echo "You can find the files in $TMPDIR"
read
sleep 3
srm -rf $TMPDIR

echo "My work is done. Privacy setup is ready and started."

# done
