#!/bin/sh

# must run as root
# only tested on linux using /etc/passwd
# files modified are :
# /etc/passwd , /etc/shadow & /etc/group
# /etc/passwd.md5 for integrity checking
# /home/$USERNAME , and quota file (if it exists)
# /etc/postfix/virtual , $ACCESS & db’s if they exist
# $SQUIRREL/$USERNAME.* if they exist
# the following variables are user-modifiable

#default virtual host
VHOST=”ripserve.com”
#where to mail new account info (to sysadmin)
MAIL=”root@$VHOST”
#path to squirrelmail data, & postfix access file, both optional
SQUIRREL=”/var/lib/squirrelmail/prefs”
ACCESS=”/etc/postfix/sender_access”

PROVIDER=”$VHOST”
QUOTA=”500000 520000 500000 6000000″

function newusermail()
{
cat <<EOF | mail -s “Welcome to $PROVIDER” $USERNAME@$VHOST
$REALNAME, welcome to $PROVIDER. Your account initially has 500 megabytes of disk space. You can find information on using our services at:

http://www.ripserve.com/support/


Ripserve Ltd
PO Box 53909
London SW15 1YR
Tel: 020 8789 2620

http://www.ripserve.com/

EOF
}

function usage()
{
echo “Usage: $0 username \”realname\” password [-v virtualhost] [-s suplementarygroup]”
exit 1
}

function checkdomain()
{
#is domain plausible?
if echo $VHOST | grep -q “.\…”
then
:
else
echo “$VHOST doesn’t look like a domain, exiting”
exit 1
fi
}

#test command line input, used for degugging
function debug()
{
echo “username= $USERNAME”
echo “realname= $REALNAME”
echo “password= $PASSWORD”
echo “vhost= $VHOST”
echo “supgroup= $SUPGROUP”
echo “”
checkdomain
exit 1
}

if [ $# -lt 3 ]; then
usage
fi

USERNAME=$1
REALNAME=$2
PASSWORD=$3
SUPGROUP=”"
DATE=$(date)

while test “$1″ != “” ; do
case $1 in
-v)
VHOST=$2
shift
;;
-s)
SUPGROUP=$2
shift
;;

-d)
debug
shift
;;

-h)
usage
shift
;;
-*)
echo “Error: no such option $1″
usage
shift
;;
esac
shift
done

#check the user doesn’t already have an account
if [ -d "/home/$USERNAME" ]
then
echo “User $USERNAME already exists, exiting”
exit 1
fi

#check we’re root
USER=$(id | grep root | wc -l)
if [ $USER -eq 0 ]
then
echo “Run this as root to create new accounts”
exit 1
fi

checkdomain

#first add user a/c and passwd
/usr/sbin/useradd $USERNAME -m && /usr/bin/chfn -f “$REALNAME” $USERNAME
if [ -r /home/quota.user ]
then
/usr/sbin/setquota $USERNAME -a $QUOTA
fi

if [ -n $SUPGROUP ]
then
/usr/sbin/usermod -G $SUPGROUP $USERNAME”
fi

echo “$PASSWORD” | /usr/bin/passwd $USERNAME –stdin
md5sum /etc/passwd > /etc/passwd.md5

#postfix stuff

if [ -r /etc/postfix ]
then
echo “$USERNAME@$VHOST $USERNAME” >> /etc/postfix/virtual
/usr/sbin/postmap -r /etc/postfix/virtual
fi

if [ -r $ACCESS ]
then
echo “$USERNAME@$VHOST OK” >> $ACCESS
/usr/sbin/postmap -r $ACCESS
fi

#create new webmail settings
if [ -r $SQUIRREL ]
then
echo “email_address=$USERNAME@$VHOST” > $SQUIRREL/$USERNAME.pref
echo “full_name=$REALNAME” >> $SQUIRREL/$USERNAME.pref
echo “Support| | |support@$PROVIDER|Between 09h30 and 18h00 GMT” > $SQUIRREL/$USERNAME.abook
/bin/chown apache.apache $SQUIRREL*
fi

newusermail

echo “A new account has been created on $DATE: $USERNAME@$VHOST” | mail -s “New $PROVIDER account” $MAIL