diff --git a/.gitignore b/.gitignore index e5b67c7..788271b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.log conf/*.conf conf/*.email* +conf/*.lst* !conf/sent_userinfo_postfix.email.sample diff --git a/conf/postfix_add_mailboxes.conf.sample b/conf/postfix_add_mailboxes.conf.sample new file mode 100644 index 0000000..e247156 --- /dev/null +++ b/conf/postfix_add_mailboxes.conf.sample @@ -0,0 +1,48 @@ +# ---------------------------------------------------- +# --- +# - Parameter Settings for script 'postfix_add_mailboxes.sh'. +# --- +# ---------------------------------------------------- + + +# - in_file +# - +# - The file from wich the script reads the e-mail-address/password +# - kombination(s). Each line in this file must only contain +# - +# - +# - Defaults to: in_file="${conf_dir}/mailboxes_new.lst" +# - +#in_file="${conf_dir}/mailboxes_new.lst" + +# - db_name +# - +# - Database name for the postfix database +# - +# - Defaults to: db_name="postfix" +# - +#db_name="postfix" + +# - db_user +# - +# - Database user with access to the postfix database ($db_name) +# - +# - Defaults to: db_user="postfix" +# - +#db_user="postfix" + +# - quota +# - +# - The quota setting for the new mailboxes. +# - +# - Defaults to: quota="536870912" +# - +#quota="536870912" + +# - log_file +# - +# - Where to write logging informations? +# - +# - Defaults to: log_file="${script_dir}/log/postfix_add_mailboxes.log" +# - +#log_file="${script_dir}/log/postfix_add_mailboxes.log" diff --git a/log/postfix_add_mailboxes.log.20171101-1817 b/log/postfix_add_mailboxes.log.20171101-1817 new file mode 100644 index 0000000..0ec075e --- /dev/null +++ b/log/postfix_add_mailboxes.log.20171101-1817 @@ -0,0 +1 @@ +[ OK ]: e-mail: ckubu-test2@oopen.de -- password: EadGl15E.% diff --git a/postfix_add_mailboxes.sh b/postfix_add_mailboxes.sh index 2768b34..2cfe472 100755 --- a/postfix_add_mailboxes.sh +++ b/postfix_add_mailboxes.sh @@ -6,22 +6,18 @@ ## - ## -------------------------------------- +script_dir="$(dirname $(realpath $0))" +conf_dir="${script_dir}/conf" +conf_file="${conf_dir}/postfix_add_mailboxes.conf" -## - 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_new.lst - -log_file=/tmp/postfix_add_mailboxes.log +## --- 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 ## --- @@ -93,6 +89,76 @@ trim() { 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 ## - @@ -256,6 +322,14 @@ while read email passwd ; do 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 "" diff --git a/sent_userinfo_postfix.sh b/sent_userinfo_postfix.sh index 314908d..542c01d 100755 --- a/sent_userinfo_postfix.sh +++ b/sent_userinfo_postfix.sh @@ -5,7 +5,7 @@ conf_dir="${script_dir}/conf" conf_file="${conf_dir}/sent_userinfo_postfix.conf" tmp_dir="$(mktemp -d)" -logfile="${script_dir}/sent_userinfo_postfix.$(date +%Y-%m-%d-%H%M).log" +logfile="${script_dir}/log/sent_userinfo_postfix.$(date +%Y-%m-%d-%H%M).log" #--------------------------------------- #----------------------------- @@ -40,6 +40,12 @@ echononl(){ rm /tmp/shprompt$$ } +warn (){ + echo "" + echo -e "\t[ \033[33m\033[1mWarning\033[m ]: $*" + echo "" +} + fatal(){ echo "" echo -e "[ \033[31m\033[1mFehler\033[m ]: $*" @@ -96,6 +102,57 @@ if [[ ! -f $user_info_file ]];then fi +echo "" +echo -e "\033[32m--\033[m" +echo "" +echo "Is this a tset-run, sending the user info to only one given address?" +echo "" +echo "" +_TEST_RUN= +while [ "$_TEST_RUN" != "yes" -o "$_TEST_RUN" != "no" ] ; do + echononl "\033[1mTest run? [yes/no]:\033[m " + read _TEST_RUN + ## - To lower case + _TEST_RUN=${_TEST_RUN,,} + if [ "X$_TEST_RUN" = "X" ]; then + echo -e "\n\t\033[33m\033[1mAn entry is required! Type 'yes' or 'no'\033[m.\n" + _TEST_RUN="" + continue + fi + if [ "$_TEST_RUN" = "yes" -o "$_TEST_RUN" = "no" ] ; then + break + fi + echo -e "\n\t\033[33m\033[1mWrong entry! Type 'yes' or 'no'.\033[m\n" +done +if [[ "$_TEST_RUN" = "yes" ]]; then + TEST_RUN=true +else + TEST_RUN=false +fi + + +if $TEST_RUN ; then + + echo "" + echo -e "\033[32m--\033[m" + echo "" + echo "Give the recipient address for the test run." + echo "" + echo "" + test_email="" + while [[ "X$test_email" = "X" ]] ; do + echononl "\033[1mRecipient address for test run:\033[m " + read test_email + if [[ "X$test_email" = "X" ]] ; then + echo -e "\n\t\033[33m\033[1mAn entry is required!\033[m" + fi + done +fi + + + + + echo "" echo "" echo -e "\033[32mSettings for script \033[37m\033[1msent_userinfo_postfix.sh\033[m" @@ -107,6 +164,16 @@ echo " Mail Sender Organisation..........: $email_from_org" echo "" echo " Mail User.........................: $mail_user" echo " Mail Group........................: $mail_group" +echo "" +echo " Test Run..........................: $TEST_RUN" +if $TEST_RUN ; then + echo " Recipient address for test run....: $test_email" +fi +echo "" + +if ! $TEST_RUN ; then + warn "This is \033[1mNOT\033[m a test run. All local mailboxes will receive the user info e-mail." +fi echo "" OK= @@ -127,7 +194,18 @@ while [ "$OK" != "yes" -o "$OK" != "no" ] ; do done [[ $OK = "yes" ]] || fatal "Repeat execution with different parameters." - +echo "" +echononl " Create log directory '$(dirname "$logfile")'.." +if [[ ! -d "$(dirname "$logfile")" ]] ; then + mkdir "$(dirname "$logfile")" + if [[ $? -eq 0 ]]; then + echo_ok + else + echo_failed + fi +else + echo_skipped +fi > $logfile @@ -151,7 +229,14 @@ echo -e "\n\t --- Sending userinfo into all local virtual mailboxes --\n" | tee ## - list of local virtual domains ## - -domains=`su postgres -c"psql -At -F ' ' postfix -c\"SELECT domain FROM domain WHERE domain != 'ALL' ORDER BY domain\""` +if $TEST_RUN ; then + if [[ ! "${test_email}" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]] ; then + fatal "The given recipient address (${test_email}) for test run does not look like a correct e-mail address!" + fi + domains="${test_email##*@}" +else + domains=`su postgres -c"psql -At -F ' ' postfix -c\"SELECT domain FROM domain WHERE domain != 'ALL' ORDER BY domain\""` +fi declare -i num_dom=0; declare -i num_mbox_failed=0; @@ -163,7 +248,11 @@ email_from_regex="$(echo ${email_from//\@/\\@})" for domain in $domains ;do echo -e "\nDOMAIN: $domain\n" | tee -a $logfile - local_parts=`su postgres -c"psql -At -F ' ' postfix -c\"SELECT local_part FROM mailbox WHERE domain = '$domain'\""` + if $TEST_RUN ; then + local_parts="${test_email%%@*}" + else + local_parts=`su postgres -c"psql -At -F ' ' postfix -c\"SELECT local_part FROM mailbox WHERE domain = '$domain'\""` + fi for local_part in $local_parts ; do cp "$user_info_file" "$tmp_dir" perl -i -n -p -e "s/%email_to%/$local_part\@$domain/" "${tmp_dir}/$(basename $user_info_file)" @@ -183,6 +272,10 @@ for domain in $domains ;do num_dom=num_dom+1 done +if ! $TEST_RUN ; then + mv "$user_info_file" "${user_info_file}.SENT.$(date +%Y-%m-%d)" +fi + echo -e "\n\n----- Statistics -----\n\n\tSent mail to $num_mbox mailboxe(s) of $num_dom domain(s)" | tee -a $logfile if [ $num_mbox_failed -gt 0 ];then