Add support for MySQL Database backend.
This commit is contained in:
parent
d65dc0eb6e
commit
c8de12dd2a
@ -15,6 +15,16 @@
|
|||||||
# -
|
# -
|
||||||
#in_file="${conf_dir}/mailboxes_new.lst"
|
#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
|
# - db_name
|
||||||
# -
|
# -
|
||||||
# - Database name for the postfix database
|
# - Database name for the postfix database
|
||||||
@ -23,13 +33,26 @@
|
|||||||
# -
|
# -
|
||||||
#db_name="postfix"
|
#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_user="postfix"
|
# - Defaults to: db_name="postfix"
|
||||||
# -
|
# -
|
||||||
#db_user="postfix"
|
#db_name="postfix"
|
||||||
|
|
||||||
|
# - mysql_credential_args (root access to MySQL Database)
|
||||||
|
# -
|
||||||
|
# - 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
|
# - quota
|
||||||
# -
|
# -
|
||||||
|
@ -10,17 +10,26 @@ script_dir="$(dirname $(realpath $0))"
|
|||||||
conf_dir="${script_dir}/conf"
|
conf_dir="${script_dir}/conf"
|
||||||
conf_file="${conf_dir}/postfix_add_mailboxes.conf"
|
conf_file="${conf_dir}/postfix_add_mailboxes.conf"
|
||||||
|
|
||||||
|
tmp_err_msg="$(mktemp)"
|
||||||
|
|
||||||
|
|
||||||
## --- Default Settings
|
## --- Default Settings
|
||||||
## ---
|
## ---
|
||||||
|
DEFAULT_db_type="pgsql"^
|
||||||
DEFAULT_db_name="postfix"
|
DEFAULT_db_name="postfix"
|
||||||
DEFAULT_db_user="postfix"
|
|
||||||
DEFAULT_quota="536870912"
|
DEFAULT_quota="536870912"
|
||||||
DEFAULT_in_file="${conf_dir}/mailboxes_new.lst"
|
DEFAULT_in_file="${conf_dir}/mailboxes_new.lst"
|
||||||
DEFAULT_log_file="${script_dir}/log/postfix_add_mailboxes.log"
|
DEFAULT_log_file="${script_dir}/log/postfix_add_mailboxes.log"
|
||||||
|
|
||||||
## --- some functions
|
## --- some functions
|
||||||
## ---
|
## ---
|
||||||
|
clean_up() {
|
||||||
|
|
||||||
|
# Perform program exit housekeeping
|
||||||
|
rm -f $tmp_err_msg
|
||||||
|
exit $1
|
||||||
|
}
|
||||||
|
|
||||||
echononl(){
|
echononl(){
|
||||||
echo X\\c > /tmp/shprompt$$
|
echo X\\c > /tmp/shprompt$$
|
||||||
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
|
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
|
||||||
@ -35,9 +44,9 @@ fatal(){
|
|||||||
echo ""
|
echo ""
|
||||||
echo -e "[ \033[31m\033[1mError\033[m ]: $*"
|
echo -e "[ \033[31m\033[1mError\033[m ]: $*"
|
||||||
echo ""
|
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 ""
|
echo ""
|
||||||
exit 1
|
clean_up 1
|
||||||
}
|
}
|
||||||
|
|
||||||
warn (){
|
warn (){
|
||||||
@ -103,8 +112,25 @@ else
|
|||||||
fi
|
fi
|
||||||
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_name" ]] || db_name="$DEFAULT_db_name"
|
||||||
[[ -n "$db_user" ]] || db_user="$DEFAULT_db_user"
|
|
||||||
[[ -n "$quota" ]] || quota="$DEFAULT_quota"
|
[[ -n "$quota" ]] || quota="$DEFAULT_quota"
|
||||||
[[ -n "$in_file" ]] || in_file="$DEFAULT_in_file"
|
[[ -n "$in_file" ]] || in_file="$DEFAULT_in_file"
|
||||||
[[ -n "$log_file" ]] || log_file="$DEFAULT_log_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 ""
|
||||||
echo " File containing the new mailboxes and passwords.......: $in_file"
|
echo " File containing the new mailboxes and passwords.......: $in_file"
|
||||||
echo ""
|
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 ""
|
||||||
echo " Database name for the postfix DB......................: $db_name"
|
if [[ "$db_type" = "pgsql" ]] ; then
|
||||||
echo " Database user to access the postfix DB................: $db_user"
|
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 ""
|
echo ""
|
||||||
OK=
|
OK=
|
||||||
@ -164,7 +200,7 @@ echo
|
|||||||
## -
|
## -
|
||||||
## - Logfile
|
## - Logfile
|
||||||
## -
|
## -
|
||||||
echononl "\tBackup existing log file.."
|
echononl " Backup existing log file.."
|
||||||
if [ -f "$log_file" ]; then
|
if [ -f "$log_file" ]; then
|
||||||
mv "$log_file" "${log_file}.${date_suffix}"
|
mv "$log_file" "${log_file}.${date_suffix}"
|
||||||
if [ "$?" = "0" ]; then
|
if [ "$?" = "0" ]; then
|
||||||
@ -176,7 +212,7 @@ else
|
|||||||
echo_skipped
|
echo_skipped
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echononl "\tCreate log file $log_file.."
|
echononl " Create log file $log_file.."
|
||||||
touch $log_file
|
touch $log_file
|
||||||
if [ "$?" = "0" ]; then
|
if [ "$?" = "0" ]; then
|
||||||
echo_ok
|
echo_ok
|
||||||
@ -210,7 +246,6 @@ while read email passwd ; do
|
|||||||
if [[ ! $email =~ $regex_email ]]; then
|
if [[ ! $email =~ $regex_email ]]; then
|
||||||
error "email: give e-mail address ($email) is NOT VALID"
|
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 "[ FAILED ]: The given e-mail address \"${user}@$domain\" is not VALID" >> $log_file
|
||||||
echo " Domain $domain is not configured as maildomain." >> $log_file
|
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -219,11 +254,18 @@ while read email passwd ; do
|
|||||||
read user domain <<<$(IFS="@" ; echo $email)
|
read user domain <<<$(IFS="@" ; echo $email)
|
||||||
|
|
||||||
echo
|
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
|
## - 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
|
if [[ "X$domain_exists" = "X" ]] ; then
|
||||||
warn "Domain $domain is not configured as maildomain."
|
warn "Domain $domain is not configured as maildomain."
|
||||||
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
|
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
|
||||||
@ -249,7 +291,7 @@ while read email passwd ; do
|
|||||||
if [[ -z "$_passwd" ]]; then
|
if [[ -z "$_passwd" ]]; then
|
||||||
password_accepted=false
|
password_accepted=false
|
||||||
while ! $password_accepted ; do
|
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="[#_\!\%/=@-]"
|
regex="[#_\!\%/=@-]"
|
||||||
[[ $passwd =~ $regex ]] || continue
|
[[ $passwd =~ $regex ]] || continue
|
||||||
regex="[123456789].*[123456789]"
|
regex="[123456789].*[123456789]"
|
||||||
@ -260,10 +302,15 @@ while read email passwd ; do
|
|||||||
passwd=$_passwd
|
passwd=$_passwd
|
||||||
fi
|
fi
|
||||||
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
|
if [[ "X$mb_exists" == "X1" ]] ; then
|
||||||
warn "A Mailbox ${user}@$domain already exists."
|
warn "A Mailbox ${user}@$domain already exists."
|
||||||
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
|
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
|
||||||
@ -271,7 +318,14 @@ while read email passwd ; do
|
|||||||
continue
|
continue
|
||||||
fi
|
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
|
if [[ "X$alias_exists" == "X1" ]] ; then
|
||||||
warn "A Forwarding Address ${user}@$domain already exists."
|
warn "A Forwarding Address ${user}@$domain already exists."
|
||||||
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
|
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
|
||||||
@ -279,39 +333,52 @@ while read email passwd ; do
|
|||||||
continue
|
continue
|
||||||
fi
|
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')"
|
if [[ "$db_type" = "pgsql" ]] ; then
|
||||||
#echo -e "$insert_mb_stmt" | psql -U$db_user $db_name > /dev/null
|
su postgres -c"psql $db_name -c\"\
|
||||||
|
SET client_encoding to 'UTF8'; \
|
||||||
#sql_file=`mktemp`
|
INSERT INTO mailbox (username,password,name,maildir,local_part,quota,domain,created,modified,active) \
|
||||||
#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
|
VALUES ('${user}@$domain', '$passwd','','${domain}/${user}/','$user','$quota','$domain',NOW(),NOW(),'t')\"" \
|
||||||
#psql -Upostfix postfix < $sql_file
|
> $tmp_err_msg 2>&1
|
||||||
#rm $sql_file
|
elif [[ "$db_type" = "mysql" ]] ; then
|
||||||
|
$(mysql "$mysql_credential_args" "$db_name" -N -s -e"
|
||||||
su postgres -c"psql $db_name -c\"\
|
SET NAMES utf8;
|
||||||
SET client_encoding to 'UTF8'; \
|
INSERT INTO mailbox (username,password,name,maildir,local_part,quota,domain,created,modified,active)
|
||||||
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)" \
|
||||||
VALUES ('${user}@$domain', '$passwd','','${domain}/${user}/','$user','$quota','$domain',NOW(),NOW(),'t')\"" \
|
> $tmp_err_msg 2>&1)
|
||||||
> /dev/null 2>&1
|
else
|
||||||
|
fatal "Database type '$db_type' is not supported."
|
||||||
|
fi
|
||||||
|
|
||||||
if [ "$?" = "0" ]; then
|
if [ "$?" = "0" ]; then
|
||||||
echo_ok
|
echo_ok
|
||||||
else
|
else
|
||||||
echo_failed
|
echo_failed
|
||||||
|
error "$(cat "$tmp_err_msg")"
|
||||||
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
|
echo "[ FAILED ]: Cannot create e-mail address \"${user}@$domain\"" >> $log_file
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echononl "\tCreate entry in table \"alias\".."
|
echononl " Create entry in table \"alias\".."
|
||||||
su postgres -c "psql $db_name -c\"\
|
if [[ "$db_type" = "pgsql" ]] ; then
|
||||||
SET client_encoding to 'UTF8'; \
|
su postgres -c "psql $db_name -c\"\
|
||||||
INSERT INTO alias (address,goto,domain,created,modified) \
|
SET client_encoding to 'UTF8'; \
|
||||||
VALUES ('${user}@$domain','${user}@$domain','$domain',NOW(),NOW())\"" > /dev/null 2>&1
|
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
|
if [ "$?" = "0" ]; then
|
||||||
echo_ok
|
echo_ok
|
||||||
echo -e "\t email: ${user}@$domain"
|
echo -e " email: ${user}@$domain"
|
||||||
echo -e "\t password: $passwd"
|
echo -e " password: $passwd"
|
||||||
echo "[ OK ]: e-mail: ${user}@$domain -- password: $passwd" >> $log_file
|
echo "[ OK ]: e-mail: ${user}@$domain -- password: $passwd" >> $log_file
|
||||||
else
|
else
|
||||||
echo_failed
|
echo_failed
|
||||||
@ -320,10 +387,29 @@ while read email passwd ; do
|
|||||||
echo " remove that Entry." >> $log_file
|
echo " remove that Entry." >> $log_file
|
||||||
fi
|
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
|
done < $in_file
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo ""
|
||||||
echononl "\tMove file '$in_file'.."
|
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
|
if [[ $? -eq 0 ]]; then
|
||||||
echo_ok
|
echo_ok
|
||||||
else
|
else
|
||||||
@ -336,4 +422,4 @@ echo ""
|
|||||||
|
|
||||||
cd $pwd
|
cd $pwd
|
||||||
|
|
||||||
exit 0
|
clean_up 0
|
||||||
|
Loading…
Reference in New Issue
Block a user