#!/bin/bash #DESC Configures OS email relay for sendmail. #HELP usage: updateos config_relay {server_name} {login} {password} {port} {email_from} {y} #HELP #HELP First 4 are required. Email from (5th) can be "None". If it is, the generics table is not built. #HELP The 6th clv anything to not prompt for vars. #isRoot ID=$(/usr/bin/id -u) [ $ID -ne 0 ] && echo "You must be root to run $0." && exit 1 #a. Get Server. [ "$1" == "" ] && echo -n "Enter SMTP Server Name/IP: " && read SERVER || SERVER="$1" [ $"SERVER" == "" ] && echo "Server can't be null" && exit 1 #b. Get Login. [ "$2" == "" ] && echo -n "Enter SMTP Server Login: " && read LOGIN || LOGIN="$2" [ $"LOGIN" == "" ] && echo "Login can't be null" && exit 1 #c. Get SMTP Server Password. [ "$3" == "" ] && echo -n "Enter SMTP Server Password: " && read PASSWD || PASSWD="$3" [ $"PASSWD" == "" ] && echo "Password can't be null" && exit 1 #d. SMTP port. [ "$4" == "" ] && echo -n "Enter Server SMTP port (Enter for none): " && read SMTP_PORT || SMTP_PORT="$4" [ $"SMTP_PORT" == "" ] && echo "SMTP port can't be null" && exit 1 #e. Get Email From Address. [ "$5" == "" ] && echo -n "Enter Server Email From Address (Enter for none): " && read EMAIL_FROM || EMAIL_FROM="$5" [ "$EMAIL_FROM" == "" ] && EMAIL_FROM="None" #f. Be Sure. echo "SERVER: $SERVER" echo "LOGIN: $LOGIN" echo "PASSWORD: $PASSWD" echo "SERVER EMAIL FROM ADDRESS: $EMAIL_FROM" REPLY="$6" [ "$REPLY" == "" ] && echo -n "Are these correct? (y/n):" && read REPLY [ "$REPLY" != "y" ] && exit 1 #1. Remove postfix if there and install and enable sendmail instead. yum repolist [ "$?" != "0" ] && echo "Server needs to be registered with Redhat." && exit 1 yum -y remove postfix yum -y install sendmail sendmail-cf [ "$?" != "0" ] && echo "Unable to install the required packages." && exit 1 chkconfig sendmail on #2. Create mail auth folder. rm -rf /etc/mail/auth mkdir /etc/mail/auth chmod 777 /etc/mail/auth #3. Backup config files just in case. cp -f /etc/mail/sendmail.mc /tmp/sendmail.mc.`date +"%m%d%H%M%S"` cp -f /etc/mail/sendmail.cf /tmp/sendmail.cf.`date +"%m%d%H%M%S"` #4. Create generics table. if [ "$EMAIL_FROM" != "None" ] && [ "`cat /etc/mail/genericstable | grep $EMAIL_FROM`" == "" ] ; then echo "root $EMAIL_FROM">/etc/mail/genericstable echo "root@localhost $EMAIL_FROM">>/etc/mail/genericstable echo "root@`hostname` $EMAIL_FROM">>/etc/mail/genericstable echo "root@`hostname`.teleflora.com $EMAIL_FROM">>/etc/mail/genericstable echo "rti $EMAIL_FROM">>/etc/mail/genericstable echo "rti@localhost $EMAIL_FROM">>/etc/mail/genericstable echo "rti@`hostname` $EMAIL_FROM">>/etc/mail/genericstable echo "rti@`hostname`.teleflora.com $EMAIL_FROM">>/etc/mail/genericstable echo "tfsupport $EMAIL_FROM">>/etc/mail/genericstable echo "tfsupport@localhost $EMAIL_FROM">>/etc/mail/genericstable echo "tfsupport@`hostname` $EMAIL_FROM">>/etc/mail/genericstable echo "tfsupport@`hostname`.teleflora.com $EMAIL_FROM">>/etc/mail/genericstable fi #5. Make changes to sendmail.mc and build new sendmail.cf. cp -f /etc/mail/sendmail.mc /etc/mail/sendmail.mc.new sed -i '/^MAILER/d' /etc/mail/sendmail.mc.new sed -i '/MAILER(cyrusv2)/d' /etc/mail/sendmail.mc.new echo "GENERICS_DOMAIN(\`localhost')dnl">>/etc/mail/sendmail.mc.new echo "GENERICS_DOMAIN(\``hostname`')dnl">>/etc/mail/sendmail.mc.new echo "GENERICS_DOMAIN(\``hostname`.teleflora.com')dnl">>/etc/mail/sendmail.mc.new echo "define(\`RELAY_MAILER_ARGS',\`TCP \$h $SMTP_PORT')dnl">>/etc/mail/sendmail.mc.new echo "define(\`ESMTP_MAILER_ARGS',\`TCP \$h $SMTP_PORT')dnl">>/etc/mail/sendmail.mc.new echo "define(\`SMART_HOST',\`$SERVER')dnl">>/etc/mail/sendmail.mc.new echo "define(\`confAUTH_MECHANISMS',\`EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl">>/etc/mail/sendmail.mc.new echo "FEATURE(authinfo,\`hash /etc/mail/auth/authinfo')dnl">>/etc/mail/sendmail.mc.new echo "FEATURE(\`genericstable')dnl">>/etc/mail/sendmail.mc.new echo "MAILER(smtp)dnl">>/etc/mail/sendmail.mc.new echo "MAILER(procmail)dnl">>/etc/mail/sendmail.mc.new m4 /etc/mail/sendmail.mc.new > /etc/mail/sendmail.cf mv -f /etc/mail/sendmail.mc.new /etc/mail/sendmail.mc #6. Create authentication file and build authentication db. echo "AuthInfo:$SERVER \"U:$LOGIN\" \"P:$PASSWD\" \"M:PLAIN\"">/etc/mail/auth/authinfo makemap hash /etc/mail/auth/authinfo < /etc/mail/auth/authinfo #7. Set permissions on authentication files. chmod 600 /etc/mail/auth/authinfo chmod 700 /etc/mail/auth #8. Restart sendmail. service sendmail restart echo "Done." exit 0