diff --git a/seqs/mailserver.cfg.example b/seqs/mailserver.cfg.example index f5421d9..59b85f6 100644 --- a/seqs/mailserver.cfg.example +++ b/seqs/mailserver.cfg.example @@ -2,20 +2,40 @@ # MAS = Mail Server -MAS_DOMAIN="mydomain.com" -MAS_RELAYHOST= -MAS_RELAYUSER= -MAS_RELAYPASS= +readonly MAS_DOMAIN="mydomain.com" +readonly MAS_RELAYHOST= +readonly MAS_RELAYUSER= +readonly MAS_RELAYPASS= -MAS_DBUSER='pfa' -MAS_DBPASS='pass' -MAS_DBNAME='pfa_db' -MAS_mysql_virtual_domains_maps="user = '\$MAS_DBUSER' -password = '\$MAS_DBPASS' -hosts = localhost -dbname = '\$MAS_DBNAME' -query = SELECT domain FROM domain WHERE domain='%s' AND active = '1' +# Settings for virtualizing mailboxes and domains + +readonly MAS_DBUSER='pfa' +readonly MAS_DBPASS='pass' +readonly MAS_DBNAME='pfa_db' +readonly MAS_DBHOST='localhost' +readonly MAS_VIRTUAL_USER='vmail' +readonly MAS_VIRTUAL_USER_ID='5000' +readonly MAS_VIRTUAL_FOLDER_BASE='/var/vmail' +readonly MAS_VIRTUAL_USER_PART="user=$MAS_DBUSER +password=$MAS_DBPASS +hosts=$MAS_DBHOST +dbname=$MAS_DBNAME" + +MAS_mysql_virtual_domains_maps="query = SELECT domain FROM domain WHERE domain='%s' AND active = '1' #query = SELECT domain FROM domain WHERE domain='%s' #optional query to use when relaying for backup MX #query = SELECT domain FROM domain WHERE domain='%s' AND backupmx = '0' AND active = '1' #expansion_limit = 100" + +MAS_mysql_virtual_mailbox_maps="query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1' +#expansion_limit = 100" + +MAS_mysql_virtual_alias_domain_mailbox_maps="query = SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = CONCAT('%u', '@', alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1'" + +MAS_mysql_virtual_alias_maps="query = SELECT goto FROM alias WHERE address='%s' AND active = '1' +#expansion_limit = 100" + +MAS_mysql_virtual_alias_domain_maps="query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'" + +MAS_mysql_virtual_alias_domain_catchall_maps="# handles catch-all settings of target-domain +query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'" diff --git a/seqs/mailserver.sh b/seqs/mailserver.sh index 53e2b8d..343bed3 100755 --- a/seqs/mailserver.sh +++ b/seqs/mailserver.sh @@ -6,36 +6,34 @@ # MDA = Mail Delivery Agent (dovecot) # MUA = Mail User Agent (Mail program used by the user) -toolName=mailserver -mtaName=postfix -mtaUser=postfix -mtaDeps="$mtaName $mtaName-mysql" -mtaConfLoc="/etc/$mtaName" -mtaMysqlConfLoc="$mtaConfLoc/sql" -mdaName=dovecot -mdaConfLoc="/etc/$mdaName" -mdaConfDir="$mdaConfLoc/conf.d" -mdaDeps="dovecot-imapd dovecot-lmtpd dovecot-mysql dovecot-managesieved dovecot-sieve" +readonly toolName=mailserver +readonly mtaName=postfix +readonly mtaUser=postfix +readonly mtaDeps="$mtaName $mtaName-mysql" +readonly mtaConfLoc="/etc/$mtaName" +readonly mtaMysqlConfLoc="$mtaConfLoc/sql" +readonly mdaName=dovecot +readonly mdaConfLoc="/etc/$mdaName" +readonly mdaConfDir="$mdaConfLoc/conf.d" +readonly mdaDeps="dovecot-imapd dovecot-lmtpd dovecot-mysql dovecot-managesieved dovecot-sieve" -# Get script working directory -# (when called from a different directory) -WDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >>/dev/null 2>&1 && pwd )" -CONFIG=0 -CONFIG_FILE_NAME="${toolName}.cfg" -CONFIG_FILE_TEMPLATE="$WDIR/${CONFIG_FILE_NAME}.example" +sq_aptOpt= +sq_config=0 seq_config() { - if [ $(id -u) -ne 0 ] ; then - endReturn -o 1 "No root" - fi - initSeqConfig "$CONFIG_FILE_NAME" "$CONFIG_FILE_TEMPLATE" - if [ $? -eq 0 ] ; then - CONFIG=1 + root || endReturn -o 1 "No root" + + #initSeqConfig "$CONFIG_FILE_NAME" "$CONFIG_FILE_TEMPLATE" + if initSeqConfig "${seq_configName:?}" "${seq_configTemplate:?}" ; then + sq_config=1 info " Domain: ${MAS_DOMAIN:-}" - elif [ $? -eq 1 ] ; then - # Config $CONFIG_FILE_NAME created. Needs modification first - dry || return -1 + else + dry || return 1 fi + + ## Apt cmdline option to suppress user interaction + interactive || sq_aptOpt="-y" + return 0 } @@ -47,21 +45,16 @@ step_1() { step_2_info() { echo "Install $mtaName"; } step_2() { - local aptOpt= - if quiet;then - aptOpt="-y" - else - read -p "In the following dialog chose \"Internet site\" and enter $MAS_DOMAIN as your domain. Enter to continue..." - fi - exe apt install $mtaDeps $aptOpt + confirm -n -y "In the following dialog chose \"Internet site\" and enter $MAS_DOMAIN as your domain. Enter to continue..." + exe apt install $mtaDeps ${sq_aptOpt:-} } step_3_info() { echo "Enable $mtaName"; } step_3() { exe systemctl enable $mtaName - echo -e " [I] Printing $mtaName status\n" + info "Printing $mtaName status" exe service $mtaName status - echo -e "\n [I] Installed postfix version: $(postconf mail_version)" + info "Installed postfix version: $(postconf mail_version)" } step_4_info() { echo "$mtaName basic domain configuration"; } @@ -74,17 +67,18 @@ step_4() { step_5_info() { echo "$mtaName enable submission service"; } step_5() { - info -e " [I] Copy following lines...\n" + local mtaConfSubmission="${seq_origin:?}/$toolName/submissionService" + local mtaConfSmtps="${seq_origin:?}/$toolName/smtpsService" + + info "Copy following lines..." exe cat "$mtaConfSubmission" - exe read -rep $'\nPress Enter to open the '$mtaConfLoc'/master.cf' - exe vi $mtaConfLoc/master.cf - exe info + confirm -n -y "Press Enter to open the $mtaConfLoc/master.cf" + editor $mtaConfLoc/master.cf + info exe cat "$mtaConfSmtps" - exe read -rep $'\nPress Enter to open the '$mtaConfLoc'/master.cf' - exe vi $mtaConfLoc/master.cf + confirm -n -y "Press Enter to open the $mtaConfLoc/master.cf" + editor $mtaConfLoc/master.cf } -mtaConfSubmission="$WDIR/$toolName/submissionService" -mtaConfSmtps="$WDIR/$toolName/smtpsService" step_6_info() { echo "Configure TLS"; } step_6() { @@ -111,8 +105,8 @@ step_7() { step_8_info() { echo "Install $mdaName"; } step_8() { - exe apt install $mdaDeps - info -e "\n [I] Installed version: $(dovecot --version)" + exe apt install $mdaDeps ${sq_aptOpt} + info "Installed version: $(dovecot --version)" } step_9_info() { @@ -207,18 +201,11 @@ step_20_info() { } step_20_alias() { echo "virtual"; } step_20() { - local qOpt= - if quiet ; then - qOpt="-q" - fi - exe $WDIR/postfixadmin.sh ${qOpt} install + exe ${seq_origin:?}/postfixadmin.sh ${sqr_args} install } step_21_info() { echo "Create $mtaName mysql query files"; } step_21() { - # eval needed to expand sourced configuration variables - local localMysqlUser=`eval "echo \"$MAS_VIRTUAL_USER_PART\""` - exe mkdir -p "$mtaMysqlConfLoc" local mtaFile @@ -232,11 +219,10 @@ step_21() { "mysql_virtual_alias_domain_catchall_maps"\ ) - for mtaFile in ${mtaMysqlFiles[@]} - do - eval 'mtaVar=$MAS_'${mtaFile} + for mtaFile in ${mtaMysqlFiles[@]} ; do + mtaVar="MAS_${mtaFile}" info "creating ${mtaFile}.cf" - exe echo -e "$localMysqlUser\n$mtaVar" > "$mtaMysqlConfLoc/${mtaFile}.cf" + exep echo -e "${MAS_VIRTUAL_USER_PART:?}\n${!mtaVar:?}" \> "$mtaMysqlConfLoc/${mtaFile}.cf" done exe chown -R root:${mtaUser} "$mtaMysqlConfLoc" @@ -273,65 +259,73 @@ step_23() { step_24_info() { echo "$mdaName virtualisation configuration instructions"; } step_24() { - echo "# Configuring Mailbox Location" - echo " [/etc/dovecot/conf.d/10-mail.conf]" - echo " mail_location = maildir:~/Maildir" - echo " mail_home = ${MAS_VIRTUAL_FOLDER_BASE}/%d/%n" - echo - echo "# Configure authentication" - echo " [/etc/dovecot/conf.d/10-auth.conf]" - echo " # Username with domain" - echo " auth_username_format = %u" - echo " # Find and uncomment following line" - echo " !include auth-sql.conf.ext" - echo " # Comment following line to prevent local users from sending mail" - echo " # without having registered an email address" - echo " #!include auth-system.conf.ext" - echo " # Debug login issues in /var/log/maillog by adding:" - echo " auth_debug = yes" - echo " auth_debug_passwords = yes" - echo - echo "# Adding mysql login information" - echo " [/etc/dovecot/dovecot-sql.conf.ext]" - echo " driver = mysql" - echo " connect = host=$MAS_DBHOST dbname=$MAS_DBNAME user=$MAS_DBUSER password='${MAS_DBPASS}'" - echo " default_pass_scheme = MD5" - echo " password_query = SELECT username AS user,password FROM mailbox WHERE username = '%u' AND active='1'" - echo " user_query = SELECT maildir, $MAS_VIRTUAL_USER_ID AS uid, $MAS_VIRTUAL_USER_ID AS gid FROM mailbox WHERE username = '%u' AND active='1'" - echo " iterate_query = SELECT username AS user FROM mailbox" + color green + cat < \"$mtaClientAccessLoc\"" - echo " [I] Don't forget to add the following" - echo " [$mtaConfLoc/main.cf]" - echo " smtpd_relay_restrictions =" - echo " check_client_access hash:$mtaClientAccessLoc" + info "Generating $mtaClientAccessLoc" + exep echo "# myhost.lan OK" \> "$mtaClientAccessLoc" + info "Don't forget to add the following" + info -a "[$mtaConfLoc/main.cf]" + info -a " smtpd_relay_restrictions =" + info -a " check_client_access hash:$mtaClientAccessLoc" fi info "Updating $mtaClientAccessLoc" exe postmap "$mtaClientAccessLoc" @@ -375,10 +369,10 @@ step_54() { if [ ! -f "$mtaRecipientAccessLoc" ] ; then info "Generating $mtaRecipientAccessLoc" exep "echo \"# unwanted@${MAS_DOMAIN} 550 No mailbox. Nothing to see here.\" > \"$mtaRecipientAccessLoc\"" - echo " [I] Don't forget to add the following" - echo " [$mtaConfLoc/main.cf]" - echo " smtpd_recipient_restrictions =" - echo " check_recipient_access hash:$mtaRecipientAccessLoc" + info "Don't forget to add the following" + info -a "[$mtaConfLoc/main.cf]" + info -a " smtpd_recipient_restrictions =" + info -a " check_recipient_access hash:$mtaRecipientAccessLoc" fi info "Updating $mtaRecipientAccessLoc" exe postmap "$mtaRecipientAccessLoc" @@ -397,10 +391,10 @@ step_56() { exe postconf -e "smtp_sasl_mechanism_filter = plain" exe postconf -e "smtp_tls_security_level = encrypt" exe postconf -e "smtp_tls_mandatory_ciphers = high" - echo " [I] Don't forget to add credentials for the new relay" - echo " [$saslPassFile]" - echo " user@extern.com username:passwort" - echo " postmap $saslPassFile" + info "Don't forget to add credentials for the new relay" + info -a "[$saslPassFile]" + info -a " user@extern.com username:passwort" + info -a "postmap $saslPassFile" fi info "Updating $mtaSenderRelayLoc" exe postmap "$mtaSenderRelayLoc" @@ -423,21 +417,17 @@ step_100() { -f) shift fromAdr="-f ${1:-} " - shift - ;; + shift ;; -u) shift asUser="sudo -u ${1:-} " - shift - ;; + shift ;; *) - break - ;; + break ;; esac done - if [ -n "${1:-}" ] ; then - toAdr="$1" - fi + [ -n "${1:-}" ] && toAdr="$1" + exe ${asUser}sh -c "echo \"Subject: Test from Postfix\nIt is \$(date)\n\nGreetings \$(whoami)\" | sendmail ${fromAdr}$toAdr" } @@ -455,11 +445,11 @@ step_104_alias() { echo "delqueue"; } step_104() { shift local msgId="ALL" - if [ -n "${1:-}" ] ; then - msgId="$1" - fi + [ -n "${1:-}" ] && msgId="$1" exe postsuper -d "$msgId" } +# shellcheck disable=SC2034 # Appears unused readonly sqr_minVersion=16 +# shellcheck disable=SC1091 # Don't follow this source . /usr/local/bin/sequencer.sh