Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
making life easy with virtual/mailhost
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
ang
n00b
n00b


Joined: 30 Jan 2003
Posts: 29

PostPosted: Sun Mar 16, 2003 12:10 pm    Post subject: making life easy with virtual/mailhost Reply with quote

I have installed yesterday the virtual/mailhost setup on a server. To be able to add users easily, I have also created a small script:

I provide hosting services and I have to use different groups of users: some have real accounts. Some use only mailing services, some others belong to mx-ed domains, where mail is only being gathered on my server and processed somewhere else.

Furthermore, user data should be seperated from each other's stuff at least on the level of groups.

This script creates and adds a new linux user, a group for the existing domain, the user to the new/existing group and cron

So what happens here?

If the domain does not exist, the script exists. So first either set BIND_FILES variable, or delete that part. But it is better to have first configured the FQDN for setting up virtualmailhosts.

Then, we check, if the username (LinuxUserName) exist. If yes, the script fails.

Else, the user and a group based on the domain name without the tld is being created, and login information is being uploaded to mysql.

(I have hacked an other row in the mailsql.users table to cricumvent a bug with virtual delivery and mailboxes). And also, check that you use the mysql-virtual-uid.cf and mysql-virtual-gid.cf files and required virtual maps also.

At the end, you'll have a user in /etc/passwd, who can not login to shell (because the shell is set to /bin/false), but may fully use all mysql.auth based services: imap, imaps, pop3, pop3s and squirrelmail... ;-)

If you take the example:
Username: billg
Name: Billing Gate
Mail Alias: billing.gate@bar.org

In the end he shall have his home directory in /home/bar/billg, with right permissions set able to send and receive mails.

Code:

#!/bin/bash

if [[ (-z "${1}") || (-z "${2}") || (-z "${3}") || (-z "${4}") || (-n "${6}") || ("${1}" = "--help") ]]
then
    echo ""
    echo "NewUser for VirtualHost w. Postfix MysqlAuth"
    echo "Written by ang (ang at cpst.hu)"
    echo
    echo "  Usage: "`basename ${0}`" LinuxUsername \"Full Name\" \"Mail Alias\" DomainName [password]"
    echo "  Usage Example: "`basename $0`" billg \"Billing Gate\" \"billing.gate@foo.bar.org\" bar.org [password]"
    exit 0
fi

# Gather configuration
MailSqlUser=<set yours here>
MailSqlPassword=<set yours here>

uname=`echo ${1} | tr -d ' :.'`
rname=${2}
aliasname=${3}
dname=${4}
gname=`echo ${4} | sed -e 's/^www\.//' -e 's/\.hu$//' -e 's/\.com$//' -e 's/\.org$//' -e 's/\.net$//' -e 's/\./-/g' | head -c 16`

# Password config
pass=`echo "${uname} ${rname}" | md5sum | head -c 7`
[[ "$5" ]] && pass=$5

BIND_FILES="/chroot/dns/var/bind/pri/"

if [ ! -e ${BIND_FILES}${dname} ]; then
echo "No domain data in ${BIND_FILES} for ${2}"
exit 0
fi

# Setup complete, prompt
echo "** Creating user with the following settings:"
echo " - User: ${uname}"
echo " - Real name: ${rname}"
echo " - Mail alias: ${aliasname}"
echo " - Domain / Group name: ${dname} / ${gname} (domain exists)"
echo " - Password: ${pass}"

echo
echo "** Ctrl-C now to abort, enter to continue"

read i

echo "Working..."

uid=`cat /etc/passwd | grep ${uname} | cut -f 3 -d :`

if [ -e ${uid} ]; then
echo "** Adding user..."
/usr/sbin/adduser -d /home/${gname}/${uname} -g users -G cron -s /bin/false ${uname}
chfn -f "${rname}" -r ${gname} ${uname}
# Hack to make passwd work
{ echo ${pass}; sleep 1; echo ${pass}; } | passwd ${uname} 2>/dev/null
else
echo -n "** Username ${uname} already exists in /etc/passwd as UID: "
echo ${uid}
#exit 0
fi

uid=`cat /etc/passwd | grep ${uname} | cut -f 3 -d :`

if [ -e `cat /etc/group | grep ${gname}` ]; then
echo "** Group for domain ${gname} does not exist yet... Creating..."
groupadd -g ${uid} ${gname}
fi

# Do userdata database stuff
uid=`cat /etc/passwd | grep ${uname} | cut -f 3 -d :`
gid=`cat /etc/group | grep ${gname} | cut -f 3 -d :`

dbsuccess=0
echo "REPLACE INTO mailsql.transport SET domain = '${dname}', destination = 'virtual:';" > /tmp/domainsql
cat /tmp/domainsql | mysql -u ${MailSqlUser} --password=${MailSqlPassword} && dbsuccess=1
rm /tmp/domainsql

[ $dbsuccess -eq 0 ] && echo "** Database operation failed for ${dname} virtual host data" && exit 1
[ $dbsuccess -eq 1 ] && echo "** Database operation succeeded for ${dname} virtual host data"

if [ -e `cat /etc/group | grep ${uname} | grep ${gname}` ]; then
echo "** ${uname} not member of group ${gname} yet... Creating membership..."
usermod -G ${gname} ${uname}
fi

if [ ! -e /home/${gname} ]; then
echo "** Creating /home/${gname} home directories..."
mkdir /home/${gname}
chown root.root /home/${gname}
chmod 0750 /home/${gname}
else
echo "** /home/${gname} exist already. Not created."
fi

if [ ! -e /home/${gname}/${uname} ]; then
echo "Creating /home/${gname}/${uname} home directories..."
mkdir /home/${gname}/${uname}
chown ${uname}.${gname} /home/${gname}/${uname}
chmod 0750 /home/${gname}/${uname}
#mkdir /home/${gname}/${uname}/.maildir/
#chown ${uname}.${gname} /home/${gname}/${uname}
#chmod 0700 /home/${gname}/${uname}/.maildir
#mkdir /home/${gname}/${uname}/.maildir-sent/
#chown ${uname}.${gname} /home/${gname}/${uname}/.maildir-sent/
#chmod 0700 /home/${gname}/${uname}/.maildir-sent/
else
echo "** /home/${gname}/${uname} exist already. Not created."
fi

# Do userdata database stuff
uid=`cat /etc/passwd | grep ${uname} | cut -f 3 -d :`
gid=`cat /etc/group | grep ${gname} | cut -f 3 -d :`

dbsuccess=0
echo "REPLACE INTO mailsql.users SET email = '${uname}@${dname}', clear = '${pass}', name = '${rname}', uid = '${uid}', gid = '${gid}', homedir = '/home/${gname}/${uname}', maildir = '/home/${gname}/${uname}/.maildir/', maildir_bug = '${gname}/${uname}/.maildir/', postfix='y';" > /tmp/usersql
cat /tmp/usersql | mysql -u mailsql --password=${MailSqlPassword} && dbsuccess=1
rm /tmp/usersql

[ $dbsuccess -eq 0 ] && echo "** Database operation failed for ${uname} userdata" && exit 1
[ $dbsuccess -eq 1 ] && echo "** Database operation succeeded for ${uname} userdata"

echo "** Done adding ${rname} as ${uname} to ${gname} receiving mail as ${aliasname}."
echo "** To account shell login enable default shell in /etc/passwd by setting it to /bin/bash!!!"


The script is based on delta407's vhost scripts, but uses the virtual/mailhost setup as suggested in http://www.gentoo.org/doc/en/virt-mail-howto.xml

I appreciate all kind of feedback.
Back to top
View user's profile Send private message
tagore
Tux's lil' helper
Tux's lil' helper


Joined: 24 Oct 2002
Posts: 77
Location: Uruguay

PostPosted: Wed May 21, 2003 1:01 pm    Post subject: Reply with quote

Not work for my :(

Quote:

** Creating user with the following settings:
- User: cesar
- Real name: Cesar Bermudez
- Mail alias: cesar@midomain.com.uy
- Domain / Group name: midomain.com.uy / midomain-com (domain exists)
- Password: test

** Ctrl-C now to abort, enter to continue

Working...
** Adding user...
** Group for domain midomain-com does not exist yet... Creating...
** Database operation succeeded for midomain.com.uy virtual host data
** cesar not member of group midomain-com yet... Creating membership...
** Creating /home/midomain-com home directories...
Creating /home/midomain-com/cesar home directories...
ERROR 1054 at line 1: Unknown column 'maildir_bug' in 'field list'
** Database operation failed for cesar userdata



Plis showme your mysql dump, whitout pass, and fake domains plis.
cheers.
Back to top
View user's profile Send private message
PhotonicGuy
n00b
n00b


Joined: 05 May 2010
Posts: 7

PostPosted: Mon May 10, 2010 1:48 pm    Post subject: Reply with quote

Not working. Same as ang
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum