postfix/postfix_add_mailboxes.sh
2017-02-21 02:32:44 +01:00

267 lines
7.6 KiB
Bash
Executable File

#!/usr/bin/env bash
## --------------------------------------
## -
## - Add mailboxes read from a flat file
## -
## --------------------------------------
## - a.mx.oopen.de
db_name=postfix
db_user=postfix
## - mailbox related
## -
# - 2GB
#quota=2147483648
# - 512MB
quota=536870912
#_passwd='$E%R&T/Z(U'
in_file=/root/mailboxes_ak.lst
log_file=/tmp/postfix_add_mailboxes.log
## --- some functions
## ---
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo -e -n "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
fatal(){
echo ""
echo -e "[ \033[31m\033[1mError\033[m ]: $*"
echo ""
echo -e "\t\033[31m\033[1mInstalllation is canceled\033[m\033[m"
echo ""
exit 1
}
warn (){
echo ""
echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
info (){
echo ""
echo -e "\t[ \033[33m\033[1mInfo\033[m ]: $*"
echo ""
}
ok (){
echo ""
echo -e "\t[ \033[36m\033[1mOk\033[m ]: $*"
echo ""
}
error(){
echo ""
echo -e "\t[ \033[31m\033[1mFehler\033[m ]: $*"
echo ""
}
echo_ok() {
echo -e "\033[75G[ \033[32mok\033[m ]"
}
echo_failed(){
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
}
## - remove leading/trailling whitespaces
## -
trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
## ---
## --- END: functions
date_suffix="`date +%Y%m%d-%H%M`"
echo
## -
## - Logfile
## -
echononl "\tBackup existing log file.."
if [ -f "$log_file" ]; then
mv "$log_file" "${log_file}.${date_suffix}"
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
fi
else
echo_skipped
fi
echononl "\tCreate log file $log_file.."
touch $log_file
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
fi
echo ""
curdir=`pwd`
cd /tmp
while read email passwd ; do
## - get rid of empty lines
[[ -z "$email" ]] && continue
## - get rid of comment lines
[[ $email =~ ^[[:space:]]*# ]] && continue
## - remove leading/trailling whitespaces
## -
email=`trim "$email"`
password=`trim $passwd`
## - regex tha email addresses must matc
## -
#regex_email="^[a-z0-9!#\$%&'*+/=?^_\`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?\$"
regex_email="^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$"
if [[ ! $email =~ $regex_email ]]; then
error "email: give e-mail address ($email) is NOT VALID"
echo "[ FAILED ]: The given e-mail address \"${user}@$domain\" is not VALID" >> $log_file
echo " Domain $domain is not configured as maildomain." >> $log_file
continue
fi
## - splitt in name and domain part
## -
read user domain <<<$(IFS="@" ; echo $email)
echo
echo -e "\t\033[37m\033[1mHandling E-Mail addess ${user}@${domain}..\033[m"
## - check if domain is already configured
## -
domain_exists=`su postgres -c"psql $db_name -At -c\"SELECT 1 FROM domain WHERE domain = '$domain'\""`
if [[ "X$domain_exists" = "X" ]] ; then
warn "Domain $domain is not configured as maildomain."
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
echo " Domain $domain is not configured as maildomain." >> $log_file
continue
fi
if [[ "$user" = "abuse" ]]; then
warn "E-mail address $email is not supported"
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
echo " E-mail address $email is not supported." >> $log_file
continue
elif [[ "$user" = "postmaster" ]];then
warn "E-mail address $email is not supported"
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
echo " E-mail address $email is not supported." >> $log_file
continue
fi
## - If password is not given, then take a default
## -
if [[ -z "$passwd" ]]; then
if [[ -z "$_passwd" ]]; then
passwd=`tr -cd '[:alnum:]\!@#$%' < /dev/urandom | fold -w10 | head -n1`
password_accepted=false
while ! $password_accepted ; do
passwd=`tr -cd '[:alnum:]\!@#$%' < /dev/urandom | fold -w10 | head -n1`
regex="[\!@#$%_]"
[[ $passwd =~ $regex ]] || continue
regex="[0123456789].*[0123456789]"
[[ $passwd =~ $regex ]] || continue
password_accepted=true
done
else
passwd=$_passwd
fi
fi
#password="${password/\'/\\\'}"
#password=`echo $password | sed "d/'/"`
mb_exists=`su postgres -c"psql $db_name -At -c\"SELECT 1 FROM mailbox WHERE username = '${user}@$domain'\""`
if [[ "X$mb_exists" == "X1" ]] ; then
warn "A Mailbox ${user}@$domain already exists."
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
echo " A Mailbox ${user}@$domain already exists." >> $log_file
continue
fi
alias_exists=`su postgres -c"psql $db_name -At -c\"SELECT 1 FROM alias WHERE address = '${user}@$domain'\""`
if [[ "X$alias_exists" == "X1" ]] ; then
warn "A Forwarding Address ${user}@$domain already exists."
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
echo " A Forwarding Address ${user}@$domain already exists." >> $log_file
continue
fi
echononl "\tCreate entry in table \"mailbox\".."
#insert_mb_stmt="SET client_encoding to 'UTF8';\nINSERT INTO mailbox (username,password,name,maildir,local_part,quota,domain,created,modified,active) VALUES ('${user}@$domain','$passwd','','${domain}/${user}/','$user','$quota','$domain',NOW(),NOW(),'t')"
#echo -e "$insert_mb_stmt" | psql -U$db_user $db_name > /dev/null
#sql_file=`mktemp`
#echo "SET client_encoding to 'UTF8';\nINSERT INTO mailbox (username,password,name,maildir,local_part,quota,domain,created,modified,active) VALUES ('${user}@$domain','$passwd','','${domain}/${user}/','$user','$quota','$domain',NOW(),NOW(),'t')" > $sql_file
#psql -Upostfix postfix < $sql_file
#rm $sql_file
su postgres -c"psql $db_name -c\"\
SET client_encoding to 'UTF8'; \
INSERT INTO mailbox (username,password,name,maildir,local_part,quota,domain,created,modified,active) \
VALUES ('${user}@$domain', '$passwd','','${domain}/${user}/','$user','$quota','$domain',NOW(),NOW(),'t')\"" \
> /dev/null 2>&1
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
continue
fi
echononl "\tCreate entry in table \"alias\".."
su postgres -c "psql $db_name -c\"\
SET client_encoding to 'UTF8'; \
INSERT INTO alias (address,goto,domain,created,modified) \
VALUES ('${user}@$domain','${user}@$domain','$domain',NOW(),NOW())\"" > /dev/null 2>&1
if [ "$?" = "0" ]; then
echo_ok
echo -e "\t email: ${user}@$domain"
echo -e "\t password: $passwd"
echo "[ OK ]: e-mail: ${user}@$domain -- password: $passwd" >> $log_file
else
echo_failed
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
echo " Note: Entry in table \"alias\" was done; Take care, to" >> $log_file
echo " remove that Entry." >> $log_file
fi
done < $in_file
echo
echo -e "See \033[37m\033[1m$log_file\033[m to see the results again."
echo ""
cd $pwd
exit 0