#!/bin/sh
#
#    Copyright (c) 1996-2007 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#      Script to configure Relay Host for sendmail to work without DNS 
#

SENDMAIL_CF=/etc/mail/sendmail.cf
SENDMAIL_TEMP=/etc/mail/sendmail.tmp
SUBMIT_CF=/etc/mail/submit.cf
SUBMIT_TMP=/etc/mail/submit.tmp
HOSTS_FILE=/etc/hosts
RESOLVCONF=/etc/resolv.conf
DS_PATTERN="DS"
cmd_option=`echo $1`
case "$cmd_option" in
3)
        #Remove the current IP from sendmail.cf
        ip1=`/bin/grep ^DS $SENDMAIL_CF | /usr/bin/cut -d [ -f 2`
        ip2=`echo $ip1 | /usr/bin/cut -d ] -f 1`

        echo "Removing $ip2 as Relay Host.."
        /bin/sed -e "s/^DS.*/DS/" < $SENDMAIL_CF > $SENDMAIL_TEMP
        /bin/mv $SENDMAIL_TEMP $SENDMAIL_CF

		if [ -f "$SUBMIT_CF" ]; then
			/bin/sed -e "s/^DS.*/DS/" $SUBMIT_CF > $SUBMIT_TMP
			/bin/cp $SUBMIT_TMP $SUBMIT_CF
		fi
        #Remove the IP entry from /etc/hosts
        /bin/grep -v $ip2 $HOSTS_FILE > $HOSTS_FILE.temp
        /bin/mv $HOSTS_FILE.temp $HOSTS_FILE
	/bin/chmod 644 $HOSTS_FILE
        ;;
*)
        ch=$1
        case "$ch" in

        5)
                #Get the relay host IP from sendmail.cf
                ip1=`/bin/grep ^DS $SENDMAIL_CF | /usr/bin/cut -d [ -f 2`
                ip2=`echo $ip1 | /usr/bin/cut -d ] -f 1`
                if [ "$ip2" = "$DS_PATTERN" ]
                then
                        echo
                else
						entry=`/bin/grep "$ip2" $HOSTS_FILE`
						if [ "1$entry" = "1" ]
						then
							echo "$ip2"
						else
							echo "$entry"
						fi
                fi
                ;;
        1)
                #Get the relay host IP from sendmail.cf
                ip1=`/bin/grep ^DS $SENDMAIL_CF | /usr/bin/cut -d [ -f 2`
                ip2=`echo $ip1 | /usr/bin/cut -d ] -f 1`
                if [ "$ip2" = "$DS_PATTERN" ]
                then
                        echo "Relay Host IP is not set"
                        echo
                else
                        echo "Relay Host IP is: $ip2"
                        echo
                fi
                ;;

        2)
                highSecurityModeFlag=`/fabos/cliexec/config get security.policymode 2` ;
                if [ $highSecurityModeFlag -eq 1 ]; then
                        echo " SMTP service is disabled in SIOM with High Security Mode. Cannot Set Relay Host IP"
                        exit 1
                fi
#                echo "enter the Relay Host IP:"
#                read relay_host
				relay_host=$2
                if [ -z "$relay_host" ]  #Is Null?
                then
                        echo "Relay Host IP not set. Exitting"
                        exit 1
                fi
                echo "Setting $relay_host as Relay Host.."

                #Put the given IP in the DS field of sendmail.cf
                /bin/sed -e "s/^DS.*/DS\[$relay_host\]/" < $SENDMAIL_CF > $SENDMAIL_TEMP
                /bin/mv $SENDMAIL_TEMP $SENDMAIL_CF

				#If the sendmail package has submit.cf, change the DS file.
				if [ -f "$SUBMIT_CF" ]; then
					/bin/sed -e "s/^DS.*/DS[$relay_host]/" $SUBMIT_CF > $SUBMIT_TMP
					/bin/mv $SUBMIT_TMP $SUBMIT_CF
				fi

#                echo "enter the Domain Name:"
#                read domain
				domain=$3
                if [ -z "$domain" ]  #Is Null?
                then
                        echo "Domain Name not set. Exitting"    
                        exit 1
                fi

                #Make an entry of relay host and domain name in /etc/hosts
				/bin/grep -v '#Maps_mail_cfg' $HOSTS_FILE > $HOSTS_FILE.temp
				echo -e "$relay_host             $domain            #Maps_mail_cfg">> $HOSTS_FILE.temp
                /bin/mv $HOSTS_FILE.temp $HOSTS_FILE
		/bin/chmod 644 $HOSTS_FILE
                ;;

        3)
                #Remove the current IP from sendmail.cf
                ip1=`/bin/grep ^DS $SENDMAIL_CF | /usr/bin/cut -d [ -f 2`
                ip2=`echo $ip1 | /usr/bin/cut -d ] -f 1`

                echo "Removing $ip2 as Relay Host.."
                /bin/sed -e "s/^DS.*/DS/" < $SENDMAIL_CF > $SENDMAIL_TEMP
                /bin/mv $SENDMAIL_TEMP $SENDMAIL_CF

				if [ -f "$SUBMIT_CF" ]; then
					/bin/sed -e "s/^DS.*/DS/" $SUBMIT_CF > $SUBMIT_TMP
					/bin/cp $SUBMIT_TMP $SUBMIT_CF
				fi
                #Remove the IP entry from /etc/hosts
				/bin/grep -v '#Maps_mail_cfg' $HOSTS_FILE > $HOSTS_FILE.temp
                /bin/mv $HOSTS_FILE.temp $HOSTS_FILE
		/bin/chmod 644 $HOSTS_FILE
                ;;

        4)
                exit 0
                ;;
        *)
                echo "integer must be between 1 and 4 - please re-enter"
                ;;
        esac
;;
esac
