Add support for MySQL Database backend.

This commit is contained in:
Christoph 2017-11-02 14:19:04 +01:00
parent d65dc0eb6e
commit c8de12dd2a
2 changed files with 153 additions and 44 deletions

View File

@ -15,6 +15,16 @@
# -
#in_file="${conf_dir}/mailboxes_new.lst"
# - db_type
# -
# - Type of Postfix Database
# -
# - Possible values are 'pgsql' (PostgeSQL) or 'mysql' (MySQL)
# -
# - Defaults to: db_type="pgsql"
# -
#db_type="pgsql"
# - db_name
# -
# - Database name for the postfix database
@ -23,13 +33,26 @@
# -
#db_name="postfix"
# - db_user
# - db_name
# -
# - Database user with access to the postfix database ($db_name)
# - Database name for the postfix database
# -
# - Defaults to: db_name="postfix"
# -
# - Defaults to: db_user="postfix"
#db_name="postfix"
# - mysql_credential_args (root access to MySQL Database)
# -
#db_user="postfix"
# - Example
# - mysql_credential_args="--login-path=local"
# - mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf" (Debian default)
# - mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
# -
# - Defaults to:
# - '/etc/mysql/debian.cnf' if MySQL is installed from debian package system
# - '/usr/local/mysql/sys-maint.cnf' otherwise
# -
#mysql_credential_args=""
# - quota
# -

View File

@ -10,17 +10,26 @@ script_dir="$(dirname $(realpath $0))"
conf_dir="${script_dir}/conf"
conf_file="${conf_dir}/postfix_add_mailboxes.conf"
tmp_err_msg="$(mktemp)"
## --- Default Settings
## ---
DEFAULT_db_type="pgsql"^
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
## ---
clean_up() {
# Perform program exit housekeeping
rm -f $tmp_err_msg
exit $1
}
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
@ -35,9 +44,9 @@ 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 -e " \033[31m\033[1mInstalllation is canceled\033[m\033[m"
echo ""
exit 1
clean_up 1
}
warn (){
@ -103,8 +112,25 @@ else
fi
fi
[[ -n "$db_type" ]] || db_type="$DEFAULT_db_type"
if [[ "$db_type" != "pgsql" ]] && [[ "$db_type" != "mysql" ]]; then
fatal "Unknown Database Type '$db_type' for Password Database (Parameter db_type)"
fi
if [[ "$db_type" = "mysql" ]]; then
if [[ -z "$mysql_credential_args" ]]; then
if [[ -f "/etc/mysql/debian.cnf" ]]; then
mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
else
fatal "No credentials for access to MySQL is given!"
fi
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"
@ -120,10 +146,20 @@ echo -e "\033[32mSettings for script \033[37m\033[1msent_userinfo_postfix.sh\033
echo ""
echo " File containing the new mailboxes and passwords.......: $in_file"
echo ""
echo " Mailbox quota to set for each new mailbox.............: $quota"
echo " Mailbox quota to set for each new mailbox.............: $quota ($(echo "scale=2; 536870912 / 1024 /1024" | bc) MB)"
echo ""
echo " Database name for the postfix DB......................: $db_name"
echo " Database user to access the postfix DB................: $db_user"
if [[ "$db_type" = "pgsql" ]] ; then
echo " Type of postfix databae...............................: PostgreSQL ($db_type)"
echo " Database name for the postfix DB......................: $db_name"
elif [[ "$db_type" = "mysql" ]] ; then
echo " Type of postfix databae...............................: MySQL ($db_type)"
echo " Database name for the postfix DB......................: $db_name"
echo " MySQL credential args.................................: $mysql_credential_args"
fi
if [[ "$db_type" = "mysql" ]] ; then
echo " Type of postfix databae...............................: MySQL ($db_type)"
fi
echo ""
OK=
@ -164,7 +200,7 @@ echo
## -
## - Logfile
## -
echononl "\tBackup existing log file.."
echononl " Backup existing log file.."
if [ -f "$log_file" ]; then
mv "$log_file" "${log_file}.${date_suffix}"
if [ "$?" = "0" ]; then
@ -176,7 +212,7 @@ else
echo_skipped
fi
echononl "\tCreate log file $log_file.."
echononl " Create log file $log_file.."
touch $log_file
if [ "$?" = "0" ]; then
echo_ok
@ -210,7 +246,6 @@ while read email passwd ; do
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
@ -219,11 +254,18 @@ while read email passwd ; do
read user domain <<<$(IFS="@" ; echo $email)
echo
echo -e "\t\033[37m\033[1mHandling E-Mail addess ${user}@${domain}..\033[m"
echo -e " \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 [[ "$db_type" = "pgsql" ]] ; then
domain_exists=`su postgres -c"psql $db_name -At -c\"SELECT 1 FROM domain WHERE domain = '$domain'\""`
elif [[ "$db_type" = "mysql" ]] ; then
domain_exists="$(mysql "$mysql_credential_args" "$db_name" \
-N -s -e"SELECT 1 FROM domain WHERE domain = '$domain'")"
else
fatal "Database type '$db_type' is not supported."
fi
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
@ -249,7 +291,7 @@ while read email passwd ; do
if [[ -z "$_passwd" ]]; then
password_accepted=false
while ! $password_accepted ; do
passwd=`tr -cd '[:alnum:]#_\!\%/=@-' < /dev/urandom | tr '0' 'O' | fold -w10 | head -n1`
passwd=`tr -cd '[:alnum:]#_\!\%/=@-' < /dev/urandom | tr '0' 'O' | fold -w12 | head -n1`
regex="[#_\!\%/=@-]"
[[ $passwd =~ $regex ]] || continue
regex="[123456789].*[123456789]"
@ -260,10 +302,15 @@ while read email passwd ; do
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 [[ "$db_type" = "pgsql" ]] ; then
mb_exists=`su postgres -c"psql $db_name -At -c\"SELECT 1 FROM mailbox WHERE username = '${user}@$domain'\""`
elif [[ "$db_type" = "mysql" ]] ; then
mb_exists="$(mysql "$mysql_credential_args" "$db_name" \
-N -s -e"SELECT 1 FROM mailbox WHERE username = '${user}@$domain'")"
else
fatal "Database type '$db_type' is not supported."
fi
if [[ "X$mb_exists" == "X1" ]] ; then
warn "A Mailbox ${user}@$domain already exists."
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
@ -271,7 +318,14 @@ while read email passwd ; do
continue
fi
alias_exists=`su postgres -c"psql $db_name -At -c\"SELECT 1 FROM alias WHERE address = '${user}@$domain'\""`
if [[ "$db_type" = "pgsql" ]] ; then
alias_exists=`su postgres -c"psql $db_name -At -c\"SELECT 1 FROM alias WHERE address = '${user}@$domain'\""`
elif [[ "$db_type" = "mysql" ]] ; then
alias_exists="$(mysql "$mysql_credential_args" "$db_name" \
-N -s -e"SELECT 1 FROM alias WHERE address = '${user}@$domain'")"
else
fatal "Database type '$db_type' is not supported."
fi
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
@ -279,39 +333,52 @@ while read email passwd ; do
continue
fi
echononl "\tCreate entry in table \"mailbox\".."
echononl " Create 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 [[ "$db_type" = "pgsql" ]] ; then
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')\"" \
> $tmp_err_msg 2>&1
elif [[ "$db_type" = "mysql" ]] ; then
$(mysql "$mysql_credential_args" "$db_name" -N -s -e"
SET NAMES 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(),1)" \
> $tmp_err_msg 2>&1)
else
fatal "Database type '$db_type' is not supported."
fi
if [ "$?" = "0" ]; then
echo_ok
else
echo_failed
error "$(cat "$tmp_err_msg")"
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
echononl " Create entry in table \"alias\".."
if [[ "$db_type" = "pgsql" ]] ; then
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())\"" > $tmp_err_msg 2>&1
elif [[ "$db_type" = "mysql" ]] ; then
$(mysql "$mysql_credential_args" "$db_name" -N -s -e"
SET NAMES utf8;
INSERT INTO alias (address,goto,domain,created,modified)
VALUES ('${user}@$domain','${user}@$domain','$domain',NOW(),NOW())" \
> $tmp_err_msg 2>&1)
else
fatal "Database type '$db_type' is not supported."
fi
if [ "$?" = "0" ]; then
echo_ok
echo -e "\t email: ${user}@$domain"
echo -e "\t password: $passwd"
echo -e " email: ${user}@$domain"
echo -e " password: $passwd"
echo "[ OK ]: e-mail: ${user}@$domain -- password: $passwd" >> $log_file
else
echo_failed
@ -320,10 +387,29 @@ while read email passwd ; do
echo " remove that Entry." >> $log_file
fi
# - Test imap connection on the new mailbox using curl
# -
# - Note: turn off history expansion (set +H), to prevent the shell from
# - interpreting sign "!"
# -
set +H
echononl " Test imap connection to mailbox '${user}@$domain'.."
curl --url "imap://127.0.0.1" --user "${user}@$domain:${passwd}" > $tmp_err_msg 2>&1
if [[ $? -ne 0 ]]; then
echo_failed
error "Testing an imap connection failed\n\t $(cat "$tmp_err_msg")"
echo "[ FAILED ]: Testing an imap connection failed!"
else
echo_ok
fi
set -H
done < $in_file
echo ""
echo ""
echononl "\tMove file '$in_file'.."
mv "$in_file" "${in_file}.$(date +%Y-%m-%d)"
mv "$in_file" "${in_file}.ADDED.$(date +%Y-%m-%d)"
if [[ $? -eq 0 ]]; then
echo_ok
else
@ -336,4 +422,4 @@ echo ""
cd $pwd
exit 0
clean_up 0