#!/usr/bin/env bash ## -------------------------------------- ## - ## - Add mailboxes read from a flat file ## - ## -------------------------------------- script_dir="$(dirname $(realpath $0))" conf_dir="${script_dir}/conf" conf_file="${conf_dir}/postfix_add_mailboxes.conf" ## --- Default Settings ## --- DEFAULT_db_name="postfix" DEFAULT_db_user="postfix" DEFAULT_quota="536870912" DEFAULT_in_file="${conf_dir}/mailboxes_new.lst" DEFAULT_log_file="${script_dir}/log/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 "" echo "" echononl " Loading default Configuration values from $(basename ${conf_file}).." if [[ ! -f "$conf_file" ]]; then echo_skipped else source "${conf_file}" > /dev/null 2>&1 if [[ $? -eq 0 ]]; then echo_ok else echo_failed fi fi [[ -n "$db_name" ]] || db_name="$DEFAULT_db_name" [[ -n "$db_user" ]] || db_user="$DEFAULT_db_user" [[ -n "$quota" ]] || quota="$DEFAULT_quota" [[ -n "$in_file" ]] || in_file="$DEFAULT_in_file" [[ -n "$log_file" ]] || log_file="$DEFAULT_log_file" if [[ ! -f "$in_file" ]];then fatal "File containing the email/password pairs '$in_file' does not exist !!" fi echo "" echo "" echo -e "\033[32mSettings for script \033[37m\033[1msent_userinfo_postfix.sh\033[m" echo "" echo " File containing the new mailboxes and passwords.......: $in_file" echo "" echo " Mailbox quota to set for each new mailbox.............: $quota" echo "" echo " Database name for the postfix DB......................: $db_name" echo " Database user to access the postfix DB................: $db_user" echo "" OK= while [ "$OK" != "yes" -o "$OK" != "no" ] ; do echononl "\033[1mParameters ok? [yes/no]:\033[m " read OK ## - To lower case OK=${OK,,} if [ "X$OK" = "X" ]; then echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m\n" OK="" continue fi if [ "$OK" != "yes" -o "$OK" != "no" ] ; then break fi echo -e "\n\t\033[33m\033[1mWrong entry!\033[m\n" done [[ $OK = "yes" ]] || fatal "Repeat execution with different parameters." echo "" echononl " Create log directory '$(dirname "$log_file")'.." if [[ ! -d "$(dirname "$log_file")" ]] ; then mkdir "$(dirname "$log_file")" if [[ $? -eq 0 ]]; then echo_ok else echo_failed fi else echo_skipped fi 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 password_accepted=false while ! $password_accepted ; do passwd=`tr -cd '[:alnum:]#_\!\%/=@-' < /dev/urandom | tr '0' 'O' | fold -w10 | head -n1` regex="[#_\!\%/=@-]" [[ $passwd =~ $regex ]] || continue regex="[123456789].*[123456789]" [[ $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 echononl "\tMove file '$in_file'.." mv "$in_file" "${in_file}.$(date +%Y-%m-%d)" if [[ $? -eq 0 ]]; then echo_ok else echo_failed fi echo echo -e "See \033[37m\033[1m$log_file\033[m to see the results again." echo "" cd $pwd exit 0