#! /bin/ksh
#
# ident   "@(#)nsmail_sync.sh 1.15     01/03/28 SMI"
#
# nsmail_sync - NSMAIL synchronization command
#
# The purpose of this command is to execute on all nodes whenever the
# row corresponding to the LOGHOST_CM format is updated in the
# CCD. Whenever a row of this format is removed, the corresponding
# logical host along with its associated data services is shut down on
# the master node of the logical host. When a row of this format is
# added to the CCD, the corresponding logical host and its associated
# data services is brought up on the current master.
#
# The arguments obtained by this command are:
#
# <keyword> - can be one of CCDSYNC_PRE_ADDU, CCDSYNC_POST_ADDU,
#               CCDSYNC_PRE_REMOVE, CCDSYNC_POST_REMOVE
#
# <row>     - the row being added/removed
#
# <ccdfile> - the full pathname of the dynamic CCD file. This is
#         required because the dynamic CCD cannot be queried in
#         the conventional manner from this command. It has to be
#         queried as a static database.
#
# <numnodes> - integer identifying the number of nodes in the static
#          cluster configuration.
#
# <curr_members> - list of node ids identifying the current cluster
#          membership.
#
# <localnodeid> - node id of this node.
 

# DEBUG
#DEBUG=0
#set -x

ID=/usr/bin/id
RCP="/usr/bin/rcp -p"
RSH=/usr/bin/rsh 
PS=/bin/ps

TMPFILE=/var/opt/SUNWcluster/run/nsmail.privsetup.$$

PROG="nsmail `basename $0`"


ETCSKEY=/etc/skeykeys
BAKSKEY=/etc/skeykeys.`date '+%H.%M_%b%d%y'`
POSTSKEY=

HANSM=scnsmail

# Netscape Mail config file
NSM_CONF=

# Netscape Mail daemon
#NSM_PROG=NscpMail
#NSM_PROG=NscpMsg
LHOST=

MASTER=
MASTERPRIV=
NONMASTERS=
NONMASTERSPRIV=

MAILHOST=
MAILHOSTPRIV=
NONMAILHOSTS=
NONMAILHOSTSPRIV=


MYHOST=
MYHOSTPRIV=
NOTMYHOST=
NOTMYHOSTPRIV=

MYPHYSHOST=
SIBPHYSHOST=
SIBPRIVLINK=
NOTMASTEREDHOST=
MTAUID=
MTAGID=
ISMS4=1

IS_SKEY=

STARTTAG="Start of lines added by Sun Cluster HA-NSM"
ENDTAG="End of lines added by Sun Cluster HA-NSM"

. ds_utilities


setNSM_PROG()
{
	#set -x	
	. ds_utilities
	NSM_CONF=$(get_val_for_key ${NSMAIL_FORMAT} ${row} CONF_FILE)
	if [ "${NSM_CONF}" = "/etc/netscape.mail.conf" ]; then
		NSM_PROG=NscpMail
	else
		NSM_PROG=NscpMsg
	fi
}

isnsms4()
{
	#set -x
	if [ "${NSM_PROG}" = "NscpMsg" ]; then
		print 1
	else
		print 0
	fi
	return 0
}


# DEBUG
echod()
{

	if [ $DEBUG -eq 1 ]; then
		echo "$1"
	fi
}

################################################################################
#
# message printing utilities
#
################################################################################
msgt()
{
	echo "\t$*"
}

msg()
{
	echo  "$PROG: $*"
}

errmsg()
{
        typeset prefix

    prefix="SUNWcluster.ha.nsmail.sync.$1"
    shift
    log_error $prefix "$*"
}

################################################################################
#
# cleanup [exitstatus]
#
################################################################################
cleanup()
{

	rm -f $TMPFILE >/dev/null 2>&1

	if [ "$1" -a "$1" != "0" ]; then
		if [ "$2" ]; then
			msg `gettext  "$2"`
		fi
		msg `gettext "Did NOT complete successfully!"`
		errmsg "4016" `gettext "Did NOT complete successfully! : $2"`
	fi

	if [ "$1" ]; then
		exit $1
	else
		exit 0
	fi

	return 0
}

################################################################################
#
# remcopy <file> <src_host> <dest_host_list>
#
# Distribute a file from a source host to a list of destination hosts.
# 
################################################################################
remcopy()
{
	file=$1
	src_host=$2
	dest_hosts=$3

	for host in $dest_hosts; do
		#
		# Work around the fact specifying our local host name in a
		# rsh/rcp command will cause a permission error.
		#
		if [ "$host" = "$MYHOSTPRIV" ]; then
			host_str=
		else
			host_str="$host:"
		fi
	
		if [ "$src_host" = "$MYHOSTPRIV" ]; then
			src_host_str=
		else
			src_host_str="$src_host:"
		fi
		$RCP ${src_host_str}${file} ${host_str}${file}
		if [ $? -ne 0 ]; then
			errmsg "4001"  `gettext \
"Failed to copy file \"$file\" from \"$src_host\" to \"$host\""`
			return 1
		fi 
	done
	return 0
}


################################################################################
#
# hupit <prog>
#
# Send a HUP signal to <prog>.
# Note: uses ps -e to get pid.
#
################################################################################
hupit()
{
	hupprog=$1

	huppid=`ps -e|grep $hupprog 2>/dev/null`
	if [ -z "$huppid" ]; then
		errmsg "4002" `gettext "Failed to get process id of \"$hupprog\" on \"$huphost\""`
		return 1
	fi
	huppid=`echo $huppid | awk '{print $1}'`
	kill -HUP $huppid 2>/dev/null
	return $?
}

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

################################################################################
#
# initvars
#
# Initialize some variables
#
################################################################################
function initvars
{

#set -x

	inst_name=$(get_val_for_key ${NSMAIL_FORMAT} ${row} NAME)	

	conf_row=$(get NSMAIL4_CONF ${inst_name})
	# remove the leading NSMAIL4_CONF field in the CCD row
	conf_row=${conf_row#*:}

	LHOST=$(get_val_for_key ${NSMAIL_FORMAT} ${row} LOGICAL_HOST)
	#print "LHOST = ${LHOST}"
	#NSM_CONF=$(get_val_for_key ${NSMAIL_FORMAT} ${row} CONF4_FILE)
	NSM_CONF=$(get_val_for_key ${NSMAIL_FORMAT} ${row} CONF_FILE)

	#
	# Get the local host primary and private names.
	# We pick the first private name haget gives us.
	#
	MYHOST=`uname -n`
	set `haget -f private_links -h $MYHOST`
	MYHOSTPRIV=$1

	MASTER=`haget -f master -h $LHOST`
	if [ -z "$MASTER" ]; then
		errmsg "4003" `gettext "There is no current master for logical host $LHOST"`
		return 1
	fi
	set `haget -f private_links -h $MASTER`
	MASTERPRIV=$1

	#
	# Get a list of all potential masters, excluding the current master.
	# From this list we create an equivalent list of private link names.
	#
	NONMASTERS=`haget -f physical_hosts -h $LHOST | grep -v \^$MASTER\$`
	for i in $NONMASTERS ; do
		set `haget -f private_links -h $i`
		NONMASTERSPRIV="$NONMASTERSPRIV $1"
	done

		
	#
	# if service not registered (which is ok with us), svc_on will be empty, 
	# so we treat is as  as a string.
	#
	svc_on=`haget -f service_is_on -s $service 2>/dev/null`
	if [ "$svc_on" = "1" ]; then
		errmsg "4005" `gettext "Service \"$service\" is in \"on\" state. Aborting."`
		return 1
	fi

	return 0
}

################################################################################
#
# confirm
#
# Confirm with the user that he wants us to do the work.
#
################################################################################ 
confirm()
{
	msgt `gettext "This utility is about to replicate the changes to the private"`
	msgt `gettext "file systems that were done on host \"$MASTER\" by the Netscape Mail"`
	msgt `gettext "Server installation software.  It assumes that host \"$MASTER\", which"`
	msgt `gettext "is the current master of logical host \"$LHOST\", is the host that the"`
	msgt `gettext "Netscape installation software was run on."`
	#msgt `gettext "  If this is not the case, or"`
	#msgt `gettext "you just don't want to run this utility, answer \"n\" to the prompt."`
	#msgt `gettext "Otherwise, press <CR> or \"y\" to continue."`
	echo

	#prompt=`gettext "Do you want to continue (y|n) [y] ?"`
	#res=`ckyorn -d y -p "$prompt" `
	res=y
	case $res in
	y)
		#
		# We will use through the rest of the program the MAIL*
		# variables when we want to refer to the host that Netscape Mail
		# was originally installed on, so we can replicated the installation
		# in the other potential masters.
		# The distinction between the current master and the mail host
		# will allow us in the future to support running this program
		# even when the logical host was moved from the original installation
		# host.
		#
		MAILHOST=$MASTER
		MAILHOSTPRIV=$MASTERPRIV
		NONMAILHOSTS=$NONMASTER
		NONMAILHOSTSPRIV=$NONMASTERSPRIV
		return 0
		;;
	n|q)
		msg `gettext "Quiting."`
		return 1
		;;
	esac

}

#
# export_conf_file
#
# export /etc/nsserver.cfg file
#
export_conf_file()
{
        typeset rows i str

        rows=$(get_all NSMAIL4_CONF)

        if [[ -n "${rows}" ]]; then
                for r in ${rows}; do
                        r="`echo ${r} | tr ':' ' '`"
                        let i=0
                        for str in ${r}; do
                                if [ i -eq 2 ]; then
                                        variable_name=${str}
                                elif [ i -eq 3 ]; then
                                        variable_val=${str}
                                fi
                                let i=i+1
                        done
                        export ${variable_name}=${variable_val}
                done
        fi
        return 0

}

#################################################################################
# cpmailconf()
#
# Copy from the mail host the netscape.mail.conf config file to other hosts
# and source it in.
#
# Sourcing this file adds the following variables:
# DomainName, PostOffice, ProgramDir, MailboxDir, MailUserName, NewUserForms
#
################################################################################
function cpmailconf
{
#set -x
	typeset rows i str

	rows=$(get_all NSMAIL4_CONF)

	if [[ -n "${rows}" ]]; then
		for r in ${rows}; do
			r="`echo ${r} | tr ':' ' '`"
			let i=0
			for str in ${r}; do
				if [ i -eq 2 ]; then
					variable_name=${str}
				elif [ i -eq 3 ]; then
					variable_val=${str}
				fi
				let i=i+1
			done
			if [[ ${ISMS4} = 0 ]]; then
				if [ ${variable_name} = "AccountURL" ]; then
					variable_val=`echo ${variable_val} | sed 's/\}/\:/g'`
				fi
			fi
			export ${variable_name}=${variable_val}
		done
	fi
	return 0
}

################################################################################
#
# cpmailconf_35()
#
# Copy from the mail host the netscape.mail.conf config file to other hosts
# and source it in.
#
# Sourcing this file adds the following variables:
# DomainName, PostOffice, ProgramDir, MailboxDir, MailUserName, NewUserForms
#
################################################################################
function cpmailconf_35
{
#set -x
	typeset rows i str

	rows=$(get_all NSMAIL3_CONF)

	if [[ -n "${rows}" ]]; then
		for r in ${rows}; do
			r="`echo ${r} | tr ':' ' '`"
			let i=0
			for str in ${r}; do
				if [ i -eq 2 ]; then
					variable_name=${str}
				elif [ i -eq 3 ]; then
					variable_val=${str}
				fi
				let i=i+1
			done
			if [ ${variable_name} = "AccountURL" ]; then
				variable_val=`echo ${variable_val} | sed 's/\}/\
:/g'`
			fi
			export ${variable_name}=${variable_val}
		done
	fi
	return 0
}

function cpmailconf_nonmh
{

#set -x
	cpmailconf

	typeset rows i str

	#if [[ -f ${NSM_CONF} ]]; then
	#	rm -f ${NSM_CONF}
	#fi

	if [[ ! -f "/etc/nsserver.cfg" ]]; then
		touch ${NSM_CONF}
	fi
	#echo "# Please do not edit this file" >> ${NSM_CONF}

	rows=$(get_all NSMAIL4_CONF)

	if [[ -n "${rows}" ]]; then
		for r in ${rows}; do
			r="`echo ${r} | tr ':' ' '`"
			let i=0
			for str in ${r}; do
				if [ i -eq 2 ]; then
					variable_name=${str}
				elif [ i -eq 3 ]; then
					variable_val=${str}
				fi
				let i=i+1
			done

			export ${variable_name}=${variable_val}

			# copy only needed variables into ${NSM_CONF}
			if [ ${variable_name} = "SERVERROOT" ]; then
				echo ${variable_val} >> /etc/nsserver.cfg
			fi
		done
	fi

	msg `gettext "Copied $NSM_CONF"`
#	. $NSM_CONF
	#if [ $? -ne 0 ]; then
		#errmsg "4006" `gettext "Error while sourcing $NSM_CONF"`
		#return 1
	#fi
       
	return 0
}

function cpmailconf_nonmh_35
{

#set -x
        cpmailconf_35

        typeset rows i str

        if [[ -f ${NSM_CONF} ]]; then
                rm -f ${NSM_CONF}
        fi

        touch ${NSM_CONF}
        echo "# Please do not edit this file" >> ${NSM_CONF}

        rows=$(get_all NSMAIL3_CONF)

        if [[ -n "${rows}" ]]; then
                for r in ${rows}; do
                        r="`echo ${r} | tr ':' ' '`"
                        let i=0
                        for str in ${r}; do
                                if [ i -eq 2 ]; then
                                        variable_name=${str}
                                elif [ i -eq 3 ]; then
                                        variable_val=${str}
                                fi
                                let i=i+1
                        done
                        if [ ${variable_name} = "AccountURL" ]; then
                                variable_val=`echo ${variable_val} | sed 's/\}/\:/g'`
                        fi

                        # copy only needed variables into ${NSM_CONF}
                        if [ ${variable_name} != "MTAUID" -a \
                             ${variable_name} != "MTAGID" -a \
                             ${variable_name} != "LHLIBMATCH" -a \
                             ${variable_name} != "mh_finger" ]; then
                                echo "${variable_name}=${variable_val}" >> ${NSM_CONF}
                        fi
                done
        fi

        msg `gettext "Copied $NSM_CONF"`
        . $NSM_CONF
        if [ $? -ne 0 ]; then
                errmsg "4006" `gettext "Error while sourcing $NSM_CONF"`
                return 1
        fi
       
        return 0

}

################################################################################
#
# vrfyids
# 
# Verify that the uid and gid of the mail system, as specified in 
# netscape.mail.conf, exist on all hosts and are the same as on the mail host.
#  
################################################################################
vrfyids() 
{

		temp=`$ID ${MAILSRV} 2>/dev/null`
		if [ $? -ne 0 ]; then
			errmsg "4007" `gettext "User \"$MAILSRV\" does not exist on host \"$MYHOST\""`
			return 1
		fi
		temp2=`echo $temp | sed 's/uid=\([0-9]*\).*/\1/'`
		if [ $MTAUID -ne $temp2 ]; then
			errmsg "4008" `gettext \
"The user id of user \"$MAILSRV\" on host \"$MYHOST\"
does not match the one on the mail server \"$MAILSERVER\""`
			return 1
		fi
		temp2=`echo $temp | sed 's/.*gid=\([0-9]*\).*/\1/'`
		if [ $MTAGID -ne $temp2 ]; then
			errmsg "4009" `gettext \
"The group id of user \"$MAILSRV\" on host \"$MYHOST\"
does not match the one on the mail server \"$MAILSERVER\""`
			return 1
		fi

	msg `gettext  \
"Verified that user \"$MAILSRV\" has uid $MTAUID and gid 
$MTAGID set cluster-wide"`

	return 0
}

vrfyids_35() 
{

                temp=`$ID $MailUserName 2>/dev/null`
                if [ $? -ne 0 ]; then
                        errmsg "4017" `gettext "User \"$MailUserName\" does not exist on host \"$MYHOST\""`
                        return 1
                fi
                temp2=`echo $temp | sed 's/uid=\([0-9]*\).*/\1/'`
                if [ $MTAUID -ne $temp2 ]; then
                        errmsg "4018" `gettext \
"The user id of user \"$MailUserName\" on host \"$MYHOST\"
does not match the one on the mail server \"$MAILSERVER\""`
                        return 1
                fi
                temp2=`echo $temp | sed 's/.*gid=\([0-9]*\).*/\1/'`
                if [ $MTAGID -ne $temp2 ]; then
                        errmsg "4019" `gettext \
"The group id of user \"$MailUserName\" on host \"$MYHOST\"
does not match the one on the mail server \"$MAILSERVER\""`
                        return 1
                fi

        msg `gettext  \
"Verified that user \"$MailUserName\" has uid $MTAUID and gid 
$MTAGID set cluster-wide"`

        return 0
}


################################################################################
#
# stopnsmail
#
# stop Nestscape Mail if it is running
#
################################################################################
stopnsmail()
{
export_conf_file
PID=`${PS} -u ${MAILSRV} | grep -v PID | awk '{print $1}'`
for i in $PID
do
        echo $i
        kill -9 $i
done
}

stopnsmail_old()
{
		nsmail_pid=`ps -e|grep $NSM_PROG 2>/dev/null`
		if [ ! -z "$nsmail_pid" ]; then
			nsmail_pid=`echo $nsmail_pid | awk '{print $1}'`
			kill $nsmail_pid 2>/dev/null
			msg `gettext "Shutdown existing Netscape Mail process on \"$host\""`
		fi
}

stopnsmail_35()
{
                nsmail_pid=`ps -e|grep $NSM_PROG 2>/dev/null`
                if [ ! -z "$nsmail_pid" ]; then
                        nsmail_pid=`echo $nsmail_pid | awk '{print $1}'`
                        kill $nsmail_pid 2>/dev/null
                        msg `gettext "Shutdown existing Netscape Mail process on \"$host\""`
                fi
}


################################################################################
#
# skeys
#
# Set up the file /etc/skeykeys on all machines to be a symbolic link
# to a file in the multi-host disk. We will put it in our HA subdir of the $Postffice
# directory.
#
################################################################################
skeys_mh() 
{
#set -x
	print "skeys_mh()"

	ls $ETCSKEY >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		#
		# It turns our that Netscape Mail 3.0 does not install
		# this file, so if we don't find it, we bail out
		# of this routine.
		#
		IS_SKEY=0
		return 0
	fi

	IS_SKEY=1
	test  -d $PostOffice/$HANSM  
	if [ $? -ne 0 ]; then
		# directory does not exist, create it
		mkdir $PostOffice/$HANSM || return 1
	fi

	test -h $ETCSKEY
	if [ $? -eq 0 ]; then
		#
		# The file in /etc is a symlink. Could be from a previous run.
		# We'll remove it at re-link just in case something changed.
		# If the file on the multi-host disk does not exist, create it.
		#
		eval "test -f $POSTSKEY || touch $POSTSKEY; rm $ETCSKEY;  \
			ln -s $POSTSKEY $ETCSKEY" || return 1
	else
		#
		# The file in /etc is a regular file. Back it up and make a symlink to
		# the file in the multi-host disk. If the later file does not exist,
		# make a copy from the file in /etc.
		#
		eval "test -f $POSTSKEY || cp $ETCSKEY $POSTSKEY; mv $ETCSKEY $BAKSKEY;  \
			ln -s $POSTSKEY $ETCSKEY" || return 1
	fi

	msg `gettext "Linked $ETCSKEY to $POSTSKEY"`

	return 0
		
}

skeys_nonmh()
{
		print "skeys_nonmh()"

		ls $ETCSKEY >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			IS_SKEY=0
			ln -s $POSTSKEY $ETCSKEY || return 1
		else
			IS_SKEY=1
			test -h $ETCSKEY
			if [ $? -eq 0 ]; then
				eval "rm $ETCSKEY; ln -s $POSTSKEY $ETCSKEY" || return 1
			else
				eval "mv $ETCSKEY $BAKSKEY; ln -s $POSTSKEY $ETCSKEY" || return 1
			fi
		fi

	msg `gettext "Linked $ETCSKEY to $POSTSKEY"`

	return 0
}

################################################################################
#
# libs
#
# Make symbolic links from /usr/lib to the Nescape libraries
#
################################################################################
libs()
{

#set -x
	# To add a new library, just add a variable that it's value is the
        # library path in /usr/lib, a corresponding LH_* empty variable,
        # and update the variable LIBVARS with the varibale name.
        #
	LIBMAIL=/usr/lib/libNSmail.so; LH_LIBMAIL=${ProgramDir}/lib/libNSmail.so
	LIBFILTER=/usr/lib/libNSfilter.so; LH_LIBFILTER=${ProgramDir}/lib/filter.so
	#LIBMATCH=/usr/lib/libNSmatch.so; LH_LIBMATCH=${ProgramDir}/lib/nislook.so
	LIBMATCH=/usr/lib/libNSmatch.so; LH_LIBMATCH=${LHLIBMATCH}
	LIBCACHE=/usr/lib/liblcache10.so; LH_LIBCACHE=${ServerRoot}/lib/liblcache10.so
	LIBCACHE3=/usr/lib/liblcache30.so; LH_LIBCACHE3=${ServerRoot}/lib/liblcache30.so
	LIBLDAP=/usr/lib/libldap10.so; LH_LIBLDAP=${ServerRoot}/lib/libldap10.so
	LIBLDAP3=/usr/lib/libldap30.so; LH_LIBLDAP3=${ServerRoot}/lib/libldap30.so

	LIBVARS="LIBMAIL LIBFILTER LIBMATCH LIBCACHE LIBLDAP LIBCACHE3 LIBLDAP3"

	DATESTR=`date '+%H.%M_%b%d%y'`

        for libvar in $LIBVARS ; do
                        eval "lib=\$$libvar"
                        eval "lhlib=\$LH_${libvar}"
                        if [ -z "$lhlib" ]; then
                                continue 2
                        fi
                        test -f $lib -a ! -h $lib
                        if [ $? -eq 0 ]; then
                                #
                                # This is a real file. back it up and
                                # create the symlink.
                                #
                                mv $lib $lib.$DATESTR
				ln -s $lhlib $lib || return 1
                        else
                                #
                                # Either a symlink or does not exist.
                                #
                                rm -f $lib
				ln -s $lhlib $lib || return 1
                        fi
                MSGSTR=`gettext 'Linked %s to %s'`
                msg `printf "$MSGSTR\n" "$lib" "$lhlib"`
        done
        return 0

}

################################################################################
#
# fingerd
#
# Disable fingerd on this server (if it's disbaled on the sibling).
#
################################################################################
fingerd()
{


	INETDCONF=/etc/inetd.conf
	tmpinet=/var/opt/SUNWcluster/run/inetd.conf.$$

	cmd="grep \"^[ \t]*finger\" $INETDCONF"

		eval "$cmd" >/dev/null 2>&1
		case $? in
		0)
			# fingerd is enabled on this host
			nonmh_finger=1
			;;
		1)
			# fingerd is disabled on $host
			nonmh_finger=0
			;;
		*)
			errmsg "4011" `gettext "Couldn't access \"$INETDCONF\" on \"$host\""`
			return 1
			;;
		esac


		if [ $mh_finger -eq 1 -a $nonmh_finger -eq 0 ]; then
			#
			# mail host has fingerd enabled, and $host has it disabled.
			# Enable fingerd on $host.
			#
			grep \"$STARTTAG\" $INETDCONF >/dev/null 2>&1
			grepstat=$?
			if [ $grepstat -eq 2 ]; then
				errmsg "4011" `gettext "Couldn't access \"$INETDCONF\" on \"$host\""`
				return 1
			fi

			if [ $grepstat -eq 0 ]; then
				cmd2="cp $INETDCONF $tmpinet; sed -e \"/$STARTTAG/,/$ENDTAG/d\" \
					$tmpinet > $INETDCONF; rm -f $tmpinet"
				eval "$cmd2"
				if [ $? -ne 0 ]; then
					errmsg "4012" `gettext "Failed to edit \"$INETDCONF\" on \"$host\""`
					return 1
				fi
			fi

			eval "cat >> $INETDCONF" <<EOF
# $STARTTAG
finger	stream	tcp	nowait	nobody	/usr/sbin/in.fingerd	in.fingerd
# $ENDTAG
EOF
			if [ $? -ne 0 ]; then
				errmsg "4012" `gettext "Failed to edit \"$INETDCONF\" on \"$host\""`
				return 1
			fi

			hupit inetd || return 1
			msg `gettext "Enabled \"fingerd\" on \"$host\""`

		elif [ $mh_finger -eq 0 -a $nonmh_finger -eq 1 ]; then
			#
			# mail host has fingerd disbaled, and $host has it enabled.
			# Disbale it on $host.
			#
			cmd2="cp $INETDCONF $tmpinet; sed -e \"/$STARTTAG/,/$ENDTAG/d\" \
				-e \"/^[ \t]*finger/d\" $tmpinet > $INETDCONF; rm -f $tmpinet"
			eval "$cmd2"
			if [ $? -ne 0 ]; then
				errmsg "4012" `gettext "Failed to edit \"$INETDCONF\" on \"$host\""`
				return 1
			fi

			hupit inetd || return 1
			msg `gettext "Disabled \"fingerd\" on \"$host\""`

		fi
			
	return 0
}
################################################################################
# 
# createnscpmsg_nonmh
#
# create /etc/NscpMsg
#
################################################################################
createnscpmsg_nonmh()
{
	NSCPMSG=/etc/NscpMsg

	eval "rm /etc/NscpMsg
	      ln -s $SERVERROOT/bin/msg/admin/bin/NscpMsg $NSCPMSG" || return 1
	return 0
}

################################################################################
#
# mvsendmail
#
# Stop sendmail.
# Move the Solaris sendmail to a backup file and make a link to Netscape sendmail.
# Start the solaris sendmail in a queue drain mode. 
#
################################################################################
mvsendmail_nonmh() 
{
#set -x

	SENDMAIL=/usr/lib/sendmail
	MAILQ=/bin/mailq
	PROCESSQ=/usr/lib/processq

		/etc/init.d/sendmail stop >/dev/null 2>&1

		test -f $SENDMAIL -a ! -h $SENDMAIL
		if [ $? -eq 0 ]; then
			eval "mv $SENDMAIL ${SENDMAIL}.bk; \
				rm $MAILQ; \
				rm $PROCESSQ; \
				ln -s $SERVERROOT/bin/msg/admin/bin/sendmail $SENDMAIL; \
				ln -s $SENDMAIL $MAILQ; \
				ln -s $SENDMAIL $PROCESSQ" || return 1
		else
			eval "test -h $SENDMAIL && rm -f $SENDMAIL; \
				rm $MAILQ; \
				rm $PROCESSQ; \
				ln -s $SERVERROOT/bin/msg/admin/bin/sendmail $SENDMAIL; \
				ln -s $SENDMAIL $MAILQ; \
				ln -s $SENDMAIL $PROCESSQ" || return 1
		fi

		/etc/init.d/SUNWscnsm start || return 1

	msg `gettext "Linked $SENDMAIL to $SERVERROOT/bin/msg/admin/bin/sendmail"`
	msg `gettext "Linked $MAILQ to $SENDMAIL"`
	msg `gettext "Linked $PROCESSQ to $SENDMAIL"`
	msg `gettext "Started Solaris sendmail to drain private mail queue"`
	return 0
}

mvsendmail_nonmh_35() 
{
#set -x

        SENDMAIL=/usr/lib/sendmail

                /etc/init.d/sendmail stop >/dev/null 2>&1

                test -f $SENDMAIL -a ! -h $SENDMAIL
                if [ $? -eq 0 ]; then
                        eval "mv $SENDMAIL ${SENDMAIL}.bk; \
                                ln -s $ProgramDir/bin/sendmail $SENDMAIL" || return 1
                else
                        eval "test -h $SENDMAIL && rm -f $SENDMAIL; \
                                ln -s $ProgramDir/bin/sendmail $SENDMAIL" || return 1
                fi

                /etc/init.d/SUNWscnsm start || return 1

        msg `gettext "Linked $SENDMAIL to $ProgramDir/bin/sendmail"`
        msg `gettext "Started Solaris sendmail to drain private mail queue"`
        return 0
}

mvsendmail_mh_35()
{

	/etc/init.d/SUNWscnsm start || return 1

	msg `gettext "Linked $SENDMAIL to $ProgramDir/bin/sendmail"`
	msg `gettext "Started Solaris sendmail to drain private mail queue"`
	return 0
}

mvsendmail_mh()
{

	# stop the original sendmail process on the master host too, since
	# NMS 4.1 install fails to do so (bug in
	# $SERVERROOT/bin/msg/install/bin/msgsetup.sh)
	/etc/init.d/sendmail stop >/dev/null 2>&1

	/etc/init.d/SUNWscnsm start || return 1

	msg `gettext "Linked $SENDMAIL to $SERVERROOT/bin/msg/admin/bin/sendmail"`
	msg `gettext "Linked $MAILQ to $SENDMAIL"`
	msg `gettext "Linked $PROCESSQ to $SENDMAIL"`
	msg `gettext "Started Solaris sendmail to drain private mail queue"`
	return 0
}


################################################################################
#
# chgperm
#
# Chnage permissions and ownership on some files
#
################################################################################
chgperm_mh()
{
	# this is just a no-op for consistency w/ chgperm_mh_35
	return 0
}

chgperm_mh_35() 
{
	if [ $IS_SKEY -eq 1 ]; then
		chmod 750 $PostOffice/$HANSM
		chown $MTAUID $PostOffice/$HANSM
		chgrp $MTAGID $PostOffice/$HANSM

		chmod 664 $POSTSKEY
		chgrp $MTAGID $POSTSKEY
	fi

	msg `gettext "Changed permissions on a few files"`
	return 0
}

chgperm_nonmh()
{
	if [ $IS_SKEY -eq 1 ]; then
		chgrp -h $MTAGID $ETCSKEY
	fi

	export_conf_file

	chgrp $MTAGID /etc/nsserver.cfg
	chmod 444 /etc/nsserver.cfg

	chgrp -h $MTAGID /usr/lib/sendmail

	chmod g+s /var/mail

	msg `gettext "Changed permissions on a few files"`
	return 0
}

chgperm_nonmh_35()
{
        if [ $IS_SKEY -eq 1 ]; then
                chgrp -h $MTAGID $ETCSKEY
        fi

        chgrp $MTAGID $NSM_CONF
        chmod 444 $NSM_CONF

        chgrp -h $MTAGID /usr/lib/sendmail

        chmod g+s /var/mail

        msg `gettext "Changed permissions on a few files"`
        return 0
}

################################################################################
#
# Main
#
################################################################################
TRAPSIGNALS="1 2 3 15"
trap "cleanup 1" $TRAPSIGNALS
#set -x

PATH="/opt/SUNWcluster/bin:/usr/5bin:/usr/local/bin:\
/usr/bsd:/usr/ucb:$PATH"
#export PATH

#
# Check for root
#
uid=`${ID}` 
if [ $? -ne 0 ]; then 
	errmsg "4013" `gettext "Cannot get user id"` 
	cleanup 1 
fi       
uid=`set -- ${uid}; echo $1` 
if [ "${uid}" != "uid=0(root)" ]; then
	errmsg "4014" `gettext "This program must be executed by root"` 
	cleanup 1 
fi

	typeset keyword
	typeset localhost

	localhost=`uname -n`

	keyword=$1
	row=$2
	row=${row#*:}

setNSM_PROG
ISMS4=$(isnsms4)

if [[ ${ISMS4} = 1 ]]; then

	case ${keyword} in

	  CCDSYNC_POST_REMOVE)
	    ;;
	  CCDSYNC_PRE_REMOVE)
	    ;;
	  CCDSYNC_PRE_ADDU)
	    ;;
	  CCDSYNC_POST_ADDU | DO_SETUP)

	    . ds_utilities
	    if [ $? -ne 0 ]; then
		errmsg "4015" `gettext "Error while sourcing ds_utilities"`
		return 1
	    fi

	initvars || cleanup 1

	is_member "${MYHOST}" "${MASTER}"
	ret1=$?

	is_member "${MYHOST}" "${NONMASTERS}"
	ret2=$?

	if [[ "${ret1}" -ne 0 && "${ret2}" -ne 0 ]]; then
		cleanup 0
	fi

	export CLUSTERNAME

	confirm || cleanup 1

	# No need to run this on the master node
	if [[ "${MYHOST}" = "${MASTER}" ]]; then
		cpmailconf || cleanup 1
	else
		cpmailconf_nonmh || cleanup 1
	fi

	#checkcomm || cleanup 1
	vrfyids || cleanup 1
	stopnsmail || cleanup 1

	#POSTSKEY=$PostOffice/$HANSM/skeykeys
	#if [[ "${MYHOST}" = "${MASTER}" ]]; then
	#	skeys_mh || cleanup 1
	#else
	#	skeys_nonmh || cleanup 1
	#fi

	# No need to run this on the master node
	#if [[ "${MYHOST}" != "${MASTER}" ]]; then
	#    libs || cleanup 1
	#	fingerd || cleanup 1
	#fi

	if [[ "${MYHOST}" = "${MASTER}" ]]; then
	    mvsendmail_mh || cleanup 1
	#	chgperm_mh || cleanup 1
	else
	    mvsendmail_nonmh || cleanup 1
	    createnscpmsg_nonmh || cleanup 1
		chgperm_nonmh || cleanup 1
	fi

		;;
		*)
		;;
	esac

else

	case ${keyword} in

	  CCDSYNC_POST_REMOVE)
	    ;;
	  CCDSYNC_PRE_REMOVE)
	    ;;
	  CCDSYNC_PRE_ADDU)
	    ;;
	  CCDSYNC_POST_ADDU | DO_SETUP)

	    . ds_utilities
	    if [ $? -ne 0 ]; then
		errmsg "4015" `gettext "Error while sourcing ds_utilities"`
		return 1
	    fi

	initvars || cleanup 1

	is_member "${MYHOST}" "${MASTER}"
	ret1=$?

	is_member "${MYHOST}" "${NONMASTERS}"
	ret2=$?

	if [[ "${ret1}" -ne 0 && "${ret2}" -ne 0 ]]; then
		cleanup 0
	fi

	export CLUSTERNAME

	confirm || cleanup 1

	# No need to run this on the master node
	if [[ "${MYHOST}" = "${MASTER}" ]]; then
		cpmailconf_35 || cleanup 1
	else
		cpmailconf_nonmh_35 || cleanup 1
	fi

	#checkcomm || cleanup 1
	vrfyids_35 || cleanup 1
	stopnsmail_35 || cleanup 1

	POSTSKEY=$PostOffice/$HANSM/skeykeys
	if [[ "${MYHOST}" = "${MASTER}" ]]; then
		skeys_mh || cleanup 1
	else
		skeys_nonmh || cleanup 1
	fi

	# No need to run this on the master node
	if [[ "${MYHOST}" != "${MASTER}" ]]; then
	    libs || cleanup 1
		fingerd || cleanup 1
	fi

	if [[ "${MYHOST}" = "${MASTER}" ]]; then
	    mvsendmail_mh_35 || cleanup 1
		chgperm_mh_35 || cleanup 1
	else
	    mvsendmail_nonmh_35 || cleanup 1
		chgperm_nonmh_35 || cleanup 1
	fi

		;;
		*)
		;;
	esac

fi

echo "Completed successfully!"
cleanup 0
