#!/bin/sh
# guest.sh
# 2013-03-25
# by Gernot Walzl
# Creates an account for guests.
# The guest's home directory gets cleaned up on logout.
# KDE (&KDM) is required.
USERNAME="guest"
USERHOME="/home/${USERNAME}"
FILENAME=".${USERNAME}.tar.gz"
set -e
print_usage () {
echo "Usage: $0 {create|destroy|init|clean}"
}
create () {
AGIDS="audio,cdrom,floppy,plugdev,video,power,netdev,lp,scanner"
useradd --create-home --uid 1100 --gid users --groups "$AGIDS" --shell /bin/bash "$USERNAME"
cp "$0" "${USERHOME}/.guest.sh"
mkdir -p "${USERHOME}/.kde/shutdown"
cat > "${USERHOME}/.kde/shutdown/guest.sh" << EOF
#!/bin/sh
~/.guest.sh clean
EOF
chmod +x "${USERHOME}/.kde/shutdown/guest.sh"
chown -cR "${USERNAME}:users" "$USERHOME"
chmod 711 "$USERHOME"
echo ""
echo "Please enter a long random string as password."
echo "Set up KDM to login without password for the guest account."
echo ""
passwd "$USERNAME"
}
destroy () {
userdel "$USERNAME" || exit 1
rm -rf "$USERHOME"
}
init () {
cd "$USERHOME" || exit 1
tar -cvpzf "$FILENAME" --exclude="$FILENAME" "$USERHOME"
}
clean () {
cd "$USERHOME" || exit 1
if [ -r "$FILENAME" ]; then
rm -rf "/tmp/${USERNAME}-trash"
mkdir -p "/tmp/${USERNAME}-trash"
find "$USERHOME" -mindepth 1 -maxdepth 1 \
! \( -name "$FILENAME" -o -name '.guest.sh' -o -name '.gvfs' \) -print0 \
| xargs -0 -I{} mv {} "/tmp/${USERNAME}-trash"
cd / || exit 1
HOMEDIR=$(echo "$USERHOME" | sed 's#^/##')
tar xvf "${USERHOME}/${FILENAME}" "$HOMEDIR"
else
echo "Error: $FILENAME is not readable."
fi
}
case "$1" in
'create')
create
;;
'destroy')
destroy
;;
'init')
init
;;
'clean')
clean
;;
*)
print_usage
esac