#!/bin/sh if [ $# \< 2 -o $# \> 3 ]; then echo "Usage: userforward {account} {forward@address.somewhere} [force]" exit 1 fi user=$1 addr=$2 force=$3 set -e uid=`id -u $user` if [ $uid \< 1000 -a "x$force" != "xforce" ]; then echo "ERROR: User is not a real user (uid too low)!" exit 1 fi homedir=`getent passwd $user | awk -F: '{print $6}';` if [ -e $homedir/.forward -a "x$force" != "xforce" ]; then echo "ERROR: User \"$user\" already has a .forward!" exit 1 fi if [ -d $homedir ]; then touch $homedir/.forward echo "echo $addr>$homedir/.forward" echo $addr>$homedir/.forward chown $user. $homedir/.forward else echo "ERROR: Homedir \"$homedir\" of user \"$user\" doesn't exist!" exit 1 fi mailspool=/var/mail/$user thismaildomain=`cat /etc/mailname 2> /dev/null || hostname -d` if [ -f $mailspool ]; then if [ -s $mailspool -a -x /usr/bin/formail -a -x /usr/lib/sendmail ]; then mv $mailspool $mailspool.off /usr/bin/formail -A "X-Resent-By: postmaster@$thismaildomain" -i "To: $addr" -R Cc: Old-Cc: -R Bcc: Old-Bcc: -s /usr/lib/sendmail -t < $mailspool.off rm -f $mailspool.off echo "Old incoming mails forwarded to new account." else echo "OBS! Old incoming mails not forwarded." fi fi echo "Done!"